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 $path_to_root="../..";
15 include($path_to_root . "/includes/ui/allocation_cart.inc");
16 include_once($path_to_root . "/includes/session.inc");
17 include_once($path_to_root . "/includes/data_checks.inc");
18 include_once($path_to_root . "/sales/includes/sales_ui.inc");
19 include_once($path_to_root . "/sales/includes/sales_db.inc");
22 if ($use_popup_windows)
23 $js .= get_js_open_window(900, 500);
25 add_js_file('allocate.js');
27 page(_("Allocate Customer Payment or Credit Note"), false, false, "", $js);
29 //--------------------------------------------------------------------------------
31 function clear_allocations()
33 if (isset($_SESSION['alloc']))
35 unset($_SESSION['alloc']->allocs);
36 unset($_SESSION['alloc']);
38 session_register('alloc');
41 //--------------------------------------------------------------------------------
47 for ($counter = 0; $counter < $_POST["TotalNumberOfAllocs"]; $counter++)
50 if (!check_num('amount' . $counter, 0))
52 display_error(_("The entry for one or more amounts is invalid or negative."));
53 set_focus('amount'.$counter);
57 if (!check_num('amount' . $counter,0))
59 display_error(_("The entry for an amount to allocate was negative. A positive allocation amount is expected."));
60 set_focus('amount'.$counter);
64 /*Now check to see that the AllocAmt is no greater than the
65 amount left to be allocated against the transaction under review */
66 if (input_num('amount' . $counter) > input_num('un_allocated' . $counter))
68 //$_POST['amount' . $counter] = $_POST['un_allocated' . $counter];
71 $_SESSION['alloc']->allocs[$counter]->current_allocated = input_num('amount' . $counter);
73 $total_allocated += input_num('amount' . $counter);
76 if ($total_allocated - $_SESSION['alloc']->amount > sys_prefs::allocation_settled_allowance())
78 display_error(_("These allocations cannot be processed because the amount allocated is more than the total amount left to allocate."));
79 //echo _("Total allocated:") . " " . $total_allocated ;
80 //echo " " . _("Total amount that can be allocated:") . " " . -$_SESSION['alloc']->TransAmt . "<BR>";
87 //-----------------------------------------------------------------------------------
89 function handle_process()
93 // clear all the allocations for this payment/credit
94 clear_cust_alloctions($_SESSION['alloc']->type, $_SESSION['alloc']->trans_no, $_SESSION['alloc']->date_);
96 // now add the new allocations
98 foreach ($_SESSION['alloc']->allocs as $allocn_item)
100 if ($allocn_item->current_allocated > 0)
102 add_cust_allocation($allocn_item->current_allocated,
103 $_SESSION['alloc']->type, $_SESSION['alloc']->trans_no,
104 $allocn_item->type, $allocn_item->type_no, $_SESSION['alloc']->date_);
106 update_debtor_trans_allocation($allocn_item->type, $allocn_item->type_no,
107 $allocn_item->current_allocated);
109 // Exchange Variations Joe Hunt 2008-09-20 ////////////////////////////////////////
111 exchange_variation($_SESSION['alloc']->type, $_SESSION['alloc']->trans_no,
112 $allocn_item->type, $allocn_item->type_no, $_SESSION['alloc']->date_,
113 $allocn_item->current_allocated, payment_person_types::customer());
115 ///////////////////////////////////////////////////////////////////////////
116 $total_allocated += $allocn_item->current_allocated;
119 } /*end of the loop through the array of allocations made */
121 update_debtor_trans_allocation($_SESSION['alloc']->type,
122 $_SESSION['alloc']->trans_no, $total_allocated);
124 commit_transaction();
128 //--------------------------------------------------------------------------------
130 if (isset($_POST['Process']))
135 $_POST['Cancel'] = 1;
138 //--------------------------------------------------------------------------------
140 if (isset($_POST['Cancel']))
143 meta_forward($path_to_root . "/sales/allocations/customer_allocation_main.php");
145 //--------------------------------------------------------------------------------
147 function get_allocations_for_transaction($type, $trans_no)
151 $debtor = get_customer_trans($trans_no, $type);
153 $_SESSION['alloc'] = new allocation($trans_no, $type, $debtor["debtor_no"],
154 $debtor["DebtorName"], $debtor["Total"], sql2date($debtor["tran_date"]));
156 /* Now populate the array of possible (and previous actual) allocations for this customer */
157 /*First get the transactions that have outstanding balances ie Total-alloc >0 */
159 $trans_items = get_allocatable_to_cust_transactions($_SESSION['alloc']->person_id);
161 while ($myrow = db_fetch($trans_items))
163 $_SESSION['alloc']->add_item($myrow["type"], $myrow["trans_no"],
164 sql2date($myrow["tran_date"]), sql2date($myrow["due_date"]),
165 $myrow["Total"], // trans total
166 $myrow["alloc"], // trans total allocated
167 0); // this allocation
170 /* Now get trans that might have previously been allocated to by this trans
171 NB existing entries where still some of the trans outstanding entered from
172 above logic will be overwritten with the prev alloc detail below */
174 $trans_items = get_allocatable_to_cust_transactions($_SESSION['alloc']->person_id, $trans_no, $type);
176 while ($myrow = db_fetch($trans_items))
178 $_SESSION['alloc']->add_or_update_item ($myrow["type"], $myrow["trans_no"],
179 sql2date($myrow["tran_date"]), sql2date($myrow["due_date"]),
180 $myrow["Total"], $myrow["alloc"] - $myrow["amt"], $myrow["amt"]);
183 //--------------------------------------------------------------------------------
185 function edit_allocations_for_transaction($type, $trans_no)
189 display_heading(sprintf(_("Allocation of %s # %d"), systypes::name($_SESSION['alloc']->type),$_SESSION['alloc']->trans_no));
191 display_heading($_SESSION['alloc']->person_name);
193 display_heading2(_("Date:") . " <b>" . $_SESSION['alloc']->date_ . "</b>");
194 display_heading2(_("Total:") . " <b>" . price_format($_SESSION['alloc']->amount) . "</b>");
198 start_form(false, true);
199 div_start('alloc_tbl');
200 if (count($_SESSION['alloc']->allocs) > 0)
202 start_table($table_style);
204 $th = array(_("Transaction Type"), _("#"), _("Date"), _("Due Date"), _("Amount"),
205 _("Other Allocations"), _("This Allocation"), _("Left to Allocate"), "", "");
209 $k = $counter = $total_allocated = 0;
211 foreach ($_SESSION['alloc']->allocs as $allocn_item)
213 alt_table_row_color($k);
215 label_cell(systypes::name($allocn_item->type));
216 label_cell(get_trans_view_str($allocn_item->type, $allocn_item->type_no));
217 label_cell($allocn_item->date_, "align=right");
218 label_cell($allocn_item->due_date, "align=right");
219 amount_cell($allocn_item->amount);
220 amount_cell($allocn_item->amount_allocated);
222 $_POST['amount' . $counter] = price_format($allocn_item->current_allocated);
223 amount_cells(null, 'amount' . $counter, price_format('amount' . $counter));
225 $un_allocated = round($allocn_item->amount - $allocn_item->amount_allocated, 6);
226 amount_cell($un_allocated);
228 label_cell("<a href='#' name='Alloc$counter' onclick='allocate_all(this.name.substr(5));return true;'>"
229 . _("All") . "</a>");
230 label_cell("<a href='#' name='DeAll$counter' onclick='allocate_none(this.name.substr(5));return true;'>"
231 . _("None") . "</a>".hidden("un_allocated" . $counter, price_format($un_allocated), false));
234 $total_allocated += input_num('amount' . $counter);
238 label_row(_("Total Allocated"), price_format($total_allocated),
239 "colspan=6 align=right", "nowrap align=right id='total_allocated'", 3);
240 if ($_SESSION['alloc']->amount - $total_allocated < 0)
242 $font1 = "<font color=red>";
246 $font1 = $font2 = "";
247 $left_to_allocate = $_SESSION['alloc']->amount - $total_allocated;
248 $left_to_allocate = price_format($left_to_allocate);
249 label_row(_("Left to Allocate"), $font1 . $left_to_allocate . $font2,
250 "colspan=6 align=right ", "nowrap align=right id='left_to_allocate'", 3);
253 hidden('TotalNumberOfAllocs', $counter);
254 submit_center_first('UpdateDisplay', _("Refresh"), _('Start again allocation of selected amount'), true);
255 submit('Process', _("Process"), true, _('Process allocations'), true);
256 submit_center_last('Cancel', _("Back to Allocations"),_('Abandon allocations and return to selection of allocatable amounts'), true);
260 display_note(_("There are no unsettled transactions to allocate."), 0, 1);
262 submit_center('Cancel', _("Back to Allocations"), true,
263 _('Abandon allocations and return to selection of allocatable amounts'), true);
269 //--------------------------------------------------------------------------------
271 if (isset($_GET['trans_no']) && isset($_GET['trans_type']))
273 get_allocations_for_transaction($_GET['trans_type'], $_GET['trans_no']);
275 if(get_post('UpdateDisplay'))
277 $trans_no = $_SESSION['alloc']->trans_no;
278 $type = $_SESSION['alloc']->type;
280 get_allocations_for_transaction($type, $trans_no);
281 $Ajax->activate('alloc_tbl');
284 if (isset($_SESSION['alloc']))
286 edit_allocations_for_transaction($_SESSION['alloc']->type, $_SESSION['alloc']->trans_no);
289 //--------------------------------------------------------------------------------