HEX
Server: Apache
System: Linux server1.royalgt4.com 4.18.0-553.89.1.lve.el8.x86_64 #1 SMP Wed Dec 10 13:58:50 UTC 2025 x86_64
User: mostafedeg (1125)
PHP: 5.6.40
Disabled: mail,passthru,parse_ini_file,show_source,eval,assert,pcntl_exec,dl,putenv,proc_open,popen
Upload Files
File: /home/mostafedeg/public_html/erp/controllers/debtclientController.php
<?php
// تقرير لعميل معين
//تقارير العملاء (الدفعات)
//the global file operation
include("../public/impOpreation.php");

//global varable
global $showoutside;

//to check if the page from .htacess
//$showoutside = $_GET['sn'];
// get the config file
include_once("../public/config.php");

//here the db files that include in the file
//include("../public/include_dao.php");
//Client
require_once('../models/dao/ClientDAO.class.php');
require_once('../models/dto/Client.class.php');
require_once('../models/mysql/ClientMySqlDAO.class.php');
require_once('../models/mysql/ext/ClientMySqlExtDAO.class.php');
//Clientdebtchange
require_once('../models/dao/ClientdebtchangeDAO.class.php');
require_once('../models/dto/Clientdebtchange.class.php');
require_once('../models/mysql/ClientdebtchangeMySqlDAO.class.php');
require_once('../models/mysql/ext/ClientdebtchangeMySqlExtDAO.class.php');
//Sellbill
require_once('../models/dao/SellbillDAO.class.php');
require_once('../models/dto/Sellbill.class.php');
require_once('../models/mysql/SellbillMySqlDAO.class.php');
require_once('../models/mysql/ext/SellbillMySqlExtDAO.class.php');
//Sellbilldetail
require_once('../models/dao/SellbilldetailDAO.class.php');
require_once('../models/dto/Sellbilldetail.class.php');
require_once('../models/mysql/SellbilldetailMySqlDAO.class.php');
require_once('../models/mysql/ext/SellbilldetailMySqlExtDAO.class.php');
//Returnsellbill
require_once('../models/dao/ReturnsellbillDAO.class.php');
require_once('../models/dto/Returnsellbill.class.php');
require_once('../models/mysql/ReturnsellbillMySqlDAO.class.php');
require_once('../models/mysql/ext/ReturnsellbillMySqlExtDAO.class.php');
//Returnsellbilldetail
require_once('../models/dao/ReturnsellbilldetailDAO.class.php');
require_once('../models/dto/Returnsellbilldetail.class.php');
require_once('../models/mysql/ReturnsellbilldetailMySqlDAO.class.php');
require_once('../models/mysql/ext/ReturnsellbilldetailMySqlExtDAO.class.php');
//Sellbillandrutern
require_once('../models/dao/SellbillandruternDAO.class.php');
require_once('../models/dto/Sellbillandrutern.class.php');
require_once('../models/mysql/SellbillandruternMySqlDAO.class.php');
require_once('../models/mysql/ext/SellbillandruternMySqlExtDAO.class.php');
//Sellandruternbilldetail
require_once('../models/dao/SellandruternbilldetailDAO.class.php');
require_once('../models/dto/Sellandruternbilldetail.class.php');
require_once('../models/mysql/SellandruternbilldetailMySqlDAO.class.php');
require_once('../models/mysql/ext/SellandruternbilldetailMySqlExtDAO.class.php');

//Clientarea
require_once('../models/dao/ClientareaDAO.class.php');
require_once('../models/dto/Clientarea.class.php');
require_once('../models/mysql/ClientareaMySqlDAO.class.php');
require_once('../models/mysql/ext/ClientareaMySqlExtDAO.class.php');


//Government
require_once('../models/dao/GovernmentDAO.class.php');
require_once('../models/dto/Government.class.php');
require_once('../models/mysql/GovernmentMySqlDAO.class.php');
require_once('../models/mysql/ext/GovernmentMySqlExtDAO.class.php');

//GovernArea
require_once('../models/dao/GoverareaDAO.class.php');
require_once('../models/dto/Goverarea.class.php');
require_once('../models/mysql/GoverareaMySqlDAO.class.php');
require_once('../models/mysql/ext/GoverareaMySqlExtDAO.class.php');

//typeClient
require_once('../models/dao/TypeClientDAO.class.php');
require_once('../models/dto/TypeClient.class.php');
require_once('../models/mysql/TypeClientMySqlDAO.class.php');
require_once('../models/mysql/ext/TypeClientMySqlExtDAO.class.php');

require_once('../models/dao/YoutubeLinkDAO.class.php');
require_once('../models/dto/YoutubeLink.class.php');
require_once('../models/mysql/YoutubeLinkMySqlDAO.class.php');
require_once('../models/mysql/ext/YoutubeLinkMySqlExtDAO.class.php');

//Save
//get the do the action
$do = $_GET['do'];

/* ======================

  Controller Name :- clientReportsController

  OPERTATION in Controller

  1- تقرير مديونية عميل
  ======================== */

//here the global templates
$smarty->display("header.html");

//here goes the instances and general variables
$type = new TypeClient();
$TypeClientDAO = new TypeClientMySqlDAO();
$TypeClientEX = new TypeClientMySqlExtDAO();

//Client
$client = new Client();
$clientDAO = new ClientMySqlDAO();
$clientExt = new ClientMySqlExtDAO();
//Clientdebtchange
$clientDeptChange = new Clientdebtchange();
$clientDeptChangeDAO = new ClientdebtchangeMySqlDAO();
$clientDeptChangeExt = new ClientdebtchangeMySqlExtDAO();
//Sellbill
$sellBill = new Sellbill();
$sellBillDAO = new SellbillMySqlDAO();
$sellBillExt = new SellbillMySqlExtDAO();

//Returnsellbill
$returnSellBill = new Returnsellbill();
$returnSellBillDAO = new ReturnsellbillMySqlDAO();
$returnSellBillExt = new ReturnsellbillMySqlExtDAO();
//Sellbillandrutern
$mySellbillandrutern = new Sellbillandrutern();
$mySellbillandruternRecord = new SellbillandruternMySqlDAO();
$mySellbillandruternEx = new SellbillandruternMySqlExtDAO();
//Sellbilldetail
$mySellbilldetail = new Sellbilldetail();
$mySellbilldetailRecord = new SellbilldetailMySqlDAO();
$mySellbilldetailEx = new SellbilldetailMySqlExtDAO();
//Returnsellbilldetail
$myReturnsellbilldetail = new Returnsellbilldetail();
$myReturnsellbilldetailRecord = new ReturnsellbilldetailMySqlDAO();
$myReturnsellbilldetailEx = new ReturnsellbilldetailMySqlExtDAO();
//Sellandruternbilldetail
$mySellandruternbilldetail = new Sellandruternbilldetail();
$mySellandruternbilldetailRecord = new SellandruternbilldetailMySqlDAO();
$mySellandruternbilldetailEx = new SellandruternbilldetailMySqlExtDAO();


//Clientarea
$Clientarea = new Clientarea();
$ClientareaDAO = new ClientareaMySqlDAO();
$ClientareaEX = new ClientareaMySqlExtDAO();

//bill
$bills = new Bill();
$billsDAO = new BillsMySqlDAO();
$billsEX = new BillsMySqlExtDAO();


// Government
$government = new Government();
$governmentDAO = new GovernmentMySqlDAO();
$governmentExt = new GovernmentMySqlExtDAO();

// GovernArea
$governArea = new Goverarea();
$governAreaDAO = new GoverareaMySqlDAO();
$governAreaExt = new GoverareaMySqlExtDAO();

//billsReturn
$billsReturn = new Billsreturn();
$billsReturnDAO = new BillsreturnMySqlDAO();
$billsReturnEX = new BillsreturnMySqlExtDAO();

$youtubeLink = new YoutubeLink();
$youtubeLinkDAO = new YoutubeLinkMySqlDAO();
$youtubeLinkEX = new YoutubeLinkMySqlExtDAO();

//check and use the condition that suite this action



