<?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");
//expense
require_once('../models/dao/ExpensesDAO.class.php');
require_once('../models/dto/Expense.class.php');
require_once('../models/mysql/ExpensesMySqlDAO.class.php');
require_once('../models/mysql/ext/ExpensesMySqlExtDAO.class.php');

//expensecat
require_once('../models/dao/ExpensestypeDAO.class.php');
require_once('../models/dto/Expensestype.class.php');
require_once('../models/mysql/ExpensestypeMySqlDAO.class.php');
require_once('../models/mysql/ext/ExpensestypeMySqlExtDAO.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');

//Save
require_once('../models/dao/SaveDAO.class.php');
require_once('../models/dto/Save.class.php');
require_once('../models/mysql/SaveMySqlDAO.class.php');
require_once('../models/mysql/ext/SaveMySqlExtDAO.class.php');
//Savedaily
require_once('../models/dao/SavedailyDAO.class.php');
require_once('../models/dto/Savedaily.class.php');
require_once('../models/mysql/SavedailyMySqlDAO.class.php');
require_once('../models/mysql/ext/SavedailyMySqlExtDAO.class.php');

//Usergroup
require_once('../models/dao/UsergroupDAO.class.php');
require_once('../models/dto/Usergroup.class.php');
require_once('../models/mysql/UsergroupMySqlDAO.class.php');
require_once('../models/mysql/ext/UsergroupMySqlExtDAO.class.php');

//user
require_once('../models/dao/UserDAO.class.php');
require_once('../models/dto/User.class.php');
require_once('../models/mysql/UserMySqlDAO.class.php');
require_once('../models/mysql/ext/UserMySqlExtDAO.class.php');

require_once('../models/dao/CostcenterDAO.class.php');
require_once('../models/dto/Costcenter.class.php');
require_once('../models/mysql/CostcenterMySqlDAO.class.php');
require_once('../models/mysql/ext/CostcenterMySqlExtDAO.class.php');

//get the do the action
$do = $_GET['do'];

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

  Controller Name :- expensesReportController

  OPERTATION in Controller

  1- show report
  2- search by date only or expensesType only or both
  ======================== */

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

//here goes the instances and general variables
//Expensetype
$ExpensetypeDAO = new ExpensestypeMySqlDAO();
$Expensetype = new Expensestype();
$ExpensetypeExt = new ExpensestypeMySqlExtDAO();
//Expenses
$ExpenseDAO = new ExpensesMySqlDAO();
$Expense = new Expense();
$ExpenseExt = new ExpensesMySqlExtDAO();
//save
$Save = new Save();
$SaveDAO = new SaveMySqlDAO();
$SaveExt = new SaveMySqlExtDAO();
//savedaily
$Savedaily = new Savedaily();
$SavedailyDAO = new SavedailyMySqlDAO();
//Usergroup
$Usergroup = new Usergroup();
$UsergroupDAO = new UsergroupMySqlDAO();
$UsergroupExt = new UsergroupMySqlExtDAO();
//User
$user = new User();
$userDAO = new UserMySqlDAO();
$userEX = new UserMySqlExtDAO();

//Costcenter
$Costcenter = new Costcenter();
$CostcenterDAO = new CostcenterMySqlDAO();
$CostcenterEX = new CostcenterMySqlExtDAO();

//Programsetting
$ProgramsettingDAO = new ProgramsettingsMySqlDAO();
$programSettingExt = new ProgramsettingsMySqlExtDAO();

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

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

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

