<?php

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

//global varable
global $showoutside;

//to check if the page from .htacess
//$showoutside = $_GET['sn'];
include_once("../public/config.php");


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

//Offerclient
require_once('../models/dao/OfferclientDAO.class.php');
require_once('../models/dto/Offerclient.class.php');
require_once('../models/mysql/OfferclientMySqlDAO.class.php');
require_once('../models/mysql/ext/OfferclientMySqlExtDAO.class.php');

//Offerproduct
require_once('../models/dao/OfferproductDAO.class.php');
require_once('../models/dto/Offerproduct.class.php');
require_once('../models/mysql/OfferproductMySqlDAO.class.php');
require_once('../models/mysql/ext/OfferproductMySqlExtDAO.class.php');



//Offerorder
require_once('../models/dao/OfferorderDAO.class.php');
require_once('../models/dto/Offerorder.class.php');
require_once('../models/mysql/OfferorderMySqlDAO.class.php');
require_once('../models/mysql/ext/OfferorderMySqlExtDAO.class.php');

//Offerproorder
require_once('../models/dao/OfferproorderDAO.class.php');
require_once('../models/dto/Offerproorder.class.php');
require_once('../models/mysql/OfferproorderMySqlDAO.class.php');
require_once('../models/mysql/ext/OfferproorderMySqlExtDAO.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');

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

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


//Offerclient
$offerClient = new Offerclient();
$offerClientDAO = new OfferclientMySqlDAO();
$offerClientExt = new OfferclientMySqlExtDAO();

//Offerproduct
$offerProduct = new Offerproduct();
$offerProductDAO = new OfferproductMySqlDAO();
$offerProductExt = new OfferproductMySqlExtDAO();

//Offerorder
$offerOrder = new Offerorder();
$offerOrderDAO = new OfferorderMySqlDAO();
$offerOrderExt = new OfferorderMySqlExtDAO();

//Offerproorder
$offerProorder = new Offerproorder();
$offerProorderDAO = new OfferproorderMySqlDAO();
$offerProorderExt = new OfferproorderMySqlExtDAO();

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


//check and use the condetion that suite this action
if (empty($do)) {
    //here the permssion check
    include_once("../public/authentication.php");
    $id = filter_input(INPUT_GET, 'id');
    $offerClient =$offerClientDAO->load($id);
    $smarty->assign('offerClient', $offerClient);
    $client = $clientDAO->load($offerClient->client);
    $smarty->assign('clientname', $client->clientname);
    $offerClientid= $offerClient->id;
    $offerProduct =$offerProductDAO->queryByClientid($offerClientid);
    foreach ($offerProduct as $offerPro) {
        $productid =$ProductDAO->load($offerPro->productid);
        $offerPro->prologo = $productid->logo;
        $offerPro->proname = $productid->productName;
    }
    $smarty->assign('offerProduct', $offerProduct);
    $smarty->display("offerorder/add.html");

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

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


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

} elseif ($do == "show") {
    $allclient =$offerOrderDAO->queryAll();
    foreach ($allclient as $client) {
        $proclient = R::findOne('client', 'clientid = ' . $client->client);
        $client->proclient = $proclient->clientname;
    }
    $smarty->assign('allclient', $allclient);
    $smarty->display("offerorder/show.html");

}elseif ($do == "detail") {
    include_once("../public/authentication.php");
    $id = filter_input(INPUT_GET, 'id');
    $offerOrder =$offerOrderDAO->load($id);
    $smarty->assign('offerClient', $offerOrder);
    $client = $clientDAO->load($offerOrder->client);
    $smarty->assign('clientname', $client->clientname);
    $offerClientid= $offerOrder->id;
    $offerProorder =$offerProorderDAO->queryByClientid($offerClientid);
    foreach ($offerProorder as $offerPro) {
        $productid =$ProductDAO->load($offerPro->productid);
        $offerPro->prologo = $productid->logo;
        $offerPro->proname = $productid->productName;
    }
    $smarty->assign('offerProduct', $offerProorder);
    $smarty->display("offerorder/detail.html");

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

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

    $id = filter_input(INPUT_GET, 'id');
    $offerOrder =$offerOrderDAO->load($id);
    $smarty->assign('offerClient', $offerOrder);
    $client = $clientDAO->load($offerOrder->client);
    $smarty->assign('clientname', $client->clientname);
    $offerClientid= $offerOrder->id;
    $offerProorder =$offerProorderDAO->queryByClientid($offerClientid);
    foreach ($offerProorder as $offerPro) {
        $productid =$ProductDAO->load($offerPro->productid);
        $offerPro->prologo = $productid->logo;
        $offerPro->proname = $productid->productName;
    }
    $smarty->assign('offerProduct', $offerProorder);

    $smarty->display("offerorder/edit.html");
}elseif ($do == "sucess") {
    //here the smarty templates
    $smarty->display("succes.html");
} elseif ($do == "error") {
    //here the smarty templates
    $smarty->display("error.html");
}
$smarty->assign('offerorder', 1);

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

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

// add in Storerawmaterial tbl
function add()
{

    global $offerOrder;
    global $offerOrderDAO;

    global $offerProorder;
    global $offerProorderDAO;

    $oldoffer = filter_input(INPUT_POST, 'oldoffer');
    $oldorder = filter_input(INPUT_POST, 'oldorder');

    $datenow = date("Y-m-d");
    $userid = $_SESSION["userid"];
    $del = 0;
    $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');
    $oldoffer += 0;

    $oldorder += 0;


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

    $id = $offerOrderDAO->insert($offerOrder);
        $itr = filter_input(INPUT_POST, 'itr');
        for ($i = 1; $i <= $itr; $i++) {
            if (isset($_POST['product' . $i]) && !empty($_POST['product' . $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);


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

        }

}



    ?>
