<?php 

/*

Fungsi : Controller untuk create BA

*/



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



class Ppic_ba extends CI_Controller {

	

/*-------------Inisialisasi variabel--------------*/ 

	

	//Menu ID utk keperluan otorisasi

	var $menu_list_ba  = 176;

	var $menu_create_ba = 172;

	

/*-------------Inisialisasi variabel--------------*/ 	



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

	function __construct()

	{

		parent::__construct();
	$this->load->model('ppic/ppic_model', '', TRUE);

		$this->fungsi->check_login();
	}
	public function index()
	{	
		$this->ppic_add_ba();
	}
	


function ppic_add_ba()
	{	
		//Otorisasi menu

		$this->fungsi->menu_auth($this->menu_add_dn);

		$data['menu_path'] = 'CIBITUNG > PPIC > CREATE BA';

		$data['form_action']= site_url('ppic/ppic_ba/add_ba_proc');

		$this->load->view('ppic/ppic_ba_form', $data);
	}
	/* Cabang add ba process */
	function add_ba_proc()
	{				
		//Check otorisasi transaksi
		$this->fungsi->trans_auth($this->session->userdata('add'));	
		$data['prev'] = '';	

//Config utk validasi form SJ

	$config = array(
                  array(
                     'field'   => 'mnfst',
                     'label'   => 'Manifest Number',
                     'rules'   => 'required'
                  ),

				  array(

                     'field'   => 'seal2',
                     'label'   => 'Seal 2',
                     'rules'   => 'required'

                  )

				    ,array(

                     'field'   => 'seal3',
                     'label'   => 'Seal 3',
                     'rules'   => 'required'

                  )

				    ,array(

                     'field'   => 'seal4',
                     'label'   => 'Seal 4',
                     'rules'   => 'required'

                  )
				   //Not validate form

				    ,array(

                     'field'   => 'addby',
                     'label'   => 'Add By',
					 'rules'   => ''

                  )

				  ,array(

                     'field'   => 'adddate',
                     'label'   => 'Add Date',
                     'rules'   => ''

                  )
				   //Not validate form
            );
		//Data Type form
		$this->load->helper('form'); 
	//Check validasi form
		$this->form_validation->set_rules($config); 
		if ($this->form_validation->run() == TRUE)
			{	
				/*Get BA counter*/
				$count = array();
				$count = $this->fungsi->get_no_count($this->session->userdata('idcab'), 'BA');
				$nodnform = $count[0];
				$nodn = $count[1];
		

			/* Insert SAP TP */

			$this->fungsi_sap->fm_create_tp($nodnform, $this->input->post('mnfst'),'303',$this->session->userdata('lgort'),'','PPIC',$this->input->post('vol'),$this->input->post('posting'));			

			if($this->session->userdata('sap_tp_num') != '')

				{
			/* Insert data ba */

			$ba = array( 		'dnnumb'	=> $nodnform,
     							'matdoc'	=> $this->session->userdata('sap_tp_num'),
								'idcab'		=> $this->session->userdata('idcab'),
								'mnfst'	    => $this->input->post('mnfst'),
								'vnumb'		=> $this->input->post('vnumb'),
								'noiso'		=> $this->input->post('noiso'),
								'vol'	    => $this->input->post('vol'),
								'qct'	    => $this->input->post('qct'),
								'qcair'		=> $this->input->post('qcair'),
								'qctbk'		=> $this->input->post('qctbk'),
								'qcbakar'	=> $this->input->post('qcbakar'),	
								'qcresidu'	=> $this->input->post('qcresidu'),
								'driver'	=> $this->input->post('pic'),
								'type'	    => $this->input->post('type'),	
								'quantity'	=> $this->input->post('quantity'),
								'idtrans'	=> $this->input->post('idtrans'),
								'namatrans'	=> $this->input->post('nama_trans'),
								'eta'  		=> date("Y-m-d", strtotime($this->input->post('eta'))),
								'posting'  	=> date("Y-m-d", strtotime($this->input->post('posting'))),
								'seal1'	    => $this->input->post('seal1'),
								'seal2'		=> $this->input->post('seal2'),
								'seal3'		=> $this->input->post('seal3'),
								'seal4'		=> $this->input->post('seal4'),
								'seal5'		=> $this->input->post('seal5'),
								'seal6'		=> $this->input->post('seal6'),
								'year'	    => date("Y", strtotime($this->input->post('posting'))),
								'inf'	    => $this->input->post('inf'),
								'adddate' 	=> date( 'Y-m-d H:i:s'),
								'addby'		=> $this->session->userdata('uid'),

								);

						/*Insert to t_stockbal*/

				$salbal = array( 

								'idcab'		 => $this->session->userdata('idcab'),
								'postdate'  	=> date("Y-m-d", strtotime($this->input->post('posting'))),
								'ref'	    => $nodnform,
								'qty'		 => $this->input->post('vol')* -1,
								'year'	    => date('Y'),
								'addby'		 => $this->session->userdata('uid'),
								'adddate' 	 => date( 'Y-m-d H:i:s'),

								);

				/*Insert SJ Cis*/

				$sjcis = array( 

								'no_sj'	        => $nodnform,
								'matdoc'	    => $this->session->userdata('sap_tp_num'),
								'tgl_input'		=> date("Y-m-d", strtotime($this->input->post('posting'))),
								'no_manifest'	=> $this->input->post('mnfst'),
								'no_pol'		=> $this->input->post('vnumb'),
								'no_iso'		=> $this->input->post('noiso'),
								'vol'		    => $this->input->post('vol'),
								'no_sgl_1'		=> $this->input->post('seal1'),
								'no_sgl_2'		=> $this->input->post('seal2'),
								'no_sgl_3'		=> $this->input->post('seal3'),
								'no_sgl_4'		=> $this->input->post('seal4'),
								'no_sgl_5'		=> $this->input->post('seal5'),
								'no_sgl_5'		=> $this->input->post('seal6'),
								'quantity'		=> $this->input->post('quantity'),
								'transporter'	=> $this->input->post('idtrans'),
								'eta'	    	=> date("Y-m-d", strtotime($this->input->post('eta'))),
								'nama_sopir'	=> $this->input->post('driver'),
								'type'		    => $this->input->post('type'),
								'id_cab'		=> $this->session->userdata('idcab'),

								);

			/*Insert Mutasi PO*/
			//Get next increment
			
			/*Cabang surabaya blom ada OA*/
			/*if	($this->session->userdata('idcab')!=6)			
		{	*/	
				$query_increment = $this->db->query("SHOW TABLE STATUS WHERE name='t_dn'")->result();
				foreach($query_increment as $row_incre);
				$this->fungsi->add_po_mutasi($this->session->userdata('idcab'),$row_incre->Auto_increment,$this->input->post('vol'));
			/*}*/

			$this->cabang_model->insert_sj_cis($sjcis);	

			$this->cabang_model->add_dn($dn);

			$this->cabang_model->add_saldo_branch($salbal);

			/*Set pesan sukses dan kembali ke menu add dn*/

			$this->session->set_flashdata('dnnumb', $this->input->post('dnnumb'));

			$this->session->set_flashdata('message_ok', 'Data saved, SJ Number :'.$nodnform);

			redirect('branch/cabang_dn');
				}

			}

		else

			{
				$this->cabang_add_dn();
			}

}

	/* Display list DN */

	function ppic_list_ba()

	{	
	//Check otorisasi
	$this->fungsi->trans_auth($this->session->userdata('view'));
	
	//Otorisasi menu
		$this->fungsi->menu_auth($this->menu_list_ba);

		/* Setting Parameter Untuk Paging */
		$uri_segment = 4;
		$offset =    $this->uri->segment($uri_segment);
		
		$dn = $this->ppic_model->get_ba_paging($this->session->userdata('idcab'),$this->config->item('paging_limit'), $offset)->result();
		$num_rows = $this->ppic_model->count_all_ba($this->session->userdata('idcab'));
		
		$config['base_url'] = site_url('ppic/ppic_ba/ppic_list_ba/');
		
		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('No. BA', 'Pengirim', 'Manifest', 'Volume', 'Driver', 'Options');

					$atts = array(
              'width'      => '100',
              'height'     => '100',
              'scrollbars' => 'yes',
              'status'     => 'yes',
              'resizable'  => 'yes',
              'screenx'    => '0',
              'screeny'    => '0'
            );

				

					foreach ($ba  as $bas)
					{	
	$this->table->add_row($bas->dnnumb, $bas->cabname, $bas->mnfst, $bas->vol, $bas->driver,  anchor_popup('branch/cabang_dn/print_dn_popup/'.$dns->iddn.'/'.$bas->idcab.'/'.$bas->year,'Print',array('class'=> 'print',$atts),$atts).' '.anchor('branch/cabang_dn/cancel_dn/'.$bas->iddn.'/'.$bas->idcab.'/'.$bas->year,'Cancel',array('class'=> 'delete',$atts),$atts));


					}
					$data['table'] = $this->table->generate();
				    }
			else
				{
					$data['message'] = 'No data';
				}		

		$data['menu_path'] = 'CIBITUNG > PPIC > LIST BERITA ACARA';
		$this->load->view('ppic/ppic_ba_list', $data);
	}	

}