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


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 :- productReportsController

  OPERTATION in Controller

  1- تقرير حركة صنف
  2- تقرير الأصناف بكل الأسعار
  3- تقرير الأصناف وسعر الشراء
  ======================== */

$sellAllDiscount = (float) $_REQUEST['sellAllDiscount'];
if ($sellAllDiscount > 0) {
} else {
    //here the global templates
    $smarty->display("header.html");
}

//here goes the instances and general variables
//Productcat
$productCat = new Productcat();
$productCatDAO = new ProductcatMySqlDAO();
$productCatExt = new ProductcatMySqlExtDAO();
//Product
$product = new Product();
$productDAO = new ProductMySqlDAO();
$productExt = new ProductMySqlExtDAO();
//Store
$storeDAO = new StoreMySqlDAO();

$userGroup = new Usergroup();
$userGroupDAO = new UsergroupMySqlDAO();
$userGroupEX = new UsergroupMySqlExtDAO();

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

//Programsetting
$ProgramsettingDAO = new ProgramsettingsMySqlDAO();

$userGroup = $userGroupDAO->load($_SESSION['usergroupid']);
$smarty->assign("userGroup", $userGroup);

$today = date("Y-m-d");
$smarty->assign("today", $today);
$smarty->assign("hidecat", $_SESSION['hidecat']);

//check and use the condition that suits this action
if (empty($do)) {

    //**************** NOT USED - NOT USED - NOT USED AND NOT IMPLEMENTED WELL  ********************//
    //تقرير حركة صنف

    /* include_once("../public/authentication.php");

      $productsData = getAllProducts();
      $smarty->assign("productsData",$productsData);

      $storesData = getStores();
      $smarty->assign("storesData",$storesData);
      $smarty->assign("productReports",1);
      $smarty->display("productReportsview/productReport.html"); */
}

//تقرير الأصناف بكل الأسعار
elseif ($do == "reportWithAllPrices") {

    include_once("../public/authentication.php");

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

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

    $userGroup = $userGroupDAO->load($_SESSION['usergroupid']);
    $smarty->assign("userGroup", $userGroup);

    $alldata = getProductCats();

    //		print("<prE>");
    //		print_r($alldata);
    //		print("</pre>");

    $smarty->assign("alldata", $alldata);

    $sellAllDiscount = (float) $_REQUEST['sellAllDiscount'];
    $smarty->assign("sellAllDiscount", $sellAllDiscount);
    $productCatId = $_REQUEST['productCatId'];
    $startDate = $_REQUEST['from'];
    $endDate = $_REQUEST['to'];

    if (!empty($productCatId) && $productCatId != "-1" && $productCatId != "all") {

        searchProductsWithBuyPrice($sellAllDiscount);
        $pathArr = getProductCatsParentsByCatId($productCatId);
        $message = "التصنيف: " . $pathArr;
        $smarty->assign("message", $message);
        $smarty->assign("pathArr", $pathArr);
    } elseif ($productCatId == "all") {
        //showProductsWithBuyPrice();
        searchProductsWithBuyPriceforallSimple($sellAllDiscount);
        /*
          $allcat=$productCatExt->getCategoriesWithoutProducts();
          $i=1;
          foreach( $allcat  as $mycat )
          {
          searchProductsWithBuyPriceforall($i);

          $i++;
          }
          $pathArr = getProductCatsParentsByCatId($productCatId);
          //$message = "التصنيف: ".$pathArr;
          //$smarty->assign("message",$message);

         */
    }

    if ($sellAllDiscount == 0)
        $smarty->display("productReportsview/reportWithAllPrices.html");
    else {

        $Programsettingdata = $ProgramsettingDAO->load(1);
        $smarty->assign("Programsettingdata", $Programsettingdata);
        $smarty->display("productReportsview/weightedDiscountReportPrint.html");
    }
}

//تقرير الأصناف وسعر الشراء
elseif ($do == "reportWithBuyPrice") {
    include_once("../public/authentication.php");
    //    $alldata = getProductCats();
    $alldata = $productExt->queryAllCategoriesParentLeftJoin(); //->queryAllCategoriesParent();
    $smarty->assign("alldata", $alldata);

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

    $productCatId = $_REQUEST['productCatId'];
    if (!empty($productCatId) && $productCatId != "-1" && $productCatId != "all") {
        searchProductsWithBuyPrice();
        $pathArr = getProductCatsParentsByCatId($productCatId);
        $message = "التصنيف: " . $pathArr;
        $smarty->assign("message", $message);

        $smarty->display("productReportsview/reportWithBuyPrice.html");
    } elseif ($productCatId == "all") {


        $smarty->assign("allcatdata", $alldata);
        //print_r($allcat);
        $i = 1;
        $totalcount = 0;
        $totalpricecount = 0;
        foreach ($alldata as $mycat) {


            $productCatId = $mycat->productCatId;
            searchProductsWithBuyPriceforall($productCatId, $i);

            $i++;
        }


        $smarty->assign("totalcount", $totalcount);
        $smarty->assign("totalpricecount", $totalpricecount);
        $smarty->display("productReportsview/allreportWithBuyPrice.html");
        //	$pathArr = getProductCatsParentsByCatId($productCatId);
    } else {
        $smarty->display("productReportsview/reportWithBuyPrice.html");
    }
}
//here the global templates
if ($sellAllDiscount > 0) {
} else {
    $smarty->display("footer.html");
}

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

function getProductCatParents()
{
    global $productCatExt;

    $allParents = $productCatExt->getProductCatParents();
    return $allParents;
}

function getStores()
{
    global $storeDAO;

    $storesData = $storeDAO->queryByConditions(0);
    return $storesData;
}

function getProductCats()
{
    global $productExt;
    global $productCatDAO;
    global $smarty;
    $categories;
    $productsData = $productExt->queryAllProducts();
    if (count($productsData) > 0) {
        $i = 0;
        foreach ($productsData as $pro) {
            $parentId = $pro->productCatId;
            $pathArr = fetch_recursive($parentId, $categories);
            $smarty->assign("names" . $i, $pathArr);
            $smarty->assign("parentId" . $i, $parentId);
            $i++;
        }
        $itr = $i - 1;
        $smarty->assign("itr", $itr);
    }
    return $productsData;
}

function getProductCatsParentsByCatId($productCatId)
{
    global $productExt;
    global $productCatDAO;
    global $smarty;
    $categories;
    $productsData = $productExt->queryByProductCatIdExt($productCatId);
    if (count($productsData) > 0) {
        foreach ($productsData as $pro) {
            $parentId = $pro->productCatId;
            $pathArr = fetch_recursive($parentId, $categories);

            break;
        }
    }
    return $pathArr;
}

function fetch_recursive($parentid, $categories)
{
    global $productCatExt;
    $catData = $productCatExt->getCategoryAndParentByCatId($parentid);

    if (count($catData) > 0) {
        $categories .= $catData->productCatName . '/';
        $newParentId = $catData->productCatParent;

        if ($newParentId != 0) {
            $newParentName = $catData->parentName;
            $categories .= $newParentName . '/';
            fetch_recursive($newParentId, $categories);
        }
    }
    $categories = substr($categories, 0, strlen($categories) - 1);
    return $categories;
}

//productCat
/* 	function getProductCats()
  {
  global $productCatExt ;

  //كل التصنيفات التي ليست parent لأي تصنيف آخر
  $alldata = $productCatExt->queryAllChildren();
  return $alldata;
  }
 */
function getAllProducts()
{
    global $productExt;

    $productsData = $productExt->queryAllExt();
    if (count($productsData)) {
        foreach ($productsData as $pro) {
            $productFullName = $pro->productCatName . '/' . $pro->productName;
            $pro->productName = $productFullName;
        }
    }
    return $productsData;
}

//////////////////////////**************************************//////////////////////////

