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
26 from tools.misc import currency
27 from tools.translate import _
29 class account_bank_statement(osv.osv):
30 def _default_journal_id(self, cr, uid, context={}):
31 if context.get('journal_id', False):
32 return context['journal_id']
35 def _default_balance_start(self, cr, uid, context={}):
36 cr.execute('select id from account_bank_statement where journal_id=%s order by date desc limit 1', (1,))
39 return self.browse(cr, uid, [res[0]], context)[0].balance_end
42 def _end_balance(self, cursor, user, ids, name, attr, context=None):
43 res_currency_obj = self.pool.get('res.currency')
44 res_users_obj = self.pool.get('res.users')
48 company_currency_id = res_users_obj.browse(cursor, user, user,
49 context=context).company_id.currency_id.id
51 statements = self.browse(cursor, user, ids, context=context)
52 for statement in statements:
53 res[statement.id] = statement.balance_start
54 currency_id = statement.currency.id
55 for line in statement.move_line_ids:
57 if line.account_id.id == \
58 statement.journal_id.default_debit_account_id.id:
59 res[statement.id] += res_currency_obj.compute(cursor,
60 user, company_currency_id, currency_id,
61 line.debit, context=context)
63 if line.account_id.id == \
64 statement.journal_id.default_credit_account_id.id:
65 res[statement.id] -= res_currency_obj.compute(cursor,
66 user, company_currency_id, currency_id,
67 line.credit, context=context)
68 if statement.state == 'draft':
69 for line in statement.line_ids:
70 res[statement.id] += line.amount
72 res[r] = round(res[r], 2)
75 def _get_period(self, cr, uid, context={}):
76 periods = self.pool.get('account.period').find(cr, uid)
82 def _currency(self, cursor, user, ids, name, args, context=None):
84 res_currency_obj = self.pool.get('res.currency')
85 res_users_obj = self.pool.get('res.users')
86 default_currency = res_users_obj.browse(cursor, user,
87 user, context=context).company_id.currency_id
88 for statement in self.browse(cursor, user, ids, context=context):
89 currency = statement.journal_id.currency
91 currency = default_currency
92 res[statement.id] = currency.id
94 for currency_id, currency_name in res_currency_obj.name_get(cursor,
95 user, [x for x in res.values()], context=context):
96 currency_names[currency_id] = currency_name
97 for statement_id in res.keys():
98 currency_id = res[statement_id]
99 res[statement_id] = (currency_id, currency_names[currency_id])
103 _name = "account.bank.statement"
104 _description = "Bank Statement"
106 'name': fields.char('Name', size=64, required=True, states={'confirm': [('readonly', True)]}),
107 'date': fields.date('Date', required=True,
108 states={'confirm': [('readonly', True)]}),
109 'journal_id': fields.many2one('account.journal', 'Journal', required=True,
110 states={'confirm': [('readonly', True)]}, domain=[('type', '=', 'cash')]),
111 'period_id': fields.many2one('account.period', 'Period', required=True,
112 states={'confirm':[('readonly', True)]}),
113 'balance_start': fields.float('Starting Balance', digits=(16,2),
114 states={'confirm':[('readonly',True)]}),
115 'balance_end_real': fields.float('Ending Balance', digits=(16,2),
116 states={'confirm':[('readonly', True)]}),
117 'balance_end': fields.function(_end_balance, method=True, string='Balance'),
118 'line_ids': fields.one2many('account.bank.statement.line',
119 'statement_id', 'Statement lines',
120 states={'confirm':[('readonly', True)]}),
121 'move_line_ids': fields.one2many('account.move.line', 'statement_id',
122 'Entry lines', states={'confirm':[('readonly',True)]}),
123 'state': fields.selection([('draft', 'Draft'),('confirm', 'Confirmed')],
124 'State', required=True,
125 states={'confirm': [('readonly', True)]}, readonly="1",
126 help='When new statement is created the state will be \'Draft\'. \
127 \n* And after getting confirmation from the bank it will be in \'Confirmed\' state.'),
128 'currency': fields.function(_currency, method=True, string='Currency',
129 type='many2one', relation='res.currency'),
133 'name': lambda self, cr, uid, context=None: \
134 self.pool.get('ir.sequence').get(cr, uid, 'account.bank.statement'),
135 'date': lambda *a: time.strftime('%Y-%m-%d'),
136 'state': lambda *a: 'draft',
137 'balance_start': _default_balance_start,
138 'journal_id': _default_journal_id,
139 'period_id': _get_period,
142 def button_confirm(self, cr, uid, ids, context={}):
144 res_currency_obj = self.pool.get('res.currency')
145 res_users_obj = self.pool.get('res.users')
146 account_move_obj = self.pool.get('account.move')
147 account_move_line_obj = self.pool.get('account.move.line')
148 account_bank_statement_line_obj = \
149 self.pool.get('account.bank.statement.line')
151 company_currency_id = res_users_obj.browse(cr, uid, uid,
152 context=context).company_id.currency_id.id
154 for st in self.browse(cr, uid, ids, context):
155 if not st.state=='draft':
158 if not (abs((st.balance_end or 0.0) - st.balance_end_real) < 0.0001):
159 raise osv.except_osv(_('Error !'),
160 _('The statement balance is incorrect !\n') +
161 _('The expected balance (%.2f) is different than the computed one. (%.2f)') % (st.balance_end_real, st.balance_end))
162 if (not st.journal_id.default_credit_account_id) \
163 or (not st.journal_id.default_debit_account_id):
164 raise osv.except_osv(_('Configuration Error !'),
165 _('Please verify that an account is defined in the journal.'))
167 for line in st.move_line_ids:
168 if line.state <> 'valid':
169 raise osv.except_osv(_('Error !'),
170 _('The account entries lines are not in valid state.'))
171 # for bank.statement.lines
172 # In line we get reconcile_id on bank.ste.rec.
173 # in bank stat.rec we get line_new_ids on bank.stat.rec.line
174 for move in st.line_ids:
175 context.update({'date':move.date})
176 move_id = account_move_obj.create(cr, uid, {
177 'journal_id': st.journal_id.id,
178 'period_id': st.period_id.id,
181 account_bank_statement_line_obj.write(cr, uid, [move.id], {
182 'move_ids': [(4,move_id, False)]
189 account_id = st.journal_id.default_credit_account_id.id
191 account_id = st.journal_id.default_debit_account_id.id
192 acc_cur = ((move.amount<=0) and st.journal_id.default_debit_account_id) or move.account_id
193 amount = res_currency_obj.compute(cr, uid, st.currency.id,
194 company_currency_id, move.amount, context=context,
196 if move.reconcile_id and move.reconcile_id.line_new_ids:
197 for newline in move.reconcile_id.line_new_ids:
198 amount += newline.amount
205 'partner_id': ((move.partner_id) and move.partner_id.id) or False,
206 'account_id': (move.account_id) and move.account_id.id,
207 'credit': ((amount>0) and amount) or 0.0,
208 'debit': ((amount<0) and -amount) or 0.0,
209 'statement_id': st.id,
210 'journal_id': st.journal_id.id,
211 'period_id': st.period_id.id,
212 'currency_id': st.currency.id,
215 amount = res_currency_obj.compute(cr, uid, st.currency.id,
216 company_currency_id, move.amount, context=context,
218 if st.currency.id <> company_currency_id:
219 amount_cur = res_currency_obj.compute(cr, uid, company_currency_id,
220 st.currency.id, amount, context=context,
222 val['amount_currency'] = -amount_cur
224 if move.account_id and move.account_id.currency_id and move.account_id.currency_id.id <> company_currency_id:
225 val['currency_id'] = move.account_id.currency_id.id
226 if company_currency_id==move.account_id.currency_id.id:
227 amount_cur = move.amount
229 amount_cur = res_currency_obj.compute(cr, uid, company_currency_id,
230 move.account_id.currency_id.id, amount, context=context,
232 val['amount_currency'] = amount_cur
234 torec.append(account_move_line_obj.create(cr, uid, val , context=context))
236 if move.reconcile_id and move.reconcile_id.line_new_ids:
237 for newline in move.reconcile_id.line_new_ids:
238 account_move_line_obj.create(cr, uid, {
239 'name': newline.name or move.name,
243 'partner_id': ((move.partner_id) and move.partner_id.id) or False,
244 'account_id': (newline.account_id) and newline.account_id.id,
245 'debit': newline.amount>0 and newline.amount or 0.0,
246 'credit': newline.amount<0 and -newline.amount or 0.0,
247 'statement_id': st.id,
248 'journal_id': st.journal_id.id,
249 'period_id': st.period_id.id,
250 'analytic_account_id':newline.analytic_id and newline.analytic_id.id or False,
254 # Fill the secondary amount/currency
255 # if currency is not the same than the company
256 amount_currency = False
258 if st.currency.id <> company_currency_id:
259 amount_currency = move.amount
260 currency_id = st.currency.id
261 account_move_line_obj.create(cr, uid, {
266 'partner_id': ((move.partner_id) and move.partner_id.id) or False,
267 'account_id': account_id,
268 'credit': ((amount < 0) and -amount) or 0.0,
269 'debit': ((amount > 0) and amount) or 0.0,
270 'statement_id': st.id,
271 'journal_id': st.journal_id.id,
272 'period_id': st.period_id.id,
273 'amount_currency': amount_currency,
274 'currency_id': currency_id,
277 for line in account_move_line_obj.browse(cr, uid, [x.id for x in
278 account_move_obj.browse(cr, uid, move_id,
279 context=context).line_id],
281 if line.state <> 'valid':
282 raise osv.except_osv(_('Error !'),
283 _('Ledger Posting line "%s" is not valid') % line.name)
285 if move.reconcile_id and move.reconcile_id.line_ids:
286 torec += map(lambda x: x.id, move.reconcile_id.line_ids)
288 if abs(move.reconcile_amount-move.amount)<0.0001:
290 writeoff_acc_id = False
291 #There should only be one write-off account!
292 for entry in move.reconcile_id.line_new_ids:
293 writeoff_acc_id = entry.account_id.id
296 account_move_line_obj.reconcile(cr, uid, torec, 'statement', writeoff_acc_id=writeoff_acc_id, writeoff_period_id=st.period_id.id, writeoff_journal_id=st.journal_id.id, context=context)
298 account_move_line_obj.reconcile_partial(cr, uid, torec, 'statement', context)
300 # raise osv.except_osv(_('Error !'), _('Unable to reconcile entry "%s": %.2f') % (move.name, move.amount))
302 if st.journal_id.entry_posted:
303 account_move_obj.write(cr, uid, [move_id], {'state':'posted'})
305 self.write(cr, uid, done, {'state':'confirm'}, context=context)
308 def button_cancel(self, cr, uid, ids, context={}):
310 for st in self.browse(cr, uid, ids, context):
311 if st.state=='draft':
314 for line in st.line_ids:
315 ids += [x.id for x in line.move_ids]
316 self.pool.get('account.move').unlink(cr, uid, ids, context)
318 self.write(cr, uid, done, {'state':'draft'}, context=context)
321 def onchange_journal_id(self, cursor, user, statement_id, journal_id, context=None):
323 return {'value': {'currency': False}}
325 account_journal_obj = self.pool.get('account.journal')
326 res_users_obj = self.pool.get('res.users')
327 res_currency_obj = self.pool.get('res.currency')
329 cursor.execute('SELECT balance_end_real \
330 FROM account_bank_statement \
331 WHERE journal_id = %s \
332 ORDER BY date DESC,id DESC LIMIT 1', (journal_id,))
333 res = cursor.fetchone()
334 balance_start = res and res[0] or 0.0
336 currency_id = account_journal_obj.browse(cursor, user, journal_id,
337 context=context).currency.id
339 currency_id = res_users_obj.browse(cursor, user, user,
340 context=context).company_id.currency_id.id
341 currency = res_currency_obj.name_get(cursor, user, [currency_id],
343 return {'value': {'balance_start': balance_start, 'currency': currency}}
345 def unlink(self, cr, uid, ids, context=None):
346 stat = self.read(cr, uid, ids, ['state'])
349 if t['state'] in ('draft'):
350 unlink_ids.append(t['id'])
352 raise osv.except_osv(_('Invalid action !'), _('Cannot delete bank statement which are already confirmed !'))
353 osv.osv.unlink(self, cr, uid, unlink_ids, context=context)
356 account_bank_statement()
359 class account_bank_statement_reconcile(osv.osv):
360 _name = "account.bank.statement.reconcile"
361 _description = "Statement reconcile"
363 def _total_entry(self, cursor, user, ids, name, attr, context=None):
365 for o in self.browse(cursor, user, ids, context=context):
367 for line in o.line_ids:
368 result[o.id] += line.debit - line.credit
371 def _total_new(self, cursor, user, ids, name, attr, context=None):
373 for o in self.browse(cursor, user, ids, context=context):
375 for line in o.line_new_ids:
376 result[o.id] += line.amount
379 def _total_balance(self, cursor, user, ids, name, attr, context=None):
381 for o in self.browse(cursor, user, ids, context=context):
382 result[o.id] = o.total_new - o.total_entry + o.total_amount
385 def _total_amount(self, cursor, user, ids, name, attr, context=None):
387 res_currency_obj = self.pool.get('res.currency')
388 res_users_obj = self.pool.get('res.users')
390 company_currency_id = res_users_obj.browse(cursor, user, user,
391 context=context).company_id.currency_id.id
392 currency_id = context.get('currency_id', company_currency_id)
395 if context.get('journal_id', False) and context.get('account_id',False):
396 st =self.pool.get('account.journal').browse(cursor, user, context['journal_id'])
397 acc = self.pool.get('account.account').browse(cursor, user, context['account_id'])
398 acc_cur = (( context.get('amount',0.0)<=0) and st.default_debit_account_id) or acc
400 for reconcile_id in ids:
401 res[reconcile_id] = res_currency_obj.compute(cursor, user,
402 currency_id, company_currency_id,
403 context.get('amount', 0.0), context=context, account=acc_cur)
406 def _default_amount(self, cursor, user, context=None):
409 res_currency_obj = self.pool.get('res.currency')
410 res_users_obj = self.pool.get('res.users')
412 company_currency_id = res_users_obj.browse(cursor, user, user,
413 context=context).company_id.currency_id.id
414 currency_id = context.get('currency_id', company_currency_id)
417 if context.get('journal_id', False) and context.get('account_id',False):
418 st =self.pool.get('account.journal').browse(cursor, user, context['journal_id'])
419 acc = self.pool.get('account.account').browse(cursor, user, context['account_id'])
420 acc_cur = (( context.get('amount',0.0)<=0) and st.default_debit_account_id) or acc
422 return res_currency_obj.compute(cursor, user,
423 currency_id, company_currency_id,
424 context.get('amount', 0.0), context=context, account=acc_cur)
426 def _total_currency(self, cursor, user, ids, name, attrs, context=None):
428 res_users_obj = self.pool.get('res.users')
430 company_currency_id = res_users_obj.browse(cursor, user, user,
431 context=context).company_id.currency_id.id
433 for reconcile_id in ids:
434 res[reconcile_id] = company_currency_id
437 def _default_currency(self, cursor, user, context=None):
438 res_users_obj = self.pool.get('res.users')
440 return res_users_obj.browse(cursor, user, user,
441 context=context).company_id.currency_id.id
444 def _total_second_amount(self, cursor, user, ids, name, attr,
447 for reconcile_id in ids:
448 res[reconcile_id] = context.get('amount', 0.0)
451 def _total_second_currency(self, cursor, user, ids, name, attr, context=None):
453 for reconcile_id in ids:
454 res[reconcile_id] = context.get('currency_id', False)
457 def name_get(self, cursor, user, ids, context=None):
459 for o in self.browse(cursor, user, ids, context=context):
462 for line in o.line_ids:
463 if line.amount_currency and line.currency_id:
464 result += line.amount_currency
465 res_currency = line.currency_id.code
467 result += line.debit - line.credit
469 res_currency = ' ' + res_currency
470 res.append((o.id, '[%.2f'% (result - o.total_new,) + res_currency + ']' ))
474 'name': fields.char('Date', size=64, required=True),
475 'partner_id': fields.many2one('res.partner', 'Partner', readonly=True),
476 'line_new_ids': fields.one2many('account.bank.statement.reconcile.line',
477 'line_id', 'Write-Off'),
478 'total_entry': fields.function(_total_entry, method=True,
479 string='Total entries'),
480 'total_new': fields.function(_total_new, method=True,
481 string='Total write-off'),
482 'total_second_amount': fields.function(_total_second_amount,
483 method=True, string='Payment amount',
484 help='The amount in the currency of the journal'),
485 'total_second_currency': fields.function(_total_second_currency, method=True,
486 string='Currency', type='many2one', relation='res.currency',
487 help='The currency of the journal'),
488 'total_amount': fields.function(_total_amount, method=True,
489 string='Payment amount'),
490 'total_currency': fields.function(_total_currency, method=True,
491 string='Currency', type='many2one', relation='res.currency'),
492 'total_balance': fields.function(_total_balance, method=True,
494 #line_ids define in account.py
495 'statement_line': fields.one2many('account.bank.statement.line',
496 'reconcile_id', 'Bank Statement Line'),
499 'name': lambda *a: time.strftime('%Y-%m-%d'),
500 'partner_id': lambda obj, cursor, user, context=None: \
501 context.get('partner', False),
502 'total_amount': _default_amount,
503 'total_currency': _default_currency,
504 'total_second_amount': lambda obj, cursor, user, context=None: \
505 context.get('amount', 0.0),
506 'total_second_currency': lambda obj, cursor, user, context=None: \
507 context.get('currency_id', False),
508 'total_balance': _default_amount,
510 account_bank_statement_reconcile()
512 class account_bank_statement_reconcile_line(osv.osv):
513 _name = "account.bank.statement.reconcile.line"
514 _description = "Statement reconcile line"
516 'name': fields.char('Description', size=64, required=True),
517 'account_id': fields.many2one('account.account', 'Account', required=True),
518 'line_id': fields.many2one('account.bank.statement.reconcile', 'Reconcile'),
519 'amount': fields.float('Amount', required=True),
520 'analytic_id': fields.many2one('account.analytic.account',"Analytic Account")
523 'name': lambda *a: 'Write-Off',
525 account_bank_statement_reconcile_line()
528 class account_bank_statement_line(osv.osv):
530 def onchange_partner_id(self, cursor, user, line_id, partner_id, type, currency_id,
534 res_currency_obj = self.pool.get('res.currency')
535 res_users_obj = self.pool.get('res.users')
537 company_currency_id = res_users_obj.browse(cursor, user, user,
538 context=context).company_id.currency_id.id
541 currency_id = company_currency_id
543 part = self.pool.get('res.partner').browse(cursor, user, partner_id,
545 if type == 'supplier':
546 account_id = part.property_account_payable.id
548 account_id = part.property_account_receivable.id
550 cursor.execute('SELECT sum(debit-credit) \
551 FROM account_move_line \
552 WHERE (reconcile_id is null) \
553 AND partner_id = %s \
554 AND account_id=%s', (partner_id, account_id))
555 res = cursor.fetchone()
556 balance = res and res[0] or 0.0
558 balance = res_currency_obj.compute(cursor, user, company_currency_id,
559 currency_id, balance, context=context)
560 return {'value': {'amount': balance, 'account_id': account_id}}
562 def _reconcile_amount(self, cursor, user, ids, name, args, context=None):
565 res_currency_obj = self.pool.get('res.currency')
566 res_users_obj = self.pool.get('res.users')
569 company_currency_id = res_users_obj.browse(cursor, user, user,
570 context=context).company_id.currency_id.id
572 for line in self.browse(cursor, user, ids, context=context):
573 if line.reconcile_id:
574 res[line.id] = res_currency_obj.compute(cursor, user,
575 company_currency_id, line.statement_id.currency.id,
576 line.reconcile_id.total_entry, context=context)
581 _order = "date,name desc"
582 _name = "account.bank.statement.line"
583 _description = "Bank Statement Line"
585 'name': fields.char('Name', size=64, required=True),
586 'date': fields.date('Date', required=True),
587 'amount': fields.float('Amount'),
588 'type': fields.selection([
589 ('supplier','Supplier'),
590 ('customer','Customer'),
591 ('general','General')
592 ], 'Type', required=True),
593 'partner_id': fields.many2one('res.partner', 'Partner'),
594 'account_id': fields.many2one('account.account','Account',
596 'statement_id': fields.many2one('account.bank.statement', 'Statement',
597 select=True, required=True, ondelete='cascade'),
598 'reconcile_id': fields.many2one('account.bank.statement.reconcile',
599 'Reconcile', states={'confirm':[('readonly',True)]}),
600 'move_ids': fields.many2many('account.move',
601 'account_bank_statement_line_move_rel', 'move_id','statement_id',
603 'ref': fields.char('Reference', size=32),
604 'note': fields.text('Notes'),
605 'reconcile_amount': fields.function(_reconcile_amount,
606 string='Amount reconciled', method=True, type='float'),
607 'sequence': fields.integer('Sequence', help="Gives the sequence order when displaying a list of bank statement line."),
610 'name': lambda self,cr,uid,context={}: self.pool.get('ir.sequence').get(cr, uid, 'account.bank.statement.line'),
611 'date': lambda *a: time.strftime('%Y-%m-%d'),
612 'type': lambda *a: 'general',
613 'sequence': lambda *a: 10,
616 account_bank_statement_line()
620 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: