account_invoice.py 3.75 KB
Newer Older
François C. committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
# -*- coding: utf-8 -*-

from openerp import api, models
from lxml import etree
from openerp.osv.orm import setup_modifiers


class AccountInvoice(models.Model):
    _inherit = 'account.invoice'

    @api.onchange('purchase_id')
    def purchase_order_change(self):
        res = super(AccountInvoice, self).purchase_order_change()
        for line in self.invoice_line_ids:
            suppliers = line.product_id.seller_ids.filtered(
                lambda x: x.name == self.partner_id)
            if suppliers:
                line.base_price = suppliers[0].base_price
        return res

    def fields_view_get(self, cr, uid, view_id=None, view_type='form',
                        context=None, toolbar=False, submenu=False):
        if context is None:
            context = {}

        res = super(AccountInvoice, self).fields_view_get(cr, uid,
                                                          view_id=view_id,
                                                          view_type=view_type,
                                                          context=context,
                                                          toolbar=toolbar,
                                                          submenu=submenu)

        # Read only field contact base specific groups
        account_advise = self.user_has_groups(cr, uid,
                                              'account.group_account_manager')
        doc = etree.fromstring(res['arch'])
        if not account_advise:
            if view_type == 'form':
                list_readonly_field = ['journal_id', 'account_id', 'user_id',
                                       'payment_term_id', 'fiscal_position_id',
                                       'move_id', 'date',
                                       'company_id']
                for node in doc.xpath("//field"):
                    if node.get('name') in list_readonly_field:
                        node.set('readonly', '1')
                        setup_modifiers(node)
            res['arch'] = etree.tostring(doc)

        return res

    @api.onchange('state', 'partner_id', 'invoice_line_ids')
    def _onchange_allowed_purchase_ids(self):
        '''
        The purpose of the method is to define a domain for the available
        purchase orders.
        '''
        result = super(AccountInvoice, self)._onchange_allowed_purchase_ids()
        if self.type == 'in_refund':
            if result['domain']['purchase_id']:
                result['domain']['purchase_id'][0] =\
                    ('invoice_status', 'in', ('to invoice', 'invoiced'))
        return result

    def _prepare_invoice_line_from_po_line(self, line):
        data = super(AccountInvoice, self).\
            _prepare_invoice_line_from_po_line(line)
        if self.type == 'in_refund':
            qty_invoiced = 0.0
            for inv_line in line.invoice_lines:
                if inv_line.invoice_id.state not in ['cancel']:
                    if inv_line.invoice_id.type == 'in_invoice' \
                            and inv_line.invoice_id.state == 'paid':
                        qty_invoiced += inv_line.uom_id._compute_qty_obj(
                            inv_line.uom_id,
                            inv_line.quantity,
                            line.product_uom
                        )
                    elif inv_line.invoice_id.type == 'in_refund' \
                            and inv_line.invoice_id.state == 'paid':
                        qty_invoiced -= inv_line.uom_id._compute_qty_obj(
                            inv_line.uom_id,
                            inv_line.quantity,
                            line.product_uom
                        )
            # get invoiced qty
            data.update({
                'quantity': qty_invoiced
            })
        return data