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

//Storemovement
require_once('../models/dao/StoremovementDAO.class.php');
require_once('../models/dto/Storemovement.class.php');
require_once('../models/mysql/StoremovementMySqlDAO.class.php');
require_once('../models/mysql/ext/StoremovementMySqlExtDAO.class.php');

//Sellbilldetail
require_once('../models/dao/SellbilldetailDAO.class.php');
require_once('../models/dto/Sellbilldetail.class.php');
require_once('../models/mysql/SellbilldetailMySqlDAO.class.php');
require_once('../models/mysql/ext/SellbilldetailMySqlExtDAO.class.php');
//Returnsellbilldetail
require_once('../models/dao/ReturnsellbilldetailDAO.class.php');
require_once('../models/dto/Returnsellbilldetail.class.php');
require_once('../models/mysql/ReturnsellbilldetailMySqlDAO.class.php');
require_once('../models/mysql/ext/ReturnsellbilldetailMySqlExtDAO.class.php');
//Sellandruternbilldetail
require_once('../models/dao/SellandruternbilldetailDAO.class.php');
require_once('../models/dto/Sellandruternbilldetail.class.php');
require_once('../models/mysql/SellandruternbilldetailMySqlDAO.class.php');
require_once('../models/mysql/ext/SellandruternbilldetailMySqlExtDAO.class.php');

//Buybilldetail
require_once('../models/dao/BuybilldetailDAO.class.php');
require_once('../models/dto/Buybilldetail.class.php');
require_once('../models/mysql/BuybilldetailMySqlDAO.class.php');
require_once('../models/mysql/ext/BuybilldetailMySqlExtDAO.class.php');
//Returnbuybilldetail
require_once('../models/dao/ReturnbuybilldetailDAO.class.php');
require_once('../models/dto/Returnbuybilldetail.class.php');
require_once('../models/mysql/ReturnbuybilldetailMySqlDAO.class.php');
require_once('../models/mysql/ext/ReturnbuybilldetailMySqlExtDAO.class.php');
//Buyandruternbilldetail
require_once('../models/dao/BuyandruternbilldetailDAO.class.php');
require_once('../models/dto/Buyandruternbilldetail.class.php');
require_once('../models/mysql/BuyandruternbilldetailMySqlDAO.class.php');
require_once('../models/mysql/ext/BuyandruternbilldetailMySqlExtDAO.class.php');

//Productunit
require_once('../models/dao/ProductunitDAO.class.php');
require_once('../models/dto/Productunit.class.php');
require_once('../models/mysql/ProductunitMySqlDAO.class.php');
require_once('../models/mysql/ext/ProductunitMySqlExtDAO.class.php');

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

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

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

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

  Controller Name :- StorereportCTRL تقرير حركة المنتجات بالمخازن

  OPERTATION in Controller

  1-load storerawmaterials data
  2-load rawmaterials data
  3-display show form

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

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

//here goes the instances and general variables
//Storereport
$myStorereportEx = new StorereportMySqlExtDAO();
//Storedetail
$myStoredetailEx = new StoredetailMySqlExtDAO();
$myStoredetailRecord = new StoredetailMySqlDAO();
//Store
$myStoreRecord = new StoreMySqlDAO();
$myStoreEx = new StoreMySqlExtDAO();

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

$ProgramsettingDAO = new ProgramsettingsMySqlDAO();
//Storemovement
$storeMovement = new Storemovement();
$storeMovementDAO = new StoremovementMySqlDAO();
$storeMovementEX = new StoremovementMySqlExtDAO();

//Sellbilldetail
$sellbilldetail = new Sellbilldetail();
$sellbilldetailDAO = new SellbilldetailMySqlDAO();
$sellbilldetailEX = new SellbilldetailMySqlExtDAO();
//Returnsellbilldetail
$returnSellBillDetail = new Returnsellbilldetail();
$returnSellBillDetailDAO = new ReturnsellbilldetailMySqlDAO();
$returnSellBillDetailEX = new ReturnsellbilldetailMySqlExtDAO();
//Sellandruternbilldetail
$sellAndRuternBillDetail = new Sellandruternbilldetail();
$sellAndRuternBillDetailDAO = new SellandruternbilldetailMySqlDAO();
$sellAndRuternBillDetailEX = new SellandruternbilldetailMySqlExtDAO();

//Buybilldetail
$buyBillDetail = new Buybilldetail();
$buyBillDetailDAO = new BuybilldetailMySqlDAO();
$buyBillDetailEX = new BuybilldetailMySqlExtDAO();
//Returnbuybilldetail
$returnBuyBillDetail = new Returnbuybilldetail();
$returnBuyBillDetailDAO = new ReturnbuybilldetailMySqlDAO();
$returnBuyBillDetailEX = new ReturnbuybilldetailMySqlExtDAO();
//Buyandruternbilldetail
$buyAndRuternBillDetail = new Buyandruternbilldetail();
$buyAndRuternBillDetailDAO = new BuyandruternbilldetailMySqlDAO();
$buyAndRuternBillDetailEX = new BuyandruternbilldetailMySqlExtDAO();

//Productunit
$myProductunitEx = new ProductunitMySqlExtDAO();

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

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

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

$lastLevelCatIDS = array();
$catsIDS = "";
//check and use the condition that suite this action
if ($do == "show" || empty($do)) {
    //here the permission check
    include_once("../public/authentication.php");
    ##التصنيفات
    $catDataReport = getCategoryChilds();
    $smarty->assign("catDataReport", $catDataReport[1]);


    $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("stores", $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);

    $storeId = $_REQUEST['storeId'];
    $startDate = $_REQUEST['from'];
    $endDate = $_REQUEST['to'];
    $level = filter_input(INPUT_POST, 'level');
    $productCatId = filter_input(INPUT_POST, 'productCatId' . $level);
    if (empty($productCatId) || $productCatId == -1) {
        $productCatId = filter_input(INPUT_POST, 'productCatId' . ($level - 1));
    }
    $productId = filter_input(INPUT_POST, 'productId');
    $isOptic = filter_input(INPUT_POST, 'proIsOptic');
    if (!isset($isOptic) || empty($isOptic)) {
        $isOptic = 0;
    }
    if ($isOptic == 2 && $searchtype == 1) {
        $productCatId = $productId;
    }

    if (isset($startDate) && isset($endDate) && $startDate != "" && $endDate != "") {
        $message = '';
        if (isset($storeId) && $storeId != "-1") {
            $mystoredata = $myStoreRecord->load($storeId);
            $message .= "المخزن : " . $mystoredata->storeName . "</br>";
        }else if ($_SESSION['searchinonestore'] == 0) {
            $message .= "كل مخازن المستخدم : </br>";
        }else{
            $mystoredata = $myStoreRecord->load($_SESSION['storeid']);
            $message .= "المخزن : " . $mystoredata->storeName . "</br>";
            $storeId = $_SESSION['storeid'];
        }



        ##productId
        if (isset($productId) && !empty($productId) && $productId != -1) {
            $product = $ProductDAO->load($productId);
            $message .= "المنتج : " . $product->productName . "</br>";
            if ($isOptic == 2) {
                $productsOfCat = $ProductEX->queryByProductCatIdIn($productId);
                $productId = '0';
                foreach ($productsOfCat as $value) {
                    $productId .= ',' . $value->productId;
                }
            }
        } elseif (isset($productCatId) && !empty($productCatId) && $productCatId != -1) {
            $productCat = $productCatDAO->load($productCatId);
            $message .= "التصنيف : " . $productCat->productCatName . "</br>";
            ##get subcat of cat
            $catsIDS = '' . $productCatId;
            getAllSubCat($productCatId, 1); //mode = 1 get all sub cats
            $productsOfCat = $ProductEX->queryByProductCatIdIn($catsIDS);
            $productId = '0';
            foreach ($productsOfCat as $value) {
                $productId .= ',' . $value->productId;
            }
        }

        // if (isset($storeId) && $storeId != "-1") {
        //     $mystoredata = $myStoreRecord->load($storeId);
        //     $message .= "صافى تحويلات المخزن : " . $mystoredata->storeName . "</br>";
        // }



        $message .= "من تاريخ : " . $startDate . " إلى تاريخ : " . $endDate;
        $smarty->assign('message', $message);
        ##
        $choosenStoreIds = array();
        if (isset($storeId) && $storeId != "-1") {
            $allDataArr = getData($storeId, $startDate, $endDate, $productId);
            array_push($choosenStoreIds, $storeId);
            $smarty->assign('allDataArr_' . $storeId, $allDataArr);
        } else {
            foreach ($stores as $storeId) {
                $allDataArr = getData($storeId, $startDate, $endDate, $productId);
                $smarty->assign('allDataArr_' . $storeId, $allDataArr);
                array_push($choosenStoreIds, $storeId);
            }
        }
        $smarty->assign('choosenStoreIds', $choosenStoreIds);
    }

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

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

    //here the smarty templates
    $smarty->display("netStoreTransferView/show.html");
    $smarty->assign("settlementstoreshow", 1);
}
//here the global templates
$smarty->display("footer.html");

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

