<?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
require_once('../models/dao/YoutubeLinkDAO.class.php');
require_once('../models/dto/YoutubeLink.class.php');
require_once('../models/mysql/YoutubeLinkMySqlDAO.class.php');
require_once('../models/mysql/ext/YoutubeLinkMySqlExtDAO.class.php');


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

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


//get the do the action
$do = $_GET['do'];

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

  Controller Name :- productController

  OPERTATION in Controller

  1- display add form, add supplier cat and supplier with ajax
  2- add in supplier tbl
  2.1- insert into supplier unit tbl
  2.2- insert into storedetail tbl
  2.3- insert new row into storereport tbl
  3- display show and tempdelete
  4- edit supplier
  5- update supplier
  5.1- update supplier tbl with supplierid
  5.2- delete supplier units associated to this supplier from productunit tbl, then insert it again

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

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

//here goes the instances and general variables
//Accountstree
$accountsTree = new Accountstree();
$accountsTreeDAO = new AccountstreeMySqlDAO();
$accountsTreeEX = new AccountstreeMySqlExtDAO();

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

//Programsetting
$Programsetting = new Programsetting();
$ProgramsettingDAO = new ProgramsettingsMySqlDAO();
$ProgramsettingEX = new ProgramsettingsMySqlExtDAO();


//Dailyentry
$dailyEntry = new Dailyentry();
$dailyEntryDAO = new DailyentryMySqlDAO();
$dailyEntryEX = new DailyentryMySqlExtDAO();

//Dailyentrycreditor دائن
$dailyEntryCreditor = new Dailyentrycreditor();
$dailyEntryCreditorDAO = new DailyentrycreditorMySqlDAO();
$dailyEntryCreditorEX = new DailyentrycreditorMySqlExtDAO();

//Dailyentrydebtor مدين
$dailyEntryDebtor = new Dailyentrydebtor();
$dailyEntryDebtorDAO = new DailyentrydebtorMySqlDAO();
$dailyEntryDebtorEX = new DailyentrydebtorMySqlExtDAO();

$today = date("Y-m-d");

$Programsettingdata = $ProgramsettingDAO->load(1);
$smarty->assign("Programsettingdata", $Programsettingdata);


/* * * classes ** */
$rentProducts = new Rentproduct();
$myrentProducts = new RentproductMySqlDAO();
$extrentProducts = new RentproductMySqlExtDAO();

$rentStore = new Rentstore();
$myrentStore = new RentstoreMySqlDAO();
$extrentStore = new RentstoreMySqlExtDAO();

$supplier = new Supplier();
$mysupplier = new SupplierMySqlDAO();
$extsupplier = new SupplierMySqlExtDAO();

$save = new Save();
$mysave = new SaveMySqlDAO();
$extsave = new SaveMySqlExtDAO();

$Savedaily = new Savedaily();
$SavedailyDAO = new SavedailyMySqlDAO();

$client = new Client();
$myclient = new ClientMySqlDAO();
$extclient = new ClientMySqlExtDAO();

$rentbill = new Rentbill();
$myrentbill = new RentbillMySqlDAO();
$extrentbill = new RentbillMySqlExtDAO();

$rentbillpro = new Rentbillprop();
$myrentbillpro = new RentbillpropMySqlDAO();
$extrentbillpro = new RentbillpropMySqlExtDAO();

