<?php

	// Perform Login
    $SHORTCODE_STATUS = PHPCUS_SHORTCODE::CUS_QUICK_LOGIN();
	
	// Check nonce
	if($SHORTCODE_STATUS == false)
    {
		PHPCUS_PARAM::CUS_REQUIRES_NONCE();
	}
	
	// Check array size
	PHPCUS_PARAM::CUS_ARRAY_TOO_LARGE();
	
	// Check already authenticated
	PHPCUS_PARAM::CUS_REQUIRES_AUTHENTICATION();

	// Check access rights
	PHPCUS_PARAM::CUS_HAS_PERMISSION(PHPCUS_SYSTEM::AUTHORITY_VIEW_EDIT, 'record_posdevice');
	
	if
	(
			PHPRC_HTTP_PARAM::REF_ISSET_IN_ARRAY
			(
					[
							'PARAM_REC_POSDEVICE_MODEL', 
							'PARAM_REC_POSDEVICE_CODE'
					], True
			)
	)
	{
		Try
		{
			// Read params
			// ------------------------------------------------------------------
			$PARAM_REC_POSDEVICE_MODEL = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_REC_POSDEVICE_MODEL');
			$PARAM_REC_POSDEVICE_CODE = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_REC_POSDEVICE_CODE');
				
			// Prepare PDO Handler
			// ------------------------------------------------------------------
			$DATA_SET = [];
			$PDO_HANDLE = PHPRC_PDO_MYSQL::REF_CONNECT();
			PHPRC_PDO_MYSQL::REF_TRANSACTION_BEGIN($PDO_HANDLE);
			
		
			// Execute Stored Procedure
			// ------------------------------------------------------------------
			Try
			{			
				$DATA_SET = PHPCUS_RECORD_POSDEVICE_CUSTOM::CUS_LOGIN
				(
				    $PARAM_REC_POSDEVICE_MODEL,
				    $PARAM_REC_POSDEVICE_CODE,
				    true,
					$PDO_HANDLE
				);		
				
				if(empty($DATA_SET) == false && empty($DATA_SET[0]) == false)
				{
				    PHPCUS_Account_Login_Session::$SESSION_AUTHENTICATED_REC_POSDEVICE_ID = $DATA_SET[0]["REC_POSDEVICE_ID"];
				    PHPCUS_Account_Login_Session::$SESSION_AUTHENTICATED_REC_POSDEVICE_ACC_GROUP_ID = $DATA_SET[0]["REC_POSDEVICE_ACC_GROUP_ID"];
				    PHPCUS_Account_Login_Session::$SESSION_AUTHENTICATED_REC_POSDEVICE_DESCRIPTION = $DATA_SET[0]["REC_POSDEVICE_DESCRIPTION"];
				    PHPCUS_Account_Login_Session::$SESSION_AUTHENTICATED_REC_POSDEVICE_MODEL = $DATA_SET[0]["REC_POSDEVICE_MODEL"];
				    PHPCUS_Account_Login_Session::$SESSION_AUTHENTICATED_REC_POSDEVICE_CODE = $DATA_SET[0]["REC_POSDEVICE_CODE"];
				    PHPCUS_Account_Login_Session::$SESSION_AUTHENTICATED_REC_POSDEVICE_OS = $DATA_SET[0]["REC_POSDEVICE_OS"];
				    
				    PHPCUS_Account_Login_Session::$SESSION_AUTHENTICATED_REC_POSDEVICE_MACHINE_ID = $DATA_SET[0]["REC_POSDEVICE_MACHINE_ID"];
				    PHPCUS_Account_Login_Session::$SESSION_AUTHENTICATED_REC_POSDEVICE_PERMIT_NUMBER = $DATA_SET[0]["REC_POSDEVICE_PERMIT_NUMBER"];
				    PHPCUS_Account_Login_Session::$SESSION_AUTHENTICATED_REC_POSDEVICE_DATE_ACCREDITED = $DATA_SET[0]["REC_POSDEVICE_DATE_ACCREDITED"];
				    PHPCUS_Account_Login_Session::$SESSION_AUTHENTICATED_REC_POSDEVICE_ADDRESS = $DATA_SET[0]["REC_POSDEVICE_ADDRESS"];
				    PHPCUS_Account_Login_Session::$SESSION_AUTHENTICATED_REC_POSDEVICE_CONTACT = $DATA_SET[0]["REC_POSDEVICE_CONTACT"];
				    PHPCUS_Account_Login_Session::$SESSION_AUTHENTICATED_REC_POSDEVICE_TIN = $DATA_SET[0]["REC_POSDEVICE_TIN"];
				    PHPCUS_Account_Login_Session::$SESSION_AUTHENTICATED_REC_POSDEVICE_RESET_COUNT = $DATA_SET[0]["REC_POSDEVICE_RESET_COUNT"];
				    PHPCUS_Account_Login_Session::CUS_SESSION_SAVE();
				}
			}
			Catch(Exception $PDO_ERR)
			{
				PHPRC_PDO_MYSQL::REF_TRANSACTION_ROLLBACK($PDO_HANDLE);
				//PHPRC_UPLOAD::REF_UPLOAD_TRANSACTION_ROLLBACK();
				PHPCUS_PARAM::CUS_JSON_ERROR($PDO_ERR);
			}
			PHPRC_PDO_MYSQL::REF_TRANSACTION_COMMIT($PDO_HANDLE);
			//PHPRC_UPLOAD::REF_UPLOAD_TRANSACTION_COMMIT();	
			
			// Return JSON response
			// ------------------------------------------------------------------
			PHPCUS_PARAM::CUS_JSON_SUCCESS($DATA_SET);
			
		}
		Catch(Exception $ERR)
		{
			PHPCUS_PARAM::CUS_JSON_ERROR($ERR);
		}
	}
	else
	{
		PHPCUS_PARAM::CUS_JSON_ERROR('Missing parameters');
	}

?>