<?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");
include_once("../library/num_to_ar.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');
//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');

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

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 :- clientPayedDeptReportController

  OPERTATION in Controller

  1- تقرير سداد ديون عميل بصاريات

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

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

//here goes the instances and general variables
//Programsetting
$ProgramsettingDAO = new ProgramsettingsMySqlDAO();
//Sellbill
$sellBill = new Sellbill();
$sellBillDAO = new SellbillMySqlDAO();
$sellBillEX = new SellbillMySqlExtDAO();
//Sellbilldetail
$sellBillDetail = new Sellbilldetail();
$sellBillDetailDAO = new SellbilldetailMySqlDAO();
$sellBillDetailEX = new SellbilldetailMySqlExtDAO();
//Returnsellbill
$myReturnsellbill = new Returnsellbill();
$myReturnsellbillRecord = new ReturnsellbillMySqlDAO();
$myReturnsellbillEx = new ReturnsellbillMySqlExtDAO();

//Returnsellbilldetail
$myReturnsellbilldetail = new Returnsellbilldetail();
$myReturnsellbilldetailRecord = new ReturnsellbilldetailMySqlDAO();
$myReturnsellbilldetailEx = new ReturnsellbilldetailMySqlExtDAO();
//Client
$client = new Client();
$clientDAO = new ClientMySqlDAO();
$clientExt = new ClientMySqlExtDAO();

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


//Clientdebtchange
$clientDeptChange = new Clientdebtchange();
$clientDeptChangeDAO = new ClientdebtchangeMySqlDAO();
$clientDeptChangeExt = new ClientdebtchangeMySqlExtDAO();

$Programsettingdata = $Programsetting = $ProgramsettingDAO->load(1);
$smarty->assign("Programsettingdata", $Programsettingdata);
$today = date("Y-m-d");
if (empty($do)) {// السداد على فاتورة بيع للعميل
    //here the permssion check
    include_once("../public/authentication.php");


    $start_date = $_REQUEST['from'];
    $end_date = $_REQUEST['to'];
    $client = $_POST['client'];
    $bill_no = $_REQUEST['bill_no'];
    $obgyBillType = (int) $_POST['obgyBillType'];

    $queryString = " where 1 ";
    $result_string = '';
    if ((isset($client) && $client != "-1" && $client != '')) {
        $queryString .= ' and sellbill.sellbillclientid = ' . $client . ' ';


        $messageData = $clientDAO->load($client);
        $result_string .= " اسم العميل : " . $messageData->clientname;
        $smarty->assign("client_id", $messageData->clientid);
    }
    if ((isset($bill_no) && $bill_no != "")) {
        $queryString .= ' and sellbill.sellbillid	 = ' . $bill_no . ' ';
    }

    if (isset($start_date) && $start_date != "" && isset($end_date) && $end_date != "") {
        $queryString .= ' and  date(sellbill.sellbilldate) >= "' . $start_date . '" and date(sellbill.sellbilldate) <= "' . $end_date . '" ';
        $result_string .= " التاريخ: من   :  " . $start_date . " إلى: " . $end_date;
    }
    if ((isset($obgyBillType) && !empty($obgyBillType))) {
        $queryString .= ' and sellbill.obgyBillType = ' . $obgyBillType . ' ';
    }


    if ($queryString != " where 1 ") {

        $message = $result_string;
        $smarty->assign("message", $message);

        $queryString .= ' and sellbill.conditions = 0 ';
        //////$sellbillData = $sellBillEX->queryallWithOrder($queryString);
        ##getting bills
        $sellbillData = R::getAll('SELECT sellbillid,sellbilldate,pricetype,sellbilltotalpayed,sellbillfinalbill,sellbillaftertotalbill,conditions,
                                   userid,sellbillclientid
                                   FROM sellbill ' . $queryString . ' ORDER BY  `sellbilldate`  desc ,sellbillid desc ');

        list($billIDs, $userIDs, $clientIDs) = [[], [], []];
        foreach ($sellbillData as $value) {
            $billIDs[] = $value['sellbillid'];
            $userIDs[] = $value['userid'];
            $clientIDs[] = $value['sellbillclientid'];
        }
        $billIDs = !empty($billIDs) ? array_unique($billIDs) : [0];
        $userIDs = !empty($userIDs) ? array_unique($userIDs) : [0];
        $clientIDs = !empty($clientIDs) ? array_unique($clientIDs) : [0];
        ##2-user data
        $userDataArr = R::getAll('select userid,employeename from user where userid in(' . implode(',', $userIDs) . ') ');
        $userDataArr = customArrayIndexOne($userDataArr, 'userid');
        ##3-client
        $clientDataArr = R::getAll('select clientid,clientname from client where clientid in(' . implode(',', $clientIDs) . ') ');
        $clientDataArr = customArrayIndexOne($clientDataArr, 'clientid');
        ##4-bill Pays data
        $billPaysDataArr = R::getAll('SELECT billid , sum(clientdebtchangeamount) as clientdebtchangeamount FROM clientdebtchange WHERE `billid`in(' . implode(',', $billIDs) . ') and tablename = "clientPayedDeptSellBillsController.php" group by billid ');
        $billPaysDataArr = customArrayIndexOne($billPaysDataArr, 'billid');

        $client_bills = array();
        foreach ($sellbillData as $value) {

            $value['useremployeename'] = $userDataArr[$value['userid']]['employeename'];
            $value['clientname'] = $clientDataArr[$value['sellbillclientid']]['clientname'];

            $client_dept_change_amount = $billPaysDataArr[$value['sellbillid']]['clientdebtchangeamount'];
            $waitvalue = $value['sellbillfinalbill'] - $client_dept_change_amount; //# قيمة الدين
            if ($waitvalue > 0) {
                $client_bills[] = array(
                    'id' => $value['sellbillid'],
                    'billno' => $value['sellbillid'],
                    'finalnetbillvalue' => $value['sellbillaftertotalbill'],
                    'waitvalue' => $waitvalue,
                    'billDate' => $value['sellbilldate'],
                    'userName' => $value['useremployeename'],
                    'clientName' => $value['clientname'],
                    'total_payed' => $value['sellbilltotalpayed'] + $client_dept_change_amount//check payed, save payed come from clientdebtchangeamount //sellbilltotalpayed is total of payed cash and visa payed of bill
                );
            }
        }

        $smarty->assign("client_bills", $client_bills);
    }


    $smarty->assign("customClientDeptChangeReportSellbill", 1);
    $smarty->display("clientPayedDeptSellBillsView/add.html");
} elseif ($do == "show") {

    //here the permssion check
    include_once("../public/authentication.php");


    $start_date = $_REQUEST['from'];
    $end_date = $_REQUEST['to'];
    $client = $_POST['client'];
    $bill_id = $_POST['bill_no'];

    $where = '';
    if ((isset($client) && $client != "-1" && $client != '')) {
        $where .= 'and clientid = ' . $client . ' ';
    }
    if ((isset($bill_id) && $bill_id != "")) {
        //$bill_data = $billsDAO->load($bill_id);
        //$bill_id = $bill_data->billno;
        $where .= ' and billid = ' . $bill_id . ' ';
    }

    if (isset($start_date) && $start_date != "" && isset($end_date) && $end_date != "") {
        $where .= ' and  date(clientdebtchangedate) >= "' . $start_date . '" and  date(clientdebtchangedate) <= "' . $end_date . '" ';
    }


    if ($where != '') {
        //$client_payed_bills = $clientDeptChangeExt->selectAllPayedBillsAndControl($where, 'clientPayedDeptSellBillsController.php');
        $client_payed_bills = R::getAll('SELECT distinct `billid` FROM `clientdebtchange` where `billid` != "" ' . $where . ' and tablename = "clientPayedDeptSellBillsController.php" order by `billid`');
        list($billIDs) = [[]];
        foreach ($client_payed_bills as $value) {
            $billIDs[] = $value['billid'];
        }
        $billIDs = !empty($billIDs) ? array_unique($billIDs) : [0];
        ##2-bill Pays data
        $billPaysDataArr = R::getAll('SELECT billid , sum(clientdebtchangeamount) as clientdebtchangeamount FROM clientdebtchange WHERE `billid`in(' . implode(',', $billIDs) . ') and tablename = "clientPayedDeptSellBillsController.php" group by billid ');
        $billPaysDataArr = customArrayIndexOne($billPaysDataArr, 'billid');
        ##3-getting bills
        $sellbillData = R::getAll('SELECT sellbillid,sellbilltotalpayed,sellbillfinalbill,sellbillaftertotalbill
                                   FROM sellbill where sellbillid in(' . implode(',', $billIDs) . ')'); //. ' ORDER BY sellbilldate desc ,sellbillid desc '
        $sellbillData = customArrayIndexOne($sellbillData, 'sellbillid');


        $client_bills = array();
        $total_bills = $total_wait_bills = $total_payed_bills = 0;

        foreach ($client_payed_bills as $cli_payed_bill) {
            $client_dept_change_amount = $billPaysDataArr[$cli_payed_bill['billid']]['clientdebtchangeamount'];
            $sellbillfinalbill = $sellbillData[$cli_payed_bill['billid']]['sellbillfinalbill'];
            $sellbillaftertotalbill = $sellbillData[$cli_payed_bill['billid']]['sellbillaftertotalbill'];
            $sellbilltotalpayed = $sellbillData[$cli_payed_bill['billid']]['sellbilltotalpayed'];

            $waitvalue = $sellbillfinalbill - $client_dept_change_amount;

            //if( $waitvalue > 0){

            $client_bills[] = array(
                'id' => $cli_payed_bill['billid'],
                'billno' => $cli_payed_bill['billid'],
                'finalnetbillvalue' => $sellbillaftertotalbill,
                'waitvalue' => $waitvalue,
                'total_payed' => $sellbilltotalpayed + $client_dept_change_amount, //check payed, save payed come from clientdebtchangeamount //sellbilltotalpayed is total of payed cash and visa payed of bill
            );

            $total_bills += $sellbillaftertotalbill;
            $total_wait_bills += $waitvalue;
            $total_payed_bills += $sellbilltotalpayed + $client_dept_change_amount;
            //}
        }
    }


    $smarty->assign("total_bills", $total_bills);
    $smarty->assign("total_wait_bills", $total_wait_bills);
    $smarty->assign("total_payed_bills", $total_payed_bills);

    $smarty->assign("client_bills", $client_bills);

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

    $smarty->assign("customClientDeptChangeReportSellbill", 1);
    $smarty->display("clientPayedDeptSellBillsView/show.html");
} elseif ($do == "show_details" || $do == "editprint" || $do == "details") {



    $bill_no = $_GET['id'];

    $bill_data = $sellBillDAO->load($bill_no);

    //# bill data .
    $finalnetbillvalue = $bill_data->sellbillaftertotalbill;
    $waitvalue = $bill_data->sellbillfinalbill;
    $payed = $bill_data->sellbilltotalpayed;

    //# load client data .
    $clientid = $bill_data->sellbillclientid;
    $messageData = $clientDAO->load($clientid);

    $message = " اسم العميل : " . $messageData->clientname;
    $smarty->assign("message", $message);


    //# select all rows which belongs to this bill no.
    $client_dept_Bills = $clientDeptChangeExt->selectByBillIdAndControl($bill_no, 'clientPayedDeptSellBillsController.php');


    $client_bills = array();
    $x = 1;
    foreach ($client_dept_Bills as $cli_bill_val) {

        $clientdebtchangeamount = $cli_bill_val->clientdebtchangeamount;
        $paytype = $cli_bill_val->paytype;
        $date = $cli_bill_val->clientdebtchangedate;

        if ($x == 1) {
            $wait_before = $waitvalue;
            $wait_aftre = $wait_before - ( $clientdebtchangeamount ); //$wait_before - ( $payed + $clientdebtchangeamount );
        } else {
            $wait_before = $wait_aftre;

            $wait_aftre = $wait_before - $clientdebtchangeamount;
        }
        //if( $waitvalue > 0 ){
        $client_bills[] = array(
            'itra' => $x,
            'finalnetbillvalue' => $finalnetbillvalue,
            'wait_before' => $wait_before,
            'clientdebtchangeamount' => $clientdebtchangeamount,
            'wait_after' => $wait_aftre,
            'date' => $date,
            'paytype' => $paytype,
            'comment' => $cli_bill_val->comment,
            'clientdebtchangeid' => $cli_bill_val->clientdebtchangeid,
            'userName' => $cli_bill_val->employeename,
            'dailyentryid' => $cli_bill_val->dailyentryid,
        );

        $x++;
    }

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

    $smarty->assign("client_bills", $client_bills);

    $smarty->display("clientPayedDeptSellBillsView/show_details.html");
}
###
//مرتجعات دفعات
elseif ($do == "ret") {// السداد على فاتورة مردود بيع للعميل
    //here the permssion check
    include_once("../public/authentication.php");


    $start_date = $_REQUEST['from'];
    $end_date = $_REQUEST['to'];
    $client = $_POST['client'];
    $bill_no = $_POST['bill_no'];
    $obgyBillType = (int) $_POST['obgyBillType'];

    $queryString = " where 1 ";
    $result_string = '';
    if ((isset($client) && $client != "-1" && $client != '')) {
        $queryString .= ' and returnsellbill.returnsellbillclientid = ' . $client . ' ';


        $messageData = $clientDAO->load($client);
        $result_string .= " اسم العميل : " . $messageData->clientname;
        $smarty->assign("client_id", $messageData->clientid);
    }
    if ((isset($bill_no) && $bill_no != "")) {
        $queryString .= ' and returnsellbill.returnsellbillid	 = ' . $bill_no . ' ';
    }

    if (isset($start_date) && $start_date != "" && isset($end_date) && $end_date != "") {
        $queryString .= ' and  date(returnsellbill.returnsellbilldate) >= "' . $start_date . '" and date(returnsellbill.returnsellbilldate) <= "' . $end_date . '" ';
        $result_string .= " التاريخ: من   :  " . $start_date . " إلى: " . $end_date;
    }
    if ((isset($obgyBillType) && !empty($obgyBillType))) {
        $queryString .= ' and returnsellbill.obgyBillType = ' . $obgyBillType . ' ';
    }


    if ($queryString != " where 1 ") {

        $message = $result_string;
        $smarty->assign("message", $message);

        $queryString .= ' and returnsellbill.conditions = 0 ';
        //////$sellbillData = $sellBillEX->queryallWithOrder($queryString);
        ##getting bills
        $sellbillData = R::getAll('SELECT returnsellbillid as sellbillid,returnsellbilldate as sellbilldate,pricetype,returnsellbilltotalpayed as sellbilltotalpayed,returnsellbillfinalbill as sellbillfinalbill,returnsellbillaftertotalbill as sellbillaftertotalbill,conditions,
                                   userid,returnsellbillclientid as sellbillclientid
                                   FROM returnsellbill ' . $queryString . ' ORDER BY  `returnsellbilldate`  desc ,returnsellbillid desc ');

        list($billIDs, $userIDs, $clientIDs) = [[], [], []];
        foreach ($sellbillData as $value) {
            $billIDs[] = $value['sellbillid'];
            $userIDs[] = $value['userid'];
            $clientIDs[] = $value['sellbillclientid'];
        }
        $billIDs = !empty($billIDs) ? array_unique($billIDs) : [0];
        $userIDs = !empty($userIDs) ? array_unique($userIDs) : [0];
        $clientIDs = !empty($clientIDs) ? array_unique($clientIDs) : [0];
        ##2-user data
        $userDataArr = R::getAll('select userid,employeename from user where userid in(' . implode(',', $userIDs) . ') ');
        $userDataArr = customArrayIndexOne($userDataArr, 'userid');
        ##3-client
        $clientDataArr = R::getAll('select clientid,clientname from client where clientid in(' . implode(',', $clientIDs) . ') ');
        $clientDataArr = customArrayIndexOne($clientDataArr, 'clientid');
        ##4-bill Pays data
        $billPaysDataArr = R::getAll('SELECT billid , sum(clientdebtchangeamount) as clientdebtchangeamount FROM clientdebtchange WHERE `billid`in(' . implode(',', $billIDs) . ') and tablename = "clientPayedDeptReturnSellBillsController.php" group by billid ');
        $billPaysDataArr = customArrayIndexOne($billPaysDataArr, 'billid');

        $client_bills = array();
        foreach ($sellbillData as $value) {

            $value['useremployeename'] = $userDataArr[$value['userid']]['employeename'];
            $value['clientname'] = $clientDataArr[$value['sellbillclientid']]['clientname'];

            $client_dept_change_amount = $billPaysDataArr[$value['sellbillid']]['clientdebtchangeamount'];
            $waitvalue = $value['sellbillfinalbill'] + $client_dept_change_amount; //# قيمة الدين
            if ($waitvalue != 0) {
                $client_bills[] = array(
                    'id' => $value['sellbillid'],
                    'billno' => $value['sellbillid'],
                    'finalnetbillvalue' => $value['sellbillaftertotalbill'],
                    'waitvalue' => $waitvalue,
                    'billDate' => $value['sellbilldate'],
                    'userName' => $value['useremployeename'],
                    'clientName' => $value['clientname'],
                    'total_payed' => $value['sellbilltotalpayed'] + $client_dept_change_amount//check payed, save payed come from clientdebtchangeamount //sellbilltotalpayed is total of payed cash and visa payed of bill
                );
            }
        }

        $smarty->assign("client_bills", $client_bills);
    }

    $smarty->assign("customClientDeptChangeReportSellbill", 1);
    $smarty->display("clientPayedDeptSellBillsView/addret.html");
} elseif ($do == "showret") {

    //here the permssion check
    include_once("../public/authentication.php");


    $start_date = $_REQUEST['from'];
    $end_date = $_REQUEST['to'];
    $client = $_POST['client'];
    $bill_id = $_POST['bill_no'];

    $where = '';
    if ((isset($client) && $client != "-1" && $client != '')) {
        $where .= 'and clientid = ' . $client . ' ';
    }
    if ((isset($bill_id) && $bill_id != "")) {
        //$bill_data = $billsDAO->load($bill_id);
        //$bill_id = $bill_data->billno;
        $where .= ' and billid = ' . $bill_id . ' ';
    }

    if (isset($start_date) && $start_date != "" && isset($end_date) && $end_date != "") {
        $where .= ' and  date(clientdebtchangedate) >= "' . $start_date . '" and  date(clientdebtchangedate) <= "' . $end_date . '" ';
    }


    if ($where != '') {
        //$client_payed_bills = $clientDeptChangeExt->selectAllPayedBillsAndControl($where, 'clientPayedDeptReturnSellBillsController.php');
        $client_payed_bills = R::getAll('SELECT distinct `billid` FROM `clientdebtchange` where `billid` != "" ' . $where . ' and tablename = "clientPayedDeptReturnSellBillsController.php" order by `billid`');
        list($billIDs) = [[]];
        foreach ($client_payed_bills as $value) {
            $billIDs[] = $value['billid'];
        }
        $billIDs = !empty($billIDs) ? array_unique($billIDs) : [0];
        ##2-bill Pays data
        $billPaysDataArr = R::getAll('SELECT billid , sum(clientdebtchangeamount) as clientdebtchangeamount FROM clientdebtchange WHERE `billid`in(' . implode(',', $billIDs) . ') and tablename = "clientPayedDeptReturnSellBillsController.php" group by billid ');
        $billPaysDataArr = customArrayIndexOne($billPaysDataArr, 'billid');
        ##3-getting bills
        ##getting bills
        $sellbillData = R::getAll('SELECT returnsellbillid as sellbillid,returnsellbilldate as sellbilldate,pricetype,returnsellbilltotalpayed as sellbilltotalpayed,returnsellbillfinalbill as sellbillfinalbill,returnsellbillaftertotalbill as sellbillaftertotalbill,conditions,
                                   userid,returnsellbillclientid as sellbillclientid
                                   FROM returnsellbill where sellbillid in(' . implode(',', $billIDs) . ')'); //. ' ORDER BY sellbilldate desc ,sellbillid desc '
        $sellbillData = customArrayIndexOne($sellbillData, 'sellbillid');


        $client_bills = array();
        $total_bills = $total_wait_bills = $total_payed_bills = 0;

        foreach ($client_payed_bills as $cli_payed_bill) {
            $client_dept_change_amount = $billPaysDataArr[$cli_payed_bill['billid']]['clientdebtchangeamount'];
            $sellbillfinalbill = $sellbillData[$cli_payed_bill['billid']]['sellbillfinalbill'];
            $sellbillaftertotalbill = $sellbillData[$cli_payed_bill['billid']]['sellbillaftertotalbill'];
            $sellbilltotalpayed = $sellbillData[$cli_payed_bill['billid']]['sellbilltotalpayed'];

            $waitvalue = $sellbillfinalbill - $client_dept_change_amount;

            //if( $waitvalue > 0){

            $client_bills[] = array(
                'id' => $cli_payed_bill['billid'],
                'billno' => $cli_payed_bill['billid'],
                'finalnetbillvalue' => $sellbillaftertotalbill,
                'waitvalue' => $waitvalue,
                'total_payed' => $sellbilltotalpayed + $client_dept_change_amount, //check payed, save payed come from clientdebtchangeamount //sellbilltotalpayed is total of payed cash and visa payed of bill
            );

            $total_bills += $sellbillaftertotalbill;
            $total_wait_bills += $waitvalue;
            $total_payed_bills += $sellbilltotalpayed + $client_dept_change_amount;
            //}
        }
    }


    $smarty->assign("total_bills", $total_bills);
    $smarty->assign("total_wait_bills", $total_wait_bills);
    $smarty->assign("total_payed_bills", $total_payed_bills);

    $smarty->assign("client_bills", $client_bills);

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

    $smarty->assign("customClientDeptChangeReportSellbill", 1);
    $smarty->display("clientPayedDeptSellBillsView/showret.html");
} elseif ($do == "show_details_ret" || $do == "editprintret" || $do == "detailsret") {



    $bill_no = $_GET['id'];

    $bill_data = $myReturnsellbillRecord->load($bill_no);

    //# bill data .
    $finalnetbillvalue = $bill_data->returnsellbillaftertotalbill;
    $waitvalue = $bill_data->returnsellbillfinalbill;
    $payed = $bill_data->returnsellbilltotalpayed;

    //# load client data .
    $clientid = $bill_data->returnsellbillclientid;
    $messageData = $clientDAO->load($clientid);

    $message = " اسم العميل : " . $messageData->clientname;
    $smarty->assign("message", $message);


    //# select all rows which belongs to this bill no.
    $client_dept_Bills = $clientDeptChangeExt->selectByBillIdAndControl($bill_no, 'clientPayedDeptReturnSellBillsController.php');


    $client_bills = array();
    $x = 1;
    foreach ($client_dept_Bills as $cli_bill_val) {

        $clientdebtchangeamount = $cli_bill_val->clientdebtchangeamount;
        $paytype = $cli_bill_val->paytype;
        $date = $cli_bill_val->clientdebtchangedate;

        if ($x == 1) {
            $wait_before = $waitvalue;
            $wait_aftre = $wait_before + ( $clientdebtchangeamount ); //$wait_before - ( $payed + $clientdebtchangeamount );
        } else {
            $wait_before = $wait_aftre;

            $wait_aftre = $wait_before + $clientdebtchangeamount;
        }
        //if( $waitvalue > 0 ){
        $client_bills[] = array(
            'itra' => $x,
            'finalnetbillvalue' => $finalnetbillvalue,
            'wait_before' => $wait_before,
            'clientdebtchangeamount' => $clientdebtchangeamount,
            'wait_after' => $wait_aftre,
            'date' => $date,
            'paytype' => $paytype,
            'comment' => $cli_bill_val->comment,
            'clientdebtchangeid' => $cli_bill_val->clientdebtchangeid,
            'userName' => $cli_bill_val->employeename,
            'dailyentryid' => $cli_bill_val->dailyentryid,
        );

        $x++;
    }

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

    $smarty->assign("client_bills", $client_bills);

    $smarty->display("clientPayedDeptSellBillsView/show_details_ret.html");
}


##print
elseif ($do == "print") {
#
    $billId = (int) $_GET['bill'];
    $type = (int) $_GET['type']; //0 sell , 1 ret
    $moneyPayed = (float) $_GET['m'];
//    $choice = ($_GET['ch'] == "all") ? "all" : "current"; //choice of payment print
//    $oldPayments = (float) $_GET['op'];
//    if ($choice == "all") {
//        $moneyPayed += $oldPayments;
//    }
    $totalpaid = $moneyPayed;
    $ar_number = new convert_ar($totalpaid, "male");
    $totalWritten = $ar_number->convert_number();
    $totalWritten .= ' ' . $Programsettingdata->currancy . ' فقط لاغير';
    $smarty->assign('totalWritten', $totalWritten);

    R::debug(true);
    ##get client name
    if ($type == 0) {
        $billData = R::getRow('select sellbillclientid,sellbilldate from sellbill where sellbillid=' . $billId);
        $clientid = $billData['sellbillclientid'];
        $billDate = $billData['sellbilldate'];
    } elseif ($type == 1) {
        $billData = R::getRow('select returnsellbillclientid,returnsellbilldate from returnsellbill where returnsellbillid=' . $billId);
        $clientid = $billData['returnsellbillclientid'];
        $billDate = $billData['returnsellbilldate'];
    }

    if ($Programsettingdata->laterNakdiClientId == $clientid || $clientid == 1) {
        $clientName = "نقدا";
    } else {
        $clientName = R::getCell('select clientname from client where clientid=' . $clientid);
    }

    $smarty->assign("billId", $billId);
    $smarty->assign("moneyPayed", $moneyPayed);
    $smarty->assign("type", $type);
    $smarty->assign("clientName", $clientName);
    $smarty->assign("billDate", $billDate);

    $smarty->display("clientPayedDeptSellBillsView/printReceipt.html"); //from salesreportbillsview
}

//check and use the condition that suite this action
//here the global templates
$smarty->display("footer.html");
?>