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