function searchProductsWithBuyPrice($sellAllDiscount = 0)
{
    global $productDAO;
    global $productExt;
    global $productCatExt;
    global $productCatDAO;
    global $today;
    global $smarty;
    global $ProgramsettingDAO;

    $productCatId = $_REQUEST['productCatId'];
    $startDate = $_REQUEST['from'];
    $endDate = $_REQUEST['to'];
    $querysearch = ' ';
    if ($startDate && $endDate) {
        $querysearch .= ' and product.productDate >= "' . $startDate . '" and product.productDate <= "' . $endDate . '" ';
    }
    $Programsetting = $ProgramsettingDAO->load(1);
    $pro_price = $Programsetting->Inventoryevaluation; // تقييم المخزون
    $smarty->assign('programsettingsdata', $Programsetting);

    switch ($pro_price) {
        case "first":
            $mess = "   التقييم بـ سعر شراء";
            break;
        case "last":
            $mess = "   التقييم بـ اخر سعر شراء";
            break;
        case "mean":
            $mess = "   التقييم بـ متوسط سعر الشراء";
            break;
        case "last_discount":
            $mess = "   التقييم بـ اخر سعر شراء بعد الخصم";
            break;
        case "mean_discount":
            $mess = "   التقييم بـ متوسط سعر الشراء بعد الخصم";
            break;
        case "generalPrice":
            $mess = "   التقييم بـ المتوسط العام ";
            break;
        case "tax":
            $mess = "   التقييم بـ اخر سعر شراء شامل القيمة المضافة";
            break;
        case "mean_tax":
            $mess = "   التقييم بـ متوسط سعر الشراء شامل القيمة المضافة ";
            break;
        default:
            $mess = "  التقييم بـ متوسط سعر الشراء";
            break;
    }

    $smarty->assign('mess', $mess);




    if (!empty($productCatId)) {
        //select all data from product tbl by productCatId
        //$productsData = $productExt->queryByProductCatIdNotdeleted($productCatId);

        $productsData = $productExt->querySumQuantityWithProductCatId($productCatId, $querysearch);

        if (count($productsData) > 0) {
            foreach ($productsData as $pro) {
                // get productprice by productid
                $pro->weightedDiscount = (float) ($pro->weightedDiscount - $sellAllDiscount);
                $pro->weightedDiscountBuyPrice = (float) $pro->lastbuyprice;
                $pro->weightedDiscountPer = 1 - ($pro->weightedDiscount / 100);
                $pro->weightedDiscountBuyPriceAfterDisc = round(($pro->weightedDiscountBuyPrice * $pro->weightedDiscountPer), 2);

                switch ($pro_price) {
                    case "first":
                        $productBuyPrice = (float) $pro->productBuyPrice;
                        break;
                    case "last":
                        $productBuyPrice = (float) $pro->lastbuyprice;
                        break;
                    case "mean":
                        $productBuyPrice = (float) $pro->meanbuyprice;
                        break;
                    case "last_discount":
                        $productBuyPrice = (float) $pro->lastbuyprice_withDiscount;
                        break;
                    case "mean_discount":
                        $productBuyPrice = (float) $pro->meanbuyprice_withDiscount;
                        break;
                    case "tax":
                        $productBuyPrice = (float) $pro->lastbuyprice_withTax;
                        break;
                    case "mean_tax":
                        $productBuyPrice = (float) $pro->meanbuyprice_withTax;
                        break;
                    default:
                        $productBuyPrice = (float) $pro->overAllAveragePrice;
                        break;
                }


                //                $productBuyPrice = $pro->productBuyPrice;
                $productSellAllPrice = $pro->productSellAllPrice;
                $productSellUnitPrice = $pro->productSellUnitPrice;
                $productSellHalfPrice = $pro->productSellHalfPrice;
                $price4 = $pro->price4;
                $price5 = $pro->price5;
                $price6 = $pro->price6;
                $price7 = $pro->price7;
                $price8 = $pro->price8;

                $productCatId = $pro->productCatId;

                //get productCat data to get buydiscount, discounttype and selldiscount
                $buydiscount = $pro->buydiscount;
                $selldiscount = $pro->selldiscount;
                $discounttype = $pro->discounttype;
                //$catData = $productCatDAO->load($productCatId);
                //                if (count($catData) > 0) {
                //                    $buydiscount = $catData->buydiscount;
                //                    $selldiscount = $catData->selldiscount;
                //                    $discounttype = $catData->discounttype;

                if ($discounttype == 0) { //خصم مباشر
                    $productBuyPrice = $productBuyPrice - $buydiscount;
                    $productSellAllPrice = $productSellAllPrice - $selldiscount;
                    $productSellUnitPrice = $productSellUnitPrice - $selldiscount;
                    $productSellHalfPrice = $productSellHalfPrice - $selldiscount;
                    $price4 = $price4 - $selldiscount;
                    $price5 = $price5 - $selldiscount;
                    $price6 = $price6 - $selldiscount;
                    $price7 = $price7 - $selldiscount;
                    $price8 = $price8 - $selldiscount;
                } else { //خصم نسبة
                    $productBuyPrice = $productBuyPrice - (($buydiscount / 100) * $productBuyPrice);
                    $productSellAllPrice = $productSellAllPrice - (($selldiscount / 100) * $productSellAllPrice);

                    $productSellUnitPrice = $productSellUnitPrice - (($selldiscount / 100) * $productSellUnitPrice);
                    $productSellHalfPrice = $productSellHalfPrice - (($selldiscount / 100) * $productSellHalfPrice);
                    $price4 = $price4 - (($selldiscount / 100) * $price4);
                    $price5 = $price5 - (($selldiscount / 100) * $price5);
                    $price6 = $price6 - (($selldiscount / 100) * $price6);
                    $price7 = $price7 - (($selldiscount / 100) * $price7);
                    $price8 = $price8 - (($selldiscount / 100) * $price8);
                }

                $pro->productBuyPrice = $productBuyPrice;
                $pro->productSellAllPrice = $productSellAllPrice;
                $pro->productSellUnitPrice = $productSellUnitPrice;
                $pro->productSellHalfPrice = $productSellHalfPrice;
                $pro->price4 = $price4;
                $pro->price5 = $price5;
                $pro->price6 = $price6;
                $pro->price7 = $price7;
                $pro->price8 = $price8;

                //إجمالي السعر
                $totalPrice += $pro->sumProductQuantity * $productBuyPrice;
                //                }
                //إجمالي الوحدات
                $totalQuantity += $pro->sumProductQuantity;
            }
            $smarty->assign('totalPrice', $totalPrice);
            $smarty->assign('totalQuantity', $totalQuantity);
        }
    }
    // assign your db results to the template
    $smarty->assign('productsData', $productsData);
}

