0% found this document useful (0 votes)
27 views2 pages

Update Customer Payment Allocation 33968

The document contains SQL update statements for adjusting customer payment and invoice debt records. It calculates applied and not applied payment amounts, as well as updates the debt amounts for specific invoices based on payment allocations. The updates are conditional on various factors including unpaid amounts and transaction statuses.

Uploaded by

Bonbihandmade
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)
27 views2 pages

Update Customer Payment Allocation 33968

The document contains SQL update statements for adjusting customer payment and invoice debt records. It calculates applied and not applied payment amounts, as well as updates the debt amounts for specific invoices based on payment allocations. The updates are conditional on various factors including unpaid amounts and transaction statuses.

Uploaded by

Bonbihandmade
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/ 2

update customer_payment cp2

set amount_applied = coalesce(applied_amount, 0),


amount_not_applied = amount - amount_pending - coalesce(applied_amount, 0),
payment_pressure_status = (case
when (amount - amount_pending - coalesce(applied_amount, 0) > 0) and
(amount_applied > 0) then 'PARTIAL'
when (amount - amount_pending - coalesce(applied_amount, 0) = 0) and
(amount_applied > 0) then 'APPLIED'
else 'NOT_APPLIED'
end)
from (
select id,
(select sum(apply_amount) from customer_payment_allocation where
customer_payment_id = cp.id) as applied_amount
from customer_payment cp where cp.id =33968
) cpa
where cp2.id = cpa.id
and cp2.id =33968;
--update lai so tien hoa don 56315
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 ('2063180')
) paid
where paid.group_id = cid2.group_id
and paid.transaction_number = cid2.transaction_number
and cid2.transaction_number in ('2063180');
--update lai so tien hoa don 56485
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 ('2063275')
) paid
where paid.group_id = cid2.group_id
and paid.transaction_number = cid2.transaction_number
and cid2.transaction_number in ('2063275');

You might also like