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 ##############################################################################
24 from osv import fields, osv
25 from tools.translate import _
26 import decimal_precision as dp
28 class account_bank_statement(osv.osv):
30 def create(self, cr, uid, vals, context=None):
32 if 'line_ids' in vals:
34 for line in vals['line_ids']:
36 line[2]['sequence'] = seq
37 return super(account_bank_statement, self).create(cr, uid, vals, context=context)
39 def write(self, cr, uid, ids, vals, context=None):
40 res = super(account_bank_statement, self).write(cr, uid, ids, vals, context=context)
41 account_bank_statement_line_obj = self.pool.get('account.bank.statement.line')
42 for statement in self.browse(cr, uid, ids, context):
44 for line in statement.line_ids:
46 account_bank_statement_line_obj.write(cr, uid, [line.id], {'sequence': seq}, context=context)
49 def _default_journal_id(self, cr, uid, context=None):
52 journal_pool = self.pool.get('account.journal')
53 journal_type = context.get('journal_type', False)
55 company_id = self.pool.get('res.company')._company_default_get(cr, uid, 'account.bank.statement',context=context)
57 ids = journal_pool.search(cr, uid, [('type', '=', journal_type),('company_id','=',company_id)])
62 def _end_balance(self, cursor, user, ids, name, attr, context=None):
63 res_currency_obj = self.pool.get('res.currency')
64 res_users_obj = self.pool.get('res.users')
67 company_currency_id = res_users_obj.browse(cursor, user, user,
68 context=context).company_id.currency_id.id
70 statements = self.browse(cursor, user, ids, context=context)
71 for statement in statements:
72 res[statement.id] = statement.balance_start
73 currency_id = statement.currency.id
74 for line in statement.move_line_ids:
76 if line.account_id.id == \
77 statement.journal_id.default_debit_account_id.id:
78 res[statement.id] += res_currency_obj.compute(cursor,
79 user, company_currency_id, currency_id,
80 line.debit, context=context)
82 if line.account_id.id == \
83 statement.journal_id.default_credit_account_id.id:
84 res[statement.id] -= res_currency_obj.compute(cursor,
85 user, company_currency_id, currency_id,
86 line.credit, context=context)
88 if statement.state in ('draft', 'open'):
89 for line in statement.line_ids:
90 res[statement.id] += line.amount
92 res[r] = round(res[r], 2)
95 def _get_period(self, cr, uid, context=None):
96 periods = self.pool.get('account.period').find(cr, uid)
101 def _currency(self, cursor, user, ids, name, args, context=None):
103 res_currency_obj = self.pool.get('res.currency')
104 res_users_obj = self.pool.get('res.users')
105 default_currency = res_users_obj.browse(cursor, user,
106 user, context=context).company_id.currency_id
107 for statement in self.browse(cursor, user, ids, context=context):
108 currency = statement.journal_id.currency
110 currency = default_currency
111 res[statement.id] = currency.id
113 for currency_id, currency_name in res_currency_obj.name_get(cursor,
114 user, [x for x in res.values()], context=context):
115 currency_names[currency_id] = currency_name
116 for statement_id in res.keys():
117 currency_id = res[statement_id]
118 res[statement_id] = (currency_id, currency_names[currency_id])
121 def _get_statement(self, cr, uid, ids, context=None):
123 for line in self.pool.get('account.bank.statement.line').browse(cr, uid, ids, context=context):
124 result[line.statement_id.id] = True
127 _order = "date desc, id desc"
128 _name = "account.bank.statement"
129 _description = "Bank Statement"
130 _inherit = ['mail.thread']
132 'name': fields.char('Name', size=64, required=True, states={'draft': [('readonly', False)]}, readonly=True, help='if you give the Name other then /, its created Accounting Entries Move will be with same name as statement name. This allows the statement entries to have the same references than the statement itself'), # readonly for account_cash_statement
133 'date': fields.date('Date', required=True, states={'confirm': [('readonly', True)]}, select=True),
134 'journal_id': fields.many2one('account.journal', 'Journal', required=True,
135 readonly=True, states={'draft':[('readonly',False)]}),
136 'period_id': fields.many2one('account.period', 'Period', required=True,
137 states={'confirm':[('readonly', True)]}),
138 'balance_start': fields.float('Starting Balance', digits_compute=dp.get_precision('Account'),
139 states={'confirm':[('readonly',True)]}),
140 'balance_end_real': fields.float('Ending Balance', digits_compute=dp.get_precision('Account'),
141 states={'confirm': [('readonly', True)]}),
142 'balance_end': fields.function(_end_balance,
144 'account.bank.statement': (lambda self, cr, uid, ids, c={}: ids, ['line_ids','move_line_ids'], 10),
145 'account.bank.statement.line': (_get_statement, ['amount'], 10),
147 string="Computed Balance", help='Balance as calculated based on Starting Balance and transaction lines'),
148 'company_id': fields.related('journal_id', 'company_id', type='many2one', relation='res.company', string='Company', store=True, readonly=True),
149 'line_ids': fields.one2many('account.bank.statement.line',
150 'statement_id', 'Statement lines',
151 states={'confirm':[('readonly', True)]}),
152 'move_line_ids': fields.one2many('account.move.line', 'statement_id',
153 'Entry lines', states={'confirm':[('readonly',True)]}),
154 'state': fields.selection([('draft', 'New'),
155 ('open','Open'), # used by cash statements
156 ('confirm', 'Closed')],
157 'Status', required=True, readonly="1",
158 help='When new statement is created the state will be \'Draft\'.\n'
159 'And after getting confirmation from the bank it will be in \'Confirmed\' state.'),
160 'currency': fields.function(_currency, string='Currency',
161 type='many2one', relation='res.currency'),
162 'account_id': fields.related('journal_id', 'default_debit_account_id', type='many2one', relation='account.account', string='Account used in this journal', readonly=True, help='used in statement reconciliation domain, but shouldn\'t be used elswhere.'),
167 'date': fields.date.context_today,
169 'journal_id': _default_journal_id,
170 'period_id': _get_period,
171 'company_id': lambda self,cr,uid,c: self.pool.get('res.company')._company_default_get(cr, uid, 'account.bank.statement',context=c),
174 def _check_company_id(self, cr, uid, ids, context=None):
175 for statement in self.browse(cr, uid, ids, context=context):
176 if statement.company_id.id != statement.period_id.company_id.id:
181 (_check_company_id, 'The journal and period chosen have to belong to the same company.', ['journal_id','period_id']),
184 def onchange_date(self, cr, uid, ids, date, company_id, context=None):
186 Find the correct period to use for the given date and company_id, return it and set it in the context
189 period_pool = self.pool.get('account.period')
194 ctx.update({'company_id': company_id})
195 pids = period_pool.find(cr, uid, dt=date, context=ctx)
197 res.update({'period_id': pids[0]})
198 context.update({'period_id': pids[0]})
205 def button_dummy(self, cr, uid, ids, context=None):
206 return self.write(cr, uid, ids, {}, context=context)
208 def create_move_from_st_line(self, cr, uid, st_line_id, company_currency_id, st_line_number, context=None):
211 res_currency_obj = self.pool.get('res.currency')
212 account_move_obj = self.pool.get('account.move')
213 account_move_line_obj = self.pool.get('account.move.line')
214 account_bank_statement_line_obj = self.pool.get('account.bank.statement.line')
215 st_line = account_bank_statement_line_obj.browse(cr, uid, st_line_id, context=context)
216 st = st_line.statement_id
218 context.update({'date': st_line.date})
220 move_id = account_move_obj.create(cr, uid, {
221 'journal_id': st.journal_id.id,
222 'period_id': st.period_id.id,
223 'date': st_line.date,
224 'name': st_line_number,
227 account_bank_statement_line_obj.write(cr, uid, [st_line.id], {
228 'move_ids': [(4, move_id, False)]
232 if st_line.amount >= 0:
233 account_id = st.journal_id.default_credit_account_id.id
235 account_id = st.journal_id.default_debit_account_id.id
237 acc_cur = ((st_line.amount<=0) and st.journal_id.default_debit_account_id) or st_line.account_id
239 'res.currency.compute.account': acc_cur,
241 amount = res_currency_obj.compute(cr, uid, st.currency.id,
242 company_currency_id, st_line.amount, context=context)
245 'name': st_line.name,
246 'date': st_line.date,
249 'partner_id': ((st_line.partner_id) and st_line.partner_id.id) or False,
250 'account_id': (st_line.account_id) and st_line.account_id.id,
251 'credit': ((amount>0) and amount) or 0.0,
252 'debit': ((amount<0) and -amount) or 0.0,
253 'statement_id': st.id,
254 'journal_id': st.journal_id.id,
255 'period_id': st.period_id.id,
256 'currency_id': st.currency.id,
257 'analytic_account_id': st_line.analytic_account_id and st_line.analytic_account_id.id or False
260 if st.currency.id <> company_currency_id:
261 amount_cur = res_currency_obj.compute(cr, uid, company_currency_id,
262 st.currency.id, amount, context=context)
263 val['amount_currency'] = -amount_cur
265 if st_line.account_id and st_line.account_id.currency_id and st_line.account_id.currency_id.id <> company_currency_id:
266 val['currency_id'] = st_line.account_id.currency_id.id
267 amount_cur = res_currency_obj.compute(cr, uid, company_currency_id,
268 st_line.account_id.currency_id.id, amount, context=context)
269 val['amount_currency'] = -amount_cur
271 move_line_id = account_move_line_obj.create(cr, uid, val, context=context)
272 torec.append(move_line_id)
274 # Fill the secondary amount/currency
275 # if currency is not the same than the company
276 amount_currency = False
278 if st.currency.id <> company_currency_id:
279 amount_currency = st_line.amount
280 currency_id = st.currency.id
281 account_move_line_obj.create(cr, uid, {
282 'name': st_line.name,
283 'date': st_line.date,
286 'partner_id': ((st_line.partner_id) and st_line.partner_id.id) or False,
287 'account_id': account_id,
288 'credit': ((amount < 0) and -amount) or 0.0,
289 'debit': ((amount > 0) and amount) or 0.0,
290 'statement_id': st.id,
291 'journal_id': st.journal_id.id,
292 'period_id': st.period_id.id,
293 'amount_currency': amount_currency,
294 'currency_id': currency_id,
297 for line in account_move_line_obj.browse(cr, uid, [x.id for x in
298 account_move_obj.browse(cr, uid, move_id,
299 context=context).line_id],
301 if line.state <> 'valid':
302 raise osv.except_osv(_('Error !'),
303 _('Journal item "%s" is not valid.') % line.name)
305 # Bank statements will not consider boolean on journal entry_posted
306 account_move_obj.post(cr, uid, [move_id], context=context)
309 def get_next_st_line_number(self, cr, uid, st_number, st_line, context=None):
310 return st_number + '/' + str(st_line.sequence)
312 def balance_check(self, cr, uid, st_id, journal_type='bank', context=None):
313 st = self.browse(cr, uid, st_id, context=context)
314 if not ((abs((st.balance_end or 0.0) - st.balance_end_real) < 0.0001) or (abs((st.balance_end or 0.0) - st.balance_end_cash) < 0.0001)):
315 raise osv.except_osv(_('Error !'),
316 _('The statement balance is incorrect !\nThe expected balance (%.2f) is different than the computed one. (%.2f)') % (st.balance_end_real, st.balance_end))
319 def statement_close(self, cr, uid, ids, journal_type='bank', context=None):
320 return self.write(cr, uid, ids, {'state':'confirm'}, context=context)
322 def check_status_condition(self, cr, uid, state, journal_type='bank'):
323 return state in ('draft','open')
325 def button_confirm_bank(self, cr, uid, ids, context=None):
326 obj_seq = self.pool.get('ir.sequence')
330 for st in self.browse(cr, uid, ids, context=context):
331 j_type = st.journal_id.type
332 company_currency_id = st.journal_id.company_id.currency_id.id
333 if not self.check_status_condition(cr, uid, st.state, journal_type=j_type):
336 self.balance_check(cr, uid, st.id, journal_type=j_type, context=context)
337 if (not st.journal_id.default_credit_account_id) \
338 or (not st.journal_id.default_debit_account_id):
339 raise osv.except_osv(_('Configuration Error !'),
340 _('Please verify that an account is defined in the journal.'))
342 if not st.name == '/':
345 c = {'fiscalyear_id': st.period_id.fiscalyear_id.id}
346 if st.journal_id.sequence_id:
347 st_number = obj_seq.next_by_id(cr, uid, st.journal_id.sequence_id.id, context=c)
349 st_number = obj_seq.next_by_code(cr, uid, 'account.bank.statement', context=c)
351 for line in st.move_line_ids:
352 if line.state <> 'valid':
353 raise osv.except_osv(_('Error !'),
354 _('The account entries lines are not in valid state.'))
355 for st_line in st.line_ids:
356 if st_line.analytic_account_id:
357 if not st.journal_id.analytic_journal_id:
358 raise osv.except_osv(_('No Analytic Journal !'),_("You have to assign an analytic journal on the '%s' journal!") % (st.journal_id.name,))
359 if not st_line.amount:
361 st_line_number = self.get_next_st_line_number(cr, uid, st_number, st_line, context)
362 self.create_move_from_st_line(cr, uid, st_line.id, company_currency_id, st_line_number, context)
364 self.write(cr, uid, [st.id], {
366 'balance_end_real': st.balance_end
368 self.message_append_note(cr, uid, [st.id], body=_('Statement %s is confirmed, journal items are created.') % (st_number,), context=context)
369 return self.write(cr, uid, ids, {'state':'confirm'}, context=context)
371 def button_cancel(self, cr, uid, ids, context=None):
373 account_move_obj = self.pool.get('account.move')
374 for st in self.browse(cr, uid, ids, context=context):
375 if st.state=='draft':
378 for line in st.line_ids:
379 ids += [x.id for x in line.move_ids]
380 account_move_obj.unlink(cr, uid, ids, context)
382 return self.write(cr, uid, done, {'state':'draft'}, context=context)
384 def onchange_journal_id(self, cr, uid, statement_id, journal_id, context=None):
385 cr.execute('SELECT balance_end_real \
386 FROM account_bank_statement \
387 WHERE journal_id = %s AND NOT state = %s \
388 ORDER BY date DESC,id DESC LIMIT 1', (journal_id, 'draft'))
390 balance_start = res and res[0] or 0.0
391 journal_data = self.pool.get('account.journal').read(cr, uid, journal_id, ['default_debit_account_id', 'company_id'], context=context)
392 account_id = journal_data['default_debit_account_id']
393 company_id = journal_data['company_id']
394 return {'value': {'balance_start': balance_start, 'account_id': account_id, 'company_id': company_id}}
396 def unlink(self, cr, uid, ids, context=None):
397 stat = self.read(cr, uid, ids, ['state'], context=context)
400 if t['state'] in ('draft'):
401 unlink_ids.append(t['id'])
403 raise osv.except_osv(_('Invalid action !'), _('In order to delete a bank statement, you must first cancel it to delete related journal items.'))
404 osv.osv.unlink(self, cr, uid, unlink_ids, context=context)
407 def copy(self, cr, uid, id, default=None, context=None):
412 default = default.copy()
413 default['move_line_ids'] = []
414 return super(account_bank_statement, self).copy(cr, uid, id, default, context=context)
416 account_bank_statement()
418 class account_bank_statement_line(osv.osv):
420 def onchange_partner_id(self, cr, uid, ids, partner_id, context=None):
421 obj_partner = self.pool.get('res.partner')
426 part = obj_partner.browse(cr, uid, partner_id, context=context)
427 if not part.supplier and not part.customer:
429 elif part.supplier and part.customer:
432 if part.supplier == True:
434 if part.customer == True:
436 res_type = self.onchange_type(cr, uid, ids, partner_id=partner_id, type=type, context=context)
437 if res_type['value'] and res_type['value'].get('account_id', False):
438 return {'value': {'type': type, 'account_id': res_type['value']['account_id']}}
439 return {'value': {'type': type}}
441 def onchange_type(self, cr, uid, line_id, partner_id, type, context=None):
443 obj_partner = self.pool.get('res.partner')
449 line = self.browse(cr, uid, line_id, context=context)
450 if not line or (line and not line[0].account_id):
451 part = obj_partner.browse(cr, uid, partner_id, context=context)
452 if type == 'supplier':
453 account_id = part.property_account_payable.id
455 account_id = part.property_account_receivable.id
456 res['value']['account_id'] = account_id
459 _order = "statement_id desc, sequence"
460 _name = "account.bank.statement.line"
461 _description = "Bank Statement Line"
463 'name': fields.char('Communication', size=64, required=True),
464 'date': fields.date('Date', required=True),
465 'amount': fields.float('Amount', digits_compute=dp.get_precision('Account')),
466 'type': fields.selection([
467 ('supplier','Supplier'),
468 ('customer','Customer'),
469 ('general','General')
470 ], 'Type', required=True),
471 'partner_id': fields.many2one('res.partner', 'Partner'),
472 'account_id': fields.many2one('account.account','Account',
474 'statement_id': fields.many2one('account.bank.statement', 'Statement',
475 select=True, required=True, ondelete='cascade'),
476 'journal_id': fields.related('statement_id', 'journal_id', type='many2one', relation='account.journal', string='Journal', store=True, readonly=True),
477 'analytic_account_id': fields.many2one('account.analytic.account', 'Analytic Account'),
478 'move_ids': fields.many2many('account.move',
479 'account_bank_statement_line_move_rel', 'statement_line_id','move_id',
481 'ref': fields.char('Reference', size=32),
482 'note': fields.text('Notes'),
483 'sequence': fields.integer('Sequence', select=True, help="Gives the sequence order when displaying a list of bank statement lines."),
484 'company_id': fields.related('statement_id', 'company_id', type='many2one', relation='res.company', string='Company', store=True, readonly=True),
487 'name': lambda self,cr,uid,context={}: self.pool.get('ir.sequence').get(cr, uid, 'account.bank.statement.line'),
488 'date': lambda self,cr,uid,context={}: context.get('date', fields.date.context_today(self,cr,uid,context=context)),
492 account_bank_statement_line()
494 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: