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


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



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

//Productcat
require_once('../models/dao/ProductcatDAO.class.php');
require_once('../models/dto/Productcat.class.php');
require_once('../models/mysql/ProductcatMySqlDAO.class.php');
require_once('../models/mysql/ext/ProductcatMySqlExtDAO.class.php');
//Client
require_once('../models/dao/ClientDAO.class.php');
require_once('../models/dto/Client.class.php');
require_once('../models/mysql/ClientMySqlDAO.class.php');
require_once('../models/mysql/ext/ClientMySqlExtDAO.class.php');




//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 :- productsellsreportCtrl تقرير مبيعات منتج

  OPERTATION in Controller

  1-load save data
  2-display show form
  3-display details form for save
  4-display details for operation in savedaily

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


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


//here goes the instances and general variables
//Sellbilldetail
$mySellbilldetailEx = new SellbilldetailMySqlExtDAO();

//Returnsellbilldetail
$myReturnsellbilldetailEx = new ReturnsellbilldetailMySqlExtDAO();

//Sellandruternbilldetail
$mySellandruternbilldetailEx = new SellandruternbilldetailMySqlExtDAO();



//Product
$myProductRecord = new ProductMySqlDAO();
$myProductEx = new ProductMySqlExtDAO();

//Productcat
$myProductcatRecord = new ProductcatMySqlDAO();
$myProductcatEx = new ProductcatMySqlExtDAO();

//Client
$Client = new Client();
$ClientDAO = new ClientMySqlDAO();
$ClientEX = new ClientMySqlExtDAO();


//Breadcrumb
$breadcrumbObject = new Breadcrumb();



//check and use the condetion that suite this action

if (empty($do)) {

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

    //all clinet data
    $productCatData = loadProductCat();
    $smarty->assign("productCatData", $productCatData);

    //all clinet data
    $allclient = getallclient();
    $smarty->assign("allclient", $allclient);




    $myclientdata = $ClientDAO->load($clientid);
    $myproductdata = $myProductRecord->load($productId);




    //breadcrumb
    $breadcrumbObject->add('الادارة><a href="productsellsreportController.php">التقارير</a> >تقرير مبيعات منتج', 'productsellsreportController.php', 0);
    //Now output the navigation.
    $breadCrumb = $breadcrumbObject->output();
    $smarty->assign("breadCrumb", $breadCrumb);


    $smarty->assign("settlementstoreshow", 1);
    //here the smarty templates
    $smarty->display("customersproductsview/show.html");
} elseif ($do == "show") {


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

    //all clinet data
    $productCatData = loadProductCat();
    $smarty->assign("productCatData", $productCatData);

    //all clinet data
    $allclient = getallclient();
    $smarty->assign("allclient", $allclient);


    $startDate = $_REQUEST['from'];
    $endDate = $_REQUEST['to'];
    $productId = $_REQUEST['productId'];
    $clientid = $_REQUEST['clientid'];

    /* 	print_r($startDate."<br>");
      print_r($endDate."<br>");
      print_r($clientid."<br>");
      print_r($productId."<br>"); */
    if (isset($startDate) && isset($endDate) && isset($clientid) &&
            isset($productId) && $startDate != "" && $endDate != "" && $clientid != -1 && $productId != '-1') {
        //print_r("productId".$productId);
        $myclientdata = $ClientDAO->load($clientid);

        $myproductdata = $myProductRecord->load($productId);



        $mycatdata = $myProductcatRecord->load($productCatId);
        $message = "تقرير مشتريات  العميل :" . $myclientdata->clientname . " من المنتج :" . $myproductdata->productName . "
					 <br> من تاريخ:" . $startDate . " الى تاريخ" . $endDate;
        $smarty->assign('message', $message);



        $allmydata = loadProductByProductclientAndDate($productId, $startDate, $endDate, $clientid);

        //print_r($allmydata);
        $smarty->assign("allmydata", $allmydata);
    }



    //breadcrumb
    $breadcrumbObject->add('الادارة><a href="customersproductsController.php">التقارير</a> >تقرير مبيعات منتج وعميل', 'customersproductsController.php', 0);
    //Now output the navigation.
    $breadCrumb = $breadcrumbObject->output();
    $smarty->assign("breadCrumb", $breadCrumb);






    //here the smarty templates
    $smarty->display("customersproductsview/show.html");
    $smarty->assign("settlementstoreshow", 1);
} elseif ($do == "sucess") {

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

    //here the smarty templates


    $smarty->display("error.html");
}






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

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

