<?php



function mon_fivecent_round($amount){
	
	
	$last_cent_array = explode(".",number_format($amount,2,".",""));
	
	$last_cent = $last_cent_array[1];
	
	$last_cent_10 = substr($last_cent,0,1);
	$last_cent=substr($last_cent,1,1);
	
	
	$last_cent_rounded_plus=0;
	
	if(($last_cent==0))
	{
		$last_cent_rounded = 0;
	}
	
	if(($last_cent==5))
	{
		$last_cent_rounded = 5;
	}
	
	if(($last_cent==1)or($last_cent==2))
	{
		$last_cent_rounded = 0;
	}
	if(($last_cent==3)or($last_cent==4))
	{
		$last_cent_rounded = 5;
	}
	if(($last_cent==6)or($last_cent==7))
	{
		$last_cent_rounded = 5;
	}
	if(($last_cent==8)or($last_cent==9))
	{
		$last_cent_rounded = 0;
		$last_cent_rounded_plus = 1;
	}


		$return_amount = ( $last_cent_array[0] * 1 ) + ( ( $last_cent_10 + $last_cent_rounded_plus )/ 10 ) + ( $last_cent_rounded / 100 ) ;
		

	
	return $return_amount;
	

	
	
	
}



function displaymessage($message_content,$message_type)
{
	global $dbconn;
	
 	if(($message_type=="CREATED")||($message_type=="UPDATED")||($message_type=="DELETED"))
 	{
	echo "<div class='message_box_green' valign=top onclick=\"this.style.opacity='0';this.style.visibility='hidden';\">";
	echo "<span class='fa fa-check fa-4x' style='float:right;'></span>";
	echo "".$message_content;
	echo "<br><br>Click any place on this box to close this message</div>";
	}

	if($message_type=="FAIL")
 	{
	echo "<div class='message_box_red' valign=top onclick=\"this.style.opacity='0';this.style.visibility='hidden';\">";
	echo "<span class='fa fa-times fa-4x' style='float:right;'></span>";
	echo "Please complete the following:<br>".$message_content;
	echo "<br><br>Click any place on this box to close this message</div>";
	}
	
}


$limit = 10; //this detemines max records per pageListing per page

