Release 2.1.4
[fa-stable.git] / inventory / purchasing_data.php
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 $page_security = 4;
13 $path_to_root="..";
14 include_once($path_to_root . "/includes/session.inc");
15
16 page(_("Supplier Purchasing Data"));
17
18 include_once($path_to_root . "/includes/date_functions.inc");
19 include_once($path_to_root . "/includes/ui.inc");
20 include_once($path_to_root . "/includes/manufacturing.inc");
21 include_once($path_to_root . "/includes/data_checks.inc");
22
23 check_db_has_purchasable_items(_("There are no purchasable inventory items defined in the system."));
24 check_db_has_suppliers(_("There are no suppliers defined in the system."));
25
26 //----------------------------------------------------------------------------------------
27 if ($ret = context_restore()) {
28         if(isset($ret['supplier_id']))
29                 $_POST['supplier_id'] = $ret['supplier_id'];
30 }
31 if (isset($_POST['_supplier_id_editor'])) {
32         context_call($path_to_root.'/purchasing/manage/suppliers.php?supplier_id='.$_POST['supplier_id'], 
33                 array( 'supplier_id', 'stock_id','_stock_id_edit', 'price', 
34                         'suppliers_uom', 'supplier_description','conversion_factor'));
35 }
36 simple_page_mode(true);
37 //--------------------------------------------------------------------------------------------------
38
39 if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM')
40 {
41
42         $input_error = 0;
43         if ($_POST['stock_id'] == "" || !isset($_POST['stock_id']))
44         {
45         $input_error = 1;
46         display_error( _("There is no item selected."));
47         set_focus('stock_id');
48         }
49         elseif (!check_num('price', 0))
50         {
51         $input_error = 1;
52         display_error( _("The price entered was not numeric."));
53         set_focus('price');
54         }
55         elseif (!check_num('conversion_factor'))
56         {
57         $input_error = 1;
58         display_error( _("The conversion factor entered was not numeric. The conversion factor is the number by which the price must be divided by to get the unit price in our unit of measure."));
59                 set_focus('conversion_factor');
60         }
61
62         if ($input_error == 0)
63         {
64         if ($Mode == 'ADD_ITEM') 
65         {
66
67                 $sql = "INSERT INTO ".TB_PREF."purch_data (supplier_id, stock_id, price, suppliers_uom,
68                         conversion_factor, supplier_description) VALUES (";
69                 $sql .= "'".$_POST['supplier_id']."', '" . $_POST['stock_id'] . "', " .
70                     input_num('price',0) . ", '" . $_POST['suppliers_uom'] . "', " .
71                         input_num('conversion_factor') . ", " . db_escape($_POST['supplier_description']) . ")";
72
73                 db_query($sql,"The supplier purchasing details could not be added");
74                 display_notification(_("This supplier purchasing data has been added."));
75         } else
76         {
77                 $sql = "UPDATE ".TB_PREF."purch_data SET price=" . input_num('price',0) . ",
78                                 suppliers_uom='" . $_POST['suppliers_uom'] . "',
79                                 conversion_factor=" . input_num('conversion_factor') . ",
80                                 supplier_description=" . db_escape($_POST['supplier_description']) . "
81                                 WHERE stock_id='" . $_POST['stock_id'] . "' AND
82                                 supplier_id='$selected_id'";
83                 db_query($sql,"The supplier purchasing details could not be updated");
84
85                 display_notification(_("Supplier purchasing data has been updated."));
86         }
87                 $Mode = 'RESET';
88         }
89 }
90
91 //--------------------------------------------------------------------------------------------------
92
93 if ($Mode == 'Delete')
94 {
95
96         $sql = "DELETE FROM ".TB_PREF."purch_data WHERE supplier_id='$selected_id'
97                 AND stock_id='" . $_POST['stock_id'] . "'";
98         db_query($sql,"could not delete purchasing data");
99
100         display_notification(_("The purchasing data item has been sucessfully deleted."));
101         $Mode = 'RESET';
102 }
103
104 if ($Mode == 'RESET')
105 {
106         $selected_id = -1;
107 }
108
109 if (isset($_POST['_selected_id_update']) )
110 {
111         $selected_id = $_POST['selected_id'];
112         $Ajax->activate('_page_body');
113 }
114
115 if (list_updated('stock_id')) 
116         $Ajax->activate('price_table');
117 //--------------------------------------------------------------------------------------------------
118
119 start_form(false, true);
120
121 if (!isset($_POST['stock_id']))
122         $_POST['stock_id'] = get_global_stock_item();
123
124 echo "<center>" . _("Item:"). "&nbsp;";
125 stock_purchasable_items_list('stock_id', $_POST['stock_id'], false, true);
126
127 echo "<hr></center>";
128
129 set_global_stock_item($_POST['stock_id']);
130
131 $mb_flag = get_mb_flag($_POST['stock_id']);
132
133 if ($mb_flag == -1)
134 {
135         display_error(_("Entered item is not defined. Please re-enter."));
136         set_focus('stock_id');
137 }
138 else
139 {
140
141     $sql = "SELECT ".TB_PREF."purch_data.*,".TB_PREF."suppliers.supp_name,".TB_PREF."suppliers.curr_code
142                 FROM ".TB_PREF."purch_data INNER JOIN ".TB_PREF."suppliers
143                 ON ".TB_PREF."purch_data.supplier_id=".TB_PREF."suppliers.supplier_id
144                 WHERE stock_id = '" . $_POST['stock_id'] . "'";
145
146     $result = db_query($sql, "The supplier purchasing details for the selected part could not be retrieved");
147   div_start('price_table');
148     if (db_num_rows($result) == 0)
149     {
150         display_note(_("There is no purchasing data set up for the part selected"));
151     }
152     else
153     {
154         start_table("$table_style width=65%");
155
156                 $th = array(_("Supplier"), _("Price"), _("Currency"),
157                         _("Supplier's Unit"), _("Conversion Factor"), _("Supplier's Description"), "", "");
158
159         table_header($th);
160
161         $k = $j = 0; //row colour counter
162
163         while ($myrow = db_fetch($result))
164         {
165                         alt_table_row_color($k);
166
167             label_cell($myrow["supp_name"]);
168             amount_cell($myrow["price"]);
169             label_cell($myrow["curr_code"]);
170             label_cell($myrow["suppliers_uom"]);
171             qty_cell($myrow['conversion_factor'], false, user_exrate_dec());
172             label_cell($myrow["supplier_description"]);
173                         edit_button_cell("Edit".$myrow['supplier_id'], _("Edit"));
174                         delete_button_cell("Delete".$myrow['supplier_id'], _("Delete"));
175             end_row();
176
177             $j++;
178             If ($j == 12)
179             {
180                 $j = 1;
181                         table_header($th);
182             } //end of page full new headings
183         } //end of while loop
184
185         end_table();
186     }
187  div_end();
188 }
189
190 //-----------------------------------------------------------------------------------------------
191
192 if ($Mode =='Edit')
193 {
194
195         $sql = "SELECT ".TB_PREF."purch_data.*,".TB_PREF."suppliers.supp_name FROM ".TB_PREF."purch_data
196                 INNER JOIN ".TB_PREF."suppliers ON ".TB_PREF."purch_data.supplier_id=".TB_PREF."suppliers.supplier_id
197                 WHERE ".TB_PREF."purch_data.supplier_id='$selected_id'
198                 AND ".TB_PREF."purch_data.stock_id='" . $_POST['stock_id'] . "'";
199
200         $result = db_query($sql, "The supplier purchasing details for the selected supplier and item could not be retrieved");
201
202         $myrow = db_fetch($result);
203
204     $supp_name = $myrow["supp_name"];
205     $_POST['price'] = price_format($myrow["price"]);
206     $_POST['suppliers_uom'] = $myrow["suppliers_uom"];
207     $_POST['supplier_description'] = $myrow["supplier_description"];
208     $_POST['conversion_factor'] = exrate_format($myrow["conversion_factor"]);
209 }
210
211 br();
212 hidden('selected_id', $selected_id);
213 start_table($table_style2);
214
215 if ($Mode == 'Edit')
216 {
217         hidden('supplier_id');
218         label_row(_("Supplier:"), $supp_name);
219 }
220 else
221 {
222         supplier_list_row(_("Supplier:"), 'supplier_id', null, false, true);
223         $_POST['price'] = $_POST['suppliers_uom'] = $_POST['conversion_factor'] = $_POST['supplier_description'] = "";
224 }
225 amount_row(_("Price:"), 'price', null,'', get_supplier_currency($selected_id));
226 text_row(_("Suppliers Unit of Measure:"), 'suppliers_uom', null, 50, 51);
227
228 if (!isset($_POST['conversion_factor']) || $_POST['conversion_factor'] == "")
229 {
230         $_POST['conversion_factor'] = exrate_format(1);
231 }
232 amount_row(_("Conversion Factor (to our UOM):"), 'conversion_factor',
233   exrate_format($_POST['conversion_factor']), null, null, user_exrate_dec() );
234 text_row(_("Supplier's Code or Description:"), 'supplier_description', null, 50, 51);
235
236 end_table(1);
237
238 submit_add_or_update_center($selected_id == -1, '', true);
239
240 end_form();
241 end_page();
242
243 ?>