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

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

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

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


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

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

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

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 :- clientsAndProductsReportController تقرير عملاء ومنتجات

  OPERTATION in Controller

  1- show report
  2- search with clients and products and date

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

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

//here goes the instances and general variables
$ProgramsettingDAO = new ProgramsettingsMySqlDAO();

//Product
$productDAO = new ProductMySqlDAO();
$productExt = new ProductMySqlExtDAO();
//Productcat
$productCatDAO = new ProductcatMySqlDAO();
$productCatExt = new ProductcatMySqlExtDAO();
//Client
$client = new Client();
$clientDAO = new ClientMySqlDAO();
$clientExt = new ClientMySqlExtDAO();
//Productunit
$productUnitDAO = new ProductunitMySqlDAO();
$productUnitExt = new ProductunitMySqlExtDAO();

########################################## المبيعات
//Sellbill
$Sellbill = new Sellbill();
$SellbillDAO = new SellbillMySqlDAO();
$SellbillEX = new SellbillMySqlExtDAO();

//Sellbilldetail
$Sellbilldetail = new Sellbilldetail();
$SellbilldetailDAO = new SellbilldetailMySqlDAO();
$SellbilldetailEX = new SellbilldetailMySqlExtDAO();

//Returnsellbill
$Returnsellbill = new Returnsellbill();
$ReturnsellbillDAO = new ReturnsellbillMySqlDAO();
$ReturnsellbillEX = new ReturnsellbillMySqlExtDAO();

//Returnsellbilldetail
$Returnsellbilldetail = new Returnsellbilldetail();
$ReturnsellbilldetailDAO = new ReturnsellbilldetailMySqlDAO();
$ReturnsellbilldetailEX = new ReturnsellbilldetailMySqlExtDAO();

//Sellbillandrutern
$Sellbillandrutern = new Sellbillandrutern();
$SellbillandruternDAO = new SellbillandruternMySqlDAO();
$SellbillandruternEX = new SellbillandruternMySqlExtDAO();

//Sellandruternbilldetail
$Sellandruternbilldetail = new Sellandruternbilldetail();
$SellandruternbilldetailDAO = new SellandruternbilldetailMySqlDAO();
$SellandruternbilldetailEX = new SellandruternbilldetailMySqlExtDAO();

//Storereport
$Storereport = new Storereport();
$StorereportDAO = new StorereportMySqlDAO();
$StorereportEx = new StorereportMySqlExtDAO();


################################## البصريات
//bill
$bills = new Bill();
$billsDAO = new BillsMySqlDAO();
$billsEX = new BillsMySqlExtDAO();

//billsproducts
$billsProducts = new Billsproduct();
$billsProductsDAO = new BillsproductsMySqlDAO();
$billsProductsEX = new BillsproductsMySqlExtDAO();

//bill
$billsReturn = new Billsreturn();
$billsReturnDAO = new BillsreturnMySqlDAO();
$billsReturnEX = new BillsreturnMySqlExtDAO();

//billsproducts
$billsReturnProducts = new Billsreturnproduct();
$billsReturnProductsDAO = new BillsreturnproductsMySqlDAO();
$billsReturnProductsEX = new BillsreturnproductsMySqlExtDAO();

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

$smarty->assign("hidecat", $_SESSION['hidecat']);
//check and use the condetion that suite this action
if ($do == "show" || empty($do)) {
    //here the permission check
    include_once("../public/authentication.php");

    $catData = $productCatDAO->queryByCondition(0);
    $smarty->assign("catData", $catData);

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

    $startDate = $_REQUEST['from'];
    $endDate = $_REQUEST['to'];
    $catid = $_REQUEST['catid'];
    $numberid = $_REQUEST['numberid'];
    $smarty->assign('startDate',$startDate);
    $smarty->assign('endDate',$endDate);
    $smarty->assign('catid',$catid);
    $smarty->assign('numberid',$numberid);

    //getallcatsellratex($catid, $startDate, $endDate);
    if (isset($catid) && $catid != "-1" && !empty($numberid) && (!empty($startDate) && !empty($endDate))) {

        //	print_r('2222222222222222222222222222222');
        $productData = $productDAO->load($productId);
        $clientData = $clientDAO->load($clientId);
        //	$message = "العميل :" . $clientData->clientname . "<br> المنتج: ".$productData->productName."<br> من تاريخ: " . $startDate . " إلى تاريخ: " . $endDate;
        //	$smarty->assign('message', $message);
        //getallcatsellrate($numberid, $catid, $startDate, $endDate);
        getallcatsellratex($catid, $numberid, $startDate, $endDate);
    }

    $Programsettingdata = $ProgramsettingDAO->load(1);
    $smarty->assign("Programsettingdata", $Programsettingdata);
    //here the smarty templates
    $smarty->display("catsellrateReportview/show.html");
    $smarty->assign("settlementstoreshow", 1); //for products (select2)
}



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

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

function getClients()
{
    global $clientDAO;
    $clientsData = $clientDAO->queryByCondition(0);
    return $clientsData;
}