if (empty($do)) {

    include_once("../public/authentication.php");

    $TypeClient = $TypeClientDAO->queryAll();
    $smarty->assign("TypeClient", $TypeClient);

    $youtubes = $youtubeLinkDAO->queryAll();
    $smarty->assign("youtubes", $youtubes);
    //search
    $datefrom = filter_input(INPUT_POST, 'datefrom');
    $dateto = filter_input(INPUT_POST, 'dateto');


    $queryString1 = " where 1 and sellbill.conditions = 0 "; //sellbill
    $queryString1R = " where 1 and returnsellbill.conditions = 0 "; //sellbillreturn
    $queryString1SR = " where 1 and sellbillandrutern.conditions = 0 "; //sellbillandreturn
    ##date
    if (isset($datefrom) && !empty($datefrom)) {
        if (isset($Programsetting->reportsPlusHours) && !empty($Programsetting->reportsPlusHours)) {
            $reportsPlusHours = $Programsetting->reportsPlusHours + 24; //24 to get the end of the day and add search hours to it
            $datefrom = date('Y-m-d ', strtotime('+' . $Programsetting->reportsPlusHours . ' hour +0 minutes', strtotime($datefrom)));
        } else {
            $datefrom = $datefrom . " 00:00:00";
        }

        $queryString1 .= ' and sellbill.sellbilldate  >= "' . $datefrom . '" ';
        $queryString1R .= ' and returnsellbill.returnsellbilldate  >= "' . $datefrom . '" ';
        $queryString1SR .= ' and sellbillandrutern.sellbilldate  >= "' . $datefrom . '" ';
    }
    if (isset($dateto) && !empty($dateto)) {
        if (isset($Programsetting->reportsPlusHours) && !empty($Programsetting->reportsPlusHours)) {
            $reportsPlusHours = $Programsetting->reportsPlusHours + 24; //24 to get the end of the day and add search hours to it
            $dateto = date('Y-m-d ', strtotime('+' . $reportsPlusHours . ' hour +0 minutes', strtotime($dateto)));
        } else {
            $dateto = $dateto . ' 23:59:59';
        }

        $queryString1 .= ' and sellbill.sellbilldate  <= "' . $dateto . '" ';
        $queryString1R .= ' and returnsellbill.returnsellbilldate  <= "' . $dateto . '" ';
        $queryString1SR .= ' and sellbillandrutern.sellbilldate  <= "' . $dateto . '" ';
    }

    if (!empty($datefrom) && !empty($dateto)) {
        $datefrom = $datefrom . ' 00:00:00';
        $dateto = $dateto . ' 23:59:59';
    }

    $smarty->display("debtclient/show.html");
} elseif ($do == "show") { // تقرير لعميل معين
    include_once("../public/authentication.php");
    class clientData {

        public $clientid;
        public $clientname;
        public $totalsell = 0;
        public $totalreturn = 0;
        public $totaldiscount = 0;
        public $clientdebt = 0;
        public $clientareaName ;
        public $clientgovernmentname ;

    }

    $existId = array();
    $sellbillDataArr = array();

//    $existData = 'no';
    $TypeClient = $TypeClientDAO->queryAll();
    $smarty->assign("TypeClient", $TypeClient);


    global $queryString;

    $youtubes = $youtubeLinkDAO->queryAll();
    $smarty->assign("youtubes", $youtubes);


    $TypeClient = $_POST['TypeClient'];
    $showDebitOnly = $_POST['showDebit'];


    if (isset($TypeClient) && $TypeClient != '-1' ) {
        $queryStringTy = 'where client.typeclientid like "%' . $TypeClient . '%" ';
    }elseif($TypeClient == "-20"){
        $queryStringTy ='where client.typeclientid like "% -20 %" ';
    }

    if (isset($showDebitOnly)) {
        $queryStringTy .= 'AND client.clientdebt != 0';
    }
//search
    $datefrom = filter_input(INPUT_POST, 'datefrom');
    $dateto = filter_input(INPUT_POST, 'dateto');


    $queryString1 = " and sellbill.conditions = 0 "; //sellbill
    $queryString1R = " and returnsellbill.conditions = 0 "; //sellbillreturn
    $queryString1SR = " and sellbillandrutern.conditions = 0 "; //sellbillandreturn
    ##date
    if (isset($datefrom) && !empty($datefrom)) {
        if (isset($Programsetting->reportsPlusHours) && !empty($Programsetting->reportsPlusHours)) {
            $reportsPlusHours = $Programsetting->reportsPlusHours + 24; //24 to get the end of the day and add search hours to it
            $datefrom = date('Y-m-d ', strtotime('+' . $Programsetting->reportsPlusHours . ' hour +0 minutes', strtotime($datefrom)));
        } else {
            $datefrom = $datefrom . " 00:00:00";
        }

        $queryString1 .= ' and sellbill.sellbilldate  >= "' . $datefrom . '" ';
        $queryString1R .= ' and returnsellbill.returnsellbilldate  >= "' . $datefrom . '" ';
        $queryString1SR .= ' and sellbillandrutern.sellbilldate  >= "' . $datefrom . '" ';
    }
    if (isset($dateto) && !empty($dateto)) {
        if (isset($Programsetting->reportsPlusHours) && !empty($Programsetting->reportsPlusHours)) {
            $reportsPlusHours = $Programsetting->reportsPlusHours + 24; //24 to get the end of the day and add search hours to it
            $dateto = date('Y-m-d ', strtotime('+' . $reportsPlusHours . ' hour +0 minutes', strtotime($dateto)));
        } else {
            $dateto = $dateto . ' 23:59:59';
        }

        $queryString1 .= ' and sellbill.sellbilldate  <= "' . $dateto . '" ';
        $queryString1R .= ' and returnsellbill.returnsellbilldate  <= "' . $dateto . '" ';
        $queryString1SR .= ' and sellbillandrutern.sellbilldate  <= "' . $dateto . '" ';
    }

    if (!empty($datefrom) && !empty($dateto)) {
        $datefrom = $datefrom . ' 00:00:00';
        $dateto = $dateto . ' 23:59:59';
    }


//    $alldata = $clientExt->clientdeptbytypeclient($queryStringTy);
//    foreach ($alldata as $item){
//        if (empty($item->governmentname) || empty($item->areaName)){
//            $item->governmentname = "لايوجد";
//            $item->areaName = "لايوجد";
//        }
//        $totalallClient +=$item->clientdebt;
//
//    }

    $sellBillData = $sellBillExt->ClientsWithSellbillsSells2($queryStringTy,$queryString1);
    $sellBillDataReturn = $returnSellBillExt->ClientsWithReturnSellbillsSells($queryStringTy,$queryString1R);
    $sellBillDataSellAndReturn = $mySellbillandruternEx->ClientsWithSellAndReturnSellbillsSells($queryStringTy,$queryString1SR);
    $resultsCount = count($sellBillData) + count($sellBillDataReturn) + count($sellBillDataSellAndReturn);
    $smarty->assign("resultsCount", $resultsCount);

    ##فاتورة المبيعات
    foreach ($sellBillData as $key => $value) {
        if (in_array($value->sellbillclientid, $existId)) {
            $key = array_search($value->sellbillclientid, $existId);
            $myclient = $sellbillDataArr[$key];
        } else {
            $key = -1;
            $myclient = new clientData();
            $myclient->clientid = $value->sellbillclientid;
            array_push($existId, $value->sellbillclientid);
        }

        $myclient->clientname = $value->clientname;
        $myclient->clientareaName = $value->areaName;
        $myclient->clientgovernmentname  = $value->governmentname ;
        $myclient->totalsell = $value->allGenTotal;
        $myclient->sellbillaftertotalbill += $value->sellbillaftertotalbill;
        $myclient->clientdebt = $value->clientdebt;
        $myclient->selldis += $value->allGenTotal - $value->sellbillaftertotalbill;

        if ($key == -1 && (int) $myclient->clientid > 0) {
            array_push($sellbillDataArr, $myclient);
        }
        //        var_dump($myclient->selldis);
    }
    ##فاتورة  مردودات المبيعات
    foreach ($sellBillDataReturn as $key => $value) {
        //        var_dump($value);
        if (in_array($value->returnsellbillclientid, $existId)) {
            $key = array_search($value->returnsellbillclientid, $existId);
            $myclient = $sellbillDataArr[$key];
        } else {
            $key = -1;
            $myclient = new clientData();
            $myclient->clientid = $value->returnsellbillclientid;
            array_push($existId, $value->returnsellbillclientid);
        }

        $myclient->clientname = $value->clientname;
        $myclient->clientareaName = $value->areaName;
        $myclient->clientgovernmentname  = $value->governmentname ;
        $myclient->totalreturn += $value->allGenTotalReturn;
        $myclient->returnsellbillaftertotalbill += $value->returnsellbillaftertotalbill;
        $myclient->returnselldis += $value->allGenTotalReturn - $value->returnsellbillaftertotalbill;


        if ($key == -1 && (int) $myclient->clientid > 0) {
            array_push($sellbillDataArr, $myclient);
        }
        //        var_dump($myclient->returnselldis);
    }

//    ##فاتورة  مبيعات ومرتجعات
    foreach ($sellBillDataSellAndReturn as $key => $value) {
        if (in_array($value->sellbillclientid, $existId)) {
            $key = array_search($value->sellbillclientid, $existId);
            $myclient = $sellbillDataArr[$key];
        } else {
            $key = -1;
            $myclient = new clientData();
            $myclient->clientid = $value->sellbillclientid;
            array_push($existId, $value->sellbillclientid);
        }

        $myclient->clientname = $value->clientname;
        $myclient->clientareaName = $value->areaName;
        $myclient->clientgovernmentname  = $value->governmentname ;
        if ($value->allGenTotalSellAndReturn >= 0) {
            $myclient->totalsell += $value->allGenTotalSellAndReturn;
        } else {
            $myclient->totalreturn += $value->allGenTotalSellAndReturn;
        }
        $myclient->sellbillaftertotalbill += $value->sellbillaftertotalbill;
        $myclient->sellandreturndis += $value->allGenTotalSellAndReturn - $value->sellbillaftertotalbill;


        if ($key == -1 && (int) $myclient->clientid > 0) {
            array_push($sellbillDataArr, $myclient);
        }
        //        var_dump($myclient->sellandreturndis);
    }


    ##صاقى المبيعات ,واجمالى الخصومات
    //    $t_bure = 0;
    $t_discount = 0;
    foreach ($sellbillDataArr as $t_data) {
//                var_dump($t_data);
        $t_data->t_bure = $t_data->sellbillaftertotalbill - $t_data->returnsellbillaftertotalbill;
        $t_data->t_dis = $t_data->selldis - $t_data->returnselldis + $t_data->sellandreturndis;
        $t_discount += $t_data->t_dis;
    }

    foreach ($sellbillDataArr as $dataa) {
        $clientid = $dataa->clientid;
        $totalPrice = showAllOperations($clientid, $datefrom, $dateto);
        $dataa->totalPrice = $totalPrice;
    }


    $smarty->assign("sellbillDataArr", $sellbillDataArr);
    $smarty->assign("t_discount", $t_discount);
    $smarty->assign("resultsCount", $resultsCount);
    $smarty->display("debtclient/show.html");

}


