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