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

//Sellbill
require_once('../models/dao/SellbillDAO.class.php');
require_once('../models/dto/Sellbill.class.php');
require_once('../models/mysql/SellbillMySqlDAO.class.php');
require_once('../models/mysql/ext/SellbillMySqlExtDAO.class.php');

//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();
//Product
$myProductRecord = new ProductMySqlDAO();
//Productcat
$productCatDAO = new ProductcatMySqlDAO();
$productCatExt = new ProductcatMySqlExtDAO();

$ProgramsettingDAO = new ProgramsettingsMySqlDAO();

//Sellbilldetail
$sellbilldetail = new Sellbilldetail();
$sellbilldetailDAO = new SellbilldetailMySqlDAO();
$sellbilldetailEX = new SellbilldetailMySqlExtDAO();;


//check and use the condition that suite this action
if ($do == "show" || empty($do)) {
    //here the permission check
    include_once("../public/authentication.php");

    $storeData = loadStore();
    $smarty->assign("storeData", $storeData);
    $level = $_POST['level'];
    $level = $level - 1;
    $productatId  = $_REQUEST['productCatId'.$level.''];
    
    $storeId = $_REQUEST['storeId'];
    $startDate = $_REQUEST['from'];
    $endDate = $_REQUEST['to'];
    $order = $_REQUEST['order'];
    $bure = $_REQUEST['bure'];


    print_r('<pre>');
        print_r('$productatId = '.$productatId.'<br>'.'$endDate = '.$endDate.'<br>'.'$day_before = '.$day_before);
    print_r('<pre>');


    ##التصنيفات
    $catDataReport = getCategoryChilds();
    $smarty->assign("catDataReport", $catDataReport[1]);
   
   
    $productsData = $myProductRecord->queryByProductCatId($productatId);
    //# show function 
    //    show ($day_before , $productid , $storeId);
    show ($startDate , $endDate , $productsData , $storeId);
    
    //here the smarty templates
    $smarty->display("productdetailsreport/show.html");

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

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

/* ===============================
  function in this CONTROLLER
  ================================ */
class result_products {
    public $product_id ;
    public $product_name ;
    public $balance_before ;
    public $soldNo = 0;


}

function show ( $startDate , $endDate , $productsData , $storeId ){
    global $myStorereportEx;
    global $sellbilldetailEX;

    $existId = array();
    $productDataArr = array();
    $queryStringSellBill = '';

    foreach ($productsData as $value) {
        if (in_array($value->productId, $existId)) {
            $key = array_search($value->productId, $existId);
            $myproduct = $productDataArr[$key];
        } else {
            $key = -1;
            $myproduct = new result_products();
            $myproduct->product_id = $value->productId;
            $myproduct->product_name = $value->productName;
            
            //# رصيد قبل
           // $balanceBeforeValue =  balancebefore( $startDate , $value->productId , $storeId );
           print_r('productId = '. $value->productId.'<br>');
            $balanceBeforeValue = $myStorereportEx->getBalanceBefore( $startDate , $value->productId , $storeId );

            //# فاتورة المبيعات
            $queryStringSellBill .= ' AND  product.productId = '.$value->productId.'  AND sellbill.sellbilldate >= "' . $startDate . '"  AND sellbill.sellbilldate <="' . $endDate . '"';
            $sellBillData = $sellbilldetailEX->sellbillWithProducts($queryStringSellBill);

            
            $myproduct->balance_before = $balanceBeforeValue[0]->productafter;

            array_push($existId, $value->productId);
        }
        
        if ($key == -1) {
            array_push($productDataArr, $myproduct);
        }
    }
    
    
    //# فاتورة المبيعات
    foreach ($productsData as $value) {
        if (in_array($value->productId, $existId)) {
            $key = array_search($value->productId, $existId);
            $myproduct = $productDataArr[$key];
        } else {
            $key = -1;
            $myproduct = new result_products();
            $myproduct->product_id = $value->productId;
            $myproduct->product_name = $value->productName;
            
             
           
           
            #########get quantity بالقطعة
            $quantity = $value->sellbilldetailquantity;
            $productId = $value->sellbilldetailproductid;
            $productunitId = $value->productunitid;
            $productunitData = loadProductUnitWithProductAndUnit($productId, $productunitId);
            $productnumber = $productunitData->productnumber;
            $finalquantity = $quantity * $productnumber;
            ##############
            $myproduct->soldNo += $finalquantity; //العدد بالقطعة
            //$balanceBeforeValue =  balancebefore( $startDate , 1 , $storeId );
            print_r('<pre>enter');
            print_r($sellBillData);// last row in this day.
            print_r('<pre>');

            array_push($existId, $value->productId);
        }
        
        if ($key == -1) {
            array_push($productDataArr, $myproduct);
        }
    }
    
 
    
    
    
}

// # رصيد قبل
function balancebefore ( $startDate , $productid , $storeId ){
    global $myStorereportEx;

    //# get yesterday depending on start day  
   // $day_before = date( 'Y-m-d', strtotime( $startDate . ' -1 day' ) );
    
    //# رصيد قبل
    //$balanceBeforeData = $myStorereportEx->getBalanceBefore( $day_before , $productid , $storeid );
 
        return $balanceBeforeData[0]->productafter;// last row in this day.

}

//select all store data
function loadStore() {
    global $myStoreRecord;

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


##for show new way of multi parents
function getCategoryChilds($parentid = 0) {
    global $productCatExt;
    global $productCatDAO;
    //load cat
    $parentobject = $productCatDAO->load($parentid);
    $childsArray = $productCatExt->getChilds($parentid);

    return array($parentobject, $childsArray);
}

?>