<?php

/* * *************** EDITED at 1/12/2013 --- Yasmeen ************* */

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

//Returnsellbill
require_once('../models/dao/ReturnsellbillDAO.class.php');
require_once('../models/dto/Returnsellbill.class.php');
require_once('../models/mysql/ReturnsellbillMySqlDAO.class.php');
require_once('../models/mysql/ext/ReturnsellbillMySqlExtDAO.class.php');


//Sellbillandrutern
require_once('../models/dao/SellbillandruternDAO.class.php');
require_once('../models/dto/Sellbillandrutern.class.php');
require_once('../models/mysql/SellbillandruternMySqlDAO.class.php');
require_once('../models/mysql/ext/SellbillandruternMySqlExtDAO.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');


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

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

//Buybill
require_once('../models/dao/BuybillDAO.class.php');
require_once('../models/dto/Buybill.class.php');
require_once('../models/mysql/BuybillMySqlDAO.class.php');
require_once('../models/mysql/ext/BuybillMySqlExtDAO.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');

//Buyandruternbill
require_once('../models/dao/BuyandruternbillDAO.class.php');
require_once('../models/dto/Buyandruternbill.class.php');
require_once('../models/mysql/BuyandruternbillMySqlDAO.class.php');
require_once('../models/mysql/ext/BuyandruternbillMySqlExtDAO.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');


//Returnbuybill
require_once('../models/dao/ReturnbuybillDAO.class.php');
require_once('../models/dto/Returnbuybill.class.php');
require_once('../models/mysql/ReturnbuybillMySqlDAO.class.php');
require_once('../models/mysql/ext/ReturnbuybillMySqlExtDAO.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');


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



//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 :- profitreportCTRL تقرير يومية الخزينة

  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
//Sellbill
$mySellbillEx = new SellbillMySqlExtDAO();
$mySellbillRecord = new SellbillMySqlDAO();

//Returnsellbill
$myReturnsellbillEx = new ReturnsellbillMySqlExtDAO();
$myReturnsellbillRecord = new ReturnsellbillMySqlDAO();

//Sellbillandrutern
$mySellbillandruternRecord = new SellbillandruternMySqlDAO();
$mySellbillandruternEx = new SellbillandruternMySqlExtDAO();

//Sellbilldetail
$mySellbilldetailEx = new SellbilldetailMySqlExtDAO();

//Returnsellbilldetail
$myReturnsellbilldetailEx = new ReturnsellbilldetailMySqlExtDAO();

//Sellandruternbilldetail
$mySellandruternbilldetailEx = new SellandruternbilldetailMySqlExtDAO();



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

//Productunit
$myProductunitEx = new ProductunitMySqlExtDAO();
$myProductunitRecord = new ProductunitMySqlDAO();


//Buybill
$myBuybillRecord = new BuybillMySqlDAO();

//Buybilldetail
$myBuybilldetailEx = new BuybilldetailMySqlExtDAO();

//Buyandruternbill
$myBuyandruternbillRecord = new BuyandruternbillMySqlDAO();

//Buyandruternbilldetail
$myBuyandruternbilldetailEx = new BuyandruternbilldetailMySqlExtDAO();

//Returnbuybill
$myReturnbuybillRecord = new ReturnbuybillMySqlDAO();

//Returnbuybilldetail
$myReturnbuybilldetailEx = new ReturnbuybilldetailMySqlExtDAO();

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


//check and use the condetion that suite this action

if ($do == "show" || empty($do)) {

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


    $productCatData = loadProductCat();
    $smarty->assign("productCatData", $productCatData);

    $productCatId = $_REQUEST['productCatId'];
    $startDate = $_REQUEST['from'];
    $endDate = $_REQUEST['to'];


    if (isset($startDate) && isset($endDate) && $startDate != "" && $endDate != "" && isset($productCatId) && $productCatId != "-1") {
        $productCatSearchData = loadProductCatById($productCatId);
        $smarty->assign('productCatSearchData', $productCatSearchData);

        getProductInBillByDateAndCatId($startDate, $endDate, $productCatId);

        $message = "تقرير ارباح صنف <h4>" . $productCatSearchData->productCatName . "</h4> التاريخ : من " . $startDate . " إلى " . $endDate;
        $smarty->assign("message", $message);
    }

    //here the smarty templates
    $smarty->display("profitproductcatview/show.html");
} 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
  ================================ */