function getData($storeId, $startDate, $endDate, $productId) {
    global $myStoredetailEx;
    $queryString = '';
    if (isset($productId) && !empty($productId) && $productId != -1) {
        $queryString = ' and storedetail.productid in (' . $productId . ') ';
    }
    $allDataArr = $myStoredetailEx->queryByStoreidEX($storeId, $queryString);
    foreach ($allDataArr as $storeDetail) {
        //first duration from productmovmentController
        $storeDetail->productAddQuantity = getProductAddQuantity($storeDetail->productid, $storeId);
        $storeDetail->firstDurationQuantity = getFirstDurationQuantity($storeDetail->productid, $startDate, $storeId);
        $storeDetail->transferFrom = getTransferFromQuantity($storeId, $storeDetail->productid, $startDate, $endDate);
        $storeDetail->transferTo = getTransferToQuantity($storeId, $storeDetail->productid, $startDate, $endDate);
        list($storeDetail->sell, $storeDetail->retSell, $storeDetail->sellsersName) = getSellData($storeId, $storeDetail->productid, $startDate, $endDate);
        list($storeDetail->buy, $storeDetail->retBuy) = getBuyData($storeId, $storeDetail->productid, $startDate, $endDate);
        $storeDetail->storeDeficit = getStoreDeficit($storeId, $storeDetail->productid, $startDate, $endDate);
    }
    return $allDataArr;
}

function getStoreDeficit($storeId, $productid, $startDate, $endDate) {
    global $myStorereportEx;
    $netStoreDeficit = 0;
    $storeReportData = $myStorereportEx->getStoreDeficit($productid, $storeId, $startDate, $endDate);
    foreach ($storeReportData as $value) {
        if ($value->storereporttype == 0) {
            $netStoreDeficit += $value->productquantity;
        } else if ($value->storereporttype == 1) {
            $netStoreDeficit -= $value->productquantity;
        }
    }
    return $netStoreDeficit;
}

