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