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

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

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

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

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
//Client
$client = new Client();
$clientDAO = new ClientMySqlDAO();
$clientExt = new ClientMySqlExtDAO();
//Clientdebtchange
$clientDeptChange = new Clientdebtchange();
$clientDeptChangeDAO = new ClientdebtchangeMySqlDAO();
$clientDeptChangeExt = new ClientdebtchangeMySqlExtDAO();
//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();
//Clientarea
$Clientarea = new Clientarea();
$ClientareaDAO = new ClientareaMySqlDAO();
$ClientareaEX = new ClientareaMySqlExtDAO();
$today = date("Y-m-d");
//bill
$bills = new Bill();
$billsDAO = new BillsMySqlDAO();
$billsEX = new BillsMySqlExtDAO();

//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) || $do == "clientShow2") { // تقرير لعميل معين
    include_once("../public/authentication.php");

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

    $clientid = $_REQUEST['clientid'];

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

    if (!empty($clientid) && $clientid != '-1' || !empty($startDate) && !empty($endDate) && $startDate != "" && $endDate != "") {
        $messageData = $clientDAO->load($clientid);
        $message = "كشف حساب العميل: " . $messageData->clientname;
        $message .= "<br>الدين الحالى: " . $messageData->clientdebt;
        if (!empty($startDate) && !empty($endDate) && $startDate != "" && $endDate != "") {
            $message .= "<br>التاريخ: من: " . $startDate . " إلى: " . $endDate;
        }
        $smarty->assign("message", $message);

        clientShow2($clientid, $startDate, $endDate);
    }


    $smarty->display("clientReportsview/clientShow22.html");
} else if ($do == "clientShowbure") { // تقرير لعميل معين
    include_once("../public/authentication.php");

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

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

    $clientid = $_REQUEST['clientid'];

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

    if (!empty($clientid) && $clientid != '-1' || !empty($startDate) && !empty($endDate) && $startDate != "" && $endDate != "") {
        $messageData = $clientDAO->load($clientid);
        $message = "كشف حساب السيد : " . $messageData->clientname;
        $message .= "<br>الدين الحالى: " . $messageData->clientdebt;
        if (!empty($startDate) && !empty($endDate) && $startDate != "" && $endDate != "") {
            $message .= "<br>التاريخ: من: " . $startDate . " إلى: " . $endDate;
        }
        $smarty->assign("message", $message);

        clientShowbure($clientid, $startDate, $endDate);
    }

    $smarty->display("clientReportsview/clientShowbure.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 clientShow2($clientid, $startDate, $endDate) {


    global $billsDAO;
    global $billsReturnDAO;
    global $clientDeptChangeExt;
    global $sellBillDAO;
    global $returnSellBillDAO;
    global $smarty;
    global $mySellbillandruternRecord;
    $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>");
    }

    $shownData = $clientDeptChangeExt->queryByClientIdAndDatenew($queryString);



    $totalsum = 0;

    foreach ($shownData as $data) {


        // إجمالي الفاتورة و إجمالى المستحق من فاتورة المبيعات
        if ($data->tablename == "sellbillController.php") {
            //print("d");
            $data->link = "sellbillController.php?do=showDetail&sellbillid=" . $data->clientdebtchangemodelid;
            $sellBillData = $sellBillDAO->load($data->clientdebtchangemodelid);

            $data->billid = $sellBillData->sellbillid;
            $data->totalbill = $sellBillData->sellbilltotalbill; // إجمالي الفاتورة
            $data->finalbill = $sellBillData->sellbillaftertotalbill; //إجمالى المستحق
            $data->discount = ($sellBillData->sellbilltotalbill - $sellBillData->sellbillaftertotalbill);

            $data->totalOperationCost = $sellBillData->sellbilltotalbill + $data->clientdebtchangebefore;
            $data->discards = 0;
            $data->payedamount = $sellBillData->sellbilltotalpayed;
            $data->clientdebtchangeamount = $sellBillData->sellbilltotalpayed;
            $data->payedamount = $sellBillData->sellbilltotalpayed;
        } else if ($data->tablename == "returnsellbillController.php") {
            $data->link = "returnsellbillController.php?do=showDetail&returnsellbillid=" . $data->clientdebtchangemodelid;
            $returnSellBillData = $returnSellBillDAO->load($data->clientdebtchangemodelid);
            $data->totalbill = 0; // إجمالي الفاتورة
            $data->billid = $returnSellBillData->returnsellbillid;

            $data->finalbill = $returnSellBillData->returnsellbillaftertotalbill; //إجمالى المستحق
            $data->totalOperationCost = 0 + $data->clientdebtchangebefore; //إجمالى المستحق
            $data->discards = ($returnSellBillData->returnsellbillaftertotalbill);
            $data->discount = ($returnSellBillData->sellbilltotalbill - $returnSellBillData->sellbillaftertotalbill);

            $data->payedamount = $returnSellBillData->returnsellbilltotalpayed * -1;
        } else if ($data->tablename == "sellbillandruternController.php") {
            $data->link = "sellbillandruternController.php?do=showDetail&sellbillid=" . $data->clientdebtchangemodelid;

            $sellAndReturnBillData = $mySellbillandruternRecord->load($data->clientdebtchangemodelid);
            $data->totalbill = $sellAndReturnBillData->sellbillprice;
            $data->billid = $sellAndReturnBillData->sellbillid;

            $data->totalOperationCost = $sellAndReturnBillData->sellbillprice + $data->clientdebtchangebefore;
            $data->discount = ($sellAndReturnBillData->sellbilltotalbill - $sellAndReturnBillData->sellbillaftertotalbill);
            $data->discards = ($sellAndReturnBillData->returnsellbillprice);
            $data->payedamount = $sellAndReturnBillData->sellbilltotalpayed;
        } else if ($data->tablename == "kempialaController.php") {
            $data->link = "kempialaController.php?do=editprint&id=" . $data->clientdebtchangemodelid;
            $data->discards = 0;
            $data->totalbill = 0;
            $data->totalOperationCost = $data->clientdebtchangebefore;
            $data->discount = 0;
            $data->payedamount = $data->clientdebtchangeamount;
        } else if ($data->tablename == "depositcheckController.php") {
            $data->link = "depositcheckController.php?do=editprint&id=" . $data->clientdebtchangemodelid;
            $data->discards = 0;

            $data->totalbill = 0;
            $data->totalOperationCost = $data->clientdebtchangebefore;
            $data->discount = 0;
            $data->payedamount = $data->clientdebtchangeamount;
        } else if ($data->tablename == "clientPayedDeptController.php") {
            $data->link = "clientPayedDeptController.php?do=editprint&id=" . $data->clientdebtchangeid;

            $data->discards = 0;

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

            if ($data->clientdebtchangetype == 1) {
                $data->payedamount = $data->clientdebtchangeamount;
            } else {
                $data->payedamount = $data->clientdebtchangeamount;
            }
        } else if ($data->tablename == "clientController.php") {
            $data->link = "clientController.php?do=editprint&id=" . $data->clientdebtchangemodelid;
            $data->discards = 0;
            $data->totalOperationCost = $data->clientdebtchangeamount;
            $data->clientdebtchangeamount = 0;
            $data->totalbill = 0;
            $data->payedamount = 0;
        } else if ($data->tablename == "billreceiptController.php") {
            $data->link = "billreceiptController.php?do=editprint&id=" . $data->clientdebtchangemodelid;
            $data->discards = 0;
        } else if ($data->tablename == "clientDeficitController.php") {
            $data->link = "clientDeficitController.php?do=editprint&id=" . $data->clientdebtchangemodelid;



            $data->discards = 0;
            $data->totalOperationCost = 0;

            $data->totalbill = 0;
            if ($data->clientdebtchangetype == 1) {
                $data->payedamount = $data->clientdebtchangeamount;
            } else {
                $data->payedamount = $data->clientdebtchangeamount;
            }
            $data->clientdebtchangeamount = 0;
        } else if ($data->tablename == "bills.php") {


            $data->link = "bills.php?do=details&id=" . $data->clientdebtchangemodelid;

            $billsData = $billsDAO->load($data->clientdebtchangemodelid);



            $data->billid = $billsData->billno;
            $data->totalbill = $billsData->productstotalprice; // إجمالي الفاتورة
            $data->finalbill = $billsData->finalnetbillvalue; //إجمالى الفاتورة بعد الخصمم
            $data->discount = ($billsData->productstotalprice - $billsData->finalnetbillvalue); // المبلغ المخصوم

            $data->totalOperationCost = $billsData->productstotalprice + $data->clientdebtchangebefore;
            $data->discards = 0;
            $data->payedamount = $billsData->companycarry + $billsData->cardvalue + $billsData->cashvalue;
            //$data->clientdebtchangeamount =  $sellBillData->sellbilltotalpayed;
            //$data->payedamount = $billsData->cashvalue;
        } else if ($data->tablename == "billsreturn.php") {



            $billsData = $billsDAO->load($data->clientdebtchangemodelid);
            $billsReturnData = $billsReturnDAO->queryByBillid($data->clientdebtchangemodelid);

            $data->link = "billsreturn.php?do=details&id=" . $billsReturnData[0]->id;

            $data->billid = $billsData->billno;

            $data->totalbill = 0; // إجمالي الفاتورة
            $data->finalbill = $billsData->finalnetbillvalue; //إجمالى الفاتورة بعد الخصمم
            $data->discount = 0; // المبلغ المخصوم

            $data->totalOperationCost = $billsData->productstotalprice + $data->clientdebtchangebefore;
            $data->discards = $billsReturnData[0]->returnedprice;
            $data->payedamount = -1 * ($billsReturnData[0]->clientreceivevalue);

            $data->clientdebtchangeafter = ($data->totalOperationCost + $data->payedamount) - $data->discards;
            //$data->clientdebtchangeamount =  $sellBillData->sellbilltotalpayed;
            //$data->payedamount = $billsData->cashvalue;
        } else if ($data->tablename == "clientPayedDeptReportController.php") {


            $data->billid = '';

            //$data->clientdebtchangeamount =  $sellBillData->sellbilltotalpayed;
            //$data->payedamount = $billsData->cashvalue;
        }








        $totalsum += $data->totalOperationCost;
    }


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

    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 clientShowbure($clientid, $startDate, $endDate) {

    global $clientDeptChangeExt;
    global $sellBillDAO;
    global $returnSellBillDAO;
    global $smarty;
    global $mySellbillandruternRecord;
    global $billsDAO;
    global $billsReturnDAO;
    $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>");
    }

    $shownData = $clientDeptChangeExt->queryByClientIdAndDatenew($queryString);


//     print_r("<pre>");
//      var_dump($shownData);
//      print_r("</pre>");


    $totalsum = 0;

    foreach ($shownData as $data) {


        // إجمالي الفاتورة و إجمالى المستحق من فاتورة المبيعات
        if ($data->tablename == "sellbillController.php") {
            //print("d");
            $data->link = "sellbillController.php?do=showDetail&sellbillid=" . $data->clientdebtchangemodelid;

            $sellBillData = $sellBillDAO->load($data->clientdebtchangemodelid);
            $data->billid = $sellBillData->sellbillid;

            $data->totalbill = $sellBillData->sellbilltotalbill; // إجمالي الفاتورة
            $data->finalbill = $sellBillData->sellbillaftertotalbill; //إجمالى المستحق
            $data->discount = ($sellBillData->sellbilltotalbill - $sellBillData->sellbillaftertotalbill);

            $data->totalOperationCost = $sellBillData->sellbilltotalbill + $data->clientdebtchangebefore;
            $data->discards = 0;
            $data->payedamount = $sellBillData->sellbilltotalpayed;
            //$data->clientdebtchangeamount =  $sellBillData->sellbilltotalpayed;
            $data->payedamount = $sellBillData->sellbilltotalpayed;
        } else if ($data->tablename == "returnsellbillController.php") {
            $data->link = "returnsellbillController.php?do=showDetail&returnsellbillid=" . $data->clientdebtchangemodelid;
            $returnSellBillData = $returnSellBillDAO->load($data->clientdebtchangemodelid);
            $data->totalbill = 0; // إجمالي الفاتورة
            $data->billid = $returnSellBillData->returnsellbillid;

            $data->finalbill = $returnSellBillData->returnsellbillaftertotalbill; //إجمالى المستحق
            $data->totalOperationCost = 0 + $data->clientdebtchangebefore; //إجمالى المستحق
            $data->discards = ($returnSellBillData->returnsellbillaftertotalbill);
            $data->discount = ($returnSellBillData->returnsellbilltotalbill - $returnSellBillData->returnsellbillaftertotalbill);

            $data->payedamount = $returnSellBillData->returnsellbilltotalpayed * -1;
        } else if ($data->tablename == "sellbillandruternController.php") {
            $data->link = "sellbillandruternController.php?do=showDetail&sellbillid=" . $data->clientdebtchangemodelid;

            $sellAndReturnBillData = $mySellbillandruternRecord->load($data->clientdebtchangemodelid);
            $data->totalbill = $sellAndReturnBillData->sellbillprice;
            $data->billid = $sellAndReturnBillData->sellbillid;

            $data->totalOperationCost = $sellAndReturnBillData->sellbillprice + $data->clientdebtchangebefore;
            $data->discount = ($sellAndReturnBillData->sellbilltotalbill - $sellAndReturnBillData->sellbillaftertotalbill);
            $data->discards = ($sellAndReturnBillData->returnsellbillprice);

            if ($sellAndReturnBillData->sellbillaftertotalbill < 0) {
                $data->payedamount = $sellAndReturnBillData->sellbilltotalpayed * -1;
            } else {

                $data->payedamount = $sellAndReturnBillData->sellbilltotalpayed;
            }
        } else if ($data->tablename == "kempialaController.php") {
            $data->link = "kempialaController.php?do=editprint&id=" . $data->clientdebtchangemodelid;
            $data->discards = 0;
            $data->totalbill = 0;
            $data->totalOperationCost = $data->clientdebtchangebefore;
            $data->discount = 0;
            $data->payedamount = $data->clientdebtchangeamount;
        } else if ($data->tablename == "depositcheckController.php") {
            $data->link = "depositcheckController.php?do=editprint&id=" . $data->clientdebtchangemodelid;
            $data->discards = 0;

            $data->totalbill = 0;
            $data->totalOperationCost = $data->clientdebtchangebefore;
            $data->discount = 0;
            $data->payedamount = $data->clientdebtchangeamount;
        } else if ($data->tablename == "clientPayedDeptController.php") {
            $data->link = "clientPayedDeptController.php?do=editprint&id=" . $data->clientdebtchangeid;

            $data->discards = 0;

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

            if ($data->clientdebtchangetype == 1) {
                $data->payedamount = $data->clientdebtchangeamount;
            } else {
                $data->payedamount = $data->clientdebtchangeamount;
            }
        } else if ($data->tablename == "clientController.php") {
            $data->link = "clientController.php?do=editprint&id=" . $data->clientdebtchangemodelid;
            $data->discards = 0;
            $data->totalOperationCost = $data->clientdebtchangeamount;
            // $data->clientdebtchangeamount = 0;
            $data->totalbill = 0;
            $data->payedamount = 0;
        } else if ($data->tablename == "billreceiptController.php") {
            $data->link = "billreceiptController.php?do=editprint&id=" . $data->clientdebtchangemodelid;
            $data->discards = 0;
        } else if ($data->tablename == "clientDeficitController.php") {
            $data->link = "clientDeficitController.php?do=editprint&id=" . $data->clientdebtchangeid;
            $data->discards = 0;
            $data->totalOperationCost = 0;

            $data->totalbill = 0;
            if ($data->clientdebtchangetype == 1) {
                $data->payedamount = $data->clientdebtchangeamount;
            } else {
                $data->payedamount = $data->clientdebtchangeamount;
            }
            //$data->clientdebtchangeamount = 0;
        } else if ($data->tablename == "bills.php") {


            $data->link = "bills.php?do=details&id=" . $data->clientdebtchangemodelid;

            $billsData = $billsDAO->load($data->clientdebtchangemodelid);


            $data->billid = $billsData->billno;
            $data->totalbill = $billsData->productstotalprice; // إجمالي الفاتورة
            $data->finalbill = $billsData->finalnetbillvalue; //إجمالى الفاتورة بعد الخصمم
            $data->discount = ($billsData->productstotalprice - $billsData->finalnetbillvalue); // المبلغ المخصوم

            $data->totalOperationCost = $billsData->productstotalprice + $data->clientdebtchangebefore;
            $data->discards = 0;
            $data->payedamount = $billsData->companycarry + $billsData->cardvalue + $billsData->cashvalue;
            //$data->clientdebtchangeamount =  $sellBillData->sellbilltotalpayed;
            //$data->payedamount = $billsData->cashvalue;
        } else if ($data->tablename == "billsreturn.php") {



            $billsData = $billsDAO->load($data->clientdebtchangemodelid);
            $billsReturnData = $billsReturnDAO->queryByBillid($data->clientdebtchangemodelid);

            $data->link = "billsreturn.php?do=details&id=" . $billsReturnData[0]->id;

            $data->billid = $billsData->billno;

            $data->totalbill = 0; // إجمالي الفاتورة
            $data->finalbill = $billsData->finalnetbillvalue; //إجمالى الفاتورة بعد الخصمم
            $data->discount = 0; // المبلغ المخصوم

            $data->totalOperationCost = $billsData->productstotalprice + $data->clientdebtchangebefore;
            $data->discards = $billsReturnData[0]->returnedprice;
            $data->payedamount = -1 * ($billsReturnData[0]->clientreceivevalue);

            $data->clientdebtchangeafter = ($data->totalOperationCost + $data->payedamount) - $data->discards;
            //$data->clientdebtchangeamount =  $sellBillData->sellbilltotalpayed;
            //$data->payedamount = $billsData->cashvalue;
        } else if ($data->tablename == "clientPayedDeptReportController.php") {


            $data->link = 'clientPayedDeptController.php?do=editprint&id=' . $data->clientdebtchangeid . '';
        } 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;
        }





        $totalsum += $data->totalOperationCost;
//
    }


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

    $bure = '1';
    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) {
                $mov->processname = str_replace("(معدله)", "", $mov->processname);

                if ($ii == 0) {
                    $startvalue = $mov->clientdebtchangeamount;
                    $mov->clientdebtchangeamount = $startvalue;
                    $startbefore = 0;
                    $mov->clientdebtchangebefore = $startbefore;
                    $startafter = $startvalue;
                    $mov->clientdebtchangeafter = $startafter;
                    $startbefore = $startvalue;
                } else {

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

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

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


                    //new product name with all path
                }




                $ii++;
            }
        }
		
//                print_r('<pre>');
//    foreach ($shownData as $value) {
//                        print_r('<br>clientdebtchangeafter = '.$value->clientdebtchangeafter);
//
//    }
//
//
//        print_r('<pre>');
    }

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