Skip to content

Commit

Permalink
[IMP] tms_account: Analytic configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
max3903 committed Nov 6, 2024
1 parent 4788a4b commit 4b05d42
Show file tree
Hide file tree
Showing 17 changed files with 65 additions and 211 deletions.
7 changes: 3 additions & 4 deletions tms_account/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@
"name": "TMS - Accounting",
"summary": "Track invoices linked to TMS orders",
"version": "17.0.1.0.0",
"category": "Field Service",
"category": "Warehouse Management",
"author": "Open Source Integrators, Odoo Community Association (OCA)",
"website": "https://github.com/OCA/stock-logistics-transport",
"depends": ["tms", "tms_sale", "tms_expense", "tms_purchase", "account_usability"],
"depends": ["tms_sale", "tms_expense", "tms_purchase", "account_usability"],
"data": [
"security/res_groups.xml",
"data/analytic_plan.xml",
"security/res_groups.xml",
"views/res_config_settings.xml",
"views/account_move.xml",
"views/account_analytic_plan.xml",
"views/tms_order.xml",
"views/tms_route.xml",
],
Expand Down
3 changes: 1 addition & 2 deletions tms_account/data/analytic_plan.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

<record id="tms_route_analytic_plan" model="account.analytic.plan">
<field name="name">Routes</field>
<field name="tms_flag">True</field>
</record>

<record id="tms_order_analytic_plan" model="account.analytic.plan">
<field name="name">Trips</field>
<field name="tms_flag">True</field>
</record>

</odoo>
23 changes: 11 additions & 12 deletions tms_account/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
from . import account_analytic_plan
from . import res_config_settings
from . import tms_order
from . import analytic_account
from . import tms_route
from . import account_move
from . import account_move_line
from . import purchase_order_line
from . import hr_expense
from . import sale_order_line
from . import sale_order
from . import account_analytic_line
from . import (
tms_order,
tms_route,
account_move,
account_move_line,
purchase_order_line,
hr_expense,
sale_order_line,
sale_order,
account_analytic_line,
)
14 changes: 0 additions & 14 deletions tms_account/models/account_analytic_plan.py

This file was deleted.

2 changes: 1 addition & 1 deletion tms_account/models/account_move_line.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2019 Open Source Integrators
# Copyright (C) 2024 Open Source Integrators
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo import _, api, fields, models
from odoo.tools import frozendict
Expand Down
10 changes: 0 additions & 10 deletions tms_account/models/analytic_account.py

This file was deleted.

2 changes: 2 additions & 0 deletions tms_account/models/hr_expense.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Copyright (C) 2024 Open Source Integrators
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo import api, models


Expand Down
2 changes: 2 additions & 0 deletions tms_account/models/purchase_order_line.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Copyright (C) 2024 Open Source Integrators
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo import api, models


Expand Down
76 changes: 0 additions & 76 deletions tms_account/models/res_config_settings.py

This file was deleted.

3 changes: 1 addition & 2 deletions tms_account/models/sale_order.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Copyright (C) 2019 Open Source Integrators
# Copyright (C) 2024 Open Source Integrators
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import api, models


Expand Down
17 changes: 5 additions & 12 deletions tms_account/models/sale_order_line.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2019 Open Source Integrators
# Copyright (C) 2024 Open Source Integrators
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo import models
from odoo.fields import Command
Expand All @@ -15,23 +15,17 @@ def _default_analytic_distribution(self):
trip_count = len(self.tms_order_ids)
percentage_per_trip = 100 / trip_count

route_analytic_plan_group = self.env.ref(
"tms_account.group_tms_route_analytic_plan"
)
order_analytic_plan_group = self.env.ref(
"tms_account.group_tms_order_analytic_plan"
)
analytic_account_ids = []

trip_id = self.env["tms.order"].browse(trip.id)

if trip_id.route_id and route_analytic_plan_group:
analytic_account_id = trip_id.route_id.analytic_account_id.id
if trip_id:
analytic_account_id = trip_id.analytic_account_id.id

Check warning on line 23 in tms_account/models/sale_order_line.py

View check run for this annotation

Codecov / codecov/patch

tms_account/models/sale_order_line.py#L23

Added line #L23 was not covered by tests
if analytic_account_id:
analytic_account_ids.append(str(analytic_account_id))

if order_analytic_plan_group:
analytic_account_id = trip_id.analytic_account_id.id
if trip_id.route_id:
analytic_account_id = trip_id.route_id.analytic_account_id.id

Check warning on line 28 in tms_account/models/sale_order_line.py

View check run for this annotation

Codecov / codecov/patch

tms_account/models/sale_order_line.py#L28

Added line #L28 was not covered by tests
if analytic_account_id:
analytic_account_ids.append(str(analytic_account_id))

Expand All @@ -40,7 +34,6 @@ def _default_analytic_distribution(self):
total_distribution[distribution_key] = percentage_per_trip

self.analytic_distribution = total_distribution

return total_distribution

def _prepare_invoice_line(self, **optional_values):
Expand Down
48 changes: 11 additions & 37 deletions tms_account/models/tms_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,13 @@ def _compute_total_revenue(self):
@api.model
def create(self, vals):
order = super().create(vals)
if self.env.user.has_group(
"analytic.group_analytic_accounting"
) and self.env.user.has_group("tms_account.group_tms_order_analytic_plan"):
analytic_account = self.env["account.analytic.account"].create(
{
"name": vals.get("name"),
"plan_id": self.env.ref("tms_account.tms_order_analytic_plan").id,
"trip_id": order,
}
)
order.analytic_account_id = analytic_account
analytic_account = self.env["account.analytic.account"].create(

Check warning on line 46 in tms_account/models/tms_order.py

View check run for this annotation

Codecov / codecov/patch

tms_account/models/tms_order.py#L46

Added line #L46 was not covered by tests
{
"name": vals.get("name"),
"plan_id": self.env.ref("tms_account.tms_order_analytic_plan").id,
}
)
order.analytic_account_id = analytic_account.id

