Fixed page update after item search.
[fa-stable.git] / sales / includes / ui / sales_credit_ui.inc
1 <?php
2
3 include_once($path_to_root . "/includes/ui.inc");
4
5 // ------------------------------------------------------------------------------
6
7 function display_credit_header(&$order)
8 {
9         global $table_style, $Ajax;
10         start_table("width=80% $table_style");
11         echo "<tr><td valign=top>"; // outer table
12         echo "<table>";
13
14         $customer_error = "";
15         $change_prices = 0;
16
17     if (!isset($_POST['customer_id']) && (get_global_customer() != reserved_words::get_all()))
18         $_POST['customer_id'] = get_global_customer();
19
20         customer_list_row(_("Customer:"), 'customer_id', null, false, true);
21
22         if ($order->customer_id != $_POST['customer_id'] /*|| $order->sales_type != $_POST['sales_type_id']*/)
23         {
24                 // customer has changed
25
26                 // delete all the order items - drastic but necessary because of
27                 // change of currency, sales type, etc
28 //              $order->clear_items();
29
30                 // clear the branch selection
31 //              unset($_POST['branch_id']);
32                         $_POST['branch_id'] = '';
33                         $Ajax->activate('branch_id');
34         }
35
36         customer_branches_list_row(_("Branch:"), $_POST['customer_id'], 
37           'branch_id', null, false, true, true);
38
39         //if (($_SESSION['credit_items']->order_no == 0) ||
40         //      ($order->customer_id != $_POST['customer_id']) ||
41         //      ($order->Branch != $_POST['branch_id']))
42         //      $customer_error = get_customer_details_to_order($order, $_POST['customer_id'], $_POST['branch_id']);
43         if (($order->customer_id != $_POST['customer_id']) ||
44                 ($order->Branch != $_POST['branch_id']))
45           {
46
47                                 $old_order = (PHP_VERSION<5) ? $order : clone( $order );
48                                 $customer_error = get_customer_details_to_order($order, $_POST['customer_id'], $_POST['branch_id']);
49
50                                 $_POST['Location'] = $order->Location;
51                                 $_POST['deliver_to'] = $order->deliver_to;
52                                 $_POST['delivery_address'] = $order->delivery_address;
53                                 $_POST['phone'] = $order->phone;
54                                 $Ajax->activate('Location');
55                                 $Ajax->activate('deliver_to');
56                                 $Ajax->activate('phone');
57                                 $Ajax->activate('delivery_address');
58                                 // change prices if necessary
59                                 // what about discount in template case?
60                                 if ($old_order->customer_currency != $order->customer_currency) {
61                                     $change_prices = 1;
62                                 }
63                                 if ($old_order->sales_type != $order->sales_type) {
64                                 //  || $old_order->default_discount!=$order->default_discount
65                                         $_POST['sales_type'] = $order->sales_type;
66                                     $Ajax->activate('sales_type_id');
67                                     $change_prices = 1;
68                                 }
69                                 unset($old_order);
70           }
71         set_global_customer($_POST['customer_id']);
72
73         if (!isset($_POST['ref']))
74                 $_POST['ref'] = references::get_next(11);
75         if ($_SESSION['Items']->trans_no==0)
76             ref_row(_("Reference").':', 'ref');
77         else
78             label_row(_("Reference").':', $_POST['ref'] );
79
80
81
82         echo "</table>";
83
84         echo "</td><td>"; // outer table
85
86         if (!is_company_currency($order->customer_currency))
87         {
88                 echo "<table height='5'>";
89                 label_row(_("Customer Currency:"), $order->customer_currency);
90                 exchange_rate_display($order->customer_currency, get_company_currency(),
91                         $_POST['OrderDate'], true);
92                 echo "</table>";
93                 echo "</td><td>"; // outer table
94         }
95
96         echo "<table height='5'>";
97
98     if (!isset($_POST['sales_type_id']))
99         $_POST['sales_type_id'] = $order->sales_type;
100     sales_types_list_row(_("Sales Type"), 'sales_type_id', $_POST['sales_type_id'], true);
101         
102         if ($order->sales_type != $_POST['sales_type_id']) {
103                 $myrow = get_sales_type($_POST['sales_type_id']);
104                 $order->set_sales_type($myrow['id'], $myrow['sales_type'],
105                 $myrow['tax_included'], $myrow['factor']);
106                 $Ajax->activate('sales_type_id');
107                 $change_prices = 1;
108         }
109
110         if ($change_prices != 0) {
111                 foreach ($order->line_items as $line_no=>$item) {
112                         $line = &$order->line_items[$line_no];
113                         $line->price = get_price($line->stock_id, $order->customer_currency,
114                                 $order->sales_type, $order->price_factor, $order->document_date);
115                 //              $line->discount_percent = $order->default_discount;
116                 }
117             $Ajax->activate('items_table');
118         }
119
120         label_row(_("Customer Discount:"), ($order->default_discount * 100) . "%");
121         echo "</table>";
122
123         echo "</td><td>"; // outer table
124
125         echo "<table height='5'>";
126
127         if (!isset($_POST['OrderDate']) || $_POST['OrderDate'] == "")
128                 $_POST['OrderDate'] = $order->document_date;
129
130         date_row(_("Date:"), 'OrderDate');
131
132     shippers_list_row(_("Shipping Company:"), 'ShipperID', $order->ship_via);
133
134         echo "</table>";
135
136         echo "</td></tr>";
137
138         end_table(1); // outer table
139
140         return $customer_error;
141 }
142
143 //---------------------------------------------------------------------------------
144
145 function display_credit_items($title, &$order)
146 {
147     global $table_style, $path_to_root;
148
149     display_heading($title);
150     div_start('items_table');
151     start_table("$table_style width=90%");
152     $th = array(_("Item Code"), _("Item Description"), _("Quantity"), _("Unit"),
153         _("Price"), _("Discount %"), _("Total"),'');
154
155     if (count($order->line_items)) $th[]= '';
156
157     table_header($th);
158
159     $subtotal = 0;
160     $k = 0;  //row colour counter
161
162     $id = find_submit('Edit');
163
164     foreach ($order->line_items as $line_no=>$line)
165     {
166         $line_total =   round($line->qty_dispatched * $line->price * (1 - $line->discount_percent),
167            user_price_dec());
168
169         if ( $id != $line_no)
170         {
171             alt_table_row_color($k);
172
173             label_cell("<a target='_blank' href='$path_to_root/inventory/inquiry/stock_status.php?" . SID . "stock_id=" . $line->stock_id . "'>$line->stock_id</a>");
174             label_cell($line->item_description, "nowrap");
175             qty_cell($line->qty_dispatched, false, get_qty_dec($line->stock_id));
176             label_cell($line->units);
177             amount_cell($line->price);
178
179             amount_cell($line->discount_percent * 100);
180             amount_cell($line_total);
181
182             edit_button_cell("Edit$line_no", _('Edit'),
183                                 _('Edit document line'));
184             edit_button_cell("Delete$line_no", _('Delete'),
185                                 _('Remove line from document'));
186
187             end_row();
188         }
189         else
190         {
191             credit_edit_item_controls($order, $k, $line_no);
192         }
193
194         $subtotal += $line_total;
195     }
196
197     if ($id==-1)
198         credit_edit_item_controls($order, $k);
199
200     $display_sub_total = price_format($subtotal);
201     label_row(_("Sub-total"), $display_sub_total, "colspan=6 align=right", "align=right", 2);
202
203     if (!isset($_POST['ChargeFreightCost']) OR ($_POST['ChargeFreightCost'] == ""))
204         $_POST['ChargeFreightCost'] = price_format(0);
205
206     amount_cells_ex(_("Shipping"), 'ChargeFreightCost', 8, 8, $_POST['ChargeFreightCost'], "colspan=6 align=right");
207     label_cell('', 'colspan=2');
208
209     $taxes = $order->get_taxes($_POST['ChargeFreightCost']);
210
211     $tax_total = display_edit_tax_items($taxes, 6, $_SESSION['Items']->tax_included);
212
213     $display_total = price_format(($subtotal + $_POST['ChargeFreightCost'] + $tax_total));
214
215     label_row(_("Credit Note Total"), $display_total, "colspan=6 align=right","class='amount'", 2);
216
217     end_table();
218     div_end();
219 }
220
221 //---------------------------------------------------------------------------------
222
223 function credit_edit_item_controls(&$order, $rowcounter, $line_no=-1)
224 {
225         global $Ajax;
226         alt_table_row_color($rowcounter);
227         $id = find_submit('Edit');
228
229         if ($line_no!=-1 && $line_no == $id)
230         {
231                 $_POST['stock_id'] = $order->line_items[$id]->stock_id;
232                 $_POST['qty'] = qty_format($order->line_items[$id]->qty_dispatched, $_POST['stock_id'], $dec);
233                 $_POST['price'] = price_format($order->line_items[$id]->price);
234                 $_POST['Disc'] = percent_format(($order->line_items[$id]->discount_percent)*100);
235                 $_POST['units'] = $order->line_items[$id]->units;
236                 hidden('stock_id', $_POST['stock_id']);
237                 label_cell($_POST['stock_id']);
238                 label_cell($order->line_items[$id]->item_description, "nowrap");
239             $Ajax->activate('items_table');
240         }
241         else
242         {
243                 stock_items_list_cells(null,'stock_id', null, false, true);
244                 if (list_updated('stock_id')) {
245                             $Ajax->activate('price');
246                             $Ajax->activate('qty');
247                             $Ajax->activate('units');
248                             $Ajax->activate('line_total');
249                 }
250                 $item_info = get_item_edit_info($_POST['stock_id']);
251
252                 $_POST['units'] = $item_info["units"];
253                 $_POST['qty'] = qty_format(0, $_POST['stock_id'], $dec);
254                 $_POST['price'] = price_format(get_price($_POST['stock_id'], $order->customer_currency,
255                     $order->sales_type, $order->price_factor, $order->document_date));
256
257                 // default to the customer's discount %
258                 $_POST['Disc'] = percent_format($order->default_discount * 100);
259         }
260
261         qty_cells(null, 'qty', $_POST['qty'], null, null, $dec);
262
263         label_cell($_POST['units']);
264         amount_cells(null, 'price',  null);
265         small_amount_cells(null, 'Disc', percent_format(0), null, null, user_percent_dec());
266
267         amount_cell($_POST['qty'] * $_POST['price'] * (1 - $_POST['Disc']/100));
268
269         if ($id!=-1)
270         {
271                 edit_button_cell('UpdateItem', _("Update"),
272                                 _('Confirm changes'));
273                 edit_button_cell('CancelItemChanges', _("Cancel"),
274                                 _('Cancel changes'));
275                 hidden('line_no', $line_no);
276                 set_focus('qty');
277         }
278         else
279         {
280                 submit_cells('AddItem', _("Add Item"), "colspan=2",
281                     _('Add new item to document'), true);
282         }
283
284         end_row();
285 }
286
287
288 //---------------------------------------------------------------------------------
289
290 function credit_options_controls()
291 {
292         global $table_style2, $Ajax;
293         echo "<br>";
294
295 if (isset($_POST['_CreditType_update']))
296         $Ajax->activate('options');
297
298  div_start('options');
299         start_table("$table_style2");
300
301         credit_type_list_row(_("Credit Note Type"), 'CreditType', null, true);
302
303         if ($_POST['CreditType'] == "Return")
304         {
305
306                 /*if the credit note is a return of goods then need to know which location to receive them into */
307                 if (!isset($_POST['Location']))
308                         $_POST['Location'] = $_SESSION['Items']->Location;
309                 locations_list_row(_("Items Returned to Location"), 'Location', $_POST['Location']);
310         }
311         else
312         {
313                 /* the goods are to be written off to somewhere */
314                 gl_all_accounts_list_row(_("Write off the cost of the items to"), 'WriteOffGLCode', null);
315         }
316
317         textarea_row(_("Memo"), "CreditText", null, 51, 3);
318         echo "</table>";
319  div_end();
320 }
321
322
323 //---------------------------------------------------------------------------------
324
325 ?>