<?php 

/*

Fungsi : Controller untuk delivery note

*/



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



class Cabang_dn 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_dn  = 92;

	var $menu_add_dn = 83;

	

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



/*------------Fungsi lokal------------------------*/	

//Cek valid Vol

public function valid_vol($volume)

{
if( ($this->fungsi->hapus_koma($this->input->post('vol'))) > ($this->fungsi->hapus_koma($this->input->post('totvol'))))
	{
		$this->form_validation->set_message('valid_vol', 'Error, your Volume is bigger then Stok Available');
		return false;
	}

	else

	{
		return true;

	}
}	

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

	function __construct()

	{

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

		$this->fungsi->check_login();
	}
	public function index()
	{	
		$this->cabang_add_dn();
	}
	
/*---------------------------------Cancel DN--------------------------------*/
function cancel_dn()
{
	$data['menu_path'] = 'BRANCH > DELIVERY NOTE > CANCEL DELIVERY NOTE';
	$data['form_action']= site_url('branch/cabang_dn/cancel_dn_proc');
	
	$sqldn = $this->db->query("SELECT * FROM t_dn WHERE iddn = '".$this->uri->segment(4)."'");
	foreach($sqldn->result() as $row);
			
				$data['matdoc'] = $row->matdoc;
				$data['year'] = $row->year;
				$data['dnnumb'] = $row->dnnumb;
				$data['mnfst'] = $row->mnfst;
				$data['vol'] = $row->vol;
				$data['vnumb'] = $row->vnumb;
				$data['driver'] = $row->driver;
				$data['nosj'] = $row->dnnumb;
	$data['link'] = array('link_back' => anchor('branch/cabang_dn/cabang_list_dn','Back', array('class' => 'back')));		
	$this->load->view('branch/cabang_dn_cancel_form', $data);
	
}

function cancel_dn_proc()
{	
	$this->fungsi_sap->fm_cancel_tp($this->input->post('txt_matdoc'),$this->input->post('txt_year'),$this->input->post('txt_postdate'));
	
				if($this->session->userdata('sap_cancel_tp_no') != '')
				{
					/*Cabang surabaya blom ada OA*/
					/*if	($this->session->userdata('idcab')!=6)			
		{	*/	
							/*Cancel mutasi PO*/
							$this->fungsi->cancel_po_mutasi($this->input->post('txt_iddn'));
					/*}*/
					
					/*Insert to t_stockbal*/
					$salbal = array( 
								'idcab'		 => $this->session->userdata('idcab'),
								'postdate'  	=> date("Y-m-d", strtotime($this->input->post('txt_postdate'))),
								'ref'	    => $this->session->userdata('sap_cancel_tp_no'),
								'qty'		 => $this->input->post('txt_vol'),
								'year'	    => date("Y", strtotime($this->input->post('txt_postdate'))),
								'addby'		 => $this->session->userdata('uid'),
								'adddate' 	 => date( 'Y-m-d H:i:s'),
								);
						 $this->cabang_model->add_saldo_branch($salbal);
						 		
					/*Update t_dn*/
					$dn = array( 
								'matdoc_cancel'	=> $this->session->userdata('sap_cancel_tp_no'),
								'cancelby'  	=> $this->session->userdata('uid'),
								'canceldate'	=> date("Y-m-d", strtotime($this->input->post('txt_postdate'))),
								);		
					$this->db->where('iddn', $this->input->post('txt_iddn'));
					$this->db->update('t_dn', $dn); 
					
					/*Update cis SJ*/
					$cis = array( 
								'status'	=> 3
								);		
					$this->cabang_model->update_sj_cis($this->input->post('txt_nosj'),$cis);
					 
					$this->session->set_flashdata('message_ok', 'Delivery note cancelled, material doc. no. :'.$this->session->userdata('sap_cancel_tp_no'));
					redirect('branch/cabang_dn/cabang_list_dn');	
				 
				}

}
/*---------------------------------Cancel DN--------------------------------*/

