<?php

//the global file operation
session_start();
ob_start();
//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");

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

//Clientdebtchange
require_once('../models/dao/ClientdebtchangeDAO.class.php');
require_once('../models/dto/Clientdebtchange.class.php');
require_once('../models/mysql/ClientdebtchangeMySqlDAO.class.php');
require_once('../models/mysql/ext/ClientdebtchangeMySqlExtDAO.class.php');

//Supplier
require_once('../models/dao/SupplierDAO.class.php');
require_once('../models/dto/Supplier.class.php');
require_once('../models/mysql/SupplierMySqlDAO.class.php');
require_once('../models/mysql/ext/SupplierMySqlExtDAO.class.php');

//Supplierdebtchange
require_once('../models/dao/SupplierdebtchangeDAO.class.php');
require_once('../models/dto/Supplierdebtchange.class.php');
require_once('../models/mysql/SupplierdebtchangeMySqlDAO.class.php');
require_once('../models/mysql/ext/SupplierdebtchangeMySqlExtDAO.class.php');

//Productserial
require_once('../models/dao/ProductserialDAO.class.php');
require_once('../models/dto/Productserial.class.php');
require_once('../models/mysql/ProductserialMySqlDAO.class.php');
require_once('../models/mysql/ext/ProductserialMySqlExtDAO.class.php');

//MComreceiptdetail
require_once('../models/dao/MComreceiptdetailDAO.class.php');
require_once('../models/dto/MComreceiptdetail.class.php');
require_once('../models/mysql/MComreceiptdetailMySqlDAO.class.php');
require_once('../models/mysql/ext/MComreceiptdetailMySqlExtDAO.class.php');


require_once('../models/dao/MCheckDAO.class.php');
require_once('../models/dto/MCheck.class.php');
require_once('../models/mysql/MCheckMySqlDAO.class.php');
require_once('../models/mysql/ext/MCheckMySqlExtDAO.class.php');

//MProducthistory
require_once('../models/dao/MProducthistoryDAO.class.php');
require_once('../models/dto/MProducthistory.class.php');
require_once('../models/mysql/MProducthistoryMySqlDAO.class.php');
require_once('../models/mysql/ext/MProducthistoryMySqlExtDAO.class.php');

//MComreceiptimage
require_once('../models/dao/MComreceiptimageDAO.class.php');
require_once('../models/dto/MComreceiptimage.class.php');
require_once('../models/mysql/MComreceiptimageMySqlDAO.class.php');
require_once('../models/mysql/ext/MComreceiptimageMySqlExtDAO.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');



//here goes the instances and general variables
//Client
$Client = new Client();
$ClientDAO = new ClientMySqlDAO();
$ClientEX = new ClientMySqlExtDAO();

//Clientdebtchange
$Clientdebtchange = new Clientdebtchange();
$ClientdebtchangeDAO = new ClientdebtchangeMySqlDAO();
$ClientdebtchangeEX = new ClientdebtchangeMySqlExtDAO();

//Supplier
$Supplier = new Supplier();
$SupplierDAO = new SupplierMySqlDAO();
$SupplierEX = new SupplierMySqlExtDAO();

//Supplierdebtchange
$Supplierdebtchange = new Supplierdebtchange();
$SupplierdebtchangeDAO = new SupplierdebtchangeMySqlDAO();
$SupplierdebtchangeEX = new SupplierdebtchangeMySqlExtDAO();

//Productserial
$Productserial = new Productserial();
$ProductserialDAO = new ProductserialMySqlDAO();
$ProductserialEX = new ProductserialMySqlExtDAO();

//MComreceiptdetail
$MComreceiptdetail = new MComreceiptdetail();
$MComreceiptdetailDAO = new MComreceiptdetailMySqlDAO();
$MComreceiptdetailEX = new MComreceiptdetailMySqlExtDAO();


//MCheck
$MCheck = new MCheck();
$MCheckDAO = new MCheckMySqlDAO();
$MCheckEX = new MCheckMySqlExtDAO();

//MProducthistory
$MProducthistory = new MProducthistory();
$MProducthistoryDAO = new MProducthistoryMySqlDAO();
$MProducthistoryEX = new MProducthistoryMySqlExtDAO();