//select all data from sellbill datail by product id
function showSellByProduct($productId) {
    //to use the variable out side the funcion
    global $mySellbilldetailEx;


    $sumQuantity = $mySellbilldetailEx->queryQuantityWithProduct($productId);

    return $sumQuantity;
}

// show the form by date
function showAditionalSellByProduct($productId) {
    //to use the variable out side the funcion
    global $mySellandruternbilldetailEx;


    $sumQuantity = $mySellandruternbilldetailEx->queryQuantityWithProductAndSellType($productId, 0);


    return $sumQuantity;
}

function showReturnSellByProduct($productId) {
    //to use the variable out side the funcion
    global $myReturnsellbilldetailEx;


    $sumQuantity = $myReturnsellbilldetailEx->queryQuantityWithProduct($productId);


    return $sumQuantity;
}

function showAditionalReturnSellByProduct($productId) {
    //to use the variable out side the funcion
    global $mySellandruternbilldetailEx;


    $sumQuantity = $mySellandruternbilldetailEx->queryQuantityWithProductAndSellType($productId, 1);


    return $sumQuantity;
}

//select data from product by productcatid
function loadProductByProductcatId($productcatId) {
    //to use the variable out side the funcion
    global $myProductEx;
    global $myProductRecord;
    global $smarty;

    //$productData=$myProductEx->queryWithProductcatIdAndConditions($productcatId);
    $productData = $myProductRecord->queryByProductCatId($productcatId);
    $smarty->assign("productData", $productData);


    $h = 1;
    foreach ($productData as $product) {
        $productId = $product->productId;
        $sellsQuantity = showSellByProduct($productId);
        $aditionalSellsQuantity = showAditionalSellByProduct($productId);

        $totalSellsQuantity = $sellsQuantity + $aditionalSellsQuantity;

        $returnSellQuantity = showReturnSellByProduct($productId);
        $aditionalReturnSellsQuantity = showAditionalReturnSellByProduct($productId);

        $totalReturnSellsQuantity = $returnSellQuantity + $aditionalReturnSellsQuantity;

        $remainQuantity = $totalSellsQuantity - $totalReturnSellsQuantity;

        $smarty->assign("totalSellsQuantity" . $h . "", $totalSellsQuantity);
        $smarty->assign("totalReturnSellsQuantity" . $h . "", $totalReturnSellsQuantity);
        $smarty->assign("remainQuantity" . $h . "", $remainQuantity);

        $h++;
    }
}

//select all data from sellbill datail by product id
function showSellByProductAndDate($productId, $startDate, $endDate) {
    //to use the variable out side the funcion
    global $mySellbilldetailEx;


    $sumQuantity = $mySellbilldetailEx->queryQuantityWithProductAndDate($productId, $startDate, $endDate);

    return $sumQuantity;
}

// show the form by date
function showAditionalSellByProductAndDate($productId, $startDate, $endDate) {
    //to use the variable out side the funcion
    global $mySellandruternbilldetailEx;


    $sumQuantity = $mySellandruternbilldetailEx->queryQuantityWithProductAndSellTypeAndDate($productId, 0, $startDate, $endDate);


    return $sumQuantity;
}

function showReturnSellByProductAndDate($productId, $startDate, $endDate) {
    //to use the variable out side the funcion
    global $myReturnsellbilldetailEx;


    $sumQuantity = $myReturnsellbilldetailEx->queryQuantityWithProductAndDate($productId, $startDate, $endDate);


    return $sumQuantity;
}

