<?php

// plt.php
// VIMS RDT pallet Processing
// ----------------------------------------------------------------------------------------

// ----------------------------------------------------------------------------------------
header('Cache-Control: no-cache, no-store, must-revalidate');   
header('Expires: 0');
// Header to allow Cross-Server AJAX Connection
header('Access-Control-Allow-Origin: *');

// error_reporting(E_ALL);
// ini_set('display_errors', 1);

// Create / Return Pallet Tag
function processPlt($mPlt,$mVirtualPalletLbl,$mTransactionTypeId,$mPltSts,$mCurrentUser,$mHost,$mUser,$mPwd,$mDbName)
{
//	$mParms = json_decode($mJsonObject, true);
// Blank Pallet
	if (($mVirtualPalletLbl == "0") && ($mPlt == ""))
		return("FAIL-Invalid Pallet");
// Check Pallet Type
	if ((substr($mPlt,0,3) == "PCK" && substr($mTransactionTypeId,0,2) != "PB"))
		return("FAIL-Invalid Pallet Type");
	elseif ((substr($mPlt,0,3) == "RCV" && substr($mTransactionTypeId,0,2) != "RB"))
		return("FAIL-Invalid Pallet Type");
	elseif ((substr($mPlt,0,3) == "RXD" && substr($mTransactionTypeId,0,3) != "JRB"))
		return("FAIL-Invalid Pallet Type");
	elseif ((substr($mPlt,0,3) == "REP" && substr($mTransactionTypeId,0,3) != "REPLEN"))
		return("FAIL-Invalid Pallet Type");
	
// mysql connection - Use Passed values...
	if (($mDbHost != null) && ($mDbHost != ""))
		$mConnection=mysqli_connect($mDbHost,$mDbUser,$mDbPwd,$mDbName) or
			exit("FAIL-Connect failed: ".mysqli_connect_errno()." : ". mysqli_connect_error());
// ... or get defaults from function defaultDbConnection
	else
		$mConnection =defaultDbConnection();
	
// Check Pallet Status
	if ($mPlt != "" && $mPltSts != "")
	{
		$mDataQuery = "SELECT inventory_master.id AS id, inventory_master.inventory_status_id AS inventory_status_id, inventory_status.inventory_status_code AS inventory_status_code FROM inventory_master LEFT JOIN tag on inventory_master.parent_tag_id=tag.id LEFT JOIN inventory_status ON inventory_master.inventory_status_id=inventory_status.id WHERE tag.tag_reference='".$mPlt."' LIMIT 1";
		if (!mysqli_query($mConnection,$mDataQuery))
			return "FAIL-".mysqli_error($mConnection)."-".$mDataQuery;
				
		$mData = mysqli_query($mConnection,$mDataQuery);
		$mInvId = "";
		$mInvStsId = "";
		$mInvStsCode = "";
		$mRow = mysqli_fetch_assoc($mData);
		if (mysqli_num_rows($mData) > 0)
		{
			$mInvStsId = ($mRow['inventory_status_id']);
			$mInvStsCode = ($mRow['inventory_status_code']);
// Check Inventory Status of the Pallet
			if (strtolower($mInvStsCode) != strtolower($mPltSts))
			{
				return("FAIL-This Pallet is in Use.");
			}
		}		
	}

// Test for Existing Pallet / Tag
	if ($mPlt != "")
	{
// Select by Serial
		$mDataQuery = "SELECT inventory_master.parent_tag_id AS parent_tag_id, tag.tag_reference AS tag_reference FROM inventory_master LEFT JOIN tag ON inventory_master.parent_tag_id=tag.id WHERE (inventory_master.serial_reference='".$mPlt."' OR inventory_master.serial_reference='".substr($mPlt,1)."') LIMIT 1";
		if (!mysqli_query($mConnection,$mDataQuery))
			return "FAIL-".mysqli_error($mConnection)."-".$mDataQuery;
				
		$mData = mysqli_query($mConnection,$mDataQuery);
		$mTagRef = "";
		$mRow = mysqli_fetch_assoc($mData); 
		$mTagRef = ($mRow['tag_reference']);
		if ($mTagRef != "")
		{
			return($mTagRef);
		}
		
		$mDataQuery = "SELECT id FROM tag WHERE tag_reference='".$mPlt."' AND (active IS null OR active = '1') LIMIT 1";
		if (!mysqli_query($mConnection,$mDataQuery))
			return "FAIL-".mysqli_error($mConnection)."-".$mDataQuery;
				
		$mData = mysqli_query($mConnection,$mDataQuery);
		$mTagId = "";
		$mRow = mysqli_fetch_assoc($mData); 
		$mTagId = ($mRow['id']);
		if ($mTagId == "")
		{
			$mDataQuery = "INSERT INTO tag (id,version,auto_generated,current_location_id,date_created,document_header_id,last_updated,last_updated_by,tag_reference,validated,active,picked) VALUES(DEFAULT,0,0,null,now(),null,now(),'".$mCurrentUser."','".$mPlt."',1,1,0)";
			if (!mysqli_query($mConnection,$mDataQuery))
				return "FAIL-".mysqli_error($mConnection).$mDataQuery;
		}
		return($mPlt);
	}
// Create Virtual Pallet / Tag
	else
	{
		$mDataQuery = "INSERT INTO tag (id,version,auto_generated,current_location_id,date_created,document_header_id,last_updated,last_updated_by,tag_reference,validated,active,picked) VALUES(DEFAULT,0,0,null,now(),null,now(),'".$mCurrentUser."','PLT000000',1,1,0)";
		if (!mysqli_query($mConnection,$mDataQuery))
			return "FAIL-".mysqli_error($mConnection)."-".$mDataQuery;
		$mTagId=mysqli_insert_id($mConnection);
		$mTagReference = "000000".round($mTagId);
		if (substr($mTransactionTypeId,0,2) == "PB")
			$mTagReference = "PCK".substr($mTagReference,-6);
		elseIf (substr($mTransactionTypeId,0,2) == "RB")
			$mTagReference = "RCV".substr($mTagReference,-6);
		elseIf (substr($mTransactionTypeId,0,3) == "JRB")
			$mTagReference = "RXD".substr($mTagReference,-6);
		elseIf (substr($mTransactionTypeId,0,3) == "REP")
			$mTagReference = "REP".substr($mTagReference,-6);
		else
			$mTagReference = "PLT".substr($mTagReference,-6);
		$mDataQuery = "UPDATE tag SET tag_reference='".$mTagReference."' WHERE id=".$mTagId." LIMIT 1";
		if (!mysqli_query($mConnection,$mDataQuery))
				return "FAIL-".mysqli_error($mConnection).$mDataQuery;
		return($mTagReference);
	}
}
?>