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

session_start();
ob_start();

//global varable
global $showoutside;

//to check if the page from .htacess
//$showoutside = $_GET['sn'];
// get the config file
include_once("../public/config.php");

//here the db files that include in the file
include("../public/include_dao.php");
//Sellbill
require_once('../models/dao/SellbillDAO.class.php');
require_once('../models/dto/Sellbill.class.php');
require_once('../models/mysql/SellbillMySqlDAO.class.php');
require_once('../models/mysql/ext/SellbillMySqlExtDAO.class.php');

//Returnsellbill
require_once('../models/dao/ReturnsellbillDAO.class.php');
require_once('../models/dto/Returnsellbill.class.php');
require_once('../models/mysql/ReturnsellbillMySqlDAO.class.php');
require_once('../models/mysql/ext/ReturnsellbillMySqlExtDAO.class.php');


//Sellbillandrutern
require_once('../models/dao/SellbillandruternDAO.class.php');
require_once('../models/dto/Sellbillandrutern.class.php');
require_once('../models/mysql/SellbillandruternMySqlDAO.class.php');
require_once('../models/mysql/ext/SellbillandruternMySqlExtDAO.class.php');



//Sellbilldetail
require_once('../models/dao/SellbilldetailDAO.class.php');
require_once('../models/dto/Sellbilldetail.class.php');
require_once('../models/mysql/SellbilldetailMySqlDAO.class.php');
require_once('../models/mysql/ext/SellbilldetailMySqlExtDAO.class.php');


//Returnsellbilldetail
require_once('../models/dao/ReturnsellbilldetailDAO.class.php');
require_once('../models/dto/Returnsellbilldetail.class.php');
require_once('../models/mysql/ReturnsellbilldetailMySqlDAO.class.php');
require_once('../models/mysql/ext/ReturnsellbilldetailMySqlExtDAO.class.php');

//Sellandruternbilldetail
require_once('../models/dao/SellandruternbilldetailDAO.class.php');
require_once('../models/dto/Sellandruternbilldetail.class.php');
require_once('../models/mysql/SellandruternbilldetailMySqlDAO.class.php');
require_once('../models/mysql/ext/SellandruternbilldetailMySqlExtDAO.class.php');



//Storereport
require_once('../models/dao/StorereportDAO.class.php');
require_once('../models/dto/Storereport.class.php');
require_once('../models/mysql/StorereportMySqlDAO.class.php');
require_once('../models/mysql/ext/StorereportMySqlExtDAO.class.php');
//Storedetail
require_once('../models/dao/StoredetailDAO.class.php');
require_once('../models/dto/Storedetail.class.php');
require_once('../models/mysql/StoredetailMySqlDAO.class.php');
require_once('../models/mysql/ext/StoredetailMySqlExtDAO.class.php');
//Store
require_once('../models/dao/StoreDAO.class.php');
require_once('../models/dto/Store.class.php');
require_once('../models/mysql/StoreMySqlDAO.class.php');
require_once('../models/mysql/ext/StoreMySqlExtDAO.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');
//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');
//Billname
require_once('../models/dao/BillnameDAO.class.php');
require_once('../models/dto/Billname.class.php');
require_once('../models/mysql/BillnameMySqlDAO.class.php');
require_once('../models/mysql/ext/BillnameMySqlExtDAO.class.php');
//Billsetting
require_once('../models/dao/BillsettingsDAO.class.php');
require_once('../models/dto/Billsetting.class.php');
require_once('../models/mysql/BillsettingsMySqlDAO.class.php');
require_once('../models/mysql/ext/BillsettingsMySqlExtDAO.class.php');
//User
require_once('../models/dao/UserDAO.class.php');
require_once('../models/dto/User.class.php');
require_once('../models/mysql/UserMySqlDAO.class.php');
require_once('../models/mysql/ext/UserMySqlExtDAO.class.php');
//Productunit
require_once('../models/dao/ProductunitDAO.class.php');
require_once('../models/dto/Productunit.class.php');
require_once('../models/mysql/ProductunitMySqlDAO.class.php');
require_once('../models/mysql/ext/ProductunitMySqlExtDAO.class.php');
//Productcat
require_once('../models/dao/ProductcatDAO.class.php');
require_once('../models/dto/Productcat.class.php');
require_once('../models/mysql/ProductcatMySqlDAO.class.php');
require_once('../models/mysql/ext/ProductcatMySqlExtDAO.class.php');
//Unit
require_once('../models/dao/UnitDAO.class.php');
require_once('../models/dto/Unit.class.php');
require_once('../models/mysql/UnitMySqlDAO.class.php');
require_once('../models/mysql/ext/UnitMySqlExtDAO.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');
//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');
//Soldserialproduct
require_once('../models/dao/SoldserialproductDAO.class.php');
require_once('../models/dto/Soldserialproduct.class.php');
require_once('../models/mysql/SoldserialproductMySqlDAO.class.php');
require_once('../models/mysql/ext/SoldserialproductMySqlExtDAO.class.php');

//user
require_once('../models/dao/UserDAO.class.php');
require_once('../models/dto/User.class.php');
require_once('../models/mysql/UserMySqlDAO.class.php');
require_once('../models/mysql/ext/UserMySqlExtDAO.class.php');

