<?php
global $hooks;
	
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
$hooks->add_action('silex_action','the_po'); 
function po_title(){
    echo "Purchase Order";
}

function create_po_css() {
    ?>
	<link href="/assets/global/plugins/bootstrap-daterangepicker/daterangepicker.min.css" rel="stylesheet" type="text/css" />
	<link href="/assets/global/plugins/bootstrap-datepicker/css/bootstrap-datepicker3.min.css" rel="stylesheet" type="text/css" />
    <link href="/assets/global/plugins/select2/css/select2.min.css" rel="stylesheet" type="text/css" />
	<link href="/assets/modul-css/profile/profile.min.css" rel="stylesheet" type="text/css">	
	<link href="/assets/global/plugins/typeahead/typeahead.css" rel="stylesheet" type="text/css" />
	<link href="/assets/global/plugins/select2/css/select2-bootstrap.min.css" rel="stylesheet" type="text/css" />
    <?php
}

function create_po_js() {
    ?>
	<script src="/assets/global/plugins/select2/js/select2.full.min.js" type="text/javascript"></script>
	<script src="/assets/global/plugins/jquery-validation/js/jquery.validate.min.js" type="text/javascript"></script>
	<script src="/assets/global/plugins/jquery-validation/js/additional-methods.min.js" type="text/javascript"></script>
	<script src="/assets/global/plugins/bootstrap-wizard/jquery.bootstrap.wizard.min.js" type="text/javascript"></script>
	<script src="/assets/global/plugins/jquery-repeater/jquery.repeater.js" type="text/javascript"></script>
	<script src="/assets/global/scripts/datatable.js" type="text/javascript"></script>
	<script src="/assets/global/plugins/datatables/datatables.min.js" type="text/javascript"></script>
	<script src="/assets/global/plugins/datatables/plugins/bootstrap/datatables.bootstrap.js" type="text/javascript"></script>
	<script src="/assets/modul-js/orders/purchase-order/create.js" type="text/javascript"></script>
 <?php
}

function po_js() {
    ?>
	<script src="/assets/global/plugins/select2/js/select2.full.min.js" type="text/javascript"></script>
	<script src="/assets/global/plugins/jquery-validation/js/jquery.validate.min.js" type="text/javascript"></script>
	<script src="/assets/global/plugins/jquery-validation/js/additional-methods.min.js" type="text/javascript"></script>
	<script src="/assets/global/plugins/bootstrap-wizard/jquery.bootstrap.wizard.min.js" type="text/javascript"></script>
	<script src="/assets/global/plugins/jquery-repeater/jquery.repeater.js" type="text/javascript"></script>
	<script src="/assets/global/scripts/datatable.js" type="text/javascript"></script>
	<script src="/assets/global/plugins/datatables/datatables.min.js" type="text/javascript"></script>
	<script src="/assets/global/plugins/datatables/plugins/bootstrap/datatables.bootstrap.js" type="text/javascript"></script>
	<script src="/assets/modul-js/orders/purchase-order/po.js"></script>
<?php }

function po_css() {
    ?>
	<link href="/assets/global/plugins/dropzone/dropzone.min.css" rel="stylesheet" type="text/css" />
	<link href="/assets/global/plugins/dropzone/basic.min.css" rel="stylesheet" type="text/css" />
	<link href="/assets/global/plugins/bootstrap-switch/css/bootstrap-switch.min.css" rel="stylesheet" type="text/css" />
	<link href="/assets/global/plugins/bootstrap-fileinput/bootstrap-fileinput.css" rel="stylesheet" type="text/css" />
<?php }

