<?php 
/*
Fungsi : Controller untuk Purchase Order
*/

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

class Cabang_po extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	
/*-------------Inisialisasi variabel--------------*/ 
	
	//Menu ID utk keperluan otorisasi
	var $menu_list_po  = 59;
	var $menu_add_po = 58;
	
/*-------------Inisialisasi variabel--------------*/ 

/*------------Fungsi local------------------------*/
//Cek valid Vol
public function valid_vol($volume)
{
	
	if( ($this->fungsi->hapus_koma($this->input->post('vol'))) >= $this->fungsi->get_vol($this->session->userdata('idcab')))
	{
		$this->form_validation->set_message('valid_vol', 'Error, your Volume balances is bigger then Quantity');
		return false;
	}
	else
	{
		
		return true;
	}
	
}	

/* Cabang get total Volume */
		//function jml_vol() 
		//{
		//Total Vol berdasarkan PR number
		//$this->db->get('m_op');
		//if ($this->input->post('vol')>= post('quantity'))
		
		//}
		
/*------------Fungsi local------------------------*/
	
	function __construct()
	{
		parent::__construct();
		
		$this->load->model('branch/cabang_model', '', TRUE);
		$this->load->model('admin/admin_model', '', TRUE);
		$this->fungsi->check_login();
	}
	
	
	public function index()
	{	
		$this->cabang_add_po();
		
	}
	
	/* Cabang add po dan hitung pembelian oli*/
	function cabang_add_po()
	{	
		//Otorisasi menu
		$this->fungsi->menu_auth($this->menu_add_po);
		
		$data['data_po'] = $this->cabang_model->get_all_po();
		$data['menu_path'] = 'BRANCH > OIL PURCHASE > ADD OIL PURCHASE';
		$data['form_action']= site_url('branch/cabang_po/add_po_proc');
		$this->load->view('branch/cabang_po_form', $data);
		
	}
			
	/* Cabang add po process */
	function add_po_proc()
	{	
			
		//Check otorisasi transaksi
		$this->fungsi->trans_auth($this->session->userdata('add'));
		
		$data['prev'] = '';
		
		
		/*Get po counter*/
//		$count = array();
//		$count = $this->fungsi->get_no_count($this->session->userdata('idcab'), 'po');
//		$nopoform = $count[0];
//		$nopo = $count[1];
		
		//Config utk validasi form po
	$po = array(
                  array(
                     'field'   => 'donumb',
                     'label'   => 'DO Number',
                     'rules'   => ''
                  ),
				  array(
                     'field'   => 'invoice',
                     'label'   => 'Invoice',
                     'rules'   => ''
                  ),
				  array(
                     'field'   => 'mnfst',
                     'label'   => 'Manifest',
                     'rules'   => ''
                  ),
				  array(
                     'field'   => 'vnumb',
                     'label'   => 'Vehicle Number',
                     'rules'   => ''
                  ),
				  array(
                     'field'   => 'vol',
                     'label'   => 'Volume',
                     'rules'   => 'required|callback_valid_vol'
                  )	  
				  //Not validate form
				  ,array(
                     'field'   => 'idsupp',
                     'label'   => 'ID Supplier',
                     'rules'   => ''
                  )
				   ,array(
                     'field'   => 'idtrans',
                     'label'   => 'ID Transporter',
                     'rules'   => ''
                  )
				    ,array(
                     'field'   => 'adddate',
                     'label'   => 'Add Date',
                     'rules'   => ''
                  )
				    ,array(
                     'field'   => 'chdate',
                     'label'   => 'Change Date',
                     'rules'   => ''
                  )
				    ,array(
                     'field'   => 'chby',
                     'label'   => 'Change By',
                     'rules'   => ''
                  )
				    ,array(
                     'field'   => 'addby',
                     'label'   => 'Add By',
                     'rules'   => ''
                  )
				   //Not validate form
            );
			
		//Data Type form
		$this->load->helper('form'); 
		
		
	
		//Check validasi form
		$this->form_validation->set_rules($po); 
		
		if ($this->form_validation->run() == TRUE)
			{
			/* Insert data po */
			$po = array( 		'c_amt_bapi'=> $this->fungsi->hapus_koma($this->input->post('c_amt_bapi')),
								'prnumb'	=> $this->input->post('prnumb'),
								'ponumb'	=> $this->input->post('ponumb'),
								'idcab'		=> $this->session->userdata('idcab'),
								'type'	    => $this->input->post('type'),
								'idsupp'	=> $this->input->post('idsupp'),
								'idtrans'	=> $this->input->post('idtrans'),
								'pur'		=> $this->fungsi->hapus_koma($this->input->post('pur')),
								'fee'		=> $this->fungsi->hapus_koma($this->input->post('fee')),
								'freight'	=> $this->fungsi->hapus_koma($this->input->post('freight')),
								'other'		=> $this->fungsi->hapus_koma($this->input->post('other')),
								'quantity'	=> $this->fungsi->hapus_koma($this->input->post('quantity')),
								'invoice'	=> $this->input->post('invoice'),
								'vol'		=> $this->input->post('vol'),
								'vnumb'  	=> $this->input->post('vnumb'),
								'total'	    => $this->fungsi->hapus_koma($this->input->post('total')),
								'donumb'	=> $this->input->post('donumb'),
								'year'	    => date('Y'),
								'mnfst'	    => $this->input->post('mnfst'),
								'inf'	    => $this->input->post('inf'),
								'adddate' 	=> date( 'Y-m-d H:i:s'),
								'chdate'	=> $this->input->post('chdate'),
								'chby'		=> $this->input->post('chby'),
								'addby'		=> $this->session->userdata('uid'),
								
						);
			$this->cabang_model->add_po($po);
			
			/*Set pesan sukses dan kembali ke menu add po*/
			$this->session->set_flashdata('message_ok', 'Data saved');
			redirect('branch/cabang_po');
			}
		else
			{
				$this->cabang_add_po();
			}
	}
	
	/* Display list po */
	function cabang_list_po()
	{	
		//Check otorisasi
		$this->fungsi->trans_auth($this->session->userdata('view'));
		
		//Otorisasi menu
		$this->fungsi->menu_auth($this->menu_list_po);
		
		/* Setting Parameter Untuk Paging */
		$uri_segment = 4;
		$offset =    $this->uri->segment($uri_segment);
			
		$po = $this->cabang_model->get_po_paging($this->config->item('paging_limit'), $offset)->result();
		$num_rows = $this->cabang_model->count_all_po();
		$config['base_url'] = site_url('branch/cabang_po/cabang_list_po/');
		if ($num_rows > 0) // Jika query menghasilkan data
				{
					// Membuat pagination			
					$config['total_rows'] = $num_rows;
					$config['per_page'] = $this->config->item('paging_limit');
					$config['uri_segment'] = $uri_segment;
					$this->pagination->initialize($config);
					$data['pagination'] = $this->pagination->create_links();
					$i = 0 + $offset;
					
					$tmpl = array( 'table_open'    => '<table border="0" cellpadding="1" cellspacing="1" id="list">',
								  'row_alt_start'  => '<tr class="zebra">',
								  'row_alt_end'    => '</tr>'
								  );
					$this->table->set_template($tmpl);
		
					// Set heading untuk tabel
					$this->table->set_empty("&nbsp;");
					$this->table->set_heading('Supp ID', 'Name', 'PIC', 'Status', 'Options');
				
					foreach ($op  as $ops)
					{	
			
		$this->table->add_row($ops->idop, $ops->prnumb, $ops->vol, anchor('cabang/cabang_op/edit_op/'.$ops->idop,'Edit',array('class' => 'update')));	
							
					}
					$data['table'] = $this->table->generate();
					
				}
			else
				{
					$data['message'] = 'No data';
				}		
		$data['menu_path'] = 'BRANCH > OP > OP LIST';
		$this->load->view('cabang/cabang_op_list', $data);
		
	}
	