function getBuyData($storeId, $productId, $datefrom, $dateto) {
    global $buyBillDetailEX;
    global $returnBuyBillDetailEX;
    global $buyAndRuternBillDetailEX;

    $queryString1 = " where 1 "; //buybill
    $queryString1R = " where 1 "; //buybillreturn
    $queryString1SR = " where 1 "; //buybillandreturn
    ##date
    if (isset($datefrom) && !empty($datefrom)) {
        $queryString1 .= 'and  date(buybill.buybilldate)  >= "' . $datefrom . '" ';
        $queryString1R .= 'and  date(returnbuybill.returnbuybilldate)  >= "' . $datefrom . '" ';
        $queryString1SR .= 'and  date(buyandruternbill.buybilldate)  >= "' . $datefrom . '" ';
    }
    if (isset($dateto) && !empty($dateto)) {
        $queryString1 .= 'and  date(buybill.buybilldate)  <= "' . $dateto . '" ';
        $queryString1R .= 'and  date(returnbuybill.returnbuybilldate)  <= "' . $dateto . '" ';
        $queryString1SR .= 'and  date(buyandruternbill.buybilldate)  <= "' . $dateto . '" ';
    }
    ##storeId
    $theStore;
    if (isset($storeId) && !empty($storeId) && $storeId != -1) {
        $queryString1 .= 'and  buybill.buybillstoreid  = ' . $storeId . ' ';
        $queryString1R .= 'and  returnbuybill.returnbuybillstoreid  = ' . $storeId . ' ';
        $queryString1SR .= 'and  buyandruternbill.buybillstoreid  = ' . $storeId . ' ';
    }
    ##product
    $queryString1 .= 'and  buybilldetail.buybilldetailproductid	  = ' . $productId . ' ';
    $queryString1R .= 'and  returnbuybilldetail.returnbuybilldetailproductid  = ' . $productId . ' ';
    $queryString1SR .= 'and  buyandruternbilldetail.buybilldetailproductid  = ' . $productId . ' ';

    $buyQuantity = 0;
    $retBuyQuantity = 0;
    $buyBillData = $buyBillDetailEX->queryAllGeneral($queryString1);
    $buyBillDataReturn = $returnBuyBillDetailEX->queryAllGeneral($queryString1R);
    $buyBillDataBuyAndReturn = $buyAndRuternBillDetailEX->queryAllGeneral($queryString1SR);
    ##فاتورة المشتريات
    foreach ($buyBillData as $value) {
        #########get quantity بالقطعة
        $quantity = $value->buybilldetailquantity;
        $productId = $value->buybilldetailproductid;
        $productunitId = $value->productunitid;
        $productunitData = loadProductUnitWithProductAndUnit($productId, $productunitId);
        $productnumber = $productunitData->productnumber;
        $finalquantity = $quantity * $productnumber;
        ##############
        $buyQuantity += $finalquantity; //العدد بالقطعة
    }
    ##فاتورة مردود المشتريات
    foreach ($buyBillDataReturn as $value) {
        #########get quantity بالقطعة
        $quantity = $value->returnbuybilldetailquantity;
        $productId = $value->returnbuybilldetailproductid;
        $productunitId = $value->productunitid;
        $productunitData = loadProductUnitWithProductAndUnit($productId, $productunitId);
        $productnumber = $productunitData->productnumber;
        $finalquantity = $quantity * $productnumber;
        ##############
        $retBuyQuantity += $finalquantity; //العدد بالقطعة
    }
    ##فاتورة مشتريات و مردود
    foreach ($buyBillDataBuyAndReturn as $value) {
        #########get quantity بالقطعة
        $quantity = $value->buybilldetailquantity;
        $productId = $value->buybilldetailproductid;
        $productunitId = $value->productunitid;
        $productunitData = loadProductUnitWithProductAndUnit($productId, $productunitId);
        $productnumber = $productunitData->productnumber;
        $finalquantity = $quantity * $productnumber;
        ################
        if ($value->billtype == 0) {
            $buyQuantity += $finalquantity; //العدد بالقطعة
        } else {
            $retBuyQuantity += $finalquantity; //العدد بالقطعة
        }
    }

    return array($buyQuantity, $retBuyQuantity);
}

