<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Sales_order extends CI_Controller {

	public function all_sales_order()
	{
		check_login();
		
		if($this->input->post()){

			$so_no = $this->input->post('so_no');
				$result = $this->Sales_Orders->check_sono($so_no);

				 if($result == ''){
					
			$party_id = $this->input->post('party_id');
			$so_date = $this->input->post('so_date');
			$so_no = $this->input->post('so_no');
			$instruction = $this->input->post('instruction');
			$pay_term = $this->input->post('pay_term');
			$created = $this->input->post('created');
			$modified = $this->input->post('modified');

			$save_po = array(

		                    'party_id' => $party_id,
		                    'so_date' => $so_date,
		                     'so_no' => $so_no,
		                     'instruction' => $instruction,
		                     'pay_term' => $pay_term,
		                     'created' => $created,
		                     'modified' => $modified
		                     
		  						 );
	     	   $result_id = $this->Sales_Orders->add_sales_order($save_po);

	            $product_id = $this->input->post('product_id');
				$ref_code = $this->input->post('ref_code');
				$delv_date = $this->input->post('delv_date');
				$unit_rate = $this->input->post('unit_rate');
				$gst = $this->input->post('gst');
				$quantity = $this->input->post('quantity');
				$tolerance = $this->input->post('tolerance');
				$per = 100;
				@$tott = $per+$tolerance;
				$div = $tott/100;
				$total_qty = $quantity*$div; 

			

				$amount = $this->input->post('amount');
				$created = $this->input->post('created');
			    $modified = $this->input->post('modified');

			    $save_po1 = array(

		                    'so_id' => $result_id,
		                    'product_id' => $product_id,
		                     'ref_code' => $ref_code,
		                     'delv_date' => $delv_date,
		                     'unit_rate' => $unit_rate,
		                     'gst' => $gst,
		                     'quantity' => $quantity,
		                     'tolerance' => $tolerance,
		                     'total_qty' => $total_qty,
		                     'amount' => $amount,
		                     'created' => $created,
		                     'modified' => $modified
		                     
		  						 );


			       $result = $this->Sales_Orders->add_sales_order_products($save_po1);


	        if($result){$add_sales=array('status'=>'true');}else{$add_sales=array('status'=>'false');}
	            echo json_encode($add_sales);
			

				 }else{
				     
				     	$party_id = $this->input->post('party_id');
			$so_date = $this->input->post('so_date');
			$so_no = $this->input->post('so_no');
			$instruction = $this->input->post('instruction');
			$pay_term = $this->input->post('pay_term');
			$created = $this->input->post('created');
			$modified = $this->input->post('modified');

			$save_po = array(

		                    'party_id' => $party_id,
		                    'so_date' => $so_date,
		                     'so_no' => $so_no,
		                     'instruction' => $instruction,
		                     'pay_term' => $pay_term,
		                     'created' => $created,
		                     'modified' => $modified
		                     
		  						 );
	     	   $result_id = $this->Sales_Orders->add_sales_order($save_po);

	            $product_id = $this->input->post('product_id');
				$ref_code = $this->input->post('ref_code');
				$delv_date = $this->input->post('delv_date');
				$unit_rate = $this->input->post('unit_rate');
				$gst = $this->input->post('gst');
				$quantity = $this->input->post('quantity');
				$tolerance = $this->input->post('tolerance');
				$per = 100;
				@$tott = $per+$tolerance;
				$div = $tott/100;
				$total_qty = $quantity*$div; 

			

				$amount = $this->input->post('amount');
				$created = $this->input->post('created');
			    $modified = $this->input->post('modified');

			    $save_po1 = array(

		                    'so_id' => $result_id,
		                    'product_id' => $product_id,
		                     'ref_code' => $ref_code,
		                     'delv_date' => $delv_date,
		                     'unit_rate' => $unit_rate,
		                     'gst' => $gst,
		                     'quantity' => $quantity,
		                     'tolerance' => $tolerance,
		                     'total_qty' => $total_qty,
		                     'amount' => $amount,
		                     'created' => $created,
		                     'modified' => $modified
		                     
		  						 );


			       $result = $this->Sales_Orders->add_sales_order_products($save_po1);
					
					$check=array('check_status'=>'true');
	           		 echo json_encode($check);
				 }


	      }else{

	      		$data['get_fg']=$this->Sales_Orders->get_fg();
	      		$data['get_ref_party']=$this->Sales_Orders->get_ref_party();
	      		$data['parties']=$this->Sales_Orders->get_party();
	      		$data['get_po']=$this->Sales_Orders->get_all_po();
				$this->load->view('sales/sales_order',$data);
	      }
	}



	public function edit_sales_order(){


		 check_login();

	     $id = $this->uri->segment('3');

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

			$party_id = $this->input->post('party_id');
			$so_date = $this->input->post('so_date');
			$so_no = $this->input->post('so_no');
			$instruction = $this->input->post('instruction');
			$pay_term = $this->input->post('pay_term');
			$created = $this->input->post('created');
			$modified = $this->input->post('modified');

			$save_po = array(

		                    'party_id' => $party_id,
		                    'so_date' => $so_date,
		                     'so_no' => $so_no,
		                     'instruction' => $instruction,
		                     'pay_term' => $pay_term,
		                     'created' => $created,
		                     'modified' => $modified
		                     
		  						 );
	     	
	           $result1=$this->Sales_Orders->update_so($id,$save_po);

	            $product_id = $this->input->post('product_id');
				$ref_code = $this->input->post('ref_code');
				$delv_date = $this->input->post('delv_date');
				$unit_rate = $this->input->post('unit_rate');
				$gst = $this->input->post('gst');
				$quantity = $this->input->post('quantity');
				$tolerance = $this->input->post('tolerance');
				$total_qty = $quantity*$tolerance; 
				$amount = $this->input->post('amount');
				$created = $this->input->post('created');
			    $modified = $this->input->post('modified');

			    $per = 100;
				$tott = $per+$tolerance;
				$div = $tott/100;
				$total_qty = $quantity*$div; 

			

			    $save_po1 = array(

		                    'so_id' => $id,
		                    'product_id' => $product_id,
		                     'ref_code' => $ref_code,
		                     'delv_date' => $delv_date,
		                     'unit_rate' => $unit_rate,
		                     'gst' => $gst,
		                     'quantity' => $quantity,
		                     'tolerance' => $tolerance,
		                     'total_qty' => $total_qty,
		                     'amount' => $amount,
		                     'created' => $created,
		                     'modified' => $modified
		                     
		  						 );

			  
		
				$result=$this->Sales_Orders->update_so_products($id,$save_po1);

			
			if($result){$material=array('status'=>'true');}else{$party=array('status'=>'false');}
			echo json_encode($material);
		}else{
				$data['get_fg']=$this->Sales_Orders->get_fg();
	      		
	      		$data['parties']=$this->Sales_Orders->get_party();
	      		$data['get_po']=$this->Sales_Orders->get_all_po();
	      		$data['get_edit_data'] = $this->Sales_Orders->get_edit_sales_order($id);
				$this->load->view('sales/sales_order_edit',$data);
	      }
				
	
	}


	public function edit_dc_invoice(){

		check_login();

	     $id = $this->uri->segment('3');

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

			$batch = $this->input->post('batch');
			$qty = $this->input->post('qty');
			$remarks = $this->input->post('remarks');
			$modified = $this->input->post('modified');
			

			$save_po = array(


							'qty' => $qty,
		                    'batch' => $batch,
		                     'remarks' => $remarks,
		                     'modified' => $modified
		                     
		                     
		  						 );
			
			$result=$this->Sales_Orders->update_dc_invoice_data($id,$save_po);

			
			if($result){$material=array('status'=>'true');}else{$party=array('status'=>'false');}
			echo json_encode($material);
		}else{
				
	      		$data['update_dc_invoice'] = $this->Sales_Orders->update_dc_invoice($id);
				$this->load->view('sales/edit_dc_invoice',$data);
	      }


	}
	
	
	public function all_delivery_challan(){

			check_login();
		
		
	      		$data['get_po']=$this->Sales_Orders->all_delivery_challan();
	      		
	      		
				$this->load->view('sales/all_delivery_challan',$data);
	}

	public function so_delivery_challan(){
			check_login();
		if($this->input->post()){
	      }else{
	      		$data['get_po']=$this->Sales_Orders->get_all_po();
				$this->load->view('sales/so_delivery_challan',$data);
	      }
	}

	public function sales_order_delete_data($id){

		 check_login();
		 //$id = $this->input->post('id');
			$result = $this->Sales_Orders->delete_data_sales_order_data($id);
			$result = $this->Sales_Orders->delete_data_sales_order_pro($id);
			if($result){$pro=array('status'=>'true');}else{$pro=array('status'=>'false');}
			echo json_encode($pro);
		}
		
		
		public function delete_dc($id){
		 check_login();
			$result = $this->Sales_Orders->delete_dc($id);
			if($result){$pro=array('status'=>'true');}else{$pro=array('status'=>'false');}
			echo json_encode($pro);
		}

	public function parties_code(){
		check_login();
		if($this->input->post())
		{
			$param = $this->input->post('id');
 			$data = $this->Sales_Orders->get_party_code($param);
			//print_r($data);
			echo json_encode($data);
		}
	}

	public function fg_product_code(){
		check_login();
		if($this->input->post())
		{
			//print_r($_POST);
			$param = $this->input->post('id');
 			$data = $this->Sales_Orders->fg_product_code($param);
			echo json_encode($data);
		}
	}

	public function view_sales_order(){
		check_login();
		if($this->input->post()){
			 $id = $this->input->post('so_id');
	           $result = $this->Sales_Orders->add_sales_order_products($_POST);
	           $result = $this->Sales_Orders->update_order_products_status($id);
	        if($result){$add_sales=array('status'=>'true');}else{$add_sales=array('status'=>'false');}
	            echo json_encode($add_sales);
	      }else{
	      		
	      		$id = $this->uri->segment(3);

	      		$data['get_all_so_products']=$this->Sales_Orders->get_all_so_products($id);
	      		//$data['get_all_pro_fg']=$this->Sales_Orders->get_all_pro_fg();
	      		$data['get_fg']=$this->Sales_Orders->get_fg();
				$this->load->view('sales/view_sales_order',$data);
	      }


		
	}

	

	public function sales_evaluation(){

		check_login();

		$data['get_all_confirm_so']=$this->Sales_Orders->get_all_confirm_so();
		//$data['disabled_button']=$this->Sales_Orders->disabled_button();
		$this->load->view('sales/sales_evaluation',$data);
	}

	public function view_boms(){

		check_login();
		$id = $this->uri->segment('3');
		//$data['get_all_confirm_so']=$this->Sales_Orders->get_all_confirm_so();
		
		$data['view_so_product']=$this->Sales_Orders->view_so_products($id);
		
		$this->load->view('sales/view_boms',$data);
	}
	
	
	

	public function view_boms_details(){

		check_login();
		
		$id=$this->input->post('id');
		$data['get_all_boms_details']=$this->Sales_Orders->get_destroy_bom($id);

		$all = $data['get_all_boms_details'];	 
		//foreach ($all as $value) {
		$ids = $all[0]['main_product'];
		
		//die;
		$data['get_materials_boms']=$this->Sales_Orders->get_materials_boms1($ids);
		$data['get_materials_packing_boms']=$this->Sales_Orders->get_materials_packing_boms($ids);	
/*
		var_dump($data['get_materials_packing_boms']);
		exit;*/
		//$data['get_molded_boms']=$this->Sales_Orders->get_molded_boms($ids);
		$data['get_moldedchild_boms']=$this->Sales_Orders->get_moldedchild_boms($ids);
		$data['get_printed_boms']=$this->Sales_Orders->get_printed_boms($ids);
		$data['get_printedchild_boms']=$this->Sales_Orders->get_printedchild_boms($ids);
		$data['get_assembled_boms']=$this->Sales_Orders->get_assembled_boms($ids);
		$data['get_assembled_child_boms']=$this->Sales_Orders->get_assembled_child_boms($ids);
		$data['get_sealed_boms']=$this->Sales_Orders->get_sealed_boms($ids);
		$data['get_sealed_child_boms']=$this->Sales_Orders->get_sealed_child_boms($ids);

		

		$data['get_boms_mat']=$this->Sales_Orders->get_boms_mat($ids);

		
		
		echo json_encode (array('html'=>$this->load->view('sales/bom_destroye',$data,true)));
		//print_r($aa);
		//}
	//
	//$this->load->view('sales/view_boms_details',$data);
		     
	

		
	}


	public function view_boms_details1(){

		check_login();
		
		$id=$this->input->post('id');
		$data['get_all_boms_details']=$this->Sales_Orders->get_destroy_bom($id);

		$all = $data['get_all_boms_details'];	 
		//foreach ($all as $value) {
		$ids = $all[0]['main_product'];
		
		//die;
		$data['get_materials_boms']=$this->Sales_Orders->get_materials_boms1($ids);
		$data['get_materials_packing_boms']=$this->Sales_Orders->get_materials_packing_boms($ids);	
/*
		var_dump($data['get_materials_packing_boms']);
		exit;*/
		//$data['get_molded_boms']=$this->Sales_Orders->get_molded_boms($ids);
		$data['get_moldedchild_boms']=$this->Sales_Orders->get_moldedchild_boms($ids);
		$data['get_printed_boms']=$this->Sales_Orders->get_printed_boms($ids);
		$data['get_printedchild_boms']=$this->Sales_Orders->get_printedchild_boms($ids);
		$data['get_assembled_boms']=$this->Sales_Orders->get_assembled_boms($ids);
		$data['get_assembled_child_boms']=$this->Sales_Orders->get_assembled_child_boms($ids);
		$data['get_sealed_boms']=$this->Sales_Orders->get_sealed_boms($ids);
		$data['get_sealed_child_boms']=$this->Sales_Orders->get_sealed_child_boms($ids);

		

		$data['get_boms_mat']=$this->Sales_Orders->get_boms_mat($ids);

		
		
		echo json_encode (array('html'=>$this->load->view('sales/bom_destroye1',$data,true)));
		//print_r($aa);
		//}
	//
	//$this->load->view('sales/view_boms_details',$data);
		     
	

		
	}
	
	public function fg_product_rates(){

		check_login();
		
		if($this->input->post())
		{
			//print_r($_POST);
			$param = $this->input->post('id');
 			$data = $this->Sales_Orders->fg_product_rates($param);
			
			//print_r($data);
			echo json_encode($data);
		}
	}


	public function view_invoice_details_so(){

		check_login();
		
		$id = $this->uri->segment('3');

		
		$data['get_invoice_data']=$this->Sales_Orders->get_invoice_data($id);

		$this->load->view('sales/view_po_invoice_so',$data);
		//echo json_encode (array('html'=>$this->load->view('/purchase/view_po_invoice',$data,true)));
	
	}

	

	public function view_dc_invoice(){

		check_login();
		
		$id = $this->uri->segment('3');

		
		$data['get_invoice_data']=$this->Sales_Orders->get_invoice_dc($id);

		$this->load->view('sales/view_dc_invoice',$data);
		//echo json_encode (array('html'=>$this->load->view('/purchase/view_po_invoice',$data,true)));
	
	}


	

	public function invoice_print_dc(){

		check_login();
		
		$id = $this->uri->segment('3');

		
		$data['invoice_print_dc']=$this->Sales_Orders->invoice_print_dc($id);

		$this->load->view('sales/invoice_print_dc',$data);
		//echo json_encode (array('html'=>$this->load->view('/purchase/view_po_invoice',$data,true)));
	
	}






	public function add_d_challan(){
		$data['id'] = $this->input->post('id');
		$id = $data['id'];
		$data['get_invoice_data']=$this->Sales_Orders->get_invoice_data($id);
		$data['get_dc_qty']=$this->Sales_Orders->get_dc_qty($id);
		$gst = $data['get_invoice_data'][0]['gst'];
		$product =  sale_order_pro($id); 
		$data['get_sets']=$this->Sales_Orders->get_fg_sets($product);
		
		if($gst == "" || $gst==0)
		{
			    $code['code'] = $this->Sales_Orders->get_code_dc_gst();
	      		$xx = $code['code'][0]['dc_no'];
	      		for ($dc_no = 0; $dc_no <= $xx; $dc_no++) {
		             
		            }
	      			$data['dc_no'] = $dc_no;

	      		echo json_encode (array('html'=>$this->load->view('sales/add_recive_po',$data,true)));

		}else{

			$code['code'] = $this->Sales_Orders->get_code_dc();
	      		$xx = $code['code'][0]['dc_no'];
	      		for ($dc_no = 0; $dc_no <= $xx; $dc_no++) {
		             
		            }
	      		$data['dc_no'] = $dc_no;

	      		echo json_encode (array('html'=>$this->load->view('sales/add_recive_po',$data,true)));

		}

		
		
		

	}

	
	public function bom_destroye_production(){

		if($this->input->post())
		{
			//print_r($_POST);
			$param=$this->input->post('id');
			
 			$data['bom_production'] = $this->Sales_Orders->so_po_boms_data($param);

 			
			
			echo json_encode (array('html'=>$this->load->view('sales/bom_destroye_production',$data,true)));
		}

	}

	

	public function reporting_delivery_challan(){

		check_login();
		
		
	
		$this->load->view('sales/reporting_delivery_challan');
	
	}


	
