MOON
Server: Apache
System: Linux nserver.cafsindia.com 4.18.0-553.104.1.lve.el8.x86_64 #1 SMP Tue Feb 10 20:07:30 UTC 2026 x86_64
User: cafsindia (1002)
PHP: 8.2.30
Disabled: NONE
Upload Files
File: /home/cafsindia/ntc_cafsinfotech_in_bk/application/controllers/Order_receiving.php
<?php if ( ! defined('BASEPATH')) exit('No direct script is allowed');
require_once("Base_controller.php");
class Order_receiving  extends Base_controller{
	public $control_name;
	public function __construct(){
		parent::__construct('order_receiving');
		if(!$this->Appconfig->isAppvalid()){
			redirect('config');
		}
		$this->control_name = strtolower($this->router->fetch_class());
		$cart_data      = $this->session->userdata('cart_data');
		if(!$cart_data){
			$cart_data = array();
			$this->session->set_userdata('cart_data', $cart_data);
			$cart_data    = $this->session->userdata('cart_data');
		}
	}
	// LOAD PAGE WITH TABLE DATA
	public function index(){
		$this->db->select('prime_employees_id,employee_code,employee_name');
		$this->db->from('employees');
		$this->db->where('employees.trans_status', 1);
		$employees_rslt = $this->db->get()->result();
		$get_employee_info[""] = "--- Select ---";
		foreach($employees_rslt as $employee){
			$prime_employees_id = $employee->prime_employees_id;
			$emp_info           = $employee->employee_code." - ".$employee->employee_name;
			$get_employee_info[$prime_employees_id] = $emp_info;
		}
		$data['employee_info'] = $get_employee_info;
		$data['cart_view'] = $this->get_cart_view();
		$this->load->view("$this->control_name/manage",$data);
	}
	/****************** CART OPERATION  - START **********************/
	//GET WAREHOUSE INFO
	public function get_warehouse_info(){
		$warehouse_name = $this->input->post('warehouse_name');
		$this->db->select('prime_warehouse_locations_id,floor_number,zct_21.cw_zct_21_value as location');
		$this->db->from('warehouse_locations');
		$this->db->join('zct_21','zct_21.cw_zct_21_id = warehouse_locations.bin_or_floor_location');
		$this->db->where('warehouse_locations.trans_status', 1);
		$this->db->where('warehouse_locations.warehouse_name',$warehouse_name);
		$warehouse_rslt = $this->db->get()->result();

		foreach($warehouse_rslt as $location_info){
			$prime_warehouse_locations_id = $location_info->prime_warehouse_locations_id;
			$location                     = $location_info->floor_number." - ".$location_info->location;
			$get_warehouse_info[$prime_warehouse_locations_id] = $location;
		}
		echo json_encode(array('sts'=>true,'get_warehouse_info' => $get_warehouse_info));
	}	
	//PRODUCT SEARCH BY SINGLE OR BY CUSTOMER REQUEST
	public function item_search(){
		$search_by       = (int)$this->input->post_get('search_by');
		$search_product  = $this->input->post_get('search_product');
		if($search_by === 1){
			$this->db->select('prime_warehouse_product_id,product_name,warehouse_brand.brand_name as brand');
			$this->db->from('warehouse_product');
			$this->db->join('warehouse_brand','warehouse_brand.prime_warehouse_brand_id = warehouse_product.brand_name');
			$this->db->join('customer','customer.prime_customer_id = warehouse_product.customer_name');
			$this->db->where('warehouse_product.trans_status', 1);
			$this->db->group_start();
				$this->db->like('product_name',$search_product);
				$this->db->or_like('warehouse_brand.brand_name',$search_product);
			$this->db->group_end();	
			$search_info = $this->db->get()->result();
			foreach($search_info as $search){
				$prime_warehouse_product_id = $search->prime_warehouse_product_id;
				$product_name            = $search->product_name;
				$brand_name              = $search->brand;
				$label = "$product_name  - $brand_name";
				$suggestions[] = array('value'=>$prime_warehouse_product_id,'label'=>$label,'search_by'=>$search_by);
			}
			if(empty($suggestions)){
				$suggestions[] = array('value' => "0", 'label' => "No data found for this search");
			}			
		}else
		if($search_by === 2){
			$this->db->select('prime_customer_request_id,cust_req_no,customer.customer_name as customer');
			$this->db->from('customer_request');
			$this->db->join('customer','customer.prime_customer_id = customer_request.customer_name');
			$this->db->where('customer_request.trans_status', 1);
			$this->db->where('customer_request.request_mode', 1);
			$this->db->where('customer_request.status', 1);
			$this->db->group_start();
				$this->db->like('cust_req_no',$search_product);
			$this->db->group_end();
			$search_info = $this->db->get()->result();
			foreach($search_info as $search_rslt){
				$prime_customer_request_id = $search_rslt->prime_customer_request_id;
				$cust_req_no               = $search_rslt->cust_req_no;
				$customer                  = $search_rslt->customer;
				$label                     = "$cust_req_no - $customer";
				$suggestions[] = array('value'=>$cust_req_no,'label'=>$label,'search_by'=>$search_by);
			}
			if(empty($suggestions)){
				$suggestions[] = array('value' => "0", 'label' => "No data found for this search");
			}
		}
		echo json_encode($suggestions);
	}
	