function getProductInBillByDateAndCatId($startDate, $endDate, $productcatId) {
    global $mySellbilldetailEx;
    global $myProductRecord;
    global $myProductEx;

    global $smarty;

    $productIdData = $mySellbilldetailEx->queryFromAllTableWithDateAndConditionsAndCatId($startDate, $endDate, $productcatId);
//		print("<pre>");
//		print_r($productIdData);
//		print("</pre>");
    $smarty->assign("productIdData", $productIdData);

    $totalSellPrice = 0;
    $totalQuantityUnit = 0;

    $totalReturnSellPrice = 0;
    $totalreturnQuantityUnit = 0;

    $totalBuybillPrice = 0;
    $totalBuyQuantityUnit = 0;

    $totalReturnBuybillPrice = 0;
    $totalReturnBuyQuantityUnit = 0;

    $finalProductcatProfit = 0;

    $h = 1;
    foreach ($productIdData as $productIdDa) {
        $productId = $productIdDa->productid;


        $productData = $myProductRecord->load($productId);
        $smarty->assign("productData", $productData);

        //sellbill
        $sellPriceData = getTotalSellPriceByDateAndProductId($startDate, $endDate, $productId);

        $sellPricePulsData = getTotalAditionalSellPriceByDateAndProductId($startDate, $endDate, $productId);

        $sellPrice = $sellPriceData[0] + $sellPricePulsData[0];
        $quantityUnit = $sellPriceData[1] + $sellPricePulsData[1];




        $totalSellPrice = $totalSellPrice + $sellPrice;
        $totalQuantityUnit = $totalQuantityUnit + $quantityUnit;

        //return sellbill
        $returnSellPriceData = getTotalReturnSellPriceByDateAndProductId($startDate, $endDate, $productId);

        $returnSellPricePulsData = getTotalAditionalReturnSellPriceByDateAndProductId($startDate, $endDate, $productId);



        /* 	print_r("المردودات".$returnSellPriceData[0]);
          print_r("<br>سعر الاصلى المردودات".$returnSellPriceData[2]);
          //finalbuypric
          print_r("<br>");
          print_r("المبيعات فى المردودات".$returnSellPricePulsData[0]);

          print_r("<br>سعر الاصلى المردودات".$returnSellPricePulsData[2]);
          print_r("<br>");
          //print_r($sellPricePulsData);
          print_r("<br>");
         */
        $returnSellPrice = $returnSellPriceData[0] + $returnSellPricePulsData[0];
        $returnQuantityUnit = $returnSellPriceData[1] + $returnSellPricePulsData[1];

        $totalReturnSellPrice = $totalReturnSellPrice + $returnSellPrice;
        $totalreturnQuantityUnit = $totalreturnQuantityUnit + $returnQuantityUnit;

        //buybill
        $buybillPriceData = getTotalBuyPriceByDateAndProductId($startDate, $endDate, $productId);

        $buybillPricePulsData = getTotalAditionalBuyPriceByDateAndProductId($startDate, $endDate, $productId);

        $buybillPrice = $buybillPriceData[0] + $buybillPricePulsData[0];
        $buyQuantityUnit = $buybillPriceData[1] + $buybillPricePulsData[1];


        $totalBuybillPrice = $totalBuybillPrice + $buybillPrice;
        $totalBuyQuantityUnit = $totalBuyQuantityUnit + $buyQuantityUnit;

        //return buybill
        $returnBuybillPriceData = getTotalReturnBuyPriceByDateAndProductId($startDate, $endDate, $productId);

        $returnBuybillPricePulsData = getTotalAditionalBuyPriceByDateAndProductId($startDate, $endDate, $productId);

        $returnBuybillPrice = $returnBuybillPriceData[0] + $returnBuybillPricePulsData[0];
        $returnBuyQuantityUnit = $returnBuybillPriceData[1] + $returnBuybillPricePulsData[1];

        $totalReturnBuybillPrice = $totalReturnBuybillPrice + $returnBuybillPrice;
        $totalReturnBuyQuantityUnit = $totalReturnBuyQuantityUnit + $returnBuyQuantityUnit;


        $yy = $sellPrice - $returnSellPrice;


        print_r($profitForOneProduct);
        $quantityForOneProduct = $quantityUnit - $returnQuantityUnit;

        //سعر الوحده
        $xx = ($buybillPrice) / $quantityUnit;

        $profitForOneProduct = ($yy) - ($quantityForOneProduct * $xx);
        /* $smarty->assign("profitForOneProduct", $profitForOneProduct);
          $smarty->assign("quantityForOneProduct", $quantityForOneProduct);
         */

        $smarty->assign("profitForOneProduct" . $h . '', $profitForOneProduct);
        $smarty->assign("quantityForOneProduct" . $h . '', $quantityForOneProduct);

        $finalProductcatProfit+=$profitForOneProduct;
        $h++;
    }

    /*

      $productData=$myProductEx->getName($productId);
      $smarty->assign("productData".$h.'', $productData);


      //sellbill
      $sellPriceData=getTotalSellPriceByDateAndProductId($startDate, $endDate, $productId);


      //print_r("المبيعات".$sellPriceData[0]);
      //print_r("<br>سعر الاصلى للمبيعات".$sellPriceData[2]);
      //finalbuypric
      //	print_r("<br>");
      $sellPricePulsData=getTotalAditionalSellPriceByDateAndProductId($startDate, $endDate,$productId);

      //print_r("المبيعات فى الردودات".$sellPricePulsData[0]);

      //print_r("<br>سعر الاصلى للمبيعات".$sellPricePulsData[2]);
      //print_r("<br>");
      //print_r($sellPricePulsData);
      //print_r("<br>");
      $sellPrice = $sellPriceData[0] + $sellPricePulsData[0];
      $quantityUnit = $sellPriceData[1] + $sellPricePulsData[1];

      $totalSellPrice += $sellPrice;
      $totalQuantityUnit += $quantityUnit;


      //return sellbill
      $returnSellPriceData=getTotalReturnSellPriceByDateAndProductId($startDate, $endDate, $productId);

      //print_r(" المردودات ".$returnSellPriceData[0]);

      //print_r("<br>سعر الاصلى المردودات".$returnSellPriceData[2]);
      //print_r("<br>");

      $returnSellPricePulsData=getTotalAditionalReturnSellPriceByDateAndProductId($startDate, $endDate, $productId);



      //	print_r(" فى المبيعات المردودات ".$returnSellPricePulsData[0]);

      //print_r("<br>سعر الاصلى المردودات".$returnSellPricePulsData[2]);
      //print_r("<br>");
      $returnSellPrice=$returnSellPriceData[0] + $returnSellPricePulsData[0];

      $returnQuantityUnit=$returnSellPriceData[1] + $returnSellPricePulsData[1];


      $totalReturnSellPrice += $returnSellPrice;
      $totalreturnQuantityUnit += $returnQuantityUnit;


      //buybill
      $buybillPriceData=getTotalBuyPriceByDateAndProductId($startDate, $endDate, $productId);

      $buybillPricePulsData=getTotalAditionalBuyPriceByDateAndProductId($startDate, $endDate, $productId);

      $buybillPrice = $buybillPriceData[0] + $buybillPricePulsData[0];
      $buyQuantityUnit = $buybillPriceData[1] + $buybillPricePulsData[1];


      $totalBuybillPrice += $buybillPrice;
      $totalBuyQuantityUnit += $buyQuantityUnit;


      //return buybill
      $returnBuybillPriceData=getTotalReturnBuyPriceByDateAndProductId($startDate, $endDate, $productId);

      $returnBuybillPricePulsData=getTotalAditionalBuyPriceByDateAndProductId($startDate, $endDate, $productId);


      $returnBuybillPrice = $returnBuybillPriceData[0] + $returnBuybillPricePulsData[0];
      $returnBuyQuantityUnit = $returnBuybillPriceData[1] + $returnBuybillPricePulsData[1];


      $totalReturnBuybillPrice += $returnBuybillPrice;
      $totalReturnBuyQuantityUnit += $returnBuyQuantityUnit;

      $quantityForOneProduct = $quantityUnit -  $returnQuantityUnit;


      //	$profitForOneProduct= ($sellPrice + $returnBuybillPrice) - ($buybillPrice + $returnSellPrice);

      $profitForOneProduct=(($sellPriceData[0] +$sellPricePulsData[0]) -($sellPriceData[2]+$sellPricePulsData[2]))-(($returnSellPriceData[0]+$returnSellPricePulsData[0])-($returnSellPriceData[2]+$returnSellPricePulsData[2]));




      $smarty->assign("profitForOneProduct".$h.'', $profitForOneProduct);
      $smarty->assign("quantityForOneProduct".$h.'', $quantityForOneProduct);

      $finalProductcatProfit+=$profitForOneProduct;
      $h++;

     */

    $finalQuantityUnit = $totalQuantityUnit - $totalreturnQuantityUnit;

    $smarty->assign("finalProductcatProfit", $finalProductcatProfit);
    $smarty->assign("finalQuantityUnit", $finalQuantityUnit);
}

