HEX
Server: Apache
System: Linux server1.royalgt4.com 4.18.0-553.89.1.lve.el8.x86_64 #1 SMP Wed Dec 10 13:58:50 UTC 2025 x86_64
User: mostafedeg (1125)
PHP: 5.6.40
Disabled: mail,passthru,parse_ini_file,show_source,eval,assert,pcntl_exec,dl,putenv,proc_open,popen
Upload Files
File: /home/mostafedeg/public_html/erp/controllers/rentbill.php
<?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");
include_once("dailyentryfun.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');
//here the db files that include in the file
include("../public/include_dao.php");


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

$langs = $_SESSION['erp_lang'];
include_once("../views/languages/$langs/success.php");
include_once("../views/languages/$langs/error.php");

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

//here goes the instances and general variables
//Accountstree
$accountsTree = new Accountstree();
$accountsTreeDAO = new AccountstreeMySqlDAO();
$accountsTreeEX = new AccountstreeMySqlExtDAO();

//Supplierdebtchange
$Supplierdebtchange = new Supplierdebtchange();
$SupplierdebtchangeDAO = new SupplierdebtchangeMySqlDAO();
$SupplierdebtchangeEX = new SupplierdebtchangeMySqlExtDAO();

$myClientdebtchange = new Clientdebtchange();
$myClientdebtchangeRecord = new ClientdebtchangeMySqlDAO();

//Programsetting
$Programsetting = new Programsetting();
$ProgramsettingDAO = new ProgramsettingsMySqlDAO();
$ProgramsettingEX = new ProgramsettingsMySqlExtDAO();


//Dailyentry
$dailyEntry = new Dailyentry();
$dailyEntryDAO = new DailyentryMySqlDAO();
$dailyEntryEX = new DailyentryMySqlExtDAO();

//Dailyentrycreditor دائن
$dailyEntryCreditor = new Dailyentrycreditor();
$dailyEntryCreditorDAO = new DailyentrycreditorMySqlDAO();
$dailyEntryCreditorEX = new DailyentrycreditorMySqlExtDAO();

//Dailyentrycreditor2 دائن2
$dailyEntryCreditor2 = new Dailyentrycreditor();
$dailyEntryCreditorDAO2 = new DailyentrycreditorMySqlDAO();


//Dailyentrydebtor مدين
$dailyEntryDebtor = new Dailyentrydebtor();
$dailyEntryDebtorDAO = new DailyentrydebtorMySqlDAO();
$dailyEntryDebtorEX = new DailyentrydebtorMySqlExtDAO();

//Dailyentrydebtor2 مدين2
$dailyEntryDebtor2 = new Dailyentrydebtor();
$dailyEntryDebtorDAO2 = new DailyentrydebtorMySqlDAO();

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

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


/* * * classes ** */
$rentProducts = new Rentproduct();
$myrentProducts = new RentproductMySqlDAO();
$extrentProducts = new RentproductMySqlExtDAO();

$rentStore = new Rentstore();
$myrentStore = new RentstoreMySqlDAO();
$extrentStore = new RentstoreMySqlExtDAO();

$supplier = new Supplier();
$mysupplier = new SupplierMySqlDAO();
$extsupplier = new SupplierMySqlExtDAO();

$client = new Client();
$myclient = new ClientMySqlDAO();
$extclient = new ClientMySqlExtDAO();

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

$save = new Save();
$mysave = new SaveMySqlDAO();
$extsave = new SaveMySqlExtDAO();

$Savedaily = new Savedaily();
$SavedailyDAO = new SavedailyMySqlDAO();

$rentbill = new Rentbill();
$myrentbill = new RentbillMySqlDAO();
$extrentbill = new RentbillMySqlExtDAO();

$rentbillpro = new Rentbillprop();
$myrentbillpro = new RentbillpropMySqlDAO();
$extrentbillpro = new RentbillpropMySqlExtDAO();


$myBank = new Bank();
$myBankRecord = new BankMySqlDAO();
$myBankEx = new BankMySqlExtDAO();

$myBankaccount = new Bankaccount();
$myBankaccountRecord = new BankaccountMySqlDAO();
$myBankaccountEx = new BankaccountMySqlExtDAO();

//Accountmovement
$myAccountmovement = new Accountmovement();
$myAccountmovementRecord = new AccountmovementMySqlDAO();

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


//check and use the condition that suite this action
if (empty($do)) {
    //here the permssion check
    include_once("../public/authentication.php");

    $clients = $myclient->queryAll();
    $smarty->assign('clients', $clients);
    $products = $myrentProducts->queryAll();
    $smarty->assign('products', $products);
    $suppliers = $mysupplier->queryAll();
    $smarty->assign('suppliers', $suppliers);
    $smarty->assign('today', date("Y-m-d"));

    $allbanks = $myBankRecord->queryByCondition(0);
    $smarty->assign('allbanks', $allbanks);

    //here the smarty templates
    $smarty->display("rent/bill/add.html");
} elseif ($do == "add") {
    include_once("../public/authentication.php");

    //get vars
    $clientid = filter_input(INPUT_POST, 'clientid');
    $createdate = filter_input(INPUT_POST, 'createdate');
    $rentbilltotal = filter_input(INPUT_POST, 'hrentbilltotal');
    $genpay = filter_input(INPUT_POST, 'genpay');
    $reset = $rentbilltotal - $genpay;

    $discount = filter_input(INPUT_POST, 'discount');
    $distotal = filter_input(INPUT_POST, 'distotal');

    //PAY TYPE
    $payistype = filter_input(INPUT_POST, 'payistype'); // 0 FOR cash and 1 for bank
    $bankid = filter_input(INPUT_POST, 'bankid');
    $accountid = filter_input(INPUT_POST, 'accountid');
    $paperno = filter_input(INPUT_POST, 'paperno');
    $paperval = filter_input(INPUT_POST, 'paperval');
    $paperdate = filter_input(INPUT_POST, 'paperdate');


    //Transaction
    $mytransactions = new Transaction();
    try {

        $rentbill->clientid = $clientid;
        if ($distotal > 0) {
            $rentbill->billreset = $distotal - $genpay;
        } else {
            $rentbill->billreset = $rentbilltotal - $genpay;
        }
        $rentbill->billtotal = $rentbilltotal;
        $rentbill->createdatetime = $createdate;
        $rentbill->billpayed = $genpay;
        $rentbill->firmbilltotal = 0;
        $rentbill->dailyentryid = 0;

        $rentbill->payistype = $payistype;
        $rentbill->bankid = $bankid;
        $rentbill->accountid = $accountid;
        $rentbill->paperno = $paperno;
        $rentbill->paperval = $paperval;
        $rentbill->paperdate = $paperdate;
        $rentbill->discount = $discount;
        $rentbill->distotal = $distotal;


        $insertid = $myrentbill->insert($rentbill);

        $itrs = filter_input(INPUT_POST, 'hidden_itr');
        for ($i = 1; $i <= $itrs; $i++) {
            $productid = filter_input(INPUT_POST, 'productid' . $i);
            $renttype = filter_input(INPUT_POST, 'renttype' . $i);
            $supplierid = filter_input(INPUT_POST, 'supplierid' . $i);
            $buyprice = filter_input(INPUT_POST, 'buyprice' . $i);
            $hbuytotal = filter_input(INPUT_POST, 'hbuytotal' . $i);
            $hbbuytotal = filter_input(INPUT_POST, 'hbbuytotal' . $i);
            $amount = filter_input(INPUT_POST, 'amount' . $i);
            $rentprice = filter_input(INPUT_POST, 'rentprice' . $i);
            $hrenttotal = filter_input(INPUT_POST, 'hrenttotal' . $i);
            $startdate = filter_input(INPUT_POST, 'startdate' . $i);
            $enddate = filter_input(INPUT_POST, 'enddate' . $i);
            $hrentdays = filter_input(INPUT_POST, 'hrentdays' . $i);
            $hrentdaystotal = filter_input(INPUT_POST, 'hrentdaystotal' . $i);


            if (!$productid) {
                continue;
            }


            $rentbillpro->amount = $amount;
            $rentbillpro->billid = $insertid;
            $rentbillpro->delayreset = 0;
            $rentbillpro->delaytype = 0;
            $rentbillpro->delypay = 0;
            $rentbillpro->enddate = $enddate;
            $rentbillpro->productid = $productid;
            $rentbillpro->rentdays = $hrentdays;
            $rentbillpro->rentprice = $rentprice;
            $rentbillpro->startdate = $startdate;
            $rentbillpro->status = 0; //0 for new bill, 1 for delevierd product , 2 delay
            $rentbillpro->totalprice = $hrenttotal;
            $rentbillpro->type = $renttype;
            $rentbillpro->dailyentryid = 0;

            if ($renttype == 1) {
                //there are supplier
                $rentbillpro->supplierid = $supplierid;
                $rentbillpro->supplierprice = $buyprice;
                $rentbillpro->suppliertotalpice = $hbuytotal;

                // with supplier debt and supplier change
                //supplier
                #1 increase supplier debt
                $supplierinfo = $mysupplier->load($supplierid);
                $debtBefore = $supplierinfo->suppliercurrentDebt;
                $debtAfter = $debtBefore + $hbbuytotal;

                #2 update supplier debt
                $supplier->suppliercurrentDebt = $debtAfter;
                $supplier->userid = $_SESSION['userid'];
                $supplier->supplierdate = date("Y-m-d");
                $supplier->supplierid = $supplierid;
                $extsupplier->updatedept($supplier);

                #3 insert supplier debt change
                $Supplierdebtchange->comment = "";
                $Supplierdebtchange->processname = "تاجير منتجات خارجى من فاتوره الايجار ";
                $Supplierdebtchange->supplierdebtchangeafter = $debtAfter;
                $Supplierdebtchange->supplierdebtchangeamount = $hbbuytotal;
                $Supplierdebtchange->supplierdebtchangebefore = $debtBefore;
                $Supplierdebtchange->supplierdebtchangedate = date("Y-m-d");
                $Supplierdebtchange->supplierdebtchangemodelid = $insertid;
                $Supplierdebtchange->supplierid = $supplierid;
                $Supplierdebtchange->supplierdebtchangetype = 0;
                $Supplierdebtchange->tablename = "rentbill.php";
                $Supplierdebtchange->userid = $_SESSION['userid'];
                $Supplierdebtchange->dailyentryid = 0;

                $SupplierdebtchangeDAO->insert($Supplierdebtchange);
            } else {
                $rentbillpro->supplierid = 0;
                $rentbillpro->supplierprice = 0;
                $rentbillpro->suppliertotalpice = 0;
            }

            $rentBillProId = $myrentbillpro->insert($rentbillpro);


            //update with store
            //rent amount
            $getstore = $myrentStore->queryByRentproductid($productid);
            $rentStore->rented = $amount + $getstore[0]->rented;
            $rentStore->rentproductid = $productid;
            $extrentStore->updaterented($rentStore);
        }

        //check type
        if ($payistype == 0) { // pay cash
            //if cash or client
            if ($genpay > 0) {
                //cash
                #1 decrease save
                $saveinfo = $mysave->load($_SESSION['saveid']);
                $balanceBefore = $saveinfo->savecurrentvalue;
                $balanceAfter = $balanceBefore + $genpay;

                #2 update save
                $save->savecurrentvalue = $balanceAfter;
                $save->userid = $_SESSION['userid'];
                $save->saveid = $_SESSION['saveid'];
                $extsave->updateSaveValue($save);

                #3 update save report
                $Savedaily->savedailydate = date("Y-m-d");
                $Savedaily->userid = $_SESSION['userid'];
                $Savedaily->savedailysavebefore = $balanceBefore;
                $Savedaily->savedailychangeamount = $genpay;
                $Savedaily->savedailychangetype = 0; //0 for increase
                $Savedaily->saveid = $_SESSION['saveid'];
                $Savedaily->processname = 'اضافه فاتوره للايجار';
                $Savedaily->savedailymodelid = $insertid;
                $Savedaily->savedailysaveafter = $balanceAfter;
                $Savedaily->tablename = 'rentbill.php';

                $SavedailyDAO->insert($Savedaily);
            }

            if ($reset > 0) {
                //clients
                #1 increase client debt
                $clientsing = $myclient->load($clientid);
                $debtBefore = $clientsing->clientdebt;
                $debtAfter = $debtBefore + $reset;

                #2 update client debt
                $client->clientdebt = $debtAfter;
                $client->userid = $_SESSION['userid'];
                $client->clientdate = date("Y-m-d");
                $client->clientid = $clientid;
                $extclient->updatedept($client);

                #3 insert client debt change

                $myClientdebtchange->clientdebtchangeafter = $debtAfter;
                $myClientdebtchange->clientdebtchangeamount = $reset;
                $myClientdebtchange->clientdebtchangebefore = $debtBefore;
                $myClientdebtchange->clientdebtchangedate = date("Y-m-d");
                $myClientdebtchange->clientdebtchangemodelid = $insertid;
                $myClientdebtchange->clientdebtchangetype = 0;
                $myClientdebtchange->clientid = $clientid;
                $myClientdebtchange->comment = "اضافه فاتوره ايجار جديده";
                $myClientdebtchange->processname = "اضافه فاتوره ايجار جديده";
                $myClientdebtchange->tablename = "rentbill.php";
                $myClientdebtchange->userid = $_SESSION['userid'];
                $myClientdebtchange->totalOperationCost = $rentbilltotal;
                $myClientdebtchange->dailyentryid = $insertid;

                $myClientdebtchangeRecord->insert($myClientdebtchange);
            }
        } else {//pay bank
            //get account info
            $bankaccountinfo = $myBankaccountRecord->load($accountid);
            $accountbefore = $bankaccountinfo->accountbeginingbalance;
            $accountafter = $bankaccountinfo->accountbeginingbalance + $paperval;

            //update account balance
            $account->accountbeginingbalance = $accountafter;
            $account->accountdate = $today;
            $account->accountid = $accountid;
            $myBankaccountEx->updateacount($account);

            $checkwithdrawalnote = "";

            $Checkdeposit->bankaccountid = $accountid;
            $Checkdeposit->clientid = $clientid;
            $Checkdeposit->accountbeforevalue = $accountbefore;
            $Checkdeposit->checkdepositamount = $paperval;
            $Checkdeposit->checkdepositdate = $paperdate;
            $Checkdeposit->conditions = 0;
            $Checkdeposit->checkdepositnumber = $paperno;
            $Checkdeposit->checkdeposituser = $_SESSION['userid'];
            $Checkdeposit->userid = $_SESSION['userid'];
            $Checkdeposit->dailyentryid = 0;

            $Checkdepositid = $CheckdepositDAO->insert($Checkdeposit, $checkwithdrawalnote);


            $myAccountmovement->accountmovementbefore = $accountbefore;
            $myAccountmovement->accountid = $accountid;
            $myAccountmovement->bankid = $bankid;
            $myAccountmovement->accountmovementamount = $paperval;
            $myAccountmovement->accountmovementafter = $accountafter;
            $myAccountmovement->accountmovementtype = 0;
            $myAccountmovement->processname = "ايداع شيك";
            $myAccountmovement->tablename = "rentbill.php";
            $myAccountmovement->accountmovementmodelid = $Checkdepositid;
            $myAccountmovement->accountmovementdate = $today;
            $myAccountmovement->userid = $_SESSION['userid'];
            $myAccountmovementRecord->insert($myAccountmovement);
        }






        //  قيد اليومية
        $totalResidual = $reset; // المتبقى
        $totalpaid = $genpay; // المدفوع
        $totalCost = $hbuytotal; // اجمالى التكلفة
        $totalDif = ($totalResidual + $totalpaid) - $totalCost;  // ( المتبقى + المدفوع ) - اجمالى التكلفة

        $dailyEntry->entryComment = 'ايرادات ايجار';
        $dailyEntry->reverseofid = -9;

        $dailyEntryDebtorArray = array();
        $dailyEntryCreditorArray = array();

        // Client Deptor
        if ($totalResidual > 0) {
            $dailyEntryDebtor->value = $totalResidual;
            $oldclient = $myclient->load($clientid);


            $clientTreeId = $oldclient->treeId;
            $dailyEntryDebtor->accountstreeid = $clientTreeId;
        }

        // Save Deptor
        if ($totalpaid > 0) {
            $dailyEntryDebtor2->value = $totalpaid;
            $saveid = $_SESSION['saveid'];
            $dataSave = $mysave->load($saveid);
            $idTreeSave = $dataSave->treeId;
            $dailyEntryDebtor2->accountstreeid = $idTreeSave;
        }
        // supplier exict
        if (!empty($supplierid)) {
            // Supplier Creditor
            $dailyEntryCreditor->value = $totalCost;
            $oldSupplier = $mysupplier->load($supplierid);
            $supplierTreeId = $oldSupplier->treeId;
            $dailyEntryCreditor->accountstreeid = $supplierTreeId;
        }
        // Sales Creditor
        $dailyEntryCreditor2->value = $totalDif;
        $dailyEntryCreditor2->accountstreeid = 16;

        array_push($dailyEntryDebtorArray, $dailyEntryDebtor, $dailyEntryDebtor2);
        array_push($dailyEntryCreditorArray, $dailyEntryCreditor, $dailyEntryCreditor2);


        $myDailyArr = insertEntery($dailyEntry, $dailyEntryDebtorArray, $dailyEntryCreditorArray, 1);
        $myDailyId = $myDailyArr[1];
        $rentBillProData = $myrentbillpro->load($rentBillProId);

        $rentbillpro->amount = $rentBillProData->amount;
        $rentbillpro->billid = $rentBillProData->billid;
        $rentbillpro->delayreset = $rentBillProData->delayreset;
        $rentbillpro->delaytype = $rentBillProData->delaytype;
        $rentbillpro->delypay = $rentBillProData->delypay;
        $rentbillpro->enddate = $rentBillProData->enddate;
        $rentbillpro->productid = $rentBillProData->productid;
        $rentbillpro->rentdays = $rentBillProData->rentdays;
        $rentbillpro->rentprice = $rentBillProData->rentprice;
        $rentbillpro->startdate = $rentBillProData->startdate;
        $rentbillpro->status = $rentBillProData->status;
        $rentbillpro->totalprice = $rentBillProData->totalprice;
        $rentbillpro->type = $rentBillProData->type;
        $rentbillpro->dailyentryid = $myDailyId;

        $myrentbillpro->update($rentbillpro);

        $rentbillData = $myrentbill->load($insertid);

        $rentbill->clientid = $rentbillData->clientid;
        $rentbill->billreset = $rentbillData->billreset;
        $rentbill->billtotal = $rentbillData->billtotal;
        $rentbill->createdatetime = $rentbillData->createdatetime;
        $rentbill->billpayed = $rentbillData->billpayed;
        $rentbill->firmbilltotal = $rentbillData->firmbilltotal;
        $rentbill->dailyentryid = $myDailyId;

        $myrentbill->update($rentbill);

        $mytransactions->commit();

        header("location:?do=show&id=" . $insertid);
    } catch (Exception $exc) {
        echo $exc;
        $mytransactions->rollback();

        header("location:?do=error");
    }
} elseif ($do == "show") {
    include_once("../public/authentication.php");

    $billid = filter_input(INPUT_GET, 'id');
    $today = date("Y-m-d");

    $allbills = $myrentbill->queryByCreatedatetime($today);
    $billtotal = 0;
    $billpayed = 0;
    $billreset = 0;
    foreach ($allbills as $bill) {

        //get props
        $billpro = $myrentbillpro->queryByBillid($bill->id);
        $checkstatus = $extrentbillpro->queryByStatusandbillid(0, $bill->id);
        if (count($checkstatus) > 0) {
            $bill->status = 0;
        } else {
            $bill->status = 1;
        }

        $checkstatusre = $extrentbillpro->queryByStatusandbillid(1, $bill->id);
        if (count($checkstatusre) > 0) {
            $bill->rstatus = 0;
        } else {
            $bill->rstatus = 1;
        }
        //client
        $clientinfo = $myclient->load($bill->clientid);
        $bill->clientname = $clientinfo->clientname;

        $billtotal += $bill->billtotal;
        $billpayed += $bill->billpayed;
        $billreset += $bill->billreset;
    }

    $message = " من تاريخ " . $today . " إلى تاريخ: " . $today;

    $smarty->assign('allbills', $allbills);
    $smarty->assign('billtotal', $billtotal);
    $smarty->assign('billpayed', $billpayed);
    $smarty->assign('billreset', $billreset);
    $smarty->assign('today', $message);

    $smarty->assign('customUsergroup', 1); //for delete model
    if ($billid) {
        header('location:?do=showDetail&id=' . $billid . '&p=1');
    } else {
        $smarty->display("rent/bill/show.html");
    }
} elseif ($do == "search") {
    include_once("../public/authentication.php");

    $dateNow = date("Y-m-d");
    $startDate = $_REQUEST['from'];
    $endDate = $_REQUEST['to'];

    if (empty($startDate)) {
        $startDate = date('Y-m-d');
    }
    if (empty($endDate)) {
        $endDate = date('Y-m-d');
    }



    $allbills = $extrentbill->queryBydatetime($startDate, $endDate);
    $billtotal = 0;
    $billpayed = 0;
    $billreset = 0;
    foreach ($allbills as $bill) {

        //get props
        $billpro = $myrentbillpro->queryByBillid($bill->id);
        $checkstatus = $extrentbillpro->queryByStatusandbillid(0, $bill->id);
        if (count($checkstatus) > 0) {
            $bill->status = 0;
        } else {
            $bill->status = 1;
        }

        $checkstatusre = $extrentbillpro->queryByStatusandbillid(1, $bill->id);
        if (count($checkstatusre) > 0) {
            $bill->rstatus = 0;
        } else {
            $bill->rstatus = 1;
        }
        //client
        $clientinfo = $myclient->load($bill->clientid);
        $bill->clientname = $clientinfo->clientname;

        $billtotal += $bill->billtotal;
        $billpayed += $bill->billpayed;
        $billreset += $bill->billreset;
    }
    $message = " من تاريخ " . $startDate . " إلى تاريخ: " . $endDate;
    $smarty->assign('allbills', $allbills);
    $smarty->assign('billtotal', $billtotal);
    $smarty->assign('billpayed', $billpayed);
    $smarty->assign('billreset', $billreset);
    $smarty->assign('today', $message);


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

    $smarty->assign('customUsergroup', 1); //for delete model
    $smarty->display("rent/bill/show.html");
} elseif ($do == "showDetail") {
    include_once("../public/authentication.php");

    $billid = filter_input(INPUT_GET, 'id');
    $smarty->assign('billid', $billid);
    $print = filter_input(INPUT_GET, 'p');
    $smarty->assign('print', $print);
    $allbills = $myrentbill->load($billid);
    //get props
    $billpro = $myrentbillpro->queryByBillid($billid);
    foreach ($billpro as $pro) {
        //get product name
        $productinfo = $myrentProducts->load($pro->productid);
        $pro->productname = $productinfo->name;

        $supplierinfo = $mysupplier->load($pro->supplierid);
        $pro->suppliername = $supplierinfo->suppliername;
    }
    $allbills->pros = $billpro;
    //client
    $clientinfo = $myclient->load($allbills->clientid);
    $allbills->clientname = $clientinfo->clientname;


    $allbanks = $myBankRecord->queryByCondition(0);
    $smarty->assign('allbanks', $allbanks);


    $allacounts = $myBankaccountRecord->queryByCondition(0);
    $smarty->assign('allacounts', $allacounts);


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

    $smarty->assign('customUsergroup', 1); //for delete model
    $smarty->display("rent/bill/showDetail.html");
} elseif ($do == "editprint") {
    include_once("../public/authentication.php");

    $billid = filter_input(INPUT_GET, 'id');
    $smarty->assign('billid', $billid);
    $print = filter_input(INPUT_GET, 'p');
    $smarty->assign('print', $print);
    $allbills = $myrentbill->load($billid);
    //get props
    $billpro = $myrentbillpro->queryByBillid($billid);
    foreach ($billpro as $pro) {
        //get product name
        $productinfo = $myrentProducts->load($pro->productid);
        $pro->productname = $productinfo->name;

        $supplierinfo = $mysupplier->load($pro->supplierid);
        $pro->suppliername = $supplierinfo->suppliername;
    }
    $allbills->pros = $billpro;
    //client
    $clientinfo = $myclient->load($allbills->clientid);
    $allbills->clientname = $clientinfo->clientname;



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

    $smarty->assign('customUsergroup', 1); //for delete model
    $smarty->display("rent/bill/editprint.html");
} elseif ($do == 'print') {

    $sDate = filter_input(INPUT_POST, 'sdate');
    $hegDate = $_POST['hegDate'];
    $melDate = $_POST['melDate'];
    $recordNo = $_POST['recordNo'];
    $clientName = $_POST['clientName'];
    $clientAddress = $_POST['clientAddress'];
    $clientCredit = $_POST['clientCredit'];
    $endDate = $_POST['endDate'];
    $periodDate = $_POST['periodDate'];
    $reciveDate = $_POST['reciveDate'];
    $reciHegDate = $_POST['reciHegDate'];
    $reciMelDate = $_POST['reciMelDate'];
    $ddDate = $_POST['ddDate'];
    $reciddDate = $_POST['reciddDate'];
    $value = $_POST['value'];
    $valueText = $_POST['valueText'];
    $chckNo = $_POST['chckNo'];


    $allData = array();
    array_push($allData, $sDate, $hegDate, $melDate, $recordNo, $clientName, $clientAddress, $clientCredit, $endDate, $periodDate
            , $reciveDate, $reciHegDate, $reciMelDate, $ddDate, $reciddDate, $value, $valueText, $chckNo);


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


    $billid = filter_input(INPUT_GET, 'id');
    $allbills = $myrentbill->load($billid);
    //get props
    $billpro = $myrentbillpro->queryByBillid($billid);
    foreach ($billpro as $pro) {
        //get product name
        $productinfo = $myrentProducts->load($pro->productid);
        $pro->productname = $productinfo->name;

        $supplierinfo = $mysupplier->load($pro->supplierid);
        $pro->suppliername = $supplierinfo->suppliername;

        $startDate = $pro->startdate;
        $endDate = $pro->enddate;
        $date1 = date_create($pro->startdate);
        $date2 = date_create($pro->enddate);
        $diff = date_diff($date1, $date2);
        $diff = $diff->format("%a days");
        $pro->diffdate = $diff;
    }



    $allbills->pros = $billpro;
    //client
    $clientinfo = $myclient->load($allbills->clientid);
    $allbills->clientname = $clientinfo->clientname;
    $smarty->assign('allbills', $allbills);

    $Programsettingdata = $ProgramsettingDAO->load(1);
    $smarty->assign("Programsettingdata", $Programsettingdata);
    $userdata = $userDAO->load($_SESSION['userid']);
    $smarty->assign("userdata", $userdata);

    $smarty->assign('customUsergroup', 1); //for delete model
    $smarty->display("rent/bill/print.html");
} elseif ($do == "receive") {
    include_once("../public/authentication.php");

    $proid = filter_input(INPUT_GET, 'id');

    $billpro = $myrentbillpro->load($proid);

    //get product name
    $productinfo = $myrentProducts->load($billpro->productid);
    $billpro->productname = $productinfo->name;

    $supplierinfo = $mysupplier->load($billpro->supplierid);
    $billpro->suppliername = $supplierinfo->suppliername;

    $billinfo = $myrentbill->load($billpro->billid);
    //client
    $clientinfo = $myclient->load($billinfo->clientid);
    $billpro->clientname = $clientinfo->clientname;

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

    $smarty->assign('customUsergroup', 1); //for delete model
    $smarty->display("rent/bill/product.html");
} elseif ($do == 'isreceive') {

    $proid = filter_input(INPUT_POST, 'proid');
    $isdelay = filter_input(INPUT_POST, 'isdelay');

    $delayvalue = filter_input(INPUT_POST, 'delayvalue');
    $delaytype = filter_input(INPUT_POST, 'delaytype');


    //Transaction
    $mytransactions = new Transaction();
    try {

        $billpro = $myrentbillpro->load($proid);
        $billinfo = $myrentbill->load($billpro->billid);
        //update store
        //update with store
        //rent amount
        $getstore = $myrentStore->queryByRentproductid($billpro->productid);
        $rentStore->rented = $getstore[0]->rented - $billpro->amount;
        $rentStore->rentproductid = $billpro->productid;
        $extrentStore->updaterented($rentStore);

        $clientid = $billinfo->clientid;
        //update status
        $rentbillpro->status = 1;
        $rentbillpro->id = $proid;

        if ($isdelay == 1) {
            //delapyed
            $rentbillpro->delayvalue = $delayvalue;
            $rentbillpro->delaytype = $delaytype;
            $rentbillpro->delayreset = 0;
            $rentbillpro->delypay = 0;

            $extrentbillpro->updatestatusanddelay($rentbillpro);

            if ($delaytype == 0) {
                //cash
                #1 decrease save
                $saveinfo = $mysave->load($_SESSION['saveid']);
                $balanceBefore = $saveinfo->savecurrentvalue;
                $balanceAfter = $balanceBefore + $delayvalue;

                #2 update save
                $save->savecurrentvalue = $balanceAfter;
                $save->userid = $_SESSION['userid'];
                $save->saveid = $_SESSION['saveid'];
                $extsave->updateSaveValue($save);

                #3 update save report
                $Savedaily->savedailydate = date("Y-m-d");
                $Savedaily->userid = $_SESSION['userid'];
                $Savedaily->savedailysavebefore = $balanceBefore;
                $Savedaily->savedailychangeamount = $delayvalue;
                $Savedaily->savedailychangetype = 0;
                $Savedaily->saveid = $_SESSION['saveid'];
                $Savedaily->processname = 'اضافه تاخير منتج';
                $Savedaily->savedailymodelid = $proid;
                $Savedaily->savedailysaveafter = $balanceAfter;
                $Savedaily->tablename = 'rentbill.php';

                $SavedailyDAO->insert($Savedaily);

                // قيد الكاش

                $dailyEntry->entryComment = '  مبيعات تأخير ( كاش )';
                $dailyEntry->reverseofid = -9;

                $dailyEntryDebtorArray = array();
                $dailyEntryCreditorArray = array();

                // deptor save
                $dailyEntryDebtor->value = $delayvalue;
                $saveid = $_SESSION['saveid'];
                $dataSave = $mysave->load($saveid);
                $idTreeSave = $dataSave->treeId;
                $dailyEntryDebtor->accountstreeid = $idTreeSave;

                // cerditor sales
                $dailyEntryCreditor->value = $delayvalue;
                $dailyEntryCreditor->accountstreeid = 16;

                array_push($dailyEntryDebtorArray, $dailyEntryDebtor);
                array_push($dailyEntryCreditorArray, $dailyEntryCreditor);


                insertEntery($dailyEntry, $dailyEntryDebtorArray, $dailyEntryCreditorArray, 1);
            } else {
                //clients
                #1 increase client debt
                $clientsing = $myclient->load($clientid);
                $debtBefore = $clientsing->clientdebt;
                $debtAfter = $debtBefore + $delayvalue;

                #2 update client debt
                $client->clientdebt = $debtAfter;
                $client->userid = $_SESSION['userid'];
                $client->clientdate = date("Y-m-d");
                $client->clientid = $clientid;
                $extclient->updatedept($client);

                #3 insert client debt change

                $myClientdebtchange->clientdebtchangeafter = $debtAfter;
                $myClientdebtchange->clientdebtchangeamount = $delayvalue;
                $myClientdebtchange->clientdebtchangebefore = $debtBefore;
                $myClientdebtchange->clientdebtchangedate = date("Y-m-d");
                $myClientdebtchange->clientdebtchangemodelid = $proid;
                $myClientdebtchange->clientdebtchangetype = 0;
                $myClientdebtchange->clientid = $clientid;
                $myClientdebtchange->comment = "تاخير استلام منتج ايجار";
                $myClientdebtchange->processname = "تاخير استلام منتج ايجار";
                $myClientdebtchange->tablename = "rentbill.php";
                $myClientdebtchange->userid = $_SESSION['userid'];
                $myClientdebtchange->totalOperationCost = $delayvalue;
                $myClientdebtchange->dailyentryid = $proid;

                $myClientdebtchangeRecord->insert($myClientdebtchange);

                // قيد الاجل

                $dailyEntry->entryComment = ' مبيعات تأخير ( أجل )  ';
                $dailyEntry->reverseofid = -9;

                $dailyEntryDebtorArray = array();
                $dailyEntryCreditorArray = array();

                // deptor client
                $dailyEntryDebtor->value = $delayvalue;
                $clientTreeId = $clientsing->treeId;
                $dailyEntryDebtor->accountstreeid = $clientTreeId;

                // cerditor sales
                $dailyEntryCreditor->value = $delayvalue;
                $dailyEntryCreditor->accountstreeid = 16;

                array_push($dailyEntryDebtorArray, $dailyEntryDebtor);
                array_push($dailyEntryCreditorArray, $dailyEntryCreditor);


                insertEntery($dailyEntry, $dailyEntryDebtorArray, $dailyEntryCreditorArray, 1);
            }
        } else {
            //no delay
            $extrentbillpro->updatestatus($rentbillpro);
        }


        $mytransactions->commit();
        header("location:?do=showDetail&id=" . $billpro->billid);
    } catch (Exception $e) {
        $mytransactions->rollback();
        header("location:?do=error");
    }
} elseif ($do == "delete") {//tempdelete
    include_once("../public/authentication.php");


    //Transaction
    $mytransactions = new Transaction();
    try {

        $id = filter_input(INPUT_GET, 'id');
        $dailyentryid = filter_input(INPUT_GET, 'dailyentryid');
        $billinfo = $myrentbill->load($id);

        $pros = $myrentbillpro->queryByBillid($billinfo->id);
        foreach ($pros as $onepro) {
            $getstore = $myrentStore->queryByRentproductid($onepro->productid);
            $rentStore->rented = $getstore[0]->rented - $onepro->amount;
            $rentStore->rentproductid = $onepro->productid;
            $extrentStore->updaterented($rentStore);


            if ($onepro->type == 1) {

                // with supplier debt and supplier change
                //supplier
                #1 increase supplier debt
                $supplierinfo = $mysupplier->load($onepro->supplierid);
                $debtBefore = $supplierinfo->suppliercurrentDebt;
                $debtAfter = $debtBefore - ($onepro->suppliertotalpice * $onepro->rentdays);

                #2 update supplier debt
                $supplier->suppliercurrentDebt = $debtAfter;
                $supplier->userid = $_SESSION['userid'];
                $supplier->supplierdate = date("Y-m-d");
                $supplier->supplierid = $onepro->supplierid;
                $extsupplier->updatedept($supplier);

                #3 insert supplier debt change
                $Supplierdebtchange->comment = "";
                $Supplierdebtchange->processname = "تاجير منتجات خارجى من فاتوره الايجار ";
                $Supplierdebtchange->supplierdebtchangeafter = $debtAfter;
                $Supplierdebtchange->supplierdebtchangeamount = ($onepro->suppliertotalpice * $onepro->rentdays);
                $Supplierdebtchange->supplierdebtchangebefore = $debtBefore;
                $Supplierdebtchange->supplierdebtchangedate = date("Y-m-d");
                $Supplierdebtchange->supplierdebtchangemodelid = $id;
                $Supplierdebtchange->supplierid = $onepro->supplierid;
                $Supplierdebtchange->supplierdebtchangetype = 1;
                $Supplierdebtchange->tablename = "rentbill.php";
                $Supplierdebtchange->userid = $_SESSION['userid'];
                $Supplierdebtchange->dailyentryid = 0;

                $SupplierdebtchangeDAO->insert($Supplierdebtchange);
            }
        }




        reverseEntryWithItsID($dailyentryid, 1);


        $myrentbill->delete($id);
        $myrentbillpro->deleteByBillid($id);

        //update save or client
        //check type
        //if cash or client
        if ($billinfo->billpayed > 0) {
            //cash
            #1 decrease save
            $saveinfo = $mysave->load($_SESSION['saveid']);
            $balanceBefore = $saveinfo->savecurrentvalue;
            $balanceAfter = $balanceBefore - $billinfo->billpayed;

            #2 update save
            $save->savecurrentvalue = $balanceAfter;
            $save->userid = $_SESSION['userid'];
            $save->saveid = $_SESSION['saveid'];
            $extsave->updateSaveValue($save);

            #3 update save report
            $Savedaily->savedailydate = date("Y-m-d");
            $Savedaily->userid = $_SESSION['userid'];
            $Savedaily->savedailysavebefore = $balanceBefore;
            $Savedaily->savedailychangeamount = $billinfo->billpayed;
            $Savedaily->savedailychangetype = 1;
            $Savedaily->saveid = $_SESSION['saveid'];
            $Savedaily->processname = 'حذف فاتوره للايجار';
            $Savedaily->savedailymodelid = $id;
            $Savedaily->savedailysaveafter = $balanceAfter;
            $Savedaily->tablename = 'rentbill.php';

            $SavedailyDAO->insert($Savedaily);
        }
        if ($billinfo->billreset > 0) {
            //clients
            #1 increase client debt
            $clientsing = $myclient->load($billinfo->clientid);
            $debtBefore = $clientsing->clientdebt;
            $debtAfter = $debtBefore - $billinfo->billreset;

            #2 update client debt
            $client->clientdebt = $debtAfter;
            $client->userid = $_SESSION['userid'];
            $client->clientdate = date("Y-m-d");
            $client->clientid = $billinfo->clientid;
            $extclient->updatedept($client);

            #3 insert client debt change

            $myClientdebtchange->clientdebtchangeafter = $debtAfter;
            $myClientdebtchange->clientdebtchangeamount = $billinfo->billreset;
            $myClientdebtchange->clientdebtchangebefore = $debtBefore;
            $myClientdebtchange->clientdebtchangedate = date("Y-m-d");
            $myClientdebtchange->clientdebtchangemodelid = $id;
            $myClientdebtchange->clientdebtchangetype = 1;
            $myClientdebtchange->clientid = $billinfo->clientid;
            $myClientdebtchange->comment = "حذف فاتوره ايجار ";
            $myClientdebtchange->processname = "حذف فاتوره ايجار ";
            $myClientdebtchange->tablename = "rentbill.php";
            $myClientdebtchange->userid = $_SESSION['userid'];
            $myClientdebtchange->totalOperationCost = $billinfo->billtotal;
            $myClientdebtchange->dailyentryid = $id;

            $myClientdebtchangeRecord->insert($myClientdebtchange);
        }


        $mytransactions->commit();
        header("location:?do=sucess");
    } catch (Exception $e) {
        // print_r($e);
        $mytransactions->rollback();
        header("location:?do=error");
    }
} elseif ($do == "sucess") {
    //here the smarty templates
    $smarty->display("succes.html");
} elseif ($do == "error") {
    //here the smarty templates
    $smarty->display("error.html");
}
//here the global templates
$smarty->assign("rentjs", 1);
$smarty->display("footer.html");

/* ===============================
  function in this CONTROLLER
  ================================ */
?>