///* Display detail list OP */
//	function op_detail()
//	{
//			$this->fungsi->menu_auth($this->list_op);
//			$this->fungsi->op_auth($this->session->userdata('update'));
//			$idop = $this->cabang_model->get_op_byidop($this->uri->segment(4))->row();
//			$data['menu_path'] = 'USER > MENU > DETAIL TTK';
//			$data['content'] = 'ttk/form_edit_ttk';
//			$data['button_name'] = 'Edit TTK Header';
//			$data['form_action']= site_url('ttk/ttk_add/do_update_ttk_header/'.$this->uri->segment(4));
//			$data['form_action2']= site_url('ttk/ttk_add/do_add_ttk_detail/'.$this->uri->segment(4));
//			$data['readonly']= '';
//			
//			$data['default']['txt_no_ttk'] = $no_ttk ->no_ttk;
//			$data['default']['txt_from'] = $no_ttk -> terima_dari;
//			$data['default']['cb_department'] = $no_ttk ->dept;
//			$data['default']['cb_currency'] = $no_ttk ->mata_uang;		
//			
//			$detail = $this->cabang_model->get_ttkdetail_byiddetailttk($this->uri->segment(4))->result();
//			
//			$bg = 0;
//			$i = 0;
//			$total = 0;
//				$data['table']= '<table border="0" cellpadding="1" cellspacing="1" id="list">';
//			$data['table'].= '       		<tr><th >No</th>
//    										<th>Purchase Requisition No.</th>
//    										<th>Oil Purchase No.</th>
//    										<th>Date</th>
//											<th>Volume</th>
//											<th>Price</th>
//											<th>Total</th>
//    								   <th>Action</th></tr>';
//			foreach ($detail as $details)
//			{		
//			if($bg==0)
//				{
//				$data['table'].= '<tr class="zebra">';
//				$bg++;
//				}
//				else
//				{
//				$data['table'].= '<tr>';
//				$bg--;
//				}
//
//			$total = $total + $details->jumlah_ttk;
//			$data['table'].= '<td>'.++$i.'</td>';
//			$data['table'].= '<td>'.$details->tgl_kwitansi.'</td>';
//			$data['table'].= '<td>'.$details->no_kwitansi.'</td>';
//			$data['table'].= '<td>'.$details->keterangan.'</td>';
//			$data['table'].= '<td>'.$details->no_faktur_pajak.'</td>';
//			$data['table'].= '<td>'.$details->no_po.'</td>';
//			$data['table'].= '<td>'.$details->jumlah_ttk.'</td>';
//			//$data['table'].= '<td align="center">'.$details->total.'</td>';
//		
//			$data['table'].= '<td>'.anchor('ttk/ttk_add/edit_item_detail/'.$this->uri->segment(4).'/'.$details->ttk_no_ttk,'Edit',array('class' => 'update')).'  '.
//									anchor('ttk/ttk_add/delete_item_detail/'.$this->uri->segment(4).'/'.$details->ttk_no_ttk,'Hapus',array('class'=> 'delete','onclick'=>"return confirm('Anda Yakin Akan Menghapus Data Dengan No Kwitansi ".$details->no_kwitansi." ?')")).'</td></tr>';	
//			}
//			$data['table'] .='<tr bgcolor="#FFFF99">
//   							<td>&nbsp;</td>
//							<td>&nbsp;</td>
//							<td>&nbsp;</td>
//							<td>&nbsp;</td>
//							<td>&nbsp;</td>
//							<td align="right">Total :</td>
//							<td align="right"><b>'.number_format($total).'</b></td>
//							<td>&nbsp;</td>
//						  	</tr>';
//			$data['table'].= '</table>';
//			
//			//$data['link'] = array('link_back' => anchor('ttk/ttk_add/list_detail_ttk','Kembali', array('class' => 'back')));
//			$this->load->view('templates/theme01/template', $data);		
//	}
	
	
	/* Edit op */
	function edit_op()
	{
		
		//Get data detail op by id cabang
		$sql_op_edit = $this->cabang_model->get_op_detail($this->uri->segment(4));
		
		foreach ($sql_op_edit->result() as $row_op)
		{	
			$data['default']['idcab']   = $row_op->idcab;
			$data['default']['vol']     = $row_op->vol;
			$data['default']['type']    = $row_op->type;
			$data['default']['idsupp']  = $row_op->idsupp;
			$data['default']['idtrans'] = $row_op->idtrans;
			$data['default']['vnumb']   = $row_op->vnumb;
			$data['default']['prnumb']  =  $row_op->prnumb;
			$data['default']['donumb']  =  	$row_op->donumb;
			$data['default']['mnfst']   =  $row_op->mnfst;
			$data['default']['inf']     = $row_op->inf;
			$data['default']['adddate'] =  $row_op->adddate;
			$data['default']['chdate']  =  $row_op->chdate;
			$data['default']['chby']    =  $row_op->chby;
			$data['default']['addby']   =  $row_op->addby;
		}
		$data['default']['readonly'] =  'readonly="readonly"';
		
		$data['menu_path'] = 'BRANCH > OIL PURCHASE > LIST OIL PURCHASE';
		$data['form_action']= site_url('cabang/cabang_op/edit_op_proc/'.$this->uri->segment(4));
		$data['link'] = array('link_back' => anchor('cabang/cabang_op/cabang_list_op','Back', array('class' => 'back')));
		$this->load->view('branch/cabang_op_form', $data);
	}
	
	
	/* Cabang edit op process */
	function edit_op_proc()
	{	
		//Check otorisasi transaksi
		$this->fungsi->trans_auth($this->session->userdata('edit'));
		
		//Config utk validasi form op
 $config = array(
                  array(
                     'field'   => 'donumb',
                     'label'   => 'DO Number',
                     'rules'   => ''
                  ),
				  array(
                     'field'   => 'invoice',
                     'label'   => 'Invoice',
                     'rules'   => ''
                  ),
				  array(
                     'field'   => 'mnfst',
                     'label'   => 'Manifest',
                     'rules'   => ''
                  ),
				  array(
                     'field'   => 'vnumb',
                     'label'   => 'Vehicle Number',
                     'rules'   => ''
                  ),
				  array(
                     'field'   => 'vol',
                     'label'   => 'Volume',
                     'rules'   => ''
                  )	  
				  //Not validate form
				  ,array(
                     'field'   => 'idsupp',
                     'label'   => 'ID Supplier',
                     'rules'   => ''
                  )
				   ,array(
                     'field'   => 'idtrans',
                     'label'   => 'ID Transporter',
                     'rules'   => ''
                  )
				    ,array(
                     'field'   => 'adddate',
                     'label'   => 'Add Date',
                     'rules'   => ''
                  )
				    ,array(
                     'field'   => 'chdate',
                     'label'   => 'Change Date',
                     'rules'   => ''
                  )
				    ,array(
                     'field'   => 'chby',
                     'label'   => 'Change By',
                     'rules'   => ''
                  )
				    ,array(
                     'field'   => 'addby',
                     'label'   => 'Add By',
                     'rules'   => ''
                  )
				   //Not validate form
            );
	
	
		$this->form_validation->set_rules($config); 
		
		if ($this->form_validation->run() == TRUE)
			{
			/* Insert data */
			$op = array( 		'c_amt_bapi'=> $this->input->post('c_amt_bapi'),
								'prnumb'	=> $this->input->post('prnumb'),
								'opnumb'	=> $this->input->post('opnumb'),
								'idcab'		=> $this->input->post('idcab'),
								'type'	    => $this->input->post('type'),
								'idsupp'	=> $this->input->post('idsupp'),
								'idtrans'	=> $this->input->post('idtrans'),
								'pur'		=> $this->input->post('pur'),
								'fee'		=> $this->input->post('fee'),
								'freight'	=> $this->input->post('freight'),
								'other'		=> $this->input->post('other'),
								'quantity'	=> $this->input->post('quantity'),
								'invoice'	=> $this->input->post('invoice'),
								'vol'		=> $this->input->post('vol'),
								'vnumb'  	=> $this->input->post('vnumb'),
								'total'	    => $this->input->post('total'),
								'donumb'	=> $this->input->post('donumb'),
								'year'	    => $this->input->post('year'),
								'mnfst'	    => $this->input->post('mnfst'),
								'inf'	    => $this->input->post('inf'),
								'adddate' 	=> date( 'Y-m-d H:i:s'),
								'chdate'	=> $this->input->post('chdate'),
								'chby'		=> $this->input->post('chby'),
								'addby'		=> $this->session->userdata('uid'),
								
						);
			$this->cabang_model->update_op($this->input->post('txt_idop'), $op);
			
			/*Set pesan sukses dan kembali ke menu add op*/
			$this->session->set_flashdata('message_ok', 'Data changed');
			redirect('branch/cabang_op/cabang_list_op');
			}
		else
			{
				$this->edit_supp();
			}
	}
	
}