<?php

//the global file operation
include("../public/impOpreation.php");
include("../library/regenerateSession.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");
//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');
//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');
//Billname
require_once('../models/dao/BillnameDAO.class.php');
require_once('../models/dto/Billname.class.php');
require_once('../models/mysql/BillnameMySqlDAO.class.php');
require_once('../models/mysql/ext/BillnameMySqlExtDAO.class.php');
//Billsetting
require_once('../models/dao/BillsettingsDAO.class.php');
require_once('../models/dto/Billsetting.class.php');
require_once('../models/mysql/BillsettingsMySqlDAO.class.php');
require_once('../models/mysql/ext/BillsettingsMySqlExtDAO.class.php');
//Programsetting
require_once('../models/dao/ProgramsettingsDAO.class.php');
require_once('../models/dto/Programsetting.class.php');
require_once('../models/mysql/ProgramsettingsMySqlDAO.class.php');
require_once('../models/mysql/ext/ProgramsettingsMySqlExtDAO.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 :- sellBillReportsController

  OPERTATION in Controller

  1- show sell bills only
  2- search

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

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

//here goes the instances and general variables
//Returnsellbill
$myReturnsellbill = new Returnsellbill();
$myReturnsellbillRecord = new ReturnsellbillMySqlDAO();
$myReturnsellbillEx = new ReturnsellbillMySqlExtDAO();
//Returnsellbilldetail
$myReturnsellbilldetail = new Returnsellbilldetail();
$myReturnsellbilldetailRecord = new ReturnsellbilldetailMySqlDAO();
$myReturnsellbilldetailEx = new ReturnsellbilldetailMySqlExtDAO();
//Sellbillandrutern
$mySellbillandrutern = new Sellbillandrutern();
$mySellbillandruternRecord = new SellbillandruternMySqlDAO();
$mySellbillandruternEx = new SellbillandruternMySqlExtDAO();
//Sellandruternbilldetail
$mySellandruternbilldetail = new Sellandruternbilldetail();
$mySellandruternbilldetailRecord = new SellandruternbilldetailMySqlDAO();
$mySellandruternbilldetailEx = new SellandruternbilldetailMySqlExtDAO();
//Client
$myClientRecord = new ClientMySqlDAO();
$myClient = new Client();
$myClientEx = new ClientMySqlExtDAO();
//Billname
$myBillnameRecord = new BillnameMySqlDAO();
$myBillnameEx = new BillnameMySqlExtDAO();
//Billsetting
$myBillsettingEx = new BillsettingsMySqlExtDAO();
//Programsetting
$ProgramsettingDAO = new ProgramsettingsMySqlDAO();
$programSettingExt = new ProgramsettingsMySqlExtDAO();

$youtubeLink = new YoutubeLink();
$youtubeLinkDAO = new YoutubeLinkMySqlDAO();
$youtubeLinkEX = new YoutubeLinkMySqlExtDAO();
$myUserRecord = new UserMySqlDAO();
$Programsettingdata = $ProgramsettingDAO->load(1);
$smarty->assign("Programsettingdata", $Programsettingdata);


//check and use the condition that suites this action
if (empty($do) || $do == "show") {
    //here the permission check
    include_once("../public/authentication.php");

    $clientData = loadAllClient();
    $smarty->assign("clientData", $clientData);

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

    $startDate = $_REQUEST['from'];
    $endDate = $_REQUEST['to'];
    $sellbillSerial = $_REQUEST['sellbillserial'];
    $returnsellbillId = $_POST['returnsellbillid'];
    $clientId = $_REQUEST['clientid'];

//    if (isset($clientId) && $clientId != "-1") {
//        showByClient($clientId);
//
//        $messageData = $myClientRecord->load($clientId);
//        $message = "إسم العميل: " . $messageData->clientname;
//        $smarty->assign("message", $message);
//    } elseif (isset($sellbillSerial) && $sellbillSerial != "") {
//        showBySriral($sellbillSerial);
//
//        $message = "رقم الفاتورة: " . $sellbillSerial;
//        $smarty->assign("message", $message);
//    } elseif (isset($returnsellbillId) && $returnsellbillId != "") {
//        showByReturnsellbillId($returnsellbillId);
//
//        $message = "الرقم المتسلسل : " . $returnsellbillId;
//        $smarty->assign("message", $message);
//    } elseif (isset($startDate) && $startDate != "" && isset($endDate) && $endDate != "") {
//        showByDate($startDate, $endDate);
//
//        $message = "من تاريخ : " . $startDate . " إلى تاريخ : " . $endDate . "";
//        $smarty->assign("message", $message);
//    } else {
//        $startDate = $endDate = date('Y-m-d');
//        showByDate($startDate, $endDate);
//        //showAll();
//    }
    //$message = "من تاريخ : ".date("Y-m-d")." إلى تاريخ : ".date("Y-m-d")."";
//    $smarty->assign("message", $message);
    //}

    showAll();
    $smarty->display("returnSellBillReportsview/show.html");
} elseif ($do == "returnDetail") {
    include_once("../public/authentication.php");

    $returnsellbillId = $_GET['returnsellbillid'];

    $showDetailData = showDetail($returnsellbillId);
    $smarty->assign('returnsellbillData', $showDetailData[0]);
    $smarty->assign('returnsellbilldetailData', $showDetailData[1]);
    $smarty->assign('quantity', $showDetailData[2]);

    $billsettingsData = loadBillProperty($showDetailData[0]->billnameid);
    $smarty->assign("billsettingsData", $billsettingsData);

    $billnameAddData = $myBillnameRecord->load($showDetailData[0]->billnameid);
    $smarty->assign("billnameAddData", $billnameAddData);

    $Programsettingdata = $ProgramsettingDAO->load(1);
    $smarty->assign("Programsettingdata", $Programsettingdata);

    $smarty->display("returnSellBillReportsview/returnDetail.html");
} elseif ($do == "sellAndReturnDetail") {
    include_once("../public/authentication.php");

    $sellbillId = $_GET['sellbillid'];

    $showDetailData = showsellAndReturnDetail($sellbillId);
    $smarty->assign('sellbillandruternData', $showDetailData[0]);
    $smarty->assign('sellbilldetailData', $showDetailData[1]);
    $smarty->assign('ruternbilldetailData', $showDetailData[2]);
    $smarty->assign('sellQuantity', $showDetailData[3]);
    $smarty->assign('returnQuantity', $showDetailData[4]);

    $billsettingsData = loadBillProperty($showDetailData[0]->billnameid);
    $smarty->assign("billsettingsData", $billsettingsData);

    $billnameAddData = $myBillnameRecord->load($showDetailData[0]->billnameid);
    $smarty->assign("billnameAddData", $billnameAddData);

    $Programsettingdata = $ProgramsettingDAO->load(1);
    $smarty->assign("Programsettingdata", $Programsettingdata);

    //here the smarty templates
    $smarty->display("returnSellBillReportsview/sellAndReturnDetail.html");
}
$smarty->assign("customValidation", 1);

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

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

//select data from client tbl
function loadAllClient() {
    global $myClientRecord;

    $clientData = $myClientRecord->queryAll();
    return $clientData;
}

// select from Billname tbl
function loadBillname() {
    global $myBillnameEx;

    // select all data from Billname
    $billnameData = $myBillnameEx->queryByType(0);
    return $billnameData;
}

// select from Billname tbl
function loadBillProperty($billnameid) {
    global $myBillsettingEx;

    // select all data from Billname
    $billsettingsData = $myBillsettingEx->queryWithBillnameId($billnameid);
    return $billsettingsData;
}

function showDetail($returnsellbillId) {

    global $myReturnsellbillEx;
    global $myReturnsellbilldetailEx;

    //load purchasebillreturn data by id
    $returnsellbillData = $myReturnsellbillEx->loadReturnsellbillById($returnsellbillId);

    $returnsellbilldetailData = $myReturnsellbilldetailEx->queryWithReturnsellbillId($returnsellbillId);

    $quantity = 0;
    foreach ($returnsellbilldetailData as $returnsellbilldetail) {
        $quantity = $quantity + ($returnsellbilldetail->returnsellbilldetailquantity);
    }

    return array($returnsellbillData, $returnsellbilldetailData, $quantity);
}

function showsellAndReturnDetail($sellbillid) {
    global $mySellbillandruternEx;
    global $mySellandruternbilldetailEx;

    //load Sellbillandrutern data by id
    $sellbillandruternData = $mySellbillandruternEx->loadSellbillandruternById($sellbillid);

    $sellbilldetailData = $mySellandruternbilldetailEx->queryWithSellBillIdAndSellType($sellbillid, 0);

    $sellQuantity = 0;
    foreach ($sellbilldetailData as $sellbilldetail) {
        $sellQuantity = $sellQuantity + $sellbilldetail->sellbilldetailquantity;
    }

    $ruternbilldetailData = $mySellandruternbilldetailEx->queryWithSellBillIdAndSellType($sellbillid, 1);

    $returnQuantity = 0;
    foreach ($ruternbilldetailData as $ruternbilldetail) {
        $returnQuantity = $returnQuantity + $ruternbilldetail->sellbilldetailquantity;
    }

    return array($sellbillandruternData, $sellbilldetailData, $ruternbilldetailData, $sellQuantity, $returnQuantity);
}

function showAll() {
    global $myReturnsellbillEx;
    global $mySellandruternbilldetailEx;

    global $mySellbillandruternEx;
    global $myReturnsellbilldetailEx;
    global $smarty;
    global $myClientRecord;
    global $ProgramsettingDAO;
    global $myUserRecord;

    $userData = $myUserRecord->load($_SESSION['userid']);




    $startDate = $_REQUEST['from'];
    $endDate = $_REQUEST['to'];
    $sellbillSerial = $_REQUEST['sellbillserial'];
    $sellbillId = $_REQUEST['sellbillid'];
    $clientId = $_REQUEST['clientid'];

    $message = '';
    $queryString = ' WHERE';
    $returnqueryString = ' WHERE';


    if (isset($clientId) && $clientId != '-1') {
        $messageData = $myClientRecord->load($clientId);
        $message = $message . '<br>' . "إسم العميل: " . $messageData->clientname;

        $queryString .= ' sellbillandrutern.sellbillclientid = ' . $clientId . ' AND';
        $returnqueryString .= ' returnsellbill.returnsellbillclientid = ' . $clientId . ' AND';
    }


    if ($userData->viewbills == 0) {
        $queryString .= ' sellbillandrutern.userid =' . $_SESSION['userid'] . ' AND';
        $returnqueryString .= ' returnsellbill.userid =' . $_SESSION['userid'] . ' AND';
    } elseif ($userData->viewbills == 2) {
        $queryString .= ' u.usergroupid =' . $_SESSION['usergroupid'] . ' AND';
        $returnqueryString .= ' user2.usergroupid =' . $_SESSION['usergroupid'] . ' AND';
    }

    if (isset($sellbillSerial) && $sellbillSerial != '') {

        //showBySriral($sellbillSerial);

        $message = $message . '<br>' . "رقم الفاتورة: " . $sellbillSerial;

        $queryString .= ' sellbillandrutern.sellbillSerial = "' . $sellbillSerial . '" AND';
        $returnqueryString .= ' returnsellbill.returnsellbillserial = "' . $sellbillSerial . '" AND';
    }

    if (isset($sellbillId) && $sellbillId != '') {
        //	showBySellbillId($sellbillId);
        $message = $message . '<br>' . "الرقم المتسلسل : " . $sellbillId;

        $queryString .= ' sellbillandrutern.sellbillid = "' . $sellbillId . '" AND';
        $returnqueryString .= ' returnsellbill.returnsellbillid = ' . $sellbillId . ' AND';
    }

    if (!empty($startDate) && !empty($endDate)) {
        //display the converted time ##reportsPlusHours
        $Programsetting = $ProgramsettingDAO->load(1);
        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
            $endDate = date('Y-m-d H:i:s', strtotime('+' . $reportsPlusHours . ' hour +0 minutes', strtotime($endDate)));
            $startDate = date('Y-m-d H:i:s', strtotime('+' . $Programsetting->reportsPlusHours . ' hour +0 minutes', strtotime($startDate)));
        } else {
            $endDatePrepared = $endDate . ' 23:59:59';
            $startDate = $startDate . " 00:00:00";
        }
        $message = $message . '<br>' . 'من تاريخ : ' . $startDate . 'إلى تاريخ : ' . $endDate . '';

        $queryString .= ' sellbillandrutern.sellbilldate >= "' . $startDate . '" AND sellbillandrutern.sellbilldate <= "' . $endDate . '" AND';
        $returnqueryString .= ' returnsellbill.returnsellbilldate >= "' . $startDate . '" AND returnsellbill.returnsellbilldate <= "' . $endDate . '" AND';
    } elseif (empty($startDate) && empty($endDate) && empty($sellbillSerial) && empty($sellbillid) && !isset($clientId)) {
        $startDate = $endDate = $today = date("Y-m-d");
        //display the converted time ##reportsPlusHours
        $Programsetting = $ProgramsettingDAO->load(1);
        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
            $endDate = date('Y-m-d H:i:s', strtotime('+' . $reportsPlusHours . ' hour +0 minutes', strtotime($endDate)));
            $startDate = date('Y-m-d H:i:s', strtotime('+' . $Programsetting->reportsPlusHours . ' hour +0 minutes', strtotime($startDate)));
        } else {
            $endDatePrepared = $endDate . ' 23:59:59';
            $startDate = $startDate . " 00:00:00";
        }

        $message = $message . '<br>' . "من تاريخ : " . $startDate . " إلى تاريخ : " . $endDate . "";

        $queryString .= ' sellbillandrutern.sellbilldate >= "' . $startDate . '" AND sellbillandrutern.sellbilldate <= "' . $endDate . '" AND';
        $returnqueryString .= ' returnsellbill.returnsellbilldate >= "' . $startDate . '" AND returnsellbill.returnsellbilldate <= "' . $endDate . '" AND';
        //	print_r($returnqueryString);
    }
    $smarty->assign("message", $message);


    $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>");
    }



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




    $totalBills = 0;
    $totalQTY = 0;
    $totaltax = 0;
    $totaldiscount = 0;




    $returnsellbillData = $myReturnsellbillEx->GetReturnSellBillUsingQueryString($returnqueryString);
    if (count($returnsellbillData) > 0) {
        foreach ($returnsellbillData as $sell) {

            if ($sell->conditions == 0) {
                $totalBills += $sell->returnsellbillaftertotalbill;
                $totalQTY += $sell->returnsellQuantity;
                $sell->type = "returnSell";



                //getalldiscount
                $sellbilldiscount = $sell->returnsellbilldiscount;
                $sellbilldiscounttype = $sell->returnsellbilldiscounttype; //1 مباشر
                $sellbilltotalbill = $sell->returnsellbilltotalbill;
                $sellbillaftertotalbill = $sell->returnsellbillaftertotalbill;
                $tax = $sell->tax;

                $detaildiscount = $myReturnsellbilldetailEx->getsumdiscount($sell->returnsellbillid);
                if ($sellbilldiscounttype == 1) {


                    //discount
                    $totaldiscount = $totaldiscount + $sellbilldiscount + $detaildiscount;
                    $sell->returnsellbilldiscount = ($sellbilldiscount + $detaildiscount);




                    //tax
                    $taxvalue = $sellbillaftertotalbill - ($sellbilltotalbill - ($sellbilldiscount));
                    $totaltax = $totaltax + $taxvalue;
                    $sell->tax = $taxvalue;
                } else {
                    //discount
                    $discountvalue = ($sellbilltotalbill / 100) * $sellbilldiscount;
                    $totaldiscount = $totaldiscount + $discountvalue + $detaildiscount;
                    $sell->returnsellbilldiscount = ($discountvalue + $detaildiscount);

                    //tax
                    $taxvalue = $sellbillaftertotalbill - ($sellbilltotalbill - ($discountvalue));
                    $totaltax = $totaltax + $taxvalue;
                    $sell->tax = $taxvalue;
                }
            }
        }
    }

    //get return sell bills from sellbillandrutern table
    $sellbillandruternData = $mySellbillandruternEx->GetSellBillAndReturnUsingQueryString($queryString);

    //print_r($sellbillandruternData);

    if (count($sellbillandruternData) > 0) {
        foreach ($sellbillandruternData as $sell) {
            if ($sell->conditions == 0) {
                $totalBills += $sell->sellbillaftertotalbill;
                $sell->returnsellbillid = $sell->sellbillid;
                $sell->returnsellbillaftertotalbill = $sell->sellbillaftertotalbill;
                $sell->returnsellbilldate = $sell->sellbilldate;
                $totalQTY += $sell->returnsellQuantity;
                $sell->type = "sellAndReturn";



                //getalldiscount
                $sellbilldiscount = $sell->sellbilldiscount;
                $sellbilldiscounttype = $sell->sellbilldiscounttype; //1 مباشر
                $sellbilltotalbill = $sell->sellbilltotalbill;
                $sellbillaftertotalbill = $sell->sellbillaftertotalbill;
                $tax = $sell->tax;

                $detaildiscount = $mySellandruternbilldetailEx->getsumdiscount($sell->sellbillid, 1);
                if ($sellbilldiscounttype == 1) {
                    //discount
                    $totaldiscount = $totaldiscount + $detaildiscount;
                    $sell->sellbilldiscount = $detaildiscount;


                    //tax
                    $taxvalue = 0; //$sellbillaftertotalbill-($sellbilltotalbill-($sellbilldiscount));
                    $totaltax = $totaltax + $taxvalue;
                    $sell->tax = $taxvalue;
                } else {
                    //discount
                    //$discountvalue=($sellbilltotalbill/100)*$sellbilldiscount;
                    $totaldiscount = $totaldiscount + $detaildiscount;
                    $sell->sellbilldiscount = $detaildiscount;
                    //tax
                    $taxvalue = 0; //$sellbillaftertotalbill-($sellbilltotalbill-($discountvalue));
                    $totaltax = $totaltax + $taxvalue;
                    $sell->tax = $taxvalue;
                }
            }
        }
    }

    $returnsellbillData = array_merge($returnsellbillData, $sellbillandruternData);
    // assign your db results to the template
    $smarty->assign('returnsellbillData', $returnsellbillData);
    $smarty->assign('totalBills', $totalBills);
    $smarty->assign('totalQTY', $totalQTY);
    $smarty->assign('totaltax', $totaltax);
    $smarty->assign('totaldiscount', $totaldiscount);
}

