<?php

//the global file operation
//global varable
//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');
//Sellbilldetail
require_once('../models/dao/SellbilldetailDAO.class.php');
require_once('../models/dto/Sellbilldetail.class.php');
require_once('../models/mysql/SellbilldetailMySqlDAO.class.php');
require_once('../models/mysql/ext/SellbilldetailMySqlExtDAO.class.php');
//Returnsellbill
require_once('../models/dao/ReturnsellbillDAO.class.php');
require_once('../models/dto/Returnsellbill.class.php');
require_once('../models/mysql/ReturnsellbillMySqlDAO.class.php');
require_once('../models/mysql/ext/ReturnsellbillMySqlExtDAO.class.php');
//Returnsellbilldetail
require_once('../models/dao/ReturnsellbilldetailDAO.class.php');
require_once('../models/dto/Returnsellbilldetail.class.php');
require_once('../models/mysql/ReturnsellbilldetailMySqlDAO.class.php');
require_once('../models/mysql/ext/ReturnsellbilldetailMySqlExtDAO.class.php');
//Sellbillandrutern
require_once('../models/dao/SellbillandruternDAO.class.php');
require_once('../models/dto/Sellbillandrutern.class.php');
require_once('../models/mysql/SellbillandruternMySqlDAO.class.php');
require_once('../models/mysql/ext/SellbillandruternMySqlExtDAO.class.php');
//Sellandruternbilldetail
require_once('../models/dao/SellandruternbilldetailDAO.class.php');
require_once('../models/dto/Sellandruternbilldetail.class.php');
require_once('../models/mysql/SellandruternbilldetailMySqlDAO.class.php');
require_once('../models/mysql/ext/SellandruternbilldetailMySqlExtDAO.class.php');
//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');

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


require_once('../models/dao/SellbilldetailorderDAO.class.php');
require_once('../models/dto/Sellbilldetailorder.class.php');
require_once('../models/mysql/SellbilldetailorderMySqlDAO.class.php');
require_once('../models/mysql/ext/SellbilldetailorderMySqlExtDAO.class.php');

require_once('../models/dao/SellbillorderDAO.class.php');
require_once('../models/dto/Sellbillorder.class.php');
require_once('../models/mysql/SellbillorderMySqlDAO.class.php');
require_once('../models/mysql/ext/SellbillorderMySqlExtDAO.class.php');
//
require_once('../models/dao/SizecolorDAO.class.php');
require_once('../models/dto/Sizecolor.class.php');
require_once('../models/mysql/SizecolorMySqlDAO.class.php');
require_once('../models/mysql/ext/SizecolorMySqlExtDAO.class.php');
//
require_once('../models/dao/SizecolorstoredetailDAO.class.php');
require_once('../models/dto/Sizecolorstoredetail.class.php');
require_once('../models/mysql/SizecolorstoredetailMySqlDAO.class.php');
require_once('../models/mysql/ext/SizecolorstoredetailMySqlExtDAO.class.php');

//get the do the action

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

  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
//here the global templates
//here goes the instances and general variables
//Sellbilldetailorder
$Sellbilldetailorder = new Sellbilldetailorder();
$SellbilldetailorderDAO = new SellbilldetailorderMySqlDAO();
$SellbilldetailorderEX = new SellbilldetailorderMySqlExtDAO();

//Sellbillorder
$Sellbillorder = new Sellbillorder();
$SellbillorderDAO = new SellbillorderMySqlDAO();
$SellbillorderEX = new SellbillorderMySqlExtDAO();
//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();
$myReturnsellbilldetailEx = new ReturnsellbilldetailMySqlExtDAO();

//Productcat
$productCatDAO = new ProductcatMySqlDAO();
$productCatExt = new ProductcatMySqlExtDAO();


//Sellbillandrutern
$mySellbillandrutern = new Sellbillandrutern();
$mySellbillandruternRecord = new SellbillandruternMySqlDAO();
$mySellbillandruternEx = new SellbillandruternMySqlExtDAO();
//Sellandruternbilldetail
$mySellandruternbilldetail = new Sellandruternbilldetail();
$mySellandruternbilldetailRecord = new SellandruternbilldetailMySqlDAO();
$mySellandruternbilldetailEx = new SellandruternbilldetailMySqlExtDAO();
//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();

//Costcenterdetail
$Costcenterdetail = new Costcenterdetail();
$CostcenterdetailDAO = new CostcenterdetailMySqlDAO();
$CostcenterdetailEX = new CostcenterdetailMySqlExtDAO();

//Costcenter
$Costcenter = new Costcenter();
$CostcenterDAO = new CostcenterMySqlDAO();
$CostcenterEX = new CostcenterMySqlExtDAO();
//
$sizeColor = new Sizecolor();
$sizeColorDAO = new SizecolorMySqlDAO();
$sizeColorEX = new SizecolorMySqlExtDAO();
//
$sizeColorStoreDetail = new Sizecolorstoredetail();
$sizeColorStoreDetailDAO = new SizecolorstoredetailMySqlDAO();
$sizeColorStoreDetailEX = new SizecolorstoredetailMySqlExtDAO();

//check and use the condition that suites this action


eval(base64_decode("ZnVuY3Rpb24gc2VyaWFsY2hlY2soKSB7CiAgICAvL3V1aWQgb2Ygd2luZG93cwogICAgZXhlYygnQzpcV2luZG93c1xTeXN0ZW0zMlx3YmVtXHdtaWMgY3Nwcm9kdWN0IGdldCAiVVVJRCInLCAkbWFjYWRkcmVzcywgJGlzRXhlY3V0ZWQpOwogICAgJG1hY2FkZHJlc3MgPSAkbWFjYWRkcmVzc1sxXTsKICAgICRtYWMgPSBzdHJfcmVwbGFjZShhcnJheSgnLScpLCAnJywgJG1hY2FkZHJlc3MpOwogICAgaWYgKGVtcHR5KCRtYWNhZGRyZXNzKSkgey8vbm90IHdpbmRvd3MgdXNlIGxpbnV4IG1hYwogICAgICAgIC8vbWFjIG9mIGxpbnV4CiAgICAgICAgJG1hY2FkZHJlc3MgPSBleGVjKCJpcCBsaW5rIik7CiAgICAgICAgLy8kbWFjYWRkcmVzcyA9IGV4ZWMoImlwIC1icmllZiBsaW5rIik7CiAgICAgICAgLy8kbWFjYWRkcmVzcyA9IGV4ZWMoImNhdCAvc3lzL2NsYXNzL25ldC8qL2FkZHJlc3MiKTsKICAgICAgICAkbWFjYWRkcmVzcyA9IHRyaW0oc3RyX3JlcGxhY2UoJ2xpbmsvZXRoZXIgJywgJycsICRtYWNhZGRyZXNzKSk7CiAgICAgICAgJG1hY2FkZHJlc3MgPSBzdWJzdHIoJG1hY2FkZHJlc3MsIDAsIDE3KTsKICAgICAgICAkbWFjID0gc3RyX3JlcGxhY2UoYXJyYXkoJzonKSwgJycsICRtYWNhZGRyZXNzKTsKICAgIH0KCiAgICAkcmFuZG9tX3NhbHQgPSBoYXNoKCdzaGE1MTInLCAnMTMwNScpOwogICAgLy8gQ3JlYXRlIHNhbHRlZCBwYXNzd29yZAogICAgJGhhc2hlZE1hYyA9IGhhc2goJ3NoYTUxMicsICRtYWMgLiAkcmFuZG9tX3NhbHQpOwogICAgcmV0dXJuICRoYXNoZWRNYWM7Cn0="));
eval(base64_decode("JHNlcmlhbENhbCA9IHNlcmlhbGNoZWNrKCk7CiAgICBpZiAoJHByb2ctPnNlcmlhbCAhPSAkc2VyaWFsQ2FsIHx8ICgkX1NFU1NJT05bJ2xpY19kYXRldG8nXSA8IGRhdGUoJ1ktbS1kJykgJiYgZW1wdHkoJF9QT1NUWyJlcnBEQiJdKSkpIHsKICAgICAgICAvL3NlbmQgdGhlIHVzZXIgdG8gdGhlIGxvZ291dCBwYWdlCiAgICAgICAgaGVhZGVyKCJsb2NhdGlvbjpsb2dvdXQucGhwIik7CiAgICAgICAgZXhpdCgpOwogICAgfQ=="));

function deletesellbill($sellbillid, $savePaymentDateWithBillDate = 0)
{
    global $mySellbill;
    global $mySellbillRecord;
    global $mySellbillEx;
    global $mySellbilldetail;
    global $mySellbilldetailRecord;
    global $myProductserialEx;
    global $CostcenterdetailEX;
    global $soldSerialProductExt;
    global $sizeColorStoreDetailDAO;
    global $sizeColorStoreDetailEX;
    global $bankAccountEX;
    global $bankAccountDAO;
    global $bankAccount;
    global $sellbillId; //nedded for collective product##fatma
    global $sellbillDate;
    $sellbillId = $sellbillid;


    try {

        $sellbillData = $mySellbillRecord->load($sellbillid);
        $mydate = $sellbillData->sellbilldate;
        $CostcenterdetailEX->updatedellbytypeandmodelid(0, $sellbillid, 1);


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

        $totalOperationCost = $sellbillData->sellbillaftertotalbill;

        $sellbilltotalBill = $sellbillData->sellbilltotalbill;
        $sellbillDiscount = $sellbillData->sellbilldiscount;
        $sellbillDiscountType = $sellbillData->sellbilldiscounttype;
        $sellbillAftertotalBill = $sellbillData->sellbillaftertotalbill;

        $sellbillDate = $sellbillData->sellbilldate;

        $visa = $sellbillData->visaPayed;
        $visaAccount = $sellbillData->visaAccountId;
        if (empty($visaAccount)) {
            $visa = 0;
        }
        $isBankAccountTransfer = $sellbillData->isBankAccountTransfer;

        ##get $billTotalBeforeDiscount
        $billTotalBeforeDiscount = $sellbilltotalBill;
        ##get $billDiscountVal
        $billDiscountVal = $sellbillDiscount;
        if ($sellbillDiscountType == "2") {
            $billDiscountVal = ($sellbillDiscount / 100) * $billTotalBeforeDiscount;
            $billDiscountVal = round($billDiscountVal, 2);
        }
        ########################################################################
        $note;
        $saveValuebeforeValid = 1;

        /////////////////check save value before //////////////
        //get save value
        $saveValueBefore = getSaveValueBefore($sellbillsaveId);
        $saveValueafterValid = $saveValueBefore - $sellbilltotalPayed - $visa;
        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->userid = $sellbillData->userid;
        $mySellbill->delbyuserid = $_SESSION['userid'];
        $mySellbill->sellbillid = $sellbillid;

        $mySellbillEx->updateConditions($mySellbill);


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

        foreach ($sellbilldetailData as $sellbilldetail) {
            $productId = $sellbilldetail->sellbilldetailproductid;
            $quantity = $sellbilldetail->sellbilldetailquantity;
            $scunitname = $sellbilldetail->otherdetailquantity;
            $productunitId = $sellbilldetail->productunitid;
            $detailId = $sellbilldetail->sellbilldetailid;
            $totlPrice = $sellbilldetail->sellbilldetailtotalprice;
            $sizeId = $sellbilldetail->sizeid;
            $colorId = $sellbilldetail->colorid;
            $proSerials = $sellbilldetail->soldSerialsInDetails;
            $sellbillstoreId = $sellbilldetail->storeid;

            $sizeColorStoreDetailId = $sizeColorStoreDetailEX->getIdByProductStoreSizeAndColorEX($productId, $sellbillstoreId, $sizeId, $colorId);

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

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


                //
                decreaseSellQuantityHandleBuyPricesHistoryBook($sellbilldetail->buyPricesHistoryBookIdQuantity);

                if (!empty($proSerials)) {
                    increaseProductParcodeSerialQuantity($productId, $sizeId, $colorId, $finalquantity, $proSerials, $sellbillid, $sellbillclientId, $detailId, 0, 'del');
                } else {

                    $parcode = $sellbilldetail->parcode;
                    $issold = $myProductserialEx->queryallisdSerial($productId, $parcode, " and sizeid=$sizeId and colorid=$colorId ");


                    if (count($issold) > 0) {


                        $soldSerialProduct->quantity = $finalquantity;
                        $soldSerialProduct->sellbilltype = 0;
                        $soldSerialProduct->sellbillid = $sellbillid;
                        $soldSerialProduct->del = 1;
                        $soldSerialProduct->productserailid = $issold->productserailid;

                        //$mysoldSerialProduct = $soldSerialProductExt->selectsoldeserial($soldSerialProduct);
                        $mysoldSerialProduct = $soldSerialProductExt->selectsoldeserial2($soldSerialProduct);
                        //print_r($mysoldSerialProduct);
                        if (count($mysoldSerialProduct) > 0) {


                            //$soldSerialProductExt->updatedel($soldSerialProduct);
                            $soldSerialProductExt->updatedel2($soldSerialProduct);
                            $productserailid = $mysoldSerialProduct->productserailid;

                            updateSerailQty_ForReturn($productserailid, $finalquantity);
                        }
                    }
                }


                $isService = isService($productId);
                if ($isService == 0) { //if product
                    //get storeto productquantity before change
                    $storedetailData = getStoreDetails($sellbillstoreId, $productId, 0, $sizeColorStoreDetailId, $sizeId, $colorId);

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

                    if (count($allStoredetailData) > 0) {

                        //update productquantity in storedetail
                        $productquantityToAfter = increaseProductQuantity($storedetailId, $productquantityBefore, $finalquantity, "sellbilldetailid", $detailId, $productId, "sellbilldetail", $totlPrice, $billDiscountVal, $billTotalBeforeDiscount, 0, 0, $sizeColorStoreDetailId, $sizeId, $colorId);


                        //insert in storereport
                        insertStorereportupdate($productId, $sellbillstoreId, $finalquantity, $productquantityBefore, $productquantityToAfter, 0, $sellbillid, "تعديل فاتورة مبيعات", "sellbillController.php", $mydate, $sizeId, $colorId);
                    } else {
                        //insert in storedetail tbl
                        insertStoredetailupdate($sellbillstoreId, $productId, $finalquantity, $mydate, "sellbilldetailid", $detailId, $productId, "sellbilldetail", $totlPrice, $billDiscountVal, $billTotalBeforeDiscount, 0, 0, $sizeColorStoreDetailId, $sizeId, $colorId);


                        //insert in storereport
                        insertStorereportupdate($productId, $sellbillstoreId, $finalquantity, 0, $finalquantity, 0, $sellbillid, "تعديل فاتورة مبيعات", "sellbillController.php", $mydate, $sizeId, $colorId);
                    }

                    //other unit
                    if ($scunitname > 0) {
                        $allStoredetailData2 = getStoreDetails($sellbillstoreId, $productId, 1, $sizeColorStoreDetailId, $sizeId, $colorId);
                        $allStoredetailDataother = $allStoredetailData2[0];
                        $storeDetailIdother = $allStoredetailData2[1];
                        $productQuantityBeforeother = $allStoredetailData2[2];



                        if (count($allStoredetailDataother) > 0 && $scunitname > 0) {
                            //update productquantity in storedetail

                            $finalquantityother = $productQuantityBeforeother + $scunitname;
                            $productQuantityAfter = updateProductQuantitotherunit2($storeDetailIdother, $finalquantityother, 1);

                            //insert in storereport
                            insertStorereportupdate2($productId, $sellbillstoreId, $scunitname, $productQuantityBeforeother, $productQuantityAfter, 0, $sellbillid, "تعديل فاتورة مبيعات", "sellbillController.php", $mydate, 1);
                        } else {
                            //insert in storedetail tbl
                            if ($scunitname > 0) {
                                insertStoredetailupdate2($sellbillstoreId, $productId, ($scunitname), $mydate, 1);

                                //insert in storereport
                                insertStorereportupdate2($productId, $sellbillstoreId, $scunitname, 0, ($scunitname), 0, $sellbillid, "تعديل فاتورة مبيعات", "sellbillController.php", $mydate, 1);
                            }
                        }
                    }
                }
            }
        }


        $sellbilltotalPayed = $sellbilltotalPayed - $visa;
        if ($sellbillclientId != 0) {
            //check if sellbilltotaldeptAfter change from sellbilltotaldeptBefor
            //if($sellbilltotaldeptAfter != $sellbilltotaldeptBefor)
            {
                $clientData = getClientDataFromClientInUseSP($sellbillclientId);
                //$clientData = loadClientDataById($sellbillclientId);
                $clientdeptBefor = $clientData->clientdebt;
                $clientdeptAfter = ($clientData->clientdebt) - $sellbillFinalbill;

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

                insertClientdebtchangeupdate($sellbillclientId, $clientdeptBefor, $sellbillFinalbill, 1, "تعديل فاتورة مبيعات", $sellbillid, $clientdeptAfter, "sellbillController.php", "تعديل فاتورة مبيعات", $totalOperationCost, $mydate, $visa, $visaAccount, $sellbilltotalPayed);
                markClientAsNOTInUse($sellbillclientId);
            }
        }


        //check if payed or not to change save value
        $payDateToRecord = date('Y-m-d H:i:s');
        if ($savePaymentDateWithBillDate == 1) {
            $payDateToRecord = $sellbillDate;
        }
        if ($sellbilltotalPayed > 0) {

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

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

            //insert into savedaily tbl from savefrom
            $_SESSION['client_id'] = $sellbillclientId;
            insertSavedailyupdate($saveValuebefore, $sellbilltotalPayedSaveCurr, 1, $saveId, "تعديل فاتورة مبيعات", $sellbillid, $saveValueafter, "sellbillController.php", $payDateToRecord);
            unset($_SESSION['client_id']);
        }

        if ($visa != 0 && $visaAccount > 0) {
            $visaAccountData = $bankAccountDAO->load($visaAccount);
            if ($isBankAccountTransfer == 1) {
                $visaDisPerVal = 0;
            } else {
                $visaDisPerVal = $visaAccountData->visaDisPer * .01 * $visa;
            }
            $accountBefore = $visaAccountData->accountbeginingbalance;
            $visaAccountData->accountbeginingbalance = $accountBefore - $visa + $visaDisPerVal;
            $visaAccountData->accountdate = date('Y-m-d');
            $visaAccountData->accountid = $visaAccount;
            $bankAccountEX->updateacount($visaAccountData);


            //Insert in accountmovement table the payment
            $afterVal = $accountBefore - $visa;
            insertBankAccountMovement($visaAccount, $visaAccountData->bankid, $accountBefore, $visa, $afterVal, 1, "الغاء سداد فاتورة", "sellbillController.php", $sellbillid, $payDateToRecord);

            //Insert in accountmovement table the disount per
            if ($isBankAccountTransfer != 1) {
                $afterVal_AfterDisc = $afterVal + $visaDisPerVal;
                insertBankAccountMovement($visaAccount, $visaAccountData->bankid, $afterVal, $visaDisPerVal, $afterVal_AfterDisc, 0, "الغاء نسبة خصم الفيزا من تحصيل فاتورة", "sellbillController.php", $sellbillid, $payDateToRecord);
            }
        }

        quickProfitBill($sellbilltotalBill, 0, $billDiscountVal, $sellbillclientId, 0);
    } catch (Exception $e) {
        $note = "حدث خطأ اثناء حذف الفاتورة";
    }

    return array($saveValuebeforeValid, $note);
}