//Usergroup
require_once('../models/dao/UsergroupDAO.class.php');
require_once('../models/dto/Usergroup.class.php');
require_once('../models/mysql/UsergroupMySqlDAO.class.php');
require_once('../models/mysql/ext/UsergroupMySqlExtDAO.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");
/* ======================

  Controller Name :- sellbillController

  OPERTATION in Controller

  1-display add form
  2- add in sellbill, sellbilldetail, sellbillandrutern, sellandruternbilldetail, returnsellbill, returnsellbilldetail tbl
  3- display sucess or error

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

//handling saveid session
/* if(!isset($_SESSION['saveid']))
  {
  $_SESSION['saveid'] = 1;
  }

 */
//here the global templates
//$smarty->display("headbeil.html");
$smarty->display("header.html");
//here goes the instances and general variables
//Sellbill
$mySellbill = new Sellbill();
$mySellbillRecord = new SellbillMySqlDAO();
$mySellbillEx = new SellbillMySqlExtDAO();
//Sellbilldetail
$mySellbilldetail = new Sellbilldetail();
$mySellbilldetailRecord = new SellbilldetailMySqlDAO();
$mySellbilldetailEx = new SellbilldetailMySqlExtDAO();
//Returnsellbill
$myReturnsellbill = new Returnsellbill();
$myReturnsellbillRecord = new ReturnsellbillMySqlDAO();
$myReturnsellbillEx = new ReturnsellbillMySqlExtDAO();
//Returnsellbilldetail
$myReturnsellbilldetail = new Returnsellbilldetail();
$myReturnsellbilldetailRecord = new ReturnsellbilldetailMySqlDAO();
//Sellbillandrutern
$mySellbillandrutern = new Sellbillandrutern();
$mySellbillandruternRecord = new SellbillandruternMySqlDAO();
$mySellbillandruternEx = new SellbillandruternMySqlExtDAO();
//Sellandruternbilldetail
$mySellandruternbilldetail = new Sellandruternbilldetail();
$mySellandruternbilldetailRecord = new SellandruternbilldetailMySqlDAO();
//Storereport
$myStorereport = new Storereport();
$myStorereportRecord = new StorereportMySqlDAO();
$myStorereportEx = new StorereportMySqlExtDAO();
//Storedetail
$myStoredetailEx = new StoredetailMySqlExtDAO();
$myStoredetailRecord = new StoredetailMySqlDAO();
//Store
$myStoreRecord = new StoreMySqlDAO();
//Product
$myProductRecord = new ProductMySqlDAO();
$myProductEx = new ProductMySqlExtDAO();
//save
$mySave = new Save();
$mySaveRecord = new SaveMySqlDAO();
$mySaveEx = new SaveMySqlExtDAO();
//savedaily
$mySavedaily = new Savedaily();
$mySavedailyRecord = new SavedailyMySqlDAO();
$mySavedailyEx = new SavedailyMySqlExtDAO();
//Client
$myClientRecord = new ClientMySqlDAO();
$myClient = new Client();
$myClientEx = new ClientMySqlExtDAO();
//Clientdebtchange
$myClientdebtchange = new Clientdebtchange();
$myClientdebtchangeRecord = new ClientdebtchangeMySqlDAO();
//Billname
$myBillnameRecord = new BillnameMySqlDAO();
$myBillnameEx = new BillnameMySqlExtDAO();
//Billsetting
$myBillsettingEx = new BillsettingsMySqlExtDAO();
//User
$myUserEx = new UserMySqlExtDAO();

//Productunit
$myProductunitEx = new ProductunitMySqlExtDAO();
//$Productunit= new Productunit();
//$ProductunitDAO= new ProductunitDAO();
//Productcat
$myProductcatRecord = new ProductcatMySqlDAO();
$myProductcatEx = new ProductcatMySqlExtDAO();
//Unit
$myUnitRecord = new UnitMySqlDAO();
//Productserial
$myProductserial = new Productserial();
$myProductserialEx = new ProductserialMySqlExtDAO();
//Programsetting
$ProgramsettingDAO = new ProgramsettingsMySqlDAO();
$programSettingExt = new ProgramsettingsMySqlExtDAO();
//Soldserialproduct
$soldSerialProduct = new Soldserialproduct();
$soldSerialProductDAO = new SoldserialproductMySqlDAO();
$soldSerialProductExt = new SoldserialproductMySqlExtDAO();

$user = new User();
$userDAO = new UserMySqlDAO();
$userEX = new UserMySqlExtDAO();

//Usergroup
$Usergroup = new Usergroup();
$UsergroupDAO = new UsergroupMySqlDAO();
$UsergroupEX = new UsergroupMySqlExtDAO();


$Usergroupdata = $UsergroupDAO->load($_SESSION['usergroupid']);
$smarty->assign("Usergroupdata", $Usergroupdata);


$userdata = $userDAO->load($_SESSION['userid']);
$smarty->assign("userdata", $userdata);
//check and use the condition that suites this action
if (empty($do)) {
    //here the permission check
    //include_once("../public/authentication.php");

    $billnameData = loadBillname();
    $smarty->assign("billnameData", $billnameData);

    //here the smarty templates
    $smarty->assign("customsellbill2", 1);
    $smarty->display("sellbillview/bill2.html");
} elseif ($do == "addsellBill") {
    //here the permission check
    //include_once("../public/authentication.php");

    $billnameId = $_GET['billnameid'];

    if ($billnameId != "") {
        $billsettingsData = loadBillProperty($billnameId);

        /* print_r("<pre>");
          print_r($billsettingsData);
          print_r("<pre>"); */


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

        //	$productData=loadProduct();
        //	$smarty->assign("productData",$productData);
        //$returnproductData=loadProduct();
        //	$smarty->assign("returnproductData",$returnproductData);

        $clientData = loadClient();
        $smarty->assign("clientData", $clientData);

        $storeData = loadStore();
        $smarty->assign("storeData", $storeData);

        $sellerData = loadseller();
        $smarty->assign("sellerData", $sellerData);

        $smarty->assign("billnameid", $billnameId);

        $billnameAddData = $myBillnameRecord->load($billnameId);
        $smarty->assign("billnameAddData", $billnameAddData);



        //get product categories
        $categoriesData = loadProductCat();
        $smarty->assign("categoriesData", $categoriesData);

        //get units
        $unitsData = loadUnit();
        $smarty->assign("unitsData", $unitsData);

        $smarty->assign("today", date("Y-m-d"));

        $roundNumbersStatus = checkRoundNumbers();
        $smarty->assign("roundNumbersStatus", $roundNumbersStatus);

        $previousPriceStatus = checkPreviousPrice();
        $smarty->assign("previousPriceStatus", $previousPriceStatus);


        $Programsettingdata = $ProgramsettingDAO->load(1);
        $smarty->assign("Programsettingdata", $Programsettingdata);
        //here the smarty templates
        //$smarty->display("sellbillview/billonw.html");
        $smarty->display("sellbillview/bill.html");
        $smarty->assign("customsellbill", 1);
        $smarty->assign("customreturnsellbill", 1);
        $smarty->assign("customsellbillModel", 1);
        $smarty->assign("customSellProductModel", 1);
    } else {
        header("location:sellbillController.php");
    }
} elseif ($do == "add") {
    //include_once("../public/authentication.php");

    $url = "";

    $addData = add();
    $sellbillId = $addData[0];
    $returnsellbillId = $addData[1];
    $productsBeforeValid = $addData[2];
    $saveValuebeforeValid = $addData[3];
    $billtype = $addData[4];

    if ($productsBeforeValid == 1 && $saveValuebeforeValid == 1) {
        if ($billtype == 1) {
            header("location:sellbillController.php?do=editprint&id=" . $sellbillId . "");
        } elseif ($billtype == 2) {
            header("location:returnsellbillController.php?do=editprint&id=" . $returnsellbillId . "");
        } elseif ($billtype == 3) {
            header("location:sellbillandruternController.php?do=editprint&id=" . $sellbillId . "");
        }

        //$smarty->assign('urldirect', $url);
        //$note ="تمت العملية بنجاح";
        //$smarty->assign('msgnote', $note);
        //$smarty->display("notes2.html");
    } elseif ($productsBeforeValid == 0 && $saveValuebeforeValid == 1) {
        if ($billtype == 1) {
            $url = "sellbillController.php?do=editprint&id=" . $sellbillId . "";
        } elseif ($billtype == 2) {
            $url = "returnsellbillController.php?do=editprint&id=" . $returnsellbillId . "";
        } elseif ($billtype == 3) {
            $url = "sellbillandruternController.php?do=editprint&id=" . $sellbillId . "";
        }
        $smarty->assign('urldirect', $url);
        $note = "لا يوجد كمية بالمخزن كافية للفاتورة";
        $smarty->assign('msgnote', $note);
        $smarty->display("notes2.html");
    } elseif ($productsBeforeValid == 1 && $saveValuebeforeValid == 0) {
        if ($billtype == 1) {
            $url = "sellbillController.php?do=editprint&id=" . $sellbillId . "";
        } elseif ($billtype == 2) {
            $url = "returnsellbillController.php?do=editprint&id=" . $returnsellbillId . "";
        } elseif ($billtype == 3) {
            $url = "sellbillandruternController.php?do=editprint&id=" . $sellbillId . "";
        }

        $smarty->assign('urldirect', $url);
        $note = "لا يوجد كمية بالخزنة المبلغ الكافى للفاتورة";
        $smarty->assign('msgnote', $note);
        $smarty->display("notes2.html");
    } elseif ($productsBeforeValid == 0 && $saveValuebeforeValid == 0) {
        if ($billtype == 1) {
            $url = "sellbillController.php?do=editprint&id=" . $sellbillId . "";
        } elseif ($billtype == 2) {
            $url = "returnsellbillController.php?do=editprint&id=" . $returnsellbillId . "";
        } elseif ($billtype == 3) {
            $url = "sellbillandruternController.php?do=editprint&id=" . $sellbillId . "";
        }

        $smarty->assign('urldirect', $url);
        $note = "لا يوجد كمية بالخزنة المبلغ الكافى ولا يوجد بالمخزن الكمية الكافية للفاتورة";
        $smarty->assign('msgnote', $note);
        $smarty->display("notes2.html");
    }
} elseif ($do == "addAndRetuen") {
    //include_once("../public/authentication.php");

    $billnameId = $_POST['billnameid'];

    $url = "";

    $addData = add();
    $sellbillId = $addData[0];
    $returnsellbillId = $addData[1];
    $productsBeforeValid = $addData[2];
    $saveValuebeforeValid = $addData[3];
    $billtype = $addData[4];

    if ($productsBeforeValid == 1 && $saveValuebeforeValid == 1) {
        if ($billtype == 1) {
            header("location:sellbillController.php?do=addsellBill&billnameid=" . $billnameId);
        } elseif ($billtype == 2) {
            header("location:sellbillController.php?do=addsellBill&billnameid=" . $billnameId);
        } elseif ($billtype == 3) {
            header("location:sellbillController.php?do=addsellBill&billnameid=" . $billnameId);
        }
//			$smarty->assign('urldirect', $url);
//			$note ="تمت العملية بنجاح";
//
//			$smarty->assign('msgnote', $note);
//			$smarty->display("notes2.html");
    } elseif ($productsBeforeValid == 0 && $saveValuebeforeValid == 1) {
        if ($billtype == 1) {
            $url = "sellbillController.php?do=addsellBill&billnameid=" . $billnameId;
        } elseif ($billtype == 2) {
            $url = "sellbillController.php?do=addsellBill&billnameid=" . $billnameId;
        } elseif ($billtype == 3) {
            $url = "sellbillController.php?do=addsellBill&billnameid=" . $billnameId;
        }

        $smarty->assign('urldirect', $url);
        $note = "لا يوجد كمية بالمخزن كافية للفاتورة";
        $smarty->assign('msgnote', $note);
        $smarty->display("notes2.html");
    } elseif ($productsBeforeValid == 1 && $saveValuebeforeValid == 0) {
        if ($billtype == 1) {
            $url = "sellbillController.php?do=addsellBill&billnameid=" . $billnameId;
        } elseif ($billtype == 2) {
            $url = "sellbillController.php?do=addsellBill&billnameid=" . $billnameId;
        } elseif ($billtype == 3) {
            $url = "sellbillController.php?do=addsellBill&billnameid=" . $billnameId;
        }
        $smarty->assign('urldirect', $url);
        $note = "لا يوجد كمية بالخزنة المبلغ الكافى للفاتورة";
        $smarty->assign('msgnote', $note);
        $smarty->display("notes2.html");
    } elseif ($productsBeforeValid == 0 && $saveValuebeforeValid == 0) {
        if ($billtype == 1) {
            $url = "sellbillController.php?do=addsellBill&billnameid=" . $billnameId;
        } elseif ($billtype == 2) {
            $url = "sellbillController.php?do=addsellBill&billnameid=" . $billnameId;
        } elseif ($billtype == 3) {
            $url = "sellbillController.php?do=addsellBill&billnameid=" . $billnameId;
        }

        $smarty->assign('urldirect', $url);
        $note = "لا يوجد كمية بالخزنة المبلغ الكافى ولا يوجد بالمخزن الكمية الكافية للفاتورة";
        $smarty->assign('msgnote', $note);
        $smarty->display("notes2.html");
    }
} elseif ($do == "editprint") {
    //include_once("../public/authentication.php");

    $sellbillId = $_GET['id'];

    $showDetailData = showDetail($sellbillId);

    $smarty->assign('sellbillData', $showDetailData[0]);
    $smarty->assign('sellbilldetailData', $showDetailData[1]);
    $smarty->assign("quantity", $showDetailData[2]);

    $billsettingsData = loadBillProperty($showDetailData[0]->billnameid);
    $smarty->assign("billsettingsData", $billsettingsData);


    $billnameAddData = $myBillnameRecord->load($showDetailData[0]->billnameid);
    $smarty->assign("billnameAddData", $billnameAddData);

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

    $smarty->display("sellbillview/editprint.html");
} elseif ($do == "showDetail") {
    //include_once("../public/authentication.php");

    $sellbillId = $_GET['sellbillid'];

    $showDetailData = showDetail($sellbillId);

    $smarty->assign('sellbillData', $showDetailData[0]);
    $smarty->assign('sellbilldetailData', $showDetailData[1]);
    $smarty->assign("quantity", $showDetailData[2]);

    if ($showDetailData[0]->billnameid != 0 && isset($showDetailData[0]->billnameid) && $showDetailData[0]->billnameid != "" && !empty($showDetailData[0]->billnameid)) {
        $billsettingsData = loadBillProperty($showDetailData[0]->billnameid);
        $smarty->assign("billsettingsData", $billsettingsData);

        $billnameAddData = $myBillnameRecord->load($showDetailData[0]->billnameid);
        $smarty->assign("billnameAddData", $billnameAddData);
    }
    $Programsettingdata = $ProgramsettingDAO->load(1);
    $smarty->assign("Programsettingdata", $Programsettingdata);


    /* 	print_r("<pre>");
      print_r($billsettingsData);
      print_r("<pre>"); */

    $smarty->display("sellbillview/showdetail.html");
} elseif ($do == "showDetail2") {
    //include_once("../public/authentication.php");

    $sellbillId = $_GET['sellbillid'];

    $showDetailData = showDetail($sellbillId);

    $smarty->assign('sellbillData', $showDetailData[0]);
    $smarty->assign('sellbilldetailData', $showDetailData[1]);
    $smarty->assign("quantity", $showDetailData[2]);

    $billsettingsData = loadBillProperty($showDetailData[0]->billnameid);
    $smarty->assign("billsettingsData", $billsettingsData);

    $billnameAddData = $myBillnameRecord->load($showDetailData[0]->billnameid);
    $smarty->assign("billnameAddData", $billnameAddData);

    $Programsettingdata = $ProgramsettingDAO->load(1);
    $smarty->assign("Programsettingdata", $Programsettingdata);
    //here the smarty templates
    $smarty->display("sellbillview/editprint2.html");
} elseif ($do == "delete") {
    //include_once("../public/authentication.php");

    $sellbillId = $_GET['sellbillid'];
    try {
        $saveValuebeforeValid = delete($sellbillId);
        if ($saveValuebeforeValid[0] == 1) {
            header("location:?do=sucess");
        } else {
            $url = "sellbillController.php?do=show";
            $smarty->assign('urldirect', $url);
            $note = "لا يوجد  بالخزنة المبلغ الكافى لحذف الفاتورة";
            $smarty->assign('msgnote', $note);
            $smarty->display("notes2.html");
        }
    } catch (Exception $e) {
        header("location:?do=error");
    }
} elseif ($do == "show") {
    //include_once("../public/authentication.php");

    $clientData = loadAllClient();
    $smarty->assign("clientData", $clientData);

    //$sellbillSearchData=loadAllSellBill();
    //$smarty->assign("sellbillSearchData",$sellbillSearchData);

    $startDate = $_REQUEST['from'];
    $endDate = $_REQUEST['to'];
    $sellbillSerial = $_REQUEST['sellbillserial'];
    $sellbillId = $_REQUEST['sellbillid'];
    $clientId = $_REQUEST['clientid'];

    if (isset($clientId) && $clientId != "-1") {
        showByClient($clientId);

        $messageData = $myClientRecord->load($clientId);
        $message = "إسم العميل: " . $messageData->clientname;
        $smarty->assign("message", $message);
    } elseif (isset($sellbillSerial) && $sellbillSerial != "") {
        showBySriral($sellbillSerial);

        $message = "رقم الفاتورة: " . $sellbillSerial;
        $smarty->assign("message", $message);
    } elseif (isset($sellbillId) && $sellbillId != "") {
        showBySellbillId($sellbillId);

        $message = "الرقم المتسلسل : " . $sellbillId;
        $smarty->assign("message", $message);
    } elseif (isset($startDate) && $startDate != "" && isset($endDate) && $endDate != "") {
        showByDate($startDate, $endDate);

        $message = "من تاريخ : " . $startDate . " إلى تاريخ : " . $endDate . "";
        $smarty->assign("message", $message);
    } else {
        showAll(date("Y-m-d"), date("Y-m-d"));

        $message = "من تاريخ : " . date("Y-m-d") . " إلى تاريخ : " . date("Y-m-d") . "";
        $smarty->assign("message", $message);
    }

    $smarty->display("sellbillview/show.html");
    $smarty->assign("customCheck", 1);
} elseif ($do == "executeOperation") {
    //include_once("../public/authentication.php");
    // here the function that do the action
    executeOperation();

    $clientData = loadAllClient();
    $smarty->assign("clientData", $clientData);

    $sellbillSearchData = loadAllSellBill();
    $smarty->assign("sellbillSearchData", $sellbillSearchData);

    showAll();

    $smarty->display("sellbillview/show.html");
    $smarty->assign("customCheck", 1);
} elseif ($do == "sucess") {
    //here the smarty templates
    $smarty->display("succes.html");
} elseif ($do == "error") {
    //here the smarty templates
    $smarty->display("error.html");
}
$smarty->assign("customValidation", 1);
//here the global templates
$smarty->display("footer.html");

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

function checkRoundNumbers() {
    global $programSettingExt;

    $roundNumbersStatus = $programSettingExt->getRoundNumbersStatus();
    return $roundNumbersStatus;
}

function checkPreviousPrice() {
    global $programSettingExt;

    $previousPriceStatus = $programSettingExt->getPreviousPriceStatus();
    return $previousPriceStatus;
}

//select data from client tbl
function loadClient() {
    global $myClientRecord;

    $clientData = $myClientRecord->queryByCondition(0);
    return $clientData;
}

//select data from client tbl
function loadAllClient() {
    global $myClientRecord;

    $clientData = $myClientRecord->queryAll();
    return $clientData;
}

function loadAllSellBill() {
    global $mySellbillRecord;

    // select all data from sellbill tbl
    $sellbillSearchData = $mySellbillRecord->queryAll();
    return $sellbillSearchData;
}

// select from Store tbl
function loadStore() {
    global $myStoreRecord;

    // select all data from Storetbl
    $storeData = $myStoreRecord->queryByConditions(0);
    return $storeData;
}

// select from Product tbl
function loadProduct() {
    global $myProductEx;

    // select all data from producttbl
    $productData = $myProductEx->queryWithCoditionsAndCatConditions();

    foreach ($productData as $product) {
        $productcatName;

        $product->productName = loadProductCatNameById($product->productCatId, $productcatName, 1) . "/" . $product->productName;
    }
    return $productData;
}

// select from Product tbl
function loadProductCatNameById($productCatId, $productcatName, $itr) {
    //to use the variable out side the funcion
    global $myProductcatRecord;

    $productcatNamex = $productcatName;
    // select all data from producttbl
    $productcatData = $myProductcatRecord->load($productCatId);

    if (count($productcatData) > 0) {
        if ($itr == 1) {
            $productcatNamex = $productcatData->productCatName;
        } elseif ($itr == 2) {
            $productcatNamex = $productcatData->productCatName . "/" . $productcatNamex;
        }

        if ($productcatData->productCatParent != 0) {
            return loadProductCatNameById($productcatData->productCatParent, $productcatNamex, 2);
        }
    }
    return $productcatNamex;
}

// select from Billname tbl
function loadBillname() {
    global $myBillnameEx;

    // select all data from Billname
    $billnameData = $myBillnameEx->queryByType(0);
    return $billnameData;
}

// select from Billname tbl
function loadBillProperty($billnameid) {
    global $myBillsettingEx;

    // select all data from Billname
    $billsettingsData = $myBillsettingEx->queryWithBillnameId($billnameid);
    return $billsettingsData;
}

// select from user tbl
function loadseller() {

    //to use the variable out side the funcion
    global $myUserEx;

    // select all data from user
    $userData = $myUserEx->queryWithLevel(3);

    return $userData;
}

//load sellbill detail
function showDetail($sellbillid) {
    //to use the variable out side the funcion
    global $mySellbillEx;
    global $mySellbilldetailEx;


    //load purchasebillreturn data by id
    $sellbillData = $mySellbillEx->loadSellBillById($sellbillid);

    $sellbilldetailData = $mySellbilldetailEx->queryWithSellBillId($sellbillid);

    //print_r($sellbilldetailData);
    $quantity = 0;
    foreach ($sellbilldetailData as $sellbilldetail) {
        $quantity = $quantity + ($sellbilldetail->sellbilldetailquantity);
    }



    return array($sellbillData, $sellbilldetailData, $quantity);
}

// add in sellbill tbl
function add() {
    global $mySellbill;
    global $mySellbillRecord;
    global $mySellbilldetail;
    global $mySellbilldetailRecord;

    global $myReturnsellbill;
    global $myReturnsellbillRecord;
    global $myReturnsellbilldetail;
    global $myReturnsellbilldetailRecord;


    global $mySellbillandrutern;
    global $mySellbillandruternRecord;
    global $mySellandruternbilldetail;
    global $mySellandruternbilldetailRecord;

    global $soldSerialProduct;
    global $soldSerialProductDAO;

    global $ProductunitDAO;
    global $Productunit;
    global $myProductunitEx;

    $billnameId = $_POST['billnameid'];

    $settingReturn = $_POST['settingreturn'];
    $settingClient = $_POST['settingclient'];
    $settingStore = $_POST['settingstore'];
    $settingSeller = $_POST['settingseller'];
    $settingParcode = $_POST['settingparcode'];
    $settingDate = $_POST['settingdate'];
    $settingNewproduct = $_POST['settingnewproduct'];
    $settingComment = $_POST['settingcomment'];
    $settingDiscount = $_POST['settingdiscount'];
    $settingQuantityCondition = $_POST['settingquantitycondition'];

    $tempClientName = $_POST['tempclientName'];

    $sellbillclientId = $_POST['supplier'];
    $sellbilltotaldeptBefor = $_POST['hidden_debt'];
    $sellbilltotaldeptAfter = $_POST['clientdebtafter'];
    $clientName = loadClientNameById($sellbillclientId);

    $sellbildirectPayment;
    //client
    if ($settingClient == 0 && $_POST['supplier'] != 1) {
        $sellbildirectPayment = 1;
    } else {
        $sellbildirectPayment = 0;
    }

    //Date
    if ($settingDate == 0) {
        $sellbillDate = $_POST['bn'];
    } else {
        $sellbillDate = date("Y-m-d");
    }

    //Store
    if ($settingStore == 0) {
        $sellbillstoreId = $_POST['storeid'];
    } else {
        $sellbillstoreId = $_POST['storeid'];
    }

    //seller
    if ($settingSeller == 0) {
        $sellerId = $_POST['sellerid'];
        if ($sellerId == "-1" || $sellerId == "" || $sellerId == NULL) {
            $sellerId = 0;
        }
    } else {
        $sellerId = 0;
    }

    $sellbillSerial = $_POST['bns'];
    $priceType = $_POST['hiddenpricetype'];

    $sellbilltotalBill = $_POST['sellbilltotalbill']; //اجمالى الفاتورة قبل الخصم
    $sellbillDiscountType = $_POST['distype']; //نوع الخصم

    $sellbillDiscount = $_POST['gendis']; //قيمة الخصم
    $sellbillAftertotalBill = $_POST['sellbillaftertotalbill']; //القيمة بعد الخصم
    $sellbilltotalPayed = $_POST['genpay']; //المدفوع
    $sellbillFinalbill = $sellbillAftertotalBill - $sellbilltotalPayed; //المتبقى

    $sellbillPrice = $_POST['sellbillprice']; //اجمالى المبيعات
    $returnsellbillPrice = $_POST['returnsellbillprice']; //اجمالى مردودات المبيعات

    $sellQuantity = $_POST['sellQuantity'];
    $returnsellQuantity = $_POST['returnsellQuantity'];


    $productsBeforeValid = 1;
    $saveValuebeforeValid = 1;

    //////////////////////////////////////////////////////////////////////////

    $sellbillItr = $_POST['hidden_itr'];
    $returnSellbillItr = $_POST['returnhidden_itr'];

    //check storerdetail value
    //check product quantity
    if ($sellbillPrice > 0) {
        $h = 1;
        for ($h = 1; $h <= $sellbillItr; $h++) {
            $productId = $_POST['product' . $h . ''];
            $productQuantity = $_POST['pronum' . $h . ''];

            if (isset($productId) && $productId != '-1' && $productId != "") {
                //get storefrom productquantity before change
                $storedetailData = getStoredetailData($sellbillstoreId, $productId);

                $allStoredetailData = $storedetailData[0];
                $storedetailId = $storedetailData[1];
                $productquantityBefore = $storedetailData[2];

                //check if productquantityFromBefore greater than 0 or not
                $productsBeforeValid = 1;
                if ($productquantityBefore > 0 && count($allStoredetailData) > 0) {
                    $productsAfterValid = $productquantityBefore - $productQuantity;

                    if ($productsAfterValid >= 0) {
                        $productsBeforeValid = 1;
                    } else {
                        $productsBeforeValid = 0;
                        //return $productsBeforeValid;
                    }
                } else {
                    $productsBeforeValid = 0;
                    //return $productsBeforeValid;
                }
            }
        }
    }




    /////////////////check savefrom value before transfer//////////////////////

    if ($returnsellbillPrice > 0 && ($sellbillPrice <= 0 || $sellbillAftertotalBill)) {
        $saveValueafterValid;

        //get save value
        $saveValueBefore = getSaveValueBefore($_SESSION['saveid']);

        $saveValueafterValid = $saveValueBefore - $sellbilltotalPayed;

        if ($saveValueafterValid >= 0) {
            $saveValuebeforeValid = 1;
        } else {
            $saveValuebeforeValid = 0;
            //return $saveValuebeforeValid;
        }
    }


    $billtype;

    //insert data into Sellbillandrutern tbl
    if ($sellbillPrice > 0 && $returnsellbillPrice > 0) {
        $billtype = 3; // مبيعات و ومردودات

        $mySellbillandrutern->billnameid = $billnameId;
        $mySellbillandrutern->conditions = 0;
        $mySellbillandrutern->sellbildirectpayment = $sellbildirectPayment;
        $mySellbillandrutern->sellbillaftertotalbill = $sellbillAftertotalBill;
        $mySellbillandrutern->sellbillclientid = $sellbillclientId;
        $mySellbillandrutern->sellbillclientname = $clientName;
        $mySellbillandrutern->sellbilldate = $sellbillDate;
        $mySellbillandrutern->sellbilldiscount = $sellbillDiscount;
        $mySellbillandrutern->sellbilldiscounttype = $sellbillDiscountType;
        $mySellbillandrutern->tempclientName = $tempClientName;
        $mySellbillandrutern->sellQuantity = $sellQuantity;
        $mySellbillandrutern->returnsellQuantity = $returnsellQuantity;


        if ($sellbilltotalBill < 0) {
            $sellbillFinalbill = $sellbillAftertotalBill + $sellbilltotalPayed;
        }
        $mySellbillandrutern->sellbillfinalbill = $sellbillFinalbill;
        $mySellbillandrutern->sellbillsaveid = $_SESSION['saveid'];
        $mySellbillandrutern->sellbillserial = $sellbillSerial;
        $mySellbillandrutern->sellbillstoreid = $sellbillstoreId;
        $mySellbillandrutern->sellbillsysdate = date("Y-m-d H:i:s");
        $mySellbillandrutern->sellbilltotalbill = $sellbilltotalBill;
        $mySellbillandrutern->sellbilltotaldeptafter = $sellbilltotaldeptAfter;
        $mySellbillandrutern->sellbilltotaldeptbefor = $sellbilltotaldeptBefor;
        $mySellbillandrutern->sellbilltotalpayed = $sellbilltotalPayed;
        $mySellbillandrutern->sellerid = $sellerId;
        $mySellbillandrutern->userid = $_SESSION['userid'];
        $mySellbillandrutern->sellbillprice = $sellbillPrice;
        $mySellbillandrutern->returnsellbillprice = $returnsellbillPrice;

        $sellbillId = $mySellbillandruternRecord->insert($mySellbillandrutern);



        //insert Sell Data into Sellandruternbilldetail tbl
        $sellbillItr = $_POST['hidden_itr'];
        $h = 1;
        for ($h = 1; $h <= $sellbillItr; $h++) {
            $parcode = $_POST['parcode' . $h . ''];
            $parcodeType = $_POST['parcodeType' . $h . ''];
            $productserailId = $_POST['productserailid' . $h . ''];
            $productId = $_POST['product' . $h . ''];
            $productunitId = $_POST['productunit' . $h . ''];
            $quantity = $_POST['pronum' . $h . ''];
            $price = $_POST['proprice' . $h . ''];
            $discountValue = $_POST['prodis' . $h . ''];
            $totalPrice = $_POST['hidden_prototal' . $h . ''];
            $comment = $_POST['comment' . $h . ''];
            $prstorid = $_POST['prstorid' . $h . ''];

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

                $sellbillstoreId = $prstorid;
            }


            if ($productunitId == "") {
                $productunitId = 0;
            }


            if (isset($productId) && $productId != '-1' && $productId != "") {


                $productData = loadProductById($productId);


                $mySellandruternbilldetail->buyprice = $productData->productBuyPrice;
                $mySellandruternbilldetail->discountvalue = $discountValue;
                $mySellandruternbilldetail->note = $comment;
                $mySellandruternbilldetail->parcode = $parcode;
                $mySellandruternbilldetail->pricetype = $priceType;
                $mySellandruternbilldetail->productunitid = $productunitId;
                $mySellandruternbilldetail->sellbilldetailcatid = $productData->productCatId;
                $mySellandruternbilldetail->sellbilldetaildate = date("Y-m-d");
                $mySellandruternbilldetail->sellbilldetailprice = $price;
                $mySellandruternbilldetail->sellbilldetailproductid = $productId;
                $mySellandruternbilldetail->sellbilldetailquantity = $quantity;
                $mySellandruternbilldetail->sellbilldetailtotalprice = $totalPrice;
                $mySellandruternbilldetail->sellbillid = $sellbillId;
                $mySellandruternbilldetail->selltype = 0; //مبيعات
                $mySellandruternbilldetail->buydiscount = $productData->buydiscount;
                $mySellandruternbilldetail->buydiscounttype = $productData->discounttype;
                $mySellandruternbilldetail->storeid = $sellbillstoreId;


                $mySellandruternbilldetailRecord->insert($mySellandruternbilldetail);


                $productunitData = loadProductUnitWithProductAndUnit($productId, $productunitId);
                $productnumber = $productunitData->productnumber;
                $finalquantity = $quantity * $productnumber;
                //update DON if is Serial
                if ($parcodeType == "serialnumber") {
                    //update don column (quantity) in productserial table
                    updateSerailQty($productserailId, $finalquantity);
                    insertIntoSoldSerialProduct($productserailId, $finalquantity, $sellbillId, 1);
                }



                //get store productquantity before change
                $storedetailData = getStoredetailData($sellbillstoreId, $productId);

                $allStoredetailData = $storedetailData[0];
                $storedetailId = $storedetailData[1];
                $productquantityBefore = $storedetailData[2];



                if (count($allStoredetailData) > 0) {

                    //update productquantity in storedetail
                    $productquantityAfter = decreaseProductQuantity($storedetailId, $productquantityBefore, $finalquantity);


                    //insert in storereport
                    insertStorereport($productId, $sellbillstoreId, $finalquantity, $productquantityBefore, $productquantityAfter, 1, $sellbillId, "اضافة فاتورة مبيعات", "sellbillandruternController.php");
                } else {
                    $specialQuantityAfter = $finalquantity * -1;

                    //insert in storedetail tbl
                    insertStoredetail($sellbillstoreId, $productId, $specialQuantityAfter);

                    //insert in storereport
                    insertStorereport($productId, $sellbillstoreId, $finalquantity, 0, $specialQuantityAfter, 1, $sellbillId, "اضافة فاتورة مبيعات", "sellbillandruternController.php");
                }
            }
        }



        //insert Return Data into Sellandruternbilldetail tbl
        $returnSellbillItr = $_POST['returnhidden_itr'];
        $h = 1;
        for ($h = 1; $h <= $returnSellbillItr; $h++) {
            $parcode = $_POST['returnparcode' . $h . ''];
            $parcodeType = $_POST['returnparcodeType' . $h . ''];
            $productserailId = $_POST['returnproductserailid' . $h . ''];
            $productId = $_POST['returnproduct' . $h . ''];
            $productunitId = $_POST['returnproductunit' . $h . ''];
            $quantity = $_POST['returnpronum' . $h . ''];
            $price = $_POST['returnproprice' . $h . ''];
            $discountValue = $_POST['returnprodis' . $h . ''];
            $totalPrice = $_POST['returnhidden_prototal' . $h . ''];
            $comment = $_POST['returncomment' . $h . ''];
            $reprstorid = $_POST['reprstorid' . $h . ''];

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

                $sellbillstoreId = $reprstorid;
            }

            if ($productunitId == "") {
                $productunitId = 0;
            }

            if (isset($productId) && $productId != '-1' && $productId != "") {
                $productData = loadProductById($productId);


                $mySellandruternbilldetail->buyprice = $productData->productBuyPrice;
                $mySellandruternbilldetail->discountvalue = $discountValue;
                $mySellandruternbilldetail->note = $comment;
                $mySellandruternbilldetail->parcode = $parcode;
                $mySellandruternbilldetail->pricetype = $priceType;
                $mySellandruternbilldetail->productunitid = $productunitId;
                $mySellandruternbilldetail->sellbilldetailcatid = $productData->productCatId;
                $mySellandruternbilldetail->sellbilldetaildate = date("Y-m-d");
                $mySellandruternbilldetail->sellbilldetailprice = $price;
                $mySellandruternbilldetail->sellbilldetailproductid = $productId;
                $mySellandruternbilldetail->sellbilldetailquantity = $quantity;
                $mySellandruternbilldetail->sellbilldetailtotalprice = $totalPrice;
                $mySellandruternbilldetail->sellbillid = $sellbillId;
                $mySellandruternbilldetail->selltype = 1; //مردودات
                $mySellandruternbilldetail->buydiscount = $productData->buydiscount;
                $mySellandruternbilldetail->buydiscounttype = $productData->discounttype;
                $mySellandruternbilldetail->storeid = $sellbillstoreId;
                $mySellandruternbilldetailRecord->insert($mySellandruternbilldetail);

                $productunitData = loadProductUnitWithProductAndUnit($productId, $productunitId);
                $productnumber = $productunitData->productnumber;
                $finalquantity = $quantity * $productnumber;

                //update DON if is Serial
                if ($parcodeType == "serialnumber") {
                    //update don column (quantity) in productserial table
                    updateSerailQty_ForReturn($productserailId, $finalquantity);
                }


                //get storeto productquantity before change
                $storedetailData = getStoredetailData($sellbillstoreId, $productId);

                $allStoredetailData = $storedetailData[0];
                $storedetailId = $storedetailData[1];
                $productquantityBefore = $storedetailData[2];

                if (count($allStoredetailData) > 0) {

                    //update productquantity in storedetail
                    $productquantityToAfter = increaseProductQuantity($storedetailId, $productquantityBefore, $finalquantity);


                    //insert in storereport
                    insertStorereport($productId, $sellbillstoreId, $finalquantity, $productquantityBefore, $productquantityToAfter, 0, $sellbillId, "اضافة فاتورة مردودات مبيعات", "sellbillandruternController.php");
                } else {
                    //insert in storedetail tbl
                    insertStoredetail($sellbillstoreId, $productId, $finalquantity);


                    //insert in storereport
                    insertStorereport($productId, $sellbillstoreId, $finalquantity, 0, $finalquantity, 0, $sellbillId, "اضافة فاتورة مردودات مبيعات", "sellbillandruternController.php");
                }
            }
        }



        if ($sellbillclientId != 0) {
            //check if sellbilltotaldeptAfter change from sellbilltotaldeptBefor
            //if($sellbilltotaldeptAfter != $sellbilltotaldeptBefor)
            {

                //update rawmaterialssupplierdebt
                updateClientDebt($sellbillclientId, $sellbilltotaldeptAfter);

                if ($sellbilltotalBill < 0) {
                    $sellbillFinalbill = $sellbillFinalbill * -1;
                    insertClientdebtchange($sellbillclientId, $sellbilltotaldeptBefor, $sellbillFinalbill, 1, "اضافة فاتورة مبيعات ومردودات", $sellbillId, $sellbilltotaldeptAfter, "sellbillandruternController.php", "اضافة فاتورة مبيعات ومردودات", $sellbillAftertotalBill);
                } else {
                    insertClientdebtchange($sellbillclientId, $sellbilltotaldeptBefor, $sellbillFinalbill, 0, "اضافة فاتورة مبيعات ومردودات", $sellbillId, $sellbilltotaldeptAfter, "sellbillandruternController.php", "اضافة فاتورة مبيعات ومردودات", $sellbillAftertotalBill);
                }
            }
        }


        //check if payed or not to change save value

        if ($sellbilltotalPayed > 0) {
            if ($sellbillAftertotalBill > 0) {

                //get saveValue before and saveValue after Plus lPayed
                $saveData = getSaveValueAndPlus($_SESSION['saveid'], $sellbilltotalPayed);
                $saveId = $saveData[0];
                $saveValuebefore = $saveData[1];
                $saveValueafter = $saveData[2];

                //update save value after add sellbill
                updateSave($saveId, $saveValueafter);

                //insert into savedaily tbl from save
                insertSavedaily($saveValuebefore, $sellbilltotalPayed, 0, $saveId, "اضافة فاتورة مبيعات ومردودات", $sellbillId, $saveValueafter, "sellbillandruternController.php");
            } else {
                //get saveValue before and saveValue after Plus lPayed
                $saveData = getSaveValueAndMins($_SESSION['saveid'], $sellbilltotalPayed);
                $saveId = $saveData[0];
                $saveValuebefore = $saveData[1];
                $saveValueafter = $saveData[2];

                //update save value after add sellbill
                updateSave($saveId, $saveValueafter);

                $specialSellbilltotalPayed = $sellbilltotalPayed * -1;

                //insert into savedaily tbl from save
                insertSavedaily($saveValuebefore, $specialSellbilltotalPayed, 1, $saveId, "اضافة فاتورة مبيعات ومردودات", $sellbillId, $saveValueafter, "sellbillandruternController.php");
            }
        }
    }







    //insert data into Sellbill tbl
    elseif ($sellbillPrice > 0 && $returnsellbillPrice <= 0) {
        $billtype = 1; //مبيعات


        $mySellbill->billnameid = $billnameId;
        $mySellbill->conditions = 0;
        $mySellbill->sellbildirectpayment = $sellbildirectPayment;
        $mySellbill->sellbillaftertotalbill = $sellbillAftertotalBill;
        $mySellbill->sellbillclientid = $sellbillclientId;
        $mySellbill->sellbillclientname = $clientName;
        $mySellbill->sellbilldate = $sellbillDate;
        $mySellbill->sellbilldiscount = $sellbillDiscount;
        $mySellbill->sellbilldiscounttype = $sellbillDiscountType;
        $mySellbill->sellbillfinalbill = $sellbillFinalbill;
        $mySellbill->sellbillsaveid = $_SESSION['saveid'];
        $mySellbill->sellbillserial = $sellbillSerial;
        $mySellbill->sellbillstoreid = $sellbillstoreId;
        $mySellbill->sellbillsysdate = date("Y-m-d H:i:s");
        $mySellbill->sellbilltotalbill = $sellbilltotalBill;
        $mySellbill->sellbilltotaldeptafter = $sellbilltotaldeptAfter;
        $mySellbill->sellbilltotaldeptbefor = $sellbilltotaldeptBefor;
        $mySellbill->sellbilltotalpayed = $sellbilltotalPayed;
        $mySellbill->sellerid = $sellerId;
        $mySellbill->userid = $_SESSION['userid'];
        $mySellbill->tempclientName = $tempClientName;
        $mySellbill->sellQuantity = $sellQuantity;

        $sellbillId = $mySellbillRecord->insert($mySellbill);



        //insert Sell Data into Sellbilldetail tbl
        $sellbillItr = $_POST['hidden_itr'];
        $h = 1;
        for ($h = 1; $h <= $sellbillItr; $h++) {
            $parcode = $_POST['parcode' . $h . ''];
            $parcodeType = $_POST['parcodeType' . $h . ''];
            $productserailId = $_POST['productserailid' . $h . ''];
            $productId = $_POST['product' . $h . ''];
            $productunitId = $_POST['productunit' . $h . ''];
            $quantity = $_POST['pronum' . $h . ''];
            $price = $_POST['proprice' . $h . ''];
            $discountValue = $_POST['prodis' . $h . ''];
            $totalPrice = $_POST['hidden_prototal' . $h . ''];
            $comment = $_POST['comment' . $h . ''];
            $prstorid = $_POST['prstorid' . $h . ''];

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

                $sellbillstoreId = $prstorid;
            }

            if ($productunitId == "") {
                $productunitId = 0;
            }


            if (isset($productId) && $productId != '-1' && $productId != "") {
                $productData = loadProductById($productId);


                $mySellbilldetail->buyprice = $productData->productBuyPrice;
                $mySellbilldetail->discountvalue = $discountValue;
                $mySellbilldetail->note = $comment;
                $mySellbilldetail->parcode = $parcode;
                $mySellbilldetail->pricetype = $priceType;
                $mySellbilldetail->productunitid = $productunitId;
                $mySellbilldetail->sellbilldetailcatid = $productData->productCatId;
                $mySellbilldetail->sellbilldetaildate = date("Y-m-d");
                $mySellbilldetail->sellbilldetailprice = $price;
                $mySellbilldetail->sellbilldetailproductid = $productId;
                $mySellbilldetail->sellbilldetailquantity = $quantity;
                $mySellbilldetail->sellbilldetailtotalprice = $totalPrice;
                $mySellbilldetail->sellbillid = $sellbillId;
                $mySellbilldetail->buydiscount = $productData->buydiscount;
                $mySellbilldetail->buydiscounttype = $productData->discounttype;
                $mySellbilldetail->storeid = $sellbillstoreId;

                $mySellbilldetailRecord->insert($mySellbilldetail);
                //print("qty: ".$quantity);
                //print("<br>productserailId: ".$productserailId);
                //update DON if is Serial

                $productunitData = loadProductUnitWithProductAndUnit($productId, $productunitId);
                $productnumber = $productunitData->productnumber;
                $finalquantity = $quantity * $productnumber;
                if ($parcodeType == "serialnumber") {
                    //print("<br>serialnumber<br>");
                    //updateSerailDon($productserailId, 1, $sellbillId);
                    updateSerailQty($productserailId, $finalquantity);
                    insertIntoSoldSerialProduct($productserailId, $finalquantity, $sellbillId, 0);
                }



                //get store productquantity before change
                $storedetailData = getStoredetailData($sellbillstoreId, $productId);

                $allStoredetailData = $storedetailData[0];
                $storedetailId = $storedetailData[1];
                $productquantityBefore = $storedetailData[2];


                if (count($allStoredetailData) > 0) {

                    //update productquantity in storedetail
                    $productquantityAfter = decreaseProductQuantity($storedetailId, $productquantityBefore, $finalquantity);


                    //insert in storereport
                    insertStorereport($productId, $sellbillstoreId, $finalquantity, $productquantityBefore, $productquantityAfter, 1, $sellbillId, "اضافة فاتورة مبيعات", "sellbillController.php");
                } else {
                    $specialQuantityAfter = $finalquantity * -1;

                    //insert in storedetail tbl
                    insertStoredetail($sellbillstoreId, $productId, $specialQuantityAfter);

                    //insert in storereport
                    insertStorereport($productId, $sellbillstoreId, $finalquantity, 0, $specialQuantityAfter, 1, $sellbillId, "اضافة فاتورة مبيعات", "sellbillController.php");
                }
            }
        }

        if ($sellbillclientId != 0) {
            //check if sellbilltotaldeptAfter change from sellbilltotaldeptBefor
            //if($sellbilltotaldeptAfter != $sellbilltotaldeptBefor)
            {

                //update rawmaterialssupplierdebt
                updateClientDebt($sellbillclientId, $sellbilltotaldeptAfter);

                insertClientdebtchange($sellbillclientId, $sellbilltotaldeptBefor, $sellbillFinalbill, 0, "اضافة فاتورة مبيعات", $sellbillId, $sellbilltotaldeptAfter, "sellbillController.php", "اضافة فاتورة مبيعات", $sellbillAftertotalBill);
            }
        }


        //check if payed or not to change save value

        if ($sellbilltotalPayed > 0) {
            //get saveValue before and saveValue after Plus lPayed
            $saveData = getSaveValueAndPlus($_SESSION['saveid'], $sellbilltotalPayed);
            $saveId = $saveData[0];
            $saveValuebefore = $saveData[1];
            $saveValueafter = $saveData[2];

            //update save value after add sellbill
            updateSave($saveId, $saveValueafter);

            //insert into savedaily tbl from save
            insertSavedaily($saveValuebefore, $sellbilltotalPayed, 0, $saveId, "اضافة فاتورة مبيعات", $sellbillId, $saveValueafter, "sellbillController.php");
        }
    }






    //insert data into returnSellbill tbl
    elseif ($sellbillPrice <= 0 && $returnsellbillPrice > 0) {
        $billtype = 2; //مردودات مبيعات



        $myReturnsellbill->billnameid = $billnameId;
        $myReturnsellbill->conditions = 0;
        $myReturnsellbill->returnsellbildirectpayment = $sellbildirectPayment;
        $myReturnsellbill->returnsellbillaftertotalbill = $sellbillAftertotalBill * -1;
        $myReturnsellbill->returnsellbillclientid = $sellbillclientId;
        $myReturnsellbill->returnsellbillclientname = $clientName;
        $myReturnsellbill->returnsellbilldate = $sellbillDate;
        $myReturnsellbill->returnsellbilldiscount = $sellbillDiscount;
        $myReturnsellbill->returnsellbilldiscounttype = $sellbillDiscountType;

        $sellbillFinalbill = ($sellbillAftertotalBill * -1) - $sellbilltotalPayed;
        $myReturnsellbill->returnsellbillfinalbill = $sellbillFinalbill;
        $myReturnsellbill->returnsellbillsaveid = $_SESSION['saveid'];
        $myReturnsellbill->returnsellbillserial = $sellbillSerial;
        $myReturnsellbill->returnsellbillstoreid = $sellbillstoreId;
        $myReturnsellbill->returnsellbillsysdate = date("Y-m-d");
        $myReturnsellbill->returnsellbilltotalbill = $sellbilltotalBill * -1;
        $myReturnsellbill->returnsellbilltotaldeptafter = $sellbilltotaldeptAfter;
        $myReturnsellbill->returnsellbilltotaldeptbefor = $sellbilltotaldeptBefor;
        $myReturnsellbill->returnsellbilltotalpayed = $sellbilltotalPayed;
        $myReturnsellbill->sellerid = $sellerId;
        $myReturnsellbill->userid = $_SESSION['userid'];
        $myReturnsellbill->tempclientName = $tempClientName;
        $myReturnsellbill->returnsellQuantity = $returnsellQuantity;


        $returnsellbillId = $myReturnsellbillRecord->insert($myReturnsellbill);


        //insert Return Data into returnsellbilldetail tbl
        $returnSellbillItr = $_POST['returnhidden_itr'];
        $h = 1;
        for ($h = 1; $h <= $returnSellbillItr; $h++) {
            $parcode = $_POST['returnparcode' . $h . ''];
            $parcodeType = $_POST['returnparcodeType' . $h . ''];
            $productserailId = $_POST['returnproductserailid' . $h . ''];
            $productId = $_POST['returnproduct' . $h . ''];
            $productunitId = $_POST['returnproductunit' . $h . ''];
            $quantity = $_POST['returnpronum' . $h . ''];
            $price = $_POST['returnproprice' . $h . ''];
            $discountValue = $_POST['returnprodis' . $h . ''];
            $totalPrice = $_POST['returnhidden_prototal' . $h . ''];
            $comment = $_POST['returncomment' . $h . ''];

            $reprstorid = $_POST['reprstorid' . $h . ''];
            if (isset($reprstorid) && $reprstorid != -1 && $reprstorid != '') {

                $sellbillstoreId = $reprstorid;
            }


            if ($productunitId == "") {
                $productunitId = 0;
            }


            if (isset($productId) && $productId != '-1' && $productId != "") {
                $productData = loadProductById($productId);
                $productunitData = loadProductUnitWithProductAndUnit($productId, $productunitId);

                $myReturnsellbilldetail->buyprice = $productData->productBuyPrice;
                $myReturnsellbilldetail->conditions = 0;
                $myReturnsellbilldetail->discountvalue = $discountValue;
                $myReturnsellbilldetail->note = $comment;
                $myReturnsellbilldetail->parcode = $parcode;
                $myReturnsellbilldetail->pricetype = $priceType;
                $myReturnsellbilldetail->productunitid = $productunitId;
                $myReturnsellbilldetail->returnsellbilldetailcatid = $productData->productCatId;
                $myReturnsellbilldetail->returnsellbilldetailprice = $price;
                $myReturnsellbilldetail->returnsellbilldetailproductid = $productId;
                $myReturnsellbilldetail->returnsellbilldetailquantity = $quantity;
                $myReturnsellbilldetail->returnsellbilldetailtotalprice = $totalPrice;
                $myReturnsellbilldetail->returnsellbillid = $returnsellbillId;
                $myReturnsellbilldetail->buydiscount = $productData->buydiscount;
                $myReturnsellbilldetail->buydiscounttype = $productData->discounttype;
                $myReturnsellbilldetail->storeid = $sellbillstoreId;

                $myReturnsellbilldetailRecord->insert($myReturnsellbilldetail);


                $productunitData = loadProductUnitWithProductAndUnit($productId, $productunitId);
                $productnumber = $productunitData->productnumber;
                $finalquantity = $quantity * $productnumber;

                //update DON if is Serial
                if ($parcodeType == "serialnumber") {
                    //updateSerailDon($productserailId, 0, 0);
                    updateSerailQty_ForReturn($productserailId, $finalquantity);
                }



                //get storeto productquantity before change
                $storedetailData = getStoredetailData($sellbillstoreId, $productId);

                $allStoredetailData = $storedetailData[0];
                $storedetailId = $storedetailData[1];
                $productquantityBefore = $storedetailData[2];

                if (count($allStoredetailData) > 0) {

                    //update productquantity in storedetail
                    $productquantityToAfter = increaseProductQuantity($storedetailId, $productquantityBefore, $finalquantity);


                    //insert in storereport
                    insertStorereport($productId, $sellbillstoreId, $finalquantity, $productquantityBefore, $productquantityToAfter, 0, $returnsellbillId, "اضافة فاتورة مردودات مبيعات", "returnsellbillController.php");
                } else {
                    //insert in storedetail tbl
                    insertStoredetail($sellbillstoreId, $productId, $finalquantity);


                    //insert in storereport
                    insertStorereport($productId, $sellbillstoreId, $finalquantity, 0, $finalquantity, 0, $returnsellbillId, "اضافة فاتورة مردودات مبيعات", "returnsellbillController.php");
                }
            }
        }



        if ($sellbillclientId != 0) {
            //check if sellbilltotaldeptAfter change from sellbilltotaldeptBefor
            //if($sellbilltotaldeptAfter != $sellbilltotaldeptBefor)
            {

                //update rawmaterialssupplierdebt
                updateClientDebt($sellbillclientId, $sellbilltotaldeptAfter);

                insertClientdebtchange($sellbillclientId, $sellbilltotaldeptBefor, $sellbillFinalbill, 1, "اضافة فاتورة مردوات مبيعات", $returnsellbillId, $sellbilltotaldeptAfter, "returnsellbillController.php", "اضافة فاتورة مردوات مبيعات", $sellbillAftertotalBill);
            }
        }


        //check if payed or not to change save value

        if ($sellbilltotalPayed > 0) {

            //get saveValue before and saveValue after mins sellbilltotalPayed
            $saveData = getSaveValueAndMins($_SESSION['saveid'], $sellbilltotalPayed);
            $saveId = $saveData[0];
            $saveValuebefore = $saveData[1];
            $saveValueafter = $saveData[2];

            //update saveFrom value after add transfer
            updateSave($saveId, $saveValueafter);

            //insert into savedaily tbl from savefrom

            $specialSellbilltotalPayed = $sellbilltotalPayed * -1;

            insertSavedaily($saveValuebefore, $specialSellbilltotalPayed, 1, $saveId, "اضافة فاتورة مردوات مبيعات", $returnsellbillId, $saveValueafter, "returnsellbillController.php");
        }
    }



    return array($sellbillId, $returnsellbillId, $productsBeforeValid, $saveValuebeforeValid, $billtype);
}

