Last active
March 28, 2024 14:02
-
-
Save M4hd1BD/9f661e189e85111df097897ce83f5a40 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.id | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment