6 include($path_to_root . "/includes/ui/allocation_cart.inc");
7 include_once($path_to_root . "/includes/session.inc");
8 include_once($path_to_root . "/includes/data_checks.inc");
9 include_once($path_to_root . "/sales/includes/sales_ui.inc");
10 include_once($path_to_root . "/sales/includes/sales_db.inc");
13 if ($use_popup_windows)
14 $js .= get_js_open_window(900, 500);
16 add_js_file('allocate.js');
18 page(_("Allocate Customer Payment or Credit Note"), false, false, "", $js);
20 //--------------------------------------------------------------------------------
22 function clear_allocations()
24 if (isset($_SESSION['alloc']))
26 unset($_SESSION['alloc']->allocs);
27 unset($_SESSION['alloc']);
29 session_register('alloc');
32 //--------------------------------------------------------------------------------
38 for ($counter = 0; $counter < $_POST["TotalNumberOfAllocs"]; $counter++)
41 if (!check_num('amount' . $counter, 0))
43 display_error(_("The entry for one or more amounts is invalid or negative."));
44 set_focus('amount'.$counter);
48 if (!check_num('amount' . $counter,0))
50 display_error(_("The entry for an amount to allocate was negative. A positive allocation amount is expected."));
51 set_focus('amount'.$counter);
55 /*Now check to see that the AllocAmt is no greater than the
56 amount left to be allocated against the transaction under review */
57 if (input_num('amount' . $counter) > $_POST['un_allocated' . $counter])
59 //$_POST['amount' . $counter] = $_POST['un_allocated' . $counter];
62 $_SESSION['alloc']->allocs[$counter]->current_allocated = input_num('amount' . $counter);
64 $total_allocated += input_num('amount' . $counter);
67 if ($total_allocated - $_SESSION['alloc']->amount > sys_prefs::allocation_settled_allowance())
69 display_error(_("These allocations cannot be processed because the amount allocated is more than the total amount left to allocate."));
70 //echo _("Total allocated:") . " " . $total_allocated ;
71 //echo " " . _("Total amount that can be allocated:") . " " . -$_SESSION['alloc']->TransAmt . "<BR>";
78 //-----------------------------------------------------------------------------------
80 function handle_process()
84 // clear all the allocations for this payment/credit
85 clear_cust_alloctions($_SESSION['alloc']->type, $_SESSION['alloc']->trans_no);
87 // now add the new allocations
89 foreach ($_SESSION['alloc']->allocs as $allocn_item)
91 if ($allocn_item->current_allocated > 0)
93 add_cust_allocation($allocn_item->current_allocated,
94 $_SESSION['alloc']->type, $_SESSION['alloc']->trans_no,
95 $allocn_item->type, $allocn_item->type_no, $_SESSION['alloc']->date_);
97 update_debtor_trans_allocation($allocn_item->type, $allocn_item->type_no,
98 $allocn_item->current_allocated);
99 $total_allocated += $allocn_item->current_allocated;
103 } /*end of the loop through the array of allocations made */
105 update_debtor_trans_allocation($_SESSION['alloc']->type,
106 $_SESSION['alloc']->trans_no, $total_allocated);
108 commit_transaction();
112 //--------------------------------------------------------------------------------
114 if (isset($_POST['Process']))
119 $_POST['Cancel'] = 1;
122 //--------------------------------------------------------------------------------
124 if (isset($_POST['Cancel']))
127 meta_forward($path_to_root . "/sales/allocations/customer_allocation_main.php");
129 //--------------------------------------------------------------------------------
131 function get_allocations_for_transaction($type, $trans_no)
135 $debtor = get_customer_trans($trans_no, $type);
137 $_SESSION['alloc'] = new allocation($trans_no, $type, $debtor["debtor_no"],
138 $debtor["DebtorName"], $debtor["Total"], sql2date($debtor["tran_date"]));
140 /* Now populate the array of possible (and previous actual) allocations for this customer */
141 /*First get the transactions that have outstanding balances ie Total-alloc >0 */
143 $trans_items = get_allocatable_to_cust_transactions($_SESSION['alloc']->person_id);
145 while ($myrow = db_fetch($trans_items))
147 $_SESSION['alloc']->add_item($myrow["type"], $myrow["trans_no"],
148 sql2date($myrow["tran_date"]), sql2date($myrow["due_date"]),
149 $myrow["Total"], // trans total
150 $myrow["alloc"], // trans total allocated
151 0); // this allocation
154 /* Now get trans that might have previously been allocated to by this trans
155 NB existing entries where still some of the trans outstanding entered from
156 above logic will be overwritten with the prev alloc detail below */
158 $trans_items = get_allocatable_to_cust_transactions($_SESSION['alloc']->person_id, $trans_no, $type);
160 while ($myrow = db_fetch($trans_items))
162 $_SESSION['alloc']->add_or_update_item ($myrow["type"], $myrow["trans_no"],
163 sql2date($myrow["tran_date"]), sql2date($myrow["due_date"]),
164 $myrow["Total"], $myrow["alloc"] - $myrow["amt"], $myrow["amt"]);
167 //--------------------------------------------------------------------------------
169 function edit_allocations_for_transaction($type, $trans_no)
173 display_heading(sprintf(_("Allocation of %s # %d"), systypes::name($_SESSION['alloc']->type),$_SESSION['alloc']->trans_no));
175 display_heading($_SESSION['alloc']->person_name);
177 display_heading2(_("Date:") . " <b>" . $_SESSION['alloc']->date_ . "</b>");
178 display_heading2(_("Total:") . " <b>" . price_format($_SESSION['alloc']->amount) . "</b>");
182 start_form(false, true);
183 div_start('alloc_tbl');
184 if (count($_SESSION['alloc']->allocs) > 0)
186 start_table($table_style);
188 $th = array(_("Transaction Type"), _("#"), _("Date"), _("Due Date"), _("Amount"),
189 _("Other Allocations"), _("This Allocation"), _("Left to Allocate"), "", "");
193 $k = $counter = $total_allocated = 0;
195 foreach ($_SESSION['alloc']->allocs as $allocn_item)
197 alt_table_row_color($k);
199 label_cell(systypes::name($allocn_item->type));
200 label_cell(get_trans_view_str($allocn_item->type, $allocn_item->type_no));
201 label_cell($allocn_item->date_, "align=right");
202 label_cell($allocn_item->due_date, "align=right");
203 amount_cell($allocn_item->amount);
204 amount_cell($allocn_item->amount_allocated);
206 $_POST['amount' . $counter] = price_format($allocn_item->current_allocated);
207 amount_cells(null, 'amount' . $counter, price_format('amount' . $counter));
209 $un_allocated = round($allocn_item->amount - $allocn_item->amount_allocated, 6);
210 amount_cell($un_allocated);
212 label_cell("<a href='#' name='Alloc$counter' onclick='allocate_all(this.name.substr(5));return true;'>"
213 . _("All") . "</a>");
214 label_cell("<a href='#' name='DeAll$counter' onclick='allocate_none(this.name.substr(5));return true;'>"
215 . _("None") . "</a>".hidden("un_allocated" . $counter, $un_allocated, false));
218 $total_allocated += input_num('amount' . $counter);
222 label_row(_("Total Allocated"), price_format($total_allocated),
223 "colspan=6 align=right", "nowrap align=right id='total_allocated'");
224 if ($_SESSION['alloc']->amount - $total_allocated < 0)
226 $font1 = "<font color=red>";
230 $font1 = $font2 = "";
231 $left_to_allocate = $_SESSION['alloc']->amount - $total_allocated;
232 $left_to_allocate = price_format($left_to_allocate);
233 label_row(_("Left to Allocate"), $font1 . $left_to_allocate . $font2,
234 "colspan=6 align=right ", "nowrap align=right id='left_to_allocate'");
237 hidden('TotalNumberOfAllocs', $counter);
238 submit_center_first('UpdateDisplay', _("Refresh"), _('Start again allocation of selected amount'), true);
239 submit('Process', _("Process"), true, _('Process allocations'), true);
240 submit_center_last('Cancel', _("Back to Allocations"),_('Abandon allocations and return to selection of allocatable amounts'), true);
244 display_note(_("There are no unsettled transactions to allocate."), 0, 1);
246 submit_center('Cancel', _("Back to Allocations"), true,
247 _('Abandon allocations and return to selection of allocatable amounts'), true);
253 //--------------------------------------------------------------------------------
255 if (isset($_GET['trans_no']) && isset($_GET['trans_type']))
257 get_allocations_for_transaction($_GET['trans_type'], $_GET['trans_no']);
259 if(get_post('UpdateDisplay'))
261 $trans_no = $_SESSION['alloc']->trans_no;
262 $type = $_SESSION['alloc']->type;
264 get_allocations_for_transaction($type, $trans_no);
265 $Ajax->activate('alloc_tbl');
268 if (isset($_SESSION['alloc']))
270 edit_allocations_for_transaction($_SESSION['alloc']->type, $_SESSION['alloc']->trans_no);
273 //--------------------------------------------------------------------------------