[ADD] account,account_followup : Tooltips added for the wizards
[odoo/odoo.git] / addons / account / wizard / account_fiscalyear_close.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
6 #
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.
11 #
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.
16 #
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/>.
19 #
20 ##############################################################################
21
22 from osv import fields, osv
23 from tools.translate import _
24
25 class account_fiscalyear_close(osv.osv_memory):
26     """
27     Closes Account Fiscalyear and Generate Opening entries for New Fiscalyear
28     """
29     _name = "account.fiscalyear.close"
30     _description = "Fiscalyear Close"
31     _columns = {
32        'fy_id': fields.many2one('account.fiscalyear', \
33                                  'Fiscal Year to close', required=True, help="Select a Fiscal year to close"),
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, help="Give name of the new entries"),
41     }
42     _defaults = {
43         'report_name':'End of Fiscal Year Entry',
44     }
45
46     def data_save(self, cr, uid, ids, context=None):
47         """
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
52
53         """
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
61         data =  self.read(cr, uid, ids, context=context)
62
63         if context is None:
64             context = {}
65         fy_id = data[0]['fy_id']
66
67         cr.execute("SELECT id FROM account_period WHERE date_stop < (SELECT date_start FROM account_fiscalyear WHERE id = %s)", (str(data[0]['fy2_id']),))
68         fy_period_set = ','.join(map(lambda id: str(id[0]), cr.fetchall()))
69         cr.execute("SELECT id FROM account_period WHERE date_start > (SELECT date_stop FROM account_fiscalyear WHERE id = %s)", (str(fy_id),))
70         fy2_period_set = ','.join(map(lambda id: str(id[0]), cr.fetchall()))
71
72         period = obj_acc_period.browse(cr, uid, data[0]['period_id'], context=context)
73         new_fyear = obj_acc_fiscalyear.browse(cr, uid, data[0]['fy2_id'], context=context)
74         old_fyear = obj_acc_fiscalyear.browse(cr, uid, data[0]['fy_id'], context=context)
75
76         new_journal = data[0]['journal_id']
77         new_journal = obj_acc_journal.browse(cr, uid, new_journal, context=context)
78
79         if not new_journal.default_credit_account_id or not new_journal.default_debit_account_id:
80             raise osv.except_osv(_('UserError'),
81                     _('The journal must have default credit and debit account'))
82         if (not new_journal.centralisation) or new_journal.entry_posted:
83             raise osv.except_osv(_('UserError'),
84                     _('The journal must have centralised counterpart without the Skipping draft state option checked!'))
85
86         move_ids = obj_acc_move_line.search(cr, uid, [
87             ('journal_id', '=', new_journal.id), ('period_id.fiscalyear_id', '=', new_fyear.id)])
88
89         if move_ids:
90             obj_acc_move_line._remove_move_reconcile(cr, uid, move_ids, context=context)
91             obj_acc_move_line.unlink(cr, uid, move_ids, context=context)
92
93         cr.execute("SELECT id FROM account_fiscalyear WHERE date_stop < %s", (str(new_fyear.date_start),))
94         result = cr.dictfetchall()
95         fy_ids = ','.join([str(x['id']) for x in result])
96         query_line = obj_acc_move_line._query_get(cr, uid,
97                 obj='account_move_line', context={'fiscalyear': fy_ids})
98         cr.execute('select id from account_account WHERE active AND company_id = %s', (old_fyear.company_id.id,))
99         ids = map(lambda x: x[0], cr.fetchall())
100         for account in obj_acc_account.browse(cr, uid, ids,
101             context={'fiscalyear': fy_id}):
102             accnt_type_data = account.user_type
103             if not accnt_type_data:
104                 continue
105             if accnt_type_data.close_method=='none' or account.type == 'view':
106                 continue
107             if accnt_type_data.close_method=='balance':
108                 if abs(account.balance)>0.0001:
109                     obj_acc_move_line.create(cr, uid, {
110                         'debit': account.balance>0 and account.balance,
111                         'credit': account.balance<0 and -account.balance,
112                         'name': data[0]['report_name'],
113                         'date': period.date_start,
114                         'journal_id': new_journal.id,
115                         'period_id': period.id,
116                         'account_id': account.id
117                     }, {'journal_id': new_journal.id, 'period_id':period.id})
118             if accnt_type_data.close_method == 'unreconciled':
119                 offset = 0
120                 limit = 100
121                 while True:
122                     cr.execute('SELECT id, name, quantity, debit, credit, account_id, ref, ' \
123                                 'amount_currency, currency_id, blocked, partner_id, ' \
124                                 'date_maturity, date_created ' \
125                             'FROM account_move_line ' \
126                             'WHERE account_id = %s ' \
127                                 'AND ' + query_line + ' ' \
128                                 'AND reconcile_id is NULL ' \
129                             'ORDER BY id ' \
130                             'LIMIT %s OFFSET %s', (account.id, limit, offset))
131                     result = cr.dictfetchall()
132                     if not result:
133                         break
134                     for move in result:
135                         move.pop('id')
136                         move.update({
137                             'date': period.date_start,
138                             'journal_id': new_journal.id,
139                             'period_id': period.id,
140                         })
141                         obj_acc_move_line.create(cr, uid, move, {
142                             'journal_id': new_journal.id,
143                             'period_id': period.id,
144                             })
145                     offset += limit
146
147                 #We have also to consider all move_lines that were reconciled
148                 #on another fiscal year, and report them too
149                 offset = 0
150                 limit = 100
151                 while True:
152                     cr.execute('SELECT  DISTINCT b.id, b.name, b.quantity, b.debit, b.credit, b.account_id, b.ref, ' \
153                                 'b.amount_currency, b.currency_id, b.blocked, b.partner_id, ' \
154                                 'b.date_maturity, b.date_created ' \
155                             'FROM account_move_line a, account_move_line b ' \
156                             'WHERE b.account_id = %s ' \
157                                 'AND b.reconcile_id is NOT NULL ' \
158                                 'AND a.reconcile_id = b.reconcile_id ' \
159                                 'AND b.period_id IN ('+fy_period_set+') ' \
160                                 'AND a.period_id IN ('+fy2_period_set+') ' \
161                             'ORDER BY id ' \
162                             'LIMIT %s OFFSET %s', (account.id, limit, offset))
163                     result = cr.dictfetchall()
164                     if not result:
165                         break
166                     for move in result:
167                         move.pop('id')
168                         move.update({
169                             'date': period.date_start,
170                             'journal_id': new_journal.id,
171                             'period_id': period.id,
172                         })
173                         obj_acc_move_line.create(cr, uid, move, {
174                             'journal_id': new_journal.id,
175                             'period_id': period.id,
176                             })
177                     offset += limit
178             if accnt_type_data.close_method=='detail':
179                 offset = 0
180                 limit = 100
181                 while True:
182                     cr.execute('SELECT id, name, quantity, debit, credit, account_id, ref, ' \
183                                 'amount_currency, currency_id, blocked, partner_id, ' \
184                                 'date_maturity, date_created ' \
185                             'FROM account_move_line ' \
186                             'WHERE account_id = %s ' \
187                                 'AND ' + query_line + ' ' \
188                             'ORDER BY id ' \
189                             'LIMIT %s OFFSET %s', (account.id, limit, offset))
190
191                     result = cr.dictfetchall()
192                     if not result:
193                         break
194                     for move in result:
195                         move.pop('id')
196                         move.update({
197                             'date': period.date_start,
198                             'journal_id': new_journal.id,
199                             'period_id': period.id,
200                         })
201                         obj_acc_move_line.create(cr, uid, move)
202                     offset += limit
203         ids = obj_acc_move_line.search(cr, uid, [('journal_id','=',new_journal.id),
204             ('period_id.fiscalyear_id','=',new_fyear.id)])
205         context['fy_closing'] = True
206
207         if ids:
208             obj_acc_move_line.reconcile(cr, uid, ids, context=context)
209         new_period = data[0]['period_id']
210         ids = obj_acc_journal_period.search(cr, uid, [('journal_id','=',new_journal.id),('period_id','=',new_period)])
211         if not ids:
212             ids = [obj_acc_journal_period.create(cr, uid, {
213                    'name': (new_journal.name or '')+':'+(period.code or ''),
214                    'journal_id': new_journal.id,
215                    'period_id': period.id
216                })]
217         cr.execute('UPDATE account_fiscalyear ' \
218                     'SET end_journal_period_id = %s ' \
219                     'WHERE id = %s', (ids[0], old_fyear.id))
220         return {}
221
222 account_fiscalyear_close()
223
224 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: