<?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_DOES_NOT_SUPPORT_ARRAY();
	
	// Check already authenticated
	PHPCUS_PARAM::CUS_REQUIRES_AUTHENTICATION();

	// Check access rights
	PHPCUS_PARAM::CUS_HAS_PERMISSION(PHPCUS_SYSTEM::AUTHORITY_VIEW_EDIT, 'record_eod_csrep');
	
	if
	(
			PHPRC_HTTP_PARAM::REF_ISSET_IN_ARRAY
			(
					[
						'PARAM_REC_EODREP_REC_POSDEVICE_ID',
					    'PARAM_REC_EODREP_TIME_CLOSED'
					], True
			)
	)
	{
		Try
		{
			// Read params
			// ------------------------------------------------------------------
			$PARAM_REC_EODREP_REC_POSDEVICE_ID = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_REC_EODREP_REC_POSDEVICE_ID', false);
			$PARAM_REC_EODREP_TIME_CLOSED = PHPCUS_PARAM::CUS_READ_DATETIME('PARAM_REC_EODREP_TIME_CLOSED', false);

				
			// Prepare PDO Handler
			// ------------------------------------------------------------------
			$DATA_SET = [];
			
			$PDO_HANDLE = PHPRC_PDO_MYSQL::REF_CONNECT();
			PHPRC_PDO_MYSQL::REF_TRANSACTION_BEGIN($PDO_HANDLE);
			
		
			// Execute Stored Procedure
			// ------------------------------------------------------------------
			Try
			{
			    // Update CSREP
			    // Populate Map Array
			    // ...........................................
			    $GET_MAP_ARRAY = PHPCUS_RECORD_EOD_CSREP::CUS_PREPARE_MAPPED_ARRAY_FOR_GET();
			    
			    $GET_MAP_ARRAY['PARAM_REC_EODREP_REC_POSDEVICE_ID'] = $PARAM_REC_EODREP_REC_POSDEVICE_ID;
			    $GET_MAP_ARRAY['PARAM_REC_EODREP_TIME_CLOSED_FROM'] = date_format($PARAM_REC_EODREP_TIME_CLOSED, 'Y-m-d') . ' 00:00:00.000000';
			    $GET_MAP_ARRAY['PARAM_REC_EODREP_TIME_CLOSED_TO'] = date_format($PARAM_REC_EODREP_TIME_CLOSED, 'Y-m-d') . ' 23:59:59.999999';
			    $GET_MAP_ARRAY["PARAM_SKIP"] = 0;
			    $GET_MAP_ARRAY["PARAM_LIMIT"] = 1;
			        
			    // Execute Stored Procedure
			    // ...........................................
			    $DATA_SET = PHPCUS_RECORD_EOD_CSREP::CUS_GET_MA($GET_MAP_ARRAY);   
				if(empty($DATA_SET) || empty($DATA_SET[0]) || isset($DATA_SET[0]['REC_EODREP_ID']) == false)
				{
				    throw new Exception('Missing expected result : $DATA_SET[0][REC_EODREP_ID]');
				}
				
				// Update Z-Reading
				$DATA_SET = PHPCUS_RECORD_EOD_CUSTOM::CUS_ZREADING_COMPUTE
				(
				    $DATA_SET[0]['REC_EODREP_ID'],
				    true,
				    $PDO_HANDLE
				);	
				
				// Get Z-Reading
				$GET_MAP_ARRAY = PHPCUS_RECORD_EOD_ZREADING::CUS_PREPARE_MAPPED_ARRAY_FOR_GET();
				$GET_MAP_ARRAY['PARAM_REC_EODREP_ZREAD_ID'] = $DATA_SET[0]['INSERTED_ID'];
				$GET_MAP_ARRAY["PARAM_SKIP"] = 0;
				$GET_MAP_ARRAY["PARAM_LIMIT"] = 1;		
				$DATA_SET = PHPCUS_RECORD_EOD_ZREADING::CUS_GET_MA
				(
				    $GET_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');
	}

?>