function recordsetNav($db_query, $page_url, $offset=0, $limit=0, $tablewidth='100%', $verbiage=1, $displaytotal=1, $extraurl='', $blocksize=10)
{
	global $dbconn;
	global $addon_path;
	global $theme_path;
	$addon_path="";

	global $lang_navigation_next;
	global $lang_navigation_previous;
	global $lang_navigation_first;
	global $lang_navigation_last;



$pos = strrpos($page_url, "?");
if ($pos === false) {
  $offSetVar="?offset";
}
else
{
  $offSetVar="&offset";
}


//$offSetVar="?offset";
//$offSetVar="&offset";

  $db_result = mysqli_query($dbconn,$db_query);
  $totalrecords = mysqli_num_rows($db_result);
  $pagenumber = intval(($offset + $limit) / $limit);
  $totalpages = intval($totalrecords/$limit);

  if ($totalrecords%$limit > 0) //partial page
  {
		$lastpage = $totalpages * $limit;
		$totalpages++;
  } else {
		$lastpage = ($totalpages - 1) * $limit;
  }

   //start building navigation string
    $navstring = "<center>";


  if ($totalrecords > $limit) //only show <<PREV NEXT>> row if $totalrecords is greater than $limit
  {
        $navstring .= "<table class='pager_main' >"; //my own defined table
		

		if ($offset != 0)
		{
        
			$imgfirst ="<span class='fa fa-angle-double-left'></span> First";
			$imgprev = "<span class='fa fa-angle-left '></span> Previous";
			$navstring .= "<td class='pager_arrow_box' ><a title='First' href='javascript:void' onclick=\"mon_send_get('".$page_url."','".$offSetVar."=0".$extraurl."','div_main')\" >$imgfirst</a></td>";
			$navstring .= "<td class='pager_arrow_box' ><a title='Previous' href='javascript:void' onclick=\"mon_send_get('".$page_url."','".$offSetVar."=".($offset-$limit).$extraurl."','div_main')\"><span>$imgprev</span></a></td>";
		} else
        {
		
			$imgfirstin ="<span class='fa fa-angle-double-left'></span> First ";
			$imgprevin ="<span class='fa fa-angle-left'></span> Previous ";
			$navstring .= "<td class='pager_arrow_box' >$imgfirstin</td>";
			$navstring .= "<td class='pager_arrow_box' >$imgprevin</td>";

        }

		if ($totalpages < $blocksize) {
			$blocksize = $totalpages;
			  $firstpage = 1;
		} elseif ($pagenumber > $blocksize) {
			$firstpage = ($pagenumber - $blocksize) + 2;
		} elseif ($pagenumber == $blocksize) {
			  $firstpage = 2;
		} else {
			$firstpage = 1;
		}

		$blocklimit = $blocksize + $firstpage;

		//page numbers
		for ($i=$firstpage; $i<$blocklimit; $i++) {
			if ($i == $pagenumber) {
				$navstring .= "<td class='pager_page_box' >". $i."</td>";
			} else {
				if ($i <= $totalpages) {
					$nextoffset = $limit * ($i-1);
					$navstring .= "<td  class='pager_page_box' ><a title='Page ". $i ." of ". $totalpages ."'  href='javascript:void' onclick=\"mon_send_get('".$page_url."','".$offSetVar."=".$nextoffset.$extraurl."','div_main')\">$i</a></td>";
				}
			}
		}

		if ($totalrecords - $offset > $limit)
        {
			$imglast ="Last <span class='fa fa-angle-double-right'></span>";
			$imgnext ="Next <span class='fa fa-angle-right'></span>";
			$navstring .= "<td class='pager_arrow_box' ><a title='Next' href='javascript:void'  onclick=\"mon_send_get('".$page_url."','".$offSetVar."=".($offset+$limit).$extraurl."','div_main')\">$imgnext</a></td>";
			$navstring .= "<td  class='pager_arrow_box' ><a title='Last' href='javascript:void' onclick=\"mon_send_get('".$page_url."','".$offSetVar."=".$lastpage.$extraurl."','div_main')\">$imglast</a></td>";
		}else
		{
        
			$imglastin ="Last <span class='fa fa-angle-double-left'></span>";
			$imgnextin ="Next <span class='fa fa-angle-left'></span>";
			$navstring .= "<td class='pager_arrow_box' >$imgnextin</td>";
			$navstring .= "<td class='pager_arrow_box' >$imglastin</td>";
        }
		
		$navstring .= "</tr>";
    	$navstring .= "</table>";
  }


  if ($verbiage && $displaytotal == 1) {
	//	$navstring .= "<tr><td>"; //my own defined <tr><td>
		$navstring .= "<table class='pager_info_box' ><tr><td align=center  >";
		$CurrentUserLang=1;
		
			$navstring .= "Page: ".$pagenumber." of ".$totalpages." ";
			$navstring .= " Total Records: ".$totalrecords."";
		
		
		$navstring .= "</td></tr>";
	    $navstring .= "</table>";
	}
 
 $navstring .= "</center>";
  echo $navstring;

}