function showByClient($clientId) {
    global $myReturnsellbillEx;
    global $mySellbillandruternEx;
    global $smarty;

    $totalBills = 0;
    $totalQTY = 0;


    $returnsellbillData = $myReturnsellbillEx->queryWithClientId($clientId);
    if (count($returnsellbillData) > 0) {
        if ($sell->conditions == 0) {
            foreach ($returnsellbillData as $sell) {
                $totalBills += $sell->returnsellbillaftertotalbill;
                $totalQTY += $sell->returnsellQuantity;
                $sell->type = "returnSell";
            }
        }
    }

    //get return sell bills from sellbillandrutern table
    $sellbillandruternData = $mySellbillandruternEx->queryReturnSellBillsOnlyWithClientId($clientId);

    if (count($sellbillandruternData) > 0) {
        foreach ($sellbillandruternData as $sell) {
            if ($sell->conditions == 0) {
                $totalBills += $sell->sellbillaftertotalbill;
                $sell->returnsellbillid = $sell->sellbillid;
                $sell->returnsellbillaftertotalbill = $sell->sellbillaftertotalbill;
                $sell->returnsellbilldate = $sell->sellbilldate;
                $totalQTY += $sell->returnsellQuantity;
                $sell->type = "sellAndReturn";
            }
        }
    }

    $returnsellbillData = array_merge($returnsellbillData, $sellbillandruternData);
    // assign your db results to the template
    $smarty->assign('returnsellbillData', $returnsellbillData);
    $smarty->assign('totalBills', $totalBills);
    $smarty->assign('totalQTY', $totalQTY);
}

function showBySriral($serial) {
    global $myReturnsellbillEx;
    global $mySellbillandruternEx;
    global $smarty;

    $totalBills = 0;
    $totalQTY = 0;


    $returnsellbillData = $myReturnsellbillEx->queryWithSerail($serial);
    if (count($returnsellbillData) > 0) {
        foreach ($returnsellbillData as $sell) {
            if ($sell->conditions == 0) {
                $totalBills += $sell->returnsellbillaftertotalbill;
                $totalQTY += $sell->returnsellQuantity;
                $sell->type = "returnSell";
            }
        }
    }

    //get return sell bills from sellbillandrutern table
    $sellbillandruternData = $mySellbillandruternEx->queryReturnSellBillsOnlyWithSerail($serial);

    if (count($sellbillandruternData) > 0) {
        foreach ($sellbillandruternData as $sell) {
            if ($sell->conditions == 0) {
                $totalBills += $sell->sellbillaftertotalbill;
                $sell->returnsellbillid = $sell->sellbillid;
                $sell->returnsellbillaftertotalbill = $sell->sellbillaftertotalbill;
                $sell->returnsellbilldate = $sell->sellbilldate;
                $totalQTY += $sell->returnsellQuantity;
                $sell->type = "sellAndReturn";
            }
        }
    }

    $returnsellbillData = array_merge($returnsellbillData, $sellbillandruternData);
    // assign your db results to the template
    $smarty->assign('returnsellbillData', $returnsellbillData);
    $smarty->assign('totalBills', $totalBills);
    $smarty->assign('totalQTY', $totalQTY);
}

