X-Git-Url: https://delta.frontaccounting.com/gitweb/?a=blobdiff_plain;ds=sidebyside;f=admin%2Fdb%2Ffiscalyears_db.inc;h=9ace5d195ee0b3c684a1f1a811dae7cb56803ed1;hb=7840540918bfd37c886877a5d52e3ba0b3373289;hp=ff4e4bcd3b7664ed72206df3585c4096ee5c5db6;hpb=af781e43e60e9cbaea7f7466678a28ca5d67d56c;p=fa-stable.git diff --git a/admin/db/fiscalyears_db.inc b/admin/db/fiscalyears_db.inc index ff4e4bcd..9ace5d19 100644 --- a/admin/db/fiscalyears_db.inc +++ b/admin/db/fiscalyears_db.inc @@ -81,6 +81,18 @@ function is_date_in_fiscalyears($date, $closed=true) return db_fetch($result) !== false; } +function get_fiscalyear_begin_for_date($date) +{ + $date = date2sql($date); + $sql = "SELECT begin FROM ".TB_PREF."fiscal_year WHERE '$date' >= begin AND '$date' <= end"; + $result = db_query($sql, "could not get begin date of the fiscal year"); + $row = db_fetch_row($result); + if ($row != false) + return sql2date($row[0]); + else + return begin_fiscalyear(); +} + function check_begin_end_date($date1, $date2) { $sql = "SELECT MAX(end), MIN(begin) FROM ".TB_PREF."fiscal_year"; @@ -168,7 +180,7 @@ function open_year($year) $from = sql2date($myrow['begin']); begin_transaction(); - open_transactions($from); + close_transactions(add_days($from, -1)); commit_transaction(); } @@ -325,22 +337,40 @@ function delete_this_fiscalyear($selected_id) db_query($sql, "Could not delete exchange rates"); $sql = "DELETE FROM ".TB_PREF."budget_trans WHERE tran_date <= '$to'"; db_query($sql, "Could not delete exchange rates"); - - $sql = "SELECT account, SUM(amount) AS amount FROM ".TB_PREF."gl_trans WHERE tran_date <= '$to' GROUP by account"; + $sql = "SELECT account, SUM(amount) AS amount, person_type_id, person_id FROM " + .TB_PREF."gl_trans WHERE tran_date <= '$to' GROUP by account, person_type_id, person_id"; $result = db_query($sql, "Could not retrieve gl trans"); + $trans_no = get_next_trans_no(ST_JOURNAL); + + $last_account=''; + $new = false; while ($row = db_fetch($result)) { - $sql = "DELETE FROM ".TB_PREF."gl_trans WHERE tran_date <= '$to' AND account = '{$row['account']}'"; - db_query($sql, "Could not delete gl trans"); - if (is_account_balancesheet($row['account'])) + if ($last_account != $row['account']) // deletes all subledgers postings, so do it once for account { - $trans_no = get_next_trans_no(ST_JOURNAL); - $sql = "INSERT INTO ".TB_PREF."gl_trans (type, type_no, tran_date, account, memo_, amount) VALUES - (".ST_JOURNAL.", $trans_no, '$to', '{$row['account']}', '$ref', {$row['amount']})"; + $sql = "DELETE FROM ".TB_PREF."gl_trans WHERE tran_date <= '$to' AND account = '{$row['account']}'"; + db_query($sql, "Could not delete gl trans"); + $last_account = $row['account']; + } + if (is_account_balancesheet($row['account']) && $row['amount']) + { + $sql = "INSERT INTO ".TB_PREF."gl_trans (type, type_no, tran_date, account, memo_, amount, person_type_id, person_id) VALUES + (".ST_JOURNAL.", $trans_no, '$to', '{$row['account']}', '$ref', {$row['amount']}, " + .db_escape($row['person_type_id'], true).", ".db_escape($row['person_id'], true).")"; db_query($sql, "Could not insert gl trans"); + $new = true; } } - + if ($new) + { + global $Refs; + $trans_type = ST_JOURNAL; + $reference = $Refs->get_next($trans_type); + $Refs->save($trans_type, $trans_no, $reference); + $date_ = sql2date($to); + + add_audit_trail($trans_type, $trans_no, $date_); + } $sql = "SELECT bank_act, SUM(amount) AS amount FROM ".TB_PREF."bank_trans WHERE trans_date <= '$to' GROUP BY bank_act"; $result = db_query($sql, "Could not retrieve bank trans"); while ($row = db_fetch($result))