+ $amount, $amount_allocated, $current_allocated, $ref);
+ }
+
+ //
+ // Read payment or credit current/available allocations to cart.
+ //
+ // FIXME - read all transactions below twice seems to be suboptimal
+ //
+ function read($type = null, $trans_no = 0)
+ {
+ if ($type == null) { // re-read
+ $type = $this->type;
+ $trans_no = $this->trans_no;
+ }
+ if ($type == ST_BANKPAYMENT || $type == ST_BANKDEPOSIT) {
+ $bank_trans = db_fetch(get_bank_trans($type, $trans_no));
+ $this->person_type = $bank_trans['person_type_id'] == PT_SUPPLIER;
+ } else
+ $this->person_type = $type == ST_SUPPCREDIT || $type == ST_SUPPAYMENT;
+ $this->allocs = array();
+
+ if ($trans_no) {
+ $trans = $this->person_type ? get_supp_trans($trans_no, $type)
+ : get_customer_trans($trans_no, $type);
+
+ $this->person_id = $trans[$this->person_type ? 'supplier_id':'debtor_no'];
+ $this->person_name = $trans[$this->person_type ? "supplier_name":"DebtorName"];
+ $this->amount = $trans["Total"];
+ $this->date_ = sql2date($trans["tran_date"]);
+ }
+ else {
+ $this->person_id = get_post($this->person_type ? 'supplier_id':'customer_id');
+ $this->date_ = get_post($this->person_type ? 'DatePaid':'DateBanked', Today());
+ }
+
+ /* Now populate the array of possible (and previous actual) allocations
+ for this customer/supplier. First get the transactions that have
+ outstanding balances ie Total-alloc >0 */
+
+ if ($this->person_type)
+ $trans_items = get_allocatable_to_supp_transactions($this->person_id);
+ else
+ $trans_items = get_allocatable_to_cust_transactions($this->person_id);
+
+ while ($myrow = db_fetch($trans_items))
+ {
+ $this->add_item($myrow["type"], $myrow["trans_no"],
+ sql2date($myrow["tran_date"]),
+ sql2date($myrow["due_date"]),
+ $myrow["Total"], // trans total
+ $myrow["alloc"], // trans total allocated
+ 0,
+ $myrow["reference"]); // this allocation
+ }
+ if ($trans_no == 0) return; // this is new payment
+
+ /* Now get trans that might have previously been allocated to by this trans
+ NB existing entries where still some of the trans outstanding entered from
+ above logic will be overwritten with the prev alloc detail below */
+
+ if ($this->person_type)
+ $trans_items = get_allocatable_to_supp_transactions($this->person_id,
+ $trans_no, $type);
+ else
+ $trans_items = get_allocatable_to_cust_transactions($this->person_id,
+ $trans_no, $type);
+
+ while ($myrow = db_fetch($trans_items))
+ {
+ $this->add_or_update_item ($myrow["type"], $myrow["trans_no"],
+ sql2date($myrow["tran_date"]),
+ sql2date($myrow["due_date"]),
+ $myrow["Total"],
+ $myrow["alloc"] - $myrow["amt"], $myrow["amt"], $myrow["reference"]);
+ }
+ }
+ //
+ // Update allocations in database.
+ //
+ function write()
+ {
+ begin_transaction();
+
+ if ($this->person_type)
+ clear_supp_alloctions($this->type, $this->trans_no, $this->date_);
+ else
+ clear_cust_alloctions($this->type, $this->trans_no, $this->date_);
+
+ // now add the new allocations
+ $total_allocated = 0;
+ foreach ($this->allocs as $alloc_item)
+ {
+ if ($alloc_item->current_allocated > 0)
+ {
+ if ($this->person_type) {
+ add_supp_allocation($alloc_item->current_allocated,
+ $this->type, $this->trans_no,
+ $alloc_item->type, $alloc_item->type_no, $this->date_);
+
+ update_supp_trans_allocation($alloc_item->type,
+ $alloc_item->type_no, $alloc_item->current_allocated);
+ } else {
+ add_cust_allocation($alloc_item->current_allocated,
+ $this->type, $this->trans_no,
+ $alloc_item->type, $alloc_item->type_no, $this->date_);
+
+ update_debtor_trans_allocation($alloc_item->type,
+ $alloc_item->type_no, $alloc_item->current_allocated);
+ }
+ // Exchange Variations Joe Hunt 2008-09-20 ////////////////////
+ exchange_variation($this->type, $this->trans_no,
+ $alloc_item->type, $alloc_item->type_no, $this->date_,
+ $alloc_item->current_allocated,
+ $this->person_type ? PT_SUPPLIER : PT_CUSTOMER);
+
+
+ //////////////////////////////////////////////////////////////
+ $total_allocated += $alloc_item->current_allocated;
+ }
+
+ } /*end of the loop through the array of allocations made */
+ if ($this->person_type)
+ update_supp_trans_allocation($this->type, $this->trans_no,
+ $total_allocated);
+ else
+ update_debtor_trans_allocation($this->type, $this->trans_no,
+ $total_allocated);
+
+ commit_transaction();
+
+ }