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()
51 $year = get_company_pref('f_year');
53 $sql = "SELECT * FROM ".TB_PREF."fiscal_year WHERE id=".db_escape($year);
55 $result = db_query($sql, "could not get current fiscal year");
57 return db_fetch($result);
61 function delete_fiscalyear($id)
65 $sql="DELETE FROM ".TB_PREF."fiscal_year WHERE id=".db_escape($id);
67 db_query($sql, "could not delete fiscal year");
72 function is_date_in_fiscalyears($date, $closed=true)
74 $date = date2sql($date);
75 $sql = "SELECT * FROM ".TB_PREF."fiscal_year WHERE '$date' >= begin AND '$date' <= end";
77 $sql .= " AND closed=0";
78 $result = db_query($sql, "could not get all fiscal years");
79 return db_fetch($result) !== false;
82 function get_fiscalyear_begin_for_date($date)
84 $date = date2sql($date);
85 $sql = "SELECT begin FROM ".TB_PREF."fiscal_year WHERE '$date' >= begin AND '$date' <= end";
86 $result = db_query($sql, "could not get begin date of the fiscal year");
87 $row = db_fetch_row($result);
89 return sql2date($row[0]);
91 return begin_fiscalyear();
94 function check_begin_end_date($date1, $date2)
96 $sql = "SELECT MAX(end), MIN(begin) FROM ".TB_PREF."fiscal_year";
97 $result = db_query($sql, "could not retrieve last fiscal years");
98 $row = db_fetch_row($result);
101 $max = add_days(sql2date($row[0]), 1);
102 $min = add_days(sql2date($row[1]), -1);
103 return ($max === $date1 || $min === $date2);
106 function next_begin_date()
108 $sql = "SELECT MAX(end) FROM ".TB_PREF."fiscal_year";
109 $result = db_query($sql, "could not retrieve last fiscal years");
110 $row = db_fetch_row($result);
111 if ($row[0] === null)
113 return add_days(sql2date($row[0]), 1);
116 function check_years_before($date, $closed=false)
118 $date = date2sql($date);
119 $sql = "SELECT COUNT(*) FROM ".TB_PREF."fiscal_year WHERE begin < '$date'";
121 $sql .= " AND closed=0";
123 $result = db_query($sql, "could not check fiscal years before");
124 $row = db_fetch_row($result);
125 return ($row[0] > 0);
128 //---------------------------------------------------------------------------------------------
129 function close_year($year)
131 $co = get_company_prefs();
132 if (get_gl_account($co['retained_earnings_act']) == false || get_gl_account($co['profit_loss_year_act']) == false)
134 display_error(_("The Retained Earnings Account or the Profit and Loss Year Account has not been set in System and General GL Setup"));
137 if (!is_account_balancesheet($co['retained_earnings_act']) || is_account_balancesheet($co['profit_loss_year_act']))
139 display_error(_("The Retained Earnings Account should be a Balance Account or the Profit and Loss Year Account should be an Expense Account (preferred the last one in the Expense Class)"));
145 $myrow = get_fiscalyear($year);
147 // retrieve total balances from balance sheet accounts
148 $sql = "SELECT SUM(amount) FROM ".TB_PREF."gl_trans INNER JOIN ".TB_PREF."chart_master ON account=account_code
149 INNER JOIN ".TB_PREF."chart_types ON account_type=id INNER JOIN ".TB_PREF."chart_class ON class_id=cid
150 WHERE ctype>=".CL_ASSETS." AND ctype <=".CL_EQUITY." AND tran_date <= '$to'";
151 $result = db_query($sql, "The total balance could not be calculated");
153 $row = db_fetch_row($result);
154 $balance = round2($row[0], user_price_dec());
160 $trans_type = ST_JOURNAL;
161 $trans_id = get_next_trans_no($trans_type);
163 add_gl_trans($trans_type, $trans_id, $to, $co['retained_earnings_act'],
164 0, 0, _("Closing Year"), -$balance);
165 add_gl_trans($trans_type, $trans_id, $to, $co['profit_loss_year_act'],
166 0, 0, _("Closing Year"), $balance);
169 close_transactions($to);
171 commit_transaction();
175 function open_year($year)
177 $myrow = get_fiscalyear($year);
178 $from = sql2date($myrow['begin']);
181 close_transactions(add_days($from, -1));
182 commit_transaction();
185 //---------------------------------------------------------------------------------------------
186 function delete_attachments_and_comments($type_no, $trans_no)
189 $sql = "SELECT * FROM ".TB_PREF."attachments WHERE type_no = $type_no AND trans_no = $trans_no";
190 $result = db_query($sql, "Could not retrieve attachments");
192 while ($row = db_fetch($result))
195 $dir = company_path(). "/attachments";
196 if (file_exists($dir."/".$row['unique_name']))
197 unlink($dir."/".$row['unique_name']);
201 $sql = "DELETE FROM ".TB_PREF."attachments WHERE type_no = $type_no AND trans_no = $trans_no";
202 db_query($sql, "Could not delete attachment");
204 $sql = "DELETE FROM ".TB_PREF."comments WHERE type = $type_no AND id = $trans_no";
205 db_query($sql, "Could not delete comments");
206 $sql = "DELETE FROM ".TB_PREF."refs WHERE type = $type_no AND id = $trans_no";
207 db_query($sql, "Could not delete refs");
210 //---------------------------------------------------------------------------------------------
211 function delete_this_fiscalyear($selected_id)
213 global $db_connections;
215 db_backup($db_connections[user_company()], 'Security backup before Fiscal Year Removal');
217 $ref = _("Open Balance");
218 $myrow = get_fiscalyear($selected_id);
220 $sql = "SELECT order_no, trans_type FROM ".TB_PREF."sales_orders WHERE ord_date <= '$to' AND type <> 1"; // don't take the templates
221 $result = db_query($sql, "Could not retrieve sales orders");
222 while ($row = db_fetch($result))
224 $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']}";
225 $res = db_query($sql, "Could not retrieve sales order details");
226 $row2 = db_fetch_row($res);
227 if ($row2[0] == $row2[1])
229 $sql = "DELETE FROM ".TB_PREF."sales_order_details WHERE order_no = {$row['order_no']} AND trans_type = {$row['trans_type']}";
230 db_query($sql, "Could not delete sales order details");
231 $sql = "DELETE FROM ".TB_PREF."sales_orders WHERE order_no = {$row['order_no']} AND trans_type = {$row['trans_type']}";
232 db_query($sql, "Could not delete sales order");
233 delete_attachments_and_comments($row['trans_type'], $row['order_no']);
236 $sql = "SELECT order_no FROM ".TB_PREF."purch_orders WHERE ord_date <= '$to'";
237 $result = db_query($sql, "Could not retrieve purchase orders");
238 while ($row = db_fetch($result))
240 $sql = "SELECT SUM(quantity_ordered), SUM(quantity_received) FROM ".TB_PREF."purch_order_details WHERE order_no = {$row['order_no']}";
241 $res = db_query($sql, "Could not retrieve purchase order details");
242 $row2 = db_fetch_row($res);
243 if ($row2[0] == $row2[1])
245 $sql = "DELETE FROM ".TB_PREF."purch_order_details WHERE order_no = {$row['order_no']}";
246 db_query($sql, "Could not delete purchase order details");
247 $sql = "DELETE FROM ".TB_PREF."purch_orders WHERE order_no = {$row['order_no']}";
248 db_query($sql, "Could not delete purchase order");
249 delete_attachments_and_comments(ST_PURCHORDER, $row['order_no']);
252 $sql = "SELECT id FROM ".TB_PREF."grn_batch WHERE delivery_date <= '$to'";
253 $result = db_query($sql, "Could not retrieve grn batch");
254 while ($row = db_fetch($result))
256 $sql = "DELETE FROM ".TB_PREF."grn_items WHERE grn_batch_id = {$row['id']}";
257 db_query($sql, "Could not delete grn items");
258 $sql = "DELETE FROM ".TB_PREF."grn_batch WHERE id = {$row['id']}";
259 db_query($sql, "Could not delete grn batch");
260 delete_attachments_and_comments(25, $row['id']);
262 $sql = "SELECT trans_no, type FROM ".TB_PREF."debtor_trans WHERE tran_date <= '$to' AND
263 (ov_amount + ov_gst + ov_freight + ov_freight_tax + ov_discount) = alloc";
264 $result = db_query($sql, "Could not retrieve debtor trans");
265 while ($row = db_fetch($result))
267 if ($row['type'] == ST_SALESINVOICE)
269 $deliveries = get_sales_parent_numbers($row['type'], $row['trans_no']);
270 foreach ($deliveries as $delivery)
272 $sql = "DELETE FROM ".TB_PREF."debtor_trans_details WHERE debtor_trans_no = $delivery AND debtor_trans_type = ".ST_CUSTDELIVERY;
273 db_query($sql, "Could not delete debtor trans details");
274 $sql = "DELETE FROM ".TB_PREF."debtor_trans WHERE trans_no = $delivery AND type = ".ST_CUSTDELIVERY;
275 db_query($sql, "Could not delete debtor trans");
276 delete_attachments_and_comments(ST_CUSTDELIVERY, $delivery);
279 $sql = "DELETE FROM ".TB_PREF."cust_allocations WHERE trans_no_from = {$row['trans_no']} AND trans_type_from = {$row['type']}";
280 db_query($sql, "Could not delete cust allocations");
281 $sql = "DELETE FROM ".TB_PREF."debtor_trans_details WHERE debtor_trans_no = {$row['trans_no']} AND debtor_trans_type = {$row['type']}";
282 db_query($sql, "Could not delete debtor trans details");
283 $sql = "DELETE FROM ".TB_PREF."debtor_trans WHERE trans_no = {$row['trans_no']} AND type = {$row['type']}";
284 db_query($sql, "Could not delete debtor trans");
285 delete_attachments_and_comments($row['type'], $row['trans_no']);
287 $sql = "SELECT trans_no, type FROM ".TB_PREF."supp_trans WHERE tran_date <= '$to' AND
288 ABS(ov_amount + ov_gst + ov_discount) = alloc";
289 $result = db_query($sql, "Could not retrieve supp trans");
290 while ($row = db_fetch($result))
292 $sql = "DELETE FROM ".TB_PREF."supp_allocations WHERE trans_no_from = {$row['trans_no']} AND trans_type_from = {$row['type']}";
293 db_query($sql, "Could not delete supp allocations");
294 $sql = "DELETE FROM ".TB_PREF."supp_invoice_items WHERE supp_trans_no = {$row['trans_no']} AND supp_trans_type = {$row['type']}";
295 db_query($sql, "Could not delete supp invoice items");
296 $sql = "DELETE FROM ".TB_PREF."supp_trans WHERE trans_no = {$row['trans_no']} AND type = {$row['type']}";
297 db_query($sql, "Could not delete supp trans");
298 delete_attachments_and_comments($row['type'], $row['trans_no']);
300 $sql = "SELECT id FROM ".TB_PREF."workorders WHERE released_date <= '$to' AND closed=1";
301 $result = db_query($sql, "Could not retrieve supp trans");
302 while ($row = db_fetch($result))
304 $sql = "SELECT issue_no FROM ".TB_PREF."wo_issues WHERE workorder_id = {$row['id']}";
305 $res = db_query($sql, "Could not retrieve wo issues");
306 while ($row2 = db_fetch_row($res))
308 $sql = "DELETE FROM ".TB_PREF."wo_issue_items WHERE issue_id = {$row2[0]}";
309 db_query($sql, "Could not delete wo issue items");
311 delete_attachments_and_comments(ST_MANUISSUE, $row['id']);
312 $sql = "DELETE FROM ".TB_PREF."wo_issues WHERE workorder_id = {$row['id']}";
313 db_query($sql, "Could not delete wo issues");
314 $sql = "DELETE FROM ".TB_PREF."wo_manufacture WHERE workorder_id = {$row['id']}";
315 db_query($sql, "Could not delete wo manufacture");
316 $sql = "DELETE FROM ".TB_PREF."wo_requirements WHERE workorder_id = {$row['id']}";
317 db_query($sql, "Could not delete wo requirements");
318 $sql = "DELETE FROM ".TB_PREF."workorders WHERE id = {$row['id']}";
319 db_query($sql, "Could not delete workorders");
320 delete_attachments_and_comments(ST_WORKORDER, $row['id']);
322 $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
324 $result = db_query($sql, "Could not retrieve supp trans");
325 while ($row = db_fetch($result))
327 $sql = "DELETE FROM ".TB_PREF."stock_moves WHERE tran_date <= '$to' AND loc_code = '{$row['loc_code']}' AND stock_id = '{$row['stock_id']}'";
328 db_query($sql, "Could not delete stock moves");
330 $std_cost = ($qty == 0 ? 0 : round2($row['std_cost'] / $qty, user_price_dec()));
331 $sql = "INSERT INTO ".TB_PREF."stock_moves (stock_id, loc_code, tran_date, reference, qty, standard_cost) VALUES
332 ('{$row['stock_id']}', '{$row['loc_code']}', '$to', '$ref', $qty, $std_cost)";
333 db_query($sql, "Could not insert stock move");
335 $sql = "DELETE FROM ".TB_PREF."voided WHERE date_ <= '$to'";
336 db_query($sql, "Could not delete voided items");
337 $sql = "DELETE FROM ".TB_PREF."trans_tax_details WHERE tran_date <= '$to'";
338 db_query($sql, "Could not delete trans tax details");
339 $sql = "DELETE FROM ".TB_PREF."exchange_rates WHERE date_ <= '$to'";
340 db_query($sql, "Could not delete exchange rates");
341 $sql = "DELETE FROM ".TB_PREF."budget_trans WHERE tran_date <= '$to'";
342 db_query($sql, "Could not delete exchange rates");
343 $sql = "SELECT account, SUM(amount) AS amount, person_type_id, person_id FROM "
344 .TB_PREF."gl_trans WHERE tran_date <= '$to' GROUP by account, person_type_id, person_id";
345 $result = db_query($sql, "Could not retrieve gl trans");
346 $trans_no = get_next_trans_no(ST_JOURNAL);
350 while ($row = db_fetch($result))
352 if ($last_account != $row['account']) // deletes all subledgers postings, so do it once for account
354 $sql = "DELETE FROM ".TB_PREF."gl_trans WHERE tran_date <= '$to' AND account = '{$row['account']}'";
355 db_query($sql, "Could not delete gl trans");
356 $last_account = $row['account'];
358 if (is_account_balancesheet($row['account']) && $row['amount'])
360 $sql = "INSERT INTO ".TB_PREF."gl_trans (type, type_no, tran_date, account, memo_, amount, person_type_id, person_id) VALUES
361 (".ST_JOURNAL.", $trans_no, '$to', '{$row['account']}', '$ref', {$row['amount']}, "
362 .db_escape($row['person_type_id'], true).", ".db_escape($row['person_id'], true).")";
363 db_query($sql, "Could not insert gl trans");
370 $trans_type = ST_JOURNAL;
371 $reference = $Refs->get_next($trans_type);
372 $Refs->save($trans_type, $trans_no, $reference);
373 $date_ = sql2date($to);
375 add_audit_trail($trans_type, $trans_no, $date_);
377 $sql = "SELECT bank_act, SUM(amount) AS amount FROM ".TB_PREF."bank_trans WHERE trans_date <= '$to' GROUP BY bank_act";
378 $result = db_query($sql, "Could not retrieve bank trans");
379 while ($row = db_fetch($result))
381 $sql = "DELETE FROM ".TB_PREF."bank_trans WHERE trans_date <= '$to' AND bank_act = '{$row['bank_act']}'";
382 db_query($sql, "Could not delete bank trans");
383 $sql = "INSERT INTO ".TB_PREF."bank_trans (type, trans_no, trans_date, bank_act, ref, amount) VALUES
384 (0, 0, '$to', '{$row['bank_act']}', '$ref', {$row['amount']})";
385 db_query($sql, "Could not insert bank trans");
388 $sql = "DELETE FROM ".TB_PREF."audit_trail WHERE gl_date <= '$to'";
389 db_query($sql, "Could not delete audit trail");
391 $sql = "SELECT type, id FROM ".TB_PREF."comments WHERE type != ".ST_SALESQUOTE." AND type != ".ST_SALESORDER." AND type != ".ST_PURCHORDER;
392 $result = db_query($sql, "Could not retrieve comments");
393 while ($row = db_fetch($result))
395 $sql = "SELECT count(*) FROM ".TB_PREF."gl_trans WHERE type = {$row['type']} AND type_no = {$row['id']}";
396 $res = db_query($sql, "Could not retrieve gl_trans");
397 $row2 = db_fetch_row($res);
398 if ($row2[0] == 0) // if no link, then delete comments
400 $sql = "DELETE FROM ".TB_PREF."comments WHERE type = {$row['type']} AND id = {$row['id']}";
401 db_query($sql, "Could not delete comments");
404 $sql = "SELECT type, id FROM ".TB_PREF."refs WHERE type != ".ST_SALESQUOTE." AND type != ".ST_SALESORDER." AND type != ".ST_PURCHORDER;
405 $result = db_query($sql, "Could not retrieve refs");
406 while ($row = db_fetch($result))
408 $sql = "SELECT count(*) FROM ".TB_PREF."gl_trans WHERE type = {$row['type']} AND type_no = {$row['id']}";
409 $res = db_query($sql, "Could not retrieve gl_trans");
410 $row2 = db_fetch_row($res);
411 if ($row2[0] == 0) // if no link, then delete refs
413 $sql = "DELETE FROM ".TB_PREF."refs WHERE type = {$row['type']} AND id = {$row['id']}";
414 db_query($sql, "Could not delete refs");
418 delete_fiscalyear($selected_id);
419 commit_transaction();