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

// error_reporting(0);

class Generate_bill extends CI_Controller 

{

	public function __construct()

	{

	 	parent::__construct();	

		$this->load->model('Generate_bill_model');



		$this->load->library('Generate_bill_lib'); 

		$this->load->library("pagination");

		//$this->load->model('General_masters_model');

	}



	public function generate_bill()

	{  

		if(!$this->session->userdata('logged_in')) 

			redirect('Login');

		//else

            //redirect('login?goto='."http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]");

		$url		=	'Generate_bill/generate_bill';

		$order_field= 	'view';

		$join_array = 	array(0=>array(0=>USER_PERM_TBL,1=>"up_menu_id=menu_id",2=>''));

		$result 	=	$this->Generate_bill_model->select_data(MENU_TBL,'*',array("url"=>$url,"up_emp_id"=>$this->session->userdata('user_id')),'1',$join_array,$order_field,"DESC",'','','');

		if(empty($result))

		{

			$data['page_title'] 	=	'Warning';

	        $data['page_name']  	= 	'warning';

	        $data['page_master']	= 	'Dashboard';

	        $data['page_master_url'] = 	'Dashboard';

	        $data['page_function']	=	'';

			$this->load->view('template/default', $data);

		}

		else

		{	

			$condition 				=	array();

			

			if($this->input->post('search')){



				if($this->input->post('bill_created_date')!=''){

					$bill_created_date = date("Y-m-d", strtotime($this->input->post('bill_created_date')));

				}



				else{

					$bill_created_date = '';

				}



				$search_data = array(

					'bill_id'              => $this->input->post('bill_id'),

					'bill_created_date'    => $bill_created_date,

					'bill_status'     	   => $this->input->post('bill_status')

					

				);



				$this->session->set_userdata('search_criteria_data',$search_data);

			}

			else{



				$search_data = array(

					'bill_id'               => '',

					'bill_created_date'     => '',

					'bill_status'           => ''



				);

				$this->session->set_userdata('search_criteria_data',$search_data);

			}



			

			if ($this->session->userdata('search_criteria_data')){

				$GetData = $this->session->userdata('search_criteria_data');

			}



			$data['search_data']  = $GetData;



			// var_dump($GetData);

			// 			die();



			$config = array();

			$config['base_url']        = base_url().'Generate_bill/generate_bill';

			if($GetData!=''){

				$config['total_rows']      = $this->Generate_bill_model->search_record_count($GetData);

			}

			else{

				$config['total_rows']      = $this->Generate_bill_model->record_count();

			}



			$config['per_page']        = 50;

        	$config['uri_segment']     = 3;





        	$config['full_tag_open'] = '<ul class="pagination" style="float:right;">';

        	$config['full_tag_close'] = '</ul>';

        	$config['first_link'] = false;

        	$config['last_link'] = false;

        	$config['first_tag_open'] = '<li>';

        	$config['first_tag_close'] = '</li>';

        	$config['prev_link'] = '&laquo';

        	$config['prev_tag_open'] = '<li class="prev">';

        	$config['prev_tag_close'] = '</li>';

        	$config['next_link'] = '&raquo';

        	$config['next_tag_open'] = '<li>';

        	$config['next_tag_close'] = '</li>';

        	$config['last_tag_open'] = '<li>';

        	$config['last_tag_close'] = '</li>';

        	$config['cur_tag_open'] = '<li class="active"><a href="#">';

        	$config['cur_tag_close'] = '</a></li>';

        	$config['num_tag_open'] = '<li>';

        	$config['num_tag_close'] = '</li>';























        	$this->pagination->initialize($config);

        	$page                      = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;

        	$data['bill_details']  = $this->Generate_bill_model->select_bill_details($config["per_page"], $page, $GetData);

        	$data["links"]             = $this->pagination->create_links();



			// $data['bill_details']  = $this->Generate_bill_model->select_bill_details($search_data);

			// var_dump($data['bill_details']);

			// die();

			$data['billno']  = $this->Generate_bill_model->select_bill_no();

			// var_dump($data['search_data']);

			// die();

			$data['page_title'] 	=	'Generate Bill';

		    $data['page_name']  	= 	'generate_bill/bill-lists';

		    $data['page_master']	= 	'Generate Bill';

		    $data['page_master_url'] = 	'Generate_Bill/generate_bill';

		    $data['page_function']	=	'List';

			$this->load->view('template/default', $data);

		}

	}





	public function add_bill()

	{



		//$this->load->library('ckeditor');

		//$this->load->library('ckfinder');







		//$this->ckeditor->basePath = base_url().'assets/ckeditor/';

		// $this->ckeditor->config['toolbar'] = array(

		               // array('FontSize' ) );

		//$this->ckeditor->config['language'] = 'it';

		//$this->ckeditor->config['width'] = '730px';

		//$this->ckeditor->config['height'] = '300px';            



		//Add Ckfinder to Ckeditor

		//$this->ckfinder->SetupCKEditor($this->ckeditor,'../../assets/ckfinder/'); 		

		if(!$this->session->userdata('logged_in')) 

			redirect('Login');

		//else

            //redirect('login?goto='."http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]");			

		$url		=	'Generate_bill/generate_bill';

		$order_field= 	'add';

		$join_array = 	array(0=>array(0=>USER_PERM_TBL,1=>"up_menu_id=menu_id",2=>''));

		$result 	=	$this->Generate_bill_model->select_data(MENU_TBL,'*',array("url"=>$url,"up_emp_id"=>$this->session->userdata('user_id')),'1',$join_array,$order_field,"DESC",'','','');

		if(empty($result))

		{

			$data['page_title'] 	=	'Warning';

	        $data['page_name']  	= 	'warning';

	        $data['page_master']	= 	'Dashboard';

	        $data['page_master_url'] = 	'Dashboard';

	        $data['page_function']	=	'';

			$this->load->view('template/default', $data);

		}

		else

		{

			$date_format_text =array(0=>"dd/mm/yyyy",1=>"mm/dd/yyyy",2=>"yyyy/mm/dd",3=>"dd-mm-yyyy",4=>"mm-dd-yyyy",5=>"yyyy-mm-dd");

			if ($this->input->post('submit'))

			{	

				//$data['country_drpdwn'] =	$this->General_masters_model->select_data(COUNTRY_TBL,$select='country_id,name',array("country_id"=>$_POST['cmp_country']), $by_row=0, array(),'sortname',"ASC",'', 0, null);

				//$data['state_drpdwn']   =	$this->General_masters_model->select_data(STATE_TBL,$select='state_id,state',array("state_id"=>$_POST['cmp_state']), $by_row=0, array(),'state',"ASC",'', 0, null);					

				//$data['currency_drpdwn']=	$this->General_masters_model->select_data(CURRENCY_TBL,$select='currency_id,currency,currency_code',array("currency_id"=>$_POST['cmp_currency']), $by_row=0, array(),'currency',"ASC",'', 0, null);					

				$this->form_validation->set_error_delimiters('<div style="color:red;text-align:center" class="error"><strong>', '</strong></div>');

				$this->form_validation->set_rules($this->get_bill_rule(0));

				if ($this->form_validation->run('add_bill') == true) 

				{

					$config['upload_path']   = 'uploads/company/';

					$config['allowed_types'] = 'gif|jpg|png';

					$config['max_size']		 = '5000';

					$config['max_width']  	 = '10240';

					$config['max_height']    = '7680';

					$this->load->library('upload', $config);

					$logo_name			 	 = "";

					

					$branch_id = $this->input->post("bill_brnch_id");

					// echo $branch_id;

					// die();

					$select_max_bill_no = $this->Generate_bill_model->select_max_bill_no($branch_id);

					//var_dump($select_max_bill_no);

					//die();

					$bill_no = $select_max_bill_no[0]['bill_no'];

					//var_dump($bill_no);



					if($bill_no!=''){





						$insert_array	=	array(	"bill_brnch_id"				=>	$this->input->post("bill_brnch_id"),

													"bill_company_id"			=>	$this->input->post("bill_company_id"),

													"bill_cnsgnr_id"			=>	$this->input->post("bill_cnsgnr_id"),

													"bill_cnsgne_id"			=>	$this->input->post("bill_cnsgne_id"),

													"bill_notify_id"			=>	$this->input->post("bill_notify_id"),

													"bill_notify_party2"			=>	$this->input->post("bill_notify_party2"),

													"bill_port_id"			    =>	$this->input->post("bill_port_id"),

													//"pre_carriage"			    =>	$this->input->post("pre_carriage"),

													"place_receipt"			    =>	$this->input->post("place_receipt"),

													"ocean_vessel"			    =>	$this->input->post("ocean_vessel"),

													"voyage"			    	=>	$this->input->post("voyage"),

													"port_of_loading"			=>	$this->input->post("port_of_loading"),

													"place_of_delivery"			=>	$this->input->post("place_of_delivery"),

													"no_of_bl"			    	=>	$this->input->post("no_of_bl"),

													//"freight_payment"			=>	$this->input->post("freight_payment"),

													"freight_charges"			=>	$this->input->post("freight_charges"),

													"delivery_of_goods"			=>	$this->input->post("delivery_of_goods"),

													"place_issue"			    =>	$this->input->post("place_issue"),

													// "bill_products"			    =>	$this->input->post("content"),

													"bill_no"			        =>	++$bill_no,

													"bill_created_date"		    =>	date('Y-m-d H:i:s'),

													"date_of_issue"             =>  $this->input->post("date_of_issue"),

													"container_no"			    =>	$this->input->post("container_no"),

													"goods"			            =>	$this->input->post("goods"),

													"gross_weight"			    =>	$this->input->post("gross_weight"),

													"cube_measure"			    =>	$this->input->post("cube_measure"),

													"desc_goods"                =>  $this->input->post("desc_goods"),

													"temp_cntrl"                =>  $this->input->post("temp_cntrl")

												);

					// var_dump($insert_array);

					// die();

						

							$this->Generate_bill_model->save_data(BILL_TBL,$insert_array,array(),0);

							$this->session->set_flashdata('msg', "Bill Added Successfully..!");

							redirect('Generate_bill/generate_bill');

						}

						else{

							$insert_array	=	array(	"bill_brnch_id"			=>	$this->input->post("bill_brnch_id"),

													"bill_company_id"			=>	$this->input->post("bill_company_id"),

													"bill_cnsgnr_id"			=>	$this->input->post("bill_cnsgnr_id"),

													"bill_cnsgne_id"			=>	$this->input->post("bill_cnsgne_id"),

													"bill_notify_id"			=>	$this->input->post("bill_notify_id"),

													"bill_notify_party2"			=>	$this->input->post("bill_notify_party2"),

													"bill_port_id"			    =>	$this->input->post("bill_port_id"),



													//"pre_carriage"			    =>	$this->input->post("pre_carriage"),

													"place_receipt"			    =>	$this->input->post("place_receipt"),

													"ocean_vessel"			    =>	$this->input->post("ocean_vessel"),

													"voyage"			    	=>	$this->input->post("voyage"),

													"port_of_loading"			=>	$this->input->post("port_of_loading"),

													"place_of_delivery"			=>	$this->input->post("place_of_delivery"),

													"no_of_bl"			    	=>	$this->input->post("no_of_bl"),

													//"freight_payment"			=>	$this->input->post("freight_payment"),

													"freight_charges"			=>	$this->input->post("freight_charges"),

													"delivery_of_goods"			=>	$this->input->post("delivery_of_goods"),

													"place_issue"			    =>	$this->input->post("place_issue"),



													// "bill_products"			    =>	$this->input->post("content"),

													"bill_no"			        =>	$this->input->post("branch_bill_no"),

													"bill_created_date"		    =>	date('Y-m-d H:i:s'),

													"date_of_issue"             =>  $this->input->post("date_of_issue"),

													"container_no"			    =>	$this->input->post("container_no"),

													"goods"			            =>	$this->input->post("goods"),

													"gross_weight"			    =>	$this->input->post("gross_weight"),

													"cube_measure"			    =>	$this->input->post("cube_measure"),

													"desc_goods"                =>  $this->input->post("desc_goods"),

													"temp_cntrl"                =>  $this->input->post("temp_cntrl")

												);

					// 	var_dump($insert_array);

					// die();

						

							$this->Generate_bill_model->save_data(BILL_TBL,$insert_array,array(),0);

							$this->session->set_flashdata('msg', "Bill Added Successfully..!");

							redirect('Generate_bill/generate_bill');

						}

					

				}

				else

				{

					$this->form_validation->set_message('rule', 'Error Message');

				}

			}

// 				$data['product_content'] = '<table border="0" cellpadding="0" cellspacing="0" style="width: 100%;">

// 	<tbody>

// 		<tr>

// 			<td style="width:29%;"><span style="font-size:11px"><strong>Container Nos. / Marks &amp; Nos.</strong></span><p>&nbsp;</p></td>

// 			<td style="width: 42%;"><span style="font-size:11px"><strong>No.of Containers / Packages / Description of Goods</strong></span><p>&nbsp;</p></td>

// 			<td style="width: 19%;"><span style="font-size:11px"><strong>Gross Weight.</strong></span><p>&nbsp;</p></td>

// 			<td style="width: 10%;"><strong><span style="font-size:11px">Cube (m3)</span></strong><p>&nbsp;</p></td>

// 		</tr>

// 		<tr>

// 			<td><span style="font-size:10px;">&nbsp;</span></td>

// 			<td><span style="font-size:10px;">&nbsp;</span></td>

// 			<td><span style="font-size:10px;">&nbsp;</span></td>

// 			<td><span style="font-size:10px;">&nbsp;</span></td>

// 		</tr>

// 	</tbody>

// </table>



// <p>&nbsp;</p>

// ';



	// var_dump($data['product_content']);

	// die();



			//$data['recent_list'] 	=	$this->Generate_bill_model->select_data(BILL_TBL,$select='cmp_id,cmp_name,cmp_pol,cmp_shrt_code',array(), $by_row=0, array(),'cmp_id',"DESC",'', 10,0);

			$data['branch_bill_no'] = $this->Generate_bill_model->select_data(BRANCH_TBL,$select='branch_bill_no',array(), $by_row=0, array(),'branch_name',"ASC",'', 0, null);

		

			$data['branch_drpdwn']  =	$this->Generate_bill_model->select_data(BRANCH_TBL,$select='branch_id,branch_name,branch_bill_no',array(), $by_row=0, array(),'branch_name',"ASC",'', 0, null);

			$data['company_drpdwn'] =	$this->Generate_bill_model->select_data(COMPANY_TBL,$select='cmp_id,cmp_name',array(), $by_row=0, array(),'cmp_name',"ASC",'', 0, null);

			$data['consignor_drpdwn'] =	$this->Generate_bill_model->select_data(CONSIGNOR_TBL,$select='consignor_id,consignor_name',array(), $by_row=0, array(),'consignor_name',"ASC",'', 0, null);

			$data['consignee_drpdwn'] =	$this->Generate_bill_model->select_data(CONSIGNEE_TBL,$select='consignee_id,consignee_name',array(), $by_row=0, array(),'consignee_name',"ASC",'', 0, null);

			$data['notify_drpdwn'] =	$this->Generate_bill_model->select_data(NOTIFY_TBL,$select='notify_id,notify_name',array(), $by_row=0, array(),'notify_name',"ASC",'', 0, null);

			$data['portname_drpdwn'] =	$this->Generate_bill_model->select_data(PORT_TBL,$select='port_of_dis_id,port_of_dis_name',array(), $by_row=0, array(),'port_of_dis_name',"ASC",'', 0, null);



			$data['date_format_text']=	$date_format_text;

			$data['page_title'] 	=	'Bill';

		    $data['page_name']  	= 	'generate_bill/add-bill';

		    $data['page_master']	= 	'Generate Bill';

		    $data['page_master_url'] = 	'Generate_Bill/add_bill';

		    $data['page_function']	=	'Add';

			$this->load->view('template/default', $data);

		}

	}





