<?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, 'category_product');
	
	if
	(
			PHPRC_HTTP_PARAM::REF_ISSET_IN_ARRAY
			(
					[
							'PARAM_CAT_PRODUCT_ID',
							'PARAM_CAT_PRODUCT_BARCODE', 
							'PARAM_CAT_PRODUCT_ITEMCODE', 
							'PARAM_CAT_PRODUCT_DESCRIPTION', 
							'PARAM_CAT_PRODUCT_ALIAS_MAIN', 
							'PARAM_CAT_PRODUCT_ALIAS_SUB', 
							'PARAM_CAT_PRODUCT_CAT_PRODTYPE_ID_LV1', 
							'PARAM_CAT_PRODUCT_CAT_PRODTYPE_ID_LV2', 
							'PARAM_CAT_PRODUCT_CAT_PRODTYPE_ID_LV3', 
							'PARAM_CAT_PRODUCT_CAT_BRAND_ID', 
							'PARAM_CAT_PRODUCT_CAT_UOM_ID', 
							'PARAM_CAT_PRODUCT_CAT_DISCOUNT_ID', 
							'PARAM_CAT_PRODUCT_CAT_TAXMODEL_ID', 
							'PARAM_CAT_PRODUCT_AMOUNT_COST', 
							'PARAM_CAT_PRODUCT_AMOUNT_PRICE', 
							'PARAM_CAT_PRODUCT_AMOUNT_VAT', 
							'PARAM_CAT_PRODUCT_AMOUNT_POINT', 
							'PARAM_CAT_PRODUCT_ALLOW_DISCOUNT', 
							'PARAM_CAT_PRODUCT_ALLOW_SENIOR', 
							'PARAM_CAT_PRODUCT_IS_BASICCOM', 
							'PARAM_CAT_PRODUCT_IS_SERVICE', 
							'PARAM_CAT_PRODUCT_REORDER_POINT', 
							'PARAM_CAT_PRODUCT_REORDER_QUANTITY', 
							'PARAM_CAT_PRODUCT_CAT_PRODUCT_ID', 
							'PARAM_CAT_PRODUCT_PER_PIECE_QTY', 
							'PARAM_CAT_PRODUCT_CAT_SUPPLIER_ID', 
							'PARAM_CAT_PRODUCT_IS_INVENTORY_ONLY', 
							'PARAM_CAT_PRODUCT_IS_SERIALIZED', 
							'PARAM_CAT_PRODUCT_STATUS'
							// , 'PARAM_CAT_PRODUCT_ID_FILE'
					], True
			)
	)
	{
		Try
		{
			// Read params
			// ------------------------------------------------------------------
			$PARAM_CAT_PRODUCT_ID = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_CAT_PRODUCT_ID', true);
			$PARAM_CAT_PRODUCT_BARCODE = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_CAT_PRODUCT_BARCODE', true);
			$PARAM_CAT_PRODUCT_ITEMCODE = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_CAT_PRODUCT_ITEMCODE', true);
			$PARAM_CAT_PRODUCT_DESCRIPTION = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_CAT_PRODUCT_DESCRIPTION', true);
			$PARAM_CAT_PRODUCT_ALIAS_MAIN = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_CAT_PRODUCT_ALIAS_MAIN', true);
			$PARAM_CAT_PRODUCT_ALIAS_SUB = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_CAT_PRODUCT_ALIAS_SUB', true);
			$PARAM_CAT_PRODUCT_CAT_PRODTYPE_ID_LV1 = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_CAT_PRODUCT_CAT_PRODTYPE_ID_LV1', true);
			$PARAM_CAT_PRODUCT_CAT_PRODTYPE_ID_LV2 = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_CAT_PRODUCT_CAT_PRODTYPE_ID_LV2', true);
			$PARAM_CAT_PRODUCT_CAT_PRODTYPE_ID_LV3 = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_CAT_PRODUCT_CAT_PRODTYPE_ID_LV3', true);
			$PARAM_CAT_PRODUCT_CAT_BRAND_ID = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_CAT_PRODUCT_CAT_BRAND_ID', true);
			$PARAM_CAT_PRODUCT_CAT_UOM_ID = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_CAT_PRODUCT_CAT_UOM_ID', true);
			$PARAM_CAT_PRODUCT_CAT_DISCOUNT_ID = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_CAT_PRODUCT_CAT_DISCOUNT_ID', true);
			$PARAM_CAT_PRODUCT_CAT_TAXMODEL_ID = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_CAT_PRODUCT_CAT_TAXMODEL_ID', true);
			$PARAM_CAT_PRODUCT_AMOUNT_COST = PHPCUS_PARAM::CUS_READ_DECIMAL('PARAM_CAT_PRODUCT_AMOUNT_COST', true);
			$PARAM_CAT_PRODUCT_AMOUNT_PRICE = PHPCUS_PARAM::CUS_READ_DECIMAL('PARAM_CAT_PRODUCT_AMOUNT_PRICE', true);
			$PARAM_CAT_PRODUCT_AMOUNT_VAT = PHPCUS_PARAM::CUS_READ_DECIMAL('PARAM_CAT_PRODUCT_AMOUNT_VAT', true);
			$PARAM_CAT_PRODUCT_AMOUNT_POINT = PHPCUS_PARAM::CUS_READ_DECIMAL('PARAM_CAT_PRODUCT_AMOUNT_POINT', true);
			$PARAM_CAT_PRODUCT_ALLOW_DISCOUNT = PHPCUS_PARAM::CUS_READ_INT('PARAM_CAT_PRODUCT_ALLOW_DISCOUNT', true);
			$PARAM_CAT_PRODUCT_ALLOW_SENIOR = PHPCUS_PARAM::CUS_READ_INT('PARAM_CAT_PRODUCT_ALLOW_SENIOR', true);
			$PARAM_CAT_PRODUCT_IS_BASICCOM = PHPCUS_PARAM::CUS_READ_INT('PARAM_CAT_PRODUCT_IS_BASICCOM', true);
			$PARAM_CAT_PRODUCT_IS_SERVICE = PHPCUS_PARAM::CUS_READ_INT('PARAM_CAT_PRODUCT_IS_SERVICE', true);
			$PARAM_CAT_PRODUCT_REORDER_POINT = PHPCUS_PARAM::CUS_READ_DECIMAL('PARAM_CAT_PRODUCT_REORDER_POINT', true);
			$PARAM_CAT_PRODUCT_REORDER_QUANTITY = PHPCUS_PARAM::CUS_READ_DECIMAL('PARAM_CAT_PRODUCT_REORDER_QUANTITY', true);
			$PARAM_CAT_PRODUCT_CAT_PRODUCT_ID = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_CAT_PRODUCT_CAT_PRODUCT_ID', true);
			$PARAM_CAT_PRODUCT_PER_PIECE_QTY = PHPCUS_PARAM::CUS_READ_DECIMAL('PARAM_CAT_PRODUCT_PER_PIECE_QTY', true);
			$PARAM_CAT_PRODUCT_CAT_SUPPLIER_ID = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_CAT_PRODUCT_CAT_SUPPLIER_ID', true);
			$PARAM_CAT_PRODUCT_IS_INVENTORY_ONLY = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_CAT_PRODUCT_IS_INVENTORY_ONLY', true);
			$PARAM_CAT_PRODUCT_IS_SERIALIZED = PHPCUS_PARAM::CUS_READ_INT('PARAM_CAT_PRODUCT_IS_SERIALIZED', true);
			$PARAM_CAT_PRODUCT_STATUS = PHPCUS_PARAM::CUS_READ_INT('PARAM_CAT_PRODUCT_STATUS', true);
			
			$PARAM_CAT_PRODUCT_ID_FILE = PHPCUS_PARAM::CUS_READ_FILES('PARAM_CAT_PRODUCT_ID_FILE', true);
			$PARAM_ARRAY_COUNT = count($PARAM_CAT_PRODUCT_ID);
			
				
			// Validate if array has same size
			// ------------------------------------------------------------------
			PHPCUS_PARAM::CUS_ARRAY_SIZE_MISMATCH
			(
					[
							$PARAM_CAT_PRODUCT_ID,
							$PARAM_CAT_PRODUCT_BARCODE, 
							$PARAM_CAT_PRODUCT_ITEMCODE, 
							$PARAM_CAT_PRODUCT_DESCRIPTION, 
							$PARAM_CAT_PRODUCT_ALIAS_MAIN, 
							$PARAM_CAT_PRODUCT_ALIAS_SUB, 
							$PARAM_CAT_PRODUCT_CAT_PRODTYPE_ID_LV1, 
							$PARAM_CAT_PRODUCT_CAT_PRODTYPE_ID_LV2, 
							$PARAM_CAT_PRODUCT_CAT_PRODTYPE_ID_LV3, 
							$PARAM_CAT_PRODUCT_CAT_BRAND_ID, 
							$PARAM_CAT_PRODUCT_CAT_UOM_ID, 
							$PARAM_CAT_PRODUCT_CAT_DISCOUNT_ID, 
							$PARAM_CAT_PRODUCT_CAT_TAXMODEL_ID, 
							$PARAM_CAT_PRODUCT_AMOUNT_COST, 
							$PARAM_CAT_PRODUCT_AMOUNT_PRICE, 
							$PARAM_CAT_PRODUCT_AMOUNT_VAT, 
							$PARAM_CAT_PRODUCT_AMOUNT_POINT, 
							$PARAM_CAT_PRODUCT_ALLOW_DISCOUNT, 
							$PARAM_CAT_PRODUCT_ALLOW_SENIOR, 
							$PARAM_CAT_PRODUCT_IS_BASICCOM, 
							$PARAM_CAT_PRODUCT_IS_SERVICE, 
							$PARAM_CAT_PRODUCT_REORDER_POINT, 
							$PARAM_CAT_PRODUCT_REORDER_QUANTITY, 
							$PARAM_CAT_PRODUCT_CAT_PRODUCT_ID, 
							$PARAM_CAT_PRODUCT_PER_PIECE_QTY, 
							$PARAM_CAT_PRODUCT_CAT_SUPPLIER_ID, 
							$PARAM_CAT_PRODUCT_IS_INVENTORY_ONLY, 
							$PARAM_CAT_PRODUCT_IS_SERIALIZED, 
							$PARAM_CAT_PRODUCT_STATUS
							// ,$PARAM_CAT_PRODUCT_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_CATEGORY_PRODUCT::CUS_PREPARE_MAPPED_ARRAY_FOR_SAVE();
					
					$SAVE_MAP_ARRAY['PARAM_CAT_PRODUCT_ID'] = $PARAM_CAT_PRODUCT_ID[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_CAT_PRODUCT_BARCODE'] = $PARAM_CAT_PRODUCT_BARCODE[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_CAT_PRODUCT_ITEMCODE'] = $PARAM_CAT_PRODUCT_ITEMCODE[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_CAT_PRODUCT_DESCRIPTION'] = $PARAM_CAT_PRODUCT_DESCRIPTION[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_CAT_PRODUCT_ALIAS_MAIN'] = $PARAM_CAT_PRODUCT_ALIAS_MAIN[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_CAT_PRODUCT_ALIAS_SUB'] = $PARAM_CAT_PRODUCT_ALIAS_SUB[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_CAT_PRODUCT_CAT_PRODTYPE_ID_LV1'] = $PARAM_CAT_PRODUCT_CAT_PRODTYPE_ID_LV1[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_CAT_PRODUCT_CAT_PRODTYPE_ID_LV2'] = $PARAM_CAT_PRODUCT_CAT_PRODTYPE_ID_LV2[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_CAT_PRODUCT_CAT_PRODTYPE_ID_LV3'] = $PARAM_CAT_PRODUCT_CAT_PRODTYPE_ID_LV3[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_CAT_PRODUCT_CAT_BRAND_ID'] = $PARAM_CAT_PRODUCT_CAT_BRAND_ID[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_CAT_PRODUCT_CAT_UOM_ID'] = $PARAM_CAT_PRODUCT_CAT_UOM_ID[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_CAT_PRODUCT_CAT_DISCOUNT_ID'] = $PARAM_CAT_PRODUCT_CAT_DISCOUNT_ID[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_CAT_PRODUCT_CAT_TAXMODEL_ID'] = $PARAM_CAT_PRODUCT_CAT_TAXMODEL_ID[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_CAT_PRODUCT_AMOUNT_COST'] = $PARAM_CAT_PRODUCT_AMOUNT_COST[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_CAT_PRODUCT_AMOUNT_PRICE'] = $PARAM_CAT_PRODUCT_AMOUNT_PRICE[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_CAT_PRODUCT_AMOUNT_VAT'] = $PARAM_CAT_PRODUCT_AMOUNT_VAT[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_CAT_PRODUCT_AMOUNT_POINT'] = $PARAM_CAT_PRODUCT_AMOUNT_POINT[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_CAT_PRODUCT_ALLOW_DISCOUNT'] = $PARAM_CAT_PRODUCT_ALLOW_DISCOUNT[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_CAT_PRODUCT_ALLOW_SENIOR'] = $PARAM_CAT_PRODUCT_ALLOW_SENIOR[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_CAT_PRODUCT_IS_BASICCOM'] = $PARAM_CAT_PRODUCT_IS_BASICCOM[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_CAT_PRODUCT_IS_SERVICE'] = $PARAM_CAT_PRODUCT_IS_SERVICE[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_CAT_PRODUCT_REORDER_POINT'] = $PARAM_CAT_PRODUCT_REORDER_POINT[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_CAT_PRODUCT_REORDER_QUANTITY'] = $PARAM_CAT_PRODUCT_REORDER_QUANTITY[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_CAT_PRODUCT_CAT_PRODUCT_ID'] = $PARAM_CAT_PRODUCT_CAT_PRODUCT_ID[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_CAT_PRODUCT_PER_PIECE_QTY'] = $PARAM_CAT_PRODUCT_PER_PIECE_QTY[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_CAT_PRODUCT_CAT_SUPPLIER_ID'] = $PARAM_CAT_PRODUCT_CAT_SUPPLIER_ID[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_CAT_PRODUCT_IS_INVENTORY_ONLY'] = $PARAM_CAT_PRODUCT_IS_INVENTORY_ONLY[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_CAT_PRODUCT_IS_SERIALIZED'] = $PARAM_CAT_PRODUCT_IS_SERIALIZED[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_CAT_PRODUCT_STATUS'] = $PARAM_CAT_PRODUCT_STATUS[$ARRAY_CTR];
							
					$DATA_SET = PHPCUS_CATEGORY_PRODUCT::CUS_SAVE_MA
					(
							$SAVE_MAP_ARRAY,
							true,
							$PDO_HANDLE
					);
			
					
					if
					(
					    isset($PARAM_CAT_PRODUCT_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/pub/Category/product/',
					        $DATA_SET[0]['INSERTED_ID'],
					        $PARAM_CAT_PRODUCT_ID_FILE['name'][$ARRAY_CTR]
					    );			
					    
					    PHPRC_UPLOAD::REF_UPLOAD_TRANSACTION_MOVEFILE
					    (
					        $PARAM_CAT_PRODUCT_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');
	}

?>