<?php

// Check nonce
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, 'mobile_service_code');

if
(
		PHPRC_HTTP_PARAM::REF_ISSET_IN_ARRAY
		(
				[
						'PARAM_SCODE_ID',
						'PARAM_SCODE_SHORTCODE',
						'PARAM_SCODE_COMPANY_CODE',
						'PARAM_SCODE_CODE',
						'PARAM_SCODE_RESPONSE',
						'PARAM_SCODE_CHARGE',
						'PARAM_SCODE_FILTER_STATUS',
						'PARAM_SCODE_FILTER_LIMIT',
						'PARAM_SCODE_STATUS'
				], True
				)
		)
{
	Try
	{
		// Read params
		// ------------------------------------------------------------------
		$PARAM_SCODE_ID = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_SCODE_ID', true);
		$PARAM_SCODE_SHORTCODE = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_SCODE_SHORTCODE', true);
		$PARAM_SCODE_COMPANY_CODE = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_SCODE_COMPANY_CODE', true);
		$PARAM_SCODE_CODE = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_SCODE_CODE', true);
		$PARAM_SCODE_RESPONSE = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_SCODE_RESPONSE', true);
		$PARAM_SCODE_CHARGE = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_SCODE_CHARGE', true);
		$PARAM_SCODE_FILTER_STATUS = PHPCUS_PARAM::CUS_READ_INT('PARAM_SCODE_FILTER_STATUS', true);
		$PARAM_SCODE_FILTER_LIMIT = PHPCUS_PARAM::CUS_READ_INT('PARAM_SCODE_FILTER_LIMIT', true);
		$PARAM_SCODE_STATUS = PHPCUS_PARAM::CUS_READ_INT('PARAM_SCODE_STATUS', true);
		
		$PARAM_ARRAY_COUNT = count($PARAM_SCODE_ID);
		
		
		// Validate if array has same size
		// ------------------------------------------------------------------
		PHPCUS_PARAM::CUS_ARRAY_SIZE_MISMATCH
		(
				[
						$PARAM_SCODE_ID,
						$PARAM_SCODE_SHORTCODE,
						$PARAM_SCODE_COMPANY_CODE,
						$PARAM_SCODE_CODE,
						$PARAM_SCODE_RESPONSE,
						$PARAM_SCODE_CHARGE,
						$PARAM_SCODE_FILTER_STATUS,
						$PARAM_SCODE_FILTER_LIMIT,
						$PARAM_SCODE_STATUS
				]
				);
		
		
		// Prepare PDO Handler
		// ------------------------------------------------------------------
		$FULL_DATA_SET = [];
		$ARRAY_CTR = 0;
		
		$PDO_HANDLE = PHPRC_PDO_MYSQL::REF_CONNECT();
		PHPRC_PDO_MYSQL::REF_TRANSACTION_BEGIN($PDO_HANDLE);
		
		
		// Execute Stored Procedure
		// ------------------------------------------------------------------
		Try
		{
			while($ARRAY_CTR < $PARAM_ARRAY_COUNT)
			{
				$DATA_SET = PHPCUS_MOBILE_SERVICE_CODE::CUS_SAVE
				(
						$PARAM_SCODE_ID[$ARRAY_CTR],
						$PARAM_SCODE_SHORTCODE[$ARRAY_CTR],
						$PARAM_SCODE_COMPANY_CODE[$ARRAY_CTR],
						
						$PARAM_SCODE_CODE[$ARRAY_CTR],
						$PARAM_SCODE_RESPONSE[$ARRAY_CTR],
						$PARAM_SCODE_CHARGE[$ARRAY_CTR],
						$PARAM_SCODE_FILTER_STATUS[$ARRAY_CTR],
						$PARAM_SCODE_FILTER_LIMIT[$ARRAY_CTR],
						$PARAM_SCODE_STATUS[$ARRAY_CTR],
						
						true,
						$PDO_HANDLE
						);
				
				$FULL_DATA_SET = PHPRC_PDO_MYSQL_ARRAY::REF_MERGE($FULL_DATA_SET, $DATA_SET);
				$ARRAY_CTR ++;
			}
			
		}
		Catch(Exception $PDO_ERR)
		{
			PHPRC_PDO_MYSQL::REF_TRANSACTION_ROLLBACK($PDO_HANDLE);
			PHPCUS_PARAM::CUS_JSON_ERROR($PDO_ERR);
		}
		PHPRC_PDO_MYSQL::REF_TRANSACTION_COMMIT($PDO_HANDLE);
		
		
		
		// Return JSON response
		// ------------------------------------------------------------------
		PHPCUS_PARAM::CUS_JSON_SUCCESS($FULL_DATA_SET);
		
	}
	Catch(Exception $ERR)
	{
		PHPCUS_PARAM::CUS_JSON_ERROR($ERR);
	}
}
else
{
	PHPCUS_PARAM::CUS_JSON_ERROR('Missing parameters');
}

?>