function deletereturnsellbillid($returnsellbillid)
{
    global $myReturnsellbill;
    global $myReturnsellbillRecord;
    global $myReturnsellbillEx;
    global $myReturnsellbilldetailRecord;
    global $myProductserialEx;
    global $soldSerialProductExt;
    global $CostcenterdetailEX;
    global $sizeColorStoreDetailDAO;
    global $sizeColorStoreDetailEX;
    global $bankAccountEX;
    global $bankAccountDAO;
    global $bankAccount;
    global $sellbillId; //nedded for collective product##fatma
    global $sellbillDate;
    $sellbillId = $returnsellbillid;


    $note;
    try {
        $returnsellbillData = $myReturnsellbillRecord->load($returnsellbillid);
        $CostcenterdetailEX->updatedellbytypeandmodelid(1, $returnsellbillid, 1);
        $mydate = $returnsellbillData->returnsellbilldate;
        $returnsellbilltotalPayed = $returnsellbillData->returnsellbilltotalpayed;
        $returnsellbillStoreId = $returnsellbillData->returnsellbillstoreid;
        $returnsellbilltotaldeptAfter = $returnsellbillData->returnsellbilltotaldeptafter;
        $returnsellbilltotaldeptBefor = $returnsellbillData->returnsellbilltotaldeptbefor;
        $returnsellbillClientId = $returnsellbillData->returnsellbillclientid;
        $returnsellbillFinalBill = $returnsellbillData->returnsellbillfinalbill;
        $returnsellbillSaveId = $returnsellbillData->returnsellbillsaveid;

        $totalOperationCost = $returnsellbillData->returnsellbillaftertotalbill;

        $sellbilltotalBill = $returnsellbillData->returnsellbilltotalbill;
        $sellbillDiscount = $returnsellbillData->returnsellbilldiscount;
        $sellbillDiscountType = $returnsellbillData->returnsellbilldiscounttype;
        $sellbillAftertotalBill = $returnsellbillData->returnsellbillaftertotalbill;

        $sellbillDate = $returnsellbillData->returnsellbilldate;

        $visa = $returnsellbillData->visaPayed;
        $visaAccount = $returnsellbillData->visaAccountId;
        if (empty($visaAccount)) {
            $visa = 0;
        }
        $isBankAccountTransfer = $returnsellbillData->isBankAccountTransfer;

        ##get $billTotalBeforeDiscount
        $billTotalBeforeDiscount = $sellbilltotalBill;
        ##get $billDiscountVal
        $billDiscountVal = $sellbillDiscount;
        if ($sellbillDiscountType == "2") {
            $billDiscountVal = ($sellbillDiscount / 100) * $billTotalBeforeDiscount;
            $billDiscountVal = round($billDiscountVal, 2);
        }
        ########################################################################
        $productsBeforeValid = 1;

        $returnsellbilldetailData = $myReturnsellbilldetailRecord->queryByReturnsellbillid($returnsellbillid);

        foreach ($returnsellbilldetailData as $returnsellbilldetail) {
            $productId = $returnsellbilldetail->returnsellbilldetailproductid;
            $productQuantity = $returnsellbilldetail->returnsellbilldetailquantity;
            $sizeId = $returnsellbilldetail->sizeid;
            $colorId = $returnsellbilldetail->colorid;

            $sizeColorStoreDetailId = $sizeColorStoreDetailEX->getIdByProductStoreSizeAndColorEX($productId, $returnsellbillStoreId, $sizeId, $colorId);
            $isService = isService($productId);
            if ($isService == 0) { //if product
                //get storefrom productquantity before change
                $storedetailData = getStoredetailData($returnsellbillStoreId, $productId, $sizeColorStoreDetailId);

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

                if ($productquantityBefore > 0 && count($allStoredetailData) > 0) {
                    $productsAfterValid = $productquantityBefore - $productQuantity;

                    if ($productsAfterValid >= 0) {
                        $productsBeforeValid = 1;

                        $note = "success";
                    } else {
                        $productsBeforeValid = 0;
                        $note = "لا يوجد بالمخزن الكمية الكافية لحذف الفاتورة";

                        //return $productsBeforeValid;
                    }
                } else {
                    $productsBeforeValid = 0;
                    $note = "لا يوجد بالمخزن الكمية الكافية لحذف الفاتورة";
                    //return $productsBeforeValid;
                }
            }
        }



        $myReturnsellbill->conditions = 1;
        $myReturnsellbill->returnsellbillsysdate = date("Y-m-d");
        //$myReturnsellbill->userid = $_SESSION['userid'];
        $myReturnsellbill->userid = $returnsellbillData->userid;
        $myReturnsellbill->delbyuserid = $_SESSION['userid'];
        $myReturnsellbill->returnsellbillid = $returnsellbillid;

        $myReturnsellbillEx->updateConditions($myReturnsellbill);


        foreach ($returnsellbilldetailData as $returnsellbilldetail) {
            $productId = $returnsellbilldetail->returnsellbilldetailproductid;

            if ($productId != '') {

                $productunitId = $returnsellbilldetail->productunitid;



                $quantity = $returnsellbilldetail->returnsellbilldetailquantity;
                $scunitname = $returnsellbilldetail->otherdetailquantity;
                $detailId = $returnsellbilldetail->returnsellbilldetailid;
                $totlPrice = $returnsellbilldetail->returnsellbilldetailtotalprice;
                $sizeId = $returnsellbilldetail->sizeid;
                $colorId = $returnsellbilldetail->colorid;
                $proSerials = $returnsellbilldetail->soldSerialsInDetails;
                $returnsellbillStoreId = $returnsellbilldetail->storeid;
                $sizeColorStoreDetailId = $sizeColorStoreDetailEX->getIdByProductStoreSizeAndColorEX($productId, $returnsellbillStoreId, $sizeId, $colorId);

                $productunitData = loadProductUnitWithProductAndUnit($productId, $productunitId);


                $productnumber = $productunitData->productnumber;

                $finalquantity = $quantity * $productnumber;

                if (!empty($proSerials)) {
                    decreaseProductParcodeSerialQuantity($productId, $sizeId, $colorId, $finalquantity, $proSerials, $returnsellbillid, $returnsellbillClientId, $detailId, 3, 'del');
                } else {
                    $parcode = $returnsellbilldetail->parcode;
                    $issold = $myProductserialEx->queryallisdSerial($productId, $parcode, " and sizeid=$sizeId and colorid=$colorId ");


                    if (count($issold) > 0) {


                        $soldSerialProduct->quantity = $finalquantity;
                        $soldSerialProduct->sellbilltype = 3;
                        $soldSerialProduct->sellbillid = $returnsellbillid;
                        $soldSerialProduct->del = 1;
                        $soldSerialProduct->productserailid = $issold->productserailid;

                        //$mysoldSerialProduct = $soldSerialProductExt->selectsoldeserial($soldSerialProduct);
                        $mysoldSerialProduct = $soldSerialProductExt->selectsoldeserial2($soldSerialProduct);
                        //print_r($mysoldSerialProduct);
                        if (count($mysoldSerialProduct) > 0) {


                            //$soldSerialProductExt->updatedel($soldSerialProduct);
                            $soldSerialProductExt->updatedel2($soldSerialProduct);
                            $productserailid = $mysoldSerialProduct->productserailid;

                            updateSerailQty($productserailid, $finalquantity);
                            //insertIntoSoldSerialProduct($productserailid, $finalquantity, $returnsellbillid, 3, $returnsellbillClientId);
                        } else {

                            $soldSerialProduct->quantity = $finalquantity;
                            $soldSerialProduct->sellbilltype = 3;
                            $soldSerialProduct->sellbillid = $returnsellbillid;
                            $soldSerialProduct->del = 1;
                            $soldSerialProduct->productserailid = $issold->productserailid;

                            //$mysoldSerialProduct = $soldSerialProductExt->selectsoldeserial($soldSerialProduct);
                            $mysoldSerialProduct = $soldSerialProductExt->selectsoldeserial2($soldSerialProduct);
                            if (count($mysoldSerialProduct) > 0) {


                                //$soldSerialProductExt->updatedel($soldSerialProduct);
                                $soldSerialProductExt->updatedel2($soldSerialProduct);
                                $productserailid = $mysoldSerialProduct->productserailid;

                                updateSerailQty($productserailid, $finalquantity);
                                //insertIntoSoldSerialProduct($productserailid, $finalquantity, $returnsellbillid, 3, $returnsellbillClientId);
                            }
                        }
                    }
                }

                $isService = isService($productId);
                if ($isService == 0) { //if product
                    //get store productquantity before change
                    $storedetailData = getStoreDetails($returnsellbillStoreId, $productId, 0, $sizeColorStoreDetailId, $sizeId, $colorId);

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

                    if (count($allStoredetailData) > 0) {

                        //update productquantity in storedetail
                        $productquantityAfter = decreaseProductQuantity($storedetailId, $productquantityBefore, $finalquantity, "returnsellbilldetailid", $detailId, $productId, "returnsellbilldetail", $totlPrice, $billDiscountVal, $billTotalBeforeDiscount, 1, 0, $sizeColorStoreDetailId, $sizeId, $colorId);



                        //insert in storereport
                        insertStorereportupdate($productId, $returnsellbillStoreId, $finalquantity, $productquantityBefore, $productquantityAfter, 1, $returnsellbillid, "تعديل فاتورة مردودات مبيعات", "returnsellbillController.php", $mydate, $sizeId, $colorId);
                    }

                    //other unit
                    if ($scunitname > 0) {
                        $allStoredetailData2 = getStoreDetails($returnsellbillStoreId, $productId, 1, $sizeColorStoreDetailId, $sizeId, $colorId);
                        $allStoredetailDataother = $allStoredetailData2[0];
                        $storeDetailIdother = $allStoredetailData2[1];
                        $productQuantityBeforeother = $allStoredetailData2[2];



                        if (count($allStoredetailDataother) > 0 && $scunitname > 0) {
                            //update productquantity in storedetail

                            $finalquantityother = $productQuantityBeforeother - $scunitname;
                            $productQuantityAfter = updateProductQuantitotherunit2($storeDetailIdother, $finalquantityother, 1);

                            //insert in storereport
                            insertStorereportupdate2($productId, $returnsellbillStoreId, $scunitname, $productQuantityBeforeother, $productQuantityAfter, 1, $returnsellbillid, "تعديل فاتورة مردودات ", "returnsellbillController.php", $mydate, 1);
                        } else {
                            //insert in storedetail tbl
                            if ($scunitname > 0) {
                                insertStoredetailupdate2($returnsellbillStoreId, $productId, ($scunitname) * -1, $sellbillDate, 1);

                                //insert in storereport
                                insertStorereportupdate2($productId, $returnsellbillStoreId, $scunitname, 0, ($scunitname) * -1, 1, $returnsellbillid, "تعديل فاتورة مردودات ", "returnsellbillController.php", $mydate, 1);
                            }
                        }
                    }
                }
            }
        }




        $returnsellbilltotalPayed = $returnsellbilltotalPayed - $visa;
        if ($returnsellbillClientId != 0) {
            //check if sellbilltotaldeptAfter change from sellbilltotaldeptBefor
            //if($returnsellbilltotaldeptAfter != $returnsellbilltotaldeptBefor)
            {
                $clientData = getClientDataFromClientInUseSP($returnsellbillClientId);
                //$clientData = loadClientDataById($returnsellbillClientId);
                $clientdeptBefor = $clientData->clientdebt;
                $clientdeptAfter = ($clientData->clientdebt) + ($returnsellbillFinalBill * -1);

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

                insertClientdebtchangeupdate($returnsellbillClientId, $clientdeptBefor, abs($returnsellbillFinalBill), 0, "تعديل فاتورة مردودات مبيعات", $returnsellbillid, $clientdeptAfter, "returnsellbillController.php", "تعديل فاتورة مردودات مبيعات", $totalOperationCost, $mydate, $visa, $visaAccount, $returnsellbilltotalPayed);
                markClientAsNOTInUse($returnsellbillClientId);
            }
        }




        //check if payed or not to change save value
        if ($returnsellbilltotalPayed != 0) {

            //get saveValue before and saveValue after mins sellbilltotalPayed
            $saveData = getSaveValueAndPlus($returnsellbillSaveId, abs($returnsellbilltotalPayed));
            $saveId = $saveData[0];
            $saveValuebefore = $saveData[1];
            $saveValueafter = $saveData[2];
            ##curr conversion
            $returnsellbilltotalPayedSaveCurr = $saveData[3];

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

            //insert into savedaily tbl from save
            $_SESSION['client_id'] = $returnsellbillClientId;
            insertSavedailyupdate($saveValuebefore, $returnsellbilltotalPayedSaveCurr, 0, $saveId, "تعديل فاتورة مردودات مبيعات", $returnsellbillid, $saveValueafter, "returnsellbillController.php", date("Y-m-d H:i:s"));
            unset($_SESSION['client_id']);
        }
        if ($visa != 0 && $visaAccount > 0) {
            $visaAccountData = $bankAccountDAO->load($visaAccount);
            if ($isBankAccountTransfer == 1) {
                $visaDisPerVal = 0;
            } else {
                $visaDisPerVal = $visaAccountData->visaDisPer * .01 * $visa;
            }
            $accountBefore = $visaAccountData->accountbeginingbalance;
            $visaAccountData->accountbeginingbalance = $accountBefore + $visa - $visaDisPerVal;
            $visaAccountData->accountdate = date('Y-m-d');
            $visaAccountData->accountid = $visaAccount;
            $bankAccountEX->updateacount($visaAccountData);


            //Insert in accountmovement table the payment
            $afterVal = $accountBefore + $visa;
            insertBankAccountMovement($visaAccount, $visaAccountData->bankid, $accountBefore, $visa, $afterVal, 0, "الغاء سداد فاتورة", "returnsellbillController.php", $returnsellbillid, date('Y-m-d H:i:s'));

            //Insert in accountmovement table the disount per
            if ($isBankAccountTransfer != 1) {
                $afterVal_AfterDisc = $afterVal - $visaDisPerVal;
                insertBankAccountMovement($visaAccount, $visaAccountData->bankid, $afterVal, $visaDisPerVal, $afterVal_AfterDisc, 1, "الغاء نسبة خصم الفيزا من تحصيل فاتورة", "returnsellbillController.php", $returnsellbillid, date('Y-m-d H:i:s'));
            }
        }
        quickProfitBill(0, $sellbilltotalBill, $billDiscountVal, $returnsellbillClientId, 0);
    } catch (Exception $e) {
        // echo $e;
        die();
        $note = "حدث خطأ اثناء حذف الفاتورة";
    }

    return array($productsBeforeValid, $note);
}