//MComreceiptimage
$MComreceiptimage = new MComreceiptimage();
$MComreceiptimageDAO = new MComreceiptimageMySqlDAO();
$MComreceiptimageEX = new MComreceiptimageMySqlExtDAO();

//Product
$myProduct = new Product();
$myProductRecord = new ProductMySqlDAO();
$myProductEx = new ProductMySqlExtDAO();

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

} elseif ($do == "add") {
    try {
        $clientData = add();
        $flag = $clientData[0];
        $clientid = $clientData[1];

        if ($flag == '0') {
            $client = getClient(); //undeleted suppliers
            $smarty->assign("client", $client);
            if (isset($clientid)) {
                $choosenclient = $ClientDAO->load($clientid);
                $smarty->assign("choosenclient", $choosenclient);
            }
            $smarty->display("billreceiptview/add/clients.html");
        } else {
            echo $flag;
        }
    } catch (Exception $e) {
        //header("location:?do=error");
    }
} elseif ($do == "addsupplier") {
    $i = $_GET["itr"];
    try {
        $supplierData = addSupplier();
        $flag = $supplierData[0];
        $supplierid = $supplierData[1];

        if ($flag == '0') {
            $supplier = getSupplier(); //undeleted suppliers
            $smarty->assign("supplier", $supplier);
            if (isset($supplierid)) {
                $choosensupplier = $SupplierDAO->load($supplierid);
                $smarty->assign("choosensupplier", $choosensupplier);
            }
            $smarty->assign("i", $i);
            $smarty->display("billreceiptview/add/suppliers.html");
        } else {
            echo $flag;
        }
    } catch (Exception $e) {
        //header("location:?do=error");
    }
} elseif ($do == "addcompanysupplier") {
    $i = $_GET["itr"];
    try {
        $supplierData = addSupplier();
        $flag = $supplierData[0];
        $supplierid = $supplierData[1];

        if ($flag == '0') {
            $supplier = getSupplier(); //undeleted suppliers
            $smarty->assign("supplier", $supplier);
            if (isset($supplierid)) {
                $choosensupplier = $SupplierDAO->load($supplierid);
                $smarty->assign("choosensupplier", $choosensupplier);
            }
            $smarty->assign("i", $i);
            $smarty->display("billreceiptview/add/supplier1.html");
        } else {
            echo $flag;
        }
    } catch (Exception $e) {
        //header("location:?do=error");
    }
} elseif ($do == "getallsupplier") {
    $SupplierData = getallsupplier();
    print_r($SupplierData);
    $smarty->assign("SupplierData", $SupplierData);
} elseif ($do == "getserial") {
    $ourserial = $_GET['ourserial'];
    $updatevalue = detail($ourserial);

    echo $updatevalue;
} elseif ($do == "getallproductserial") {
    $productid = $_GET['productid'];
    $serial = $_GET['serial'];
    $i = $_GET["no"];
    $productData = getbuyandsellbill($productid, $serial);

    if ($productData->billid != "") {
        $suppliername = getsuppliername($productData->billid);
    }
    $text = $suppliername->suppliername;
    $id = $suppliername->supplierid;
    $enddate = $productData->enddate; // or your date as well
    $startdate = $productData->startdate;
    $datediff = abs(strtotime($enddate) - strtotime($startdate));
    $days = floor($datediff / (60 * 60 * 24));

    if ($productData->expireDate > $days) {
        $status = 0;
    } elseif ($productData->expireDate < $days) {
        $status = 1;
    } else {
        $status = "";
    }
    $datakkl = array($productData->sellbillid, $productData->billid
        , $productData->sellbilldate, $productData->buybilldate, $productData->type
        , $productData->don, $productData->expireDate, $status, $id, $text);
    echo (json_encode($datakkl));


    //$smarty->display("billreceiptview/add/buybill.html");
} elseif ($do == "insertimage") {
    $image = $_GET['imageval'];
    $receptnumber = $_GET['receptnumber'];
    $imagedata = insertimage($image, $receptnumber);

    echo $imagedata;
} elseif ($do == "updateimage") {
    $image = $_GET['imageval'];
    $receptnumber = $_GET['receptnumber'];
    $imagedata = updateimage($image, $receptnumber);

    echo $imagedata;
}



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