function cabang_add_dn()
	{	
		//Otorisasi menu

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

		$data['menu_path'] = 'BRANCH > DELIVERY NOTE > CREATE DELIVERY NOTE';

		$data['form_action']= site_url('branch/cabang_dn/add_dn_proc');

		$this->load->view('branch/cabang_dn_form', $data);
	}
	/* Cabang add dn process */
	function add_dn_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'   => 'eta',
                     'label'   => 'Estimate Time Arrival',
                     'rules'   => 'required'

                  ),
				   array(
                     'field'   => 'posting',
                     'label'   => 'Posting Date Available',
                     'rules'   => 'required'

                  ),

				  array(
                     'field'   => 'vol',
                     'label'   => 'Volume',
                     'rules'   => 'required|callback_valid_vol'

                  )

				  //Not validate form

				    ,array(

                     'field'   => 'seal1',
                     'label'   => 'Seal 1',
                     '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'

                  )

				    ,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 SJ counter*/
				$count = array();
				$count = $this->fungsi->get_no_count($this->session->userdata('idcab'), 'SJ');
				$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 dn */

			$dn = 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'),
								'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();
			}

	}
/*Print SJ*/

function print_dn_popup()

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

	//Get data for print stb by no stb & year

	$query_d = $this->cabang_model->get_list_dn_print_paging($this->uri->segment(4),$this->uri->segment(5),$this->uri->segment(6));
	//$query_d = $this->cabang_model->get_list_dn_print_paging($this->uri->segment(6),$this->uri->segment(7),$this->uri->segment(8));
	//$query_dn = $this->cabang_model->get_list_dn_print_paging('1','3','2013');
	
	//print_r($query_dn->vnumb);
	foreach ($query_d->result() as $row_dn)

		{

			$data['txt_iddn']     = $row_dn->iddn;
			$data['txt_dnnumb']   = $row_dn->dnnumb;
			$data['txt_mnfst']    = $row_dn->mnfst;
			$data['txt_adddate']  = date("d.m.Y",   strtotime($row_dn->adddate));	
			$data['txt_vnumb']    = $row_dn->vnumb;
			$data['txt_noiso']    = $row_dn->noiso;
			$data['txt_inf']      = $row_dn->inf;
			$data['txt_vol']      = $row_dn->vol;
			$data['txt_seal1']    = $row_dn->seal1;
			$data['txt_seal2']    = $row_dn->seal2;
			$data['txt_seal3']    = $row_dn->seal3;
			$data['txt_seal4']    = $row_dn->seal4;
        	$data['txt_seal5']    = $row_dn->seal5;
			$data['txt_seal6']    = $row_dn->seal6;
			$data['txt_driver']   = $row_dn->driver;
			$data['txt_type']     = $row_dn->type;
			$data['txt_year']     = $row_dn->year;
			$data['txt_namatrans']     = $row_dn->namatrans;
			$data['txt_quantity'] = $row_dn->quantity;
			$data['txt_addby']    = $row_dn->addby;
			$data['txt_idcab']    = $row_dn->idcab;

			}

//Get data detail cabang by id cabang

				$sql_cab_detail = $this->admin_model->get_cab_detail($row_dn->idcab);
				foreach ($sql_cab_detail->result() as $row_cab)

				{	
					$data['txt_cabname'] = $row_cab->cabname;
				}
//Mutasi PO
/*$data['mutasi_po'] = $this->db->query("select * from t_po_mutasi WHERE  idcab ='".$this->uri->segment(5)."' AND iddn = '".$this->uri->segment(4)."' ORDER BY ebeln")->result();*/

                $this->load->view('popup/popup_sj', $data);

}

	/* Display list DN */

	function cabang_list_dn()

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

		/* Setting Parameter Untuk Paging */
		$uri_segment = 4;
		$offset =    $this->uri->segment($uri_segment);
		
		$dn = $this->cabang_model->get_dn_paging($this->session->userdata('idcab'),$this->config->item('paging_limit'), $offset)->result();
		$num_rows = $this->cabang_model->count_all_dn($this->session->userdata('idcab'));
		
		$config['base_url'] = site_url('branch/cabang_dn/cabang_list_dn/');
		
		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('SJ Number', 'Branch', 'Manifest', 'Volume', 'Driver', 'Options');

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

				

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


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

		$data['menu_path'] = 'BRANCH > DELIVERY NOTE > LIST DELIVERY NOTE';
		$this->load->view('branch/cabang_dn_list', $data);
	}	

}