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");
14 // ------------------------------------------------------------------------------
16 function display_credit_header(&$order)
20 start_outer_table(TABLESTYLE, "width='80%'");
26 if (!isset($_POST['customer_id']) && (get_global_customer() != ALL_TEXT))
27 $_POST['customer_id'] = get_global_customer();
29 customer_list_row(_("Customer:"), 'customer_id', null, false, true, false, true);
31 if ($order->customer_id != $_POST['customer_id'] /*|| $order->sales_type != $_POST['sales_type_id']*/)
33 // customer has changed
34 $Ajax->activate('branch_id');
37 customer_branches_list_row(_("Branch:"), $_POST['customer_id'],
38 'branch_id', null, false, true, true, true);
40 if (($order->customer_id != $_POST['customer_id']) ||
41 ($order->Branch != $_POST['branch_id']))
44 $old_order = (PHP_VERSION<5) ? $order : clone $order;
45 $customer_error = get_customer_details_to_order($order, $_POST['customer_id'], $_POST['branch_id']);
47 $_POST['Location'] = $order->Location;
48 $_POST['deliver_to'] = $order->deliver_to;
49 $_POST['delivery_address'] = $order->delivery_address;
50 $_POST['phone'] = $order->phone;
51 $Ajax->activate('Location');
52 $Ajax->activate('deliver_to');
53 $Ajax->activate('phone');
54 $Ajax->activate('delivery_address');
55 // change prices if necessary
56 // what about discount in template case?
57 if ($old_order->customer_currency != $order->customer_currency) {
60 if ($old_order->sales_type != $order->sales_type) {
61 $_POST['sales_type_id'] = $order->sales_type;
62 $Ajax->activate('sales_type_id');
65 if ($old_order->dimension_id != $order->dimension_id) {
66 $_POST['dimension_id'] = $order->dimension_id;
67 $Ajax->activate('dimension_id');
69 if ($old_order->dimension2_id != $order->dimension2_id) {
70 $_POST['dimension2_id'] = $order->dimension2_id;
71 $Ajax->activate('dimension2_id');
75 set_global_customer($_POST['customer_id']);
77 if (!isset($_POST['ref']))
78 $_POST['ref'] = $Refs->get_next(ST_CUSTCREDIT, null, array('customer' => get_post('customer_id'),
79 'branch' => get_post('branch_id'), 'date' => get_post('OrderDate')));
80 if ($order->trans_no==0)
81 ref_row(_("Reference").':', 'ref');
83 label_row(_("Reference").':', $order->reference);
86 if (!is_company_currency($order->customer_currency))
90 label_row(_("Customer Currency:"), $order->customer_currency);
91 exchange_rate_display(get_company_currency(), $order->customer_currency,
96 if (!isset($_POST['sales_type_id']))
97 $_POST['sales_type_id'] = $order->sales_type;
98 sales_types_list_row(_("Sales Type"), 'sales_type_id', $_POST['sales_type_id'], true);
100 if ($order->sales_type != $_POST['sales_type_id']) {
101 $myrow = get_sales_type($_POST['sales_type_id']);
102 $order->set_sales_type($myrow['id'], $myrow['sales_type'],
103 $myrow['tax_included'], $myrow['factor']);
104 $Ajax->activate('sales_type_id');
108 shippers_list_row(_("Shipping Company:"), 'ShipperID', $order->ship_via);
110 label_row(_("Customer Discount:"), ($order->default_discount * 100) . "%");
114 if (!isset($_POST['OrderDate']) || $_POST['OrderDate'] == "")
115 $_POST['OrderDate'] = $order->document_date;
117 date_row(_("Date:"), 'OrderDate', '', $order->trans_no==0, 0, 0, 0, null, true);
118 if (isset($_POST['_OrderDate_changed'])) {
119 if (!is_company_currency($order->customer_currency)
120 && (get_base_sales_type()>0)) {
123 $Ajax->activate('_ex_rate');
126 $dim = get_company_pref('use_dimension');
128 dimensions_list_row(_("Dimension").":", 'dimension_id',
129 null, true, ' ', false, 1, false);
131 hidden('dimension_id', 0);
133 dimensions_list_row(_("Dimension")." 2:", 'dimension2_id',
134 null, true, ' ', false, 2, false);
136 hidden('dimension2_id', 0);
138 end_outer_table(1); // outer table
140 if ($change_prices != 0) {
141 foreach ($order->line_items as $line_no=>$item) {
142 $line = &$order->line_items[$line_no];
143 $line->price = get_price($line->stock_id, $order->customer_currency,
144 $order->sales_type, $order->price_factor, get_post('OrderDate'));
146 $Ajax->activate('items_table');
150 return $customer_error;
153 //---------------------------------------------------------------------------------
155 function display_credit_items($title, &$order)
157 global $path_to_root;
159 display_heading($title);
160 div_start('items_table');
161 start_table(TABLESTYLE, "width='90%'");
162 $th = array(_("Item Code"), _("Item Description"), _("Quantity"), _("Unit"),
163 _("Price"), _("Discount %"), _("Total"),'');
165 if (count($order->line_items)) $th[]= '';
170 $k = 0; //row colour counter
172 $id = find_submit('Edit');
174 foreach ($order->get_items() as $line_no=>$line)
176 $line_total = round($line->qty_dispatched * $line->price * (1 - $line->discount_percent),
179 if ( $id != $line_no)
181 alt_table_row_color($k);
183 label_cell("<a target='_blank' href='$path_to_root/inventory/inquiry/stock_status.php?stock_id=" . $line->stock_id . "'>$line->stock_id</a>");
184 label_cell($line->item_description, "nowrap");
185 qty_cell($line->qty_dispatched, false, get_qty_dec($line->stock_id));
186 label_cell($line->units);
187 amount_cell($line->price);
189 percent_cell($line->discount_percent * 100);
190 amount_cell($line_total);
192 edit_button_cell("Edit$line_no", _('Edit'),
193 _('Edit document line'));
194 delete_button_cell("Delete$line_no", _('Delete'),
195 _('Remove line from document'));
201 credit_edit_item_controls($order, $k, $line_no);
204 $subtotal += $line_total;
208 credit_edit_item_controls($order, $k);
211 $display_sub_total = price_format($subtotal);
212 label_row(_("Sub-total"), $display_sub_total, "colspan=$colspan align=right", "align=right", 2);
214 if (!isset($_POST['ChargeFreightCost']) OR ($_POST['ChargeFreightCost'] == ""))
215 $_POST['ChargeFreightCost'] = 0;
217 label_cell(_("Shipping"), "colspan=$colspan align=right");
218 small_amount_cells(null, 'ChargeFreightCost', price_format(get_post('ChargeFreightCost',0)));
219 label_cell('', 'colspan=2');
222 $taxes = $order->get_taxes($_POST['ChargeFreightCost']);
224 $tax_total = display_edit_tax_items($taxes, $colspan, $order->tax_included, 2);
226 $display_total = price_format(($subtotal + $_POST['ChargeFreightCost'] + $tax_total));
228 label_row(_("Credit Note Total"), $display_total, "colspan=$colspan align=right","class='amount'", 2);
234 //---------------------------------------------------------------------------------
236 function credit_edit_item_controls(&$order, $rowcounter, $line_no=-1)
239 alt_table_row_color($rowcounter);
240 $id = find_submit('Edit');
242 if ($line_no!=-1 && $line_no == $id)
244 $_POST['stock_id'] = $order->line_items[$id]->stock_id;
245 $dec = get_qty_dec($_POST['stock_id']);
246 $_POST['qty'] = qty_format($order->line_items[$id]->qty_dispatched, $_POST['stock_id'], $dec);
247 $_POST['price'] = price_format($order->line_items[$id]->price);
248 $_POST['Disc'] = percent_format(($order->line_items[$id]->discount_percent)*100);
249 $units = $order->line_items[$id]->units;
250 hidden('stock_id', $_POST['stock_id']);
251 label_cell($_POST['stock_id']);
252 label_cell($order->line_items[$id]->item_description, "nowrap");
253 $Ajax->activate('items_table');
257 stock_items_list_cells(null,'stock_id', null, false, true);
258 if (list_updated('stock_id')) {
259 $Ajax->activate('price');
260 $Ajax->activate('qty');
261 $Ajax->activate('units');
262 $Ajax->activate('line_total');
264 $item_info = get_item_edit_info($_POST['stock_id']);
266 $dec = $item_info['decimals'];
267 $_POST['qty'] = number_format2(0, $dec);
268 $units = $item_info["units"];
269 $price = get_price($_POST['stock_id'],
270 $order->customer_currency, $order->sales_type,
271 $order->price_factor, get_post('OrderDate'));
272 $_POST['price'] = price_format($price);
274 // default to the customer's discount %
275 $_POST['Disc'] = percent_format($order->default_discount * 100);
278 qty_cells(null, 'qty', $_POST['qty'], null, null, $dec);
280 label_cell($units, '', 'units');
281 amount_cells(null, 'price');
282 small_amount_cells(null, 'Disc', percent_format($_POST['Disc']), null, null, user_percent_dec());
284 amount_cell(input_num('qty') * input_num('price') * (1 - input_num('Disc')/100), false, '', 'line_total');
288 button_cell('UpdateItem', _("Update"),
289 _('Confirm changes'), ICON_UPDATE);
290 button_cell('CancelItemChanges', _("Cancel"),
291 _('Cancel changes'), ICON_CANCEL);
292 hidden('line_no', $line_no);
297 submit_cells('AddItem', _("Add Item"), "colspan=2",
298 _('Add new item to document'), true);
305 //---------------------------------------------------------------------------------
307 function credit_options_controls($credit)
312 if (isset($_POST['_CreditType_update']))
313 $Ajax->activate('options');
315 div_start('options');
316 start_table(TABLESTYLE2);
318 credit_type_list_row(_("Credit Note Type"), 'CreditType', null, true);
320 if ($_POST['CreditType'] == "Return")
323 /*if the credit note is a return of goods then need to know which location to receive them into */
324 if (!isset($_POST['Location']))
325 $_POST['Location'] = $credit->Location;
326 locations_list_row(_("Items Returned to Location"), 'Location', $_POST['Location']);
330 /* the goods are to be written off to somewhere */
331 gl_all_accounts_list_row(_("Write off the cost of the items to"), 'WriteOffGLCode', null);
334 textarea_row(_("Memo"), "CreditText", null, 51, 3);
340 //---------------------------------------------------------------------------------