Moving bugfixes from 2.3 to 2.4 and updated install languages for Denmark and Sweden.
[fa-stable.git] / manufacturing / work_order_costs.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_WORKORDERCOST';
13 $path_to_root = "..";
14 include_once($path_to_root . "/includes/session.inc");
15 include_once($path_to_root . "/includes/inventory.inc");
16
17 include_once($path_to_root . "/includes/date_functions.inc");
18 include_once($path_to_root . "/gl/includes/db/gl_db_bank_trans.inc");
19
20 include_once($path_to_root . "/manufacturing/includes/manufacturing_db.inc");
21 include_once($path_to_root . "/manufacturing/includes/manufacturing_ui.inc");
22
23 $js = "";
24 if ($SysPrefs->use_popup_windows)
25         $js .= get_js_open_window(900, 500);
26 if (user_use_date_picker())
27         $js .= get_js_date_picker();
28 page(_($help_context = "Work Order Additional Costs"), false, false, "", $js);
29
30 if (isset($_GET['trans_no']) && $_GET['trans_no'] != "")
31 {
32         $_POST['selected_id'] = $_GET['trans_no'];
33 }
34
35 //--------------------------------------------------------------------------------------------------
36
37 if (isset($_GET['AddedID']))
38 {
39         $id = $_GET['AddedID'];
40         $stype = ST_WORKORDER;
41
42         display_notification(_("The additional cost has been entered."));
43
44     display_note(get_trans_view_str($stype, $id, _("View this Work Order")));
45
46         display_note(get_gl_view_str($stype, $id, _("View the GL Journal Entries for this Work Order")), 1);
47
48         hyperlink_params("work_order_costs.php", _("Enter another additional cost."), "trans_no=$id");
49  
50         hyperlink_no_params("search_work_orders.php", _("Select another &Work Order to Process"));
51         br();
52
53         end_page();
54         exit;
55 }
56
57 //--------------------------------------------------------------------------------------------------
58
59 $wo_details = get_work_order($_POST['selected_id']);
60
61 if (strlen($wo_details[0]) == 0)
62 {
63         display_error(_("The order number sent is not valid."));
64         exit;
65 }
66
67 //--------------------------------------------------------------------------------------------------
68
69 function can_process()
70 {
71         global $wo_details;
72         
73         if (!check_num('costs', 0))
74         {
75                 display_error(_("The amount entered is not a valid number or less then zero."));
76                 set_focus('costs');
77                 return false;
78         }
79
80         if (!is_date($_POST['date_']))
81         {
82                 display_error(_("The entered date is invalid."));
83                 set_focus('date_');
84                 return false;
85         }
86         elseif (!is_date_in_fiscalyear($_POST['date_']))
87         {
88                 display_error(_("The entered date is out of fiscal year or is closed for further data entry."));
89                 set_focus('date_');
90                 return false;
91         }
92         if (date_diff2(sql2date($wo_details["released_date"]), $_POST['date_'], "d") > 0)
93         {
94                 display_error(_("The additional cost date cannot be before the release date of the work order."));
95                 set_focus('date_');
96                 return false;
97         }
98
99         return true;
100 }
101
102 //--------------------------------------------------------------------------------------------------
103
104 if (isset($_POST['process']) && can_process() == true)
105 {
106         $date = $_POST['date_'];
107         begin_transaction();
108         add_gl_trans_std_cost(ST_WORKORDER, $_POST['selected_id'], $_POST['date_'], $_POST['cr_acc'],
109                 0, 0, $date.": ".$wo_cost_types[$_POST['PaymentType']], -input_num('costs'), PT_WORKORDER, $_POST['PaymentType']);
110         $is_bank_to = is_bank_account($_POST['cr_acc']);
111         if ($is_bank_to)
112         {
113                 add_bank_trans(ST_WORKORDER, $_POST['selected_id'], $is_bank_to, "",
114                         $_POST['date_'], -input_num('costs'), PT_WORKORDER, $_POST['PaymentType'], get_company_currency(),
115                         "Cannot insert a destination bank transaction");
116         }
117
118         add_gl_trans_std_cost(ST_WORKORDER, $_POST['selected_id'], $_POST['date_'], $_POST['db_acc'],
119                 $_POST['dim1'], $_POST['dim2'], $date.": ".$wo_cost_types[$_POST['PaymentType']], input_num('costs'), PT_WORKORDER, 
120                         $_POST['PaymentType']);
121                         
122         //Apply the costs to manfuctured stock item as adjustement
123         $wo = get_work_order($_POST['selected_id']);
124         if ($_POST['PaymentType'] == 0)
125                 add_labour_cost($wo['stock_id'], $wo['units_reqd'], $_POST['date_'], input_num('costs'), true);
126         else
127                 add_overhead_cost($wo['stock_id'], $wo['units_reqd'], $_POST['date_'], input_num('costs'), true);
128                         
129         commit_transaction();   
130         meta_forward($_SERVER['PHP_SELF'], "AddedID=".$_POST['selected_id']);
131 }
132
133 //-------------------------------------------------------------------------------------
134
135 display_wo_details($_POST['selected_id']);
136
137 //-------------------------------------------------------------------------------------
138
139 if (!isset($_POST['ref']))
140         $_POST['ref'] = $Refs->get_next(ST_JOURNAL, null, get_post('date_'));
141
142 start_form();
143
144 hidden('selected_id', $_POST['selected_id']);
145 //hidden('WOReqQuantity', $_POST['WOReqQuantity']);
146
147 start_table(TABLESTYLE2);
148
149 br();
150
151
152 date_row(_("Date:"), 'date_');
153 ref_row(_("Reference:"), 'ref', '');
154
155 yesno_list_row(_("Type:"), 'PaymentType', null, $wo_cost_types[WO_OVERHEAD], $wo_cost_types[WO_LABOUR]);
156
157 $item_accounts = get_stock_gl_code($wo_details['stock_id']);
158 $_POST['db_acc'] = $item_accounts['assembly_account'];
159 $r = get_default_bank_account(get_company_pref('curr_default'));
160 $_POST['cr_acc'] = $r[0];
161
162 amount_row(_("Additional Costs:"), 'costs');
163 gl_all_accounts_list_row(_("Debit Account"), 'db_acc', null);
164 gl_all_accounts_list_row(_("Credit Account"), 'cr_acc', null);
165 textarea_row(_("Memo:"), 'memo', null, 40, 5);
166 end_table(1);
167 hidden('dim1', $item_accounts["dimension_id"]);
168 hidden('dim2', $item_accounts["dimension2_id"]);
169
170 submit_center('process', _("Process Additional Cost"), true, '', true);
171
172 end_form();
173
174 end_page();
175