diff --git a/application/modules/cashin/cahsin.model.js b/application/modules/cashin/cahsin.model.js index 1adb2a8..f12b950 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,79 +475,66 @@ const getPartnerRecon = async (trx) => { // .toSql() const res = await trx - .distinct([ + .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 - `), - - // jangan SUM karena duplicate row identik - 'tdrb.i_reservation_amount as amount', - + 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', - - '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 - `), + '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', - '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('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('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'); - // .havingRaw( - // 'coalesce(SUM(tdrb.i_reservation_amount),0) > coalesce(SUM(tdcid.m_amount),0)' - // ); + .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; }; @@ -555,112 +542,88 @@ const getPartnerRecon = async (trx) => { const getPartnerReconSales = async (trx) => { const res = await trx - .distinct([ + .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 - `), - - // jangan SUM karena duplicate row identik - 'tdskm.i_total_billing as amount', - + 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 AS 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 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') + + .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'); + .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 - .distinct([ + .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`), - - // jangan SUM karena row duplicate identik - 'tdskm.i_total_price as amount', - + 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 'OP' - END AS c_in_period - `), + 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', - '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' - ) - + + .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'); + .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; }; @@ -668,58 +631,43 @@ const getPartnerReconSalesInternal = async (trx) => { const getPartnerReconTopUpCashlessInternal = async (trx) => { const res = await trx - .distinct([ + .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 - `), - - // jangan SUM karena duplicate row identik - 'tdskm.i_amount as amount', - + 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 'OP' - END AS c_in_period - `), + 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', - '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' - ) - + + .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'); + .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; };