Added GRN 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 $page_security = 'SA_VOIDTRANSACTION';
13 $path_to_root = "..";
14 include($path_to_root . "/includes/db_pager.inc");
15 include_once($path_to_root . "/includes/session.inc");
16
17 include_once($path_to_root . "/includes/date_functions.inc");
18 include_once($path_to_root . "/includes/ui.inc");
19 include_once($path_to_root . "/includes/data_checks.inc");
20 include_once($path_to_root . "/admin/db/transactions_db.inc");
21
22 include_once($path_to_root . "/admin/db/voiding_db.inc");
23 $js = "";
24 if ($use_date_picker)
25         $js .= get_js_date_picker();
26 if ($use_popup_windows)
27         $js .= get_js_open_window(800, 500);
28         
29 page(_($help_context = "Void a Transaction"), false, false, "", $js);
30
31 simple_page_mode(true);
32 //----------------------------------------------------------------------------------------
33 function exist_transaction($type, $type_no)
34 {
35         $void_entry = get_voided_entry($type, $type_no);
36
37         if ($void_entry != null)
38                 return false;
39
40         switch ($type) 
41         {
42                 case ST_JOURNAL : // it's a journal entry
43                         if (!exists_gl_trans($type, $type_no))
44                                 return false;
45                         break;
46
47                 case ST_BANKPAYMENT : // it's a payment
48                 case ST_BANKDEPOSIT : // it's a deposit
49                 case ST_BANKTRANSFER : // it's a transfer
50                         if (!exists_bank_trans($type, $type_no))
51                                 return false;
52                         break;
53
54                 case ST_SALESINVOICE : // it's a customer invoice
55                 case ST_CUSTCREDIT : // it's a customer credit note
56                 case ST_CUSTPAYMENT : // it's a customer payment
57                 case ST_CUSTDELIVERY : // it's a customer dispatch
58                         if (!exists_customer_trans($type, $type_no))
59                                 return false;
60                         break;
61
62                 case ST_LOCTRANSFER : // it's a stock transfer
63                         if (get_stock_transfer_items($type_no) == null)
64                                 return false;
65                         break;
66
67                 case ST_INVADJUST : // it's a stock adjustment
68                         if (get_stock_adjustment_items($type_no) == null)
69                                 return false;
70                         break;
71
72                 case ST_PURCHORDER : // it's a PO
73                         return false;
74
75                 case ST_SUPPRECEIVE : // it's a GRN
76                         if (exists_grn_on_invoices($type_no))
77                                 return false;
78                         break;
79
80                 case ST_SUPPINVOICE : // it's a suppler invoice
81                 case ST_SUPPCREDIT : // it's a supplier credit note
82                 case ST_SUPPAYMENT : // it's a supplier payment
83                         if (!exists_supp_trans($type, $type_no))
84                                 return false;
85                         break;
86
87                 case ST_WORKORDER : // it's a work order
88                         if (!get_work_order($type_no, true))
89                                 return false;
90                         break;
91
92                 case ST_MANUISSUE : // it's a work order issue
93                         if (!exists_work_order_issue($type_no))
94                                 return false;
95                         break;
96
97                 case ST_MANURECEIVE : // it's a work order production
98                         if (!exists_work_order_produce($type_no))
99                                 return false;
100                         break;
101
102                 case ST_SALESORDER: // it's a sales order
103                 case ST_SALESQUOTE: // it's a sales quotation
104                         return false;
105                 case ST_COSTUPDATE : // it's a stock cost update
106                         return false;
107                         break;
108         }
109
110         return true;
111 }
112
113 function view_link($trans)
114 {
115         if (!isset($trans['type']))
116                 $trans['type'] = $_POST['filterType'];
117         return get_trans_view_str($trans["type"], $trans["trans_no"]);
118 }
119
120 function select_link($row)
121 {
122         if (!isset($row['type']))
123                 $row['type'] = $_POST['filterType'];
124         return button('Edit'.$row["trans_no"], _("Select"), _("Select"), ICON_EDIT);
125 }
126
127 function gl_view($row)
128 {
129         if (!isset($row['type']))
130                 $row['type'] = $_POST['filterType'];
131         return get_gl_view_str($row["type"], $row["trans_no"]);
132 }
133
134 function date_view($row)
135 {
136         return $row['trans_date'];
137 }
138
139 function ref_view($row)
140 {
141         return $row['ref'];
142 }
143
144 function voiding_controls()
145 {
146         global $selected_id;
147
148         $not_implemented =  array(ST_PURCHORDER, ST_SALESORDER, ST_SALESQUOTE, ST_COSTUPDATE);
149
150         start_form();
151
152     start_table(TABLESTYLE_NOBORDER);
153         start_row();
154
155         systypes_list_cells(_("Type:"), 'filterType', null, true, $not_implemented);
156         if (list_updated('filterType'))
157                 $selected_id = -1;
158
159         if (!isset($_POST['FromTransNo']))
160         $_POST['FromTransNo'] = "1";
161     if (!isset($_POST['ToTransNo']))
162         $_POST['ToTransNo'] = "999999";
163
164     ref_cells(_("from #:"), 'FromTransNo');
165
166     ref_cells(_("to #:"), 'ToTransNo');
167
168     submit_cells('ProcessSearch', _("Search"), '', '', 'default');
169                 
170         end_row();
171     end_table(1);
172     
173         $trans_ref = false;
174         $sql = get_sql_for_view_transactions($_POST['filterType'], $_POST['FromTransNo'], $_POST['ToTransNo'], $trans_ref);
175         if ($sql == "")
176                 return;
177
178         $cols = array(
179                 _("#") => array('insert'=>true, 'fun'=>'view_link'), 
180                 _("Reference") => array('fun'=>'ref_view'), 
181                 _("Date") => array('type'=>'date', 'fun'=>'date_view'),
182                 _("GL") => array('insert'=>true, 'fun'=>'gl_view'),
183                 _("Select") => array('insert'=>true, 'fun'=>'select_link') 
184         );
185
186         $table =& new_db_pager('transactions', $sql, $cols);
187         $table->width = "40%";
188         display_db_pager($table);
189
190         start_table(TABLESTYLE2);
191
192         if ($selected_id != -1)
193         {
194                 hidden('trans_no', $selected_id);
195                 hidden('selected_id', $selected_id);
196         }
197         else
198         {
199                 hidden('trans_no', '');
200                 $_POST['memo_'] = '';
201         }       
202     label_row(_("Transaction #:"), ($selected_id==-1?'':$selected_id));
203
204     date_row(_("Voiding Date:"), 'date_');
205
206     textarea_row(_("Memo:"), 'memo_', null, 30, 4);
207
208         end_table(1);
209
210     if (!isset($_POST['ProcessVoiding']))
211         submit_center('ProcessVoiding', _("Void Transaction"), true, '', 'default');
212     else 
213     {
214                 if (!exist_transaction($_POST['filterType'],$_POST['trans_no']))
215                 {
216                         display_error(_("The entered transaction does not exist or cannot be voided."));
217                         unset($_POST['trans_no']);
218                         unset($_POST['memo_']);
219                         unset($_POST['date_']);
220                 submit_center('ProcessVoiding', _("Void Transaction"), true, '', 'default');
221                 }       
222                 else
223                 {
224                 display_warning(_("Are you sure you want to void this transaction ? This action cannot be undone."), 0, 1);
225                         br();
226                 submit_center_first('ConfirmVoiding', _("Proceed"), '', true);
227                 submit_center_last('CancelVoiding', _("Cancel"), '', 'cancel');
228         }       
229     }
230
231         end_form();
232 }
233
234 //----------------------------------------------------------------------------------------
235
236 function check_valid_entries()
237 {
238         if (is_closed_trans($_POST['filterType'],$_POST['trans_no']))
239         {
240                 display_error(_("The selected transaction was closed for edition and cannot be voided."));
241                 set_focus('trans_no');
242                 return;
243         }
244         if (!is_date($_POST['date_']))
245         {
246                 display_error(_("The entered date is invalid."));
247                 set_focus('date_');
248                 return false;
249         }
250         if (!is_date_in_fiscalyear($_POST['date_']))
251         {
252                 display_error(_("The entered date is not in fiscal year."));
253                 set_focus('date_');
254                 return false;
255         }
256
257         if (!is_numeric($_POST['trans_no']) OR $_POST['trans_no'] <= 0)
258         {
259                 display_error(_("The transaction number is expected to be numeric and greater than zero."));
260                 set_focus('trans_no');
261                 return false;
262         }
263
264         return true;
265 }
266
267 //----------------------------------------------------------------------------------------
268
269 function handle_void_transaction()
270 {
271         if (check_valid_entries()==true) 
272         {
273                 $void_entry = get_voided_entry($_POST['filterType'], $_POST['trans_no']);
274                 if ($void_entry != null) 
275                 {
276                         display_error(_("The selected transaction has already been voided."), true);
277                         unset($_POST['trans_no']);
278                         unset($_POST['memo_']);
279                         unset($_POST['date_']);
280                         set_focus('trans_no');
281                         return;
282                 }
283
284                 $ret = void_transaction($_POST['filterType'], $_POST['trans_no'],
285                         $_POST['date_'], $_POST['memo_']);
286
287                 if ($ret) 
288                 {
289                         display_notification_centered(_("Selected transaction has been voided."));
290                         unset($_POST['trans_no']);
291                         unset($_POST['memo_']);
292                         unset($_POST['date_']);
293                 }
294                 else {
295                         display_error(_("The entered transaction does not exist or cannot be voided."));
296                         set_focus('trans_no');
297
298                 }
299         }
300 }
301
302 //----------------------------------------------------------------------------------------
303
304 if (!isset($_POST['date_']))
305 {
306         $_POST['date_'] = Today();
307         if (!is_date_in_fiscalyear($_POST['date_']))
308                 $_POST['date_'] = end_fiscalyear();
309 }               
310         
311 if (isset($_POST['ProcessVoiding']))
312 {
313         if (!check_valid_entries())
314                 unset($_POST['ProcessVoiding']);
315         $Ajax->activate('_page_body');
316 }
317
318 if (isset($_POST['ConfirmVoiding']))
319 {
320         handle_void_transaction();
321         $Ajax->activate('_page_body');
322 }
323
324 if (isset($_POST['CancelVoiding']))
325 {
326         $selected_id = -1;
327         $Ajax->activate('_page_body');
328 }
329
330 //----------------------------------------------------------------------------------------
331
332 voiding_controls();
333
334 end_page();
335
336 ?>