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

//damagedProduct
require_once('../models/dao/DamagedproductDAO.class.php');
require_once('../models/dto/Damagedproduct.class.php');
require_once('../models/mysql/DamagedproductMySqlDAO.class.php');
require_once('../models/mysql/ext/DamagedproductMySqlExtDAO.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');

//unit
require_once('../models/dao/UnitDAO.class.php');
require_once('../models/dto/Unit.class.php');
require_once('../models/mysql/UnitMySqlDAO.class.php');
require_once('../models/mysql/ext/UnitMySqlExtDAO.class.php');

//productUnit
require_once('../models/dao/ProductunitDAO.class.php');
require_once('../models/dto/Productunit.class.php');
require_once('../models/mysql/ProductunitMySqlDAO.class.php');
require_once('../models/mysql/ext/ProductunitMySqlExtDAO.class.php');

//Programsetting
require_once('../models/dao/ProgramsettingsDAO.class.php');
require_once('../models/dto/Programsetting.class.php');
require_once('../models/mysql/ProgramsettingsMySqlDAO.class.php');
require_once('../models/mysql/ext/ProgramsettingsMySqlExtDAO.class.php');

//Store
require_once('../models/dao/StoreDAO.class.php');
require_once('../models/dto/Store.class.php');
require_once('../models/mysql/StoreMySqlDAO.class.php');
require_once('../models/mysql/ext/StoreMySqlExtDAO.class.php');

//Storereport
require_once('../models/dao/StorereportDAO.class.php');
require_once('../models/dto/Storereport.class.php');
require_once('../models/mysql/StorereportMySqlDAO.class.php');
require_once('../models/mysql/ext/StorereportMySqlExtDAO.class.php');

//Storedetail
require_once('../models/dao/StoredetailDAO.class.php');
require_once('../models/dto/Storedetail.class.php');
require_once('../models/mysql/StoredetailMySqlDAO.class.php');
require_once('../models/mysql/ext/StoredetailMySqlExtDAO.class.php');

//Productionorder
require_once('../models/dao/ProductionorderDAO.class.php');
require_once('../models/dto/Productionorder.class.php');
require_once('../models/mysql/ProductionorderMySqlDAO.class.php');
require_once('../models/mysql/ext/ProductionorderMySqlExtDAO.class.php');

//Productionrate
require_once('../models/dao/ProductionrateDAO.class.php');
require_once('../models/dto/Productionrate.class.php');
require_once('../models/mysql/ProductionrateMySqlDAO.class.php');
require_once('../models/mysql/ext/ProductionrateMySqlExtDAO.class.php');

//Productionrateproduct
require_once('../models/dao/ProductionrateproductDAO.class.php');
require_once('../models/dto/Productionrateproduct.class.php');
require_once('../models/mysql/ProductionrateproductMySqlDAO.class.php');
require_once('../models/mysql/ext/ProductionrateproductMySqlExtDAO.class.php');
/* --------------------------------------------------------------------------------------------------------- */
//get the do the action
$do = $_GET['do'];

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

  Controller Name :- damagedProductController

  OPERTATION in Controller

  1- add damagedProduct
  2- edit damagedProduct
  3- delete damagedProduct

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

//here goes the instances and general variables
//Programsetting
$ProgramsettingDAO = new ProgramsettingsMySqlDAO();

//damagedProduct
$damagedProduct = new Damagedproduct();
$damagedProductDAO = new DamagedproductMySqlDAO();
$damagedProductEx = new DamagedproductMySqlExtDAO();

//product
$product = new Product();
$productDAO = new ProductMySqlDAO();
$productEx = new ProductMySqlExtDAO();

//unit
$unit = new Unit();
$unitDAO = new UnitMySqlDAO();
$unitEx = new UnitMySqlExtDAO();

//Productunit
$myProductunitEx = new ProductunitMySqlExtDAO();

//Storereport
$myStorereport = new Storereport();
$myStorereportRecord = new StorereportMySqlDAO();
$myStorereportEx = new StorereportMySqlExtDAO();

//Storedetail
$myStoredetail = new Storedetail();
$myStoredetailEx = new StoredetailMySqlExtDAO();
$myStoredetailRecord = new StoredetailMySqlDAO();

//Store
$store = new Store();
$storeRecord = new StoreMySqlDAO();

//production Order
$productionOrder = new Productionorder();
$productionOrdereDAO = new ProductionorderMySqlDAO();
$productionOrderEX = new ProductionorderMySqlExtDAO();

//Productionrate
$productionrate = new Productionrate();
$productionrateDAO = new ProductionrateMySqlDAO();
$productionrateEX = new ProductionrateMySqlExtDAO();

//Productionrateproduct
$Productionrateproduct = new Productionrateproduct();
$ProductionrateproductDAO = new ProductionrateproductMySqlDAO();
$ProductionrateproductEX = new ProductionrateproductMySqlExtDAO();


/* --------------------------------------------------------------------------------------------------------- */

if (empty($do)) {

    //here the permission check
    //include_once("../public/authentication.php");
    //Programsettingdata
    $Programsettingdata = $ProgramsettingDAO->load(1);
    $smarty->assign("Programsettingdata", $Programsettingdata);

    //All stores
    $allStores = $storeRecord->queryAll();
    $smarty->assign("allStores", $allStores);

    //production Order
    $id = $_GET['id'];
    $smarty->assign("id", $id);

    $productionOrderData = $productionOrdereDAO->load($id);
    $smarty->assign("productionOrderData", $productionOrderData);

    //final Product Name
    $finalOrder = $productionOrderEX->queryAllWithProductName($id);
    $orderName = $finalOrder[0];
    $smarty->assign("orderName", $orderName);
    //here the smarty templates
    $smarty->display("damagedProductView/add.html");
}

//add
elseif ($do == "add") {
    ////include_once("../public/authentication.php");
    /* try
      { */
    $id = $_POST['id'];
    add();
    header("location:productionOrderController.php?do=recieve&id=$id");

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

//show
elseif ($do == "show") {
    //include_once("../public/authentication.php");
    try {
        $alldamadgedData = $damagedProductEx->queryAllWithStoreName();
        $smarty->assign("alldamadgedData", $alldamadgedData);
        //print_r($alldamadgedData);
        $smarty->display("damagedProductView/show.html");

        /* $productunitData=loadProductUnitWithProductAndUnit($productId,$unitId);
          $productnumber=$productunitData->productnumber;
          $finalquantity=$productQuantity*$productnumber;

          foreach ($alldamadgedData as $damagedProduct)
          {

          //$quant = $damagedProduct->productQuantity;
          $price = $damagedProduct->productBuyPrice;
          //$totalprice = $quant*$price;
          echo "totalprice--".$totalprice."</br>";
          } */

        //header("location:?do=sucess");
    } catch (Exception $e) {
        echo $e;
        // header("location:?do=error");
    }
}

//edit
elseif ($do == "edit") {
    //here the permssion check
    //include_once("../public/authentication.php");
    // here the function that do the action

    try {
        //Programsettingdata
        $Programsettingdata = $ProgramsettingDAO->load(1);
        $smarty->assign("Programsettingdata", $Programsettingdata);

        //All stores
        $allStores = $storeRecord->queryAll();
        $smarty->assign("allStores", $allStores);

        $allData = edit();
        $smarty->assign("allData", $allData);
        //header("location:?do=sucess");
    } catch (Exception $e) {
        header("location:?do=error");
    }
    $smarty->assign("customDamadagProduct", 1);
    $smarty->display("damagedProductView/edit.html");
}

//update
elseif ($do == "update") {
    //here the permssion check
    //include_once("../public/authentication.php");
    // here the function that do the action

    try {
        // here the function that do the action
        update();
        header("location:?do=sucess");
    } catch (Exception $e) {
        header("location:?do=error");
    }
}

// delet
else if ($do == 'delete') {
    //here the permssion check
    //include_once("../public/authentication.php");
    // here the function that do the action

    try {
        // here the function that do the action
        delete();
        header("location:?do=sucess");
    } catch (Exception $e) {
        header("location:?do=error");
    }
} elseif ($do == "sucess") {
    /* $id = $_GET['id'];
      $smarty->assign("id",$id);
      //here the smarty templates
      $smarty->display("productionOrderView/recieve.html"); */
} elseif ($do == "error") {
    //here the smarty templates
    $smarty->display("error.html");
}



//here the global templates
$smarty->assign("customDamadagProduct", 1);
$smarty->display("footer.html");

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

//add

function add() {
    //damadage table
    global $damagedProduct;
    global $damagedProductDAO;
    global $damagedProductEx;
    global $smarty;

    //Storedetail
    global $storedetail;
    global $storedetailEx;
    global $storedetailRecord;

    //Store report
    global $storereport;
    global $storereportRecord;
    global $storereportEx;
    global $qloop;

    //production order
    global $productionOrder;
    global $productionOrdereDAO;
    global $productionOrderEX;

    //Productionrate
    global $productionrate;
    global $productionrateDAO;
    global $productionrateEX;

    //Productionrateproduct
    global $Productionrateproduct;
    global $ProductionrateproductDAO;

    $productId = $_POST['productId'];
    $unitId = $_POST['unitId'];
    $productQuantity = $_POST['productQuantity'];
    $store = $_POST['store'];
    $cost = $_POST['cost'];

    $id = $_POST['id'];
    print_r("id=" . $id);
    $productionOrderData = $productionOrdereDAO->load($id);

    $damagedProduct->productId = $productId;
    $damagedProduct->measurUnitId = $unitId;

    $damagedProduct->productQuantity = $productQuantity;
    $damagedProduct->storeId = $store;
    $damagedProduct->cost = $cost;

    $damagedProduct->operationDate = date("Y-m-d");
    $damagedProduct->userID = $_SESSION['userid'];
    $damagedProduct->productionOrderId = $id;

    //insert in damaged Product table
    $modelId = $damagedProductDAO->insert($damagedProduct);

    /* ------------------------  خصم المواد الخام للمنتج التالف من المخزن ----------------- */
    $productionrate = $productionrateEX->queryOneRateByfinalName($productionOrderData->productId);
    $Productionrateproduct = $ProductionrateproductDAO->queryByProductionRateId($productionrate->productionRateId);
    print_r($productionrate);

    $i = 0;
    foreach ($Productionrateproduct as $p) {

        $productId = $Productionrateproduct[$i]->productId;
        print_r("</br>productId = " . $productId . "</br>");


        $quantity = $Productionrateproduct[$i]->quantity;
        print_r("</br>productQuantity = " . $quantity . "</br>");

        print_r("</br>productQuantity = " . $productQuantity . "</br>");

        $unitId = $Productionrateproduct[$i]->unitId;
        $productunitData = loadProductUnitWithProductAndUnit($productId, $unitId);
        $myproductNumber = $productunitData->productnumber;

        $storeValue = $quantity * $productQuantity * $myproductNumber;
        print_r("</br>storeValue = " . $storeValue . "</br>");

        $storedetailData = getStoredetailData($store, $productId);
        $productquantityBefore = $storedetailData[2];

        print_r("</br>productquantityBefore = " . $productquantityBefore . "</br>");
        //-------------------------update in storedetail tbl----------------------//
        $newQt = $productquantityBefore - $storeValue;

        print_r("</br>newQt = " . $newQt . "</br>");

        updateQuantityOfProduct($store, $productId, $newQt);



        /* ------------------------ insert in store report------------------------------------- */
        //get store productquantity before change
        $storedetailData = getStoredetailData($store, $productId);

        $allStoredetailData = $storedetailData[0];
        $storedetailId = $storedetailData[1];

        $productquantityAfter = $productquantityBefore - $storeValue;
        // print_r("productquantityAfter = ".$productquantityAfter);

        insertStorereport($productId, $store, $productQuantity, $productquantityBefore, $productquantityAfter, 1, $modelId, " اضافه منتج تالف", "damagedProductController.php");


        $i++;
    }
}

//update and decrease Rawmaterialamount in storerawmaterialdetails tbl
function decreaseProductQuantity($storedetailId, $productquantityBefore, $productChangeAmount) {

    //to use the variable out side the funcion
    global $myStoredetail;
    global $myStoredetailEx;


    //increase productquantity
    $productquantityAfter = $productquantityBefore - $productChangeAmount;

    //update productquantity in storedetail tbl
    $myStoredetail->storedetailid = $storedetailId;
    $myStoredetail->userid = $_SESSION['userid'];
    $myStoredetail->storedetaildate = date("Y-m-d");
    $myStoredetail->productquantity = $productquantityAfter;

    $myStoredetailEx->updateProductquantity($myStoredetail);
    // print_r("mmmmmm".$productquantityAfter);
    return $productquantityAfter;
}

/* ------------update store details ------------- */

function updateQuantityOfProduct($storeid, $productid, $productChangeAmount) {
    global $myStoredetailEx;

    $myStoredetailEx->updateStoreDetails($storeid, $productid, $productChangeAmount);
}

/* ----------------------- store report-------------------- */

function insertStorereport($productid, $storeid, $productChangeAmount, $productbefore, $productafter, $storereporttype, $storereportmodelid, $processname, $tablename) {
    global $myStorereport;
    global $myStorereportRecord;

    $myStorereport->processname = $processname;
    $myStorereport->productafter = $productafter;
    $myStorereport->productbefore = $productbefore;
    $myStorereport->productquantity = $productChangeAmount;
    $myStorereport->productid = $productid;
    $myStorereport->storeid = $storeid;
    $myStorereport->storereportdate = date("Y-m-d");
    $myStorereport->storereportmodelid = $storereportmodelid;
    $myStorereport->storereporttype = $storereporttype;
    $myStorereport->tablename = $tablename;
    $myStorereport->userid = $_SESSION['userid'];

    $myStorereportRecord->insert($myStorereport);
}

//  return  productquantityBefore
function getStoredetailData($storeid, $productid) {

    //to use the variable out side the funcion
    global $myStoredetailEx;

    //select data from storerawmaterialdetails by storeid and rawmaterialid
    $storedetailData = $myStoredetailEx->queryWithStoreAndProduct($productid, $storeid);


    $storedetailId = $storedetailData->storedetailid;
    $productquantityBefore = $storedetailData->productquantity;
    //print_r("productquantityBefore".$productquantityBefore);

    return array($storedetailData, $storedetailId, $productquantityBefore);
}

//  update store deyails
function increaseProductQuantity($storedetailId, $productquantityBefore, $productChangeAmount) {

    //to use the variable out side the funcion
    global $myStoredetail;
    global $myStoredetailEx;

    //increase productquantity
    $productquantityAfter = $productquantityBefore + $productChangeAmount;

    //update productquantity in storedetail tbl

    $myStoredetail->userid = $_SESSION['userid'];
    $myStoredetail->storedetaildate = date("Y-m-d");

    $myStoredetail->storedetailid = $storedetailId;
    $myStoredetail->productquantity = $productquantityAfter;

    $myStoredetailEx->updateProductquantity($myStoredetail);

    return $productquantityAfter;
}

function loadProductUnitWithProductAndUnit($productId, $unitId) {

    //to use the variable out side the funcion
    global $myProductunitEx;

    // select all data from productunit tbl
    $productunitData = $myProductunitEx->queryWithProductIdAndUnitId($productId, $unitId);

    return $productunitData;
}

//edit
function edit() {
    global $damagedProduct;
    global $damagedProductDAO;
    global $damagedProductEx;


    $id = $_GET["id"];

    $allData = $damagedProductDAO->load($id);
    //print_r($operationRow);
    return $allData;
}

//update
function update() {
    global $damagedProduct;
    global $damagedProductDAO;
    global $damagedProductEx;

    $damagedProductId = $_POST["damagedProductId"];
    $productId = $_POST["productId"];
    $unitId = $_POST["unitId"];
    $store = $_POST["store"];
    //print_r($store);
    $productQuantity = $_POST["productQuantity"];
    $cost = $_POST["cost"];

    //echo "</br>damagedProductId".$damagedProductId."</br>productId".$productId."</br>unitId".$unitId."</br>productQuantity".$productQuantity."</br>cost".$cost."</br>";

    $damagedProduct->damagedProductId = $damagedProductId;
    $damagedProduct->productId = $productId;
    $damagedProduct->measurUnitId = $unitId;
    $damagedProduct->productQuantity = $productQuantity;
    $damagedProduct->cost = $cost;
    $damagedProduct->storeId = $store;

    $damagedProduct->userID = $_SESSION['userid'];
    $damagedProduct->operationDate = date('Y-m-d');

    $damagedProductDAO->update($damagedProduct);

    // print_r($damagedProduct);
}

//delete
function delete() {
    global $damagedProduct;
    global $damagedProductDAO;

    $id = $_GET["id"];

    $damagedProductDAO->delete($id);
}

?>