mirror of
https://github.com/mohamadihsan/topuponline.git
synced 2026-12-05 10:46:28 +00:00
enhancement generate and posting invoice online topup
This commit is contained in:
@@ -85,7 +85,7 @@ const apiGenerateInvoice = async (req, res) => {
|
||||
|
||||
result = {
|
||||
code: '200',
|
||||
message: 'Succes posting',
|
||||
message: 'Succes generate invoice online topup',
|
||||
data: invoice,
|
||||
};
|
||||
|
||||
|
||||
@@ -26,26 +26,30 @@ const insertPostingEt = async (invoice, trx) => {
|
||||
reference: invoice.c_invoice_no,
|
||||
amount: invoice.amount,
|
||||
ba: invoice.ba,
|
||||
indeks: (index+1),
|
||||
indeks: (index + 1),
|
||||
totalidx: type.length,
|
||||
doc_header_text: invoice.c_invoice_no,
|
||||
pos_status : 3
|
||||
pos_status: 3
|
||||
}
|
||||
|
||||
if(item == 'H'){
|
||||
data = {...data, ...{
|
||||
if (item == 'H') {
|
||||
data = {
|
||||
...data, ...{
|
||||
text: `Posting Invoice CMS ${invoice.c_invoice_no}`,
|
||||
debit_kredit: item,
|
||||
account: invoice.c_account_topup,
|
||||
customer: null
|
||||
}}
|
||||
}else{
|
||||
data = {...data, ...{
|
||||
}
|
||||
}
|
||||
} else {
|
||||
data = {
|
||||
...data, ...{
|
||||
text: `Posting Invoice CMS ${invoice.c_invoice_no}`,
|
||||
debit_kredit: item,
|
||||
account: null,
|
||||
customer: invoice.c_account_code_topup
|
||||
}}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
et.push(data)
|
||||
@@ -58,44 +62,117 @@ const insertPostingEt = async (invoice, trx) => {
|
||||
|
||||
}
|
||||
|
||||
const getPostingEt = async(trx) => {
|
||||
const getPostingEt = async (trx) => {
|
||||
|
||||
const result = await trx('public.posting_et as pe')
|
||||
.leftJoin('public.posting_et as pe2', 'pe2.reference', 'pe.reference')
|
||||
.select([
|
||||
'pe.doc_header_text',
|
||||
'pe.ba',
|
||||
'pe.doc_date',
|
||||
'pe.posting_date',
|
||||
'pe.fiscal_year',
|
||||
trx.raw(`TO_CHAR(pe.doc_date::DATE, 'MM') AS fis_period`),
|
||||
'pe.doc_type',
|
||||
'pe.reference',
|
||||
'pe.tcode',
|
||||
'pe.amount',
|
||||
trx.raw(`'IDR' AS currency`),
|
||||
const result = await trx.raw(`
|
||||
WITH params AS (
|
||||
|
||||
trx.raw(`MAX(pe2.customer) FILTER (WHERE pe2.debit_kredit = 'S') AS customer`),
|
||||
trx.raw(`MAX(pe2.account) FILTER (WHERE pe2.debit_kredit = 'H') AS account`),
|
||||
trx.raw(`MAX(pe2.text) FILTER (WHERE pe2.debit_kredit = 'S') AS text1`),
|
||||
trx.raw(`MAX(pe2.text) FILTER (WHERE pe2.debit_kredit = 'H') AS text2`)
|
||||
])
|
||||
.where('pe.tcode', 'FB70')
|
||||
.whereIn('pe.pos_status', ['3'])
|
||||
.groupBy([
|
||||
'pe.doc_header_text',
|
||||
'pe.ba',
|
||||
'pe.doc_date',
|
||||
'pe.posting_date',
|
||||
'pe.fiscal_year',
|
||||
trx.raw(`TO_CHAR(pe.doc_date::DATE, 'MM')`),
|
||||
'pe.doc_type',
|
||||
'pe.reference',
|
||||
'pe.tcode',
|
||||
'pe.amount'
|
||||
]);
|
||||
SELECT
|
||||
EXTRACT(YEAR FROM CURRENT_DATE)::TEXT AS curr_year,
|
||||
EXTRACT(YEAR FROM CURRENT_DATE - INTERVAL '1 year')::TEXT AS prev_year,
|
||||
TO_CHAR(CURRENT_DATE, 'MM') AS curr_month
|
||||
|
||||
return result
|
||||
),
|
||||
|
||||
base AS (
|
||||
|
||||
SELECT
|
||||
pe.doc_header_text,
|
||||
pe.ba,
|
||||
pe.doc_date,
|
||||
pe.posting_date,
|
||||
pe.fiscal_year,
|
||||
TO_CHAR(pe.doc_date::DATE, 'MM') AS fis_period,
|
||||
pe.doc_type,
|
||||
pe.reference,
|
||||
pe.tcode,
|
||||
pe.amount,
|
||||
pe.customer,
|
||||
pe.account,
|
||||
pe.text,
|
||||
pe.debit_kredit
|
||||
|
||||
FROM public.posting_et pe
|
||||
CROSS JOIN params p
|
||||
|
||||
WHERE
|
||||
pe.tcode = 'FB70'
|
||||
AND pe.pos_status = '3'
|
||||
|
||||
-- Prefix search still index friendly
|
||||
AND pe.doc_header_text LIKE 'INV%'
|
||||
|
||||
AND (
|
||||
|
||||
-- Current year all months
|
||||
pe.fiscal_year = p.curr_year
|
||||
|
||||
OR (
|
||||
|
||||
-- Previous year December only
|
||||
-- only when current month is January
|
||||
p.curr_month = '01'
|
||||
|
||||
AND pe.fiscal_year = p.prev_year
|
||||
|
||||
AND TO_CHAR(pe.doc_date::DATE, 'MM') = '12'
|
||||
|
||||
)
|
||||
|
||||
)
|
||||
|
||||
)
|
||||
|
||||
SELECT
|
||||
b.doc_header_text,
|
||||
b.ba,
|
||||
b.doc_date,
|
||||
b.posting_date,
|
||||
b.fiscal_year,
|
||||
b.fis_period,
|
||||
b.doc_type,
|
||||
b.reference,
|
||||
b.tcode,
|
||||
b.amount,
|
||||
'IDR'::VARCHAR AS currency,
|
||||
|
||||
MAX(b.customer) FILTER (
|
||||
WHERE b.debit_kredit = 'S'
|
||||
) AS customer,
|
||||
|
||||
MAX(b.account) FILTER (
|
||||
WHERE b.debit_kredit = 'H'
|
||||
) AS account,
|
||||
|
||||
MAX(b.text) FILTER (
|
||||
WHERE b.debit_kredit = 'S'
|
||||
) AS text1,
|
||||
|
||||
MAX(b.text) FILTER (
|
||||
WHERE b.debit_kredit = 'H'
|
||||
) AS text2
|
||||
|
||||
FROM base b
|
||||
|
||||
GROUP BY
|
||||
b.doc_header_text,
|
||||
b.ba,
|
||||
b.doc_date,
|
||||
b.posting_date,
|
||||
b.fiscal_year,
|
||||
b.fis_period,
|
||||
b.doc_type,
|
||||
b.reference,
|
||||
b.tcode,
|
||||
b.amount
|
||||
|
||||
ORDER BY
|
||||
b.fiscal_year,
|
||||
b.doc_date,
|
||||
b.reference
|
||||
`);
|
||||
|
||||
return result.rows || result
|
||||
|
||||
}
|
||||
|
||||
@@ -103,7 +180,7 @@ const setReturnSAP = async (invoice, resSAP, trx, posting = false, c_invoice_no
|
||||
let t_invoice = {}
|
||||
let t_load_balance = {}
|
||||
let posting_et = {}
|
||||
if(resSAP.type == 'S'){
|
||||
if (resSAP.type == 'S') {
|
||||
// update invoice
|
||||
t_invoice = await trx('payment.t_d_invoice').update({
|
||||
c_return_sap: resSAP.data.E_DOCUMENTNUMBER
|
||||
@@ -119,26 +196,26 @@ const setReturnSAP = async (invoice, resSAP, trx, posting = false, c_invoice_no
|
||||
.whereRaw(`c_load_balance_no IN (SELECT c_load_balance_no FROM payment.t_d_payment_request_topup_online WHERE c_payment = '${c_invoice_no}')`)
|
||||
|
||||
posting_et = await trx('public.posting_et').update({
|
||||
pos_status : 0,
|
||||
status0 : resSAP.type,
|
||||
no_doksap : resSAP.data.E_DOCUMENTNUMBER ? resSAP.data.E_DOCUMENTNUMBER : null,
|
||||
status1 : resSAP.message ? resSAP.message : null
|
||||
}, ['file_name','status0']).where({
|
||||
pos_status: 0,
|
||||
status0: resSAP.type,
|
||||
no_doksap: resSAP.data.E_DOCUMENTNUMBER ? resSAP.data.E_DOCUMENTNUMBER : null,
|
||||
status1: resSAP.message ? resSAP.message : null
|
||||
}, ['file_name', 'status0']).where({
|
||||
reference: c_invoice_no
|
||||
})
|
||||
|
||||
if(posting) {
|
||||
if (posting) {
|
||||
await writePaynentHistory(t_invoice[0], "SUCCESSFULLY", 102, trx)
|
||||
} else {
|
||||
await writePaynentHistory(t_invoice[0], "SUCCESS TO RETRY SAP", 118, trx)
|
||||
}
|
||||
|
||||
}else {
|
||||
} else {
|
||||
|
||||
let text = resSAP.message;
|
||||
let result = text.toUpperCase();
|
||||
|
||||
if(result.includes("ALREADY")){
|
||||
if (result.includes("ALREADY")) {
|
||||
let result = text.toUpperCase().split('-');
|
||||
let doc_number = result[1].trim().split(" ")[0]
|
||||
// update invoice
|
||||
@@ -146,11 +223,11 @@ const setReturnSAP = async (invoice, resSAP, trx, posting = false, c_invoice_no
|
||||
let year = d.getFullYear();
|
||||
|
||||
posting_et = await trx('public.posting_et').update({
|
||||
pos_status : 0,
|
||||
status0 : 'S',
|
||||
no_doksap : doc_number,
|
||||
status1 : resSAP.message ? resSAP.message : null
|
||||
}, ['file_name','status0']).where({
|
||||
pos_status: 0,
|
||||
status0: 'S',
|
||||
no_doksap: doc_number,
|
||||
status1: resSAP.message ? resSAP.message : null
|
||||
}, ['file_name', 'status0']).where({
|
||||
reference: c_invoice_no
|
||||
})
|
||||
|
||||
@@ -168,20 +245,20 @@ const setReturnSAP = async (invoice, resSAP, trx, posting = false, c_invoice_no
|
||||
}, ['c_load_balance_no', 'c_return_sap'])
|
||||
.whereRaw(`c_load_balance_no IN (SELECT c_load_balance_no FROM payment.t_d_payment_request_topup_online WHERE c_payment = '${c_invoice_no}')`)
|
||||
|
||||
if(posting) {
|
||||
if (posting) {
|
||||
await writePaynentHistory(t_invoice[0], "SUCCESSFULLY", 102, trx)
|
||||
} else {
|
||||
await writePaynentHistory(t_invoice[0], "SUCCESS TO RETRY SAP", 118, trx)
|
||||
}
|
||||
|
||||
}else{
|
||||
} else {
|
||||
// update posting et
|
||||
posting_et = await trx('public.posting_et').update({
|
||||
pos_status : resSAP.type == 'S' ? 0 : 3,
|
||||
status0 : resSAP.type,
|
||||
no_doksap : resSAP.data.E_DOCUMENTNUMBER ? resSAP.data.E_DOCUMENTNUMBER : null,
|
||||
status1 : resSAP.message ? resSAP.message : null
|
||||
}, ['file_name','status0']).where({
|
||||
pos_status: resSAP.type == 'S' ? 0 : 3,
|
||||
status0: resSAP.type,
|
||||
no_doksap: resSAP.data.E_DOCUMENTNUMBER ? resSAP.data.E_DOCUMENTNUMBER : null,
|
||||
status1: resSAP.message ? resSAP.message : null
|
||||
}, ['file_name', 'status0']).where({
|
||||
reference: c_invoice_no
|
||||
})
|
||||
|
||||
@@ -191,10 +268,10 @@ const setReturnSAP = async (invoice, resSAP, trx, posting = false, c_invoice_no
|
||||
c_invoice_no: c_invoice_no
|
||||
});
|
||||
|
||||
if(posting) {
|
||||
await writePaynentHistory(t_invoice[0], `SUCCESS WITH SAP ERR: ${resSAP.message}`,102, trx)
|
||||
if (posting) {
|
||||
await writePaynentHistory(t_invoice[0], `SUCCESS WITH SAP ERR: ${resSAP.message}`, 102, trx)
|
||||
} else {
|
||||
await writePaynentHistory(t_invoice[0], `FAIL TO RETRY WITH SAP ERR: ${resSAP.message}`,118, trx)
|
||||
await writePaynentHistory(t_invoice[0], `FAIL TO RETRY WITH SAP ERR: ${resSAP.message}`, 118, trx)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -204,9 +281,9 @@ const setReturnSAP = async (invoice, resSAP, trx, posting = false, c_invoice_no
|
||||
// update posting et
|
||||
|
||||
return {
|
||||
invoice : t_invoice,
|
||||
load_balance : t_load_balance,
|
||||
posting_et : posting_et
|
||||
invoice: t_invoice,
|
||||
load_balance: t_load_balance,
|
||||
posting_et: posting_et
|
||||
}
|
||||
|
||||
}
|
||||
@@ -215,18 +292,18 @@ const writePaynentHistory = async (data, message, i_log_name, trx) => {
|
||||
console.log(data)
|
||||
const result = await trx('log.t_log_invoice').insert({
|
||||
// i_log_name : 102,
|
||||
i_log_name : i_log_name,
|
||||
d_log : trx.raw('NOW()'),
|
||||
c_invoice_no : data?.c_invoice_no || '',
|
||||
c_partner_name : data?.c_partner_name || '',
|
||||
j_new_data : JSON.stringify(data),
|
||||
i_log_name: i_log_name,
|
||||
d_log: trx.raw('NOW()'),
|
||||
c_invoice_no: data?.c_invoice_no || '',
|
||||
c_partner_name: data?.c_partner_name || '',
|
||||
j_new_data: JSON.stringify(data),
|
||||
d_payment_date: trx.raw('NOW()'),
|
||||
c_note : message,
|
||||
c_status : 'S',
|
||||
d_created : trx.raw('NOW()'),
|
||||
m_amount : data?.amount || 0,
|
||||
m_mdr_amount : 0,
|
||||
m_mdr_net : data?.amount || 0
|
||||
c_note: message,
|
||||
c_status: 'S',
|
||||
d_created: trx.raw('NOW()'),
|
||||
m_amount: data?.amount || 0,
|
||||
m_mdr_amount: 0,
|
||||
m_mdr_net: data?.amount || 0
|
||||
|
||||
}, ['i_log_name', 'c_note'])
|
||||
|
||||
@@ -234,7 +311,7 @@ const writePaynentHistory = async (data, message, i_log_name, trx) => {
|
||||
|
||||
}
|
||||
|
||||
const getParameterSetting = async(trx, code) => {
|
||||
const getParameterSetting = async (trx, code) => {
|
||||
|
||||
const result = await trx('public.t_m_global_parameter_sap AS tmgps').select([
|
||||
"c_value"
|
||||
@@ -247,7 +324,7 @@ const getParameterSetting = async(trx, code) => {
|
||||
|
||||
}
|
||||
|
||||
const getInvoice = async(code, trx) => {
|
||||
const getInvoice = async (code, trx) => {
|
||||
|
||||
const result = await trx('payment.t_d_invoice')
|
||||
.where('c_invoice_no', '=', code)
|
||||
|
||||
@@ -0,0 +1,143 @@
|
||||
-- =========================================================
|
||||
-- RECOMMENDED INDEXES FOR PRODUCTION
|
||||
-- =========================================================
|
||||
|
||||
-- Main filtering index
|
||||
CREATE INDEX IF NOT EXISTS idx_posting_et_main_filter
|
||||
ON public.posting_et (
|
||||
tcode,
|
||||
pos_status,
|
||||
fiscal_year
|
||||
);
|
||||
|
||||
-- Prefix LIKE 'INV%'
|
||||
CREATE INDEX IF NOT EXISTS idx_posting_et_doc_header
|
||||
ON public.posting_et (doc_header_text);
|
||||
|
||||
-- Join & aggregation optimization
|
||||
CREATE INDEX IF NOT EXISTS idx_posting_et_reference_debit
|
||||
ON public.posting_et (
|
||||
reference,
|
||||
debit_kredit
|
||||
);
|
||||
|
||||
-- Optional covering index for large tables
|
||||
CREATE INDEX IF NOT EXISTS idx_posting_et_covering
|
||||
ON public.posting_et (
|
||||
tcode,
|
||||
pos_status,
|
||||
fiscal_year,
|
||||
reference
|
||||
);
|
||||
|
||||
|
||||
|
||||
-- =========================================================
|
||||
-- PRODUCTION QUERY
|
||||
-- =========================================================
|
||||
|
||||
WITH params AS (
|
||||
|
||||
SELECT
|
||||
EXTRACT(YEAR FROM CURRENT_DATE)::TEXT AS curr_year,
|
||||
EXTRACT(YEAR FROM CURRENT_DATE - INTERVAL '1 year')::TEXT AS prev_year,
|
||||
TO_CHAR(CURRENT_DATE, 'MM') AS curr_month
|
||||
|
||||
),
|
||||
|
||||
base AS (
|
||||
|
||||
SELECT
|
||||
pe.doc_header_text,
|
||||
pe.ba,
|
||||
pe.doc_date,
|
||||
pe.posting_date,
|
||||
pe.fiscal_year,
|
||||
TO_CHAR(pe.doc_date::DATE, 'MM') AS fis_period,
|
||||
pe.doc_type,
|
||||
pe.reference,
|
||||
pe.tcode,
|
||||
pe.amount,
|
||||
pe.customer,
|
||||
pe.account,
|
||||
pe.text,
|
||||
pe.debit_kredit
|
||||
|
||||
FROM public.posting_et pe
|
||||
CROSS JOIN params p
|
||||
|
||||
WHERE
|
||||
pe.tcode = 'FB70'
|
||||
AND pe.pos_status = '3'
|
||||
|
||||
-- Prefix search still index friendly
|
||||
AND pe.doc_header_text LIKE 'INV%'
|
||||
|
||||
AND (
|
||||
|
||||
-- Current year all months
|
||||
pe.fiscal_year = p.curr_year
|
||||
|
||||
OR (
|
||||
|
||||
-- Previous year December only
|
||||
-- only when current month is January
|
||||
p.curr_month = '01'
|
||||
|
||||
AND pe.fiscal_year = p.prev_year
|
||||
|
||||
AND TO_CHAR(pe.doc_date::DATE, 'MM') = '12'
|
||||
|
||||
)
|
||||
|
||||
)
|
||||
|
||||
)
|
||||
|
||||
SELECT
|
||||
b.doc_header_text,
|
||||
b.ba,
|
||||
b.doc_date,
|
||||
b.posting_date,
|
||||
b.fiscal_year,
|
||||
b.fis_period,
|
||||
b.doc_type,
|
||||
b.reference,
|
||||
b.tcode,
|
||||
b.amount,
|
||||
'IDR'::VARCHAR AS currency,
|
||||
|
||||
MAX(b.customer) FILTER (
|
||||
WHERE b.debit_kredit = 'S'
|
||||
) AS customer,
|
||||
|
||||
MAX(b.account) FILTER (
|
||||
WHERE b.debit_kredit = 'H'
|
||||
) AS account,
|
||||
|
||||
MAX(b.text) FILTER (
|
||||
WHERE b.debit_kredit = 'S'
|
||||
) AS text1,
|
||||
|
||||
MAX(b.text) FILTER (
|
||||
WHERE b.debit_kredit = 'H'
|
||||
) AS text2
|
||||
|
||||
FROM base b
|
||||
|
||||
GROUP BY
|
||||
b.doc_header_text,
|
||||
b.ba,
|
||||
b.doc_date,
|
||||
b.posting_date,
|
||||
b.fiscal_year,
|
||||
b.fis_period,
|
||||
b.doc_type,
|
||||
b.reference,
|
||||
b.tcode,
|
||||
b.amount
|
||||
|
||||
ORDER BY
|
||||
b.fiscal_year,
|
||||
b.doc_date,
|
||||
b.reference;
|
||||
@@ -2,38 +2,63 @@
|
||||
|
||||
-- DROP FUNCTION payment.sp_payment_req_topup_online(varchar, bool);
|
||||
|
||||
CREATE OR REPLACE FUNCTION payment.sp_payment_req_topup_online(character varying, boolean)
|
||||
RETURNS SETOF payment.invoice_rec
|
||||
LANGUAGE plpgsql
|
||||
CREATE OR REPLACE FUNCTION payment.sp_payment_req_topup_online(
|
||||
p_load_balance VARCHAR,
|
||||
p_repost BOOLEAN
|
||||
)
|
||||
RETURNS TABLE (
|
||||
out_i_partner INT4,
|
||||
out_c_partner_name VARCHAR,
|
||||
out_c_invoice_no VARCHAR,
|
||||
out_amount NUMERIC,
|
||||
out_file_name VARCHAR,
|
||||
out_tcode VARCHAR,
|
||||
out_reversal VARCHAR,
|
||||
out_doc_type VARCHAR,
|
||||
out_posting_date VARCHAR,
|
||||
out_doc_date VARCHAR,
|
||||
out_ba VARCHAR,
|
||||
out_c_account_topup VARCHAR,
|
||||
out_c_name VARCHAR,
|
||||
out_c_account_code_topup VARCHAR,
|
||||
out_c_bank VARCHAR,
|
||||
out_c_bankaccount VARCHAR,
|
||||
out_c_bankaccountname VARCHAR,
|
||||
out_doc_header_text VARCHAR,
|
||||
out_fisc_year VARCHAR,
|
||||
out_fisc_period VARCHAR
|
||||
)
|
||||
LANGUAGE plpgsql
|
||||
AS $function$
|
||||
DECLARE
|
||||
p_load_balance ALIAS FOR $1;
|
||||
p_repost ALIAS FOR $2;
|
||||
v_trx_date DATE;
|
||||
result_record payment.invoice_rec;
|
||||
|
||||
BEGIN
|
||||
|
||||
v_trx_date := p_load_balance::DATE;
|
||||
|
||||
-- OPTIMIZATION: Consolidated set-based processing with all CTEs to eliminate loop overhead
|
||||
-- Processes all eligible partners in a single set-based flow
|
||||
RETURN QUERY
|
||||
|
||||
WITH eligible_partners AS (
|
||||
-- Get unique list of partners with billable open reservations
|
||||
SELECT DISTINCT tdrb.i_partner
|
||||
|
||||
SELECT DISTINCT
|
||||
tdrb.i_partner
|
||||
FROM trx.t_d_load_balance tdl
|
||||
INNER JOIN trx.t_d_reservation_balance tdrb
|
||||
ON tdrb.c_load_balance_no = tdl.c_load_balance_no
|
||||
INNER JOIN public.t_m_partner tmp
|
||||
ON tmp.i_partner = tdrb.i_partner
|
||||
WHERE tdl.d_load_balance < v_trx_date
|
||||
WHERE
|
||||
tdl.d_load_balance < v_trx_date
|
||||
AND tdrb.c_invoice_no IS NULL
|
||||
AND tdl.c_invoice_no IS NULL
|
||||
AND tmp.b_invoice = TRUE
|
||||
AND tdrb.c_status = 'A'
|
||||
AND tdl.b_void = p_repost
|
||||
|
||||
),
|
||||
|
||||
partner_codes AS (
|
||||
-- Fetch partner details once to avoid repetitive t_m_partner queries
|
||||
|
||||
SELECT
|
||||
tmp.i_partner,
|
||||
tmp.c_code,
|
||||
@@ -44,59 +69,136 @@ BEGIN
|
||||
tmp.c_bankaccount,
|
||||
tmp.c_bankaccountname
|
||||
FROM public.t_m_partner tmp
|
||||
WHERE tmp.i_partner IN (SELECT i_partner FROM eligible_partners)
|
||||
WHERE EXISTS (
|
||||
SELECT 1
|
||||
FROM eligible_partners ep
|
||||
WHERE ep.i_partner = tmp.i_partner
|
||||
)
|
||||
|
||||
),
|
||||
|
||||
payment_codes AS (
|
||||
|
||||
SELECT
|
||||
ep.i_partner,
|
||||
'INV' || pc.c_code || TO_CHAR(v_trx_date, 'YYMMDD') ||
|
||||
LPAD(
|
||||
COALESCE(MAX(CAST(RIGHT(tpr.c_payment, 4) AS INTEGER)), 0)::TEXT
|
||||
, 4, '0') AS code_payment
|
||||
|
||||
'INV'
|
||||
|| pc.c_code
|
||||
|| TO_CHAR(v_trx_date, 'YYMMDD')
|
||||
|| LPAD(
|
||||
(
|
||||
COALESCE(
|
||||
MAX(
|
||||
CAST(
|
||||
RIGHT(tpr.c_payment, 4) AS INTEGER
|
||||
)
|
||||
),
|
||||
0
|
||||
) + 1
|
||||
)::TEXT,
|
||||
4,
|
||||
'0'
|
||||
) AS code_payment
|
||||
|
||||
FROM eligible_partners ep
|
||||
JOIN partner_codes pc
|
||||
|
||||
INNER JOIN partner_codes pc
|
||||
ON pc.i_partner = ep.i_partner
|
||||
|
||||
LEFT JOIN payment.t_d_payment_request_topup_online tpr
|
||||
ON tpr.i_partner = ep.i_partner
|
||||
AND tpr.d_date_payment_request::DATE = v_trx_date
|
||||
GROUP BY ep.i_partner, pc.c_code
|
||||
|
||||
GROUP BY
|
||||
ep.i_partner,
|
||||
pc.c_code
|
||||
|
||||
),
|
||||
topup_requests AS (
|
||||
-- Bulk insert payment requests and capture all load_balance_nos
|
||||
INSERT INTO payment.t_d_payment_request_topup_online
|
||||
(d_date_payment_request, i_partner, i_company, i_holding, c_load_balance_no, c_status, c_payment, m_amount, i_issuer_fee, i_channel_fee)
|
||||
|
||||
inserted_payment_requests AS (
|
||||
|
||||
INSERT INTO payment.t_d_payment_request_topup_online (
|
||||
d_date_payment_request,
|
||||
i_partner,
|
||||
i_company,
|
||||
i_holding,
|
||||
c_load_balance_no,
|
||||
c_status,
|
||||
c_payment,
|
||||
m_amount,
|
||||
i_issuer_fee,
|
||||
i_channel_fee
|
||||
)
|
||||
|
||||
SELECT
|
||||
v_trx_date,
|
||||
tdrb.i_partner,
|
||||
tmpc.i_company,
|
||||
1 AS i_holding,
|
||||
1,
|
||||
tdl.c_load_balance_no,
|
||||
tdl.c_status,
|
||||
pc.code_payment,
|
||||
SUM(tdrb.i_reservation_amount),
|
||||
SUM(tdrb.i_issuer_fee),
|
||||
SUM(tdrb.i_channel_fee)
|
||||
|
||||
FROM trx.t_d_load_balance tdl
|
||||
|
||||
INNER JOIN trx.t_d_reservation_balance tdrb
|
||||
ON tdrb.c_load_balance_no = tdl.c_load_balance_no
|
||||
|
||||
INNER JOIN t_m_partner_company tmpc
|
||||
ON tmpc.i_partner = tdrb.i_partner
|
||||
|
||||
INNER JOIN payment_codes pc
|
||||
ON pc.i_partner = tdrb.i_partner
|
||||
WHERE tdrb.i_partner IN (SELECT i_partner FROM eligible_partners)
|
||||
|
||||
WHERE
|
||||
EXISTS (
|
||||
SELECT 1
|
||||
FROM eligible_partners ep
|
||||
WHERE ep.i_partner = tdrb.i_partner
|
||||
)
|
||||
AND tdrb.c_invoice_no IS NULL
|
||||
AND tdl.c_invoice_no IS NULL
|
||||
AND tdl.d_load_balance < v_trx_date
|
||||
AND tdl.b_void = p_repost
|
||||
AND tdrb.c_status = 'A'
|
||||
GROUP BY tdrb.i_partner, tmpc.i_company, tdl.c_load_balance_no, tdl.c_status, pc.code_payment, tdl.i_load_balance
|
||||
ORDER BY tdl.i_load_balance
|
||||
RETURNING c_load_balance_no, i_partner
|
||||
|
||||
GROUP BY
|
||||
tdrb.i_partner,
|
||||
tmpc.i_company,
|
||||
tdl.c_load_balance_no,
|
||||
tdl.c_status,
|
||||
pc.code_payment,
|
||||
tdl.i_load_balance
|
||||
|
||||
RETURNING
|
||||
payment.t_d_payment_request_topup_online.c_load_balance_no,
|
||||
payment.t_d_payment_request_topup_online.i_partner,
|
||||
payment.t_d_payment_request_topup_online.c_payment
|
||||
|
||||
),
|
||||
invoices_created AS (
|
||||
-- Bulk insert invoices and mark payment requests as sent
|
||||
INSERT INTO payment.t_d_invoice
|
||||
(i_partner, c_invoice_no, c_partner_code, c_partner_name, c_status, c_status_name, i_company, i_holding, m_amount, m_issuer_fee, m_channel_fee, m_mdr_net, d_invoice_date, c_source)
|
||||
|
||||
inserted_invoices AS (
|
||||
|
||||
INSERT INTO payment.t_d_invoice (
|
||||
i_partner,
|
||||
c_invoice_no,
|
||||
c_partner_code,
|
||||
c_partner_name,
|
||||
c_status,
|
||||
c_status_name,
|
||||
i_company,
|
||||
i_holding,
|
||||
m_amount,
|
||||
m_issuer_fee,
|
||||
m_channel_fee,
|
||||
m_mdr_net,
|
||||
d_invoice_date,
|
||||
c_source
|
||||
)
|
||||
|
||||
SELECT
|
||||
tdprt.i_partner,
|
||||
tdprt.c_payment,
|
||||
@@ -112,72 +214,103 @@ BEGIN
|
||||
COALESCE(SUM(tdprt.m_amount), 0),
|
||||
v_trx_date,
|
||||
'ONLINE'
|
||||
|
||||
FROM payment.t_d_payment_request_topup_online tdprt
|
||||
|
||||
INNER JOIN partner_codes pc
|
||||
ON pc.i_partner = tdprt.i_partner
|
||||
WHERE tdprt.i_partner IN (SELECT i_partner FROM eligible_partners)
|
||||
|
||||
WHERE
|
||||
EXISTS (
|
||||
SELECT 1
|
||||
FROM eligible_partners ep
|
||||
WHERE ep.i_partner = tdprt.i_partner
|
||||
)
|
||||
AND tdprt.d_date_payment_request::DATE = v_trx_date
|
||||
AND tdprt.b_sent = FALSE
|
||||
AND tdprt.b_void = FALSE
|
||||
AND tdprt.c_payment NOT LIKE 'MINV%'
|
||||
AND EXISTS (
|
||||
SELECT 1 FROM topup_requests tr
|
||||
WHERE tr.c_load_balance_no = tdprt.c_load_balance_no
|
||||
)
|
||||
GROUP BY tdprt.i_partner, tdprt.c_payment, pc.c_code, pc.c_name, tdprt.i_company, tdprt.i_holding
|
||||
RETURNING c_invoice_no, i_partner
|
||||
|
||||
GROUP BY
|
||||
tdprt.i_partner,
|
||||
tdprt.c_payment,
|
||||
pc.c_code,
|
||||
pc.c_name,
|
||||
tdprt.i_company,
|
||||
tdprt.i_holding
|
||||
|
||||
RETURNING
|
||||
payment.t_d_invoice.c_invoice_no,
|
||||
payment.t_d_invoice.i_partner
|
||||
|
||||
),
|
||||
mark_requests_sent AS (
|
||||
-- Bulk update payment requests to mark as sent (in single operation, not per invoice)
|
||||
|
||||
update_payment_request AS (
|
||||
|
||||
UPDATE payment.t_d_payment_request_topup_online tpr
|
||||
SET b_sent = TRUE
|
||||
WHERE tpr.i_partner IN (SELECT i_partner FROM eligible_partners)
|
||||
AND tpr.d_date_payment_request::DATE = v_trx_date
|
||||
AND EXISTS (
|
||||
SELECT 1 FROM invoices_created ic
|
||||
WHERE ic.i_partner = tpr.i_partner
|
||||
SET
|
||||
b_sent = TRUE
|
||||
WHERE
|
||||
EXISTS (
|
||||
SELECT 1
|
||||
FROM inserted_invoices ii
|
||||
WHERE ii.i_partner = tpr.i_partner
|
||||
)
|
||||
AND tpr.d_date_payment_request::DATE = v_trx_date
|
||||
|
||||
RETURNING
|
||||
tpr.i_partner
|
||||
|
||||
),
|
||||
update_reservations AS (
|
||||
-- Bulk update reservations with invoice numbers (single operation, not per detail)
|
||||
|
||||
update_reservation_balance AS (
|
||||
|
||||
UPDATE trx.t_d_reservation_balance tdrb
|
||||
SET c_invoice_no = tpr.c_payment
|
||||
SET
|
||||
c_invoice_no = tpr.c_payment
|
||||
|
||||
FROM payment.t_d_payment_request_topup_online tpr
|
||||
WHERE tdrb.c_load_balance_no = tpr.c_load_balance_no
|
||||
AND tdrb.i_partner IN (SELECT i_partner FROM eligible_partners)
|
||||
|
||||
WHERE
|
||||
tdrb.c_load_balance_no = tpr.c_load_balance_no
|
||||
AND tpr.d_date_payment_request::DATE = v_trx_date
|
||||
AND tpr.b_sent = TRUE
|
||||
),
|
||||
final_results AS (
|
||||
-- Build result set with all required fields
|
||||
|
||||
RETURNING
|
||||
tdrb.c_load_balance_no
|
||||
|
||||
)
|
||||
|
||||
SELECT
|
||||
tdi.i_partner,
|
||||
tdi.c_partner_name,
|
||||
tdi.c_invoice_no,
|
||||
tdi.m_amount AS amount,
|
||||
tdi.c_invoice_no AS c_file_name,
|
||||
'FB70' AS tcode,
|
||||
'A' AS reversal,
|
||||
'DR' AS doc_type,
|
||||
TO_CHAR(tdi.d_invoice_date, 'YYYYMMDD') AS posting_date,
|
||||
TO_CHAR(tdi.d_invoice_date, 'YYYYMMDD') AS doc_date,
|
||||
'C100' AS ba,
|
||||
pc.c_account_topup,
|
||||
pc.c_name,
|
||||
pc.c_account_code_topup,
|
||||
pc.c_bank,
|
||||
pc.c_bankaccount,
|
||||
pc.c_bankaccountname,
|
||||
CONCAT('IVSAP', tdi.c_invoice_no) AS doc_header_text,
|
||||
TO_CHAR(tdi.d_invoice_date, 'YYYY') AS fisc_year,
|
||||
TO_CHAR(tdi.d_invoice_date, 'MM') AS fisc_period
|
||||
tdi.i_partner::INT4,
|
||||
tdi.c_partner_name::VARCHAR,
|
||||
tdi.c_invoice_no::VARCHAR,
|
||||
tdi.m_amount::NUMERIC,
|
||||
tdi.c_invoice_no::VARCHAR,
|
||||
'FB70'::VARCHAR,
|
||||
'A'::VARCHAR,
|
||||
'DR'::VARCHAR,
|
||||
TO_CHAR(tdi.d_invoice_date, 'YYYYMMDD')::VARCHAR,
|
||||
TO_CHAR(tdi.d_invoice_date, 'YYYYMMDD')::VARCHAR,
|
||||
'C100'::VARCHAR,
|
||||
pc.c_account_topup::VARCHAR,
|
||||
pc.c_name::VARCHAR,
|
||||
pc.c_account_code_topup::VARCHAR,
|
||||
pc.c_bank::VARCHAR,
|
||||
pc.c_bankaccount::VARCHAR,
|
||||
pc.c_bankaccountname::VARCHAR,
|
||||
CONCAT('IVSAP', tdi.c_invoice_no)::VARCHAR,
|
||||
TO_CHAR(tdi.d_invoice_date, 'YYYY')::VARCHAR,
|
||||
TO_CHAR(tdi.d_invoice_date, 'MM')::VARCHAR
|
||||
|
||||
FROM payment.t_d_invoice tdi
|
||||
|
||||
INNER JOIN partner_codes pc
|
||||
ON pc.i_partner = tdi.i_partner
|
||||
WHERE tdi.d_invoice_date = v_trx_date
|
||||
AND tdi.c_source = 'ONLINE'
|
||||
)
|
||||
SELECT * FROM final_results;
|
||||
|
||||
WHERE
|
||||
tdi.d_invoice_date = v_trx_date
|
||||
AND tdi.c_source = 'ONLINE';
|
||||
|
||||
RAISE NOTICE 'sp_payment_req_topup_online completed for date: %', v_trx_date;
|
||||
|
||||
@@ -187,6 +320,7 @@ $function$;
|
||||
|
||||
|
||||
-- //////////////////////////////// OLD STORE PROCEDURE ///////////////////////////////
|
||||
/*
|
||||
-- DROP FUNCTION payment.sp_payment_req_topup_online(varchar, bool);
|
||||
|
||||
CREATE OR REPLACE FUNCTION payment.sp_payment_req_topup_online(character varying, boolean)
|
||||
@@ -354,6 +488,8 @@ BEGIN
|
||||
$function$
|
||||
;
|
||||
|
||||
*/
|
||||
|
||||
|
||||
|
||||
-- Indexes to optimize performance
|
||||
|
||||
Reference in New Issue
Block a user