Quantity On Hand (QOH) Now Available On Inventory Adjustments
[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='90%'");
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"), _("QOH"), _("Quantity"),
60                 _("Unit"), _("Unit Cost"), _("Total"), "");
61                 if ( count($order->line_items)) $th[] = '';
62         }
63
64         table_header($th);
65         $total = 0;
66         $k = 0;  //row colour counter
67
68         $low_stock = $order->check_qoh($_POST['StockLocation'], $_POST['AdjDate']);
69         $id = get_post('selected_id', find_submit('Edit'));
70         foreach ($order->line_items as $line_no=>$stock_item)
71         {
72
73                 $total += ($stock_item->standard_cost * $stock_item->quantity);
74
75                 if ($id != $line_no)
76                 {
77                         if (in_array($stock_item->stock_id, $low_stock))
78                                 start_row("class='stockmankobg'");      // notice low stock status
79                         else 
80                                 alt_table_row_color($k);
81
82                 if ($order->fixed_asset)
83                                 label_cell($stock_item->stock_id);
84                 else
85                                 view_stock_status_cell($stock_item->stock_id);
86
87                         label_cell($stock_item->item_description);
88                         $dec = get_qty_dec($stock_item->stock_id);
89                 if (!$order->fixed_asset)
90                         {
91                                 $qoh = get_qoh_on_date($stock_item->stock_id, $_POST['StockLocation']);
92                                 qty_cell($qoh, false, $dec);
93                         }
94                 qty_cell($stock_item->quantity, false, $dec);
95                         label_cell($stock_item->units);
96                         amount_decimal_cell($stock_item->standard_cost);
97
98                         if ($order->fixed_asset) {
99                         $price = get_purchase_value($stock_item->stock_id);
100                                 amount_cell($price);
101                 } else
102                                 amount_cell($stock_item->standard_cost * $stock_item->quantity);
103
104                 if (!$order->fixed_asset) {
105                         if ($id == -1)
106                                 edit_button_cell("Edit$line_no", _("Edit"),     _('Edit document line'));
107                         else
108                                 label_cell('');
109                 }
110                 if ($id == -1)
111                                 delete_button_cell("Delete$line_no", _("Delete"), _('Remove line from document'));
112                 else
113                         label_cell('');
114                         
115                         end_row();
116                 }
117                 else
118                 {
119                         adjustment_edit_item_controls($order, $line_no);
120                 }
121         }
122
123         if ($id == -1)
124                 adjustment_edit_item_controls($order);
125         $row = !$order->fixed_asset ? 6 : 5;
126         label_row(_("Total"), number_format2($total,user_price_dec()), "align=right colspan=$row", "align=right", 2);
127
128     end_table();
129
130         if ($id != -1)
131             hidden('selected_id', $id);
132         if ($low_stock)
133                 display_note(_("Marked items have insufficient quantities in stock as on day of adjustment."), 0, 1, "class='stockmankofg'");
134         div_end();
135 }
136
137 //---------------------------------------------------------------------------------
138
139 function adjustment_edit_item_controls(&$order, $line_no=-1)
140 {
141         global $Ajax;
142         start_row();
143
144         $dec2 = 0;
145
146         if ($line_no != -1)
147         {
148                 if (find_submit('Edit') != -1) {
149                         $_POST['stock_id'] = $order->line_items[$line_no]->stock_id;
150                         $_POST['qty'] = qty_format($order->line_items[$line_no]->quantity, 
151                                 $order->line_items[$line_no]->stock_id, $dec);
152                 //$_POST['std_cost'] = price_format($order->line_items[$line_no]->standard_cost);
153                         $_POST['std_cost'] = price_decimal_format($order->line_items[$line_no]->standard_cost, $dec2);
154                 }
155                 $_POST['units'] = $order->line_items[$line_no]->units;
156                 hidden('stock_id', $_POST['stock_id']);
157                 label_cell($_POST['stock_id']);
158                 label_cell($order->line_items[$line_no]->item_description, 'nowrap');
159             $Ajax->activate('items_table');
160         }
161         else
162         {
163                 if ($order->fixed_asset)
164                         stock_disposable_fa_list_cells(null, 'stock_id', null, false, true, $order->line_items);
165                 else
166                         stock_costable_items_list_cells(null, 'stock_id', null, false, true);
167
168                 if (list_updated('stock_id')) {
169                         $Ajax->activate('items_table');
170                 }
171
172         }
173
174         $item_info = get_item_edit_info(get_post('stock_id'));
175         if ($item_info) {
176                 $dec = $item_info['decimals'];
177                 $_POST['std_cost'] = price_decimal_format($item_info["material_cost"], $dec2);
178                 $_POST['units'] = $item_info["units"];
179         } else
180                 $dec = 0;
181         if ($order->fixed_asset) {
182                 hidden('qty', -1);
183                 qty_cell(1, false, 0);
184         } else {
185                 $qoh = get_qoh_on_date(get_post('stock_id'), $_POST['StockLocation']);
186                 qty_cell($qoh, false, $dec);
187                 label_cell( text_input('qty', get_post('qty', '0'), 15, 15, '',  'class="amount active" dec='.$dec), "nowrap align=right");
188         }
189         label_cell(get_post('units'), '', 'units');
190
191         if ($order->fixed_asset) {
192                 hidden('std_cost');
193                 label_cell($_POST['std_cost'], "nowrap align=right ", 'std_cost_view');
194                 $price = get_purchase_value($_POST['stock_id']);
195                 amount_cell($price, false, '', 'initial_price');
196         } else {
197                 if (get_post('_qty_changed')) {
198                         $Ajax->activate('items_table');
199                         set_focus(input_num('qty') >= 0 ? 'std_cost' : 'AddItem');
200                 }
201                 if (input_num('qty') >= 0)
202                     amount_cells(null, 'std_cost', null, null, null, $dec);
203                 else {
204                     hidden('std_cost');
205                         label_cell($_POST['std_cost'], "nowrap align=right");
206                 }
207                 label_cell("&nbsp;");
208         }
209
210         if ($line_no != -1)
211         {
212                 button_cell('UpdateItem', _("Update"),
213                                 _('Confirm changes'), ICON_UPDATE);
214                 button_cell('CancelItemChanges', _("Cancel"),
215                                 _('Cancel changes'), ICON_CANCEL);
216                 hidden('LineNo', $line_no);
217                 set_focus('qty');
218         }
219         else
220         {
221                 submit_cells('AddItem', _("Add Item"), "colspan=2",
222                     _('Add new item to document'), true);
223         }
224
225         end_row();
226 }
227
228
229 //---------------------------------------------------------------------------------
230
231 function adjustment_options_controls()
232 {
233           echo "<br>";
234           start_table();
235
236           textarea_row(_("Memo"), 'memo_', null, 50, 3);
237
238           end_table(1);
239 }
240
241
242 //---------------------------------------------------------------------------------
243