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

//MInitialmaintenance
require_once('../models/dao/MInitialmaintenanceDAO.class.php');
require_once('../models/dto/MInitialmaintenance.class.php');
require_once('../models/mysql/MInitialmaintenanceMySqlDAO.class.php');
require_once('../models/mysql/ext/MInitialmaintenanceMySqlExtDAO.class.php');
//MInitialmaintenancedetail
require_once('../models/dao/MInitialmaintenancedetailDAO.class.php');
require_once('../models/dto/MInitialmaintenancedetail.class.php');
require_once('../models/mysql/MInitialmaintenancedetailMySqlDAO.class.php');
require_once('../models/mysql/ext/MInitialmaintenancedetailMySqlExtDAO.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');

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



//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');
//Clientdebtchange
require_once('../models/dao/ClientdebtchangeDAO.class.php');
require_once('../models/dto/Clientdebtchange.class.php');
require_once('../models/mysql/ClientdebtchangeMySqlDAO.class.php');
require_once('../models/mysql/ext/ClientdebtchangeMySqlExtDAO.class.php');


//MCheck
require_once('../models/dao/MCheckDAO.class.php');
require_once('../models/dto/MCheck.class.php');
require_once('../models/mysql/MCheckMySqlDAO.class.php');
require_once('../models/mysql/ext/MCheckMySqlExtDAO.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
$mStageProperityDAO = new MStageproperityMySqlDAO();



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

$MInitialNaintenance = new MInitialmaintenance();
$MInitialNaintenanceDAO = new MInitialmaintenanceMySqlDAO();
$MInitialMaintenanceDetail = new MInitialmaintenancedetail();
$MInitialMaintenanceDetailDAO = new MInitialmaintenancedetailMySqlDAO();
$MInitialMaintenanceDetailEX = new MInitialmaintenancedetailMySqlExtDAO();


$MExternalMaintenance = new MExternalmaintenance();
$MExternalMaintenanceDAO = new MExternalmaintenanceMySqlDAO();
$MExternalMaintenanceEX = new MExternalmaintenanceMySqlExtDAO();

$MInsurance = new MInsurance();
$MInsuranceDAO = new MInsuranceMySqlDAO();
$MInsuranceEX = new MInsuranceMySqlExtDAO();

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

$MComreceiptdetailDAO = new MComreceiptdetailMySqlDAO();
$MComreceiptdetailEX = new MComreceiptdetailMySqlExtDAO();

$Client = new Client();
$ClientDAO = new ClientMySqlDAO();

$myClientdebtchange = new Clientdebtchange();
$myClientdebtchangeRecord = new ClientdebtchangeMySqlDAO();

$mCheckDAO = new MCheckMySqlDAO();

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

} elseif ($do == "loadProperities") {

    try {
        $StageProperityIDsArray = '';
        $mStageProperity = $mStageProperityDAO->queryAll();
        foreach ($mStageProperity as $myMStageProperity) {
            $StageProperityIDsArray.=$myMStageProperity->stageProperityId . ',';
        }
        $smarty->assign("StageProperityIDsArray", $StageProperityIDsArray);
        $smarty->assign("mStageProperity", $mStageProperity);
        $smarty->display("checkReportView/StageProperity.html");
    } catch (Exception $e) {
        //header("location:?do=error");
    }
} elseif ($do == "loadCosts") {

    try {
        $MExternalMaintenance = loadCosts();
        $smarty->assign("MExternalMaintenance", $MExternalMaintenance);
        $smarty->display("checkReportView/allCosts.html");
    } catch (Exception $e) {
        //header("location:?do=error");
    }
} elseif ($do == "makeDiscount") {

    try {
        //get new discount
        $discount = $_REQUEST["discount"];
        $ourSerial = $_REQUEST["ourSerial"];
        //get previous discount
        $oldDiscount = $MComreceiptdetailEX->getdiscountByOurserialEX($ourSerial);
        //affect recit total
        $recID = $MComreceiptdetailEX->getReceiptId($ourSerial);
        $data = $MComreceiptDAO->load($recID);
        $total = $data->totalCost;
        $totalCostNew = $total + $oldDiscount - $discount;
        //echo ' $total = '.$total.' $oldDiscount = '.$oldDiscount.' $discount = '.$discount.' $totalCostNew = '.$totalCostNew;
        $MComreceiptEX->increaseComRecIDTotalCost($recID, $totalCostNew);
        //update discount
        $MComreceiptdetailEX->updatediscountByOurSerial($discount, $ourSerial);
    } catch (Exception $e) {
        //header("location:?do=error");
    }
} elseif ($do == "getdDiscount") {

    try {
        $ourSerial = $_REQUEST["ourSerial"];
        $oldDiscount = $MComreceiptdetailEX->getdiscountByOurserialEX($ourSerial);
        echo $oldDiscount;
    } catch (Exception $e) {
        //header("location:?do=error");
    }
} elseif ($do == "insertcheckReport") {
    try {
        insertcheckReport();
    } catch (Exception $e) {
        //header("location:?do=error");
    }
}


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

