X-Git-Url: https://delta.frontaccounting.com/gitweb/?a=blobdiff_plain;f=includes%2Fdb%2Fallocations_db.inc;h=3362f3109430f14e5ba03dd7ec519f22708c1c4a;hb=f11b39846d81bd043490ba9596224b859e47467c;hp=8e284321b9679b1b25d6726f0640d70a4b6e17be;hpb=020d309dc37e9400d27ef3cad381e6f1b04f67fd;p=fa-stable.git diff --git a/includes/db/allocations_db.inc b/includes/db/allocations_db.inc index 8e284321..3362f310 100644 --- a/includes/db/allocations_db.inc +++ b/includes/db/allocations_db.inc @@ -16,15 +16,17 @@ // // Returns table of payments currently allocated to selected transaction. // -function get_payments_for($trans_no, $trans_type) +function get_payments_for($trans_no, $trans_type, $person_id) { $allocations = array(); - $sql = "(SELECT * FROM ".TB_PREF."cust_allocations - WHERE (trans_type_to=".db_escape($trans_type)." AND trans_no_to=".db_escape($trans_no).")) + $sql = "(SELECT alloc.*, trans.tran_date FROM ".TB_PREF."cust_allocations alloc + LEFT JOIN ".TB_PREF."debtor_trans trans ON trans.type=alloc.trans_type_from AND trans.trans_no=alloc.trans_no_from + WHERE (trans_type_to=".db_escape($trans_type)." AND trans_no_to=".db_escape($trans_no)." AND person_id=".db_escape($person_id).")) UNION - (SELECT * FROM ".TB_PREF."supp_allocations - WHERE (trans_type_to=".db_escape($trans_type)." AND trans_no_to=".db_escape($trans_no)."))"; + (SELECT alloc.*, trans.tran_date FROM ".TB_PREF."supp_allocations alloc + LEFT JOIN ".TB_PREF."supp_trans trans ON trans.type=alloc.trans_type_from AND trans.trans_no=alloc.trans_no_from + WHERE (trans_type_to=".db_escape($trans_type)." AND trans_no_to=".db_escape($trans_no)." AND person_id=".db_escape($person_id)."))"; $result = db_query($sql, "error reading current allocations"); while($dat = db_fetch($result)) @@ -38,51 +40,32 @@ function get_payments_for($trans_no, $trans_type) // // Unified inteface to (re)allocate payments to supp/cust/transaction // -function allocate_payment($type_from, $no_from, $type_to, $no_to, $amount, $date) +function allocate_payment($type_from, $no_from, $type_to, $no_to, $person_id, $amount, $date) { $date = date2sql($date); // FIXME types if (in_array($type_to, array(ST_SALESORDER, ST_CUSTCREDIT, ST_CUSTDELIVERY, ST_SALESINVOICE))) - { $allocations = 'cust_allocations'; - $payments='debtor_trans'; - $transactions = $type_to == ST_SALESORDER ? 'sales_orders' : 'debtor_trans'; - $where = $type_to == ST_SALESORDER ? "order_no=".db_escape($no_to) : "type=".db_escape($type_to)." AND trans_no=".db_escape($no_to); - } else - { $allocations = 'supp_allocations'; - $payments='supp_trans'; - $transactions = $type_to == ST_PURCHORDER ? 'purch_orders' : 'supp_trans'; - $where = $type_to == ST_PURCHORDER ? "order_no=".db_escape($no_to) : "type=".db_escape($type_to)." AND trans_no=".db_escape($no_to); - } - // add/update/delete allocation + $sql = "DELETE FROM ".TB_PREF.$allocations." + WHERE trans_type_from=".db_escape($type_from)." AND trans_no_from=".db_escape($no_from) + ." AND trans_type_to=".db_escape($type_to)." AND trans_no_to=".db_escape($no_to)." AND person_id=".db_escape($person_id); + db_query($sql, "The existing allocations could not be updated"); if (floatcmp($amount, 0) != 0) { - $sql = "REPLACE ".TB_PREF.$allocations." SET amt = ". db_escape($amount).",date_alloc='$date'" - .",trans_type_from=".db_escape($type_from).",trans_no_from=".db_escape($no_from) - .",trans_type_to=".db_escape($type_to).",trans_no_to=".db_escape($no_to); + $sql = "INSERT INTO ".TB_PREF.$allocations." (amt, date_alloc, trans_type_from, trans_no_from, trans_type_to, trans_no_to, person_id) + VALUES (". db_escape($amount).",'$date',".db_escape($type_from).",".db_escape($no_from) + .",".db_escape($type_to).",".db_escape($no_to).",".db_escape($person_id).")"; + db_query($sql, "The existing allocations could not be updated"); } - else { - $sql = "DELETE FROM ".TB_PREF.$allocations." - WHERE trans_type_from=".db_escape($type_from)." AND trans_no_from=".db_escape($no_from) - ." AND trans_type_to=".db_escape($type_to)." AND trans_no_to=".db_escape($no_to); + if ($allocations == 'cust_allocations') { + update_debtor_trans_allocation($type_from, $no_from, $person_id); + update_debtor_trans_allocation($type_to, $no_to, $person_id); + } else { + update_supp_trans_allocation($type_from, $no_from, $person_id); + update_supp_trans_allocation($type_to, $no_to, $person_id); } - db_query($sql, "The existing allocations could not be updated"); - - // referesh allocation summaries - $sql = "UPDATE ".TB_PREF.$transactions." trans, - (SELECT SUM(amt) amt FROM ".TB_PREF.$allocations." WHERE trans_type_to=".db_escape($type_to)." AND trans_no_to=".db_escape($no_to).") paym - SET alloc = paym.amt - WHERE $where"; - db_query($sql, "The transaction allocations could not be updated"); - - $sql = "UPDATE ".TB_PREF.$payments." trans, - (SELECT SUM(amt) amt FROM ".TB_PREF.$allocations." WHERE trans_type_from=".db_escape($type_from)." AND trans_no_from=".db_escape($no_from).") paym - SET alloc = paym.amt - WHERE type=".db_escape($type_from)." AND trans_no=".db_escape($no_from); - - return db_query($sql, "The payment allocations could not be updated"); } //---------------------------------------------------------------------------------------- @@ -91,7 +74,7 @@ function allocate_payment($type_from, $no_from, $type_to, $no_to, $amount, $date // $allocs is table of payments which should be reallocated // $free_remainder should be true if old allacations should be freed when not allocated to new transaction. // -function reallocate_payments($trans_no, $trans_type, $alloc_date, $max_alloc, $allocs, $free_remainder=true) +function reallocate_payments($trans_no, $trans_type, $alloc_date, $max_alloc, $allocs, $person_id, $free_remainder=true) { foreach($allocs as $alloc) @@ -102,14 +85,13 @@ function reallocate_payments($trans_no, $trans_type, $alloc_date, $max_alloc, $a $max_alloc = floatcmp($max_alloc, $amount) > 0 ? $max_alloc-$amount : 0; $same_to = $trans_type == $alloc['trans_type_to'] && $trans_no == $alloc['trans_no_to']; - if (!$same_to || ($remainder > 0)) - { - allocate_payment($alloc['trans_type_from'], $alloc['trans_no_from'], $trans_type, $trans_no, $amount, $alloc_date); - } + + allocate_payment($alloc['trans_type_from'], $alloc['trans_no_from'], $trans_type, $trans_no, $person_id, $amount, $alloc_date); + if (!$same_to && ($remainder > 0 || $free_remainder)) { allocate_payment($alloc['trans_type_from'], $alloc['trans_no_from'], - $alloc['trans_type_to'], $alloc['trans_no_to'], $free_remainder ? 0 : $remainder, $alloc_date); + $alloc['trans_type_to'], $alloc['trans_no_to'], $person_id, $free_remainder ? 0 : $remainder, $alloc_date); } if (!$free_remainder && $max_alloc==0) break;