diff --git a/models/sale_intervention.py b/models/sale_intervention.py index 853c9afee375d1fd5ad7bc864b147c310ff9e7e8..7a81e40101528e129c419c1d9279b04acac8802a 100644 --- a/models/sale_intervention.py +++ b/models/sale_intervention.py @@ -11,7 +11,6 @@ class SaleIntervention(models.Model): """ Rewrite function to use Taxes """ - product_tmpl = self.intervention_type_id plant_categ_id = self.env.ref("ap_sale_project.product_category_plant") vat_plant_categ = ( self.env["product.category"].browse(plant_categ_id.id).taxes_id @@ -22,106 +21,108 @@ class SaleIntervention(models.Model): vat_furniture_categ = ( self.env["product.category"].browse(furniture_categ_id.id).taxes_id ) - pricelist = self.project_subvention_id.product_pricelist_ids.filtered( - lambda i: i.intervention_type_id.id == product_tmpl.id + lines = self.read_group( + (("project_id", "=", self.project_id.id),), + ("price", "intervention_length", "plant_qty"), + "intervention_type_id", + lazy=False, ) - if ( - pricelist - and pricelist.item_ids.filtered(lambda i: i.categ_id == plant_categ_id) - and self.env.user.company_id.vat - ): - interventions = self.project_id.intervention_ids.filtered( - lambda i: i.intervention_type_id == self.intervention_type_id + for line in lines: + product_tmpl_id = line["intervention_type_id"][0] + product_tmpl = self.env["product.template"].browse(product_tmpl_id) + pricelist = self.project_subvention_id.product_pricelist_ids.filtered( + lambda i: i.intervention_type_id.id == product_tmpl_id ) - quantity = 0 - price = 0 - for inter in interventions: - quantity += ( - inter.intervention_length + if ( + pricelist + and pricelist.item_ids.filtered(lambda i: i.categ_id == plant_categ_id) + and self.env.user.company_id.vat + ): + quantity = ( + line["intervention_length"] if product_tmpl.uom_name == "m" - else inter.plant_qty + else line["plant_qty"] ) - price += inter.price - if quantity != 0: - price_unit = price / quantity - plant_pricelist_item = pricelist.item_ids.filtered( - lambda i: i.categ_id == plant_categ_id - ) - plant_price = plant_pricelist_item.fixed_price - 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: - sol_plant = existing_lines.filtered( - lambda sol: sol.tax_id == vat_plant_categ + if quantity != 0: + price_unit = line["price"] / quantity + plant_pricelist_item = pricelist.item_ids.filtered( + lambda i: i.categ_id == plant_categ_id ) - if len(sol_plant) == 1: - sol_plant.write( - { - "product_uom_qty": quantity, - } + plant_price = plant_pricelist_item.fixed_price + 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: + sol_plant = existing_lines.filtered( + lambda sol: sol.tax_id == vat_plant_categ + ) + if len(sol_plant) == 1: + sol_plant.write( + { + "product_uom_qty": quantity, + } + ) + else: + raise UserWarning( + "Impossible de mettre à jour la ligne des plants" + ) + sol_furniture = existing_lines.filtered( + lambda sol: sol.tax_id == vat_furniture_categ ) + if len(sol_furniture) == 1: + sol_furniture.write( + { + "product_uom_qty": quantity, + "price_unit": price_unit - plant_price, + } + ) + else: + raise UserWarning( + "Impossible de mettre à jour la ligne des fournitures" + ) else: - raise UserWarning( - "Impossible de mettre à jour la ligne des plants" + sols = list() + data_sol_section = { + "order_id": self.sale_order_id.id, + "product_id": product_tmpl.product_variant_id.id, + "product_uom_qty": quantity, + "sale_project_id": self.project_id.id, + } + sol_name = ( + "Projet " + self.project_id.name + " - " + product_tmpl.name ) - sol_furniture = existing_lines.filtered( - lambda sol: sol.tax_id == vat_furniture_categ - ) - if len(sol_furniture) == 1: - sol_furniture.write( + sols.append( { - "product_uom_qty": quantity, - "price_unit": price_unit - plant_price, + **data_sol_section, + **{ + "name": sol_name, + "display_type": "line_section", + }, } ) - else: - raise UserWarning( - "Impossible de mettre à jour la ligne des fournitures" + sols.append( + { + **data_sol_section, + **{ + "price_unit": plant_price, + "name": sol_name + " - Plants garantis 3 ans", + "tax_id": [(6, 0, vat_plant_categ.ids)], + }, + } ) - else: - sols = list() - data_sol_section = { - "order_id": self.sale_order_id.id, - "product_id": product_tmpl.product_variant_id.id, - "product_uom_qty": quantity, - "sale_project_id": self.project_id.id, - } - sol_name = ( - "Projet " + self.project_id.name + " - " + product_tmpl.name - ) - sols.append( - { - **data_sol_section, - **{ - "name": sol_name, - "display_type": "line_section", - }, - } - ) - sols.append( - { - **data_sol_section, - **{ - "price_unit": plant_price, - "name": sol_name + " - Plants garantis 3 ans", - "tax_id": [(6, 0, vat_plant_categ.ids)], - }, - } - ) - sols.append( - { - **data_sol_section, - **{ - "price_unit": price_unit - plant_price, - "name": sol_name + " - Paillage, protections, " - "ingénierie, livraison et suivi", - "tax_id": [(6, 0, vat_furniture_categ.ids)], - }, - } - ) - for sol in sols: - self.env["sale.order.line"].create(sol) - else: - return super(SaleIntervention, self)._update_order_lines() + sols.append( + { + **data_sol_section, + **{ + "price_unit": price_unit - plant_price, + "name": sol_name + " - Paillage, protections, " + "ingénierie, livraison et suivi", + "tax_id": [(6, 0, vat_furniture_categ.ids)], + }, + } + ) + for sol in sols: + self.env["sale.order.line"].create(sol) + else: + return super(SaleIntervention, self)._update_order_lines()