<?php 

/*

Fungsi : Controller untuk create BA

*/



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



class Security_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('security/security_model', '', TRUE);

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


function security_add_ba()
	{	
		//Otorisasi menu

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

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

		$data['form_action']= site_url('security/security_ba/add_ba_proc');

		$this->load->view('security/security_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 BA

	$config = array(
                  array(
                     'field'   => 'no_manifest',
                     'label'   => 'Manifest Number',
                     '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 BA counter*/
				$count = array();
				$count = $this->fungsi->get_no_count($this->session->userdata('idcab'), 'BA');
				$nobaform = $count[0];
				$noba = $count[1];
		
				{
			/* Insert data ba */

			$ba_security = array( 		'no_manifest'=> $this->input->post('no_manifest'),
								'vnumb'		=> $this->input->post('vnumb'),
								'vol_security'	    => $this->input->post('vol_security'),
								'driver'	=> $this->input->post('driver'),
								'type'	    => $this->input->post('type'),	
								'jam_datang_security'  	=> date("Y-m-d", strtotime($this->input->post('jam_datang_security'))),
								'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'),

								);

						
			$this->cabang_model->add_ba_security($ba_security);


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

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

			redirect('security/security_add_ba');
				}

			}

		else

			{
				$this->security_add_ba();
			}

}

	/* Display list BA */

	function security_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->security_model->get_ba_paging($this->session->userdata('idcab'),$this->config->item('paging_limit'), $offset)->result();
		$num_rows = $this->security_model->count_all_ba($this->session->userdata('idcab'));
		
		$config['base_url'] = site_url('security/security_ba/security_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 > SECURITY > LIST BERITA ACARA';
		$this->load->view('security/security_ba_list', $data);
	}	

}