function showAditionalReturnSellByProductAndProduct($productId, $startDate, $endDate) {
    //to use the variable out side the funcion
    global $mySellandruternbilldetailEx;


    $sumQuantity = $mySellandruternbilldetailEx->queryQuantityWithProductAndSellTypeAndDate($productId, 1, $startDate, $endDate);


    return $sumQuantity;
}

//select data from product by productcatid
function loadProductByProductclientAndDate($productid, $startDate, $endDate, $clientid) {
    //to use the variable out side the funcion
    global $myProductEx;
    global $mySellbilldetailEx;
    global $smarty;

    //$productData=$myProductEx->queryWithProductcatIdAndConditions($productcatId);
    $productData = $mySellbilldetailEx->getallproductbyclientanddate($startDate, $endDate, $productid, $clientid);

    return $productData;
    //$smarty->assign("productData",$productData);
}

//function loadProductCat()
//	{
//		global $myProductcatEx;
//
//		$productCatParentData=$myProductcatEx->queryProductcatParentWithCondition();
//
//
//
//		foreach($productCatParentData as $productCatParent)
//		{
//			print("parentname".$productCatParent->productCatName."<br/>");
//			print("parentid".$productCatParent->productCatId."<br/>");
//
//			$productcatData=loadProductCatNameById($productCatParent->productCatId,$productCatParent->productCatName);
//
//			$productCatParent->productCatName=$productCatParent->productCatName.'/'.$productcatData[0];
//
//			$productCatParent->productCatName=$productcatData[1];
//
//			print("endddddddddddddddddddddddddddd<br/>");
//			print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><br/><br/>");
//		}
//
//		return $productCatParentData;
//	}
//
//
//
//
//
//
//	// select from Product tbl
//	function loadProductCatNameById($productCatId,$productcatName)
//	{
//		//to use the variable out side the funcion
//		global $myProductcatEx;
//
//		$productcatNamex=$productcatName;
//		$productCatIdx=$productCatId;
//
//		// select all data from producttbl
//		$productcatData=$myProductcatEx->queryWithParentAndCondition($productCatId);
//		print("<pre/>");
//		print_r($productcatData);
//		print("<pre/>");
//
//		if(count($productcatData) > 0)
//		{
//			print("yesssssssssssssssssss"."<br/>");
//			foreach($productcatData as $productcat)
//			{
//				print("oldname : ".$productcatNamex."<br/>");
//				print("oldid : ".$productCatIdx."<br/>");
//
//				$productcatNamex=$productcatNamex."/".$productcat->productCatName;
//				$productCatIdx=$productcat->productCatId;
//
//				print("newname : ".$productcatNamex."<br/>");
//				print("newId : ".$productCatIdx."<br/><br/>");
//				print("-------------------------------------<br/>");
//
//return loadProductCatNameById($productCatIdx,$productcatNamex,2);
//
//			}
//
//
//		}
//
//
//		return array($productCatIdx,$productcatNamex);
//	}
// select from productcatdata tbl
function loadProductCat() {
    //to use the variable out side the funcion
    global $myProductcatEx;


    $productcatData = $myProductcatEx->queryProductcatInProduct();

    foreach ($productcatData as $productcat) {
        $productcatName;

        $productcat->productCatName = loadProductCatNameById($productcat->productCatId, $productcatName, 1);
    }

    return $productcatData;
}

// select from Product tbl
function loadProductCatNameById($productCatId, $productcatName, $itr) {
    //to use the variable out side the funcion
    global $myProductcatRecord;

    $productcatNamex = $productcatName;
    // select all data from producttbl
    $productcatData = $myProductcatRecord->load($productCatId);

    if (count($productcatData) > 0) {

        if ($itr == 1) {
            $productcatNamex = $productcatData->productCatName;
        } elseif ($itr == 2) {
            $productcatNamex = $productcatData->productCatName . "/" . $productcatNamex;
        }



        if ($productcatData->productCatParent != 0) {
            return loadProductCatNameById($productcatData->productCatParent, $productcatNamex, 2);
        }
    }

    return $productcatNamex;
}

function getallclient() {
    global $Client;
    global $ClientDAO;
    global $ClientEX;


    $allclientdata = $ClientDAO->queryAll();
    return $allclientdata;
}

?>