<?php

//the global file operation
include("../public/impOpreation.php");

// get the config file
include_once("../public/config.php");

//here the db files that include in the file
include("../public/include_dao.php");

//Billproperty
require_once('../models/dao/BillpropertyDAO.class.php');
require_once('../models/dto/Billproperty.class.php');
require_once('../models/mysql/BillpropertyMySqlDAO.class.php');
require_once('../models/mysql/ext/BillpropertyMySqlExtDAO.class.php');
//Billsetting
require_once('../models/dao/BillsettingsDAO.class.php');
require_once('../models/dto/Billsetting.class.php');
require_once('../models/mysql/BillsettingsMySqlDAO.class.php');
require_once('../models/mysql/ext/BillsettingsMySqlExtDAO.class.php');
//Billname
require_once('../models/dao/BillnameDAO.class.php');
require_once('../models/dto/Billname.class.php');
require_once('../models/mysql/BillnameMySqlDAO.class.php');
require_once('../models/mysql/ext/BillnameMySqlExtDAO.class.php');
//Relusergrouppropertie
require_once('../models/dao/RelusergrouppropertiesDAO.class.php');
require_once('../models/dto/Relusergrouppropertie.class.php');
require_once('../models/mysql/RelusergrouppropertiesMySqlDAO.class.php');
require_once('../models/mysql/ext/RelusergrouppropertiesMySqlExtDAO.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');

//Simplemanufacturesetting
require_once('../models/dao/SimplemanufacturesettingDAO.class.php');
require_once('../models/dto/Simplemanufacturesetting.class.php');
require_once('../models/mysql/SimplemanufacturesettingMySqlDAO.class.php');
require_once('../models/mysql/ext/SimplemanufacturesettingMySqlExtDAO.class.php');
//properities
require_once('../models/dao/PropertiesDAO.class.php');
require_once('../models/dto/Propertie.class.php');
require_once('../models/mysql/PropertiesMySqlDAO.class.php');
require_once('../models/mysql/ext/PropertiesMySqlExtDAO.class.php');

