<?php
error_reporting(0); //6dec25
if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')){
	ob_start("ob_gzhandler");
}else{
	ob_start();	
} 

include('config/minho.php');
include('config/autoloadClass.php');
$autoLoad 	= new autoloadClass();
$formClass 	= new formClass();
$output 	= "";
$project 	= isset($_POST['project']) ? str_replace("_", " ", trim(strip_tags($_POST['project']))) : "";
$model 		= isset($_POST['model']) ? str_replace("_", " ", trim(strip_tags($_POST['model']))) : "";
$priceDate 	= isset($_POST['pricelist_date']) ? $_POST['pricelist_date'] : "";
$phase 		= isset($_POST['phase']) ? trim(strip_tags($_POST['phase'])) : "";
$block 		= isset($_POST['block']) ? trim(strip_tags($_POST['block'])) : "";
$lot 		= isset($_POST['lot']) ? htmlentities(trim(strip_tags($_POST['lot']))) : "";
$action 	= isset($_POST['action']) ? trim(strip_tags(htmlentities(addslashes($_POST['action'])))) : "";
$mid 		= isset($_POST['mid']) ? trim(strip_tags(htmlentities(addslashes($_POST['mid'])))) : "";
$mtype 		= isset($_POST['mtype']) ? trim(strip_tags(htmlentities(addslashes($_POST['mtype'])))) : "";
$rPhase 	= "";
$rBlock 	= "";
$rLot 		= "";
$rBroker 	= "";
$rAgent 	= "";
$rProject 	= "";
$rModel 	= "";

if ($action == "edit" || $action == "view" || $action == "transfer") {

    $reserveArr = $formClass->getReservationDetails($mid);
    $rPhase 	= $reserveArr[0]['Phase'];
    $rBlock 	= $reserveArr[0]['Block'];
    $rLot 		= $reserveArr[0]['Lot'];
    $rBroker 	= str_replace("_", " ", $reserveArr[0]['Broker']);
    $rAgent 	= str_replace("_", " ", $reserveArr[0]['Agent']);
    $rProject 	= str_replace("_", " ", $reserveArr[0]['Project']);
    $rModel 	= str_replace("_", " ", $reserveArr[0]['Model']);
}

$pricelistDate 	= date("Y-m-d", strtotime($priceDate));
$modelSelect 	= array("field" => "PricelistDetails,TermsDiscount,CashDiscount", "field_value" => "PricelistDetails", "table" => "pricelist", "project" => $project, "model" => $model, 
	"pricelist_date" => $pricelistDate, "action" => $action);

$s = $formClass->selectPricelistBox($modelSelect);

if (!empty($s)) {
    for ($c = 0; $c < count($s); $c++) {
        $p = json_decode($s[$c]['PricelistDetails'], true);
        if (count($p['details']) != 0) {
            for ($a = 0; $a < count($p['details']); $a++) {
                if ($p['details'][$a]['phase'] == $phase && $p['details'][$a]['block'] == $block && htmlentities($p['details'][$a]['lot']) == $lot) {
                    if ($p['details'][$a]['detail_terms'] == 'cash') {
                        $output = $s[$c]['CashDiscount'];
                    } else {
                        $output = $s[$c]['TermsDiscount'];
                    }
                }
            }
        }
    }
}

$main 					= array();
$main['discount'] 		= $output;
$resultArr 				= array();
$resultArr['output'] 	= $main;

echo json_encode($resultArr);