<?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");
//Supplier
require_once('../models/dao/SupplierDAO.class.php');
require_once('../models/dto/Supplier.class.php');
require_once('../models/mysql/SupplierMySqlDAO.class.php');
require_once('../models/mysql/ext/SupplierMySqlExtDAO.class.php');
//Supplierdebtchange
require_once('../models/dao/SupplierdebtchangeDAO.class.php');
require_once('../models/dto/Supplierdebtchange.class.php');
require_once('../models/mysql/SupplierdebtchangeMySqlDAO.class.php');
require_once('../models/mysql/ext/SupplierdebtchangeMySqlExtDAO.class.php');
//Buybill
require_once('../models/dao/BuybillDAO.class.php');
require_once('../models/dto/Buybill.class.php');
require_once('../models/mysql/BuybillMySqlDAO.class.php');
require_once('../models/mysql/ext/BuybillMySqlExtDAO.class.php');
//Buybilldetail
require_once('../models/dao/BuybilldetailDAO.class.php');
require_once('../models/dto/Buybilldetail.class.php');
require_once('../models/mysql/BuybilldetailMySqlDAO.class.php');
require_once('../models/mysql/ext/BuybilldetailMySqlExtDAO.class.php');
//Returnbuybill
require_once('../models/dao/ReturnbuybillDAO.class.php');
require_once('../models/dto/Returnbuybill.class.php');
require_once('../models/mysql/ReturnbuybillMySqlDAO.class.php');
require_once('../models/mysql/ext/ReturnbuybillMySqlExtDAO.class.php');
//Returnbuybilldetail
require_once('../models/dao/ReturnbuybilldetailDAO.class.php');
require_once('../models/dto/Returnbuybilldetail.class.php');
require_once('../models/mysql/ReturnbuybilldetailMySqlDAO.class.php');
require_once('../models/mysql/ext/ReturnbuybilldetailMySqlExtDAO.class.php');
//Buyandruternbill
require_once('../models/dao/BuyandruternbillDAO.class.php');
require_once('../models/dto/Buyandruternbill.class.php');
require_once('../models/mysql/BuyandruternbillMySqlDAO.class.php');
require_once('../models/mysql/ext/BuyandruternbillMySqlExtDAO.class.php');
//Buyandruternbilldetail
require_once('../models/dao/BuyandruternbilldetailDAO.class.php');
require_once('../models/dto/Buyandruternbilldetail.class.php');
require_once('../models/mysql/BuyandruternbilldetailMySqlDAO.class.php');
require_once('../models/mysql/ext/BuyandruternbilldetailMySqlExtDAO.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');

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

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

  Controller Name :- supplierReportsController

  OPERTATION in Controller

  1- عرض تقرير دفعات
  2-  تقرير لمورد معين

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

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

//here goes the instances and general variables
//Supplier
$supplier = new Supplier();
$supplierDAO = new SupplierMySqlDAO();
$supplierExt = new SupplierMySqlExtDAO();

//Supplierdebtchange
$supplierDeptChange = new Supplierdebtchange();
$supplierDeptChangeDAO = new SupplierdebtchangeMySqlDAO();
$supplierDeptChangeExt = new SupplierdebtchangeMySqlExtDAO();

//Buybill
$buyBill = new Buybill();
$buyBillDAO = new BuybillMySqlDAO();

//$buyBillDAO->load();
$buyBillExt = new BuybillMySqlExtDAO();

//Buybilldetail
$buyBillDetail = new Buybilldetail();
$buyBillDetailDAO = new BuybilldetailMySqlDAO();
$buyBillDetailExt = new BuybilldetailMySqlExtDAO();

//Returnbuybill
$returnBuyBill = new Returnbuybill();
$returnBuyBillDAO = new ReturnbuybillMySqlDAO();
$returnBuyBillExt = new ReturnbuybillMySqlExtDAO();

//Returnbuybilldetail
$returnBuyBillDetail = new Returnbuybilldetail();
$returnBuyBillDetailDAO = new ReturnbuybilldetailMySqlDAO();
$returnBuyBillDetailExt = new ReturnbuybilldetailMySqlExtDAO();

//Buyandruternbill
$buyAndReturnBill = new Buyandruternbill();
$buyAndReturnBillDAO = new BuyandruternbillMySqlDAO();
$buyAndReturnBillExt = new BuyandruternbillMySqlExtDAO();