//function post_inventory_balance($type, $warehouse_code, $product_code, $uom, $quantity, $color,$size, $pattern)
function post_inventory_balance(
				$device_id, 
				$doc_date,
				$doc_no, 
				$doc_item_data_id,
				$module, 
				$type, 
				$warehouse_code, 
				$product_code, 
				$uom, 
				$quantity, 
				$color,
				$size, 
				$pattern)
{
	global $key_table_prefix;
	global $dbconn;
	
	
	
	$uom="";
	
	
	//get info:
	$query_whs="select * from t_".$key_table_prefix."_warehouse 
		where whs_code  ='".$warehouse_code."' ";
	$result_whs  = mysqli_query($dbconn,$query_whs);
	if($line_whs = mysqli_fetch_array($result_whs,MYSQLI_ASSOC))
	{
		$this_warehouse_id = $line_whs["whs_id"];
	}
	
	$query_pdt="select * from t_".$key_table_prefix."_product
		where pdt_code  ='".$product_code."' ";
	$result_pdt  = mysqli_query($dbconn,$query_pdt);
	if($line_pdt = mysqli_fetch_array($result_pdt,MYSQLI_ASSOC))
	{
		$this_product_id = $line_pdt["pdt_id"];
		
		$product_name = $line_pdt["pdt_name"];
		
		$barcode = $line_pdt["pdt_barcode"];
		$factory_code = $line_pdt["pdt_barcode"];
		
	}
	
	$query_check="select * from t_".$key_table_prefix."_balance
				where bal_warehouse_code = '".$warehouse_code."'
				and bal_product_code = '".$product_code."'
				
	 			";
				
				//and bal_uom = '".$uom."'
				
				
	$query_check=$query_check." and bal_color = '".$color."' ";
	$query_check=$query_check." and bal_size = '".$size."' ";
	$query_check=$query_check." and bal_pattern = '".$pattern."' ";

	
	
	$result_check = mysqli_query($dbconn,$query_check) or die("Error ");
	if($line_check = mysqli_fetch_array($result_check,MYSQLI_ASSOC))
	{
		$current_balance= $line_check["bal_quantity"];
		
	}
	else
	{
		//insert
		$query_insert="insert into  t_".$key_table_prefix."_balance
					(
					bal_warehouse_code,
					bal_warehouse_id,
					
					bal_product_code,
					bal_product_id,
					
					bal_color,
					bal_size,
					bal_pattern,
					
					bal_uom,
					bal_quantity,
					
					client_id,
					created_date,
					modified_date
						
					)
					values
					(
					'".$warehouse_code."',
					'".($this_warehouse_id+0)."',
				
					'".$product_code."',
					'".($this_product_id+0)."',		
					
					'".$color."',	
					'".$size."',	
					'".$pattern."',	
					
					'".$uom."',	
					'0',	
					
					'0',	
					'".date("Y-m-d H:i:s")."',
					'".date("Y-m-d H:i:s")."'
						
					)
					
		 			";
		
		$result_insert = mysqli_query($dbconn,$query_insert) or die("Error Insert New Balance Slot".mysqli_error($dbconn));
		
		$current_balance= 0;
	}
	
	$stock_card_quantity = $quantity;
	
	

	if($type=="PLUS")
	{
		$new_balance =  $current_balance + $quantity;
		$stock_card_quantity = $quantity; //positive
		
	}
	
	if($type=="MINUS")
	{
		$new_balance =  $current_balance - $quantity;	
		$stock_card_quantity = 0 - $quantity;  //negative
	}





	$query_udpate="update t_".$key_table_prefix."_balance
				set bal_quantity = '".($new_balance+0)."'
				where bal_warehouse_code = '".$warehouse_code."'
				and bal_product_code = '".$product_code."'
				
				";
				
				//and bal_uom = '".$uom."'
	$query_udpate=$query_udpate." and bal_color = '".$color."' ";
	$query_udpate=$query_udpate." and bal_size = '".$size."' ";
	$query_udpate=$query_udpate." and bal_pattern = '".$pattern."' ";
	
	
	$result_balance = mysqli_query($dbconn,$query_udpate) or die("Error Updating Balance ");
	
		
		
		
		
	//---------------------inject stock card--------------------------------------------
			
				
				
		$query_insert="insert into  t_".$key_table_prefix."_stock_card
					(
					device_id,
					
					stc_date,
					stc_module,
					stc_type,
					stc_doc_no,
					stc_item_data_id,
					
					
					stc_warehouse_code,
					stc_warehouse_id,
					
					
					stc_stock_id,
					stc_stock_code,
					stc_barcode,
					stc_factory_code,
					
					
					stc_stock_name,
					
					stc_color,
					stc_size,
					stc_pattern,
					
					
					stc_uom,
					stc_base_uom,
					
					stc_quantity,
					stc_base_quantity,
					stc_conversion,
					
					
					stc_balance_before,
					stc_balance_after,
					
					client_id,
					created_date,
					modified_date
						
					)
					values
					(
					'".$device_id."',
					
					'".$doc_date."',
					'".$module."',
					'".$type."',
					'".$doc_no."',
					'".$doc_item_data_id."',
					
						
					'".$warehouse_code."',
					'".($this_warehouse_id+0)."',
				
					'".($this_product_id+0)."',	
					'".$product_code."',
					'".$barcode."',
					'".$factory_code."',
					
					'".$product_name."',
						
					
					'".$color."',	
					'".$size."',	
					'".$pattern."',	
					
					'".$uom."',	
					'".$uom."',	
					
					'".($stock_card_quantity+0)."',
					'".($stock_card_quantity+0)."',
					'1',
					
					
					'".($current_balance+0)."',
					'".($new_balance+0)."',
					
					'0',	
					'".date("Y-m-d H:i:s")."',
					'".date("Y-m-d H:i:s")."'
						
					)
					
					";

		$result_insert = mysqli_query($dbconn,$query_insert) or die("Error Insert New Stock Card".mysqli_error($dbconn));
				
		
		
		
}