function loadCosts() {
    //MInsurance
    global $MInsurance;
    global $MInsuranceDAO;
    global $MInsuranceEX;
    //MInitialMaintenanceDetail
    global $MInitialMaintenanceDetail;
    global $MInitialMaintenanceDetailEX;
    //MExternalMaintenance
    global $MExternalMaintenance;
    global $MExternalMaintenanceEX;

    $ourSerial = $_REQUEST["ourSerial"];

    $myInitialMaintenanceCost = 0;
    $InitialMaintenanceClientCost = 0;
    $myExternalMaintenanceCost = 0;

    $shipCostGo = 0;
    $shipCostReturned = 0;
    $insuranceCostReplace = 0;
    $insuranceCostDiscount = 0;
    $insuranceCostMaintenance = 0;
    $itemDiscount = 0;

    $totalOfTotals = 0;
    //shipCostGo//shipCostReturned
    $MInsuranceData = $MInsuranceDAO->queryByOurSerial($ourSerial);

    if (count($MInsuranceData) > 0) {
        foreach ($MInsuranceData as $MInsurance) {
            $shipCostGo += $MInsurance->shipCostGo;
            $shipCostReturned += $MInsurance->shipCostReturned;
            if ($MInsurance->type == 0) {
                $insuranceCostReplace += $MInsurance->insuranceCost;
            }
            if ($MInsurance->type == 1) {
                $insuranceCostDiscount -= $MInsurance->insuranceCost;
            }
            if ($MInsurance->type == 2) {
                $insuranceCostMaintenance += $MInsurance->insuranceCost;
            }
            //if($MInsurance->type==3){$insuranceCostMaintenance += $MInsurance->insuranceCost;}
            $itemDiscount += $MInsurance->insuranceDiscount;
        }
    }
    //myInitialMaintenanceCost
    $MInitialMaintenanceDetailData = $MInitialMaintenanceDetailEX->queryByOurSerialEX($ourSerial);
    if (count($MInitialMaintenanceDetailData) > 0) {
        foreach ($MInitialMaintenanceDetailData as $myMInitialMaintenanceDetail) {
            $myInitialMaintenanceCost+=$myMInitialMaintenanceDetail->propertyValue; ///////////////////////
        }
        //$InitialMaintenanceClientCost = $MInitialMaintenanceDetailData[0]->totalClientCost;
    }
    //myExternalMaintenanceCost
    $MExternalMaintenanceData = $MExternalMaintenanceEX->queryByOurSerialEX($ourSerial);
    if (count($MExternalMaintenanceData) > 0) {
        foreach ($MExternalMaintenanceData as $MyMExternalMaintenanceData) {
            $myExternalMaintenanceCost += $MyMExternalMaintenanceData->supplierCost;
        }
    }



    $totalOfTotals = $myInitialMaintenanceCost + $myExternalMaintenanceCost + $shipCostGo + $shipCostReturned + $insuranceCostReplace - $insuranceCostDiscount + $insuranceCostMaintenance - $itemDiscount;

    $MExternalMaintenance->InitialMaintenanceCost = $myInitialMaintenanceCost;
    $MExternalMaintenance->InitialMaintenanceClientCost = $InitialMaintenanceClientCost;
    $MExternalMaintenance->ExternalMaintenanceCost = $myExternalMaintenanceCost;
    $MExternalMaintenance->shipCostGo = $shipCostGo;
    $MExternalMaintenance->shipCostReturned = $shipCostReturned;
    $MExternalMaintenance->insuranceCostReplace = $insuranceCostReplace;
    $MExternalMaintenance->insuranceCostDiscount = $insuranceCostDiscount;
    $MExternalMaintenance->insuranceCostMaintenance = $insuranceCostMaintenance;
    $MExternalMaintenance->totalOfTotals = $totalOfTotals;
    $MExternalMaintenance->itemDiscount = $itemDiscount;
    return $MExternalMaintenance;
}

