Skip to content

Commit

Permalink
[ADD] tms_sale
Browse files Browse the repository at this point in the history
  • Loading branch information
EdgarRetes committed Oct 18, 2024
1 parent 11c868d commit b30ae9d
Show file tree
Hide file tree
Showing 24 changed files with 1,179 additions and 0 deletions.
1 change: 1 addition & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ env:
# See https://github.com/OCA/odoo-community.org/issues/37#issuecomment-470686449
parserOptions:
ecmaVersion: 2019
sourceType: module

overrides:
- files:
Expand Down
35 changes: 35 additions & 0 deletions tms_sale/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
**This file is going to be generated by oca-gen-addon-readme.**

*Manual changes will be overwritten.*

Please provide content in the ``readme`` directory:

* **DESCRIPTION.rst** (required)
* INSTALL.rst (optional)
* CONFIGURE.rst (optional)
* **USAGE.rst** (optional, highly recommended)
* DEVELOP.rst (optional)
* ROADMAP.rst (optional)
* HISTORY.rst (optional, recommended)
* **CONTRIBUTORS.rst** (optional, highly recommended)
* CREDITS.rst (optional)

Content of this README will also be drawn from the addon manifest,
from keys such as name, authors, maintainers, development_status,
and license.

A good, one sentence summary in the manifest is also highly recommended.


Automatic changelog generation
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

`HISTORY.rst` can be auto generated using `towncrier <https://pypi.org/project/towncrier>`_.

Just put towncrier compatible changelog fragments into `readme/newsfragments`
and the changelog file will be automatically generated and updated when a new fragment is added.

Please refer to `towncrier` documentation to know more.

NOTE: the changelog will be automatically generated when using `/ocabot merge $option`.
If you need to run it manually, refer to `OCA/maintainer-tools README <https://github.com/OCA/maintainer-tools>`_.
2 changes: 2 additions & 0 deletions tms_sale/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import models
from . import wizard
30 changes: 30 additions & 0 deletions tms_sale/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Copyright (C) 2018 Open Source Integrators
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
{
"name": "TMS - Sales",
"version": "17.0.1.0.0",
"summary": "Sell transportation management system.",
"category": "TMS",
"author": "Open Source Integrators, Odoo Community Association (OCA)",
"website": "https://github.com/OCA/stock-logistics-transport",
"depends": ["tms", "tms_product", "sale_management", "web"],
"data": [
"security/ir.model.access.csv",
"wizard/sale_order_line_trip_views.xml",
"wizard/seat_ticket_line_views.xml",
"views/sale_order_views.xml",
"views/tms_order_views.xml",
"views/product_template_views.xml",
],
"assets": {
"web.assets_backend": [
"tms_sale/static/src/js/line_trip_wizard_controller.js",
"tms_sale/static/src/js/line_ticket_wizard_controller.js",
"tms_sale/static/src/js/sale_order_line_product_field.js",
],
},
"license": "AGPL-3",
"development_status": "Alpha",
"maintainers": ["max3903", "santiagordz", "EdgarRetes"],
"installable": True,
}
4 changes: 4 additions & 0 deletions tms_sale/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from . import sale_order_line
from . import sale_order
from . import seat_ticket
from . import tms_order
204 changes: 204 additions & 0 deletions tms_sale/models/sale_order.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
# Copyright (C) 2024 Open Source Integrators
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from markupsafe import Markup

from odoo import _, api, fields, models


class SaleOrder(models.Model):
_inherit = "sale.order"

tms_order_ids = fields.Many2many(
"tms.order",
compute="_compute_tms_order_ids",
string="Transport orders associated to this sale",
copy=False,
)
tms_order_count = fields.Integer(
string="Transport Orders", compute="_compute_tms_order_ids"
)

has_tms_order = fields.Boolean(compute="_compute_has_tms_order")

def action_view_trip_sale_order_line(self):
return {

Check warning on line 24 in tms_sale/models/sale_order.py

View check run for this annotation

Codecov / codecov/patch

tms_sale/models/sale_order.py#L24

Added line #L24 was not covered by tests
"type": "ir.actions.act_window",
"res_model": "sale.order.line.trip",
"target": "new",
"view_mode": "form",
"view_type": "form",
}

