Focus set to invalid field after submit check fail
[fa-stable.git] / manufacturing / work_order_issue.php
1 <?php
2
3 $page_security = 3;
4 $path_to_root="..";
5 include_once($path_to_root . "/includes/ui/items_cart.inc");
6
7 include_once($path_to_root . "/includes/session.inc");
8
9 include_once($path_to_root . "/includes/date_functions.inc");
10 include_once($path_to_root . "/includes/data_checks.inc");
11
12 include_once($path_to_root . "/manufacturing/includes/manufacturing_db.inc");
13 include_once($path_to_root . "/manufacturing/includes/manufacturing_ui.inc");
14 include_once($path_to_root . "/manufacturing/includes/work_order_issue_ui.inc");
15 $js = "";
16 if ($use_date_picker)
17         $js .= get_js_date_picker();
18 page(_("Issue Items to Work Order"), false, false, "", $js);
19
20 //-----------------------------------------------------------------------------------------------
21
22 if (isset($_GET['AddedID'])) 
23 {
24         echo "<center>" . _("The work order issue has been entered.");
25         echo "<br>";
26         hyperlink_no_params("search_work_orders.php", _("Select another Work Order to Process"));
27         echo "<br><br>";
28         end_page();
29         exit;
30 }
31
32 //--------------------------------------------------------------------------------------------------
33
34 function handle_new_order()
35 {
36         if (isset($_SESSION['issue_items']))
37         {
38                 $_SESSION['issue_items']->clear_items();
39                 unset ($_SESSION['issue_items']);
40         }
41
42      Session_register("issue_items");
43
44      $_SESSION['issue_items'] = new items_cart;
45      $_SESSION['issue_items']->order_id = $_GET['trans_no'];
46 }
47
48 //-----------------------------------------------------------------------------------------------
49
50 function can_process()
51 {
52         if (!is_date($_POST['date_'])) 
53         {
54                 display_error(_("The entered date for the issue is invalid."));
55                 set_focus('date_');
56                 return false;
57         } 
58         elseif (!is_date_in_fiscalyear($_POST['date_'])) 
59         {
60                 display_error(_("The entered date is not in fiscal year."));
61                 set_focus('date_');
62                 return false;
63         }
64         if (!references::is_valid($_POST['ref'])) 
65         {
66                 display_error(_("You must enter a reference."));
67                 set_focus('ref');
68                 return false;
69         }
70
71         if (!is_new_reference($_POST['ref'], 28)) 
72         {
73                 display_error(_("The entered reference is already in use."));
74                 set_focus('ref');
75                 return false;
76         }
77
78         $failed_item = $_SESSION['issue_items']->check_qoh($_POST['Location'], $_POST['date_'], !$_POST['IssueType']);
79         if ($failed_item != null) 
80         {
81         display_error( _("The issue cannot be processed because an entered item would cause a negative inventory balance :") .
82                 " " . $failed_item->stock_id . " - " .  $failed_item->item_description);
83                 return false;
84         }
85
86         return true;
87 }
88
89 if (isset($_POST['Process']))
90 {
91
92         // if failed, returns a stockID
93         $failed_data = add_work_order_issue($_SESSION['issue_items']->order_id,
94                 $_POST['ref'], $_POST['IssueType'], $_SESSION['issue_items']->line_items,
95                 $_POST['Location'], $_POST['WorkCentre'], $_POST['date_'], $_POST['memo_']);
96
97         if ($failed_data != null) 
98         {
99                 display_error(_("The process cannot be completed because there is an insufficient total quantity for a component.") . "<br>"
100                 . _("Component is :"). $failed_data[0] . "<br>"
101                 . _("From location :"). $failed_data[1] . "<br>");
102         } 
103         else 
104         {
105                 meta_forward($_SERVER['PHP_SELF'], "AddedID=1");
106         }
107
108 } /*end of process credit note */
109
110 //-----------------------------------------------------------------------------------------------
111
112 function check_item_data()
113 {
114         if (!is_numeric($_POST['qty']))
115         {
116                 display_error(_("The quantity entered is not a valid number."));
117                 set_focus('qty');
118                 return false;
119         }
120
121         if ($_POST['qty'] <= 0)
122         {
123                 display_error(_("The quantity entered must be greater than zero."));
124                 set_focus('qty');
125                 return false;
126         }
127
128         if (!is_numeric($_POST['std_cost']) || $_POST['std_cost'] < 0)
129         {
130                 display_error(_("The entered standard cost is negative or invalid."));
131                 set_focus('std_cost');
132                 return false;
133         }
134
135         return true;
136 }
137
138 //-----------------------------------------------------------------------------------------------
139
140 function handle_update_item()
141 {
142     if($_POST['UpdateItem'] != "" && check_item_data())
143     {
144         $_SESSION['issue_items']->update_cart_item($_POST['stock_id'], $_POST['qty'], $_POST['std_cost']);
145     }
146 }
147
148 //-----------------------------------------------------------------------------------------------
149
150 function handle_delete_item()
151 {
152         $_SESSION['issue_items']->remove_from_cart($_GET['Delete']);
153 }
154
155 //-----------------------------------------------------------------------------------------------
156
157 function handle_new_item()
158 {
159         if (!check_item_data())
160                 return;
161
162         add_to_order($_SESSION['issue_items'], $_POST['stock_id'], $_POST['qty'], $_POST['std_cost']);
163 }
164
165 //-----------------------------------------------------------------------------------------------
166
167 if ($_GET['Delete']!="")
168         handle_delete_item();
169
170 if ($_POST['AddItem']!="")
171         handle_new_item();
172
173 if ($_POST['UpdateItem']!="")
174         handle_update_item();
175
176 //-----------------------------------------------------------------------------------------------
177
178 if (isset($_GET['trans_no']))
179 {
180         handle_new_order();
181 }
182
183 //-----------------------------------------------------------------------------------------------
184
185 display_order_header($_SESSION['issue_items']);
186
187 start_form(false, true);
188
189 start_table("$table_style width=90%", '10');
190 echo "<tr><td>";
191 display_adjustment_items(_("Items to Issue"), $_SESSION['issue_items']);
192 adjustment_options_controls();
193 echo "</td></tr>";
194
195 end_table();
196
197 if (!isset($_POST['Process']))
198 {
199         start_table();
200     start_row();
201     submit_cells('Update', _("Update"));
202         if ($_SESSION['issue_items']->count_items() >= 1)
203         {
204             submit_cells('Process', _("Process Issue"));
205         }
206         end_row();
207         end_table();
208 }
209
210 end_form();
211
212 //------------------------------------------------------------------------------------------------
213
214 end_page();
215
216 ?>