<?php

//the global file operation
session_start();
ob_start();

//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');
//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');
//get the do the action
$do = $_GET['do'];

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

  Controller Name :- supplierReportsController

  OPERTATION in Controller

  1- عرض تقرير دفعات
  2-  تقرير لمورد معين

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

//here goes the instances and general variables
//Sellbill
$sellBill = new Sellbill();
$sellBillDAO = new SellbillMySqlDAO();
$sellBillExt = new SellbillMySqlExtDAO();
//Sellbilldetail
$sellBillDetail = new Sellbilldetail();
$sellBillDetailDAO = new SellbilldetailMySqlDAO();
$sellBillDetailExt = new SellbilldetailMySqlExtDAO();
//Product
$product = new Product();
$productDAO = new ProductMySqlDAO();
$productExt = new ProductMySqlExtDAO();
//Productcat
$productCatDAO = new ProductcatMySqlDAO();
$productCatExt = new ProductcatMySqlExtDAO();

$today = date("Y-m-d");

//check and use the condition that suite this action
if (empty($do) || $do == "show") {// تقرير المبيعات
    //here the permssion check


    $startDate = $_REQUEST['from'];
    $endDate = $_REQUEST['to'];
    $catId = $_REQUEST['catid'];
    if (!empty($catId) && !empty($startDate) && !empty($endDate)) {
        show($startDate, $endDate, $catId);
    }
    $smarty->display("sellBillReportsview/result.html");
} elseif ($do == "pdetails") {
    //here the permssion check


    $startDate = $_REQUEST['from'];
    $endDate = $_REQUEST['to'];
    $proId = $_REQUEST['proid'];
    if (!empty($proId) && !empty($startDate) && !empty($endDate)) {
        showProductDetails($startDate, $endDate, $proId);
    }
    $smarty->display("sellBillReportsview/productDetails.html");
}

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

//show
function show($startDate, $endDate, $catId) {
    global $sellBillDetailExt;
    global $sellBillDetailDAO;
    global $smarty;

    $shownData = $sellBillDetailExt->queryByDateAndProductCat($startDate, $endDate, $catId);

    $totalQty = 0;
    $totalPrice = 0;
    foreach ($shownData as $data) {
        $totalQty += $data->sellbilldetailquantity;
        $totalPrice += $totalQty * $data->sellbilldetailprice;
    }
    $smarty->assign('totalQty', $totalQty);
    $smarty->assign('totalPrice', $totalPrice);

    $smarty->assign('shownData', $shownData);
}

function showProductDetails($startDate, $endDate, $proId) {
    global $sellBillDetailExt;
    global $sellBillDetailDAO;
    global $smarty;

    $shownData = $sellBillDetailExt->queryProductDetailsByDate($startDate, $endDate, $proId);
    $smarty->assign('shownData', $shownData);
}

?>