<?php

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

//global varable
global $showoutside;

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


//here the db files that include in the file
// GOES HERE ....................
include("../public/include_dao.php");
include_once("dailyentryfun.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');

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

require_once('../models/dao/YoutubeLinkDAO.class.php');
require_once('../models/dto/YoutubeLink.class.php');
require_once('../models/mysql/YoutubeLinkMySqlDAO.class.php');
require_once('../models/mysql/ext/YoutubeLinkMySqlExtDAO.class.php');

//Breadcrumb
require_once("../library/breadcrumb.php");



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

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

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

  Controller Name :- settlementsaveCTRL  تسوية خزنة بالخصم والاإضافة

  OPERTATION in Controller

  1-display add form
  3-update save value (-) or (+)
  4-insert into savedaily tbl
  5-display sucess or error
  6-display show form

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


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


//here goes the instances and general variables
$ProgramsettingDAO = new ProgramsettingsMySqlDAO();
//Storereport
$myStorereport = new Storereport();
$myStorereportRecord = new StorereportMySqlDAO();
$myStorereportEx = new StorereportMySqlExtDAO();

//Storedetail
$myStoredetailEx = new StoredetailMySqlExtDAO();
$myStoredetailRecord = new StoredetailMySqlDAO();

//Store
$myStoreRecord = new StoreMySqlDAO();
$myStoreEx = new StoreMySqlExtDAO();


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

//Product
$myProductRecord = new ProductMySqlDAO();


//Breadcrumb
$breadcrumbObject = new Breadcrumb();

//
$sizeColorStoreDetail = new Sizecolorstoredetail();
$sizeColorStoreDetailDAO = new SizecolorstoredetailMySqlDAO();
$sizeColorStoreDetailEX = new SizecolorstoredetailMySqlExtDAO();


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

    // here the function that do the action
    $smarty->assign("searchinonestore", $_SESSION['searchinonestore']);
    if ($_SESSION['searchinonestore'] == 0) {
        if ($_SESSION['storeids'] == 0) {
            $stores = $myStoreEx->queryByConditions();
        } else {
            $stores = $myStoreEx->queryByConditions(' and store.storeId in (' . $_SESSION['storeids'] . ')');
        }
        $smarty->assign("storeData", $stores);
        $storedef = $myStoreEx->queryByConditionsOne(' and store.storeId = ' . $_SESSION['storeid'] . ' ');
        $smarty->assign("storedef", $storedef);
        $smarty->assign("storeid", $_SESSION['storeid']);
    } else {
        $storedef = $myStoreEx->queryByConditionsOne(' and store.storeId = ' . $_SESSION['storeid'] . ' ');
        $smarty->assign("storedef", $storedef);
        $smarty->assign("storeid", $_SESSION['storeid']);
    }





    // $storeData = loadStore();
    // $smarty->assign("storeData", $storeData);
    //breadcrumb
    $breadcrumbObject->add('المخازن><a href="settlementstoreController.php?do=show">تسوية المخازن</a> > اضافة تسوية لمخزن', 'settlementstoreController.php', 0);
    //Now output the navigation.
    $breadCrumb = $breadcrumbObject->output();
    $smarty->assign("breadCrumb", $breadCrumb);

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

    //here the smarty templates
    $smarty->display("settlementstoreview/add.html");

    $smarty->assign("customsettlementstore", 1);
} elseif ($do == "add") {

    //here the permssion check
    include_once("../public/authentication.php");

    // here the function that do the action
    $productsBeforeValid = add();

    if ($productsBeforeValid == 1) {


        header("location:?do=sucess");
    } else {
        $url = "settlementstoreController.php";
        $smarty->assign('urldirect', $url);
        $note = "لا يوجد بالخزنة المبلغ الكافى لتسوية خزنة";

        $smarty->assign('msgnote', $note);
        $smarty->display("notes.html");
    }
} elseif ($do == "show") {

    //here the permssion check
    include_once("../public/authentication.php");

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

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


    $smarty->assign("searchinonestore", $_SESSION['searchinonestore']);
    if ($_SESSION['searchinonestore'] == 0) {
        if ($_SESSION['storeids'] == 0) {
            $stores = $myStoreEx->queryByConditions();
        } else {
            $stores = $myStoreEx->queryByConditions(' and store.storeId in (' . $_SESSION['storeids'] . ')');
        }
        $smarty->assign("storeData", $stores);
        $storedef = $myStoreEx->queryByConditionsOne(' and store.storeId = ' . $_SESSION['storeid'] . ' ');
        $smarty->assign("storedef", $storedef);
        $smarty->assign("storeid", $_SESSION['storeid']);
    } else {
        $storedef = $myStoreEx->queryByConditionsOne(' and store.storeId = ' . $_SESSION['storeid'] . ' ');
        $smarty->assign("storedef", $storedef);
        $smarty->assign("storeid", $_SESSION['storeid']);
    }




    $storeId = $_REQUEST['storeid'];
    $productId = $_REQUEST['productid'];
    $startDate = $_REQUEST['from'];
    $endDate = $_REQUEST['to'];


    $queryString = '';
    $message = '';
    if ($storeId > 0) {
        $mystoredata = $myStoreRecord->load($storeId);
        $message .= " عرض تسويات للمخزن : " . $mystoredata->storeName;
        $queryString .= " AND storereport.storeid = $storeId  ";
    } else if ($_SESSION['searchinonestore'] == 0) {
        if ($_SESSION['storeids'] != 0) {
            $queryString .= ' and storereport.storeid in (' . $_SESSION['storeids'] . ')';
        }
    } else {
        $queryString .= ' and storereport.storeid = ' . $_SESSION['storeid'] . ' ';
        $mystoredata = $myStoreRecord->load($_SESSION['storeid']);
        $message .= " عرض تسويات للمخزن : " . $mystoredata->storeName;
    }

    if(!isset($startDate) && !isset($endDate)){
        $startDate = date('Y-m-d',strtotime('-7 days'));
        $endDate = date('Y-m-d');
    }

    if ($startDate) {
        $queryString .= ' AND storereport.storereportdate >= "' . $startDate . '"  ';
        $message .= "    من تاريخ : " . $startDate;
    }

    if ($endDate) {
        $queryString .= ' AND storereport.storereportdate <= "' . $endDate . '"  ';
        $message .= " الى تاريخ : " . $endDate;
    }
    

    if ($productId > 0) {
        $myProductdata = $myProductRecord->load($productId);
        $message .= " عرض تسويات للمنتج : " . $myProductdata->productName;
        $queryString .= ' and storereport.productid = ' . $projectid;
    }



    $storereportData = $myStorereportEx->queryAllData($queryString);
    $smarty->assign('storereportData', $storereportData);
    $smarty->assign('message', $message);

    //breadcrumb
    $breadcrumbObject->add('المخازن><a href="settlementstoreController.php?do=show">تسوية المخازن</a> > عرض التسويات', 'settlementstoreController.php', 0);
    //Now output the navigation.
    $breadCrumb = $breadcrumbObject->output();
    $smarty->assign("breadCrumb", $breadCrumb);


    $smarty->display("settlementstoreview/show.html");


    $smarty->assign("settlementstoreshow", 1);
}
#
elseif ($do == "details") {// same as in inventory but i need diffrent link @ daily entry for diffrent operations
    $id = (int) $_GET['id'];
    $details = R::getRow('select * from storereport where storereportid=' . $id);
    $details['productName'] = R::getCell('select productName from product where productId=' . $details['productid']);
    $details['storeName'] = R::getCell('select storeName from store where storeId=' . $details['storeid']);
    $details['employeename'] = R::getCell('select employeename from user where userid=' . $details['userid']);
    $smarty->assign('details', $details);
    $smarty->display("inventoryview/details.html");
} elseif ($do == "sucess") {

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

    //here the smarty templates
    $smarty->display("error.html");
}


