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