// show the form by date
function getTotalSellPriceByDateAndProductId($startDate, $endDate, $ProductIdselected) {
    global $mySellbilldetailEx;
    global $myProductunitRecord;
    global $mySellbillRecord;
    global $myProductRecord;
    global $myProductEx;
    global $myProductunitEx;

    $sellPrice = 0;
    $quantityUnit = 0;
    $finalbuypric = 0;
    $sellbilldetailData = $mySellbilldetailEx->queryWithDateAndConditionsAndProductIdExt($startDate, $endDate, $ProductIdselected);

    foreach ($sellbilldetailData as $sellbilldetail) {
        $quantity = $sellbilldetail->sellbilldetailquantity;
        $price = $sellbilldetail->sellbilldetailprice;
        $totalPrice = $sellbilldetail->sellbilldetailtotalprice;
        $discountValue = $sellbilldetail->discountvalue;
        $productunitId = $sellbilldetail->productunitid;
        $sellbillTotalBill = $sellbilldetail->sellbilltotalbill;
        $sellbillDiscount = $sellbilldetail->sellbilldiscount;
        $sellbillDiscountType = $sellbilldetail->sellbilldiscounttype;
        $buyprice = $sellbilldetail->buyprice;

        if ($sellbillDiscountType == 2) {
            $sellbillDiscount = ($sellbillDiscount / 100) * $sellbillTotalBill;
        }

        $totalPriceBeforeDiscount = $price * $quantity; //product price before discount

        $productnumber = $myProductunitEx->getProductNumber($productunitId);

        $quantityUnit += ($quantity * $productnumber); //الكمية * عدد القطع في وحدة القياس
        //first Step قيمة خصم المنتج على الوحدة
        //$firstStep = $discountValue;
        //second Stepقيمة المنتج من الخصم على المنتج
        //$secondStep = $totalPrice / $sellbillTotalBill;

        if ($sellbillTotalBill == 0) {
            $sellbillTotalBill = 1;
        }
        //third Step قيمة الخصم للمنتجات
        $thirdStep = ($totalPrice / $sellbillTotalBill) * $sellbillDiscount;

        //fourth Step  قيمة خصم الفاتورة للوحدة
        //$fourthStep = $thirdStep;
        //$sellPriceForOneProduct= $totalPriceBeforeDiscount - ($firstStep + $fourthStep);
        $allbuyprice = $buyprice * $quantity;
        $finalbuypric+=$allbuyprice;
        $sellPriceForOneProduct = $totalPriceBeforeDiscount - ($discountValue + $thirdStep);
        $sellPrice += $sellPriceForOneProduct;
    }



    return array($sellPrice, $quantityUnit, $finalbuypric);
}