	public function edit_bill()

	{	





		$this->load->library('ckeditor');

		$this->load->library('ckfinder');







		$this->ckeditor->basePath = base_url().'assets/ckeditor/';

		// $this->ckeditor->config['toolbar'] = array(

		//                 array( 'Source', '-', 'Bold', 'Italic', 'Underline', '-','Cut','Copy','Paste','PasteText','PasteFromWord','-','Undo','Redo','-','NumberedList','BulletedList' )

		//                                                     );

		$this->ckeditor->config['language'] = 'it';

		$this->ckeditor->config['width'] = '730px';

		$this->ckeditor->config['height'] = '300px';            



		//Add Ckfinder to Ckeditor

		$this->ckfinder->SetupCKEditor($this->ckeditor,'../../assets/ckfinder/'); 	

		if(!$this->session->userdata('logged_in')) 

			redirect('Login');

		//else

            //redirect('login?goto='."http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]");

		$url		=	'Generate_bill/generate_bill';

		$order_field= 	'edit';

		$join_array = 	array(0=>array(0=>USER_PERM_TBL,1=>"up_menu_id=menu_id",2=>''));

		$result 	=	$this->Generate_bill_model->select_data(MENU_TBL,'*',array("url"=>$url,"up_emp_id"=>$this->session->userdata('user_id')),'1',$join_array,$order_field,"DESC",'','','');

		if(empty($result))

		{

			$data['page_title'] 	=	'Warning';

	        $data['page_name']  	= 	'warning';

	        $data['page_master']	= 	'Dashboard';

	        $data['page_master_url'] = 	'Dashboard';

	        $data['page_function']	=	'';

			$this->load->view('template/default', $data);

		}

		else

		{	

			$data['branch_drpdwn']  =	$this->Generate_bill_model->select_data(BRANCH_TBL,$select='branch_id,branch_name,branch_bill_no',array(), $by_row=0, array(),'branch_name',"ASC",'', 0, null);

			$data['company_drpdwn'] =	$this->Generate_bill_model->select_data(COMPANY_TBL,$select='cmp_id,cmp_name',array(), $by_row=0, array(),'cmp_name',"ASC",'', 0, null);

			$data['consignor_drpdwn'] =	$this->Generate_bill_model->select_data(CONSIGNOR_TBL,$select='consignor_id,consignor_name',array(), $by_row=0, array(),'consignor_name',"ASC",'', 0, null);

			$data['consignee_drpdwn'] =	$this->Generate_bill_model->select_data(CONSIGNEE_TBL,$select='consignee_id,consignee_name',array(), $by_row=0, array(),'consignee_name',"ASC",'', 0, null);

			$data['notify_drpdwn'] =	$this->Generate_bill_model->select_data(NOTIFY_TBL,$select='notify_id,notify_name',array(), $by_row=0, array(),'notify_name',"ASC",'', 0, null);

			$data['portname_drpdwn'] =	$this->Generate_bill_model->select_data(PORT_TBL,$select='port_of_dis_id,port_of_dis_name',array(), $by_row=0, array(),'port_of_dis_name',"ASC",'', 0, null);



			$bill_id 				=	$this->uri->segment(3);

			$data['bill_id']		=	$bill_id;



			

			$data['edit_bill']    = 	$this->Generate_bill_model->select_data(BILL_TBL,'*',array("bill_id"=>$bill_id),1, array(),'',"",'', '','');

			$data['branch_name']  =  $this->Generate_bill_model->select_brnach_name($bill_id);

			//$branch_cmp_id 			=	$data['edit_branch']->branch_cmp_id;

			//$data['recent_list'] 	=	$this->General_masters_model->select_data(BRANCH_TBL,$select='branch_id,branch_name,branch_pol,branch_shrt_code',array(), $by_row=0, array(),'branch_id',"DESC",'', 10,0);

			//$data['company_drpdwn'] =	$this->General_masters_model->select_data(COMPANY_TBL,$select='cmp_id,cmp_name',array("cmp_id" => $branch_cmp_id), $by_row=0, array(),'cmp_name',"ASC",'', 0, null);

			$data['page_title'] 	=	'Bill';

		    $data['page_name']  	= 	'generate_bill/edit-bill';

		    $data['page_master']	= 	'Generate Bill';

		    $data['page_master_url'] = 	'generate_bill/edit_bill';

		    $data['page_function']	=	'Edit';

			if ($this->input->post('submit'))

			{			

				$this->form_validation->set_error_delimiters('<div style="color:red;text-align:center" class="error"><strong>', '</strong></div>');

				$this->form_validation->set_rules($this->get_bill_rule(1));

				if ($this->form_validation->run('edit_bill') == true) 

				{





					//$branch_id = $this->input->post("bill_brnch_id");

										//$select_max_bill_no = $this->Generate_bill_model->select_max_bill_no($branch_id);

					//var_dump($select_max_bill_no);

					//die();

					//$bill_no = $select_max_bill_no[0]['bill_no'];

					//var_dump($bill_no);

   					//if($bill_no!=''){



   						// $insert_array	=	array(	"bill_brnch_id"				=>	$this->input->post("bill_brnch_id"),

									// 			"bill_company_id"			=>	$this->input->post("bill_company_id"),

									// 			"bill_cnsgnr_id"			=>	$this->input->post("bill_cnsgnr_id"),

									// 			"bill_cnsgne_id"			=>	$this->input->post("bill_cnsgne_id"),

									// 			"bill_notify_id"			=>	$this->input->post("bill_notify_id"),

									// 			"bill_port_id"			    =>	$this->input->post("bill_port_id"),



									// 			"pre_carriage"			    =>	$this->input->post("pre_carriage"),

									// 			"place_receipt"			    =>	$this->input->post("place_receipt"),

									// 			"ocean_vessel"			    =>	$this->input->post("ocean_vessel"),

									// 			"voyage"			    	=>	$this->input->post("voyage"),

									// 			"port_of_loading"			=>	$this->input->post("port_of_loading"),

									// 			"place_of_delivery"			=>	$this->input->post("place_of_delivery"),

									// 			"no_of_bl"			    	=>	$this->input->post("no_of_bl"),

									// 			"freight_payment"			=>	$this->input->post("freight_payment"),

									// 			"freight_charges"			=>	$this->input->post("freight_charges"),

									// 			"delivery_of_goods"			=>	$this->input->post("delivery_of_goods"),

									// 			"place_issue"			    =>	$this->input->post("place_issue"),

									// 			"date_of_issue"             =>  $this->input->post("date_of_issue"),

									// 				"container_no"			    =>	$this->input->post("container_no"),

									// 				"goods"			            =>	$this->input->post("goods"),

									// 				"gross_weight"			    =>	$this->input->post("gross_weight"),

									// 				"cube_measure"			    =>	$this->input->post("cube_measure"),





									// 			// "bill_products"			    =>	$this->input->post("content"),

									// 			//"bill_no"			        =>	++$bill_no,

									// 		);

					//$bill_id 		=	$this->input->post("bill_id");

					//$this->Generate_bill_model->save_data(BILL_TBL,$insert_array,array("bill_id"=>$bill_id),$bill_id);

					//$this->session->set_flashdata('msg', "Bill Updated Successfully..!");

					//redirect('Generate_bill/generate_bill');

					//}

					//else{

						$insert_array	=	array(	"bill_brnch_id"				=>	$this->input->post("bill_brnch_id"),

												"bill_company_id"			=>	$this->input->post("bill_company_id"),

												"bill_cnsgnr_id"			=>	$this->input->post("bill_cnsgnr_id"),

												"bill_cnsgne_id"			=>	$this->input->post("bill_cnsgne_id"),

												"bill_notify_id"			=>	$this->input->post("bill_notify_id"),

												"bill_notify_party2"			=>	$this->input->post("bill_notify_party2"),

												"bill_port_id"			    =>	$this->input->post("bill_port_id"),



												"pre_carriage"			    =>	$this->input->post("pre_carriage"),

												"place_receipt"			    =>	$this->input->post("place_receipt"),

												"ocean_vessel"			    =>	$this->input->post("ocean_vessel"),

												"voyage"			    	=>	$this->input->post("voyage"),

												"port_of_loading"			=>	$this->input->post("port_of_loading"),

												"place_of_delivery"			=>	$this->input->post("place_of_delivery"),

												"no_of_bl"			    	=>	$this->input->post("no_of_bl"),

												"freight_payment"			=>	$this->input->post("freight_payment"),

												"freight_charges"			=>	$this->input->post("freight_charges"),

												"delivery_of_goods"			=>	$this->input->post("delivery_of_goods"),

												"place_issue"			    =>	$this->input->post("place_issue"),

												"date_of_issue"             =>  $this->input->post("date_of_issue"),

												

													"container_no"			    =>	$this->input->post("container_no"),

													"goods"			            =>	$this->input->post("goods"),

													"gross_weight"			    =>	$this->input->post("gross_weight"),

													"cube_measure"			    =>	$this->input->post("cube_measure"),

													"desc_goods"                =>  $this->input->post("desc_goods"),

													"temp_cntrl"                =>  $this->input->post("temp_cntrl")





												// "bill_products"			    =>	$this->input->post("content"),

												//"bill_no"			        =>	$this->input->post("branch_bill_no"),

											);

					$bill_id 		=	$this->input->post("bill_id");

					$this->Generate_bill_model->save_data(BILL_TBL,$insert_array,array("bill_id"=>$bill_id),$bill_id);

					$this->session->set_flashdata('msg', "Bill Updated Successfully..!");

					redirect('Generate_bill/generate_bill');

					//}

				}



				else

				{

					$this->form_validation->set_message('rule', 'Error Message');

				}

			}

		}

		$this->load->view('template/default', $data);

	}





	public function view_bill(){



		$this->load->library('ckeditor');

		$this->load->library('ckfinder');







		$this->ckeditor->basePath = base_url().'assets/ckeditor/';

		// $this->ckeditor->config['toolbar'] = array(

		//                 array( 'Source', '-', 'Bold', 'Italic', 'Underline', '-','Cut','Copy','Paste','PasteText','PasteFromWord','-','Undo','Redo','-','NumberedList','BulletedList' )

		//                                                     );

		$this->ckeditor->config['language'] = 'it';

		$this->ckeditor->config['width'] = '730px';

		$this->ckeditor->config['height'] = '300px';            



		//Add Ckfinder to Ckeditor

		$this->ckfinder->SetupCKEditor($this->ckeditor,'../../assets/ckfinder/'); 	

		if(!$this->session->userdata('logged_in')) 

			redirect('Login');

		//else

            //redirect('login?goto='."http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]");

		$url		=	'Generate_bill/generate_bill';

		$order_field= 	'edit';

		$join_array = 	array(0=>array(0=>USER_PERM_TBL,1=>"up_menu_id=menu_id",2=>''));

		$result 	=	$this->Generate_bill_model->select_data(MENU_TBL,'*',array("url"=>$url,"up_emp_id"=>$this->session->userdata('user_id')),'1',$join_array,$order_field,"DESC",'','','');

		if(empty($result))

		{

			$data['page_title'] 	=	'Warning';

	        $data['page_name']  	= 	'warning';

	        $data['page_master']	= 	'Dashboard';

	        $data['page_master_url'] = 	'Dashboard';

	        $data['page_function']	=	'';

			$this->load->view('template/default', $data);

		}

		else

		{	

			$data['branch_drpdwn']  =	$this->Generate_bill_model->select_data(BRANCH_TBL,$select='branch_id,branch_name,branch_bill_no',array(), $by_row=0, array(),'branch_name',"ASC",'', 0, null);

			$data['company_drpdwn'] =	$this->Generate_bill_model->select_data(COMPANY_TBL,$select='cmp_id,cmp_name',array(), $by_row=0, array(),'cmp_name',"ASC",'', 0, null);

			$data['consignor_drpdwn'] =	$this->Generate_bill_model->select_data(CONSIGNOR_TBL,$select='consignor_id,consignor_name',array(), $by_row=0, array(),'consignor_name',"ASC",'', 0, null);

			$data['consignee_drpdwn'] =	$this->Generate_bill_model->select_data(CONSIGNEE_TBL,$select='consignee_id,consignee_name',array(), $by_row=0, array(),'consignee_name',"ASC",'', 0, null);

			$data['notify_drpdwn'] =	$this->Generate_bill_model->select_data(NOTIFY_TBL,$select='notify_id,notify_name',array(), $by_row=0, array(),'notify_name',"ASC",'', 0, null);

			$data['portname_drpdwn'] =	$this->Generate_bill_model->select_data(PORT_TBL,$select='port_of_dis_id,port_of_dis_name',array(), $by_row=0, array(),'port_of_dis_name',"ASC",'', 0, null);



			$bill_id 				=	$this->uri->segment(3);

			$data['bill_id']		=	$bill_id;



			$data['branch_name']  =  $this->Generate_bill_model->select_brnach_name($bill_id);

			$data['view_bill']    = 	$this->Generate_bill_model->select_data(BILL_TBL,'*',array("bill_id"=>$bill_id),1, array(),'',"",'', '','');

			//$branch_cmp_id 			=	$data['edit_branch']->branch_cmp_id;

			//$data['recent_list'] 	=	$this->General_masters_model->select_data(BRANCH_TBL,$select='branch_id,branch_name,branch_pol,branch_shrt_code',array(), $by_row=0, array(),'branch_id',"DESC",'', 10,0);

			//$data['company_drpdwn'] =	$this->General_masters_model->select_data(COMPANY_TBL,$select='cmp_id,cmp_name',array("cmp_id" => $branch_cmp_id), $by_row=0, array(),'cmp_name',"ASC",'', 0, null);

			$data['page_title'] 	=	'Bill';

		    $data['page_name']  	= 	'generate_bill/view-bill';

		    $data['page_master']	= 	'Generate Bill';

		    $data['page_master_url'] = 	'generate_bill/view_bill';

		    $data['page_function']	=	'View';

			if ($this->input->post('submit'))

			{			

				// $this->form_validation->set_error_delimiters('<div style="color:red;text-align:center" class="error"><strong>', '</strong></div>');

				// $this->form_validation->set_rules($this->get_bill_rule(1));

				// if ($this->form_validation->run('edit_bill') == true) 

				// {

					$insert_array	=	array(	"bill_brnch_id"				=>	$this->input->post("bill_brnch_id"),

												"bill_company_id"			=>	$this->input->post("bill_company_id"),

												"bill_cnsgnr_id"			=>	$this->input->post("bill_cnsgnr_id"),

												"bill_cnsgne_id"			=>	$this->input->post("bill_cnsgne_id"),

												"bill_notify_id"			=>	$this->input->post("bill_notify_id"),

												"bill_port_id"			    =>	$this->input->post("bill_port_id"),





												"pre_carriage"			    =>	$this->input->post("pre_carriage"),

												"place_receipt"			    =>	$this->input->post("place_receipt"),

												"ocean_vessel"			    =>	$this->input->post("ocean_vessel"),

												"voyage"			    	=>	$this->input->post("voyage"),

												"port_of_loading"			=>	$this->input->post("port_of_loading"),

												"place_of_delivery"			=>	$this->input->post("place_of_delivery"),

												"no_of_bl"			    	=>	$this->input->post("no_of_bl"),

												"freight_payment"			=>	$this->input->post("freight_payment"),

												"freight_charges"			=>	$this->input->post("freight_charges"),

												"delivery_of_goods"			=>	$this->input->post("delivery_of_goods"),

												"place_issue"			    =>	$this->input->post("place_issue"),

												"date_of_issue"             =>  $this->input->post("date_of_issue"),

												"bill_products"			    =>	$this->input->post("content")

											);

					$bill_id 		=	$this->input->post("bill_id");

					$this->Generate_bill_model->save_data(BILL_TBL,$insert_array,array("bill_id"=>$bill_id),$bill_id);

					$this->session->set_flashdata('msg', "Bill Updated Successfully..!");

					redirect('Generate_bill/generate_bill');

				// }

				// else

				// {

				// 	$this->form_validation->set_message('rule', 'Error Message');

				// }

			}

		}

		$this->load->view('template/default', $data);

	}







