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