$youtubeLink = new YoutubeLink();
$youtubeLinkDAO = new YoutubeLinkMySqlDAO();
$youtubeLinkEX = new YoutubeLinkMySqlExtDAO();
//check and use the condition that suite this action
if (empty($do)) {
    //here the permssion check
    include_once("../public/authentication.php");

    $products = $myrentProducts->queryAll();
    $smarty->assign('products', $products);

    $productid = filter_input(INPUT_POST, 'productid');
    $startdate = filter_input(INPUT_POST, 'startdate');
    $enddate = filter_input(INPUT_POST, 'enddate');
    if (!$startdate && !$enddate) {
        $showsearch = 0;
    } else {
        $showsearch = 1;

        function unique_multidim_array($array, $key) {
            $temp_array = array();
            $i = 0;
            $key_array = array();
            foreach ($array as $val) {
                if (!in_array($val[$key], $key_array)) {
                    $key_array[$i] = $val[$key];
                    $temp_array[$i] = $val;
                }
                $i++;
            }
            return $temp_array;
        }

        $smarty->assign('startdate', $startdate);
        $smarty->assign('enddate', $enddate);

        if ($productid == 0) {
            //all products
            $smarty->assign('productname', 'كل المنتجات');

            //one product
            function date_range($first, $last, $step = '+1 day', $output_format = 'Y-m-d') {
                $dates = array();
                $current = strtotime($first);
                $last = strtotime($last);
                while ($current <= $last) {
                    $dates[] = date($output_format, $current);
                    $current = strtotime($step, $current);
                }
                return $dates;
            }

            $dateranges = date_range($startdate, $enddate);

            $allpros = array();

            foreach ($products as $oneproduct) {
                $mainproduct_arrays = array();
                for ($i = 0; $i < count($dateranges); $i++) {
                    //get array with used products in this day
                    $rentedbills = $extrentbillpro->rentinthisday($dateranges[$i], $oneproduct->id);
                    foreach ($rentedbills as $sinpro) {
                        array_push($mainproduct_arrays, get_object_vars($sinpro));
                    }
                }

                $array_uniq = unique_multidim_array($mainproduct_arrays, 'id');
                $max = 0;
                foreach ($array_uniq as $sin) {
                    $max = $max + $sin['amount'];
                }
                $productinfo = $myrentProducts->load($oneproduct->id);
                $productava = $productinfo->quantity - $max;
                if ($productava < 0) {
                    $productava = 0;
                }

                $productmax = array("productname" => $productinfo->name, "max" => $productava);

                array_push($allpros, $productmax);
            }

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

            //$smarty->assign('productname', $productinfo->name);
            $smarty->assign('isisngle', 0);
        } else {


            //one product
            function date_range($first, $last, $step = '+1 day', $output_format = 'Y-m-d') {
                $dates = array();
                $current = strtotime($first);
                $last = strtotime($last);
                while ($current <= $last) {
                    $dates[] = date($output_format, $current);
                    $current = strtotime($step, $current);
                }
                return $dates;
            }

            $dateranges = date_range($startdate, $enddate);
            $mainproduct_arrays = array();
            for ($i = 0; $i < count($dateranges); $i++) {
                //get array with used products in this day
                $rentedbills = $extrentbillpro->rentinthisday($dateranges[$i], $productid);
                foreach ($rentedbills as $sinpro) {
                    array_push($mainproduct_arrays, get_object_vars($sinpro));
                }
            }
            $array_uniq = unique_multidim_array($mainproduct_arrays, 'id');
            $max = 0;
            foreach ($array_uniq as $sin) {
                $max = $max + $sin['amount'];
            }
            $productinfo = $myrentProducts->load($productid);
            $productava = $productinfo->quantity - $max;
            if ($productava < 0) {
                $productava = 0;
            }

            $productmax = array("productname" => $productinfo->name, "max" => $productava);
            $smarty->assign('productmax', $productmax);
            $smarty->assign('productname', $productinfo->name);
            $smarty->assign('isisngle', 1);
        }
    }
    $smarty->assign('showsearch', $showsearch);

    $youtubes = $youtubeLinkDAO->queryAll();
    $smarty->assign("youtubes", $youtubes);

    //here the smarty templates
    $smarty->display("rent/reports/rentproduct_ava.html");
}
//here the global templates
$smarty->assign("rentjs", 1);
$smarty->display("footer.html");

/* ===============================
  function in this CONTROLLER
  ================================ */
?>