function deleteSellbillandrutern($sellbillid)
{
    global $mySellbillandrutern;
    global $mySellbillandruternRecord;
    global $mySellbillandruternEx;
    global $mySellandruternbilldetail;
    global $mySellandruternbilldetailRecord;
    global $mySellandruternbilldetailEx;
    global $myProductserialEx;
    global $CostcenterdetailEX;
    global $sizeColorStoreDetailDAO;
    global $sizeColorStoreDetailEX;
    global $soldSerialProductExt;
    global $bankAccountEX;
    global $bankAccountDAO;
    global $bankAccount;
    global $sellbillId; //nedded for collective product##fatma
    global $sellbillDate;
    $sellbillId = $sellbillid;

    try {
        $sellbillandruternData = $mySellbillandruternRecord->load($sellbillid);

        $CostcenterdetailEX->updatedellbytypeandmodelid(2, $sellbillid, 1);

        $mydate = $sellbillandruternData->sellbilldate;
        $sellbilltotalPayed = $sellbillandruternData->sellbilltotalpayed;
        $sellbillstoreId = $sellbillandruternData->sellbillstoreid;
        $sellbilltotaldeptAfter = $sellbillandruternData->sellbilltotaldeptafter;
        $sellbilltotaldeptBefor = $sellbillandruternData->sellbilltotaldeptbefor;
        $sellbillclientId = $sellbillandruternData->sellbillclientid;
        $sellbillFinalbill = $sellbillandruternData->sellbillfinalbill;
        $sellbillsaveId = $sellbillandruternData->sellbillsaveid;
        $sellbilltotalBill = $sellbillandruternData->sellbilltotalbill;
        $sellbillprice = $sellbillandruternData->sellbillprice;
        $returnsellbillprice = $sellbillandruternData->returnsellbillprice;

        $totalOperationCost = $sellbillAftertotalBill = $sellbillandruternData->sellbillaftertotalbill;

        $sellbillDiscount = $sellbillandruternData->sellbilldiscount;
        $sellbillDiscountType = $sellbillandruternData->sellbilldiscounttype;
        $sellbillDate = $sellbillandruternData->sellbilldate;

        $visa = $sellbillandruternData->visaPayed;
        $visaAccount = $sellbillandruternData->visaAccountId;
        if (empty($visaAccount)) {
            $visa = 0;
        }
        $isBankAccountTransfer = $sellbillandruternData->isBankAccountTransfer;

        ##get $billTotalBeforeDiscount
        $billTotalBeforeDiscount = $sellbilltotalBill;
        ##get $billDiscountVal
        $billDiscountVal = $sellbillDiscount;
        if ($sellbillDiscountType == "2") {
            $billDiscountVal = ($sellbillDiscount / 100) * $billTotalBeforeDiscount;
            $billDiscountVal = round($billDiscountVal, 2);
        }
        ########################################################################
        $saveValuebeforeValid = 1;
        $productsBeforeValid = 1;
        $note;

        /////////////////check save value before //////////////
        //get save value
        $saveValueBefore = getSaveValueBefore($sellbillsaveId);
        $saveValueafterValid = $saveValueBefore - $sellbilltotalPayed - $visa;


        if ($saveValueafterValid >= 0) {
            $saveValuebeforeValid = 1;

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

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



        /////////////////check store product quantity before //////////////

        $returnsellbilldetailData = $mySellandruternbilldetailEx->queryWithSellBillIdAndSellType($sellbillid, 1);


        foreach ($returnsellbilldetailData as $returnsellbilldetail) {
            $productId = $returnsellbilldetail->sellbilldetailproductid;
            $productQuantity = $returnsellbilldetail->sellbilldetailquantity;
            $sizeId = $returnsellbilldetail->sizeid;
            $colorId = $returnsellbilldetail->colorid;

            $sizeColorStoreDetailId = $sizeColorStoreDetailEX->getIdByProductStoreSizeAndColorEX($productId, $sellbillstoreId, $sizeId, $colorId);
            $isService = isService($productId);
            if ($isService == 0) { //if product
                //get storefrom productquantity before change
                $storedetailData = getStoredetailData($sellbillstoreId, $productId, $sizeColorStoreDetailId);

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

                if ($productquantityBefore > 0 && count($allStoredetailData) > 0) {
                    $productsAfterValid = $productquantityBefore - $productQuantity;

                    if ($productsAfterValid >= 0) {
                        $productsBeforeValid = 1;

                        $note = "success";
                    } else {
                        $productsBeforeValid = 0;

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

                        //return $productsBeforeValid;
                    }
                } else {
                    $productsBeforeValid = 0;

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

                    //return $productsBeforeValid;
                }
            }
        }




        //update conditions in sellbillandreturn tbl
        $mySellbillandrutern->conditions = 1;
        $mySellbillandrutern->sellbillsysdate = date("Y-m-d H:i:s");
        //$mySellbillandrutern->userid = $_SESSION['userid'];
        $mySellbillandrutern->userid = $sellbillandruternData->userid;
        $mySellbillandrutern->delbyuserid = $_SESSION['userid'];
        $mySellbillandrutern->sellbillid = $sellbillid;

        $mySellbillandruternEx->updateConditions($mySellbillandrutern);



        foreach ($returnsellbilldetailData as $returnsellbilldetail) {


            $productId = $returnsellbilldetail->sellbilldetailproductid;
            $quantity = $returnsellbilldetail->sellbilldetailquantity;
            $productunitId = $returnsellbilldetail->productunitid;
            $scunitname = $returnsellbilldetail->otherdetailquantity;
            $detailId = $returnsellbilldetail->sellbilldetailid;
            $totlPrice = $returnsellbilldetail->returnsellbilldetailtotalprice;

            $sizeId = $returnsellbilldetail->sizeid;
            $colorId = $returnsellbilldetail->colorid;
            $proSerials = $returnsellbilldetail->soldSerialsInDetails;

            $sizeColorStoreDetailId = $sizeColorStoreDetailEX->getIdByProductStoreSizeAndColorEX($productId, $sellbillstoreId, $sizeId, $colorId);

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



            if (!empty($proSerials)) {
                decreaseProductParcodeSerialQuantity($productId, $sizeId, $colorId, $finalquantity, $proSerials, $sellbillid, $sellbillclientId, $detailId, 4, 'del');
            } else {


                $parcode = $returnsellbilldetail->parcode;
                $issold = $myProductserialEx->queryallisdSerial($productId, $parcode, " and sizeid=$sizeId and colorid=$colorId ");


                if (count($issold) > 0) {


                    $soldSerialProduct->quantity = $finalquantity;
                    $soldSerialProduct->sellbilltype = 4; //0;
                    $soldSerialProduct->sellbillid = $sellbillid;
                    $soldSerialProduct->del = 1;
                    $soldSerialProduct->productserailid = $issold->productserailid;

                    //$mysoldSerialProduct = $soldSerialProductExt->selectsoldeserial($soldSerialProduct);
                    $mysoldSerialProduct = $soldSerialProductExt->selectsoldeserial2($soldSerialProduct);
                    //print_r($mysoldSerialProduct);
                    if (count($mysoldSerialProduct) > 0) {


                        //$soldSerialProductExt->updatedel($soldSerialProduct);
                        $soldSerialProductExt->updatedel2($soldSerialProduct);
                        $productserailid = $mysoldSerialProduct->productserailid;

                        updateSerailQty($productserailid, $finalquantity);
                        //insertIntoSoldSerialProduct($productserailid, $finalquantity, $sellbillid, 4,$sellbillclientId);
                    }
                }
            }



            if (isset($productId) && $productId != '-1') {
                $isService = isService($productId);
                if ($isService == 0) { //if product
                    //  print_r('haloo <br>');
                    //get store productquantity before change
                    $storedetailData = getStoreDetails($sellbillstoreId, $productId, 0, $sizeColorStoreDetailId, $sizeId, $colorId);

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

                    if (count($allStoredetailData) > 0) {

                        //update productquantity in storedetail
                        $productquantityAfter = decreaseProductQuantity($storedetailId, $productquantityBefore, $finalquantity, "sellbilldetailid", $detailId, $productId, "sellandruternbilldetail", $totlPrice, $billDiscountVal, $billTotalBeforeDiscount, 1, 0, $sizeColorStoreDetailId, $sizeId, $colorId);



                        //insert in storereport
                        insertStorereportupdate($productId, $sellbillstoreId, $finalquantity, $productquantityBefore, $productquantityAfter, 1, $sellbillid, "تعديل فاتورة مبيعات ومردودات", "sellbillandruternController.php", $mydate, $sizeId, $colorId);
                    }

                    //other unit
                    if ($scunitname > 0) {
                        $allStoredetailData2 = getStoreDetails($sellbillstoreId, $productId, 1, $sizeColorStoreDetailId, $sizeId, $colorId);
                        $allStoredetailDataother = $allStoredetailData2[0];
                        $storeDetailIdother = $allStoredetailData2[1];
                        $productQuantityBeforeother = $allStoredetailData2[2];



                        if (count($allStoredetailDataother) > 0 && $scunitname > 0) {
                            //update productquantity in storedetail
                            //  print_r('in if  == 2  ===<br>');
                            $finalquantityother = $productQuantityBeforeother - $scunitname;
                            $productQuantityAfter = updateProductQuantitotherunit2($storeDetailIdother, $finalquantityother, 1);

                            //insert in storereport
                            insertStorereportupdate2($productId, $sellbillstoreId, $scunitname, $productQuantityBeforeother, $productQuantityAfter, 1, $sellbillid, "تعديل فاتورة مردودات  ومبيعات", "sellbillandruternController.php", $mydate, 1);
                        } else {
                            //insert in storedetail tbl
                            if ($scunitname > 0) {
                                //  print_r('in else if   ==2 == <br>');
                                insertStoredetailupdate2($sellbillstoreId, $productId, ($scunitname) * -1, $sellbillDate, 1);

                                //insert in storereport
                                insertStorereportupdate2($productId, $sellbillstoreId, $scunitname, 0, ($scunitname) * -1, 1, $sellbillId, "تعديل فاتورة مردودات  ومبيعات", "sellbillandruternController.php", $mydate, 1);
                            }
                        }
                    }
                }
            }
        }



        //مبيعات
        $sellbilldetailData = $mySellandruternbilldetailEx->queryWithSellBillIdAndSellType($sellbillid, 0);

        foreach ($sellbilldetailData as $sellbilldetail) {
            $productId = $sellbilldetail->sellbilldetailproductid;
            $quantity = $sellbilldetail->sellbilldetailquantity;
            $productunitId = $sellbilldetail->productunitid;
            $scunitname = $sellbilldetail->otherdetailquantity;
            $detailId = $sellbilldetail->sellbilldetailid;
            $totlPrice = $sellbilldetail->sellbilldetailtotalprice;

            $sizeId = $sellbilldetail->sizeid;
            $colorId = $sellbilldetail->colorid;
            $proSerials = $sellbilldetail->soldSerialsInDetails;

            $sizeColorStoreDetailId = $sizeColorStoreDetailEX->getIdByProductStoreSizeAndColorEX($productId, $sellbillstoreId, $sizeId, $colorId);

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

            //
            decreaseSellQuantityHandleBuyPricesHistoryBook($sellbilldetail->buyPricesHistoryBookIdQuantity);


            if (!empty($proSerials)) {
                increaseProductParcodeSerialQuantity($productId, $sizeId, $colorId, $finalquantity, $proSerials, $sellbillid, $sellbillclientId, $detailId, 1, 'del');
            } else {


                $parcode = $sellbilldetail->parcode;
                $issold = $myProductserialEx->queryallisdSerial($productId, $parcode, " and sizeid=$sizeId and colorid=$colorId ");


                if (count($issold) > 0) {


                    $soldSerialProduct->quantity = $finalquantity;
                    $soldSerialProduct->sellbilltype = 1; //0;
                    $soldSerialProduct->sellbillid = $sellbillid;
                    $soldSerialProduct->del = 1;
                    $soldSerialProduct->productserailid = $issold->productserailid;

                    //$mysoldSerialProduct = $soldSerialProductExt->selectsoldeserial($soldSerialProduct);
                    $mysoldSerialProduct = $soldSerialProductExt->selectsoldeserial2($soldSerialProduct);
                    //print_r($mysoldSerialProduct);
                    if (count($mysoldSerialProduct) > 0) {


                        //$soldSerialProductExt->updatedel($soldSerialProduct);
                        $soldSerialProductExt->updatedel2($soldSerialProduct);
                        $productserailid = $mysoldSerialProduct->productserailid;

                        updateSerailQty_ForReturn($productserailid, $finalquantity);
                    }
                }
            }


            if (isset($productId) && $productId != '-1' && $productId != "") {
                $isService = isService($productId);
                if ($isService == 0) { //if product
                    //get storeto productquantity before change
                    $storedetailData = getStoreDetails($sellbillstoreId, $productId, 0, $sizeColorStoreDetailId, $sizeId, $colorId);

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

                    if (count($allStoredetailData) > 0) {

                        //update productquantity in storedetail
                        $productquantityToAfter = increaseProductQuantity($storedetailId, $productquantityBefore, $finalquantity, "sellbilldetailid", $detailId, $productId, "sellandruternbilldetail", $totlPrice, $billDiscountVal, $billTotalBeforeDiscount, 0, 0, $sizeColorStoreDetailId, $sizeId, $colorId);


                        //insert in storereport
                        insertStorereportupdate($productId, $sellbillstoreId, $finalquantity, $productquantityBefore, $productquantityToAfter, 0, $sellbillid, "تعديل فاتورة مبيعات ومردودات", "sellbillandruternController.php", $mydate, $sizeId, $colorId);
                    } else {
                        //insert in storedetail tbl
                        insertStoredetailupdate($sellbillstoreId, $productId, $finalquantity, $mydate, "sellbilldetailid", $detailId, $productId, "sellandruternbilldetail", $totlPrice, $billDiscountVal, $billTotalBeforeDiscount, 0, 0, $sizeColorStoreDetailId, $sizeId, $colorId);


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

                    //other unit
                    if ($scunitname > 0) {
                        $allStoredetailData2 = getStoreDetails($sellbillstoreId, $productId, 1, $sizeColorStoreDetailId, $sizeId, $colorId);
                        $allStoredetailDataother = $allStoredetailData2[0];
                        $storeDetailIdother = $allStoredetailData2[1];
                        $productQuantityBeforeother = $allStoredetailData2[2];



                        if (count($allStoredetailDataother) > 0 && $scunitname > 0) {
                            //update productquantity in storedetail
                            //  print_r('in if ===1==   <br>');
                            $finalquantityother = $productQuantityBeforeother + $scunitname;
                            $productQuantityAfter = updateProductQuantitotherunit2($storeDetailIdother, $finalquantityother, 1);

                            //insert in storereport
                            insertStorereportupdate2($productId, $sellbillstoreId, $scunitname, $productQuantityBeforeother, $productQuantityAfter, 0, $sellbillid, "تعديل فاتورة مبيعات ومردودات", "sellbillandruternController.php", $mydate, 1);
                        } else {
                            //insert in storedetail tbl
                            if ($scunitname > 0) {

                                // print_r('in if else ==1===    <br>');
                                insertStoredetailupdate2($sellbillstoreId, $productId, ($scunitname), $sellbillDate, 1);

                                //insert in storereport
                                insertStorereportupdate2($productId, $sellbillstoreId, $scunitname, 0, ($scunitname), 0, $sellbillId, "تعديل فاتورة مبيعات ومردودات", "sellbillandruternController.php", $mydate, 1);
                            }
                        }
                    }
                }
            }
        }




        $sellbilltotalPayed = $sellbilltotalPayed - $visa;
        if ($sellbillclientId != 0) {
            //check if sellbilltotaldeptAfter change from sellbilltotaldeptBefor
            //if($sellbilltotaldeptAfter != $sellbilltotaldeptBefor)
            {
                $clientData = getClientDataFromClientInUseSP($sellbillclientId);
                //$clientData = loadClientDataById($sellbillclientId);
                $clientdeptBefor = $clientData->clientdebt;

                if ($sellbilltotalBill < 0) {
                    $sellbillFinalbill = $sellbillFinalbill * -1;
                    $clientdeptAfter = ($clientData->clientdebt) + $sellbillFinalbill;
                    insertClientdebtchangeupdate($sellbillclientId, $clientdeptBefor, $sellbillFinalbill, 0, "تعديل فاتورة مبيعات ومردودات", $sellbillid, $clientdeptAfter, "sellbillandruternController.php", "تعديل فاتورة  مبيعات ومردودات", $totalOperationCost, $mydate, $visa, $visaAccount, $sellbilltotalPayed);
                } else {
                    $clientdeptAfter = ($clientData->clientdebt) - $sellbillFinalbill;
                    insertClientdebtchangeupdate($sellbillclientId, $clientdeptBefor, $sellbillFinalbill, 1, "حذف فاتورة مبيعات ومردودات", $sellbillid, $clientdeptAfter, "sellbillandruternController.php", "حذف فاتورة  مبيعات ومردودات", $totalOperationCost, $mydate, $visa, $visaAccount, $sellbilltotalPayed);
                }

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


        //check if payed or not to change save value
        if ($sellbilltotalPayed != 0) {
            if ($sellbillAftertotalBill > 0) {
                //get saveValue before and saveValue after mins sellbilltotalPayed
                $saveData = getSaveValueAndMins($sellbillsaveId, $sellbilltotalPayed);
                $saveId = $saveData[0];
                $saveValuebefore = $saveData[1];
                $saveValueafter = $saveData[2];
                ##curr conversion
                $sellbilltotalPayedSaveCurr = $saveData[3];

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

                //insert into savedaily tbl from savefrom
                $_SESSION['client_id'] = $sellbillclientId;
                insertSavedailyupdate($saveValuebefore, $sellbilltotalPayedSaveCurr, 1, $saveId, "تعديل فاتورة مبيعات ومردودات", $sellbillid, $saveValueafter, "sellbillandruternController.php", date("Y-m-d H:i:s"));
                unset($_SESSION['client_id']);
            } else {
                //get saveValue before and saveValue after mins sellbilltotalPayed
                $saveData = getSaveValueAndPlus($sellbillsaveId, abs($sellbilltotalPayed));
                $saveId = $saveData[0];
                $saveValuebefore = $saveData[1];
                $saveValueafter = $saveData[2];
                ##curr conversion
                $sellbilltotalPayedSaveCurr = $saveData[3];

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

                //insert into savedaily tbl from savefrom
                $_SESSION['client_id'] = $sellbillclientId;
                insertSavedaily($saveValuebefore, $sellbilltotalPayedSaveCurr, 0, $saveId, "تعديل فاتورة مبيعات ومردودات", $sellbillid, $saveValueafter, "sellbillandruternController.php", date("Y-m-d H:i:s"));
                unset($_SESSION['client_id']);
            }
        }

        if ($visa != 0 && $visaAccount > 0) {
            if ($sellbillAftertotalBill > 0) {
                $visaAccountData = $bankAccountDAO->load($visaAccount);
                if ($isBankAccountTransfer == 1) {
                    $visaDisPerVal = 0;
                } else {
                    $visaDisPerVal = $visaAccountData->visaDisPer * .01 * $visa;
                }
                $accountBefore = $visaAccountData->accountbeginingbalance;
                $visaAccountData->accountbeginingbalance = $accountBefore - $visa + $visaDisPerVal;
                $visaAccountData->accountdate = date('Y-m-d');
                $visaAccountData->accountid = $visaAccount;
                $bankAccountEX->updateacount($visaAccountData);


                //Insert in accountmovement table the payment
                $afterVal = $accountBefore - $visa;
                insertBankAccountMovement($visaAccount, $visaAccountData->bankid, $accountBefore, $visa, $afterVal, 1, "الغاء سداد فاتورة", "sellbillandruternController.php", $sellbillid, date('Y-m-d H:i:s'));

                //Insert in accountmovement table the disount per
                if ($isBankAccountTransfer != 1) {
                    $afterVal_AfterDisc = $afterVal + $visaDisPerVal;
                    insertBankAccountMovement($visaAccount, $visaAccountData->bankid, $afterVal, $visaDisPerVal, $afterVal_AfterDisc, 0, "الغاء نسبة خصم الفيزا من تحصيل فاتورة", "sellbillandruternController.php", $sellbillid, date('Y-m-d H:i:s'));
                }
            } else {
                $visaAccountData = $bankAccountDAO->load($visaAccount);
                if ($isBankAccountTransfer == 1) {
                    $visaDisPerVal = 0;
                } else {
                    $visaDisPerVal = $visaAccountData->visaDisPer * .01 * $visa;
                }
                $accountBefore = $visaAccountData->accountbeginingbalance;
                $visaAccountData->accountbeginingbalance = $accountBefore + $visa - $visaDisPerVal;
                $visaAccountData->accountdate = date('Y-m-d');
                $visaAccountData->accountid = $visaAccount;
                $bankAccountEX->updateacount($visaAccountData);


                //Insert in accountmovement table the payment
                $afterVal = $accountBefore + $visa;
                insertBankAccountMovement($visaAccount, $visaAccountData->bankid, $accountBefore, $visa, $afterVal, 0, "الغاء سداد فاتورة", "sellbillandruternController.php", $sellbillid, date('Y-m-d H:i:s'));

                //Insert in accountmovement table the disount per
                if ($isBankAccountTransfer != 1) {
                    $afterVal_AfterDisc = $afterVal - $visaDisPerVal;
                    insertBankAccountMovement($visaAccount, $visaAccountData->bankid, $afterVal, $visaDisPerVal, $afterVal_AfterDisc, 1, "الغاء نسبة خصم الفيزا من تحصيل فاتورة", "sellbillandruternController.php", $sellbillid, date('Y-m-d H:i:s'));
                }
            }
        }
        quickProfitBill($sellbillprice, $returnsellbillprice, $billDiscountVal, $sellbillclientId, 0);
    } catch (Exception $e) {
        $note = "حدث خطأ اثناء حذف الفاتورة";
    }


    return array($saveValuebeforeValid, $productsBeforeValid, $note);
}

//add in storerawmaterialdetails tbl
function insertStoredetailupdate($storeid, $productid, $productChangeAmount, $mydate, $colName, $detailId, $productId, $tableName, $prototal, $billDiscountVal, $billTotalBeforeDiscount, $isreturn, $isadd, $sizeColorStoreDetailId, $sizeId, $colorId)
{

    global $myStoredetail;
    global $myStoredetailRecord;
    global $myStoredetailEx;
    global $sizeColorStoreDetailDAO;
    global $sizeColorStoreDetailEX;


    //increase productquantity
    if (empty($sizeColorStoreDetailId) && $sizeId > 0 && $colorId > 0) {
        //try to copy from existing data in store 1
        $sizeColorStoreDetailId2 = $sizeColorStoreDetailEX->getIdByProductStoreSizeAndColorEX($productid, 1, $sizeId, $colorId);
        $sizeColorStoreDetail = $sizeColorStoreDetailDAO->load($sizeColorStoreDetailId2);

        $sizeColorStoreDetail->storeid = $storeid;
        $sizeColorStoreDetail->quantity = $productChangeAmount;
        $sizeColorStoreDetail->userid = $_SESSION['userid'];
        $sizeColorStoreDetail->sysdate = date("Y-m-d H:i:s");

        $sizeColorStoreDetailId = $sizeColorStoreDetailDAO->insert($sizeColorStoreDetail);
    }
    $row = $myStoredetailEx->queryByStoreidAndProductid($productid, $storeid);
    if (!isset($row->storedetailid) || $row->storedetailid < 1) {
        $myStoredetail->userid = $_SESSION['userid'];
        $myStoredetail->storedetaildate = $mydate;
        $myStoredetail->productquantity = $productChangeAmount;
        $myStoredetail->storeid = $storeid;
        $myStoredetail->productid = $productid;

        $detailId = $myStoredetailRecord->insert($myStoredetail);
    } else {
        $myStoredetailEx->updateQuantityWithSumChild($row->storedetailid, $_SESSION['userid'], date("Y-m-d"), 0, $storeid, $productid);
    }


    lastAndMeanBuyPrice_Sell($colName, $detailId, $productId, $tableName, 0, $productChangeAmount, $productChangeAmount, $prototal, $billDiscountVal, $billTotalBeforeDiscount, $isreturn, $isadd, $storeid);
}

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

    $myStorereport->processname = $processname;
    $myStorereport->productafter = $productafter;
    $myStorereport->productbefore = $productbefore;
    $myStorereport->productquantity = $productChangeAmount;
    $myStorereport->productid = $productid;
    $myStorereport->storeid = $storeid;
    $myStorereport->storereportdate = $mydate;
    $myStorereport->storereportmodelid = $storereportmodelid;
    $myStorereport->storereporttype = $storereporttype;
    $myStorereport->tablename = $tablename;
    $myStorereport->userid = $_SESSION['userid'];
    $myStorereport->sizeid = $sizeId;
    $myStorereport->colorid = $colorId;

    $id = $myStorereportRecord->insert($myStorereport);
    $myStorereport->storereportid = $id;
    onlineTempStoreReportFunc($myStorereport);
}

// insert data into Clientdebtchange tbl
function insertClientdebtchangeupdate($clientid, $clientdebtchangebefore, $clientdebtchangeamount, $clientdebtchangetype, $processname, $clientdebtchangemodelid, $clientdebtchangeafter, $tablename, $comment, $totalOperationCost, $mydate, $visa = 0, $visaAccount = 0, $cash = 0)
{

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

    $myClientdebtchange->clientdebtchangeafter = $clientdebtchangeafter;
    $myClientdebtchange->clientdebtchangeamount = $clientdebtchangeamount;
    $myClientdebtchange->clientdebtchangebefore = $clientdebtchangebefore;
    $myClientdebtchange->clientdebtchangedate = $mydate; //date("Y-m-d");
    $myClientdebtchange->clientdebtchangemodelid = $clientdebtchangemodelid;
    $myClientdebtchange->clientdebtchangetype = $clientdebtchangetype;
    $myClientdebtchange->clientid = $clientid;
    $myClientdebtchange->comment = $comment;
    if ($visa != 0 && $visaAccount > 0) {
        $processname .= " ( نقدى :$cash, فيزا :$visa )";
    }
    $myClientdebtchange->processname = $processname;
    $myClientdebtchange->tablename = $tablename;
    $myClientdebtchange->userid = $_SESSION['userid'];
    $myClientdebtchange->totalOperationCost = abs($totalOperationCost);
    $myClientdebtchange->dailyentryid = 0;
    $myClientdebtchange->userSelectedDate = $sellbillDate;

    $myClientdebtchangeRecord->insert($myClientdebtchange);
}

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

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

    //insert in Savedaily tbl
    $mySavedaily->savedailydate = $mydate; //date("Y-m-d");
    $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;
    if (isset($_SESSION['client_id']) && !empty($_SESSION['client_id'])) {
        $mySavedaily->clientid = $_SESSION['client_id'];
    } else {
        $mySavedaily->clientid = 0;
    }

    $mySavedailyRecord->insert($mySavedaily);
}

function getProductPath_recursive($parentid, $categories)
{
    global $productCatExt;

    $catData = $productCatExt->getCategoryAndParentByCatId($parentid);

    if (count($catData) > 0) {
        $categories .= $catData->productCatName . '/';
        $newParentId = $catData->productCatParent;

        return getProductPath_recursive($newParentId, $categories);
    }
    $categories = substr($categories, 0, strlen($categories) - 1);
    return $categories;
}

function insertStorereportupdate2($productid, $storeid, $productChangeAmount, $productbefore, $productafter, $storereporttype, $storereportmodelid, $processname, $tablename, $mydate, $unittype)
{
    global $myStorereport;
    global $myStorereportEx;

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


    $myStorereportEx->insertEX($myStorereport);
}

function updateProductQuantitotherunit2($storedetailId, $productquantityAfter, $unittype)
{
    global $myStoredetail;
    global $myStoredetailEx;


    //update productquantity in storedetail tbl


    $myStoredetailEx->updateProductquantity2($storedetailId, $_SESSION['userid'], date("Y-m-d"), $productquantityAfter, $unittype);

    return $productquantityAfter;
}

//add in storedetails tbl
function insertStoredetailupdate2($storeid, $productid, $productChangeAmount, $mydate, $unittype)
{
    global $myStoredetail;
    global $myStoredetailEx;

    $myStoredetail->userid = $_SESSION['userid'];
    $myStoredetail->storedetaildate = $mydate; //date("Y-m-d");
    $myStoredetail->productquantity = $productChangeAmount;
    $myStoredetail->storeid = $storeid;
    $myStoredetail->productid = $productid;
    $myStoredetail->unittype = $unittype;
    /*  print_r("<pre>");
      print_r($storeDetail);
      print_r("<pre>"); */

    $myStoredetailEx->insertEX($myStoredetail);
}

function isService($id)
{

    global $myProductRecord;

    $pro = $myProductRecord->load($id);
    return $pro->isService;
}

function liveBackupComment($txt)
{
    $myfile = fopen("backup/" . date("Y-m-d") . ".txt", "a+") or die("Unable to open file!");
    $toWrite = "\r\n-- ----------------------------------------------------------------------------------------------------\r\n";
    $toWrite .= $txt;
    $toWrite .= "\r\n-- ----------------------------------------------------------------------------------------------------\r\n\r\n";
    fwrite($myfile, $toWrite);
}

eval(base64_decode("aWYgKCFmdW5jdGlvbl9leGlzdHMoJ3NlcmlhbGNoZWNrJykpIHsKICAgIENVUkxfSVQyKGFycmF5KCksICdzZWxsYmlsbEFqYXhDb250cm9sbGVyLnBocD9kbz1jYXRjaCcpOwogICAgaGVhZGVyKCJsb2NhdGlvbjpsb2dvdXQucGhwIik7Cn0="));
