Skip to content

Commit

Permalink
[ADD] fieldservice_account_analytic: Contractor costs
Browse files Browse the repository at this point in the history
  • Loading branch information
EdgarRetes committed Dec 4, 2024
1 parent 5c93b75 commit 3dcd910
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 0 deletions.
1 change: 1 addition & 0 deletions fieldservice_account_analytic/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"data": [
"data/ir_rule.xml",
"data/analytic_plan.xml",
"security/fsm_order_cost.xml",
"security/ir.model.access.csv",
"report/fsm_order_report_template.xml",
"views/fsm_location.xml",
Expand Down
1 change: 1 addition & 0 deletions fieldservice_account_analytic/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
account_move,
analytic_account,
fsm_location,
fsm_order_cost,
fsm_order,
fsm_route,
res_company,
Expand Down
25 changes: 25 additions & 0 deletions fieldservice_account_analytic/models/fsm_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,26 @@ class FSMOrder(models.Model):

analytic_account_id = fields.Many2one("account.analytic.account", copy=False)

contractor_cost_ids = fields.One2many(
"fsm.order.cost", "fsm_order_id", string="Contractor Costs"
)

contractor_total = fields.Float(
compute="_compute_contractor_cost", string="Contractor Cost Estimate"
)

def _compute_total_cost(self):
"""To be overridden as needed from other modules"""
for order in self:
order.total_cost = 0.0

@api.depends("contractor_cost_ids")
def _compute_contractor_cost(self):
for order in self:
order.contractor_total = 0.0
for cost in order.contractor_cost_ids:
order.contractor_total += cost.price_unit * cost.quantity

@api.onchange("customer_id")
def _onchange_customer_id_location(self):
self.location_id = (
Expand All @@ -39,11 +54,16 @@ def write(self, vals):
for order in self:
if "customer_id" not in vals and not order.customer_id:
order.customer_id = order.location_id.customer_id.id

if "contractor_cost_ids" in vals:
for line in self.contractor_cost_ids:
line.analytic_distribution = line._default_analytic_distribution()
return res

@api.model_create_multi
def create(self, vals_list):
record = super().create(vals_list)

if self.env.user.has_group("analytic.group_analytic_accounting"):
for vals in vals_list:
analytic_account = self.env["account.analytic.account"].create(
Expand All @@ -56,4 +76,9 @@ def create(self, vals_list):
}
)
record.analytic_account_id = analytic_account

if "contractor_cost_ids" in vals:
for line in record.contractor_cost_ids:
line.analytic_distribution = line._default_analytic_distribution()

return record
61 changes: 61 additions & 0 deletions fieldservice_account_analytic/models/fsm_order_cost.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import api, fields, models


class FsmOrderCost(models.Model):
_name = "fsm.order.cost"
_inherit = "analytic.mixin"
_description = "Fsm Order Cost"

fsm_order_id = fields.Many2one(
comodel_name="fsm.order",
required=True,
)
price_unit = fields.Float(
string="Unit Price",
required=True,
)
quantity = fields.Float(
required=True,
default=1,
)
product_id = fields.Many2one(
comodel_name="product.product",
required=True,
)

@api.onchange("product_id")
def onchange_product_id(self):
for cost in self:
if not cost.product_id:
continue
cost.price_unit = cost.product_id.standard_price

def _default_analytic_distribution(self):
total_distribution = {}
for order in self.fsm_order_id:
if not order:
return {}
order_count = len(self.fsm_order_id)
percentage_per_order = 100 / order_count

analytic_account_ids = []

order_id = self.env["fsm.order"].browse(order.id)

analytic_account_id = order_id.location_id.analytic_account_id.id
if analytic_account_id:
analytic_account_ids.append(str(analytic_account_id))

analytic_account_id = order_id.analytic_account_id.id
if analytic_account_id:
analytic_account_ids.append(str(analytic_account_id))

if analytic_account_ids:
distribution_key = ",".join(analytic_account_ids)
total_distribution[distribution_key] = percentage_per_order

self.analytic_distribution = total_distribution

return total_distribution
20 changes: 20 additions & 0 deletions fieldservice_account_analytic/security/fsm_order_cost.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<odoo>
<record model="ir.model.access" id="fsm_order_cost_access_base_user">
<field name="name">fsm.order.cost access base user</field>
<field name="model_id" ref="model_fsm_order_cost" />
<field name="group_id" ref="base.group_user" />
<field name="perm_read" eval="1" />
<field name="perm_create" eval="0" />
<field name="perm_write" eval="0" />
<field name="perm_unlink" eval="0" />
</record>
<record model="ir.model.access" id="fsm_order_cost_access_fieldservice_user">
<field name="name">fsm.order.cost access fieldservice user</field>
<field name="model_id" ref="model_fsm_order_cost" />
<field name="group_id" ref="fieldservice.group_fsm_user" />
<field name="perm_read" eval="1" />
<field name="perm_create" eval="1" />
<field name="perm_write" eval="1" />
<field name="perm_unlink" eval="1" />
</record>
</odoo>
16 changes: 16 additions & 0 deletions fieldservice_account_analytic/views/fsm_order.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,22 @@
<field name="analytic_account_id" />
</group>
</group>
<group string="Contractor Costs">
<field name="contractor_cost_ids" nolabel="1">
<list editable="bottom">
<field name="product_id" />
<field name="quantity" />
<field name="price_unit" />
<field
name="analytic_distribution"
widget="analytic_distribution"
/>
</list>
</field>
</group>
<group>
<field name="contractor_total" readonly="1" />
</group>
</page>
</notebook>
<field name="region_id" position="after">
Expand Down

0 comments on commit 3dcd910

Please sign in to comment.