<?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");
//Sparepart
require_once('../models/dao/SparepartDAO.class.php');
require_once('../models/dto/Sparepart.class.php');
require_once('../models/mysql/SparepartMySqlDAO.class.php');
require_once('../models/mysql/ext/SparepartMySqlExtDAO.class.php');
//Sparepartstore
require_once('../models/dao/SparepartstoreDAO.class.php');
require_once('../models/dto/Sparepartstore.class.php');
require_once('../models/mysql/SparepartstoreMySqlDAO.class.php');
require_once('../models/mysql/ext/SparepartstoreMySqlExtDAO.class.php');
//Sparepartstoredetail
require_once('../models/dao/SparepartstoredetailsDAO.class.php');
require_once('../models/dto/Sparepartstoredetail.class.php');
require_once('../models/mysql/SparepartstoredetailsMySqlDAO.class.php');
require_once('../models/mysql/ext/SparepartstoredetailsMySqlExtDAO.class.php');
//Productrecieve
require_once('../models/dao/ProductrecieveDAO.class.php');
require_once('../models/dto/Productrecieve.class.php');
require_once('../models/mysql/ProductrecieveMySqlDAO.class.php');
require_once('../models/mysql/ext/ProductrecieveMySqlExtDAO.class.php');

require_once('../models/dao/SparepartstorereportDAO.class.php');
require_once('../models/dto/Sparepartstorereport.class.php');
require_once('../models/mysql/SparepartstorereportMySqlDAO.class.php');
require_once('../models/mysql/ext/SparepartstorereportMySqlExtDAO.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');

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

  OPERTATION in Controller

  1-display add form
  2- add in sparepartstoredetail , sparepartstorereport and productrecieve table
  3-display sucess or error
  4-display show
  ======================== */

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

//Sparepart
$sparePart = new Sparepart();
$sparePartDAO = new SparepartMySqlDAO();
$sparePartExt = new SparepartMySqlExtDAO();
//Sparepartstore
$sparePartStore = new Sparepartstore();
$sparePartStoreDAO = new SparepartstoreMySqlDAO();
$sparePartStoreExt = new SparepartstoreMySqlExtDAO();
//Sparepartstoredetail
$sparePartDetail = new Sparepartstoredetail();
$sparePartDetailDAO = new SparepartstoredetailsMySqlDAO();
$sparePartDetailExt = new SparepartstoredetailsMySqlExtDAO();
//Productrecieve
$productRecieve = new Productrecieve();
$productRecieveDAO = new ProductrecieveMySqlDAO();
$productRecieveExt = new ProductrecieveMySqlExtDAO();

$sparePartReport = new Sparepartstorereport();
$sparePartReportDAO = new SparepartstorereportMySqlDAO();
$sparePartReportExt = new SparepartstorereportMySqlExtDAO();

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

    //get spare parts
    $spareParts = getSpareParts();
    $smarty->assign("spareParts", $spareParts);

    //here the smarty templates
    $smarty->display("productRecieveview/add.html");
} elseif ($do == "add") {
    include_once("../public/authentication.php");
    try {
        add();
        header("location:?do=sucess");
    } catch (Exception $e) {
        header("location:?do=error");
    }
} elseif ($do == "show") {
    include_once("../public/authentication.php");
    $sparepartid = $_REQUEST['sparepartid'];
    $mysparePartdata = $sparePartDAO->load($sparepartid);
    //print_r($mysparePartdata);
    $message = "عرض إيصالات صيانه :" . $mysparePartdata->sparepartname;
    $smarty->assign('message', $message);


    if (isset($sparepartid) && $sparepartid != '-1') {
        showWithSparePartId($sparepartid);
    } else {
        show();
    }

    //get spare parts
    $spareParts = getSpareParts();
    $smarty->assign("spareParts", $spareParts);

    $smarty->display("productRecieveview/show.html");

    $smarty->assign("customCheck", 1);
} elseif ($do == "sucess") {
    //here the smarty templates
    $smarty->display("succes.html");
} elseif ($do == "error") {
    //here the smarty templates
    $smarty->display("error.html");
}
$smarty->assign("customValidation", 1);
//here the global templates

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

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

function getSpareParts() {
    global $sparePartDAO;

    $spareParts = $sparePartDAO->queryByConditions(0);
    return $spareParts;
}

function add() {
    global $productRecieve;
    global $productRecieveDAO;
    global $sparePartDetail;
    global $sparePartDetailDAO;
    global $sparePartDetailExt;
    global $sparePartReport;
    global $sparePartReportDAO;

    $recieptnum = $_POST['recieptnum'];
    $clientname = $_POST['clientname'];
    $type = $_POST['type'];
    $sparepartid = $_POST['sparepartid'];
    $cost = $_POST['cost'];
    $partsnumber = $_POST['partsnumber'];

    $productRecieve->clientname = $clientname;
    $productRecieve->cost = $cost;
    $productRecieve->partsnumber = $partsnumber;
    $productRecieve->productrecievedate = date("Y-m-d");
    $productRecieve->recieptnum = $recieptnum;
    $productRecieve->sparepartid = $sparepartid;
    $productRecieve->type = $type;
    $productRecieve->userid = $_SESSION['userid'];

    $productRecieveId = $productRecieveDAO->insert($productRecieve);

    if (isset($productRecieveId)) {
        $partsDetail = $sparePartDetailExt->queryBySparepartidExt($sparepartid);
        if (count($partsDetail) > 0) {
            $sparePartDetail->partquantity = $partsDetail->partquantity - $partsnumber;
            $sparePartDetail->sparepartid = $sparepartid;
            $sparePartDetail->sparepartstoredetailid = $partsDetail->sparepartstoredetailid;

            $sparePartDetailExt->updateQuantity($sparePartDetail);

            //insert into sparepartstorereport
            $sparePartReport->partafter = $partsDetail->partquantity - $partsnumber;
            $sparePartReport->partbefore = $partsDetail->partquantity;
            $sparePartReport->partquantity = $partsnumber;
            $sparePartReport->processname = "استلام منتج";
            $sparePartReport->sparepartid = $sparepartid;
            $sparePartReport->sparepartstoreId = $partsDetail->sparepartstoreId;
            $sparePartReport->storereportdate = date("Y-m-d");
            $sparePartReport->storereportmodelid = $productRecieveId;
            $sparePartReport->storereporttype = 1;
            $sparePartReport->tablename = "productRecieveController.php";
            $sparePartReport->userid = $_SESSION['userid'];

            $sparePartReportDAO->insert($sparePartReport);
        }
    }
}

//show
function show() {
    global $productRecieveDAO;
    global $productRecieveExt;
    global $sparePartDetailDAO;
    global $sparePartDetailExt;
    global $smarty;

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

    //select all data from Supplier tbl
    // get the all colums number
    $allColums = $productRecieveExt->queryAllExt();

    $paginate->setTotal(count($allColums));
    //get the selected data with limitation
    $productsData = $productRecieveExt->queryAllExtLimited($paginate->getCurrentIndex(), $paginate->getLimit());

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

function showWithSparePartId($sparepartid) {
    global $productRecieveDAO;
    global $productRecieveExt;
    global $sparePartDetailDAO;
    global $sparePartDetailExt;
    global $smarty;

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

    //select all data from Supplier tbl
    // get the all colums number
    $allColums = $productRecieveExt->queryWithSparePartIdExt($sparepartid);

    $paginate->setTotal(count($allColums));
    //get the selected data with limitation
    $productsData = $productRecieveExt->queryWithSparePartIdExtLimited($sparepartid, $paginate->getCurrentIndex(), $paginate->getLimit());

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

?>