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