<?php
require_once($global['root']."model/Crud.php");
$obj_crud = new Crud();

require_once($global['root-model']."Region.php");
$obj_region = new Region();

require_once($global['root-model']."Unit.php");
$obj_unit = new Unit();

require_once($global['root-model']."Client.php");
$obj_client = new Client();

require_once($global['root-model']."Admin.php");
$obj_admin = new Admin();

require_once($global['root-model']."Ppn.php");
$obj_ppn = new Ppn();

require_once($global['root-model']."PO.php");
$obj_po = new PO();

require_once($global['root-model']."Activity.php");
$obj_activity = new Activity();

require_once($global['root-model']."Access.php");
$obj_access = new Access();

if(isset($_SESSION['order_token'])){
	$order_token = $_SESSION['order_token'];
} else {
	$_SESSION['order_token'] = generate_order_token();
}

if(!isset($_GET['action'])){ 

	$data_region = $obj_region->get_data_select($obj_crud);
	$data_unit = $obj_unit->get_data_select($obj_crud);
	$data_ppn = $obj_ppn->get_ppn($obj_crud);
	$data_admin = $obj_admin->get_admin_pic($obj_crud);
	$data_admin_client = $obj_admin->get_admin_pic_client($obj_crud);

	$acsp_admin_region = $obj_access->check_access($obj_cruds,$_SESSION['admin_id'],'show_admin_region');

    if(isset($_SESSION['vmessage'])){
        foreach($_SESSION['vmessage'] as $svm){
            $vmessage[] = $svm;
        }
        unset($_SESSION['vmessage']);
    } else {
        $vmessage = "";
    }

    if(isset($_SESSION['valert'])){
        foreach($_SESSION['valert'] as $vlr){
            $valert[] = $vlr;
        }
        unset($_SESSION['valert']);
    } else {
        $valert = "";
    }


} else {
	if($_GET['action'] == "save"){
		date_default_timezone_set("Asia/Jakarta");
		$N_user_id = $_SESSION['admin_id'];
        $N_user_name = $_SESSION['admin_username'];

		$ponumber = $obj_crud->escape_string(check_input($_POST['ponumber']));
		$region = $obj_crud->escape_string(check_input($_POST['region']));
		$pic = $obj_crud->escape_string(check_input($_POST['pic']));
		$document_date = $obj_crud->escape_string(check_input($_POST['document_date']));
		$date_format = date("Y-m-d", strtotime($document_date));
		$cregion = "0";
		$csub = $obj_crud->escape_string(check_input($_POST['csub']));
		$picc = $obj_crud->escape_string(check_input($_POST['picc']));
		//client
		$clientid = $obj_crud->escape_string(check_input($_POST['clientid']));
		if($clientid == ""){
			$data_client = $obj_client->check_client_email($obj_crud,$cemail);
			if(is_array($data_client)){
				$clientid = $data_client[0]['client_id'];
				$cname = $data_client[0]['client_name'];
				$cemail = $data_client[0]['client_email'];
				$cphone = $data_client[0]['client_phone'];
				$caddress = $data_client[0]['client_address'];
			} else {
				$cname = $obj_crud->escape_string(check_input($_POST['cname']));
				$cemail = $obj_crud->escape_string(check_input($_POST['cemail']));
				$cphone = $obj_crud->escape_string(check_input($_POST['cphone']));
				$caddress = $obj_crud->escape_string(check_input($_POST['caddress']));
				$create_client = $obj_client->create_client($obj_crud,$cname,$cemail,$cphone,$cregion,$caddress);
				$clientid = $create_client;
			}
		} else {
			$cname = $obj_crud->escape_string(check_input($_POST['cname']));
			$cemail = $obj_crud->escape_string(check_input($_POST['cemail']));
			$cphone = $obj_crud->escape_string(check_input($_POST['cphone']));
			$caddress = $obj_crud->escape_string(check_input($_POST['caddress']));
		}


		$posubtotal = $obj_crud->escape_string(check_input($_POST['posubtotal']));
		$poppn = $obj_crud->escape_string(check_input($_POST['poppn']));
		$poppn_price = $obj_crud->escape_string(check_input($_POST['poppn_price']));
		$pototal = $obj_crud->escape_string(check_input($_POST['pototal']));
		$postatus = "open";

		//Item list count
		if(isset($_POST['hdesc'])){
			$total_item = count($_POST['hdesc']);
		} else {
			$total_item = 0;
		}

		$po_id = $obj_po->generate_id($obj_crud);
		$check_id = $obj_po->check_po_id($obj_crud,$po_id);
		if($check_id > 0){
			$po_id = $obj_po->generate_id($obj_crud);
		}


		$check_po_num = $obj_po->check_po_number($obj_crud,$ponumber);
		if($check_po_num == 0){
			$result = $obj_po->insert_new_po($obj_crud,$po_id,$ponumber,$region,$pic,$date_format,$clientid,$cname,$cemail,$cphone,$caddress,$posubtotal,$poppn,$poppn_price,$pototal,$total_item,$postatus,$csub,$picc);
			if($result != 0){
				//insert item
				for($x=0;$x<= $total_item;$x++) {
					if($_POST['hdesc'][$x] != ""){
						$result = $obj_po->insert_at_po($obj_crud,$po_id,$_POST['hdesc'][$x],$_POST['hqty'][$x],$_POST['hunit'][$x],$_POST['hunittxt'][$x],$_POST['hprice'][$x],$_POST['hproduct'][$x]);
					}
				}
				//save activity
				$act_type = "open";
				$act_notes = "New Purchase Order Created";
				$save_activity = $obj_activity->insert_activity($obj_crud, $po_id, $N_user_id, $N_user_name, $act_type, $act_notes);
				$_SESSION['vmessage'][] = "Successfully create new purchase order with number ".$po_id.".";
	            $_SESSION['valert'][] = "success";
			} else {
				$_SESSION['vmessage'][]= "Failed to create new purchase order!"; 
	            $_SESSION['valert'][] = "error";
			}
		} else {
			$_SESSION['vmessage'][]= "Failed to create po! purchase order number ".$ponumber." already register!"; 
	        $_SESSION['valert'][] = "error";
		}

    	header("Location:".$path['po']);
	}
}
?>