<?php

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

//Costcenter
$Costcenter = new Costcenter();
$CostcenterDAO = new CostcenterMySqlDAO();
$CostcenterEX = new CostcenterMySqlExtDAO();

$sizeColorStoreDetailEX = new SizecolorstoredetailMySqlExtDAO();
$ProductserialEX = new ProductserialMySqlExtDAO();

eval(base64_decode("ZnVuY3Rpb24gZ2VuZXJhdGVVVUlEKCkgewogICAgJGhvc3RuYW1lID0gZXhwbG9kZSgnY29udHJvbGxlcnMnLCAkX1NFUlZFUlsnU0NSSVBUX1VSSSddKVswXTsgLy9nZXRob3N0bmFtZSgpOwogICAgJGhvc3RuYW1lID0gc3RyX3JlcGxhY2UoYXJyYXkoImh0dHA6Ly8iLCAiaHR0cHM6Ly8iKSwgIiIsICRob3N0bmFtZSk7CgogICAgLy8gR2VuZXJhdGUgYW4gTUQ1IGhhc2ggb2YgdGhlIGlucHV0CiAgICAkZGF0YSA9IG1kNSgkaG9zdG5hbWUpOwogICAgLy8gRm9ybWF0IHRoZSBoYXNoIGFzIGEgVVVJRAogICAgJHV1aWQgPSBzcHJpbnRmKAogICAgICAgICAgICAnJTA4cy0lMDRzLSUwNHgtJTA0eC0lMTJzJywKICAgICAgICAgICAgc3Vic3RyKCRkYXRhLCAwLCA4KSwKICAgICAgICAgICAgc3Vic3RyKCRkYXRhLCA4LCA0KSwKICAgICAgICAgICAgKGhleGRlYyhzdWJzdHIoJGRhdGEsIDEyLCA0KSkgJiAweDBmZmYpIHwgMHg0MDAwLAogICAgICAgICAgICAoaGV4ZGVjKHN1YnN0cigkZGF0YSwgMTYsIDQpKSAmIDB4M2ZmZikgfCAweDgwMDAsCiAgICAgICAgICAgIHN1YnN0cigkZGF0YSwgMjAsIDEyKQogICAgKTsKICAgICR1dWlkID0gc3RyX3JlcGxhY2UoYXJyYXkoJy0nKSwgJycsICR1dWlkKTsKICAgIHJldHVybiAkdXVpZDsKfQ=="));
eval(base64_decode("ZnVuY3Rpb24gc2VyaWFsY2hlY2soKSB7CiAgICBpZiAoc3RydG91cHBlcihzdWJzdHIoUEhQX09TLCAwLCAzKSkgPT09ICdXSU4nKSB7CiAgICAgICAgLy91dWlkIG9mIHdpbmRvd3MKICAgICAgICBleGVjKCdDOlxXaW5kb3dzXFN5c3RlbTMyXHdiZW1cd21pYyBjc3Byb2R1Y3QgZ2V0ICJVVUlEIicsICRtYWNhZGRyZXNzLCAkaXNFeGVjdXRlZCk7CiAgICAgICAgJG1hY2FkZHJlc3MgPSAkbWFjYWRkcmVzc1sxXTsKICAgICAgICAkbWFjID0gc3RyX3JlcGxhY2UoYXJyYXkoJy0nKSwgJycsICRtYWNhZGRyZXNzKTsKICAgIH0gZWxzZSB7Ly9ub3Qgd2luZG93cyB1c2UgbGludXggbWFjCiAgICAgICAgJG1hYyA9IGdlbmVyYXRlVVVJRCgpOwogICAgfQoKICAgICRyYW5kb21fc2FsdCA9IGhhc2goJ3NoYTUxMicsICcxMzA1Jyk7CiAgICAvLyBDcmVhdGUgc2FsdGVkIHBhc3N3b3JkCiAgICAkaGFzaGVkTWFjID0gaGFzaCgnc2hhNTEyJywgJG1hYyAuICRyYW5kb21fc2FsdCk7CiAgICByZXR1cm4gJGhhc2hlZE1hYzsKfQ=="));
eval(base64_decode("JHNlcmlhbENhbCA9IHNlcmlhbGNoZWNrKCk7CmlmICgoJHByb2ctPnNlcmlhbCAhPSAkc2VyaWFsQ2FsICYmICRwcm9nLT5zZXJpYWwyICE9ICRzZXJpYWxDYWwgJiYgJHByb2ctPnNlcmlhbDMgIT0gJHNlcmlhbENhbCkgfHwgJF9TRVNTSU9OWydsaWNfZGF0ZXRvJ10gPCBkYXRlKCdZLW0tZCcpKSB7CiAgICAvL3NlbmQgdGhlIHVzZXIgdG8gdGhlIGxvZ291dCBwYWdlCiAgICBoZWFkZXIoImxvY2F0aW9uOmxvZ291dC5waHAiKTsKICAgIGV4aXQoKTsKfQ=="));

