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