The document contains SQL commands for updating customer payment and invoice debt records based on specific transaction numbers. It calculates applied amounts, updates payment statuses, and adjusts debt amounts for invoices based on payment allocations. The final commands synchronize the status of the payment and allocation records in the database.
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 ratings0% found this document useful (0 votes)
11 views3 pages
Update Sodu 33968
The document contains SQL commands for updating customer payment and invoice debt records based on specific transaction numbers. It calculates applied amounts, updates payment statuses, and adjusts debt amounts for invoices based on payment allocations. The final commands synchronize the status of the payment and allocation records in the database.
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/ 3
Begin;
--select * from customer_payment_allocation where orc_transaction_number
in('2063275','2063180','2063280') and customer_payment_id= 33968 ; 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'); --update lai so tien hoa don 56487 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 ('2063280') ) paid where paid.group_id = cid2.group_id and paid.transaction_number = cid2.transaction_number and cid2.transaction_number in ('2063280'); update customer_payment set orc_sync_status='SYNCHRONIZED' ,orc_receipt_number='9006079' where id =33968; update customer_payment_allocation set orc_sync_status='SYNCHRONIZED' where customer_payment_id =33968; Commit;