	public function get_bill_rule($update=0)

	{

		$config 	= array();

		 $config []	=   array(

		                     'field'   => 'bill_brnch_id', 

		                     'label'   => 'Brnach Name', 

		                     'rules'   => 'required'

		                  );

        $config []	=   array(

		                     'field'   => 'bill_cnsgnr_id', 

		                     'label'   => 'Consignor Name', 

		                     'rules'   => 'required'

		                  );

        $config []	=   array(

		                     'field'   => 'bill_cnsgne_id', 

		                     'label'   => 'Consignee', 

		                     'rules'   => 'required'

		                  );

        // $config []	=   array(

		      //                'field'   => 'bill_notify_id', 

		      //                'label'   => 'Nofify Name', 

		      //                'rules'   => 'required'

		      //             );

        // $config []	=   array(

		      //                'field'   => 'bill_notify_party2', 

		      //                'label'   => 'Nofify Party 2', 

		      //                'rules'   => 'required'

		      //             );

        $config []	=   array(

		                     'field'   => 'bill_port_id', 

		                     'label'   => 'Port Name', 

		                     'rules'   => 'required'

		                  );

		 $config []	=   array(

				             'field'   => 'ocean_vessel', 

				             'label'   => 'Ocean Vessel', 

				             'rules'   => 'required'

				          );

		 $config []	=   array(

						     'field'   => 'voyage', 

						     'label'   => 'Voyage', 

						     'rules'   => 'required'

						  );



        $config []	=   array(

						     'field'   => 'port_of_loading', 

						     'label'   => 'Port of loading', 

						     'rules'   => 'required'

						  );



        $config []	=   array(

						     'field'   => 'place_of_delivery', 

						     'label'   => 'Place of delivery', 

						     'rules'   => 'required'

						  );

         $config []	=   array(

						     'field'   => 'voyage', 

						     'label'   => 'Voyage', 

						     'rules'   => 'required'

						  );

        //    $config []	=   array(

						  //     'field'   => 'freight_payment', 

						  //     'label'   => 'Freight payment', 

						  //     'rules'   => 'required'

						  // );

 			$config []	=   array(

						      'field'   => 'place_issue', 

						      'label'   => 'Place of issue', 

						      'rules'   => 'required'

						  );

            $config []	=   array(

						      'field'   => 'date_of_issue', 

						      'label'   => 'Date of issue', 

						      'rules'   => 'required'

						  );

             $config []	=   array(

						      'field'   => 'container_no', 

						      'label'   => 'Container No', 

						      'rules'   => 'required'

						  );

        return $config;

    }