@api.depends("order_line")
def _compute_has_tms_order(self):
for sale in self:
has_tms_order = any(
line.product_template_id.tms_trip
and line.product_template_id.trip_product_type == "trip"
for line in sale.order_line
)
sale.has_tms_order = has_tms_order

Check warning on line 40 in tms_sale/models/sale_order.py

View check run for this annotation

Codecov / codecov/patch

tms_sale/models/sale_order.py#L40

Added line #L40 was not covered by tests

@api.depends("order_line")
def _compute_tms_order_ids(self):
for sale in self:
tms = self.env["tms.order"].search(

Check warning on line 45 in tms_sale/models/sale_order.py

View check run for this annotation

Codecov / codecov/patch

tms_sale/models/sale_order.py#L45

Added line #L45 was not covered by tests
[
"|",
("sale_id", "=", sale.id),
("sale_line_id", "in", sale.order_line.ids),
]
)
sale.tms_order_ids = tms
sale.tms_order_count = len(sale.tms_order_ids)

Check warning on line 53 in tms_sale/models/sale_order.py

View check run for this annotation

Codecov / codecov/patch

tms_sale/models/sale_order.py#L52-L53

Added lines #L52 - L53 were not covered by tests

def _tms_generate_line_tms_orders(self, new_tms_sol):
"""
Generate TMS Orders for the given sale order lines.
Override this method to filter lines to generate TMS Orders for.
"""
self.ensure_one()
new_tms_orders = self.env["tms.order"]

Check warning on line 62 in tms_sale/models/sale_order.py

View check run for this annotation

Codecov / codecov/patch

tms_sale/models/sale_order.py#L61-L62

Added lines #L61 - L62 were not covered by tests

for line in new_tms_sol:
for i in range(int(line.product_uom_qty) - len(line.tms_order_ids)):
vals = line._prepare_line_tms_values(line)
tms_by_line = self.env["tms.order"].sudo().create(vals)
line.write({"tms_order_ids": [(4, tms_by_line.id)]})
new_tms_orders |= tms_by_line
i = i # pre-commit

Check warning on line 70 in tms_sale/models/sale_order.py

View check run for this annotation

Codecov / codecov/patch

tms_sale/models/sale_order.py#L66-L70

Added lines #L66 - L70 were not covered by tests

return new_tms_orders

Check warning on line 72 in tms_sale/models/sale_order.py

View check run for this annotation

Codecov / codecov/patch

tms_sale/models/sale_order.py#L72

Added line #L72 was not covered by tests

def _tms_generate(self):
self.ensure_one()
new_tms_orders = self.env["tms.order"]

Check warning on line 76 in tms_sale/models/sale_order.py

View check run for this annotation

Codecov / codecov/patch

tms_sale/models/sale_order.py#L75-L76

Added lines #L75 - L76 were not covered by tests

new_tms_line_sol = self.order_line.filtered(
lambda L: L.product_id.trip_product_type == "trip"
and len(L.tms_order_ids) != L.product_uom_qty
and len(L.tms_order_ids) < L.product_uom_qty
)

new_tms_orders |= self._tms_generate_line_tms_orders(new_tms_line_sol)

Check warning on line 84 in tms_sale/models/sale_order.py

View check run for this annotation

Codecov / codecov/patch

tms_sale/models/sale_order.py#L84

Added line #L84 was not covered by tests

return new_tms_orders

Check warning on line 86 in tms_sale/models/sale_order.py

View check run for this annotation

Codecov / codecov/patch

tms_sale/models/sale_order.py#L86

Added line #L86 was not covered by tests

def _tms_generation(self):
"""
Create TMS Orders based on the products' configuration.
:rtype: list(TMS Orders)
:return: list of newly created TMS Orders
"""
created_tms_orders = self.env["tms.order"]

Check warning on line 94 in tms_sale/models/sale_order.py

View check run for this annotation

Codecov / codecov/patch

tms_sale/models/sale_order.py#L94

Added line #L94 was not covered by tests

for sale in self:
new_tms_orders = self._tms_generate()

Check warning on line 97 in tms_sale/models/sale_order.py

View check run for this annotation

Codecov / codecov/patch

tms_sale/models/sale_order.py#L97

Added line #L97 was not covered by tests

if len(new_tms_orders) > 0:
created_tms_orders |= new_tms_orders
sale._post_tms_message(new_tms_orders)

