~openerp-commiter/openobject-addons/trunk-addons-fixes

« back to all changes in this revision

Viewing changes to account/invoice.py

  • Committer: DHS(OpenERP)
  • Date: 2010-10-26 07:16:33 UTC
  • mfrom: (4280.1.186 openobject-addons)
  • Revision ID: dhs@tinyerp.com-20101026071633-v9t8xjhwx71i4zwo
[MERGE] Merged main branch

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
import decimal_precision as dp
25
25
 
26
26
import netsvc
 
27
import pooler
27
28
from osv import fields, osv, orm
28
 
import pooler
29
29
from tools.translate import _
30
30
 
31
31
class account_invoice(osv.osv):
57
57
                                            ('company_id', '=', company_id),
58
58
                                            ('refund_journal', '=', refund_journal.get(type_inv, False))],
59
59
                                                limit=1)
60
 
        if res:
61
 
            return res[0]
62
 
        else:
63
 
            return False
 
60
        return res and res[0] or False
64
61
 
65
62
    def _get_currency(self, cr, uid, context=None):
66
63
        user = pooler.get_pool(cr.dbname).get('res.users').browse(cr, uid, [uid])[0]
67
64
        if user.company_id:
68
65
            return user.company_id.currency_id.id
69
 
        else:
70
 
            return pooler.get_pool(cr.dbname).get('res.currency').search(cr, uid, [('rate','=',1.0)])[0]
 
66
        return pooler.get_pool(cr.dbname).get('res.currency').search(cr, uid, [('rate','=', 1.0)])[0]
71
67
 
72
68
    def _get_journal_analytic(self, cr, uid, type_inv, context=None):
73
69
        type2journal = {'out_invoice': 'sale', 'in_invoice': 'purchase', 'out_refund': 'sale', 'in_refund': 'purchase'}
134
130
                result = inv.amount_total - amount
135
131
            # Use is_zero function to avoid rounding trouble => should be fixed into ORM
136
132
            res[inv.id] = not self.pool.get('res.currency').is_zero(cr, uid, inv.company_id.currency_id, result) and result or 0.0
137
 
 
138
133
        return res
139
134
 
140
135
    # Give Journal Items related to the payment reconciled to this invoice
359
354
            res['arch'] = etree.tostring(doc)
360
355
        return res
361
356
 
 
357
    def get_log_context(self, cr, uid, context=None):
 
358
        if context is None:
 
359
            context = {}
 
360
        mob_obj = self.pool.get('ir.model.data')
 
361
        res = mob_obj.get_object_reference(cr, uid, 'account', 'invoice_form') or False
 
362
        view_id = res and res[1] or False
 
363
        context.update({'view_id': view_id})
 
364
        return context
 
365
 
362
366
    def create(self, cr, uid, vals, context=None):
 
367
        if context is None:
 
368
            context = {}
363
369
        try:
364
370
            res = super(account_invoice, self).create(cr, uid, vals, context)
365
371
            for inv_id, name in self.name_get(cr, uid, [res], context=context):
 
372
                ctx = context.copy()
 
373
                if vals.get('type', 'in_invoice') in ('out_invoice', 'out_refund'):
 
374
                    ctx = self.get_log_context(cr, uid, context=ctx)
366
375
                message = _("Invoice '%s' is waiting for validation.") % name
367
 
                self.log(cr, uid, inv_id, message)
 
376
                self.log(cr, uid, inv_id, message, context=ctx)
368
377
            return res
369
378
        except Exception, e:
370
379
            if '"journal_id" viol' in e.args[0]:
491
500
            res = {'value':{'date_due': pterm_list[-1]}}
492
501
        else:
493
502
             raise osv.except_osv(_('Data Insufficient !'), _('The Payment Term of Supplier does not have Payment Term Lines(Computation) defined !'))
494
 
 
495
503
        return res
496
504
 
497
505
    def onchange_invoice_line(self, cr, uid, ids, lines):
504
512
        val = {}
505
513
        dom = {}
506
514
        obj_journal = self.pool.get('account.journal')
 
515
        account_obj = self.pool.get('account.account')
 
516
        inv_line_obj = self.pool.get('account.invoice.line')
507
517
        if company_id and part_id and type:
508
518
            acc_id = False
509
519
            partner_obj = self.pool.get('res.partner').browse(cr,uid,part_id)
528
538
                    else:
529
539
                        acc_id = pay_res_id
530
540
                    val= {'account_id': acc_id}
531
 
            account_obj = self.pool.get('account.account')
532
541
            if ids:
533
542
                if company_id:
534
543
                    inv_obj = self.browse(cr,uid,ids)
539
548
                                if not result_id:
540
549
                                    raise osv.except_osv(_('Configuration Error !'),
541
550
                                        _('Can not find account chart for this company in invoice line account, Please Create account.'))
542
 
                                self.pool.get('account.invoice.line').write(cr, uid, [line.id], {'account_id': result_id[0]})
 
551
                                inv_line_obj.write(cr, uid, [line.id], {'account_id': result_id[0]})
543
552
            else:
544
553
                if invoice_line:
545
554
                    for inv_line in invoice_line:
580
589
            else:
581
590
                val['currency_id'] = company.currency_id.id
582
591
 
583
 
        return {'value': val, 'domain': dom }
 
592
        return {'value': val, 'domain': dom}
584
593
 
585
594
    # go from canceled state to draft state
586
595
    def action_cancel_draft(self, cr, uid, ids, *args):
792
801
            line = []
793
802
            for key, val in line2.items():
794
803
                line.append((0,0,val))
795
 
 
796
804
        return line
797
805
 
798
806
    def action_move_create(self, cr, uid, ids, *args):
967
975
            'analytic_account_id':x.get('account_analytic_id',False),
968
976
        }
969
977
 
970
 
    def action_number(self, cr, uid, ids, *args):
 
978
    def action_number(self, cr, uid, ids, context=None):
 
979
        if context is None:
 
980
            context = {}
971
981
        #TODO: not correct fix but required a frech values before reading it.
972
982
        self.write(cr, uid, ids, {})
973
983
 
981
991
            self.write(cr, uid, ids, {'internal_number':number})
982
992
 
983
993
            if invtype in ('in_invoice', 'in_refund'):
984
 
                ref = reference
 
994
                if not reference:
 
995
                    ref = self._convert_ref(cr, uid, number)
 
996
                else:
 
997
                    ref = reference
985
998
            else:
986
999
                ref = self._convert_ref(cr, uid, number)
987
1000
 
998
1011
                        (ref, move_id))
999
1012
 
1000
1013
            for inv_id, name in self.name_get(cr, uid, [id]):
 
1014
                ctx = context.copy()
 
1015
                if obj_inv.type in ('out_invoice', 'out_refund'):
 
1016
                    ctx = self.get_log_context(cr, uid, context=ctx)
1001
1017
                message = _('Invoice ') + " '" + name + "' "+ _("is validated.")
1002
 
                self.log(cr, uid, inv_id, message)
1003
 
 
 
1018
                self.log(cr, uid, inv_id, message, context=ctx)
1004
1019
        return True
1005
1020
 
1006
1021
    def action_cancel(self, cr, uid, ids, *args):
1489
1504
        taxes = self.pool.get('account.account').browse(cr, uid, account_id).tax_ids
1490
1505
        fpos = fposition_id and self.pool.get('account.fiscal.position').browse(cr, uid, fposition_id) or False
1491
1506
        res = self.pool.get('account.fiscal.position').map_tax(cr, uid, fpos, taxes)
1492
 
        r = {'value':{'invoice_line_tax_id': res}}
1493
 
        return r
 
1507
        return {'value':{'invoice_line_tax_id': res}}
 
1508
 
1494
1509
account_invoice_line()
1495
1510
 
1496
1511
class account_invoice_tax(osv.osv):
1522
1537
        'amount': fields.float('Amount', digits_compute=dp.get_precision('Account')),
1523
1538
        'manual': fields.boolean('Manual'),
1524
1539
        'sequence': fields.integer('Sequence', help="Gives the sequence order when displaying a list of invoice tax."),
1525
 
 
1526
1540
        'base_code_id': fields.many2one('account.tax.code', 'Base Code', help="The account basis of the tax declaration."),
1527
1541
        'base_amount': fields.float('Base Code Amount', digits_compute=dp.get_precision('Account')),
1528
1542
        'tax_code_id': fields.many2one('account.tax.code', 'Tax Code', help="The tax basis of the tax declaration."),
1560
1574
 
1561
1575
    _order = 'sequence'
1562
1576
    _defaults = {
1563
 
        'manual': lambda *a: 1,
1564
 
        'base_amount': lambda *a: 0.0,
1565
 
        'tax_amount': lambda *a: 0.0,
 
1577
        'manual': 1,
 
1578
        'base_amount': 0.0,
 
1579
        'tax_amount': 0.0,
1566
1580
    }
1567
1581
    def compute(self, cr, uid, invoice_id, context={}):
1568
1582
        tax_grouped = {}