function showByReturnsellbillId($returnsellbillId) {
    global $myReturnsellbillEx;
    global $mySellbillandruternEx;
    global $smarty;

    $totalBills = 0;
    $totalQTY = 0;


    $returnsellbillData = $myReturnsellbillEx->queryWithReturnsellbillid($returnsellbillId);
    if (count($returnsellbillData) > 0) {
        foreach ($returnsellbillData as $sell) {
            if ($sell->conditions == 0) {
                $totalBills += $sell->returnsellbillaftertotalbill;
                $totalQTY += $sell->returnsellQuantity;
                $sell->type = "returnSell";
            }
        }
    }

    //get return sell bills from sellbillandrutern table
    $sellbillandruternData = $mySellbillandruternEx->queryReturnSellBillsOnlyWithSellbillid($returnsellbillId);

    if (count($sellbillandruternData) > 0) {
        foreach ($sellbillandruternData as $sell) {
            if ($sell->conditions == 0) {
                $totalBills += $sell->sellbillaftertotalbill;
                $sell->returnsellbillid = $sell->sellbillid;
                $sell->returnsellbillaftertotalbill = $sell->sellbillaftertotalbill;
                $sell->returnsellbilldate = $sell->sellbilldate;
                $totalQTY += $sell->returnsellQuantity;
                $sell->type = "sellAndReturn";
            }
        }
    }

    $returnsellbillData = array_merge($returnsellbillData, $sellbillandruternData);
    // assign your db results to the template
    $smarty->assign('returnsellbillData', $returnsellbillData);
    $smarty->assign('totalBills', $totalBills);
    $smarty->assign('totalQTY', $totalQTY);
}

function showByDate($startDate, $endDate) {
    global $myReturnsellbillEx;
    global $mySellbillandruternEx;
    global $smarty;

    $totalBills = 0;
    $totalQTY = 0;


    $returnsellbillData = $myReturnsellbillEx->queryWithDate($startDate, $endDate);
    if (count($returnsellbillData) > 0) {
        foreach ($returnsellbillData as $sell) {
            if ($sell->conditions == 0) {
                $totalBills += $sell->returnsellbillaftertotalbill;
                $totalQTY += $sell->returnsellQuantity;
                $sell->type = "returnSell";
            }
        }
    }

    //get return sell bills from sellbillandrutern table
    $sellbillandruternData = $mySellbillandruternEx->queryReturnSellBillsOnlyWithDate($startDate, $endDate);

    if (count($sellbillandruternData) > 0) {
        foreach ($sellbillandruternData as $sell) {
            if ($sell->conditions == 0) {
                $totalBills += $sell->sellbillaftertotalbill;
                $sell->returnsellbillid = $sell->sellbillid;
                $sell->returnsellbillaftertotalbill = $sell->sellbillaftertotalbill;
                $sell->returnsellbilldate = $sell->sellbilldate;
                $totalQTY += $sell->returnsellQuantity;
                $sell->type = "sellAndReturn";
            }
        }
    }

    $returnsellbillData = array_merge($returnsellbillData, $sellbillandruternData);
    // assign your db results to the template
    $smarty->assign('returnsellbillData', $returnsellbillData);
    $smarty->assign('totalBills', $totalBills);
    $smarty->assign('totalQTY', $totalQTY);
}

?>