// show the form by date
function getTotalAditionalSellPriceByDateAndProductId($startDate, $endDate, $ProductIdselected) {
    global $mySellandruternbilldetailEx;
    global $mySellbillandruternRecord;
    global $myProductunitRecord;
    global $myProductRecord;
    global $myProductEx;
    global $myProductunitEx;

    $sellPrice = 0;
    $quantityUnit = 0;
    $fianlbuyprice = 0;

    $sellandreturndetailData = $mySellandruternbilldetailEx->queryWithDateAndConditionsTypeAndProductIdExt($startDate, $endDate, 0, $ProductIdselected);


    foreach ($sellandreturndetailData as $sellandreturndetail) {
        $quantity = $sellandreturndetail->sellbilldetailquantity;
        $price = $sellandreturndetail->sellbilldetailprice;
        $totalPrice = $sellandreturndetail->sellbilldetailtotalprice;
        $discountValue = $sellandreturndetail->discountvalue;
        $productunitId = $sellandreturndetail->productunitid;
        $sellbillTotalBill = $sellandreturndetail->sellbillprice;
        $sellbillDiscount = $sellandreturndetail->sellbilldiscount; //محتاجة تتغير
        $sellbillDiscountType = $sellandreturndetail->sellbilldiscounttype;
        $buyprice = $sellandreturndetail->buyprice;



        if ($sellbillDiscountType == 2) {
            $sellbillDiscount = ($sellbillDiscount / 100) * $sellbillTotalBill;
        }

//
//			//load Product Data
//			$productData=$myProductRecord->load($productId);
//
//			$productSellPrice;
//			//تجزئة
//			if($priceType == 1)
//			{
//				$productSellPrice=$productData->productSellUnitPrice;
//			}
//			//جملة
//			elseif($priceType == 0)
//			{
//				$productSellPrice=$productData->productSellAllPrice;
//			}
//			//نصف جملة
//			elseif($priceType == 2)
//			{
//				$productSellPrice=$productData->productSellHalfPrice;
//			}
//

        $totalPriceBeforeDiscount = $price * $quantity;

        $productnumber = $myProductunitEx->getProductNumber($productunitId);

        $quantityUnit += ($quantity * $productnumber);

        if ($sellbillTotalBill == 0) {
            $sellbillTotalBill = 1;
        }
        //third Step قيمة الخصم للمنتجات
        $thirdStep = ($totalPrice / $sellbillTotalBill) * $sellbillDiscount;
        $allbuyprice = $quantity * $buyprice;
        $fianlbuyprice+=$allbuyprice;

        $sellPriceForOneProduct = $totalPriceBeforeDiscount - ($discountValue + $thirdStep);
        $sellPrice += $sellPriceForOneProduct;
    }



    return array($sellPrice, $quantityUnit, $fianlbuyprice);
}

function getTotalReturnSellPriceByDateAndProductId($startDate, $endDate, $ProductIdselected) {
    global $myReturnsellbilldetailEx;
    global $myReturnsellbillRecord;
    global $myProductunitRecord;
    global $myProductRecord;
    global $myProductEx;
    global $myProductunitEx;

    $ReturnSellPrice = 0;
    $quantityUnit = 0;
    $finalbuyprice = 0;

    $returnsellbilldetailData = $myReturnsellbilldetailEx->queryWithDateAndConditionsAndProductIdExt($startDate, $endDate, $ProductIdselected);


    foreach ($returnsellbilldetailData as $returnsellbilldetail) {
        $quantity = $returnsellbilldetail->returnsellbilldetailquantity;
        $price = $returnsellbilldetail->returnsellbilldetailprice;
        $totalPrice = $returnsellbilldetail->returnsellbilldetailtotalprice;
        $discountValue = $returnsellbilldetail->discountvalue;
        $productunitId = $returnsellbilldetail->productunitid;
        $sellbillTotalBill = $returnsellbilldetail->returnsellbilltotalbill;
        $sellbillDiscount = $returnsellbilldetail->returnsellbilldiscount;
        $sellbillDiscountType = $returnsellbilldetail->returnsellbilldiscounttype;
        $buyprice = $returnsellbilldetail->buyprice;

        if ($sellbillDiscountType == 2) {
            $sellbillDiscount = ($sellbillDiscount / 100) * $sellbillTotalBill;
        }

        $totalPriceBeforeDiscount = $price * $quantity;

        $productnumber = $myProductunitEx->getProductNumber($productunitId);

        $quantityUnit += ($quantity * $productnumber);

        if ($sellbillTotalBill == 0) {
            $sellbillTotalBill = 1;
        }
        //third Step قيمة الخصم للمنتجات
        $thirdStep = ($totalPrice / $sellbillTotalBill) * $sellbillDiscount;


        $allbuyprice = $quantity * $buyprice;

        //print_r("<br>buyprice".$buyprice);
        //print_r("<br>quantity".$quantity);
        $finalbuyprice+=$allbuyprice;
        $ReturnSellPriceForOneProduct = $totalPriceBeforeDiscount - ($discountValue + $thirdStep);
        $ReturnSellPrice += $ReturnSellPriceForOneProduct;
    }

//print($ReturnSellPrice."ReturnSellPrice<br>");

    return array($ReturnSellPrice, $quantityUnit, $finalbuyprice);
}

