<?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_pos_shift');
	
	if
	(
			PHPRC_HTTP_PARAM::REF_ISSET_IN_ARRAY
			(
					[
							'PARAM_REC_SHIFT_ID',
							'PARAM_REC_SHIFT_ACC_GROUP_ID', 
							'PARAM_REC_SHIFT_ACC_DEVICE_ID', 
							'PARAM_REC_SHIFT_ACC_ACCOUNT_ID', 
							'PARAM_REC_SHIFT_TIME_BEGIN', 
							'PARAM_REC_SHIFT_TIME_END', 
							'PARAM_REC_SHIFT_CASH_BEGIN', 
							'PARAM_REC_SHIFT_CREDIT_10KS', 
							'PARAM_REC_SHIFT_CREDIT_5KS', 
							'PARAM_REC_SHIFT_CREDIT_2KS', 
							'PARAM_REC_SHIFT_CREDIT_1KS', 
							'PARAM_REC_SHIFT_CREDIT_500S', 
							'PARAM_REC_SHIFT_CREDIT_200S', 
							'PARAM_REC_SHIFT_CREDIT_100S', 
							'PARAM_REC_SHIFT_CREDIT_50S', 
							'PARAM_REC_SHIFT_CREDIT_20S', 
							'PARAM_REC_SHIFT_CREDIT_10S', 
							'PARAM_REC_SHIFT_CREDIT_5S', 
							'PARAM_REC_SHIFT_CREDIT_1S', 
							'PARAM_REC_SHIFT_CREDIT_CENTS', 
							'PARAM_REC_SHIFT_CREDIT_CHECK_COUNT', 
							'PARAM_REC_SHIFT_CREDIT_CHECK_AMOUNT', 
							'PARAM_REC_SHIFT_STATUS'
							// , 'PARAM_REC_SHIFT_ID_FILE'
					], True
			)
	)
	{
		Try
		{
			// Read params
			// ------------------------------------------------------------------
			$PARAM_REC_SHIFT_ID = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_REC_SHIFT_ID', true);
			$PARAM_REC_SHIFT_ACC_GROUP_ID = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_REC_SHIFT_ACC_GROUP_ID', true);
			$PARAM_REC_SHIFT_ACC_DEVICE_ID = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_REC_SHIFT_ACC_DEVICE_ID', true);
			$PARAM_REC_SHIFT_ACC_ACCOUNT_ID = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_REC_SHIFT_ACC_ACCOUNT_ID', true);
			$PARAM_REC_SHIFT_TIME_BEGIN = PHPCUS_PARAM::CUS_READ_DATETIME('PARAM_REC_SHIFT_TIME_BEGIN', true);
			$PARAM_REC_SHIFT_TIME_END = PHPCUS_PARAM::CUS_READ_DATETIME('PARAM_REC_SHIFT_TIME_END', true);
			$PARAM_REC_SHIFT_CASH_BEGIN = PHPCUS_PARAM::CUS_READ_DECIMAL('PARAM_REC_SHIFT_CASH_BEGIN', true);
			$PARAM_REC_SHIFT_CREDIT_10KS = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_REC_SHIFT_CREDIT_10KS', true);
			$PARAM_REC_SHIFT_CREDIT_5KS = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_REC_SHIFT_CREDIT_5KS', true);
			$PARAM_REC_SHIFT_CREDIT_2KS = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_REC_SHIFT_CREDIT_2KS', true);
			$PARAM_REC_SHIFT_CREDIT_1KS = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_REC_SHIFT_CREDIT_1KS', true);
			$PARAM_REC_SHIFT_CREDIT_500S = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_REC_SHIFT_CREDIT_500S', true);
			$PARAM_REC_SHIFT_CREDIT_200S = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_REC_SHIFT_CREDIT_200S', true);
			$PARAM_REC_SHIFT_CREDIT_100S = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_REC_SHIFT_CREDIT_100S', true);
			$PARAM_REC_SHIFT_CREDIT_50S = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_REC_SHIFT_CREDIT_50S', true);
			$PARAM_REC_SHIFT_CREDIT_20S = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_REC_SHIFT_CREDIT_20S', true);
			$PARAM_REC_SHIFT_CREDIT_10S = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_REC_SHIFT_CREDIT_10S', true);
			$PARAM_REC_SHIFT_CREDIT_5S = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_REC_SHIFT_CREDIT_5S', true);
			$PARAM_REC_SHIFT_CREDIT_1S = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_REC_SHIFT_CREDIT_1S', true);
			$PARAM_REC_SHIFT_CREDIT_CENTS = PHPCUS_PARAM::CUS_READ_DECIMAL('PARAM_REC_SHIFT_CREDIT_CENTS', true);
			$PARAM_REC_SHIFT_CREDIT_CHECK_COUNT = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_REC_SHIFT_CREDIT_CHECK_COUNT', true);
			$PARAM_REC_SHIFT_CREDIT_CHECK_AMOUNT = PHPCUS_PARAM::CUS_READ_DECIMAL('PARAM_REC_SHIFT_CREDIT_CHECK_AMOUNT', true);
			$PARAM_REC_SHIFT_STATUS = PHPCUS_PARAM::CUS_READ_INT('PARAM_REC_SHIFT_STATUS', true);
			
			//$PARAM_REC_SHIFT_ID_FILE = PHPCUS_PARAM::CUS_READ_FILES('PARAM_REC_SHIFT_ID_FILE');
			$PARAM_ARRAY_COUNT = count($PARAM_REC_SHIFT_ID);
			
				
			// Validate if array has same size
			// ------------------------------------------------------------------
			PHPCUS_PARAM::CUS_ARRAY_SIZE_MISMATCH
			(
					[
							$PARAM_REC_SHIFT_ID,
							$PARAM_REC_SHIFT_ACC_GROUP_ID, 
							$PARAM_REC_SHIFT_ACC_DEVICE_ID, 
							$PARAM_REC_SHIFT_ACC_ACCOUNT_ID, 
							$PARAM_REC_SHIFT_TIME_BEGIN, 
							$PARAM_REC_SHIFT_TIME_END, 
							$PARAM_REC_SHIFT_CASH_BEGIN, 
							$PARAM_REC_SHIFT_CREDIT_10KS, 
							$PARAM_REC_SHIFT_CREDIT_5KS, 
							$PARAM_REC_SHIFT_CREDIT_2KS, 
							$PARAM_REC_SHIFT_CREDIT_1KS, 
							$PARAM_REC_SHIFT_CREDIT_500S, 
							$PARAM_REC_SHIFT_CREDIT_200S, 
							$PARAM_REC_SHIFT_CREDIT_100S, 
							$PARAM_REC_SHIFT_CREDIT_50S, 
							$PARAM_REC_SHIFT_CREDIT_20S, 
							$PARAM_REC_SHIFT_CREDIT_10S, 
							$PARAM_REC_SHIFT_CREDIT_5S, 
							$PARAM_REC_SHIFT_CREDIT_1S, 
							$PARAM_REC_SHIFT_CREDIT_CENTS, 
							$PARAM_REC_SHIFT_CREDIT_CHECK_COUNT, 
							$PARAM_REC_SHIFT_CREDIT_CHECK_AMOUNT, 
							$PARAM_REC_SHIFT_STATUS
							// ,$PARAM_REC_SHIFT_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_POS_SHIFT::CUS_PREPARE_MAPPED_ARRAY_FOR_SAVE();
					
					$SAVE_MAP_ARRAY['PARAM_REC_SHIFT_ID'] = $PARAM_REC_SHIFT_ID[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_REC_SHIFT_ACC_GROUP_ID'] = $PARAM_REC_SHIFT_ACC_GROUP_ID[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_REC_SHIFT_ACC_DEVICE_ID'] = $PARAM_REC_SHIFT_ACC_DEVICE_ID[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_REC_SHIFT_ACC_ACCOUNT_ID'] = $PARAM_REC_SHIFT_ACC_ACCOUNT_ID[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_REC_SHIFT_ACC_ACCOUNT_SERVER_ID'] = PHPRC_SQL_FLAGS::FLAG_BIGINT_NULL;
$SAVE_MAP_ARRAY['PARAM_REC_SHIFT_TIME_BEGIN'] = $PARAM_REC_SHIFT_TIME_BEGIN[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_REC_SHIFT_TIME_END'] = $PARAM_REC_SHIFT_TIME_END[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_REC_SHIFT_CASH_BEGIN'] = $PARAM_REC_SHIFT_CASH_BEGIN[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_REC_SHIFT_CREDIT_10KS'] = $PARAM_REC_SHIFT_CREDIT_10KS[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_REC_SHIFT_CREDIT_5KS'] = $PARAM_REC_SHIFT_CREDIT_5KS[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_REC_SHIFT_CREDIT_2KS'] = $PARAM_REC_SHIFT_CREDIT_2KS[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_REC_SHIFT_CREDIT_1KS'] = $PARAM_REC_SHIFT_CREDIT_1KS[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_REC_SHIFT_CREDIT_500S'] = $PARAM_REC_SHIFT_CREDIT_500S[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_REC_SHIFT_CREDIT_200S'] = $PARAM_REC_SHIFT_CREDIT_200S[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_REC_SHIFT_CREDIT_100S'] = $PARAM_REC_SHIFT_CREDIT_100S[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_REC_SHIFT_CREDIT_50S'] = $PARAM_REC_SHIFT_CREDIT_50S[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_REC_SHIFT_CREDIT_20S'] = $PARAM_REC_SHIFT_CREDIT_20S[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_REC_SHIFT_CREDIT_10S'] = $PARAM_REC_SHIFT_CREDIT_10S[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_REC_SHIFT_CREDIT_5S'] = $PARAM_REC_SHIFT_CREDIT_5S[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_REC_SHIFT_CREDIT_1S'] = $PARAM_REC_SHIFT_CREDIT_1S[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_REC_SHIFT_CREDIT_CENTS'] = $PARAM_REC_SHIFT_CREDIT_CENTS[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_REC_SHIFT_CREDIT_CHECK_COUNT'] = $PARAM_REC_SHIFT_CREDIT_CHECK_COUNT[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_REC_SHIFT_CREDIT_CHECK_AMOUNT'] = $PARAM_REC_SHIFT_CREDIT_CHECK_AMOUNT[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_REC_SHIFT_STATUS'] = $PARAM_REC_SHIFT_STATUS[$ARRAY_CTR];
							
					$DATA_SET = PHPCUS_RECORD_POS_SHIFT::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/pos_shift');
						PHPRC_FSO_DIRECTORY::REF_CREATE(PHPSET::$ROOT . '/upload/priv/Record/pos_shift/' .  floor($DATA_SET[0]['INSERTED_ID'] / 5000));
					
						PHPRC_UPLOAD::REF_UPLOAD_TRANSACTION_MOVEFILE
						(
							$PARAM_REC_SHIFT_ID_FILE['tmp_name'][$ARRAY_CTR],
							PHPSET::$ROOT . '/upload/priv/Record/pos_shift/' .  floor($DATA_SET[0]['INSERTED_ID'] / 5000) . '/' . $DATA_SET[0]['INSERTED_ID'] . '.' .
							PHPVC_STR::VAR_LCASE(PathInfo($PARAM_REC_SHIFT_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');
	}

?>