- // clear any allocations for this transaction
- $sql = "SELECT * FROM ".TB_PREF."cust_allocations
- WHERE (trans_type_from=".db_escape($type)." AND trans_no_from=".db_escape($type_no).")
- OR (trans_type_to=".db_escape($type)." AND trans_no_to=".db_escape($type_no).")";
- $result = db_query($sql, "could not void debtor transactions for type=$type and trans_no=$type_no");
-
- while ($row = db_fetch($result))
- {
- $sql = "UPDATE ".TB_PREF."debtor_trans SET alloc=alloc - " . $row['amt'] . "
- WHERE (type= " . $row['trans_type_from'] . " AND trans_no=" . $row['trans_no_from'] . ")
- OR (type=" . $row['trans_type_to'] . " AND trans_no=" . $row['trans_no_to'] . ")";
- db_query($sql, "could not clear allocation");
- // 2008-09-20 Joe Hunt
- if ($date != "")
- exchange_variation($type, $type_no, $row['trans_type_to'], $row['trans_no_to'], $date,
- $row['amt'], PT_CUSTOMER, true);
- // 2014-01-16 Joe Hunt. Fix for voiding allocated currency sales_invoices.
- elseif ($type == ST_SALESINVOICE && $row['trans_type_to'] == $type && $row['trans_no_to'] == $type_no)
- exchange_variation($row['trans_type_from'], $row['trans_no_from'], $row['trans_type_to'], $row['trans_no_to'],
- sql2date($row['date_alloc']), $row['amt'], PT_CUSTOMER, true);
- //////////////////////
- }
-
+ $sql = "UPDATE ".TB_PREF."cust_allocations ca
+ LEFT JOIN ".TB_PREF."debtor_trans paym ON ca.trans_type_from=paym.type AND ca.trans_no_from=paym.trans_no AND ca.person_id=paym.debtor_no
+ LEFT JOIN ".TB_PREF."debtor_trans dt ON ca.trans_type_to=dt.type AND ca.trans_no_to=dt.trans_no AND ca.person_id=dt.debtor_no
+ LEFT JOIN ".TB_PREF."sales_orders so ON ca.trans_type_to=so.trans_type AND ca.trans_no_to=so.order_no AND ca.person_id=so.debtor_no
+ SET paym.alloc=paym.alloc - ca.amt,
+ dt.alloc=dt.alloc - ca.amt,
+ so.alloc=so.alloc - ca.amt
+ WHERE ((ca.trans_type_from=".db_escape($type)." AND ca.trans_no_from=".db_escape($type_no).")
+ OR (ca.trans_type_to=".db_escape($type)." AND ca.trans_no_to=".db_escape($type_no)."))";
+ if ($person_id)
+ $sql .= " AND ca.person_id=".db_escape($person_id);
+ db_query($sql, "could not clear allocation");