/*
  function getallcatsellrate($numberid, $catid, $startDate, $endDate)
  {


  //print_r('111111111111111111111111');
  global $smarty;
  global $Sellbill;
  global $SellbillDAO;
  global $SellbillEX;


  global $Sellbilldetail;
  global $SellbilldetailDAO;
  global $SellbilldetailEX;


  global $Sellbillandrutern;
  global $SellbillandruternDAO;
  global $SellbillandruternEX;


  global $Sellandruternbilldetail;
  global $SellandruternbilldetailDAO;
  global $SellandruternbilldetailEX;


  global $Returnsellbill;
  global $ReturnsellbillDAO;
  global $ReturnsellbillEX;

  global $Returnsellbilldetail;
  global $ReturnsellbilldetailDAO;
  global $ReturnsellbilldetailEX;

  $mydata=$SellbilldetailEX->getallcatsellrate( $catid, $startDate, $endDate);


  $totalquantity=0;
  $sellquantity=0;
  $returnquantity=0;
  $totalsellprice=0;
  $totalreturnprice=0;
  $numberid=$numberid;

  foreach ($mydata as $alldata)
  {

  $price=$alldata->price;
  $quantity=$alldata->quantity;
  $type=$alldata->type;

  if($type ==0)
  {
  $myprice=($quantity*$price);
  $totalsellprice =$totalsellprice+($price);
  $sellquantity =$sellquantity+($myprice/$price);
  $totalquantity=$totalquantity+($totalsellprice/$price);
  }
  elseif($type == 1)
  {
  $myprice=($quantity*$price);
  $totalreturnprice =$totalreturnprice+($price);
  $returnquantity =$returnquantity+($myprice/$price);
  $totalquantity=$totalquantity+($totalreturnprice/$price);

  }

  }

  $final=$sellquantity-$returnquantity;
  $smarty->assign("totalsellprice",$totalsellprice);
  $smarty->assign("sellquantity",$sellquantity);
  $smarty->assign("totalreturnprice",$totalreturnprice);
  $smarty->assign("returnquantity",$returnquantity);
  $smarty->assign("totalquantity",$totalquantity);
  $smarty->assign("totalquantity",$totalquantity);
  $smarty->assign("final",$final);

  $smarty->assign("mydata",$mydata);
  $smarty->assign("mydata",$mydata);
  }
 */

function getallcatsellratex($catid, $numberid, $startDate, $endDate)
{
    global $StorereportEx;
    global $productExt;
    global $productCatDAO;
    global $queryString;
    global $smarty;
    $queryString = ' WHERE';

    $allData = "";
    ##########################المبيعات

    $tablename = "sellbillController.php";
    $tablename1 = "returnsellbillController.php";
    $tablename2 = "sellbillandruternController.php";
    ##########################البصريات   
    $tablename3 = "bills.php";
    $tablename4 = "billsreturn.php";

    $allData = $StorereportEx->queryWithtablenameforcatrate($catid, $startDate, $endDate, $tablename, $tablename1, $tablename2, $tablename3, $tablename4);


    $totalnumofsells = 0;
    $newarray;
    $productArr = [];
    foreach ($allData as $data) {
        //getAllproduct ids
        $productArr[] = $data->productid;

        $allselldata = $StorereportEx->loadWithtablenamexandtypeforcatrateWithDate($data->productid, 1, $tablename, $tablename1, $tablename2, $tablename3, $tablename4, $startDate, $endDate);
        $sellquantity = $allselldata->productquantity;
        $data->sellquantity = $sellquantity;
        $productname = $allselldata->productName;
        $data->productname = $productname;


        $allselldata = $StorereportEx->loadWithtablenamexandtypeforcatrateWithDate($data->productid, 0, $tablename, $tablename1, $tablename2, $tablename3, $tablename4, $startDate, $endDate);
        $returnquantity = $allselldata->productquantity;
        $data->returnquantity = $returnquantity;

        $sellandreturn = $sellquantity - $returnquantity;
        $data->sellandreturn = $sellandreturn;
        // var_dump($sellquantity,$returnquantity,$sellandreturn);

        //$allDatax=$StorereportEx->queryWithtablenamexandtypex($data->productid,$numberid,$tablename,$tablename1,$tablename2);
        //if ($sellandreturn <= $numberid) {
        if ($sellandreturn <= $numberid) {
            $newarray[$data->productid] = $data;
            $totalnumofsells += $sellandreturn;
        }
    }
    $productsArray = $productExt->getCatProducts($catid);
    if (!empty($productsArray)) {
        $catData = $productCatDAO->load($catid);
        foreach ($productsArray as $product) {
            if (!in_array($product->productId, $productArr)) {

                $proObj = new stdClass();
                $proObj->productid = $product->productId;
                $proObj->productName = $product->productName;
                $proObj->productCatName = $catData->productCatName;
                $proObj->sellquantity = 0;
                $proObj->returnquantity = 0;
                $proObj->sellandreturn = 0;
                $newarray[$product->productId] = $proObj;
            }
        }
    }
    $smarty->assign("newarray", $newarray);
    //$smarty->assign("allDatax",$allDatax);
    $smarty->assign("totalnumofsells", $totalnumofsells);
}