function insertcheckReport() {
    global $MComreceiptEX;
    global $MComreceiptDAO;
    global $ClientDAO;
    global $Client;
    global $MComreceipt;
    global $MComreceiptDAO;
    global $mCheckDAO;
    $type = $_REQUEST["type"];
    $ourSerial = $_REQUEST["ourSerial"];
    $detailId = $_REQUEST["detailId"];

    if ($type == 1) {
        $nextStage = $_REQUEST["nextStage"];
        if ($nextStage == 1) {
            //استلام وفحص
            $checkComment = $_REQUEST["checkComment"];
            if (!isset($checkComment) || empty($checkComment)) {
                $checkComment = '';
            }
            $initialCost = $_REQUEST["initialCost"];
            insertProductHistory($ourSerial, 9, 2, $initialCost, $checkComment, 0);

            //صيانة فورية
            $handelReport = $_REQUEST["handelReport"];
            if (!isset($handelReport) || empty($handelReport)) {
                $handelReport = '';
            }
            $totalClientCost = $_REQUEST["totalClientCost"];
            if (!isset($totalClientCost) || empty($totalClientCost)) {
                $totalClientCost = 0;
            }
            $initialMaintenanceId = insertInitialmaintenance($ourSerial, $totalClientCost, $handelReport);

            $StageProperityIDsArray = $_REQUEST["StageProperityIDsArray"];
            $StageProperityIDsArray = rtrim($StageProperityIDsArray, ',');
            $StageProperityIDsArray = explode(",", $StageProperityIDsArray);
            foreach ($StageProperityIDsArray as $myStageProperityIDsArray) {
                $myCheckBox = $_REQUEST["checkboxNo" . $myStageProperityIDsArray];
                $propertyValueEntered = $_REQUEST["cost" . $myStageProperityIDsArray];
                if (!isset($propertyValueEntered) || empty($propertyValueEntered)) {
                    $propertyValueEntered = 0;
                }
                if ($myCheckBox) {
                    insertInitialMaintenanceDetail($initialMaintenanceId, $myStageProperityIDsArray, $propertyValueEntered);
                }
            }

            insertProductHistory($ourSerial, 2, 6, $totalClientCost, $handelReport, 0);
            updateCurrentStage(6, $detailId);
            //لو الفاتورة انتهت اعمل done = 1
            /* $flag=checkIfComReceiptDone($ourSerial);
              if($flag == 0)
              {
              markAsDone($ourSerial);
              } */

            //اثر فى دين العميل
            $result = $MComreceiptEX->getClientIdUsingOurSerial($ourSerial);
            $clientId = $result->clientid;
            $comRecID = $result->id;
            $Client = $ClientDAO->load($clientId);
            $newDebt = $Client->clientdebt + $totalClientCost;
            //3- insert in client debt change
            insertClientdebtchange($clientId, $Client->clientdebt, 0, 0, 'صيانة فورية لمنتج', $comRecID, $newDebt, 'billreceiptController.php', '', 0);
            $Client->clientdebt = $newDebt;
            $ClientDAO->update($Client);

            //اثر فى اجمالى الفاتورة totalCost
            increaseComRecIDTotalCost($comRecID, $totalClientCost);
        } elseif ($nextStage == 2) {
            $supplier = $_REQUEST["supplier"];
            if (isset($supplier) && $supplier != -1) {
                $checkComment = $_REQUEST["checkComment"];
                if (!isset($checkComment) || empty($checkComment)) {
                    $checkComment = '';
                }
                $initialCost = $_REQUEST["initialCost"];
                //insertProductHistory($ourSerial,1,1,$initialCost,$checkComment,0);
                //$supplier = $_REQUEST["supplier"];
                $myDate = $_REQUEST["myDate"];
                if (!isset($myDate) || empty($myDate)) {
                    $myDate = date("Y-m-d");
                }
                insertExternalMaintenance($ourSerial, $supplier, $checkComment, 0, '', '', '', $myDate);
                insertProductHistory($ourSerial, 9, 3, $initialCost, $checkComment, 0);
                updateCurrentStage(3, $detailId);
            }
        } elseif ($nextStage == 3) {
            $checkComment = $_REQUEST["checkComment"];
            $initialCost = $_REQUEST["initialCost"];
            //insertProductHistory($ourSerial,1,1,$initialCost,$checkComment,0);

            insertInsurance($ourSerial, 1, 4, 0, 0, 0, 0, 0, 0, 0, 0, 4, '', '', 0, 0, 0, '', 0);
            insertProductHistory($ourSerial, 9, 4, $initialCost, $checkComment, 0);
            updateCurrentStage(4, $detailId);
        } elseif ($nextStage == 4) {
            $checkComment = $_REQUEST["checkComment"];
            $initialCost = $_REQUEST["initialCost"];
            //insertProductHistory($ourSerial,1,1,$initialCost,$checkComment,1);
            insertProductHistory($ourSerial, 9, 5, $initialCost, $checkComment, 1);
            insertProductHistory($ourSerial, 5, 6, 0, $checkComment, 1);
            updateCurrentStage(6, $detailId);

            /* $flag=checkIfComReceiptDone($ourSerial);
              if($flag == 0)
              {
              markAsDone($ourSerial);
              } */
        }
    } elseif ($type == 2) {
        $delevery = $_REQUEST["delevery"];
        if (isset($delevery) || !empty($delevery)) {
            //تسليم
            if ($delevery == 0) {
                //echo 1;
                $checkComment = $_REQUEST["checkComment"];
                $totalClientCost = $_REQUEST["totalClientCostHandel"];
                $checkId = $_REQUEST["checkId"];
                //echo 'totalClientCost = '.$totalClientCost;
                //1- mark com recit as done
                //2- update client debt
                $result = $MComreceiptEX->getClientIdUsingOurSerial($ourSerial);
                $clientId = $result->clientid;
                $comRecID = $result->id;
                $currentstageId = $result->currentstage;

                $Client = $ClientDAO->load($clientId);
                $newDebt = $Client->clientdebt + $totalClientCost;
                //3- insert in client debt change
                insertClientdebtchange($clientId, $Client->clientdebt, $totalClientCost, 0, 'تسليم منتج تمت صيانته', $comRecID, $newDebt, 'billreceiptController.php', '', $totalClientCost);
                $Client->clientdebt = $newDebt;
                $ClientDAO->update($Client);
                //4- insert in ProductHistory
                insertProductHistory($ourSerial, $currentstageId, 11, $totalClientCost, $checkComment, 0);
                //insertProductHistory($ourSerial,6,11,$totalClientCost,$checkComment,0);
                updateCurrentStage(11, $detailId);

                $flag = checkIfComReceiptDone($ourSerial);
                if ($flag == 0) {
                    markAsDone($ourSerial);
                }

                //del in mcheck
                $mCheckDAO->delete($checkId);
            }
            //مراحل اخرى
            elseif ($delevery == 1) {
                //echo 0;
                $nextStage = $_REQUEST["otherNextStage"];

                $result = $MComreceiptEX->getClientIdUsingOurSerial($ourSerial);
                $currentstageId = $result->currentstage;

                if ($nextStage == 1) {
                    //استلام وفحص
                    $checkComment = $_REQUEST["checkComment"];
                    if (!isset($checkComment) || empty($checkComment)) {
                        $checkComment = '';
                    }
                    $initialCost = $_REQUEST["otherInitialCost"];
                    insertProductHistory($ourSerial, $currentstageId, 9, $initialCost, $checkComment, 0);

                    //صيانة فورية
                    $handelReport = $_REQUEST["otherHandelReport"];
                    if (!isset($handelReport) || empty($handelReport)) {
                        $handelReport = '';
                    }
                    $totalClientCost = $_REQUEST["totalClientCost"];
                    if (!isset($totalClientCost) || empty($totalClientCost)) {
                        $totalClientCost = 0;
                    }
                    $initialMaintenanceId = insertInitialmaintenance($ourSerial, $totalClientCost, $handelReport);


                    $StageProperityIDsArray = $_REQUEST["StageProperityIDsArray"];
                    $StageProperityIDsArray = rtrim($StageProperityIDsArray, ',');
                    $StageProperityIDsArray = explode(",", $StageProperityIDsArray);
                    foreach ($StageProperityIDsArray as $myStageProperityIDsArray) {
                        $myCheckBox = $_REQUEST["checkboxNo" . $myStageProperityIDsArray];
                        $propertyValueEntered = $_REQUEST["cost" . $myStageProperityIDsArray];
                        if (!isset($propertyValueEntered) || empty($propertyValueEntered)) {
                            $propertyValueEntered = 0;
                        }
                        if ($myCheckBox) {
                            insertInitialMaintenanceDetail($initialMaintenanceId, $myStageProperityIDsArray, $propertyValueEntered);
                        }
                    }

                    insertProductHistory($ourSerial, 9, 2, $totalClientCost, $handelReport, 0);
                    insertProductHistory($ourSerial, 2, 6, 0, '', 0);
                    updateCurrentStage(6, $detailId);

                    //لو الفاتورة انتهت اعمل done = 1
                    /* $flag=checkIfComReceiptDone($ourSerial);
                      if($flag == 0)
                      {
                      markAsDone($ourSerial);
                      } */

                    //اثر فى دين العميل
                    $result = $MComreceiptEX->getClientIdUsingOurSerial($ourSerial);
                    $clientId = $result->clientid;
                    $comRecID = $result->id;
                    $Client = $ClientDAO->load($clientId);
                    $newDebt = $Client->clientdebt + $totalClientCost;
                    //3- insert in client debt change
                    insertClientdebtchange($clientId, $Client->clientdebt, 0, 0, 'صيانة فورية لمنتج', $comRecID, $newDebt, 'billreceiptController.php', '', 0);
                    $Client->clientdebt = $newDebt;
                    $ClientDAO->update($Client);

                    //اثر فى اجمالى الفاتورة totalCost
                    increaseComRecIDTotalCost($comRecID, $totalClientCost);
                } elseif ($nextStage == 2) {
                    $supplier = $_REQUEST["supplier"];
                    if (isset($supplier) && $supplier != -1) {
                        $checkComment = $_REQUEST["checkComment"];
                        if (!isset($checkComment) || empty($checkComment)) {
                            $checkComment = '';
                        }
                        $initialCost = $_REQUEST["initialCost"];
                        insertProductHistory($ourSerial, $currentstageId, 9, $initialCost, $checkComment, 0);

                        //$supplier = $_REQUEST["supplier"];
                        $myDate = $_REQUEST["myDate"];
                        if (!isset($myDate) || empty($myDate)) {
                            $myDate = date("Y-m-d");
                        }
                        insertExternalMaintenance($ourSerial, $supplier, $checkComment, 0, '', '', '', $myDate);
                        insertProductHistory($ourSerial, 9, 3, $initialCost, $checkComment, 0);
                        updateCurrentStage(3, $detailId);
                    }
                } elseif ($nextStage == 3) {
                    $checkComment = $_REQUEST["checkComment"];
                    $initialCost = $_REQUEST["initialCost"];
                    insertProductHistory($ourSerial, $currentstageId, 9, $initialCost, $checkComment, 0);

                    insertInsurance($ourSerial, $currentstageId, 4, 0, 0, 0, 0, 0, 0, 0, 0, 4, '', '', 0, 0, 0, '', 0);
                    insertProductHistory($ourSerial, 9, 4, $initialCost, $checkComment, 0);
                    updateCurrentStage(4, $detailId);
                } elseif ($nextStage == 4) {
                    $checkComment = $_REQUEST["checkComment"];
                    $initialCost = $_REQUEST["initialCost"];
                    insertProductHistory($ourSerial, $currentstageId, 9, $initialCost, $checkComment, 1);
                    insertProductHistory($ourSerial, 9, 5, $initialCost, $checkComment, 1);
                    insertProductHistory($ourSerial, 5, 6, 0, $checkComment, 1);
                    updateCurrentStage(6, $detailId);

                    /* $flag=checkIfComReceiptDone($ourSerial);
                      if($flag == 0)
                      {
                      markAsDone($ourSerial);
                      } */
                }
            }
        } else {
            //echo 'f empty';//do nothing
        }
    }
}