//load client by Id tbl
function loadClientNameById($clientId) {
    //to use the variable out side the funcion
    global $myClientRecord;

    //load client by Id tbl
    $clientData = $myClientRecord->load($clientId);
    $clientName = $clientData->clientname;

    return $clientName;
}

//load client by Id tbl
function loadClientDataById($clientId) {
    //to use the variable out side the funcion
    global $myClientRecord;

    //load client by Id tbl
    $clientData = $myClientRecord->load($clientId);

    return $clientData;
}

// load Product by id tbl
function loadProductById($productid) {
    //to use the variable out side the funcion
    global $myProductRecord;
    global $myProductEx;

    // load Product by id tbl
    $productData = $myProductEx->loadByProductId($productid);

    return $productData;
}

function loadProductUnitWithProductAndUnit($productId, $unitId) {

    //to use the variable out side the funcion
    global $myProductunitEx;

    // select all data from productunit tbl
    $productunitData = $myProductunitEx->queryWithProductIdAndUnitId($productId, $unitId);

    return $productunitData;
}

function showAll($startDate, $endDate) {
    global $mySellbillEx;

    global $smarty;

    $totalBills = 0;

    $sellbillData = $mySellbillEx->queryWithDate($startDate, $endDate);
    if (count($sellbillData) > 0) {
        foreach ($sellbillData as $sell) {
            if ($sell->conditions == 0) {
                $totalBills += $sell->sellbillaftertotalbill;
            }
        }
    }
    // assign your db results to the template
    $smarty->assign('sellbillData', $sellbillData);
    $smarty->assign('totalBills', $totalBills);
}

