Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[14.0] edi_purchase: use edi.configuration + add edi_purchase_ubl_output_oca #1098

Draft
wants to merge 2 commits into
base: 14.0
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions edi_purchase_oca/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from . import models
from . import components
15 changes: 12 additions & 3 deletions edi_purchase_oca/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,18 @@
Define EDI Configuration for Purchase Orders""",
"version": "14.0.1.0.0",
"license": "LGPL-3",
"author": "ForgeFlow, Odoo Community Association (OCA)",
"author": "ForgeFlow, Camptocamp, Odoo Community Association (OCA)",
"website": "https://github.com/OCA/edi",
"depends": ["purchase", "edi_oca", "component_event"],
"data": ["views/purchase_order_views.xml", "views/edi_exchange_record_views.xml"],
"demo": [],
"data": [
"views/purchase_order_views.xml",
"views/edi_exchange_record_views.xml",
"views/res_partner_view.xml",
"data/edi_configuration.xml",
],
"demo": [
"demo/edi_backend.xml",
"demo/edi_exchange_type.xml",
"demo/edi_configuration.xml",
],
}
1 change: 1 addition & 0 deletions edi_purchase_oca/components/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import listeners
27 changes: 27 additions & 0 deletions edi_purchase_oca/components/listeners.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Copyright 2024 Camptocamp SA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo.addons.component.core import Component


class EDIConfigPOListener(Component):
_name = "edi.listener.config.purchase.order"
_inherit = "base.event.listener"
_apply_on = ["purchase.order"]

def on_record_create(self, record, fields=None):
trigger = "on_record_create"
return self._exec_conf(record, trigger)

def on_record_write(self, record, fields=None):
trigger = "on_record_write"
return self._exec_conf(record, trigger)

def on_edi_purchase_order_state_change(self, record, state=None):
trigger = "on_edi_purchase_order_state_change"
return self._exec_conf(record, trigger)

def _exec_conf(self, record, trigger, conf_field="edi_purchase_conf_ids"):
confs = record.partner_id[conf_field].edi_get_conf(trigger)
for conf in confs:
conf.edi_exec_snippet_do(record)
13 changes: 13 additions & 0 deletions edi_purchase_oca/data/edi_configuration.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<!-- Generic state change -->
<record
id="edi_conf_trigger_purchase_order_state_change"
model="edi.configuration.trigger"
>
<field name="name">On PO state change</field>
<field name="code">on_edi_purchase_order_state_change</field>
<field name="description">Trigger when a purchase order state changes</field>
<field name="model_id" ref="purchase.model_purchase_order" />
</record>
</odoo>
11 changes: 11 additions & 0 deletions edi_purchase_oca/demo/edi_backend.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<record id="demo_edi_backend_type_purchase" model="edi.backend.type">
<field name="name">Purchase DEMO</field>
<field name="code">purchase_demo</field>
</record>
<record id="demo_edi_backend" model="edi.backend">
<field name="name">purchase DEMO</field>
<field name="backend_type_id" ref="demo_edi_backend_type_purchase" />
</record>
</odoo>
23 changes: 23 additions & 0 deletions edi_purchase_oca/demo/edi_configuration.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8" ?>
<odoo>
<record id="demo_edi_configuration_confirmed" model="edi.configuration">
<field name="name">Demo PO send</field>
<field
name="description"
>Show case how you can send out an order automatically</field>
<field name="type_id" ref="demo_edi_exc_type_order_out" />
<field name="backend_id" ref="demo_edi_backend" />
<field name="model_id" ref="purchase.model_purchase_order" />
<field name="trigger_id" ref="edi_conf_trigger_purchase_order_state_change" />
<field name="snippet_do">
# ('draft', 'RFQ'),
# ('sent', 'RFQ Sent'),
# ('to approve', 'To Approve'),
# ('purchase', 'Purchase Order'),
# ('done', 'Locked'),
# ('cancel', 'Cancelled')
if record.state == 'purchase':
record._edi_send_via_edi(conf.type_id)
</field>
</record>
</odoo>
14 changes: 14 additions & 0 deletions edi_purchase_oca/demo/edi_exchange_type.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8" ?>
<odoo>

<record id="demo_edi_exc_type_order_out" model="edi.exchange.type">
<field name="backend_type_id" ref="demo_edi_backend_type_purchase" />
<field name="backend_id" ref="demo_edi_backend" />
<field name="name">Demo Purchase Order out</field>
<field name="code">demo_PurchaseOrder_out</field>
<field name="direction">output</field>
<field name="exchange_filename_pattern">{record_name}-{type.code}-{dt}</field>
<field name="exchange_file_ext">xml</field>
</record>

</odoo>
1 change: 1 addition & 0 deletions edi_purchase_oca/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from . import purchase_order
from . import res_partner
15 changes: 11 additions & 4 deletions edi_purchase_oca/models/purchase_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,23 @@

class PurchaseOrder(models.Model):
_name = "purchase.order"
_inherit = ["purchase.order", "edi.exchange.consumer.mixin"]
_inherit = [
"purchase.order",
"edi.exchange.consumer.mixin",
]

def button_confirm(self):
result = super().button_confirm()
if self:
res = super().button_confirm()
# DEPRECATED: rely on `on_edi_purchase_order_state_change`
# provided automatically by edi.consumer.mixin
if self: # TODO: is this check necessary?
self._event("on_button_confirm_purchase_order").notify(self)
return result
return res

def button_cancel(self):
result = super().button_cancel()
# DEPRECATED: rely on `on_edi_purchase_order_state_change`
# provided automatically by edi.consumer.mixin
if self:
self._event("on_button_cancel_purchase_order").notify(self)
return result
18 changes: 18 additions & 0 deletions edi_purchase_oca/models/res_partner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright 2024 Camptocamp SA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).


from odoo import fields, models


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

edi_purchase_conf_ids = fields.Many2many(
string="EDI purchase configuration",
comodel_name="edi.configuration",
relation="res_partner_edi_purchase_configuration_rel",
column1="partner_id",
column2="conf_id",
domain=[("model_name", "=", "purchase.order")],
)
1 change: 1 addition & 0 deletions edi_purchase_oca/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
* Lois Rilo <[email protected]>
* Simone Orsi <[email protected]>
9 changes: 9 additions & 0 deletions edi_purchase_oca/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
This module intends to create a base to be extended by local edi rules
for purchase.


Configuration
~~~~~~~~~~~~~

TODO

Deprecated mode
~~~~~~~~~~~~~~~

In order to add a new integration, you need to create a listener:

.. code-block:: python
Expand Down
1 change: 1 addition & 0 deletions edi_purchase_oca/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import test_edi_conf
53 changes: 53 additions & 0 deletions edi_purchase_oca/tests/test_edi_conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Copyright 2024 CamptoCamp SA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from unittest import mock

from odoo.addons.edi_oca.tests.common import EDIBackendCommonComponentTestCase


class TestsPurchaseEDIConfiguration(EDIBackendCommonComponentTestCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.purchase_order = cls.env["purchase.order"]
cls.product = cls.env["product.product"].create(
{
"name": "Product 1",
"default_code": "1234567",
}
)
cls.exc_type_out = cls.env.ref("edi_purchase_oca.demo_edi_exc_type_order_out")
cls.edi_conf = cls.env.ref("edi_purchase_oca.demo_edi_configuration_confirmed")
cls.partner.edi_purchase_conf_ids = cls.edi_conf

@mock.patch("odoo.addons.edi_oca.models.edi_backend.EDIBackend._validate_data")
@mock.patch("odoo.addons.edi_oca.models.edi_backend.EDIBackend._exchange_generate")
@mock.patch("odoo.addons.edi_oca.models.edi_backend.EDIBackend._exchange_send")
def test_order_confirm(self, mock_send, mock_generate, mock_validate):
mock_generate.return_value = "TEST PO OUT"
order = self.purchase_order.create(
{
"partner_id": self.partner.id,
"order_line": [
(
0,
0,
{
"product_id": self.product.id,
"product_qty": 10,
"price_unit": 100.0,
},
)
],
}
)
self.assertEqual(order.state, "draft")
self.assertEqual(len(order.exchange_record_ids), 0)
order.button_confirm()
self.assertEqual(order.state, "purchase")
self.assertEqual(len(order.exchange_record_ids), 1)
self.assertEqual(order.exchange_record_ids[0].type_id, self.exc_type_out)
self.assertEqual(
order.exchange_record_ids[0]._get_file_content(), "TEST PO OUT"
)
22 changes: 22 additions & 0 deletions edi_purchase_oca/views/res_partner_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<record id="view_partner_form" model="ir.ui.view">
<field name="name">res.partner.edi.purchase</field>
<field name="model">res.partner</field>
<field name="inherit_id" ref="edi_oca.view_partner_form" />
<field name="arch" type="xml">
<group name="edi_main" position="inside">
<group name="purchase" string="Purchase">
<field name="edi_purchase_conf_ids" nolabel="1">
<tree>
<field name="name" />
<field name="trigger_id" />
<field name="type_id" />
<field name="model_id" />
</tree>
</field>
</group>
</group>
</field>
</record>
</odoo>
1 change: 1 addition & 0 deletions edi_purchase_ubl_output_oca/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
wait for the bot
1 change: 1 addition & 0 deletions edi_purchase_ubl_output_oca/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import components
18 changes: 18 additions & 0 deletions edi_purchase_ubl_output_oca/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright 2021 Camptocamp SA
# @author: Simone Orsi <[email protected]>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

{
"name": "EDI UBL Purchase",
"summary": """Handle outbound exchanges for purchases.""",
"version": "14.0.1.0.0",
"development_status": "Alpha",
"license": "AGPL-3",
"website": "https://github.com/OCA/edi",
"author": "Camptocamp, Odoo Community Association (OCA)",
"maintainers": ["simahawk"],
"depends": ["edi_purchase_oca", "edi_ubl_oca", "purchase_order_ubl"],
"demo": [
"demo/edi_exchange_type.xml",
],
}
1 change: 1 addition & 0 deletions edi_purchase_ubl_output_oca/components/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import generate
27 changes: 27 additions & 0 deletions edi_purchase_ubl_output_oca/components/generate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Copyright 2021 Camptocamp SA
# @author: Simone Orsi <[email protected]>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).


from odoo.addons.component.core import Component


class EDIExchangePOGenerate(Component):
"""Generate purchase orders."""

_name = "edi.output.ubl.purchase.order"
_inherit = "edi.component.output.mixin"
_usage = "output.generate.purchase.order"

def generate(self):
return self._generate_ubl_xml()

Check warning on line 17 in edi_purchase_ubl_output_oca/components/generate.py

View check run for this annotation

Codecov / codecov/patch

edi_purchase_ubl_output_oca/components/generate.py#L17

Added line #L17 was not covered by tests

# TODO: add tests
def _generate_ubl_xml(self):
order = self.record
doc_type = order.get_ubl_purchase_order_doc_type()

Check warning on line 22 in edi_purchase_ubl_output_oca/components/generate.py

View check run for this annotation

Codecov / codecov/patch

edi_purchase_ubl_output_oca/components/generate.py#L21-L22

Added lines #L21 - L22 were not covered by tests
if not doc_type:
raise NotImplementedError("TODO: handle no doc type")
version = order.get_ubl_version()
xml_string = order.generate_ubl_xml_string(doc_type, version=version)
return xml_string

Check warning on line 27 in edi_purchase_ubl_output_oca/components/generate.py

View check run for this annotation

Codecov / codecov/patch

edi_purchase_ubl_output_oca/components/generate.py#L24-L27

Added lines #L24 - L27 were not covered by tests
20 changes: 20 additions & 0 deletions edi_purchase_ubl_output_oca/demo/edi_exchange_type.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8" ?>
<odoo>
<record id="demo_edi_exc_type_order_out" model="edi.exchange.type">
<field name="backend_type_id" ref="edi_ubl_oca.edi_backend_type_ubl" />
<field name="name">Demo UBL PO out</field>
<field name="code">demo_UBL_PO_out</field>
<field name="direction">output</field>
<field name="exchange_filename_pattern">{record_name}-{type.code}-{dt}</field>
<field name="exchange_file_ext">xml</field>
<field
name="model_ids"
eval="[(6, 0, [ref('purchase.model_purchase_order')])]"
/>
<field name="advanced_settings_edit">
components:
generate:
usage: output.generate.purchase.order
</field>
</record>
</odoo>
7 changes: 7 additions & 0 deletions edi_purchase_ubl_output_oca/readme/CONFIGURATION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
On your exchange type configured for UBL outbound exchanges use this conf in `advanced_settings_edit`::

components:
generate:
usage: output.generate.purchase.order

That's it.
1 change: 1 addition & 0 deletions edi_purchase_ubl_output_oca/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Simone Orsi <[email protected]>
5 changes: 5 additions & 0 deletions edi_purchase_ubl_output_oca/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Handle purchase exchanges with the EDI framework.

This module is mostly a glue module for `purchase_order_ubl` with `edi_oca`.

Allows you to generate and send purchase orders as UBL XML files.
6 changes: 6 additions & 0 deletions setup/edi_purchase_ubl_output_oca/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import setuptools

setuptools.setup(
setup_requires=['setuptools-odoo'],
odoo_addon=True,
)
Loading