<?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
// GOES HERE ....................
include("../public/include_dao.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');

//Shippercompanie
require_once('../models/dao/ShippercompaniesDAO.class.php');
require_once('../models/dto/Shippercompanie.class.php');
require_once('../models/mysql/ShippercompaniesMySqlDAO.class.php');
require_once('../models/mysql/ext/ShippercompaniesMySqlExtDAO.class.php');

//Representativecompany
require_once('../models/dao/RepresentativecompanyDAO.class.php');
require_once('../models/dto/Representativecompany.class.php');
require_once('../models/mysql/RepresentativecompanyMySqlDAO.class.php');
require_once('../models/mysql/ext/RepresentativecompanyMySqlExtDAO.class.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');

//MInsurance
require_once('../models/dao/MInsuranceDAO.class.php');
require_once('../models/dto/MInsurance.class.php');
require_once('../models/mysql/MInsuranceMySqlDAO.class.php');
require_once('../models/mysql/ext/MInsuranceMySqlExtDAO.class.php');


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

$langs = $_SESSION['erp_lang'];
include_once("../views/languages/$langs/success.php");
include_once("../views/languages/$langs/error.php");

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

  Controller Name :- propertiesCTRL

  OPERTATION in Controller

  1-display add form
  2- add in properity tbl
  3- select data from usergroup tbl
  4-add in relusergroupproperties tbl
  5-display sucess or error

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


//here the global templates
$smarty->display("header.html");
//$smarty->display("maintennanceHeader.html");
//$smarty->display("quciktask.html");
//here goes the instances and general variables
$MComReceiptDetailEX = new MComreceiptdetailMySqlExtDAO();

$ShipperCompanieDAO = new ShippercompaniesMySqlDAO();
$RepresentativeCompanyDAO = new RepresentativecompanyMySqlDAO();



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

$MInsurance = new MInsurance();
$MInsuranceDAO = new MInsuranceMySqlDAO();
$MInsuranceEX = new MInsuranceMySqlExtDAO();
//check and use the condetion that suite this action
if (empty($do) || $do == "all") {

    // here the function that do the action
    $comReceiptData = $MComReceiptDetailEX->getProductsInpreparationStage($_SESSION['branchId']);

    $smarty->assign("itr", count($comReceiptData));
    $smarty->assign("comReceiptData", $comReceiptData);

    /* print_r('<pre>');
      print_r($comReceiptData);
      print_r('</pre>'); */


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


    //here the smarty templates
    $smarty->display("preparationView/preparation.html");
} elseif ($do == "insuranceGO") {
    try {
        insuranceGO();
        $note = " تمت العملية بنجاح";
        $url = "preparationController.php?do=all";
        $smarty->assign('urldirect', $url);
        $smarty->assign('msgnote', $note);
        $smarty->display("notes2.html");
    } catch (Exception $e) {
        $note = "لقد حدث خطأ اثناء اجراء العملية";
        $url = "preparationController.php?do=all";
        $smarty->assign('urldirect', $url);
        $smarty->assign('msgnote', $note);
        $smarty->display("notes2.html");
    }
} elseif ($do == "add") {
    /* try
      { */
    add();
    /* header("location:?do=sucess");
      }
      catch(Exception $e)
      {
      header("location:?do=error");
      } */
} elseif ($do == "sucess") {

    //here the smarty templates
    $smarty->display("succes.html");
} elseif ($do == "error") {

    //here the smarty templates
    $smarty->display("error.html");
}






//here the global templates

$smarty->assign("customValidation", 1);
$smarty->assign("GeneralSearch", 1);
$smarty->display("footer.html");

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

function insuranceGO() {
    $itr = $_POST["hidden_itr"];
    for ($i = 1; $i <= $itr; $i++) {
        $thisGo = $_POST["go" . $i];
        $ourserial = $_POST["ourserial" . $i];
        $productSerial = $_POST["productSerial" . $i];
        if (isset($thisGo) && !empty($thisGo) && isset($ourserial) && !empty($ourserial) && isset($productSerial) && !empty($productSerial)) {

            ////////////////////update not insert
            updateInsuranceStages($ourserial, 4, 7);
            insertProductHistory($ourserial, 4, 7, 0, '', 0);
            updateCurrentStage(7, $ourserial);
        }
    }
}

function insertInsurance($ourSerial, $stageIdBefore, $currentStage, $shipNameGo, $shipCompanyGo, $shipPersonalGo, $shipCostGo
, $shipNameReturned, $shipCompanyReturned, $shipPersonalReturned, $shipCostReturned, $type, $oldSN, $newSN, $insuranceCost
, $kindOfInsurance, $insuranceDate, $insuranceDiscount, $comment, $returned) {
    global $MInsurance;
    global $MInsuranceDAO;

    $MInsurance->ourSerial = $ourSerial;
    $MInsurance->stageIdBefore = $stageIdBefore;
    $MInsurance->currentStage = $currentStage;
    $MInsurance->shipNameGo = $shipNameGo;
    $MInsurance->shipCompanyGo = $shipCompanyGo;
    $MInsurance->shipPersonalGo = $shipPersonalGo;
    $MInsurance->shipCostGo = $shipCostGo;
    $MInsurance->shipNameReturned = $shipNameReturned;
    $MInsurance->shipCompanyReturned = $shipCompanyReturned;
    $MInsurance->shipPersonalReturned = $shipPersonalReturned;
    $MInsurance->shipCostReturned = $shipCostReturned;
    $MInsurance->type = $type;
    $MInsurance->oldSN = $oldSN;
    $MInsurance->newSN = $newSN;
    $MInsurance->insuranceCost = $insuranceCost;
    $MInsurance->kindOfInsurance = $kindOfInsurance;
    $MInsurance->insuranceDate = $insuranceDate;
    $MInsurance->insuranceDiscount = $insuranceDiscount;
    $MInsurance->comment = $comment;
    $MInsurance->returned = $returned;

    $MInsurance->branchId = $_SESSION['branchId'];
    $MInsurance->userId = $_SESSION['userid'];
    $MInsurance->del = 0;

    $MInsuranceDAO->insert($MInsurance);
}

function updateCurrentStage($currentstage, $ourserial) {
    global $MComReceiptDetailEX;

    $MComReceiptDetailEX->updateCurrentStageUsingOurSerial($currentstage, $ourserial);
}

function updateInsuranceStages($ourSerial, $stageIdBefore, $currentStage) {
    global $MInsurance;
    global $MInsuranceEX;

    $MInsuranceEX->updateInsuranceCurrentStage($stageIdBefore, $currentStage, $ourSerial);
}

function insertProductHistory($ourSerial, $stageFrom, $stageTo, $cost, $comment, $status) {
    global $MProductHistory;
    global $MProductHistoryDAO;

    $MProductHistory->ourSerial = $ourSerial;
    $MProductHistory->stageFrom = $stageFrom;
    $MProductHistory->stageTo = $stageTo;
    $MProductHistory->cost = $cost;
    $MProductHistory->comment = $comment;
    $MProductHistory->status = $status;

    $MProductHistory->userId = $_SESSION['userid'];
    $MProductHistory->productHistoryDate = date("Y-m-d H:i:s");
    $MProductHistory->branchId = $_SESSION['branchId'];
    $MProductHistory->del = 0;

    $MProductHistoryDAO->insert($MProductHistory);
}

?>