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 ***********************************************************************/
12 /* definition of the Debtor Receipt/Credit note allocation class */
14 //-----------------------------------------------------------------------------------
24 var $amount; /*Total amount of the transaction in FX */
26 var $allocs; /*array of transactions allocated to */
28 function allocation($trans_no, $type, $person_id, $person_name, $amount, $date_)
30 $this->allocs = array();
32 $this->trans_no = $trans_no;
34 $this->person_id = $person_id;
35 $this->person_name = $person_name;
36 $this->amount = $amount;
37 $this->date_ = $date_;
40 function add_item($type, $type_no, $date_, $due_date, $amount, $amount_allocated,
45 $this->allocs[count($this->allocs)] = new allocation_item($type, $type_no,
46 $date_, $due_date, $amount, $amount_allocated, $current_allocated);
55 function update_item($index, $type, $type_no, $date_, $due_date,
56 $amount, $amount_allocated, $current_allocated)
60 $this->allocs[$index] = new allocation_item($type, $type_no,
61 $date_, $due_date, $amount, $amount_allocated, $current_allocated);
70 function add_or_update_item($type, $type_no, $date_, $due_date,
71 $amount, $amount_allocated, $current_allocated)
73 for ($i = 0; $i < count($this->allocs); $i++)
75 $item = $this->allocs[$i];
76 if (($item->type == $type) && ($item->type_no == $type_no))
78 return $this->update_item($i, $type, $type_no, $date_, $due_date,
79 $amount, $amount_allocated, $current_allocated);
82 return $this->add_item($type, $type_no, $date_, $due_date,
83 $amount, $amount_allocated, $current_allocated);
88 //-----------------------------------------------------------------------------------
99 var $amount_allocated;
102 var $current_allocated;
104 function allocation_item ($type, $type_no, $date_, $due_date, $amount,
105 $amount_allocated, $current_allocated)
109 $this->type_no = $type_no;
111 $this->date_ = $date_;
112 $this->due_date = $due_date;
114 $this->amount = $amount;
115 $this->amount_allocated = $amount_allocated;
116 $this->current_allocated = $current_allocated;
120 //--------------------------------------------------------------------------------
121 function get_allocations_for_transaction($type, $trans_no)
123 $sup = $type == 21 || $type == 22;
126 $trans = $sup ? get_supp_trans($trans_no, $type)
127 : get_customer_trans($trans_no, $type);
129 $_SESSION['alloc'] = new allocation($trans_no, $type,
130 $trans[$sup ? 'supplier_id':'debtor_no'],
131 $trans[$sup ? "supplier_name":"DebtorName"],
132 $trans["Total"], sql2date($trans["tran_date"]));
134 $_SESSION['alloc'] = new allocation($trans_no, $type,
135 $_POST[$sup ? 'supplier_id':'customer_id'], '', 0,
136 $_POST[$sup ? 'DatePaid':'DateBanked'] );
139 /* Now populate the array of possible (and previous actual) allocations for this customer/supplier */
140 /*First get the transactions that have outstanding balances ie Total-alloc >0 */
143 $trans_items = get_allocatable_to_supp_transactions($_SESSION['alloc']->person_id);
145 $trans_items = get_allocatable_to_cust_transactions($_SESSION['alloc']->person_id);
147 while ($myrow = db_fetch($trans_items))
149 $_SESSION['alloc']->add_item($myrow["type"], $myrow["trans_no"],
150 sql2date($myrow["tran_date"]),
151 sql2date($myrow["due_date"]),
152 $myrow["Total"], // trans total
153 $myrow["alloc"], // trans total allocated
154 0); // this allocation
157 if ($trans_no == 0) return; // this is new payment
159 /* Now get trans that might have previously been allocated to by this trans
160 NB existing entries where still some of the trans outstanding entered from
161 above logic will be overwritten with the prev alloc detail below */
164 $trans_items = get_allocatable_to_supp_transactions($_SESSION['alloc']->person_id, $trans_no, $type);
166 $trans_items = get_allocatable_to_cust_transactions($_SESSION['alloc']->person_id, $trans_no, $type);
168 while ($myrow = db_fetch($trans_items))
170 $_SESSION['alloc']->add_or_update_item ($myrow["type"], $myrow["trans_no"],
171 sql2date($myrow["tran_date"]),
172 sql2date($myrow["due_date"]),
174 $myrow["alloc"] - $myrow["amt"], $myrow["amt"]);
178 //---------------------------------------------------------------------------------------------------
180 function show_allocatable($show_totals) {
184 $k = $counter = $total_allocated = 0;
186 if (count($_SESSION['alloc']->allocs))
188 start_table($table_style);
189 $th = array(_("Transaction Type"), _("#"), _("Date"), _("Due Date"), _("Amount"),
190 _("Other Allocations"), _("This Allocation"), _("Left to Allocate"),'','');
193 foreach ($_SESSION['alloc']->allocs as $alloc_item)
195 alt_table_row_color($k);
196 label_cell(systypes::name($alloc_item->type));
197 label_cell(get_trans_view_str($alloc_item->type, $alloc_item->type_no));
198 label_cell($alloc_item->date_, "align=right");
199 label_cell($alloc_item->due_date, "align=right");
200 amount_cell($alloc_item->amount);
201 amount_cell($alloc_item->amount_allocated);
203 $_POST['amount' . $counter] = price_format($alloc_item->current_allocated);
204 amount_cells(null, "amount" . $counter, price_format('amount' . $counter));
206 $un_allocated = round($alloc_item->amount - $alloc_item->amount_allocated, 6);
207 amount_cell($un_allocated);
208 label_cell("<a href='#' name=Alloc$counter onclick='allocate_all(this.name.substr(5));return true;'>"
209 . _("All") . "</a>");
210 label_cell("<a href='#' name=DeAll$counter onclick='allocate_none(this.name.substr(5));return true;'>"
211 . _("None") . "</a>".hidden("un_allocated" . $counter,
212 price_format($un_allocated), false));
215 $total_allocated += input_num('amount' . $counter);
219 label_row(_("Total Allocated"), price_format($total_allocated),
220 "colspan=6 align=right", "align=right id='total_allocated'", 3);
221 $amount = $_SESSION['alloc']->amount;
223 if ($_SESSION['alloc']->type == 21 || $_SESSION['alloc']->type == 22)
226 if ($amount - $total_allocated < 0)
228 $font1 = "<font color=red>";
232 $font1 = $font2 = "";
233 $left_to_allocate = price_format($amount - $total_allocated);
234 label_row(_("Left to Allocate"), $font1 . $left_to_allocate . $font2,
235 "colspan=6 align=right", "nowrap align=right id='left_to_allocate'",
240 hidden('TotalNumberOfAllocs', $counter);
242 //--------------------------------------------------------------------------------
244 function check_allocations()
246 $total_allocated = 0;
248 for ($counter = 0; $counter < $_POST["TotalNumberOfAllocs"]; $counter++)
250 if (!check_num('amount' . $counter, 0))
252 display_error(_("The entry for one or more amounts is invalid or negative."));
253 set_focus('amount'.$counter);
257 /*Now check to see that the AllocAmt is no greater than the
258 amount left to be allocated against the transaction under review */
259 if (input_num('amount' . $counter) > get_post('un_allocated' . $counter))
261 //$_POST['amount' . $counter] = $_POST['un_allocated' . $counter];
264 $_SESSION['alloc']->allocs[$counter]->current_allocated = input_num('amount' . $counter);
266 $total_allocated += input_num('amount' . $counter);
269 $amount = $_SESSION['alloc']->amount;
270 if ($_SESSION['alloc']->type == 21 || $_SESSION['alloc']->type == 22)
273 if ($total_allocated - $amount > sys_prefs::allocation_settled_allowance())
275 display_error(_("These allocations cannot be processed because the amount allocated is more than the total amount left to allocate."));
281 //-----------------------------------------------------------------------------------
283 function handle_allocate()
285 $sup = $_SESSION['alloc']->type == 21 || $_SESSION['alloc']->type == 22;
290 clear_supp_alloctions($_SESSION['alloc']->type, $_SESSION['alloc']->trans_no, $_SESSION['alloc']->date_);
292 clear_cust_alloctions($_SESSION['alloc']->type, $_SESSION['alloc']->trans_no, $_SESSION['alloc']->date_);
294 // now add the new allocations
295 $total_allocated = 0;
296 foreach ($_SESSION['alloc']->allocs as $alloc_item)
298 if ($alloc_item->current_allocated > 0)
301 add_supp_allocation($alloc_item->current_allocated,
302 $_SESSION['alloc']->type, $_SESSION['alloc']->trans_no,
303 $alloc_item->type, $alloc_item->type_no, $_SESSION['alloc']->date_);
305 update_supp_trans_allocation($alloc_item->type, $alloc_item->type_no,
306 $alloc_item->current_allocated);
308 add_cust_allocation($alloc_item->current_allocated,
309 $_SESSION['alloc']->type, $_SESSION['alloc']->trans_no,
310 $alloc_item->type, $alloc_item->type_no, $_SESSION['alloc']->date_);
312 update_debtor_trans_allocation($alloc_item->type, $alloc_item->type_no,
313 $alloc_item->current_allocated);
315 // Exchange Variations Joe Hunt 2008-09-20 ////////////////////////////////////////
317 exchange_variation($_SESSION['alloc']->type, $_SESSION['alloc']->trans_no,
318 $alloc_item->type, $alloc_item->type_no, $_SESSION['alloc']->date_,
319 $alloc_item->current_allocated,
320 $sup ? payment_person_types::supplier() :payment_person_types::customer());
323 ///////////////////////////////////////////////////////////////////////////
324 $total_allocated += $alloc_item->current_allocated;
327 } /*end of the loop through the array of allocations made */
329 update_supp_trans_allocation($_SESSION['alloc']->type,
330 $_SESSION['alloc']->trans_no, $total_allocated);
332 update_debtor_trans_allocation($_SESSION['alloc']->type,
333 $_SESSION['alloc']->trans_no, $total_allocated);
335 commit_transaction();