User side percent/qty/amount/exrate input formatting via onblur handler.
[fa-stable.git] / sales / allocations / customer_allocate.php
1 <?php
2
3 $path_to_root="../..";
4 $page_security = 3;
5
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");
11
12 $js = "";
13 if ($use_popup_windows)
14         $js .= get_js_open_window(900, 500);
15
16 add_js_allocate();
17
18 page(_("Allocate Customer Payment or Credit Note"), false, false, "", $js);
19
20 //--------------------------------------------------------------------------------
21
22 function clear_allocations()
23 {
24         if (isset($_SESSION['alloc']))
25         {
26                 unset($_SESSION['alloc']->allocs);
27                 unset($_SESSION['alloc']);
28         }
29         session_register('alloc');
30 }
31
32 //--------------------------------------------------------------------------------
33
34 function check_data()
35 {
36         $total_allocated = 0;
37
38         for ($counter = 0; $counter < $_POST["TotalNumberOfAllocs"]; $counter++)
39         {
40
41                 if (!check_num('amount' . $counter))
42                 {
43                         display_error(_("The entry for one or more amounts is invalid."));
44                         return false;
45                 }
46
47                 if (!check_num('amount' . $counter,0))
48                 {
49                         display_error(_("The entry for an amount to allocate was negative. A positive allocation amount is expected."));
50                         return false;
51                 }
52
53                   /*Now check to see that the AllocAmt is no greater than the
54                 amount left to be allocated against the transaction under review */
55                 if (input_num('amount' . $counter) > $_POST['un_allocated' . $counter])
56                 {
57                     //$_POST['amount' . $counter] = $_POST['un_allocated' . $counter];
58                 }
59
60                 $_SESSION['alloc']->allocs[$counter]->current_allocated = input_num('amount' . $counter);
61
62                 $total_allocated += input_num('amount' . $counter);
63         }
64
65         if ($total_allocated - $_SESSION['alloc']->amount > sys_prefs::allocation_settled_allowance())
66         {
67                 display_error(_("These allocations cannot be processed because the amount allocated is more than the total amount left to allocate."));
68                 //echo  _("Total allocated:") . " " . $total_allocated ;
69                 //echo "  " . _("Total amount that can be allocated:") . " " . -$_SESSION['alloc']->TransAmt . "<BR>";
70                 return false;
71         }
72
73         return true;
74 }
75
76 //-----------------------------------------------------------------------------------
77
78 function handle_process()
79 {
80         begin_transaction();
81
82         // clear all the allocations for this payment/credit
83         clear_cust_alloctions($_SESSION['alloc']->type, $_SESSION['alloc']->trans_no);
84
85         // now add the new allocations
86         $total_allocated = 0;
87         foreach ($_SESSION['alloc']->allocs as $allocn_item)
88         {
89                 if ($allocn_item->current_allocated > 0)
90                 {
91                         add_cust_allocation($allocn_item->current_allocated,
92                                 $_SESSION['alloc']->type, $_SESSION['alloc']->trans_no,
93                         $allocn_item->type, $allocn_item->type_no, $_SESSION['alloc']->date_);
94
95                         update_debtor_trans_allocation($allocn_item->type, $allocn_item->type_no,
96                                 $allocn_item->current_allocated);
97                         $total_allocated += $allocn_item->current_allocated;
98
99                 }
100
101         }  /*end of the loop through the array of allocations made */
102
103         update_debtor_trans_allocation($_SESSION['alloc']->type,
104                 $_SESSION['alloc']->trans_no, $total_allocated);
105
106         commit_transaction();
107
108         clear_allocations();
109 }
110
111 //--------------------------------------------------------------------------------
112
113 if (isset($_POST['Process']))
114 {
115         if (check_data())
116         {
117                 handle_process();
118                 $_POST['Cancel'] = 1;
119         }
120 }
121
122 //--------------------------------------------------------------------------------
123
124 if (isset($_POST['Cancel']))
125 {
126         clear_allocations();
127         meta_forward($path_to_root . "/sales/allocations/customer_allocation_main.php");
128         exit;
129 }
130
131 //--------------------------------------------------------------------------------
132
133 function get_allocations_for_transaction($type, $trans_no)
134 {
135         clear_allocations();
136
137         $debtor = get_customer_trans($trans_no, $type);
138
139         $_SESSION['alloc'] = new allocation($trans_no, $type, $debtor["debtor_no"],
140                 $debtor["DebtorName"], $debtor["Total"], sql2date($debtor["tran_date"]));
141
142         /* Now populate the array of possible (and previous actual) allocations for this customer */
143         /*First get the transactions that have outstanding balances ie Total-alloc >0 */
144
145         $trans_items = get_allocatable_to_cust_transactions($_SESSION['alloc']->person_id);
146
147         while ($myrow = db_fetch($trans_items))
148         {
149                 $_SESSION['alloc']->add_item($myrow["type"], $myrow["trans_no"],
150                         sql2date($myrow["tran_date"]), sql2date($myrow["due_date"]),
151                         $myrow["Total"], // trans total
152                         $myrow["alloc"], // trans total allocated
153                         0); // this allocation
154         }
155
156
157         /* Now get trans that might have previously been allocated to by this trans
158         NB existing entries where still some of the trans outstanding entered from
159         above logic will be overwritten with the prev alloc detail below */
160
161         $trans_items = get_allocatable_to_cust_transactions($_SESSION['alloc']->person_id, $trans_no, $type);
162
163         while ($myrow = db_fetch($trans_items))
164         {
165                 $_SESSION['alloc']->add_or_update_item ($myrow["type"], $myrow["trans_no"],
166                         sql2date($myrow["tran_date"]), sql2date($myrow["due_date"]),
167                         $myrow["Total"], $myrow["alloc"] - $myrow["amt"], $myrow["amt"]);
168         }
169 }
170
171 //--------------------------------------------------------------------------------
172
173 function edit_allocations_for_transaction($type, $trans_no)
174 {
175         global $table_style;
176
177         start_form(false, true);
178
179     display_heading(sprintf(_("Allocation of %s # %d"), systypes::name($_SESSION['alloc']->type),$_SESSION['alloc']->trans_no));
180
181     display_heading($_SESSION['alloc']->person_name);
182
183     display_heading2(_("Date:") . " <b>" . $_SESSION['alloc']->date_ . "</b>");
184     display_heading2(_("Total:") . " <b>" . price_format($_SESSION['alloc']->amount) . "</b>");
185
186     echo "<br>";
187
188     if (count($_SESSION['alloc']->allocs) > 0)
189     {
190                 start_table($table_style);
191
192                 $th = array(_("Transaction Type"), _("#"), _("Date"), _("Due Date"), _("Amount"),
193                         _("Other Allocations"), _("This Allocation"), _("Left to Allocate"), "", "");
194
195                 table_header($th);
196
197         $k = $counter = $total_allocated = 0;
198
199         foreach ($_SESSION['alloc']->allocs as $allocn_item)
200         {
201                 alt_table_row_color($k);
202
203             label_cell(systypes::name($allocn_item->type));
204                 label_cell(get_trans_view_str($allocn_item->type, $allocn_item->type_no));
205                 label_cell($allocn_item->date_, "align=right");
206                 label_cell($allocn_item->due_date, "align=right");
207                 amount_cell($allocn_item->amount);
208                         amount_cell($allocn_item->amount_allocated);
209
210             if (!check_num('amount' . $counter))
211                 $_POST['amount' . $counter] = price_format($allocn_item->current_allocated);
212             amount_cells(null, 'amount' . $counter, $_POST['amount' . $counter]);
213
214                 $un_allocated = round($allocn_item->amount - $allocn_item->amount_allocated, 6);
215                 hidden("un_allocated" . $counter, $un_allocated);
216                 amount_cell($un_allocated);
217
218                         label_cell("<a href='#' name=Alloc$counter onclick='allocate_all(this.name.substr(5));return true;'>"
219                                          . _("All") . "</a>");
220                         label_cell("<a href='#' name=DeAll$counter onclick='allocate_none(this.name.substr(5));return true;'>"
221                                          . _("None") . "</a>");
222                         end_row();
223
224             $total_allocated += input_num('amount' . $counter);
225             $counter++;
226         }
227
228         label_row(_("Total Allocated"), price_format($total_allocated),
229                 "colspan=6 align=right", "nowrap align=right");
230         if ($_SESSION['alloc']->amount - $total_allocated < 0)
231         {
232                 $font1 = "<font color=red>";
233                 $font2 = "</font>";
234         }
235         else
236                 $font1 = $font2 = "";
237                 $left_to_allocate = $_SESSION['alloc']->amount - $total_allocated;
238                 $left_to_allocate = price_format($left_to_allocate);
239         label_row(_("Left to Allocate"), $font1 . $left_to_allocate . $font2,
240                 "colspan=6 align=right ", "nowrap align=right");
241         end_table(1);
242
243         hidden('TotalNumberOfAllocs', $counter);
244                 hidden('left_to_allocate', $left_to_allocate);
245         submit_center_first('UpdateDisplay', _("Update"));
246         submit('Process', _("Process"));
247         }
248         else
249         {
250         display_note(_("There are no unsettled transactions to allocate."), 0, 1);
251     }
252
253         submit_center_last('Cancel', _("Back to Allocations"));
254
255         end_form();
256 }
257
258 //--------------------------------------------------------------------------------
259
260 if (isset($_GET['trans_no']) && isset($_GET['trans_type']))
261 {
262         get_allocations_for_transaction($_GET['trans_type'], $_GET['trans_no']);
263 }
264
265 if (isset($_SESSION['alloc']))
266 {
267         edit_allocations_for_transaction($_SESSION['alloc']->type, $_SESSION['alloc']->trans_no);
268 }
269
270 //--------------------------------------------------------------------------------
271
272 end_page();
273
274 ?>