//check and use the condition that suite this action
if (empty($do)) {
    //here the permission check
    include_once("../public/authentication.php");

    $alldata = $ExpensetypeExt->queryAllChildrenExt2();
    $smarty->assign("alldata", $alldata);

    $allCostcenter = $CostcenterEX->queryAllnotdel();
    $smarty->assign("allCostcenter", $allCostcenter);

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

    $expensestypeid = $_REQUEST['expensestypeid'];
    $productid = $_REQUEST['productid'];
    $from = $_REQUEST['from'];
    $to = $_REQUEST['to'];
    $Costcenterid = $_REQUEST['Costcenterid'];


    $message = '';
    $queryString = '';
    if (isset($expensestypeid) && $expensestypeid != "-1") {
        $mytypedata = $ExpensetypeDAO->load($expensestypeid);
        $message .= " عرض مصروفات " . $mytypedata->expensestypename;
        $smarty->assign('message', $message);

        $queryString .= " and expenses.expensestypeid = $expensestypeid ";
    }

    if (isset($from) && $from != "") {
        $message .= " من تاريخ : " . $from;
        $queryString .= " and expenses.expensesdate >= '" . $from . "' ";
    }
    if (isset($to) && $to != "") {
        $message .= "  الى تاريخ  " . $to;
        $queryString .= " and expenses.expensesdate <= '" . $to . "' ";
    }

    if ($productid > 0) {
        $queryString .= " and expenses.productid = $productid ";
    }

    if ($Costcenterid > 0) {
        $queryString .= " and expenses.costcenterid = $Costcenterid ";
    }
    $queryString .= " order by expensesid desc ";


    $expenseData = $ExpenseExt->queryAllQueryString($queryString);


    $sum = 0;
    foreach ($expenseData as $data) {
        if ($data->conditions == 0) {
            $sum += $data->expensesValue;
        }
    }
    $smarty->assign('sum', $sum);
    $smarty->assign('expenseData', $expenseData);

//    if (isset($to) && $to != "" && isset($from) && $from != "" && isset($expensestypeid) && $expensestypeid != "-1") {
//        $mytypedata = $ExpensetypeDAO->load($expensestypeid);
//        $message = " عرض مصروفات " . $mytypedata->expensestypename . " من تاريخ : " . $from . "  الى تاريخ  " . $to;
//        $smarty->assign('message', $message);
//
//        showWithDateAndType($from, $to, $expensestypeid, $productid, $Costcenterid);
//    } else if (isset($expensestypeid) && $expensestypeid != "-1") {
//        $mytypedata = $ExpensetypeDAO->load($expensestypeid);
//        $message = " عرض مصروفات " . $mytypedata->expensestypename;
//        $smarty->assign('message', $message);
//        showwithsearch();
//    } else if (isset($to) && $to != "" && isset($from) && $from != "") {
//        $message = "عرض مصروفات من تاريخ : " . $from . "  الى تاريخ  " . $to;
//        $smarty->assign('message', $message);
//
//        showWithDate($from, $to);
//    } else {
//        show();
//    }

    $smarty->assign("customCheck", 1);
    $smarty->display("expensesReportview/show.html");
} elseif ($do == "showByType") {

    $youtubes = $youtubeLinkDAO->queryAll();
    $smarty->assign("youtubes", $youtubes);
    $alldata = $ExpensetypeExt->queryAllChildrenExt2();
    $smarty->assign("alldata", $alldata);
    $allSaves = $SaveDAO->queryByConditions(0);
    $smarty->assign("allSaves", $allSaves);
    $allUsers = $userDAO->queryByConditions(0);
    $smarty->assign("allUsers", $allUsers);

    $expensestypeid = (int) $_REQUEST['expensestypeid'];
    $saveid = (int) $_REQUEST['saveid'];
    $userid = (int) $_REQUEST['userid'];
    $from = $_REQUEST['from'];
    $to = $_REQUEST['to'];

    $queryString = '';
    if (isset($expensestypeid) && $expensestypeid > 0) {
        $queryString .= " and expensestype.expensestypeid = $expensestypeid ";
    }
    if (isset($saveid) && $saveid > 0) {
        $queryString .= " and expenses.saveid = $saveid ";
    }
    if (isset($userid) && $userid > 0) {
        $queryString .= " and expenses.userid = $userid ";
    }
    if (isset($from) && !empty($from)) {
        $queryString .= " and expenses.expensesdate >= '" . $from . "' ";
    }
    if (isset($to) && !empty($to)) {
        $queryString .= " and expenses.expensesdate <= '" . $to . "' ";
    }

    $results = $ExpensetypeExt->sumOfEachTypeQueryString($queryString);
    $smarty->assign("results", $results);

    $smarty->assign("customCheck", 1);
    $smarty->display("expensesReportview/showbytype.html");
}


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

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

function showwithsearch() {
    global $ExpensetypeExt;
    global $ExpenseDAO;
    global $ExpenseExt;
    global $today;
    global $smarty;

    $expensestypeid = $_REQUEST['expensestypeid'];

    $expenseData = $ExpenseExt->queryByExpensetypeidExt($expensestypeid);
    $sum = 0;
    foreach ($expenseData as $data) {
        $sum += $data->expensesValue;
    }
    $smarty->assign('sum', $sum);
    $smarty->assign('expenseData', $expenseData);
}

//show
function show() {
    global $ExpensetypeExt;
    global $ExpenseDAO;
    global $ExpenseExt;
    global $today;
    global $smarty;

    $expenseData = $ExpenseExt->queryAllexpense();

    $sum = 0;
    foreach ($expenseData as $data) {
        $sum += $data->expensesValue;
    }

    $smarty->assign('sum', $sum);
    $smarty->assign('expenseData', $expenseData);
}

function showWithDate($from, $to) {
    global $ExpensetypeExt;
    global $ExpenseDAO;
    global $ExpenseExt;
    global $today;
    global $smarty;

    $expenseData = $ExpenseExt->queryAllWithDate($from, $to);

    $sum = 0;
    foreach ($expenseData as $data) {
        $sum += $data->expensesValue;
    }
    $smarty->assign('sum', $sum);
    $smarty->assign('expenseData', $expenseData);
}

function showWithDateAndType($from, $to, $expensestypeid, $productid = '', $Costcenterid = '') {
    global $ExpensetypeExt;
    global $ExpenseDAO;
    global $ExpenseExt;
    global $today;
    global $smarty;

    $queryString = '';
    if ($productid > 0) {
        $queryString .= ' and expenses.productid = ' . $productid;
    }

    if ($Costcenterid > 0) {
        $queryString .= ' and expenses.costcenterid = ' . $Costcenterid;
    }



    $expenseData = $ExpenseExt->queryAllWithDateAndType($from, $to, $expensestypeid, $queryString);

    $sum = 0;
    foreach ($expenseData as $data) {
        $sum += $data->expensesValue;
    }
    $smarty->assign('sum', $sum);
    $smarty->assign('expenseData', $expenseData);
}

?>