//Buyandruternbilldetail
$buyAndReturnBillDetail = new Buyandruternbilldetail();
$buyAndReturnBillDetailDAO = new BuyandruternbilldetailMySqlDAO();
$buyAndReturnBillDetailExt = new BuyandruternbilldetailMySqlExtDAO();

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

$today = date("Y-m-d");

//check and use the condition that suite this action
if (empty($do) || $do == "supplierShow2") { // تقرير دفعات
    //here the permssion check
    include_once("../public/authentication.php");

    $supplierData = getSupplierData();
    $smarty->assign('supplierData', $supplierData);

    $startDate = $_REQUEST['from'];
    $endDate = $_REQUEST['to'];
    $supplierId = $_POST['supplierId'];

    if ((isset($supplierId) && $supplierId != "-1")) {
        $messageData = $supplierDAO->load($supplierId);
        $message = " اسم المورد : " . $messageData->suppliername . "التاريخ: من: " . $startDate . " إلى: " . $endDate;
        $smarty->assign("message", $message);
        $smarty->assign("supplierDebt", $messageData->suppliercurrentDebt);

        supplierShow2($supplierId, $startDate, $endDate);
    } else {
        //showall();
        // show($supplierId,$opertype,$today,$today);
    }

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

    $smarty->assign("customSupplierReports", 1);
    $smarty->display("supplierReportsview/supplierShow2.html");
}











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

/* ===============================
  function in this CONTROLLER
  ================================ */

function getSupplierData()
{
    global $supplierDAO;
    global $supplierExt;

    $alldata = $supplierExt->queryByCondition(0);
    // $alldata = $supplierDAO->queryAll();
    return $alldata;
}

function supplierShow2($supplierid, $startDate, $endDate)
{
    global $supplierDAO;
    global $supplierDeptChangeExt;

    global $buyAndReturnBillDAO;
    global $buyAndReturnBillExt;
    global $buyAndReturnBillDetailExt;

    global $returnBuyBillDAO;
    global $returnBuyBillExt;

    global $buyBillDAO;
    global $buyBillExt;


    global $smarty;

    $queryString = ' WHERE ';

    $message = 'تقرير كشف حساب مورد';
    if (isset($supplierid) && $supplierid != '-1') {
        $suppData = $supplierDAO->load($supplierid);
        $message .= " اسم المورد " . $suppData->suppliername;
        $smarty->assign('message', $message);


        $queryString .= ' supplier.supplierid =' . $supplierid . ' AND';
    }

    if (!empty($startDate) && !empty($endDate)) {

        $message .= " من تاريخ  " . $startDate . "  الى تاريخ" . $endDate;
        $smarty->assign('message', $message);

        $queryString .= ' supplierdebtchange.supplierdebtchangedate >= "' . $startDate . ' 00:00:00" AND supplierdebtchange.supplierdebtchangedate <= "' . $endDate . ' 23:59:59" AND';
    }


    $queryString .= ' supplierdebtchange.del = 0 AND';



    $arr = explode(' ', $queryString);

    if (isset($arr) && count($arr) > 0) {
        $lastWord = end($arr);
        if ($lastWord == 'AND') { //remove it
            array_pop($arr);
            //form the string again
            $queryString = implode(' ', $arr);
        } else if ($lastWord == 'WHERE') { //remove it
            array_pop($arr);
            $queryString = ' ';
        }
        //print("<br>queryString: ".$queryString."<br>");
    }

    $shownData = $supplierDeptChangeExt->queryBySupplierIdAndDatenew($queryString);



    foreach ($shownData as $data) {


        if ($data->tablename == "buyBillController.php") {
            $data->link = "buyBillController.php?do=editprint&id=" . $data->supplierdebtchangemodelid;

            $buybilldata = $buyBillExt->loadExt($data->supplierdebtchangemodelid);
            $data->totalbill = $buybilldata->buybilltotalbill;
            $data->finalbill = $buybilldata->buybillaftertotalbill;
            $data->discount = ($buybilldata->buybilltotalbill - $buybilldata->buybillaftertotalbill);

            $data->totalOperationCost = $buybilldata->buybilltotalbill + $data->supplierdebtchangebefore;
            $data->discards = 0;
            $data->payedamount = $buybilldata->buybilltotalpayed;
            $data->billComment = $buybilldata->comment;
        } elseif ($data->tablename == "returnBuyBillController.php") {

            $data->link = "returnBuyBillController.php?do=editprint&id=" . $data->supplierdebtchangemodelid;

            $buybilldata = $returnBuyBillExt->loadExt($data->supplierdebtchangemodelid);
            $data->totalbill = 0; //$buybilldata->returnbuybilltotalbill;
            $data->finalbill = $buybilldata->returnbuybillaftertotalbill;
            $data->discount = ($buybilldata->returnbuybilltotalbill - $buybilldata->returnbuybillaftertotalbill);

            $data->totalOperationCost = 0 + $data->supplierdebtchangebefore;
            $data->discards = $buybilldata->returnbuybilltotalbill;
            $data->payedamount = $buybilldata->returnbuybilltotalpayed;
            $data->billComment = $buybilldata->comment;
        } elseif ($data->tablename == "buyAndReturnBillController.php") {
            $data->link = "buyAndReturnBillController.php?do=editprint&id=" . $data->supplierdebtchangemodelid;

            $buybilldata = $buyAndReturnBillExt->loadExt($data->supplierdebtchangemodelid);
            $data->totalbill = $buyAndReturnBillDetailExt->buytotalpriceBillsOnly($data->supplierdebtchangemodelid, 0);
            $data->finalbill = $buybilldata->buybillaftertotalbill;
            $data->discount = ($buybilldata->buybilltotalbill - $buybilldata->buybillaftertotalbill);

            $data->totalOperationCost = $buybilldata->buybilltotalbill + $data->supplierdebtchangebefore;
            $data->discards = $buyAndReturnBillDetailExt->buytotalpriceBillsOnly($data->supplierdebtchangemodelid, 1);
            $data->payedamount = $buybilldata->buybilltotalpayed;
            $data->billComment = $buybilldata->comment;
        } elseif ($data->tablename == "kempialaController.php") {
            $data->link = "kempialaController.php?do=editprint&id=" . $data->supplierdebtchangemodelid;
            $data->discards = 0;
            $data->totalbill = 0;
            $data->totalOperationCost = $data->supplierdebtchangebefore;
            $data->discount = 0;
            $data->payedamount = $data->supplierdebtchangeamount;
        } elseif ($data->tablename == "checkwithdrawalController.php") {
            $data->link = "checkwithdrawalController.php?do=editprint&id=" . $data->supplierdebtchangemodelid;

            $data->discards = 0;

            $data->totalbill = 0;
            $data->totalOperationCost = $data->supplierdebtchangebefore;
            $data->discount = 0;
            $data->payedamount = $data->supplierdebtchangeamount;
        } else if ($data->tablename == "supplierPayedDeptController.php") {
            $deletdRows = $supplierDeptChangeExt->queryByComment($data->supplierdebtchangeid);
            if (count($deletdRows) <= 0) {

                $data->link = "supplierPayedDeptController.php?do=editprint&id=" . $data->supplierdebtchangeid;

                $data->discards = 0;

                $data->totalbill = 0;
                $data->totalOperationCost = $data->supplierdebtchangebefore;
                $data->discount = 0;

                if ($data->supplierdebtchangetype == 1) {
                    $data->payedamount = $data->supplierdebtchangeamount;
                } else {
                    $data->payedamount = $data->supplierdebtchangeamount;
                }
            } else {
                $data->supplierdebtchangeid = -1;
            }
        } else if ($data->tablename == "supplierController.php") {


            $data->link = "supplierController.php?do=editprint&id=" . $data->supplierdebtchangemodelid;
            $data->discards = 0;
            $data->totalOperationCost = $data->supplierdebtchangeamount;
            $data->supplierdebtchangeamount = 0;
            $data->totalbill = 0;
            $data->payedamount = 0;
        } else if ($data->tablename == "supplierDeficitController.php") {
            $data->link = "supplierDeficitController.php?do=editprint&id=".$data->supplierdebtchangeid;
            $data->discards = 0;
            $data->totalOperationCost = 0;

            $data->totalbill = 0;
            if ($data->supplierdebtchangetype == 1) {
                $data->payedamount = $data->supplierdebtchangeamount;
            } else {
                $data->payedamount = $data->supplierdebtchangeamount;
            }
            //$data->supplierdebtchangeamount = 0;
        }elseif ($data->tablename == "datedCheckedController.php") {
            $data->link = "datedCheckedController.php?do=details&id=" . $data->supplierdebtchangemodelid;

            $data->discards = 0;

            $data->totalbill = 0;
            $data->totalOperationCost = $data->supplierdebtchangebefore;
            $data->discount = 0;
            $data->payedamount = $data->supplierdebtchangeamount;
        }








        $totalsum += $data->totalOperationCost;
    }


    $smarty->assign('totalsum', $totalsum);

    $bure = '1';
    if ($bure == "1") {


        $smarty->assign('burex', $bure);
        foreach ($shownData as $datax) {
            if ($datax->supplierdebtchangeid > 0) {
                $finalstring = $datax->finalstring;

                //	print_r($finalstring.'<br>');
                $type = $datax->supplierdebtchangetype;
                $supplierdebtchangeid = $datax->supplierdebtchangeid;

                if ($type == 1) {

                    $otherfinal = substr_replace($finalstring, '0', -1);

                    foreach ($shownData as $xx) {

                        if ($xx->finalstring == $otherfinal) {
                            $xx->supplierdebtchangeid = -1;
                            $datax->supplierdebtchangeid = -1;
                        }
                    }
                }
            }
        }

        $ii = 0;
        //select all data from storemovement
        $startvalue = 0;
        $startbefore = 0;
        $startafter = 0;
        foreach ($shownData as $mov) {
            if ($bure == "1" && $mov->supplierdebtchangeid > 0) {
                if ($ii == 0) {
                    $startvalue = $mov->supplierdebtchangeamount;
                    $mov->supplierdebtchangeamount = $startvalue;
                    $startbefore = $mov->supplierdebtchangebefore;
                    $mov->supplierdebtchangebefore = $startbefore;
                    $startafter = $mov->supplierdebtchangeafter;
                    $mov->supplierdebtchangeafter = $startafter;
                } else {
                    $startvalue = abs($mov->supplierdebtchangeamount);
                    //	 print_r('startvaluecccc: '.$startvalue.'<br>');
                    $mov->supplierdebtchangeafter = $startbefore;
                    if ($mov->supplierdebtchangetype == "0") {
                        $mov->supplierdebtchangebefore = $startafter;
                        $mov->supplierdebtchangeafter = $startafter + $startvalue;
                        $startafter = $startafter + $startvalue;
                    } else {
                        //print_r('in else <br>');
                        //	print_r('startafter: '.$startafter.'<br>');
                        // if ($startvalue > 0) {
                        if ($mov->supplierdebtchangeamount > 0) {
                            $mov->supplierdebtchangebefore = $startafter;
                            $mov->supplierdebtchangeafter = $startafter - $startvalue;
                            $startafter = $startafter - $startvalue;
                        } else {

                            $mov->supplierdebtchangebefore = $startafter;
                            $mov->supplierdebtchangeafter = $startafter + $startvalue;
                            $startafter = $startafter + $startvalue;
                        }
                    }
                }
                $ii++;
            }
        }
    }





    // assign db results to the template
    $smarty->assign('shownData', $shownData);
}

///nora//////////////////

function supplierShow($supplierid, $startDate, $endDate)
{
    global $supplierDeptChangeExt;
    global $supplierDAO;
    global $buyBillDAO;
    global $returnBuyBillDAO;
    global $buyAndReturnBillDAO;
    global $smarty;


    $bure = $_POST["bure"];

    $queryString = ' WHERE';


    if (isset($supplierid) && $supplierid != '-1') {
        $suppliernamedata = $supplierDAO->load($supplierid);
        $message = " اسم المورد " . $suppliernamedata->suppliername;
        $smarty->assign('message', $message);

        $queryString .= ' supplierdebtchange.supplierid =' . $supplierid . ' AND';
    }

    if (!empty($startDate) && !empty($endDate)) {

        $message = " تقرير عميل من تاريخ  " . $startDate . "  الى تاريخ" . $endDate;
        $smarty->assign('message', $message);

        $queryString .= '  date( supplierdebtchange.supplierdebtchangedate ) >= "' . $startDate . '" AND date( supplierdebtchange.supplierdebtchangedate ) <= "' . $endDate . '" AND';
    }

    $arr = explode(' ', $queryString);
    if (isset($arr) && count($arr) > 0) {
        $lastWord = end($arr);
        if ($lastWord == 'AND') { //remove it
            array_pop($arr);
            //form the string again
            $queryString = implode(' ', $arr);
        } else if ($lastWord == 'WHERE') { //remove it
            array_pop($arr);
            $queryString = ' ';
        }
        //print("<br>queryString: ".$queryString."<br>");
    }

    // get the all colums number
    $shownData = $supplierDeptChangeExt->queryBySupplierIdAndDate2($queryString);


    foreach ($shownData as $data) {

        if ($data->supplierdebtchangemodelid != -1) {

            if ($data->tablename == "buyBillController.php") {
                $buybillid = $buyBillDAO->load($data->supplierdebtchangemodelid);
                $data->totalbillvalue = $buybillid->buybillaftertotalbill;
                $data->buybillpaid = $buybillid->buybilltotalpayed;
                $data->link = "buyBillController.php?do=details&id=" . $data->supplierdebtchangemodelid;
            } elseif ($data->tablename == "returnBuyBillController.php") {
                $buyreturnbillid = $returnBuyBillDAO->load($data->supplierdebtchangemodelid);
                $data->totalbillvalue = $buyreturnbillid->returnbuybillaftertotalbill;
                $data->buybillpaid = $buyreturnbillid->returnbuybilltotalpayed;
                $data->link = "returnBuyBillController.php?do=details&id=" . $data->supplierdebtchangemodelid;
            } elseif ($data->tablename == "buyAndReturnBillController.php") {
                $buyAndReturnBill = $buyAndReturnBillDAO->load($data->supplierdebtchangemodelid);
                $data->totalbillvalue = $buyAndReturnBill->buybillaftertotalbill;
                $data->buybillpaid = $buyAndReturnBill->buybilltotalpayed;
                $data->link = "buyAndReturnBillController.php?do=details&id=" . $data->supplierdebtchangemodelid;
            } elseif ($data->tablename == "kempialaController.php") {
                $data->totalbillvalue = $data->supplierdebtchangeamount;
                $data->buybillpaid = $data->supplierdebtchangeamount;
                $data->link = "kempialaController.php?do=editprint&id=" . $data->supplierdebtchangemodelid;
            } elseif ($data->tablename == "checkwithdrawalController.php") {
                $data->totalbillvalue = $data->supplierdebtchangeamount;
                $data->buybillpaid = $data->supplierdebtchangeamount;
                $data->link = "checkwithdrawalController.php?do=editprint&id=" . $data->supplierdebtchangemodelid;
            }
        } else {
            $data->totalbillvalue = $data->supplierdebtchangeamount;
            $data->buybillpaid = $data->supplierdebtchangeamount;
            $data->link = "#";
        }
    }


    if ($bure == "1") {

        $smarty->assign('burex', $bure);
        foreach ($shownData as $datax) {
            //print_r($datax);

            $finalstring = $datax->finalstring;

            //print_r($finalstring.'<br>');
            $type = $datax->supplierdebtchangetype;
            $supplierdebtchangeid = $datax->supplierdebtchangeid;



            if ($type == 1) {

                $otherfinal = substr_replace($finalstring, '0', -1);

                foreach ($shownData as $xx) {

                    if ($xx->finalstring == $otherfinal) {



                        $xx->supplierdebtchangeid = -1;
                        $datax->supplierdebtchangeid = -1;

                        print_r($datax->supplierdebtchangeid . '<br>');
                    }
                }
            }
        }
    }



    $ii = 0;
    //select all data from storemovement
    $startvalue = 0;
    $startbefore = 0;
    $startafter = 0;

    foreach ($shownData as $mov) {


        if ($bure == "1" && $mov->supplierdebtchangeid > 0) {


            if ($ii == 0) {

                $startvalue = $mov->supplierdebtchangeamount;
                $mov->supplierdebtchangeamount = $startvalue;
                $startbefore = $mov->supplierdebtchangebefore;
                $mov->supplierdebtchangebefore = $startbefore;
                $startafter = $mov->supplierdebtchangeafter;
                $mov->supplierdebtchangeafter = $startafter;
            } else {

                $startvalue = $mov->supplierdebtchangeamount;
                $mov->supplierdebtchangeafter = $startbefore;

                if ($mov->supplierdebtchangetype == "1") {

                    //print_r('in if<br>');
                    $mov->supplierdebtchangebefore = $startbefore + $startvalue;
                    $startbefore = $startbefore + $startvalue;
                } else {
                    //print_r('in else if<br>');
                    //print_r('startafter'.$startbefore .'<br>');
                    //print_r('startvalue'.$startbefore .'<br>');
                    //	print_r($startafter - $startvalue .'<br>');
                    $mov->supplierdebtchangebefore = $startbefore - $startvalue;
                    $startbefore = $startbefore - $startvalue;
                }


                //new product name with all path
            }





            $ii++;
        }
    }


    // assign db results to the template
    //print_r($shownData);
    $smarty->assign('shownData', $shownData);
}

function showBuyBillsBySupplierAndDate($supplierid, $startDate, $endDate)
{
    global $buyBillExt;
    global $buyAndReturnBillExt;
    global $buyBillDetailExt;
    global $buyAndReturnBillDetailExt;
    global $smarty;

    $totalBills = 0;
    $totalQTY = 0;

    $buybillData = $buyBillExt->queryWithSupplierIdAndDate($supplierid, $startDate, $endDate);
    //get products number in buy bills

    if (count($buybillData) > 0) {
        foreach ($buybillData as $bill) {
            $totalQty1 = $buyBillDetailExt->countProductInBuyBill($bill->buybillid);
            $bill->totalQty = $totalQty1;
            $totalQTY += $totalQty1;
            $totalBills += $bill->buybillaftertotalbill;
        }
    }

    //get buy bills from  buyandruternbill table
    $buybillandruternData = $buyAndReturnBillExt->queryBuyBillsOnlyWithSupplierIdAndDate($supplierid, $startDate, $endDate);
    //get products number in  buyandruternbill
    if (count($buybillandruternData) > 0) {
        foreach ($buybillandruternData as $bill) {
            $buybillid = $bill->buybillid;
            $buybilldiscountrype = $bill->buybilldiscountrype;
            $buybilldiscount = $bill->buybilldiscount;

            $allmydetales = $buyAndReturnBillDetailExt->queryByBuybillidANDType($buybillid, 0);
            //	print_r($allmydetales);

            $mytotal = 0;
            foreach ($allmydetales as $myallmydetales) {

                $buybilldetailtotalprice = $myallmydetales->buybilldetailtotalprice;
                $mytotal += $buybilldetailtotalprice;
            }

            //print_r('<br>');
            //print_r($mytotal);
            $myfialtotal = 0;
            //خصم مباشر
            if ($buybilldiscountrype == 0) {
                //print_r("in if <br>".$buybilldiscount);
                $myfialtotal = $mytotal - $buybilldiscount;
            } else {
                //print_r("in else <br>".$buybilldiscount.'<br>');
                //print_r((($mytotal*$buybilldiscount)/100).'<br>');
                $myfialtotal = $mytotal - (($mytotal * $buybilldiscount) / 100);
            }

            $bill->buybillaftertotalbill = $myfialtotal;
            $totalQty2 = $buyAndReturnBillDetailExt->countProductInBuyBillsOnly($bill->buybillid);
            $bill->totalQty = $totalQty2;
            $totalQTY += $totalQty2;
            $totalBills += $bill->buybillaftertotalbill;
        }
    }

    $buybillData = array_merge($buybillData, $buybillandruternData);

    $smarty->assign("buytotalQTY", $totalQTY);
    $smarty->assign("buytotalBills", $totalBills);

    return $buybillData;
}

function showReturnBuyBillsBySupplierAndDate($supplierid, $startDate, $endDate)
{
    global $returnBuyBillExt;
    global $buyAndReturnBillExt;
    global $returnBuyBillDetailExt;
    global $buyAndReturnBillDetailExt;
    global $smarty;

    $totalBills = 0;
    $totalQTY = 0;

    $returnbuybillData = $returnBuyBillExt->queryWithSupplierIdAndDate($supplierid, $startDate, $endDate);
    //get products number in return buy bills
    if (count($returnbuybillData) > 0) {
        foreach ($returnbuybillData as $bill) {
            //print_r($bill);
            if ($bill->returnbuybillaftertotalbill < 0) {
                $bill->returnbuybillaftertotalbill = ($bill->returnbuybillaftertotalbill * -1);
            }
            $totalQty1 = $returnBuyBillDetailExt->countProductInBuyBill($bill->returnbuybillid);
            $bill->totalQty = $totalQty1;
            $totalQTY += $totalQty1;
            if ($bill->returnbuybillaftertotalbill > 0) {
                $totalBills += $bill->returnbuybillaftertotalbill;
            } else {

                $totalBills -= $bill->returnbuybillaftertotalbill;
            }
        }
    }


    //get return buy bills from  buyandruternbill table
    $buybillandruternData = $buyAndReturnBillExt->queryReturnBuyBillsOnlyWithSupplierIdAndDate($supplierid, $startDate, $endDate);

    if (count($buybillandruternData) > 0) {
        foreach ($buybillandruternData as $buy) {
            $buy->returnbuybillid = $buy->buybillid;

            $allmydetales = $buyAndReturnBillDetailExt->queryByBuybillidANDType($buy->buybillid, 1);
            //	print_r($allmydetales);

            $mytotal = 0;
            foreach ($allmydetales as $myallmydetales) {

                $buybilldetailtotalprice = $myallmydetales->buybilldetailtotalprice;
                $mytotal += $buybilldetailtotalprice;
            }

            $buy->buybillaftertotalbill = $mytotal;

            $buy->returnbuybillSerial = $buy->buybillSerial;
            $buy->returnbuybillaftertotalbill = $buy->buybillaftertotalbill;
            $buy->returnbuybilldate = $buy->buybilldate;

            $totalQty2 = $buyAndReturnBillDetailExt->countProductInReturnBuyBillsOnly($buy->buybillid);
            $buy->totalQty = $totalQty2;
            $totalQTY += $totalQty2;
            $totalBills += $mytotal; //$bill->buybillaftertotalbill;
        }
    }
    $returnbuybillData = array_merge($returnbuybillData, $buybillandruternData);

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

    return $returnbuybillData;
}

function showx($supplierId, $startDate, $endDate)
{
    global $supplierDeptChangeExt;
    global $supplierdebtchange;
    global $supplierDAO;
    global $smarty;
    global $queryString;
    $queryString = ' WHERE';

    if (isset($supplierId) && $supplierId != "-1" && $supplierId != "-2") {
        $messageData = $supplierDAO->load($supplierId);
        $message = "إسم المورد: " . $messageData->suppliername;
        $smarty->assign("message", $message);

        $queryString .= ' supplierdebtchange.supplierid =' . $supplierId . ' AND';
    }

    if (isset($supplierId) && $supplierId != "-1" && $supplierId == "-2") {

        $message = " كل الموردين ";
        $smarty->assign("message", $message);

        $queryString .= ' supplierdebtchange.supplierid > 0 AND';
    }


    if (!empty($startDate) && !empty($endDate)) {



        $message = " تقرير عجز مورد من تاريخ " . $startDate . "  الى تاريخ" . $endDate;
        $smarty->assign('message', $message);


        $queryString .= ' supplierdebtchange.supplierdebtchangedate >= "' . $startDate . '" AND supplierdebtchange.supplierdebtchangedate <= "' . $endDate . '" AND';
    }

    $arr = explode(' ', $queryString);
    if (isset($arr) && count($arr) > 0) {
        $lastWord = end($arr);
        if ($lastWord == 'AND') { //remove it
            array_pop($arr);
            //form the string again
            $queryString = implode(' ', $arr);
        } else if ($lastWord == 'WHERE') { //remove it
            array_pop($arr);
            $queryString = ' ';
        }
        //print("<br>queryString: ".$queryString."<br>");
    }

    $shownData = $supplierDeptChangeExt->searchPayedDeptOnly($queryString);

    $sum;
    foreach ($shownData as $data) {
        $sum += $data->supplierdebtchangeamount;
    }
    $smarty->assign('sum', $sum);

    // assign db results to the template
    $smarty->assign('shownData', $shownData);
}