//
//like searchProductsWithBuyPrice()
function searchProductsWithBuyPriceforallSimple($sellAllDiscount = 0)
{
    global $productDAO;
    global $productExt;
    global $productCatExt;
    global $productCatDAO;
    global $today;
    global $smarty;
    global $ProgramsettingDAO;

    $Programsetting = $ProgramsettingDAO->load(1);
    $pro_price = $Programsetting->Inventoryevaluation; // تقييم المخزون
    $smarty->assign('programsettingsdata', $Programsetting);

    switch ($pro_price) {
        case "first":
            $mess = "   التقييم بـ سعر شراء";
            break;
        case "last":
            $mess = "   التقييم بـ اخر سعر شراء";
            break;
        case "mean":
            $mess = "   التقييم بـ متوسط سعر الشراء";
            break;
        case "last_discount":
            $mess = "   التقييم بـ اخر سعر شراء بعد الخصم";
            break;
        case "mean_discount":
            $mess = "   التقييم بـ متوسط سعر الشراء بعد الخصم";
            break;
        case "generalPrice":
            $mess = "   التقييم بـ المتوسط العام ";
            break;
        case "tax":
            $mess = "   التقييم بـ اخر سعر شراء شامل القيمة المضافة";
            break;
        case "mean_tax":
            $mess = "   التقييم بـ متوسط سعر الشراء شامل القيمة المضافة ";
            break;
        default:
            $mess = "  التقييم بـ متوسط سعر الشراء";
            break;
    }

    $smarty->assign('mess', $mess);

    $startDate = $_REQUEST['from'];
    $endDate = $_REQUEST['to'];
    $querysearch = '';
    if ($startDate && $endDate) {
        $querysearch .= ' and product.productDate >= "' . $startDate . '" and product.productDate <= "' . $endDate . '" ';
    }


    $productsData = $productExt->querySumQuantity($querysearch);

    if (count($productsData) > 0) {
        foreach ($productsData as $pro) {
            // get productprice by productid
            $pro->weightedDiscount = (float) ($pro->weightedDiscount - $sellAllDiscount);
            $pro->weightedDiscountBuyPrice = (float) $pro->lastbuyprice;
            $pro->weightedDiscountPer = 1 - ($pro->weightedDiscount / 100);
            $pro->weightedDiscountBuyPriceAfterDisc = round(($pro->weightedDiscountBuyPrice * $pro->weightedDiscountPer), 2);

            switch ($pro_price) {
                case "first":
                    $productBuyPrice = (float) $pro->productBuyPrice;
                    break;
                case "last":
                    $productBuyPrice = (float) $pro->lastbuyprice;
                    break;
                case "mean":
                    $productBuyPrice = (float) $pro->meanbuyprice;
                    break;
                case "last_discount":
                    $productBuyPrice = (float) $pro->lastbuyprice_withDiscount;
                    break;
                case "mean_discount":
                    $productBuyPrice = (float) $pro->meanbuyprice_withDiscount;
                    break;
                case "tax":
                    $productBuyPrice = (float) $pro->lastbuyprice_withTax;
                    break;
                case "mean_tax":
                    $productBuyPrice = (float) $pro->meanbuyprice_withTax;
                    break;
                default:
                    $productBuyPrice = (float) $pro->overAllAveragePrice;
                    break;
            }


            //                $productBuyPrice = $pro->productBuyPrice;
            $productSellAllPrice = $pro->productSellAllPrice;
            $productSellUnitPrice = $pro->productSellUnitPrice;
            $productSellHalfPrice = $pro->productSellHalfPrice;
            $price4 = $pro->price4;
            $price5 = $pro->price5;
            $price6 = $pro->price6;
            $price7 = $pro->price7;
            $price8 = $pro->price8;

            $productCatId = $pro->productCatId;

            //get productCat data to get buydiscount, discounttype and selldiscount
            $catData = $productCatDAO->load($productCatId);
            if (count($catData) > 0) {
                $buydiscount = $catData->buydiscount;
                $selldiscount = $catData->selldiscount;
                $discounttype = $catData->discounttype;

                if ($discounttype == 0) { //خصم مباشر
                    $productBuyPrice = $productBuyPrice - $buydiscount;
                    $productSellAllPrice = $productSellAllPrice - $selldiscount;
                    $productSellUnitPrice = $productSellUnitPrice - $selldiscount;
                    $productSellHalfPrice = $productSellHalfPrice - $selldiscount;
                    $price4 = $price4 - $selldiscount;
                    $price5 = $price5 - $selldiscount;
                    $price6 = $price6 - $selldiscount;
                    $price7 = $price7 - $selldiscount;
                    $price8 = $price8 - $selldiscount;
                } else { //خصم نسبة
                    $productBuyPrice = $productBuyPrice - (($buydiscount / 100) * $productBuyPrice);
                    $productSellAllPrice = $productSellAllPrice - (($selldiscount / 100) * $productSellAllPrice);

                    $productSellUnitPrice = $productSellUnitPrice - (($selldiscount / 100) * $productSellUnitPrice);
                    $productSellHalfPrice = $productSellHalfPrice - (($selldiscount / 100) * $productSellHalfPrice);
                    $price4 = $price4 - (($selldiscount / 100) * $price4);
                    $price5 = $price5 - (($selldiscount / 100) * $price5);
                    $price6 = $price6 - (($selldiscount / 100) * $price6);
                    $price7 = $price7 - (($selldiscount / 100) * $price7);
                    $price8 = $price8 - (($selldiscount / 100) * $price8);
                }

                $pro->productBuyPrice = $productBuyPrice;
                $pro->productSellAllPrice = $productSellAllPrice;
                $pro->productSellUnitPrice = $productSellUnitPrice;
                $pro->productSellHalfPrice = $productSellHalfPrice;
                $pro->price4 = $price4;
                $pro->price5 = $price5;
                $pro->price6 = $price6;
                $pro->price7 = $price7;
                $pro->price8 = $price8;

                //إجمالي السعر
                $totalPrice += $pro->sumProductQuantity * $productBuyPrice;
            }

            //إجمالي الوحدات
            $totalQuantity += $pro->sumProductQuantity;
        }
        $smarty->assign('totalPrice', $totalPrice);
        $smarty->assign('totalQuantity', $totalQuantity);
    }
    // assign your db results to the template
    $smarty->assign('productsData', $productsData);
}

