<?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");
//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');
//Clientdebtchange
require_once('../models/dao/ClientdebtchangeDAO.class.php');
require_once('../models/dto/Clientdebtchange.class.php');
require_once('../models/mysql/ClientdebtchangeMySqlDAO.class.php');
require_once('../models/mysql/ext/ClientdebtchangeMySqlExtDAO.class.php');

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

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

  Controller Name :- clientReportsController

  OPERTATION in Controller

  1- تقرير تحصيل
  ======================== */

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

//here goes the instances and general variables
//Client
$client = new Client();
$clientDAO = new ClientMySqlDAO();
$clientExt = new ClientMySqlExtDAO();
//Clientdebtchange
$clientDeptChange = new Clientdebtchange();
$clientDeptChangeDAO = new ClientdebtchangeMySqlDAO();
$clientDeptChangeExt = new ClientdebtchangeMySqlExtDAO();

$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'];
    if (isset($startDate) && isset($endDate) && $startDate != "" && $endDate != "") {
        $message = "التاريخ: من: " . $startDate . " إلى: " . $endDate;
        $smarty->assign("message", $message);

        show($startDate, $endDate);
    }
    $smarty->display("clientReportsview/show.html");
}
//here the global templates
$smarty->display("footer.html");

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

//show
function show($startDate, $endDate) {
    global $clientDeptChangeExt;
    global $smarty;

    // required connect
    SmartyPaginate::connect();
    // set items per page
    SmartyPaginate::setLimit(25);
    // get the all colums number
    $allColums = $clientDeptChangeExt->queryPayedDeptOnly($startDate, $endDate);
    SmartyPaginate::setTotal(count($allColums));
    //get the selected data with limitation
    $shownData = $clientDeptChangeExt->queryPayedDeptOnlyLimited($startDate, $endDate, SmartyPaginate::getCurrentIndex(), SmartyPaginate::getLimit());

    $sum;
    foreach ($shownData as $data) {
        $sum += $data->clientdebtchangeamount;
    }
    $smarty->assign('sum', $sum);

    // assign db results to the template
    $smarty->assign('shownData', $shownData);
    // assign {$paginate} var
    SmartyPaginate::assign($smarty);
    //text that show in navgation
    SmartyPaginate::setNextText('التالى');
    SmartyPaginate::setPrevText('السابق');
    //add any varaible in url
    SmartyPaginate::setUrl('clientReportsController.php?do=show&from=' . $startDate . '&to=' . $endDate);
}

?>