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


if ( ! function_exists('site_date'))
{
   function get_avalabe_prodcut_qty($srh_warehouse_id,$product_id){
	   
	   $balance=0;
	   $purchased_qty=0;
	   $sold_qty=0;
	   $transfer_reseve_qty=0;
	   $grn_qty=0;
	   $transferd_qty=0;
	   $product_damaged_qty=0;
	   $issued_resved_qty=0;
	   $ref_pur = new Purchases_Model();
	   $ref_issu = new Sales_Model();
	   $ref_dmg = new Product_Damage_Model();
	   $ref_rtn = new Sales_Return_Model();
	   $ref_trns = new Transfer_Model();
	   
	   	$grn_qty=$ref_pur->getGRNQtyByWarehouseId($srh_warehouse_id,$product_id);
		$issued_qty=$ref_issu->getSoldQtyByWarehouseIdandDateRang($srh_warehouse_id,$product_id);
		$issued_resved_qty=$ref_issu->Sales_Model->getIssueReturnQty($srh_warehouse_id,$product_id);
					
		$transferd_qty=$ref_trns->getTransferdQtyByWarehouseId($srh_warehouse_id,$product_id);
		$transfer_reseve_qty=$ref_trns->getTransferResevedQtyByWarehouseId($srh_warehouse_id,$product_id);
		$product_damaged_qty=$ref_dmg->getProductDamagedQtyByWarehouseId($srh_warehouse_id,$product_id);
		
				
				
				//$product_damaged_qty=$this->Product_Damage_Model->getProductDamagedQtyByWarehouseId($srh_warehouse_id,$products->product_id,$srh_from_date,$srh_to_date);
				//$sales_return_qty=$this->Sales_Return_Model->getSalesReturnQtyByWarehouseId($srh_warehouse_id,$products->product_id,$srh_from_date,$srh_to_date);
				
				
				$balance=$purchased_qty+$sold_qty+$transfer_reseve_qty+$grn_qty-$transferd_qty-$product_damaged_qty+$issued_resved_qty;
				return $balance;

   }  
}



if ( ! function_exists('site_date'))
{
   function site_date($date)
   {

	if($date){
			return date('j/m/Y', strtotime($date)); 
		
	}
   }  
}
if ( ! function_exists('site_date_time'))
{
   function site_date_time($site_date_time)
   {

	if($site_date_time){
			return date('j/m/Y g:i a', strtotime($site_date_time)); 
		
	}
   }  
}

if ( ! function_exists('display_date_time_format'))
{
   function display_date_time_format($datetime)
   {
	   return date('d/m/y  H:i A', strtotime($datetime)); //$dateTime->format("d/m/y  H:i A"); 
	   
   }  
}

if ( ! function_exists('display_time_format'))
{
   function display_time_format($datetime)
   {
	   return date('H:i A', strtotime($datetime)); //$dateTime->format("d/m/y  H:i A"); 
	   
   }  
}

if ( ! function_exists('asset_url'))
{
   function asset_url($type=0)
   {
	   return base_url().'thems/';
	   
   }  
}

function check_is_avalable_warehouse_by_user_id($warehouse_id){
	$CI = & get_instance(); 
	$ref_whe = new Warehouse_Model();
	$user_id=$CI->session->userdata('ss_user_id');
	$is_avalable=$ref_whe->check_is_user_assigned_to_warehouses($user_id, $warehouse_id);	
	return $is_avalable;	
}

function get_current_user_info(){
	$CI = & get_instance();
	$ref_usr = new User_Model(); 
	$curr_user_info=$ref_usr->get_current_user_info();	
	return $curr_user_info;
}

function check_is_avalable_permission($col){
	$CI = & get_instance(); 
	$ref_whe = new Warehouse_Model();
	$warehouse_id=$CI->session->userdata('ss_warehouse_id');
	$group_id=$CI->session->userdata('ss_group_id');
	//echo $col;
	$user_id=$CI->session->userdata('ss_user_id');
	$is_avalable=$ref_whe->check_is_avalable_permission($group_id, $user_id, $col);	
	return $is_avalable;	
}


 	function image_url($type=0)
   {
	   return base_url().'thems/images';
	   
   }
   
   function premition($sub_menu_name)
   {
	   $CI = & get_instance();  //get instance, access the CI superobject
	   
	  if($CI->session->userdata('ss_user_group_name') and $CI->session->userdata('ss_user_group_name')=='Sale mStaff')
	 return true;
   }
   
   
   
   function is_logged_in() {
		// Get current CodeIgniter instance
		$CI =& get_instance();
		// We need to use $CI->session instead of $this->session
		$ss_user_id = $CI->session->userdata('ss_user_id');
		//echo "uid:".$user_id;
		if ($ss_user_id) { 
			return true;
			//redirect(base_url(),'refresh');
		} else { return false; }
	}

function in_multiarray($field,$elem, $array,$field_2,$elem_2)
{
    $top = sizeof($array) - 1;
    $bottom = 0;
    while($bottom <= $top)
    {
        if($array[$bottom][$field] == $elem && $array[$bottom][$field_2]==$elem_2){
            return true;
		}
        else 
            if(is_array($array[$bottom][$field]))
                if(in_multiarray($elem, ($array[$bottom][$field])))
                    return true;

        $bottom++;
    }        
    return false;
}      
?>