function searchProductsWithBuyPriceforall($productCatId, $i)
{
    global $productDAO;
    global $productExt;
    global $productCatExt;
    global $productCatDAO;

    global $totalcount;
    global $totalpricecount;

    global $today;
    global $smarty;
    global $ProgramsettingDAO;

    $productCatId = $productCatId; //$_REQUEST['productCatId'];
    $Programsetting = $ProgramsettingDAO->load(1);
    $pro_price = $Programsetting->Inventoryevaluation; // تقييم المخزون
    $smarty->assign('programsettingsdata', $Programsetting);

    switch ($pro_price) {
        case "first":
            $mess = "   التقييم بـ سعر شراء";
            break;
        case "last":
            $mess = "   التقييم بـ اخر سعر شراء";
            break;
        case "mean":
            $mess = "   التقييم بـ متوسط سعر الشراء";
            break;
        case "last_discount":
            $mess = "   التقييم بـ اخر سعر شراء بعد الخصم";
            break;
        case "mean_discount":
            $mess = "   التقييم بـ متوسط سعر الشراء بعد الخصم";
            break;
        case "generalPrice":
            $mess = "   التقييم بـ المتوسط العام ";
            break;
        case "tax":
            $mess = "   التقييم بـ اخر سعر شراء شامل القيمة المضافة";
            break;
        case "mean_tax":
            $mess = "   التقييم بـ متوسط سعر الشراء شامل القيمة المضافة ";
            break;
        default:
            $mess = "  التقييم بـ متوسط سعر الشراء";
            break;
    }

    $smarty->assign('mess', $mess);

    if (!empty($productCatId)) {
        //select all data from product tbl by productCatId
        //$productsData = $productExt->queryByProductCatIdNotdeleted($productCatId);

        $productsData = $productExt->querySumQuantityWithProductCatId($productCatId);

        if (count($productsData) > 0) {
            foreach ($productsData as $pro) {
                // get productprice by productid

                switch ($pro_price) {
                    case "first":
                        $productBuyPrice = (float) $pro->productBuyPrice;
                        break;
                    case "last":
                        $productBuyPrice = (float) $pro->lastbuyprice;
                        break;
                    case "mean":
                        $productBuyPrice = (float) $pro->meanbuyprice;
                        break;
                    case "last_discount":
                        $productBuyPrice = (float) $pro->lastbuyprice_withDiscount;
                        break;
                    case "mean_discount":
                        $productBuyPrice = (float) $pro->meanbuyprice_withDiscount;
                        break;
                    case "tax":
                        $productBuyPrice = (float) $pro->lastbuyprice_withTax;
                        break;
                    case "mean_tax":
                        $productBuyPrice = (float) $pro->meanbuyprice_withTax;
                        break;
                    default:
                        $productBuyPrice = (float) $pro->overAllAveragePrice;
                        break;
                }

                //                $productBuyPrice = $pro->productBuyPrice;
                $productSellAllPrice = $pro->productSellAllPrice;
                $productSellUnitPrice = $pro->productSellUnitPrice;
                $productSellHalfPrice = $pro->productSellHalfPrice;

                $productCatId = $pro->productCatId;

                //get productCat data to get buydiscount, discounttype and selldiscount
                $buydiscount = $pro->buydiscount;
                $selldiscount = $pro->selldiscount;
                $discounttype = $pro->discounttype;
                //$catData = $productCatDAO->load($productCatId);
                //                if (count($catData) > 0) {
                //                    $buydiscount = $catData->buydiscount;
                //                    $selldiscount = $catData->selldiscount;
                //                    $discounttype = $catData->discounttype;

                if ($discounttype == 0) { //خصم مباشر
                    $productBuyPrice = $productBuyPrice - $buydiscount;
                    $productSellAllPrice = $productSellAllPrice - $selldiscount;
                    $productSellUnitPrice = $productSellUnitPrice - $selldiscount;
                    $productSellHalfPrice = $productSellHalfPrice - $selldiscount;
                } else { //خصم نسبة
                    $productBuyPrice = $productBuyPrice - (($buydiscount / 100) * $productBuyPrice);
                    $productSellAllPrice = $productSellAllPrice - (($selldiscount / 100) * $productSellAllPrice);

                    $productSellUnitPrice = $productSellUnitPrice - (($selldiscount / 100) * $productSellUnitPrice);
                    $productSellHalfPrice = $productSellHalfPrice - (($selldiscount / 100) * $productSellHalfPrice);
                }

                $pro->productBuyPrice = $productBuyPrice;
                $pro->productSellAllPrice = $productSellAllPrice;
                $pro->productSellUnitPrice = $productSellUnitPrice;
                $pro->productSellHalfPrice = $productSellHalfPrice;

                //إجمالي السعر
                $totalPrice += $pro->sumProductQuantity * $productBuyPrice;
                //                }
                //إجمالي الوحدات
                $totalQuantity += $pro->sumProductQuantity;
            }
            $smarty->assign('totalPrice' . $i, $totalPrice);
            $smarty->assign('totalQuantity' . $i, $totalQuantity);
            $totalcount = $totalcount + $totalQuantity;
            $totalpricecount = $totalpricecount + $totalPrice;
            //print_r('totalPrice'+$totalPrice.'<br>');
            //print_r('totalQuantity'+$totalQuantity.'<br>');
        }
    }
    // assign your db results to the template
    //$smarty->assign('productsData', $productsData);
}

