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