Skip to content
Extraits de code Groupes Projets
Valider 59d8b1e9 rédigé par jordan's avatar jordan
Parcourir les fichiers

[update] refactor code to put update_order_lines in project + add clean_sale_order function

parent dc72be80
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
......@@ -757,7 +757,7 @@ class SaleIntervention(models.Model):
% rec.project_subvention_id.name
)
total_price += rec.price
rec._update_order_lines()
rec.project_id.update_order_lines()
return total_price
def _get_plants_qties(self):
......@@ -936,51 +936,13 @@ class SaleIntervention(models.Model):
# ------------------------------------------------------
# ------------------------------------------------------
# Business methods
# CRUD methods (ORM overrides)
# ------------------------------------------------------
def _update_order_lines(self):
# group by intervention_type for current project
lines = self.read_group(
(("project_id", "=", self.project_id.id),),
("price", "intervention_length", "plant_qty"),
"intervention_type_id",
lazy=False,
)
for line in lines:
product_tmpl_id = line["intervention_type_id"][0]
product_tmpl = self.env["product.template"].browse(product_tmpl_id)
quantity = (
line["intervention_length"]
if product_tmpl.uom_name == "m"
else line["plant_qty"]
)
data = {}
if quantity != 0:
data = {
"price_unit": line["price"] / quantity,
"product_uom_qty": quantity,
}
existing_lines = self.sale_order_id.order_line.filtered(
lambda r: r.product_id.id == product_tmpl.product_variant_id.id
and r.sale_project_id.id == self.project_id.id
)
if existing_lines:
# Update these lines
existing_lines.write(data)
else:
# Add missing fields and create new lines
data.update(
{
"order_id": self.sale_order_id.id,
"name": "Projet "
+ self.project_id.name
+ " - "
+ product_tmpl.name,
"product_id": product_tmpl.product_variant_id.id,
"sale_project_id": self.project_id.id,
}
)
self.env["sale.order.line"].create(data)
def unlink(self):
project_id = self.project_id
res = super().unlink()
project_id.update_order_lines()
return res
class SaleInterventionStock(models.Model):
......
......@@ -22,3 +22,17 @@ class SaleOrder(models.Model):
inverse_name="sale_order_id",
string="Articles d'interventions'",
)
# ------------------------------------------------------
# Fields declaration
# ------------------------------------------------------
def clean_sale_order(self):
current_inter = list()
for project in self.sale_project_ids:
for inter in project.intervention_ids:
current_inter.append(
(project.id, inter.intervention_type_id.id))
for so in self.order_line:
if so.product_id and (so.sale_project_id.id, so.product_id.id)\
not in current_inter:
so.unlink()
......@@ -151,7 +151,6 @@ class SaleProject(models.Model):
# ------------------------------------------------------
# CRUD methods (ORM overrides)
# ------------------------------------------------------
@api.model_create_multi
def create(self, vals_list):
res = super().create(vals_list)
......@@ -206,7 +205,50 @@ class SaleProject(models.Model):
# ------------------------------------------------------
# Business methods
# ------------------------------------------------------
def update_order_lines(self):
# group by intervention_type for current project
lines = self.env['sale.intervention'].read_group(
(("project_id", "=", self.id),),
("price", "intervention_length", "plant_qty"),
"intervention_type_id",
lazy=False,
)
for line in lines:
product_tmpl_id = line["intervention_type_id"][0]
product_tmpl = self.env["product.template"].browse(product_tmpl_id)
quantity = (
line["intervention_length"]
if product_tmpl.uom_name == "m"
else line["plant_qty"]
)
data = {}
if quantity != 0:
data = {
"price_unit": line["price"] / quantity,
"product_uom_qty": quantity,
}
existing_lines = self.sale_order_id.order_line.filtered(
lambda r: r.product_id.id == product_tmpl.product_variant_id.id
and r.sale_project_id.id == self.id
)
if existing_lines:
# Update these lines
existing_lines.write(data)
else:
# Add missing fields and create new lines
data.update(
{
"order_id": self.sale_order_id.id,
"name": "Projet "
+ self.name
+ " - "
+ product_tmpl.name,
"product_id": product_tmpl.product_variant_id.id,
"sale_project_id": self.id,
}
)
self.env["sale.order.line"].create(data)
self.sale_order_id.clean_sale_order()
class SaleFinancialHelp(models.Model):
_name = "sale.financial.help"
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter