<?php 
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();
$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']) ? trim($_POST['pricelist_date']) : "";
$phase 		= isset($_POST['phase']) ? trim($_POST['phase']) : "";
$block 		= isset($_POST['block']) ? trim($_POST['block']) : "";
$lot 		= isset($_POST['lot']) ? trim($_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 	= "";
$priceDate 	= date('Y-m-d', strtotime($priceDate));

$main 		= array();

$modelSelect = array("field" => "PricelistDetails,TermsDiscount,CashDiscount", "field_value" => "PricelistDetails", "table" => "pricelist", "project" => $project, "model" => $model, 
	"pricelist_date" => $priceDate, "action" => $action);

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

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 && $p['details'][$a]['lot'] == $lot) {

                $detailTerms 		= $p['details'][$a]['detail_terms'];
                $pricelistStatus 	= $p['details'][$a]['pricelist_status'];
                $selectPhase 		= $p['details'][$a]['phase'];
                $selectBlock 		= $p['details'][$a]['block'];
                $selectLot 			= $p['details'][$a]['lot'];
                $lotSize 			= $p['details'][$a]['lot_size'];
                $lotType 			= $p['details'][$a]['lot_type'];
                $floorArea 			= $p['details'][$a]['floor_area'];
                $contractPrice 		= $p['details'][$a]['contract_price'];
                $loanableAmount 	= $p['details'][$a]['loanable_amount'];
                $pfNet 				= $p['details'][$a]['pf_net'];
                $pfPayable 			= $p['details'][$a]['pf_payable'];
                $amortization 		= $p['details'][$a]['amortization'];
                $required_gross 	= $p['details'][$a]['required_gross'];
                $pricelistId 		= $p['details'][$a]['pricelist_id'];

                if ($p['details'][$a]['detail_terms'] == 'cash') {
                    $discount 		= $s[$c]['CashDiscount'];
                } else {
                    $discount 		= $s[$c]['TermsDiscount'];
                }
            }
        }
    }
}

$main['phase'] 				= $selectPhase;
$main['block'] 				= $selectBlock;
$main['lot'] 				= $selectLot;
$main['discount'] 			= $discount;
$main['detail_terms'] 		= $detailTerms;
$main['pricelist_status'] 	= $pricelistStatus;
$main['lot'] 				= $selectLot;
$main['lot_size'] 			= $lotSize;
$main['lot_type'] 			= $lotType;
$main['floor_area'] 		= $floorArea;
$main['contract_price'] 	= $contractPrice;
$main['loanable_amount'] 	= $loanableAmount;
$main['pf_net'] 			= $pfNet;
$main['pf_payable'] 		= $pfPayable;
$main['required_gross'] 	= $required_gross;
$main['amortization'] 		= $amortization;
$main['pricelist_id'] 		= $pricelistId;
$resultArr 					= array();
$resultArr['output'] 		= $main;

echo json_encode($resultArr);