$smarty->assign("customClientArea", 1);

//here the global templates
$smarty->display("footer.html");

function showAllOperations($clientid, $datefrom, $dateto) {
    global $clientDeptChangeExt;
    global $sellBillDAO;
    global $returnSellBillDAO;
    global $smarty;
    global $mySellbillandruternRecord;
    //فواتير المبيعات
    //from sellbill table and sellbillandreturn table
    $_SESSION['stop_checking_result'] = 1;
    $sellbillData = showSellBillsByClientAndDate($clientid, $datefrom, $dateto);
    //فواتير مردودات المبيعات
    //from returnsellbill table and sellbillandreturn table
    $returnsellbillData = showReturnSellBillsByClientAndDate($clientid, $datefrom, $dateto);



    //get from clientdeptchange table
    if (!empty($datefrom) && !empty($dateto) && $datefrom != "" && $dateto != "") {
        $cliDate = '  DATE(clientdebtchange.clientdebtchangedate) >=  "' . $datefrom . '"  AND DATE(clientdebtchange.clientdebtchangedate) <= "' . $dateto . '" AND ';
    }
    $clientShowData = $clientDeptChangeExt->queryByClientIdAndDate3($clientid, $cliDate);
    if (count($clientShowData) > 0) {
        //        print_r($clientShowData);

        foreach ($clientShowData as $c) {//when the payment is in the bill must select from the sel bill
            //            print_r('<pre>');
            //            print_r($c);
            //            print_r('<pre>');
            if ($c->tablename == "sellbillController.php") {
                $alldata = $sellBillDAO->load($c->clientdebtchangemodelid);

                $c->clientdebtchangeamount = $alldata->sellbilltotalpayed;
                $totalPrice += $alldata->sellbilltotalpayed;
            } else {

                $clientdebtchangetype = $c->clientdebtchangetype;
                //print_r($clientdebtchangetype.'<br>');
                if ($c->tablename != 'clientController.php') {
                    if ($clientdebtchangetype == 0) {
                        //$totalPrice += $c->sellbilltotalpayed;
                        $totalPrice += $c->clientdebtchangeamount;
                    } else {
                        $totalPrice -= $c->clientdebtchangeamount;
                        //$totalPrice -= $c->sellbilltotalpayed;
                    }
                } else {
                    $clientControllerVal = $c->clientdebtchangeamount;
                }

                //$totalPrice += $c->clientdebtchangeamount;
            }

            //print_r('<br>$totalPrice = ' . $totalPrice . '  process name =  ' . $c->processname);
        }

        //print_r($clientShowData);
    }
    //    var_dump($totalPrice);
    //# جمع دفعة فى فاتورة مبيعات على اجمالى الدفعات
    foreach ($sellbillData as $data) {
        if ($data->sellbilltotalpayed != 0 && $data->conditions == 0) {
            $totalPrice += $data->sellbilltotalpayed;
        }
    }

    //# جمع دفعة فى فاتورة المبيعات والمرتجعات على اجمالى الدفعات
    foreach ($returnsellbillData[1] as $data) {
        if ($data->sellbilltotalpayed != 0 && $data->conditions == 0) {
            $totalPrice += $data->sellbilltotalpayed;
        }
    }

    //print_r('$clientControllerVal = ' . $clientControllerVal);
    // assign db results to the template
    unset($_SESSION['stop_checking_result']);
    return $totalPrice;
}
function showSellBillsByClientAndDate($clientid, $datefrom, $dateto) {
    global $sellBillExt;
    global $mySellbillandruternEx;
    global $mySellbilldetailEx;
    global $mySellandruternbilldetailEx;
    global $smarty;
    $bure = $_POST['bure'];

    $totalBills = 0;
    $totalQTY = 0;

    if (!empty($datefrom) && !empty($dateto) && $datefrom != "" && $dateto != "") {
        $sellBillDate = ' sellbill.sellbilldate  >= "' . $datefrom . '" and sellbill.sellbilldate <= "' . $dateto . '" AND ';
        $sellBillReData = 'sellbillandrutern.sellbilldate  >= "' . $datefrom . '" and sellbillandrutern.sellbilldate <= "' . $dateto . '" and ';
    }
    if ($bure == 1) {
        $sellbillData = $sellBillExt->queryWithClientIdAndDate2($clientid, $sellBillDate);
    } else {
        $sellbillData = $sellBillExt->queryWithClientIdAndDateExt($clientid, $sellBillDate);
    }
    //get products number in sell bills
    if (count($sellbillData) > 0) {
        foreach ($sellbillData as $bill) {
            $totalQty1 = $mySellbilldetailEx->countProductInSellBill($bill->sellbillid);
            $bill->sellQuantity = $totalQty1;
            if ($bill->conditions == 0) {
                $totalQTY += $totalQty1;
                $totalBills += $bill->sellbillaftertotalbill;
            } /* else {
              $totalQTY -= $totalQty1;
              $totalBills -= $bill->sellbillaftertotalbill;
              } */
        }
    }


    //get sell bills from sellbillandrutern table
    if ($bure == 1) {
        $sellbillandruternData = $mySellbillandruternEx->querySellBillsOnlyWithClientIdAndDate2($clientid, $sellBillReData);
    } else {
        $sellbillandruternData = $mySellbillandruternEx->querySellBillsOnlyWithClientIdAndDateExt($clientid, $sellBillReData);
    }
    //get products number in sellbillandrutern
    if (count($sellbillandruternData) > 0) {
        foreach ($sellbillandruternData as $bill) {

            $bill->userid = -1;
            $sellbilldiscounttype = $bill->sellbilldiscounttype;
            $sellbilldiscount = $bill->sellbilldiscount;
            $sellbillprice;
            if ($sellbilldiscounttype == 1) {
                $sellbillprice = ($bill->sellbillprice) - ($bill->sellbilldiscount);
            } else {

                $sellbillprice = ($bill->sellbillprice) - ((($bill->sellbillprice) * ($bill->sellbilldiscount)) / 100);
            }
            $bill->sellbillprice = $sellbillprice;
            //print_r($bill);
            if ($bure == 1) {
                $totalQty2 = $mySellandruternbilldetailEx->countProductInSellBill($bill->sellbillid);
            } else {
                $totalQty2 = $mySellandruternbilldetailEx->countProductInSellBillExt($bill->sellbillid);
            }
            $bill->sellQuantity = $totalQty2;

            if ($bill->conditions == 0) {
                $totalQTY += $totalQty2;
                $totalBills += $sellbillprice; //(($bill->sellbillprice)-($bill->sellbilldiscount));
            } /* else {
              $totalQTY -= $totalQty2;
              $totalBills -= $sellbillprice; //(($bill->sellbillprice)-($bill->sellbilldiscount));
              } */
        }
    }

    $sellbillData = array_merge($sellbillData, $sellbillandruternData);

    //print_r($sellbillData);
    $smarty->assign("selltotalQTY", $totalQTY);
    $smarty->assign("selltotalBills", $totalBills);
    return $sellbillData;
}
function showReturnSellBillsByClientAndDate($clientid, $datefrom, $dateto) {
    global $returnSellBillExt;
    global $mySellbillandruternEx;
    global $myReturnsellbilldetailEx;
    global $mySellandruternbilldetailEx;
    global $smarty;



    $totalBills = 0;
    $totalQTY = 0;

    $bure = $_POST['bure'];

    if (!empty($datefrom) && !empty($dateto) && $datefrom != "" && $dateto != "") {
        $returnSellBillDate = ' returnsellbill.returnsellbilldate  >= "' . $datefrom . '" and returnsellbill.returnsellbilldate <= "' . $dateto . '" AND ';
        $selbillandruternReData = '  sellbillandrutern.sellbilldate  >= "' . $datefrom . '" and sellbillandrutern.sellbilldate <= "' . $dateto . '" and ';
    }
    if ($bure == 1) {
        $returnsellbillData = $returnSellBillExt->queryWithClinetIdAndDate2($clientid, $returnSellBillDate);
    } else {
        $returnsellbillData = $returnSellBillExt->queryWithClinetIdAndDateExt($clientid, $returnSellBillDate);
    }
    //get products number in returnsellbill
    if (count($returnsellbillData) > 0) {
        foreach ($returnsellbillData as $bill) {
            $totalQty1 = $myReturnsellbilldetailEx->countProductInSellBill($bill->returnsellbillid);
            $bill->returnsellQuantity = $totalQty1;
            if ($bill->conditions == 0) {
                $totalQTY += $totalQty1;
                $totalBills += $bill->returnsellbillaftertotalbill;
            }
            /* else {
              $totalQTY -= $totalQty1;
              $totalBills -= $bill->returnsellbillaftertotalbill;
              } */
        }
    }


    //get return sell bills from sellbillandrutern table
    //$sellbillandruternData =$mySellbillandruternEx->querySellBillsOnlyWithClientIdAndDate($clientid, $startDate,$endDate);
    if ($bure == 1) {
        $sellbillandruternData = $mySellbillandruternEx->queryReturnSellBillsOnlyWithClientIdAndDate2($clientid, $selbillandruternReData);
    } else {
        $sellbillandruternData = $mySellbillandruternEx->queryReturnSellBillsOnlyWithClientIdAndDateExt($clientid, $selbillandruternReData);
    }

    //print_r($sellbillandruternData);
    if (count($sellbillandruternData) > 0) {
        foreach ($sellbillandruternData as $sell) {

            $sell->userid = -1;

            $returnsellbillprice = ($sell->returnsellbillprice);


            /////////////////////
            if ($sell->conditions == 0) {
                $totalBills += $returnsellbillprice; //$sell->sellbillaftertotalbill;
            }
            /* else {
              $totalBills -= $returnsellbillprice;
              } */

            $sell->returnsellbillid = $sell->sellbillid;
            $sell->returnsellbillserial = $sell->sellbillserial;
            $sell->returnsellbillaftertotalbill = $sell->sellbillaftertotalbill;
            $sell->returnsellbilldate = $sell->sellbilldate;
            $totalQty2 = $mySellandruternbilldetailEx->countProductInReturnSellBill($sell->sellbillid);
            $sell->sellQuantity = $totalQty2;
            if ($sell->conditions == 0) {
                $totalQTY += $totalQty2;
            }
        }
    }
    $returnsellbillData = array_merge($returnsellbillData, $sellbillandruternData);
    $returnsellbill = array($returnsellbillData, $sellbillandruternData);

    $smarty->assign("returntotalQTY", $totalQTY);
    $smarty->assign("returntotalBills", $totalBills);

    return $returnsellbill;
}

?>