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

//Premium
require_once('../models/dao/PremiumDAO.class.php');
require_once('../models/dto/Premium.class.php');
require_once('../models/mysql/PremiumMySqlDAO.class.php');
require_once('../models/mysql/ext/PremiumMySqlExtDAO.class.php');

//PremiumInstallment
require_once('../models/dao/PremiumInstallmentsDAO.class.php');
require_once('../models/dto/PremiumInstallment.class.php');
require_once('../models/mysql/PremiumInstallmentsMySqlDAO.class.php');
require_once('../models/mysql/ext/PremiumInstallmentsMySqlExtDAO.class.php');

//Premiumautomatic
require_once('../models/dao/PremiumautomaticDAO.class.php');
require_once('../models/dto/Premiumautomatic.class.php');
require_once('../models/mysql/PremiumautomaticMySqlDAO.class.php');
require_once('../models/mysql/ext/PremiumautomaticMySqlExtDAO.class.php');


//PremiumClient
require_once('../models/dao/PremiumclientDAO.class.php');
require_once('../models/dto/Premiumclient.class.php');
require_once('../models/mysql/PremiumclientMySqlDAO.class.php');
require_once('../models/mysql/ext/PremiumclientMySqlExtDAO.class.php');

//typeClient
require_once('../models/dao/TypeClientDAO.class.php');
require_once('../models/dto/TypeClient.class.php');
require_once('../models/mysql/TypeClientMySqlDAO.class.php');
require_once('../models/mysql/ext/TypeClientMySqlExtDAO.class.php');

//typeSupplier
require_once('../models/dao/TypesupplierDAO.class.php');
require_once('../models/dto/Typesupplier.class.php');
require_once('../models/mysql/TypesupplierMySqlDAO.class.php');
require_once('../models/mysql/ext/TypesupplierMySqlExtDAO.class.php');



//Sellbilldetail
require_once('../models/dao/SellbilldetailDAO.class.php');
require_once('../models/dto/Sellbilldetail.class.php');
require_once('../models/mysql/SellbilldetailMySqlDAO.class.php');
require_once('../models/mysql/ext/SellbilldetailMySqlExtDAO.class.php');

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

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

  Controller Name :- productionExecutionAjaxController

  OPERTATION in Controller
  ======================== */

//here goes the instances and general variables

//Client
$client = new Client();
$clientDAO = new ClientMySqlDAO();
$clientExt = new ClientMySqlExtDAO();

//Premium
$premium = new Premium();
$premiumDAO = new PremiumMySqlDAO();
$premiumExt = new PremiumMySqlExtDAO();
//premium client

$premiumClient = new Premiumclient();
$premiumClientDAO = new PremiumclientMySqlDAO();
$premiumClientEXT = new PremiumclientMySqlExtDAO();

//PremiumInstallment
$PremiumInstallment = new PremiumInstallment();
$PremiumInstallmentDAO = new PremiumInstallmentsMySqlDAO();
$PremiumInstallmentExt = new PremiumInstallmentsMySqlExtDAO();

//Premiumautomatic
$Premiumautomatic = new Premiumautomatic();
$PremiumautomaticDAO = new PremiumautomaticMySqlDAO();
$PremiumautomaticExt = new PremiumautomaticMySqlExtDAO();

//typeClient
$type = new TypeClient();
$TypeClientDAO = new TypeClientMySqlDAO();
$TypeClientEX = new TypeClientMySqlExtDAO();

//typeSupplier
$typeSupplier = new Typesupplier();
$TypeSupplierDAO = new TypesupplierMySqlDAO();
$TypeSupplierEX = new TypesupplierMySqlExtDAO();


//Sellbilldetail
$mySellbilldetail = new Sellbilldetail();
$mySellbilldetailRecord = new SellbilldetailMySqlDAO();
$mySellbilldetailEx = new SellbilldetailMySqlExtDAO();

$today = date("Y-m-d");
$smarty->assign("today", $today);


if ($do == "getClientdata") {

    $clientID = filter_input(INPUT_POST, 'clientID');

    $clientData = $clientExt->getalldata($clientID);

    $clientData->typeclientid = explode(',', $clientData->typeclientid);


    $typeClient = $TypeClientDAO->queryAll();
    $azmy = array();
    foreach ($typeClient as $item) {
//var_dump($item);
        if (in_array($item->typeId, $clientData->typeclientid)) {
            array_push($azmy, $item->typeName);
        }

    }

    if (in_array('-20', $clientData->typeclientid)){
        array_push($azmy, "كل انواع العملاء");

    };

    $aaaa = implode(",", $azmy);
    $clientData->typeName = $aaaa ;

    echo json_encode($clientData);

}

if ($do == "getpremiumdata") {

    $id = filter_input(INPUT_POST, 'id');

    $premiumData = $premiumExt->getpremiumdatabyid($id);
    echo json_encode($premiumData);

}

if ($do == "getpremium") {

    $id = filter_input(INPUT_POST, 'id');

    $premiumData = $premiumDAO->load($id);
    echo json_encode($premiumData);

}
if ($do == "installmentDates") {

    $start_date = filter_input(INPUT_POST, 'installment_date');          ## installments start date
    $installment_nos = filter_input(INPUT_POST, 'installment_nos');             ## no of installment per  months
    $installment_months = filter_input(INPUT_POST, 'installment_months');    ## no of months
    $installment_days = filter_input(INPUT_POST, 'installment_days');    ## no of days
    $rest_payment = filter_input(INPUT_POST, 'rest_payment'); //باقي المبلغ
    if ($start_date) {
        $start_date = date("Y-m-d", strtotime($start_date));
        ## installments start date
    }else{
        $start_date = date("Y-m-d");
    }

    $installments_dates = array();
    $row = array();


    $installment_no = $installment_nos;
    $installment_value = round($rest_payment / $installment_no);
    $last_installment_value = $rest_payment;


    for ($i = 1; $i < $installment_no; $i++) {

        $row['date'] = $start_date;
        $row['value'] = $installment_value;
        $installments_dates[] = $row;

        $last_installment_value = $last_installment_value - $installment_value;
        if ($installment_months) {
            $start_date = date('Y-m-d', strtotime("+" . $installment_months . " months ", strtotime($start_date)));
        } else if ($installment_days) {
            $start_date = date("Y-m-d", strtotime("+" . $installment_days . " days ", strtotime($start_date)));
        }else{
            $start_date = date("Y-m-d", strtotime("+" . 1 . " day ", strtotime($start_date)));
        }
    }

    ## then last  installment   ( to avoid fractions )
    if ($last_installment_value > 0) {
        $row['date'] = $start_date;
        $row['value'] = $last_installment_value;
        $installments_dates[] = $row;

        $start_date = date('Y-m-d', strtotime("+" . $installment_months . " months ", strtotime($start_date)));
    }


    $smarty->assign("installments_dates", $installments_dates);
    $smarty->display("premiumautomaticview/auto.html");


}

if ($do == "getpaypremiumautodata") {

    $id = filter_input(INPUT_POST, 'id');

    $premiumData = $PremiumInstallmentExt->getpremiumautodatabyid($id);
    echo json_encode($premiumData);

}

if ($do == "getdelaypremiumauto") {

    $id = filter_input(INPUT_POST, 'id');

    $premiumData = $PremiumInstallmentDAO->load($id);
    echo json_encode($premiumData);

}



if ($do == "checkPremiumClient") {

    $clientID = filter_input(INPUT_POST, 'clientID');

    $premiumData = $premiumExt->checkPremiumClientData($clientID);
//    var_dump($premiumData);
    if (count($premiumData) > 0){
        echo 1;
    }else{
        echo 0;
    }

}

if ($do == "checkPremiumClientData") {

    $clientID = filter_input(INPUT_POST, 'clientID');

    $premiumData = $premiumExt->checkPremiumClientData($clientID);
    $total = 0;
    foreach ($premiumData as $premium){
        $total += $premium->value - $premium->rest;
    }

    $smarty->assign("premiumData", $premiumData);
    $smarty->assign("total", $total);
    $smarty->display("clientPayedDeptview/autoClientPremium.html");
}




?>