// show the form by date
function getTotalAditionalReturnSellPriceByDateAndProductId($startDate, $endDate, $ProductIdselected) {
    global $mySellandruternbilldetailEx;
    global $mySellbillandruternRecord;
    global $myProductunitRecord;
    global $myProductRecord;
    global $myProductunitEx;

    $ReturnSellPrice = 0;
    $quantityUnit = 0;
    $finalbuyprice = 0;

    $sellandreturndetailData = $mySellandruternbilldetailEx->queryWithDateAndConditionsTypeAndProductIdExt($startDate, $endDate, 1, $ProductIdselected);


    //print_r($sellandreturndetailData);
    foreach ($sellandreturndetailData as $sellandreturndetail) {
        $quantity = $sellandreturndetail->sellbilldetailquantity;
        $price = $sellandreturndetail->sellbilldetailprice;
        $totalPrice = $sellandreturndetail->sellbilldetailtotalprice;
        $discountValue = $sellandreturndetail->discountvalue;
        $productunitId = $sellandreturndetail->productunitid;
        //$sellbillTotalBill=$sellbillData->sellbillprice;
        $sellbillDiscount = $sellandreturndetail->sellbilldiscount; //محتاجة تتغير
        $totalPrice = $sellandreturndetail->sellbilldetailtotalprice;
        $discountvalue = $sellandreturndetail->discountvalue;
        $buyprice = $sellandreturndetail->buyprice;
        //print_r("<br>sellbillDiscount".$sellbillDiscount."<br>");
        //print_r("<br>price".$price."<br>");
        //print_r("<br>discountvalue".$discountvalue."<br>");
        //خصم نسبه
        /* if($sellbillDiscountType == 2)
          {
          $sellbillDiscount = ($sellbillDiscount / 100) * $sellbillTotalBill;
          }
         */
        $totalPriceBeforeDiscount = $price * $quantity;

        $productnumber = $myProductunitEx->getProductNumber($productunitId);

        $quantityUnit += ($quantity * $productnumber);

        if ($sellbillTotalBill == 0) {
            $sellbillTotalBill = 1;
        }
        //third Step قيمة الخصم للمنتجات
        $allbuyprice = $quantity * $buyprice;
        $finalbuyprice+=$allbuyprice;
        $thirdStep = ($totalPrice / $sellbillTotalBill);
        //print_r("<br>totalPriceBeforeDiscount".$totalPriceBeforeDiscount."<br>");
        $ReturnSellPriceForOneProduct = $totalPriceBeforeDiscount - ($discountValue * $quantity);
        //$ReturnSellPriceForOneProduct = $totalPriceBeforeDiscount - ($discountValue + $thirdStep);
        //print_r("--------------<br>ReturnSellPriceForOneProduct".$ReturnSellPriceForOneProduct."<br>-------");
        $ReturnSellPrice += $ReturnSellPriceForOneProduct;
    }
    //print_r("<br>".$ReturnSellPrice."<br>");

    return array($ReturnSellPrice, $quantityUnit, $finalbuyprice);
}