function showByClient($clientId) {
    global $mySellbillEx;
    global $smarty;

    $totalBills = 0;
    $sellbillData = $mySellbillEx->queryWithClientId($clientId);
    if (count($sellbillData) > 0) {
        foreach ($sellbillData as $sell) {
            if ($sell->conditions == 0) {
                $totalBills += $sell->sellbillaftertotalbill;
            }
        }
    }
    // assign your db results to the template
    $smarty->assign('sellbillData', $sellbillData);
    $smarty->assign('totalBills', $totalBills);
}

function showBySriral($serial) {
    global $mySellbillEx;
    global $smarty;

    $totalBills = 0;
    $sellbillData = $mySellbillEx->queryWithSerail($serial);
    if (count($sellbillData) > 0) {
        foreach ($sellbillData as $sell) {
            if ($sell->conditions == 0) {
                $totalBills += $sell->sellbillaftertotalbill;
            }
        }
    }

    // assign your db results to the template
    $smarty->assign('sellbillData', $sellbillData);
    $smarty->assign('totalBills', $totalBills);
}

function showBySellbillId($sellbillId) {
    global $mySellbillEx;
    global $smarty;

    $totalBills = 0;
    $sellbillData = $mySellbillEx->queryWithSellbillId($sellbillId);
    if (count($sellbillData) > 0) {
        foreach ($sellbillData as $sell) {
            if ($sell->conditions == 0) {
                $totalBills += $sell->sellbillaftertotalbill;
            }
        }
    }

    // assign your db results to the template
    $smarty->assign('sellbillData', $sellbillData);
    $smarty->assign('totalBills', $totalBills);
}

