Merged bugfixes upto [0000072] (version 2.0.3).
[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_file('allocate.js');
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, 0))
42                 {
43                         display_error(_("The entry for one or more amounts is invalid or negative."));
44                         set_focus('amount'.$counter);
45                         return false;
46                 }
47
48                 if (!check_num('amount' . $counter,0))
49                 {
50                         display_error(_("The entry for an amount to allocate was negative. A positive allocation amount is expected."));
51                 set_focus('amount'.$counter);
52                         return false;
53                 }
54
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])
58                 {
59                     //$_POST['amount' . $counter] = $_POST['un_allocated' . $counter];
60                 }
61
62                 $_SESSION['alloc']->allocs[$counter]->current_allocated = input_num('amount' . $counter);
63
64                 $total_allocated += input_num('amount' . $counter);
65         }
66
67         if ($total_allocated - $_SESSION['alloc']->amount > sys_prefs::allocation_settled_allowance())
68         {
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>";
72                 return false;
73         }
74
75         return true;
76 }
77
78 //-----------------------------------------------------------------------------------
79
80 function handle_process()
81 {
82         begin_transaction();
83
84         // clear all the allocations for this payment/credit
85         clear_cust_alloctions($_SESSION['alloc']->type, $_SESSION['alloc']->trans_no, $_SESSION['alloc']->date_);
86
87         // now add the new allocations
88         $total_allocated = 0;
89         foreach ($_SESSION['alloc']->allocs as $allocn_item)
90         {
91                 if ($allocn_item->current_allocated > 0)
92                 {
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_);
96
97                         update_debtor_trans_allocation($allocn_item->type, $allocn_item->type_no,
98                                 $allocn_item->current_allocated);
99
100                         // Exchange Variations Joe Hunt 2008-09-20 ////////////////////////////////////////
101
102                         exchange_variation($_SESSION['alloc']->type, $_SESSION['alloc']->trans_no,
103                                 $allocn_item->type, $allocn_item->type_no, $_SESSION['alloc']->date_,
104                                 $allocn_item->current_allocated, payment_person_types::customer());
105
106                         ///////////////////////////////////////////////////////////////////////////
107                         $total_allocated += $allocn_item->current_allocated;
108                 }
109
110         }  /*end of the loop through the array of allocations made */
111
112         update_debtor_trans_allocation($_SESSION['alloc']->type,
113                 $_SESSION['alloc']->trans_no, $total_allocated);
114
115         commit_transaction();
116
117         clear_allocations();
118 }
119 //--------------------------------------------------------------------------------
120
121 if (isset($_POST['Process']))
122 {
123         if (check_data())
124         {
125                 handle_process();
126                 $_POST['Cancel'] = 1;
127         }
128 }
129 //--------------------------------------------------------------------------------
130
131 if (isset($_POST['Cancel']))
132 {
133         clear_allocations();
134         meta_forward($path_to_root . "/sales/allocations/customer_allocation_main.php");
135 }
136 //--------------------------------------------------------------------------------
137
138 function get_allocations_for_transaction($type, $trans_no)
139 {
140         clear_allocations();
141
142         $debtor = get_customer_trans($trans_no, $type);
143
144         $_SESSION['alloc'] = new allocation($trans_no, $type, $debtor["debtor_no"],
145                 $debtor["DebtorName"], $debtor["Total"], sql2date($debtor["tran_date"]));
146
147         /* Now populate the array of possible (and previous actual) allocations for this customer */
148         /*First get the transactions that have outstanding balances ie Total-alloc >0 */
149
150         $trans_items = get_allocatable_to_cust_transactions($_SESSION['alloc']->person_id);
151
152         while ($myrow = db_fetch($trans_items))
153         {
154                 $_SESSION['alloc']->add_item($myrow["type"], $myrow["trans_no"],
155                         sql2date($myrow["tran_date"]), sql2date($myrow["due_date"]),
156                         $myrow["Total"], // trans total
157                         $myrow["alloc"], // trans total allocated
158                         0); // this allocation
159         }
160
161         /* Now get trans that might have previously been allocated to by this trans
162         NB existing entries where still some of the trans outstanding entered from
163         above logic will be overwritten with the prev alloc detail below */
164
165         $trans_items = get_allocatable_to_cust_transactions($_SESSION['alloc']->person_id, $trans_no, $type);
166
167         while ($myrow = db_fetch($trans_items))
168         {
169                 $_SESSION['alloc']->add_or_update_item ($myrow["type"], $myrow["trans_no"],
170                         sql2date($myrow["tran_date"]), sql2date($myrow["due_date"]),
171                         $myrow["Total"], $myrow["alloc"] - $myrow["amt"], $myrow["amt"]);
172         }
173 }
174 //--------------------------------------------------------------------------------
175
176 function edit_allocations_for_transaction($type, $trans_no)
177 {
178         global $table_style;
179
180     display_heading(sprintf(_("Allocation of %s # %d"), systypes::name($_SESSION['alloc']->type),$_SESSION['alloc']->trans_no));
181
182     display_heading($_SESSION['alloc']->person_name);
183
184     display_heading2(_("Date:") . " <b>" . $_SESSION['alloc']->date_ . "</b>");
185     display_heading2(_("Total:") . " <b>" . price_format($_SESSION['alloc']->amount) . "</b>");
186
187     echo "<br>";
188
189         start_form(false, true);
190         div_start('alloc_tbl');
191     if (count($_SESSION['alloc']->allocs) > 0)
192     {
193                 start_table($table_style);
194
195                 $th = array(_("Transaction Type"), _("#"), _("Date"), _("Due Date"), _("Amount"),
196                         _("Other Allocations"), _("This Allocation"), _("Left to Allocate"), "", "");
197
198                 table_header($th);
199
200         $k = $counter = $total_allocated = 0;
201
202         foreach ($_SESSION['alloc']->allocs as $allocn_item)
203         {
204                         alt_table_row_color($k);
205
206             label_cell(systypes::name($allocn_item->type));
207                 label_cell(get_trans_view_str($allocn_item->type, $allocn_item->type_no));
208                 label_cell($allocn_item->date_, "align=right");
209                 label_cell($allocn_item->due_date, "align=right");
210                 amount_cell($allocn_item->amount);
211                         amount_cell($allocn_item->amount_allocated);
212
213             $_POST['amount' . $counter] = price_format($allocn_item->current_allocated);
214             amount_cells(null, 'amount' . $counter, price_format('amount' . $counter));
215
216                 $un_allocated = round($allocn_item->amount - $allocn_item->amount_allocated, 6);
217                 amount_cell($un_allocated);
218
219                         label_cell("<a href='#' name='Alloc$counter' onclick='allocate_all(this.name.substr(5));return true;'>"
220                                          . _("All") . "</a>");
221                         label_cell("<a href='#' name='DeAll$counter' onclick='allocate_none(this.name.substr(5));return true;'>"
222                                          . _("None") . "</a>".hidden("un_allocated" . $counter, $un_allocated, false));
223                         end_row();
224
225             $total_allocated += input_num('amount' . $counter);
226             $counter++;
227         }
228
229         label_row(_("Total Allocated"), price_format($total_allocated),
230                 "colspan=6 align=right", "nowrap align=right id='total_allocated'");
231         if ($_SESSION['alloc']->amount - $total_allocated < 0)
232         {
233                 $font1 = "<font color=red>";
234                 $font2 = "</font>";
235         }
236         else
237                 $font1 = $font2 = "";
238                 $left_to_allocate = $_SESSION['alloc']->amount - $total_allocated;
239                 $left_to_allocate = price_format($left_to_allocate);
240         label_row(_("Left to Allocate"), $font1 . $left_to_allocate . $font2,
241                 "colspan=6 align=right ", "nowrap align=right id='left_to_allocate'");
242         end_table(1);
243
244         hidden('TotalNumberOfAllocs', $counter);
245         submit_center_first('UpdateDisplay', _("Refresh"), _('Start again allocation of selected amount'), true);
246         submit('Process', _("Process"), true, _('Process allocations'), true);
247                 submit_center_last('Cancel', _("Back to Allocations"),_('Abandon allocations and return to selection of allocatable amounts'), true);
248         }
249         else
250         {
251         display_note(_("There are no unsettled transactions to allocate."), 0, 1);
252
253                 submit_center('Cancel', _("Back to Allocations"), true,
254                         _('Abandon allocations and return to selection of allocatable amounts'), true);
255     }
256         div_end();
257         end_form();
258 }
259
260 //--------------------------------------------------------------------------------
261
262 if (isset($_GET['trans_no']) && isset($_GET['trans_type']))
263 {
264         get_allocations_for_transaction($_GET['trans_type'], $_GET['trans_no']);
265 }
266 if(get_post('UpdateDisplay'))
267 {
268         $trans_no = $_SESSION['alloc']->trans_no;
269         $type = $_SESSION['alloc']->type;
270         clear_allocations();
271         get_allocations_for_transaction($type, $trans_no);
272         $Ajax->activate('alloc_tbl');
273 }
274
275 if (isset($_SESSION['alloc']))
276 {
277         edit_allocations_for_transaction($_SESSION['alloc']->type, $_SESSION['alloc']->trans_no);
278 }
279
280 //--------------------------------------------------------------------------------
281
282 end_page();
283
284 ?>