+ $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, $person_id=null, $person_type_id=null)
+ {
+ if ($type !== null) { // otherwise re-read
+ $type = $this->type;
+ $trans_no = $this->trans_no;
+
+ if (isset($person_type_id))
+ {
+ $this->person_type = $person_type_id;
+ $this->person_id = $person_id;
+ } else { // guess person_type_id
+ if (in_array($type, array(ST_BANKPAYMENT, ST_BANKDEPOSIT)))
+ {
+ $bank_trans = db_fetch(get_bank_trans($type, $trans_no));
+ $this->person_type = $bank_trans['person_type_id'];
+ } else
+ $this->person_type = in_array($type, array(ST_SUPPCREDIT, ST_SUPPAYMENT)) ? PT_SUPPLIER : PT_CUSTOMER;
+ }
+
+ if ($trans_no) {
+ $trans = $this->person_type == PT_SUPPLIER ? get_supp_trans($trans_no, $type, $person_id)
+ : get_customer_trans($trans_no, $type, $person_id);
+
+ $this->person_id = $trans[$this->person_type == PT_SUPPLIER ? 'supplier_id':'debtor_no'];
+ $this->person_name = $trans[$this->person_type == PT_SUPPLIER ? "supplier_name":"DebtorName"];
+ $this->date_ = sql2date($trans["tran_date"]);
+ $this->person_curr = $trans['curr_code'];
+ $this->currency = isset($trans['bank_curr_code']) ? $trans['bank_curr_code'] : $trans['curr_code'];
+ $this->bank_amount = isset($trans["bank_amount"]) ? $trans["bank_amount"] : $trans["Total"]; // not set for journal entry
+ $this->amount = $trans["Total"];
+ }
+ }
+ /* 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 */
+
+ $this->allocs = array();
+ if ($this->person_id)
+ {
+ if ($this->person_type==PT_SUPPLIER)
+ $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 ($this->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==PT_SUPPLIER)
+ $trans_items = get_allocatable_to_supp_transactions($this->person_id,
+ $this->trans_no, $this->type);
+ else
+ $trans_items = get_allocatable_to_cust_transactions($this->person_id,
+ $this->trans_no, $this->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()
+ {
+ global $no_exchange_variations;
+
+ begin_transaction();
+
+ if ($this->person_type == PT_SUPPLIER)
+ clear_supp_alloctions($this->type, $this->trans_no, $this->person_id, $this->date_);
+ else
+ clear_cust_alloctions($this->type, $this->trans_no, $this->person_id, $this->date_);
+
+ // now add the new allocations
+ $total_allocated = 0;
+ $dec = user_price_dec();
+ foreach ($this->allocs as $alloc_item)
+ {
+ if ($alloc_item->current_allocated > 0)
+ {
+ $amount = round($alloc_item->current_allocated, $dec);
+
+ if ($this->person_type == PT_SUPPLIER) {
+ add_supp_allocation($amount,
+ $this->type, $this->trans_no,
+ $alloc_item->type, $alloc_item->type_no, $this->person_id, $this->date_);
+
+ update_supp_trans_allocation($alloc_item->type, $alloc_item->type_no, $this->person_id);
+ } else {
+ add_cust_allocation($amount,
+ $this->type, $this->trans_no,
+ $alloc_item->type, $alloc_item->type_no, $this->person_id, $this->date_);
+
+ update_debtor_trans_allocation($alloc_item->type, $alloc_item->type_no, $this->person_id);
+ }
+ // Exchange Variations Joe Hunt 2008-09-20 ////////////////////
+ if ($alloc_item->type != ST_SALESORDER && !@$no_exchange_variations
+ && $alloc_item->type != ST_PURCHORDER && $alloc_item->type != ST_JOURNAL && $this->type != ST_JOURNAL)
+ exchange_variation($this->type, $this->trans_no,
+ $alloc_item->type, $alloc_item->type_no, $this->date_,
+ $amount, $this->person_type);
+
+ //////////////////////////////////////////////////////////////
+ $total_allocated += $alloc_item->current_allocated;
+ }
+
+ } /*end of the loop through the array of allocations made */
+ if ($this->person_type == PT_SUPPLIER)
+ update_supp_trans_allocation($this->type, $this->trans_no, $this->person_id);
+ else
+ update_debtor_trans_allocation($this->type, $this->trans_no, $this->person_id);
+
+ commit_transaction();
+
+ }