Check warning on line 101 in tms_sale/models/sale_order.py

View check run for this annotation

Codecov / codecov/patch

tms_sale/models/sale_order.py#L100-L101

Added lines #L100 - L101 were not covered by tests

return created_tms_orders

Check warning on line 103 in tms_sale/models/sale_order.py

View check run for this annotation

Codecov / codecov/patch

tms_sale/models/sale_order.py#L103

Added line #L103 was not covered by tests

def _post_tms_message(self, tms_orders):
"""
Post messages to the Sale Order and the newly created TMS Orders
"""
self.ensure_one()

Check warning on line 109 in tms_sale/models/sale_order.py

View check run for this annotation

Codecov / codecov/patch

tms_sale/models/sale_order.py#L109

Added line #L109 was not covered by tests
for tms_order in tms_orders:
tms_order.message_mail_with_source(

Check warning on line 111 in tms_sale/models/sale_order.py

View check run for this annotation

Codecov / codecov/patch

tms_sale/models/sale_order.py#L111

Added line #L111 was not covered by tests
"mail.message_origin_link",
render_values={"self": tms_order, "origin": self},
subtype_id=self.env.ref("mail.mt_note").id,
author_id=self.env.user.partner_id.id,
)
message = _(

Check warning on line 117 in tms_sale/models/sale_order.py

View check run for this annotation

Codecov / codecov/patch

tms_sale/models/sale_order.py#L117

Added line #L117 was not covered by tests
"Transport Order(s) Created: %s",
Markup(
f"""<a href=# data-oe-model=tms.order data-oe-id={tms_order.id}"""
f""">{tms_order.name}</a>"""
),
)
self.message_post(body=message)

Check warning on line 124 in tms_sale/models/sale_order.py

View check run for this annotation

Codecov / codecov/patch

tms_sale/models/sale_order.py#L124

Added line #L124 was not covered by tests

def _action_create_new_trips(self):
if any(
sol.product_id.trip_product_type == "trip"
for sol in self.order_line.filtered(
lambda x: x.display_type not in ("line_section", "line_note")
)
):
self._tms_generation()

Check warning on line 133 in tms_sale/models/sale_order.py

View check run for this annotation

Codecov / codecov/patch

tms_sale/models/sale_order.py#L133

Added line #L133 was not covered by tests

def action_view_tms_order(self):
tms_orders = self.mapped("tms_order_ids")
action = self.env["ir.actions.act_window"]._for_xml_id(

Check warning on line 137 in tms_sale/models/sale_order.py

View check run for this annotation

Codecov / codecov/patch

tms_sale/models/sale_order.py#L136-L137

Added lines #L136 - L137 were not covered by tests
"tms.action_tms_dash_order"
)
if len(tms_orders) > 1:
action["domain"] = [("id", "in", tms_orders.ids)]

Check warning on line 141 in tms_sale/models/sale_order.py

View check run for this annotation

Codecov / codecov/patch

tms_sale/models/sale_order.py#L141

Added line #L141 was not covered by tests
elif len(tms_orders) == 1:
action["views"] = [(self.env.ref("tms.tms_order_view_form").id, "form")]
action["res_id"] = tms_orders.id

Check warning on line 144 in tms_sale/models/sale_order.py

View check run for this annotation

Codecov / codecov/patch

tms_sale/models/sale_order.py#L143-L144

Added lines #L143 - L144 were not covered by tests
else:
action = {"type": "ir.actions.act_window_close"}
return action

Check warning on line 147 in tms_sale/models/sale_order.py

View check run for this annotation

Codecov / codecov/patch

tms_sale/models/sale_order.py#L146-L147

Added lines #L146 - L147 were not covered by tests

def remove_lines_with_trips(
self, initial_trips, initial_line_ids, initial_quantities
):
current_order_line_ids = set(self.order_line.ids)
removed_lines = initial_line_ids - current_order_line_ids

Check warning on line 153 in tms_sale/models/sale_order.py

View check run for this annotation

Codecov / codecov/patch

tms_sale/models/sale_order.py#L152-L153

Added lines #L152 - L153 were not covered by tests

if removed_lines:
trips_to_delete = initial_trips.filtered(
lambda trip_id: not (trip_id.sale_line_id & self.order_line)
)
if trips_to_delete:
trips_to_delete.unlink()

