Skip to content

Commit

Permalink
[ADD] purchase_transport_mode
Browse files Browse the repository at this point in the history
  • Loading branch information
santostelmo committed Nov 6, 2023
1 parent e7e5fc2 commit 029e7fb
Show file tree
Hide file tree
Showing 26 changed files with 1,009 additions and 0 deletions.
92 changes: 92 additions & 0 deletions purchase_transport_mode/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
=======================
Purchase Transport Mode
=======================

..
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:de72b94c90dd1d08449772428e9d13b2683f202b7831109f787ab7c77da09d45
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
:target: https://odoo-community.org/page/development-status
:alt: Beta
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fpurchase--workflow-lightgray.png?logo=github
:target: https://github.com/OCA/purchase-workflow/tree/16.0/purchase_transport_mode
:alt: OCA/purchase-workflow
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/purchase-workflow-16-0/purchase-workflow-16-0-purchase_transport_mode
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
:target: https://runboat.odoo-community.org/builds?repo=OCA/purchase-workflow&target_branch=16.0
:alt: Try me on Runboat

|badge1| |badge2| |badge3| |badge4| |badge5|

Purchases can be received by different modes of transport. For example, the transport can be done in a truck with a loading capacity of X tonnes or Y tonnes.
With this module it is possible to validate that the purchase complies with the requirements of a particular mode of transport.

**Table of contents**

.. contents::
:local:

Usage
=====

To use this module, you need to:

#. In Purchase settings enable purchase transport mode validation
#. Create a transport modes with contraints applied on PO
#. Select transport mode on PO

If the transport requirements are not met, a message in yellow will be displayed at the top of the form.

Bug Tracker
===========

Bugs are tracked on `GitHub Issues <https://github.com/OCA/purchase-workflow/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us to smash it by providing a detailed and welcomed
`feedback <https://github.com/OCA/purchase-workflow/issues/new?body=module:%20purchase_transport_mode%0Aversion:%2016.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

Do not contact contributors directly about support or help with technical issues.

Credits
=======

Authors
~~~~~~~

* Camptocamp
* BCIM

Contributors
~~~~~~~~~~~~

* Telmo Santos <[email protected]>
* Cyril Jeanneret <[email protected]>
* Jacques-Etienne Baudoux (BCIM) <[email protected]>
* Simone Orsi <[email protected]>

Maintainers
~~~~~~~~~~~

This module is maintained by the OCA.

.. image:: https://odoo-community.org/logo.png
:alt: Odoo Community Association
:target: https://odoo-community.org

OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.

This module is part of the `OCA/purchase-workflow <https://github.com/OCA/purchase-workflow/tree/16.0/purchase_transport_mode>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
1 change: 1 addition & 0 deletions purchase_transport_mode/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
27 changes: 27 additions & 0 deletions purchase_transport_mode/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Copyright 2023 Camptocamp (<https://www.camptocamp.com>).
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
{
"name": "Purchase Transport Mode",
"version": "16.0.1.0.0",
"development_status": "Beta",
"summary": "Purchase expection based on constraints",
"author": "Camptocamp, BCIM, Odoo Community Association (OCA)",
"license": "AGPL-3",
"category": "Purchase",
"depends": [
"purchase",
"purchase_exception",
],
"website": "https://github.com/OCA/purchase-workflow",
"data": [
"security/ir.model.access.csv",
"templates/purchase_order_transport_mode_status.xml",
"views/res_partner_views.xml",
"views/purchase_transport_mode_views.xml",
"views/purchase_transport_mode_constraint_views.xml",
"views/purchase_order_view.xml",
"views/res_config_settings_views.xml",
"data/purchase_exception_data.xml",
],
"installable": True,
}
11 changes: 11 additions & 0 deletions purchase_transport_mode/data/purchase_exception_data.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo noupdate="1">
<record id="po_excep_transport_mode" model="exception.rule">
<field name="name">Transport mode</field>
<field name="description">Transport mode requirements not satisfied</field>
<field name="sequence">50</field>
<field name="model">purchase.order</field>
<field name="code">if not self.transport_mode_status_ok:
failed=True</field>
</record>
</odoo>
6 changes: 6 additions & 0 deletions purchase_transport_mode/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from . import purchase_transport_mode
from . import purchase_transport_mode_constraint
from . import res_partner
from . import purchase_order
from . import res_company
from . import res_config_settings
70 changes: 70 additions & 0 deletions purchase_transport_mode/models/purchase_order.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Copyright 2023 Camptocamp (<https://www.camptocamp.com>).
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).


from odoo import api, fields, models

from odoo.addons.base_sparse_field.models.fields import Serialized


class PurchaseOrder(models.Model):
_inherit = "purchase.order"
_name = "purchase.order"

transport_mode_id = fields.Many2one("purchase.transport.mode")
transport_mode_status = Serialized(
compute="_compute_transport_mode_validation_status",
help="Collect and validate order details to satisfy transport mode requirements",
default={},
)
transport_mode_status_display = fields.Html(
compute="_compute_transport_mode_validation_status",
help="Render transport_mode_status in the UI",
)
transport_mode_status_ok = fields.Boolean(
compute="_compute_transport_mode_validation_status",
help="All transport mode requirements are satisfied",
)

@api.depends("transport_mode_id")
def _compute_transport_mode_validation_status(self):
for rec in self:
rec.transport_mode_status = rec._get_transport_mode_validation_status()
rec.transport_mode_status_display = (
rec._get_transport_mode_validation_status_display()
)
rec.transport_mode_status_ok = False if rec.transport_mode_status else True

@api.onchange("partner_id")
def onchange_partner_id(self):
if self.partner_id:
self.transport_mode_id = (
self.partner_id.commercial_partner_id.purchase_transport_mode_id
)

def _get_transport_mode_validation_status(self):
self.ensure_one()
errors = []
if (
not self.company_id.purchase_transport_mode_contraint_enabled
or not isinstance(self.id, int) # Record is not saved yet
):
return {}
for constraint in self.transport_mode_id.constraint_ids:
if not constraint.filter_valid_purchase(self):
error_message = "{}: {}".format(
constraint.name, constraint.description or ""
)
errors.append(error_message)
if errors:
return {"errors": errors}
return {}

def _get_transport_mode_validation_status_display(self):
errors = self.transport_mode_status.get("errors", [])
if errors:
return self.env["ir.qweb"]._render(
"purchase_transport_mode.purchase_order_transport_mode_status_display",
{"order": self, "errors": errors},
)
return ""
18 changes: 18 additions & 0 deletions purchase_transport_mode/models/purchase_transport_mode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright 2023 Camptocamp (<https://www.camptocamp.com>).
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).


from odoo import fields, models


class PurchaseTransportMode(models.Model):
_name = "purchase.transport.mode"
_description = "Transport Mode"

name = fields.Char(required=True)

constraint_ids = fields.One2many(
comodel_name="purchase.transport.mode.constraint",
inverse_name="purchase_transport_mode_id",
string="Purchase Transport mode Constraints",
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Copyright 2023 Camptocamp (<https://www.camptocamp.com>).
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).


from odoo import fields, models
from odoo.tools.safe_eval import safe_eval


class PurchaseTransportModeConstraints(models.Model):
_name = "purchase.transport.mode.constraint"
_description = "Transport Mode Constraint"

name = fields.Char(required=True)
description = fields.Char()
purchase_domain = fields.Char(
string="Source Purchase Domain",
default=[],
copy=False,
help="Domain based on purchase",
)

purchase_transport_mode_id = fields.Many2one("purchase.transport.mode")

def filter_valid_purchase(self, purchase):
if not self.purchase_domain:
return purchase
domain = safe_eval(self.purchase_domain or "[]")
if not domain:
return purchase
return purchase.filtered_domain(domain)
10 changes: 10 additions & 0 deletions purchase_transport_mode/models/res_company.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright 2023 Camptocamp (<https://www.camptocamp.com>).
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from odoo import fields, models


class ResCompany(models.Model):
_inherit = "res.company"

purchase_transport_mode_contraint_enabled = fields.Boolean()
13 changes: 13 additions & 0 deletions purchase_transport_mode/models/res_config_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright 2023 Camptocamp (<https://www.camptocamp.com>).
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from odoo import fields, models


class ResConfigSettings(models.TransientModel):
_inherit = "res.config.settings"

purchase_transport_mode_contraint_enabled = fields.Boolean(
related="company_id.purchase_transport_mode_contraint_enabled",
readonly=False,
string="Validate purchase transport mode",
)
10 changes: 10 additions & 0 deletions purchase_transport_mode/models/res_partner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright 2023 Camptocamp (<https://www.camptocamp.com>).
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from odoo import fields, models


class ResPartner(models.Model):
_inherit = "res.partner"

purchase_transport_mode_id = fields.Many2one("purchase.transport.mode")
4 changes: 4 additions & 0 deletions purchase_transport_mode/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
* Telmo Santos <[email protected]>
* Cyril Jeanneret <[email protected]>
* Jacques-Etienne Baudoux (BCIM) <[email protected]>
* Simone Orsi <[email protected]>
2 changes: 2 additions & 0 deletions purchase_transport_mode/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Purchases can be received by different modes of transport. For example, the transport can be done in a truck with a loading capacity of X tonnes or Y tonnes.
With this module it is possible to validate that the purchase complies with the requirements of a particular mode of transport.
7 changes: 7 additions & 0 deletions purchase_transport_mode/readme/USAGE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
To use this module, you need to:

#. In Purchase settings enable purchase transport mode validation
#. Create a transport modes with contraints applied on PO
#. Select transport mode on PO

If the transport requirements are not met, a message in yellow will be displayed at the top of the form.
3 changes: 3 additions & 0 deletions purchase_transport_mode/security/ir.model.access.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
access_purchase_transport_mode,access_purchase_transport_mode,model_purchase_transport_mode,purchase.group_purchase_user,1,1,1,1
access_purchase_transport_mode_constraint,access_purchase_transport_mode_constraint,model_purchase_transport_mode_constraint,purchase.group_purchase_user,1,1,1,1
Loading

0 comments on commit 029e7fb

Please sign in to comment.