Skip to content

Commit

Permalink
[UPD] edi_purchase_oca: Update for edi configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
thienvh332 committed Sep 17, 2024
1 parent 17a9bd6 commit 17407ad
Show file tree
Hide file tree
Showing 12 changed files with 170 additions and 15 deletions.
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
7 changes: 6 additions & 1 deletion edi_purchase_oca/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
"author": "ForgeFlow, 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"],
"data": [
"views/purchase_order_views.xml",
"views/edi_exchange_record_views.xml",
"views/res_partner_view.xml",
"data/edi_configuration.xml",
],
"demo": [],
}
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 listener_purchase_order
41 changes: 41 additions & 0 deletions edi_purchase_oca/components/listener_purchase_order.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Copyright 2024 Camptocamp SA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo.addons.component.core import Component


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

def on_button_confirm_purchase_order(self, record):
trigger = "on_button_confirm_purchase_order"

Check warning on line 13 in edi_purchase_oca/components/listener_purchase_order.py

View check run for this annotation

Codecov / codecov/patch

edi_purchase_oca/components/listener_purchase_order.py#L13

Added line #L13 was not covered by tests
for rec in record:
confs = self.env["edi.configuration"].edi_get_conf(

Check warning on line 15 in edi_purchase_oca/components/listener_purchase_order.py

View check run for this annotation

Codecov / codecov/patch

edi_purchase_oca/components/listener_purchase_order.py#L15

Added line #L15 was not covered by tests
trigger, rec._name, rec.partner_id
)
for conf in confs:
conf.edi_exec_snippet_do(rec)

Check warning on line 19 in edi_purchase_oca/components/listener_purchase_order.py

View check run for this annotation

Codecov / codecov/patch

edi_purchase_oca/components/listener_purchase_order.py#L19

Added line #L19 was not covered by tests

def on_record_write(self, record, fields=None, **kwargs):
trigger = "on_record_write"
if kwargs.get("vals", False):
for rec in record:
confs = self.env["edi.configuration"].edi_get_conf(
trigger, rec._name, rec.partner_id
)
for conf in confs:
conf.edi_exec_snippet_do(rec, **kwargs)

Check warning on line 29 in edi_purchase_oca/components/listener_purchase_order.py

View check run for this annotation

Codecov / codecov/patch

edi_purchase_oca/components/listener_purchase_order.py#L29

Added line #L29 was not covered by tests

def on_record_create(self, record, fields=None, **kwargs):
trigger = "on_record_create"
val_list = kwargs.get("vals", False)
if val_list:
for rec, vals in zip(record, val_list):
kwargs["vals"] = {rec.id: vals}
confs = self.env["edi.configuration"].edi_get_conf(
trigger, rec._name, rec.partner_id
)
for conf in confs:
conf.edi_exec_snippet_do(rec, **kwargs)
8 changes: 4 additions & 4 deletions edi_purchase_oca/data/edi_configuration.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
<field name="trigger">on_button_confirm_purchase_order</field>
<field name="model" ref="purchase.model_purchase_order" />
<field name="snippet_before_do">
result={
"snippet_var_do": {}
}
result={
"snippet_var_do": {}
}
</field>
<field name="snippet_do" />
</record>
<record id="edi_conf_send_via_email" model="edi.configuration">
<field name="name">Send EDI Quoctation Config</field>
<field name="name">Send EDI Quotation Config</field>
<field name="code">send_via_email_rfq</field>
<field name="trigger">send_via_email_rfq</field>
<field
Expand Down
2 changes: 2 additions & 0 deletions edi_purchase_oca/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
from . import purchase_order
from . import res_partner
from . import edi_configuration
15 changes: 15 additions & 0 deletions edi_purchase_oca/models/edi_configuration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright 2024 Camptocamp SA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import fields, models


class EdiConfiguration(models.Model):
_inherit = "edi.configuration"

trigger = fields.Selection(
selection_add=[
("on_button_confirm_purchase_order", "On Button Confirm Purchase Order"),
("send_via_email_rfq", "Send via Email RFQ"),
],
)
13 changes: 8 additions & 5 deletions edi_purchase_oca/models/purchase_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@

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

def button_confirm(self):
result = super().button_confirm()
if self:
self._event("on_button_confirm_purchase_order").notify(self)
return result
res = super().button_confirm()
self._event("on_button_confirm_purchase_order").notify(self)
return res

Check warning on line 18 in edi_purchase_oca/models/purchase_order.py

View check run for this annotation

Codecov / codecov/patch

edi_purchase_oca/models/purchase_order.py#L16-L18

Added lines #L16 - L18 were not covered by tests

def button_cancel(self):
result = super().button_cancel()
Expand Down
3 changes: 1 addition & 2 deletions edi_purchase_oca/models/res_partner.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,5 @@ class ResPartner(models.Model):
relation="res_partner_edi_configuration_rel",
column1="partner_id",
column2="conf_id",
# TODO: Domain for Purchase model
domain="[('model_name', '=', 'purchase.order')]"
domain="[('model_name', '=', 'purchase.order')]",
)
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_configuration
85 changes: 85 additions & 0 deletions edi_purchase_oca/tests/test_edi_configuration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# Copyright 2024 CamptoCamp SA,
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

import mock

from odoo.exceptions import UserError

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


class TestsPurchaseEDIConfiguration(EDIBackendCommonComponentRegistryTestCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls._load_module_components(cls, "edi_purchase_oca")
cls.edi_configuration = cls.env["edi.configuration"]
cls.purchase_order = cls.env["purchase.order"]
cls.product = cls.env["product.product"].create(
{
"name": "Product 1",
"default_code": "1234567",
}
)

def setUp(self):
super().setUp()
self.create_config = self.edi_configuration.create(
{
"name": "Create Config",
"active": True,
"code": "create_config",
"backend_id": self.backend.id,
"type_id": self.exchange_type_out.id,
"trigger": "on_record_create",
"model": self.env["ir.model"]._get_id("purchase.order"),
}
)

def test_edi_configuration_snippet_before_do(self):
self.create_config.snippet_before_do = "record.button_cancel()"
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.assertTrue(order)
self.assertEqual(order.state, "cancel")

def test_edi_configuration_snippet_do(self):
self.create_config.snippet_do = "record._edi_send_via_edi(conf.type_id)"
with mock.patch.object(
type(self.backend), "exchange_generate", return_value=True
):

with self.assertRaises(UserError) as err:
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.assertRegex(
err.exception.args[0], r"Record ID=\d+ has no file to send!"
)
8 changes: 5 additions & 3 deletions edi_purchase_oca/views/res_partner_view.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<record id="view_partner_form_inherit" model="ir.ui.view">
<field name="name">res.partner.form.inherit.sales_purchases</field>
Expand All @@ -7,8 +7,10 @@
<field name="arch" type="xml">
<xpath expr="//page[@name='sales_purchases']" position="inside">
<group string="Edi Configuration">
<field name="edi_purchase_conf_ids"
context="{'default_model_name': 'purchase.order', 'default_partner_ids': active_ids}" />
<field
name="edi_purchase_conf_ids"
context="{'default_model_name': 'purchase.order', 'default_partner_ids': active_ids}"
/>
</group>
</xpath>
</field>
Expand Down

0 comments on commit 17407ad

Please sign in to comment.