<?php

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

//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
// GOES HERE ....................
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');

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


//here goes the instances and general variables
//Offerclient
$offerClient = new Offerclient();
$offerClientDAO = new OfferclientMySqlDAO();
$offerClientExt = new OfferclientMySqlExtDAO();

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




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

    $smarty->display("offerclient/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");
    } catch (Exception $e) {
        header("location:?do=error");
    }

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


}elseif ($do == "detail") {
    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("offerclient/detail.html");

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

    $id = filter_input(INPUT_GET, 'id');
    $offerClient =$offerClientDAO->load($id);
    $smarty->assign('offerClient', $offerClient);
//    var_dump($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;
        $offerPro->id = $productid->id;

    }
    $smarty->assign('offerProduct', $offerProduct);

    $smarty->display("offerclient/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('offerclient', 1);

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

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

// add in Storerawmaterial tbl
function add()
{

    global $offerClient;
    global $offerClientDAO;

    global $offerProduct;
    global $offerProductDAO;
    $oldoffer = filter_input(INPUT_POST, 'oldoffer');
    $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;



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

    $id = $offerClientDAO->insert($offerClient);
    $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);


            $offerProduct->clientid = $id;
            $offerProduct->productid = $product;
            $offerProduct->quantity = $quantproduct;
            $offerProduct->storequant = $quantstore;
            $offerProduct->availablequant = $quantavailable;
            $offerProduct->miunquant = $quantminus;
            $offerProduct->price = $price;
            $offerProduct->total = $total;

            $offerProductDAO->insert($offerProduct);

        }

    }

}


    ?>
