2 /**********************************************************************
3 Copyright (C) FrontAccounting, LLC.
4 Released under the terms of the GNU General Public License, GPL,
5 as published by the Free Software Foundation, either version 3
6 of the License, or (at your option) any later version.
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
13 Class for supplier/customer payment/credit allocations edition
16 //-----------------------------------------------------------------------------------
24 var $person_name = '';
26 var $amount = 0; /*Total amount of the transaction in FX */
28 var $allocs; /*array of transactions allocated to */
30 function allocation($type, $trans_no)
32 $this->allocs = array();
34 $this->trans_no = $trans_no;
36 $this->read(); // read payment or credit
39 function add_item($type, $type_no, $date_, $due_date, $amount, $amount_allocated,
44 $this->allocs[count($this->allocs)] = new allocation_item($type, $type_no,
45 $date_, $due_date, $amount, $amount_allocated, $current_allocated);
54 function update_item($index, $type, $type_no, $date_, $due_date,
55 $amount, $amount_allocated, $current_allocated)
59 $this->allocs[$index] = new allocation_item($type, $type_no,
60 $date_, $due_date, $amount, $amount_allocated, $current_allocated);
69 function add_or_update_item($type, $type_no, $date_, $due_date,
70 $amount, $amount_allocated, $current_allocated)
72 for ($i = 0; $i < count($this->allocs); $i++)
74 $item = $this->allocs[$i];
75 if (($item->type == $type) && ($item->type_no == $type_no))
77 return $this->update_item($i, $type, $type_no, $date_, $due_date,
78 $amount, $amount_allocated, $current_allocated);
81 return $this->add_item($type, $type_no, $date_, $due_date,
82 $amount, $amount_allocated, $current_allocated);
86 // Read payment or credit current/available allocations to cart.
88 function read($type = null, $trans_no = 0)
90 if ($type == null) { // re-read
92 $trans_no = $this->trans_no;
95 $sup = $type == ST_SUPPCREDIT || $type == ST_SUPPAYMENT;
96 $this->allocs = array();
99 $trans = $sup ? get_supp_trans($trans_no, $type)
100 : get_customer_trans($trans_no, $type);
102 $this->person_id = $trans[$sup ? 'supplier_id':'debtor_no'];
103 $this->person_name = $trans[$sup ? "supplier_name":"DebtorName"];
104 $this->amount = $trans["Total"];
105 $this->date_ = sql2date($trans["tran_date"]);
108 $this->person_id = get_post($sup ? 'supplier_id':'customer_id');
109 $this->date_ = get_post($sup ? 'DatePaid':'DateBanked', Today());
112 /* Now populate the array of possible (and previous actual) allocations
113 for this customer/supplier. First get the transactions that have
114 outstanding balances ie Total-alloc >0 */
117 $trans_items = get_allocatable_to_supp_transactions($this->person_id);
119 $trans_items = get_allocatable_to_cust_transactions($this->person_id);
121 while ($myrow = db_fetch($trans_items))
123 $this->add_item($myrow["type"], $myrow["trans_no"],
124 sql2date($myrow["tran_date"]),
125 sql2date($myrow["due_date"]),
126 $myrow["Total"], // trans total
127 $myrow["alloc"], // trans total allocated
128 0); // this allocation
131 if ($trans_no == 0) return; // this is new payment
133 /* Now get trans that might have previously been allocated to by this trans
134 NB existing entries where still some of the trans outstanding entered from
135 above logic will be overwritten with the prev alloc detail below */
138 $trans_items = get_allocatable_to_supp_transactions($this->person_id,
141 $trans_items = get_allocatable_to_cust_transactions($this->person_id,
144 while ($myrow = db_fetch($trans_items))
146 $this->add_or_update_item ($myrow["type"], $myrow["trans_no"],
147 sql2date($myrow["tran_date"]),
148 sql2date($myrow["due_date"]),
150 $myrow["alloc"] - $myrow["amt"], $myrow["amt"]);
154 // Update allocations in database.
158 $sup = $this->type == ST_SUPPCREDIT || $this->type == ST_SUPPAYMENT;
163 clear_supp_alloctions($this->type, $this->trans_no, $this->date_);
165 clear_cust_alloctions($this->type, $this->trans_no, $this->date_);
167 // now add the new allocations
168 $total_allocated = 0;
169 foreach ($this->allocs as $alloc_item)
171 if ($alloc_item->current_allocated > 0)
174 add_supp_allocation($alloc_item->current_allocated,
175 $this->type, $this->trans_no,
176 $alloc_item->type, $alloc_item->type_no, $this->date_);
178 update_supp_trans_allocation($alloc_item->type,
179 $alloc_item->type_no, $alloc_item->current_allocated);
181 add_cust_allocation($alloc_item->current_allocated,
182 $this->type, $this->trans_no,
183 $alloc_item->type, $alloc_item->type_no, $this->date_);
185 update_debtor_trans_allocation($alloc_item->type,
186 $alloc_item->type_no, $alloc_item->current_allocated);
188 // Exchange Variations Joe Hunt 2008-09-20 ////////////////////
190 exchange_variation($this->type, $this->trans_no,
191 $alloc_item->type, $alloc_item->type_no, $this->date_,
192 $alloc_item->current_allocated,
197 //////////////////////////////////////////////////////////////
198 $total_allocated += $alloc_item->current_allocated;
201 } /*end of the loop through the array of allocations made */
203 update_supp_trans_allocation($this->type, $this->trans_no,
206 update_debtor_trans_allocation($this->type, $this->trans_no,
209 commit_transaction();
215 //-----------------------------------------------------------------------------------
217 class allocation_item
226 var $amount_allocated;
229 var $current_allocated;
231 function allocation_item ($type, $type_no, $date_, $due_date, $amount,
232 $amount_allocated, $current_allocated)
236 $this->type_no = $type_no;
238 $this->date_ = $date_;
239 $this->due_date = $due_date;
241 $this->amount = $amount;
242 $this->amount_allocated = $amount_allocated;
243 $this->current_allocated = $current_allocated;
247 //--------------------------------------------------------------------------------
249 function show_allocatable($show_totals) {
251 global $table_style, $systypes_array;
253 $k = $counter = $total_allocated = 0;
255 if (count($_SESSION['alloc']->allocs))
257 start_table("$table_style width=60%");
258 $th = array(_("Transaction Type"), _("#"), _("Date"), _("Due Date"), _("Amount"),
259 _("Other Allocations"), _("This Allocation"), _("Left to Allocate"),'','');
262 foreach ($_SESSION['alloc']->allocs as $alloc_item)
264 alt_table_row_color($k);
265 label_cell($systypes_array[$alloc_item->type]);
266 label_cell(get_trans_view_str($alloc_item->type, $alloc_item->type_no));
267 label_cell($alloc_item->date_, "align=right");
268 label_cell($alloc_item->due_date, "align=right");
269 amount_cell($alloc_item->amount);
270 amount_cell($alloc_item->amount_allocated);
272 $_POST['amount' . $counter] = price_format($alloc_item->current_allocated);
273 amount_cells(null, "amount" . $counter, price_format('amount' . $counter));
275 $un_allocated = round($alloc_item->amount - $alloc_item->amount_allocated, 6);
276 amount_cell($un_allocated);
277 label_cell("<a href='#' name=Alloc$counter onclick='allocate_all(this.name.substr(5));return true;'>"
278 . _("All") . "</a>");
279 label_cell("<a href='#' name=DeAll$counter onclick='allocate_none(this.name.substr(5));return true;'>"
280 . _("None") . "</a>".hidden("un_allocated" . $counter,
281 price_format($un_allocated), false));
284 $total_allocated += input_num('amount' . $counter);
288 label_row(_("Total Allocated"), price_format($total_allocated),
289 "colspan=6 align=right", "align=right id='total_allocated'", 3);
290 $amount = $_SESSION['alloc']->amount;
292 if ($_SESSION['alloc']->type == 21 || $_SESSION['alloc']->type == 22)
295 if ($amount - $total_allocated < 0)
297 $font1 = "<font color=red>";
301 $font1 = $font2 = "";
302 $left_to_allocate = price_format($amount - $total_allocated);
303 label_row(_("Left to Allocate"), $font1 . $left_to_allocate . $font2,
304 "colspan=6 align=right", "nowrap align=right id='left_to_allocate'",
309 hidden('TotalNumberOfAllocs', $counter);
311 //--------------------------------------------------------------------------------
313 function check_allocations()
317 $total_allocated = 0;
319 for ($counter = 0; $counter < $_POST["TotalNumberOfAllocs"]; $counter++)
321 if (!check_num('amount' . $counter, 0))
323 display_error(_("The entry for one or more amounts is invalid or negative."));
324 set_focus('amount'.$counter);
328 /*Now check to see that the AllocAmt is no greater than the
329 amount left to be allocated against the transaction under review */
330 if (input_num('amount' . $counter) > get_post('un_allocated' . $counter))
332 //$_POST['amount' . $counter] = $_POST['un_allocated' . $counter];
335 $_SESSION['alloc']->allocs[$counter]->current_allocated = input_num('amount' . $counter);
337 $total_allocated += input_num('amount' . $counter);
340 $amount = $_SESSION['alloc']->amount;
341 if ($_SESSION['alloc']->type == 21 || $_SESSION['alloc']->type == 22)
344 if ($total_allocated - ($amount + input_num('discount')) > $SysPrefs->allocation_settled_allowance())
346 display_error(_("These allocations cannot be processed because the amount allocated is more than the total amount left to allocate."));