<?php

//the global file operation
include("../public/impOpreation.php");

// get the config file
include_once("../public/config.php");

//here the db files that include in the file
include("../public/include_dao.php");

//MExternalmaintenance
require_once('../models/dao/MExternalmaintenanceDAO.class.php');
require_once('../models/dto/MExternalmaintenance.class.php');
require_once('../models/mysql/MExternalmaintenanceMySqlDAO.class.php');
require_once('../models/mysql/ext/MExternalmaintenanceMySqlExtDAO.class.php');

//Supplier
require_once('../models/dao/SupplierDAO.class.php');
require_once('../models/dto/Supplier.class.php');
require_once('../models/mysql/SupplierMySqlDAO.class.php');
require_once('../models/mysql/ext/SupplierMySqlExtDAO.class.php');

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

//here the global templates
$smarty->display("header.html");
//$smarty->display("maintennanceHeader.html");
//here goes the instances and general variables
$MExternalmaintenance = new MExternalmaintenance();
$MExternalmaintenanceEX = new MExternalmaintenanceMySqlExtDAO();
$SupplierDAO = new SupplierMySqlDAO();
//check and use the condetion that suite this action
if (empty($do) || $do == "all") {
    showAll();
    $allSuppliers = $SupplierDAO->queryAll();
    $smarty->assign("allSuppliers", $allSuppliers);


    $smarty->assign("title1", 'الصيانة');
    $smarty->assign("title2", 'إدارة الصيانة الخارجية');
    $smarty->assign("title3", 'عرض المنتجات');
    $smarty->assign("link", '#');
    $smarty->display("maintennanceHeader.html");


    $smarty->display("externalMaintenanceGoReportView/externalMaintenanceGoReport.html");
}







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

////****************************  Functions  ********************///
// show the form
function showAll() {
    //global $expensseDAO;
    global $MExternalmaintenanceEX;
    global $smarty;


    $supplierid = $_POST["supplierid"];
    $from = $_POST["from"];
    $to = $_POST["to"];

    $queryString = ' WHERE';

    if (isset($supplierid) && $supplierid != -1) {
        $queryString .= '  m_externalmaintenance.supplierId = ' . $supplierid . ' AND';
    }
    if (isset($from) && !empty($from) && isset($to) && !empty($to)) {
        $queryString .= ' DATE( m_externalmaintenance.externalmaintenanceDate )  >= "' . $from . '" AND DATE( m_externalmaintenance.externalmaintenanceDate ) <= "' . $to . '"';
    }

    //explode string on space character to remove last AND occurence
    $arr = explode(' ', $queryString);
    //print_r($arr);
    if (isset($arr) && count($arr) > 0) {
        $lastWord = end($arr);
        if ($lastWord == 'AND') {//remove it
            array_pop($arr);
            //form the string again
            $queryString = implode(' ', $arr);
        } else if ($lastWord == 'WHERE') {//remove it
            array_pop($arr);
            $queryString = ' ';
        }
    }
    //print_r($queryString);
    if ($queryString == ' ') {
        /* $today=date("Y-m-d");
          $queryString .= ' WHERE DATE( sellbill.sellbillsysdate )  >= "'.$today.'" AND DATE( sellbill.sellbillsysdate ) <= "'.$today.'" and sellbill.deletedsellid = 0 '; */
        $queryString .= ' WHERE externalmaintenanceReturn = 0';
    } else {
        $queryString .= ' AND externalmaintenanceReturn = 0';
    }

    $queryString .= ' AND m_externalmaintenance.del = 0  AND m_comreceiptdetail.currentstage = 3 AND m_externalmaintenance.branchId  = ' . $_SESSION['branchId'];

    $MExternalmaintenanceData = $MExternalmaintenanceEX->queryByExternalmaintenanceReturnEX($queryString);


    $smarty->assign("MExternalmaintenanceData", $MExternalmaintenanceData);










    // required connect
    $paginate = new SmartyPaginate;
    $paginate->connect();
    // set items per page
    $paginate->setLimit(100);

    //select all data from user tbl
    // get the all colums number
    //	$allColums = $expensseDAO->queryAll();
    $paginate->setTotal(count($MExternalmaintenanceData));
    //get the selected data with limitation
    $MExternalmaintenanceData = $MExternalmaintenanceEX->queryByExternalmaintenanceReturnEXWithLimit($queryString, $paginate->getCurrentIndex(), $paginate->getLimit());
    /* print_r('<pre>');
      print_r($MExternalmaintenanceData);
      print_r('</pre>'); */

    // assign your db results to the template
    $smarty->assign("MExternalmaintenanceData", $MExternalmaintenanceData);
    // assign {$paginate} var
    $paginate->assign($smarty);
    //text that show in navgation
    $paginate->setNextText('التالى');
    $paginate->setPrevText('السابق');
    //add any varaible in url
    $paginate->setUrl('externalMaintenanceGoReport.php?do=all');
}

?>