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

//MStageproperity
require_once('../models/dao/MStageproperityDAO.class.php');
require_once('../models/dto/MStageproperity.class.php');
require_once('../models/mysql/MStageproperityMySqlDAO.class.php');
require_once('../models/mysql/ext/MStageproperityMySqlExtDAO.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');

//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');

//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');
//Supplierdebtchange
require_once('../models/dao/SupplierdebtchangeDAO.class.php');
require_once('../models/dto/Supplierdebtchange.class.php');
require_once('../models/mysql/SupplierdebtchangeMySqlDAO.class.php');
require_once('../models/mysql/ext/SupplierdebtchangeMySqlExtDAO.class.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');

//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');

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

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

  Controller Name :- productController

  OPERTATION in Controller

  1- display add form, add supplier cat and supplier with ajax
  2- add in supplier tbl
  2.1- insert into supplier unit tbl
  2.2- insert into storedetail tbl
  2.3- insert new row into storereport tbl
  3- display show and tempdelete
  4- edit supplier
  5- update supplier
  5.1- update supplier tbl with supplierid
  5.2- delete supplier units associated to this supplier from productunit tbl, then insert it again

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

//here goes the instances and general variables

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

$MComreceiptdetailEX = new MComreceiptdetailMySqlExtDAO();

//Supplier
$supplier = new Supplier();
$supplierDAO = new SupplierMySqlDAO();
//Supplierdebtchange
$supplierDebtChange = new Supplierdebtchange();
$supplierDebtChangeDAO = new SupplierdebtchangeMySqlDAO();

$MExternalmaintenance = new MExternalmaintenance();
$MExternalmaintenanceEX = new MExternalmaintenanceMySqlExtDAO();

$MComreceiptEX = new MComreceiptMySqlExtDAO();

//check and use the condition that suite this action
if (empty($do)) {

} elseif ($do == "insertcheckReport") {
    /* try
      { */
    insertcheckReport();
    /* }
      catch(Exception $e)
      {
      //header("location:?do=error");
      } */
}


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

function insertcheckReport() {
    global $supplier;
    global $supplierDAO;
    global $MComreceiptEX;

    $supplierId = $_REQUEST["supplierId"];
    $ourSerial = $_REQUEST["ourSerial"];
    $checkComment = $_REQUEST["checkComment"];
    $supplierCost = $_REQUEST["supplierCost"];
    $externalMintenanceId = $_REQUEST["externalMintenanceId"];

    if (isset($supplierCost) && !empty($supplierCost) && $supplierCost > 0) {
        insertProductHistory($ourSerial, 3, 9, $supplierCost, $checkComment, 0);
        updateCurrentStageUsingOurSerial(9, $ourSerial);
        updateMExternalmaintenance($externalMintenanceId, 1, $checkComment, $supplierCost);
        $supplier = $supplierDAO->load($supplierId);
        $newDebt = $supplier->suppliercurrentDebt + $supplierCost;
        insertSupplierDebtChange($supplierId, $supplier->suppliercurrentDeb, $supplierCost, 0, 'استلام من صيانة خارجية', $externalMintenanceId, $newDebt, 'externalMaintenanceGoReport.php', '');
        $supplier->suppliercurrentDebt = $newDebt;
        $supplierDAO->update($supplier);
        //اثر فى اجمالى الفاتورة totalCost
        $result = $MComreceiptEX->getClientIdUsingOurSerial($ourSerial);
        $clientId = $result->clientid;
        $comRecID = $result->id;
        increaseComRecIDTotalCost($comRecID, $supplierCost);
    }
}

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);
}

function updateCurrentStageUsingOurSerial($currentstage, $ourserial) {
    global $MComreceiptdetailEX;

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

function updateMExternalmaintenance($externalMintenanceId, $externalmaintenanceReturn, $returnComment, $supplierCost) {
    global $MExternalmaintenance;
    global $MExternalmaintenanceEX;

    $MExternalmaintenanceEX->updateMExternalMaintenanceEX($externalMintenanceId, $externalmaintenanceReturn, $returnComment, $supplierCost, date("Y-m-d"));
}

function increaseComRecIDTotalCost($comRecID, $changeAmount) {
    global $MComreceiptEX;

    $totalCost = $MComreceiptEX->getTotalCostComRecIDTotalCost($comRecID);
    $totalCostNew = $totalCost + $changeAmount;
    $MComreceiptEX->increaseComRecIDTotalCost($comRecID, $totalCostNew);
}

// insert data into Clientdebtchange tbl
function insertSupplierDebtChange($supplierId, $supplierDebtChangeBefore, $supplierDebtChangeAmount, $supplierDebtChangeType, $processname, $supplierDebtChangeModelId, $supplierDebtChangeAfter, $tablename, $comment) {
    global $supplierDebtChange;
    global $supplierDebtChangeDAO;

    $supplierDebtChange->comment = $comment;
    $supplierDebtChange->processname = $processname;
    $supplierDebtChange->supplierdebtchangeafter = $supplierDebtChangeAfter;
    $supplierDebtChange->supplierdebtchangeamount = $supplierDebtChangeAmount;
    $supplierDebtChange->supplierdebtchangebefore = $supplierDebtChangeBefore;
    $supplierDebtChange->supplierdebtchangedate = date("Y-m-d");
    $supplierDebtChange->supplierdebtchangemodelid = $supplierDebtChangeModelId;
    $supplierDebtChange->supplierdebtchangetype = $supplierDebtChangeType;
    $supplierDebtChange->supplierid = $supplierId;
    $supplierDebtChange->tablename = $tablename;
    $supplierDebtChange->userid = $_SESSION['userid'];

    $supplierDebtChangeDAO->insert($supplierDebtChange);
}

?>