--todo 8527 (solved by HVH)

-- Script to correct data related to todo 8527
-- Step 1: Correct closing amount of the payment
update artransaction q set q.closingamount=q.amount
where q.artransaction_id in 
      (select distinct(p.artransaction_id)
          from artransaction a, artransaction b, artransaction p
         where a.closingtransaction_id = b.artransaction_id
           and b.artransactiontype_id = 1
           and p.artransaction_id = b.closingtransaction_id);
-- Step 2: Correct closing amount and closingtransaction of the invoices that were closed by another invoice.
update artransaction r
   set r.closingamount         = r.amount,
       r.closingtransaction_id = (select c.closingtransaction_id
                                    from artransaction c
                                   where c.artransaction_id =
                                         r.closingtransaction_id)
 where r.artransaction_id in
       (select distinct (a.artransaction_id)
          from artransaction a, artransaction b, artransaction p
         where a.closingtransaction_id = b.artransaction_id
           and b.artransactiontype_id = 1
           and p.artransaction_id = b.closingtransaction_id);