function checkIfComReceiptDone($ourSerial) {
    global $MComreceiptdetailEX;

    $id = $MComreceiptdetailEX->getReceiptId($ourSerial);
    $data = $MComreceiptdetailEX->getCurrentStages($id);

    $flag = 0;
    foreach ($data as $myData) {
        if ($myData->currentstage != 11) {
            $flag = 1;
            return $flag;
        }
    }
}

function markAsDone($ourSerial) {
    global $MComreceiptdetailEX;
    global $MComreceiptEX;

    $id = $MComreceiptdetailEX->getReceiptId($ourSerial);
    $MComreceiptEX->markAsDone($id);
}

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

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

function insertInitialmaintenance($ourSerial, $totalClientCost, $comment) {
    global $MInitialNaintenance;
    global $MInitialNaintenanceDAO;

    $MInitialNaintenance->ourSerial = $ourSerial;
    $MInitialNaintenance->totalClientCost = $totalClientCost;
    $MInitialNaintenance->comment = $comment;

    $MInitialNaintenance->initialMaintenanceDate = date("Y-m-d");
    $MInitialNaintenance->userId = $_SESSION['userid'];
    $MInitialNaintenance->branchId = $_SESSION['branchId'];
    $MInitialNaintenance->del = 0;

    $initialMaintenanceId = $MInitialNaintenanceDAO->insert($MInitialNaintenance);
    return $initialMaintenanceId;
}

function insertInitialMaintenanceDetail($initialMaintenanceId, $propertyId, $propertyValue) {
    global $MInitialMaintenanceDetail;
    global $MInitialMaintenanceDetailDAO;

    $MInitialMaintenanceDetail->initialMaintenanceId = $initialMaintenanceId;
    $MInitialMaintenanceDetail->propertyId = $propertyId;
    $MInitialMaintenanceDetail->propertyValue = $propertyValue;

    $MInitialMaintenanceDetail->initialMaintenanceDetailDate = date("Y-m-d");
    $MInitialMaintenanceDetail->userId = $_SESSION['userid'];
    $MInitialMaintenanceDetail->branchId = $_SESSION['branchId'];
    $MInitialMaintenanceDetail->del = 0;

    $MInitialMaintenanceDetailDAO->insert($MInitialMaintenanceDetail);
}

function insertExternalMaintenance($ourSerial, $supplierId, $comment, $externalmaintenanceReturn, $returnComment, $supplierCost, $returnedDate, $myDate) {
    global $MExternalMaintenance;
    global $MExternalMaintenanceDAO;

    $MExternalMaintenance->ourSerial = $ourSerial;
    $MExternalMaintenance->supplierId = $supplierId;
    $MExternalMaintenance->comment = $comment;
    $MExternalMaintenance->externalmaintenanceReturn = $externalmaintenanceReturn;
    $MExternalMaintenance->returnComment = $returnComment;
    $MExternalMaintenance->supplierCost = $supplierCost;
    $MExternalMaintenance->returnedDate = $returnedDate;

    $MExternalMaintenance->externalmaintenanceDate = $myDate; //date("Y-m-d");
    $MExternalMaintenance->branchId = $_SESSION['branchId'];
    $MExternalMaintenance->userId = $_SESSION['userid'];
    $MExternalMaintenance->del = 0;

    $MExternalMaintenanceDAO->insert($MExternalMaintenance);
}

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

    $MInsurance->ourSerial = $ourSerial;
    $MInsurance->stageIdBefore = $stageId;
    $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->insuranceDiscount = $insuranceDiscount;
    $MInsurance->comment = $comment;
    $MInsurance->returned = $returned;

    $MInsurance->insuranceDate = date("Y-m-d");
    $MInsurance->userId = $_SESSION['userid'];
    $MInsurance->branchId = $_SESSION['branchId'];
    $MInsurance->del = 0;

    $MInsuranceDAO->insert($MInsurance);
}

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 updateCurrentStage($currentstage, $id) {
    global $MComreceiptdetailEX;

    $MComreceiptdetailEX->updateCurrentStage($currentstage, $id);
}

