Multi is the default api for methods, it is not necessary to explicitly decorate methods with it, adds clutter and most people use it because they see that the rest of the code uses it. Done with `find . -type f -name '*.py' | xargs sed -i '/@api.multi/d'`
112 lines
4.9 KiB
Python
112 lines
4.9 KiB
Python
# -*- coding: utf-8 -*-
|
|
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
|
|
|
from odoo import api, fields, models, _
|
|
|
|
|
|
class AccountMove(models.Model):
|
|
_inherit = 'account.move'
|
|
|
|
purchase_vendor_bill_id = fields.Many2one('purchase.bill.union', store=False, readonly=True,
|
|
states={'draft': [('readonly', False)]},
|
|
string='Auto-complete',
|
|
help="Auto-complete from a past bill / purchase order.")
|
|
purchase_id = fields.Many2one('purchase.order', store=False, readonly=True,
|
|
states={'draft': [('readonly', False)]},
|
|
string='Purchase Order',
|
|
help="Auto-complete from a past purchase order.")
|
|
|
|
@api.onchange('purchase_vendor_bill_id', 'purchase_id')
|
|
def _onchange_purchase_auto_complete(self):
|
|
''' Load from either an old purchase order, either an old vendor bill.
|
|
|
|
When setting a 'purchase.bill.union' in 'purchase_vendor_bill_id':
|
|
* If it's a vendor bill, 'invoice_vendor_bill_id' is set and the loading is done by '_onchange_invoice_vendor_bill'.
|
|
* If it's a purchase order, 'purchase_id' is set and this method will load lines.
|
|
|
|
/!\ All this not-stored fields must be empty at the end of this function.
|
|
'''
|
|
if self.purchase_vendor_bill_id.vendor_bill_id:
|
|
self.invoice_vendor_bill_id = self.purchase_vendor_bill_id.vendor_bill_id
|
|
self._onchange_invoice_vendor_bill()
|
|
elif self.purchase_vendor_bill_id.purchase_order_id:
|
|
self.purchase_id = self.purchase_vendor_bill_id.purchase_order_id
|
|
self.purchase_vendor_bill_id = False
|
|
|
|
if not self.purchase_id:
|
|
return
|
|
|
|
# Copy partner.
|
|
self.partner_id = self.purchase_id.partner_id
|
|
self.fiscal_position_id = self.purchase_id.fiscal_position_id
|
|
self.invoice_payment_term_id = self.purchase_id.payment_term_id
|
|
self.currency_id = self.purchase_id.currency_id
|
|
|
|
# Copy purchase lines.
|
|
po_lines = self.purchase_id.order_line - self.line_ids.mapped('purchase_line_id')
|
|
new_lines = self.env['account.move.line']
|
|
for line in po_lines.filtered(lambda l: not l.display_type):
|
|
new_line = new_lines.new(line._prepare_account_move_line(self))
|
|
new_line.account_id = new_line._get_computed_account()
|
|
new_line._onchange_price_subtotal()
|
|
new_lines += new_line
|
|
new_lines._onchange_mark_recompute_taxes()
|
|
|
|
# Compute invoice_origin.
|
|
origins = set(self.line_ids.mapped('purchase_line_id.order_id.name'))
|
|
self.invoice_origin = ','.join(list(origins))
|
|
|
|
# Compute ref.
|
|
refs = set(self.line_ids.mapped('purchase_line_id.order_id.partner_ref'))
|
|
refs = [ref for ref in refs if ref]
|
|
self.ref = ','.join(refs)
|
|
|
|
# Compute _invoice_payment_ref.
|
|
if len(refs) == 1:
|
|
self._invoice_payment_ref = refs[0]
|
|
|
|
self.purchase_id = False
|
|
self._onchange_currency()
|
|
|
|
@api.model_create_multi
|
|
def create(self, vals_list):
|
|
# OVERRIDE
|
|
moves = super(AccountMove, self).create(vals_list)
|
|
for move in moves:
|
|
if move.reversed_entry_id:
|
|
continue
|
|
purchase = move.line_ids.mapped('purchase_line_id.order_id')
|
|
if not purchase:
|
|
continue
|
|
refs = ["<a href=# data-oe-model=purchase.order data-oe-id=%s>%s</a>" % tuple(name_get) for name_get in purchase.name_get()]
|
|
message = _("This vendor bill has been created from: %s") % ','.join(refs)
|
|
move.message_post(body=message)
|
|
return moves
|
|
|
|
def write(self, vals):
|
|
# OVERRIDE
|
|
old_purchases = [move.mapped('line_ids.purchase_line_id.order_id') for move in self]
|
|
res = super(AccountMove, self).write(vals)
|
|
for i, move in enumerate(self):
|
|
new_purchases = move.mapped('line_ids.purchase_line_id.order_id')
|
|
if not new_purchases:
|
|
continue
|
|
diff_purchases = new_purchases - old_purchases[i]
|
|
if diff_purchases:
|
|
refs = ["<a href=# data-oe-model=purchase.order data-oe-id=%s>%s</a>" % tuple(name_get) for name_get in diff_purchases.name_get()]
|
|
message = _("This vendor bill has been modified from: %s") % ','.join(refs)
|
|
move.message_post(body=message)
|
|
return res
|
|
|
|
|
|
class AccountMoveLine(models.Model):
|
|
""" Override AccountInvoice_line to add the link to the purchase order line it is related to"""
|
|
_inherit = 'account.move.line'
|
|
|
|
purchase_line_id = fields.Many2one('purchase.order.line', 'Purchase Order Line', ondelete='set null', index=True)
|
|
|
|
def _copy_data_extend_business_fields(self, values):
|
|
# OVERRIDE to copy the 'purchase_line_id' field as well.
|
|
super(AccountMoveLine, self)._copy_data_extend_business_fields(values)
|
|
values['purchase_line_id'] = self.purchase_line_id.id
|