require_once('../models/dao/YoutubeLinkDAO.class.php');
require_once('../models/dto/YoutubeLink.class.php');
require_once('../models/mysql/YoutubeLinkMySqlDAO.class.php');
require_once('../models/mysql/ext/YoutubeLinkMySqlExtDAO.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 :- simpleManufactureSettingsController

  OPERTATION in Controller

  1-
  2-
  3-
  4-

  ======================== */
$userID = $_SESSION['userid'];

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

//here goes the instances and general variables
//Billproperty
$Billproperty = new Billproperty();
$BillpropertyDAO = new BillpropertyMySqlDAO();
$BillpropertyEX = new BillpropertyMySqlExtDAO();

//Billname
$Billname = new Billname();
$BillnameDAO = new BillnameMySqlDAO();
$BillnameEX = new BillnameMySqlExtDAO();

//Billsetting
$Billsetting = new Billsetting();
$BillsettingDAO = new BillsettingsMySqlDAO();
$BillsettingEX = new BillsettingsMySqlExtDAO();

//Relusergrouppropertie
$myRelusergrouppropertie = new Relusergrouppropertie();
$myRelusergrouppropertieRecord = new RelusergrouppropertiesMySqlDAO();
$myRelusergrouppropertieExt = new RelusergrouppropertiesMySqlExtDAO();

//here goes the instances and general variables
$myProperities = new Propertie();
$myProperitiesRecord = new PropertiesMySqlDAO();
$myProperitiesEx = new PropertiesMySqlExtDAO();

//Store
$myStoreRecord = new StoreMySqlDAO();

//Simplemanufacturesetting
$simplesetting = new Simplemanufacturesetting();
$simplesettingDao = new SimplemanufacturesettingMySqlDAO();
$simplesettingExt = new SimplemanufacturesettingMySqlExtDAO();

$youtubeLink = new YoutubeLink();
$youtubeLinkDAO = new YoutubeLinkMySqlDAO();
$youtubeLinkEX = new YoutubeLinkMySqlExtDAO();

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

    $simpleSettingData = $simplesettingDao->load(1);
    $smarty->assign('simpleSettingData', $simpleSettingData);

    $storeData = $myStoreRecord->queryByConditions(0);
    $smarty->assign('storeData', $storeData);


    $youtubes = $youtubeLinkDAO->queryAll();
    $smarty->assign("youtubes", $youtubes);

    $smarty->display("simpleManufactureSettingsView/add.html");
} elseif ($do == "show") {
    header('location:simpleManufactureSettingsController.php');
} elseif ($_GET['do'] == "update") {
    try {

        $storeId = filter_input(INPUT_POST, 'storeid');
        $reducestoreamount = filter_input(INPUT_POST, 'reducestoreamount');
        $Productionequationproducts = filter_input(INPUT_POST, 'manufacturing');
        $orderclient = filter_input(INPUT_POST, 'orderclient');
        $lowOutPriceAlert = filter_input(INPUT_POST, 'lowOutPriceAlert');
        $taskOrderName = filter_input(INPUT_POST, 'taskOrderName');

        $simplesetting = $simplesettingDao->load(1);

        if ($simplesetting->taskOrderName != $taskOrderName) {//name changed update it in menu
            $myProperities = $myProperitiesRecord->load(309);
            switch ($taskOrderName) {
                case 1:
                    $myProperities->propertyname = "طلبية";
                    break;
                default:
                    $myProperities->propertyname = "أمر شغل";
                    break;
            }
            $myProperitiesRecord->update($myProperities);
            $fh = fopen('../temp__cashedheader/cashedheader_' . $_SESSION['dbname'] . '_' . $_SESSION['userid'] . '.html', 'w');
            fclose($fh);
        }

        $simplesetting->storeid = $storeId;
        $simplesetting->reducestoreamount = $reducestoreamount;
        $simplesetting->productsproductionequation = $Productionequationproducts;
        $simplesetting->orderclient = $orderclient;
        $simplesetting->lowOutPriceAlert = $lowOutPriceAlert;
        $simplesetting->taskOrderName = $taskOrderName;

        if ($simplesetting->id)
            $simplesettingDao->update($simplesetting);
        else
            $simplesettingDao->insert($simplesetting);



        $holePartNoOfDigits = (int) $_POST['holePartNoOfDigits'];
        $fractionPartNoOfDigits = (int) $_POST['fractionPartNoOfDigits'];
        if ($holePartNoOfDigits > 0 || $fractionPartNoOfDigits > 0) {
            if (empty($holePartNoOfDigits))
                $holePartNoOfDigits = 8;
            if ($_POST['fractionPartNoOfDigits'] == "")
                $fractionPartNoOfDigits = 2;

            $sum = $holePartNoOfDigits + $fractionPartNoOfDigits;
            $colType = " DECIMAL($sum,$fractionPartNoOfDigits) ";
            //productionrate tables
            $simplesettingExt->runSqlQuery("ALTER TABLE `productionrateproduct` CHANGE `quantity` `quantity` $colType NOT NULL");
            $simplesettingExt->runSqlQuery("ALTER TABLE `taskorder` CHANGE `num` `num` $colType NOT NULL");
            $simplesettingExt->runSqlQuery("ALTER TABLE `taskorderproducts` CHANGE `quantity` `quantity` $colType NOT NULL");
            //productionexecution tables
            $simplesettingExt->runSqlQuery("ALTER TABLE `productionexecution` CHANGE `num` `num` $colType NOT NULL");
            $simplesettingExt->runSqlQuery("ALTER TABLE `productionexecutionoutput` CHANGE `num` `num` $colType NOT NULL");
            $simplesettingExt->runSqlQuery("ALTER TABLE `productionexecutionproduct` CHANGE `quantity` `quantity` $colType NOT NULL, CHANGE `actualquantity` `actualquantity` $colType NOT NULL, CHANGE `quantityUnit` `quantityUnit` $colType NOT NULL, CHANGE `actualUnitVal` `actualUnitVal` $colType NOT NULL");
            $simplesettingExt->runSqlQuery("ALTER TABLE `productionexecutionwaste` CHANGE `num` `num` $colType NOT NULL");
            //store tables
            $simplesettingExt->runSqlQuery("ALTER TABLE `storemovement` CHANGE `transferproductamount` `transferproductamount` $colType NOT NULL");
            $simplesettingExt->runSqlQuery("ALTER TABLE `storedetail` CHANGE `productquantity` `productquantity` $colType NOT NULL");
            $simplesettingExt->runSqlQuery("ALTER TABLE `sizecolorstoredetail` CHANGE `quantity` `quantity` $colType NOT NULL");
            $simplesettingExt->runSqlQuery("ALTER TABLE `storereport` CHANGE `productquantity` `productquantity` $colType NOT NULL, CHANGE `productbefore` `productbefore` $colType NOT NULL, CHANGE `productafter` `productafter` $colType NOT NULL");
            //restaurantrawdestruction
            $simplesettingExt->runSqlQuery("ALTER TABLE `restaurantrawdestruction` CHANGE `quantity` `quantity` $colType NOT NULL");
        }

        header("location:?do=sucess");
    } catch (Exception $e) {
        header("location:?do=error");
        //	echo $e;
    }
} elseif ($_GET['do'] == "sucess") {
    $smarty->display("succes.html");
} elseif ($_GET['do'] == "error") {
    $smarty->display("error.html");
}





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