function mon_register_document_number($vmodule)
{
	global $key_table_prefix,$CurrentUserID;
	global $dbconn;		
        

	$query="select *
		from t_".$key_table_prefix."_document_control
		where doc_module= '".$vmodule."'
		";
	$result=mysqli_query($dbconn,$query);
	if($line=mysqli_fetch_array($result,MYSQLI_ASSOC))
	{


	}
	else{

		$query_create="";
		$query_create=$query_create."INSERT INTO t_".$key_table_prefix."_document_control(";
          $query_create=$query_create." doc_module ,";
          $query_create=$query_create." doc_next_number ,";
		$query_create=$query_create."created_by,";
		$query_create=$query_create."modified_by,";
		$query_create=$query_create."created_date,";
		$query_create=$query_create."modified_date";
		$query_create=$query_create.")VALUES(";

          $query_create=$query_create."'".$vmodule."',";
          $query_create=$query_create."'10000',";
                
		$query_create=$query_create."'".$CurrentUserID."',";
		$query_create=$query_create."'".$CurrentUserID."',";
		$query_create=$query_create."'".date("Y-m-d H:i:s")."',";
		$query_create=$query_create."'".date("Y-m-d H:i:s")."'";
		$query_create=$query_create.");";


		$result_create = mysqli_query($dbconn,$query_create) or die("Creation failed- Err msg  ". mysqli_error($dbconn));

	}



	$query="select *
		from t_".$key_table_prefix."_document_control
		where doc_module = '".$vmodule."'
		 for update ";
	$result=mysqli_query($dbconn,$query);
	if($line=mysqli_fetch_array($result,MYSQLI_ASSOC))
	{
                $this_doc_no=$vmodule."".$line["doc_next_number"];

		$query_update="update t_".$key_table_prefix."_document_control
				set doc_next_number = doc_next_number + 1
				where doc_module ='".$vmodule."'
				";
		$result_update=mysqli_query($dbconn,$query_update);

		return $this_doc_no;


	}




}




function mon_license_registration_machine()
{
	return php_uname();

}


function mon_license_registration()
{
	return true;
}


function toMYSQLString($input_string)
{
	//htmlentities($_REQUEST["shp_description"],ENT_QUOTES).
	$input_string=str_replace(chr(34),'',$input_string);
	$input_string=str_replace(chr(39),'',$input_string);
	$input_string=str_replace(chr(92),'',$input_string);
	return $input_string;
}


function fromMYSQLString($input_string)
{
	
	//return stripslashes($input_string,ENT_QUOTES);
	$input_string=str_replace(chr(34),'',$input_string);
	$input_string=str_replace(chr(39),'',$input_string);
	$input_string=str_replace(chr(92),'',$input_string);
	return $input_string;
}


function toMySQLTime($field_date)
{
  $hour=0;
  $second="00";
  $first_array=explode(" ",$field_date);
  $second_array=explode(":",$first_array[0]);
  
  $hour=$second_array[0]+0;
  
  if(strtoupper($first_array[1])=="AM")
   	{
     	        
  		if($hour>11){$hour=0;}
	}
  else
	{
		if($hour>11)
		{
			$hour=12;
			
		}
		else{
			$hour=12+$second_array[0];	
		}
		
	}


  
  $minute=$second_array[1];
  $second=$second+$second_array[2];
  if(count($first_array)==2 and count($second_array)==2)
	{
	$hour=substr($hour+100,1);
	$second=substr($second+100,1);
	  return $hour.":".$minute.":".$second;
	}
	else
	{
	   return "00:00:00";
	}
}

