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