//show
function showProductsWithBuyPrice()
{
    global $productDAO;
    global $productExt;
    global $productCatExt;
    global $productCatDAO;
    global $today;
    global $smarty;

    // get the all colums number
    $productsData = $productExt->querySumQuantityWithBuyPrice(); //select not deleted
    SmartyPaginate::setTotal(count($allColums));

    if (count($productsData) > 0) {
        foreach ($productsData as $pro) {
            // get productprice by productid
            $productBuyPrice = $pro->productBuyPrice;
            $productSellAllPrice = $pro->productSellAllPrice;
            $productSellUnitPrice = $pro->productSellUnitPrice;
            $productSellHalfPrice = $pro->productSellHalfPrice;

            $productCatId = $pro->productCatId;

            //get productCat data to get buydiscount, discounttype and selldiscount
            $catData = $productCatDAO->load($productCatId);
            if (count($catData) > 0) {
                $buydiscount = $catData->buydiscount;
                $selldiscount = $catData->selldiscount;
                $discounttype = $catData->discounttype;

                if ($discounttype == 0) { //خصم مباشر
                    $productBuyPrice = $productBuyPrice - $buydiscount;
                    $productSellAllPrice = $productSellAllPrice - $selldiscount;
                    $productSellUnitPrice = $productSellUnitPrice - $selldiscount;
                    $productSellHalfPrice = $productSellHalfPrice - $selldiscount;
                } else { //خصم نسبة
                    $productBuyPrice = $productBuyPrice - (($buydiscount / 100) * $productBuyPrice);
                    $productSellAllPrice = $productSellAllPrice - (($selldiscount / 100) * $productSellAllPrice);

                    $productSellUnitPrice = $productSellUnitPrice - (($selldiscount / 100) * $productSellUnitPrice);
                    $productSellHalfPrice = $productSellHalfPrice - (($selldiscount / 100) * $productSellHalfPrice);
                }

                $pro->productBuyPrice = $productBuyPrice;
                $pro->productSellAllPrice = $productSellAllPrice;
                $pro->productSellUnitPrice = $productSellUnitPrice;
                $pro->productSellHalfPrice = $productSellHalfPrice;

                //إجمالي السعر
                $totalPrice += $pro->sumProductQuantity * $productBuyPrice;
            }

            //إجمالي الوحدات
            $totalQuantity += $pro->sumProductQuantity;
        }
        $smarty->assign('totalPrice', $totalPrice);
        $smarty->assign('totalQuantity', $totalQuantity);
    }

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

//////////////////////////**************************************//////////////////////////
