<?php

session_start();
ob_start();
$do = isset($_GET['do']) ? $_GET['do'] : "";
include("../public/impOpreation.php");

include_once("../public/config.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");

$smarty->display("header.html");

$today = date("Y-m-d");
if (empty($do)) {
    $smarty->display("updatePro.html");
} elseif ($do == "add") {
    try {
        $proId = filter_input(INPUT_POST, 'proId');
        $oldproId = filter_input(INPUT_POST, 'oldproId');
        echo "proId: ".$proId." oldproId: ".$oldproId.'<br/>';
        R::debug(true);
        if($proId && $oldproId){
            R::exec("UPDATE product SET productId = $proId WHERE productId = $oldproId");
            R::exec("UPDATE storedetail SET productid = $proId WHERE productid = $oldproId");
            R::exec("UPDATE buyandruternbilldetail SET buybilldetailproductid = $proId WHERE buybilldetailproductid = $oldproId");
            R::exec("UPDATE buybilldetail SET buybilldetailproductid = $proId WHERE buybilldetailproductid = $oldproId");
            R::exec("UPDATE buypriceshistorybook SET productid = $proId WHERE productid = $oldproId");
            R::exec("UPDATE productunit SET productid = $proId WHERE productid = $oldproId");
            R::exec("UPDATE quickprofitproduct SET productId = $proId WHERE productId = $oldproId");
            R::exec("UPDATE returnbuybilldetail SET returnbuybilldetailproductid = $proId WHERE returnbuybilldetailproductid = $oldproId");
            R::exec("UPDATE returnsellbilldetail SET returnsellbilldetailproductid = $proId WHERE returnsellbilldetailproductid = $oldproId");
            R::exec("UPDATE sellandruternbilldetail SET sellbilldetailproductid = $proId WHERE sellbilldetailproductid = $oldproId");
            R::exec("UPDATE sellbilldetail SET sellbilldetailproductid = $proId WHERE sellbilldetailproductid = $oldproId");
            R::exec("UPDATE sellbilldetailoffer SET sellbilldetailproductid = $proId WHERE sellbilldetailproductid = $oldproId");
            R::exec("UPDATE sizecolorstoredetail SET productid = $proId WHERE productid = $oldproId");
            R::exec("UPDATE storemovement SET productid = $proId WHERE productid = $oldproId");
            R::exec("UPDATE storereport SET productid = $proId WHERE productid = $oldproId");
        } else {
            // die();
            header("location:?do=error");
        }
        // die();
        header("location:?do=sucess");
    } catch (Exception $e) {
        echo $e;
        // die();
        header("location:?do=error");
    }
} elseif($do == "show"){
    $smarty->display("updatePro.html");
} elseif ($do == "sucess") {
    //here the smarty templates
    $smarty->display("succes.html");
} elseif ($do == "error") {
    //here the smarty templates
    $smarty->display("error.html");
}

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

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