//date format
function toMySQLDate($field_date)
{
  
  $array=explode("-",$field_date);
  
  if(strlen($array[0])==4)
  	{
		$year=$array[0];
	}else
	{
	  $year="0000";
	}
  
  if(strlen($array[1])==2)
  	{
		$month=$array[1];
	}else
	{
	  $month="00";
	}
  
  if(strlen($array[2])==2)
  	{
	$day=$array[2];
	}else
	{
	  $day="00";
	}
 
	  return $year."-".$month."-".$day;
}


function toDisplayDate($field_date)
{
  
 
  if($field_date=="0000-00-00")
 	{
	   return "";
	}
	else
	{
	  return $field_date;
	}
	
}


function toDisplayTime($field_time)
{
  if($field_time=="00:00:00" )
	  {
	    return "12:00 AM";
	}
 else
	 { 
	     if($field_time=="" )
		{
		    return "12:00 AM";
		}
		else
		{
			
			
		  $time_array=explode(":",$field_time);
		  $session="AM";
		  
		  $hour=0+$time_array[0];
		  $minute=$time_array[1];
		  $second=$time_array[2];
		  
		  if($hour>12)
		  	{
				$hour=$hour-12;
				$session="PM";
			}
			
		 if($hour==12)
		  	{
				$hour=12;
				$session="PM";
			}
			
			
		 if($hour==0)
		 {
			$hour="12";
		 }
			
		return substr($hour+100,1,2).":".$minute." ".$session;
		}
	}
	   
	  
}




function check_time_format($check_time){
 $return_flag=false;
 
  	if((substr($check_time, -2)=="PM")or(substr($check_time, -2)=="AM"))
	  	{
	    	$check_time= substr($check_time, 0, 5);
	    	$time_array=explode(":",$check_time);
			if(count($time_array)>1){//at least got hour and minutes(no second)
			  	
				if(($time_array[0]>0)and($time_array[0]<13))
				  	{
				   	if(($time_array[1]>=0)and($time_array[1]<60))
				   		{
					     	  $return_flag=true;
						}
					}
			}
	    
		} 
 

	return $return_flag;
}


function mon_log_audit($user_name,$user_id,$module_id,$module_data_id,$user_action)
{
 	global $dbconn;
 	global $key_table_prefix;
 
	$query_log="INSERT INTO t_".$key_table_prefix."_log(";
	$query_log=$query_log."log_data_id,";
	$query_log=$query_log."log_user_name,";
	$query_log=$query_log."log_module,";
	$query_log=$query_log."log_action,";
	$query_log=$query_log."created_date,";
	$query_log=$query_log."log_user_id)";
	$query_log=$query_log."VALUES(";
	$query_log=$query_log."'$module_data_id',";
	$query_log=$query_log."'$user_name',";
	$query_log=$query_log."'$module_id',";
	$query_log=$query_log."'$user_action',";
    $query_log=$query_log."'".date("Y-m-d H:i:s")."',";
	$query_log=$query_log."'$user_id'";	
	$query_log=$query_log.");";
	$result_log = mysqli_query($dbconn,$query_log) or die("Loggin failed- Err msg  ". mysqli_error($dbconn));
	


	
}



function mon_get_userfullname($usr_id)
{
	global $dbconn;
	
 	global $key_table_prefix;
	$query_user="select usr_full_name from t_".$key_table_prefix."_user where usr_id = '".$usr_id."'; ";
	$result_user = mysqli_query($dbconn,$query_user) or die("Retrieval failed- Err msg  ". mysqli_error($dbconn));
	if($line_user=mysqli_fetch_array($result_user,MYSQLI_ASSOC))
	{
		return $line_user["usr_full_name"];
	}
	
}








?>