Check warning on line 160 in tms_sale/models/sale_order.py

View check run for this annotation

Codecov / codecov/patch

tms_sale/models/sale_order.py#L160

Added line #L160 was not covered by tests

for line in self.order_line:
if line.id in initial_quantities:
if line.product_uom_qty < initial_quantities[line.id]:
trips_to_delete = line.tms_order_ids[:1]

Check warning on line 165 in tms_sale/models/sale_order.py

View check run for this annotation

Codecov / codecov/patch

tms_sale/models/sale_order.py#L165

Added line #L165 was not covered by tests
if trips_to_delete:
trips_to_delete.unlink()

Check warning on line 167 in tms_sale/models/sale_order.py

View check run for this annotation

Codecov / codecov/patch

tms_sale/models/sale_order.py#L167

Added line #L167 was not covered by tests

@api.model
def create(self, vals):
order = super().create(vals)

Check warning on line 171 in tms_sale/models/sale_order.py

View check run for this annotation

Codecov / codecov/patch

tms_sale/models/sale_order.py#L171

Added line #L171 was not covered by tests
if "order_line" in vals and order.has_tms_order:
order._action_create_new_trips()
return order

Check warning on line 174 in tms_sale/models/sale_order.py

View check run for this annotation

Codecov / codecov/patch

tms_sale/models/sale_order.py#L173-L174

Added lines #L173 - L174 were not covered by tests

@api.model
def write(self, vals):
initial_trips = self.env["tms.order"].search(

Check warning on line 178 in tms_sale/models/sale_order.py

View check run for this annotation

Codecov / codecov/patch

tms_sale/models/sale_order.py#L178

Added line #L178 was not covered by tests
[("sale_line_id", "in", self.order_line.ids)]
)
initial_order_line_ids = set(self.order_line.ids)

Check warning on line 181 in tms_sale/models/sale_order.py

View check run for this annotation

Codecov / codecov/patch

tms_sale/models/sale_order.py#L181

Added line #L181 was not covered by tests
initial_quantities = {line.id: line.product_uom_qty for line in self.order_line}

result = super().write(vals)

Check warning on line 184 in tms_sale/models/sale_order.py

View check run for this annotation

Codecov / codecov/patch

tms_sale/models/sale_order.py#L184

Added line #L184 was not covered by tests

if "order_line" in vals and self.has_tms_order:
self.remove_lines_with_trips(

Check warning on line 187 in tms_sale/models/sale_order.py

View check run for this annotation

Codecov / codecov/patch

tms_sale/models/sale_order.py#L187

Added line #L187 was not covered by tests
initial_trips, initial_order_line_ids, initial_quantities
)
self._action_create_new_trips()

Check warning on line 190 in tms_sale/models/sale_order.py

View check run for this annotation

Codecov / codecov/patch

tms_sale/models/sale_order.py#L190

Added line #L190 was not covered by tests

if "state" in vals:
if self.state == "sale":
stage = self.env.ref("tms.tms_stage_order_confirmed")

Check warning on line 194 in tms_sale/models/sale_order.py

View check run for this annotation

Codecov / codecov/patch

tms_sale/models/sale_order.py#L194

Added line #L194 was not covered by tests
elif self.state == "cancel":
stage = self.env.ref("tms.tms_stage_order_cancelled")

Check warning on line 196 in tms_sale/models/sale_order.py

View check run for this annotation

Codecov / codecov/patch

tms_sale/models/sale_order.py#L196

Added line #L196 was not covered by tests
else:
stage = self.env.ref("tms.tms_stage_order_draft")

Check warning on line 198 in tms_sale/models/sale_order.py

View check run for this annotation

Codecov / codecov/patch

tms_sale/models/sale_order.py#L198

Added line #L198 was not covered by tests

for line in self.order_line:
for trip in line.tms_order_ids:
trip.stage_id = stage

Check warning on line 202 in tms_sale/models/sale_order.py

View check run for this annotation

Codecov / codecov/patch

tms_sale/models/sale_order.py#L202

Added line #L202 was not covered by tests

return result

Check warning on line 204 in tms_sale/models/sale_order.py

View check run for this annotation

Codecov / codecov/patch

tms_sale/models/sale_order.py#L204

Added line #L204 was not covered by tests
Loading

0 comments on commit b30ae9d

Please sign in to comment.