function getClient() {
    global $ClientDAO;
    $clientsData = $ClientDAO->queryByCondition(0);
    return $clientsData;
}

function getSupplier() {
    global $SupplierDAO;
    $supplierData = $SupplierDAO->queryByCondition(0);
    return $supplierData;
}

function add() {
    global $Client;
    global $ClientDAO;
    $flag = '';

    $clientname = $_REQUEST['newclientName'];
    $clientdebt = $_REQUEST['newclientdebt'];
    $clientaddress = $_REQUEST['clientaddress'];
    $clientphone = $_REQUEST['clientphone'];
    $clientmobile = $_REQUEST['clientmobile'];
    $clientdetails = $_REQUEST['clientdetails'];

    if (!empty($clientname)) {
        if (count($ClientDAO->queryByClientname($clientname)) == 0) {
            $Client->clientname = $clientname;
            $Client->clientaddress = $clientaddress;
            $Client->clientphone = $clientphone;
            $Client->clientmobile = $clientmobile;

            if (isset($clientdebt) && !empty($clientdebt)) {
                $Client->clientdebt = $clientdebt;
            } else {
                $Client->clientdebt = 0;
            }

            $Client->conditions = 0;
            $Client->userid = $_SESSION['userid'];
            $Client->clientdetails = $clientdetails;
            $Client->clientdate = date("Y-m-d");
            $Client->branchId = $_SESSION['branchId'];

            $clientid = $ClientDAO->insert($Client);
            AddClientDebtChange($clientdebt, "إضافة عميل جديد", $clientdebt, $clientid, $clientdetails);

            $flag = 0;
        } else {
            $flag = 1;
        }
    } else {
        $flag = 2;
    }
    return array($flag, $clientid);
}

function AddClientDebtChange($payed, $processname, $clientdebtchangeafter, $clientid, $comment) {
    //Supplierdebtchange
    global $Clientdebtchange;
    global $ClientdebtchangeDAO;

    $Clientdebtchange->processname = $processname;
    $Clientdebtchange->totalOperationCost = $payed;
    $Clientdebtchange->clientdebtchangeafter = $clientdebtchangeafter;
    $Clientdebtchange->clientdebtchangeamount = $payed;
    $Clientdebtchange->clientdebtchangebefore = 0;
    $Clientdebtchange->clientdebtchangedate = date("Y-m-d");
    $Clientdebtchange->tablename = "clientController.php";
    $Clientdebtchange->clientdebtchangetype = 0;
    $Clientdebtchange->clientid = $clientid;
    $Clientdebtchange->clientdebtchangemodelid = $clientid;
    $Clientdebtchange->comment = $comment;
    $Clientdebtchange->userid = $_SESSION['userid'];

    //$supplierDebtChange->comment; //allow null

    $ClientdebtchangeDAO->insert($Clientdebtchange);
}

//add supplier
function addSupplier() {
    global $Supplier;
    global $SupplierDAO;
    $flag = '';

    $suppliername = $_REQUEST['newsupplierName'];
    $suppliercurrentDebt = $_REQUEST['newsupplierdebt'];
    $supplieraddress = $_REQUEST['supplieraddress'];
    $supplierphone = $_REQUEST['supplierphone'];
    $supplierdetails = $_REQUEST['supplierdetails'];

    if (!empty($suppliername)) {
        if (count($SupplierDAO->queryBySuppliername($suppliername)) == 0) {
            $Supplier->suppliername = $suppliername;
            $Supplier->supplieraddress = $supplieraddress;
            $Supplier->supplierphone = $supplierphone;
            $Supplier->supplierdetails = $supplierdetails;

            if (isset($suppliercurrentDebt) && !empty($suppliercurrentDebt)) {
                $Supplier->suppliercurrentDebt = $suppliercurrentDebt;
            } else {
                $Supplier->suppliercurrentDebt = 0;
            }

            $Supplier->conditions = 0;
            $Supplier->userid = $_SESSION['userid'];
            $Supplier->supplierdate = date("Y-m-d");


            $supplierid = $SupplierDAO->insert($Supplier);
            AddSupplierDebtChange($suppliercurrentDebt, "إضافة عميل مورد", $suppliercurrentDebt, $supplierid, $supplierdetails);

            $flag = 0;
        } else {
            $flag = 1;
        }
    } else {
        $flag = 2;
    }
    return array($flag, $supplierid);
}

