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

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();

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

    $clientsData = getClients();
    $smarty->assign("clientsData", $clientsData);

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

    if (isset($productId) && $productId != "-1" && $productId != "" && isset($clientId) && $clientId != "-1" && isset($startDate) && isset($endDate) && $startDate != "" && $endDate != "") {
        $productData = $productDAO->load($productId);
        $clientData = $clientDAO->load($clientId);
        $message = "العميل :" . $clientData->clientname . "<br> المنتج: " . $productData->productName . "<br> من تاريخ: " . $startDate . " إلى تاريخ: " . $endDate;
        $smarty->assign('message', $message);

        loadPricesForClient($productId, $clientId, $startDate, $endDate);
    }

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

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


if ($do == "rates") {
    //here the permission check
    include_once("../public/authentication.php");

    $clientsData = getClients();
    $smarty->assign("clientsData", $clientsData);

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

    if (isset($productId) && $productId != "-1" && isset($clientId) && $clientId != "-1" && isset($startDate) && isset($endDate) && $startDate != "" && $endDate != "") {
        $productData = $productDAO->load($productId);
        $clientData = $clientDAO->load($clientId);
        $message = "العميل :" . $clientData->clientname . "<br> المنتج: " . $productData->productName . "<br> من تاريخ: " . $startDate . " إلى تاريخ: " . $endDate;
        $smarty->assign('message', $message);

        loadPricesForClient($productId, $clientId, $startDate, $endDate);
    }

    $Programsettingdata = $ProgramsettingDAO->load(1);
    $smarty->assign("Programsettingdata", $Programsettingdata);
    //here the smarty templates
    $smarty->display("clientsAndsellProductsReportview/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 loadPricesForClient($productId, $clientId, $startDate, $endDate) {
    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->getallproductbyclientanddatenor($startDate, $endDate, $productId, $clientId);


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


    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);
}

?>