function getTotalBuyPriceByDateAndProductId($startDate, $endDate, $ProductIdselected) {
    global $mySellbilldetailEx;
    global $myProductunitRecord;
    global $mySellbillRecord;
    global $myProductRecord;
    global $myProductunitEx;

    $sellPrice = 0;
    $quantityUnit = 0;

    $sellbilldetailData = $mySellbilldetailEx->queryWithDateAndConditionsAndProductIdExt($startDate, $endDate, $ProductIdselected);



    foreach ($sellbilldetailData as $sellbilldetail) {
        $quantity = $sellbilldetail->sellbilldetailquantity;
        $price = $sellbilldetail->sellbilldetailprice;
        $totalPrice = $sellbilldetail->sellbilldetailtotalprice;
        $discountValue = $sellbilldetail->discountvalue;
        $productunitId = $sellbilldetail->productunitid;
        $buyPrice = $sellbilldetail->buyprice;
        $buyDiscount = $sellbilldetail->buydiscount;
        $buyDiscountType = $sellbilldetail->buydiscounttype;

        $productnumber = $myProductunitEx->getProductNumber($productunitId);

        $productBuyPrice = $buyPrice;
        if ($buyDiscount > 0 && $buyDiscountType == 0) {
            $productBuyPrice = $buyPrice - $buyDiscount;
        } elseif ($buyDiscount > 0 && $buyDiscountType == 1) {
            $productBuyPrice = $buyPrice - (($buyDiscount / 100) * $buyPrice);
        }

        $productBuyPrice = $productBuyPrice * $productnumber * $quantity;

        $quantityUnit += ($quantity * $productnumber);

        $sellPrice += $productBuyPrice;
    }



    return array($sellPrice, $quantityUnit);
}

function getTotalAditionalBuyPriceByDateAndProductId($startDate, $endDate, $ProductIdselected) {
    global $mySellandruternbilldetailEx;
    global $mySellbillandruternRecord;
    global $myProductunitRecord;
    global $myProductRecord;
    global $myProductunitEx;

    $sellPrice = 0;
    $quantityUnit = 0;

    $sellandreturndetailData = $mySellandruternbilldetailEx->queryWithDateAndConditionsTypeAndProductIdExt($startDate, $endDate, 0, $ProductIdselected);

    foreach ($sellandreturndetailData as $sellandreturndetail) {
        $quantity = $sellandreturndetail->sellbilldetailquantity;
        $price = $sellandreturndetail->sellbilldetailprice;
        $totalPrice = $sellandreturndetail->sellbilldetailtotalprice;
        $discountValue = $sellandreturndetail->discountvalue;
        $productunitId = $sellandreturndetail->productunitid;
        $buyPrice = $sellandreturndetail->buyprice;
        $buyDiscount = $sellandreturndetail->buydiscount;
        $buyDiscountType = $sellandreturndetail->buydiscounttype;

        $productnumber = $myProductunitEx->getProductNumber($productunitId);

        $productBuyPrice = $buyPrice;
        if ($buyDiscount > 0 && $buyDiscountType == 0) {
            $productBuyPrice = $buyPrice - $buyDiscount;
        } elseif ($buyDiscount > 0 && $buyDiscountType == 1) {
            $productBuyPrice = $buyPrice - (($buyDiscount / 100) * $buyPrice);
        }

        $productBuyPrice = $productBuyPrice * $productnumber * $quantity;

        $quantityUnit += ($quantity * $productnumber);

        $sellPrice += $productBuyPrice;
    }



    return array($sellPrice, $quantityUnit);
}

