<?php

	// Perform Login
	$CURRENT_MODULE_KEY = 'record_stock_card';
    $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, $CURRENT_MODULE_KEY);
	
	if
	(
			PHPRC_HTTP_PARAM::REF_ISSET_IN_ARRAY
			(
					[
							'PARAM_REC_STOCKCARD_ID',
							'PARAM_REC_STOCKCARD_CAT_PRODUCT_ID', 
							'PARAM_REC_STOCKCARD_CAT_STOCKFLOW_ID', 
							'PARAM_REC_STOCKCARD_QUANTITY', 
							'PARAM_REC_STOCKCARD_REMARKS', 
					        'PARAM_REC_STOCKCARD_DATE_TRANSACTION', 
							'PARAM_REC_STOCKCARD_STATUS'
							// , 'PARAM_REC_STOCKCARD_ID_FILE'
					], True
			)
	)
	{
		Try
		{
			// Read params
			// ------------------------------------------------------------------
			$PARAM_REC_STOCKCARD_ID = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_REC_STOCKCARD_ID', true);
			$PARAM_REC_STOCKCARD_CAT_PRODUCT_ID = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_REC_STOCKCARD_CAT_PRODUCT_ID', true);
			$PARAM_REC_STOCKCARD_CAT_STOCKFLOW_ID = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_REC_STOCKCARD_CAT_STOCKFLOW_ID', true);
			$PARAM_REC_STOCKCARD_QUANTITY = PHPCUS_PARAM::CUS_READ_DECIMAL('PARAM_REC_STOCKCARD_QUANTITY', true);
			$PARAM_REC_STOCKCARD_REMARKS = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_REC_STOCKCARD_REMARKS', true);
			$PARAM_REC_STOCKCARD_DATE_TRANSACTION = PHPCUS_PARAM::CUS_READ_DATETIME('PARAM_REC_STOCKCARD_DATE_TRANSACTION', true);
			$PARAM_REC_STOCKCARD_STATUS = PHPCUS_PARAM::CUS_READ_INT('PARAM_REC_STOCKCARD_STATUS', true);
			
			//$PARAM_REC_STOCKCARD_ID_FILE = PHPCUS_PARAM::CUS_READ_FILES('PARAM_REC_STOCKCARD_ID_FILE');
			$PARAM_ARRAY_COUNT = count($PARAM_REC_STOCKCARD_ID);
			
				
			// Validate if array has same size
			// ------------------------------------------------------------------
			PHPCUS_PARAM::CUS_ARRAY_SIZE_MISMATCH
			(
					[
							$PARAM_REC_STOCKCARD_ID,
							$PARAM_REC_STOCKCARD_CAT_PRODUCT_ID, 
							$PARAM_REC_STOCKCARD_CAT_STOCKFLOW_ID, 
							$PARAM_REC_STOCKCARD_QUANTITY, 
							$PARAM_REC_STOCKCARD_REMARKS, 
					        $PARAM_REC_STOCKCARD_DATE_TRANSACTION,
							$PARAM_REC_STOCKCARD_STATUS
							// ,$PARAM_REC_STOCKCARD_ID_FILE['name']
					]
			);
			
			
			// Abort if already processed
			// ------------------------------------------------------------------
			$PARAM_REQUEST_UNIQUE_VALUE = null;
			if(PHPRC_HTTP_PARAM::REF_ISSET('PARAM_REQUEST_UNIQUE_VALUE'))
			{
				$PARAM_REQUEST_UNIQUE_VALUE = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_REQUEST_UNIQUE_VALUE', false);
			}
			else
			{
				// Dismiss Session
				PHPVC_SESSION::VAR_STOP();
			}
			
			if(PHPVC_STR::VAR_IS_BLANK($PARAM_REQUEST_UNIQUE_VALUE) == false)
			{
				$ARR_INDEX = array_search($CURRENT_MODULE_KEY, PHPCUS_Account_Login_Session::$SESSION_REQUEST_KEY); 
				if
				(
					$ARR_INDEX !== false &&
					PHPCUS_Account_Login_Session::$SESSION_REQUEST_LAST_VALUE[$ARR_INDEX] == $PARAM_REQUEST_UNIQUE_VALUE
				)
				{
					PHPCUS_PARAM::CUS_JSON_ERROR('Given Request is already processed.');
				}
			}
		
		
			// 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_STOCK_CARD_CUSTOM::CUS_PREPARE_MAPPED_ARRAY_FOR_BACKDATE();
					
					$SAVE_MAP_ARRAY['PARAM_REC_STOCKCARD_ID'] = $PARAM_REC_STOCKCARD_ID[$ARRAY_CTR];
                    $SAVE_MAP_ARRAY['PARAM_REC_STOCKCARD_CAT_PRODUCT_ID'] = $PARAM_REC_STOCKCARD_CAT_PRODUCT_ID[$ARRAY_CTR];
                    $SAVE_MAP_ARRAY['PARAM_REC_STOCKCARD_CAT_STOCKFLOW_ID'] = $PARAM_REC_STOCKCARD_CAT_STOCKFLOW_ID[$ARRAY_CTR];
                    $SAVE_MAP_ARRAY['PARAM_REC_STOCKCARD_QUANTITY'] = $PARAM_REC_STOCKCARD_QUANTITY[$ARRAY_CTR];
                    $SAVE_MAP_ARRAY['PARAM_REC_STOCKCARD_REMARKS'] = $PARAM_REC_STOCKCARD_REMARKS[$ARRAY_CTR];
                    $SAVE_MAP_ARRAY['PARAM_REC_STOCKCARD_DATE_TRANSACTION'] = $PARAM_REC_STOCKCARD_DATE_TRANSACTION[$ARRAY_CTR];
                    $SAVE_MAP_ARRAY['PARAM_REC_STOCKCARD_STATUS'] = $PARAM_REC_STOCKCARD_STATUS[$ARRAY_CTR];
							
					$DATA_SET = PHPCUS_RECORD_STOCK_CARD_CUSTOM::CUS_BACKDATE_MA
					(
							$SAVE_MAP_ARRAY,
							true,
							$PDO_HANDLE
					);
			
					/*
					if
					(
						isset($PARAM_REC_STOCKCARD_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/stock_card/', 
							$DATA_SET[0]['INSERTED_ID'],
							$PARAM_REC_STOCKCARD_ID_FILE['name'][$ARRAY_CTR]
						);
						PHPRC_UPLOAD::REF_UPLOAD_TRANSACTION_MOVEFILE
						(
							$PARAM_REC_STOCKCARD_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();
					
					
			// Update processed request key
			// ------------------------------------------------------------------
			if(PHPVC_STR::VAR_IS_BLANK($PARAM_REQUEST_UNIQUE_VALUE) == false)
			{
				$ARR_INDEX = array_search($CURRENT_MODULE_KEY, PHPCUS_Account_Login_Session::$SESSION_REQUEST_KEY);
				if($ARR_INDEX !== false)
				{
					PHPCUS_Account_Login_Session::$SESSION_REQUEST_LAST_VALUE[$ARR_INDEX] = $PARAM_REQUEST_UNIQUE_VALUE;
				}
				else
				{
					array_push(PHPCUS_Account_Login_Session::$SESSION_REQUEST_KEY, $CURRENT_MODULE_KEY);
					array_push(PHPCUS_Account_Login_Session::$SESSION_REQUEST_LAST_VALUE, $PARAM_REQUEST_UNIQUE_VALUE);
				}
				PHPCUS_Account_Login_Session::CUS_SESSION_SAVE_THEN_CLOSE();
			}			
			
			
			// 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');
	}

?>