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/billreceiptController.php
<?php

//the global file operation
include("../public/impOpreation.php");

// get the config file
include_once("../public/config.php");

//here the db files that include in the file
include("../public/include_dao.php");

//MComreceipt
require_once('../models/dao/MComreceiptDAO.class.php');
require_once('../models/dto/MComreceipt.class.php');
require_once('../models/mysql/MComreceiptMySqlDAO.class.php');
require_once('../models/mysql/ext/MComreceiptMySqlExtDAO.class.php');

//MComreceiptdetail
require_once('../models/dao/MComreceiptdetailDAO.class.php');
require_once('../models/dto/MComreceiptdetail.class.php');
require_once('../models/mysql/MComreceiptdetailMySqlDAO.class.php');
require_once('../models/mysql/ext/MComreceiptdetailMySqlExtDAO.class.php');

//MFinishingpayed
require_once('../models/dao/MFinishingpayedDAO.class.php');
require_once('../models/dto/MFinishingpayed.class.php');
require_once('../models/mysql/MFinishingpayedMySqlDAO.class.php');
require_once('../models/mysql/ext/MFinishingpayedMySqlExtDAO.class.php');

//MMaintenancereceipt
require_once('../models/dao/MMaintenancereceiptDAO.class.php');
require_once('../models/dto/MMaintenancereceipt.class.php');
require_once('../models/mysql/MMaintenancereceiptMySqlDAO.class.php');
require_once('../models/mysql/ext/MMaintenancereceiptMySqlExtDAO.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');

//Supplier
require_once('../models/dao/SupplierDAO.class.php');
require_once('../models/dto/Supplier.class.php');
require_once('../models/mysql/SupplierMySqlDAO.class.php');
require_once('../models/mysql/ext/SupplierMySqlExtDAO.class.php');

//Supplierdebtchange
require_once('../models/dao/SupplierdebtchangeDAO.class.php');
require_once('../models/dto/Supplierdebtchange.class.php');
require_once('../models/mysql/SupplierdebtchangeMySqlDAO.class.php');
require_once('../models/mysql/ext/SupplierdebtchangeMySqlExtDAO.class.php');

//Product
require_once('../models/dao/ProductDAO.class.php');
require_once('../models/dto/Product.class.php');
require_once('../models/mysql/ProductMySqlDAO.class.php');
require_once('../models/mysql/ext/ProductMySqlExtDAO.class.php');

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

//Savedaily
require_once('../models/dao/SavedailyDAO.class.php');
require_once('../models/dto/Savedaily.class.php');
require_once('../models/mysql/SavedailyMySqlDAO.class.php');
require_once('../models/mysql/ext/SavedailyMySqlExtDAO.class.php');
//Programsetting
require_once('../models/dao/ProgramsettingsDAO.class.php');
require_once('../models/dto/Programsetting.class.php');
require_once('../models/mysql/ProgramsettingsMySqlDAO.class.php');
require_once('../models/mysql/ext/ProgramsettingsMySqlExtDAO.class.php');

//MProducthistory
require_once('../models/dao/MProducthistoryDAO.class.php');
require_once('../models/dto/MProducthistory.class.php');
require_once('../models/mysql/MProducthistoryMySqlDAO.class.php');
require_once('../models/mysql/ext/MProducthistoryMySqlExtDAO.class.php');

//Productserial
require_once('../models/dao/ProductserialDAO.class.php');
require_once('../models/dto/Productserial.class.php');
require_once('../models/mysql/ProductserialMySqlDAO.class.php');
require_once('../models/mysql/ext/ProductserialMySqlExtDAO.class.php');

require_once('../models/dao/MCheckDAO.class.php');
require_once('../models/dto/MCheck.class.php');
require_once('../models/mysql/MCheckMySqlDAO.class.php');
require_once('../models/mysql/ext/MCheckMySqlExtDAO.class.php');

//MComreceiptimage
require_once('../models/dao/MComreceiptimageDAO.class.php');
require_once('../models/dto/MComreceiptimage.class.php');
require_once('../models/mysql/MComreceiptimageMySqlDAO.class.php');
require_once('../models/mysql/ext/MComreceiptimageMySqlExtDAO.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'];

$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");
//$smarty->display("maintennanceHeader.html");
//here goes the instances and general variables
//MComreceiptDAO
$MComreceipt = new MComreceipt();
$MComreceiptDAO = new MComreceiptMySqlDAO();
$MComreceiptEX = new MComreceiptMySqlExtDAO();

//MComreceiptdetail
$MComreceiptdetail = new MComreceiptdetail();
$MComreceiptdetailDAO = new MComreceiptdetailMySqlDAO();
$MComreceiptdetailEX = new MComreceiptdetailMySqlExtDAO();
//MFinishingpayed
$MFinishingpayed = new MFinishingpayed();
$MFinishingpayedDAO = new MFinishingpayedMySqlDAO();
$MFinishingpayedEX = new MFinishingpayedMySqlExtDAO();

//MMaintenancereceipt
$MMaintenancereceipt = new MMaintenancereceipt();
$MMaintenancereceiptDAO = new MMaintenancereceiptMySqlDAO();
$MMaintenancereceiptEX = new MMaintenancereceiptMySqlExtDAO();

//Clientdebtchange
$Clientdebtchange = new Clientdebtchange();
$ClientdebtchangeDAO = new ClientdebtchangeMySqlDAO();
$ClientdebtchangeEX = new ClientdebtchangeMySqlExtDAO();

//Client
$Client = new Client();
$ClientDAO = new ClientMySqlDAO();
$ClientEX = new ClientMySqlExtDAO();

//Supplier
$Supplier = new Supplier();
$SupplierDAO = new SupplierMySqlDAO();
$SupplierEX = new SupplierMySqlExtDAO();

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

//Product
$Product = new Product();
$ProductDAO = new ProductMySqlDAO();
$ProductEX = new ProductMySqlExtDAO();

//MProducthistory
$MProducthistory = new MProducthistory();
$MProducthistoryDAO = new MProducthistoryMySqlDAO();
$MProducthistoryEX = new MProducthistoryMySqlExtDAO();

//Programsetting
$ProgramsettingDAO = new ProgramsettingsMySqlDAO();

//Save
$Save = new Save();
$SaveDAO = new SaveMySqlDAO();
$SaveEX = new SaveMySqlExtDAO();

//Savedaily
$Savedaily = new Savedaily();
$SavedailyDAO = new SavedailyMySqlDAO();
$SavedailyEX = new SavedailyMySqlExtDAO();


//Productserial
$Productserial = new Productserial();
$ProductserialDAO = new ProductserialMySqlDAO();
$ProductserialEX = new ProductserialMySqlExtDAO();

//MCheck
$MCheck = new MCheck();
$MCheckDAO = new MCheckMySqlDAO();
$MCheckEX = new MCheckMySqlExtDAO();

//MComreceiptimage
$MComreceiptimage = new MComreceiptimage();
$MComreceiptimageDAO = new MComreceiptimageMySqlDAO();
$MComreceiptimageEX = new MComreceiptimageMySqlExtDAO();


$youtubeLink = new YoutubeLink();
$youtubeLinkDAO = new YoutubeLinkMySqlDAO();
$youtubeLinkEX = new YoutubeLinkMySqlExtDAO();
//Capital
//check and use the condetion that suite this action
if (empty($do)) {

    include_once("../public/authentication.php");
    //date
    $today = date("Y-m-d");
    $smarty->assign("today", $today);

    //client
    $clientsData = getclient();
    $smarty->assign("clientsData", $clientsData);

    //supplier
    $supplierData = getSupplier();
    $smarty->assign("supplierData", $supplierData);

    $receptid = $MComreceiptEX->loadid();
    $id = $receptid->id;
    $smarty->assign("id", $id);
    //getserialnumber
    $todayx = date("Ymd");
    $receiptdata = $MComreceiptEX->loadcase($todayx);
    $receiptserial = $receiptdata->receiptserial;
    if ($receiptserial != "") {

        $year = substr($receiptserial, 8, 2);

        $year++;
        $todayx = $todayx . $year;
    } else {

        $todayx = $todayx . '1';
    }

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



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

    $smarty->assign("title1", 'إدارة الفواتير');
    $smarty->assign("title2", 'فاتورة الاستلام');
    $smarty->assign("title3", 'إضافة فاتورة');
    $smarty->assign("link", '#');
    $smarty->display("maintennanceHeader.html");



    //check if there is any rows in capital table
    $smarty->display("billreceiptview/bill/bill_info.html");
    $smarty->display("billreceiptview/bill/client_insurance.html");
    $smarty->display("billreceiptview/bill/company_insurance.html");
    $smarty->display("billreceiptview/bill/mantance.html");
    $smarty->display("billreceiptview/bill/total.html");
    $smarty->display("billreceiptview/bill/bill_footer.html");
    $smarty->assign("html2canvas", 1);
    $smarty->assign("custombill", 1);
} /* elseif ($do = 'action') {
  //nothing

  }
 */ elseif ($do == "add") {
    // include_once("../public/authentication.php");
    include_once("../public/authentication.php");

    $receptdata = $MComreceiptEX->queryAllMComreceipt();
    $ID = $receptdata->id;

    $id = $ID + 1;
    //add();
    /* try
      { */
    // here the function that do the action
    add();
    /* header("location:?do=sucess");
      }
      catch(Exception $e)
      {
      header("location:?do=error");
      } */
} elseif ($do == "show") {
    include_once("../public/authentication.php");

    $today = date("Y-m-d");
    $smarty->assign("today", $today);
    $youtubes = $youtubeLinkDAO->queryAll();
    $smarty->assign("youtubes", $youtubes);
    //client
    $clientsData = getclient();
    $smarty->assign("clientsData", $clientsData);

    //serialData
    $serialData = getserialnumber();
    $smarty->assign("serialData", $serialData);

    //serialproductData
    $serialproductData = getserialproduct();
    $smarty->assign("serialproductData", $serialproductData);

    $serialid = $_REQUEST['serialid'];
    $serialproductid = $_REQUEST['serialproductid'];
    $clientid = $_REQUEST['clientid'];
    $startDate = $_REQUEST['from'];
    $endDate = $_REQUEST['to'];
    $documentid = $_REQUEST['documentid'];
    if ((!empty($clientid)) || (!empty($serialid)) || (!empty($serialproductid)) || (!empty($documentid)) || (!empty($startDate) && !empty($endDate))) {
        /* $message = " اسم العميل ".$clientname. "التاريخ: من: ".$startDate." إلى: ".$endDate;
          $smarty->assign("message",$message);
         */
        show($clientid, $serialid, $serialproductid, $documentid, $startDate, $endDate);
    } else {
        $today = date("Y-m-d");
        show($clientid, $serialid, $serialproductid, $documentid, $today, $today);
    }

    $smarty->assign("title1", 'إدارة الفواتير');
    $smarty->assign("title2", 'فاتورة الاستلام');
    $smarty->assign("title3", 'عرض فاتورة الاستلام');
    $smarty->assign("link", '#');
    $smarty->display("maintennanceHeader.html");


    //show();
    $smarty->display("billreceiptview/show/show.html");
}
//here the global templates
elseif ($do == "edit") {

    include_once("../public/authentication.php");
    //date
    $today = date("Y-m-d");
    $smarty->assign("today", $today);

    //client
    $clientsData = getclient();
    $smarty->assign("clientsData", $clientsData);

    //supplier
    $supplierData = getSupplier();
    $smarty->assign("supplierData", $supplierData);

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

    $loadData = edit();

    $clientdata = $loadData[0];
    $companydata = $loadData[1];
    $mantancedata = $loadData[2];
    $allrecept = $loadData[3];


    $smarty->assign("clientdata", $clientdata);
    $smarty->assign("clientitr", count($clientdata));
    $smarty->assign("companydata", $companydata);
    $smarty->assign("companyitr", count($companydata));
    $smarty->assign("mantancedata", $mantancedata);
    $smarty->assign("mantanceitr", count($mantancedata));
    $smarty->assign("allrecept", $allrecept);


    $smarty->assign("custombilledit", 1);
    $smarty->assign("html2canvas", 1);

    $smarty->assign("title1", 'إدارة الفواتير');
    $smarty->assign("title2", 'فاتورة الاستلام');
    $smarty->assign("title3", 'تعديل فاتورة');
    $smarty->assign("link", '#');
    $smarty->display("maintennanceHeader.html");


    //check if there is any rows in capital table
    $smarty->display("billreceiptview/edit/billx_info.html");
    $smarty->display("billreceiptview/edit/client_insurancex.html");
    $smarty->display("billreceiptview/edit/company_insurancex.html");
    $smarty->display("billreceiptview/edit/mantancex.html");
    $smarty->display("billreceiptview/edit/totalx.html");
    $smarty->display("billreceiptview/edit/billx_footer.html");
} elseif ($do == "editprint") {

    include_once("../public/authentication.php");
    //date
    $today = date("Y-m-d");
    $smarty->assign("today", $today);

    //client
    $clientsData = getclient();
    $smarty->assign("clientsData", $clientsData);

    //supplier
    $supplierData = getSupplier();
    $smarty->assign("supplierData", $supplierData);

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

    $loadData = edit();

    $clientdata = $loadData[0];
    $companydata = $loadData[1];
    $mantancedata = $loadData[2];
    $allrecept = $loadData[3];


    $smarty->assign("clientdata", $clientdata);
    $smarty->assign("clientitr", count($clientdata));
    $smarty->assign("companydata", $companydata);
    $smarty->assign("companyitr", count($companydata));
    $smarty->assign("mantancedata", $mantancedata);
    $smarty->assign("mantanceitr", count($mantancedata));
    $smarty->assign("allrecept", $allrecept);


    $smarty->assign("custombilledit", 1);
    $smarty->assign("html2canvas", 1);


    $smarty->assign("title1", 'إدارة الفواتير');
    $smarty->assign("title2", 'فاتورة الاستلام');
    $smarty->assign("title3", 'تعديل فاتورة');
    $smarty->assign("link", '#');
    $smarty->display("maintennanceHeader.html");



    //check if there is any rows in capital table
    $smarty->display("billreceiptview/editprint/billprint_info.html");
    $smarty->display("billreceiptview/editprint/client_insuranceprint.html");
    $smarty->display("billreceiptview/editprint/company_insuranceprint.html");
    $smarty->display("billreceiptview/editprint/mantanceprint.html");
    $smarty->display("billreceiptview/editprint/totalprint.html");
    //$smarty->display("billreceiptview/editprint/billprint_footer.html");
} elseif ($do == "editprintx") {

    include_once("../public/authentication.php");
    //date
    $today = date("Y-m-d");
    $smarty->assign("today", $today);

    //client
    $clientsData = getclient();
    $smarty->assign("clientsData", $clientsData);

    //supplier
    $supplierData = getSupplier();
    $smarty->assign("supplierData", $supplierData);

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

    $loadData = edit();

    $clientdata = $loadData[0];
    $companydata = $loadData[1];
    $mantancedata = $loadData[2];
    $allrecept = $loadData[3];


    $smarty->assign("clientdata", $clientdata);
    $smarty->assign("clientitr", count($clientdata));
    $smarty->assign("companydata", $companydata);
    $smarty->assign("companyitr", count($companydata));
    $smarty->assign("mantancedata", $mantancedata);
    $smarty->assign("mantanceitr", count($mantancedata));
    $smarty->assign("allrecept", $allrecept);


    $smarty->assign("custombilledit", 1);
    $smarty->assign("html2canvas", 1);


    $smarty->assign("title1", 'إدارة الفواتير');
    $smarty->assign("title2", 'فاتورة الاستلام');
    $smarty->assign("title3", 'تعديل فاتورة');
    $smarty->assign("link", '#');
    $smarty->display("maintennanceHeader.html");



    //check if there is any rows in capital table
    $smarty->display("billreceiptview/editprint2/print1.html");

    //$smarty->display("billreceiptview/editprint/billprint_footer.html");
} elseif ($do == "update") {


    include_once("../public/authentication.php");
    //add();
    try {
        // here the function that do the action
        update();
        header("location:?do=sucess");
    } catch (Exception $e) {
        header("location:?do=error");
    }
} elseif ($do == "delete") {


    include_once("../public/authentication.php");
    //add();
    try {
        // here the function that do the action
        delete();
        header("location:?do=sucessxdd");
    } catch (Exception $e) {
        header("location:?do=error");
    }
} elseif ($do == "sucess") {
    //include_once("../public/authentication.php");
    //here the smarty templates
    $id = getid();

    $smarty->assign("id", $id);
    $smarty->display("success2.html");
} elseif ($do == "sucessxdd") {

    $smarty->display("succes.html");
} elseif ($do == "error") {

    //here the smarty templates
    $smarty->display("error.html");
}
$smarty->assign("customreceipt", 1);
$smarty->assign("GeneralSearch", 1);
$smarty->display("footer.html");

////****************************  Functions  ********************///

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

function getid() {
    global $MComreceiptEX;

    $receptdata = $MComreceiptEX->queryAllMComreceipt();

    $ID = $receptdata->id;

    $id = $ID;

    return $id;
}

function getclient() {
    global $ClientDAO;

    $clientsData = $ClientDAO->queryByCondition(0);

    return $clientsData;
}

function getSupplier() {
    global $SupplierDAO;
    $supplierData = $SupplierDAO->queryByCondition(0);
    return $supplierData;
}

function getproduct() {
    global $ClientDAO;

    $clientsData = $ClientDAO->queryByCondition(0);

    return $clientsData;
}

function getserialnumber() {
    global $MComreceiptEX;

    $serialData = $MComreceiptEX->getserialnumber();

    return $serialData;
}

function getserialproduct() {
    global $MComreceiptdetailEX;

    $serialproductData = $MComreceiptdetailEX->queryallserialproduct();

    return $serialproductData;
}

function add() {

    global $MComreceipt;
    global $MComreceiptDAO;
    global $MComreceiptdetail;
    global $MComreceiptdetailDAO;
    global $MMaintenancereceipt;
    global $MMaintenancereceiptDAO;
    global $ClientEX;
    global $Client;
    global $ClientDAO;
    global $Clientdebtchange;
    global $ClientdebtchangeDAO;
    global $MProducthistory;
    global $MProducthistoryDAO;
    global $MCheck;
    global $MCheckDAO;
    global $MComreceiptEX;
    global $MComreceiptimage;
    global $MComreceiptimageDAO;
    global $MComreceiptimageEX;
    global $smarty;



    $clientitr = $_POST['clint_insure_no'];
    $receptnumber = $_POST['receptnumber'];
    $documentnumber = $_POST['documentnumber'];
    $receptdate = $_POST['receptdate'];
    $compclient = $_POST['comp_client'];
    $clienttotalintial = $_POST['client_total_intial'];
    $clienttotalship = $_POST['client_total_ship'];
    $recepttotalintinal = $_POST['recept_total_intinal'];
    $companytotalship = $_POST['recept_total_shop'];



    if (isset($compclient) && !empty($compclient) && $compclient != '-1' && $compclient != '') {
        $MComreceipt->receiptserial = $receptnumber;
        $MComreceipt->documentid = $documentnumber;
        $MComreceipt->receiptdat = $receptdate;
        $MComreceipt->userid = $_SESSION['userid'];
        $MComreceipt->branchid = $_SESSION['branchId'];
        $MComreceipt->initialcost = $recepttotalintinal;
        $MComreceipt->shipcost = $companytotalship;
        $MComreceipt->status = 0;
        $MComreceipt->del = 0;
        $MComreceipt->clientid = $compclient;

        $MComreceiptId = $MComreceiptDAO->insert($MComreceipt);

        print_r('com recit entered' . '<br/>');
        print_r('clientitr' . $clientitr . '<br/>');


        /// insert for client insurance
        for ($h = 1; $h <= $clientitr; $h++) {
            $product = $_POST['product' . $h . ''];
            print_r($product);
            $clientserial = $_POST['client_serial' . $h . ''];

            $clientsellbilltype = $_POST['client_sellbill_type' . $h . ''];
            $clientsellbillserial = $_POST['client_sellbill_serial' . $h . ''];
            $clientsellbilldate = $_POST['client_sellbill_date' . $h . ''];
            $clientbuybillserial = $_POST['client_buybill_serial' . $h . ''];
            $clientbuybilltype = $_POST['client_buybill_type' . $h . ''];
            $clientbuybilldate = $_POST['client_buybill_date' . $h . ''];
            $clientinsurceduration = $_POST['client_insurce_duration' . $h . ''];
            $optionsRadios = $_POST['optionsRadios' . $h . ''];
            $clineterror = $_POST['clinet_error' . $h . ''];
            $clientnote = $_POST['client_note' . $h . ''];
            $clientattch = $_POST['client_attch' . $h . ''];
            $clientintinalcost = $_POST['client_intinal_cost' . $h . ''];
            $clientshipcost = $_POST['client_ship_cost' . $h . ''];
            $clientsupplier = $_POST['client_supplier' . $h . ''];

            $random = gen_random_string(1);
            $todayx = $receptnumber . $h . x;
            //insert MComreceipt


            if (isset($product) && !empty($product) && $product != '-1' && $product != '') {

                //insert MComreceiptdetail
                $MComreceiptdetail->receiptid = $MComreceiptId;
                $MComreceiptdetail->ourserial = $todayx;
                $MComreceiptdetail->productserial = $clientserial;
                $MComreceiptdetail->productId = $product;
                $MComreceiptdetail->sellbillid = $clientsellbillserial;
                $MComreceiptdetail->sellbilldate = $clientsellbilldate;
                $MComreceiptdetail->buybillid = $clientbuybillserial;
                $MComreceiptdetail->buybilldate = $clientbuybilldate;
                $MComreceiptdetail->granttype = 1;
                $MComreceiptdetail->initialcost = $clientintinalcost;
                $MComreceiptdetail->del = 0;
                $MComreceiptdetail->comment = $clientnote;
                $MComreceiptdetail->attach = $clientattch;
                $MComreceiptdetail->supplierid = $clientsupplier;
                $MComreceiptdetail->insduration = $clientinsurceduration;
                $MComreceiptdetail->insstatus = $optionsRadios;
                $MComreceiptdetail->problem = $clineterror;
                $MComreceiptdetail->shipcost = $clientshipcost;
                $MComreceiptdetail->branchid = $_SESSION['branchId'];
                $MComreceiptdetail->currentstage = 9;

                $MComreceiptdetailDAO->insert($MComreceiptdetail);


                //insert into Check
                $MCheck->branchId = $_SESSION['branchId'];
                $MCheck->checkDate = date("Y-m-d");
                $MCheck->del = 0;
                $MCheck->stageFrom = 1;
                $MCheck->ourSerial = $todayx;
                $MCheck->userId = $_SESSION['userid'];

                $MCheckDAO->insert($MCheck);
                // insert into MProducthistory
                $MProducthistory->ourSerial = $todayx;
                $MProducthistory->branchId = $_SESSION['branchId'];
                $MProducthistory->productHistoryDate = date("Y-m-d H:i:s");
                $MProducthistory->userId = $_SESSION['userid'];
                $MProducthistory->cost = 0;
                $MProducthistory->del = 0;
                $MProducthistory->status = 0;
                $MProducthistory->stageFrom = 1;
                $MProducthistory->stageTo = 9;
                $MProducthistory->comment = " اضافه فاتوره";

                $MProducthistoryDAO->insert($MProducthistory);
            }
        }


        /// insert for company insurance
        $companyitr = $_POST['company_insure_no'];
        $companytotalshipx = $_POST['company_total_ship'];
        $companytotalintial = $_POST['company_total_intial'];
        for ($r = 1; $r <= $companyitr; $r++) {
            $productcompany = $_POST['productcompany' . $r . ''];
            $companyserial = $_POST['company_serial' . $r . ''];

            $companybuybilltype = $_POST['company_buybill_type' . $r . ''];
            $companybuybillserial = $_POST['company_buybill_serial' . $r . ''];
            $companybuybilldate = $_POST['company_buybill_date' . $r . ''];
            $companyinsurceduration = $_POST['company_insurce_duration' . $r . ''];
            $companyoptionsRadios = $_POST['company_optionsRadios' . $r . ''];
            $companyerror = $_POST['company_error' . $r . ''];
            $companynote = $_POST['company_note' . $r . ''];
            $companyattch = $_POST['company_attch' . $r . ''];
            $companyshipcost = $_POST['company_ship_cost' . $r . ''];
            $companyintinalcost = $_POST['company_intinal_cost' . $r . ''];
            $companysupplier = $_POST['company_supplier' . $r . ''];


            //insert MComreceipt
            $random = gen_random_string(1);
            $todayx = $receptnumber . $r . c;


            if (isset($productcompany) && !empty($productcompany) && $productcompany != '-1' && $productcompany != '') {


                //insert MComreceiptdetail
                $MComreceiptdetail->receiptid = $MComreceiptId;
                $MComreceiptdetail->ourserial = $todayx;
                $MComreceiptdetail->productserial = $companyserial;
                $MComreceiptdetail->productId = $productcompany;
                $MComreceiptdetail->sellbillid = 0;
                $MComreceiptdetail->sellbilldate = "";
                $MComreceiptdetail->buybillid = $companybuybillserial;
                $MComreceiptdetail->buybilldate = $companybuybilldate;
                $MComreceiptdetail->granttype = 2;
                $MComreceiptdetail->initialcost = $companyintinalcost;
                $MComreceiptdetail->del = 0;
                $MComreceiptdetail->comment = $companynote;
                $MComreceiptdetail->attach = $companyattch;
                $MComreceiptdetail->supplierid = $companysupplier;
                $MComreceiptdetail->insduration = $companyinsurceduration;
                $MComreceiptdetail->insstatus = $companyoptionsRadios;
                $MComreceiptdetail->problem = $companyerror;
                $MComreceiptdetail->shipcost = $companyshipcost;
                $MComreceiptdetail->branchid = $_SESSION['branchId'];
                $MComreceiptdetail->currentstage = 9;

                $MComreceiptdetailDAO->insert($MComreceiptdetail);


                //insert into Check
                $MCheck->branchId = $_SESSION['branchId'];
                $MCheck->checkDate = date("Y-m-d");
                $MCheck->del = 0;
                $MCheck->stageFrom = 1;
                $MCheck->ourSerial = $todayx;
                $MCheck->userId = $_SESSION['userid'];

                $MCheckDAO->insert($MCheck);
                // insert into MProducthistory
                $MProducthistory->ourSerial = $todayx;
                $MProducthistory->branchId = $_SESSION['branchId'];
                $MProducthistory->productHistoryDate = date("Y-m-d H:i:s");
                $MProducthistory->userId = $_SESSION['userid'];
                $MProducthistory->cost = 0;
                $MProducthistory->del = 0;
                $MProducthistory->status = 0;
                $MProducthistory->stageFrom = 1;
                $MProducthistory->stageTo = 9;
                $MProducthistory->comment = " اضافه فاتوره";

                $MProducthistoryDAO->insert($MProducthistory);
            }
        }



        /// insert for manitannce
        $mantanceitr = $_POST['mantance_insure_no'];
        $mantancetotalintial = $_POST['mantance_total_intial'];
        for ($x = 1; $x <= $mantanceitr; $x++) {
            $productmantance = $_POST['productmantance' . $x . ''];
            $mantanceserial = $_POST['mantance_serial' . $x . ''];

            $mantancedescription = $_POST['mantance_description' . $x . ''];
            $mantanceerror = $_POST['mantance_error' . $x . ''];
            $mantancenote = $_POST['mantance_note' . $x . ''];
            $mantanceattch = $_POST['mantance_attch' . $x . ''];
            $mantanceintinalcost = $_POST['mantance_intinal_cost' . $x . ''];
            $mantancetotalintial = $_POST['mantance_total_intial'];
            $random = gen_random_string(1);
            $todayx = $receptnumber . $x . m;

            if (isset($productmantance) && !empty($productmantance) && $productmantance != '-1' && $productmantance != '') {

                //insert MComreceiptdetail
                $MComreceiptdetail->receiptid = $MComreceiptId;
                $MComreceiptdetail->ourserial = $todayx;
                $MComreceiptdetail->productserial = $mantanceserial;
                $MComreceiptdetail->productId = $productmantance;
                $MComreceiptdetail->sellbillid = 0;
                $MComreceiptdetail->sellbilldate = "";
                $MComreceiptdetail->buybillid = 0;
                $MComreceiptdetail->buybilldate = "";
                $MComreceiptdetail->granttype = 0;
                $MComreceiptdetail->initialcost = $mantanceintinalcost;
                $MComreceiptdetail->del = 0;
                $MComreceiptdetail->comment = $mantancenote;
                $MComreceiptdetail->attach = $mantanceattch;
                $MComreceiptdetail->supplierid = 0;
                $MComreceiptdetail->insduration = 0;
                $MComreceiptdetail->insstatus = 0;
                $MComreceiptdetail->problem = $mantanceerror;
                $MComreceiptdetail->shipcost = 0;
                $MComreceiptdetail->branchid = $_SESSION['branchId'];
                $MComreceiptdetail->currentstage = 9;

                $MComreceiptdetailDAO->insert($MComreceiptdetail);

                //insert into Check
                $MCheck->branchId = $_SESSION['branchId'];
                $MCheck->checkDate = date("Y-m-d");
                $MCheck->del = 0;
                $MCheck->stageFrom = 1;
                $MCheck->ourSerial = $todayx;
                $MCheck->userId = $_SESSION['userid'];

                $MCheckDAO->insert($MCheck);
                // insert into MProducthistory
                $MProducthistory->ourSerial = $todayx;
                $MProducthistory->branchId = $_SESSION['branchId'];
                $MProducthistory->productHistoryDate = date("Y-m-d H:i:s");
                $MProducthistory->userId = $_SESSION['userid'];
                $MProducthistory->cost = 0;
                $MProducthistory->del = 0;
                $MProducthistory->status = 0;
                $MProducthistory->stageFrom = 1;
                $MProducthistory->stageTo = 9;
                $MProducthistory->comment = " اضافه فاتوره";

                $MProducthistoryDAO->insert($MProducthistory);
            }
        }

        $companytotalship = $_POST['recept_total_shop'];
        $clientData = $ClientDAO->load($compclient);
        $clientdebt = $clientData->clientdebt;

        if ($clientdebt > 0) {
            $debtAfter = $clientdebt + $companytotalship;
        } else {
            $debtAfter = $companytotalship;
        }

        $Client->clientdebt = $debtAfter;
        $Client->userid = $_SESSION['userid'];
        $Client->clientdate = $today;
        $Client->clientid = $compclient;

        $ClientEX->updatedept($Client);

        $Clientdebtchange->clientdebtchangeafter = $debtAfter;
        $Clientdebtchange->clientdebtchangeamount = $companytotalship;
        $Clientdebtchange->clientdebtchangebefore = $clientdebt;
        $Clientdebtchange->clientdebtchangedate = date("Y-m-d");
        $Clientdebtchange->clientdebtchangemodelid = $MComreceiptId;
        $Clientdebtchange->clientdebtchangetype = 0;
        $Clientdebtchange->tablename = "billreceiptController.php";
        $Clientdebtchange->userid = $_SESSION['userid'];
        $Clientdebtchange->clientid = $compclient;
        $Clientdebtchange->processname = "اضافة فاتورة استلام صيانة";
        $Clientdebtchange->totalOperationCost = $companytotalship;


        //Insert Clientdebtchange
        $clientdebtchangeId = $ClientdebtchangeDAO->insert($Clientdebtchange);

        // insert into MMaintenancereceipt
        $MMaintenancereceipt->branchId = $_SESSION['branchId'];
        $MMaintenancereceipt->receiptId = $MComreceiptId;
        $MMaintenancereceipt->type = 0;
        $MMaintenancereceipt->maintenanceReceiptDAte = date("Y-m-d");
        $MMaintenancereceipt->totalCost = $companytotalship;
        $MMaintenancereceipt->userId = $_SESSION['userid'];
        $MMaintenancereceipt->del = 0;

        $MMaintenancereceiptDAO->insert($MMaintenancereceipt);
    }

    return $MComreceiptId;
}

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

function show($clientid, $serialid, $serialproductid, $documentid, $startDate, $endDate) {
    global $ClientDAO;
    global $MComreceiptEX;
    global $smarty;
    global $queryString;
    $queryString = ' WHERE';

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

        $queryString .= '  m_comreceipt.clientid = ' . $clientid . ' AND';
    }
    if (isset($clientid) && $clientid != "-1" && $clientid == "all") {
        $message = " كل العملاء";
        $smarty->assign('message', $message);
        $queryString .= '  m_comreceipt.clientid > 0  AND';
    }

    if (!empty($serialproductid)) {

        $queryString .= '  m_comreceiptdetail.productserial = ' . $serialproductid . '  AND';
    }
    if (!empty($documentid)) {

        $queryString .= '  m_comreceipt.documentid = ' . $documentid . '  AND';
    }

    if (!empty($serialid)) {

        $queryString .= ' m_comreceipt.receiptserial = ' . $serialid . ' AND';
    }
    if (!empty($startDate) && !empty($endDate)) {

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


        $queryString .= '  m_comreceipt.receiptdat >= "' . $startDate . '" AND m_comreceipt.receiptdat <= "' . $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 = $MComreceiptEX->queryAllforreceiptshow($queryString);


    $cost = 0;
    foreach ($showndata as $receiptdata) {

        $cost +=$receiptdata->Initialcost;
        //$cost= round(($cost1 *2))/2;
    }


    //print_r($shownData);
    $smarty->assign('showndata', $showndata);
    $smarty->assign('cost', $cost);
}

/* =============================================edit====================================================== */

function edit() {
    global $MComreceiptdetailEX;
    global $MComreceiptEX;

    $id = $_GET['id'];
    $clientdata = $MComreceiptdetailEX->queryallrecept($id, 1);

    $companydata = $MComreceiptdetailEX->queryallrecept($id, 2);
    $mantancedata = $MComreceiptdetailEX->queryallrecept($id, 0);
    $allrecept = $MComreceiptEX->loadEX($id);

    return array($clientdata, $companydata, $mantancedata, $allrecept);
}

/* =============================================edit print====================================================== */

function editprint() {
    global $MComreceiptimageEX;

    $id = $_GET['id'];

    $imagedata = $MComreceiptimageEX->loadimage($id);

    return $imagedata;
}

/* ========================================update=========================================================== */
/* ========================================update=========================================================== */

function update() {
    global $MComreceipt;
    global $MComreceiptDAO;
    global $MComreceiptdetail;
    global $MComreceiptdetailDAO;
    global $MMaintenancereceipt;
    global $MMaintenancereceiptEX;
    global $ClientEX;
    global $Client;
    global $ClientDAO;
    global $Clientdebtchange;
    global $ClientdebtchangeDAO;
    global $MProducthistory;
    global $MProducthistoryEX;
    global $MCheck;
    global $MCheckEX;
    global $MCheckDAO;
    global $MComreceiptdetailEX;
    global $MProducthistoryDAO;

    //variable for MComreceipt
    $MComreceiptId = $_POST['receptid'];
    $clientitr = $_POST['clint_insure_no'];
    $receptnumber = $_POST['receptnumber'];
    $documentnumber = $_POST['documentnumber'];
    $receptdate = $_POST['receptdate'];
    $compclient = $_POST['comp_client'];
    $recepttotalintinal = $_POST['recept_total_intinal'];
    $companytotalship = $_POST['recept_total_shop'];
    $receptstatus = $_POST['receptstatus'];
    $totalship = $_POST['totalship'];
    $totalcost = $_POST['totalcost'];
    if (isset($compclient) && !empty($compclient) && $compclient != '-1' && $compclient != '') {
        $MComreceipt->id = $MComreceiptId;
        $MComreceipt->receiptserial = $receptnumber;
        $MComreceipt->documentid = $documentnumber;
        $MComreceipt->receiptdat = $receptdate;
        $MComreceipt->userid = $_SESSION['userid'];
        $MComreceipt->branchid = $_SESSION['branchId'];
        $MComreceipt->initialcost = $recepttotalintinal;
        $MComreceipt->shipcost = $companytotalship;
        $MComreceipt->status = $receptstatus;
        $MComreceipt->del = 0;
        $MComreceipt->clientid = $compclient;
        $MComreceipt->totalCost = $totalcost;

        $MComreceiptDAO->update($MComreceipt);


        /* =======================================update product=================================================== */
        //variable for MComreceiptdetail
        for ($h = 1; $h <= $clientitr; $h++) {
            $product = $_POST['product' . $h . ''];
            $clientserial = $_POST['client_serial' . $h . ''];
            $clientsellbilltype = $_POST['client_sellbill_type' . $h . ''];
            $clientsellbillserial = $_POST['client_sellbill_serial' . $h . ''];
            $clientsellbilldate = $_POST['client_sellbill_date' . $h . ''];
            $clientbuybillserial = $_POST['client_buybill_serial' . $h . ''];
            $clientbuybilltype = $_POST['client_buybill_type' . $h . ''];
            $clientbuybilldate = $_POST['client_buybill_date' . $h . ''];
            $clientinsurceduration = $_POST['client_insurce_duration' . $h . ''];
            $optionsRadios = $_POST['optionsRadios' . $h . ''];
            $clineterror = $_POST['clinet_error' . $h . ''];
            $clientnote = $_POST['client_note' . $h . ''];
            $clientattch = $_POST['client_attch' . $h . ''];
            $clientintinalcost = $_POST['client_intinal_cost' . $h . ''];
            $clientintinalcostx = $_POST['client_intinal_costx' . $h . ''];
            $clientshipcost = $_POST['client_ship_cost' . $h . ''];
            $clientshipcostx = $_POST['client_ship_costx' . $h . ''];
            $clientsupplier = $_POST['client_supplier' . $h . ''];
            $productname = $_POST['productname' . $h . ''];
            $detailid = $_POST['detailid' . $h . ''];
            $stagefrom = $_POST['stagefrom' . $h . ''];
            $stageto = $_POST['stageto' . $h . ''];
            $currentstage = $_POST['currentstage' . $h . ''];
            $status = $_POST['status' . $h . ''];
            $cost = $_POST['cost' . $h . ''];
            $del = $_POST['del' . $h . ''];


            //print_r($detailid);
            //$alldetail = $MComreceiptdetailDAO->queryByOurserial($productname);
            //insert MComreceipt

            if (isset($product) && !empty($product) && $product != '-1' && $product != '') {

                //update del
                // $MComreceiptdetailEX->updatedetaildel(1,$MComreceiptId);
                // $MCheckEX->updatecheckdel(1,$productname);
                // $MProducthistoryEX->updatehistorydel(1,$productname);

                $newserial = $receptnumber . $h . xp;

                //insert MComreceiptdetail
                $MComreceiptdetail->receiptid = $MComreceiptId;
                $MComreceiptdetail->productserial = $clientserial;
                $MComreceiptdetail->productId = $product;
                $MComreceiptdetail->sellbillid = $clientsellbillserial;
                $MComreceiptdetail->sellbilldate = $clientsellbilldate;
                $MComreceiptdetail->buybillid = $clientbuybillserial;
                $MComreceiptdetail->buybilldate = $clientbuybilldate;
                $MComreceiptdetail->granttype = 1;
                $MComreceiptdetail->comment = $clientnote;
                $MComreceiptdetail->attach = $clientattch;
                $MComreceiptdetail->supplierid = $clientsupplier;
                $MComreceiptdetail->insduration = $clientinsurceduration;
                $MComreceiptdetail->insstatus = $optionsRadios;
                $MComreceiptdetail->problem = $clineterror;
                $MComreceiptdetail->branchid = $_SESSION['branchId'];


                //update MCheck
                $MCheck->branchId = $_SESSION['branchId'];
                $MCheck->checkDate = date("Y-m-d");
                $MCheck->userId = $_SESSION['userid'];

                //update MProducthistory
                $MProducthistory->branchId = $_SESSION['branchId'];
                $MProducthistory->productHistoryDate = date("Y-m-d H:i:s");
                $MProducthistory->userId = $_SESSION['userid'];
                $MProducthistory->comment = " تعديل فاتوره";

                if ($detailid != "") {
                    $alldata = $MComreceiptdetailEX->loaddata($detailid, 1);
                    $ourserial = $alldata->ourserial;
                    if (count($alldata) > 0) {
                        if ($del == 0) {

                            $MComreceiptdetail->id = $detailid;
                            $MComreceiptdetail->ourserial = $ourserial;
                            $MComreceiptdetail->del = 0;
                            $MComreceiptdetail->currentstage = $currentstage;
                            $MComreceiptdetail->initialcost = $clientintinalcost;
                            $MComreceiptdetail->shipcost = $clientshipcost;

                            //update MComreceiptdetail
                            $MComreceiptdetailDAO->update($MComreceiptdetail);

                            //update check
                            $allcheckdata = $MCheckEX->loadserial($ourserial);

                            $checkId = $allcheckdata->checkId;
                            $Serial = $allcheckdata->ourSerial;
                            $MCheck->stageFrom = $stagefrom;
                            $MCheck->checkId = $checkId;
                            $MCheck->ourSerial = $Serial;
                            $MCheck->del = 0;

                            $MCheckDAO->update($MCheck);

                            //update producthistory
                            $allhistory = $MProducthistoryEX->loadserial($ourserial);

                            $historyid = $allhistory->productHistoryId;
                            $historyserial = $allhistory->ourSerial;
                            $MProducthistory->ourSerial = $historyserial;
                            $MProducthistory->cost = $cost;
                            $MProducthistory->status = $status;
                            $MProducthistory->stageFrom = $stagefrom;
                            $MProducthistory->stageTo = $stageto;
                            $MProducthistory->del = 0;
                            $MProducthistory->productHistoryId = $historyid;

                            $MProducthistoryDAO->update($MProducthistory);
                        } else {
                            //$ourserial = $alldata->ourserial;
                            $MComreceiptdetail->id = $detailid;
                            $MComreceiptdetail->ourserial = $ourserial;
                            $MComreceiptdetail->del = 1;
                            $MComreceiptdetail->currentstage = $currentstage;
                            $MComreceiptdetail->initialcost = $clientintinalcostx;
                            $MComreceiptdetail->shipcost = $clientshipcostx;

                            //update MComreceiptdetail
                            $MComreceiptdetailDAO->update($MComreceiptdetail);

                            //update check
                            $allcheckdata = $MCheckEX->loadserial($ourserial);

                            $checkId = $allcheckdata->checkId;
                            $Serial = $allcheckdata->ourSerial;
                            $MCheck->stageFrom = $stagefrom;
                            $MCheck->checkId = $checkId;
                            $MCheck->ourSerial = $Serial;
                            $MCheck->del = 1;

                            $MCheckDAO->update($MCheck);

                            //update producthistory
                            $allhistory = $MProducthistoryEX->loadserial($ourserial);

                            print_r($allhistory);
                            $historyid = $allhistory->productHistoryId;
                            $historyserial = $allhistory->ourSerial;
                            $MProducthistory->ourSerial = $historyserial;
                            $MProducthistory->cost = $cost;
                            $MProducthistory->status = $status;
                            $MProducthistory->stageFrom = $stagefrom;
                            $MProducthistory->stageTo = $stageto;
                            $MProducthistory->del = 1;
                            $MProducthistory->productHistoryId = $historyid;

                            $MProducthistoryDAO->update($MProducthistory);
                        }
                    }
                } else {

                    $MComreceiptdetail->ourserial = $newserial;
                    $MComreceiptdetail->del = 0;
                    $MComreceiptdetail->currentstage = 1;
                    $MComreceiptdetail->initialcost = $clientintinalcost;
                    $MComreceiptdetail->shipcost = $clientshipcost;

                    // insert MComreceiptdetail
                    $MComreceiptdetailDAO->insert($MComreceiptdetail);

                    // insert check
                    $MCheck->del = 0;
                    $MCheck->ourSerial = $newserial;
                    $MCheck->stageFrom = 1;
                    $MCheckDAO->insert($MCheck);

                    //insert producthistory
                    $MProducthistory->ourSerial = $newserial;
                    $MProducthistory->cost = 0;
                    $MProducthistory->status = 0;
                    $MProducthistory->stageFrom = 1;
                    $MProducthistory->stageTo = 9;
                    $MProducthistory->del = 0;

                    $MProducthistoryDAO->insert($MProducthistory);
                }
            }
        }
        /* ========================================update company=========================================================== */

        /// insert for company insurance
        $companyitr = $_POST['company_insure_no'];
        $companytotalship = $_POST['company_total_ship'];
        $companytotalintial = $_POST['company_total_intial'];
        for ($r = 1; $r <= $companyitr; $r++) {
            $productcompany = $_POST['productcompany' . $r . ''];
            $companyserial = $_POST['company_serial' . $r . ''];
            $companybuybilltype = $_POST['company_buybill_type' . $r . ''];
            $companybuybillserial = $_POST['company_buybill_serial' . $r . ''];
            $companybuybilldate = $_POST['company_buybill_date' . $r . ''];
            $companyinsurceduration = $_POST['company_insurce_duration' . $r . ''];
            $companyoptionsRadios = $_POST['company_optionsRadios' . $r . ''];
            $companyerror = $_POST['company_error' . $r . ''];
            $companynote = $_POST['company_note' . $r . ''];
            $companyattch = $_POST['company_attch' . $r . ''];
            $companyshipcost = $_POST['company_ship_cost' . $r . ''];
            $companyshipcostx = $_POST['company_ship_costx' . $r . ''];
            $companyintinalcost = $_POST['company_intinal_cost' . $r . ''];
            $companyintinalcostx = $_POST['company_intinal_costx' . $r . ''];
            $companysupplier = $_POST['company_supplier' . $r . ''];
            $productname = $_POST['productname' . $r . ''];
            $companystagefrom = $_POST['companystagefrom' . $r . ''];
            $companystageto = $_POST['companystageto' . $r . ''];
            $companycurrentstage = $_POST['companycurrentstage' . $r . ''];
            $companystatus = $_POST['companystatus' . $r . ''];
            $companycost = $_POST['companycost' . $r . ''];
            $companydetailid = $_POST['companydetailid' . $r . ''];
            $delcompany = $_POST['delcompany' . $r . ''];

            //insert MComreceipt

            if (isset($productcompany) && !empty($productcompany) && $productcompany != '-1' && $productcompany != '') {

                //insert MComreceiptdetail
                $newcompanyserial = $receptnumber . $r . xc;

                $MComreceiptdetail->receiptid = $MComreceiptId;
                $MComreceiptdetail->productserial = $companyserial;
                $MComreceiptdetail->productId = $productcompany;
                $MComreceiptdetail->sellbillid = 0;
                $MComreceiptdetail->sellbilldate = "";
                $MComreceiptdetail->buybillid = $companybuybillserial;
                $MComreceiptdetail->buybilldate = $companybuybilldate;
                $MComreceiptdetail->granttype = 2;
                $MComreceiptdetail->comment = $companynote;
                $MComreceiptdetail->attach = $companyattch;
                $MComreceiptdetail->supplierid = $companysupplier;
                $MComreceiptdetail->insduration = $companyinsurceduration;
                $MComreceiptdetail->insstatus = $companyoptionsRadios;
                $MComreceiptdetail->problem = $companyerror;
                $MComreceiptdetail->branchid = $_SESSION['branchId'];

                //update MCheck
                $MCheck->branchId = $_SESSION['branchId'];
                $MCheck->checkDate = date("Y-m-d");
                $MCheck->userId = $_SESSION['userid'];

                //update MProducthistory
                $MProducthistory->branchId = $_SESSION['branchId'];
                $MProducthistory->productHistoryDate = date("Y-m-d H:i:s");
                $MProducthistory->userId = $_SESSION['userid'];
                $MProducthistory->comment = " تعديل فاتوره";

                if ($companydetailid != "") {
                    $allcompanydata = $MComreceiptdetailEX->loaddata($companydetailid, 2);
                    $ourserial = $allcompanydata->ourserial;
                    if (count($allcompanydata) > 0) {
                        if ($delcompany == 0) {

                            $MComreceiptdetail->id = $companydetailid;
                            $MComreceiptdetail->ourserial = $ourserial;
                            $MComreceiptdetail->del = 0;
                            $MComreceiptdetail->currentstage = $companycurrentstage;
                            $MComreceiptdetail->shipcost = $companyshipcost;
                            $MComreceiptdetail->initialcost = $companyintinalcost;


                            //update MComreceiptdetail
                            $MComreceiptdetailDAO->update($MComreceiptdetail);

                            //update check
                            $allcompanycheckdata = $MCheckEX->loadserial($ourserial);

                            $checkId = $allcompanycheckdata->checkId;
                            $Serial = $allcompanycheckdata->ourSerial;
                            $MCheck->stageFrom = $companystagefrom;
                            $MCheck->checkId = $checkId;
                            $MCheck->ourSerial = $Serial;
                            $MCheck->del = 0;

                            $MCheckDAO->update($MCheck);

                            //update producthistory
                            $allcompanyhistory = $MProducthistoryEX->loadserial($ourserial);

                            $historyid = $allcompanyhistory->productHistoryId;
                            $historyserial = $allcompanyhistory->ourSerial;
                            $MProducthistory->ourSerial = $historyserial;
                            $MProducthistory->cost = $companycost;
                            $MProducthistory->status = $companystatus;
                            $MProducthistory->stageFrom = $companystagefrom;
                            $MProducthistory->stageTo = $companystageto;
                            $MProducthistory->del = 0;
                            $MProducthistory->productHistoryId = $historyid;

                            $MProducthistoryDAO->update($MProducthistory);
                        } else {
                            $ourserial = $allcompanydata->ourserial;
                            $MComreceiptdetail->id = $companydetailid;
                            $MComreceiptdetail->ourserial = $ourserial;
                            $MComreceiptdetail->del = 1;
                            $MComreceiptdetail->currentstage = $companycurrentstage;
                            $MComreceiptdetail->shipcost = $companyshipcostx;
                            $MComreceiptdetail->initialcost = $companyintinalcostx;

                            //update MComreceiptdetail
                            $MComreceiptdetailDAO->update($MComreceiptdetail);

                            //update check
                            $allcompanycheckdata = $MCheckEX->loadserial($ourserial);

                            $checkId = $allcompanycheckdata->checkId;
                            $Serial = $allcompanycheckdata->ourSerial;
                            $MCheck->stageFrom = $companystagefrom;
                            $MCheck->checkId = $checkId;
                            $MCheck->ourSerial = $Serial;
                            $MCheck->del = 1;

                            $MCheckDAO->update($MCheck);

                            //update producthistory
                            $allcompanyhistory = $MProducthistoryEX->loadserial($ourserial);

                            $historyid = $allcompanyhistory->productHistoryId;
                            $historyserial = $allcompanyhistory->ourSerial;
                            $MProducthistory->ourSerial = $historyserial;
                            $MProducthistory->cost = $companycost;
                            $MProducthistory->status = $companystatus;
                            $MProducthistory->stageFrom = $companystagefrom;
                            $MProducthistory->stageTo = $companystageto;
                            $MProducthistory->del = 1;
                            $MProducthistory->productHistoryId = $historyid;

                            $MProducthistoryDAO->update($MProducthistory);
                        }
                    }
                } else {

                    $MComreceiptdetail->ourserial = $newcompanyserial;
                    $MComreceiptdetail->del = 0;
                    $MComreceiptdetail->currentstage = 1;
                    $MComreceiptdetail->shipcost = $companyshipcost;
                    $MComreceiptdetail->initialcost = $companyintinalcost;

                    // insert MComreceiptdetail
                    $MComreceiptdetailDAO->insert($MComreceiptdetail);

                    // insert check
                    $MCheck->del = 0;
                    $MCheck->ourSerial = $newcompanyserial;
                    $MCheck->stageFrom = 1;
                    $MCheckDAO->insert($MCheck);

                    //insert producthistory
                    $MProducthistory->ourSerial = $newcompanyserial;
                    $MProducthistory->cost = 0;
                    $MProducthistory->status = 0;
                    $MProducthistory->stageFrom = 1;
                    $MProducthistory->stageTo = 9;
                    $MProducthistory->del = 0;

                    $MProducthistoryDAO->insert($MProducthistory);
                }
            }
        }

        /* ====================================update mantance======================================================= */
        /// insert for manitannce
        $mantanceitr = $_POST['mantance_insure_no'];
        $mantancetotalintial = $_POST['mantance_total_intial'];
        for ($x = 1; $x <= $mantanceitr; $x++) {

            $newmantanceserial = $receptnumber . $x . xm;

            $productmantance = $_POST['productmantance' . $x . ''];
            $mantanceserial = $_POST['mantance_serial' . $x . ''];

            $mantancedescription = $_POST['mantance_description' . $x . ''];
            $mantanceerror = $_POST['mantance_error' . $x . ''];
            $mantancenote = $_POST['mantance_note' . $x . ''];
            $mantanceattch = $_POST['mantance_attch' . $x . ''];
            $mantanceintinalcost = $_POST['mantance_intinal_cost' . $x . ''];
            $mantanceintinalcostx = $_POST['mantance_intinal_costx' . $x . ''];
            $productname = $_POST['productname' . $x . ''];
            $mancedetailid = $_POST['mancedetailid' . $x . ''];
            $mantancestagefrom = $_POST['mantancestagefrom' . $x . ''];
            $mantancestageto = $_POST['mantancestageto' . $x . ''];
            $mantancecurrentstage = $_POST['mantancecurrentstage' . $x . ''];
            $mantancestatus = $_POST['mantancestatus' . $x . ''];
            $mantancecost = $_POST['mantancecost' . $x . ''];
            $delmantance = $_POST['delmantance' . $x . ''];

            //$alldetail = $MComreceiptdetailDAO->queryByOurserial($productname);

            if (isset($productmantance) && !empty($productmantance) && $productmantance != '-1' && $productmantance != '') {

                //insert MComreceiptdetail
                $MComreceiptdetail->receiptid = $MComreceiptId;
                $MComreceiptdetail->productserial = $mantanceserial;
                $MComreceiptdetail->productId = $productmantance;
                $MComreceiptdetail->sellbillid = 0;
                $MComreceiptdetail->sellbilldate = "";
                $MComreceiptdetail->buybillid = 0;
                $MComreceiptdetail->buybilldate = "";
                $MComreceiptdetail->granttype = 0;
                $MComreceiptdetail->comment = $mantancenote;
                $MComreceiptdetail->attach = $mantanceattch;
                $MComreceiptdetail->supplierid = 0;
                $MComreceiptdetail->insduration = 0;
                $MComreceiptdetail->insstatus = 0;
                $MComreceiptdetail->problem = $mantanceerror;
                $MComreceiptdetail->shipcost = 0;
                $MComreceiptdetail->branchid = $_SESSION['branchId'];

                //update MCheck
                $MCheck->branchId = $_SESSION['branchId'];
                $MCheck->checkDate = date("Y-m-d");
                $MCheck->userId = $_SESSION['userid'];

                //update MProducthistory
                $MProducthistory->branchId = $_SESSION['branchId'];
                $MProducthistory->productHistoryDate = date("Y-m-d H:i:s");
                $MProducthistory->userId = $_SESSION['userid'];
                $MProducthistory->comment = " تعديل فاتوره";

                if ($mancedetailid != "") {
                    $allmantancedata = $MComreceiptdetailEX->loaddata($mancedetailid, 0);
                    $ourserial = $allmantancedata->ourserial;
                    if (count($allmantancedata) > 0) {
                        if ($delmantance == 0) {

                            $MComreceiptdetail->id = $mancedetailid;
                            $MComreceiptdetail->ourserial = $ourserial;
                            $MComreceiptdetail->del = 0;
                            $MComreceiptdetail->currentstage = $mantancecurrentstage;
                            $MComreceiptdetail->initialcost = $mantanceintinalcost;


                            //update MComreceiptdetail
                            $MComreceiptdetailDAO->update($MComreceiptdetail);

                            //update check
                            $allmantancecheckdata = $MCheckEX->loadserial($ourserial);

                            $checkId = $allmantancecheckdata->checkId;
                            $Serial = $allmantancecheckdata->ourSerial;
                            $MCheck->stageFrom = $mantancestagefrom;
                            $MCheck->checkId = $checkId;
                            $MCheck->ourSerial = $Serial;
                            $MCheck->del = 0;

                            $MCheckDAO->update($MCheck);

                            //update producthistory
                            $allmantancehistory = $MProducthistoryEX->loadserial($ourserial);

                            $historyid = $allmantancehistory->productHistoryId;
                            $historyserial = $allmantancehistory->ourSerial;
                            $MProducthistory->ourSerial = $historyserial;
                            $MProducthistory->cost = $mantancecost;
                            $MProducthistory->status = $mantancestatus;
                            $MProducthistory->stageFrom = $mantancestagefrom;
                            $MProducthistory->stageTo = $mantancestageto;
                            $MProducthistory->del = 0;
                            $MProducthistory->productHistoryId = $historyid;

                            $MProducthistoryDAO->update($MProducthistory);
                        } else {
                            $ourserial = $allmantancedata->ourserial;
                            $MComreceiptdetail->id = $mancedetailid;
                            $MComreceiptdetail->ourserial = $ourserial;
                            $MComreceiptdetail->del = 1;
                            $MComreceiptdetail->currentstage = $mantancecurrentstage;
                            $MComreceiptdetail->initialcost = $mantanceintinalcostx;


                            //update MComreceiptdetail
                            $MComreceiptdetailDAO->update($MComreceiptdetail);

                            //update check
                            $allmantancecheckdata = $MCheckEX->loadserial($ourserial);

                            $checkId = $allmantancecheckdata->checkId;
                            $Serial = $allmantancecheckdata->ourSerial;
                            $MCheck->stageFrom = $mantancestagefrom;
                            $MCheck->checkId = $checkId;
                            $MCheck->ourSerial = $Serial;
                            $MCheck->del = 1;

                            $MCheckDAO->update($MCheck);

                            //update producthistory
                            $allmantancehistory = $MProducthistoryEX->loadserial($ourserial);

                            $historyid = $allmantancehistory->productHistoryId;
                            $historyserial = $allmantancehistory->ourSerial;
                            $MProducthistory->ourSerial = $historyserial;
                            $MProducthistory->cost = $mantancecost;
                            $MProducthistory->status = $mantancestatus;
                            $MProducthistory->stageFrom = $mantancestagefrom;
                            $MProducthistory->stageTo = $mantancestageto;
                            $MProducthistory->del = 1;
                            $MProducthistory->productHistoryId = $historyid;

                            $MProducthistoryDAO->update($MProducthistory);
                        }
                    }
                } else {

                    $MComreceiptdetail->ourserial = $newmantanceserial;
                    $MComreceiptdetail->del = 0;
                    $MComreceiptdetail->currentstage = 1;

                    // insert MComreceiptdetail
                    $MComreceiptdetailDAO->insert($MComreceiptdetail);

                    // insert check
                    $MCheck->del = 0;
                    $MCheck->ourSerial = $newmantanceserial;
                    $MCheck->stageFrom = 1;
                    $MCheckDAO->insert($MCheck);

                    //insert producthistory
                    $MProducthistory->ourSerial = $newmantanceserial;
                    $MProducthistory->cost = 0;
                    $MProducthistory->status = 0;
                    $MProducthistory->stageFrom = 1;
                    $MProducthistory->stageTo = 9;
                    $MProducthistory->del = 0;

                    $MProducthistoryDAO->insert($MProducthistory);
                }
            }
        }

        /* ======================================updat client debt ================================= */

        $companytotalship = $_POST['recept_total_shop'];
        if ($totalship > 0) {

            //get saveFromValue before and saveFromValue after mins transfermoneyValue
            $clientDatavalue = getdebtValueAndMins($compclient, $totalship);
            $clientId = $clientDatavalue[0];
            $debtValuebefore = $clientDatavalue[1];
            $debtValueafter = $clientDatavalue[2];

            $Client->clientdebt = $debtValueafter;
            $Client->userid = $_SESSION['userid'];
            $Client->clientdate = date("Y-m-d");
            $Client->clientid = $clientId;

            $ClientEX->updatedept($Client);
            $ClientdebtchangeDAO->deleteByClientdebtchangemodelid($MComreceiptId);
        }


        if ($debtValueafter > 0) {
            $debtAfter = $debtValueafter + $companytotalship;
        } else {
            $debtAfter = $debtValueafter;
        }

        $Client->clientdebt = $debtAfter;
        $Client->userid = $_SESSION['userid'];
        $Client->clientdate = date("Y-m-d");
        $Client->clientid = $clientId;

        $ClientEX->updatedept($Client);


        $Clientdebtchange->clientdebtchangeafter = $debtAfter;
        $Clientdebtchange->clientdebtchangeamount = $companytotalship;
        $Clientdebtchange->clientdebtchangebefore = $debtValueafter;
        $Clientdebtchange->clientdebtchangedate = date("Y-m-d");
        $Clientdebtchange->clientdebtchangemodelid = $MComreceiptId;
        $Clientdebtchange->clientdebtchangetype = 0;
        $Clientdebtchange->tablename = "billreceiptController.php";
        $Clientdebtchange->userid = $_SESSION['userid'];
        $Clientdebtchange->clientid = $compclient;
        $Clientdebtchange->processname = "تعديل فاتورة استلام صيانة";
        $Clientdebtchange->totalOperationCost = $companytotalship;


        //Insert Clientdebtchange
        $clientdebtchangeId = $ClientdebtchangeDAO->insert($Clientdebtchange);


        $MMaintenancereceipt->branchId = $_SESSION['branchId'];
        $MMaintenancereceipt->receiptId = $MComreceiptId;
        $MMaintenancereceipt->type = 0;
        $MMaintenancereceipt->maintenanceReceiptDAte = date("Y-m-d");
        $MMaintenancereceipt->totalCost = $companytotalship;
        $MMaintenancereceipt->userId = $_SESSION['userid'];
        $MMaintenancereceipt->del = 0;

        $MMaintenancereceiptEX->updatemantannce($MMaintenancereceipt);
    }
}

function delete() {
    global $MComreceiptdetailDAO;
    global $MComreceiptdetail;
    global $MComreceiptDAO;
    global $MComreceiptdetailEX;
    global $MComreceipt;
    global $MComreceiptEX;
    global $MCheckEX;
    global $MCheck;
    global $MProducthistoryEX;
    global $MProducthistory;
    global $Client;
    global $ClientEX;
    global $ClientdebtchangeDAO;
    global $MMaintenancereceiptEX;
    global $MMaintenancereceipt;



    $receptid = $_GET['id'];

    $allreeiptdata = $MComreceiptDAO->load($receptid);
    $totalship = $allreeiptdata->shipcost;
    $compclient = $allreeiptdata->clientid;
    print_r($allreeiptdata);

    $MComreceipt->id = $receptid;
    $MComreceipt->branchid = $_SESSION['branchId'];
    $MComreceipt->userid = $_SESSION['userid'];
    $MComreceipt->del = 1;
    $MComreceipt->receiptdat = date("Y-m-d");

    $MComreceiptEX->updatedel($MComreceipt);

    /////
    $MMaintenancereceipt->receiptId = $receptid;
    $MMaintenancereceipt->userId = $_SESSION['userid'];
    $MMaintenancereceipt->branchId = $_SESSION['branchId'];
    $MMaintenancereceipt->del = 1;
    $MMaintenancereceipt->maintenanceReceiptDAte = date("Y-m-d");

    $MMaintenancereceiptEX->updatedelete($MMaintenancereceipt);

    //print_r($MComreceipt);

    $allreceptdetail = $MComreceiptdetailDAO->queryByReceiptid($receptid);

    //print_r($allreceptdetail);

    foreach ($allreceptdetail as $datadetail) {

        $alldata = $MComreceiptdetailEX->loadfordelete($datadetail->ourserial);
        $checkid = $alldata->checkId;
        $historyid = $alldata->productHistoryId;
        $receptdetailid = $alldata->id;


        //del MComreceiptdetail
        $MComreceiptdetail->id = $receptdetailid;
        $MComreceiptdetail->branchid = $_SESSION['branchId'];
        $MComreceiptdetail->del = 1;

        $MComreceiptdetailEX->updatedetaildelx($MComreceiptdetail);

        //del m_check
        $MCheck->branchId = $_SESSION['branchId'];
        $MCheck->userId = $_SESSION['userid'];
        $MCheck->checkDate = date("Y-m-d");
        $MCheck->checkId = $checkid;
        $MCheck->del = 1;

        $MCheckEX->updatecheckdelx($MCheck);

        //del MProducthistory
        $MProducthistory->branchId = $_SESSION['branchId'];
        $MProducthistory->userId = $_SESSION['userid'];
        $MProducthistory->productHistoryDate = date("Y-m-d H:i:s");
        $MProducthistory->productHistoryId = $historyid;
        $MProducthistory->del = 1;

        $MProducthistoryEX->updatehistorydelx($MProducthistory);
    }


    if ($totalship > 0) {
        print_r($totalship);
        //get saveFromValue before and saveFromValue after mins transfermoneyValue
        $clientDatavalue = getdebtValueAndMins($compclient, $totalship);
        $clientId = $clientDatavalue[0];
        $debtValuebefore = $clientDatavalue[1];
        $debtValueafter = $clientDatavalue[2];
        print_r($clientDatavalue);
        $Client->clientdebt = $debtValueafter;
        $Client->userid = $_SESSION['userid'];
        $Client->clientdate = date("Y-m-d");
        $Client->clientid = $clientId;

        $ClientEX->updatedept($Client);

        print_r($Client);
        $ClientdebtchangeDAO->deleteByClientdebtchangemodelid($MComreceiptId);
    }
}

function getdebtValueAndMins($clientid, $clientvalue) {

    //to use the variable out side the funcion
    global $Client;
    global $ClientDAO;

    $clientDatavalue = $ClientDAO->load($clientid);

    $clientId = $clientDatavalue->clientid;
    $debtValuebefore = $clientDatavalue->clientdebt;

    $debtValueafter = $debtValuebefore - $clientvalue;

    return array($clientId, $debtValuebefore, $debtValueafter);
}

//here will create random dealerid
function gen_random_string($length = 16) {
    $chars = "1234567890"; //length:36//ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
    $final_rand = "";
    for ($i = 0; $i < $length; $i++) {
        $final_rand .= $chars[rand(0, strlen($chars) - 1)];
    }
    return $final_rand;
}

$dealerid = gen_random_string(5);
?>