Overhead and labour cost changed to define standard costs.
[fa-stable.git] / inventory / includes / item_adjustments_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 include_once($path_to_root . "/includes/ui/items_cart.inc");
14
15 //--------------------------------------------------------------------------------
16
17 function add_to_order(&$order, $new_item, $new_item_qty, $standard_cost)
18 {
19     if ($order->find_cart_item($new_item))
20          display_error(_("For Part :") . $new_item . " " . "This item is already on this document. You can change the quantity on the existing line if necessary.");
21         else
22          $order->add_to_cart (count($order->line_items), $new_item, $new_item_qty, $standard_cost);
23 }
24
25 //--------------------------------------------------------------------------------
26
27 function display_order_header(&$order)
28 {
29         global $Refs;
30
31         start_outer_table(TABLESTYLE2, "width='70%'"); // outer table
32         table_section(1);
33
34         locations_list_row(_("Location:"), 'StockLocation', null, false, false, $order->fixed_asset);
35     date_row(_("Date:"), 'AdjDate', '', true);
36
37         table_section(2, "50%");
38
39         ref_row(_("Reference:"), 'ref', '', $Refs->get_next(ST_INVADJUST, null, array('location'=>get_post('StockLocation'), 'date'=>get_post('AdjDate'))),
40                  false, ST_INVADJUST);
41
42         end_outer_table(1); // outer table
43 }
44
45 //---------------------------------------------------------------------------------
46
47 function display_adjustment_items($title, &$order)
48 {
49         global $path_to_root;
50
51         display_heading($title);
52     div_start('items_table');
53         start_table(TABLESTYLE, "width='80%'");
54
55         if ($order->fixed_asset) {
56                 $th = array(_("Item Code"), _("Item Description"), _("Quantity"),
57                 _("Unit"), _("Unit Cost"), _("Initial Value"), "");
58         } else {
59                 $th = array(_("Item Code"), _("Item Description"), _("Quantity"),
60                 _("Unit"), _("Unit Cost"), _("Total"), "");
61                 if ( count($order->line_items)) $th[] = '';
62         }
63         if ( count($order->line_items)) $th[] = '';
64
65         table_header($th);
66         $total = 0;
67         $k = 0;  //row colour counter
68
69         $low_stock = $order->check_qoh($_POST['StockLocation'], $_POST['AdjDate']);
70         $id = find_submit('Edit');
71         foreach ($order->line_items as $line_no=>$stock_item)
72         {
73
74                 $total += ($stock_item->standard_cost * $stock_item->quantity);
75
76                 if ($id != $line_no)
77                 {
78                         if (in_array($stock_item->stock_id, $low_stock))
79                                 start_row("class='stockmankobg'");      // notice low stock status
80                         else 
81                                 alt_table_row_color($k);
82
83                 if ($order->fixed_asset)
84                                 label_cell($stock_item->stock_id);
85                 else
86                                 view_stock_status_cell($stock_item->stock_id);
87
88                         label_cell($stock_item->item_description);
89                 qty_cell($stock_item->quantity, false, get_qty_dec($stock_item->stock_id));
90                         label_cell($stock_item->units);
91                         amount_decimal_cell($stock_item->standard_cost);
92
93                         if ($order->fixed_asset) {
94                         $price = get_purchase_value($stock_item->stock_id);
95                                 amount_cell($price);
96                 } else
97                                 amount_cell($stock_item->standard_cost * $stock_item->quantity);
98
99                 if (!$order->fixed_asset) {
100                         edit_button_cell("Edit$line_no", _("Edit"),     _('Edit document line'));
101                 }
102                         delete_button_cell("Delete$line_no", _("Delete"), _('Remove line from document'));
103                         end_row();
104                 }
105                 else
106                 {
107                         adjustment_edit_item_controls($order, $line_no);
108                 }
109         }
110
111         if ($id == -1)
112                 adjustment_edit_item_controls($order);
113
114         label_row(_("Total"), number_format2($total,user_price_dec()), "align=right colspan=5", "align=right", 2);
115
116     end_table();
117         if ($low_stock)
118                 display_note(_("Marked items have insufficient quantities in stock as on day of adjustment."), 0, 1, "class='stockmankofg'");
119         div_end();
120 }
121
122 //---------------------------------------------------------------------------------
123
124 function adjustment_edit_item_controls(&$order, $line_no=-1)
125 {
126         global $Ajax;
127         start_row();
128
129         $dec2 = 0;
130         $id = find_submit('Edit');
131         if ($line_no != -1 && $line_no == $id)
132         {
133                 $_POST['stock_id'] = $order->line_items[$id]->stock_id;
134                 $_POST['qty'] = qty_format($order->line_items[$id]->quantity, 
135                         $order->line_items[$id]->stock_id, $dec);
136                 //$_POST['std_cost'] = price_format($order->line_items[$id]->standard_cost);
137                 $_POST['std_cost'] = price_decimal_format($order->line_items[$id]->standard_cost, $dec2);
138                 $_POST['units'] = $order->line_items[$id]->units;
139
140                 hidden('stock_id', $_POST['stock_id']);
141                 label_cell($_POST['stock_id']);
142                 label_cell($order->line_items[$id]->item_description, 'nowrap');
143             $Ajax->activate('items_table');
144         }
145         else
146         {
147                 if ($order->fixed_asset)
148                         stock_disposable_fa_list_cells(null, 'stock_id', null, false, true, $order->line_items);
149                 else
150                         stock_costable_items_list_cells(null, 'stock_id', null, false, true);
151
152                 if (list_updated('stock_id')) {
153                             $Ajax->activate('units');
154                             $Ajax->activate('qty');
155                             $Ajax->activate('std_cost');
156                             $Ajax->activate('std_cost_view');
157                             $Ajax->activate('initial_price');
158                 }
159
160         $item_info = get_item_edit_info($_POST['stock_id']);
161                 $dec = $item_info['decimals'];
162                 $_POST['qty'] = number_format2(0, $dec);
163                 $_POST['std_cost'] = price_decimal_format($item_info["material_cost"], $dec2);
164                 $_POST['units'] = $item_info["units"];
165         }
166
167         if ($order->fixed_asset) {
168                 hidden('qty', -1);
169                 qty_cell(1, false, 0);
170         } else
171                 qty_cells(null, 'qty', $_POST['qty'], null, null, $dec);
172
173         label_cell($_POST['units'], '', 'units');
174
175         if ($order->fixed_asset) {
176                 /* TODO: check if this is a correct way of creating a fixed item
177                         that still has to be submitted in the form */ 
178                 hidden('std_cost');
179                 label_cell($_POST['std_cost'], "nowrap align=right ", 'std_cost_view');
180                 $price = get_purchase_value($_POST['stock_id']);
181                 amount_cell($price, false, '', 'initial_price');
182         } else {
183                 //amount_cells(null, 'std_cost', $_POST['std_cost']);
184                 amount_cells(null, 'std_cost', null, null, null, $dec2);
185                 label_cell("&nbsp;");
186         }
187
188         if ($id != -1)
189         {
190                 button_cell('UpdateItem', _("Update"),
191                                 _('Confirm changes'), ICON_UPDATE);
192                 button_cell('CancelItemChanges', _("Cancel"),
193                                 _('Cancel changes'), ICON_CANCEL);
194                 hidden('LineNo', $line_no);
195                 set_focus('qty');
196         }
197         else
198         {
199                 submit_cells('AddItem', _("Add Item"), "colspan=2",
200                     _('Add new item to document'), true);
201         }
202
203         end_row();
204 }
205
206
207 //---------------------------------------------------------------------------------
208
209 function adjustment_options_controls()
210 {
211           echo "<br>";
212           start_table();
213
214           textarea_row(_("Memo"), 'memo_', null, 50, 3);
215
216           end_table(1);
217 }
218
219
220 //---------------------------------------------------------------------------------
221