public function company_dc(){

		check_login();
		if($this->input->post())
		{

			 $result = $this->Sales_Orders->add_dc_company($_POST);

	        if($result){$add_sales=array('status'=>'true');}else{$add_sales=array('status'=>'false');}
	            echo json_encode($add_sales);
		}else{

		$data['sales_order'] = $this->Sales_Orders->get_all_confirm_so();

		$data['dc_company_data'] = $this->Sales_Orders->dc_company_data();
	
		$this->load->view('sales/company_dc',$data);
		}
	}

	public function dc_company_invoice(){
		check_login();		
		$id = $this->uri->segment('3');
		$data['get_dcs'] = $this->Sales_Orders->get_dcs_company_dc($id);
		$this->load->view('sales/dc_company_invoice',$data);
	}

	public function get_so_dcs(){

		check_login();
		
		if($this->input->post())
		{
			$param = $this->input->post('id');
 			$data['so_dcs'] = $this->Sales_Orders->get_so_dcs($param);
			
			echo json_encode (array('html'=>$this->load->view('sales/so_dcs_challans',$data,true)));
		}
	}

	

	public function delivery_challan_qty(){

		check_login();
		if($this->input->post())
			{
				$challan_id = $this->input->post('challan_id');

					$data2 = $this->Sales_Orders->challan_qty_delivery($challan_id);
					$data['total_qty'] = $data2[0]['qty'];
				   echo json_encode ($data);

				}
			
				
			}
		


	public function reporting_delivery_challandata(){

	    check_login();
		if($this->input->post())
		{
		    $start = $this->input->post('start');
		    $end = $this->input->post('end');
			
		   
		    $data['reporting_data']=$this->Sales_Orders->reporting_data($start,$end);
			echo json_encode (array('html'=>$this->load->view('sales/reporting_data',$data,true)));

			}
		}
	public function create_bills(){
		check_login();		
		$data['get_bills'] = $this->Sales_Orders->get_all_createbills();
		$this->load->view('sales/create_bills',$data);
	}
    
    	public function un_create_bills(){
		check_login();		
		$data['get_bills'] = $this->Sales_Orders->get_all_createbills();
		$this->load->view('sales/un_create_bills',$data);
	}
	public function create_sales_tax(){
		check_login();		
		$data['get_bills_tax'] = $this->Sales_Orders->get_all_tax_bills();
		$this->load->view('sales/create_bills_tax',$data);
	}

	public function view_all_dc(){
		check_login();		
		$id = $this->uri->segment('3');
		$data['view_all_dc'] = $this->Sales_Orders->view_all_dc($id);
		$this->load->view('sales/view_all_dc',$data);
	}

	
	public function view_sales_tax_bill(){
		check_login();		
		$id = $this->uri->segment('3');
		$data['view_all_dc'] = $this->Sales_Orders->view_sales_tax_bill($id);
		$this->load->view('sales/view_sales_tax_bill',$data);
	}

	

	public function view_filter_sales_tax_bill(){
		check_login();		
		$id = $this->uri->segment('3');
		$data['view_all_dc'] = $this->Sales_Orders->view_sales_tax_bill($id);
		$this->load->view('sales/filter_view_sales_tax',$data);
	}

	public function view_filter_sales_bill(){
		check_login();		
		$id = $this->uri->segment('3');
		$data['view_all'] = $this->Sales_Orders->fulter_view_sales_tax_bill($id);
		$this->load->view('sales/view_filter_sales_bill',$data);
	}

	

	public function bill_details() {
		$selected_ids = $this->input->post('dc_ids');
		
		$data['selected_ids'] = $selected_ids;
		$code['code'] = $this->Sales_Orders->get_billno_bill();
		if($code['code']==""){
			$data['bill_no'] = '10001';
		}else{
		$xx = $code['code'][0]['bill_no'];
	    for ($dc_no = 0; $dc_no <= $xx; $dc_no++) {
		}
	    $data['bill_no'] = $dc_no;
		}
// 		print_r($data['bill_no']);
// 		die;
		$this->load->view('sales/bill_details', $data);
	}

	public function add_bill(){
		$selected_ids = $this->input->post('ids');
		$created = $this->input->post('created');
		$bill_no = $this->input->post('bill_no');
		$insert_date = $this->input->post('insert_date');
		$ids_array = explode(',', $selected_ids);
		$add_sales = array('status' => 'false'); // Initialize the status as false
		
		foreach ($ids_array as $id) {
			$toalQty = qty_dc_record($id);
			$so_id = get_so_from_dcrecord($ids_array[0]);
			$dc_ids = $id;
			$get_ref = get_so_from_dcrecord($id);
			$product = sale_order_pro($get_ref);
			$rates = get_fg_rates($product);
			$gstRate = 0.18;
			$totalValueWithGST = $rates * $toalQty * $gstRate;
			////insert value////
			$totalValue = $rates * $toalQty + $totalValueWithGST;
			$created = $this->input->post('created');
			$save_po = array(
				'insert_date' => $insert_date,
				'bill_no' => $bill_no,
				'so_id' => $so_id,
				'qty' => $toalQty,
				'dc_id' => $dc_ids,
				'total_value' => $totalValue,
				'created' => $created,
			);
			$result = $this->Sales_Orders->add_bills($save_po);
			$result1=$this->Sales_Orders->update_dc_status($dc_ids);
			//print_r($so_id);
			$conditions=$this->Sales_Orders->check_dc_status($so_id);
			 if($conditions == 1){
				$result2=$this->Sales_Orders->update_so_status($so_id);
			 }
			if ($result) {
				$add_sales['status'] = 'true'; // Update the status to true
			}
		}
		echo json_encode($add_sales);
	}
	
	public function update_sales_tax_bill(){
		$so_id = $this->input->post('so_id');
		$result2=$this->Sales_Orders->sales_tax_update_so_status($so_id);
		if($result2){$add_sales=array('status'=>'true');}else{$add_sales=array('status'=>'false');}
		echo json_encode($add_sales);
	}
	
	public function complete_bills_reports(){
		check_login();
		if($this->input->post())
			{
				
			$id = $this->input->post('so_id');
			$bill_no = $this->input->post('bill_no');
			$parties = $this->input->post('parties');
			$date = $this->input->post('date');
			$enddate = $this->input->post('enddate');
			// print_r($enddate);
			// die;
			$data['filter_data'] = array();
			
			if (!empty($id) || !empty($bill_no) || !empty($date) || !empty($enddate) || !empty($parties)) {
				$data['filter_data'] = $this->Sales_Orders->filter_bill_report($id,$bill_no, $date, $enddate,$parties);
			}
				
		echo json_encode(array('html' => $this->load->view('sales/filter_bills_report_data', $data, true)));}else{
		$data['so_data'] = $this->Sales_Orders->so_bill_reports();
		$data['bill_no'] = $this->Sales_Orders->bill_no();
		$data['get_company'] = $this->Sales_Orders->get_company();
		$this->load->view('sales/bill_reports', $data);
		}
	}
	public function billing(){
	check_login();
	$data['bill_get'] = $this->Sales_Orders->get_all_bills();
	$this->load->view('sales/view_billing_bill',$data);
	}

	public function add_bill_payment(){
		check_login();
		$so_id = $this->input->post('id');
		$data['get_bill_data'] = $this->Sales_Orders->get_bill_data_modal($so_id);
		$data['banks'] = $this->Sales_Orders->get_banks();	
		echo json_encode (array('html'=>$this->load->view('sales/get_bill_data_modal',$data,true)));
	}

	public function add_bill_receviepayment(){
		check_login();
		if($this->input->post())
		{
			$so_id = $this->input->post('so_id');
			$amount = $this->input->post('amount');
			$bank = $this->input->post('bank');
			$user_id = $this->input->post('user_id');
			$created = $this->input->post('created');
			$mode = $this->input->post('mode');
			$cheque_number = $this->input->post('cheque_number');
			$fund_no = $this->input->post('fund_no');
			//condition 
			$get_bill = $this->Sales_Orders->get_bill_data_modal($so_id);
			$total = $get_bill[0]['total_value']*95.5/100; 
			$pai_amount = bill_pay_amount($get_bill[0]['so_id']);
			$amount_form = $pai_amount+$amount;
			$total_amount  = intval($total-$amount_form);
			if ($total_amount == 0) {
				$result1=$this->Sales_Orders->update_payment_status($so_id);
			}
			//condition
			$save_po = array(
							'so_id' => $so_id,
		                    'bank' => $bank,
		                     'mode' => $mode,
		                     'cheque_number' => $cheque_number,
		                     'fund_no' => $fund_no,
							 'amount' => $amount,
		                     'created_at' => $created,
							 'user_id' => $user_id,
		                     );
			 $result = $this->Sales_Orders->add_bill_receviepayment($save_po);
	        if($result){$add_sales=array('status'=>'true');}else{$add_sales=array('status'=>'false');}
	            echo json_encode($add_sales);
		}
	}

	public function billing_report(){
		check_login();
		if($this->input->post())
			{
			$id = $this->input->post('so_id');
			$party = $this->input->post('parties');
			// $enddate = $this->input->post('enddate');
			// // print_r($enddate);
			// die;
			$data['filter_data'] = array();
			if (!empty($id) || !empty($party)) {
				$data['filter_data'] = $this->Sales_Orders->filter_bill_payment_report($id,$party);
			}
		echo json_encode(array('html' => $this->load->view('sales/filter_bills_payments_report_data', $data, true)));}else{
		$data['so_data'] = $this->Sales_Orders->bill_bill_reports();
		$data['get_parties'] = $this->Sales_Orders->get_company();
		$this->load->view('sales/payment_bill_reports', $data);
		}
	}

	public function banking(){

		check_login();
		$data['banks'] = $this->Sales_Orders->get_banks();
		$this->load->view('sales/banking',$data);

	}

	public function banking_report(){

		check_login();
		if($this->input->post())
			{
			$id = $this->input->post('bank');
			$data['filter_data'] = array();
			if (!empty($id)) {
				$data['filter_data'] = $this->Sales_Orders->filter_banking($id);
			}
		echo json_encode(array('html' => $this->load->view('sales/filter_banking', $data, true)));}else{ }
	

	}

	public function logout()
	{
		check_login();
		$this->session->sess_destroy();
		redirect(base_url(),'refresh'); 
	}
	
}
