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 ##############################################################################
23 from lxml import etree
26 from osv import fields
28 from tools.translate import _
30 class account_move_line(osv.osv):
31 _inherit = 'account.move.line'
32 def _unreconciled(self, cr, uid, ids, prop, unknow_none, context):
34 for line in self.browse(cr, uid, ids, context=context):
35 res[line.id] = line.debit - line.credit
36 if line.reconcile_partial_id:
38 for partial in line.reconcile_partial_id.line_partial_ids:
39 res[line.id] += partial.debit - partial.credit
40 res[line.id] = abs(res[line.id])
44 'amount_unreconciled': fields.function(_unreconciled, method=True, string='Unreconciled Amount'),
48 class account_voucher(osv.osv):
49 def _get_type(self, cr, uid, ids, context={}):
50 return context.get('type', False)
52 def _get_period(self, cr, uid, context={}):
53 if context.get('period_id', False):
54 return context.get('period_id')
55 periods = self.pool.get('account.period').find(cr, uid)
56 return periods and periods[0] or False
58 def _get_journal(self, cr, uid, context={}):
59 journal_pool = self.pool.get('account.journal')
60 if context.get('journal_id', False):
61 return context.get('journal_id')
62 if not context.get('journal_id', False) and context.get('search_default_journal_id', False):
63 return context.get('search_default_journal_id')
65 ttype = context.get('type', 'bank')
66 if ttype in ('payment', 'receipt'):
68 res = journal_pool.search(cr, uid, [('type', '=', ttype)], limit=1)
69 return res and res[0] or False
71 def _get_tax(self, cr, uid, context={}):
72 journal_pool = self.pool.get('account.journal')
73 journal_id = context.get('journal_id', False)
75 ttype = context.get('type', 'bank')
76 res = journal_pool.search(cr, uid, [('type', '=', ttype)], limit=1)
83 journal = journal_pool.browse(cr, uid, journal_id)
84 account_id = journal.default_credit_account_id or journal.default_debit_account_id
85 if account_id and account_id.tax_ids:
86 tax_id = account_id.tax_ids[0].id
90 def _get_currency(self, cr, uid, context):
91 journal_pool = self.pool.get('account.journal')
92 journal_id = context.get('journal_id', False)
94 journal = journal_pool.browse(cr, uid, journal_id)
95 currency_id = journal.company_id.currency_id.id
97 currency_id = journal.currency.id
100 def _get_partner(self, cr, uid, context={}):
101 return context.get('partner_id', False)
103 def _get_reference(self, cr, uid, context={}):
104 return context.get('reference', False)
106 def _get_narration(self, cr, uid, context={}):
107 return context.get('narration', False)
109 def name_get(self, cr, uid, ids, context=None):
112 return [(r['id'], (str(r['amount']) or '')) for r in self.read(cr, uid, ids, ['amount'], context, load='_classic_write')]
114 def fields_view_get(self, cr, uid, view_id=None, view_type=False, context=None, toolbar=False, submenu=False):
115 res = super(account_voucher,self).fields_view_get(cr, uid, view_id=view_id, view_type=view_type, context=context, toolbar=toolbar, submenu=submenu)
116 doc = etree.XML(res['arch'])
117 nodes = doc.xpath("//field[@name='partner_id']")
118 if context.get('type', 'sale') in ('purchase', 'payment'):
120 node.set('domain', "[('supplier', '=', True)]")
121 res['arch'] = etree.tostring(doc)
124 _name = 'account.voucher'
125 _description = 'Accounting Voucher'
126 _order = "date desc, id desc"
127 # _rec_name = 'number'
129 'type':fields.selection([
131 ('purchase','Purchase'),
132 ('payment','Payment'),
133 ('receipt','Receipt'),
134 ],'Default Type', readonly=True, states={'draft':[('readonly',False)]}),
135 'name':fields.char('Memo', size=256, readonly=True, states={'draft':[('readonly',False)]}),
136 'date':fields.date('Date', readonly=True, states={'draft':[('readonly',False)]}, help="Effective date for accounting entries"),
137 'journal_id':fields.many2one('account.journal', 'Journal', required=True, readonly=True, states={'draft':[('readonly',False)]}),
138 'account_id':fields.many2one('account.account', 'Account', required=True, readonly=True, states={'draft':[('readonly',False)]}),
139 'line_ids':fields.one2many('account.voucher.line','voucher_id','Voucher Lines', readonly=True, states={'draft':[('readonly',False)]}),
140 'line_cr_ids':fields.one2many('account.voucher.line','voucher_id','Credits',
141 domain=[('type','=','cr')], context={'default_type':'cr'}, readonly=True, states={'draft':[('readonly',False)]}),
142 'line_dr_ids':fields.one2many('account.voucher.line','voucher_id','Debits',
143 domain=[('type','=','dr')], context={'default_type':'dr'}, readonly=True, states={'draft':[('readonly',False)]}),
144 'period_id': fields.many2one('account.period', 'Period', required=True, readonly=True, states={'draft':[('readonly',False)]}),
145 'narration':fields.text('Notes', readonly=True, states={'draft':[('readonly',False)]}),
146 'currency_id':fields.many2one('res.currency', 'Currency', readonly=True, states={'draft':[('readonly',False)]}),
147 # 'currency_id': fields.related('journal_id','currency', type='many2one', relation='res.currency', string='Currency', store=True, readonly=True, states={'draft':[('readonly',False)]}),
148 'company_id': fields.many2one('res.company', 'Company', required=True, readonly=True, states={'draft':[('readonly',False)]}),
149 'state':fields.selection(
151 ('proforma','Pro-forma'),
153 ('cancel','Cancelled')
154 ], 'State', readonly=True, size=32,
155 help=' * The \'Draft\' state is used when a user is encoding a new and unconfirmed Voucher. \
156 \n* The \'Pro-forma\' when voucher is in Pro-forma state,voucher does not have an voucher number. \
157 \n* The \'Posted\' state is used when user create voucher,a voucher number is generated and voucher entries are created in account \
158 \n* The \'Cancelled\' state is used when user cancel voucher.'),
159 'amount': fields.float('Total', digits=(16, 2), required=True, readonly=True, states={'draft':[('readonly',False)]}),
160 'tax_amount':fields.float('Tax Amount', digits=(14,2), readonly=True, states={'draft':[('readonly',False)]}),
161 'reference': fields.char('Ref #', size=64, readonly=True, states={'draft':[('readonly',False)]}, help="Transaction reference number."),
162 'number': fields.char('Number', size=32, readonly=True,),
163 'move_id':fields.many2one('account.move', 'Account Entry'),
164 'move_ids': fields.related('move_id','line_id', type='one2many', relation='account.move.line', string='Journal Items', readonly=True),
165 'partner_id':fields.many2one('res.partner', 'Partner', change_default=1, readonly=True, states={'draft':[('readonly',False)]}),
166 'audit': fields.related('move_id','to_check', type='boolean', relation='account.move', string='Audit Complete ?'),
167 'pay_now':fields.selection([
168 ('pay_now','Pay Directly'),
169 ('pay_later','Pay Later or Group Funds'),
170 ],'Payment', select=True, readonly=True, states={'draft':[('readonly',False)]}),
171 'tax_id':fields.many2one('account.tax', 'Tax', readonly=True, states={'draft':[('readonly',False)]}),
172 'pre_line':fields.boolean('Previous Payments ?', required=False),
173 'date_due': fields.date('Due Date', readonly=True, states={'draft':[('readonly',False)]}),
176 'period_id': _get_period,
177 'partner_id': _get_partner,
178 'journal_id':_get_journal,
179 'currency_id': _get_currency,
180 'reference': _get_reference,
181 'narration':_get_narration,
184 'pay_now': 'pay_later',
186 'date' : time.strftime('%Y-%m-%d'),
187 'company_id': lambda self,cr,uid,c: self.pool.get('res.company')._company_default_get(cr, uid, 'account.voucher',context=c),
191 def compute_tax(self, cr, uid, ids, context={}):
192 tax_pool = self.pool.get('account.tax')
193 partner_pool = self.pool.get('res.partner')
194 position_pool = self.pool.get('account.fiscal.position')
195 voucher_line_pool = self.pool.get('account.voucher.line')
196 voucher_pool = self.pool.get('account.voucher')
198 for voucher in voucher_pool.browse(cr, uid, ids, context):
200 for line in voucher.line_ids:
201 voucher_amount += line.untax_amount or line.amount
202 line.amount = line.untax_amount or line.amount
203 voucher_line_pool.write(cr, uid, [line.id], {'amount':line.amount, 'untax_amount':line.untax_amount})
205 if not voucher.tax_id:
206 self.write(cr, uid, [voucher.id], {'amount':voucher_amount, 'tax_amount':0.0})
209 tax = [tax_pool.browse(cr, uid, voucher.tax_id.id)]
210 partner = partner_pool.browse(cr, uid, voucher.partner_id.id) or False
211 taxes = position_pool.map_tax(cr, uid, partner and partner.property_account_position or False, tax)
212 tax = tax_pool.browse(cr, uid, taxes)
214 total = voucher_amount
217 if not tax[0].price_include:
218 for tax_line in tax_pool.compute_all(cr, uid, tax, voucher_amount, 1).get('taxes',[]):
219 total_tax += tax_line.get('amount')
223 for line in voucher.line_ids:
227 for tax_line in tax_pool.compute_all(cr, uid, tax, line.untax_amount or line.amount, 1).get('taxes',[]):
228 line_tax += tax_line.get('amount')
229 line_total += tax_line.get('price_unit')
230 total_tax += line_tax
231 untax_amount = line.untax_amount or line.amount
232 voucher_line_pool.write(cr, uid, [line.id], {'amount':line_total, 'untax_amount':untax_amount})
234 self.write(cr, uid, [voucher.id], {'amount':total, 'tax_amount':total_tax})
237 def onchange_price(self, cr, uid, ids, line_ids, tax_id, partner_id=False, context={}):
238 tax_pool = self.pool.get('account.tax')
239 partner_pool = self.pool.get('res.partner')
240 position_pool = self.pool.get('account.fiscal.position')
241 voucher_line_pool = self.pool.get('account.voucher.line')
246 voucher_total_tax = 0.0
248 voucher_line_ids = []
253 for line in line_ids:
255 line_amount = line[2].get('amount')
256 voucher_line_ids += [line[1]]
257 voucher_total += line_amount
259 total = voucher_total
262 tax = [tax_pool.browse(cr, uid, tax_id)]
264 partner = partner_pool.browse(cr, uid, partner_id) or False
265 taxes = position_pool.map_tax(cr, uid, partner and partner.property_account_position or False, tax)
266 tax = tax_pool.browse(cr, uid, taxes)
268 if not tax[0].price_include:
269 for tax_line in tax_pool.compute_all(cr, uid, tax, voucher_total, 1).get('taxes',[]):
270 total_tax += tax_line.get('amount')
274 'amount':total or voucher_total,
275 'tax_amount':total_tax
281 def onchange_term_id(self, cr, uid, ids, term_id, amount):
282 term_pool = self.pool.get('account.payment.term')
285 default = {'date_due':False}
286 if term_id and amount:
287 terms = term_pool.compute(cr, uid, term_id, amount)
289 due_date = terms[-1][0]
293 return {'value':default}
295 def onchange_journal_voucher(self, cr, uid, ids, line_ids=False, tax_id=False, price=0.0, partner_id=False, journal_id=False, ttype=False, context={}):
297 Returns a dict that contains new values and context
299 @param partner_id: latest value from user input for field partner_id
300 @param args: other arguments
301 @param context: context arguments, like lang, time zone
303 @return: Returns a dict which contains new values, and context
309 if not partner_id or not journal_id:
312 partner_pool = self.pool.get('res.partner')
313 journal_pool = self.pool.get('account.journal')
315 journal = journal_pool.browse(cr, uid, journal_id)
316 partner = partner_pool.browse(cr, uid, partner_id)
319 if journal.type in ('sale','sale_refund'):
320 account_id = partner.property_account_receivable.id
322 elif journal.type in ('purchase', 'purchase_refund','expense'):
323 account_id = partner.property_account_payable.id
326 account_id = journal.default_credit_account_id.id or journal.default_debit_account_id.id
329 default['value']['account_id'] = account_id
330 default['value']['type'] = ttype or tr_type
332 vals = self.onchange_journal(cr, uid, ids, journal_id, line_ids, tax_id, partner_id, context)
333 default['value'].update(vals.get('value'))
337 def onchange_partner_id(self, cr, uid, ids, partner_id, journal_id, price, currency_id, ttype, context={}):
339 Returns a dict that contains new values and context
341 @param partner_id: latest value from user input for field partner_id
342 @param args: other arguments
343 @param context: context arguments, like lang, time zone
345 @return: Returns a dict which contains new values, and context
350 currency_pool = self.pool.get('res.currency')
351 move_pool = self.pool.get('account.move')
352 line_pool = self.pool.get('account.voucher.line')
353 move_line_pool = self.pool.get('account.move.line')
354 partner_pool = self.pool.get('res.partner')
355 journal_pool = self.pool.get('account.journal')
357 vals = self.onchange_journal(cr, uid, ids, journal_id, [], False, partner_id, context)
358 vals = vals.get('value')
359 currency_id = vals.get('currency_id', currency_id)
361 'value':{'line_ids':[], 'line_dr_ids':[], 'line_cr_ids':[], 'pre_line': False, 'currency_id':currency_id},
367 if not partner_id and ids:
368 line_ids = line_pool.search(cr, uid, [('voucher_id','=',ids[0])])
370 line_pool.unlink(cr, uid, line_ids)
373 journal = journal_pool.browse(cr, uid, journal_id)
374 partner = partner_pool.browse(cr, uid, partner_id)
376 if journal.type in ('sale','sale_refund'):
377 account_id = partner.property_account_receivable.id
378 elif journal.type in ('purchase', 'purchase_refund','expense'):
379 account_id = partner.property_account_payable.id
381 account_id = journal.default_credit_account_id.id or journal.default_debit_account_id.id
383 default['value']['account_id'] = account_id
384 if journal.type not in ('cash', 'bank'):
389 account_type = 'receivable'
390 if ttype == 'payment':
391 account_type = 'payable'
392 total_debit = price or 0.0
394 total_credit = price or 0.0
395 account_type = 'receivable'
396 ids = move_line_pool.search(cr, uid, [('account_id.type','=', account_type), ('reconcile_id','=', False), ('partner_id','=',partner_id)], context=context)
398 moves = move_line_pool.browse(cr, uid, ids)
400 company_currency = journal.company_id.currency_id.id
401 if company_currency != currency_id and ttype == 'payment':
402 total_debit = currency_pool.compute(cr, uid, currency_id, company_currency, total_debit)
403 elif company_currency != currency_id and ttype == 'receipt':
404 total_credit = currency_pool.compute(cr, uid, currency_id, company_currency, total_credit)
407 if line.credit and line.reconcile_partial_id and ttype == 'receipt':
409 if line.debit and line.reconcile_partial_id and ttype == 'payment':
411 total_credit += line.credit or 0.0
412 total_debit += line.debit or 0.0
415 if line.credit and line.reconcile_partial_id and ttype == 'receipt':
417 if line.debit and line.reconcile_partial_id and ttype == 'payment':
420 orignal_amount = line.credit or line.debit or 0.0
422 'name':line.move_id.name,
423 'type': line.credit and 'dr' or 'cr',
424 'move_line_id':line.id,
425 'account_id':line.account_id.id,
426 'amount_original':currency_pool.compute(cr, uid, company_currency, currency_id, orignal_amount),
427 'date_original':line.date,
428 'date_due':line.date_maturity,
429 'amount_unreconciled':currency_pool.compute(cr, uid, company_currency, currency_id, line.amount_unreconciled)
432 amount = min(line.amount_unreconciled, total_debit)
433 rs['amount'] = currency_pool.compute(cr, uid, company_currency, currency_id, amount)
434 total_debit -= amount
436 amount = min(line.amount_unreconciled, total_credit)
437 rs['amount'] = currency_pool.compute(cr, uid, company_currency, currency_id, amount)
438 total_credit -= amount
440 default['value']['line_ids'].append(rs)
441 if rs['type'] == 'cr':
442 default['value']['line_cr_ids'].append(rs)
444 default['value']['line_dr_ids'].append(rs)
446 if ttype == 'payment' and len(default['value']['line_cr_ids']) > 0:
447 default['value']['pre_line'] = 1
448 elif ttype == 'receipt' and len(default['value']['line_dr_ids']) > 0:
449 default['value']['pre_line'] = 1
452 def onchange_date(self, cr, user, ids, date, context={}):
454 @param date: latest value from user input for field date
455 @param args: other arguments
456 @param context: context arguments, like lang, time zone
457 @return: Returns a dict which contains new values, and context
459 period_pool = self.pool.get('account.period')
460 pids = period_pool.search(cr, user, [('date_start','<=',date), ('date_stop','>=',date)])
469 def onchange_journal(self, cr, uid, ids, journal_id, line_ids, tax_id, partner_id, context={}):
472 journal_pool = self.pool.get('account.journal')
473 journal = journal_pool.browse(cr, uid, journal_id)
474 account_id = journal.default_credit_account_id or journal.default_debit_account_id
476 if account_id and account_id.tax_ids:
477 tax_id = account_id.tax_ids[0].id
479 vals = self.onchange_price(cr, uid, ids, line_ids, tax_id, partner_id, context)
480 vals['value'].update({'tax_id':tax_id})
481 currency_id = journal.company_id.currency_id.id
483 currency_id = journal.currency.id
484 vals['value'].update({'currency_id':currency_id})
487 def proforma_voucher(self, cr, uid, ids, context=None):
488 self.action_move_line_create(cr, uid, ids, context=context)
491 def action_cancel_draft(self, cr, uid, ids, context={}):
492 wf_service = netsvc.LocalService("workflow")
493 for voucher_id in ids:
494 wf_service.trg_create(uid, 'account.voucher', voucher_id, cr)
495 self.write(cr, uid, ids, {'state':'draft'})
498 def cancel_voucher(self, cr, uid, ids, context={}):
499 reconcile_pool = self.pool.get('account.move.reconcile')
500 move_pool = self.pool.get('account.move')
501 voucher_line_pool = self.pool.get('account.voucher.line')
503 for voucher in self.browse(cr, uid, ids):
505 for line in voucher.move_ids:
506 if line.reconcile_id:
507 recs += [line.reconcile_id.id]
508 if line.reconcile_partial_id:
509 recs += [line.reconcile_partial_id.id]
511 reconcile_pool.unlink(cr, uid, recs)
514 move_pool.button_cancel(cr, uid, [voucher.move_id.id])
515 move_pool.unlink(cr, uid, [voucher.move_id.id])
520 self.write(cr, uid, ids, res)
523 def unlink(self, cr, uid, ids, context=None):
524 for t in self.read(cr, uid, ids, ['state'], context=context):
525 if t['state'] not in ('draft', 'cancel'):
526 raise osv.except_osv(_('Invalid action !'), _('Cannot delete Voucher(s) which are already opened or paid !'))
527 return super(account_voucher, self).unlink(cr, uid, ids, context=context)
529 # TODO: may be we can remove this method if not used anyware
530 def onchange_payment(self, cr, uid, ids, pay_now, journal_id, partner_id, ttype='sale'):
534 res = {'account_id':False}
535 partner_pool = self.pool.get('res.partner')
536 journal_pool = self.pool.get('account.journal')
537 if pay_now == 'pay_later':
538 partner = partner_pool.browse(cr, uid, partner_id)
539 journal = journal_pool.browse(cr, uid, journal_id)
540 if journal.type in ('sale','sale_refund'):
541 account_id = partner.property_account_receivable.id
542 elif journal.type in ('purchase', 'purchase_refund','expense'):
543 account_id = partner.property_account_payable.id
545 account_id = journal.default_credit_account_id.id or journal.default_debit_account_id.id
546 res['account_id'] = account_id
549 def action_move_line_create(self, cr, uid, ids, context=None):
551 def _get_payment_term_lines(term_id, amount):
552 term_pool = self.pool.get('account.payment.term')
553 if term_id and amount:
554 terms = term_pool.compute(cr, uid, term_id, amount)
559 move_pool = self.pool.get('account.move')
560 move_line_pool = self.pool.get('account.move.line')
561 analytic_pool = self.pool.get('account.analytic.line')
562 currency_pool = self.pool.get('res.currency')
563 invoice_pool = self.pool.get('account.invoice')
564 for inv in self.browse(cr, uid, ids):
569 elif inv.journal_id.sequence_id:
570 name = self.pool.get('ir.sequence').get_id(cr, uid, inv.journal_id.sequence_id.id)
572 raise osv.except_osv(_('Error !'), _('Please define a sequence on the journal !'))
576 'journal_id': inv.journal_id.id,
577 'narration' : inv.narration,
580 'period_id': inv.period_id and inv.period_id.id or False
582 move_id = move_pool.create(cr, uid, move)
584 #create the first line manually
585 company_currency = inv.journal_id.company_id.currency_id.id
588 # TODO: is there any other alternative then the voucher type ??
589 # -for sale, purchase we have but for the payment and receipt we do not have as based on the bank/cash journal we can not know its payment or receipt
590 if inv.type in ('purchase', 'payment'):
591 credit = currency_pool.compute(cr, uid, inv.currency_id.id, company_currency, inv.amount)
592 elif inv.type in ('sale', 'receipt'):
593 debit = currency_pool.compute(cr, uid, inv.currency_id.id, company_currency, inv.amount)
602 'name':inv.name or '/',
605 'account_id':inv.account_id.id,
607 'journal_id':inv.journal_id.id,
608 'period_id':inv.period_id.id,
609 'partner_id':inv.partner_id.id,
610 'currency_id':inv.currency_id.id,
611 'amount_currency':inv.amount,
613 'date_maturity':inv.date_due
616 if (debit == 0.0 or credit == 0.0 or debit+credit > 0) and (debit > 0.0 or credit > 0.0):
617 master_line = move_line_pool.create(cr, uid, move_line)
620 line_total = debit - credit
621 if inv.type == 'sale':
622 line_total = line_total - currency_pool.compute(cr, uid, inv.currency_id.id, company_currency, inv.tax_amount)
623 elif inv.type == 'purchase':
624 line_total = line_total + currency_pool.compute(cr, uid, inv.currency_id.id, company_currency, inv.tax_amount)
626 for line in inv.line_ids:
629 amount = currency_pool.compute(cr, uid, inv.currency_id.id, company_currency, line.amount)
632 'journal_id':inv.journal_id.id,
633 'period_id':inv.period_id.id,
634 'name':line.name and line.name or '/',
635 'account_id':line.account_id.id,
637 'partner_id':inv.partner_id.id,
638 'currency_id':inv.currency_id.id,
639 'amount_currency':line.amount,
640 'analytic_account_id':line.account_analytic_id and line.account_analytic_id.id or False,
648 if line.type == 'dr':
653 if (line.type=='dr'):
655 move_line['debit'] = amount
658 move_line['credit'] = amount
660 if inv.tax_id and inv.type in ('sale', 'purchase'):
662 'account_tax_id':inv.tax_id.id,
664 master_line = move_line_pool.create(cr, uid, move_line)
665 if line.move_line_id.id:
666 rec_ids = [master_line, line.move_line_id.id]
667 rec_list_ids.append(rec_ids)
669 if not self.pool.get('res.currency').is_zero(cr, uid, inv.currency_id, line_total):
675 'partner_id':inv.partner_id.id,
677 'credit':diff>0 and diff or 0.0,
678 'debit':diff<0 and -diff or 0.0,
681 if inv.type in ('sale', 'receipt'):
682 # if inv.journal_id.type in ('sale','sale_refund', 'cash', 'bank'):
683 account_id = inv.partner_id.property_account_receivable.id
685 account_id = inv.partner_id.property_account_payable.id
686 move_line['account_id'] = account_id
687 move_line_id = move_line_pool.create(cr, uid, move_line)
689 self.write(cr, uid, [inv.id], {
694 move_pool.post(cr, uid, [move_id], context={})
695 for rec_ids in rec_list_ids:
696 if len(rec_ids) >= 2:
697 move_line_pool.reconcile_partial(cr, uid, rec_ids)
700 def copy(self, cr, uid, id, default={}, context=None):
709 if 'date' not in default:
710 default['date'] = time.strftime('%Y-%m-%d')
711 return super(account_voucher, self).copy(cr, uid, id, default, context)
715 class account_voucher_line(osv.osv):
716 _name = 'account.voucher.line'
717 _description = 'Voucher Lines'
718 _order = "move_line_id"
720 def _compute_balance(self, cr, uid, ids, name, args, context=None):
721 currency_pool = self.pool.get('res.currency')
723 for line in self.browse(cr, uid, ids):
725 company_currency = line.voucher_id.journal_id.company_id.currency_id.id
726 voucher_currency = line.voucher_id.currency_id.id
727 move_line = line.move_line_id or False
730 res['amount_original'] = 0.0
731 res['amount_unreconciled'] = 0.0
733 elif move_line and move_line.credit > 0:
734 res['amount_original'] = currency_pool.compute(cr, uid, company_currency, voucher_currency, move_line.credit)
736 res['amount_original'] = currency_pool.compute(cr, uid, company_currency, voucher_currency, move_line.debit)
739 res['amount_unreconciled'] = currency_pool.compute(cr, uid, company_currency, voucher_currency, move_line.amount_unreconciled)
740 rs_data[line.id] = res
744 'voucher_id':fields.many2one('account.voucher', 'Voucher', required=1, ondelete='cascade'),
745 'name':fields.char('Description', size=256),
746 'account_id':fields.many2one('account.account','Account', required=True),
747 'partner_id':fields.related('voucher_id', 'partner_id', type='many2one', relation='res.partner', string='Partner'),
748 'untax_amount':fields.float('Untax Amount'),
749 'amount':fields.float('Amount', digits=(14,2)),
750 'type':fields.selection([('dr','Debit'),('cr','Credit')], 'Cr/Dr'),
751 'account_analytic_id': fields.many2one('account.analytic.account', 'Analytic Account'),
752 'move_line_id': fields.many2one('account.move.line', 'Journal Item'),
753 'date_original': fields.related('move_line_id','date', type='date', relation='account.move.line', string='Date', readonly=1),
754 'date_due': fields.related('move_line_id','date_maturity', type='date', relation='account.move.line', string='Due Date', readonly=1),
755 'amount_original': fields.function(_compute_balance, method=True, multi='dc', type='float', string='Originial Amount', store=True),
756 'amount_unreconciled': fields.function(_compute_balance, method=True, multi='dc', type='float', string='Open Balance', store=True),
757 'company_id': fields.related('voucher_id','company_id', relation='res.company', string='Company', store=True),
760 'name': lambda *a: ''
763 def onchange_move_line_id(self, cr, user, ids, move_line_id, context={}):
765 Returns a dict that contains new values and context
767 @param move_line_id: latest value from user input for field move_line_id
768 @param args: other arguments
769 @param context: context arguments, like lang, time zone
771 @return: Returns a dict which contains new values, and context
774 move_line_pool = self.pool.get('account.move.line')
776 move_line = move_line_pool.browse(cr, user, move_line_id, context=context)
777 move_id = move_line.move_id.id
780 amount = move_line.credit
783 amount = move_line.debit
784 account_id = move_line.account_id.id
786 'account_id':account_id,
793 def default_get(self, cr, user, fields_list, context=None):
795 Returns default values for fields
796 @param fields_list: list of fields, for which default values are required to be read
797 @param context: context arguments, like lang, time zone
799 @return: Returns a dict that contains default values for fields
801 journal_id = context.get('journal_id', False)
802 partner_id = context.get('partner_id', False)
803 journal_pool = self.pool.get('account.journal')
804 partner_pool = self.pool.get('res.partner')
805 values = super(account_voucher_line, self).default_get(cr, user, fields_list, context=context)
806 if (not journal_id) or ('account_id' not in fields_list):
808 journal = journal_pool.browse(cr, user, journal_id)
811 if journal.type in ('sale', 'sale_refund'):
812 account_id = journal.default_credit_account_id and journal.default_credit_account_id.id or False
814 elif journal.type in ('purchase', 'expense', 'purchase_refund'):
815 account_id = journal.default_debit_account_id and journal.default_debit_account_id.id or False
818 partner = partner_pool.browse(cr, user, partner_id, context=context)
819 if context.get('type') == 'payment':
821 account_id = partner.property_account_payable.id
822 elif context.get('type') == 'receipt':
823 account_id = partner.property_account_receivable.id
825 if (not account_id) and 'account_id' in fields_list:
826 raise osv.except_osv(_('Invalid Error !'), _('Please change partner and try again !'))
828 'account_id':account_id,
832 account_voucher_line()
834 class account_bank_statement(osv.osv):
835 _inherit = 'account.bank.statement'
837 def create_move_from_st_line(self, cr, uid, st_line_id, company_currency_id, next_number, context=None):
838 voucher_obj = self.pool.get('account.voucher')
839 wf_service = netsvc.LocalService("workflow")
840 st_line = self.pool.get('account.bank.statement.line').browse(cr, uid, st_line_id, context=context)
841 if st_line.voucher_id:
842 voucher_obj.write(cr, uid, [st_line.voucher_id.id], {'number': next_number}, context=context)
843 wf_service.trg_validate(uid, 'account.voucher', st_line.voucher_id.id, 'proforma_voucher', cr)
844 return self.pool.get('account.move.line').write(cr, uid, [x.id for x in st_line.voucher_id.move_ids], {'statement_id': st_line.statement_id.id}, context=context)
845 return super(account_bank_statement, self).create_move_from_st_line(cr, uid, st_line, company_currency_id, next_number, context=context)
847 account_bank_statement()
849 class account_bank_statement_line(osv.osv):
850 _inherit = 'account.bank.statement.line'
852 def _amount_reconciled(self, cursor, user, ids, name, args, context=None):
855 res_currency_obj = self.pool.get('res.currency')
857 company_currency_id = False
859 for line in self.browse(cursor, user, ids, context=context):
860 if not company_currency_id:
861 company_currency_id = line.company_id.id
863 res[line.id] = res_currency_obj.compute(cursor, user,
864 company_currency_id, line.statement_id.currency.id,
865 line.voucher_id.amount, context=context)
871 'amount_reconciled': fields.function(_amount_reconciled,
872 string='Amount reconciled', method=True, type='float'),
873 'voucher_id': fields.many2one('account.voucher', 'Payment'),
877 def unlink(self, cr, uid, ids, context=None):
878 statement_line = self.browse(cr, uid, ids, context)
880 for st_line in statement_line:
881 unlink_ids.append(st_line.voucher_id.id)
882 self.pool.get('account.voucher').unlink(cr, uid, unlink_ids, context=context)
883 return super(account_bank_statement_line, self).unlink(cr, uid, ids, context=context)
885 account_bank_statement_line()