function showByDate($startDate, $endDate) {
    global $mySellbillEx;
    global $smarty;

    $totalBills = 0;
    $sellbillData = $mySellbillEx->queryWithDate($startDate, $endDate);
    if (count($sellbillData) > 0) {
        foreach ($sellbillData as $sell) {
            if ($sell->conditions == 0) {
                $totalBills += $sell->sellbillaftertotalbill;
            }
        }
    }

    // assign your db results to the template
    $smarty->assign('sellbillData', $sellbillData);
    $smarty->assign('totalBills', $totalBills);
}

function delete($sellbillid) {
    global $mySellbill;
    global $mySellbillRecord;
    global $mySellbillEx;
    global $mySellbilldetail;
    global $mySellbilldetailRecord;

    try {

        $sellbillData = $mySellbillRecord->load($sellbillid);

        $sellbilltotalPayed = $sellbillData->sellbilltotalpayed;
        $sellbillstoreId = $sellbillData->sellbillstoreid;
        $sellbilltotaldeptAfter = $sellbillData->sellbilltotaldeptafter;
        $sellbilltotaldeptBefor = $sellbillData->sellbilltotaldeptbefor;
        $sellbillclientId = $sellbillData->sellbillclientid;
        $sellbillFinalbill = $sellbillData->sellbillfinalbill;
        $sellbillsaveId = $sellbillData->sellbillsaveid;

        $totalOperationCost = $sellbillData->sellbillaftertotalbill;

        $note;
        $saveValuebeforeValid = 1;

        /////////////////check save value before //////////////
        //get save value
        $saveValueBefore = getSaveValueBefore($sellbillsaveId);
        $saveValueafterValid = $saveValueBefore - $sellbilltotalPayed;
        if ($saveValueafterValid >= 0) {
            $saveValuebeforeValid = 1;

            $note = "success";
        } else {
            $saveValuebeforeValid = 0;
            //return $saveValuebeforeValid;

            $note = "لا يوجد بالخزنة المبلغ الكافى لحذف الفاتورة";
        }



        $mySellbill->conditions = 1;
        $mySellbill->sellbillsysdate = date("Y-m-d H:i:s");
        $mySellbill->userid = $_SESSION['userid'];
        $mySellbill->sellbillid = $sellbillid;

        $mySellbillEx->updateConditions($mySellbill);


        $sellbilldetailData = $mySellbilldetailRecord->queryBySellbillid($sellbillid);

        foreach ($sellbilldetailData as $sellbilldetail) {
            $productId = $sellbilldetail->sellbilldetailproductid;
            $quantity = $sellbilldetail->sellbilldetailquantity;


            if (isset($productId) && $productId != '-1' && $productId != "") {

                //get storeto productquantity before change
                $storedetailData = getStoredetailData($sellbillstoreId, $productId);

                $allStoredetailData = $storedetailData[0];
                $storedetailId = $storedetailData[1];
                $productquantityBefore = $storedetailData[2];

                if (count($allStoredetailData) > 0) {

                    //update productquantity in storedetail
                    $productquantityToAfter = increaseProductQuantity($storedetailId, $productquantityBefore, $quantity);


                    //insert in storereport
                    insertStorereport($productId, $sellbillstoreId, $quantity, $productquantityBefore, $productquantityToAfter, 0, $sellbillid, "حذف فاتورة مبيعات", "sellbillController.php");
                } else {
                    //insert in storedetail tbl
                    insertStoredetail($sellbillstoreId, $productId, $quantity);


                    //insert in storereport
                    insertStorereport($productId, $sellbillstoreId, $quantity, 0, $quantity, 0, $sellbillid, "حذف فاتورة مبيعات", "sellbillController.php");
                }
            }
        }



        if ($sellbillclientId != 0) {
            //check if sellbilltotaldeptAfter change from sellbilltotaldeptBefor
            //if($sellbilltotaldeptAfter != $sellbilltotaldeptBefor)
            {

                $clientData = loadClientDataById($sellbillclientId);
                $clientdeptBefor = $clientData->clientdebt;
                $clientdeptAfter = ($clientData->clientdebt) - $sellbillFinalbill;

                //update rawmaterialssupplierdebt
                updateClientDebt($sellbillclientId, $clientdeptAfter);

                insertClientdebtchange($sellbillclientId, $clientdeptBefor, $sellbillFinalbill, 1, "حذف فاتورة مبيعات", $sellbillid, $clientdeptAfter, "sellbillController.php", "حذف فاتورة مبيعات", $totalOperationCost);
            }
        }


        //check if payed or not to change save value

        if ($sellbilltotalPayed > 0) {

            //get saveValue before and saveValue after mins sellbilltotalPayed
            $saveData = getSaveValueAndMins($sellbillsaveId, $sellbilltotalPayed);
            $saveId = $saveData[0];
            $saveValuebefore = $saveData[1];
            $saveValueafter = $saveData[2];

            //update saveFrom value after add transfer
            updateSave($saveId, $saveValueafter);

            //insert into savedaily tbl from savefrom
            insertSavedaily($saveValuebefore, $sellbilltotalPayed, 1, $saveId, "حذف فاتورة مبيعات", $sellbillid, $saveValueafter, "sellbillController.php");
        }
    } catch (Exception $e) {
        $note = "حدث خطأ اثناء حذف الفاتورة";
    }

    return array($saveValuebeforeValid, $note);
}

function executeOperation() {
    //to use the variable out side the funcion
    global $mySellbillRecord;
    global $smarty;

    $outputString;

    $operationType = $_POST['operation'];
    $smarty->assign("operationType", $operationType);

    $choosedItemArr = $_POST['choosedItem'];
    //print_r($choosedItemArr);
    if (count($choosedItemArr) > 0) {
        foreach ($choosedItemArr as $sellbillId) {

            //get store name
            $sellbillData = $mySellbillRecord->load($sellbillId);
            $sellbillSerial = $sellbillData->sellbillserial;

            if ($operationType == '1') {
                //tempdelete
                $note = delete($sellbillId);
                if ($note[1] != "success") {
                    $outputString .= $sellbillSerial . ": " . $note[1] . "<br/>";
                } else {
                    $outputString .= $sellbillSerial . ": تمت العملية بنجاح <br/>";
                }
            }
        }

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

function loadProductCat() {
    global $myProductEx;

    $allParents = $myProductEx->queryAllCategories();

    return $allParents;
}

function loadUnit() {
    global $myUnitRecord;

    $unitsData = $myUnitRecord->queryByCondition(0);
    return $unitsData;
}

////////////////////////////save///////////////////////////
// get savecurrentvalue from save tbl
function getSaveValueBefore($saveid) {

    //to use the variable out side the funcion
    global $mySave;
    global $mySaveRecord;

    $saveData = $mySaveRecord->load($saveid);
    $saveValuebefore = $saveData->savecurrentvalue;

    return $saveValuebefore;
}

// get initialvalue and plus from save tbl
function getSaveValueAndPlus($saveid, $savevaluechanged) {

    //to use the variable out side the funcion
    global $mySave;
    global $mySaveRecord;

    $saveData = $mySaveRecord->load($saveid);
    $saveId = $saveData->saveid;
    $saveValuebefore = $saveData->savecurrentvalue;

    $saveValueafter = $saveValuebefore + $savevaluechanged;

    return array($saveId, $saveValuebefore, $saveValueafter);
}

// get initialvalue and mins from save tbl
function getSaveValueAndMins($saveid, $savevaluechanged) {

    //to use the variable out side the funcion
    global $mySave;
    global $mySaveRecord;

    $saveData = $mySaveRecord->load($saveid);
    $saveId = $saveData->saveid;
    $saveValuebefore = $saveData->savecurrentvalue;

    $saveValueafter = $saveValuebefore - $savevaluechanged;

    return array($saveId, $saveValuebefore, $saveValueafter);
}

// update in Save tbl
function updateSave($saveid, $savevalueafter) {

    //to use the variable out side the funcion
    global $mySave;
    global $mySaveEx;

    //update initialvalue in Save tbl
    $mySave->savecurrentvalue = $savevalueafter;
    $mySave->userid = $_SESSION['userid'];
    $mySave->saveid = $saveid;

    $mySaveEx->updateSaveValue($mySave);
}

// insert in Savedaily tbl
function insertSavedaily($savedailysavebefore, $savedailychangeamount, $savedailychangetype, $saveid, $processname, $savedailymodelid, $savedailysaveafter, $tablename) {

    //to use the variable out side the funcion
    global $mySavedaily;
    global $mySavedailyRecord;

    //insert in Savedaily tbl
    $mySavedaily->savedailydate = date("Y-m-d H:i:s");
    $mySavedaily->userid = $_SESSION['userid'];
    $mySavedaily->savedailysavebefore = $savedailysavebefore;
    $mySavedaily->savedailychangeamount = $savedailychangeamount;
    $mySavedaily->savedailychangetype = $savedailychangetype;
    $mySavedaily->saveid = $saveid;
    $mySavedaily->processname = $processname;
    $mySavedaily->savedailymodelid = $savedailymodelid;
    $mySavedaily->savedailysaveafter = $savedailysaveafter;
    $mySavedaily->tablename = $tablename;

    $mySavedailyRecord->insert($mySavedaily);
}

/////////////////////supplier///////////////////////
// update in rawmaterialssuppliers tbl
function updateSupplierDebt($supplierid, $supplierdebtAfter) {

    //to use the variable out side the funcion
    global $myRawmaterialssuppliers;
    global $myRawmaterialssuppliersEx;

    //update rawmaterialssupplierdebt in rawmaterialssuppliers tbl
    $myRawmaterialssuppliers->userid = $_SESSION['userid'];
    $myRawmaterialssuppliers->rawmaterialssupplierdebt = $supplierdebtAfter;
    $myRawmaterialssuppliers->rawmaterialssupplierid = $supplierid;

    $myRawmaterialssuppliersEx->updateSupplierDebt($myRawmaterialssuppliers);
}

/////////////////////////////////store////////////////////////////
// get productquantity from storedetail tbl
function getStoredetailData($storeid, $productid) {

    //to use the variable out side the funcion
    global $myStoredetailEx;

    //select data from storerawmaterialdetails by storeid and rawmaterialid
    $storedetailData = $myStoredetailEx->queryWithStoreAndProduct($productid, $storeid);

    $storedetailId = $storedetailData->storedetailid;
    $productquantityBefore = $storedetailData->productquantity;

    return array($storedetailData, $storedetailId, $productquantityBefore);
}

//update and increase productquantity in storedetail tbl
function increaseProductQuantity($storedetailId, $productquantityBefore, $productChangeAmount) {

    //to use the variable out side the funcion
    global $myStoredetail;
    global $myStoredetailEx;

    //increase productquantity
    $productquantityAfter = $productquantityBefore + $productChangeAmount;

    //update productquantity in storedetail tbl
    $myStoredetail->storedetailid = $storedetailId;
    $myStoredetail->userid = $_SESSION['userid'];
    $myStoredetail->storedetaildate = date("Y-m-d");
    $myStoredetail->productquantity = $productquantityAfter;

    $myStoredetailEx->updateProductquantity($myStoredetail);

    return $productquantityAfter;
}

//update and decrease Rawmaterialamount in storerawmaterialdetails tbl
function decreaseProductQuantity($storedetailId, $productquantityBefore, $productChangeAmount) {

    //to use the variable out side the funcion
    global $myStoredetail;
    global $myStoredetailEx;

    //increase productquantity
    $productquantityAfter = $productquantityBefore - $productChangeAmount;

    //update productquantity in storedetail tbl
    $myStoredetail->storedetailid = $storedetailId;
    $myStoredetail->userid = $_SESSION['userid'];
    $myStoredetail->storedetaildate = date("Y-m-d");
    $myStoredetail->productquantity = $productquantityAfter;

    $myStoredetailEx->updateProductquantity($myStoredetail);

    return $productquantityAfter;
}

//add in storerawmaterialdetails tbl
function insertStoredetail($storeid, $productid, $productChangeAmount) {

    global $myStoredetail;
    global $myStoredetailRecord;

    $myStoredetail->userid = $_SESSION['userid'];
    $myStoredetail->storedetaildate = date("Y-m-d");
    $myStoredetail->productquantity = $productChangeAmount;
    $myStoredetail->storeid = $storeid;
    $myStoredetail->productid = $productid;

    $myStoredetailRecord->insert($myStoredetail);
}

//add in Storemovement tbl
function insertStorereport($productid, $storeid, $productChangeAmount, $productbefore, $productafter, $storereporttype, $storereportmodelid, $processname, $tablename) {
    global $myStorereport;
    global $myStorereportRecord;

    $myStorereport->processname = $processname;
    $myStorereport->productafter = $productafter;
    $myStorereport->productbefore = $productbefore;
    $myStorereport->productquantity = $productChangeAmount;
    $myStorereport->productid = $productid;
    $myStorereport->storeid = $storeid;
    $myStorereport->storereportdate = date("Y-m-d");
    $myStorereport->storereportmodelid = $storereportmodelid;
    $myStorereport->storereporttype = $storereporttype;
    $myStorereport->tablename = $tablename;
    $myStorereport->userid = $_SESSION['userid'];

    $myStorereportRecord->insert($myStorereport);
}

///////////////////////////////////clients//////////////////////
// update in rawmaterialssuppliers tbl
function updateClientDebt($clientid, $clientdebtAfter) {

    //to use the variable out side the funcion
    global $myClient;
    global $myClientEx;

    //update clientdebt in client tbl
    $myClient->userid = $_SESSION['userid'];
    $myClient->clientdebt = $clientdebtAfter;
    $myClient->clientid = $clientid;

    $myClientEx->updateClientdebt($myClient);
}

// insert data into Clientdebtchange tbl
function insertClientdebtchange($clientid, $clientdebtchangebefore, $clientdebtchangeamount, $clientdebtchangetype, $processname, $clientdebtchangemodelid, $clientdebtchangeafter, $tablename, $comment, $totalOperationCost) {

    //to use the variable out side the funcion
    global $myClientdebtchange;
    global $myClientdebtchangeRecord;

    $myClientdebtchange->clientdebtchangeafter = $clientdebtchangeafter;
    $myClientdebtchange->clientdebtchangeamount = $clientdebtchangeamount;
    $myClientdebtchange->clientdebtchangebefore = $clientdebtchangebefore;
    $myClientdebtchange->clientdebtchangedate = date("Y-m-d");
    $myClientdebtchange->clientdebtchangemodelid = $clientdebtchangemodelid;
    $myClientdebtchange->clientdebtchangetype = $clientdebtchangetype;
    $myClientdebtchange->clientid = $clientid;
    $myClientdebtchange->comment = $comment;
    $myClientdebtchange->processname = $processname;
    $myClientdebtchange->tablename = $tablename;
    $myClientdebtchange->userid = $_SESSION['userid'];
    $myClientdebtchange->totalOperationCost = $totalOperationCost;

    $myClientdebtchangeRecord->insert($myClientdebtchange);
}

function updateSerailDon($productserailid, $don, $billId) {
    global $myProductserial;
    global $myProductserialEx;

    $myProductserial->productserailid = $productserailid;
    $myProductserial->don = $don;
    $myProductserial->sellbillid = $billId;

    $myProductserialEx->updateDonExt($myProductserial);
}

function updateSerailQty($productserailId, $soldQuantity) {
    global $myProductserial;
    global $myProductserialEx;

    $productQuantity = $myProductserialEx->getProductQuantity($productserailId);
    if ($productQuantity > 0) {
        $availableQty = $productQuantity - $soldQuantity;
        //if(isset($availableQty) && $availableQty < 0)
//			{
//				//في حالة بيع منتج بكمية أكبر من عدد السريالات
//			}
//			else
//			{
//
//			}

        $myProductserial->productserailid = $productserailId;
        $myProductserial->don = $availableQty;
        //print_r($myProductserial);
        $myProductserialEx->updateDon($myProductserial);
    }
}

function updateSerailQty_ForReturn($productserailId, $returnedQuantity) {
    global $myProductserial;
    global $myProductserialEx;

    $productQuantity = $myProductserialEx->getProductQuantity($productserailId);
    if ($productQuantity > 0) {
        $availableQty = $productQuantity + $returnedQuantity;

        $myProductserial->productserailid = $productserailid;
        $myProductserial->don = $availableQty;

        $myProductserialEx->updateDon($myProductserial);
    }
}

function insertIntoSoldSerialProduct($productserailId, $soldQuantity, $sellbillId, $sellbilltype) {
    global $soldSerialProduct;
    global $soldSerialProductDAO;

    $soldSerialProduct->productserailid = $productserailId;
    $soldSerialProduct->quantity = $soldQuantity;
    $soldSerialProduct->sellbilltype = $sellbilltype;
    $soldSerialProduct->sellbillid = $sellbillId;
    //print("<br>");
    //print_r($soldSerialProduct);
    $soldSerialProductDAO->insert($soldSerialProduct);
}

?>