<?php

// Perform Login
$CURRENT_MODULE_KEY = 'record_pos_sale_list';
$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_POSSALE_REC_CLIENT_ID',
            'PARAM_REC_POSSALE_REC_LOYALTY_ID',
            'PARAM_REC_POSSALE_CODE',
            'PARAM_REC_POSSALE_DATE_DUE',
            'PARAM_REC_POSSALE_REMARKS',
            'PARAM_REC_POSSALE_ACC_ACCOUNT_ID',
            'PARAM_REC_POSSALE_DATE_TRANSACT',
            'PARAM_REC_POSSALE_STATUS',
            
            'PARAM_REC_POSSALE_LIST_CAT_PRODUCT_ID',
            'PARAM_REC_POSSALE_LIST_CAT_UOM_ID',
            'PARAM_REC_POSSALE_LIST_AMOUNT_PRICE',
            'PARAM_REC_POSSALE_LIST_AMOUNT_VAT',
            'PARAM_REC_POSSALE_LIST_QUANTITY',
            'PARAM_REC_POSSALE_LIST_QUANTITY_PCS',
            'PARAM_REC_POSSALE_LIST_CAT_DISCOUNT_ID',
            'PARAM_REC_POSSALE_LIST_TOPAY_SUBTOTAL',
            'PARAM_REC_POSSALE_LIST_TOPAY_DISCOUNT',
            'PARAM_REC_POSSALE_LIST_TOPAY_VATABLE',
            'PARAM_REC_POSSALE_LIST_TOPAY_VATEXEMPT',
            'PARAM_REC_POSSALE_LIST_TOPAY_NONVAT',
            'PARAM_REC_POSSALE_LIST_TOPAY_ZERORATED',
            'PARAM_REC_POSSALE_LIST_TOPAY_VATOTHER',
            'PARAM_REC_POSSALE_LIST_TOPAY_VAT',
            'PARAM_REC_POSSALE_LIST_TOPAY_VAT_LESS',
            'PARAM_REC_POSSALE_LIST_POINTS',
            
            'PARAM_REC_POSPAY_CAT_PAYTYPE_ID',
            'PARAM_REC_POSPAY_AMOUNT_PAID',
            'PARAM_REC_POSPAY_CHANGE',
            'PARAM_REC_POSPAY_NUMBER',
            'PARAM_REC_POSPAY_PAYEE',
            'PARAM_REC_POSPAY_CAT_BANK_ID',
            'PARAM_REC_POSPAY_CAT_CARDTYPE_ID',
            'PARAM_REC_POSPAY_REC_LOYALTY_ID',
            'PARAM_REC_POSPAY_REC_VOUCHER_ID',
            'PARAM_REC_POSPAY_CHECK_DATE',
            'PARAM_REC_POSPAY_REMARKS',
            'PARAM_REC_POSPAY_ACC_ACCOUNT_ID',
            'PARAM_REC_POSPAY_DATE_PROCESSED',
            'PARAM_REC_POSPAY_IS_INITIAL_PAY'
            // , 'PARAM_REC_POSSALE_LIST_ID_FILE'
        ], True
        )
    )
{
    Try
    {
        
       
        
        // Read params
        // ------------------------------------------------------------------
        $PARAM_REC_POSSALE_REC_CLIENT_ID = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_REC_POSSALE_REC_CLIENT_ID', true);
        $PARAM_REC_POSSALE_REC_LOYALTY_ID = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_REC_POSSALE_REC_LOYALTY_ID', true);
        $PARAM_REC_POSSALE_CODE = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_REC_POSSALE_CODE', true);
        $PARAM_REC_POSSALE_DATE_DUE = PHPCUS_PARAM::CUS_READ_DATETIME('PARAM_REC_POSSALE_DATE_DUE', true);
        $PARAM_REC_POSSALE_REMARKS = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_REC_POSSALE_REMARKS', true);
        $PARAM_REC_POSSALE_ACC_ACCOUNT_ID = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_REC_POSSALE_ACC_ACCOUNT_ID', true);
        $PARAM_REC_POSSALE_DATE_TRANSACT = PHPCUS_PARAM::CUS_READ_DATETIME('PARAM_REC_POSSALE_DATE_TRANSACT', true);
        $PARAM_REC_POSSALE_STATUS = PHPCUS_PARAM::CUS_READ_INT('PARAM_REC_POSSALE_STATUS', true);   
        $PARAM_REC_POSSALE_ID = -1;
        $PARAM_REC_POSSALE_REC_POSDEVICE_ID = PHPCUS_Account_Login_Session::$SESSION_AUTHENTICATED_REC_POSDEVICE_ID;
        
        $PARAM_REC_POSSALE_LIST_CAT_PRODUCT_ID = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_REC_POSSALE_LIST_CAT_PRODUCT_ID', true);
        $PARAM_REC_POSSALE_LIST_CAT_UOM_ID = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_REC_POSSALE_LIST_CAT_UOM_ID', true);
        $PARAM_REC_POSSALE_LIST_AMOUNT_PRICE = PHPCUS_PARAM::CUS_READ_DECIMAL('PARAM_REC_POSSALE_LIST_AMOUNT_PRICE', true);
        $PARAM_REC_POSSALE_LIST_AMOUNT_VAT = PHPCUS_PARAM::CUS_READ_DECIMAL('PARAM_REC_POSSALE_LIST_AMOUNT_VAT', true);
        $PARAM_REC_POSSALE_LIST_QUANTITY = PHPCUS_PARAM::CUS_READ_DECIMAL('PARAM_REC_POSSALE_LIST_QUANTITY', true);
        $PARAM_REC_POSSALE_LIST_QUANTITY_PCS = PHPCUS_PARAM::CUS_READ_DECIMAL('PARAM_REC_POSSALE_LIST_QUANTITY_PCS', true);
        $PARAM_REC_POSSALE_LIST_CAT_DISCOUNT_ID = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_REC_POSSALE_LIST_CAT_DISCOUNT_ID', true);
        $PARAM_REC_POSSALE_LIST_TOPAY_SUBTOTAL = PHPCUS_PARAM::CUS_READ_DECIMAL('PARAM_REC_POSSALE_LIST_TOPAY_SUBTOTAL', true);
        $PARAM_REC_POSSALE_LIST_TOPAY_DISCOUNT = PHPCUS_PARAM::CUS_READ_DECIMAL('PARAM_REC_POSSALE_LIST_TOPAY_DISCOUNT', true);
        $PARAM_REC_POSSALE_LIST_TOPAY_VATABLE = PHPCUS_PARAM::CUS_READ_DECIMAL('PARAM_REC_POSSALE_LIST_TOPAY_VATABLE', true);
        $PARAM_REC_POSSALE_LIST_TOPAY_VATEXEMPT = PHPCUS_PARAM::CUS_READ_DECIMAL('PARAM_REC_POSSALE_LIST_TOPAY_VATEXEMPT', true);
        $PARAM_REC_POSSALE_LIST_TOPAY_NONVAT = PHPCUS_PARAM::CUS_READ_DECIMAL('PARAM_REC_POSSALE_LIST_TOPAY_NONVAT', true);
        $PARAM_REC_POSSALE_LIST_TOPAY_ZERORATED = PHPCUS_PARAM::CUS_READ_DECIMAL('PARAM_REC_POSSALE_LIST_TOPAY_ZERORATED', true);
        $PARAM_REC_POSSALE_LIST_TOPAY_VATOTHER = PHPCUS_PARAM::CUS_READ_DECIMAL('PARAM_REC_POSSALE_LIST_TOPAY_VATOTHER', true);
        $PARAM_REC_POSSALE_LIST_TOPAY_VAT = PHPCUS_PARAM::CUS_READ_DECIMAL('PARAM_REC_POSSALE_LIST_TOPAY_VAT', true);
        $PARAM_REC_POSSALE_LIST_TOPAY_VAT_LESS = PHPCUS_PARAM::CUS_READ_DECIMAL('PARAM_REC_POSSALE_LIST_TOPAY_VAT_LESS', true);
        $PARAM_REC_POSSALE_LIST_POINTS = PHPCUS_PARAM::CUS_READ_DECIMAL('PARAM_REC_POSSALE_LIST_POINTS', true);
        $PARAM_REC_POSSALE_LIST_STATUS = 2;
        
        $PARAM_REC_POSPAY_CAT_PAYTYPE_ID = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_REC_POSPAY_CAT_PAYTYPE_ID', true);
        $PARAM_REC_POSPAY_AMOUNT_PAID = PHPCUS_PARAM::CUS_READ_DECIMAL('PARAM_REC_POSPAY_AMOUNT_PAID', true);
        $PARAM_REC_POSPAY_CHANGE = PHPCUS_PARAM::CUS_READ_DECIMAL('PARAM_REC_POSPAY_CHANGE', true);
        $PARAM_REC_POSPAY_NUMBER = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_REC_POSPAY_NUMBER', true);
        $PARAM_REC_POSPAY_PAYEE = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_REC_POSPAY_PAYEE', true);
        $PARAM_REC_POSPAY_CAT_BANK_ID = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_REC_POSPAY_CAT_BANK_ID', true);
        $PARAM_REC_POSPAY_CAT_CARDTYPE_ID = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_REC_POSPAY_CAT_CARDTYPE_ID', true);
        $PARAM_REC_POSPAY_REC_LOYALTY_ID = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_REC_POSPAY_REC_LOYALTY_ID', true);
        $PARAM_REC_POSPAY_REC_VOUCHER_ID = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_REC_POSPAY_REC_VOUCHER_ID', true);
        $PARAM_REC_POSPAY_CHECK_DATE = PHPCUS_PARAM::CUS_READ_DATETIME('PARAM_REC_POSPAY_CHECK_DATE', true);
        $PARAM_REC_POSPAY_REMARKS = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_REC_POSPAY_REMARKS', true);
        $PARAM_REC_POSPAY_ACC_ACCOUNT_ID = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_REC_POSPAY_ACC_ACCOUNT_ID', true);
        $PARAM_REC_POSPAY_DATE_PROCESSED = PHPCUS_PARAM::CUS_READ_DATETIME('PARAM_REC_POSPAY_DATE_PROCESSED', true);
        $PARAM_REC_POSPAY_IS_INITIAL_PAY = PHPCUS_PARAM::CUS_READ_INT('PARAM_REC_POSPAY_IS_INITIAL_PAY', true);
        $PARAM_REC_POSPAY_STATUS = 2;
        
        //$PARAM_REC_POSSALE_LIST_ID_FILE = PHPCUS_PARAM::CUS_READ_FILES('PARAM_REC_POSSALE_LIST_ID_FILE');
        $PARAM_ARRAY_COUNT = count($PARAM_REC_POSSALE_REC_CLIENT_ID);
        
        
        // 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);
        }
        
        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. [' . $PARAM_REQUEST_UNIQUE_VALUE . ']');
            }
        }
        
        
        // Validate if array has same size
        // ------------------------------------------------------------------
        PHPCUS_PARAM::CUS_ARRAY_SIZE_MISMATCH
        (
            [
                $PARAM_REC_POSSALE_REC_CLIENT_ID,
                $PARAM_REC_POSSALE_REC_LOYALTY_ID,
                $PARAM_REC_POSSALE_CODE,
                $PARAM_REC_POSSALE_DATE_DUE,
                $PARAM_REC_POSSALE_REMARKS,
                $PARAM_REC_POSSALE_ACC_ACCOUNT_ID,
                $PARAM_REC_POSSALE_DATE_TRANSACT,
                $PARAM_REC_POSSALE_STATUS,
                
                $PARAM_REC_POSSALE_LIST_CAT_PRODUCT_ID,
                $PARAM_REC_POSSALE_LIST_CAT_UOM_ID,
                $PARAM_REC_POSSALE_LIST_AMOUNT_PRICE,
                $PARAM_REC_POSSALE_LIST_AMOUNT_VAT,
                $PARAM_REC_POSSALE_LIST_QUANTITY,
                $PARAM_REC_POSSALE_LIST_QUANTITY_PCS,
                $PARAM_REC_POSSALE_LIST_CAT_DISCOUNT_ID,
                $PARAM_REC_POSSALE_LIST_TOPAY_SUBTOTAL,
                $PARAM_REC_POSSALE_LIST_TOPAY_DISCOUNT,
                $PARAM_REC_POSSALE_LIST_TOPAY_VATABLE,
                $PARAM_REC_POSSALE_LIST_TOPAY_VATEXEMPT,
                $PARAM_REC_POSSALE_LIST_TOPAY_NONVAT,
                $PARAM_REC_POSSALE_LIST_TOPAY_ZERORATED,
                $PARAM_REC_POSSALE_LIST_TOPAY_VATOTHER,
                $PARAM_REC_POSSALE_LIST_TOPAY_VAT,
                $PARAM_REC_POSSALE_LIST_TOPAY_VAT_LESS,
                $PARAM_REC_POSSALE_LIST_POINTS,
                                
                $PARAM_REC_POSPAY_CAT_PAYTYPE_ID,
                $PARAM_REC_POSPAY_AMOUNT_PAID,
                $PARAM_REC_POSPAY_CHANGE,
                $PARAM_REC_POSPAY_NUMBER,
                $PARAM_REC_POSPAY_PAYEE,
                $PARAM_REC_POSPAY_CAT_BANK_ID,
                $PARAM_REC_POSPAY_CAT_CARDTYPE_ID,
                $PARAM_REC_POSPAY_REC_LOYALTY_ID,
                $PARAM_REC_POSPAY_REC_VOUCHER_ID,
                $PARAM_REC_POSPAY_CHECK_DATE,
                $PARAM_REC_POSPAY_REMARKS,
                $PARAM_REC_POSPAY_ACC_ACCOUNT_ID,
                $PARAM_REC_POSPAY_DATE_PROCESSED,
                $PARAM_REC_POSPAY_IS_INITIAL_PAY            
                // ,$PARAM_REC_POSSALE_LIST_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 Main Transaction Record
                if($ARRAY_CTR == 0)
                {
                    $SAVE_MAP_ARRAY = PHPCUS_RECORD_POS_SALE::CUS_PREPARE_MAPPED_ARRAY_FOR_SAVE();
                    
                    $SAVE_MAP_ARRAY['PARAM_REC_POSSALE_ID'] = -1;
                    $SAVE_MAP_ARRAY['PARAM_REC_POSSALE_REC_POSDEVICE_ID'] = $PARAM_REC_POSSALE_REC_POSDEVICE_ID;
                    $SAVE_MAP_ARRAY['PARAM_REC_POSSALE_REC_CLIENT_ID'] = $PARAM_REC_POSSALE_REC_CLIENT_ID[0];
                    $SAVE_MAP_ARRAY['PARAM_REC_POSSALE_REC_LOYALTY_ID'] = $PARAM_REC_POSSALE_REC_LOYALTY_ID[0];
                    $SAVE_MAP_ARRAY['PARAM_REC_POSSALE_CODE'] = $PARAM_REC_POSSALE_CODE[0];
                    $SAVE_MAP_ARRAY['PARAM_REC_POSSALE_DATE_DUE'] = $PARAM_REC_POSSALE_DATE_DUE[0];
                    $SAVE_MAP_ARRAY['PARAM_REC_POSSALE_REMARKS'] = $PARAM_REC_POSSALE_REMARKS[0];
                    $SAVE_MAP_ARRAY['PARAM_REC_POSSALE_ACC_ACCOUNT_ID'] = $PARAM_REC_POSSALE_ACC_ACCOUNT_ID[0];
                    $SAVE_MAP_ARRAY['PARAM_REC_POSSALE_ACC_ACCOUNT_SERVER_ID'] = PHPRC_SQL_FLAGS::FLAG_BIGINT_NULL;
                    $SAVE_MAP_ARRAY['PARAM_REC_POSSALE_DATE_TRANSACT'] = $PARAM_REC_POSSALE_DATE_TRANSACT[0];
                    $SAVE_MAP_ARRAY['PARAM_REC_POSSALE_STATUS'] = $PARAM_REC_POSSALE_STATUS[0];
                    
                    $DATA_SET = PHPCUS_RECORD_POS_SALE::CUS_SAVE_MA
                    (
                        $SAVE_MAP_ARRAY,
                        true,
                        $PDO_HANDLE
                    );
                    
                    if(empty($DATA_SET) || empty($DATA_SET[0]) || isset($DATA_SET[0]['INSERTED_ID']) == false )
                    {
                        throw new Exception('Missing expecting output');
                    }
                    else 
                    {
                        $PARAM_REC_POSSALE_ID = PHPVC_NUM::VAR_LONGVAL($DATA_SET[0]['INSERTED_ID']);
                    }
                }
               
                
                // ~~~~~
                
                
                // Save Items
                if($PARAM_REC_POSSALE_LIST_CAT_PRODUCT_ID[$ARRAY_CTR] > 0)
                {
                    $SAVE_MAP_ARRAY = PHPCUS_RECORD_POS_SALE_LIST::CUS_PREPARE_MAPPED_ARRAY_FOR_SAVE();
                    
                    $SAVE_MAP_ARRAY['PARAM_REC_POSSALE_LIST_ID'] = -1;
                    $SAVE_MAP_ARRAY['PARAM_REC_POSSALE_LIST_REC_POSSALE_ID'] = $PARAM_REC_POSSALE_ID;
                    $SAVE_MAP_ARRAY['PARAM_REC_POSSALE_LIST_CAT_PRODUCT_ID'] = $PARAM_REC_POSSALE_LIST_CAT_PRODUCT_ID[$ARRAY_CTR];
                    $SAVE_MAP_ARRAY['PARAM_REC_POSSALE_LIST_CAT_UOM_ID'] = $PARAM_REC_POSSALE_LIST_CAT_UOM_ID[$ARRAY_CTR];
                    $SAVE_MAP_ARRAY['PARAM_REC_POSSALE_LIST_AMOUNT_PRICE'] = $PARAM_REC_POSSALE_LIST_AMOUNT_PRICE[$ARRAY_CTR];
                    $SAVE_MAP_ARRAY['PARAM_REC_POSSALE_LIST_AMOUNT_VAT'] = $PARAM_REC_POSSALE_LIST_AMOUNT_VAT[$ARRAY_CTR];
                    $SAVE_MAP_ARRAY['PARAM_REC_POSSALE_LIST_QUANTITY'] = $PARAM_REC_POSSALE_LIST_QUANTITY[$ARRAY_CTR];
                    $SAVE_MAP_ARRAY['PARAM_REC_POSSALE_LIST_QUANTITY_PCS'] = $PARAM_REC_POSSALE_LIST_QUANTITY_PCS[$ARRAY_CTR];
                    $SAVE_MAP_ARRAY['PARAM_REC_POSSALE_LIST_CAT_DISCOUNT_ID'] = $PARAM_REC_POSSALE_LIST_CAT_DISCOUNT_ID[$ARRAY_CTR];
                    $SAVE_MAP_ARRAY['PARAM_REC_POSSALE_LIST_TOPAY_SUBTOTAL'] = $PARAM_REC_POSSALE_LIST_TOPAY_SUBTOTAL[$ARRAY_CTR];
                    $SAVE_MAP_ARRAY['PARAM_REC_POSSALE_LIST_TOPAY_DISCOUNT'] = $PARAM_REC_POSSALE_LIST_TOPAY_DISCOUNT[$ARRAY_CTR];
                    $SAVE_MAP_ARRAY['PARAM_REC_POSSALE_LIST_TOPAY_VATABLE'] = $PARAM_REC_POSSALE_LIST_TOPAY_VATABLE[$ARRAY_CTR];
                    $SAVE_MAP_ARRAY['PARAM_REC_POSSALE_LIST_TOPAY_VATEXEMPT'] = $PARAM_REC_POSSALE_LIST_TOPAY_VATEXEMPT[$ARRAY_CTR];
                    $SAVE_MAP_ARRAY['PARAM_REC_POSSALE_LIST_TOPAY_NONVAT'] = $PARAM_REC_POSSALE_LIST_TOPAY_NONVAT[$ARRAY_CTR];
                    $SAVE_MAP_ARRAY['PARAM_REC_POSSALE_LIST_TOPAY_ZERORATED'] = $PARAM_REC_POSSALE_LIST_TOPAY_ZERORATED[$ARRAY_CTR];
                    $SAVE_MAP_ARRAY['PARAM_REC_POSSALE_LIST_TOPAY_VATOTHER'] = $PARAM_REC_POSSALE_LIST_TOPAY_VATOTHER[$ARRAY_CTR];
                    $SAVE_MAP_ARRAY['PARAM_REC_POSSALE_LIST_TOPAY_VAT'] = $PARAM_REC_POSSALE_LIST_TOPAY_VAT[$ARRAY_CTR];
                    $SAVE_MAP_ARRAY['PARAM_REC_POSSALE_LIST_TOPAY_VAT_LESS'] = $PARAM_REC_POSSALE_LIST_TOPAY_VAT_LESS[$ARRAY_CTR];
                    $SAVE_MAP_ARRAY['PARAM_REC_POSSALE_LIST_POINTS'] = $PARAM_REC_POSSALE_LIST_POINTS[$ARRAY_CTR];
                    $SAVE_MAP_ARRAY['PARAM_REC_POSSALE_LIST_STATUS'] = $PARAM_REC_POSSALE_LIST_STATUS;
                    
                    $DATA_SET = PHPCUS_RECORD_POS_SALE_LIST::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_sale_list');
                 PHPRC_FSO_DIRECTORY::REF_CREATE(PHPSET::$ROOT . '/upload/priv/Record/pos_sale_list/' .  floor($DATA_SET[0]['INSERTED_ID'] / 5000));
                 
                 PHPRC_UPLOAD::REF_UPLOAD_TRANSACTION_MOVEFILE
                 (
                 $PARAM_REC_POSSALE_LIST_ID_FILE['tmp_name'][$ARRAY_CTR],
                 PHPSET::$ROOT . '/upload/priv/Record/pos_sale_list/' .  floor($DATA_SET[0]['INSERTED_ID'] / 5000) . '/' . $DATA_SET[0]['INSERTED_ID'] . '.' .
                 PHPVC_STR::VAR_LCASE(PathInfo($PARAM_REC_POSSALE_LIST_ID_FILE['name'][$ARRAY_CTR], PATHINFO_EXTENSION))
                 );
                 }
                 */
                
                //$FULL_DATA_SET = PHPRC_PDO_MYSQL_ARRAY::REF_MERGE($FULL_DATA_SET, $DATA_SET);
                $ARRAY_CTR ++;
            }
                   
 
            $ARRAY_CTR = 0;
            while($ARRAY_CTR < $PARAM_ARRAY_COUNT)
            {
                // Save Payment
                if($PARAM_REC_POSPAY_CAT_PAYTYPE_ID[$ARRAY_CTR] > 0)
                {
                    $SAVE_MAP_ARRAY = PHPCUS_RECORD_POS_PAYMENT::CUS_PREPARE_MAPPED_ARRAY_FOR_SAVE();
                    
                    $SAVE_MAP_ARRAY['PARAM_REC_POSPAY_ID'] = -1;
                    $SAVE_MAP_ARRAY['PARAM_REC_POSPAY_REC_POSSALE_ID'] = $PARAM_REC_POSSALE_ID;
                    $SAVE_MAP_ARRAY['PARAM_REC_POSPAY_CAT_PAYTYPE_ID'] = $PARAM_REC_POSPAY_CAT_PAYTYPE_ID[$ARRAY_CTR];
                    $SAVE_MAP_ARRAY['PARAM_REC_POSPAY_AMOUNT_PAID'] = $PARAM_REC_POSPAY_AMOUNT_PAID[$ARRAY_CTR];
                    $SAVE_MAP_ARRAY['PARAM_REC_POSPAY_CHANGE'] = $PARAM_REC_POSPAY_CHANGE[$ARRAY_CTR];
                    $SAVE_MAP_ARRAY['PARAM_REC_POSPAY_NUMBER'] = $PARAM_REC_POSPAY_NUMBER[$ARRAY_CTR];
                    $SAVE_MAP_ARRAY['PARAM_REC_POSPAY_PAYEE'] = $PARAM_REC_POSPAY_PAYEE[$ARRAY_CTR];
                    $SAVE_MAP_ARRAY['PARAM_REC_POSPAY_CAT_BANK_ID'] = $PARAM_REC_POSPAY_CAT_BANK_ID[$ARRAY_CTR];
                    $SAVE_MAP_ARRAY['PARAM_REC_POSPAY_CAT_CARDTYPE_ID'] = $PARAM_REC_POSPAY_CAT_CARDTYPE_ID[$ARRAY_CTR];
                    $SAVE_MAP_ARRAY['PARAM_REC_POSPAY_REC_LOYALTY_ID'] = $PARAM_REC_POSPAY_REC_LOYALTY_ID[$ARRAY_CTR];
                    $SAVE_MAP_ARRAY['PARAM_REC_POSPAY_REC_VOUCHER_ID'] = $PARAM_REC_POSPAY_REC_VOUCHER_ID[$ARRAY_CTR];
                    $SAVE_MAP_ARRAY['PARAM_REC_POSPAY_CHECK_DATE'] = $PARAM_REC_POSPAY_CHECK_DATE[$ARRAY_CTR];
                    $SAVE_MAP_ARRAY['PARAM_REC_POSPAY_REMARKS'] = $PARAM_REC_POSPAY_REMARKS[$ARRAY_CTR];
                    $SAVE_MAP_ARRAY['PARAM_REC_POSPAY_ACC_ACCOUNT_ID'] = $PARAM_REC_POSPAY_ACC_ACCOUNT_ID[$ARRAY_CTR];
                    $SAVE_MAP_ARRAY['PARAM_REC_POSPAY_DATE_PROCESSED'] = $PARAM_REC_POSPAY_DATE_PROCESSED[$ARRAY_CTR];
                    $SAVE_MAP_ARRAY['PARAM_REC_POSPAY_IS_INITIAL_PAY'] = $PARAM_REC_POSPAY_IS_INITIAL_PAY[$ARRAY_CTR];
                    $SAVE_MAP_ARRAY['PARAM_REC_POSPAY_STATUS'] = $PARAM_REC_POSPAY_STATUS;
                    
                    $DATA_SET = PHPCUS_RECORD_POS_PAYMENT::CUS_SAVE_MA
                    (
                        $SAVE_MAP_ARRAY,
                        true,
                        $PDO_HANDLE
                     );
                }
                
                $ARRAY_CTR ++;
            }
     
            
            // Populate Map Array
            // ...........................................
            $GET_MAP_ARRAY = PHPCUS_RECORD_POS_SALE_LIST::CUS_PREPARE_MAPPED_ARRAY_FOR_GET();
            $GET_MAP_ARRAY['PARAM_REC_POSSALE_LIST_REC_POSSALE_ID'] = $PARAM_REC_POSSALE_ID;
              
            
            // Execute Stored Procedure
            // ...........................................
            $DATA_SET = PHPCUS_RECORD_POS_SALE_LIST::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();
        
  
        // 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);
            }
        }
        
        
        // 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');
}

?>