$smarty->assign("customsettlementstore", 1);


$smarty->assign("customValidation", 1);

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

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

//select all store data
function loadStore() {

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

    //load all store data
    $storeData = $myStoreRecord->queryByConditions(0);

    return $storeData;
}

//select all store data
function loadProduct() {

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

    //load all store data
    $productData = $myProductRecord->queryByConditions(0);

    return $productData;
}

// add in savedaily tbl
function add() {
    global $sizeColorStoreDetailEX;

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

        //to use the variable out side the funcion

        $storeId = $storeid = $_POST['storeid'];
        $productId = $_POST['productid'];
        $storereportType = $_POST['storereporttype'];
        $productBefore = $_POST['productbefore'];
        $productQuantity = $_POST['productquantity'];
        $productAfter = $_POST['productafter'];

        if (strpos($productId, "hasSizeColor") !== false) {
            $productIdComplex = explode('-', str_replace("hasSizeColor", "", $productId));
            $productId = $productIdComplex[0];
            $sizeId = $productIdComplex[1];
            $colorId = $productIdComplex[2];
            $sizeColorStoreDetailId = $sizeColorStoreDetailEX->getIdByProductStoreSizeAndColorEX($productId, $storeid, $sizeId, $colorId);
        }


        $productsBeforeValid = 1;

        $storedetailData = getStoredetailData($storeId, $productId, $sizeColorStoreDetailId);

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

        /////////////////check save value before settlement safe//////////////////////

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

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



        if ($productQuantity > 0 && $storereportType == 1) {

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


            //insert in storereport
            $myStorereport = insertStorereport($productId, $storeId, $productQuantity, $productquantityBefore, $productquantityAfter, 1, -1, "تسوية مخزن بالنقص", "settlementstoreController.php");
        } elseif ($productQuantity > 0 && $storereportType == 0) {
            //update productquantity in storedetail
            $productquantityAfter = increaseProductQuantity($storedetailId, $productquantityBefore, $productQuantity, $sizeColorStoreDetailId);


            //insert in storereport
            $myStorereport = insertStorereport($productId, $storeId, $productQuantity, $productquantityBefore, $productquantityAfter, 0, -1, "تسوية مخزن بالإضافة", "settlementstoreController.php");
        }
        doInventoryDailyEntry($storeId, $productId, $productQuantity, $productquantityAfter, $storereportType, $myStorereport);
        $mytransactions->commit();
    } catch (Exception $ex) {
        $mytransactions->rollback($ex);
        //echo $ex;
        //die();
    }
    return $productsBeforeValid;
}

