<?php
error_reporting(0);
if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) ob_start("ob_gzhandler"); else ob_start();
session_start();
session_cache_limiter('private');
$cache_limiter 	= session_cache_limiter();
session_cache_expire(60);
$cache_expire 	= session_cache_expire(); 
date_default_timezone_set('Asia/Manila'); 
$CURRENT_DATE 	= date('Y-m-d H:i:s');
include_once('../config/minho.php');
include_once('../config/autoloadClass.php');
$autoload	=	new autoloadClass();
$mecon		=	new condbClass();
$utility	=	new utilityClass(); 

if(isset($_POST['current_project']) && isset($_POST['current_model'])){
$project		= isset($_POST['current_project'])  ? str_replace('_',' ',htmlentities(addslashes(strip_tags(trim($_POST['current_project']))))) : ""; 
$model			= isset($_POST['current_model']) 	? str_replace('_',' ',htmlentities(addslashes(strip_tags(trim($_POST['current_model']))))) : ""; 
$promo			= isset($_POST['promo_promo']) 		? htmlentities(addslashes(strip_tags(trim($_POST['promo_promo'])))) : ""; 
$startDate		= isset($_POST['promo_start_date']) ? trim($_POST['promo_start_date']) : "";
$endDate		= isset($_POST['promo_end_date']) 	? trim($_POST['promo_end_date']) : "";
$amount			= isset($_POST['promo_amount']) 	? str_replace(",","",$_POST['promo_amount']) : "";
$conditions		= isset($_POST['promo_conditions']) ? htmlentities(addslashes(strip_tags(trim($_POST['promo_conditions'])))) : "";
$status			= isset($_POST['promo_status']) 	? trim($_POST['promo_status']) : "";
$phase			= isset($_POST['phase']) 			? $_POST['phase'] : "";
$block			= isset($_POST['block']) 			? $_POST['block'] : "";
$lot			= isset($_POST['lot']) 				? $_POST['lot'] : "";
$task			= isset($_POST['action'])			? trim($_POST['action']) : "";
$mainid			= isset($_POST['mainid'])			? trim($_POST['mainid']) : "";

$currentUser 	= $_SESSION['danvillee_username'];
$timeStamp		= date('Y-m-d H:i:s');
//SAVING PROMO TABLE DETAILS

if($task=="create"):
	$promoMainId				=	$mecon->getNextId("Id","promo");
elseif($task=="edit"):
	$promoMainId				=	$mainid; 
endif;

$detailsArr = array();
$ctrDetails	= count($phase);
if(isset($_REQUEST['phase'])):
	$b = 1;
	for($a=0; $a<$ctrDetails; $a++){
		$detailsArr[$a]['id'] 		= $b;
		$detailsArr[$a]['phase'] 	= $phase[$a];
		$detailsArr[$a]['block'] 	= $block[$a];
		$detailsArr[$a]['lot'] 		= $lot[$a];  
		$detailsArr[$a]['promo_id'] = $promoMainId;
		$b++;
	}
endif;

$outputDetails 					= $detailsArr; 
$resultDetailsArr 				= array();
$resultDetailsArr['details']  	= $outputDetails; 
$promoTableDetails				= json_encode($resultDetailsArr);
$mainArr	= array(); 
$output 	= ""; 
$mainArr['Project']			=	$project;
$mainArr['Model']			=	$model;
$mainArr['Promo']			=	$promo;
$mainArr['StartDate']		=	date("Y-m-d", strtotime($startDate));
$mainArr['EndDate']			=	date("Y-m-d", strtotime($endDate));
$mainArr['Amount']			=	$amount;
$mainArr['Conditions']		=	$conditions;
$mainArr['PromoStatus']		=	$status;
$mainArr['PromoDetails']	=	$promoTableDetails;
$condArr = array();
$actArr = array();
if($task=="create"):
	$mainArr['Id']				= $mecon->getNextId("Id","promo");
	$mainArr['CreatedDate'] 	= $timeStamp;
	$mainArr['CreatedBy'] 		= $currentUser;
	$output = $mecon->insertSingleQuery($mainArr,"promo");
	
	//LOG TASK
	$actArr['UserType']			= "accountant,assistant_accountant";
	$actArr['Remarks']			= "Created New Promo : $promo";
	$actArr['Action']			= "create";
	$actArr['ActionType']		= "promo";
	$actArr['ActionContent']	= json_encode($mainArr);
	$actArr['ActionBy'] 		= $currentUser;	
	$actArr['ActionDate']		= $timeStamp;
	$actArr['TransId']			= $promoMainId;
	
	$utility->encodeActivityLog($actArr);
elseif($task=="edit"):
	$mainArr['UpdatedDate'] 	= $timeStamp;
	$mainArr['UpdatedBy'] 		= $currentUser;
	$condArr['Id']				= $mainid;
	$output = $mecon->updateQuery($mainArr,"promo",$condArr);
	//LOG TASK
	$actArr['UserType']			= "accountant,assistant_accountant";
	$actArr['Remarks']			= "Updated Promo : $promo";
	$actArr['Action']			= "edit";
	$actArr['ActionType']		= "promo";
	$actArr['ActionContent']	= json_encode($mainArr);
	$actArr['ActionBy'] 		= $currentUser;	
	$actArr['ActionDate']		= $timeStamp;
	$actArr['TransId']			= $promoMainId;
	$utility->encodeActivityLog($actArr);
endif;

$resultArr 						= array();
$resultArr['output']	  		= $output;  
 
echo json_encode($resultArr);

}
exit();
ob_end_flush();