	//ADD PRODUCT TO CART
	public function add_products(){
		$cart_data          = $this->session->userdata('cart_data');
		$search_by          = (int)$this->input->post('search_by');
		$search_product     = $this->input->post('search_product');
		$cart_data['error'] = array();
		if($search_by === 1){
			$this->db->select('prime_warehouse_product_id,product_name,warehouse_product.product_type,warehouse_brand.brand_name as brand');
			$this->db->from('warehouse_product');
			$this->db->join('warehouse_brand','warehouse_brand.prime_warehouse_brand_id = warehouse_product.brand_name');
			$this->db->join('customer','customer.prime_customer_id = warehouse_product.customer_name');
			$this->db->where('warehouse_product.trans_status', 1);
			$this->db->where('prime_warehouse_product_id',$search_product);
			$search_info = $this->db->get()->row();
			$prime_warehouse_product_id = $search_info->prime_warehouse_product_id;
			$product_name            = $search_info->product_name;
			$brand_name              = $search_info->brand;
			$product_type            = $search_info->product_type;
			if(!$cart_data['order_info'][$prime_warehouse_product_id]){
				$cart_data['order_info'][$prime_warehouse_product_id] = array('product_id'=>$prime_warehouse_product_id,'product_name'=>$product_name,'brand_name'=>$brand_name,'packing_type'=>"",'product_type'=>'','repacking'=>"",'warehouse_name'=>"",'location'=>"",'location_space'=>"",'batch_no'=>"",'stock_expiry_date'=>"",'price'=>0,'quantity'=>1,'no_of_units'=>0,'wastage_units'=>0);
			}else{
				// updated for cart array
				$cart_quantity = (int)$cart_data['order_info'][$prime_warehouse_product_id]['quantity']+1;
				$cart_data['order_info'][$prime_warehouse_product_id] = array('product_id'=>$prime_warehouse_product_id,'product_name'=>$product_name,'brand_name'=>$brand_name,'packing_type'=>"",'product_type'=>'','repacking'=>"",'warehouse_name'=>"",'location'=>"",'location_space'=>"",'batch_no'=>"",'stock_expiry_date'=>"",'price'=>0,'quantity'=>$cart_quantity,'no_of_units'=>0,'wastage_units'=>0);
			}
		}else
		if($search_by === 2){
			$this->db->select('*,customer.customer_name as customer_name');
			$this->db->from('customer_request');
			$this->db->join('customer','customer.prime_customer_id = customer_request.customer_name');
			$this->db->where('customer_request.trans_status', 1);
			$this->db->where('cust_req_no',$search_product);
			$cust_info = $this->db->get()->row();
			$request_id        = $cust_info->prime_customer_request_id;
			$prime_customer_id = $cust_info->prime_customer_id;
			$cust_req_no       = $cust_info->cust_req_no;
			$customer_name     = $cust_info->customer_name;
			$from_date         = $cust_info->from_date;
			$to_date           = $cust_info->to_date;
			$pickup_location   = $cust_info->pickup_location;
			$pickup_landmark   = $cust_info->pickup_landmark;
			$pickup_time       = $cust_info->pickup_time;
			$can_process = false;			
			if((int)$request_id > 0){
				if(!$cart_data['cust_req'][$cust_req_no]){
					$cart_data['cust_req'][$cust_req_no] = $cust_req_no;
					$can_process = true;					
				}else{
					$cart_data['error'][] = "Adding same request numbers"; 
					$can_process = false;
				}
				
				if(!$cart_data['cust_info']){
					$cart_data['cust_info'] = array('prime_customer_id'=>$prime_customer_id,'customer_name'=>$customer_name,'from_date'=>$from_date,'to_date'=>$to_date,'pickup_location'=>$pickup_location,'pickup_landmark'=>$pickup_landmark,'pickup_time'=>$pickup_time,);
					$can_process = true;
				}else{
					$chk_customer_id = $cart_data['cust_info']['prime_customer_id'];
					if((int)$chk_customer_id !== (int)$prime_customer_id){
						$cart_data['error'][] = "Add Same request numbers from same customer"; 
						$can_process = false;
					}else{
						$can_process = true;
					}
				}		
			}
			if($can_process){
				$this->db->select('warehouse_product.prime_warehouse_product_id,warehouse_product.product_name,warehouse_product.product_type,warehouse_brand.brand_name, customer_request_product_information.quantity as quantity');
				$this->db->from('customer_request_product_information');
				$this->db->join('warehouse_product','warehouse_product.prime_warehouse_product_id = customer_request_product_information.product');
				$this->db->join('warehouse_brand','warehouse_brand.prime_warehouse_brand_id = warehouse_product.brand_name');
				$this->db->where('customer_request_product_information.trans_status', 1);
				$this->db->where('prime_customer_request_id',$request_id);
				$product_info = $this->db->get()->result();
				foreach($product_info  as $product){
					$product_id    = $product->prime_warehouse_product_id;
					$product_name  = $product->product_name;
					$brand_name    = $product->brand_name;
					$product_type  = $product->product_type;
					$db_quantity   = $product->quantity;
					
					if(!$cart_data['order_info'][$product_id]){
					$cart_data['order_info'][$product_id] = array('product_id'=>$product_id,'product_name'=> $product_name,'brand_name'=>$brand_name,'packing_type'=>"",'product_type'=>'','repacking'=>"",'warehouse_name'=>"",'location'=>"",'location_space'=>"",'batch_no'=>"",'stock_expiry_date'=>"",'price'=>0,'quantity'=>$db_quantity,'no_of_units'=>0,'wastage_units'=>0);
					}else{
						$quantity     = $cart_data['order_info'][$product_id]['quantity'];
						$db_quantity  = $db_quantity + $quantity;
						$cart_data['order_info'][$product_id] = array('product_id'=>$product_id,'product_name'=>$product_name,'brand_name'=>$brand_name,'packing_type'=>"",'product_type'=>'','repacking'=>"",'warehouse_name'=>"",'location'=>"",'location_space'=>"",'batch_no'=>"",'stock_expiry_date'=>"",'price'=>0,'quantity'=>$db_quantity,'no_of_units'=>0,'wastage_units'=>0);
					}
				}
			}
		}
		$this->session->set_userdata('cart_data', $cart_data);
		$cart_view = $this->get_cart_view();
		$cart_data          = $this->session->userdata('cart_data');
		echo json_encode(array('cart_view'=>$cart_view,'error'=>$cart_data['error']),JSON_FORCE_OBJECT);
	}
	
