From e1af54e851ed1ac5e54053da49225696d5f3c113 Mon Sep 17 00:00:00 2001 From: Mohamad Ihsan Date: Fri, 1 May 2026 18:02:59 +0700 Subject: [PATCH] bug fixed get partner recon --- application/modules/cashin/cahsin.model.js | 324 ++++++++++++--------- 1 file changed, 188 insertions(+), 136 deletions(-) diff --git a/application/modules/cashin/cahsin.model.js b/application/modules/cashin/cahsin.model.js index f12b950..1adb2a8 100644 --- a/application/modules/cashin/cahsin.model.js +++ b/application/modules/cashin/cahsin.model.js @@ -300,10 +300,10 @@ const getReserve = async (trx, code, date, invoice) => { .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)` - // ); - + // .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; }; @@ -475,66 +475,79 @@ const getPartnerRecon = async (trx) => { // .toSql() const res = await trx - .select([ + .distinct([ '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'), + + trx.raw(` + TO_CHAR(tdpr.d_payment, 'YYYY-MM-DD') AS d_payment + `), + + // jangan SUM karena duplicate row identik + 'tdrb.i_reservation_amount as amount', + '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`), + + 'tdi.i_company', + 'tdi.i_holding', + + trx.raw(` + CASE + WHEN DATE_TRUNC('month', tdpr.d_payment) + = DATE_TRUNC('month', tdi.d_invoice_date) + THEN 'IP' + ELSE 'OP' + END AS 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( + 'trx.t_d_load_balance as tdlb', + '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') + .andOnVal('tdpr.c_status', '=', 'S'); + } + ) + + .innerJoin( + 'public.t_m_partner as tmp', + '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('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)' - // ); + .whereNull('tdrb.c_cash_in_no'); + // .havingRaw( + // 'coalesce(SUM(tdrb.i_reservation_amount),0) > coalesce(SUM(tdcid.m_amount),0)' + // ); return res; }; @@ -542,88 +555,112 @@ const getPartnerRecon = async (trx) => { const getPartnerReconSales = async (trx) => { const res = await trx - .select([ + .distinct([ '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'), + + trx.raw(` + TO_CHAR(tdpr.d_payment, 'YYYY-MM-DD') AS d_payment + `), + + // jangan SUM karena duplicate row identik + 'tdskm.i_total_billing 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; + trx.raw(` + CASE + WHEN DATE_TRUNC('month', tdpr.d_payment) + = DATE_TRUNC('month', tdsi.d_invoice_date) + THEN 'IP' + ELSE 'OP' + END AS c_in_period + `), + ]) + + .from('kmt_mobile.t_d_sales_kmt_mobile AS tdskm') + + .innerJoin( + 'payment.t_d_sales_invoice as tdsi', + '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') + .andOnVal('tdpr.c_status', '=', 'S'); + } + ) + + .innerJoin( + 'public.t_m_partner as tmp', + 'tmp.i_partner', + 'tdskm.i_partner' + ) + + .where('tdskm.c_sales_status', 'A') + .where('tdsi.c_status', '!=', 'X') + .whereNotNull('tdsi.c_return_sap') + .whereNull('tdskm.c_cash_in_no'); }; const getPartnerReconSalesInternal = async (trx) => { const res = await trx - .select([ + .distinct([ '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'), + trx.raw(`TO_CHAR(tdpr.d_payment, 'YYYY-MM-DD') AS d_payment`), + + // jangan SUM karena row duplicate identik + 'tdskm.i_total_price 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`), + + trx.raw(` + CASE + WHEN DATE_TRUNC('month', tdpr.d_payment) + = DATE_TRUNC('month', tdsi.d_invoice_date) + THEN 'IP' + ELSE 'OP' + END AS 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'); - }) + + .innerJoin( + 'payment.t_d_sales_invoice_internal as tdsi', + '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') + .andOnVal('tdpr.c_status', '=', 'S'); + } + ) + + .innerJoin( + 'public.t_m_partner as tmp', + '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' - ]); + .whereNull('tdskm.i_cash_in'); return res; }; @@ -631,43 +668,58 @@ const getPartnerReconSalesInternal = async (trx) => { const getPartnerReconTopUpCashlessInternal = async (trx) => { const res = await trx - .select([ + .distinct([ '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'), + + trx.raw(` + TO_CHAR(tdpr.d_payment, 'YYYY-MM-DD') AS d_payment + `), + + // jangan SUM karena duplicate row identik + 'tdskm.i_amount 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`), + + trx.raw(` + CASE + WHEN DATE_TRUNC('month', tdpr.d_payment) + = DATE_TRUNC('month', tdsi.d_invoice_date) + THEN 'IP' + ELSE 'OP' + END AS 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'); - }) + + .innerJoin( + 'payment.t_d_topup_cashless_invoice_internal as tdsi', + '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') + .andOnVal('tdpr.c_status', '=', 'S'); + } + ) + + .innerJoin( + 'public.t_m_partner as tmp', + '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' - ]); + .whereNull('tdskm.i_cash_in'); return res; };