<?php

// get the config file
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'];


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

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

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

$myBankaccountRecord = new BankaccountMySqlDAO();


//check and use the condition that suite this action
if ($do == 'fetchproduct') {

    $productid = filter_input(INPUT_POST, 'productid');
    $product = $myrentProducts->load($productid);
    $getstore = $myrentStore->queryByRentproductid($productid);
    $product->avaible = $getstore[0]->amount - $getstore[0]->rented;
    echo json_encode($product);
} elseif ($do == 'getproducts') {

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

    $name = filter_input(INPUT_GET, 'term'); //to search for
    $limit = intval(filter_input(INPUT_GET, 'page_limit'));
    $ids = filter_input(INPUT_GET, 'ids');
    $myid = filter_input(INPUT_GET, 'myid');

    //commented to allow that account used many times in the same entry
    $ids = rtrim($ids, ",");
    $idsQueryString = '';
    if (isset($ids) && !empty($ids)) {
        $idsQueryString.=' and (id not in (' . $ids . ') ';
        if (isset($myid) && !empty($myid)) {
            $idsQueryString.=' or id = ' . $myid . ' ) ';
        } else {
            $idsQueryString.= ' ) ';
        }
    }

    $allData = $extrentProducts->getproducts($name, $idsQueryString);
    if (count($allData) > 0) {
        foreach ($allData as $myData) {
            $row_array['id'] = $myData->id;
            $row_array['text'] = $myData->name;
            array_push($return_arr, $row_array);
        }
    }

    //echo $return_arr;
    echo json_encode($return_arr);
} elseif ($do == 'calmaxava') {

    $startdate = filter_input(INPUT_POST, 'startdate');
    $enddate = filter_input(INPUT_POST, 'enddate');
    $productid = filter_input(INPUT_POST, 'productid');

    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++) {
        //print_r($dateranges[$i]);
        //echo '<br/>';
        //get array with used products in this day
        if (!$productid) {
            $productid = 0;
        }
        $rentedbills = $extrentbillpro->rentinthisday($dateranges[$i], $productid);
        foreach ($rentedbills as $sinpro) {
            array_push($mainproduct_arrays, get_object_vars($sinpro));
        }
    }

    //print_r($mainproduct_arrays);

    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;
    }

    $array_uniq = unique_multidim_array($mainproduct_arrays, 'id');
    //echo 'uniq';
    //print_r($array_uniq);

    $max = 0;
    foreach ($array_uniq as $sin) {
        $max = $max + $sin['amount'];
    }

    //print_r($max);

    $productinfo = $myrentProducts->load($productid);
    $productava = $productinfo->quantity - $max;
    if ($productava < 0) {
        $productava = 0;
    }

    if (!$productid) {
        $productava = 0;
    }
    echo ($productava);
} elseif ($do == 'fetchaccounts') {

    $bankid = filter_input(INPUT_POST, 'bankid');
    $allacounts = $myBankaccountRecord->queryByBankid($bankid);

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

    //here the smarty templates
    $smarty->display("rent/bill/accounts.html");
}
?>