Check warning on line 52 in tms_account/models/tms_order.py

View check run for this annotation

Codecov / codecov/patch

tms_account/models/tms_order.py#L52

Added line #L52 was not covered by tests
return order

@api.model
Expand Down Expand Up @@ -100,41 +96,19 @@ def _default_analytic_distribution(self):
# Initialize distribution dictionary
distribution = {}

# Fetch the analytic accounts based on group names
route_analytic_plan_group = self.env.ref(
"tms_account.group_tms_route_analytic_plan"
)
order_analytic_plan_group = self.env.ref(
"tms_account.group_tms_order_analytic_plan"
)

analytic_account_ids = []

# Iterate over tms_order_ids to determine analytic accounts
for tms_order in self.sale_id.tms_order_ids:
if tms_order.route_id and route_analytic_plan_group:
analytic_account_id = tms_order.route_id.analytic_account_id.id
analytic_accounts = self.env["account.analytic.account"].search(
[("id", "=", analytic_account_id)]
)
account_id = str(analytic_accounts.id)
if account_id:
analytic_account_ids.append(account_id)

if order_analytic_plan_group:
analytic_account_id = tms_order.analytic_account_id.id
analytic_accounts = self.env["account.analytic.account"].search(
[("id", "=", analytic_account_id)]
)
account_id = str(analytic_accounts.id)
if account_id:
analytic_account_ids.append(account_id)
if tms_order.route_id and tms_order.route_id.analytic_account_id:
analytic_account_ids.append(tms_order.route_id.analytic_account_id.id)

Check warning on line 104 in tms_account/models/tms_order.py

View check run for this annotation

Codecov / codecov/patch

tms_account/models/tms_order.py#L104

Added line #L104 was not covered by tests
if tms_order.analytic_account_id:
analytic_account_ids.append(tms_order.analytic_account_id.id)

Check warning on line 106 in tms_account/models/tms_order.py

View check run for this annotation

Codecov / codecov/patch

tms_account/models/tms_order.py#L106

Added line #L106 was not covered by tests

# Ensure distribution is provided with unique analytic accounts
analytic_account_ids = list(set(analytic_account_ids))
if analytic_account_ids:
distribution[", ".join(analytic_account_ids)] = 100

return distribution

def _handle_bills(self):
Expand Down
22 changes: 9 additions & 13 deletions tms_account/models/tms_route.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
class TMSRoute(models.Model):
_inherit = "tms.route"

analytic_plan_id = fields.Many2one("account.analytic.plan")
analytic_account_id = fields.Many2one(
"account.analytic.account",
domain=[("plan_id", "=", "%(tms_account.tms_route_analytic_plan)d")],
Expand Down Expand Up @@ -34,16 +33,13 @@ def _compute_total_revenue(self):
@api.model
def create(self, vals_list):
route = super().create(vals_list)
if self.env.user.has_group("tms_account.group_tms_route_analytic_plan"):
# If analytic account not provided
if vals_list.get("analytic_account_id") in [False, None]:
# Create analytic account
analytic_plan = self.env.ref("tms_account.tms_route_analytic_plan")
account_vals_list = {"name": route.name, "plan_id": analytic_plan.id}
AccountAnalyticAccount = self.env["account.analytic.account"]
account = AccountAnalyticAccount.create(account_vals_list)

# Set the analytic_account_id
route.analytic_account_id = account.id

# If analytic account not provided
if vals_list.get("analytic_account_id") in [False, None]:
# Create analytic account
plan = self.env.ref("tms_account.tms_route_analytic_plan")
analytic = self.env["account.analytic.account"].create(

Check warning on line 40 in tms_account/models/tms_route.py

View check run for this annotation

Codecov / codecov/patch

tms_account/models/tms_route.py#L39-L40

Added lines #L39 - L40 were not covered by tests
{"name": route.name, "plan_id": plan.id}
)
# Set the analytic_account_id
route.analytic_account_id = analytic.id

Check warning on line 44 in tms_account/models/tms_route.py

View check run for this annotation

Codecov / codecov/patch

tms_account/models/tms_route.py#L44

Added line #L44 was not covered by tests
return route
14 changes: 0 additions & 14 deletions tms_account/views/account_analytic_plan.xml

This file was deleted.

10 changes: 1 addition & 9 deletions tms_account/views/res_config_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,12 @@
<field name="priority" eval="40" />
<field name="inherit_id" ref="tms.res_config_settings_view_form" />
<field name="arch" type="xml">
<xpath expr="//setting[@name='tms_sale_setting']" position="after">
<field name="tms_analytic_plan_domain" invisible="1" />
<xpath expr="//setting[@id='tms_account']" position="after">
<setting
help="Manage analytic account for trips or routes."
invisible="not module_tms_account"
>
<field name="group_analytic_accounting" />
<span />
<field
name="tms_analytic_plan"
invisible="not group_analytic_accounting"
domain="tms_analytic_plan_domain"
widget="many2many_checkboxes"
/>
</setting>
</xpath>
</field>
Expand Down
10 changes: 10 additions & 0 deletions tms_account/views/tms_order.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@
invisible="not sale_id and not purchase_ids"
/>
</xpath>
<xpath expr="//notebook" position="inside">
<page string="Accounting">
<group id="account_main">
<group id="account_left">
<field name="analytic_account_id" />
</group>
<group id="account_right" />
</group>
</page>
</xpath>
</field>
</record>

Expand Down
Loading

0 comments on commit 4b05d42

Please sign in to comment.