Inserted Copyright Notice and fixed graphic items
[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 Affero General Public License,
5         AGPL, as published by the Free Software Foundation, either version 
6         3 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/agpl-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 $table_style, $Ajax;
19         start_table("width=80% $table_style");
20         echo "<tr><td valign=top>"; // outer table
21         echo "<table>";
22
23         $customer_error = "";
24         $change_prices = 0;
25
26     if (!isset($_POST['customer_id']) && (get_global_customer() != reserved_words::get_all()))
27         $_POST['customer_id'] = get_global_customer();
28
29         customer_list_row(_("Customer:"), 'customer_id', null, 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);
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'] = references::get_next(11);
84         if ($_SESSION['Items']->trans_no==0)
85             ref_row(_("Reference").':', 'ref');
86         else
87             label_row(_("Reference").':', $_POST['ref'] );
88
89
90
91         echo "</table>";
92
93         echo "</td><td>"; // outer table
94
95         if (!is_company_currency($order->customer_currency))
96         {
97             div_start('currency');
98                 echo "<table height='5'>";
99                 label_row(_("Customer Currency:"), $order->customer_currency);
100                 exchange_rate_display($order->customer_currency, get_company_currency(),
101                         $_POST['OrderDate']);
102                 echo "</table>";
103                 echo "</td><td>"; // outer table
104             div_end();
105         }
106
107         echo "<table height='5'>";
108
109     if (!isset($_POST['sales_type_id']))
110         $_POST['sales_type_id'] = $order->sales_type;
111     sales_types_list_row(_("Sales Type"), 'sales_type_id', $_POST['sales_type_id'], true);
112         
113         if ($order->sales_type != $_POST['sales_type_id']) {
114                 $myrow = get_sales_type($_POST['sales_type_id']);
115                 $order->set_sales_type($myrow['id'], $myrow['sales_type'],
116                 $myrow['tax_included'], $myrow['factor']);
117                 $Ajax->activate('sales_type_id');
118                 $change_prices = 1;
119         }
120
121     shippers_list_row(_("Shipping Company:"), 'ShipperID', $order->ship_via);
122
123         label_row(_("Customer Discount:"), ($order->default_discount * 100) . "%");
124         echo "</table>";
125
126         echo "</td><td>"; // outer table
127
128         echo "<table height='5'>";
129
130         if (!isset($_POST['OrderDate']) || $_POST['OrderDate'] == "")
131                 $_POST['OrderDate'] = $order->document_date;
132
133         date_row(_("Date:"), 'OrderDate', '', null, 0, 0, 0, null, true);
134         if (isset($_POST['_OrderDate_changed'])) {
135                 $change_prices = 1;
136                 $Ajax->activate('currency');
137         }
138         // 2008-11-12 Joe Hunt added dimensions
139         $dim = get_company_pref('use_dimension');
140         if ($dim > 0)
141                 dimensions_list_row(_("Dimension").":", 'dimension_id', 
142                         null, true, ' ', false, 1, false);
143         else
144                 hidden('dimension_id', 0);
145         if ($dim > 1)
146                 dimensions_list_row(_("Dimension")." 2:", 'dimension2_id', 
147                         null, true, ' ', false, 2, false);
148         else
149                 hidden('dimension2_id', 0);
150
151         echo "</table>";
152
153         echo "</td></tr>";
154
155         end_table(1); // outer table
156
157         if ($change_prices != 0) {
158                 foreach ($order->line_items as $line_no=>$item) {
159                         $line = &$order->line_items[$line_no];
160                         $line->price = get_price($line->stock_id, $order->customer_currency,
161                                 $order->sales_type, $order->price_factor, get_post('OrderDate'));
162                 //              $line->discount_percent = $order->default_discount;
163                 }
164             $Ajax->activate('items_table');
165         }
166
167
168         return $customer_error;
169 }
170
171 //---------------------------------------------------------------------------------
172
173 function display_credit_items($title, &$order)
174 {
175     global $table_style, $path_to_root;
176
177     display_heading($title);
178     div_start('items_table');
179     start_table("$table_style width=90%");
180     $th = array(_("Item Code"), _("Item Description"), _("Quantity"), _("Unit"),
181         _("Price"), _("Discount %"), _("Total"),'');
182
183     if (count($order->line_items)) $th[]= '';
184
185     table_header($th);
186
187     $subtotal = 0;
188     $k = 0;  //row colour counter
189
190     $id = find_submit('Edit');
191
192     foreach ($order->line_items as $line_no=>$line)
193     {
194         $line_total =   round($line->qty_dispatched * $line->price * (1 - $line->discount_percent),
195            user_price_dec());
196
197         if ( $id != $line_no)
198         {
199             alt_table_row_color($k);
200
201             label_cell("<a target='_blank' href='$path_to_root/inventory/inquiry/stock_status.php?" . SID . "stock_id=" . $line->stock_id . "'>$line->stock_id</a>");
202             label_cell($line->item_description, "nowrap");
203             qty_cell($line->qty_dispatched, false, get_qty_dec($line->stock_id));
204             label_cell($line->units);
205             amount_cell($line->price);
206
207                 percent_cell($line->discount_percent * 100);
208             amount_cell($line_total);
209
210             edit_button_cell("Edit$line_no", _('Edit'),
211                                 _('Edit document line'));
212             delete_button_cell("Delete$line_no", _('Delete'),
213                                 _('Remove line from document'));
214
215             end_row();
216         }
217         else
218         {
219             credit_edit_item_controls($order, $k, $line_no);
220         }
221
222         $subtotal += $line_total;
223     }
224
225     if ($id==-1)
226         credit_edit_item_controls($order, $k);
227
228     $display_sub_total = price_format($subtotal);
229     label_row(_("Sub-total"), $display_sub_total, "colspan=6 align=right", "align=right", 2);
230
231     if (!isset($_POST['ChargeFreightCost']) OR ($_POST['ChargeFreightCost'] == ""))
232         $_POST['ChargeFreightCost'] = price_format(0);
233
234     amount_cells_ex(_("Shipping"), 'ChargeFreightCost', 8, 8, $_POST['ChargeFreightCost'], "colspan=6 align=right");
235     label_cell('', 'colspan=2');
236
237     $taxes = $order->get_taxes($_POST['ChargeFreightCost']);
238
239     $tax_total = display_edit_tax_items($taxes, 6, $_SESSION['Items']->tax_included);
240
241     $display_total = price_format(($subtotal + $_POST['ChargeFreightCost'] + $tax_total));
242
243     label_row(_("Credit Note Total"), $display_total, "colspan=6 align=right","class='amount'", 2);
244
245     end_table();
246     div_end();
247 }
248
249 //---------------------------------------------------------------------------------
250
251 function credit_edit_item_controls(&$order, $rowcounter, $line_no=-1)
252 {
253         global $Ajax;
254         alt_table_row_color($rowcounter);
255         $id = find_submit('Edit');
256
257         if ($line_no!=-1 && $line_no == $id)
258         {
259                 $_POST['stock_id'] = $order->line_items[$id]->stock_id;
260                 $_POST['qty'] = qty_format($order->line_items[$id]->qty_dispatched, $_POST['stock_id'], $dec);
261                 $_POST['price'] = price_format($order->line_items[$id]->price);
262                 $_POST['Disc'] = percent_format(($order->line_items[$id]->discount_percent)*100);
263                 $_POST['units'] = $order->line_items[$id]->units;
264                 hidden('stock_id', $_POST['stock_id']);
265                 label_cell($_POST['stock_id']);
266                 label_cell($order->line_items[$id]->item_description, "nowrap");
267             $Ajax->activate('items_table');
268         }
269         else
270         {
271                 stock_items_list_cells(null,'stock_id', null, false, true);
272                 if (list_updated('stock_id')) {
273                             $Ajax->activate('price');
274                             $Ajax->activate('qty');
275                             $Ajax->activate('units');
276                             $Ajax->activate('line_total');
277                 }
278                 $item_info = get_item_edit_info($_POST['stock_id']);
279
280                 $dec = $item_info['decimals'];
281                 $_POST['qty'] = number_format2(0, $dec);
282                 $_POST['units'] = $item_info["units"];
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                 button_cell('UpdateItem', _("Update"),
301                                 _('Confirm changes'), ICON_UPDATE);
302                 button_cell('CancelItemChanges', _("Cancel"),
303                                 _('Cancel changes'), ICON_CANCEL);
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 ?>