<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/*	
 *	Author 	: Ladur
 *	Tanggal : 20-07-2014	
 *	Revisi 	: 
 */ 

class Sales_quotation extends CI_Controller {
	// inisialisasi manual
	public $menu_id = 192;
	
	public function __construct() {
        parent::__construct();
		$this->load->model('m_menu');
		$this->load->model('m_salesquotation');		
	}	
	
	public function index() {
		// set data
		$data['menu_id'] =  $this->menu_id;
		
		// get link from tbl_menu 
		$data['menu_label'] =  $this->m_menu->get_menulabel($this->menu_id);
		$data['menu_link'] = $this->m_menu->get_menulink($this->menu_id);
		
		// set for view
		$this->load->view('v_header');
		$this->load->view('sales/sales_quotation/v_salesquotation', $data);
		$this->load->view('v_footer');
	}
	
	public function insert() {
		$detail 			= $this->input->post('detail');
		$cmb_customer		= $this->input->post('cmb_customer');
		$txa_address		= $this->input->post('txa_address');
		$txa_ship_to		= $this->input->post('txa_ship_to');
		$txt_sq_no			= $this->input->post('txt_sq_no');
		$txt_sq_date		= $this->input->post('txt_sq_date');
		$txt_po_no			= $this->input->post('txt_po_no');
		$txt_po_date		= $this->input->post('txt_po_date');
		$cmb_salesman		= $this->input->post('cmb_salesman');
		$txa_description	= $this->input->post('txa_description');
		
		$sub_total = 999;
		$disc_percent = 7;
		$disc = 9;
		$tax = 3;	
		$tax_percennt = 5;
		$total_amount = 123456;
		
		
		$data = array (
			'BranchCode'		=> $this->session->userdata('branch'),
			'CustomerCode' 		=> $cmb_customer,
			'BillingAddress' 	=> $txa_address,
			'ShippingAddress' 	=> $txa_ship_to,
			'SQuoNumber' 		=> $txt_sq_no,
			'SQuoDate' 			=> date("Y-m-d", strtotime($txt_sq_date)),		
			'CustPONumber' 		=> $txt_po_no,
			'CustPODate' 		=> date("Y-m-d", strtotime($txt_po_date)),			
			'SalesmanCode' 		=> $cmb_salesman,
			'SQuoDesc' 			=> $txa_description,
			'SQuoSubTotal' 		=> $sub_total,
			'SQuoDiscPcnt' 		=> $disc_percent,
			'SQuoDiscount' 		=> $disc,
			'SQuoTaxes'			=> $tax,
			'SQuoTaxPcnt' 		=> $tax_percennt,
			'SQuoTotalAmount' 	=> $total_amount,
			'LastUser' 			=> $this->session->userdata("username")
		);
		$detail=json_decode($detail);
		
		
		// transaction begin
		$this->db->trans_begin();
		
		// insert database
		$this->m_salesquotation->insert($data);			
		if($detail){   
			foreach($detail as $row){			
				$detail_data = array(
					'SQuoNumber' 		=> $txt_sq_no,
					'ItemCode' 			=> $row->ItemCode,
					'itemUnit'			=> $row->ItemUnit,
					'itemQty'			=> $row->ItemQty,
					'ItemPrice'			=> $row->ItemPrice,
					'ItemDiscPcnt'		=> $row->ItemDiscPcnt,
					'ItemDiscount'		=> $row->ItemDiscount,
					'ItemAmount'		=> $row->ItemAmount,
					'ItemSN'			=> $row->ItemSN,
					'LastUser' 		    => $this->session->userdata("username")				
				);                    
				$this->m_salesquotation->insert_detail($detail_data);					
			}
		}
		if ($this->db->trans_status() === FALSE) {
			$this->db->trans_rollback();
			$result['status'] = false;
			$result['message'] = 'Penyimpanan gagal, silahkan cek kembali data Anda';
		}
		else {
			$this->db->trans_commit();
			$result['status'] = true;
			$result['message'] = 'PR Berhasil Tersimpan, menunggu approval atasan Anda';
		}		
		
		echo json_encode($result);
	}
	
	//Function Form Edit
	public function edit(){
		 // set data
		$data['menu_id'] =  $this->menu_id;
		//$data['menu_model'] = $this->menu_model;
		
		// get link from tbl_menu 
		$data['menu_link'] = $this->m_menu->get_menulink($this->menu_id);
		
		// get label from tbl_menu 
		$data['menu_label'] = $this->m_menu->get_menulabel($this->menu_id);
		
		// inisialisasi additional parameter
        $id = str_replace($data['menu_link'].'/edit/',"",$this->uri->uri_string());
		$data['detail'] = $this->m_salesquotation->get_detail($id);
		
		if($data['detail'] != false){
			// set for view
			$this->load->view('v_header');
			$this->load->view('sales/sales_quotation/v_edit_salesquotation',$data);
			$this->load->view('v_footer');
		}
	}
	
	// Function Update
	public function update() {
	  	$departement_code	= $this->input->post('cmb_departement');
		$txt_no= $this->input->post('txt_no');
		$txt_date = $this->input->post('txt_date');
		$txa_description    = $this->input->post('txa_description');
		$detail = $this->input->post('detail');
		
		$data = array (
			
			'DepartmentCode' 		=> $departement_code,
			'PReqNumber' 			=> $txt_no,
			'PReqDate' 				=> date("Y-m-d", strtotime($txt_date)),	
            'PReqDesc'				=> $txa_description,		
			'AppLevel1Status' 		=> 0,
			'LastUser' 				=> $this->session->userdata("username")
		);
		$detail=json_decode($detail);
		
		
		// transaction begin
		$this->db->trans_begin();
		// insert database
		$this->m_purchases_requisition->update($data,$txt_no);
		$this->m_purchases_requisition->delete_detail($txt_no);
		if($detail){   
			foreach($detail as $row){			
				$detail_data=array(
					'PReqNumber' 		=> $txt_no,
					'ItemCode' 			=> $row->ItemCode,
					'itemUnit'			=> $row->ItemUnit,
					'itemQty'			=> $row->ItemQty,
					'PReqDesc'			=> $row->PReqDesc,
					'RequiredDate'      => date("Y-m-d", strtotime($row->RequiredDate)),
					'LastUser' 		    => $this->session->userdata("username")				
				);                    
				$this->m_purchases_requisition->insert_detail($detail_data);					
			}
		}
		if ($this->db->trans_status() === FALSE) {
			$this->db->trans_rollback();
			$result['status'] = false;
			$result['message'] = 'Penyimpanan gagal, silahkan cek kembali data Anda';
		}
		else {
			$this->db->trans_commit();
			$result['status'] = true;
			$result['message'] = 'PR Berhasil Tersimpan, menunggu approval atasan Anda';
		}		
		
		echo json_encode($result);
	}
	
	// Function Delete
	public function delete() {
		// inisialisasi additional parameter
        $id = $this->input->post('id');
		
		$action = $this->m_purchases_requisition->delete($id);
		if($action == 1) {
			$result['status'] = true;
			$result['message'] = 'Data bank berhasil dihapus';
		}
		else {
			$result['status'] = false;
			$result['message'] = 'Data gagal dihapus';
		}
		echo json_encode($result);		
	}
	
	public function ajax_item(){		
		$data['menu_link'] = $this->m_menu->get_menulink($this->menu_id);
		// $id = $this->uri->segment(4);
		// $id = str_replace("~", "/", $id) ;
		$id = str_replace($data['menu_link'].'/ajax_item/',"",$this->uri->uri_string());
		
		//$id = str_replace($data['menu_link'].'/ajax_item/',"",$this->uri->uri_string());
		echo json_encode($this->m_salesquotation->get_detail_item($id));
		//echo $this->db->last_query(); die();		
	}
}