a0e3b86fb187249097e1705797e2c2edfc6a12e6
[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_allocate();
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                         return false;
50                  }
51
52                   /*Now check to see that the AllocAmt is no greater than the
53                  amount left to be allocated against the transaction under review */
54                  if (input_num('amount' . $counter) > $_POST['un_allocated' . $counter])
55                  {
56                      //$_POST['amount' . $counter] = $_POST['un_allocated' . $counter];
57                  }
58
59                  $_SESSION['alloc']->allocs[$counter]->current_allocated = input_num('amount' . $counter);
60
61                  $total_allocated += input_num('amount' . $counter);
62         }
63
64         if ($total_allocated + $_SESSION['alloc']->amount > sys_prefs::allocation_settled_allowance())
65         {
66                 display_error(_("These allocations cannot be processed because the amount allocated is more than the total amount left to allocate."));
67            //echo  _("Total allocated:") . " " . $total_allocated ;
68            //echo "  " . _("Total amount that can be allocated:") . " " . -$_SESSION['alloc']->TransAmt . "<BR>";
69                 return false;
70         }
71
72         return true;
73 }
74
75 //-----------------------------------------------------------------------------------
76
77 function handle_process()
78 {
79         begin_transaction();
80
81         // clear all the allocations for this payment/credit
82         clear_supp_alloctions($_SESSION['alloc']->type, $_SESSION['alloc']->trans_no);
83
84         // now add the new allocations
85         $total_allocated = 0;
86         foreach ($_SESSION['alloc']->allocs as $alloc_item)
87         {
88                 if ($alloc_item->current_allocated > 0)
89                 {
90                         add_supp_allocation($alloc_item->current_allocated,
91                                 $_SESSION['alloc']->type, $_SESSION['alloc']->trans_no,
92                         $alloc_item->type, $alloc_item->type_no, $_SESSION['alloc']->date_);
93
94                         update_supp_trans_allocation($alloc_item->type, $alloc_item->type_no,
95                                 $alloc_item->current_allocated);
96                         $total_allocated += $alloc_item->current_allocated;
97                 }
98
99         }  /*end of the loop through the array of allocations made */
100         update_supp_trans_allocation($_SESSION['alloc']->type,
101                 $_SESSION['alloc']->trans_no, $total_allocated);
102
103         commit_transaction();
104
105         clear_allocations();
106 }
107
108 //--------------------------------------------------------------------------------
109
110 if (isset($_POST['Process']))
111 {
112         if (check_data()) 
113         {
114                 handle_process();
115                 $_POST['Cancel'] = 1;
116         }
117 }
118
119 //--------------------------------------------------------------------------------
120
121 if (isset($_POST['Cancel']))
122 {
123         clear_allocations();
124         meta_forward($path_to_root . "/purchasing/allocations/supplier_allocation_main.php");
125         exit;
126 }
127
128 //--------------------------------------------------------------------------------
129
130 function get_allocations_for_transaction($type, $trans_no)
131 {
132         clear_allocations();
133
134         $supptrans = get_supp_trans($trans_no, $type);
135
136         $_SESSION['alloc'] = new allocation($trans_no, $type,
137                 $supptrans["supplier_id"], $supptrans["supplier_name"],
138                 $supptrans["Total"], sql2date($supptrans["tran_date"]));
139
140         /* Now populate the array of possible (and previous actual) allocations for this supplier */
141         /*First get the transactions that have outstanding balances ie Total-alloc >0 */
142
143         $trans_items = get_allocatable_to_supp_transactions($_SESSION['alloc']->person_id);
144
145         while ($myrow = db_fetch($trans_items))
146         {
147                 $_SESSION['alloc']->add_item($myrow["type"], $myrow["trans_no"],
148                         sql2date($myrow["tran_date"]),
149                         sql2date($myrow["due_date"]),
150                         $myrow["Total"], // trans total
151                         $myrow["alloc"], // trans total allocated
152                         0); // this allocation
153         }
154
155
156         /* Now get trans that might have previously been allocated to by this trans
157         NB existing entries where still some of the trans outstanding entered from
158         above logic will be overwritten with the prev alloc detail below */
159
160         $trans_items = get_allocatable_to_supp_transactions($_SESSION['alloc']->person_id, $trans_no, $type);
161
162         while ($myrow = db_fetch($trans_items))
163         {
164                 $_SESSION['alloc']->add_or_update_item ($myrow["type"], $myrow["trans_no"],
165                         sql2date($myrow["tran_date"]),
166                         sql2date($myrow["due_date"]),
167                         $myrow["Total"],
168                         $myrow["alloc"] - $myrow["amt"], $myrow["amt"]);
169         }
170 }
171
172 //--------------------------------------------------------------------------------
173
174 function edit_allocations_for_transaction($type, $trans_no)
175 {
176         global $table_style;
177
178         start_form(false, true);
179
180     display_heading(_("Allocation of") . " " . 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     if (count($_SESSION['alloc']->allocs) > 0)
190     {
191                 start_table($table_style);
192                 $th = array(_("Transaction Type"), _("#"), _("Date"), _("Due Date"), _("Amount"), 
193                         _("Other Allocations"), _("This Allocation"), _("Left to Allocate"),'');
194                 table_header($th);      
195
196         $k = $counter = $total_allocated = 0;
197
198         foreach ($_SESSION['alloc']->allocs as $alloc_item)
199         {
200                 alt_table_row_color($k);
201
202             label_cell(systypes::name($alloc_item->type));
203                 label_cell(get_trans_view_str($alloc_item->type, $alloc_item->type_no));
204                 label_cell($alloc_item->date_, "align=right");
205                 label_cell($alloc_item->due_date, "align=right");
206                 amount_cell($alloc_item->amount);
207                 amount_cell($alloc_item->amount_allocated);
208
209             if (!isset($_POST['amount' . $counter]) || $_POST['amount' . $counter] == "")
210                 $_POST['amount' . $counter] = price_format($alloc_item->current_allocated);
211             amount_cells(null, "amount" . $counter, price_format('amount' . $counter));
212
213                 $un_allocated = round($alloc_item->amount - $alloc_item->amount_allocated, 6);
214                 hidden("un_allocated" . $counter, $un_allocated);
215                 amount_cell($un_allocated);
216                         label_cell("<a href='#' name=Alloc$counter onclick='allocate_all(this.name.substr(5));return true;'>"
217                                          . _("All") . "</a>");
218                         label_cell("<a href='#' name=DeAll$counter onclick='allocate_none(this.name.substr(5));return true;'>"
219                                          . _("None") . "</a>");
220
221 //                      label_cell("<a href='#' onclick='forms[0].amount$counter.value=forms[0].un_allocated$counter.value; return true;'>" . _("All") . "</a>");
222 //                      label_cell("<a href='#' onclick='forms[0].amount$counter.value=0; return true;'>" . _("None") . "</a>");
223                         end_row();
224
225             $total_allocated += input_num('amount' . $counter);
226             $counter++;
227         }
228                 
229         label_row(_("Total Allocated"), number_format2($total_allocated,user_price_dec()),
230                 "colspan=6 align=right", "align=right");
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 = price_format(-$_SESSION['alloc']->amount - $total_allocated); 
239         label_row(_("Left to Allocate"), $font1 . $left_to_allocate . $font2, "colspan=6 align=right", 
240                 "nowrap align=right");
241                 end_table();            
242
243                 hidden('TotalNumberOfAllocs', $counter);
244                 hidden('left_to_allocate', $left_to_allocate);
245         echo "<br><center>";
246         submit('UpdateDisplay', _("Update"));
247         echo "&nbsp;";
248         submit('Process', _("Process"));
249         echo "&nbsp;";
250         } 
251         else 
252         {
253         display_note(_("There are no unsettled transactions to allocate."), 0, 1);
254         echo "<center>";
255     }
256
257         submit('Cancel', _("Back to Allocations"));
258         echo "</center><br><br>";
259
260         end_form();
261 }
262
263 //--------------------------------------------------------------------------------
264
265 if (isset($_GET['trans_no']) && isset($_GET['trans_type']))
266 {
267         get_allocations_for_transaction($_GET['trans_type'], $_GET['trans_no']);
268 }
269
270 if (isset($_SESSION['alloc']))
271 {
272         edit_allocations_for_transaction($_SESSION['alloc']->type, $_SESSION['alloc']->trans_no);
273 }
274
275 //--------------------------------------------------------------------------------
276
277 end_page();
278
279 ?>