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