function getTotalAditionalReturnBuyPriceByDateAndProductId($startDate, $endDate, $ProductIdselected) {
    global $mySellandruternbilldetailEx;
    global $mySellbillandruternRecord;
    global $myProductunitRecord;
    global $myProductRecord;
    global $myProductunitEx;

    $sellPrice = 0;
    $quantityUnit = 0;

    $sellandreturndetailData = $mySellandruternbilldetailEx->queryWithDateAndConditionsTypeAndProductIdExt($startDate, $endDate, 1, $ProductIdselected);

    foreach ($sellandreturndetailData as $sellandreturndetail) {
        $quantity = $sellandreturndetail->sellbilldetailquantity;
        $price = $sellandreturndetail->sellbilldetailprice;
        $totalPrice = $sellandreturndetail->sellbilldetailtotalprice;
        $discountValue = $sellandreturndetail->discountvalue;
        $productunitId = $sellandreturndetail->productunitid;
        $buyPrice = $sellandreturndetail->buyprice;
        $buyDiscount = $sellandreturndetail->buydiscount;
        $buyDiscountType = $sellandreturndetail->buydiscounttype;

        $productnumber = $myProductunitEx->getProductNumber($productunitId);

        $productBuyPrice = $buyPrice;
        if ($buyDiscount > 0 && $buyDiscountType == 0) {
            $productBuyPrice = $buyPrice - $buyDiscount;
        } elseif ($buyDiscount > 0 && $buyDiscountType == 1) {
            $productBuyPrice = $buyPrice - (($buyDiscount / 100) * $buyPrice);
        }

        $productBuyPrice = $productBuyPrice * $productnumber * $quantity;


        $quantityUnit += ($quantity * $productnumber);

        $sellPrice += $productBuyPrice;
    }
    return array($sellPrice, $quantityUnit);
}

function getTotalReturnBuyPriceByDateAndProductId($startDate, $endDate, $ProductIdselected) {
    global $myReturnsellbilldetailEx;
    global $myReturnsellbillRecord;
    global $myProductunitRecord;
    global $myProductRecord;
    global $myProductunitEx;

    $sellPrice = 0;
    $quantityUnit = 0;


    $returnsellbilldetailData = $myReturnsellbilldetailEx->queryWithDateAndConditionsAndProductIdExt($startDate, $endDate, $ProductIdselected);


    foreach ($returnsellbilldetailData as $returnsellbilldetail) {
        $quantity = $returnsellbilldetail->returnsellbilldetailquantity;
        $price = $returnsellbilldetail->returnsellbilldetailprice;
        $totalPrice = $returnsellbilldetail->returnsellbilldetailtotalprice;
        $discountValue = $returnsellbilldetail->discountvalue;
        $productunitId = $returnsellbilldetail->productunitid;
        $buyPrice = $returnsellbilldetail->buyprice;
        $buyDiscount = $returnsellbilldetail->buydiscount;
        $buyDiscountType = $returnsellbilldetail->buydiscounttype;

        $productnumber = $myProductunitEx->getProductNumber($productunitId);

        $productBuyPrice = $buyPrice;
        if ($buyDiscount > 0 && $buyDiscountType == 0) {
            $productBuyPrice = $buyPrice - $buyDiscount;
        } elseif ($buyDiscount > 0 && $buyDiscountType == 1) {
            $productBuyPrice = $buyPrice - (($buyDiscount / 100) * $buyPrice);
        }

        $productBuyPrice = $productBuyPrice * $productnumber * $quantity;

        $quantityUnit += ($quantity * $productnumber);

        $sellPrice += $productBuyPrice;
    }
    return array($sellPrice, $quantityUnit);
}

function loadProductCat() {
    global $myProductcatEx;
    global $myProductunitEx;

    $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) {
    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 loadProductCatById($productCatId) {
    global $myProductcatRecord;

    $productCatSearchData = $myProductcatRecord->load($productCatId);
    return $productCatSearchData;
}

?>