<?php 
/*
Fungsi : Controller untuk ow
*/

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

class Cabang_ow 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_ow  = 112;
	var $menu_add_ow = 111;
	var $menu_add_waste = 113;
	
/*-------------Inisialisasi variabel--------------*/ 

/*------------Fungsi local------------------------*/


/* 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('branch/branch_model', '', TRUE);
		$this->load->model('admin/admin_model', '', TRUE);
		$this->fungsi->check_login();
	}
	
	
	
	public function index()
	{	
		$this->cabang_add_ow();
		
	}

	/* Cabang add ow */
	function cabang_add_ow()
	{	
		//Otorisasi menu
		$this->fungsi->menu_auth($this->menu_add_ow);
			
		$data['menu_path'] = 'BRANCH > OTHER WASTE > ADD OTHER WASTE PURCHASE';
		$data['form_action']= site_url('branch/cabang_ow/add_ow_proc');
		$this->load->view('branch/cabang_ow_form', $data);
		
		
	}
	
				
	/* Cabang add ow process */
	function add_ow_proc()
	{	
			
		//Check otorisasi transaksi
		$this->fungsi->trans_auth($this->session->userdata('add'));
		
		$data['prev'] = '';
		
		
		/*Get OW counter*/
		$count = array();
		$count = $this->fungsi->get_no_count($this->session->userdata('idcab'), 'OW');
		$noowform = $count[0];
		$noow = $count[1];
		
		//Config utk validasi form ow
	$ow = array(
                  array(
                     'field'   => 'idlimb',
                     'label'   => 'ID Limbah',
                     'rules'   => ''
                  ),
				  array(
                     'field'   => 'invoice',
                     'label'   => 'Invoice',
                     'rules'   => ''
                  ),
				  array(
                     'field'   => 'mnfst',
                     'label'   => 'Manifest',
                     'rules'   => ''
                  ),
				  array(
                     'field'   => 'vnumb',
                     'label'   => 'Vehicle Number',
                     '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'   => 'addby',
                     'label'   => 'Add By',
                     'rules'   => ''
                  )
				   //Not validate form
            );
			
		//Data Type form
		$this->load->helper('form'); 
		
		
	
		//Check validasi form
		$this->form_validation->set_rules($ow); 
		
		if ($this->form_validation->run() == TRUE)
			{
			/* Insert data ow */
			$ow = array( 		
								'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_ow($ow);
			/*Set pesan sukses dan kembali ke menu add op*/
			//$this->session->set_flashdata('prnumb', $this->input->post('prnumb'));
			$this->session->set_flashdata('message_ok', 'Data saved');
			redirect('branch/cabang_add_ow');
			}
		else
			{
				$this->cabang_add_ow();
			}
	}
	
	/* Cabang add ow process */
	function cabang_add_waste()
	
	{	
		//Otorisasi menu
		$this->fungsi->menu_auth($this->menu_add_waste);
		$data['menu_path'] = 'BRANCH > OTHER WASTE > MASTER WASTE';
		$data['form_action']= site_url('branch/cabang_ow/cabang_add_waste');
		$this->load->view('branch/cabang_waste_master_form', $data);
	
		
		/*Get OP counter*/
//		$count = array();
//		$count = $this->fungsi->get_no_count($this->session->userdata('idcab'), 'OP');
//		$noopform = $count[0];
//		$noop = $count[1];
		
		//Config utk validasi form op
	$waste = array(
                  array(
                     'field'   => 'idlimb',
                     'label'   => 'ID Limbah',
                     'rules'   => ''
                  ),
				  array(
                     'field'   => 'invoice',
                     'label'   => 'Invoice',
                     'rules'   => ''
                  ),
				  array(
                     'field'   => 'mnfst',
                     'label'   => 'Manifest',
                     'rules'   => ''
                  ),
				  array(
                     'field'   => 'vnumb',
                     'label'   => 'Vehicle Number',
                     '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'   => 'addby',
                     'label'   => 'Add By',
                     'rules'   => ''
                  )
				   //Not validate form
            );
			
		//Data Type form
		$this->load->helper('form'); 
		
		//Check validasi form
		$this->form_validation->set_rules($waste); 
		
		if ($this->form_validation->run() == TRUE)
			{
			/* Insert data ow */
			$waste = array( 		
								'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_waste($waste);
			$this->session->set_flashdata('message_ok', 'Data saved');
			redirect('branch/cabang_waste_master_form');
			}
		else
			{
				$this->cabang_add_waste();
			}
	}
	

	/* Display list ow */
	function cabang_list_ow()
	{	
		//Check otorisasi
		$this->fungsi->trans_auth($this->session->userdata('view'));
		
		//Otorisasi menu
		$this->fungsi->menu_auth($this->menu_list_ow);
		
		/* Setting Parameter Untuk Paging */
		$uri_segment = 4;
		$offset =    $this->uri->segment($uri_segment);
			
		$ow = $this->cabang_model->get_ow_paging($this->config->item('paging_limit'), $offset)->result();
		$num_rows = $this->cabang_model->count_all_op();
		$config['base_url'] = site_url('branch/cabang_ow/cabang_list_ow/');
		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('ID OW', 'PR Number', 'Vol', 'Options');
				
					foreach ($ow  as $ows)
					{	
			
		$this->table->add_row($ops->idop, $ops->prnumb, $ops->vol, anchor('branch/cabang_ow/edit_ow/'.$ops->idop,'Cancel',array('class' => 'update')).'  '.anchor('branch/cabang_op/edit_op/'.$ops->idop,'Delete',array('class'=> 'delete','onclick'=>"return confirm('Delete OP with ID ".$ops->idop." ?')")));
						
					}
					$data['table'] = $this->table->generate();
					
				}
			else
				{
					$data['message'] = 'No data';
				}		
		$data['menu_path'] = 'BRANCH > OTHER WASTE > LIST OTHER WASTE';
		$this->load->view('branch/cabang_ow_list', $data);
		
	}
	

	
	
	/* Edit op */
	function edit_ow()
	{
		
		//Get data detail op by id cabang
		$sql_ow_edit = $this->cabang_model->get_ow_detail($this->uri->segment(4));
		
		foreach ($sql_ow_edit->result() as $row_ow)
		{	
			$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 > OTHER WASTE > LIST OTHER WASTE TRANSACTION';
		$data['form_action']= site_url('branch/cabang_ow/edit_ow_proc/'.$this->uri->segment(4));
		$data['link'] = array('link_back' => anchor('branch/cabang_ow/cabang_list_ow','Back', array('class' => 'back')));
		$this->load->view('branch/cabang_ow_form', $data);
	}
	
	
	/* Cabang edit op process */
	function edit_ow_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();
			}
	}
	
}