<?php
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');

//Checkdeposit
require_once('../models/dao/CheckdepositDAO.class.php');
require_once('../models/dto/Checkdeposit.class.php');
require_once('../models/mysql/CheckdepositMySqlDAO.class.php');
require_once('../models/mysql/ext/CheckdepositMySqlExtDAO.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');


//Bill
require_once('../models/dao/BillsDAO.class.php');
require_once('../models/dto/Bill.class.php');
require_once('../models/mysql/BillsMySqlDAO.class.php');
require_once('../models/mysql/ext/BillsMySqlExtDAO.class.php');

//Save
require_once('../models/dao/SaveDAO.class.php');
require_once('../models/dto/Save.class.php');
require_once('../models/mysql/SaveMySqlDAO.class.php');
require_once('../models/mysql/ext/SaveMySqlExtDAO.class.php');


require_once('../models/dao/CheckwithdrawalDAO.class.php');
require_once('../models/dto/Checkwithdrawal.class.php');
require_once('../models/mysql/CheckwithdrawalMySqlDAO.class.php');
require_once('../models/mysql/ext/CheckwithdrawalMySqlExtDAO.class.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 :- clientReportsController

  OPERTATION in Controller

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

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

//here goes the instances and general variables
//Client
$client = new Client();
$clientDAO = new ClientMySqlDAO();
$clientExt = new ClientMySqlExtDAO();
//Clientdebtchange
$clientDeptChange = new Clientdebtchange();
$clientDeptChangeDAO = new ClientdebtchangeMySqlDAO();
$clientDeptChangeExt = new ClientdebtchangeMySqlExtDAO();

//Checkdeposit
$Checkdeposit = new Checkdeposit();
$CheckdepositDAO = new CheckdepositMySqlDAO();
$CheckdepositEX = new CheckdepositMySqlExtDAO();

//Sellbill
$sellBillDAO = new SellbillMySqlDAO();
$sellBillExt = new SellbillMySqlExtDAO();

//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();

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

//bill
$billsReturn = new Billsreturn();
$billsReturnDAO = new BillsreturnMySqlDAO();
$billsReturnEX = new BillsreturnMySqlExtDAO();
//Clientarea
$Clientarea = new Clientarea();
$ClientareaDAO = new ClientareaMySqlDAO();
$ClientareaEX = new ClientareaMySqlExtDAO();
$checkWithdrawalDAO = new CheckwithdrawalMySqlDAO;
$checkWithdrawalExtDAO = new CheckwithdrawalMySqlExtDAO;
$checkWithdrawal = new Checkwithdrawal;
//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();

//Save
$save = new Save();
$saveDAO = new SaveMySqlDAO();
$saveExt = new SaveMySqlExtDAO();

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

//check and use the condition that suite this action



if (empty($do) || $do == "show") { // تقرير لعميل معين
    include_once("../public/authentication.php");

    $clientData = getClientData();
    $smarty->assign('clientData', $clientData);

    $clientid = $_REQUEST['clientid'];
    $supplierid = $_REQUEST['supplierid']; 

    $startDate = $_REQUEST['from'];
    $endDate = $_REQUEST['to'];
    $order = (int)$_REQUEST['order'];

    $message = '';
    if (!empty($clientid) && $clientid != '-1' || !empty($startDate) && !empty($endDate) && $startDate != "" && $endDate != "") {
        $messageData = $clientDAO->load($clientid);
        $message .= "إسم العميل: " . $messageData->clientname . '<br>';

        clientShow($clientid, $startDate, $endDate, $order);
    }
    $supplierData = getSupplierData();
    $smarty->assign('supplierData', $supplierData);

    
    if (!empty($supplierid) && $supplierid != '-1' || !empty($startDate) && !empty($endDate) && $startDate != "" && $endDate != "") {
        $messageData = $supplierDAO->load($supplierid);
        $message .= "إسم المورد: " . $messageData->suppliername  . '<br>';


       

        supplierShow($supplierid, $startDate, $endDate, $order);
    }
     $message .= "التاريخ: من: " . $startDate . " إلى: " . $endDate;
     $smarty->assign("message", $message);

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

    $smarty->display("reportClientAndSupplierView/show.html");
}

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

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

function getClientData() {
    global $clientDAO;
    global $clientExt;

    $alldata = $clientExt->queryAllsup();
    return $alldata;
}
function getSupplierData() {
    global $supplierDAO;
    global $supplierExt;

    $alldata = $supplierDAO->queryAll();
    return $alldata;
}
function clientShow($clientid, $startDate, $endDate, $order) {


    //
    global $CheckdepositEX;


    $bure = $_POST["bure"];
    global $clientDeptChangeExt;
    global $sellBillDAO;
    global $returnSellBillDAO;
    global $smarty;
    global $mySellbillandruternRecord;
    global $billsDAO;
    $queryString = ' WHERE';


    if (isset($clientid) && $clientid != '-1') {
        /* $clientnamedata=$clientDAO->load($clientname);
          $message=" اسم العميل ".$clientnamedata->clientname;
          $smarty->assign('message',$message); */

        $queryString .= ' clientdebtchange.clientid =' . $clientid . ' AND';
    }

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

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

        $queryString .= ' clientdebtchange.clientdebtchangedate >= "' . $startDate . '" AND clientdebtchange.clientdebtchangedate <= "' . $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>");
    }
    
    if($order == 1){
        $orderBy = " order by clientdebtchangedate desc ";
    }else{
        $orderBy = " order by clientdebtchangeid desc ";   
    }
    
    $shownData = $clientDeptChangeExt->queryByClientIdAndDate($queryString, $orderBy);

    //print_r($shownData);

    foreach ($shownData as $data) {
        if ($data->clientdebtchangetype == 0) {
            $total = $total + $data->clientdebtchangeamount;
        } else {

            $total = $total - $data->clientdebtchangeamount;
        }


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

            // إجمالي الفاتورة و إجمالى المستحق من فاتورة المبيعات
            if ($data->tablename == "sellbillController.php") {
                //print("d");
                $data->link = "sellbillController.php?do=showDetail&sellbillid=" . $data->clientdebtchangemodelid;
                $sellBillData = $sellBillDAO->load($data->clientdebtchangemodelid);
                $data->totalbill = $sellBillData->sellbilltotalbill; // إجمالي الفاتورة
                $data->finalbill = $returnSellBillData->sellbillaftertotalbill; //إجمالى المستحق
                $data->totalOperationCost = $sellBillData->sellbillaftertotalbill;
            } else if ($data->tablename == "returnsellbillController.php") {
                $data->link = "returnsellbillController.php?do=showDetail&returnsellbillid=" . $data->clientdebtchangemodelid;
                $returnSellBillData = $returnSellBillDAO->load($data->clientdebtchangemodelid);
                $data->totalbill = $returnSellBillData->returnsellbilltotalbill; // إجمالي الفاتورة
                $data->finalbill = $returnSellBillData->returnsellbillaftertotalbill; //إجمالى المستحق
                $data->totalOperationCost = $returnSellBillData->returnsellbillaftertotalbill;
            } else if ($data->tablename == "sellbillandruternController.php") {
                $data->link = "sellbillandruternController.php?do=showDetail&sellbillid=" . $data->clientdebtchangemodelid;

                $sellAndReturnBillData = $mySellbillandruternRecord->load($data->clientdebtchangemodelid);
                $data->totalOperationCost = $sellAndReturnBillData->sellbillaftertotalbill;
            } else if ($data->tablename == "bills.php") {
                $data->link = "bills.php?do=details&id=" . $data->clientdebtchangemodelid;
                $BillsData = $billsDAO->load($data->clientdebtchangemodelid);
                $data->totalbill = $BillsData->productstotalprice; // إجمالي الفاتورة
                $data->finalbill = $BillsData->finalnetbillvalue; //إجمالى المستحق
                $data->totalOperationCost = $BillsData->finalnetbillvalue;
            } else if ($data->tablename == "billsreturn.php") {
                $data->link = "billsreturn.php?do=details&id=" . $data->clientdebtchangemodelid;
                $BillsData = $billsDAO->load($data->clientdebtchangemodelid);
                $data->totalbill = $BillsData->productstotalprice; // إجمالي الفاتورة
                $data->finalbill = $BillsData->finalnetbillvalue; //إجمالى المستحق
                $data->totalOperationCost = $BillsData->finalnetbillvalue;
            } else if ($data->tablename == "kempialaController.php") {
                $data->link = "kempialaController.php?do=editprint&id=" . $data->clientdebtchangemodelid;
            } else if ($data->tablename == "depositcheckController.php") {
                $data->link = "depositcheckController.php?do=editprint&id=" . $data->clientdebtchangemodelid;
                $id = $data->clientdebtchangemodelid;
                $checkdata = $CheckdepositEX->loadEX($id);
                $data->bankname = $checkdata->bankname;
                $data->accountname = $checkdata->accountname;
            } else if ($data->tablename == "clientPayedDeptController.php") {
                $data->link = "clientPayedDeptController.php?do=editprint&id=" . $data->clientdebtchangeid;
            } else if ($data->tablename == "clientPayedDeptReportController.php") {
                $data->link = "clientPayedDeptController.php?do=editprint&id=" . $data->clientdebtchangeid;
            } else if ($data->tablename == "clientController.php") {
                $data->link = "clientController.php?do=editprint&id=" . $data->clientdebtchangemodelid;
            } else if ($data->tablename == "billreceiptController.php") {
                $data->link = "billreceiptController.php?do=editprint&id=" . $data->clientdebtchangemodelid;
            } else if ($data->tablename == "rb_kashf.php") {
                $data->link = "rb_kashf.php?do=editprint&id=" . $data->clientdebtchangemodelid;
            }else if ($data->tablename == "premiumController.php") {
                $data->link = "premiumController.php?do=details&id=" . $data->clientdebtchangemodelid;
            }
            //else
            //{
            ///print($data->tablename." ");
            //}
        } else {
            $data->link = "#";
        }
    }



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

    if ($bure == "1") {



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

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

            if ($type == 1) {

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

                foreach ($shownData as $xx) {

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



        $ii = 0;
        //select all data from storemovement
        $startvalue = 0;
        $startbefore = 0;
        $startafter = 0;
        foreach ($shownData as $mov) {


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


                if ($ii == 0) {

                    $startvalue = $mov->clientdebtchangeamount;
                    $mov->clientdebtchangeamount = $startvalue;
                    $startbefore = $mov->clientdebtchangebefore;
                    $mov->clientdebtchangebefore = $startbefore;
                    $startafter = $mov->clientdebtchangeafter;
                    $mov->clientdebtchangeafter = $startafter;
                } else {

                    $startvalue = $mov->clientdebtchangeamount;
                    $mov->clientdebtchangeafter = $startbefore;

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

                        //print_r('in if<br>');
                        $mov->clientdebtchangebefore = $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->clientdebtchangebefore = $startbefore - $startvalue;
                        $startbefore = $startbefore - $startvalue;
                    }


                    //new product name with all path
                }





                $ii++;
            }
        }
    }







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

}


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


    $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>");
    }
    
    if($order == 1){
        $orderBy = " order by supplierdebtchangedate desc ";
    }else{
        $orderBy = " order by supplierdebtchangeid desc ";   
    }

    // get the all colums number
    $shownData = $supplierDeptChangeExt->queryBySupplierIdAndDate2($queryString, $orderBy);
//    var_dump($shownData);
    $i = 1;
    foreach ($shownData as $data) {
       
        if ($data->tablename == "supplierPayedDeptController.php" && $data->del == 0) {
            $data->totalbillvalue = $data->supplierdebtchangeamount; //$data->totalbillvalue;
        }
        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 = abs($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 = abs($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;

                $id = $data->supplierdebtchangemodelid;
                $checkdata = $checkWithdrawalExtDAO->queryByIdExt($id);
                $data->bankname = $checkdata->bankname;
                $data->accountname = $checkdata->accountname;
            } elseif ($data->tablename == "checkwithdrawalController.php") {
                $data->totalbillvalue = $data->supplierdebtchangeamount;
                $data->buybillpaid = $data->supplierdebtchangeamount;
                $data->link = "checkwithdrawalController.php?do=editprint&id=" . $data->supplierdebtchangemodelid;
            } elseif ($data->tablename == "supplierController.php") {
                $data->totalbillvalue = $data->supplierdebtchangeamount;
                $data->buybillpaid = $data->supplierdebtchangeamount;
                $data->link = "supplierController.php?do=editprint&id=" . $data->supplierdebtchangemodelid;
            } elseif ($data->tablename == "supplierPayedDeptController.php") {
                $data->totalbillvalue = $data->supplierdebtchangeamount;
                $data->buybillpaid = $data->supplierdebtchangeamount;
                $data->link = "supplierReportsController.php?do=editprint&id=" . $data->supplierdebtchangeid;
            }
        } else {
            $data->totalbillvalue = $data->supplierdebtchangeamount;
            $data->buybillpaid = $data->supplierdebtchangeamount;
            $data->link = "#";
        }
        $i++;
    }

    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++;
        }
    }


    $smarty->assign('shownData2', $shownData);
}



?>