function getSellData($storeId, $productId, $datefrom, $dateto) {
    global $sellbilldetailEX;
    global $returnSellBillDetailEX;
    global $sellAndRuternBillDetailEX;
    global $userDAO;

    $queryString1 = " where 1 "; //sellbill
    $queryString1R = " where 1 "; //sellbillreturn
    $queryString1SR = " where 1 "; //sellbillandreturn
    ##date
    if (isset($datefrom) && !empty($datefrom)) {
        $queryString1 .= 'and  date(sellbill.sellbilldate)  >= "' . $datefrom . '" ';
        $queryString1R .= 'and  date(returnsellbill.returnsellbilldate)  >= "' . $datefrom . '" ';
        $queryString1SR .= 'and  date(sellbillandrutern.sellbilldate)  >= "' . $datefrom . '" ';
    }
    if (isset($dateto) && !empty($dateto)) {
        $queryString1 .= 'and  date(sellbill.sellbilldate)  <= "' . $dateto . '" ';
        $queryString1R .= 'and  date(returnsellbill.returnsellbilldate)  <= "' . $dateto . '" ';
        $queryString1SR .= 'and  date(sellbillandrutern.sellbilldate)  <= "' . $dateto . '" ';
    }
    ##storeId
    $theStore;
    if (isset($storeId) && !empty($storeId) && $storeId != -1) {
        $queryString1 .= 'and  sellbill.sellbillstoreid  = ' . $storeId . ' ';
        $queryString1R .= 'and  returnsellbill.returnsellbillstoreid  = ' . $storeId . ' ';
        $queryString1SR .= 'and  sellbillandrutern.sellbillstoreid  = ' . $storeId . ' ';
    }
    ##product
    $queryString1 .= 'and  sellbilldetail.sellbilldetailproductid  = ' . $productId . ' ';
    $queryString1R .= 'and  returnsellbilldetail.returnsellbilldetailproductid  = ' . $productId . ' ';
    $queryString1SR .= 'and  sellandruternbilldetail.sellbilldetailproductid  = ' . $productId . ' ';

    $soldQuantity = 0;
    $retSoldQuantity = 0;
    $sellerSales = array();
    $sellBillData = $sellbilldetailEX->queryAllGeneral($queryString1);
    $sellBillDataReturn = $returnSellBillDetailEX->queryAllGeneral($queryString1R);
    $sellBillDataSellAndReturn = $sellAndRuternBillDetailEX->queryAllGeneral($queryString1SR);
    ##فاتورة المبيعات
    foreach ($sellBillData as $value) {
        #########get quantity بالقطعة
        $quantity = $value->sellbilldetailquantity;
        $productId = $value->sellbilldetailproductid;
        $productunitId = $value->productunitid;
        $productunitData = loadProductUnitWithProductAndUnit($productId, $productunitId);
        $productnumber = $productunitData->productnumber;
        $finalquantity = $quantity * $productnumber;
        ##############
        $soldQuantity += $finalquantity; //العدد بالقطعة
        if (!isset($sellerSales[$value->userid])) {
            $sellerSales[$value->userid] = 0;
        }
        $sellerSales[$value->userid] += $finalquantity;
    }
    ##فاتورة مردود المبيعات
    foreach ($sellBillDataReturn as $value) {
        #########get quantity بالقطعة
        $quantity = $value->returnsellbilldetailquantity;
        $productId = $value->returnsellbilldetailproductid;
        $productunitId = $value->productunitid;
        $productunitData = loadProductUnitWithProductAndUnit($productId, $productunitId);
        $productnumber = $productunitData->productnumber;
        $finalquantity = $quantity * $productnumber;
        ##############
        $retSoldQuantity += $finalquantity; //العدد بالقطعة
        if (!isset($sellerSales[$value->userid])) {
            $sellerSales[$value->userid] = 0;
        }
        $sellerSales[$value->userid] -= $finalquantity;
    }
    ##فاتورة مبيعات و مردود
    foreach ($sellBillDataSellAndReturn as $value) {
        #########get quantity بالقطعة
        $quantity = $value->sellbilldetailquantity;
        $productId = $value->sellbilldetailproductid;
        $productunitId = $value->productunitid;
        $productunitData = loadProductUnitWithProductAndUnit($productId, $productunitId);
        $productnumber = $productunitData->productnumber;
        $finalquantity = $quantity * $productnumber;
        ################
        if (!isset($sellerSales[$value->userid])) {
            $sellerSales[$value->userid] = 0;
        }

        if ($value->selltype == 0) {
            $soldQuantity += $finalquantity; //العدد بالقطعة
            $sellerSales[$value->userid] += $finalquantity;
        } else {
            $retSoldQuantity += $finalquantity; //العدد بالقطعة
            $sellerSales[$value->userid] -= $finalquantity;
        }
    }

    //get user names
    $sellsersName = '';
    foreach ($sellerSales as $key => $value) {
        $userData = $userDAO->load($key);
        //$sellsersName[$userData->employeename] = $value;
        $sellsersName .= " $userData->employeename : $value <br/>";
    }
    $sellsersName = trim($sellsersName, "<br/>");
    return array($soldQuantity, $retSoldQuantity, $sellsersName);
}