    public function get_branch()

	{

		$entered_str 	=	$this->input->get("q");

		echo $this->Generate_bill_model->get_branch($entered_str);

	}



	public function get_company()

	{

		$entered_str 	=	$this->input->get("q");

		echo $this->Generate_bill_model->get_company($entered_str);

	}



	public function get_consignor(){

		$entered_str 	=	$this->input->get("q");

		echo $this->Generate_bill_model->get_consignor($entered_str);

	}

	public function get_consignee(){

			$entered_str 	=	$this->input->get("q");

			echo $this->Generate_bill_model->get_consignee($entered_str);

		}

	public function get_notify(){

		$entered_str 	=	$this->input->get("q");

		echo $this->Generate_bill_model->get_notify($entered_str);

	}

	public function get_portname(){

			$entered_str 	=	$this->input->get("q");

			echo $this->Generate_bill_model->get_portname($entered_str);

		}



	public function insert_branch_bill_no(){

		$branch_bill_no    = $this->input->post("branch_bill_no");



		$data['insert_branch_bill_no']  =  $this->Generate_bill_model->insert_branch_bill_no($branch_bill_no);

	}



	public function changestatus(){

		$id= $this->input->post('bill_id');

		$data['change_status'] = $this->Generate_bill_model->changestatus($id);

	}

	public function rejectstatus(){

			$id= $this->input->post('bill_id');

			$data['reject_status'] = $this->Generate_bill_model->rejectstatus($id);

		}







	public function	get_branch_bill_no(){

		$id=$this->input->post('id');

		echo $this->Generate_bill_model->get_branch_bill_no($id);



	}



	public function get_bill_details(){

			



		$bill_id 				=	$this->uri->segment(3);

		$data['bill_id']   = $bill_id;



		$data['get_bill_details'] = $this->Generate_bill_model->get_bill_details($bill_id);

		// var_dump($data['get_bill_details']);

		// die();

		



		$this->load->view('generate_bill/print-bill-draft', $data);

	}	

	public function get_bill_details_after_approval(){

			



		$bill_id 				=	$this->uri->segment(3);

		$data['bill_id']   = $bill_id;



		$data['get_bill_details'] = $this->Generate_bill_model->get_bill_details($bill_id);

		// var_dump($data['get_bill_details']);

		// die();

		



		$this->load->view('generate_bill/print-bill-draft-after-approval', $data);

	}







	 public function pdf_new($bill_id)

    {

       error_reporting(-1);

	   ini_set('max_execution_time', -1);

		$this->load->library('m_pdf');

    	$bill_id 				=	$this->uri->segment(3);

		

		$data['bill_id']   = $bill_id;

		

		$data['get_bill_details'] = $this->Generate_bill_model->get_bill_details($bill_id);

		$style_dataOne = file_get_contents(base_url()."assets/css/style.css");

		$style_dataTwo = file_get_contents(base_url()."assets/css/bootstrap.css");



        //load the view and saved it into $html variable

        $html		= $this->load->view('generate_bill/pdf-demo', $data, true);

        $header		= $this->load->view('generate_bill/pdf-demo-header', $data, true);

        $footer		= $this->load->view('generate_bill/pdf-demo-footer-original', $data, true);

  //       	echo $header;

 	// die();

 		$pdfFilePath = "download-pdf.pdf";



       

 		

//	$this->m_pdf->pdf->useOddEven = 1;    // Use different Odd/Even headers and footers and mirror margins



// Define the Headers before writing anything so they appear on the first page



/*$this->m_pdf->pdf->SetHTMLHeader('<div style="text-align: right; font-weight: bold;">My document</div>','O');



$this->m_pdf->pdf->SetHTMLHeader('<div style="border-bottom: 1px solid #000000;">My document</div>','E');



$this->m_pdf->pdf->SetHTMLFooter($footer);  // Note that the second parameter is optional : default = 'O' for ODD



$this->m_pdf->pdf->SetHTMLFooter($footer, 'E');



$this->m_pdf->pdf->WriteHTML('Hallo World');



$this->m_pdf->pdf->Output();

		

		

		

 exit;*/

 

 

 $this->m_pdf->pdf->mPDF('utf-8', 'A4');

$this->m_pdf->pdf->SetWatermarkText('DRAFT');



//$this->m_pdf->pdf->SetWatermarkImage( base_url() . 'images/border.svg',1,'',array(160,10));

//$this->m_pdf->pdf->Image('files/images/frontcover.jpg',0,0,210,297,'jpg','',true, false);

//$this->m_pdf->pdf->SetWatermarkImage( base_url() . 'images/border.png', 0.15, 'F');



//$this->m_pdf->pdf->showWatermarkImage = true;





$this->m_pdf->pdf->pagenumPrefix = '';//'Page number ';



$this->m_pdf->pdf->pagenumSuffix = '/';



$this->m_pdf->pdf->nbpgPrefix = '';//' out of ';



$this->m_pdf->pdf->nbpgSuffix = '';//' pages';





$this->m_pdf->pdf->showWatermarkText = true;

 		$wm = base_url() . 'images/draft.png';

		$this->m_pdf->pdf->WriteHTML($style_dataOne,1);

		$this->m_pdf->pdf->WriteHTML($style_dataTwo,1);

	

 		$this->m_pdf->pdf->SetHTMLHeader($header);

		$this->m_pdf->pdf->SetHTMLFooter($footer);

		$this->m_pdf->pdf->SetHTMLFooter($footer,"E");

		//$this->m_pdf->pdf->use_kwt = true;

		$this->m_pdf->pdf->shrink_tables_to_fit=0;// = true;

		

		$this->m_pdf->pdf->AddPage('', // L - landscape, P - portrait 

								   '', '', '', '',

								   5, // margin_left

								   5, // margin right

								   138, // margin top

								   55, // margin bottom

								   5, // margin header

								   5); 

       // $this->m_pdf->pdf->WriteHTML('<div style="border-left:thin solid #000; border-right:thin solid #000; height:100%;">'.$html.'</div>',2);

	$this->m_pdf->pdf->WriteHTML($html,2);



 

        //download it.

        $this->m_pdf->pdf->Output($pdfFilePath, "D");  



    }



    public function pdf_after_approval_original($bill_id)

    {

      error_reporting(-1);

	   ini_set('max_execution_time', -1);

		$this->load->library('m_pdf');

    	$bill_id 				=	$this->uri->segment(3);

		

		$data['bill_id']   = $bill_id;

		

		$data['get_bill_details'] = $this->Generate_bill_model->get_bill_details($bill_id);

		$style_dataOne = file_get_contents(base_url()."assets/css/style.css");

		$style_dataTwo = file_get_contents(base_url()."assets/css/bootstrap.css");



        //load the view and saved it into $html variable

        $html		= $this->load->view('generate_bill/pdf-demo', $data, true);

        $header		= $this->load->view('generate_bill/pdf-demo-header-original', $data, true);

        $footer		= $this->load->view('generate_bill/pdf-demo-footer-original', $data, true);

     //    	echo $header;

 	//die();

 		$pdfFilePath = "original-pdf.pdf";



       

 		

//	$this->m_pdf->pdf->useOddEven = 1;    // Use different Odd/Even headers and footers and mirror margins



// Define the Headers before writing anything so they appear on the first page



/*$this->m_pdf->pdf->SetHTMLHeader('<div style="text-align: right; font-weight: bold;">My document</div>','O');



$this->m_pdf->pdf->SetHTMLHeader('<div style="border-bottom: 1px solid #000000;">My document</div>','E');



$this->m_pdf->pdf->SetHTMLFooter($footer);  // Note that the second parameter is optional : default = 'O' for ODD



$this->m_pdf->pdf->SetHTMLFooter($footer, 'E');



$this->m_pdf->pdf->WriteHTML('Hallo World');



$this->m_pdf->pdf->Output();

		

		

		

 exit;*/

 

 

 $this->m_pdf->pdf->mPDF('utf-8', 'A4');

//$this->m_pdf->pdf->SetWatermarkText('DRAFT');



//$this->m_pdf->pdf->SetWatermarkImage( base_url() . 'images/water-mark-img.png',1,'',array(160,10));

//$this->m_pdf->pdf->SetWatermarkImage( base_url() . 'images/water-mark-img.png', 0.15, 'F');



//$this->m_pdf->pdf->SetWatermarkImage( base_url() . 'images/water-mark-img.png', 1, '', array(160,10));



$this->m_pdf->pdf->showWatermarkImage = true;





$this->m_pdf->pdf->pagenumPrefix = '';//'Page number ';



$this->m_pdf->pdf->pagenumSuffix = '/';



$this->m_pdf->pdf->nbpgPrefix = '';//' out of ';



$this->m_pdf->pdf->nbpgSuffix = '';//' pages';





//$this->m_pdf->pdf->showWatermarkText = true;

 		$wm = base_url() . 'images/draft.png';

		$this->m_pdf->pdf->WriteHTML($style_dataOne,1);

		$this->m_pdf->pdf->WriteHTML($style_dataTwo,1);

	

 		$this->m_pdf->pdf->SetHTMLHeader($header);

		$this->m_pdf->pdf->SetHTMLFooter($footer);

		$this->m_pdf->pdf->SetHTMLFooter($footer,"E");

		//$this->m_pdf->pdf->use_kwt = true;

		$this->m_pdf->pdf->shrink_tables_to_fit=0;// = true;

		

		$this->m_pdf->pdf->AddPage('', // L - landscape, P - portrait 

								   '', '', '', '',

								   5, // margin_left

								   5, // margin right

								   138, // margin top

								   55, // margin bottom

								   5, // margin header

								   5); 

        $this->m_pdf->pdf->WriteHTML($html,2);

	 //$this->m_pdf->pdf->WriteHTML('<div style="border-left:thin solid #000; border-right:thin solid #000; height:100%;">'.$html.'</div>',2);



 

        //download it.

        $this->m_pdf->pdf->Output($pdfFilePath, "D");  



    



    }





    public function pdf_after_approval_non_negotiable($bill_id)

    {

      error_reporting(-1);

	   ini_set('max_execution_time', -1);

		$this->load->library('m_pdf');

    	$bill_id 				=	$this->uri->segment(3);

		

		$data['bill_id']   = $bill_id;

		

		$data['get_bill_details'] = $this->Generate_bill_model->get_bill_details($bill_id);

		$style_dataOne = file_get_contents(base_url()."assets/css/style.css");

		$style_dataTwo = file_get_contents(base_url()."assets/css/bootstrap.css");



        //load the view and saved it into $html variable

        $html		= $this->load->view('generate_bill/pdf-demo', $data, true);

        $header		= $this->load->view('generate_bill/pdf-demo-header-non-negotiable', $data, true);

        $footer		= $this->load->view('generate_bill/pdf-demo-footer-original', $data, true);

     //    	echo $html;

 	//die();

 		$pdfFilePath = "non-negotiable-pdf.pdf";



       

 		

//	$this->m_pdf->pdf->useOddEven = 1;    // Use different Odd/Even headers and footers and mirror margins



// Define the Headers before writing anything so they appear on the first page



/*$this->m_pdf->pdf->SetHTMLHeader('<div style="text-align: right; font-weight: bold;">My document</div>','O');



$this->m_pdf->pdf->SetHTMLHeader('<div style="border-bottom: 1px solid #000000;">My document</div>','E');



$this->m_pdf->pdf->SetHTMLFooter($footer);  // Note that the second parameter is optional : default = 'O' for ODD



$this->m_pdf->pdf->SetHTMLFooter($footer, 'E');



$this->m_pdf->pdf->WriteHTML('Hallo World');



$this->m_pdf->pdf->Output();

		

		

		

 exit;*/

 

 

 $this->m_pdf->pdf->mPDF('utf-8', 'A4');

//$this->m_pdf->pdf->SetWatermarkText('DRAFT');



//$this->m_pdf->pdf->SetWatermarkImage( base_url() . 'images/border.png',1,'',array(160,10));

//$this->m_pdf->pdf->SetWatermarkImage( base_url() . 'images/border.png', 0.15, 'F');



$this->m_pdf->pdf->showWatermarkImage = true;





$this->m_pdf->pdf->pagenumPrefix = '';//'Page number ';



$this->m_pdf->pdf->pagenumSuffix = '/';



$this->m_pdf->pdf->nbpgPrefix = '';//' out of ';



$this->m_pdf->pdf->nbpgSuffix = '';//' pages';





//$this->m_pdf->pdf->showWatermarkText = true;

 		$wm = base_url() . 'images/draft.png';

		$this->m_pdf->pdf->WriteHTML($style_dataOne,1);

		$this->m_pdf->pdf->WriteHTML($style_dataTwo,1);

	

 		$this->m_pdf->pdf->SetHTMLHeader($header);

		$this->m_pdf->pdf->SetHTMLFooter($footer);

		$this->m_pdf->pdf->SetHTMLFooter($footer,"E");

		//$this->m_pdf->pdf->use_kwt = true;

		$this->m_pdf->pdf->shrink_tables_to_fit=0;// = true;

		

		$this->m_pdf->pdf->AddPage('', // L - landscape, P - portrait 

								   '', '', '', '',

								   5, // margin_left

								   5, // margin right

								   138, // margin top

								   55, // margin bottom

								   5, // margin header

								   5); 

        $this->m_pdf->pdf->WriteHTML($html,2);

	 //$this->m_pdf->pdf->WriteHTML('<div style="border-left:thin solid #000; border-right:thin solid #000; height:100%;">'.$html.'</div>',2);



 

        //download it.

        $this->m_pdf->pdf->Output($pdfFilePath, "D");  



    



    }	

	

}