	//UPDATE PRODUCT TO CART
	public function update_product(){
		$product_id         = (int)trim($this->input->post('product_id'));
		$product_name       = $this->input->post('product_name');
		$brand_name         = $this->input->post('brand_name');
		$packing_type       = $this->input->post('packing_type');
		$product_type       = $this->input->post('product_type');
		$repacking          = $this->input->post('repacking');
		$warehouse_name     = $this->input->post('warehouse_name');
		$location           = $this->input->post('location');
		$location_space     = $this->input->post('location_space');
		$batch_no      		= $this->input->post('batch_no');
		$stock_expiry_date  = $this->input->post('stock_expiry_date');
		$price     			= $this->input->post('price');
		$quantity           = $this->input->post('quantity');
		//$shortfall          = $this->input->post('shortfall');
		$no_of_units        = $this->input->post('no_of_units');
		//$wastage            = $this->input->post('wastage');
		$wastage_units      = $this->input->post('wastage_units');
		$frm                = $this->input->post('frm');
		$cart_data          = $this->session->userdata('cart_data');
		if(!$stock_expiry_date || $stock_expiry_date === "01-01-1970" || $stock_expiry_date === ""){
			$stock_expiry_date = "";
		}		
		
		/*if($frm === "bulk"){
			if($packing_type === ""){
				$product_type = "";
			}
		}else
		if($frm === "sensitive"){
			if($packing_type === ""){
				$product_type = "";
			}
		}*/
		
		if($cart_data['order_info'][$product_id]){
			$cart_data['order_info'][$product_id] = array('product_id'=>$product_id,'product_name'=>$product_name,'brand_name'=>$brand_name,'packing_type'=>$packing_type,'product_type'=>$product_type,'repacking'=>$repacking,'warehouse_name'=>$warehouse_name,'location'=>$location,'location_space'=>$location_space,'batch_no'=>$batch_no,'stock_expiry_date'=>$stock_expiry_date,'price'=>$price,'quantity'=>$quantity,'no_of_units'=>$no_of_units,'wastage_units'=>$wastage_units);
		}
		$this->session->set_userdata('cart_data', $cart_data);
		$cart_view = $this->get_cart_view();
		echo json_encode(array('cart_view'=>$cart_view));
	}
	