function getTransferFromQuantity($storeId, $productid, $startDate, $endDate) {
    global $storeMovementEX;
    $storeMovementData = $storeMovementEX->queryByStoreidfromAndProductAndDate($storeId, $productid, $startDate, $endDate);
    $sumQuantity = 0;
    foreach ($storeMovementData as $value) {
        $sumQuantity += $value->transferproductamount;
    }
    return $sumQuantity;
}

function getTransferToQuantity($storeId, $productid, $startDate, $endDate) {
    global $storeMovementEX;
    $storeMovementData = $storeMovementEX->queryByStoreidtoAndProductAndDate($storeId, $productid, $startDate, $endDate);
    $sumQuantity = 0;
    foreach ($storeMovementData as $value) {
        $sumQuantity += $value->transferproductamount;
    }
    return $sumQuantity;
}

function getFirstDurationQuantity($productId, $startDate, $storeId) {
    global $myStorereportEx;

    $queryString = " AND productid = $productId ";
    $queryString .= ' AND storereportdate < "' . $startDate . '"';
    $queryString .= ' AND storeid = ' . $storeId . '';
    $storreportdata = $myStorereportEx->queryAlllastdealNew($queryString);
    $productafter = $storreportdata->productafter + 0;
    return $productafter;
}

function getProductAddQuantity($productId, $storeId) {
    global $myStorereportEx;

    ##quantity of add of product
    //get quantity without date
    $queryString = " AND productid = $productId ";
    $queryString .= ' AND storeid = ' . $storeId . '';
    $queryString .= ' AND tablename = "productController.php"';
    $storreportdata = $myStorereportEx->queryAlllastdealNew($queryString);
    $productafter = $storreportdata->productafter + 0;
    return $productafter;
}

function getAllSubCat($catid, $mode) {
    global $productCatExt;
    global $productCatDAO;
    global $catsIDS;
    global $lastLevelCatIDS;
    //mode = 1 get all sub cats
    //mode = 2 get last level cats

    $result = $productCatExt->queryByParentExt2($catid);
    if (count($result) > 0) {
        foreach ($result as $data) {
            if ($mode == 1) {
                $catsIDS .= "," . $data->productCatId;
                getAllSubCat($data->productCatId, $mode);
            } elseif ($mode == 2) {
                $childData = $productCatExt->queryByParentExt2($data->productCatId);
                if (count($childData) > 0) {
                    getAllSubCat($data->productCatId, $mode);
                } else {
                    if (!empty($data->productCatId)) {
                        array_push($lastLevelCatIDS, $data->productCatId);
                    }
                }
            }
        }
    } else {
//        if ($mode == 2) {
//            $childData = $productCatExt->queryByParentExt2($data->productCatId);
//            if (count($childData) > 0) {
//                getAllSubCat($data->productCatId, $mode);
//            } else {
//                if (!empty($data->productCatId)) {
//                    array_push($lastLevelCatIDS, $data->productCatId);
//                }
//            }
//        }
    }
}

?>