3cbc3c2c45d23478fde69917cb7844794322c9ee
[fa-stable.git] / inventory / transfers.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 = 'SA_LOCATIONTRANSFER';
13 $path_to_root = "..";
14 include_once($path_to_root . "/includes/ui/items_cart.inc");
15
16 include_once($path_to_root . "/includes/session.inc");
17
18 include_once($path_to_root . "/includes/date_functions.inc");
19 include_once($path_to_root . "/includes/data_checks.inc");
20
21 include_once($path_to_root . "/inventory/includes/stock_transfers_ui.inc");
22 include_once($path_to_root . "/inventory/includes/inventory_db.inc");
23 $js = "";
24 if ($SysPrefs->use_popup_windows)
25         $js .= get_js_open_window(800, 500);
26 if (user_use_date_picker())
27         $js .= get_js_date_picker();
28
29 if (isset($_GET['NewTransfer'])) {
30         if (isset($_GET['FixedAsset'])) {
31                 $page_security = 'SA_ASSETTRANSFER';
32                 $_SESSION['page_title'] = _($help_context = "Fixed Assets Location Transfers");
33         }
34         else {
35                 $_SESSION['page_title'] = _($help_context = "Inventory Location Transfers");
36         }
37 }
38 page($_SESSION['page_title'], false, false, "", $js);
39
40 //-----------------------------------------------------------------------------------------------
41
42 check_db_has_costable_items(_("There are no inventory items defined in the system (Purchased or manufactured items)."));
43
44 //-----------------------------------------------------------------------------------------------
45
46 if (isset($_GET['AddedID'])) 
47 {
48         $trans_no = $_GET['AddedID'];
49         $trans_type = ST_LOCTRANSFER;
50
51         display_notification_centered(_("Inventory transfer has been processed"));
52         display_note(get_trans_view_str($trans_type, $trans_no, _("&View this transfer")));
53
54   $itm = db_fetch(get_stock_transfer_items($_GET['AddedID']));
55
56   if (is_fixed_asset($itm['mb_flag']))
57           hyperlink_params($_SERVER['PHP_SELF'], _("Enter &Another Fixed Assets Transfer"), "NewTransfer=1&FixedAsset=1");
58   else
59           hyperlink_params($_SERVER['PHP_SELF'], _("Enter &Another Inventory Transfer"), "NewTransfer=1");
60
61         display_footer_exit();
62 }
63 //--------------------------------------------------------------------------------------------------
64
65 function line_start_focus() {
66   global        $Ajax;
67
68   $Ajax->activate('items_table');
69   set_focus('_stock_id_edit');
70 }
71 //-----------------------------------------------------------------------------------------------
72
73 function handle_new_order()
74 {
75         if (isset($_SESSION['transfer_items']))
76         {
77                 $_SESSION['transfer_items']->clear_items();
78                 unset ($_SESSION['transfer_items']);
79         }
80
81         $_SESSION['transfer_items'] = new items_cart(ST_LOCTRANSFER);
82   $_SESSION['transfer_items']->fixed_asset = isset($_GET['FixedAsset']);
83         $_POST['AdjDate'] = new_doc_date();
84         if (!is_date_in_fiscalyear($_POST['AdjDate']))
85                 $_POST['AdjDate'] = end_fiscalyear();
86         $_SESSION['transfer_items']->tran_date = $_POST['AdjDate'];     
87 }
88
89 //-----------------------------------------------------------------------------------------------
90
91 if (isset($_POST['Process']))
92 {
93
94         $tr = &$_SESSION['transfer_items'];
95         $input_error = 0;
96
97         if (count($tr->line_items) == 0)        {
98                 display_error(_("You must enter at least one non empty item line."));
99                 set_focus('stock_id');
100                 $input_error = 1;
101         }
102         if (!check_reference($_POST['ref'], ST_LOCTRANSFER))
103         {
104                 set_focus('ref');
105                 $input_error = 1;
106         } 
107         elseif (!is_date($_POST['AdjDate'])) 
108         {
109                 display_error(_("The entered transfer date is invalid."));
110                 set_focus('AdjDate');
111                 $input_error = 1;
112         } 
113         elseif (!is_date_in_fiscalyear($_POST['AdjDate'])) 
114         {
115                 display_error(_("The entered date is out of fiscal year or is closed for further data entry."));
116                 set_focus('AdjDate');
117                 $input_error = 1;
118         } 
119         elseif ($_POST['FromStockLocation'] == $_POST['ToStockLocation'])
120         {
121                 display_error(_("The locations to transfer from and to must be different."));
122                 set_focus('FromStockLocation');
123                 $input_error = 1;
124         }
125         elseif (!$SysPrefs->allow_negative_stock())
126         {
127                 $low_stock = $tr->check_qoh($_POST['FromStockLocation'], $_POST['AdjDate'], true);
128
129                 if ($low_stock)
130                 {
131                 display_error(_("The transfer cannot be processed because it would cause negative inventory balance in source location for marked items as of document date or later."));
132                         $input_error = 1;
133                 }
134         }
135
136         if ($input_error == 1)
137                 unset($_POST['Process']);
138 }
139
140 //-------------------------------------------------------------------------------
141
142 if (isset($_POST['Process']))
143 {
144
145         $trans_no = add_stock_transfer($_SESSION['transfer_items']->line_items,
146                 $_POST['FromStockLocation'], $_POST['ToStockLocation'],
147                 $_POST['AdjDate'], $_POST['ref'], $_POST['memo_']);
148         new_doc_date($_POST['AdjDate']);
149         $_SESSION['transfer_items']->clear_items();
150         unset($_SESSION['transfer_items']);
151
152         meta_forward($_SERVER['PHP_SELF'], "AddedID=$trans_no");
153 } /*end of process credit note */
154
155 //-----------------------------------------------------------------------------------------------
156
157 function check_item_data()
158 {
159         if (!check_num('qty', 0) || input_num('qty') == 0)
160         {
161                 display_error(_("The quantity entered must be a positive number."));
162                 set_focus('qty');
163                 return false;
164         }
165         return true;
166 }
167
168 //-----------------------------------------------------------------------------------------------
169
170 function handle_update_item()
171 {
172         $id = $_POST['LineNo'];
173         if (!isset($_POST['std_cost']))
174                 $_POST['std_cost'] = $_SESSION['transfer_items']->line_items[$id]->standard_cost;
175         $_SESSION['transfer_items']->update_cart_item($id, input_num('qty'), $_POST['std_cost']);
176         line_start_focus();
177 }
178
179 //-----------------------------------------------------------------------------------------------
180
181 function handle_delete_item($id)
182 {
183         $_SESSION['transfer_items']->remove_from_cart($id);
184         line_start_focus();
185 }
186
187 //-----------------------------------------------------------------------------------------------
188
189 function handle_new_item()
190 {
191         if (!isset($_POST['std_cost']))
192                 $_POST['std_cost'] = 0;
193         add_to_order($_SESSION['transfer_items'], $_POST['stock_id'], input_num('qty'), $_POST['std_cost']);
194         line_start_focus();
195 }
196
197 //-----------------------------------------------------------------------------------------------
198 $id = find_submit('Delete');
199 if ($id != -1)
200         handle_delete_item($id);
201         
202 if (isset($_POST['AddItem']) && check_item_data())
203         handle_new_item();
204
205 if (isset($_POST['UpdateItem']) && check_item_data())
206         handle_update_item();
207
208 if (isset($_POST['CancelItemChanges'])) {
209         line_start_focus();
210 }
211 //-----------------------------------------------------------------------------------------------
212
213 if (isset($_GET['NewTransfer']) || !isset($_SESSION['transfer_items']))
214 {
215         if (isset($_GET['fixed_asset']))
216                 check_db_has_disposable_fixed_assets(_("There are no fixed assets defined in the system."));
217         else
218                 check_db_has_costable_items(_("There are no inventory items defined in the system (Purchased or manufactured items)."));
219
220         handle_new_order();
221 }
222
223 //-----------------------------------------------------------------------------------------------
224 start_form();
225
226 display_order_header($_SESSION['transfer_items']);
227
228 start_table(TABLESTYLE, "width='70%'", 10);
229 start_row();
230 echo "<td>";
231 display_transfer_items(_("Items"), $_SESSION['transfer_items']);
232 transfer_options_controls();
233 echo "</td>";
234 end_row();
235 end_table(1);
236
237 submit_center_first('Update', _("Update"), '', null);
238 submit_center_last('Process', _("Process Transfer"), '',  'default');
239
240 end_form();
241 end_page();
242