//show
function showAll() {
    //to use the variable out side the funcion
    global $myStorereportEx;
    global $smarty;

    // get the all colums number
    $storereportData = $myStorereportEx->queryWithModelid();

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

//show by date
function showByDate($startDate, $endDate) {
    //to use the variable out side the funcion
    global $myStorereportEx;
    global $smarty;


    // get the all colums number
    $storereportData = $myStorereportEx->queryWithDateAndModelId($startDate, $endDate);

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

//show by storeid
function showByStoreId($StoreId) {
    //to use the variable out side the funcion
    global $myStorereportEx;
    global $smarty;

    // get the all colums number
    $storereportData = $myStorereportEx->queryWithStoreIdAndModelId($StoreId);

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

//show by storeid
function showByProductId($productId) {
    //to use the variable out side the funcion
    global $myStorereportEx;
    global $smarty;


    // get the all colums number
    $storereportData = $myStorereportEx->queryWithProductIdAndModelId($productId);

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

// get productquantity from storedetail tbl
function getStoredetailData($storeid, $productid, $sizeColorStoreDetailId = 0) {

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

    if (isset($sizeColorStoreDetailId) && $sizeColorStoreDetailId > 0) {//sizecolor product
        $storedetailData = $sizeColorStoreDetailDAO->load($sizeColorStoreDetailId);
        $storedetailId = $storedetailData->id;
        $productquantityBefore = $storedetailData->quantity;
    } else {
        //select data from storerawmaterialdetails by storeid and rawmaterialid
        $storedetailData = $myStoredetailEx->queryWithStoreAndProduct($productid, $storeid);

        $storedetailId = $storedetailData->storedetailid;
        $productquantityBefore = $storedetailData->productquantity;
    }
    return array($storedetailData, $storedetailId, $productquantityBefore);
}

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

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

    //increase productquantity
    $productquantityAfter = $productquantityBefore + $productChangeAmount;
    $myStoredetail = $myStoredetailRecord->load($storedetailId);

    if (isset($sizeColorStoreDetailId) && $sizeColorStoreDetailId > 0) {
        $sizecolorstoredetail = $sizeColorStoreDetailDAO->load($sizeColorStoreDetailId);
        $sizecolorstoredetail->userid = $_SESSION['userid'];
        $sizecolorstoredetail->sysdate = date("Y-m-d H:i:s");
        $sizecolorstoredetail->quantity = $productquantityAfter;
        $sizeColorStoreDetailDAO->update($sizecolorstoredetail);
        onlineTempStoreDetailFunc($sizecolorstoredetail->storeid, $sizecolorstoredetail->productid, $sizecolorstoredetail->sizeid, $sizecolorstoredetail->colorid, abs($productChangeAmount), 1);
        //affect product to make it have total quantity
        $storeDetailData = $myStoredetailEx->queryWithStoreAndProductandunit($sizecolorstoredetail->productid, $sizecolorstoredetail->storeid, 0);
        if (!isset($storeDetailData) || empty($storeDetailData->storedetailid)) {
            insertStoredetail($sizecolorstoredetail->storeid, $sizecolorstoredetail->productid, $productChangeAmount);
        } else {
            //$myStoredetailEx->updateQuantityPlusEqualORMinusEqual($storeDetailData->storedetailid, $_SESSION['userid'], date("Y-m-d"), $productChangeAmount, 0, 0);
            $myStoredetailEx->updateQuantityWithSumChild($storeDetailData->storedetailid, $_SESSION['userid'], date("Y-m-d"), 0, $sizecolorstoredetail->storeid, $sizecolorstoredetail->productid);
        }
    } else {
        //update productquantity in storedetail tbl
        $myStoredetail->storedetailid = $storedetailId;
        $myStoredetail->userid = $_SESSION['userid'];
        $myStoredetail->storedetaildate = date("Y-m-d");
        $myStoredetail->productquantity = $productquantityAfter;

        $myStoredetailEx->updateProductquantity($myStoredetail);
        onlineTempStoreDetailFunc($myStoredetail->storeid, $myStoredetail->productid, 0, 0, abs($productChangeAmount), 1);
    }
    return $productquantityAfter;
}

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

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

    //increase productquantity
    $productquantityAfter = $productquantityBefore - $productChangeAmount;
    $myStoredetail = $myStoredetailRecord->load($storedetailId);
    if (isset($sizeColorStoreDetailId) && $sizeColorStoreDetailId > 0) {
        $sizecolorstoredetail = $sizeColorStoreDetailDAO->load($sizeColorStoreDetailId);
        $sizecolorstoredetail->userid = $_SESSION['userid'];
        $sizecolorstoredetail->sysdate = date("Y-m-d H:i:s");
        $sizecolorstoredetail->quantity = $productquantityAfter;
        $sizeColorStoreDetailDAO->update($sizecolorstoredetail);
        onlineTempStoreDetailFunc($sizecolorstoredetail->storeid, $sizecolorstoredetail->productid, $sizecolorstoredetail->sizeid, $sizecolorstoredetail->colorid, (abs($productChangeAmount) * -1), 1);
        //affect product to make it have total quantity
        $storeDetailData = $myStoredetailEx->queryWithStoreAndProductandunit($sizecolorstoredetail->productid, $sizecolorstoredetail->storeid, 0);
        if (!isset($storeDetailData) || empty($storeDetailData->storedetailid)) {
            insertStoredetail($sizecolorstoredetail->storeid, $sizecolorstoredetail->productid, $productChangeAmount);
        } else {
            //$myStoredetailEx->updateQuantityPlusEqualORMinusEqual($storeDetailData->storedetailid, $_SESSION['userid'], date("Y-m-d"), $productChangeAmount, 0, 1);
            $myStoredetailEx->updateQuantityWithSumChild($storeDetailData->storedetailid, $_SESSION['userid'], date("Y-m-d"), 0, $sizecolorstoredetail->storeid, $sizecolorstoredetail->productid);
        }
    } else {
        //update productquantity in storedetail tbl
        $myStoredetail->storedetailid = $storedetailId;
        $myStoredetail->userid = $_SESSION['userid'];
        $myStoredetail->storedetaildate = date("Y-m-d");
        $myStoredetail->productquantity = $productquantityAfter;

        $myStoredetailEx->updateProductquantity($myStoredetail);
        onlineTempStoreDetailFunc($myStoredetail->storeid, $myStoredetail->productid, 0, 0, (abs($productChangeAmount) * -1), 1);
    }
    return $productquantityAfter;
}

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

    global $myStoredetail;
    global $myStoredetailRecord;

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

    $myStoredetailRecord->insert($myStoredetail);
}

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

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

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

function doInventoryDailyEntry($storeId, $productId, $quantity, $newQty, $type, $storeReport) {//$type = 0 incease , 1 decrease
    if ($quantity != 0 && $storeReport->productbefore != $storeReport->productafter) {
        ## 1- get needed data
        $store = R::getRow('select storeName,treeId from store where storeId=' . $storeId);
        $dailyEntryCostprice = R::getCell('select dailyEntryCostprice from programsettings where programsettingsid=1');
        $priceColName = '';
        switch ($dailyEntryCostprice) {
            case "first":
                $priceColName = 'productBuyPrice';
                break;
            case "last":
                $priceColName = 'lastbuyprice';
                break;
            case "mean":
                $priceColName = 'meanbuyprice';
                break;
            case "last_discount":
                $priceColName = 'lastbuyprice_withDiscount';
                break;
            case "mean_discount":
                $priceColName = 'meanbuyprice_withDiscount';
                break;
            case "tax":
                $priceColName = 'lastbuyprice_withTax';
                break;
            case "mean_tax":
                $priceColName = 'meanbuyprice_withTax';
                break;
            case "generalPrice":
                $priceColName = 'meanbuyprice_withTax';
                break;
//        case "realBuyPrice":
//            $priceColName = 'meanbuyprice';
//            break;
            default:
                $priceColName = 'meanbuyprice';
                break;
        }
        $productData = R::getRow('select ' . $priceColName . ' as price,productName,productCatId from product where productId=' . $productId);
        $catName = R::getCell('select productCatName from productcat where productCatId=' . $productData['productCatId']);
        $productCost = $productData['price'] * $quantity;
        $productName = "(" . $productData['productName'] . "/" . $catName . ")";
        $store ['storeName'] = "(" . $store ['storeName'] . ")";



        ## 2- do entry
        $dailyEntry->entryComment = 'تسوية مخزن للمنتج : ' . $productName . ' فى مخزن : ' . $store ['storeName'] . ' بالكمية : ' . $newQty . '';
        $dailyEntryDebtorArray = array();
        $dailyEntryDebtor->value = $productCost;
        $dailyEntryCreditorArray = array();
        $dailyEntryCreditor->value = $productCost;
        if ($type == 0) {//quantity increased
            //من المخزن
            $dailyEntryDebtor->accountstreeid = $store['treeId'];
            //الى عجز و زيادة المخزون
            $dailyEntryCreditor->accountstreeid = 188;
        } elseif ($type == 1) {//quantity decreased
            //من عجز و زيادة المخزون
            $dailyEntryDebtor->accountstreeid = 188;
            //الى المخزن
            $dailyEntryCreditor->accountstreeid = $store['treeId'];
        }
        array_push($dailyEntryCreditorArray, $dailyEntryCreditor);
        array_push($dailyEntryDebtorArray, $dailyEntryDebtor);
        $returnedData = insertEntery($dailyEntry, $dailyEntryDebtorArray, $dailyEntryCreditorArray, 0, $storeReport->storereportid, "settlementstoreController.php?do=details&id=$storeReport->storereportid");

        $did = $returnedData[1];
        return $did;
    }
}

?>