From 48fb78025295ddb0663890ee6184eb70fe4bf770 Mon Sep 17 00:00:00 2001 From: Mohamad Ihsan Date: Wed, 25 Jun 2025 19:52:47 +0700 Subject: [PATCH] add new service completing data v2 --- application/modules/pos/pos.controller.js | 55 ++++++++++++ application/modules/pos/pos.model.js | 103 ++++++++++++++++++++++ application/routes/pos.routes.js | 6 ++ 3 files changed, 164 insertions(+) diff --git a/application/modules/pos/pos.controller.js b/application/modules/pos/pos.controller.js index af0751c..2f23d7d 100644 --- a/application/modules/pos/pos.controller.js +++ b/application/modules/pos/pos.controller.js @@ -2754,6 +2754,60 @@ const completeDataSalesInternal = async (req, res) => { } }; +const completeDataSalesInternalV2 = async (req, res) => { + try { + let uniqueCode = helper.getUniqueCode(); + let result = {}; + + console.log(`${uniqueCode} [ COMPLETING DATA SALES INTERNAL V2 ]`); + + // GET PPN FROM GLOBAL PARAMETER + let ppn = await model.getPPNCardSales(); + if (!ppn) { + result = { + code: '400', + message: 'value pajak belum di set', + data: {}, + }; + winston.logger.warn( + `${uniqueCode} [ VALIDATE COMPLETING DATA SALES INTERNAL V2 ] : ${JSON.stringify( + result + )}` + ); + return res.status(200).json(result); + } + + ppn = parseFloat(ppn.c_value); + + //COMPLETING DATA SALES INTERNAL + await model.completingDataSalesNewCard(ppn); + await model.completingDataSalesOldCard(ppn); + await model.updateDataSalesIsComplete(); + + result = { + code: '00', + message: 'Successfully.', + data: {}, + }; + + return res.status(200).json(result); + } catch (error) { + // create log + winston.logger.error( + `[ RESPONSE COMPLETING DATA SALES INTERNAL V2 ]: 500 internal server error - backend server | ${error.message}` + ); + + return res.status(200).json({ + code: '500', + message: + process.env.NODE_ENV != 'production' + ? error.message + : '500 internal server error - backend server.', + data: {}, + }); + } +}; + const testAPI = async (req, res) => { try { let result = {}; @@ -2806,5 +2860,6 @@ module.exports = { confirmSales, getLastBalanceCTS, completeDataSalesInternal, + completeDataSalesInternalV2, testAPI, }; diff --git a/application/modules/pos/pos.model.js b/application/modules/pos/pos.model.js index 96b0785..d042852 100644 --- a/application/modules/pos/pos.model.js +++ b/application/modules/pos/pos.model.js @@ -243,6 +243,106 @@ const completingDataSalesInternal = async (ppn) => { return result; }; +const completingDataSalesNewCard = async (ppn) => { + return await knex.raw(` + UPDATE trx.t_d_sales_internal AS trx + SET + c_material_number_sap = subquery.c_material_number_sap, + i_card_theme = subquery.i_card_theme, + i_tax_amount = subquery.tax, + is_complete = subquery.is_complete + FROM ( + SELECT DISTINCT ON (trx.t_d_sales_internal.c_card_number, t_m_card_theme.c_card_type_code, t_m_card_theme.c_card_digit_key, t_m_card_theme.c_variant_code) + trx.t_d_sales_internal.c_card_number, + t_m_card_theme.i_card_theme, + t_m_card_theme.c_material_number_sap, + trx.t_d_sales_internal.i_card_price AS card_selling_price, + ROUND(trx.t_d_sales_internal.i_card_price * 100 / (100 + ${ppn}::float)) AS card_price_before_tax, + ROUND((${ppn}::float / 100::float) * (ROUND(trx.t_d_sales_internal.i_card_price * 100 / (100 + ${ppn}::float)))) AS tax, + CASE + WHEN trx.t_d_sales_internal.c_category = 'S' THEN true + ELSE false + END is_complete + FROM trx.t_d_sales_internal + JOIN public.t_m_card_theme ON + SUBSTRING(trx.t_d_sales_internal.c_card_number, 1, 1) = t_m_card_theme.c_card_type_code + AND SUBSTRING(trx.t_d_sales_internal.c_card_number, 2, 2) = t_m_card_theme.c_card_digit_key + AND SUBSTRING(trx.t_d_sales_internal.c_card_number, 4, 1) = t_m_card_theme.c_variant_code + WHERE + trx.t_d_sales_internal.is_complete is not true + AND trx.t_d_sales_internal.i_card_price > 0 + AND trx.t_d_sales_internal.i_sales_invoice_internal is null + ORDER BY + trx.t_d_sales_internal.c_card_number, + t_m_card_theme.c_card_type_code, + t_m_card_theme.c_card_digit_key, + t_m_card_theme.c_variant_code, + t_m_card_theme.i_year DESC, + trx.t_d_sales_internal.d_create ASC + ) AS subquery + WHERE + trx.c_card_number = subquery.c_card_number + AND trx.i_sales_invoice_internal IS NULL + `); +}; + +const completingDataSalesOldCard = async (ppn) => { + return await knex.raw(` + UPDATE trx.t_d_sales_internal AS trx + SET + c_old_material_number_sap = subquery.c_material_number_sap, + i_old_card_theme = subquery.i_card_theme, + i_old_tax_amount = subquery.tax + FROM ( + SELECT DISTINCT ON (trx.t_d_sales_internal.c_old_card_number, t_m_card_theme.c_card_type_code, t_m_card_theme.c_card_digit_key, t_m_card_theme.c_variant_code) + trx.t_d_sales_internal.c_old_card_number, + t_m_card_theme.i_card_theme, + t_m_card_theme.c_material_number_sap, + trx.t_d_sales_internal.i_old_card_price AS card_selling_price, + ROUND(trx.t_d_sales_internal.i_old_card_price * 100 / (100 + ${ppn}::float)) AS card_price_before_tax, + ROUND((${ppn}::float / 100::float) * (ROUND(trx.t_d_sales_internal.i_old_card_price * 100 / (100 + ${ppn}::float)))) AS tax, + CASE + WHEN trx.t_d_sales_internal.c_category = 'S' THEN true + WHEN trx.t_d_sales_internal.c_category IN ('N', 'W') THEN true + ELSE false + END is_complete + FROM trx.t_d_sales_internal + JOIN public.t_m_card_theme ON + SUBSTRING(trx.t_d_sales_internal.c_old_card_number, 1, 1) = t_m_card_theme.c_card_type_code + AND SUBSTRING(trx.t_d_sales_internal.c_old_card_number, 2, 2) = t_m_card_theme.c_card_digit_key + AND SUBSTRING(trx.t_d_sales_internal.c_old_card_number, 4, 1) = t_m_card_theme.c_variant_code + WHERE + trx.t_d_sales_internal.is_complete is not true + AND trx.t_d_sales_internal.i_old_card_price > 0 + AND trx.t_d_sales_internal.c_category IN ('N', 'W') + AND trx.t_d_sales_internal.i_sales_invoice_internal is null + ORDER BY + trx.t_d_sales_internal.c_old_card_number, + t_m_card_theme.c_card_type_code, + t_m_card_theme.c_card_digit_key, + t_m_card_theme.c_variant_code, + t_m_card_theme.i_year DESC, + trx.t_d_sales_internal.d_create ASC + ) AS subquery + WHERE + trx.c_old_card_number = subquery.c_old_card_number + AND trx.i_sales_invoice_internal IS NULL + `); +}; + +const updateDataSalesIsComplete = async () => { + return await knex.raw(` + UPDATE trx.t_d_sales_internal SET is_complete = true + WHERE + is_complete is not true + AND c_material_number_sap is not null + AND i_tax_amount is not null + AND c_old_material_number_sap is not null + AND i_old_tax_amount is not null + AND c_category IN ('N', 'W') + `); +}; + module.exports = { getCardBlacklist, getCardReplacement, @@ -259,4 +359,7 @@ module.exports = { updateReplacementByCardNumber, getPPNCardSales, completingDataSalesInternal, + completingDataSalesNewCard, + completingDataSalesOldCard, + updateDataSalesIsComplete, }; \ No newline at end of file diff --git a/application/routes/pos.routes.js b/application/routes/pos.routes.js index b8e1659..ee610a3 100644 --- a/application/routes/pos.routes.js +++ b/application/routes/pos.routes.js @@ -88,6 +88,12 @@ router.post( controller.completeDataSalesInternal ); +// API Scheduler Lengkapi Data Sales Internal V2 +router.post( + "/pos/sales/complete-data-v2", + controller.completeDataSalesInternalV2 +); + // API Test router.post( "/pos/test",