Bug 5187: Displaying the list of suppliers in the void part is either wrong or buggy...
[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 (user_use_date_picker())
25         $js .= get_js_date_picker();
26 if ($SysPrefs->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($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         }
108
109         return true;
110 }
111
112 function view_link($trans)
113 {
114         if (!isset($trans['type']))
115                 $trans['type'] = $_POST['filterType'];
116         return get_trans_view_str($trans["type"], $trans["trans_no"]);
117 }
118
119 function select_link($row)
120 {
121         if (!isset($row['type']))
122                 $row['type'] = $_POST['filterType'];
123         if (!is_date_in_fiscalyear($row['trans_date'], true))
124                 return _("N/A");
125         return button('Edit'.$row["trans_no"], _("Select"), _("Select"), ICON_EDIT);
126 }
127
128 function gl_view($row)
129 {
130         if (!isset($row['type']))
131                 $row['type'] = $_POST['filterType'];
132         return get_gl_view_str($row["type"], $row["trans_no"]);
133 }
134
135 function date_view($row)
136 {
137         return $row['trans_date'];
138 }
139
140 function ref_view($row)
141 {
142         return $row['ref'];
143 }
144
145 function is_selected($row) // Function added by faisal
146 {
147         global $selected_id;
148         return $row['trans_no'] == $selected_id ? true : false;
149 }
150
151 function voiding_controls()
152 {
153         global $selected_id;
154
155         $not_implemented =  array(ST_PURCHORDER, ST_SALESORDER, ST_SALESQUOTE, ST_COSTUPDATE, ST_CUSTOMER, ST_SUPPLIER);
156
157         start_form();
158
159     start_table(TABLESTYLE_NOBORDER);
160         start_row();
161
162         systypes_list_cells(_("Transaction Type:"), 'filterType', null, true, $not_implemented);
163         if (list_updated('filterType'))
164                 $selected_id = -1;
165
166         if (!isset($_POST['FromTransNo']))
167         $_POST['FromTransNo'] = "1";
168     if (!isset($_POST['ToTransNo']))
169         $_POST['ToTransNo'] = "999999";
170
171     ref_cells(_("from #:"), 'FromTransNo');
172
173     ref_cells(_("to #:"), 'ToTransNo');
174
175     submit_cells('ProcessSearch', _("Search"), '', '', 'default');
176
177         end_row();
178     end_table(1);
179     
180         $trans_ref = false;
181         $sql = get_sql_for_view_transactions(get_post('filterType'), get_post('FromTransNo'), get_post('ToTransNo'), $trans_ref);
182         if ($sql == "")
183                 return;
184
185         $cols = array(
186                 _("#") => array('insert'=>true, 'fun'=>'view_link'),
187                 _("Reference") => array('fun'=>'ref_view'),
188                 _("Date") => array('type'=>'date', 'fun'=>'date_view'),
189                 _("GL") => array('insert'=>true, 'fun'=>'gl_view'),
190                 _("Select") => array('insert'=>true, 'fun'=>'select_link')
191         );
192
193         $table =& new_db_pager('transactions', $sql, $cols);
194         $table->set_marker('is_selected', _("Marked transactions will be voided.")); //Added by Faisal
195
196         $table->width = "40%";
197         display_db_pager($table);
198
199         start_table(TABLESTYLE2);
200
201         if ($selected_id != -1)
202         {
203                 hidden('trans_no', $selected_id);
204                 hidden('selected_id', $selected_id);
205         }
206         else
207         {
208                 hidden('trans_no', '');
209                 $_POST['memo_'] = '';
210         }       
211     label_row(_("Transaction #:"), ($selected_id==-1?'':$selected_id));
212
213     date_row(_("Voiding Date:"), 'date_');
214
215     textarea_row(_("Memo:"), 'memo_', null, 30, 4);
216
217         end_table(1);
218
219     if (!isset($_POST['ProcessVoiding']))
220         submit_center('ProcessVoiding', _("Void Transaction"), true, '', 'default');
221     else 
222     {
223                 if (!exist_transaction($_POST['filterType'],$_POST['trans_no']))
224                 {
225                         display_error(_("The entered transaction does not exist or cannot be voided."));
226                         unset($_POST['trans_no']);
227                         unset($_POST['memo_']);
228                         unset($_POST['date_']);
229                 submit_center('ProcessVoiding', _("Void Transaction"), true, '', 'default');
230                 }       
231                 else
232                 {
233                 if ($_POST['filterType'] == ST_SUPPRECEIVE) { 
234                 $result = get_grn_items($_POST['trans_no']);
235                 if (db_num_rows($result) > 0) {
236                     while ($myrow = db_fetch($result)) {
237                         if (is_inventory_item($myrow["item_code"])) {
238                             if (check_negative_stock($myrow["item_code"], -$myrow["qty_recd"], null, $_POST['date_'])) {
239                                 $stock = get_item($myrow["item_code"]);
240                                 display_error(_("The void cannot be processed because there is an insufficient quantity for item:") .
241                                     " " . $stock['stock_id'] . " - " . $stock['description'] . " - " .
242                                     _("Quantity On Hand") . " = " . number_format2(get_qoh_on_date($stock['stock_id'], null, 
243                                     $_POST['date_']), get_qty_dec($stock['stock_id'])));
244                                 return false;
245                             }
246                         }
247                     }
248                 }
249             }
250                 display_warning(_("Are you sure you want to void this transaction ? This action cannot be undone."), 0, 1);
251                         br();
252                 submit_center_first('ConfirmVoiding', _("Proceed"), '', true);
253                 submit_center_last('CancelVoiding', _("Cancel"), '', 'cancel');
254         }       
255     }
256
257         end_form();
258 }
259
260 //----------------------------------------------------------------------------------------
261
262 function check_valid_entries()
263 {
264         if (is_closed_trans($_POST['filterType'],$_POST['trans_no']))
265         {
266                 display_error(_("The selected transaction was closed for edition and cannot be voided."));
267                 set_focus('trans_no');
268                 return false;
269         }
270         if (!is_date($_POST['date_']))
271         {
272                 display_error(_("The entered date is invalid."));
273                 set_focus('date_');
274                 return false;
275         }
276         if (!is_date_in_fiscalyear($_POST['date_']))
277         {
278                 display_error(_("The entered date is out of fiscal year or is closed for further data entry."));
279                 set_focus('date_');
280                 return false;
281         }
282
283         if (!is_numeric($_POST['trans_no']) OR $_POST['trans_no'] <= 0)
284         {
285                 display_error(_("The transaction number is expected to be numeric and greater than zero."));
286                 set_focus('trans_no');
287                 return false;
288         }
289
290         return true;
291 }
292
293 //----------------------------------------------------------------------------------------
294
295 function handle_void_transaction()
296 {
297         if (check_valid_entries()==true) 
298         {
299                 $void_entry = get_voided_entry($_POST['filterType'], $_POST['trans_no']);
300                 if ($void_entry != null) 
301                 {
302                         display_error(_("The selected transaction has already been voided."), true);
303                         unset($_POST['trans_no']);
304                         unset($_POST['memo_']);
305                         unset($_POST['date_']);
306                         set_focus('trans_no');
307                         return;
308                 }
309
310                 $msg = void_transaction($_POST['filterType'], $_POST['trans_no'],
311                         $_POST['date_'], $_POST['memo_']);
312
313                 if (!$msg) 
314                 {
315                         display_notification_centered(_("Selected transaction has been voided."));
316                         unset($_POST['trans_no']);
317                         unset($_POST['memo_']);
318                 }
319                 else {
320                         display_error($msg);
321                         set_focus('trans_no');
322
323                 }
324         }
325 }
326
327 //----------------------------------------------------------------------------------------
328
329 if (!isset($_POST['date_']))
330 {
331         $_POST['date_'] = Today();
332         if (!is_date_in_fiscalyear($_POST['date_']))
333                 $_POST['date_'] = end_fiscalyear();
334 }               
335         
336 if (isset($_POST['ProcessVoiding']))
337 {
338         if (!check_valid_entries())
339                 unset($_POST['ProcessVoiding']);
340         $Ajax->activate('_page_body');
341 }
342
343 if (isset($_POST['ConfirmVoiding']))
344 {
345         handle_void_transaction();
346         $selected_id = '';
347         $Ajax->activate('_page_body');
348 }
349
350 if (isset($_POST['CancelVoiding']))
351 {
352         $selected_id = -1;
353         $Ajax->activate('_page_body');
354 }
355
356 //----------------------------------------------------------------------------------------
357
358 voiding_controls();
359
360 end_page();
361