// insert data into Clientdebtchange tbl
function insertClientdebtchange($clientid, $clientdebtchangebefore, $clientdebtchangeamount, $clientdebtchangetype, $processname, $clientdebtchangemodelid, $clientdebtchangeafter, $tablename, $comment, $totalOperationCost) {

    //to use the variable out side the funcion
    global $myClientdebtchange;
    global $myClientdebtchangeRecord;

    $myClientdebtchange->clientdebtchangeafter = $clientdebtchangeafter;
    $myClientdebtchange->clientdebtchangeamount = $clientdebtchangeamount;
    $myClientdebtchange->clientdebtchangebefore = $clientdebtchangebefore;
    $myClientdebtchange->clientdebtchangedate = date("Y-m-d");
    $myClientdebtchange->clientdebtchangemodelid = $clientdebtchangemodelid;
    $myClientdebtchange->clientdebtchangetype = $clientdebtchangetype;
    $myClientdebtchange->clientid = $clientid;
    $myClientdebtchange->comment = $comment;
    $myClientdebtchange->processname = $processname;
    $myClientdebtchange->tablename = $tablename;
    $myClientdebtchange->userid = $_SESSION['userid'];
    $myClientdebtchange->totalOperationCost = $totalOperationCost;

    $myClientdebtchangeRecord->insert($myClientdebtchange);
}

?>