mirror of
https://github.com/mohamadihsan/topuponline.git
synced 2026-12-05 11:06:28 +00:00
794 lines
26 KiB
JavaScript
794 lines
26 KiB
JavaScript
// GET PARTNER, MERCHANT, TERMINAL FROM TID
|
|
const generateInvoice = async (invoiceDate, repost, trx) => {
|
|
let result = await trx.raw(
|
|
`SELECT * FROM payment.sp_payment_req_topup_online('${invoiceDate}', ${repost})`
|
|
);
|
|
|
|
return result.rows || result;
|
|
};
|
|
|
|
const insertPostingEt = async (code, dataCashIn, trx) => {
|
|
let type = ['S', 'H'];
|
|
let et = [];
|
|
let posting_et = [];
|
|
type.forEach((item, index) => {
|
|
let data = {
|
|
file_name: code,
|
|
tcode: dataCashIn.tcode,
|
|
reversal: dataCashIn.reversal,
|
|
doc_type: dataCashIn.doc_type,
|
|
fiscal_year: dataCashIn.fisc_year,
|
|
posting_date: dataCashIn.posting_date,
|
|
doc_date: dataCashIn.doc_date,
|
|
no_dok: code,
|
|
reference: code,
|
|
amount: dataCashIn.amount,
|
|
ba: dataCashIn.ba,
|
|
indeks: index + 1,
|
|
totalidx: type.length,
|
|
doc_header_text: code,
|
|
pos_status: 4,
|
|
};
|
|
|
|
if (item == 'H') {
|
|
data = {
|
|
...data,
|
|
...{
|
|
text: `Cash-in CMS ${code}`,
|
|
debit_kredit: item,
|
|
account: dataCashIn.c_account_topup,
|
|
customer: null,
|
|
},
|
|
};
|
|
} else {
|
|
data = {
|
|
...data,
|
|
...{
|
|
text: `Cash-in CMS ${code}`,
|
|
debit_kredit: item,
|
|
account: null,
|
|
customer: dataCashIn.c_account_code_topup,
|
|
},
|
|
};
|
|
}
|
|
|
|
et.push(data);
|
|
});
|
|
|
|
posting_et = await trx('public.posting_et').insert(et, [
|
|
'indeks',
|
|
'customer',
|
|
'account',
|
|
'ba',
|
|
'fiscal_year',
|
|
'amount',
|
|
'no_dok',
|
|
'text',
|
|
'debit_kredit',
|
|
]);
|
|
|
|
return posting_et[0];
|
|
};
|
|
|
|
const cashInPostingTable = 'payment.t_cash_in_sap_posting';
|
|
|
|
const getPostingEt = (db, limit) => db('public.posting_et AS pe')
|
|
.select(
|
|
'pe.doc_header_text', 'pe.ba', 'pe.doc_date', 'pe.posting_date', 'pe.fiscal_year',
|
|
'pe.doc_type', 'pe.reference', 'pe.no_dok', 'pe.tcode', 'pe.amount',
|
|
db.raw("TO_CHAR(pe.doc_date::date, 'MM') AS fis_period"),
|
|
db.raw("'IDR' AS currency"),
|
|
db.raw("MAX(pe.customer) FILTER (WHERE pe.debit_kredit = 'S') AS customer"),
|
|
db.raw("MAX(pe.account) FILTER (WHERE pe.debit_kredit = 'H') AS account"),
|
|
db.raw("MAX(pe.text) FILTER (WHERE pe.debit_kredit = 'S') AS text1"),
|
|
db.raw("MAX(pe.text) FILTER (WHERE pe.debit_kredit = 'H') AS text2"),
|
|
)
|
|
.join('payment.t_d_cash_in AS cash', 'cash.c_cash_in_no', 'pe.no_dok')
|
|
.where({ 'pe.tcode': 'FB70', 'pe.pos_status': '4' })
|
|
.whereRaw("NULLIF(BTRIM(cash.c_return_sap::text), '') IS NULL")
|
|
.whereNotExists(db(cashInPostingTable).whereColumn('cash_in_no', 'pe.no_dok'))
|
|
.groupBy(
|
|
'pe.doc_header_text', 'pe.ba', 'pe.doc_date', 'pe.posting_date', 'pe.fiscal_year',
|
|
'pe.doc_type', 'pe.reference', 'pe.no_dok', 'pe.tcode', 'pe.amount',
|
|
)
|
|
.havingRaw("MAX(NULLIF(BTRIM(pe.customer::text), '')) FILTER (WHERE pe.debit_kredit = 'S') IS NOT NULL")
|
|
.havingRaw("MAX(NULLIF(BTRIM(pe.account::text), '')) FILTER (WHERE pe.debit_kredit = 'H') IS NOT NULL")
|
|
.orderBy('pe.no_dok')
|
|
.limit(limit);
|
|
|
|
// Batasi pengiriman lintas replika tanpa memegang koneksi selama panggilan SAP.
|
|
const claimCashInPosting = (db, code) => db.transaction(async (trx) => {
|
|
const { rows: [{ acquired }] } = await trx.raw('SELECT pg_try_advisory_xact_lock(17290, 1) AS acquired');
|
|
if (!acquired) return 'busy';
|
|
const sending = await trx(cashInPostingTable)
|
|
.where('state', 'sending')
|
|
.whereRaw("updated_at > now() - interval '30 seconds'")
|
|
.first('cash_in_no');
|
|
if (sending) return 'busy';
|
|
|
|
const pending = await trx('payment.t_d_cash_in AS cash')
|
|
.where('cash.c_cash_in_no', code)
|
|
.whereRaw("NULLIF(BTRIM(cash.c_return_sap::text), '') IS NULL")
|
|
.whereExists(trx('public.posting_et').where({ no_dok: code, tcode: 'FB70', pos_status: '4' }))
|
|
.first('cash.c_cash_in_no');
|
|
if (!pending) return 'skipped';
|
|
|
|
const inserted = await trx(cashInPostingTable)
|
|
.insert({ cash_in_no: code, state: 'sending' })
|
|
.onConflict('cash_in_no').ignore().returning('cash_in_no');
|
|
return inserted.length === 1 ? 'claimed' : 'skipped';
|
|
});
|
|
|
|
const releaseUnsentCashInPosting = (db, code) => db(cashInPostingTable)
|
|
.where({ cash_in_no: code, state: 'sending' }).del();
|
|
|
|
const recordCashInOutcome = (db, code, { documentNo, documentYear, status }) => db(cashInPostingTable)
|
|
.where({ cash_in_no: code, state: 'sending' })
|
|
.update({
|
|
state: documentNo ? 'accepted' : 'unknown',
|
|
document_no: documentNo || null,
|
|
document_year: documentYear || null,
|
|
outcome: status,
|
|
updated_at: db.fn.now(),
|
|
});
|
|
|
|
const getAcceptedCashInPostings = (db, limit) => db(cashInPostingTable)
|
|
.where('state', 'accepted').orderBy('updated_at').limit(limit).select('cash_in_no');
|
|
|
|
// Pulihkan hasil SAP yang sudah dikonfirmasi jika penyimpanan CMS sebelumnya gagal.
|
|
const finishCashInPosting = (db, code) => db.transaction(async (trx) => {
|
|
const posting = await trx(cashInPostingTable)
|
|
.where({ cash_in_no: code, state: 'accepted' }).forUpdate().first();
|
|
if (!posting) return;
|
|
|
|
const { document_no: documentNo, document_year: documentYear } = posting;
|
|
const cashIn = await trx('payment.t_d_cash_in').where('c_cash_in_no', code)
|
|
.update({ c_return_sap: documentNo, c_return_year_sap: documentYear }).returning('i_cash_in');
|
|
if (cashIn.length !== 1) throw new Error('Cash-in document missing or duplicated during SAP finalization');
|
|
await trx('trx.t_d_reservation_balance')
|
|
.whereIn('c_reservation_no', trx('payment.t_d_cash_in_detail')
|
|
.where('i_cash_in', cashIn[0].i_cash_in).select('c_reservation_no'))
|
|
.update({ c_return_cash_in_sap: documentNo, c_cash_in_no: code });
|
|
await trx('public.posting_et').where({ no_dok: code, tcode: 'FB70' })
|
|
.update({ pos_status: 0, status0: 'S', no_doksap: documentNo, status1: 'SAP confirmed' });
|
|
await trx(cashInPostingTable).where({ cash_in_no: code })
|
|
.update({ state: 'completed', updated_at: trx.fn.now() });
|
|
});
|
|
|
|
const getRecon = async (trx) => {
|
|
let sql = `SELECT
|
|
tdi.c_invoice_no,
|
|
'FB70' AS tcode,
|
|
'A' as reversal,
|
|
'DR' AS doc_type,
|
|
'C100' AS ba,
|
|
to_char(tdpr.d_payment, 'MM') AS fisc_period,
|
|
to_char(tdpr.d_payment, 'YYYY') AS fisc_year,
|
|
to_char(tdpr.d_payment, 'YYYYMMDD') AS doc_date,
|
|
tdrb.i_partner,
|
|
tdrb.c_partner_code,
|
|
tdrb.c_partner_name,
|
|
tmp.c_account_topup,
|
|
tmp.c_account_code_topup,
|
|
i_company,
|
|
i_holding,
|
|
to_char(tdpr.d_payment, 'YYYY-MM-DD') AS d_payment_recon,
|
|
to_char(tdi.d_invoice_date, 'YYYY-MM-DD') AS d_invoice_date,
|
|
sum(tdrb.i_reservation_amount) AS amount,
|
|
CASE
|
|
WHEN to_char(tdpr.d_payment, 'MM') = to_char(tdi.d_invoice_date, 'MM')
|
|
THEN FALSE
|
|
ELSE
|
|
TRUE
|
|
END b_past_month
|
|
FROM
|
|
trx.t_d_reservation_balance tdrb
|
|
LEFT JOIN trx.t_d_load_balance tdlb
|
|
ON tdlb.c_load_balance_no = tdrb.c_load_balance_no
|
|
LEFT JOIN payment.t_d_invoice tdi
|
|
ON tdi.c_invoice_no = tdlb.c_invoice_no
|
|
LEFT JOIN public.t_m_partner tmp
|
|
ON tmp.i_partner = tdrb.i_partner
|
|
LEFT JOIN payment.t_d_payment_recon tdpr
|
|
ON tdpr.c_reservation_no = tdrb.c_reservation_no
|
|
WHERE
|
|
tdrb.c_status = 'A'
|
|
AND tdlb.c_status = 'A'
|
|
AND tdi.c_return_sap is not null
|
|
AND tdi.c_status != 'X'
|
|
AND tdrb.c_reservation_no in (
|
|
SELECT c_reservation_no FROM payment.t_d_payment_recon tdpr WHERE tdpr.c_status = 'S'
|
|
)
|
|
GROUP BY 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17, tdpr.d_payment, b_past_month
|
|
ORDER BY tdpr.d_payment`;
|
|
let result = await trx.raw(sql);
|
|
|
|
return result.rows;
|
|
};
|
|
|
|
const getReserve = async (trx, code, date, invoice) => {
|
|
const res = await trx
|
|
.select([
|
|
'tdrb.c_reservation_no',
|
|
'tdrb.i_reservation_amount',
|
|
'tdpr.c_payment_recon_no',
|
|
'tdpr.d_payment',
|
|
'tdpr.d_payment_recon',
|
|
'tdrb.c_partner_code',
|
|
'tdrb.c_partner_name',
|
|
'tdrb.c_load_balance_no',
|
|
'tdi.c_invoice_no',
|
|
'tdi.m_amount as invoice_amount'
|
|
])
|
|
.from('trx.t_d_reservation_balance AS tdrb')
|
|
.leftJoin('trx.t_d_load_balance as tdlb', function () {
|
|
this.on('tdrb.c_load_balance_no', '=', 'tdlb.c_load_balance_no');
|
|
})
|
|
.innerJoin('payment.t_d_invoice as tdi', function () {
|
|
this.on('tdi.c_invoice_no', '=', 'tdlb.c_invoice_no').orOn(
|
|
'tdi.c_invoice_no',
|
|
'=',
|
|
'tdrb.c_invoice_no'
|
|
);
|
|
})
|
|
.innerJoin('payment.t_d_payment_recon as tdpr', function () {
|
|
this.on('tdpr.c_reservation_no', '=', 'tdrb.c_reservation_no');
|
|
})
|
|
.where('tdrb.c_status', '=', 'A')
|
|
.where('tdi.c_status', '!=', 'X')
|
|
.where('tdrb.c_partner_code', '=', code)
|
|
.where('tdpr.d_payment', '=', date)
|
|
.where('tdi.c_invoice_no', '=', invoice)
|
|
.where('tdpr.c_status', '=', 'S')
|
|
.whereNotNull('tdi.c_return_sap')
|
|
.whereNotNull('tdrb.c_load_balance_no')
|
|
.whereNull('tdrb.c_cash_in_no')
|
|
// .whereRaw(
|
|
// `tdrb.c_reservation_no in (SELECT c_reservation_no FROM payment.t_d_payment_recon where c_status = 'S' ) AND tdrb.c_reservation_no NOT IN (SELECT c_reservation_no FROM payment.t_d_cash_in_detail WHERE b_reversal IS NOT TRUE)`
|
|
// );
|
|
|
|
return res;
|
|
};
|
|
|
|
|
|
|
|
const getSalesKMT = async (trx, code, date, invoice) => {
|
|
const res = await trx
|
|
.select([
|
|
'tdskm.c_sales_no',
|
|
'tdskm.i_total_billing',
|
|
'tdpr.c_payment_recon_no',
|
|
'tdpr.d_payment',
|
|
'tdpr.d_payment_recon',
|
|
'tdskm.c_partner_code',
|
|
'tdskm.c_partner_name',
|
|
'tdsi.c_invoice_no',
|
|
'tdsi.m_total_billing as invoice_amount'
|
|
])
|
|
.from('kmt_mobile.t_d_sales_kmt_mobile AS tdskm')
|
|
.innerJoin('payment.t_d_payment_recon_sales_kmt_mobile as tdpr', 'tdpr.c_sales_no', 'tdskm.c_sales_no')
|
|
.innerJoin('payment.t_d_sales_invoice as tdsi', 'tdsi.i_sales_invoice', 'tdskm.i_sales_invoice')
|
|
|
|
.where('tdskm.c_sales_status', '=', 'A')
|
|
.where('tdsi.c_status', '!=', 'X')
|
|
.where('tdskm.c_partner_code', '=', code)
|
|
.where('tdpr.d_payment', '=', date)
|
|
.where('tdsi.c_invoice_no', '=', invoice)
|
|
.where('tdpr.c_status', '=', 'S')
|
|
.whereNotNull('tdsi.c_return_sap')
|
|
.whereRaw(
|
|
`NOT EXISTS (
|
|
SELECT 1
|
|
FROM payment.t_d_cash_in_detail cid
|
|
WHERE cid.c_reservation_no = tdskm.c_sales_no
|
|
AND cid.b_reversal IS NOT TRUE
|
|
)`
|
|
);
|
|
|
|
return res;
|
|
};
|
|
|
|
const getSalesInternal = async (trx, code, date, invoice) => {
|
|
const res = await trx
|
|
.select([
|
|
'tdskm.c_sales_no',
|
|
'tdskm.i_total_price',
|
|
'tdpr.c_payment_recon_no',
|
|
'tdpr.d_payment',
|
|
'tdpr.d_payment_recon',
|
|
'tmp.c_code as c_partner_code',
|
|
'tmp.c_name as c_partner_name',
|
|
'tdsi.c_invoice_no',
|
|
'tdsi.i_total_price as invoice_amount'
|
|
])
|
|
.from('trx.t_d_sales_internal AS tdskm')
|
|
.innerJoin('payment.t_d_payment_recon_sales_internal as tdpr', 'tdpr.c_sales_no', 'tdskm.c_sales_no')
|
|
.innerJoin('payment.t_d_sales_invoice_internal as tdsi', 'tdsi.i_sales_invoice_internal', 'tdskm.i_sales_invoice_internal')
|
|
.innerJoin('public.t_m_partner as tmp', 'tmp.i_partner', 'tdskm.i_partner')
|
|
.where('tdsi.c_status', '!=', 'X')
|
|
.where('tmp.c_code', '=', code)
|
|
.where('tdpr.d_payment', '=', date)
|
|
.where('tdsi.c_invoice_no', '=', invoice)
|
|
.where('tdpr.c_status', '=', 'S')
|
|
.whereNotNull('tdsi.c_return_sap_fi')
|
|
.whereRaw(
|
|
`NOT EXISTS (
|
|
SELECT 1
|
|
FROM payment.t_d_cash_in_detail cid
|
|
WHERE cid.c_reservation_no = tdskm.c_sales_no
|
|
AND cid.b_reversal IS NOT TRUE)`
|
|
);
|
|
|
|
return res;
|
|
};
|
|
|
|
const getTopUpCashlessInternal = async (trx, code, date, invoice) => {
|
|
const res = await trx
|
|
.select([
|
|
'tdskm.c_topup_no',
|
|
'tdskm.i_amount',
|
|
'tdpr.c_payment_recon_no',
|
|
'tdpr.d_payment',
|
|
'tdpr.d_payment_recon',
|
|
'tmp.c_code as c_partner_code',
|
|
'tmp.c_name as c_partner_name',
|
|
'tdsi.c_invoice_no',
|
|
'tdsi.i_total_amount as invoice_amount'
|
|
])
|
|
.from('trx.t_d_topup_cashless_internal AS tdskm')
|
|
.innerJoin('payment.t_d_payment_recon_topup_cashless_internal as tdpr', 'tdpr.c_topup_no', 'tdskm.c_topup_no')
|
|
.innerJoin('payment.t_d_topup_cashless_invoice_internal as tdsi', 'tdsi.i_topup_cashless_invoice_internal', 'tdskm.i_topup_cashless_invoice_internal')
|
|
.innerJoin('public.t_m_partner as tmp', 'tmp.i_partner', 'tdskm.i_partner')
|
|
.where('tdsi.c_status', '!=', 'X')
|
|
.where('tmp.c_code', '=', code)
|
|
.where('tdpr.d_payment', '=', date)
|
|
.where('tdsi.c_invoice_no', '=', invoice)
|
|
.where('tdpr.c_status', '=', 'S')
|
|
.whereNotNull('tdsi.c_return_sap')
|
|
.whereRaw(
|
|
`NOT EXISTS (
|
|
SELECT 1
|
|
FROM payment.t_d_cash_in_detail cid
|
|
WHERE cid.c_reservation_no = tdskm.c_topup_no
|
|
AND cid.b_reversal IS NOT TRUE
|
|
)`
|
|
);
|
|
|
|
return res;
|
|
};
|
|
|
|
const getPartnerRecon = async (trx) => {
|
|
// const res = await trx
|
|
// .select([
|
|
// 'tdpr.c_partner_code',
|
|
// 'tdrb.c_partner_name',
|
|
// 'tdi.c_invoice_no',
|
|
// 'tdi.d_invoice_date',
|
|
// trx.raw("to_char(tdpr.d_payment, 'YYYY-MM-DD') AS d_payment"),
|
|
// trx.raw('SUM(tdrb.i_reservation_amount) AS amount'),
|
|
// 'tmp.c_account_topup',
|
|
// 'tmp.c_account_code_topup',
|
|
// 'i_company',
|
|
// 'i_holding',
|
|
// trx.raw(`CASE
|
|
// WHEN to_char(tdpr.d_payment, 'MM') >= to_char(tdi.d_invoice_date, 'MM')
|
|
// THEN 'IP'
|
|
// ELSE
|
|
// 'OP'
|
|
// END c_in_period`),
|
|
// ])
|
|
// .from('trx.t_d_reservation_balance AS tdrb')
|
|
// .leftJoin('trx.t_d_load_balance as tdlb', function () {
|
|
// this.on('tdrb.c_load_balance_no', '=', 'tdlb.c_load_balance_no');
|
|
// })
|
|
// .leftJoin('payment.t_d_invoice as tdi', function () {
|
|
// this.on('tdi.c_invoice_no', '=', 'tdlb.c_invoice_no').orOn(
|
|
// 'tdi.c_invoice_no',
|
|
// '=',
|
|
// 'tdrb.c_invoice_no'
|
|
// );
|
|
// })
|
|
// .leftJoin('payment.t_d_payment_recon as tdpr', function () {
|
|
// this.on('tdpr.c_reservation_no', '=', 'tdrb.c_reservation_no');
|
|
// })
|
|
// .leftJoin('public.t_m_partner as tmp', function () {
|
|
// this.on('tmp.i_partner', '=', 'tdrb.i_partner');
|
|
// })
|
|
|
|
// .where('tdrb.c_status', '=', 'A')
|
|
// .where('tdi.c_status', '!=', 'X')
|
|
// .where('tdpr.c_status', '=', 'S')
|
|
// .whereNotNull('tdi.c_return_sap')
|
|
// .whereNotNull('tdrb.c_load_balance_no')
|
|
// .whereRaw(
|
|
// `tdrb.c_reservation_no in (SELECT c_reservation_no FROM payment.t_d_payment_recon where c_status = 'S' ) AND tdrb.c_reservation_no NOT IN (SELECT c_reservation_no FROM payment.t_d_cash_in_detail WHERE b_reversal IS NOT TRUE)`
|
|
// )
|
|
// .groupBy([
|
|
// 'tdpr.c_partner_code',
|
|
// 'tdrb.c_partner_name',
|
|
// 'tdi.c_invoice_no',
|
|
// 'tdi.d_invoice_date',
|
|
// 'tdpr.d_payment',
|
|
// 'tmp.c_account_topup',
|
|
// 'tmp.c_account_code_topup',
|
|
// 'tdi.i_company',
|
|
// 'i_holding',
|
|
// ])
|
|
// .limit(1);
|
|
// .toSql()
|
|
|
|
const res = await trx
|
|
.select([
|
|
'tdpr.c_partner_code',
|
|
'tdrb.c_partner_name',
|
|
'tdi.c_invoice_no',
|
|
'tdi.d_invoice_date',
|
|
trx.raw("to_char(tdpr.d_payment, 'YYYY-MM-DD') AS d_payment"),
|
|
trx.raw('coalesce(SUM(tdrb.i_reservation_amount),0) AS amount'),
|
|
// trx.raw('coalesce(SUM(tdcid.m_amount),0) AS amount_cash_in'),
|
|
'tmp.c_account_topup',
|
|
'tmp.c_account_code_topup',
|
|
'i_company',
|
|
'i_holding',
|
|
trx.raw(`CASE
|
|
WHEN date_trunc('month', tdpr.d_payment) >= date_trunc('month', tdi.d_invoice_date)
|
|
THEN 'IP'
|
|
ELSE
|
|
'OP'
|
|
END c_in_period`),
|
|
])
|
|
.from('trx.t_d_reservation_balance AS tdrb')
|
|
.leftJoin('trx.t_d_load_balance as tdlb', function () {
|
|
this.on('tdrb.c_load_balance_no', '=', 'tdlb.c_load_balance_no');
|
|
})
|
|
.innerJoin('payment.t_d_invoice as tdi', function () {
|
|
this.on('tdi.c_invoice_no', '=', 'tdlb.c_invoice_no').orOn(
|
|
'tdi.c_invoice_no',
|
|
'=',
|
|
'tdrb.c_invoice_no'
|
|
);
|
|
})
|
|
.innerJoin('payment.t_d_payment_recon as tdpr', function () {
|
|
this.on('tdpr.c_reservation_no', '=', 'tdrb.c_reservation_no');
|
|
})
|
|
.innerJoin('public.t_m_partner as tmp', function () {
|
|
this.on('tmp.i_partner', '=', 'tdrb.i_partner');
|
|
})
|
|
// .leftJoin('payment.t_d_cash_in_detail as tdcid', function () {
|
|
// this.on('tdcid.c_reservation_no', '=', 'tdrb.c_reservation_no');
|
|
// })
|
|
|
|
.where('tdrb.c_status', '=', 'A')
|
|
.where('tdi.c_status', '!=', 'X')
|
|
.where('tdpr.c_status', '=', 'S')
|
|
.whereNotNull('tdi.c_return_sap')
|
|
.whereNotNull('tdrb.c_load_balance_no')
|
|
.whereNull('tdrb.c_cash_in_no')
|
|
.groupBy([
|
|
'tdpr.c_partner_code',
|
|
'tdrb.c_partner_name',
|
|
'tdi.c_invoice_no',
|
|
'tdi.d_invoice_date',
|
|
'tdpr.d_payment',
|
|
'tmp.c_account_topup',
|
|
'tmp.c_account_code_topup',
|
|
'tdi.i_company',
|
|
'i_holding',
|
|
]);
|
|
// .havingRaw(
|
|
// 'coalesce(SUM(tdrb.i_reservation_amount),0) > coalesce(SUM(tdcid.m_amount),0)'
|
|
// );
|
|
|
|
return res;
|
|
};
|
|
|
|
const getPartnerReconSales = async (trx) => {
|
|
|
|
const res = await trx
|
|
.select([
|
|
'tdsi.c_partner_code',
|
|
'tdsi.c_partner_name',
|
|
'tdsi.c_invoice_no',
|
|
'tdsi.d_invoice_date',
|
|
trx.raw("to_char(tdpr.d_payment, 'YYYY-MM-DD') AS d_payment"),
|
|
trx.raw('coalesce(SUM(tdskm.i_total_billing),0) AS amount'),
|
|
'tmp.c_account_topup',
|
|
'tmp.c_account_code_topup',
|
|
trx.raw(`CASE WHEN date_trunc('month', tdpr.d_payment) >= date_trunc('month', tdsi.d_invoice_date)
|
|
THEN 'IP' ELSE 'OP'
|
|
END c_in_period`),
|
|
])
|
|
.from('kmt_mobile.t_d_sales_kmt_mobile AS tdskm')
|
|
|
|
.innerJoin('payment.t_d_sales_invoice as tdsi', function () {
|
|
this.on('tdsi.i_sales_invoice', '=', 'tdskm.i_sales_invoice');
|
|
})
|
|
.innerJoin('payment.t_d_payment_recon_sales_kmt_mobile as tdpr', function () {
|
|
this.on('tdpr.c_sales_no', '=', 'tdskm.c_sales_no');
|
|
})
|
|
.innerJoin('public.t_m_partner as tmp', function () {
|
|
this.on('tmp.i_partner', '=', 'tdskm.i_partner');
|
|
})
|
|
.where('tdskm.c_sales_status', '=', 'A')
|
|
.where('tdsi.c_status', '!=', 'X')
|
|
.where('tdpr.c_status', '=', 'S')
|
|
.whereNotNull('tdsi.c_return_sap')
|
|
.whereNull('tdskm.c_cash_in_no')
|
|
.groupBy([
|
|
'tdsi.c_partner_code',
|
|
'tdsi.c_partner_name',
|
|
'tdsi.c_invoice_no',
|
|
'tdsi.d_invoice_date',
|
|
'tdpr.d_payment',
|
|
'tmp.c_account_topup',
|
|
'tmp.c_account_code_topup'
|
|
]);
|
|
|
|
return res;
|
|
};
|
|
|
|
const getPartnerReconSalesInternal = async (trx) => {
|
|
|
|
const res = await trx
|
|
.select([
|
|
'tmp.c_code as c_partner_code',
|
|
'tmp.c_name as c_partner_name',
|
|
'tdsi.c_invoice_no',
|
|
'tdsi.d_invoice_date',
|
|
trx.raw("to_char(tdpr.d_payment, 'YYYY-MM-DD') AS d_payment"),
|
|
trx.raw('coalesce(SUM(tdskm.i_total_price),0) AS amount'),
|
|
'tmp.c_account_sales_revenue',
|
|
'tmp.c_account_code_sales_ar',
|
|
trx.raw(`CASE WHEN date_trunc('month', tdpr.d_payment) >= date_trunc('month', tdsi.d_invoice_date)
|
|
THEN 'IP' ELSE 'IP'
|
|
END c_in_period`),
|
|
])
|
|
.from('trx.t_d_sales_internal AS tdskm')
|
|
|
|
.innerJoin('payment.t_d_sales_invoice_internal as tdsi', function () {
|
|
this.on('tdsi.i_sales_invoice_internal', '=', 'tdskm.i_sales_invoice_internal');
|
|
})
|
|
.innerJoin('payment.t_d_payment_recon_sales_internal as tdpr', function () {
|
|
this.on('tdpr.c_sales_no', '=', 'tdskm.c_sales_no');
|
|
})
|
|
.innerJoin('public.t_m_partner as tmp', function () {
|
|
this.on('tmp.i_partner', '=', 'tdskm.i_partner');
|
|
})
|
|
.where('tdsi.c_status', '!=', 'X')
|
|
.where('tdpr.c_status', '=', 'S')
|
|
.whereNotNull('tdsi.c_return_sap_fi')
|
|
.whereNull('tdskm.i_cash_in')
|
|
.groupBy([
|
|
'tmp.c_code',
|
|
'tmp.c_name',
|
|
'tdsi.c_invoice_no',
|
|
'tdsi.d_invoice_date',
|
|
'tdpr.d_payment',
|
|
'tmp.c_account_sales_revenue',
|
|
'tmp.c_account_code_sales_ar'
|
|
]);
|
|
|
|
return res;
|
|
};
|
|
|
|
const getPartnerReconTopUpCashlessInternal = async (trx) => {
|
|
|
|
const res = await trx
|
|
.select([
|
|
'tmp.c_code as c_partner_code',
|
|
'tmp.c_name as c_partner_name',
|
|
'tdsi.c_invoice_no',
|
|
'tdsi.d_invoice_date',
|
|
trx.raw("to_char(tdpr.d_payment, 'YYYY-MM-DD') AS d_payment"),
|
|
trx.raw('coalesce(SUM(tdskm.i_amount),0) AS amount'),
|
|
'tmp.c_account_topup',
|
|
'tmp.c_account_code_topup',
|
|
trx.raw(`CASE WHEN date_trunc('month', tdpr.d_payment) >= date_trunc('month', tdsi.d_invoice_date)
|
|
THEN 'IP' ELSE 'IP'
|
|
END c_in_period`),
|
|
])
|
|
.from('trx.t_d_topup_cashless_internal AS tdskm')
|
|
|
|
.innerJoin('payment.t_d_topup_cashless_invoice_internal as tdsi', function () {
|
|
this.on('tdsi.i_topup_cashless_invoice_internal', '=', 'tdskm.i_topup_cashless_invoice_internal');
|
|
})
|
|
.innerJoin('payment.t_d_payment_recon_topup_cashless_internal as tdpr', function () {
|
|
this.on('tdpr.c_topup_no', '=', 'tdskm.c_topup_no');
|
|
})
|
|
.innerJoin('public.t_m_partner as tmp', function () {
|
|
this.on('tmp.i_partner', '=', 'tdskm.i_partner');
|
|
})
|
|
.where('tdsi.c_status', '!=', 'X')
|
|
.where('tdpr.c_status', '=', 'S')
|
|
.whereNotNull('tdsi.c_return_sap')
|
|
.whereNull('tdskm.i_cash_in')
|
|
.groupBy([
|
|
'tmp.c_code',
|
|
'tmp.c_name',
|
|
'tdsi.c_invoice_no',
|
|
'tdsi.d_invoice_date',
|
|
'tdpr.d_payment',
|
|
'tmp.c_account_topup',
|
|
'tmp.c_account_code_topup'
|
|
]);
|
|
|
|
return res;
|
|
};
|
|
|
|
const generateCode = async (trx, c_partner_code, d_payment_recon) => {
|
|
let code = '';
|
|
let prefix = `CIN${c_partner_code}${d_payment_recon}`;
|
|
|
|
let result = await trx
|
|
.select([trx.raw(`right(c_cash_in_no, 3) AS code`)])
|
|
.from('payment.t_d_cash_in')
|
|
.where('c_cash_in_no', 'like', `${prefix}%`)
|
|
// .where("d_project_date", projectDate)
|
|
.orderBy('c_cash_in_no', 'DESC')
|
|
.first();
|
|
|
|
if (result) {
|
|
let counter = parseInt(result.code, 10) + 1;
|
|
counter = String(counter).padStart(3, '0');
|
|
code = `${prefix}${counter}`;
|
|
} else {
|
|
code = `${prefix}001`;
|
|
}
|
|
|
|
return code;
|
|
};
|
|
|
|
const insertCashInDetail = async (trx, data) => {
|
|
let result = [];
|
|
// if (data.length > 0) {
|
|
// result = await trx('payment.t_d_cash_in_detail').insert(data, ['*']);
|
|
// }
|
|
|
|
if (data) {
|
|
result = await trx('payment.t_d_cash_in_detail').insert(data, ['*']);
|
|
}
|
|
|
|
return result;
|
|
};
|
|
|
|
const updateIntoReserve = async (trx, dataReserve, reserveNo) => {
|
|
let result = await trx('trx.t_d_reservation_balance')
|
|
.update(dataReserve, ['*'])
|
|
.where({ c_reservation_no: reserveNo });
|
|
return result;
|
|
};
|
|
|
|
const updateIntoReserveBulk = async (trx, dataReserve, reserveNos) => {
|
|
if (!Array.isArray(reserveNos) || reserveNos.length === 0) {
|
|
return 0;
|
|
}
|
|
|
|
return trx('trx.t_d_reservation_balance')
|
|
.whereIn('c_reservation_no', reserveNos)
|
|
.update(dataReserve);
|
|
};
|
|
|
|
const updateIntoSalesKMT = async (trx, dataSales, salesNo) => {
|
|
let result = await trx('kmt_mobile.t_d_sales_kmt_mobile')
|
|
.update(dataSales, ['*'])
|
|
.where({ c_sales_no: salesNo });
|
|
return result;
|
|
};
|
|
|
|
const updateIntoSalesKMTBulk = async (trx, dataSales, salesNos) => {
|
|
if (!Array.isArray(salesNos) || salesNos.length === 0) {
|
|
return 0;
|
|
}
|
|
|
|
return trx('kmt_mobile.t_d_sales_kmt_mobile')
|
|
.whereIn('c_sales_no', salesNos)
|
|
.update(dataSales);
|
|
};
|
|
|
|
const updateIntoSalesInternal = async (trx, dataSales, salesNo) => {
|
|
let result = await trx('trx.t_d_sales_internal')
|
|
.update(dataSales, ['*'])
|
|
.where({ c_sales_no: salesNo });
|
|
return result;
|
|
};
|
|
|
|
const updateIntoSalesInternalBulk = async (trx, dataSales, salesNos) => {
|
|
if (!Array.isArray(salesNos) || salesNos.length === 0) {
|
|
return 0;
|
|
}
|
|
|
|
return trx('trx.t_d_sales_internal')
|
|
.whereIn('c_sales_no', salesNos)
|
|
.update(dataSales);
|
|
};
|
|
|
|
const updateIntoTopUpCashlessInternal = async (trx, dataTopup, topupNo) => {
|
|
let result = await trx('trx.t_d_topup_cashless_internal')
|
|
.update(dataTopup, ['*'])
|
|
.where({ c_topup_no: topupNo });
|
|
return result;
|
|
};
|
|
|
|
const updateIntoTopUpCashlessInternalBulk = async (
|
|
trx,
|
|
dataTopup,
|
|
topupNos
|
|
) => {
|
|
if (!Array.isArray(topupNos) || topupNos.length === 0) {
|
|
return 0;
|
|
}
|
|
|
|
return trx('trx.t_d_topup_cashless_internal')
|
|
.whereIn('c_topup_no', topupNos)
|
|
.update(dataTopup);
|
|
};
|
|
|
|
const insertCashIn = async (trx, data) => {
|
|
let result = await trx('payment.t_d_cash_in').insert(data, ['*']);
|
|
return result[0];
|
|
};
|
|
|
|
const insertCashInDetailBulk = async (trx, data) => {
|
|
if (!Array.isArray(data) || data.length === 0) {
|
|
return 0;
|
|
}
|
|
|
|
return trx('payment.t_d_cash_in_detail').insert(data);
|
|
};
|
|
|
|
const writePaynentHistory = async (data, message, i_log_name, trx) => {
|
|
const result = await trx('log.t_log_invoice').insert(
|
|
{
|
|
// i_log_name : 102,
|
|
i_log_name: 119,
|
|
d_log: trx.raw('NOW()'),
|
|
c_invoice_no: data.c_cash_in_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,
|
|
m_mdr_amount: 0,
|
|
m_mdr_net: data.amount,
|
|
},
|
|
['i_log_name', 'c_note']
|
|
);
|
|
|
|
return result;
|
|
};
|
|
|
|
module.exports = {
|
|
generateInvoice,
|
|
insertPostingEt,
|
|
getPostingEt,
|
|
claimCashInPosting,
|
|
getAcceptedCashInPostings,
|
|
finishCashInPosting,
|
|
releaseUnsentCashInPosting,
|
|
recordCashInOutcome,
|
|
writePaynentHistory,
|
|
getRecon,
|
|
generateCode,
|
|
insertCashInDetail,
|
|
getPartnerRecon,
|
|
getPartnerReconSales,
|
|
getPartnerReconSalesInternal,
|
|
getPartnerReconTopUpCashlessInternal,
|
|
getReserve,
|
|
getSalesKMT,
|
|
getSalesInternal,
|
|
getTopUpCashlessInternal,
|
|
insertCashIn,
|
|
insertCashInDetailBulk,
|
|
updateIntoReserve,
|
|
updateIntoReserveBulk,
|
|
updateIntoSalesKMT,
|
|
updateIntoSalesKMTBulk,
|
|
updateIntoSalesInternal,
|
|
updateIntoSalesInternalBulk,
|
|
updateIntoTopUpCashlessInternal,
|
|
updateIntoTopUpCashlessInternalBulk,
|
|
};
|