0% found this document useful (0 votes)
26 views1 page

Update Sodu 1876677

The document contains a SQL transaction that updates the 'customer_invoice_debt' table by recalculating the 'debt_amount' and 'debt_amount_vnd' fields based on the unpaid invoice amounts and payments made. It uses a subquery to sum the applied payment amounts for specific transactions while considering various conditions. The transaction is executed for a specific transaction number '1876677'.

Uploaded by

anhbh2012
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views1 page

Update Sodu 1876677

The document contains a SQL transaction that updates the 'customer_invoice_debt' table by recalculating the 'debt_amount' and 'debt_amount_vnd' fields based on the unpaid invoice amounts and payments made. It uses a subquery to sum the applied payment amounts for specific transactions while considering various conditions. The transaction is executed for a specific transaction number '1876677'.

Uploaded by

anhbh2012
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

BEGIN;

update customer_invoice_debt cid2


set debt_amount =
(case
when cid2.invoice_unpaid_amount is null or cid2.invoice_unpaid_amount = 0 then 0
else ((coalesce (cid2.invoice_unpaid_amount, 0) - coalesce(paid.paid_amount,
0))/coalesce(cid2.exchange_rate, 1))
end),
debt_amount_vnd =
(case
when cid2.invoice_unpaid_amount is null or cid2.invoice_unpaid_amount = 0 then 0
else coalesce (cid2.invoice_unpaid_amount, 0) - coalesce(paid.paid_amount, 0)
end)
from(
select cid.group_id, cid.transaction_number,
(select sum(coalesce(cpa.apply_amount_invoice_currency,0)*
coalesce(cpa.currency_rate, 1)) from customer_payment_allocation cpa
inner join customer_payment cp
on cpa.customer_payment_id = cp.id
where
(cp.is_reverse is null or cp.is_reverse != true)
and cpa.invoice_no = cid.invoice_finance_number
and cpa.orc_transaction_number = cid.transaction_number
and ((cid.modified_time is not null and cpa.created_time > cid.modified_time)
or (cid.modified_time is null and cid.created_time is not null and
cpa.created_time > cid.created_time)
or (cpa.orc_sync_status != 'SYNCHRONIZED')
)
) as paid_amount
from customer_invoice_debt cid where cid.transaction_number in ('1876677')
) paid
where paid.group_id = cid2.group_id
and paid.transaction_number = cid2.transaction_number
and cid2.transaction_number in ('1876677');
COMMIT;

You might also like