Closed transactions exlded from voiding
[fa-stable.git] / admin / void_transaction.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 $path_to_root="..";
13 $page_security = 14;
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 . "/includes/ui.inc");
18 include_once($path_to_root . "/includes/data_checks.inc");
19
20 include_once($path_to_root . "/admin/db/voiding_db.inc");
21 $js = "";
22 if ($use_date_picker)
23         $js .= get_js_date_picker();
24 if ($use_popup_windows)
25         $js .= get_js_open_window(800, 500);
26         
27 page(_("Void a Transaction"), false, false, "", $js);
28
29 //----------------------------------------------------------------------------------------
30 function exist_transaction($type, $type_no)
31 {
32         $void_entry = get_voided_entry($type, $type_no);
33
34         if ($void_entry != null)
35                 return false;
36
37         switch ($type) 
38         {
39                 case 0 : // it's a journal entry
40                         if (!exists_gl_trans($type, $type_no))
41                                 return false;
42                         break;
43
44                 case 1 : // it's a payment
45                 case 2 : // it's a deposit
46                 case 4 : // it's a transfer
47                         if (!exists_bank_trans($type, $type_no))
48                                 return false;
49                         break;
50
51                 case 10 : // it's a customer invoice
52                 case 11 : // it's a customer credit note
53                 case 12 : // it's a customer payment
54                 case 13 : // it's a customer dispatch
55                         if (!exists_customer_trans($type, $type_no))
56                                 return false;
57                         break;
58
59                 case systypes::location_transfer() : // it's a stock transfer
60                         if (get_stock_transfer_items($type_no) == null)
61                                 return false;
62                         break;
63
64                 case systypes::inventory_adjustment() : // it's a stock adjustment
65                         if (get_stock_adjustment_items($type_no) == null)
66                                 return false;
67                         break;
68
69                 case 25 : // it's a GRN
70                         return false;
71                 case 20 : // it's a suppler invoice
72                 case 21 : // it's a supplier credit note
73                 case 22 : // it's a supplier payment
74                         if (!exists_supp_trans($type, $type_no))
75                                 return false;
76                         break;
77
78                 case systypes::work_order() : // it's a work order
79                         if (!get_work_order($type_no, true))
80                                 return false;
81                         break;
82
83                 case 28 : // it's a work order issue
84                         if (!exists_work_order_issue($type_no))
85                                 return false;
86                         break;
87
88                 case 29 : // it's a work order production
89                         if (!exists_work_order_produce($type_no))
90                                 return false;
91                         break;
92
93                 case systypes::cost_update() : // it's a stock cost update
94                         return false;
95                         break;
96         }
97
98         return true;
99 }
100
101 function voiding_controls()
102 {
103         global $table_style2;
104         
105         start_form();
106
107         start_table($table_style2);
108
109         systypes_list_row(_("Transaction Type:"), "filterType", null, true);
110
111     text_row(_("Transaction #:"), 'trans_no', null, 12, 12);
112
113     date_row(_("Voiding Date:"), 'date_');
114
115     textarea_row(_("Memo:"), 'memo_', null, 30, 4);
116
117         end_table(1);
118
119     if (!isset($_POST['ProcessVoiding']))
120         submit_center('ProcessVoiding', _("Void Transaction"), true, '', 'default');
121     else 
122     {
123                 if (!exist_transaction($_POST['filterType'],$_POST['trans_no']))
124                 {
125                         display_error(_("The entered transaction does not exist or cannot be voided."));
126                         unset($_POST['trans_no']);
127                         unset($_POST['memo_']);
128                         unset($_POST['date_']);
129                 submit_center('ProcessVoiding', _("Void Transaction"), true, '', 'default');
130                 }       
131                 else
132                 {
133                 display_warning(_("Are you sure you want to void this transaction ? This action cannot be undone."), 0, 1);
134                 if ($_POST['filterType'] == 0) // GL transaction are not included in get_trans_view_str
135                         $view_str = get_gl_view_str($_POST['filterType'],$_POST['trans_no'], _("View Transaction"));
136                 else
137                         $view_str = get_trans_view_str($_POST['filterType'],$_POST['trans_no'], _("View Transaction"));
138                 display_note($view_str);
139                         br();
140                 submit_center_first('ConfirmVoiding', _("Proceed"), '', true);
141                 submit_center_last('CancelVoiding', _("Cancel"), '', 'cancel');
142         }       
143     }
144
145         end_form();
146 }
147
148 //----------------------------------------------------------------------------------------
149
150 function check_valid_entries()
151 {
152         if (is_closed_trans($_POST['filterType'],$_POST['trans_no']))
153         {
154                 display_error(_("The selected transaction was closed for edition and cannot be voided."));
155                 set_focus('trans_no');
156                 return;
157         }
158         if (!is_date($_POST['date_']))
159         {
160                 display_error(_("The entered date is invalid."));
161                 set_focus('date_');
162                 return false;
163         }
164         if (!is_date_in_fiscalyear($_POST['date_']))
165         {
166                 display_error(_("The entered date is not in fiscal year."));
167                 set_focus('date_');
168                 return false;
169         }
170
171         if (!is_numeric($_POST['trans_no']) OR $_POST['trans_no'] <= 0)
172         {
173                 display_error(_("The transaction number is expected to be numeric and greater than zero."));
174                 set_focus('trans_no');
175                 return false;
176         }
177
178         return true;
179 }
180
181 //----------------------------------------------------------------------------------------
182
183 function handle_void_transaction()
184 {
185         if (check_valid_entries()==true) 
186         {
187                 $void_entry = get_voided_entry($_POST['filterType'], $_POST['trans_no']);
188                 if ($void_entry != null) 
189                 {
190                         display_error(_("The selected transaction has already been voided."), true);
191                         unset($_POST['trans_no']);
192                         unset($_POST['memo_']);
193                         unset($_POST['date_']);
194                         set_focus('trans_no');
195                         return;
196                 }
197
198                 $ret = void_transaction($_POST['filterType'], $_POST['trans_no'],
199                         $_POST['date_'], $_POST['memo_']);
200
201                 if ($ret) 
202                 {
203                         display_notification_centered(_("Selected transaction has been voided."));
204                         unset($_POST['trans_no']);
205                         unset($_POST['memo_']);
206                         unset($_POST['date_']);
207                 }
208                 else {
209                         display_error(_("The entered transaction does not exist or cannot be voided."));
210                         set_focus('trans_no');
211
212                 }
213         }
214 }
215
216 //----------------------------------------------------------------------------------------
217
218 if (!isset($_POST['date_']))
219 {
220         $_POST['date_'] = Today();
221         if (!is_date_in_fiscalyear($_POST['date_']))
222                 $_POST['date_'] = end_fiscalyear();
223 }               
224         
225 if (isset($_POST['ProcessVoiding']))
226 {
227         if (!check_valid_entries())
228                 unset($_POST['ProcessVoiding']);
229         $Ajax->activate('_page_body');
230 }
231
232 if (isset($_POST['ConfirmVoiding']))
233 {
234         handle_void_transaction();
235         $Ajax->activate('_page_body');
236 }
237
238 if (isset($_POST['CancelVoiding']))
239 {
240         $Ajax->activate('_page_body');
241 }
242
243 //----------------------------------------------------------------------------------------
244
245 voiding_controls();
246
247 end_page();
248
249 ?>