You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 lines
3.9 KiB

# Copyright 2004-2009 Tiny SPRL (<http://tiny.be>).
# Copyright 2016 ACSONE SA/NV (<http://acsone.eu>)
# Copyright 2015-2019 Tecnativa - Pedro M. Baeza
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo import api, fields, models
class PurchaseOrder(models.Model):
_inherit = "purchase.order"
def _add_supplier_to_product(self):
"""Insert a mapping of products to PO lines to be picked up
in supplierinfo's create()"""
self.ensure_one()
po_line_map = {
line.product_id.product_tmpl_id.id: line for line in self.order_line
}
return super(
PurchaseOrder, self.with_context(po_line_map=po_line_map)
)._add_supplier_to_product()
class PurchaseOrderLine(models.Model):
_inherit = "purchase.order.line"
# adding discount to depends
@api.depends("discount")
def _compute_amount(self):
return super()._compute_amount()
def _prepare_compute_all_values(self):
vals = super()._prepare_compute_all_values()
vals.update({"price_unit": self._get_discounted_price_unit()})
return vals
discount = fields.Float(string="Discount (%)", digits="Discount")
_sql_constraints = [
(
"discount_limit",
"CHECK (discount <= 100.0)",
"Discount must be lower than 100%.",
)
]
def _get_discounted_price_unit(self):
"""Inheritable method for getting the unit price after applying
discount(s).
:rtype: float
:return: Unit price after discount(s).
"""
self.ensure_one()
if self.discount:
return self.price_unit * (1 - self.discount / 100)
return self.price_unit
@api.onchange("product_qty", "product_uom")
def _onchange_quantity(self):
"""
Check if a discount is defined into the supplier info and if so then
apply it to the current purchase order line
"""
res = super()._onchange_quantity()
if self.product_id:
date = None
if self.order_id.date_order:
date = self.order_id.date_order.date()
seller = self.product_id._select_seller(
partner_id=self.partner_id,
quantity=self.product_qty,
date=date,
uom_id=self.product_uom,
)
self._apply_value_from_seller(seller)
return res
@api.model
def _apply_value_from_seller(self, seller):
"""Overload this function to prepare other data from seller,
like in purchase_triple_discount module"""
if not seller:
return
self.discount = seller.discount
def _prepare_account_move_line(self, move=False):
vals = super(PurchaseOrderLine, self)._prepare_account_move_line(move)
vals["discount"] = self.discount
return vals
@api.model
def _prepare_purchase_order_line(
self, product_id, product_qty, product_uom, company_id, supplier, po
):
"""Apply the discount to the created purchase order"""
res = super()._prepare_purchase_order_line(
product_id, product_qty, product_uom, company_id, supplier, po
)
partner = supplier.name
uom_po_qty = product_uom._compute_quantity(product_qty, product_id.uom_po_id)
seller = product_id.with_company(company_id)._select_seller(
partner_id=partner,
quantity=uom_po_qty,
date=po.date_order and po.date_order.date(),
uom_id=product_id.uom_po_id,
)
res.update(self._prepare_purchase_order_line_from_seller(seller))
return res
@api.model
def _prepare_purchase_order_line_from_seller(self, seller):
"""Overload this function to prepare other data from seller,
like in purchase_triple_discount module"""
if not seller:
return {}
return {"discount": seller.discount}