Common ui code for allocations moved toallocation_cart.inc
[fa-stable.git] / includes / ui / allocation_cart.inc
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 /* definition of the Debtor Receipt/Credit note allocation class */
13
14 //-----------------------------------------------------------------------------------
15
16 class allocation 
17 {
18
19         var $trans_no; 
20         var $type;
21         var $person_id;
22         var $person_name;
23         var $date_;
24         var $amount; /*Total amount of the transaction in FX */
25         
26         var $allocs; /*array of transactions allocated to */    
27
28         function allocation($trans_no, $type, $person_id, $person_name, $amount, $date_)
29         {
30                 $this->allocs = array();
31                 
32                 $this->trans_no = $trans_no;
33                 $this->type = $type;
34                 $this->person_id = $person_id;
35                 $this->person_name = $person_name;
36                 $this->amount = $amount;
37                 $this->date_ = $date_;
38         }
39
40         function add_item($type, $type_no, $date_, $due_date, $amount, $amount_allocated, 
41                 $current_allocated)
42         {
43                 if ($amount > 0)
44                 {
45                         $this->allocs[count($this->allocs)] = new allocation_item($type, $type_no, 
46                                 $date_, $due_date, $amount, $amount_allocated, $current_allocated);
47                         return true;
48                 } 
49                 else 
50                 {
51                         return false;
52                 }
53         }
54         
55         function update_item($index, $type, $type_no, $date_, $due_date, 
56                 $amount, $amount_allocated, $current_allocated)
57         {
58                 if ($amount > 0)
59                 {
60                         $this->allocs[$index] = new allocation_item($type, $type_no, 
61                                 $date_, $due_date, $amount, $amount_allocated, $current_allocated);
62                         return true;
63                 } 
64                 else 
65                 {
66                         return false;
67                 }
68         }       
69         
70         function add_or_update_item($type, $type_no, $date_, $due_date, 
71                 $amount, $amount_allocated, $current_allocated)
72         {
73                 for ($i = 0; $i < count($this->allocs); $i++) 
74                 {
75                         $item = $this->allocs[$i];
76                         if (($item->type == $type) && ($item->type_no == $type_no)) 
77                         {
78                                 return $this->update_item($i, $type, $type_no, $date_, $due_date, 
79                                         $amount, $amount_allocated, $current_allocated);
80                         }  
81                 }
82         return $this->add_item($type, $type_no, $date_, $due_date, 
83                 $amount, $amount_allocated, $current_allocated);                                
84         }                                               
85
86
87
88 //-----------------------------------------------------------------------------------
89
90 class allocation_item 
91 {
92
93         var $type;
94         var $type_no;
95         
96         var $date_;
97         var $due_date;
98         
99         var $amount_allocated;
100         var $amount;
101         
102         var $current_allocated;
103         
104         function allocation_item ($type, $type_no, $date_, $due_date, $amount, 
105                 $amount_allocated, $current_allocated)
106         {
107
108                 $this->type = $type;
109                 $this->type_no = $type_no;
110                 
111                 $this->date_ = $date_;
112                 $this->due_date = $due_date;
113                 
114                 $this->amount = $amount;
115                 $this->amount_allocated = $amount_allocated;
116                 $this->current_allocated = $current_allocated;
117         }
118 }
119
120 //--------------------------------------------------------------------------------
121 function get_allocations_for_transaction($type, $trans_no)
122 {
123         $sup = $type == 21 || $type == 22;
124
125         if ($trans_no) {
126                 $trans = $sup ? get_supp_trans($trans_no, $type) 
127                         : get_customer_trans($trans_no, $type);
128
129                 $_SESSION['alloc'] = new allocation($trans_no, $type,
130                         $trans[$sup ? 'supplier_id':'debtor_no'], 
131                         $trans[$sup ? "supplier_name":"DebtorName"],
132                         $trans["Total"], sql2date($trans["tran_date"]));
133         } else {
134                 $_SESSION['alloc'] = new allocation($trans_no, $type,
135                         $_POST[$sup ? 'supplier_id':'customer_id'], '', 0, 
136                         $_POST[$sup ? 'DatePaid':'DateBanked'] );
137         }
138
139         /* Now populate the array of possible (and previous actual) allocations for this customer/supplier */
140         /*First get the transactions that have outstanding balances ie Total-alloc >0 */
141
142         if ($sup)
143                 $trans_items = get_allocatable_to_supp_transactions($_SESSION['alloc']->person_id);
144         else
145                 $trans_items = get_allocatable_to_cust_transactions($_SESSION['alloc']->person_id);
146
147         while ($myrow = db_fetch($trans_items))
148         {
149                 $_SESSION['alloc']->add_item($myrow["type"], $myrow["trans_no"],
150                         sql2date($myrow["tran_date"]),
151                         sql2date($myrow["due_date"]),
152                         $myrow["Total"], // trans total
153                         $myrow["alloc"], // trans total allocated
154                         0); // this allocation
155         }
156
157         if ($trans_no == 0) return; // this is new payment
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         if ($sup)
164                 $trans_items = get_allocatable_to_supp_transactions($_SESSION['alloc']->person_id, $trans_no, $type);
165         else
166                 $trans_items = get_allocatable_to_cust_transactions($_SESSION['alloc']->person_id, $trans_no, $type);
167         
168         while ($myrow = db_fetch($trans_items))
169         {
170                 $_SESSION['alloc']->add_or_update_item ($myrow["type"], $myrow["trans_no"],
171                         sql2date($myrow["tran_date"]),
172                         sql2date($myrow["due_date"]),
173                         $myrow["Total"],
174                         $myrow["alloc"] - $myrow["amt"], $myrow["amt"]);
175         }
176 }
177
178 //---------------------------------------------------------------------------------------------------
179
180 function show_allocatable($show_totals) {
181
182         global $table_style;
183         
184     $k = $counter = $total_allocated = 0;
185
186         if (count($_SESSION['alloc']->allocs)) 
187         {
188                 start_table($table_style);
189                 $th = array(_("Transaction Type"), _("#"), _("Date"), _("Due Date"), _("Amount"),
190                         _("Other Allocations"), _("This Allocation"), _("Left to Allocate"),'','');
191                 table_header($th);
192
193                 foreach ($_SESSION['alloc']->allocs as $alloc_item)
194             {
195                         alt_table_row_color($k);
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, 
212                                  price_format($un_allocated), false));
213                         end_row();
214
215                 $total_allocated += input_num('amount' . $counter);
216                     $counter++;
217                 }
218                 if ($show_totals) {
219                 label_row(_("Total Allocated"), price_format($total_allocated),
220                         "colspan=6 align=right", "align=right id='total_allocated'", 3);
221                         $amount = $_SESSION['alloc']->amount;
222
223                         if ($_SESSION['alloc']->type == 21 || $_SESSION['alloc']->type == 22) 
224                                 $amount = -$amount;
225                         
226                         if ($amount - $total_allocated < 0)
227                 {
228                         $font1 = "<font color=red>";
229                         $font2 = "</font>";
230             }
231                 else
232                         $font1 = $font2 = "";
233                         $left_to_allocate = price_format($amount - $total_allocated);
234                 label_row(_("Left to Allocate"), $font1 . $left_to_allocate . $font2, 
235                                 "colspan=6 align=right", "nowrap align=right id='left_to_allocate'",
236                                  3);
237                 }
238                 end_table(1);
239         }
240         hidden('TotalNumberOfAllocs', $counter);
241 }
242 //--------------------------------------------------------------------------------
243
244 function check_allocations()
245 {
246         $total_allocated = 0;
247
248         for ($counter = 0; $counter < $_POST["TotalNumberOfAllocs"]; $counter++)
249         {
250                 if (!check_num('amount' . $counter, 0))
251                 {
252                         display_error(_("The entry for one or more amounts is invalid or negative."));
253                         set_focus('amount'.$counter);
254                         return false;
255                  }
256
257                   /*Now check to see that the AllocAmt is no greater than the
258                  amount left to be allocated against the transaction under review */
259                  if (input_num('amount' . $counter) > get_post('un_allocated' . $counter))
260                  {
261                      //$_POST['amount' . $counter] = $_POST['un_allocated' . $counter];
262                  }
263
264                  $_SESSION['alloc']->allocs[$counter]->current_allocated = input_num('amount' . $counter);
265
266                  $total_allocated += input_num('amount' . $counter);
267         }
268
269         $amount = $_SESSION['alloc']->amount;
270         if ($_SESSION['alloc']->type == 21 || $_SESSION['alloc']->type == 22) 
271                 $amount = -$amount;
272
273         if ($total_allocated - $amount  > sys_prefs::allocation_settled_allowance())
274         {
275                 display_error(_("These allocations cannot be processed because the amount allocated is more than the total amount left to allocate."));
276                 return false;
277         }
278
279         return true;
280 }
281 //-----------------------------------------------------------------------------------
282
283 function handle_allocate()
284 {
285         $sup = $_SESSION['alloc']->type == 21 || $_SESSION['alloc']->type == 22;
286
287         begin_transaction();
288
289         if ($sup)
290                 clear_supp_alloctions($_SESSION['alloc']->type, $_SESSION['alloc']->trans_no, $_SESSION['alloc']->date_);
291         else
292                 clear_cust_alloctions($_SESSION['alloc']->type, $_SESSION['alloc']->trans_no, $_SESSION['alloc']->date_);
293
294         // now add the new allocations
295         $total_allocated = 0;
296         foreach ($_SESSION['alloc']->allocs as $alloc_item)
297         {
298                 if ($alloc_item->current_allocated > 0)
299                 {
300                         if ($sup) {
301                                 add_supp_allocation($alloc_item->current_allocated,
302                                         $_SESSION['alloc']->type, $_SESSION['alloc']->trans_no,
303                                 $alloc_item->type, $alloc_item->type_no, $_SESSION['alloc']->date_);
304
305                                 update_supp_trans_allocation($alloc_item->type, $alloc_item->type_no,
306                                         $alloc_item->current_allocated);
307                         } else {
308                                 add_cust_allocation($alloc_item->current_allocated,
309                                         $_SESSION['alloc']->type, $_SESSION['alloc']->trans_no,
310                                 $alloc_item->type, $alloc_item->type_no, $_SESSION['alloc']->date_);
311                         
312                                 update_debtor_trans_allocation($alloc_item->type, $alloc_item->type_no,
313                                         $alloc_item->current_allocated);
314                         }
315                         // Exchange Variations Joe Hunt 2008-09-20 ////////////////////////////////////////
316
317                         exchange_variation($_SESSION['alloc']->type, $_SESSION['alloc']->trans_no,
318                                 $alloc_item->type, $alloc_item->type_no, $_SESSION['alloc']->date_,
319                                 $alloc_item->current_allocated,
320                                 $sup ? payment_person_types::supplier() :payment_person_types::customer());
321                                 
322
323                         ///////////////////////////////////////////////////////////////////////////
324                         $total_allocated += $alloc_item->current_allocated;
325                 }
326
327         }  /*end of the loop through the array of allocations made */
328         if ($sup)
329                 update_supp_trans_allocation($_SESSION['alloc']->type,
330                         $_SESSION['alloc']->trans_no, $total_allocated);
331         else
332                 update_debtor_trans_allocation($_SESSION['alloc']->type,
333                         $_SESSION['alloc']->trans_no, $total_allocated);
334         
335         commit_transaction();
336
337 }
338 ?>