function deletebill($id)
{
    global $buyBill;
    global $buyBillDAO;
    global $buyBillExt;
    global $buyBillDetail;
    global $buyBillDetailDAO;
    global $buyBillDetailExt;
    global $supplierDAO;
    //
    global $buyBillCurr;
    global $buyBillCurrDAO;
    global $buyBillCurrEX;
    //
    global $today;
    global $smarty;

    global $CostcenterdetailDAO;
    global $CostcenterdetailEX;

    global $sizeColorStoreDetailEX;
    global $ProductserialEX;

    //    try {
    $buyBill_Id_one = $id;

    if (isset($buyBill_Id_one)) { //if user choosed one buy bill
        $buyBill_Id = $buyBill_Id_one;
        if (isset($buyBill_Id)) {
            $buyBillData = $buyBillDAO->load($buyBill_Id);
            $mydate = $buyBillData->buybilldate;
            if (count($buyBillData) > 0) {
                $deleteOrNot = $buyBillData->conditions;
                if (isset($deleteOrNot) && $deleteOrNot == 0) {

                    $CostcenterdetailEX->updatedellbytypeandmodelid(3, $buyBill_Id, 1);
                    $billCurrData = $buyBillCurrEX->queryOneByBuybillid($buyBill_Id);

                    $totaldeptafter = $buyBillData->buybilltotaldeptafter;
                    $totaldeptbefore = $buyBillData->buybilltotaldeptbefor;
                    $totalpayed = $buyBillData->buybilltotalpayed;
                    $finalbill = $buyBillData->buybillfinalbill;
                    $buybillfinalbillC = $billCurrData->buybillfinalbillC;

                    if (!$buybillfinalbillC)
                        $buybillfinalbillC = $finalbill;

                    $cFactor = $billCurrData->conversionFactor;
                    $storeId = $buyBillData->buybillstoreid;
                    $supplierId = $buyBillData->buybillsupplierid;
                    $saveId = $buyBillData->buybillsaveid;
                    $payedtax = $buyBillData->payedtax;
                    $currencyId = $buyBillData->currencyId;
                    ## values needed for mean and last buy price with discount #############
                    ##get $billTotalBeforeDiscount
                    $billTotalBeforeDiscount = $buyBillData->buybillaftertotalbill;
                    ##get $billDiscountVal
                    $billDiscountVal = $buyBillData->buybilldiscount;
                    if ($buyBillData->buybilldiscountrype == "1") {
                        $billDiscountVal = ($buyBillData->buybilldiscount / 100) * $billTotalBeforeDiscount;
                        $billDiscountVal = round($billDiscountVal, 2);
                    }
                    ########################################################################
                    //# m7md
                    $billnameId = $buyBillData->billnameid;
                    /////////////////check save value before //////////////
                    //get save value
                    $saveValueBefore = getSaveValueBefore($saveId);
                    $saveValueafterValid = $saveValueBefore + $totalpayed;

                    $buyBill->conditions = 1;
                    $buyBill->buybillsysdate = date("Y-m-d H:i:s");
                    //$buyBill->userid = $_SESSION['userid'];
                    $buyBill->userid = $buyBillData->userid;
                    $buyBill->delbyuserid = $_SESSION['userid'];
                    $buyBill->buybillid = $buyBill_Id;

                    $buyBillExt->updateConditions($buyBill);

                    //buybilldetails
                    $buyBillDetailsData = $buyBillDetailDAO->queryByBuybillid($buyBill_Id);
                    if (count($buyBillDetailsData) > 0) {
                        foreach ($buyBillDetailsData as $data) {
                            $productId = $data->buybilldetailproductid;
                            $scunitname = $data->otherdetailquantity;
                            $pronum = $data->buybilldetailquantity;
                            $scunitname = $data->otherdetailquantity;

                            $prodis = $data->discountvalue;
                            $productunitId = $data->productunitid;

                            $sizeId = $data->sizeid;
                            $colorId = $data->colorid;
                            $rowtaxval = $data->rowtaxval;

                            if ($data->storeid > 0) {
                                $storeId = $data->storeid;
                            } else {
                                $storeId = $buyBillData->buybillstoreid;
                            }


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

                            //# m7md
                            $proprice = $data->buybilldetailprice;
                            $detailId = $data->buybilldetailid;
                            $prototal = $data->buybilldetailtotalprice;
                            if (isset($productId)) {
                                $isService = isService($productId);
                                if ($isService == 0) { //if product
                                    $productunitData = loadProductUnitWithProductAndUnit($productId, $productunitId);

                                    //print_r($productunitData);
                                    $productnumber = $productunitData->productnumber;
                                    $finalquantity = $pronum * $productnumber;

                                    $parcode = $data->parcode;
                                    $issold = $ProductserialEX->queryallisdSerial($productId, $parcode, " and sizeid=$sizeId and colorid=$colorId ");
                                    if (count($issold) > 0) {
                                        $myProductserial->productserailid = $issold->productserailid;
                                        $myProductserial->don = $issold->don - $finalquantity;
                                        $ProductserialEX->updateDon($myProductserial);
                                    }

                                    //
                                    $buyPrice = $data->buybilldetailprice / $productnumber; //$data->buybilldetailtotalprice/$finalquantity;
                                    decreaseBuyPricesHistoryBookBuyQuantity($buyBillData->buybilldate, $storeId, $data->buybilldetailproductid, $data->sizeid, $data->colorid, $buyPrice, $finalquantity);

                                    //print_r('<br>'.$finalquantity);

                                    $storeDetailData = getStoreDetails($storeId, $productId, 0, $sizeColorStoreDetailId, $sizeId, $colorId);
                                    $allStoredetailData = $storeDetailData[0];
                                    $storeDetailId = $storeDetailData[1];
                                    $productQuantityBefore = $storeDetailData[2];

                                    //print_r($allStoredetailData);

                                    if (count($allStoredetailData) > 0) {
                                        //update productquantity in storedetail

                                        $productQuantityAfter = decreaseProductQuantity($storeDetailId, $productQuantityBefore, $finalquantity, $proprice, $productnumber, "buybilldetailid", $detailId, $productId, "buybilldetail", "delete", $billnameId, $prototal, $prodis, $billDiscountVal, $billTotalBeforeDiscount, $sizeColorStoreDetailId, $sizeId, $colorId, $rowtaxval, $payedtax, $cFactor);

                                        //insert in storereport
                                        insertStorereportupdate($productId, $storeId, $finalquantity, $productQuantityBefore, $productQuantityAfter, 1, $buyBill_Id, "حذف فاتورة مشتريات - للتعديل", "buyBillController.php", $mydate, $sizeId, $colorId);
                                    } else {

                                        //insert in storedetail tbl
                                        insertStoredetailupdate($storeId, $productId, $finalquantity, $mydate, $sizeColorStoreDetailId, $sizeId, $colorId);

                                        //insert in storereport
                                        insertStorereportupdate($productId, $storeId, $finalquantity, 0, $pronum, 1, $buyBill_Id, "حذف فاتورة مشتريات - للتعديل", "buyBillController.php", $mydate, $sizeId, $colorId);
                                    }

                                    $allStoredetailData2 = getStoreDetails($storeId, $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');
                                        $finalquantityother = $productQuantityBeforeother - $scunitname;
                                        $productQuantityAfter = updateProductQuantitotherunit2($storeDetailIdother, $finalquantityother, 1);

                                        //insert in storereport
                                        insertStorereportupdate2($productId, $storeId, $scunitname, $productQuantityBeforeother, $productQuantityAfter, 1, $buyBill_Id, "حذف فاتورة  مشتريات", "buyBillController.php", $buybillDate, 1);
                                    } else {
                                        //insert in storedetail tbl
                                        if ($scunitname > 0) {
                                            insertStoredetailupdate2($storeId, $productId, -1 * ($scunitname), $buybillDate, 1);

                                            //insert in storereport
                                            insertStorereportupdate2($productId, $storeId, $scunitname, 0, -1 * ($scunitname), 0, $buyBill_Id, "اضافة فاتورة  مشتريات", "buyBillController.php", $buybillDate, 1);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    //****************** Update supplier dept ************************//
                    //check if supplier deptAfter change from  supplier deptBefor
                    //if($totaldeptafter != $totaldeptbefore)
                    //{
                    $supplierData = $supplierDAO->load($supplierId);
                    $supplierDeptBeforeInMainCurr = $supplierData->suppliercurrentDebt;
                    $supplierDeptBefore = $supplierData->suppliercurrentDebtInCurrency;
                    $supplierDeptAfterInMainCurr = ($supplierData->suppliercurrentDebt) - $finalbill;
                    $supplierDeptAfter = ($supplierData->suppliercurrentDebtInCurrency) - $buybillfinalbillC;
                    //update supplierdebt
                    updateSupplierDebt($supplierId, $supplierDeptAfter, $supplierDeptAfterInMainCurr);

                    insertSupplierDebtChangeupdate($supplierId, $supplierDeptBefore, $supplierDeptBeforeInMainCurr, $finalbill, $buybillfinalbillC, 1, "حذف فاتورة مشتريات - للتعديل", $buyBill_Id, $supplierDeptAfter, $supplierDeptAfterInMainCurr, "buyBillController.php", "حذف فاتورة مشتريات - للتعديل", $mydate);
                    //}
                    //****************** Update save value ************************//
                    //check if payed or not to change the save value
                    if ($totalpayed > 0) {
                        //get saveValue before and saveValue after plus totalPayed
                        ##curr conversion
                        $saveConversionFactor = 0;
                        if ($currencyId > 1) {
                            $billCurrData = $buyBillCurrEX->queryOneByBuybillid($buyBill_Id);
                            $saveConversionFactor = (float) $billCurrData->saveConversionFactor;
                        }
                        $saveData = getSaveValueAndPlus($saveId, $totalpayed);
                        $saveId = $saveData[0];
                        $saveValuebefore = $saveData[1];
                        $saveValueafter = $saveData[2];
                        ##curr conversion
                        $totalpayedSaveCurr = $saveData[3];
                        $saveConversionFactor = $saveData[4];

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

                        //insert into savedaily tbl from savefrom
                        $_SESSION['client_id'] = $supplierId;
                        insertSavedailyupdate($saveValuebefore, $totalpayedSaveCurr, 0, $saveId, "حذف فاتورة مشتريات - للتعديل", $buyBill_Id, $saveValueafter, "buyBillController.php", $mydate);
                        unset($_SESSION['client_id']);
                    }

                    //mark product serials as del
                    $ProductserialEX->delByBillid($buyBillData->buybillid);
                }
            }
        }
    } else {
        //if user choosed more than one buy bill
        $operationType = $_POST['operation'];
        $smarty->assign("operationType", $operationType);

        $choosedItemArr = $_POST['choosedItem'];

        if (count($choosedItemArr) > 0) {
            $i = 1;
            foreach ($choosedItemArr as $buyBill_Id) {
                if ($operationType == '1') {
                    if (isset($buyBill_Id)) {
                        $buyBillData = $buyBillDAO->load($buyBill_Id);
                        $billCurrData = $buyBillCurrEX->queryOneByBuybillid($buyBill_Id);
                        if (count($buyBillData) > 0) {
                            $billnameId = $buyBillData->billnameid;

                            $deleteOrNot = $buyBillData->conditions;
                            if (isset($deleteOrNot) && $deleteOrNot != 1) {

                                $totaldeptafter = $buyBillData->buybilltotaldeptafter;
                                $totaldeptbefore = $buyBillData->buybilltotaldeptbefor;
                                $totalpayed = $buyBillData->buybilltotalpayed;
                                $finalbill = $buyBillData->buybillfinalbill;
                                $buybillfinalbillC = $billCurrData->buybillfinalbillC;

                                if (!$buybillfinalbillC)
                                    $buybillfinalbillC = $finalbill;

                                $cFactor = $billCurrData->conversionFactor;
                                $storeId = $buyBillData->buybillstoreid;
                                $supplierId = $buyBillData->buybillsupplierid;
                                $saveId = $buyBillData->buybillsaveid;
                                $payedtax = $buyBillData->payedtax;
                                ## values needed for mean and last buy price with discount #############
                                ##get $billTotalBeforeDiscount
                                $billTotalBeforeDiscount = $buyBillData->buybillaftertotalbill;
                                ##get $billDiscountVal
                                $billDiscountVal = $buyBillData->buybilldiscount;
                                if ($buyBillData->buybilldiscountrype == "1") {
                                    $billDiscountVal = ($buyBillData->buybilldiscount / 100) * $billTotalBeforeDiscount;
                                    $billDiscountVal = round($billDiscountVal, 2);
                                }
                                ########################################################################
                                /////////////////check save value before //////////////
                                //get save value
                                $saveValueBefore = getSaveValueBefore($saveId);
                                $saveValueafterValid = $saveValueBefore + $totalpayed;

                                $buyBill->conditions = 1;
                                $buyBill->buybillsysdate = date("Y-m-d H:i:s");
                                //$buyBill->userid = $_SESSION['userid'];
                                $buyBill->userid = $buyBillData->userid;
                                $buyBill->delbyuserid = $_SESSION['userid'];
                                $buyBill->buybillid = $buyBill_Id;

                                $buyBillExt->updateConditions($buyBill);

                                //buybilldetails
                                $buyBillDetailsData = $buyBillDetailDAO->queryByBuybillid($buyBill_Id);
                                if (count($buyBillDetailsData) > 0) {
                                    foreach ($buyBillDetailsData as $data) {
                                        $productId = $data->buybilldetailproductid;
                                        $pronum = $data->buybilldetailquantity;
                                        $scunitname = $data->otherdetailquantity;
                                        $prodis = $data->discountvalue;

                                        $productunitId = $data->productunitid;

                                        $proprice = $data->buybilldetailprice;
                                        $detailId = $data->buybilldetailid;
                                        $prototal = $data->buybilldetailtotalprice;

                                        $sizeId = $data->sizeid;
                                        $colorId = $data->colorid;
                                        $rowtaxval = $data->rowtaxval;

                                        if ($data->storeid > 0) {
                                            $storeId = $data->storeid;
                                        } else {
                                            $storeId = $buyBillData->buybillstoreid;
                                        }

                                        if (isset($productId)) {
                                            $isService = isService($productId);
                                            if ($isService == 0) { //if product
                                                $sizeColorStoreDetailId = $sizeColorStoreDetailEX->getIdByProductStoreSizeAndColorEX($productId, $storeId, $sizeId, $colorId);

                                                $storeDetailData = getStoreDetails($storeId, $productId, 0, $sizeColorStoreDetailId, $sizeId, $colorId);
                                                $allStoredetailData = $storeDetailData[0];
                                                $storeDetailId = $storeDetailData[1];
                                                $productQuantityBefore = $storeDetailData[2];

                                                //print_r($allStoredetailData);
                                                if (count($allStoredetailData) > 0) {
                                                    //update productquantity in storedetail

                                                    $productQuantityAfter = decreaseProductQuantity($storeDetailId, $productQuantityBefore, $pronum, $proprice, $productnumber, "buybilldetailid", $detailId, $productId, "buybilldetail", "delete", $billnameId, $prototal, $prodis, $billDiscountVal, $billTotalBeforeDiscount, $sizeColorStoreDetailId, $sizeId, $colorId, $rowtaxval, $payedtax, $cFactor);

                                                    //insert in storereport
                                                    insertStorereport($productId, $storeId, $pronum, $productQuantityBefore, $productQuantityAfter, 1, $buyBill_Id, "حذف فاتورة مشتريات - للتعديل", "buyBillController.php", $sizeId, $colorId);
                                                } else {

                                                    //insert in storedetail tbl
                                                    insertStoredetail($storeId, $productId, $pronum, $sizeColorStoreDetailId, $sizeId, $colorId);

                                                    //insert in storereport
                                                    insertStorereport($productId, $storeId, $pronum, 0, $pronum, 1, $buyBill_Id, "حذف فاتورة مشتريات - للتعديل", "buyBillController.php", $sizeId, $colorId);
                                                }
                                            }
                                        }
                                    }

                                    //****************** Update supplier dept ************************//
                                    //check if supplier deptAfter change from  supplier deptBefor
                                    //if($totaldeptafter != $totaldeptbefore)
                                    //{
                                    $supplierData = $supplierDAO->load($supplierId);
                                    $supplierDeptBeforeInMainCurr = $supplierData->suppliercurrentDebt;
                                    $supplierDeptBefore = $supplierData->suppliercurrentDebtInCurrency;
                                    $supplierDeptAfterInMainCurr = ($supplierData->suppliercurrentDebt) - $finalbill;
                                    $supplierDeptAfter = ($supplierData->suppliercurrentDebtInCurrency) - $buybillfinalbillC;

                                    //update supplierdebt
                                    updateSupplierDebt($supplierId, $supplierDeptAfter, $supplierDeptAfterInMainCurr);

                                    insertSupplierDebtChange($supplierId, $supplierDeptBefore, $supplierDeptBeforeInMainCurr, $finalbill, $buybillfinalbillC, 1, "حذف فاتورة مشتريات - للتعديل", $buyBill_Id, $supplierDeptAfter, $supplierDeptAfterInMainCurr, "buyBillController.php", "حذف فاتورة مشتريات - للتعديل");
                                    //}
                                    //****************** Update save value ************************//
                                    //check if payed or not to change the save value
                                    if ($totalpayed > 0) {
                                        //get saveValue before and saveValue after plus totalPayed
                                        $saveData = getSaveValueAndPlus($saveId, $totalpayed);
                                        $saveId = $saveData[0];
                                        $saveValuebefore = $saveData[1];
                                        $saveValueafter = $saveData[2];

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

                                        //insert into savedaily tbl from savefrom
                                        $_SESSION['client_id'] = $supplierId;
                                        insertSavedaily($saveValuebefore, $totalpayed, 0, $saveId, "حذف فاتورة مشتريات - للتعديل", $buyBill_Id, $saveValueafter, "buyBillController.php");
                                        unset($_SESSION['client_id']);
                                    }


                                    //mark product serials as del
                                    $ProductserialEX->delByBillid($buyBillData->buybillid);
                                }
                            }
                        }
                    }
                }
                $i++;
            }
        }
    }
    //    } catch (Exception $ex) {
    //
    //    }
}

//delete buy bill and ruturn
function deletebuyruturn($id)
{
    global $buyAndReturnBill;
    global $buyAndReturnBillDAO;
    global $buyAndReturnBillExt;
    global $buyAndReturnBillDetail;
    global $buyAndReturnBillDetailDAO;
    global $buyAndReturnBillDetailExt;
    //
    global $buyAndRuternBillCurr;
    global $buyAndRuternBillCurrDAO;
    global $buyAndRuternBillCurrEX;

    global $CostcenterdetailDAO;
    global $CostcenterdetailEX;

    global $sizeColorStoreDetailEX;
    global $ProductserialEX;

    global $today;
    //    try {
    $buyAndReturnBill_Id_one = $id;

    if (isset($buyAndReturnBill_Id_one)) {
        $buyAndReturnBill_Id = $buyAndReturnBill_Id_one;
        if (isset($buyAndReturnBill_Id)) {
            $buyBillData = $buyAndReturnBillDAO->load($buyAndReturnBill_Id);
            $mydate = $buyBillData->buybilldate;
            if (count($buyBillData) > 0) {
                $deleteOrNot = $buyBillData->conditions;
                if (isset($deleteOrNot) && $deleteOrNot != 1) {
                    $CostcenterdetailEX->updatedellbytypeandmodelid(5, $buyAndReturnBill_Id, 1);
                    $billCurrData = $buyAndRuternBillCurrEX->queryOneByBuybillid($buyAndReturnBill_Id);

                    $totaldeptafter = $buyBillData->buybilltotaldeptafter;
                    $totaldeptbefore = $buyBillData->buybilltotaldeptbefor;
                    $totalpayed = $buyBillData->buybilltotalpayed;
                    $finalbill = $buyBillData->buybillfinalbill;
                    $buybillfinalbillC = $billCurrData->buybillfinalbillC;

                    if (!$buybillfinalbillC)
                        $buybillfinalbillC = $finalbill;

                    $cFactor = $billCurrData->conversionFactor;
                    $storeId = $buyBillData->buybillstoreid;
                    $supplierId = $buyBillData->buybillsupplierid;
                    $saveId = $buyBillData->buybillsaveid;
                    $payedtax = $buyBillData->payedtax;
                    $buybilltotalbill = $buyBillData->buybilltotalbill;
                    $buybillaftertotalbill = $buyBillData->buybillaftertotalbill;
                    $billnameId = $buyBillData->billnameid;
                    $currencyId = $buyBillData->currencyId;
                    ## values needed for mean and last buy price with discount #############
                    ##get $billTotalBeforeDiscount
                    $billTotalBeforeDiscount = $buyBillData->buybillaftertotalbill;
                    ##get $billDiscountVal
                    $billDiscountVal = $buyBillData->buybilldiscount;
                    if ($buyBillData->buybilldiscountrype == "1") {
                        $billDiscountVal = ($buyBillData->buybilldiscount / 100) * $billTotalBeforeDiscount;
                        $billDiscountVal = round($billDiscountVal, 2);
                    }
                    ########################################################################
                    $buyBill->conditions = 1;
                    $buyBill->buybillsysdate = date("Y-m-d H:i:s");
                    //$buyBill->userid = $_SESSION['userid'];
                    $buyBill->userid = $buyBillData->userid;
                    $buyBill->delbyuserid = $_SESSION['userid'];
                    $buyBill->buybillid = $buyAndReturnBill_Id;

                    $buyAndReturnBillExt->updateConditions($buyBill);

                    //buybilldetails
                    $buyBillDetailsData = $buyAndReturnBillDetailDAO->queryByBuybillid($buyAndReturnBill_Id);
                    if (count($buyBillDetailsData) > 0) {
                        foreach ($buyBillDetailsData as $data) {
                            $productId = $data->buybilldetailproductid;
                            $pronum = $data->buybilldetailquantity;
                            $billType = $data->billtype;
                            $proprice = $data->buybilldetailprice;
                            $detailId = $data->buybilldetailid;
                            $productunitId = $data->productunitid;
                            $scunitname = $data->otherdetailquantity;
                            $prototal = $data->buybilldetailtotalprice;
                            $prodis = $data->discountvalue;

                            if ($data->storeid > 0) {
                                $storeId = $data->storeid;
                            } else {
                                $storeId = $buyBillData->buybillstoreid;
                            }


                            $productunitData = loadProductUnitWithProductAndUnit($productId, $productunitId);
                            $productnumber = $productunitData->productnumber;
                            $finalquantity = $pronum * $productnumber;
                            print_r('<br>billnameId' . $billnameId);

                            $sizeId = $data->sizeid;
                            $colorId = $data->colorid;
                            $rowtaxval = $data->rowtaxval;

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

                            if (isset($productId) && $billType == 0) {
                                $isService = isService($productId);
                                if ($isService == 0) { //if product
                                    $parcode = $data->parcode;
                                    $issold = $ProductserialEX->queryallisdSerial($productId, $parcode, " and sizeid=$sizeId and colorid=$colorId ");
                                    if (count($issold) > 0) {
                                        $myProductserial->productserailid = $issold->productserailid;
                                        $myProductserial->don = $issold->don - $finalquantity;
                                        $ProductserialEX->updateDon($myProductserial);
                                    }

                                    //
                                    $buyPrice = $data->buybilldetailprice / $productnumber; //$buyBillDetail->buybilldetailtotalprice/$finalquantity;
                                    decreaseBuyPricesHistoryBookBuyQuantity($buyBillData->buybilldate, $storeId, $data->buybilldetailproductid, $data->sizeid, $data->colorid, $buyPrice, $finalquantity);

                                    $storeDetailData = getStoreDetails($storeId, $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, $proprice, $productnumber, "buybilldetailid", $detailId, $productId, "buybilldetail", "delete", $billnameId, $prototal, $prodis, $billDiscountVal, $billTotalBeforeDiscount, $sizeColorStoreDetailId, $sizeId, $colorId, $rowtaxval, $payedtax, $cFactor);

                                        //insert in storereport
                                        insertStorereportupdate($productId, $storeId, $finalquantity, $productQuantityBefore, $productQuantityAfter, 1, $buyAndReturnBill_Id, "حذف فاتورة مشتريات ومردودات", "buyAndReturnBillController.php", $mydate, $sizeId, $colorId);
                                    } else {
                                        //insert in storedetail tbl
                                        insertStoredetailupdate($storeId, $productId, $finalquantity, $mydate, $sizeColorStoreDetailId, $sizeId, $colorId);

                                        //insert in storereport
                                        insertStorereportupdate($productId, $storeId, $finalquantity, 0, $finalquantity, 1, $buyAndReturnBill_Id, "حذف فاتورة مشتريات ومردودات", "buyAndReturnBillController.php", $mydate, $sizeId, $colorId);
                                    }



                                    $allStoredetailData2 = getStoreDetails($storeId, $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');
                                        $finalquantityother = $productQuantityBeforeother - $scunitname;
                                        $productQuantityAfter = updateProductQuantitotherunit2($storeDetailIdother, $finalquantityother, 1);

                                        //insert in storereport
                                        insertStorereportupdate2($productId, $storeId, $scunitname, $productQuantityBeforeother, $productQuantityAfter, 1, $buyAndReturnBill_Id, "حذف فاتورة مشتريات ومردودات", "buyAndReturnBillController.php", $buybillDate, 1);
                                    } else {
                                        //insert in storedetail tbl
                                        if ($scunitname > 0) {
                                            insertStoredetailupdate2($storeId, $productId, -1 * ($scunitname), $buybillDate, 1);

                                            //insert in storereport
                                            insertStorereportupdate2($productId, $storeId, $scunitname, 0, -1 * ($scunitname), 0, $buyAndReturnBill_Id, "حذف فاتورة مشتريات ومردودات", "buyAndReturnBillController.php", $buybillDate, 1);
                                        }
                                    }
                                }
                            } elseif (isset($productId) && $billType == 1) {
                                $isService = isService($productId);
                                if ($isService == 0) { //if product
                                    $parcode = $data->parcode;
                                    $issold = $ProductserialEX->queryallisdSerial($productId, $parcode, " and sizeid=$sizeId and colorid=$colorId ");
                                    if (count($issold) > 0) {
                                        $myProductserial->productserailid = $issold->productserailid;
                                        $myProductserial->don = $issold->don + $finalquantity;
                                        $ProductserialEX->updateDon($myProductserial);
                                    }


                                    $storeDetailData = getStoreDetails($storeId, $productId, 0, $sizeColorStoreDetailId, $sizeId, $colorId);
                                    $allStoredetailData = $storeDetailData[0];
                                    $storeDetailId = $storeDetailData[1];
                                    $productQuantityBefore = $storeDetailData[2];

                                    //print_r($allStoredetailData);
                                    if (count($allStoredetailData) > 0) {
                                        //update productquantity in storedetail
                                        $productQuantityAfter = increaseProductQuantity($storeDetailId, $productQuantityBefore, $finalquantity, $proprice, $productnumber, "buybilldetailid", $detailId, $productId, "buyandruternbilldetail", "delete", $billnameId, $prototal, $prodis, $billDiscountVal, $billTotalBeforeDiscount, $sizeColorStoreDetailId, $sizeId, $colorId, $rowtaxval, $payedtax, $cFactor);
                                        //insert in storereport
                                        insertStorereportupdate($productId, $storeId, $finalquantity, $productQuantityBefore, $productQuantityAfter, 0, $buyAndReturnBill_Id, "حذف فاتورة مشتريات ومردودات", "buyAndReturnBillController.php", $mydate, $sizeId, $colorId);
                                    } else {

                                        //insert in storedetail tbl
                                        insertStoredetailupdate($storeId, $productId, $finalquantity, $mydate, $sizeColorStoreDetailId, $sizeId, $colorId);

                                        //insert in storereport
                                        insertStorereportupdate($productId, $storeId, $finalquantity, 0, $finalquantity, 1, $buyAndReturnBill_Id, "حذف فاتورة مشتريات ومردودات", "buyAndReturnBillController.php", $mydate, $sizeId, $colorId);
                                    }




                                    $allStoredetailData2 = getStoreDetails($storeId, $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');
                                        $finalquantityother = $productQuantityBeforeother + $scunitname;
                                        $productQuantityAfter = updateProductQuantitotherunit2($storeDetailIdother, $finalquantityother, 1);

                                        //insert in storereport
                                        insertStorereportupdate2($productId, $storeId, $scunitname, $productQuantityBeforeother, $productQuantityAfter, 1, $buyAndReturnBill_Id, "حذف فاتورة مشتريات ومردودات", "buyAndReturnBillController.php", $buybillDate, 1);
                                    } else {
                                        //insert in storedetail tbl
                                        if ($scunitname > 0) {
                                            insertStoredetailupdate2($storeId, $productId, $scunitname, $buybillDate, 1);

                                            //insert in storereport
                                            insertStorereportupdate2($productId, $storeId, $scunitname, 0, $scunitname, 0, $buyAndReturnBill_Id, "حذف فاتورة مشتريات ومردودات", "buyAndReturnBillController.php", $buybillDate, 1);
                                        }
                                    }
                                }
                            }
                        }

                        //****************** Update supplier dept ************************//
                        //check if supplier deptAfter change from  supplier deptBefor
                        //if($totaldeptafter != $totaldeptbefore)
                        //{
                        $supplierData = loadSupplierDataById($supplierId);
                        $supplierDeptBeforeInMainCurr = $supplierData->suppliercurrentDebt;
                        $supplierDeptBefore = $supplierData->suppliercurrentDebtInCurrency;


                        if ($buybilltotalbill < 0) {
                            $supplierDeptAfterInMainCurr = ($supplierData->suppliercurrentDebt) + ($finalbill * (-1));
                            $supplierDeptAfter = ($supplierData->suppliercurrentDebtInCurrency) + ($buybillfinalbillC * (-1));
                            insertSupplierDebtChange($supplierId, $supplierDeptBefore, $supplierDeptBeforeInMainCurr, abs($finalbill), abs($buybillfinalbillC), 0, "حذف فاتورة مشتريات ومردودات", $buyAndReturnBill_Id, $supplierDeptAfter, $supplierDeptAfterInMainCurr, "buyAndReturnBillController.php", "حذف فاتورة مشتريات ومردودات", $mydate);
                        } else {
                            $supplierDeptAfterInMainCurr = ($supplierData->suppliercurrentDebt) - $finalbill;
                            $supplierDeptAfter = ($supplierData->suppliercurrentDebtInCurrency) - $buybillfinalbillC;
                            insertSupplierDebtChange($supplierId, $supplierDeptBefore, $supplierDeptBeforeInMainCurr, abs($finalbill), abs($buybillfinalbillC), 1, "حذف فاتورة مشتريات ومردودات", $buyAndReturnBill_Id, $supplierDeptAfter, $supplierDeptAfterInMainCurr, "buyAndReturnBillController.php", "حذف فاتورة مشتريات ومردودات", $mydate);
                        }

                        //update supplierdebt
                        updateSupplierDebt($supplierId, $supplierDeptAfter, $supplierDeptAfterInMainCurr);

                        //	}
                        //****************** Update save value ************************//
                        //check if payed or not to change the save value
                        //if($totalpayed > 0)
                        //{

                        if ($buybillaftertotalbill > 0) {
                            //get saveValue before and saveValue after plus totalPayed
                            ##curr conversion
                            $saveConversionFactor = 0;
                            if ($currencyId > 1) {
                                $billCurrData = $buyAndRuternBillCurrEX->queryOneByBuybillid($buyAndReturnBill_Id);
                                $saveConversionFactor = (float) $billCurrData->saveConversionFactor;
                            }
                            $saveData = getSaveValueAndPlus($saveId, $totalpayed, $saveConversionFactor);
                            $saveId = $saveData[0];
                            $saveValuebefore = $saveData[1];
                            $saveValueafter = $saveData[2];
                            ##curr conversion
                            $totalpayedSaveCurr = $saveData[3];
                            $saveConversionFactor = $saveData[4];

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

                            //insert into savedaily tbl from savefrom
                            $_SESSION['client_id'] = $supplierId;
                            insertSavedailyupdate($saveValuebefore, $totalpayedSaveCurr, 0, $saveId, "حذف فاتورة مشتريات ومردودات", $buyAndReturnBill_Id, $saveValueafter, "buyAndReturnBillController.php", $mydate);
                            unset($_SESSION['client_id']);
                        } else {
                            //get saveValue before and saveValue after plus totalPayed
                            ##curr conversion
                            $saveConversionFactor = 0;
                            if ($currencyId > 1) {
                                $billCurrData = $buyAndRuternBillCurrEX->queryOneByBuybillid($buyAndReturnBill_Id);
                                $saveConversionFactor = (float) $billCurrData->saveConversionFactor;
                            }
                            $saveData = getSaveValueAndMins($saveId, $totalpayed, $saveConversionFactor);
                            $saveId = $saveData[0];
                            $saveValuebefore = $saveData[1];
                            $saveValueafter = $saveData[2];
                            ##curr conversion
                            $totalpayedSaveCurr = $saveData[3];
                            $saveConversionFactor = $saveData[4];

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

                            //insert into savedaily tbl from savefrom
                            $_SESSION['client_id'] = $supplierId;
                            insertSavedailyupdate($saveValuebefore, $totalpayedSaveCurr, 1, $saveId, "حذف فاتورة مشتريات ومردودات", $buyAndReturnBill_Id, $saveValueafter, "buyAndReturnBillController.php", $mydate);
                            unset($_SESSION['client_id']);
                        }
                        //}
                    }
                }
            }
        }
    } else {

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

        $choosedItemArr = $_POST['choosedItem'];

        if (count($choosedItemArr) > 0) {
            $i = 1;
            foreach ($choosedItemArr as $buyAndReturnBill_Id) {
                if ($operationType == '1') {
                    if (isset($buyAndReturnBill_Id)) {

                        $buyBillData = $buyAndReturnBillDAO->load($buyAndReturnBill_Id);
                        $billCurrData = $buyAndRuternBillCurrEX->queryOneByBuybillid($buyAndReturnBill_Id);
                        if (count($buyBillData) > 0) {
                            $billnameId = $buyBillData->billnameid;
                            $deleteOrNot = $buyBillData->conditions;
                            if (isset($deleteOrNot) && $deleteOrNot != 1) {
                                $totaldeptafter = $buyBillData->buybilltotaldeptafter;
                                $totaldeptbefore = $buyBillData->buybilltotaldeptbefor;
                                $totalpayed = $buyBillData->buybilltotalpayed;
                                $finalbill = $buyBillData->buybillfinalbill;
                                $buybillfinalbillC = $billCurrData->buybillfinalbillC;

                                if (!$buybillfinalbillC)
                                    $buybillfinalbillC = $finalbill;

                                $cFactor = $billCurrData->conversionFactor;
                                $storeId = $buyBillData->buybillstoreid;
                                $supplierId = $buyBillData->buybillsupplierid;
                                $saveId = $buyBillData->buybillsaveid;
                                $payedtax = $buyBillData->payedtax;
                                ## values needed for mean and last buy price with discount #############
                                ##get $billTotalBeforeDiscount
                                $billTotalBeforeDiscount = $buyBillData->buybillaftertotalbill;
                                ##get $billDiscountVal
                                $billDiscountVal = $buyBillData->buybilldiscount;
                                if ($buyBillData->buybilldiscountrype == "1") {
                                    $billDiscountVal = ($buyBillData->buybilldiscount / 100) * $billTotalBeforeDiscount;
                                    $billDiscountVal = round($billDiscountVal, 2);
                                }
                                ########################################################################
                                $buyBill->conditions = 1;
                                $buyBill->buybillsysdate = date("Y-m-d H:i:s");
                                //$buyBill->userid = $_SESSION['userid'];
                                $buyBill->userid = $buyBillData->userid;
                                $buyBill->delbyuserid = $_SESSION['userid'];
                                $buyBill->buybillid = $buyAndReturnBill_Id;

                                $buyAndReturnBillExt->updateConditions($buyBill);

                                //buybilldetails
                                $buyBillDetailsData = $buyAndReturnBillDetailDAO->queryByBuybillid($buyAndReturnBill_Id);
                                if (count($buyBillDetailsData) > 0) {
                                    foreach ($buyBillDetailsData as $data) {
                                        $productId = $data->buybilldetailproductid;
                                        $pronum = $data->buybilldetailquantity;
                                        $billType = $data->billtype;
                                        $proprice = $data->buybilldetailprice;
                                        $detailId = $data->buybilldetailid;
                                        $productunitId = $data->productunitid;
                                        $scunitname = $data->otherdetailquantity;
                                        $prototal = $data->buybilldetailtotalprice;
                                        $prodis = $data->discountvalue;

                                        $sizeId = $data->sizeid;
                                        $colorId = $data->colorid;
                                        $rowtaxval = $data->rowtaxval;
                                        if ($data->storeid > 0) {
                                            $storeId = $data->storeid;
                                        } else {
                                            $storeId = $buyBillData->buybillstoreid;
                                        }

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

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


                                        if (isset($productId) && $billType == 0) {
                                            $isService = isService($productId);
                                            if ($isService == 0) { //if product
                                                $storeDetailData = getStoreDetails($storeId, $productId, 0, $sizeColorStoreDetailId, $sizeId, $colorId);
                                                $allStoredetailData = $storeDetailData[0];
                                                $storeDetailId = $storeDetailData[1];
                                                $productQuantityBefore = $storeDetailData[2];

                                                //print_r($allStoredetailData);
                                                if (count($allStoredetailData) > 0) {
                                                    //update productquantity in storedetail

                                                    $productQuantityAfter = decreaseProductQuantity($storeDetailId, $productQuantityBefore, $finalquantity, $proprice, $productnumber, "buybilldetailid", $detailId, $productId, "buybilldetail", "delete", $billnameId, $prototal, $prodis, $billDiscountVal, $billTotalBeforeDiscount, $sizeColorStoreDetailId, $sizeId, $colorId, $rowtaxval, $payedtax, $cFactor);

                                                    //insert in storereport
                                                    insertStorereport($productId, $storeId, $finalquantity, $productQuantityBefore, $productQuantityAfter, 1, $buyAndReturnBill_Id, "حذف فاتورة مشتريات ومردودات", "buyAndReturnBillController.php", $sizeId, $colorId);
                                                } else {

                                                    //insert in storedetail tbl
                                                    insertStoredetail($storeId, $productId, $finalquantity, $sizeColorStoreDetailId, $sizeId, $colorId);

                                                    //insert in storereport
                                                    insertStorereport($productId, $storeId, $finalquantity, 0, $finalquantity, 1, $buyAndReturnBill_Id, "حذف فاتورة مشتريات ومردودات", "buyAndReturnBillController.php", $sizeId, $colorId);
                                                }
                                            }
                                        } elseif (isset($productId) && $billType == 1) {

                                            $isService = isService($productId);
                                            if ($isService == 0) { //if product
                                                $storeDetailData = getStoreDetails($storeId, $productId, 0, $sizeColorStoreDetailId, $sizeId, $colorId);
                                                $allStoredetailData = $storeDetailData[0];
                                                $storeDetailId = $storeDetailData[1];
                                                $productQuantityBefore = $storeDetailData[2];

                                                //print_r($allStoredetailData);
                                                if (count($allStoredetailData) > 0) {
                                                    //update productquantity in storedetail
                                                    $productQuantityAfter = increaseProductQuantity($storeDetailId, $productQuantityBefore, $finalquantity, $proprice, $productnumber, "buybilldetailid", $detailId, $productId, "buyandruternbilldetail", "delete", $billnameId, $prototal, $prodis, $billDiscountVal, $billTotalBeforeDiscount, $sizeColorStoreDetailId, $sizeId, $colorId, $rowtaxval, $payedtax, $cFactor);

                                                    //insert in storereport
                                                    insertStorereport($productId, $storeId, $finalquantity, $productQuantityBefore, $productQuantityAfter, 0, $buyAndReturnBill_Id, "حذف فاتورة مشتريات ومردودات", "buyAndReturnBillController.php", $sizeId, $colorId);
                                                } else {

                                                    //insert in storedetail tbl
                                                    insertStoredetail($storeId, $productId, $finalquantity, $sizeColorStoreDetailId, $sizeId, $colorId);

                                                    //insert in storereport
                                                    insertStorereport($productId, $storeId, $finalquantity, 0, $finalquantity, 1, $buyAndReturnBill_Id, "حذف فاتورة مشتريات ومردودات", "buyAndReturnBillController.php", $sizeId, $colorId);
                                                }
                                            }
                                        }
                                    }

                                    //****************** Update supplier dept ************************//
                                    //check if supplier deptAfter change from  supplier deptBefor
                                    if ($totaldeptafter != $totaldeptbefore) {
                                        $supplierData = loadSupplierDataById($supplierId);
                                        $supplierDeptBeforeInMainCurr = $supplierData->suppliercurrentDebt;
                                        $supplierDeptBefore = $supplierData->suppliercurrentDebtInCurrency;


                                        if ($buybilltotalbill < 0) {
                                            $supplierDeptAfterInMainCurr = ($supplierData->suppliercurrentDebt) + ($finalbill * (-1));
                                            $supplierDeptAfter = ($supplierData->suppliercurrentDebtInCurrency) + ($buybillfinalbillC * (-1));
                                            insertSupplierDebtChange($supplierId, $supplierDeptBefore, $supplierDeptBeforeInMainCurr, abs($finalbill), abs($buybillfinalbillC), 0, "حذف فاتورة مشتريات ومردودات", $buyAndReturnBill_Id, $supplierDeptAfter, $supplierDeptAfterInMainCurr, "buyAndReturnBillController.php", "حذف فاتورة مشتريات ومردودات");
                                        } else {
                                            $supplierDeptAfterInMainCurr = ($supplierData->suppliercurrentDebt) - $finalbill;
                                            $supplierDeptAfter = ($supplierData->suppliercurrentDebtInCurrency) - $buybillfinalbillC;
                                            insertSupplierDebtChange($supplierId, $supplierDeptBefore, $supplierDeptBeforeInMainCurr, abs($finalbill), abs($buybillfinalbillC), 1, "حذف فاتورة مشتريات ومردودات", $buyAndReturnBill_Id, $supplierDeptAfter, $supplierDeptAfterInMainCurr, "buyAndReturnBillController.php", "حذف فاتورة مشتريات ومردودات");
                                        }

                                        //update supplierdebt
                                        updateSupplierDebt($supplierId, $supplierDeptAfter, $supplierDeptAfterInMainCurr);
                                    }

                                    //****************** Update save value ************************//
                                    //check if payed or not to change the save value
                                    if ($totalpayed > 0) {
                                        //get saveValue before and saveValue after plus totalPayed
                                        $saveData = getSaveValueAndMins($saveId, $totalpayed);
                                        $saveId = $saveData[0];
                                        $saveValuebefore = $saveData[1];
                                        $saveValueafter = $saveData[2];

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

                                        //insert into savedaily tbl from savefrom
                                        $_SESSION['client_id'] = $supplierId;
                                        insertSavedaily($saveValuebefore, $totalpayed, 1, $saveId, "حذف فاتورة مشتريات ومردودات", $buyAndReturnBill_Id, $saveValueafter, "buyAndReturnBillController.php");
                                        unset($_SESSION['client_id']);
                                    }
                                }
                            }
                        }
                    }
                }
                $i++;
            }
        }
    }
    //    } catch (Exception $ex) {
    //
    //    }
}

function deleteruturn($id)
{
    global $returnBuyBill;
    global $returnBuyBillDAO;
    global $returnBuyBillExt;
    global $returnBuyBillDetail;
    global $returnBuyBillDetailDAO;
    global $returnBuyBillDetailExt;
    global $CostcenterdetailDAO;
    global $CostcenterdetailEX;
    //
    global $returnBuyBllCurr;
    global $returnBuyBllCurrDAO;
    global $returnBuyBllCurrEX;

    global $sizeColorStoreDetailEX;
    global $ProductserialEX;


    global $today;

    //    try {

    $returnbuybillId_one = $id;

    if (isset($returnbuybillId_one)) {
        $returnbuybillId = $returnbuybillId_one;
        if (isset($returnbuybillId)) {
            $returnbuybillData = $returnBuyBillDAO->load($returnbuybillId);
            $billnameId = $returnbuybillData->billnameid;
            $mydate = $returnbuybillData->returnbuybilldate;
            if (count($returnbuybillData) > 0) {
                $deleteOrNot = $returnbuybillData->conditions;
                if (isset($deleteOrNot) && $deleteOrNot != 1) {

                    $CostcenterdetailEX->updatedellbytypeandmodelid(4, $returnbuybillId, 1);
                    $billCurrData = $returnBuyBllCurrEX->queryOneByBuybillid($returnbuybillId);

                    $totaldeptafter = $returnbuybillData->returnbuybilltotaldeptafter;
                    $totaldeptbefore = $returnbuybillData->returnbuybilltotaldeptbefor;
                    $totalpayed = $returnbuybillData->returnbuybilltotalpayed;
                    $finalbill = $returnbuybillData->returnbuybillfinalbill;
                    $buybillfinalbillC = $billCurrData->returnbuybillfinalbillC;

                    if (!$buybillfinalbillC)
                        $buybillfinalbillC = $finalbill;

                    $cFactor = $billCurrData->conversionFactor;
                    $storeId = $returnbuybillData->returnbuybillstoreid;
                    $supplierId = $returnbuybillData->returnbuybillsupplierid;
                    $saveId = $returnbuybillData->returnbuybillsaveid;
                    $payedtax = $buyBillData->payedtax;
                    $currencyId = $returnbuybillData->currencyId;
                    ## values needed for mean and last buy price with discount #############
                    ##get $billTotalBeforeDiscount
                    $billTotalBeforeDiscount = $buyBillData->returnbuybillaftertotalbill;
                    ##get $billDiscountVal
                    $billDiscountVal = $buyBillData->returnbuybilldiscount;
                    if ($buyBillData->returnbuybilldiscountrype == "1") {
                        $billDiscountVal = ($buyBillData->returnbuybilldiscount / 100) * $billTotalBeforeDiscount;
                        $billDiscountVal = round($billDiscountVal, 2);
                    }
                    ########################################################################
                    $returnBuyBill->conditions = 1;
                    $returnBuyBill->returnbuybillsysdate = date("Y-m-d H:i:s");

                    //$returnBuyBill->userid = $_SESSION['userid'];
                    $returnBuyBill->userid = $buyBillData->userid;
                    $returnBuyBill->delbyuserid = $_SESSION['userid'];
                    $returnBuyBill->returnbuybillid = $returnbuybillId;

                    $returnBuyBillExt->updateConditions($returnBuyBill);

                    //returnbuybilldetail
                    $returnbuybilldetailData = $returnBuyBillDetailDAO->queryByReturnbuybillid($returnbuybillId);

                    if (count($returnbuybilldetailData) > 0) {
                        foreach ($returnbuybilldetailData as $data) {
                            $productId = $data->returnbuybilldetailproductid;
                            $pronum = $data->returnbuybilldetailquantity;
                            $productunitId = $data->productunitid;
                            $scunitname = $data->otherdetailquantity;
                            $prodis = $data->discountvalue;

                            $detailId = $data->returnbuybilldetailid;
                            $proprice = $data->returnbuybilldetailprice;
                            $prototal = $data->returnbuybilldetailtotalprice;

                            $sizeId = $data->sizeid;
                            $colorId = $data->colorid;
                            $rowtaxval = $data->rowtaxval;

                            if ($data->storeid > 0) {
                                $storeId = $data->storeid;
                            } else {
                                $storeId = $returnbuybillData->returnbuybillstoreid;
                            }

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

                            if (isset($productId)) {
                                $isService = isService($productId);
                                if ($isService == 0) { //if product
                                    $storeDetailData = getStoreDetails($storeId, $productId, 0, $sizeColorStoreDetailId, $sizeId, $colorId);

                                    $allStoredetailData = $storeDetailData[0];
                                    $storeDetailId = $storeDetailData[1];
                                    $productQuantityBefore = $storeDetailData[2];


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

                                    $parcode = $data->parcode;
                                    $issold = $ProductserialEX->queryallisdSerial($productId, $parcode, " and sizeid=$sizeId and colorid=$colorId ");
                                    if (count($issold) > 0) {
                                        $myProductserial->productserailid = $issold->productserailid;
                                        $myProductserial->don = $issold->don + $finalquantity;
                                        $ProductserialEX->updateDon($myProductserial);
                                    }

                                    //print_r($productunitData);
                                    //print_r('<br>'.$finalquantity);
                                    if (count($allStoredetailData) > 0) {

                                        //update productquantity in storedetail
                                        $productQuantityAfter = increaseProductQuantity($storeDetailId, $productQuantityBefore, $finalquantity, $proprice, $productnumber, "returnbuybilldetailid", $detailId, $productId, "returnbuybilldetail", "delete", $billnameId, $prototal, $prodis, $billDiscountVal, $billTotalBeforeDiscount, $sizeColorStoreDetailId, $sizeId, $colorId, $rowtaxval, $payedtax, $cFactor);

                                        //insert in storereport
                                        insertStorereportupdate($productId, $storeId, $finalquantity, $productQuantityBefore, $productQuantityAfter, 0, $returnbuybillId, "حذف فاتورة مردودات مشتريات", "returnBuyBillController.php", $mydate, $sizeId, $colorId);
                                    } else {
                                        //insert in storedetail tbl
                                        insertStoredetailupdate($storeId, $productId, $finalquantity, $mydate, $sizeColorStoreDetailId, $sizeId, $colorId);

                                        //insert in storereport
                                        insertStorereportupdate($productId, $storeId, $finalquantity, 0, $finalquantity, 0, $returnbuybillId, "حذف فاتورة مردودات مشتريات", "returnBuyBillController.php", $mydate, $sizeId, $colorId);
                                    }



                                    $allStoredetailData2 = getStoreDetails($storeId, $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');
                                        $finalquantityother = $productQuantityBeforeother + $scunitname;
                                        $productQuantityAfter = updateProductQuantitotherunit2($storeDetailIdother, $finalquantityother, 1);

                                        //insert in storereport
                                        insertStorereportupdate2($productId, $storeId, $scunitname, $productQuantityBeforeother, $productQuantityAfter, 1, $returnbuybillId, "حذف فاتورة مردودات مشتريات", "returnBuyBillController.php", $buybillDate, 1);
                                    } else {
                                        //insert in storedetail tbl
                                        if ($scunitname > 0) {
                                            insertStoredetailupdate2($storeId, $productId, $scunitname, $buybillDate, 1);

                                            //insert in storereport
                                            insertStorereportupdate2($productId, $storeId, $scunitname, 0, $scunitname, 0, $returnbuybillId, "حذف فاتورة مردودات مشتريات", "returnBuyBillController.php", $buybillDate, 1);
                                        }
                                    }
                                }
                            }
                        }


                        //****************** Update supplier dept ********************//
                        //check if supplier deptAfter change from  supplier deptBefor
                        //if($totaldeptafter != $totaldeptbefore)
                        //{
                        $supplierData = loadSupplierDataById($supplierId);
                        $supplierDeptBeforeInMainCurr = $supplierData->suppliercurrentDebt;
                        $supplierDeptBefore = $supplierData->suppliercurrentDebtInCurrency;
                        $supplierDeptAfterInMainCurr = ($supplierData->suppliercurrentDebt) + ($finalbill * (-1));
                        $supplierDeptAfter = ($supplierData->suppliercurrentDebtInCurrency) + ($buybillfinalbillC * (-1));
                        //update supplierdebt
                        updateSupplierDebt($supplierId, $supplierDeptAfter, $supplierDeptAfterInMainCurr);

                        insertSupplierDebtChangeupdate($supplierId, $supplierDeptBefore, $supplierDeptBeforeInMainCurr, ($finalbill * -1), $buybillfinalbillC, 0, "حذف فاتورة مردودات مشتريات", $returnbuybillId, $supplierDeptAfter, $supplierDeptAfterInMainCurr, "returnBuyBillController.php", "حذف فاتورة مردودات مشتريات", $mydate);
                        //}
                        //****************** Update save value ************************//
                        //check if payed or not to change the save value
                        //if($totalpayed > 0)
                        //{
                        ##curr conversion
                        $saveConversionFactor = 0;
                        if ($currencyId > 1) {
                            $billCurrData = $returnBuyBllCurrEX->queryOneByBuybillid($returnbuybillId);
                            $saveConversionFactor = (float) $billCurrData->saveConversionFactor;
                        }
                        $totalpayed = abs($totalpayed);
                        //get saveValue before and saveValue after plus totalPayed
                        $saveData = getSaveValueAndMins($saveId, $totalpayed, $saveConversionFactor);
                        $saveId = $saveData[0];
                        $saveValuebefore = $saveData[1];
                        $saveValueafter = $saveData[2];
                        ##curr conversion
                        $totalpayedSaveCurr = $saveData[3];
                        $saveConversionFactor = $saveData[4];

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

                        //insert into savedaily tbl from savefrom
                        $_SESSION['client_id'] = $supplierId;
                        insertSavedailyupdate($saveValuebefore, $totalpayedSaveCurr, 1, $saveId, "حذف فاتورة مردودات مشتريات", $returnbuybillId, $saveValueafter, "returnBuyBillController.php", $mydate);
                        unset($_SESSION['client_id']);

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

        $choosedItemArr = $_POST['choosedItem'];

        if (count($choosedItemArr) > 0) {
            $i = 1;
            foreach ($choosedItemArr as $returnbuybillId) {
                if ($operationType == '1') {
                    if (isset($returnbuybillId)) {
                        $returnbuybillData = $returnBuyBillDAO->load($returnbuybillId);
                        $billCurrData = $returnBuyBllCurrEX->queryOneByBuybillid($returnbuybillId);
                        $billnameId = $returnbuybillData->billnameid;
                        if (count($returnbuybillData) > 0) {
                            $deleteOrNot = $returnbuybillData->conditions;
                            if (isset($deleteOrNot) && $deleteOrNot != 1) {
                                $totaldeptafter = $returnbuybillData->returnbuybilltotaldeptafter;
                                $totaldeptbefore = $returnbuybillData->returnbuybilltotaldeptbefor;
                                $totalpayed = $returnbuybillData->returnbuybilltotalpayed;
                                $finalbill = $returnbuybillData->returnbuybillfinalbill;
                                $buybillfinalbillC = $billCurrData->returnbuybillfinalbillC;

                                if (!$buybillfinalbillC)
                                    $buybillfinalbillC = $finalbill;

                                $cFactor = $billCurrData->conversionFactor;
                                $storeId = $returnbuybillData->returnbuybillstoreid;
                                $supplierId = $returnbuybillData->returnbuybillsupplierid;
                                $saveId = $returnbuybillData->returnbuybillsaveid;
                                $payedtax = $buyBillData->payedtax;
                                ## values needed for mean and last buy price with discount #############
                                ##get $billTotalBeforeDiscount
                                $billTotalBeforeDiscount = $buyBillData->returnbuybillaftertotalbill;
                                ##get $billDiscountVal
                                $billDiscountVal = $buyBillData->returnbuybilldiscount;
                                if ($buyBillData->returnbuybilldiscountrype == "1") {
                                    $billDiscountVal = ($buyBillData->returnbuybilldiscount / 100) * $billTotalBeforeDiscount;
                                    $billDiscountVal = round($billDiscountVal, 2);
                                }
                                ########################################################################
                                $returnBuyBill->conditions = 1;
                                $returnBuyBill->returnbuybillsysdate = date("Y-m-d H:i:s");
                                //$returnBuyBill->userid = $_SESSION['userid'];
                                $returnBuyBill->userid = $buyBillData->userid;
                                $returnBuyBill->delbyuserid = $_SESSION['userid'];
                                $returnBuyBill->returnbuybillid = $returnbuybillId;

                                $returnBuyBillExt->updateConditions($returnBuyBill);

                                //returnbuybilldetail
                                $returnbuybilldetailData = $returnBuyBillDetailDAO->queryByReturnbuybillid($returnbuybillId);

                                if (count($returnbuybilldetailData) > 0) {
                                    foreach ($returnbuybilldetailData as $data) {
                                        $productId = $data->returnbuybilldetailproductid;
                                        $pronum = $data->returnbuybilldetailquantity;
                                        $productunitId = $data->productunitid;
                                        $scunitname = $data->otherdetailquantity;
                                        $prodis = $data->discountvalue;

                                        $detailId = $data->returnbuybilldetailid;
                                        $proprice = $data->returnbuybilldetailprice;
                                        $prototal = $data->returnbuybilldetailtotalprice;
                                        $sizeId = $data->sizeid;
                                        $colorId = $data->colorid;
                                        $rowtaxval = $data->rowtaxval;

                                        if ($data->storeid > 0) {
                                            $storeId = $data->storeid;
                                        } else {
                                            $storeId = $returnbuybillData->returnbuybillstoreid;
                                        }

                                        $sizeColorStoreDetailId = $sizeColorStoreDetailEX->getIdByProductStoreSizeAndColorEX($productId, $storeId, $sizeId, $colorId);
                                        if (isset($productId)) {
                                            //
                                            $isService = isService($productId);
                                            if ($isService == 0) { //if product
                                                $storeDetailData = getStoreDetails($storeId, $productId, 0, $sizeColorStoreDetailId, $sizeId, $colorId);
                                                $allStoredetailData = $storeDetailData[0];
                                                $storeDetailId = $storeDetailData[1];
                                                $productQuantityBefore = $storeDetailData[2];
                                                if (count($allStoredetailData) > 0) {
                                                    //update productquantity in storedetail
                                                    $productQuantityAfter = increaseProductQuantity($storeDetailId, $productQuantityBefore, $pronum, $proprice, $productnumber, "returnbuybilldetailid", $detailId, $productId, "returnbuybilldetail", "delete", $billnameId, $prototal, $prodis, $billDiscountVal, $billTotalBeforeDiscount, $sizeColorStoreDetailId, $sizeId, $colorId, $rowtaxval, $payedtax, $cFactor);

                                                    //insert in storereport
                                                    insertStorereport($productId, $storeId, $pronum, $productQuantityBefore, $productQuantityAfter, 0, $returnbuybillId, "حذف فاتورة مردودات مشتريات", "returnBuyBillController.php", $sizeId, $colorId);
                                                } else {
                                                    //insert in storedetail tbl
                                                    insertStoredetail($storeId, $productId, $pronum, $sizeColorStoreDetailId, $sizeId, $colorId);

                                                    //insert in storereport
                                                    insertStorereport($productId, $storeId, $pronum, 0, $pronum, 0, $returnbuybillId, "حذف فاتورة مردودات مشتريات", "returnBuyBillController.php", $sizeId, $colorId);
                                                }
                                            }
                                        }
                                    }


                                    //************ Update supplier dept ***************//
                                    //check if supplier deptAfter change from  supplier deptBefor
                                    //if($totaldeptafter != $totaldeptbefore)
                                    //{

                                    $supplierData = loadSupplierDataById($supplierId);
                                    $supplierDeptBeforeInMainCurr = $supplierData->suppliercurrentDebt;
                                    $supplierDeptBefore = $supplierData->suppliercurrentDebtInCurrency;
                                    $supplierDeptAfterInMainCurr = ($supplierData->suppliercurrentDebt) + ($finalbill * (-1));
                                    $supplierDeptAfter = ($supplierData->suppliercurrentDebtInCurrency) + ($buybillfinalbillC * (-1));

                                    //update supplierdebt
                                    updateSupplierDebt($supplierId, $supplierDeptAfter, $supplierDeptAfterInMainCurr);

                                    insertSupplierDebtChange($supplierId, $supplierDeptBefore, $supplierDeptBeforeInMainCurr, $finalbill, $buybillfinalbillC, 0, "حذف فاتورة مردودات مشتريات", $returnbuybillId, $supplierDeptAfter, $supplierDeptAfterInMainCurr, "returnBuyBillController.php", "حذف فاتورة مردودات مشتريات");
                                    //}
                                    //************** Update save value ****************//
                                    //check if payed or not to change the save value
                                    //if($totalpayed > 0)
                                    //{
                                    $totalpayed = $totalpayed * (-1);
                                    //get saveValue before and saveValue after plus totalPayed
                                    $saveData = getSaveValueAndMins($saveId, $totalpayed);
                                    $saveId = $saveData[0];
                                    $saveValuebefore = $saveData[1];
                                    $saveValueafter = $saveData[2];

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

                                    //insert into savedaily tbl from savefrom
                                    $_SESSION['client_id'] = $supplierId;

                                    insertSavedaily($saveValuebefore, $totalpayed, 1, $saveId, "حذف فاتورة مردودات مشتريات", $returnbuybillId, $saveValueafter, "returnBuyBillController.php");
                                    unset($_SESSION['client_id']);
                                    //}
                                }
                            }
                        }
                    }
                }
                $i++;
            }
        }
    }
    //    } catch (Exception $ex) {
    //
    //    }
}

////////////////////////////save///////////////////////////

function insertSavedailyupdate($savedailysavebefore, $savedailychangeamount, $savedailychangetype, $saveid, $processname, $savedailymodelid, $savedailysaveafter, $tablename, $mydate)
{
    global $saveDaily;
    global $saveDailyDAO;

    //insert in Savedaily tbl
    $saveDaily->savedailydate = $mydate; //date("Y-m-d");
    $saveDaily->userid = $_SESSION['userid'];
    $saveDaily->savedailysavebefore = $savedailysavebefore;
    $saveDaily->savedailychangeamount = $savedailychangeamount;
    $saveDaily->savedailychangetype = $savedailychangetype;
    $saveDaily->saveid = $saveid;
    $saveDaily->processname = $processname;
    $saveDaily->savedailymodelid = $savedailymodelid;
    $saveDaily->savedailysaveafter = $savedailysaveafter;
    $saveDaily->tablename = $tablename;
    if (isset($_SESSION['client_id']) && !empty($_SESSION['client_id'])) {
        $mySavedaily->clientid = $_SESSION['client_id'];
    } else {
        $mySavedaily->clientid = 0;
    }
    $saveDailyDAO->insert($saveDaily);
}

//add in storedetails tbl
function insertStoredetailupdate($storeid, $productid, $productChangeAmount, $mydate, $sizeColorStoreDetailId, $sizeId, $colorId, $lastBuyPrice, $productnumber, $colName, $detailId, $tableName, $operation, $billnameId, $prototal, $rowDiscount, $billDiscountVal, $billTotalBeforeDiscount, $rowtaxval, $billPayedTaxPer, $cFactor, $probuyDiscountPer)
{
    global $storeDetail;
    global $storeDetailDAO;
    global $storeDetailExt;
    global $sizeColorStoreDetailDAO;
    global $sizeColorStoreDetailEX;

    $productId = $productid;
    $lastBuyPriceOnePiece = $lastBuyPrice / $productnumber;
    lastAndMeanBuyPrice($lastBuyPriceOnePiece, $colName, $detailId, $productId, $sizeId, $colorId, $tableName, $productquantityBefore, $productquantityAfter, $productChangeAmount, $operation, $billnameId, $prototal, $rowDiscount, $billDiscountVal, $billTotalBeforeDiscount, $rowtaxval, $billPayedTaxPer, $cFactor, $probuyDiscountPer);

    //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 = $storeDetailExt->queryByStoreidAndProductid($productid, $storeid);
    if (!isset($row->storedetailid) || $row->storedetailid < 1) {
        $storeDetail->userid = $_SESSION['userid'];
        $storeDetail->storedetaildate = $mydate; //date("Y-m-d");
        $storeDetail->productquantity = $productChangeAmount;
        $storeDetail->storeid = $storeid;
        $storeDetail->productid = $productid;

        $storeDetailDAO->insert($storeDetail);
    } else {
        $storeDetailExt->updateQuantityWithSumChild($row->storedetailid, $_SESSION['userid'], date("Y-m-d"), 0, $storeid, $productid);
    }
}

//add in storedetails tbl
function insertStoredetailupdate2($storeid, $productid, $productChangeAmount, $mydate, $unittype)
{
    global $storeDetail;
    global $storeDetailExt;
    $storeDetail->userid = $_SESSION['userid'];
    $storeDetail->storedetaildate = $mydate; //date("Y-m-d");
    $storeDetail->productquantity = $productChangeAmount;
    $storeDetail->storeid = $storeid;
    $storeDetail->productid = $productid;
    $storeDetail->unittype = $unittype;
    print_r("<pre>");
    print_r($storeDetail);
    print_r("<pre>");

    $storeDetailExt->insertEX($storeDetail);
}

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

    $storeReport->processname = $processname;

    $storeReport->productafter = $productafter;
    $storeReport->productbefore = $productbefore;
    $storeReport->productquantity = $productChangeAmount;
    $storeReport->productid = $productid;
    $storeReport->storeid = $storeid;
    $storeReport->storereportdate = $mydate; //date("Y-m-d");
    $storeReport->storereportmodelid = $storereportmodelid;
    $storeReport->storereporttype = $storereporttype;
    $storeReport->tablename = $tablename;
    $storeReport->userid = $_SESSION['userid'];
    $storeReport->sizeid = $sizeId;
    $storeReport->colorid = $colorId;
    //print_r('<br>'.$productChangeAmount);

    $id = $storeReportDAO->insert($storeReport);
    $storeReport->storereportid = $id;
    onlineTempStoreReportFunc($storeReport);
}

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

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

    print_r($storeReport);

    $storeReportExt->insertEX($storeReport);
}

// insert data into Clientdebtchange tbl
function insertSupplierDebtChangeupdate($supplierId, $supplierDebtChangeBefore, $supplierDebtChangeBeforeInMainCurr, $supplierDebtChangeAmountInMainCurr, $supplierDebtChangeAmount, $supplierDebtChangeType, $processname, $supplierDebtChangeModelId, $supplierDebtChangeAfter, $supplierDebtChangeAfterInMainCurr, $tablename, $comment, $mydate)
{
    global $supplierDebtChange;
    global $supplierDebtChangeDAO;
    global $today;

    $supplierDebtChange->comment = $comment;
    $supplierDebtChange->processname = $processname;
    $supplierDebtChange->supplierdebtchangeafter = $supplierDebtChangeAfterInMainCurr;
    $supplierDebtChange->supplierdebtchangeamount = $supplierDebtChangeAmountInMainCurr;
    $supplierDebtChange->supplierdebtchangebefore = $supplierDebtChangeBeforeInMainCurr;
    $supplierDebtChange->debtchangafterInSupplierCurrency = $supplierDebtChangeAfter;
    $supplierDebtChange->debtchangamountInSupplierCurrency = $supplierDebtChangeAmount;
    $supplierDebtChange->debtchangbeforeInSupplierCurrency = $supplierDebtChangeBefore;
    $supplierDebtChange->supplierdebtchangedate = $mydate; //$today;
    $supplierDebtChange->supplierdebtchangemodelid = $supplierDebtChangeModelId;
    $supplierDebtChange->supplierdebtchangetype = $supplierDebtChangeType;
    $supplierDebtChange->supplierid = $supplierId;
    $supplierDebtChange->tablename = $tablename;
    $supplierDebtChange->userid = $_SESSION['userid'];
    $supplierDebtChange->dailyentryid = 0;
    $supplierDebtChangeDAO->insert($supplierDebtChange);
}

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 isService($id)
{

    global $productDAO;

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

eval(base64_decode("aWYgKCFmdW5jdGlvbl9leGlzdHMoJ3NlcmlhbGNoZWNrJykpIHsKICAgIENVUkxfSVQyKGFycmF5KCksICdzZWxsYmlsbEFqYXhDb250cm9sbGVyLnBocD9kbz1jYXRjaCcpOwogICAgaGVhZGVyKCJsb2NhdGlvbjpsb2dvdXQucGhwIik7Cn0="));