function AddSupplierDebtChange($payed, $processname, $supplierdebtchangeafter, $supplierid, $comment) {
    //Supplierdebtchange
    global $Supplierdebtchange;
    global $SupplierdebtchangeDAO;

    $Supplierdebtchange->processname = $processname;
    $Supplierdebtchange->supplierdebtchangeafter = $supplierdebtchangeafter;
    $Supplierdebtchange->supplierdebtchangeamount = $payed;
    $Supplierdebtchange->supplierdebtchangebefore = 0;
    $Supplierdebtchange->supplierdebtchangedate = date("Y-m-d");
    $Supplierdebtchange->tablename = "supplierController.php";
    $Supplierdebtchange->supplierdebtchangetype = 0;
    $Supplierdebtchange->supplierid = $supplierid;
    $Supplierdebtchange->supplierdebtchangemodelid = $supplierid;
    $Supplierdebtchange->comment = $comment;
    $Supplierdebtchange->userid = $_SESSION['userid'];

    //$supplierDebtChange->comment; //allow null

    $SupplierdebtchangeDAO->insert($Supplierdebtchange);
}

function getallsupplier() {
    global $SupplierDAO;


    $row_array = array();
    $return_arr = array();

    $name = $_GET['term']; //It could be product name or category name
    $limit = intval($_GET['page_limit']);



    $SupplierData = $SupplierDAO->queryByCondition(0);
    if (count($SupplierData) > 0) {
        $row_array['id'] = -2;
        $row_array['text'] = 'اضافه جديد';
        array_push($return_arr, $row_array);
        foreach ($SupplierData as $pro) {

            //send product id and product name as something that select2 expects.
            $row_array['id'] = $pro->supplierid;
            $row_array['text'] = $pro->suppliername;


            array_push($return_arr, $row_array);
            //جملة
        }
    }

    //echo $return_arr;
    echo json_encode($return_arr);
}

function getbuyandsellbill($id, $serial) {
    global $ProductserialEX;

    $productData = $ProductserialEX->queryallbuyandsellbill($id, $serial);

    return $productData;
}

function getsuppliername($buybillid) {
    global $SupplierEX;

    $suppliername = $SupplierEX->getSuppliernamefrombuybill($buybillid);
    return $suppliername;
}

function detail($ourserial) {
    global $MComreceiptdetailEX;
    global $MCheckEX;
    global $MProducthistoryEX;


    $updatevalue = $MComreceiptdetailEX->updatedel(1, $ourserial);
    $updatevalue = $MProducthistoryEX->updatehistorydel(1, $ourserial);
    $updatevalue = $MCheckEX->updatecheckdel(1, $ourserial);

    return $updatevalue;
}

function insertimage($image, $receptnumber) {
    global $MComreceiptimage;
    global $MComreceiptimageDAO;

    $MComreceiptimage->comreceiptid = $receptnumber;
    $MComreceiptimage->receiptimage = $image;
    $MComreceiptimage->imagedate = date("Y-m-d");
    $MComreceiptimage->userid = $_SESSION['userid'];


    $id = $MComreceiptimageDAO->insert($MComreceiptimage);
}

function updateimage($image, $receptnumber) {
    global $MComreceiptimage;
    global $MComreceiptimageEX;

    $MComreceiptimage->comreceiptid = $receptnumber;
    $MComreceiptimage->receiptimage = $image;
    $MComreceiptimage->imagedate = date("Y-m-d");
    $MComreceiptimage->userid = $_SESSION['userid'];


    $id = $MComreceiptimageEX->updateimage($MComreceiptimage);
}

?>