Ajax additions to sales and purchasing modules.
[fa-stable.git] / purchasing / allocations / supplier_allocate.php
1 <?php
2
3 $path_to_root="../..";
4 include($path_to_root . "/includes/ui/allocation_cart.inc");
5 $page_security = 3;
6
7 include_once($path_to_root . "/includes/session.inc");
8
9 include_once($path_to_root . "/includes/date_functions.inc");
10
11 include_once($path_to_root . "/includes/ui.inc");
12 include_once($path_to_root . "/includes/banking.inc");
13
14 include_once($path_to_root . "/sales/includes/sales_db.inc");
15 $js = "";
16 if ($use_popup_windows)
17         $js .= get_js_open_window(900, 500);
18
19 add_js_file('allocate.js');
20
21 page(_("Allocate Supplier Payment or Credit Note"), false, false, "", $js);
22
23
24 //--------------------------------------------------------------------------------
25
26 function clear_allocations()
27 {
28         if (isset($_SESSION['alloc']))
29         {
30                 unset($_SESSION['alloc']->allocs);
31                 unset($_SESSION['alloc']);
32         }
33
34         session_register("alloc");
35 }
36
37 //--------------------------------------------------------------------------------
38
39 function check_data()
40 {
41         $total_allocated = 0;
42
43         for ($counter=0; $counter < $_POST["TotalNumberOfAllocs"]; $counter++)
44         {
45
46                 if (!check_num('amount' . $counter, 0))
47                 {
48                         display_error(_("The entry for one or more amounts is invalid or negative."));
49                         set_focus('amount');
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_supp_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 $alloc_item)
88         {
89                 if ($alloc_item->current_allocated > 0)
90                 {
91                         add_supp_allocation($alloc_item->current_allocated,
92                                 $_SESSION['alloc']->type, $_SESSION['alloc']->trans_no,
93                         $alloc_item->type, $alloc_item->type_no, $_SESSION['alloc']->date_);
94
95                         update_supp_trans_allocation($alloc_item->type, $alloc_item->type_no,
96                                 $alloc_item->current_allocated);
97                         $total_allocated += $alloc_item->current_allocated;
98                 }
99
100         }  /*end of the loop through the array of allocations made */
101         update_supp_trans_allocation($_SESSION['alloc']->type,
102                 $_SESSION['alloc']->trans_no, $total_allocated);
103
104         commit_transaction();
105
106         clear_allocations();
107 }
108
109 //--------------------------------------------------------------------------------
110
111 if (isset($_POST['Process']))
112 {
113         if (check_data())
114         {
115                 handle_process();
116                 $_POST['Cancel'] = 1;
117         }
118         $Ajax->update('_page_body');
119 }
120
121 //--------------------------------------------------------------------------------
122
123 if (isset($_POST['Cancel']))
124 {
125         clear_allocations();
126         meta_forward($path_to_root . "/purchasing/allocations/supplier_allocation_main.php");
127 }
128 if (isset($_POST['UpdateDisplay']))
129 {
130         $Ajax->update('_page_body');
131 }
132 //--------------------------------------------------------------------------------
133
134 function get_allocations_for_transaction($type, $trans_no)
135 {
136         clear_allocations();
137
138         $supptrans = get_supp_trans($trans_no, $type);
139
140         $_SESSION['alloc'] = new allocation($trans_no, $type,
141                 $supptrans["supplier_id"], $supptrans["supplier_name"],
142                 $supptrans["Total"], sql2date($supptrans["tran_date"]));
143
144         /* Now populate the array of possible (and previous actual) allocations for this supplier */
145         /*First get the transactions that have outstanding balances ie Total-alloc >0 */
146
147         $trans_items = get_allocatable_to_supp_transactions($_SESSION['alloc']->person_id);
148
149         while ($myrow = db_fetch($trans_items))
150         {
151                 $_SESSION['alloc']->add_item($myrow["type"], $myrow["trans_no"],
152                         sql2date($myrow["tran_date"]),
153                         sql2date($myrow["due_date"]),
154                         $myrow["Total"], // trans total
155                         $myrow["alloc"], // trans total allocated
156                         0); // this allocation
157         }
158
159
160         /* Now get trans that might have previously been allocated to by this trans
161         NB existing entries where still some of the trans outstanding entered from
162         above logic will be overwritten with the prev alloc detail below */
163
164         $trans_items = get_allocatable_to_supp_transactions($_SESSION['alloc']->person_id, $trans_no, $type);
165
166         while ($myrow = db_fetch($trans_items))
167         {
168                 $_SESSION['alloc']->add_or_update_item ($myrow["type"], $myrow["trans_no"],
169                         sql2date($myrow["tran_date"]),
170                         sql2date($myrow["due_date"]),
171                         $myrow["Total"],
172                         $myrow["alloc"] - $myrow["amt"], $myrow["amt"]);
173         }
174 }
175
176 //--------------------------------------------------------------------------------
177
178 function edit_allocations_for_transaction($type, $trans_no)
179 {
180         global $table_style;
181
182         start_form(false, true);
183
184     display_heading(_("Allocation of") . " " . systypes::name($_SESSION['alloc']->type) . " # " . $_SESSION['alloc']->trans_no);
185
186         display_heading($_SESSION['alloc']->person_name);
187
188     display_heading2(_("Date:") . " <b>" . $_SESSION['alloc']->date_ . "</b>");
189     display_heading2(_("Total:") . " <b>" . price_format(-$_SESSION['alloc']->amount) . "</b>");
190
191     echo "<br>";
192
193     if (count($_SESSION['alloc']->allocs) > 0)
194     {
195                 start_table($table_style);
196                 $th = array(_("Transaction Type"), _("#"), _("Date"), _("Due Date"), _("Amount"),
197                         _("Other Allocations"), _("This Allocation"), _("Left to Allocate"),'');
198                 table_header($th);
199
200         $k = $counter = $total_allocated = 0;
201
202         foreach ($_SESSION['alloc']->allocs as $alloc_item)
203         {
204                 alt_table_row_color($k);
205
206             label_cell(systypes::name($alloc_item->type));
207                 label_cell(get_trans_view_str($alloc_item->type, $alloc_item->type_no));
208                 label_cell($alloc_item->date_, "align=right");
209                 label_cell($alloc_item->due_date, "align=right");
210                 amount_cell($alloc_item->amount);
211                 amount_cell($alloc_item->amount_allocated);
212
213             if (!isset($_POST['amount' . $counter]) || $_POST['amount' . $counter] == "")
214                 $_POST['amount' . $counter] = price_format($alloc_item->current_allocated);
215             amount_cells(null, "amount" . $counter, price_format('amount' . $counter));
216
217                 $un_allocated = round($alloc_item->amount - $alloc_item->amount_allocated, 6);
218                 //hidden("un_allocated" . $counter, $un_allocated);
219                 amount_cell($un_allocated);
220                         label_cell("<a href='#' name=Alloc$counter onclick='allocate_all(this.name.substr(5));return true;'>"
221                                          . _("All") . "</a>");
222                         label_cell("<a href='#' name=DeAll$counter onclick='allocate_none(this.name.substr(5));return true;'>"
223                                          . _("None") . "</a>".hidden("un_allocated" . $counter, $un_allocated, false));
224
225 //                      label_cell("<a href='#' onclick='forms[0].amount$counter.value=forms[0].un_allocated$counter.value; return true;'>" . _("All") . "</a>");
226 //                      label_cell("<a href='#' onclick='forms[0].amount$counter.value=0; return true;'>" . _("None") . "</a>");
227                         end_row();
228
229             $total_allocated += input_num('amount' . $counter);
230             $counter++;
231         }
232
233         label_row(_("Total Allocated"), number_format2($total_allocated,user_price_dec()),
234                 "colspan=6 align=right", "align=right id='total_allocated'");
235         if (-$_SESSION['alloc']->amount - $total_allocated < 0)
236         {
237                 $font1 = "<font color=red>";
238                 $font2 = "</font>";
239         }
240         else
241                 $font1 = $font2 = "";
242                 $left_to_allocate = price_format(-$_SESSION['alloc']->amount - $total_allocated);
243         label_row(_("Left to Allocate"), $font1 . $left_to_allocate . $font2, "colspan=6 align=right",
244                 "nowrap align=right id='left_to_allocate'");
245                 end_table();
246
247                 hidden('TotalNumberOfAllocs', $counter);
248 //              hidden('left_to_allocate', $left_to_allocate);
249         echo "<br><center>";
250         submit('UpdateDisplay', _("Update"), true, '', true);
251         echo "&nbsp;";
252         submit('Process', _("Process"), true, '', true);
253         echo "&nbsp;";
254         }
255         else
256         {
257         display_note(_("There are no unsettled transactions to allocate."), 0, 1);
258         echo "<center>";
259     }
260
261         submit('Cancel', _("Back to Allocations"), true, '', true);
262         echo "</center><br><br>";
263
264         end_form();
265 }
266
267 //--------------------------------------------------------------------------------
268
269 if (isset($_GET['trans_no']) && isset($_GET['trans_type']))
270 {
271         get_allocations_for_transaction($_GET['trans_type'], $_GET['trans_no']);
272 }
273
274 if (isset($_SESSION['alloc']))
275 {
276         edit_allocations_for_transaction($_SESSION['alloc']->type, $_SESSION['alloc']->trans_no);
277 }
278
279 //--------------------------------------------------------------------------------
280
281 end_page();
282
283 ?>