	//SAVE RECEIVED ORDERS TO DB
	public function get_cart_view(){
		$cart_data = $this->session->userdata('cart_data');
		$this->db->select('cw_zct_19_id,cw_zct_19_value');
		$this->db->from('zct_19');
		$this->db->where('zct_19.cw_zct_19_status', 1);
		$product_type_list = $this->db->get()->result();		
		$this->db->select('prime_warehouse_information_id,warehouse_name');
		$this->db->from('warehouse_information');
		$this->db->where('warehouse_information.trans_status', 1);
		$warehouse_rslt = $this->db->get()->result();
		$tr_line = "";
		foreach(array_reverse($cart_data['order_info']) as $data){
			$prime_id         = $data['product_id'];
			$product_name     = $data['product_name'];
			$brand_name       = $data['brand_name'];
			$packing_type     = $data['packing_type'];
			$product_type     = $data['product_type'];
			$repacking        = $data['repacking'];
			$warehouse_name   = $data['warehouse_name'];
			$location         = $data['location'];
			$location_space   = $data['location_space'];
			$batch_no         = $data['batch_no'];
			$stock_expiry_date= $data['stock_expiry_date'];	
			$price            = $data['price'];	
			//$shortfall        = $data['shortfall'];	
			$quantity         = $data['quantity'];	
			$no_of_units      = $data['no_of_units'];	
			//$wastage          = $data['wastage'];	
			$wastage_units    = $data['wastage_units'];

			if(!$stock_expiry_date){
				$stock_expiry_date = "";
			}
			//Repacking Selected		
			if($repacking === "1"){
				$yes_selected = "selected";
			}else
			if($repacking === "2"){
				$no_selected = "selected";
			}else{
				$yes_selected = "";
				$no_selected = "";
			}
			/*//Shortfall Selected
			if($shortfall === "1"){
				$shortfall_yes = "selected";
			}else
			if($shortfall === "2"){
				$shortfall_dis = "style = 'display:none;'";
				$shortfall_no = "selected";
			}else{
				$shortfall_dis = "style = 'display:none;'";
				$shortfall_yes = "";
				$shortfall_no = "";
			}
			//Wastage Selected
			if($wastage === "1"){
				$wastage_yes = "selected";
			}else
			if($wastage === "2"){
				$wastage_dis = "style = 'display:none;'";
				$wastage_no = "selected";
			}else{
				$wastage_dis = "style = 'display:none;'";
				$wastage_yes = "";
				$wastage_no = "";
			}*/

			$product_line     = "<option value=''>--Select--</option>";
			$warehouse_line   = "<option value=''>--Select--</option>";
			$location_line    = "<option value=''>--Select--</option>";
			
			foreach($product_type_list as $product_list){
				$cw_zct_19_id 	   = $product_list->cw_zct_19_id;
			    $product_type_name = $product_list->cw_zct_19_value;
			    if($product_type === $cw_zct_19_id){
					$pro_selected = "selected";
				}else{
					$pro_selected = "";
				}
				$product_line .="<option value='$cw_zct_19_id' $pro_selected>$product_type_name</option>";
			}

			foreach($warehouse_rslt as $warehouse_info){
				$prime_warehouse_information_id = $warehouse_info->prime_warehouse_information_id;				
				$warehouse                      = $warehouse_info->warehouse_name;
				if($warehouse_name === $prime_warehouse_information_id){
					$ware_selected = "selected";
				}else{
					$ware_selected = "";
				}
				$warehouse_line .="<option value='$prime_warehouse_information_id' $ware_selected>$warehouse</option>";
			}

			$this->db->select('prime_warehouse_locations_id,floor_number,zct_21.cw_zct_21_value as location');
			$this->db->from('warehouse_locations');
			$this->db->join('zct_21','zct_21.cw_zct_21_id = warehouse_locations.bin_or_floor_location');
			$this->db->where('warehouse_locations.trans_status', 1);
			$this->db->where('warehouse_locations.warehouse_name',$warehouse_name);
			$warehouse_location_rslt = $this->db->get()->result();
			foreach($warehouse_location_rslt as $location_info){
				$prime_warehouse_locations_id = $location_info->prime_warehouse_locations_id;
				$location_name                = $location_info->floor_number." - ".$location_info->location;
				if($location === $prime_warehouse_locations_id){
					$location_selected = "selected";
				}else{
					$location_selected = "";
				}
				$location_line .="<option value='$prime_warehouse_locations_id' $location_selected>$location_name</option>";
			}
			$tr_line .= "<tr id='tr_data_$prime_id'>
							<td>
								$product_name <br/>$brand_name
								<input type='hidden' name='product_id[]' id='product_id_$prime_id' value='$prime_id'>
								<input type='hidden' name='product_name[]' id='product_name_$prime_id' value='$product_name'>
								<input type='hidden' name='brand_name[]' id='brand_name_$prime_id' value='$brand_name'>
								<input type='hidden' name='packing_type[]' id='packing_type_$prime_id' value='$packing_type'>
							</td>
							<td>
								<select name='product_type[]' id='product_type_$prime_id' onchange=get_packing_type('$prime_id')>
									$product_line
								</select>
							</td>
							<td>
								<select name='repacking[]' id='repacking_$prime_id' onchange=update_product('$prime_id');>
									<option value=''>--Select--</option>
										<option value='1' $yes_selected>Yes</option>
										<option value='2' $no_selected>No</option>
								</select>
							</td>
							<td>
								<select name='warehouse_name[]' id='warehouse_name_$prime_id' onchange=get_warehouse_info('$prime_id')>
									$warehouse_line
								</select>
							</td>
							<td>
								<select name='location[]' id='location_$prime_id' onchange=update_product('$prime_id');>
									$location_line
								</select>
							</td>
							<td>
								<input name='location_space[]' id='location_space_$prime_id'' value='$location_space' style='width:100px;' onchange=update_product('$prime_id');>
							</td>
							<td>
								<input name='batch_no[]' id='batch_no_$prime_id'' value='$batch_no' class='number' style='width:100px;' onchange=update_product('$prime_id');>
							</td>
							<td>
								<input name='stock_expiry_date[]' id='stock_expiry_date_$prime_id' value='$stock_expiry_date' class='datepicker' style='width:100px;'>
							</td>
							<td>
								<input name='price[]' id='price_$prime_id'' value='$price' class='number' style='width:100px;' onchange=update_product('$prime_id');>
							</td>
							<td>
								<input name='quantity[]' id='quantity_$prime_id' value='$quantity' class='number' style='width:100px;' onchange=update_product('$prime_id');>
							</td>
							<td>								
								<input $shortfall_dis name='no_of_units[]' id='no_of_units_$prime_id' value='$no_of_units' class='number' style='width:100px;' onchange=update_product('$prime_id');>
							</td>
							<td>								
								<input $wastage_dis name='wastage_units[]' id='wastage_units_$prime_id' value='$wastage_units' class='number' style='width:100px;' onchange=update_product('$prime_id');>
							</td>													
						</tr>";												
		}
		/*<select name='shortfall[]' id='shortfall_$prime_id' class='shortfall' onchange=update_product('$prime_id');>
		<option value=''>--Select--</option>
			<option value='1' $shortfall_yes>Yes</option>
			<option value='2' $shortfall_no>No</option>		
	</select>
	<select name='wastage[]' class='wastage' id='wastage_$prime_id' onchange=update_product('$prime_id');>
		<option value=''>--Select--</option>
			<option value='1' $wastage_yes>Yes</option>
			<option value='2' $wastage_no>No</option>
	</select>*/
		$cust_info         = $cart_data['cust_info'];
		$customer_name     = $cust_info['customer_name'];		
		$prime_customer_id = $cust_info['prime_customer_id'];
		$cart_content = "<h4 class='tab_head' style='margin:8px 0px;'>Product List</h4>
							<input name='prime_customer_id' id='prime_customer_id' value='$prime_customer_id' onchange=check_prime_id(); type='hidden'>
							<table class='table table-striped'>
								<thead>
									<tr>
										<td>Product</td>
										<td>Type</td>
										<td>Repacking</td>
										<td>Warehouse</td>
										<td>Location</td>
										<td>Location Space</td>
										<td>Batch No</td>
										<td>Stock Expiry Date</td>
										<td>Value</td>
										<td>Qty.</td>
										<td>Shortfall</td>
										<td>Wastage</td>
									</tr>
								</thead>
								<tbody>
									$tr_line
								</tbody>
					 		</table>";
		if($cust_info['from_date']){
			$from_date  = date('d-m-Y',strtotime($cust_info['from_date']));
		}else{
			$from_date  = '';
		}
		if($cust_info['to_date']){
			$to_date  = date('d-m-Y',strtotime($cust_info['to_date']));
		}else{
			$to_date  = '';
		}
		$pickup_location  = $cust_info['pickup_location'];
		$pickup_landmark  = $cust_info['pickup_landmark'];
		
		if($cust_info['pickup_time']){
			$pickup_time  = date('d-m-Y h:m:s',strtotime($cust_info['pickup_time']));
		}else{
			$pickup_time  = '';
		}
		$cust_content = "<table class='table' style='background-color:#ffffff;'>
							<tr>
								<td style='padding:4px;'><span style='color: #4e5b9a; font-size: 12px; font-weight: bold;'>Customer Name</span><br/>$customer_name</td>
								<td style='padding:4px;'><span style='color: #4e5b9a; font-size: 12px; font-weight: bold;'>From date</span><br/>$from_date</td>
								<td style='padding:4px;'><span style='color: #4e5b9a; font-size: 12px; font-weight: bold;'>To date</span><br/>$to_date</td>
							</tr>
							<tr>
								<td style='padding:4px;'><span style='color: #4e5b9a; font-size: 12px; font-weight: bold;'>Loaction</span><br/>$pickup_location</td>
								<td style='padding:4px;'><span style='color: #4e5b9a; font-size: 12px; font-weight: bold;'>Landmark</span><br/>$pickup_landmark</td>
								<td style='padding:4px;'><span style='color: #4e5b9a; font-size: 12px; font-weight: bold;'>Time</span><br/>$pickup_time</td>
							</tr>
						</table>";
		return array('cust_content'=>$cust_content,'cart_content'=>$cart_content);
	}
	/****************** CART OPERATION  - END ***********************/

