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 ***********************************************************************/
13 // fiscal year routines
14 function add_fiscalyear($from_date, $to_date, $closed)
16 $from = date2sql($from_date);
17 $to = date2sql($to_date);
19 $sql = "INSERT INTO ".TB_PREF."fiscal_year (begin, end, closed)
20 VALUES (".db_escape($from).",".db_escape($to).", ".db_escape($closed).")";
22 db_query($sql, "could not add fiscal year");
25 function update_fiscalyear($id, $closed)
27 $sql = "UPDATE ".TB_PREF."fiscal_year SET closed=".db_escape($closed)."
28 WHERE id=".db_escape($id);
30 db_query($sql, "could not update fiscal year");
33 function get_all_fiscalyears()
35 $sql = "SELECT * FROM ".TB_PREF."fiscal_year ORDER BY begin";
37 return db_query($sql, "could not get all fiscal years");
40 function get_fiscalyear($id)
42 $sql = "SELECT * FROM ".TB_PREF."fiscal_year WHERE id=".db_escape($id);
44 $result = db_query($sql, "could not get fiscal year");
46 return db_fetch($result);
49 function get_current_fiscalyear()
52 include_once($path_to_root . "/admin/db/company_db.inc");
53 $year = get_company_pref('f_year');
55 $sql = "SELECT * FROM ".TB_PREF."fiscal_year WHERE id=".db_escape($year);
57 $result = db_query($sql, "could not get current fiscal year");
59 return db_fetch($result);
63 function delete_fiscalyear($id)
67 $sql="DELETE FROM ".TB_PREF."fiscal_year WHERE id=".db_escape($id);
69 db_query($sql, "could not delete fiscal year");
74 function is_date_in_fiscalyears($date)
76 $date = date2sql($date);
77 $sql = "SELECT * FROM ".TB_PREF."fiscal_year WHERE '$date' >= begin AND '$date' <= end";
79 $result = db_query($sql, "could not get all fiscal years");
80 return db_fetch($result) !== false;
83 function is_bad_begin_date($date)
85 $bdate = date2sql($date);
86 $sql = "SELECT MAX(end) FROM ".TB_PREF."fiscal_year WHERE begin < '$bdate'";
88 $result = db_query($sql, "could not retrieve last fiscal years");
89 $row = db_fetch_row($result);
92 $max = add_days(sql2date($row[0]), 1);
93 return ($max !== $date);
96 function check_years_before($date, $closed=false)
98 $date = date2sql($date);
99 $sql = "SELECT COUNT(*) FROM ".TB_PREF."fiscal_year WHERE begin < '$date'";
101 $sql .= " AND closed=0";
103 $result = db_query($sql, "could not check fiscal years before");
104 $row = db_fetch_row($result);
105 return ($row[0] > 0);
108 //---------------------------------------------------------------------------------------------
109 function close_year($year)
111 $co = get_company_prefs();
112 if (get_gl_account($co['retained_earnings_act']) == false || get_gl_account($co['profit_loss_year_act']) == false)
114 display_error(_("The Retained Earnings Account or the Profit and Loss Year Account has not been set in System and General GL Setup"));
119 $myrow = get_fiscalyear($year);
121 // retrieve total balances from balance sheet accounts
122 $sql = "SELECT SUM(amount) FROM ".TB_PREF."gl_trans INNER JOIN ".TB_PREF."chart_master ON account=account_code
123 INNER JOIN ".TB_PREF."chart_types ON account_type=id INNER JOIN ".TB_PREF."chart_class ON class_id=cid
124 WHERE ctype>=".CL_ASSETS." AND ctype <=".CL_EQUITY." AND tran_date <= '$to'";
125 $result = db_query($sql, "The total balance could not be calculated");
127 $row = db_fetch_row($result);
128 $balance = round2($row[0], user_price_dec());
134 $trans_type = ST_JOURNAL;
135 $trans_id = get_next_trans_no($trans_type);
137 add_gl_trans($trans_type, $trans_id, $to, $co['retained_earnings_act'],
138 0, 0, _("Closing Year"), -$balance);
139 add_gl_trans($trans_type, $trans_id, $to, $co['profit_loss_year_act'],
140 0, 0, _("Closing Year"), $balance);
143 close_transactions($to);
145 commit_transaction();
149 function open_year($year)
151 $myrow = get_fiscalyear($year);
152 $from = sql2date($myrow['begin']);
155 open_transactions($from);
156 commit_transaction();
159 //---------------------------------------------------------------------------------------------
160 function delete_attachments_and_comments($type_no, $trans_no)
163 $sql = "SELECT * FROM ".TB_PREF."attachments WHERE type_no = $type_no AND trans_no = $trans_no";
164 $result = db_query($sql, "Could not retrieve attachments");
165 while ($row = db_fetch($result))
167 $dir = company_path(). "/attachments";
168 if (file_exists($dir."/".$row['unique_name']))
169 unlink($dir."/".$row['unique_name']);
170 $sql = "DELETE FROM ".TB_PREF."attachments WHERE type_no = $type_no AND trans_no = $trans_no";
171 db_query($sql, "Could not delete attachment");
173 $sql = "DELETE FROM ".TB_PREF."comments WHERE type = $type_no AND id = $trans_no";
174 db_query($sql, "Could not delete comments");
175 $sql = "DELETE FROM ".TB_PREF."refs WHERE type = $type_no AND id = $trans_no";
176 db_query($sql, "Could not delete refs");
179 //---------------------------------------------------------------------------------------------
180 function delete_this_fiscalyear($selected_id)
182 global $db_connections;
184 db_backup($db_connections[$_SESSION["wa_current_user"]->company], 'Security backup before Fiscal Year Removal');
186 $ref = _("Open Balance");
187 $myrow = get_fiscalyear($selected_id);
189 $sql = "SELECT order_no, trans_type FROM ".TB_PREF."sales_orders WHERE ord_date <= '$to' AND type <> 1"; // don't take the templates
190 $result = db_query($sql, "Could not retrieve sales orders");
191 while ($row = db_fetch($result))
193 $sql = "SELECT SUM(qty_sent), SUM(quantity) FROM ".TB_PREF."sales_order_details WHERE order_no = {$row['order_no']} AND trans_type = {$row['trans_type']}";
194 $res = db_query($sql, "Could not retrieve sales order details");
195 $row2 = db_fetch_row($res);
196 if ($row2[0] == $row2[1])
198 $sql = "DELETE FROM ".TB_PREF."sales_order_details WHERE order_no = {$row['order_no']} AND trans_type = {$row['trans_type']}";
199 db_query($sql, "Could not delete sales order details");
200 $sql = "DELETE FROM ".TB_PREF."sales_orders WHERE order_no = {$row['order_no']} AND trans_type = {$row['trans_type']}";
201 db_query($sql, "Could not delete sales order");
202 delete_attachments_and_comments($row['trans_type'], $row['order_no']);
205 $sql = "SELECT order_no FROM ".TB_PREF."purch_orders WHERE ord_date <= '$to'";
206 $result = db_query($sql, "Could not retrieve purchase orders");
207 while ($row = db_fetch($result))
209 $sql = "SELECT SUM(quantity_ordered), SUM(quantity_received) FROM ".TB_PREF."purch_order_details WHERE order_no = {$row['order_no']}";
210 $res = db_query($sql, "Could not retrieve purchase order details");
211 $row2 = db_fetch_row($res);
212 if ($row2[0] == $row2[1])
214 $sql = "DELETE FROM ".TB_PREF."purch_order_details WHERE order_no = {$row['order_no']}";
215 db_query($sql, "Could not delete purchase order details");
216 $sql = "DELETE FROM ".TB_PREF."purch_orders WHERE order_no = {$row['order_no']}";
217 db_query($sql, "Could not delete purchase order");
218 delete_attachments_and_comments(ST_PURCHORDER, $row['order_no']);
221 $sql = "SELECT id FROM ".TB_PREF."grn_batch WHERE delivery_date <= '$to'";
222 $result = db_query($sql, "Could not retrieve grn batch");
223 while ($row = db_fetch($result))
225 $sql = "DELETE FROM ".TB_PREF."grn_items WHERE grn_batch_id = {$row['id']}";
226 db_query($sql, "Could not delete grn items");
227 $sql = "DELETE FROM ".TB_PREF."grn_batch WHERE id = {$row['id']}";
228 db_query($sql, "Could not delete grn batch");
229 delete_attachments_and_comments(25, $row['id']);
231 $sql = "SELECT trans_no, type FROM ".TB_PREF."debtor_trans WHERE tran_date <= '$to' AND
232 (ov_amount + ov_gst + ov_freight + ov_freight_tax + ov_discount) = alloc";
233 $result = db_query($sql, "Could not retrieve debtor trans");
234 while ($row = db_fetch($result))
236 if ($row['type'] == ST_SALESINVOICE)
238 $deliveries = get_parent_trans(ST_SALESINVOICE,$row['trans_no']);
239 foreach ($deliveries as $delivery)
241 $sql = "DELETE FROM ".TB_PREF."debtor_trans_details WHERE debtor_trans_no = $delivery AND debtor_trans_type = ".ST_CUSTDELIVERY;
242 db_query($sql, "Could not delete debtor trans details");
243 $sql = "DELETE FROM ".TB_PREF."debtor_trans WHERE trans_no = $delivery AND type = ".ST_CUSTDELIVERY;
244 db_query($sql, "Could not delete debtor trans");
245 delete_attachments_and_comments(ST_CUSTDELIVERY, $delivery);
248 $sql = "DELETE FROM ".TB_PREF."cust_allocations WHERE trans_no_from = {$row['trans_no']} AND trans_type_from = {$row['type']}";
249 db_query($sql, "Could not delete cust allocations");
250 $sql = "DELETE FROM ".TB_PREF."debtor_trans_details WHERE debtor_trans_no = {$row['trans_no']} AND debtor_trans_type = {$row['type']}";
251 db_query($sql, "Could not delete debtor trans details");
252 $sql = "DELETE FROM ".TB_PREF."debtor_trans WHERE trans_no = {$row['trans_no']} AND type = {$row['type']}";
253 db_query($sql, "Could not delete debtor trans");
254 delete_attachments_and_comments($row['type'], $row['trans_no']);
256 $sql = "SELECT trans_no, type FROM ".TB_PREF."supp_trans WHERE tran_date <= '$to' AND
257 ABS(ov_amount + ov_gst + ov_discount) = alloc";
258 $result = db_query($sql, "Could not retrieve supp trans");
259 while ($row = db_fetch($result))
261 $sql = "DELETE FROM ".TB_PREF."supp_allocations WHERE trans_no_from = {$row['trans_no']} AND trans_type_from = {$row['type']}";
262 db_query($sql, "Could not delete supp allocations");
263 $sql = "DELETE FROM ".TB_PREF."supp_invoice_items WHERE supp_trans_no = {$row['trans_no']} AND supp_trans_type = {$row['type']}";
264 db_query($sql, "Could not delete supp invoice items");
265 $sql = "DELETE FROM ".TB_PREF."supp_trans WHERE trans_no = {$row['trans_no']} AND type = {$row['type']}";
266 db_query($sql, "Could not delete supp trans");
267 delete_attachments_and_comments($row['type'], $row['trans_no']);
269 $sql = "SELECT id FROM ".TB_PREF."workorders WHERE released_date <= '$to' AND closed=1";
270 $result = db_query($sql, "Could not retrieve supp trans");
271 while ($row = db_fetch($result))
273 $sql = "SELECT issue_no FROM ".TB_PREF."wo_issues WHERE workorder_id = {$row['id']}";
274 $res = db_query($sql, "Could not retrieve wo issues");
275 while ($row2 = db_fetch_row($res))
277 $sql = "DELETE FROM ".TB_PREF."wo_issue_items WHERE issue_id = {$row2[0]}";
278 db_query($sql, "Could not delete wo issue items");
280 delete_attachments_and_comments(ST_MANUISSUE, $row['id']);
281 $sql = "DELETE FROM ".TB_PREF."wo_issues WHERE workorder_id = {$row['id']}";
282 db_query($sql, "Could not delete wo issues");
283 $sql = "DELETE FROM ".TB_PREF."wo_manufacture WHERE workorder_id = {$row['id']}";
284 db_query($sql, "Could not delete wo manufacture");
285 $sql = "DELETE FROM ".TB_PREF."wo_requirements WHERE workorder_id = {$row['id']}";
286 db_query($sql, "Could not delete wo requirements");
287 $sql = "DELETE FROM ".TB_PREF."workorders WHERE id = {$row['id']}";
288 db_query($sql, "Could not delete workorders");
289 delete_attachments_and_comments(ST_WORKORDER, $row['id']);
291 $sql = "SELECT loc_code, stock_id, SUM(qty) AS qty, SUM(qty*standard_cost) AS std_cost FROM ".TB_PREF."stock_moves WHERE tran_date <= '$to' GROUP by
293 $result = db_query($sql, "Could not retrieve supp trans");
294 while ($row = db_fetch($result))
296 $sql = "DELETE FROM ".TB_PREF."stock_moves WHERE tran_date <= '$to' AND loc_code = '{$row['loc_code']}' AND stock_id = '{$row['stock_id']}'";
297 db_query($sql, "Could not delete stock moves");
299 $std_cost = ($qty == 0 ? 0 : round2($row['std_cost'] / $qty, user_price_dec()));
300 $sql = "INSERT INTO ".TB_PREF."stock_moves (stock_id, loc_code, tran_date, reference, qty, standard_cost) VALUES
301 ('{$row['stock_id']}', '{$row['loc_code']}', '$to', '$ref', $qty, $std_cost)";
302 db_query($sql, "Could not insert stock move");
304 $sql = "DELETE FROM ".TB_PREF."voided WHERE date_ <= '$to'";
305 db_query($sql, "Could not delete voided items");
306 $sql = "DELETE FROM ".TB_PREF."trans_tax_details WHERE tran_date <= '$to'";
307 db_query($sql, "Could not delete trans tax details");
308 $sql = "DELETE FROM ".TB_PREF."exchange_rates WHERE date_ <= '$to'";
309 db_query($sql, "Could not delete exchange rates");
310 $sql = "DELETE FROM ".TB_PREF."budget_trans WHERE tran_date <= '$to'";
311 db_query($sql, "Could not delete exchange rates");
313 $sql = "SELECT account, SUM(amount) AS amount FROM ".TB_PREF."gl_trans WHERE tran_date <= '$to' GROUP by account";
314 $result = db_query($sql, "Could not retrieve gl trans");
315 while ($row = db_fetch($result))
317 $sql = "DELETE FROM ".TB_PREF."gl_trans WHERE tran_date <= '$to' AND account = '{$row['account']}'";
318 db_query($sql, "Could not delete gl trans");
319 if (is_account_balancesheet($row['account']))
321 $trans_no = get_next_trans_no(ST_JOURNAL);
322 $sql = "INSERT INTO ".TB_PREF."gl_trans (type, type_no, tran_date, account, memo_, amount) VALUES
323 (".ST_JOURNAL.", $trans_no, '$to', '{$row['account']}', '$ref', {$row['amount']})";
324 db_query($sql, "Could not insert gl trans");
328 $sql = "SELECT bank_act, SUM(amount) AS amount FROM ".TB_PREF."bank_trans WHERE trans_date <= '$to' GROUP BY bank_act";
329 $result = db_query($sql, "Could not retrieve bank trans");
330 while ($row = db_fetch($result))
332 $sql = "DELETE FROM ".TB_PREF."bank_trans WHERE trans_date <= '$to' AND bank_act = '{$row['bank_act']}'";
333 db_query($sql, "Could not delete bank trans");
334 $sql = "INSERT INTO ".TB_PREF."bank_trans (type, trans_no, trans_date, bank_act, ref, amount) VALUES
335 (0, 0, '$to', '{$row['bank_act']}', '$ref', {$row['amount']})";
336 db_query($sql, "Could not insert bank trans");
339 $sql = "DELETE FROM ".TB_PREF."audit_trail WHERE gl_date <= '$to'";
340 db_query($sql, "Could not delete audit trail");
342 $sql = "SELECT type, id FROM ".TB_PREF."comments WHERE type != ".ST_SALESQUOTE." AND type != ".ST_SALESORDER." AND type != ".ST_PURCHORDER;
343 $result = db_query($sql, "Could not retrieve comments");
344 while ($row = db_fetch($result))
346 $sql = "SELECT count(*) FROM ".TB_PREF."gl_trans WHERE type = {$row['type']} AND type_no = {$row['id']}";
347 $res = db_query($sql, "Could not retrieve gl_trans");
348 $row2 = db_fetch_row($res);
349 if ($row2[0] == 0) // if no link, then delete comments
351 $sql = "DELETE FROM ".TB_PREF."comments WHERE type = {$row['type']} AND id = {$row['id']}";
352 db_query($sql, "Could not delete comments");
355 $sql = "SELECT type, id FROM ".TB_PREF."refs WHERE type != ".ST_SALESQUOTE." AND type != ".ST_SALESORDER." AND type != ".ST_PURCHORDER;
356 $result = db_query($sql, "Could not retrieve refs");
357 while ($row = db_fetch($result))
359 $sql = "SELECT count(*) FROM ".TB_PREF."gl_trans WHERE type = {$row['type']} AND type_no = {$row['id']}";
360 $res = db_query($sql, "Could not retrieve gl_trans");
361 $row2 = db_fetch_row($res);
362 if ($row2[0] == 0) // if no link, then delete refs
364 $sql = "DELETE FROM ".TB_PREF."refs WHERE type = {$row['type']} AND id = {$row['id']}";
365 db_query($sql, "Could not delete refs");
369 delete_fiscalyear($selected_id);
370 commit_transaction();