1 # -*- encoding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 ##############################################################################
24 from osv import fields, osv
26 from tools.misc import currency
27 from tools.translate import _
30 from mx.DateTime import RelativeDateTime, now, DateTime, localtime
33 class account_bank_statement(osv.osv):
34 def _default_journal_id(self, cr, uid, context={}):
35 if context.get('journal_id', False):
36 return context['journal_id']
39 def _default_balance_start(self, cr, uid, context={}):
40 cr.execute('select id from account_bank_statement where journal_id=%s order by date desc limit 1', (1,))
43 return self.browse(cr, uid, [res[0]], context)[0].balance_end
46 def _end_balance(self, cursor, user, ids, name, attr, context=None):
47 res_currency_obj = self.pool.get('res.currency')
48 res_users_obj = self.pool.get('res.users')
52 company_currency_id = res_users_obj.browse(cursor, user, user,
53 context=context).company_id.currency_id.id
55 statements = self.browse(cursor, user, ids, context=context)
56 for statement in statements:
57 res[statement.id] = statement.balance_start
58 currency_id = statement.currency.id
59 for line in statement.move_line_ids:
60 if currency_id == company_currency_id:
62 if line.account_id.id == \
63 statement.journal_id.default_debit_account_id.id:
64 res[statement.id] += line.debit
66 if line.account_id.id == \
67 statement.journal_id.default_credit_account_id.id:
68 res[statement.id] -= line.credit
70 if line.amount_currency > 0:
71 res[statement.id] += line.amount_currency
73 if statement.state == 'draft':
74 for line in statement.line_ids:
75 res[statement.id] += line.amount
77 res[r] = round(res[r], 2)
80 def _get_period(self, cr, uid, context={}):
81 periods = self.pool.get('account.period').find(cr, uid)
87 def _currency(self, cursor, user, ids, name, args, context=None):
89 res_currency_obj = self.pool.get('res.currency')
90 res_users_obj = self.pool.get('res.users')
91 default_currency = res_users_obj.browse(cursor, user,
92 user, context=context).company_id.currency_id
93 for statement in self.browse(cursor, user, ids, context=context):
94 currency = statement.journal_id.currency
96 currency = default_currency
97 res[statement.id] = currency.id
99 for currency_id, currency_name in res_currency_obj.name_get(cursor,
100 user, [x for x in res.values()], context=context):
101 currency_names[currency_id] = currency_name
102 for statement_id in res.keys():
103 currency_id = res[statement_id]
104 res[statement_id] = (currency_id, currency_names[currency_id])
108 _name = "account.bank.statement"
109 _description = "Bank Statement"
111 'name': fields.char('Name', size=64, required=True, states={'confirm': [('readonly', True)]}),
112 'date': fields.date('Date', required=True,
113 states={'confirm': [('readonly', True)]}),
114 'journal_id': fields.many2one('account.journal', 'Journal', required=True,
115 states={'confirm': [('readonly', True)]}, domain=[('type', '=', 'cash')]),
116 'period_id': fields.many2one('account.period', 'Period', required=True,
117 states={'confirm':[('readonly', True)]}),
118 'balance_start': fields.float('Starting Balance', digits=(16,2),
119 states={'confirm':[('readonly',True)]}),
120 'balance_end_real': fields.float('Ending Balance', digits=(16,2),
121 states={'confirm':[('readonly', True)]}),
122 'balance_end': fields.function(_end_balance, method=True, string='Balance'),
123 'line_ids': fields.one2many('account.bank.statement.line',
124 'statement_id', 'Statement lines',
125 states={'confirm':[('readonly', True)]}),
126 'move_line_ids': fields.one2many('account.move.line', 'statement_id',
127 'Entry lines', states={'confirm':[('readonly',True)]}),
128 'state': fields.selection([('draft', 'Draft'),('confirm', 'Confirm')],
129 'State', required=True,
130 states={'confirm': [('readonly', True)]}, readonly="1"),
131 'currency': fields.function(_currency, method=True, string='Currency',
132 type='many2one', relation='res.currency'),
136 'name': lambda self, cr, uid, context=None: \
137 self.pool.get('ir.sequence').get(cr, uid, 'account.bank.statement'),
138 'date': lambda *a: time.strftime('%Y-%m-%d'),
139 'state': lambda *a: 'draft',
140 'balance_start': _default_balance_start,
141 'journal_id': _default_journal_id,
142 'period_id': _get_period,
145 def button_confirm(self, cr, uid, ids, context={}):
147 res_currency_obj = self.pool.get('res.currency')
148 res_users_obj = self.pool.get('res.users')
149 account_move_obj = self.pool.get('account.move')
150 account_move_line_obj = self.pool.get('account.move.line')
151 account_bank_statement_line_obj = \
152 self.pool.get('account.bank.statement.line')
154 company_currency_id = res_users_obj.browse(cr, uid, uid,
155 context=context).company_id.currency_id.id
157 for st in self.browse(cr, uid, ids, context):
158 if not st.state=='draft':
161 if not (abs(st.balance_end - st.balance_end_real) < 0.0001):
162 raise osv.except_osv(_('Error !'),
163 _('The statement balance is incorrect !\n') +
164 _('The expected balance (%.2f) is different than the computed one. (%.2f)') % (st.balance_end_real, st.balance_end))
165 if (not st.journal_id.default_credit_account_id) \
166 or (not st.journal_id.default_debit_account_id):
167 raise osv.except_osv(_('Configuration Error !'),
168 _('Please verify that an account is defined in the journal.'))
170 for line in st.move_line_ids:
171 if line.state <> 'valid':
172 raise osv.except_osv(_('Error !'),
173 _('The account entries lines are not in valid state.'))
174 # for bank.statement.lines
175 # In line we get reconcile_id on bank.ste.rec.
176 # in bank stat.rec we get line_new_ids on bank.stat.rec.line
177 for move in st.line_ids:
178 context.update({'date':move.date})
179 move_id = account_move_obj.create(cr, uid, {
180 'journal_id': st.journal_id.id,
181 'period_id': st.period_id.id,
184 account_bank_statement_line_obj.write(cr, uid, [move.id], {
185 'move_ids': [(4,move_id, False)]
192 account_id = st.journal_id.default_credit_account_id.id
194 account_id = st.journal_id.default_debit_account_id.id
195 acc_cur = ((move.amount<=0) and st.journal_id.default_debit_account_id) or move.account_id
196 amount = res_currency_obj.compute(cr, uid, st.currency.id,
197 company_currency_id, move.amount, context=context,
199 if move.reconcile_id and move.reconcile_id.line_new_ids:
200 for newline in move.reconcile_id.line_new_ids:
201 amount += newline.amount
208 'partner_id': ((move.partner_id) and move.partner_id.id) or False,
209 'account_id': (move.account_id) and move.account_id.id,
210 'credit': ((amount>0) and amount) or 0.0,
211 'debit': ((amount<0) and -amount) or 0.0,
212 'statement_id': st.id,
213 'journal_id': st.journal_id.id,
214 'period_id': st.period_id.id,
215 'currency_id': st.currency.id,
218 amount = res_currency_obj.compute(cr, uid, st.currency.id,
219 company_currency_id, move.amount, context=context,
221 if st.currency.id <> company_currency_id:
222 amount_cur = res_currency_obj.compute(cr, uid, company_currency_id,
223 st.currency.id, amount, context=context,
225 val['amount_currency'] = -amount_cur
227 if move.account_id and move.account_id.currency_id and move.account_id.currency_id.id <> company_currency_id:
228 val['currency_id'] = move.account_id.currency_id.id
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,
253 # Fill the secondary amount/currency
254 # if currency is not the same than the company
255 amount_currency = False
257 if st.currency.id <> company_currency_id:
258 amount_currency = move.amount
259 currency_id = st.currency.id
260 account_move_line_obj.create(cr, uid, {
265 'partner_id': ((move.partner_id) and move.partner_id.id) or False,
266 'account_id': account_id,
267 'credit': ((amount < 0) and -amount) or 0.0,
268 'debit': ((amount > 0) and amount) or 0.0,
269 'statement_id': st.id,
270 'journal_id': st.journal_id.id,
271 'period_id': st.period_id.id,
272 'amount_currency': amount_currency,
273 'currency_id': currency_id,
276 for line in account_move_line_obj.browse(cr, uid, [x.id for x in
277 account_move_obj.browse(cr, uid, move_id,
278 context=context).line_id],
280 if line.state <> 'valid':
281 raise osv.except_osv(_('Error !'),
282 _('Account move line "%s" is not valid') % line.name)
284 if move.reconcile_id and move.reconcile_id.line_ids:
285 ## Search if move has already a partial reconciliation
286 previous_partial = False
287 for line_reconcile_move in move.reconcile_id.line_ids:
288 if line_reconcile_move.reconcile_partial_id:
289 previous_partial = True
292 torec += map(lambda x: x.id, move.reconcile_id.line_ids)
294 if abs(move.reconcile_amount-move.amount)<0.0001:
296 writeoff_acc_id = False
297 #There should only be one write-off account!
298 for entry in move.reconcile_id.line_new_ids:
299 writeoff_acc_id = entry.account_id.id
301 ## If we have already a partial reconciliation
302 ## We need to make a partial reconciliation
303 ## To add this amount to previous paid amount
305 account_move_line_obj.reconcile_partial(cr, uid, torec, 'statement', context)
306 ## If it's the first reconciliation, we do a full reconciliation as regular
308 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)
311 account_move_line_obj.reconcile_partial(cr, uid, torec, 'statement', context)
313 # raise osv.except_osv(_('Error !'), _('Unable to reconcile entry "%s": %.2f') % (move.name, move.amount))
315 if st.journal_id.entry_posted:
316 account_move_obj.write(cr, uid, [move_id], {'state':'posted'})
318 self.write(cr, uid, done, {'state':'confirm'}, context=context)
321 def button_cancel(self, cr, uid, ids, context={}):
323 for st in self.browse(cr, uid, ids, context):
324 if st.state=='draft':
327 for line in st.line_ids:
328 ids += [x.id for x in line.move_ids]
329 self.pool.get('account.move').unlink(cr, uid, ids, context)
331 self.write(cr, uid, done, {'state':'draft'}, context=context)
334 def onchange_journal_id(self, cursor, user, statement_id, journal_id, context=None):
336 return {'value': {'currency': False}}
338 account_journal_obj = self.pool.get('account.journal')
339 res_users_obj = self.pool.get('res.users')
340 res_currency_obj = self.pool.get('res.currency')
342 cursor.execute('SELECT balance_end_real \
343 FROM account_bank_statement \
344 WHERE journal_id = %s \
345 ORDER BY date DESC,id DESC LIMIT 1', (journal_id,))
346 res = cursor.fetchone()
347 balance_start = res and res[0] or 0.0
349 currency_id = account_journal_obj.browse(cursor, user, journal_id,
350 context=context).currency.id
352 currency_id = res_users_obj.browse(cursor, user, user,
353 context=context).company_id.currency_id.id
354 currency = res_currency_obj.name_get(cursor, user, [currency_id],
356 return {'value': {'balance_start': balance_start, 'currency': currency}}
358 def unlink(self, cr, uid, ids, context=None):
359 stat = self.read(cr, uid, ids, ['state'])
362 if t['state'] in ('draft'):
363 unlink_ids.append(t['id'])
365 raise osv.except_osv(_('Invalid action !'), _('Cannot delete bank statement which are already confirmed !'))
366 osv.osv.unlink(self, cr, uid, unlink_ids, context=context)
369 def copy(self, cr, uid, id, default=None, context=None):
374 default = default.copy()
375 default['move_line_ids'] = []
376 return super(account_bank_statement, self).copy(cr, uid, id, default, context)
378 account_bank_statement()
381 class account_bank_statement_reconcile(osv.osv):
382 _name = "account.bank.statement.reconcile"
383 _description = "Statement reconcile"
385 def _total_entry(self, cursor, user, ids, name, attr, context=None):
387 for o in self.browse(cursor, user, ids, context=context):
389 for line in o.line_ids:
390 result[o.id] += line.debit - line.credit
393 def _total_new(self, cursor, user, ids, name, attr, context=None):
395 for o in self.browse(cursor, user, ids, context=context):
397 for line in o.line_new_ids:
398 result[o.id] += line.amount
401 def _total_balance(self, cursor, user, ids, name, attr, context=None):
403 for o in self.browse(cursor, user, ids, context=context):
404 result[o.id] = o.total_new - o.total_entry + o.total_amount
407 def _total_amount(self, cursor, user, ids, name, attr, 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 for reconcile_id in ids:
423 res[reconcile_id] = res_currency_obj.compute(cursor, user,
424 currency_id, company_currency_id,
425 context.get('amount', 0.0), context=context, account=acc_cur)
428 def _default_amount(self, cursor, user, context=None):
431 res_currency_obj = self.pool.get('res.currency')
432 res_users_obj = self.pool.get('res.users')
434 company_currency_id = res_users_obj.browse(cursor, user, user,
435 context=context).company_id.currency_id.id
436 currency_id = context.get('currency_id', company_currency_id)
439 if context.get('journal_id', False) and context.get('account_id',False):
440 st =self.pool.get('account.journal').browse(cursor, user, context['journal_id'])
441 acc = self.pool.get('account.account').browse(cursor, user, context['account_id'])
442 acc_cur = (( context.get('amount',0.0)<=0) and st.default_debit_account_id) or acc
444 return res_currency_obj.compute(cursor, user,
445 currency_id, company_currency_id,
446 context.get('amount', 0.0), context=context, account=acc_cur)
448 def _total_currency(self, cursor, user, ids, name, attrs, context=None):
450 res_users_obj = self.pool.get('res.users')
452 company_currency_id = res_users_obj.browse(cursor, user, user,
453 context=context).company_id.currency_id.id
455 for reconcile_id in ids:
456 res[reconcile_id] = company_currency_id
459 def _default_currency(self, cursor, user, context=None):
460 res_users_obj = self.pool.get('res.users')
462 return res_users_obj.browse(cursor, user, user,
463 context=context).company_id.currency_id.id
466 def _total_second_amount(self, cursor, user, ids, name, attr,
469 for reconcile_id in ids:
470 res[reconcile_id] = context.get('amount', 0.0)
473 def _total_second_currency(self, cursor, user, ids, name, attr, context=None):
475 for reconcile_id in ids:
476 res[reconcile_id] = context.get('currency_id', False)
479 def name_get(self, cursor, user, ids, context=None):
481 for o in self.browse(cursor, user, ids, context=context):
484 for line in o.line_ids:
485 if line.amount_currency and line.currency_id:
486 result += line.amount_currency
487 res_currency = line.currency_id.code
489 result += line.debit - line.credit
491 res_currency = ' ' + res_currency
492 res.append((o.id, '[%.2f'% (result - o.total_new,) + res_currency + ']' ))
496 'name': fields.char('Date', size=64, required=True),
497 'partner_id': fields.many2one('res.partner', 'Partner', readonly=True),
498 'line_new_ids': fields.one2many('account.bank.statement.reconcile.line',
499 'line_id', 'Write-Off'),
500 'total_entry': fields.function(_total_entry, method=True,
501 string='Total entries'),
502 'total_new': fields.function(_total_new, method=True,
503 string='Total write-off'),
504 'total_second_amount': fields.function(_total_second_amount,
505 method=True, string='Payment amount',
506 help='The amount in the currency of the journal'),
507 'total_second_currency': fields.function(_total_second_currency, method=True,
508 string='Currency', type='many2one', relation='res.currency',
509 help='The currency of the journal'),
510 'total_amount': fields.function(_total_amount, method=True,
511 string='Payment amount'),
512 'total_currency': fields.function(_total_currency, method=True,
513 string='Currency', type='many2one', relation='res.currency'),
514 'total_balance': fields.function(_total_balance, method=True,
516 #line_ids define in account.py
517 'statement_line': fields.one2many('account.bank.statement.line',
518 'reconcile_id', 'Bank Statement Line'),
521 'name': lambda *a: time.strftime('%Y-%m-%d'),
522 'partner_id': lambda obj, cursor, user, context=None: \
523 context.get('partner', False),
524 'total_amount': _default_amount,
525 'total_currency': _default_currency,
526 'total_second_amount': lambda obj, cursor, user, context=None: \
527 context.get('amount', 0.0),
528 'total_second_currency': lambda obj, cursor, user, context=None: \
529 context.get('currency_id', False),
530 'total_balance': _default_amount,
532 account_bank_statement_reconcile()
534 class account_bank_statement_reconcile_line(osv.osv):
535 _name = "account.bank.statement.reconcile.line"
536 _description = "Statement reconcile line"
538 'name': fields.char('Description', size=64, required=True),
539 'account_id': fields.many2one('account.account', 'Account', required=True),
540 'line_id': fields.many2one('account.bank.statement.reconcile', 'Reconcile'),
541 'amount': fields.float('Amount', required=True),
544 'name': lambda *a: 'Write-Off',
546 account_bank_statement_reconcile_line()
549 class account_bank_statement_line(osv.osv):
551 def onchange_partner_id(self, cursor, user, line_id, partner_id, type, currency_id,
556 line = self.browse(cursor, user, line_id)
558 if not line or (line and not line[0].account_id):
559 part = self.pool.get('res.partner').browse(cursor, user, partner_id,
561 if type == 'supplier':
562 account_id = part.property_account_payable.id
564 account_id = part.property_account_receivable.id
565 res['value']['account_id'] = account_id
567 if not line or (line and not line[0].amount):
568 res_users_obj = self.pool.get('res.users')
569 res_currency_obj = self.pool.get('res.currency')
570 company_currency_id = res_users_obj.browse(cursor, user, user,
571 context=context).company_id.currency_id.id
573 currency_id = company_currency_id
575 cursor.execute('SELECT sum(debit-credit) \
576 FROM account_move_line \
577 WHERE (reconcile_id is null) \
578 AND partner_id = %s \
579 AND account_id=%s', (partner_id, account_id))
580 pgres = cursor.fetchone()
581 balance = pgres and pgres[0] or 0.0
583 balance = res_currency_obj.compute(cursor, user, company_currency_id,
584 currency_id, balance, context=context)
585 res['value']['amount'] = balance
588 def _reconcile_amount(self, cursor, user, ids, name, args, context=None):
591 res_currency_obj = self.pool.get('res.currency')
592 res_users_obj = self.pool.get('res.users')
595 company_currency_id = res_users_obj.browse(cursor, user, user,
596 context=context).company_id.currency_id.id
598 for line in self.browse(cursor, user, ids, context=context):
599 if line.reconcile_id:
600 res[line.id] = res_currency_obj.compute(cursor, user,
601 company_currency_id, line.statement_id.currency.id,
602 line.reconcile_id.total_entry, context=context)
607 _order = "date,name desc"
608 _name = "account.bank.statement.line"
609 _description = "Bank Statement Line"
611 'name': fields.char('Name', size=64, required=True),
612 'date': fields.date('Date', required=True),
613 'amount': fields.float('Amount'),
614 'type': fields.selection([
615 ('supplier','Supplier'),
616 ('customer','Customer'),
617 ('general','General')
618 ], 'Type', required=True),
619 'partner_id': fields.many2one('res.partner', 'Partner'),
620 'account_id': fields.many2one('account.account','Account',
622 'statement_id': fields.many2one('account.bank.statement', 'Statement',
623 select=True, required=True, ondelete='cascade'),
624 'reconcile_id': fields.many2one('account.bank.statement.reconcile',
625 'Reconcile', states={'confirm':[('readonly',True)]}),
626 'move_ids': fields.many2many('account.move',
627 'account_bank_statement_line_move_rel', 'move_id','statement_id',
629 'ref': fields.char('Ref.', size=32),
630 'note': fields.text('Notes'),
631 'reconcile_amount': fields.function(_reconcile_amount,
632 string='Amount reconciled', method=True, type='float'),
635 'name': lambda self,cr,uid,context={}: self.pool.get('ir.sequence').get(cr, uid, 'account.bank.statement.line'),
636 'date': lambda *a: time.strftime('%Y-%m-%d'),
637 'type': lambda *a: 'general',
640 account_bank_statement_line()
644 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: