Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
1 Trả lời
1615 Lượt xem

okay so, I am trying to inherit the sale.order.line model and tapping into the create() method. The goal is that when a new order line gets added if it's a service-type product, we add a new order line with a specific product that is already set up on Settings. However, on the create() call, it throws an error saying: Record does not exist or has been deleted. (Record: sale.order.line(115,), User: 2) , I am not sure why. The first thing is, that the record exits and the second thing is, not using it on the create call. Here is my code: 

from odoo import models, fields, api
from odoo.exceptions import UserError


class SaleOrderLine(models.Model):
    _inherit = "sale.order.line"

    @api.model
    def create(self, vals):
        sale_order_line = super().create(vals)
        order_id = sale_order_line.order_id.id
        product_type = sale_order_line.product_template_id.detailed_type
        shop_supplies_product = self.env["res.config.settings"].get_values()[
            "shop_supplies_product"
        ]
        shop_supplies_product = self.env["product.template"].browse(
            shop_supplies_product
        )
        shop_supplies_percentage = self.env["res.config.settings"].get_values()[
            "shop_supplies_percentage"
        ]

        if (
            product_type == "service"
            and sale_order_line.product_template_id.id != shop_supplies_product
            and shop_supplies_product
        ):
            current_shop_supplies = self.env["sale.order.line"].search(
                [
                    ("order_id", "=", sale_order_line.order_id.id),
                    ("product_template_id", "=", shop_supplies_product.id),
                ],
                limit=1,
            )
            shop_supplies_amount_nt = 0
            if current_shop_supplies:
                shop_supplies_amount_nt = current_shop_supplies.price_unit
                current_shop_supplies.unlink()

            current_shop_supplies_amount_nt = float(sale_order_line.price_subtotal)

            shop_supplies_amount_nt += (
                current_shop_supplies_amount_nt * float(shop_supplies_percentage)
            ) / 100
            order_line_fields = {
                "customer_lead": 0.0,
                "name": shop_supplies_product.name,
                "order_id": order_id,
                "price_unit": shop_supplies_amount_nt,
                "product_uom_qty": 1.0,
                "product_template_id": shop_supplies_product.id,
                "product_id": shop_supplies_product.product_variant_id.id,
            }
            self.env["sale.order.line"].sudo().create(order_line_fields)
        return sale_order_line


Ảnh đại diện
Huỷ bỏ
Tác giả Câu trả lời hay nhất

The issue was matching an id with an instance of product.template. Fixed by changing the first if clause to:

if (
        product_type == "service"
        and sale_order_line.product_template_id.id != shop_supplies_product.id
        and shop_supplies_product
    ):


Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
5
thg 10 24
16595
1
thg 7 24
1517
1
thg 3 24
970
2
thg 1 24
1935
3
thg 1 24
2703