<?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");
//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');
//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
//Buybill
$buyBill = new Buybill();
$buyBillDAO = new BuybillMySqlDAO();
$buyBillExt = new BuybillMySqlExtDAO();
//Buybilldetail
$buyBillDetail = new Buybilldetail();
$buyBillDetailDAO = new BuybilldetailMySqlDAO();
$buyBillDetailExt = new BuybilldetailMySqlExtDAO();
//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("buyBillReportsview/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("buyBillReportsview/productDetails.html");
}

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

//show
function show($startDate, $endDate, $catId) {
    global $buyBillDetailExt;
    global $buyBillDetailDAO;
    global $smarty;

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

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

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

function showProductDetails($startDate, $endDate, $proId) {
    global $buyBillDetailExt;
    global $buyBillDetailDAO;
    global $smarty;

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

?>