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