<?php

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

//global varable
global $showoutside;


include_once("../public/config.php");


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

include_once("dailyentryfun.php");
//Proclientshow
require_once('../models/dao/ProclientshowDAO.class.php');
require_once('../models/dto/Proclientshow.class.php');
require_once('../models/mysql/ProclientshowMySqlDAO.class.php');
require_once('../models/mysql/ext/ProclientshowMySqlExtDAO.class.php');

//Productshow
require_once('../models/dao/ProductshowDAO.class.php');
require_once('../models/dto/Productshow.class.php');
require_once('../models/mysql/ProductshowMySqlDAO.class.php');
require_once('../models/mysql/ext/ProductshowMySqlExtDAO.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');


//Breadcrumb
require_once("../library/breadcrumb.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 :- saveCTRL اضافة وتعديل وعرض الخزن

  OPERTATION in Controller

  1-display add form
  2- add in Save tbl
  3-insert into savedaily tbl
  4-display sucess or error
  5-display show form
  6-edit in Save data

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


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


//here goes the instances and general variables
//Proclientshow
$proclientshow = new Proclientshow();
$proclientshowDAO = new ProclientshowMySqlDAO();
$proclientshowEx = new ProclientshowMySqlExtDAO();



$Productshow= new Productshow();
$ProductshowDAO = new ProductshowMySqlDAO();
$ProductshowEx = new ProductshowMySqlExtDAO();

//Client
$client = new Client();
$clientDAO = new ClientMySqlDAO();
$clientExt = new ClientMySqlExtDAO();

//check and use the condetion that suite this action
if (empty($do)) {
    //here the permssion check
    include_once("../public/authentication.php");




    //here the smarty templates
    $smarty->display("showproducts/add.html");
}elseif ($do == "add") {

    //here the permssion check
    include_once("../public/authentication.php");

//    try {
        // here the function that do the action
        add();
//        header("location:?do=sucess");
//        $smarty->display("showproducts/show.html");
//
//    } catch (Exception $e) {
//        header("location:?do=error");
//    }
} elseif ($do == "show") {
    $allclient =$proclientshowDAO->queryAll();
    foreach ($allclient as $client) {
        $proclient = R::findOne('client', 'clientid = ' . $client->client);
        $client->proclient = $proclient->clientname;
    }

    $smarty->assign('allclient', $allclient);



    $smarty->display("showproducts/show.html");


} elseif ($do == "change") {
    include_once("../public/authentication.php");
    $id = filter_input(INPUT_POST, 'id');
    $name = filter_input(INPUT_POST, 'date');
        if ($name == 1) {
            $proclientshow =$proclientshowDAO->load($id);
            $proclientshow->del = 1;
            $proclientshowDAO->update($proclientshow);
        }else{

            $proclientshow =$proclientshowDAO->load($id);
            $proclientshow->del = 0;
            $proclientshowDAO->update($proclientshow);
        }
        echo 1;

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

    $id = filter_input(INPUT_GET, 'id');
    $allclient =$proclientshowDAO->load($id);
//    $client = $clientDAO->load($allclient->client);
    $smarty->assign('allclient', $allclient);
//    $smarty->assign('clientname', $client->clientname);


    $clientid= $allclient->id;

    $allpro =$ProductshowDAO->queryByClientid($clientid);
    $smarty->assign('allpro', $allpro);
    $smarty->display("showproducts/edit.html");
}



//edit  update
elseif ($do == "update") {

    //here the permssion check
    include_once("../public/authentication.php");
    //
    try {
        // here the function that do the action
        update();
        header("location:?do=sucess");
    } catch (Exception $e) {
        header("location:?do=error");
    }
}





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

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

// add in Storerawmaterial tbl
function add() {
    global $proclientshow;
    global $proclientshowDAO;

    global $Productshow;
    global $ProductshowDAO;


    $datenow= date("Y-m-d");
    $userid =  $_SESSION["userid"];
    $del = 1;
    $client = filter_input(INPUT_POST, 'client');
    $mydate = filter_input(INPUT_POST, 'mydate');
    $alltotal = filter_input(INPUT_POST, 'alltotal');
    $allquantity = filter_input(INPUT_POST, 'allquantity');
    $allquantstore = filter_input(INPUT_POST, 'allquantstore');
    $pricestore = filter_input(INPUT_POST, 'pricestore');
    $allquantavailable = filter_input(INPUT_POST, 'allquantavailable');
    $priceavailable = filter_input(INPUT_POST, 'priceavailable');
    $allquantminus = filter_input(INPUT_POST, 'allquantminus');
    $pricestminus = filter_input(INPUT_POST, 'pricestminus');


    $proclientshow->datenow = $datenow;
    $proclientshow->userid = $userid;
    $proclientshow->del = $del;
    $proclientshow->client = $client;
    $proclientshow->mydate =$mydate;
    $proclientshow->alltotal = $alltotal;
    $proclientshow->allquantity = $allquantity;
    $proclientshow->allquantstore =$allquantstore;
    $proclientshow->pricestore =$pricestore;
    $proclientshow->allquantavailable = $allquantavailable;
    $proclientshow->priceavailable =$priceavailable;
    $proclientshow->allquantmiun = $allquantminus;
    $proclientshow->pricemiun = $pricestminus;

    $id = $proclientshowDAO->insert($proclientshow);

    $itr = filter_input(INPUT_POST, 'itr');
    for ($i = 1; $i <= $itr; $i++) {
        $product = filter_input(INPUT_POST, 'product' . $i);
        $quantproduct = filter_input(INPUT_POST, 'quantity' . $i);
        $quantstore = filter_input(INPUT_POST, 'prostore' . $i);
        $quantavailable = filter_input(INPUT_POST, 'proavailable' . $i);
        $quantminus = filter_input(INPUT_POST, 'prominus' . $i);
        $price = filter_input(INPUT_POST, 'price' . $i);
        $total = filter_input(INPUT_POST, 'total' . $i);


        $Productshow->clientid = $id;
        $Productshow->productid =$product;
        $Productshow->quantity = $quantproduct;
        $Productshow->storequant = $quantstore;
        $Productshow->availablequant = $quantavailable;
        $Productshow->miunquant = $quantminus;
        $Productshow->price =$price;
        $Productshow->total = $total;
        $ProductshowDAO->insert($Productshow);
    }

}

function change() {

    global $proclientshow;
    global $proclientshowDAO;


    $id = filter_input(INPUT_POST, 'id');
    $name = filter_input(INPUT_POST, 'date');
    if ($name == 1) {
        $proclientshow =$proclientshowDAO->load($id);
        $proclientshow->del = 1;
        $proclientshowDAO->update($proclientshow);
    }else{

        $proclientshow =$proclientshowDAO->load($id);
        $proclientshow->del = 0;
        $proclientshowDAO->update($proclientshow);
    }
    echo 1;




}
function edit() {
    global $proclientshow;
    global $proclientshowDAO;

    global $Productshow;
    global $ProductshowDAO;



}

function update() {
    global $proclientshow;
    global $proclientshowDAO;

    global $Productshow;
    global $ProductshowDAO;

    $id = filter_input(INPUT_POST, 'id');


    $datenow= date("Y-m-d");
    $userid =  $_SESSION["userid"];
    $del = 1;
    $client = filter_input(INPUT_POST, 'client');
    $mydate = filter_input(INPUT_POST, 'mydate');
    $alltotal = filter_input(INPUT_POST, 'alltotal');
    $allquantity = filter_input(INPUT_POST, 'allquantity');
    $allquantstore = filter_input(INPUT_POST, 'allquantstore');
    $pricestore = filter_input(INPUT_POST, 'pricestore');
    $allquantavailable = filter_input(INPUT_POST, 'allquantavailable');
    $priceavailable = filter_input(INPUT_POST, 'priceavailable');
    $allquantminus = filter_input(INPUT_POST, 'allquantminus');
    $pricestminus = filter_input(INPUT_POST, 'pricestminus');

    $proclientshow =$proclientshowDAO->load($id);
    $proclientshow->datenow = $datenow;
    $proclientshow->userid = $userid;
    $proclientshow->del = $del;
    $proclientshow->client = $client;
    $proclientshow->mydate =$mydate;
    $proclientshow->alltotal = $alltotal;
    $proclientshow->allquantity = $allquantity;
    $proclientshow->allquantstore =$allquantstore;
    $proclientshow->pricestore =$pricestore;
    $proclientshow->allquantavailable = $allquantavailable;
    $proclientshow->priceavailable =$priceavailable;
    $proclientshow->allquantmiun = $allquantminus;
    $proclientshow->pricemiun = $pricestminus;

    $id = $proclientshowDAO->update($proclientshow);

    $itr = filter_input(INPUT_POST, 'itr');
    for ($i = 1; $i <= $itr; $i++) {
        $product = filter_input(INPUT_POST, 'product' . $i);
        $quantproduct = filter_input(INPUT_POST, 'quantity' . $i);
        $quantstore = filter_input(INPUT_POST, 'prostore' . $i);
        $quantavailable = filter_input(INPUT_POST, 'proavailable' . $i);
        $quantminus = filter_input(INPUT_POST, 'prominus' . $i);
        $price = filter_input(INPUT_POST, 'price' . $i);
        $total = filter_input(INPUT_POST, 'total' . $i);

        $Productshow =$ProductshowDAO->load($id);
        $Productshow->clientid = $id;
        $Productshow->productid =$product;
        $Productshow->quantity = $quantproduct;
        $Productshow->storequant = $quantstore;
        $Productshow->availablequant = $quantavailable;
        $Productshow->miunquant = $quantminus;
        $Productshow->price =$price;
        $Productshow->total = $total;
        $quantproduct +=$quantproduct;

        var_dump($quantproduct);
        $ProductshowDAO->update($Productshow);
    }
}



//function  pdf() {
//
//// create new PDF document
//    $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
//
//// set document information
//    $pdf->SetCreator(PDF_CREATOR);
//    $pdf->SetAuthor('Nicola Asuni');
//    $pdf->SetTitle('TCPDF Example 002');
//    $pdf->SetSubject('TCPDF Tutorial');
//    $pdf->SetKeywords('TCPDF, PDF, example, test, guide');
//
//// remove default header/footer
//    $pdf->setPrintHeader(false);
//    $pdf->setPrintFooter(false);
//
//// set default monospaced font
//    $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
//
//// set margins
//    $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
//
//// set auto page breaks
//    $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
//
//// set image scale factor
//    $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
//
//// set some language-dependent strings (optional)
//    if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
//        require_once(dirname(__FILE__).'/lang/eng.php');
//        $pdf->setLanguageArray($l);
//    }
//
//// ---------------------------------------------------------
//
//// set font
//    $pdf->SetFont('times', 'BI', 20);
//
//// add a page
//    $pdf->AddPage();
//
//// set some text to print
//    $txt = <<<EOD
//TCPDF Example 002
//
//Default page header and footer are disabled using setPrintHeader() and setPrintFooter() methods.
//EOD;
//
//// print a block of text using Write()
//    $pdf->Write(0, $txt, '', 0, 'C', true, 0, false, false, 0);
//
//// ---------------------------------------------------------
//
////Close and output PDF document
//    $pdf->Output('example_002.pdf', 'I');
//
////============================================================+
//// END OF FILE
////========================================
//}

//show


//show
//function delete($bankId) {
//
//    //to use the variable out side the funcion
//    global $myBankRecord;
//    global $myBankaccountRecord;
//
//    //  $bankId = $_GET['bankid'];
//
//    $rowDelData = $myBankRecord->load($bankId);
//    try {
//        if (!empty($bankId)) {
//            $bankaccountData = $myBankaccountRecord->queryByBankid($bankId);
//            if (count($bankaccountData) <= 0) {
//                $bankdeleteValid = 0;
//                $myBankRecord->delete($bankId);
//                delTreeElement($rowDelData->bankname);
//                $note = "success";
//            } else {
//                $bankdeleteValid = 1;
//                $note = "لا يمكن حذف  هذا البنك";
//            }
//        } else {
//            $bankdeleteValid = 2;
//            $note = "حدث خطأ أثناء حذف البنك";
//        }
//    } catch (Exception $e) {
//        $note = "حدث خطأ أثناء حذف البنك";
//    }
//
//
//    return array($note, $bankdeleteValid);
//}
//
////deletetemp Store tbl
//function deletetemp($bankId) {
//    //to use the variable out side the funcion
//    global $myBank;
//    global $myBankEx;
//
//
//    try {
//        if (!empty($bankId)) {
//
//            $myBank->bankdate = date("Y-m-d");
//            $myBank->conditions = 1;
//            $myBank->userid = $_SESSION["userid"];
//            $myBank->bankid = $bankId;
//
//            $myBankEx->updateConditions($myBank);
//
//            $note = "success";
//        } else {
//            $note = "حدث خطأ أثناء إخفاء البنك";
//        }
//    } catch (Exception $e) {
//        $note = "حدث خطأ أثناء إخفاء البنك";
//    }
//
//    return $note;
//}
//
////returndelete Store tbl
//function returndelete($bankId) {
//    //to use the variable out side the funcion
//    global $myBank;
//    global $myBankEx;
//
//
//    try {
//        if (!empty($bankId)) {
//            $myBank->bankdate = date("Y-m-d");
//            $myBank->conditions = 0;
//            $myBank->userid = $_SESSION["userid"];
//            $myBank->bankid = $bankId;
//
//            $myBankEx->updateConditions($myBank);
//
//
//            $note = "success";
//        } else {
//            $note = "حدث خطأ أثناء اظهار البنك";
//        }
//    } catch (Exception $e) {
//        $note = "حدث خطأ أثناء اظهار البنك";
//    }
//
//    return $note;
//}
//
////edit from Store tbl
//function edit() {
//    //to use the variable out side the funcion
//    global $myBankRecord;
//
//    //get Variable
//    $bankId = $_GET['bankid'];
//
//    $loadData = $myBankRecord->load($bankId);
//
//    return $loadData;
//}
//
//function update() {
//    //to use the variable out side the funcion
//    global $myBank;
//    global $myBankRecord;
//    global $accountsTree;
//    global $accountsTreeDAO;
//
//    $bankName = $_POST['bankname'];
//    $bankDetails = $_POST['bankdetails'];
//    $conditions = $_POST['conditions'];
//    $bankId = $_POST['bankid'];
//    $bankDate = $_POST['bankdate'];
//
//
//    //add in bank tbl
//    $myBank->bankdate = $bankDate;
//    $myBank->bankname = $bankName;
//    $myBank->conditions = $conditions;
//    $myBank->userid = $_SESSION["userid"];
//    $myBank->bankid = $bankId;
//
//
//    $oldBank = $myBankRecord->load($bankId);
//    //print_r($oldBank->bankid);
//
//    $oldAccountTree = $accountsTreeDAO->queryByName($oldBank->bankname);
//    //   print_r($oldAccountTree[0]->id);
//    $treeId = $oldAccountTree[0]->id;
//
//    $getRow = $accountsTreeDAO->load($treeId);
//
//    $accountsTree->name = $bankName;
//    $accountsTree->id = $treeId;
//    $accountsTree->del = $getRow->del;
//    $accountsTree->itemfrom = $getRow->itemfrom;
//    $accountsTree->itemtype2 = $getRow->itemtype2;
//    $accountsTree->layingOrder = $getRow->layingOrder;
//    $accountsTree->mydate = $getRow->mydate;
//    $accountsTree->notes = $getRow->notes;
//    $accountsTree->parent = $getRow->parent;
//    $accountsTree->theOrder = $getRow->theOrder;
//    $accountsTree->theValue = $getRow->theValue;
//    $accountsTree->userid = $getRow->userid;
//
//    // print_r($accountsTree);
//
//
//    $myBankRecord->update($myBank, $bankDetails);
//
//    $var = editTreeElement($accountsTree);
//    // print_r($var . 'aaaaaaaaaaaaaaaaaaaaa');
//}
//
//function executeOperation() {
//    //to use the variable out side the funcion
//    global $myBankRecord;
//    global $smarty;
//
//    $outputString;
//
//    $operationType = $_POST['operation'];
//    $smarty->assign("operationType", $operationType);
//
//    $choosedItemArr = $_POST['choosedItem'];
//    //print_r($choosedItemArr);
//    if (count($choosedItemArr) > 0) {
//        foreach ($choosedItemArr as $bankId) {
//
//            //get store name
//            $bankData = $myBankRecord->load($bankId);
//            $bankName = $bankData->bankname;
//
//            if ($operationType == '1') {
//                //tempdelete
//                $note = deletetemp($bankId);
//                if ($note != "success") {
//                    $outputString .= $bankName . ": " . $note . "<br/>";
//                } else {
//                    $outputString .= $bankName . ": " . "تمت العملية بنجاح <br/>";
//                }
//            } elseif ($operationType == "2") {
//                //returndelete
//                $note = returndelete($bankId);
//                if ($note != "success") {
//                    $outputString .= $bankName . ": " . $note . "<br/>";
//                } else {
//                    $outputString .= $bankName . ": " . "تمت العملية بنجاح <br/>";
//                }
//            } elseif ($operationType == "3") {
//                //returndelete
//                $note = delete($bankId);
//                if ($note[0] != "success") {
//                    $outputString .= $bankName . ": " . $note[0] . "<br/>";
//                } else {
//                    $outputString .= $bankName . ": " . "تمت العملية بنجاح <br/>";
//                }
//            }
//        }
//
//        $smarty->assign("outputString", $outputString);
//    }
//}

?>