	/****************** DB OPERATION  - START **********************/
	//Save to DB	
	public function save_order_receiving(){
		$cart_data         = $this->session->userdata('cart_data');				
		$cust_info         = $cart_data['cust_info'];		
		$prime_customer_id = $cust_info['prime_customer_id'];
		$sku_no            = $this->unique_code($code="ODR");
		$table_content     = $this->get_cart_view();
		if($prime_customer_id){
			$receiving_data = array(
				'sku_no'           => $sku_no,
				'customer_id'      => $prime_customer_id,
				'checked_by'       => $this->input->post('checked_by'),
				'received_vehicle' => $this->input->post('received_vehicle'),
				'driver_name'      => $this->input->post('driver_name'),
				'driver_mobile'    => $this->input->post('driver_mobile'),
				'entry_date'       => date("Y-m-d H:i:s"),
			);
			$receiving_data['trans_created_by']   = $this->session->userdata('logged_id');
			$receiving_data['trans_created_date'] = date("Y-m-d H:i:s");
			$this->db->insert('cw_order_receiving',$receiving_data);
			$order_dispatch_id = $this->db->insert_id();
			$product_info = $cart_data['order_info'];		
			$count             = count($product_id);

			foreach ($product_info as $key => $value){
				$product_type = $value['product_type'];
				if((int)$product_type === 1){
					$pack_type = "tonnes";
				}else
				if((int)$product_type === 4){
					$pack_type = "temperature";
				}else{
					$pack_type = "tonnes";
				}
				
				$this->db->from('warehouse_product');
				$this->db->where('warehouse_product.trans_status', 1);
				$this->db->where('warehouse_product.prime_warehouse_product_id', $value['product_id']);
				$avail_qty = $this->db->get()->row();
				$availabale_quantity = $avail_qty->availabale_quantity;
				$total_quantity = $availabale_quantity + $value['quantity'];
				$upd_qty  = array(
					'availabale_quantity' => $total_quantity,
					'trans_created_by'    => $this->session->userdata('logged_id'),
					'trans_created_date'  => date("Y-m-d H:i:s")
				);
				$product_data  = array(
					'order_receiving_id' => $order_dispatch_id,
					'product_id'         => $value['product_id'],
					$pack_type           =>	$value['packing_type'],
					'product_type'       => $value['product_type'],	
					'stock_expiry_date'  => date("Y-m-d H:i:s",strtotime($value['stock_expiry_date'])),
					'repacking'          => $value['repacking'],
					'warehouse_name'     => $value['warehouse_name'],
					'location'           => $value['location'],
					'location_space'     => $value['location_space'],
					'batch_no'           => $value['batch_no'],
					'price'              => $value['price'],
					'quantity'           => $value['quantity'],
					//'shortfall'          => $value['shortfall'],
					'no_of_units'        => $value['no_of_units'],
					//'wastage'            => $value['wastage'],
					'wastage_units'      => $value['wastage_units']
				);
				$product_data['trans_created_by']   = $this->session->userdata('logged_id');
				$product_data['trans_created_date'] = date("Y-m-d H:i:s");
				$insert_data = $this->db->insert('cw_product_receiving',$product_data);
				
				$this->db->where('prime_warehouse_product_id',$value['product_id']);
				$this->db->update('cw_warehouse_product',$upd_qty);
			}
			$cust_req_array    = $cart_data['cust_req'];
			foreach ($cust_req_array as $key => $value) {
				$this->db->where('cust_req_no',$key);
				$update_data = $this->db->update('customer_request',array('status'=>2));
			}
			$cart_data         = array();
			$this->session->set_userdata('cart_data',$cart_data);
			$cart_data         = $this->session->userdata('cart_data');
			$table_content     = $this->get_cart_view();
			if($insert_data){
				echo json_encode(array('success'=>true,'message'=>'Order Created Successfully','table_content'=>$table_content));
			}else{
				echo json_encode(array('success'=>false,'message'=>'Please Try After Sometime','table_content'=>$table_content));
			}
		}else{
			echo json_encode(array('success'=>false,'message'=>'Please add customer and try again','table_content'=>$table_content));
		}
	}
	/***************** DB OPERATION  - END ***********************/
	public function cancel_order(){
		$cart_data = array();
		$this->session->set_userdata('cart_data',$cart_data);
		echo json_encode(array('sts'=>true,'message'=>"Product removed successfully"));
	}
	
	//checking customer exists in session
	public function customer_exists(){
		$cart_data         = $this->session->userdata('cart_data');	
		$cust_info         = $cart_data['cust_info'];
		$prime_customer_id = $cust_info['prime_customer_id'];
		if($prime_customer_id){
			echo json_encode(array('sts'=>true));
		}else{
			echo json_encode(array('sts'=>false,'message'=>"Please Add Customer and Try Again"));
		}
	}
	
}
?>