<?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");

//MProducthistory
require_once('../models/dao/MProducthistoryDAO.class.php');
require_once('../models/dto/MProducthistory.class.php');
require_once('../models/mysql/MProducthistoryMySqlDAO.class.php');
require_once('../models/mysql/ext/MProducthistoryMySqlExtDAO.class.php');

//MComreceipt
require_once('../models/dao/MComreceiptDAO.class.php');
require_once('../models/dto/MComreceipt.class.php');
require_once('../models/mysql/MComreceiptMySqlDAO.class.php');
require_once('../models/mysql/ext/MComreceiptMySqlExtDAO.class.php');
//MComreceiptdetail
require_once('../models/dao/MComreceiptdetailDAO.class.php');
require_once('../models/dto/MComreceiptdetail.class.php');
require_once('../models/mysql/MComreceiptdetailMySqlDAO.class.php');
require_once('../models/mysql/ext/MComreceiptdetailMySqlExtDAO.class.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');

//Productserial
require_once('../models/dao/ProductserialDAO.class.php');
require_once('../models/dto/Productserial.class.php');
require_once('../models/mysql/ProductserialMySqlDAO.class.php');
require_once('../models/mysql/ext/ProductserialMySqlExtDAO.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');

//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

$MProductHistory = new MProducthistory();
$MProductHistoryDAO = new MProducthistoryMySqlDAO();
$mProductHistoryEX = new MProducthistoryMySqlExtDAO();

$MComreceipt = new MComreceipt();
$MComreceiptDAO = new MComreceiptMySqlDAO();
$MComreceiptEX = new MComreceiptMySqlExtDAO();

$MComreceiptdetailEX = new MComreceiptdetailMySqlExtDAO();

$Client = new Client();
$ClientDAO = new ClientMySqlDAO();
$ClientEX = new ClientMySqlExtDAO();
$ProductserialEX = new ProductserialMySqlExtDAO();
$ProductEX = new ProductMySqlExtDAO();

//check and use the condetion that suite this action
if (empty($do) || $do == "all") {

    $using = $_POST["using"];
    $type = $_POST["type"];
    $myList = $_POST["myList"];
    $showEnded = $_POST["showEnded"];
    $ourSerials = '';
    $data;
    $query;
    if (isset($showEnded) && !empty($showEnded) && $showEnded == 1) {
        $query = '';
    } else {
        $query = ' AND currentstage != 11 ';
    }
    /* print_r('showEnded='.$showEnded.'</br>');
      print_r('type='.$type.'</br>');
      print_r('myList='.$myList.'</br>'); */
    if (isset($using) && !empty($using) && isset($myList) && !empty($myList)) {
        if ($using == 1) {
            //using Recit Serial
            $data = getSerialsType1($myList, $query);
        }
        if ($using == 2) {
            //using Product Serial
            $data = getSerialsType2($myList, $query);
        }

        if ($using == 3) {
            //using ourSerial
            $data = $myList;
        }

        if ($using == 4 || $using == 5) {
            //using ClientId
            //with ddl i have the id
            $data = getSerialsType4_5($myList, $query);
        }

        /* print_r(gettype($myList)); */
        if (gettype($data) == 'array') {
            foreach ($data as $mydata) {
                //print_r($mydata->ourserial.'</br>');
                $ourSerials.="'" . $mydata->ourserial . "',";
            }
        } else if (gettype($data) == 'string') {
            //print_r($data);
            $ourSerials.="'" . $data . "',";
        }

        $ourSerials = rtrim($ourSerials, ",");


        //print_r($ourSerials);
        if (strlen($ourSerials)) {
            $generalSearchData = $mProductHistoryEX->getGeneralSearchData($ourSerials);

            foreach ($generalSearchData as $myData) {
                $data2 = $MComreceiptdetailEX->getGeneralSearchData2($myData->ourSerial);
                $myData->productserial = $data2->productserial;
                $myData->receiptserial = $data2->receiptserial;
                $myData->productName = $data2->productName;
                $myData->granttype = $data2->granttype;
                $myData->clientname = $data2->clientname;
                $myData->suppliername = $data2->suppliername;
                $myData->title = $data2->title;
            }

            /**/
            $previous = '';
            foreach ($generalSearchData as $myData) {

                if ($previous == $myData->ourSerial) {
                    $previous = $myData->ourSerial;
                    $myData->ourSerial = '';
                } else {
                    $previous = $myData->ourSerial;
                }
            }
            /*             * ***************************************** */
        }
    }

    /* print_r('<pre>');
      print_r($generalSearchData);
      print_r('</pre>'); */
    /* print_r('<pre>');
      print_r('using='.$using);
      print_r('</pre>'); */


    $smarty->assign("title1", 'الصيانة');
    $smarty->assign("title2", ' تقارير');
    $smarty->assign("title3", ' بحث عام الصيانة');
    $smarty->assign("link", '#');
    $smarty->display("maintennanceHeader.html");

    if (!isset($type) || empty($type)) {
        $type = 1;
    }
    $smarty->assign("type", $type);
    $smarty->assign("generalSearchData", $generalSearchData);
    $smarty->display("generalSearchView/generalSearch.html");
}
$smarty->assign("GeneralSearch", 1);
$smarty->display("footer.html");

////****************************  Functions  ********************///

function getSerialsType1($myList, $query) {
    global $MComreceiptEX;
    global $MComreceiptdetailEX;

    $id = $MComreceiptEX->getRecitIdUsingItsSerial($myList);
    $data = $MComreceiptdetailEX->getSerialsWithRectId($id, $query);
    return $data;
}

function getSerialsType2($myList, $query) {
    global $MComreceiptdetailEX;

    $data = $MComreceiptdetailEX->getSerialsWithproductserial($myList, $query);
    return $data;
}

function getSerialsType4_5($myList, $query) {
    global $MComreceiptdetailEX;
    global $MComreceiptEX;

    $data = $MComreceiptdetailEX->getSerialsWithClientId($myList, $query);
    return $data;
}

?>