|
| 1 | +# © 2025 Solvos Consultoría Informática (<http://www.solvos.es>) |
| 2 | +# License LGPL-3 - See http://www.gnu.org/licenses/lgpl-3.0.html |
| 3 | + |
| 4 | +from odoo import models, _, fields |
| 5 | +from odoo.exceptions import ValidationError |
| 6 | + |
| 7 | + |
| 8 | +class SaleOrderLine(models.Model): |
| 9 | + _inherit = "sale.order.line" |
| 10 | + |
| 11 | + offer_price = fields.Boolean() |
| 12 | + admits_offer_prices = fields.Boolean(related='product_id.admits_offer_prices', store=True) |
| 13 | + |
| 14 | + # FASE 1 desarrollo |
| 15 | + def _compute_amount(self): |
| 16 | + super()._compute_amount() |
| 17 | + # FASE 3 DEL DESARROLLO |
| 18 | + if not self.env.user.has_group('sale_order_net_price_control.group_sale_at_any_price'): |
| 19 | + |
| 20 | + for record in self.filtered(lambda r: r.product_id and r.product_uom_qty > 0): |
| 21 | + |
| 22 | + net_price_sale = record.price_subtotal / record.product_uom_qty |
| 23 | + |
| 24 | + purchase_invoice = self.env['account.move'].search([ |
| 25 | + ('invoice_line_ids.product_id', '=', record.product_id.id), |
| 26 | + ('invoice_line_ids.quantity', '>', 0), |
| 27 | + ('move_type', '=', 'in_invoice'), # factura de compra |
| 28 | + ('state', '=', 'posted'), # confirmado |
| 29 | + ], limit=1, order='name desc') |
| 30 | + |
| 31 | + if purchase_invoice: |
| 32 | + invoice_line = purchase_invoice.invoice_line_ids.filtered( |
| 33 | + lambda x: x.product_id.id == self.product_id.id |
| 34 | + ) |
| 35 | + net_price_purchase = invoice_line.price_subtotal / invoice_line.quantity |
| 36 | + |
| 37 | + if net_price_sale <= net_price_purchase: |
| 38 | + raise ValidationError(_( |
| 39 | + "Net price of '%s', '%.2f' must be higher that the last purchase invoice price '%s'" |
| 40 | + )%(record.product_template_id.name,net_price_sale,invoice_line.price_unit) ) |
| 41 | + # FASE 4 DEL DESARROLLO - parte 1 |
| 42 | + if not record.offer_price: |
| 43 | + if net_price_sale <= self._get_price_from_lowest_pricelist_id(): |
| 44 | + raise ValidationError(_( |
| 45 | + "Net price of '%s', '%.2f' must be higher that the MAYM pricelist '%s'" |
| 46 | + )%(record.product_template_id.name,net_price_sale,self._get_price_from_lowest_pricelist_id()) ) |
| 47 | + |
| 48 | + # FASE 2 desarrollo |
| 49 | + def _get_price_from_lowest_pricelist_id(self): |
| 50 | + company = self._context.get("company_id", False) and self.env["res.company"].browse( |
| 51 | + self._context["company_id"] |
| 52 | + ) or self.env.user.company_id |
| 53 | + |
| 54 | + sale_order_obj = self.env['sale.order'] |
| 55 | + self.order_id.mapped("order_line.tax_id") |
| 56 | + |
| 57 | + order_line_vals = [] |
| 58 | + for line in self: |
| 59 | + line_vals = { |
| 60 | + "product_id": line.product_id.id, |
| 61 | + "product_uom_qty": line.product_uom_qty, |
| 62 | + "order_id": False, |
| 63 | + } |
| 64 | + order_line_vals.append(line_vals) |
| 65 | + |
| 66 | + so_vals = { |
| 67 | + "partner_id": self.order_id.partner_id.id, |
| 68 | + "pricelist_id": company.lowest_pricelist_id.id, |
| 69 | + "order_line": order_line_vals, |
| 70 | + } |
| 71 | + valued_order = sale_order_obj.new(so_vals) |
| 72 | + valued_order.order_line._compute_pricelist_item_id() |
| 73 | + |
| 74 | + price = valued_order.order_line.price_unit |
| 75 | + discount = valued_order.order_line.discount |
| 76 | + return (price - (price * (discount/100))) |
0 commit comments