function the_po(){
	global $app;

	$app->get('/purchase-order', function(){ 
		global $hooks;
		global $app;
		
		$hooks->add_action('global_css', "po_css");
        $hooks->add_action('global_js', "po_js");
		$hooks->add_action('the_title',"po_title");
		
		the_head();
		include 'po.tpl.php';
		the_footer();
		 
		return "";
	}); 
	$app->post('/purchase-order/list', function(Request $request)  {
        $curpage = $request->get('page');
        include 'po.list.php';
        return '';
    }); 
	$app->get('/purchase-order/create', function(){ 
		global $hooks;
		global $app;
		
        $hooks->add_action('global_css', "create_po_css");
        $hooks->add_action('global_js', "create_po_js");
		$hooks->add_action('the_title',"po_title");
		
		the_head();
		include 'create.tpl.php';
		the_footer();
		typeAhead();
		return "";
	}); 

	
	$app->post('/purchase-order/add-purchase-order', function (Request $request) {
        global $db;
		
        $user_id = $_SESSION['user_id'];
		$forwarder_id = $request->get('forwarder_id');
		$purchase_no = $request->get('purchase_no');
		$company_name = $request->get('company_name');
		$company_address = $request->get('company_address');
		$company_city = $request->get('company_city');
		$company_zip = $request->get('company_zip');
		$company_state = $request->get('company_state');
		$company_country = $request->get('company_country');
		$company_tax_no = $request->get('company_tax_no');
		$company_contact_person = $request->get('company_contact_person');
		$company_contact_person_no = $request->get('company_contact_person_no');
		$supplier_company_name = $request->get('supplier_company_name');
		$supplier_contact_person = $request->get('supplier_contact_person');
		$supplier_contact_person_no = $request->get('supplier_contact_person_no');
		$bank_name = $request->get('bank_name');
		$bank_address = $request->get('bank_address');
		$bank_acc_no = $request->get('bank_acc_no');
		$bank_swiftcode = $request->get('bank_swiftcode');
		$item_remark = $request->get('item_remark');
		$order_currency = $request->get('order_currency');
		//$custom_form = $request->get('custom_form');
		$purchaseorderitems = $request->get('purchaseorderitems');
		$company_id = $_SESSION['company_id'];
		$branch_id = $_SESSION['branch_id'];
		
		$db->bind("marking_no", "");
		$db->bind("purchase_no", $purchase_no);
		$db->bind("company_name", $company_name);
		$db->bind("company_address", $company_address);
		$db->bind("company_city", $company_city);
		$db->bind("company_zip", $company_zip);
		$db->bind("company_state", $company_state);
		$db->bind("company_country", $company_country);
		$db->bind("company_tax_no", $company_tax_no);
		$db->bind("company_contact_person", $company_contact_person);
		$db->bind("company_contact_person_no", $company_contact_person_no);
		$db->bind("supplier_company_name", $supplier_company_name);
		$db->bind("supplier_contact_person", $supplier_contact_person);
		$db->bind("supplier_contact_person_no", $supplier_contact_person_no);
		$db->bind("bank_name", $bank_name);
		$db->bind("bank_address", $bank_address);
		$db->bind("bank_acc_no", $bank_acc_no);
		$db->bind("bank_swiftcode", $bank_swiftcode);
		$db->bind("order_currency", $order_currency);
		//$db->bind("order_remark", json_encode($custom_form));
		$db->bind("item_remark", $item_remark);
		$db->bind("branch_id", $branch_id);
		$db->bind("company_id", $company_id);
		$db->bind("forwarder_id", $forwarder_id);
		
		
		$poCreate = $db->query("INSERT INTO purchase_orders (marking_no, purchase_no, company_name, company_address, company_city, company_zip, company_state, company_country, company_tax_no, company_contact_person, company_contact_person_no, supplier_company_name, supplier_contact_person, supplier_contact_person_no, bank_name, bank_address, bank_acc_no, bank_swiftcode, order_currency, item_remark, branch_id, company_id, forwarder_id) VALUES (:marking_no,:purchase_no,:company_name,:company_address,:company_city,:company_zip,:company_state,:company_country,:company_tax_no,:company_contact_person,:company_contact_person_no,:supplier_company_name,:supplier_contact_person,:supplier_contact_person_no,:bank_name,:bank_address,:bank_acc_no,:bank_swiftcode,:order_currency,:item_remark,:branch_id,:company_id,:forwarder_id);");
		$po_id = $db->lastInsertId();
		if($poCreate){
			$ordercountCreate = $db->query("INSERT INTO order_count (id) VALUES (NULL);");
			$order_no = sprintf('%05d', $db->lastInsertId());
			$marking = getMarkingNo($company_id, $forwarder_id, $branch_id);
			$marking_no = getMarkingNo($company_id, $forwarder_id, $branch_id). "/" .$order_no;
			
			$db->bind("order_no", $order_no);
			$db->bind("marking", $marking);
			$db->bind("marking_no", $marking_no);
			$db->bind("po_id", $po_id);
			$poUpdate = $db->query("UPDATE `purchase_orders` SET `marking_no`=:marking_no, `marking`=:marking, `order_no`=:order_no WHERE po_id = :po_id");
			
			foreach ($purchaseorderitems as $key => $purchaseorderitem) {
				$subtotal = $purchaseorderitem['quantity'] * $purchaseorderitem['unit_price'];
				$db->bind("product_name", $purchaseorderitem['product_name']);
				$db->bind("item_code", $purchaseorderitem['item_code']);
				$db->bind("model_no", $purchaseorderitem['model_no']);
				$db->bind("description", $purchaseorderitem['description']);
				$db->bind("quantity", $purchaseorderitem['quantity']);
				$db->bind("uom", $purchaseorderitem['uom']);
				$db->bind("unit_price", $purchaseorderitem['unit_price']);
				$db->bind("subtotal", $subtotal);
				$db->bind("po_id", $po_id);
				
				$poItemCreate = $db->query("INSERT INTO purchase_order_items (product_name, item_code, model_no, description, quantity, uom, unit_price, subtotal, po_id) VALUES (:product_name, :item_code, :model_no, :description, :quantity, :uom, :unit_price, :subtotal, :po_id);");
			}
		}
		
		
		if ($poItemCreate) {			
		
			$chat_name = $order_no;
			
			//create messages
			$db->bind("message", "Your purchase order have been created.");
			$db->bind("chat_type", "10");
			$db->bind("from_id", $user_id);
			$createMessage  = $db->query("INSERT INTO notice_messages(message,chat_type,from_id) VALUES (:message,:chat_type,:from_id)");
			$message_id = $db->lastInsertId();
			
			//create room for user_id
			$db->bind("chat_name",$chat_name);
			$db->bind("owner_id", $user_id);
			$db->bind("po_id", $po_id);
			$db->bind("room_type", "order");
			$createRoom  = $db->query("INSERT INTO notice_rooms(chat_name,owner_id,po_id,room_type) VALUES ( :chat_name, :owner_id, :po_id, :room_type)");
			$room_id = $db->lastInsertId();
			
			//create members for room (current user)
			$db->bind("user_id", $user_id);
			$db->bind("room_id", $room_id);
			$createMembers  = $db->query("INSERT INTO notice_members(room_id,user_id,status) VALUES (:room_id, :user_id, 1)");
			
			//create unread notification
			$db->bind("room_id", $room_id);
			$db->bind("message_id", $message_id);
			$db->bind("user_id", $user_id);
			$createMembers  = $db->query("INSERT INTO notice_unread(room_id,message_id,user_id, status) VALUES (:room_id, :message_id,:user_id, 0)");
			
			$db->bind("forwarder_id", $forwarder_id);
			$companyStaffs = $db->query("SELECT * FROM user_company WHERE company_id = :forwarder_id and position = 1");
			foreach($companyStaffs as $key => $value){
				$db->bind("user_id", $value['user_id']);
				$db->bind("room_id", $room_id);
				$createMembers  = $db->query("INSERT INTO notice_members(room_id,user_id,status) VALUES (:room_id, :user_id, 1)");
				
				
				//create unread notification for admin staffs
				$db->bind("room_id", $room_id);
				$db->bind("message_id", $message_id);
				$db->bind("user_id", $value['user_id']);
				$createMembers  = $db->query("INSERT INTO notice_unread(room_id,message_id,user_id, status) VALUES (:room_id, :message_id,:user_id, 0)");
			}
						
			//connecting messages with room
			$db->bind("room_id", $room_id);
			$db->bind("message_id", $message_id);
			$createMembers  = $db->query("INSERT INTO notice_room_message(room_id,message_id) VALUES (:room_id, :message_id)");
			
			return new Response($po_id, 200);
		} else {			
			return new Response($po_id, 201);
		}
	});
	
	$app->post('/purchase-order/getMarkingNumber', function(Request $request) {		
		global $db;
		$forwarder_id = $request->get('forwarder_id');
		$company_id = $_SESSION['company_id'];
		$branch_id = $_SESSION['branch_id'];
		
		$marking_no = getMarkingNo($company_id, $forwarder_id, $branch_id);
		$order_no = getOrderNumber($company_id, $branch_id);
		return $marking_no . "/" . $order_no;
	}); 
	 $app->get('/purchase-order/details/{id}', function ($id) use ($app) {
        global $db;
        global $hooks;
        global $po_id;
        $po_id = $id;
        if (!isset($id) || !isPOMine($id)) {
            return $app->redirect('/purchase-order');
        }

        $hooks->add_action('global_css', "create_po_css");
        $hooks->add_action('global_js', "po_js");
		$hooks->add_action('the_title',"po_title");
		
        the_head();
        include 'po.details.php';
        the_footer();
        return "";
    });
	
	$app->post('/purchase-order/getModalSuccess', function(Request $request) {	
		global $db;
		$po_id = $request->get('po_id');
		$marking_no = getPOData($po_id, "marking_no");
		$order_no = substr($marking_no, -5);
		$html = '<p>Your order '.$order_no.' have successfully created. Please procees to action below : </p>
				<a href="/purchase-order" style="border-radius:12px !Important" class="btn btn-sm btn-warning">Close</a>&nbsp;
				<a href="/purchase-order/create"  style="border-radius:12px !Important" class="btn btn-lg btn-sm btn-success">Create New</a>&nbsp;
				<a href="/messenger/order/'.getRoomIDFromPO($po_id).'" style="background-color: #134f5c;border-color: #134f5c;border-radius:12px !Important" class="btn btn-lg btn-sm btn-success">Chat</a>';
		
		return $html;
	});
}

include 'po.func.php';