Also display company name in warning when pressing delete company.
[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                 case ST_SUPPRECEIVE : // it's a GRN
74                         return false;
75                 case ST_SUPPINVOICE : // it's a suppler invoice
76                 case ST_SUPPCREDIT : // it's a supplier credit note
77                 case ST_SUPPAYMENT : // it's a supplier payment
78                         if (!exists_supp_trans($type, $type_no))
79                                 return false;
80                         break;
81
82                 case ST_WORKORDER : // it's a work order
83                         if (!get_work_order($type_no, true))
84                                 return false;
85                         break;
86
87                 case ST_MANUISSUE : // it's a work order issue
88                         if (!exists_work_order_issue($type_no))
89                                 return false;
90                         break;
91
92                 case ST_MANURECEIVE : // it's a work order production
93                         if (!exists_work_order_produce($type_no))
94                                 return false;
95                         break;
96
97                 case ST_SALESORDER: // it's a sales order
98                 case ST_SALESQUOTE: // it's a sales quotation
99                         return false;
100                 case ST_COSTUPDATE : // it's a stock cost update
101                         return false;
102                         break;
103         }
104
105         return true;
106 }
107
108 function view_link($trans)
109 {
110         if (!isset($trans['type']))
111                 $trans['type'] = $_POST['filterType'];
112         return get_trans_view_str($trans["type"], $trans["trans_no"]);
113 }
114
115 function select_link($row)
116 {
117         if (!isset($row['type']))
118                 $row['type'] = $_POST['filterType'];
119         return button('Edit'.$row["trans_no"], _("Select"), _("Select"), ICON_EDIT);
120 }
121
122 function gl_view($row)
123 {
124         if (!isset($row['type']))
125                 $row['type'] = $_POST['filterType'];
126         return get_gl_view_str($row["type"], $row["trans_no"]);
127 }
128
129 function date_view($row)
130 {
131         return $row['trans_date'];
132 }
133
134 function ref_view($row)
135 {
136         return $row['ref'];
137 }
138
139 function voiding_controls()
140 {
141         global $selected_id;
142
143         $not_implemented =  array(ST_PURCHORDER, ST_SUPPRECEIVE, ST_SALESORDER, 
144                 ST_SALESQUOTE, ST_COSTUPDATE);
145
146         start_form();
147
148     start_table(TABLESTYLE_NOBORDER);
149         start_row();
150
151         systypes_list_cells(_("Type:"), 'filterType', null, true, $not_implemented);
152         if (list_updated('filterType'))
153                 $selected_id = -1;
154
155         if (!isset($_POST['FromTransNo']))
156         $_POST['FromTransNo'] = "1";
157     if (!isset($_POST['ToTransNo']))
158         $_POST['ToTransNo'] = "999999";
159
160     ref_cells(_("from #:"), 'FromTransNo');
161
162     ref_cells(_("to #:"), 'ToTransNo');
163
164     submit_cells('ProcessSearch', _("Search"), '', '', 'default');
165                 
166         end_row();
167     end_table(1);
168     
169         $trans_ref = false;
170         $sql = get_sql_for_view_transactions($_POST['filterType'], $_POST['FromTransNo'], $_POST['ToTransNo'], $trans_ref);
171         if ($sql == "")
172                 return;
173
174         $cols = array(
175                 _("#") => array('insert'=>true, 'fun'=>'view_link'), 
176                 _("Reference") => array('fun'=>'ref_view'), 
177                 _("Date") => array('type'=>'date', 'fun'=>'date_view'),
178                 _("GL") => array('insert'=>true, 'fun'=>'gl_view'),
179                 _("Select") => array('insert'=>true, 'fun'=>'select_link') 
180         );
181
182         $table =& new_db_pager('transactions', $sql, $cols);
183         $table->width = "40%";
184         display_db_pager($table);
185
186         start_table(TABLESTYLE2);
187
188         if ($selected_id != -1)
189         {
190                 hidden('trans_no', $selected_id);
191                 hidden('selected_id', $selected_id);
192         }
193         else
194         {
195                 hidden('trans_no', '');
196                 $_POST['memo_'] = '';
197         }       
198     label_row(_("Transaction #:"), ($selected_id==-1?'':$selected_id));
199
200     date_row(_("Voiding Date:"), 'date_');
201
202     textarea_row(_("Memo:"), 'memo_', null, 30, 4);
203
204         end_table(1);
205
206     if (!isset($_POST['ProcessVoiding']))
207         submit_center('ProcessVoiding', _("Void Transaction"), true, '', 'default');
208     else 
209     {
210                 if (!exist_transaction($_POST['filterType'],$_POST['trans_no']))
211                 {
212                         display_error(_("The entered transaction does not exist or cannot be voided."));
213                         unset($_POST['trans_no']);
214                         unset($_POST['memo_']);
215                         unset($_POST['date_']);
216                 submit_center('ProcessVoiding', _("Void Transaction"), true, '', 'default');
217                 }       
218                 else
219                 {
220                 display_warning(_("Are you sure you want to void this transaction ? This action cannot be undone."), 0, 1);
221                         br();
222                 submit_center_first('ConfirmVoiding', _("Proceed"), '', true);
223                 submit_center_last('CancelVoiding', _("Cancel"), '', 'cancel');
224         }       
225     }
226
227         end_form();
228 }
229
230 //----------------------------------------------------------------------------------------
231
232 function check_valid_entries()
233 {
234         if (is_closed_trans($_POST['filterType'],$_POST['trans_no']))
235         {
236                 display_error(_("The selected transaction was closed for edition and cannot be voided."));
237                 set_focus('trans_no');
238                 return;
239         }
240         if (!is_date($_POST['date_']))
241         {
242                 display_error(_("The entered date is invalid."));
243                 set_focus('date_');
244                 return false;
245         }
246         if (!is_date_in_fiscalyear($_POST['date_']))
247         {
248                 display_error(_("The entered date is not in fiscal year."));
249                 set_focus('date_');
250                 return false;
251         }
252
253         if (!is_numeric($_POST['trans_no']) OR $_POST['trans_no'] <= 0)
254         {
255                 display_error(_("The transaction number is expected to be numeric and greater than zero."));
256                 set_focus('trans_no');
257                 return false;
258         }
259
260         return true;
261 }
262
263 //----------------------------------------------------------------------------------------
264
265 function handle_void_transaction()
266 {
267         if (check_valid_entries()==true) 
268         {
269                 $void_entry = get_voided_entry($_POST['filterType'], $_POST['trans_no']);
270                 if ($void_entry != null) 
271                 {
272                         display_error(_("The selected transaction has already been voided."), true);
273                         unset($_POST['trans_no']);
274                         unset($_POST['memo_']);
275                         unset($_POST['date_']);
276                         set_focus('trans_no');
277                         return;
278                 }
279
280                 $ret = void_transaction($_POST['filterType'], $_POST['trans_no'],
281                         $_POST['date_'], $_POST['memo_']);
282
283                 if ($ret) 
284                 {
285                         display_notification_centered(_("Selected transaction has been voided."));
286                         unset($_POST['trans_no']);
287                         unset($_POST['memo_']);
288                         unset($_POST['date_']);
289                 }
290                 else {
291                         display_error(_("The entered transaction does not exist or cannot be voided."));
292                         set_focus('trans_no');
293
294                 }
295         }
296 }
297
298 //----------------------------------------------------------------------------------------
299
300 if (!isset($_POST['date_']))
301 {
302         $_POST['date_'] = Today();
303         if (!is_date_in_fiscalyear($_POST['date_']))
304                 $_POST['date_'] = end_fiscalyear();
305 }               
306         
307 if (isset($_POST['ProcessVoiding']))
308 {
309         if (!check_valid_entries())
310                 unset($_POST['ProcessVoiding']);
311         $Ajax->activate('_page_body');
312 }
313
314 if (isset($_POST['ConfirmVoiding']))
315 {
316         handle_void_transaction();
317         $Ajax->activate('_page_body');
318 }
319
320 if (isset($_POST['CancelVoiding']))
321 {
322         $selected_id = -1;
323         $Ajax->activate('_page_body');
324 }
325
326 //----------------------------------------------------------------------------------------
327
328 voiding_controls();
329
330 end_page();
331
332 ?>