<?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_DESCRIPTION', 
					'PARAM_REC_POSDEVICE_MODEL', 
					'PARAM_REC_POSDEVICE_CODE', 
					'PARAM_REC_POSDEVICE_OS'
				], True
			)
	)
	{
		Try
		{
			// Read params
			// ------------------------------------------------------------------
			$PARAM_REC_POSDEVICE_DESCRIPTION = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_REC_POSDEVICE_DESCRIPTION');
			$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');
			$PARAM_REC_POSDEVICE_OS = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_REC_POSDEVICE_OS');
				
			// Prepare PDO Handler
			// ------------------------------------------------------------------
			$DATA_SET = [];
		    $SAVE_MAP_ARRAY = [];
			$PDO_HANDLE = PHPRC_PDO_MYSQL::REF_CONNECT();
			PHPRC_PDO_MYSQL::REF_TRANSACTION_BEGIN($PDO_HANDLE);
			
		
			// Execute Stored Procedure
			// ------------------------------------------------------------------
			Try
			{
					$SAVE_MAP_ARRAY = PHPCUS_RECORD_POSDEVICE_CUSTOM::CUS_PREPARE_MAPPED_ARRAY_FOR_REGISTER();
					
                    $SAVE_MAP_ARRAY['PARAM_REC_POSDEVICE_DESCRIPTION'] = $PARAM_REC_POSDEVICE_DESCRIPTION;
                    $SAVE_MAP_ARRAY['PARAM_REC_POSDEVICE_MODEL'] = $PARAM_REC_POSDEVICE_MODEL;
                    $SAVE_MAP_ARRAY['PARAM_REC_POSDEVICE_CODE'] = $PARAM_REC_POSDEVICE_CODE;
                    $SAVE_MAP_ARRAY['PARAM_REC_POSDEVICE_OS'] = $PARAM_REC_POSDEVICE_OS;
					
					$DATA_SET = PHPCUS_RECORD_POSDEVICE_CUSTOM::CUS_REGISTER_MA
					(
						$SAVE_MAP_ARRAY,
						true,
						$PDO_HANDLE
					);
			}
			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');
	}

?>