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