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/views/Product_availability.php
<?php if ( ! defined('BASEPATH')) exit('No direct script is allowed');
require_once("Base_controller.php");
class Product_availability  extends Base_controller{
	
	public function __construct(){
		parent::__construct('product_availability');
		if(!$this->Appconfig->isAppvalid()){
			redirect('config');
		}
		$this->collect_base_info();
	}
	
	// LOAD PAGE WITH TABLE DATA
	public function index(){
		$data['table_headers']= $this->xss_clean(get_product_availability_headers());
		$this->load->view("$this->control_name/manage",$data);
	}
	
	//LOAD MODEL PAGE VIEW WITH DATA
	public function view($prime_customer_id){
		if($this->customer_exists($prime_customer_id, TRUE)){
			$product_ids              = $this->get_product_id($prime_customer_id);
			$product_ids = explode(",",$product_ids);
			$tr_line = "";
			foreach($product_ids as $product_id){
				$receiving_quantity_info = $this->receiving_quantity($product_id);
				$dispatch_quantity_info  = $this->dispatch_quantity($product_id);
				$customer_quantity_info  = $this->customer_quantity($product_id);
				$product_info            = $this->product_info($product_id);
				$rslt_info               = json_encode(array_merge($product_info[0],$receiving_quantity_info[0],$customer_quantity_info[0],$dispatch_quantity_info[0])); 
				$rslt_info = json_decode($rslt_info);
				
				$customer_quantity         = $rslt_info->customer_quantity_amount;
				$dispatch_quantity         = $rslt_info->dispatch_quantity;
				$receiving_quantity        = $rslt_info->receiving_quantity;
				$product_name              = $rslt_info->product_name;
				$brand_name                = $rslt_info->brand_name;
				$category_name             = $rslt_info->category_name;
				$product_description       = $rslt_info->product_description;
				$product_code              = $rslt_info->product_code;
				$availability_quantity     = abs((int)$receiving_quantity -((int)$customer_quantity + (int)$dispatch_quantity));
				
				$tr_line .= "<tr>
								<td>$product_name</td>
								<td>$brand_name</td>
								<td>$category_name</td>
								<td>$product_description</td>
								<td>$product_code</td>
								<td>$receiving_quantity</td>
								<td>$dispatch_quantity</td>
								<td>$availability_quantity</td>
							 </tr>";
			}
			$data['product_data'] = $tr_line;
			$this->load->view("$this->control_name/form",$data);
		}
	}
	
	//LOAD PAGE TABLE VIEW WITH DATA BASED ON SEARCH FILTERS
	public function search(){
		$search            = $this->input->get('search');
		$limit             = $this->input->get('limit');
		$offset            = $this->input->get('offset');
		$sort              = $this->input->get('sort');
		$order             = $this->input->get('order');
		$start_date        = str_replace("/","-",$this->input->get('start_date'));
		$end_date          = str_replace("/","-",$this->input->get('end_date'));	
		$from_date         = date('Y-m-d',strtotime($start_date));
		$to_date           = date('Y-m-d',strtotime($end_date));
		$this->db->select('customer.prime_customer_id,our_vendor_no, bee_level, acc, customer_name, delivery_address, postal_address, telephone, fax, mobile_number, email, customer_type, customer_category, normal_terms, early_terms, currency_code, eu_country, no_of_notes, discount, cr_limit, interest, statement, blocked_status, sales_code, tax_mode, tax_type, tax_reference, cash_sale_acc, accepts_email, contact_person, contact_persons_no');
		$this->db->from('customer');
		$this->db->join('customer_cf', 'customer.prime_customer_id = customer_cf.prime_customer_id','inner');
		$this->db->where('customer.trans_status', 1);		
		if($search){
			$this->db->group_start();
				$this->db->like('our_vendor_no',$search);
				$this->db->or_like('customer_name',$search);				
				$this->db->or_like('telephone',$search);				
				$this->db->or_like('delivery_address',$search);				
				$this->db->or_like('mobile_number',$search);				
				$this->db->or_like('email',$search);				
			$this->db->group_end();	
		}
		//$this->db->group_start();
		//$this->db->where('DATE_FORMAT(entry_date, "%Y-%m-%d") BETWEEN '. $this->db->escape($from_date).' AND '.$this->db->escape($to_date));
		//$this->db->group_end();				
		$this->db->order_by($sort,$order);
		$receiving_data = $this->db->get();
		$receiving_rslt = $receiving_data->result();
		$num_rows      = $receiving_data->num_rows();
		$datarows=array();		
		foreach($receiving_rslt as $receiving){
			$datarows[]=get_product_availability_datarows($receiving,$this);
		}
		echo json_encode(array('total'=>$num_rows,'rows'=>$datarows));
	}

	//CHECKING RECEIVING QUANTITY
	public function receiving_quantity($product_id){			
		$this->db->select('IFNULL(sum(cw_product_receiving.quantity), 0) as receiving_quantity');
		$this->db->from('product_receiving');		
		$this->db->where('product_receiving.trans_status', 1);
		$this->db->where('product_receiving.product_id', (int)$product_id);		
		$receiving_quantity_amount_info   = $this->db->get()->result_array();
		return $receiving_quantity_amount_info;
	}
	
	//CHECKING DISPATCH QUANTITY
	public function dispatch_quantity($product_id){
		$this->db->select('IFNULL(sum(cw_product_dispatch.quantity), 0) as dispatch_quantity');
		$this->db->from('product_dispatch');		
		$this->db->where('product_dispatch.trans_status', 1);
		$this->db->where('product_dispatch.product_id', (int)$product_id);		
		$dispatch_quantity_amount_info   = $this->db->get()->result_array();
		return $dispatch_quantity_amount_info;
	}
	
	//CHECKING CUSTOMER QUANTITY
	public function customer_quantity($product_id){		
		$this->db->select('IFNULL(sum(cw_customer_request_product_information.quantity), 0) as customer_quantity_amount ');
		$this->db->from('customer_request_product_information');
		$this->db->join('customer_request', 'customer_request.prime_customer_request_id = customer_request_product_information.prime_customer_request_id','inner');
		$this->db->where('customer_request.status !=', 3);
		$this->db->where('customer_request.trans_status', 1);
		$this->db->where('customer_request.request_mode', 2);
		$this->db->where('customer_request_product_information.trans_status', 1);
		$this->db->where('cw_customer_request_product_information.product', (int)$product_id);
		$customer_quantity_amount_info   = $this->db->get()->result_array();
		return $customer_quantity_amount_info;
	}
	public function product_info($product_id){
		$this->db->select('product_master.product_name,brand_master.brand_name,category_master.category_name,product_master.product_description,product_master.product_code');
		$this->db->from('product_master');
		$this->db->join('brand_master', 'product_master.brand_name = brand_master.prime_brand_master_id','inner');
		$this->db->join('category_master', 'product_master.category_name = category_master.prime_category_master_id','inner');
		$this->db->where('product_master.prime_product_master_id', (int)$product_id);
		$product_info   = $this->db->get()->result_array();
		return $product_info;
	}

	//CHECKING PRODUCT IS EXISTS
	public function customer_exists($prime_customer_id){
		$this->db->from('customer');
		$this->db->where('prime_customer_id', (int)$prime_customer_id);
		$this->db->where('trans_status',1);
		$count = $this->db->get()->num_rows();
		return ((int)$count === 1);
	}
	
	public function get_product_id($prime_customer_id){
		$this->db->select('customer_name,GROUP_CONCAT(prime_product_master_id) as product_ids');
		$this->db->from('product_master');
		$this->db->where('customer_name', (int)$prime_customer_id);
		$this->db->where('trans_status',1);
		$this->db->group_by('product_master.customer_name'); 
		$rslt = $this->db->get()->result();
		$product_ids = $rslt[0]->product_ids;
		return $product_ids;
	}
	
	public function merge_array($array1,$array2){
		if(is_array($array1) && is_array($array2)){
			$arr1_count = count($array1);
			$arr2_count = count($array2);
			if((int)$arr1_count > (int)$arr2_count){
				$temp   = $array1;
			    $array1 = $array2;
			    $array2 = $temp;
			} 
			$rlst  = array();
			foreach($array1 as $key => $value){
				foreach($array2 as $key2 => $value2){
					if((array_key_exists($key2,$array1)) && ($value['product_id'] === $value2['product_id'])){
						$rlst[$key2]  = array_merge($array1[$key2],$value2);
					}else{
						if(array_key_exists($key2,$array1)){
							$rlst[$key2] = array_merge($array1[$key2],$value2);
						}else{
							$rlst[$key2] = $value2;
						}
					}  
				}
			}
			return $rlst;
		}else{
			return FALSE;
		}
	}
}
?>