From 0fb495764565bfb03e2da331defd26837e413d41 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?R=C3=A9mi=20-=20Le=20Filament?= <remi@le-filament.com>
Date: Tue, 1 Oct 2024 18:38:45 +0200
Subject: [PATCH] [UPD] allocation of hours on projects
---
README.rst | 3 ++-
models/res_company.py | 4 +++-
models/res_config_settings.py | 2 +-
models/sale_order.py | 2 +-
models/sale_order_line.py | 38 ++++++++++++++++++++++++++++++
views/res_config_settings_view.xml | 24 +++++++------------
views/sale_view.xml | 5 +---
7 files changed, 54 insertions(+), 24 deletions(-)
diff --git a/README.rst b/README.rst
index 23c74ab..78a3784 100644
--- a/README.rst
+++ b/README.rst
@@ -8,7 +8,8 @@ LE FILAMENT - LIEN COMMANDES PROJET
====================================
Modifie le module Sale:
- - Fais le lien entre les commandes et les projets
+ - Ajoute le taux horaire sur la société et sur les bons de commande
+ - Convertit les prix vendu en heures (en divisant par le taux horaire) lors de la création des tâches
Credits
=======
diff --git a/models/res_company.py b/models/res_company.py
index 1e7a8fa..d6b6a2a 100644
--- a/models/res_company.py
+++ b/models/res_company.py
@@ -7,4 +7,6 @@ from odoo import fields, models
class Company(models.Model):
_inherit = "res.company"
- taux_horaire = fields.Integer(required=True, default=100)
+ taux_horaire = fields.Integer(
+ string="Taux horaire (€/h)", required=True, default=100
+ )
diff --git a/models/res_config_settings.py b/models/res_config_settings.py
index d9c77af..d4b6c55 100644
--- a/models/res_config_settings.py
+++ b/models/res_config_settings.py
@@ -8,5 +8,5 @@ class ResConfigSettings(models.TransientModel):
_inherit = "res.config.settings"
taux_horaire = fields.Integer(
- "Taux horaire", related="company_id.taux_horaire", readonly=False
+ "Taux horaire (€/h)", related="company_id.taux_horaire", readonly=False
)
diff --git a/models/sale_order.py b/models/sale_order.py
index fc66066..5aab687 100644
--- a/models/sale_order.py
+++ b/models/sale_order.py
@@ -8,5 +8,5 @@ class SaleOrder(models.Model):
_inherit = "sale.order"
taux_horaire = fields.Integer(
- "Taux horaire", default=lambda self: self.env.company.taux_horaire
+ "Taux horaire (€/h)", default=lambda self: self.env.company.taux_horaire
)
diff --git a/models/sale_order_line.py b/models/sale_order_line.py
index 7a3f5b8..c96b411 100644
--- a/models/sale_order_line.py
+++ b/models/sale_order_line.py
@@ -22,3 +22,41 @@ class SaleOrderLine(models.Model):
else:
planned_hours = (self.product_uom_qty * self.price_unit) / taux_horaire
return planned_hours
+
+ def _timesheet_create_project(self):
+ """
+ Reprise de la fonction native pour changer le mode de calcul des heures
+ planifiées dans timesheet
+ """
+ project = super()._timesheet_create_project()
+
+ project_uom = self.company_id.project_time_mode_id
+ uom_hour = self.env.ref("uom.product_uom_hour")
+ unit_factor = project_uom.factor * uom_hour.factor_inv
+
+ uom_unit = self.env.ref("uom.product_uom_unit")
+
+ allocated_hours = project.allocated_hours
+ # for sale_order_line in sale_order for the same project
+ # we deduce default allocation of units (Odoo considers that 1 unit = 1 hour)
+ # and we recalculate hours depending on taux_horaire
+ for line in self.order_id.order_line:
+ if (
+ line.is_service
+ and line.product_id.service_tracking
+ in ["task_in_project", "project_only"]
+ and line.product_id.project_template_id
+ == self.product_id.project_template_id
+ and line.product_uom == uom_unit
+ ):
+ allocated_hours -= line.product_uom_qty * unit_factor
+ allocated_hours += (
+ line.product_uom_qty * line.price_unit
+ ) / self.order_id.taux_horaire
+
+ project.write(
+ {
+ "allocated_hours": allocated_hours,
+ }
+ )
+ return project
diff --git a/views/res_config_settings_view.xml b/views/res_config_settings_view.xml
index 023a757..1232c94 100644
--- a/views/res_config_settings_view.xml
+++ b/views/res_config_settings_view.xml
@@ -8,23 +8,15 @@
name="name"
>res.config.settings.view.form.inherit.link.sale.project</field>
<field name="model">res.config.settings</field>
- <field name="inherit_id" ref="project.res_config_settings_view_form" />
+ <field name="inherit_id" ref="sale.res_config_settings_view_form" />
<field name="arch" type="xml">
- <div class="app_settings_block" data-string="Sales" position="inside">
- <h2>Configuration devis-projet</h2>
- <div class="row mt16 o_settings_container">
- <div class="col-12 col-lg-6 o_setting_box">
- <div class="o_setting_right_pane">
- <label
- for="taux_horaire"
- string="Taux horaire"
- class="col-3 col-lg-3 o_light_label"
- />
- <field name="taux_horaire" class="oe_inline" required="1" />
- </div>
- </div>
- </div>
- </div>
+ <app name="sale_management" position="inside">
+ <block title="Configuration devis-projet" id="sale_project_hourly_cost">
+ <setting id="hourly_cost">
+ <field name="taux_horaire" string="Taux horaire (€/h)" />
+ </setting>
+ </block>
+ </app>
</field>
</record>
diff --git a/views/sale_view.xml b/views/sale_view.xml
index d273110..3db4516 100644
--- a/views/sale_view.xml
+++ b/views/sale_view.xml
@@ -10,10 +10,7 @@
<field name="priority">100</field>
<field name="arch" type="xml">
<field name="payment_term_id" position="after">
- <field
- name="taux_horaire"
- attrs="{'readonly':[('state','!=','draft')]}"
- />
+ <field name="taux_horaire" readonly="state != 'draft'" />
</field>
</field>
--
GitLab