1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as
9 # published by the Free Software Foundation, either version 3 of the
10 # License, or (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 ##############################################################################
21 from osv import fields, osv
22 from tools.translate import _
25 class account_fiscalyear_close(osv.osv_memory):
27 Closes Account Fiscalyear and Generate Opening entries for New Fiscalyear
29 _name = "account.fiscalyear.close"
30 _description = "Fiscalyear Close"
32 'fy_id': fields.many2one('account.fiscalyear', \
33 'Fiscal Year to close', required=True),
34 'fy2_id': fields.many2one('account.fiscalyear', \
35 'New Fiscal Year', required=True),
36 'journal_id': fields.many2one('account.journal', \
37 'Opening Entries Journal', required=True, help='The best practice here is to use a journal dedicated to contain the opening entries of all fiscal years. Note that you should define it with default debit/credit accounts and with a centralized counterpart.'),
38 'period_id': fields.many2one('account.period', \
39 'Opening Entries Period', required=True),
40 'report_name': fields.char('Name of new entries',size=64, required=True),
43 'report_name':'End of Fiscal Year Entry',
46 def data_save(self, cr, uid, ids, context=None):
48 This function close account fiscalyear and create entries in new fiscalyear
49 @param cr: the current row, from the database cursor,
50 @param uid: the current user’s ID for security checks,
51 @param ids: List of Account fiscalyear close state’s IDs
54 obj_acc_period = self.pool.get('account.period')
55 obj_acc_fiscalyear = self.pool.get('account.fiscalyear')
56 obj_acc_journal = self.pool.get('account.journal')
57 obj_acc_move_line = self.pool.get('account.move.line')
58 obj_acc_account = self.pool.get('account.account')
59 obj_acc_journal_period = self.pool.get('account.journal.period')
60 obj_rec = self.pool.get('account.move.reconcile')
62 data = self.read(cr, uid, ids, context=context)
66 fy_id = data[0]['fy_id']
68 cr.execute("SELECT id FROM account_period WHERE date_stop < (SELECT date_start FROM account_fiscalyear WHERE id = %s)" , (str(data[0]['fy2_id']),))
69 fy_period_set = ','.join(map(lambda id: str(id[0]), cr.fetchall()))
70 cr.execute("SELECT id FROM account_period WHERE date_start > (SELECT date_stop FROM account_fiscalyear WHERE id = %s)" , (str(fy_id),))
71 fy2_period_set = ','.join(map(lambda id: str(id[0]), cr.fetchall()))
73 period = obj_acc_period.browse(cr, uid, data[0]['period_id'], context=context)
74 new_fyear = obj_acc_fiscalyear.browse(cr, uid, data[0]['fy2_id'], context=context)
75 old_fyear = obj_acc_fiscalyear.browse(cr, uid, data[0]['fy_id'], context=context)
77 new_journal = data[0]['journal_id']
78 new_journal = obj_acc_journal.browse(cr, uid, new_journal, context=context)
80 if not new_journal.default_credit_account_id or not new_journal.default_debit_account_id:
81 raise osv.except_osv(_('UserError'),
82 _('The journal must have default credit and debit account'))
83 if not new_journal.centralisation:
84 raise osv.except_osv(_('UserError'),
85 _('The journal must have centralised counterpart'))
87 move_ids = obj_acc_move_line.search(cr, uid, [
88 ('journal_id', '=', new_journal.id), ('period_id.fiscalyear_id', '=', new_fyear.id)])
91 obj_acc_move_line._remove_move_reconcile(cr, uid, move_ids, context=context)
92 obj_acc_move_line.unlink(cr, uid, move_ids, context=context)
94 cr.execute("SELECT id FROM account_fiscalyear WHERE date_stop < %s", (str(new_fyear.date_start),))
95 result = cr.dictfetchall()
96 fy_ids = ','.join([str(x['id']) for x in result])
97 query_line = obj_acc_move_line._query_get(cr, uid,
98 obj='account_move_line', context={'fiscalyear': fy_ids})
99 cr.execute('select id from account_account WHERE active AND company_id = %s', (old_fyear.company_id.id,))
100 ids = map(lambda x: x[0], cr.fetchall())
101 for account in obj_acc_account.browse(cr, uid, ids,
102 context={'fiscalyear': fy_id}):
103 accnt_type_data = account.user_type
104 if not accnt_type_data:
106 if accnt_type_data.close_method=='none' or account.type == 'view':
108 if accnt_type_data.close_method=='balance':
109 if abs(account.balance)>0.0001:
110 obj_acc_move_line.create(cr, uid, {
111 'debit': account.balance>0 and account.balance,
112 'credit': account.balance<0 and -account.balance,
113 'name': data[0]['report_name'],
114 'date': period.date_start,
115 'journal_id': new_journal.id,
116 'period_id': period.id,
117 'account_id': account.id
118 }, {'journal_id': new_journal.id, 'period_id':period.id})
119 if accnt_type_data.close_method == 'unreconciled':
123 cr.execute('SELECT id, name, quantity, debit, credit, account_id, ref, ' \
124 'amount_currency, currency_id, blocked, partner_id, ' \
125 'date_maturity, date_created ' \
126 'FROM account_move_line ' \
127 'WHERE account_id = %s ' \
128 'AND ' + query_line + ' ' \
129 'AND reconcile_id is NULL ' \
131 'LIMIT %s OFFSET %s', (account.id, limit, offset))
132 result = cr.dictfetchall()
138 'date': period.date_start,
139 'journal_id': new_journal.id,
140 'period_id': period.id,
142 obj_acc_move_line.create(cr, uid, move, {
143 'journal_id': new_journal.id,
144 'period_id': period.id,
148 #We have also to consider all move_lines that were reconciled
149 #on another fiscal year, and report them too
153 cr.execute('SELECT DISTINCT b.id, b.name, b.quantity, b.debit, b.credit, b.account_id, b.ref, ' \
154 'b.amount_currency, b.currency_id, b.blocked, b.partner_id, ' \
155 'b.date_maturity, b.date_created ' \
156 'FROM account_move_line a, account_move_line b ' \
157 'WHERE b.account_id = %s ' \
158 'AND b.reconcile_id is NOT NULL ' \
159 'AND a.reconcile_id = b.reconcile_id ' \
160 'AND b.period_id IN ('+fy_period_set+') ' \
161 'AND a.period_id IN ('+fy2_period_set+') ' \
163 'LIMIT %s OFFSET %s', (account.id, limit, offset))
164 result = cr.dictfetchall()
170 'date': period.date_start,
171 'journal_id': new_journal.id,
172 'period_id': period.id,
174 obj_acc_move_line.create(cr, uid, move, {
175 'journal_id': new_journal.id,
176 'period_id': period.id,
179 if accnt_type_data.close_method=='detail':
183 cr.execute('SELECT id, name, quantity, debit, credit, account_id, ref, ' \
184 'amount_currency, currency_id, blocked, partner_id, ' \
185 'date_maturity, date_created ' \
186 'FROM account_move_line ' \
187 'WHERE account_id = %s ' \
188 'AND ' + query_line + ' ' \
190 'LIMIT %s OFFSET %s', (account.id, limit, offset))
192 result = cr.dictfetchall()
198 'date': period.date_start,
199 'journal_id': new_journal.id,
200 'period_id': period.id,
202 obj_acc_move_line.create(cr, uid, move)
204 ids = obj_acc_move_line.search(cr, uid, [('journal_id','=',new_journal.id),
205 ('period_id.fiscalyear_id','=',new_fyear.id)])
206 context['fy_closing'] = True
209 obj_acc_move_line.reconcile(cr, uid, ids, context=context)
210 new_period = data[0]['period_id']
211 ids = obj_acc_journal_period.search(cr, uid, [('journal_id','=',new_journal.id),('period_id','=',new_period)])
213 ids = [obj_acc_journal_period.create(cr, uid, {
214 'name': (new_journal.name or '')+':'+(period.code or ''),
215 'journal_id': new_journal.id,
216 'period_id': period.id
218 cr.execute('UPDATE account_fiscalyear ' \
219 'SET end_journal_period_id = %s ' \
220 'WHERE id = %s', (ids[0], old_fyear.id))
223 account_fiscalyear_close()
225 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: