<?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_eod_discount');
	
	if
	(
			PHPRC_HTTP_PARAM::REF_ISSET_IN_ARRAY
			(
					[
							'PARAM_REC_EOD_DISCREP_ID',
							'PARAM_REC_EOD_DISCREP_REC_EODREP_ID', 
							'PARAM_REC_EOD_DISCREP_SENIOR_COUNT', 
							'PARAM_REC_EOD_DISCREP_SENIOR_LESSVAT', 
							'PARAM_REC_EOD_DISCREP_SENIOR_BASIC', 
							'PARAM_REC_EOD_DISCREP_PWD_COUNT', 
							'PARAM_REC_EOD_DISCREP_PWD_LESSVAT', 
							'PARAM_REC_EOD_DISCREP_PWD_BASIC', 
							'PARAM_REC_EOD_DISCREP_GOVSALE_COUNT', 
							'PARAM_REC_EOD_DISCREP_GOVSALE', 
							'PARAM_REC_EOD_DISCREP_MONA_NAME', 
							'PARAM_REC_EOD_DISCREP_MONA_AMOUNT', 
							'PARAM_REC_EOD_DISCREP_MONB_NAME', 
							'PARAM_REC_EOD_DISCREP_MONB_AMOUNT', 
							'PARAM_REC_EOD_DISCREP_MONC_NAME', 
							'PARAM_REC_EOD_DISCREP_MONC_AMOUNT', 
							'PARAM_REC_EOD_DISCREP_MOND_NAME', 
							'PARAM_REC_EOD_DISCREP_MOND_AMOUNT', 
							'PARAM_REC_EOD_DISCREP_MONE_AMOUNT', 
							'PARAM_REC_EOD_DISCREP_MONE_NAME', 
							'PARAM_REC_EOD_DISCREP_STATUS'
							// , 'PARAM_REC_EOD_DISCREP_ID_FILE'
					], True
			)
	)
	{
		Try
		{
			// Read params
			// ------------------------------------------------------------------
			$PARAM_REC_EOD_DISCREP_ID = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_REC_EOD_DISCREP_ID', true);
			$PARAM_REC_EOD_DISCREP_REC_EODREP_ID = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_REC_EOD_DISCREP_REC_EODREP_ID', true);
			$PARAM_REC_EOD_DISCREP_SENIOR_COUNT = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_REC_EOD_DISCREP_SENIOR_COUNT', true);
			$PARAM_REC_EOD_DISCREP_SENIOR_LESSVAT = PHPCUS_PARAM::CUS_READ_DECIMAL('PARAM_REC_EOD_DISCREP_SENIOR_LESSVAT', true);
			$PARAM_REC_EOD_DISCREP_SENIOR_BASIC = PHPCUS_PARAM::CUS_READ_DECIMAL('PARAM_REC_EOD_DISCREP_SENIOR_BASIC', true);
			$PARAM_REC_EOD_DISCREP_PWD_COUNT = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_REC_EOD_DISCREP_PWD_COUNT', true);
			$PARAM_REC_EOD_DISCREP_PWD_LESSVAT = PHPCUS_PARAM::CUS_READ_DECIMAL('PARAM_REC_EOD_DISCREP_PWD_LESSVAT', true);
			$PARAM_REC_EOD_DISCREP_PWD_BASIC = PHPCUS_PARAM::CUS_READ_DECIMAL('PARAM_REC_EOD_DISCREP_PWD_BASIC', true);
			$PARAM_REC_EOD_DISCREP_GOVSALE_COUNT = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_REC_EOD_DISCREP_GOVSALE_COUNT', true);
			$PARAM_REC_EOD_DISCREP_GOVSALE = PHPCUS_PARAM::CUS_READ_DECIMAL('PARAM_REC_EOD_DISCREP_GOVSALE', true);
			$PARAM_REC_EOD_DISCREP_MONA_NAME = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_REC_EOD_DISCREP_MONA_NAME', true);
			$PARAM_REC_EOD_DISCREP_MONA_AMOUNT = PHPCUS_PARAM::CUS_READ_DECIMAL('PARAM_REC_EOD_DISCREP_MONA_AMOUNT', true);
			$PARAM_REC_EOD_DISCREP_MONB_NAME = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_REC_EOD_DISCREP_MONB_NAME', true);
			$PARAM_REC_EOD_DISCREP_MONB_AMOUNT = PHPCUS_PARAM::CUS_READ_DECIMAL('PARAM_REC_EOD_DISCREP_MONB_AMOUNT', true);
			$PARAM_REC_EOD_DISCREP_MONC_NAME = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_REC_EOD_DISCREP_MONC_NAME', true);
			$PARAM_REC_EOD_DISCREP_MONC_AMOUNT = PHPCUS_PARAM::CUS_READ_DECIMAL('PARAM_REC_EOD_DISCREP_MONC_AMOUNT', true);
			$PARAM_REC_EOD_DISCREP_MOND_NAME = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_REC_EOD_DISCREP_MOND_NAME', true);
			$PARAM_REC_EOD_DISCREP_MOND_AMOUNT = PHPCUS_PARAM::CUS_READ_DECIMAL('PARAM_REC_EOD_DISCREP_MOND_AMOUNT', true);
			$PARAM_REC_EOD_DISCREP_MONE_AMOUNT = PHPCUS_PARAM::CUS_READ_DECIMAL('PARAM_REC_EOD_DISCREP_MONE_AMOUNT', true);
			$PARAM_REC_EOD_DISCREP_MONE_NAME = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_REC_EOD_DISCREP_MONE_NAME', true);
			$PARAM_REC_EOD_DISCREP_STATUS = PHPCUS_PARAM::CUS_READ_INT('PARAM_REC_EOD_DISCREP_STATUS', true);
			
			//$PARAM_REC_EOD_DISCREP_ID_FILE = PHPCUS_PARAM::CUS_READ_FILES('PARAM_REC_EOD_DISCREP_ID_FILE');
			$PARAM_ARRAY_COUNT = count($PARAM_REC_EOD_DISCREP_ID);
			
				
			// Validate if array has same size
			// ------------------------------------------------------------------
			PHPCUS_PARAM::CUS_ARRAY_SIZE_MISMATCH
			(
					[
							$PARAM_REC_EOD_DISCREP_ID,
							$PARAM_REC_EOD_DISCREP_REC_EODREP_ID, 
							$PARAM_REC_EOD_DISCREP_SENIOR_COUNT, 
							$PARAM_REC_EOD_DISCREP_SENIOR_LESSVAT, 
							$PARAM_REC_EOD_DISCREP_SENIOR_BASIC, 
							$PARAM_REC_EOD_DISCREP_PWD_COUNT, 
							$PARAM_REC_EOD_DISCREP_PWD_LESSVAT, 
							$PARAM_REC_EOD_DISCREP_PWD_BASIC, 
							$PARAM_REC_EOD_DISCREP_GOVSALE_COUNT, 
							$PARAM_REC_EOD_DISCREP_GOVSALE, 
							$PARAM_REC_EOD_DISCREP_MONA_NAME, 
							$PARAM_REC_EOD_DISCREP_MONA_AMOUNT, 
							$PARAM_REC_EOD_DISCREP_MONB_NAME, 
							$PARAM_REC_EOD_DISCREP_MONB_AMOUNT, 
							$PARAM_REC_EOD_DISCREP_MONC_NAME, 
							$PARAM_REC_EOD_DISCREP_MONC_AMOUNT, 
							$PARAM_REC_EOD_DISCREP_MOND_NAME, 
							$PARAM_REC_EOD_DISCREP_MOND_AMOUNT, 
							$PARAM_REC_EOD_DISCREP_MONE_AMOUNT, 
							$PARAM_REC_EOD_DISCREP_MONE_NAME, 
							$PARAM_REC_EOD_DISCREP_STATUS
							// ,$PARAM_REC_EOD_DISCREP_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_EOD_DISCOUNT::CUS_PREPARE_MAPPED_ARRAY_FOR_SAVE();
					
					$SAVE_MAP_ARRAY['PARAM_REC_EOD_DISCREP_ID'] = $PARAM_REC_EOD_DISCREP_ID[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_REC_EOD_DISCREP_REC_EODREP_ID'] = $PARAM_REC_EOD_DISCREP_REC_EODREP_ID[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_REC_EOD_DISCREP_SENIOR_COUNT'] = $PARAM_REC_EOD_DISCREP_SENIOR_COUNT[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_REC_EOD_DISCREP_SENIOR_LESSVAT'] = $PARAM_REC_EOD_DISCREP_SENIOR_LESSVAT[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_REC_EOD_DISCREP_SENIOR_BASIC'] = $PARAM_REC_EOD_DISCREP_SENIOR_BASIC[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_REC_EOD_DISCREP_PWD_COUNT'] = $PARAM_REC_EOD_DISCREP_PWD_COUNT[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_REC_EOD_DISCREP_PWD_LESSVAT'] = $PARAM_REC_EOD_DISCREP_PWD_LESSVAT[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_REC_EOD_DISCREP_PWD_BASIC'] = $PARAM_REC_EOD_DISCREP_PWD_BASIC[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_REC_EOD_DISCREP_GOVSALE_COUNT'] = $PARAM_REC_EOD_DISCREP_GOVSALE_COUNT[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_REC_EOD_DISCREP_GOVSALE'] = $PARAM_REC_EOD_DISCREP_GOVSALE[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_REC_EOD_DISCREP_MONA_NAME'] = $PARAM_REC_EOD_DISCREP_MONA_NAME[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_REC_EOD_DISCREP_MONA_AMOUNT'] = $PARAM_REC_EOD_DISCREP_MONA_AMOUNT[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_REC_EOD_DISCREP_MONB_NAME'] = $PARAM_REC_EOD_DISCREP_MONB_NAME[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_REC_EOD_DISCREP_MONB_AMOUNT'] = $PARAM_REC_EOD_DISCREP_MONB_AMOUNT[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_REC_EOD_DISCREP_MONC_NAME'] = $PARAM_REC_EOD_DISCREP_MONC_NAME[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_REC_EOD_DISCREP_MONC_AMOUNT'] = $PARAM_REC_EOD_DISCREP_MONC_AMOUNT[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_REC_EOD_DISCREP_MOND_NAME'] = $PARAM_REC_EOD_DISCREP_MOND_NAME[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_REC_EOD_DISCREP_MOND_AMOUNT'] = $PARAM_REC_EOD_DISCREP_MOND_AMOUNT[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_REC_EOD_DISCREP_MONE_AMOUNT'] = $PARAM_REC_EOD_DISCREP_MONE_AMOUNT[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_REC_EOD_DISCREP_MONE_NAME'] = $PARAM_REC_EOD_DISCREP_MONE_NAME[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_REC_EOD_DISCREP_STATUS'] = $PARAM_REC_EOD_DISCREP_STATUS[$ARRAY_CTR];
							
					$DATA_SET = PHPCUS_RECORD_EOD_DISCOUNT::CUS_SAVE_MA
					(
							$SAVE_MAP_ARRAY,
							true,
							$PDO_HANDLE
					);
			
					/*
					if
					(
						isset($PARAM_REC_FILEDIR_ID_FILE['name'][$ARRAY_CTR]) && 
						empty($DATA_SET) == false && 
						isset($DATA_SET[0]['INSERTED_ID'])
					)
					{		
						PHPRC_FSO_DIRECTORY::REF_CREATE(PHPSET::$ROOT . '/upload/priv/Record');
						PHPRC_FSO_DIRECTORY::REF_CREATE(PHPSET::$ROOT . '/upload/priv/Record/eod_discount');
						PHPRC_FSO_DIRECTORY::REF_CREATE(PHPSET::$ROOT . '/upload/priv/Record/eod_discount/' .  floor($DATA_SET[0]['INSERTED_ID'] / 5000));
					
						PHPRC_UPLOAD::REF_UPLOAD_TRANSACTION_MOVEFILE
						(
							$PARAM_REC_EOD_DISCREP_ID_FILE['tmp_name'][$ARRAY_CTR],
							PHPSET::$ROOT . '/upload/priv/Record/eod_discount/' .  floor($DATA_SET[0]['INSERTED_ID'] / 5000) . '/' . $DATA_SET[0]['INSERTED_ID'] . '.' .
							PHPVC_STR::VAR_LCASE(PathInfo($PARAM_REC_EOD_DISCREP_ID_FILE['name'][$ARRAY_CTR], PATHINFO_EXTENSION))
						);
					}
					*/
					
					$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');
	}

?>