<?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_child_collection');
	
	if
	(
			PHPRC_HTTP_PARAM::REF_ISSET_IN_ARRAY
			(
					[
							'PARAM_REC_CHLDCOLLECT_ID',
							'PARAM_REC_CHLDCOLLECT_REC_POSDEVICE_ID', 
							'PARAM_REC_CHLDCOLLECT_ACC_LINKSVR_ID', 
							'PARAM_REC_CHLDCOLLECT_LINKSVR_ACC_GROUP_ID', 
							'PARAM_REC_CHLDCOLLECT_LINKSVR_DATE', 
							'PARAM_REC_CHLDCOLLECT_DESCRIPTION', 
							'PARAM_REC_CHLDCOLLECT_AMOUNT', 
							'PARAM_REC_CHLDCOLLECT_REMARKS', 
							'PARAM_REC_CHLDCOLLECT_LINKSVR_PAID_BANK', 
							'PARAM_REC_CHLDCOLLECT_LINKSVR_PAID_CASH', 
							'PARAM_REC_CHLDCOLLECT_LINKSVR_PAID_CARD', 
							'PARAM_REC_CHLDCOLLECT_LINKSVR_PAID_CHECK', 
							'PARAM_REC_CHLDCOLLECT_LINKSVR_PAID_LOYALTY', 
							'PARAM_REC_CHLDCOLLECT_LINKSVR_PAID_VOUCHER', 
							'PARAM_REC_CHLDCOLLECT_LINKSVR_PAID_OTHER', 
							'PARAM_REC_CHLDCOLLECT_LINKSVR_LOYALTY_EARN', 
							'PARAM_REC_CHLDCOLLECT_STATUS'
							// , 'PARAM_REC_CHLDCOLLECT_ID_FILE'
					], True
			)
	)
	{
		Try
		{
			// Read params
			// ------------------------------------------------------------------
			$PARAM_REC_CHLDCOLLECT_ID = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_REC_CHLDCOLLECT_ID', true);
			$PARAM_REC_CHLDCOLLECT_REC_POSDEVICE_ID = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_REC_CHLDCOLLECT_REC_POSDEVICE_ID', true);
			$PARAM_REC_CHLDCOLLECT_ACC_LINKSVR_ID = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_REC_CHLDCOLLECT_ACC_LINKSVR_ID', true);
			$PARAM_REC_CHLDCOLLECT_LINKSVR_ACC_GROUP_ID = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_REC_CHLDCOLLECT_LINKSVR_ACC_GROUP_ID', true);
			$PARAM_REC_CHLDCOLLECT_LINKSVR_DATE = PHPCUS_PARAM::CUS_READ_DATETIME('PARAM_REC_CHLDCOLLECT_LINKSVR_DATE', true);
			$PARAM_REC_CHLDCOLLECT_DESCRIPTION = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_REC_CHLDCOLLECT_DESCRIPTION', true);
			$PARAM_REC_CHLDCOLLECT_AMOUNT = PHPCUS_PARAM::CUS_READ_DECIMAL('PARAM_REC_CHLDCOLLECT_AMOUNT', true);
			$PARAM_REC_CHLDCOLLECT_REMARKS = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_REC_CHLDCOLLECT_REMARKS', true);
			$PARAM_REC_CHLDCOLLECT_LINKSVR_PAID_BANK = PHPCUS_PARAM::CUS_READ_DECIMAL('PARAM_REC_CHLDCOLLECT_LINKSVR_PAID_BANK', true);
			$PARAM_REC_CHLDCOLLECT_LINKSVR_PAID_CASH = PHPCUS_PARAM::CUS_READ_DECIMAL('PARAM_REC_CHLDCOLLECT_LINKSVR_PAID_CASH', true);
			$PARAM_REC_CHLDCOLLECT_LINKSVR_PAID_CARD = PHPCUS_PARAM::CUS_READ_DECIMAL('PARAM_REC_CHLDCOLLECT_LINKSVR_PAID_CARD', true);
			$PARAM_REC_CHLDCOLLECT_LINKSVR_PAID_CHECK = PHPCUS_PARAM::CUS_READ_DECIMAL('PARAM_REC_CHLDCOLLECT_LINKSVR_PAID_CHECK', true);
			$PARAM_REC_CHLDCOLLECT_LINKSVR_PAID_LOYALTY = PHPCUS_PARAM::CUS_READ_DECIMAL('PARAM_REC_CHLDCOLLECT_LINKSVR_PAID_LOYALTY', true);
			$PARAM_REC_CHLDCOLLECT_LINKSVR_PAID_VOUCHER = PHPCUS_PARAM::CUS_READ_DECIMAL('PARAM_REC_CHLDCOLLECT_LINKSVR_PAID_VOUCHER', true);
			$PARAM_REC_CHLDCOLLECT_LINKSVR_PAID_OTHER = PHPCUS_PARAM::CUS_READ_DECIMAL('PARAM_REC_CHLDCOLLECT_LINKSVR_PAID_OTHER', true);
			$PARAM_REC_CHLDCOLLECT_LINKSVR_LOYALTY_EARN = PHPCUS_PARAM::CUS_READ_DECIMAL('PARAM_REC_CHLDCOLLECT_LINKSVR_LOYALTY_EARN', true);
			$PARAM_REC_CHLDCOLLECT_STATUS = PHPCUS_PARAM::CUS_READ_INT('PARAM_REC_CHLDCOLLECT_STATUS', true);
			
			//$PARAM_REC_CHLDCOLLECT_ID_FILE = PHPCUS_PARAM::CUS_READ_FILES('PARAM_REC_CHLDCOLLECT_ID_FILE');
			$PARAM_ARRAY_COUNT = count($PARAM_REC_CHLDCOLLECT_ID);
			
				
			// Validate if array has same size
			// ------------------------------------------------------------------
			PHPCUS_PARAM::CUS_ARRAY_SIZE_MISMATCH
			(
					[
							$PARAM_REC_CHLDCOLLECT_ID,
							$PARAM_REC_CHLDCOLLECT_REC_POSDEVICE_ID, 
							$PARAM_REC_CHLDCOLLECT_ACC_LINKSVR_ID, 
							$PARAM_REC_CHLDCOLLECT_LINKSVR_ACC_GROUP_ID, 
							$PARAM_REC_CHLDCOLLECT_LINKSVR_DATE, 
							$PARAM_REC_CHLDCOLLECT_DESCRIPTION, 
							$PARAM_REC_CHLDCOLLECT_AMOUNT, 
							$PARAM_REC_CHLDCOLLECT_REMARKS, 
							$PARAM_REC_CHLDCOLLECT_LINKSVR_PAID_BANK, 
							$PARAM_REC_CHLDCOLLECT_LINKSVR_PAID_CASH, 
							$PARAM_REC_CHLDCOLLECT_LINKSVR_PAID_CARD, 
							$PARAM_REC_CHLDCOLLECT_LINKSVR_PAID_CHECK, 
							$PARAM_REC_CHLDCOLLECT_LINKSVR_PAID_LOYALTY, 
							$PARAM_REC_CHLDCOLLECT_LINKSVR_PAID_VOUCHER, 
							$PARAM_REC_CHLDCOLLECT_LINKSVR_PAID_OTHER, 
							$PARAM_REC_CHLDCOLLECT_LINKSVR_LOYALTY_EARN, 
							$PARAM_REC_CHLDCOLLECT_STATUS
							// ,$PARAM_REC_CHLDCOLLECT_ID_FILE['name']
					]
			);
			
				
			// Prepare PDO Handler
			// ------------------------------------------------------------------
			$FULL_DATA_SET = [];
			$ARRAY_CTR = 0;
			
			$SAVE_MAP_ARRAY = [];
			$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)
				{
					$SAVE_MAP_ARRAY = PHPCUS_RECORD_CHILD_COLLECTION::CUS_PREPARE_MAPPED_ARRAY_FOR_SAVE();
					
					$SAVE_MAP_ARRAY['PARAM_REC_CHLDCOLLECT_ID'] = $PARAM_REC_CHLDCOLLECT_ID[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_REC_CHLDCOLLECT_REC_POSDEVICE_ID'] = $PARAM_REC_CHLDCOLLECT_REC_POSDEVICE_ID[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_REC_CHLDCOLLECT_ACC_LINKSVR_ID'] = $PARAM_REC_CHLDCOLLECT_ACC_LINKSVR_ID[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_REC_CHLDCOLLECT_LINKSVR_ACC_GROUP_ID'] = $PARAM_REC_CHLDCOLLECT_LINKSVR_ACC_GROUP_ID[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_REC_CHLDCOLLECT_LINKSVR_DATE'] = $PARAM_REC_CHLDCOLLECT_LINKSVR_DATE[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_REC_CHLDCOLLECT_DESCRIPTION'] = $PARAM_REC_CHLDCOLLECT_DESCRIPTION[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_REC_CHLDCOLLECT_AMOUNT'] = $PARAM_REC_CHLDCOLLECT_AMOUNT[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_REC_CHLDCOLLECT_REMARKS'] = $PARAM_REC_CHLDCOLLECT_REMARKS[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_REC_CHLDCOLLECT_LINKSVR_PAID_BANK'] = $PARAM_REC_CHLDCOLLECT_LINKSVR_PAID_BANK[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_REC_CHLDCOLLECT_LINKSVR_PAID_CASH'] = $PARAM_REC_CHLDCOLLECT_LINKSVR_PAID_CASH[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_REC_CHLDCOLLECT_LINKSVR_PAID_CARD'] = $PARAM_REC_CHLDCOLLECT_LINKSVR_PAID_CARD[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_REC_CHLDCOLLECT_LINKSVR_PAID_CHECK'] = $PARAM_REC_CHLDCOLLECT_LINKSVR_PAID_CHECK[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_REC_CHLDCOLLECT_LINKSVR_PAID_LOYALTY'] = $PARAM_REC_CHLDCOLLECT_LINKSVR_PAID_LOYALTY[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_REC_CHLDCOLLECT_LINKSVR_PAID_VOUCHER'] = $PARAM_REC_CHLDCOLLECT_LINKSVR_PAID_VOUCHER[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_REC_CHLDCOLLECT_LINKSVR_PAID_OTHER'] = $PARAM_REC_CHLDCOLLECT_LINKSVR_PAID_OTHER[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_REC_CHLDCOLLECT_LINKSVR_LOYALTY_EARN'] = $PARAM_REC_CHLDCOLLECT_LINKSVR_LOYALTY_EARN[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_REC_CHLDCOLLECT_STATUS'] = $PARAM_REC_CHLDCOLLECT_STATUS[$ARRAY_CTR];
							
					$DATA_SET = PHPCUS_RECORD_CHILD_COLLECTION::CUS_SAVE_MA
					(
							$SAVE_MAP_ARRAY,
							true,
							$PDO_HANDLE
					);
			
					/*
					if
					(
						isset($PARAM_REC_CHLDCOLLECT_ID_FILE['name'][$ARRAY_CTR]) && 
						empty($DATA_SET) == false && 
						isset($DATA_SET[0]['INSERTED_ID'])
					)
					{		
						$FILE_PATH = PHPRC_UPLOAD::REF_SUGGEST_PATH
						(
							PHPSET::$ROOT . '/Upload/priv/Record/child_collection/', 
							$DATA_SET[0]['INSERTED_ID'],
							$PARAM_REC_CHLDCOLLECT_ID_FILE['name'][$ARRAY_CTR]
						);
						PHPRC_UPLOAD::REF_UPLOAD_TRANSACTION_MOVEFILE
						(
							$PARAM_REC_CHLDCOLLECT_ID_FILE['tmp_name'][$ARRAY_CTR],
							$FILE_PATH,
							true
						);
					}
					*/
					
					$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);
				//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($FULL_DATA_SET);
			
		}
		Catch(Exception $ERR)
		{
			PHPCUS_PARAM::CUS_JSON_ERROR($ERR);
		}
	}
	else
	{
		PHPCUS_PARAM::CUS_JSON_ERROR('Missing parameters');
	}

?>