Skip to content

Commit 497f4c4

Browse files
committed
[UPD] edi_purchase_oca: Update for edi configuration
1 parent 30c316c commit 497f4c4

12 files changed

+157
-18
lines changed

edi_purchase_oca/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
from . import models
2+
from . import components

edi_purchase_oca/__manifest__.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
"author": "ForgeFlow, Odoo Community Association (OCA)",
1111
"website": "https://github.com/OCA/edi",
1212
"depends": ["purchase", "edi_oca", "component_event"],
13-
"data": ["views/purchase_order_views.xml", "views/edi_exchange_record_views.xml"],
13+
"data": [
14+
"views/purchase_order_views.xml",
15+
"views/edi_exchange_record_views.xml",
16+
"views/res_partner_view.xml",
17+
"data/edi_configuration.xml",
18+
],
1419
"demo": [],
1520
}
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from . import listener_purchase_order
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Copyright 2024 Camptocamp SA
2+
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
3+
4+
from odoo.addons.component.core import Component
5+
6+
7+
class EDIConfigPurchaseListener(Component):
8+
_name = "edi.listener.config.purchase.order"
9+
_inherit = "base.event.listener"
10+
_apply_on = ["purchase.order"]
11+
12+
def on_button_confirm_purchase_order(self, record):
13+
trigger = "on_button_confirm_purchase_order"
14+
confs = record.mapped("partner_id.edi_purchase_conf_ids").edi_get_conf(trigger)
15+
for conf in confs:
16+
conf.edi_exec_snippet_do(record)

edi_purchase_oca/data/edi_configuration.xml

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
<field name="trigger">on_button_confirm_purchase_order</field>
77
<field name="model" ref="purchase.model_purchase_order" />
88
<field name="snippet_before_do">
9-
result={
10-
"snippet_var_do": {}
11-
}
9+
result={
10+
"snippet_var_do": {}
11+
}
1212
</field>
1313
<field name="snippet_do" />
1414
</record>
1515
<record id="edi_conf_send_via_email" model="edi.configuration">
16-
<field name="name">Send EDI Quoctation Config</field>
16+
<field name="name">Send EDI Quotation Config</field>
1717
<field name="code">send_via_email_rfq</field>
1818
<field name="trigger">send_via_email_rfq</field>
1919
<field

edi_purchase_oca/models/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
from . import purchase_order
2+
from . import res_partner
3+
from . import edi_configuration
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Copyright 2024 Camptocamp SA
2+
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
3+
4+
from odoo import fields, models
5+
6+
7+
class EdiConfiguration(models.Model):
8+
_inherit = "edi.configuration"
9+
10+
trigger = fields.Selection(
11+
selection_add=[
12+
("on_button_confirm_purchase_order", "On Button Confirm Purchase Order"),
13+
("send_via_email_rfq", "Send via Email RFQ"),
14+
],
15+
)

edi_purchase_oca/models/purchase_order.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66

77
class PurchaseOrder(models.Model):
88
_name = "purchase.order"
9-
_inherit = ["purchase.order", "edi.exchange.consumer.mixin"]
9+
_inherit = [
10+
"purchase.order",
11+
"edi.exchange.consumer.mixin",
12+
]
1013

1114
def button_confirm(self):
12-
result = super().button_confirm()
13-
if self:
14-
self._event("on_button_confirm_purchase_order").notify(self)
15-
return result
15+
res = super().button_confirm()
16+
self._event("on_button_confirm_purchase_order").notify(self)
17+
return res
1618

1719
def button_cancel(self):
1820
result = super().button_cancel()

edi_purchase_oca/models/res_partner.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,5 @@ class ResPartner(models.Model):
1414
relation="res_partner_edi_configuration_rel",
1515
column1="partner_id",
1616
column2="conf_id",
17-
# TODO: Domain for Purchase model
18-
domain="[('model_name', '=', 'purchase.order')]"
17+
domain="[('model_name', '=', 'purchase.order')]",
1918
)

edi_purchase_oca/tests/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from . import test_edi_configuration
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# Copyright 2024 CamptoCamp SA
2+
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
3+
4+
import mock
5+
6+
from odoo.exceptions import UserError
7+
8+
from odoo.addons.edi_oca.tests.common import EDIBackendCommonComponentRegistryTestCase
9+
10+
11+
class TestsPurchaseEDIConfiguration(EDIBackendCommonComponentRegistryTestCase):
12+
@classmethod
13+
def setUpClass(cls):
14+
super().setUpClass()
15+
cls._load_module_components(cls, "edi_purchase_oca")
16+
cls.edi_configuration = cls.env["edi.configuration"]
17+
cls.purchase_order = cls.env["purchase.order"]
18+
cls.product = cls.env["product.product"].create(
19+
{
20+
"name": "Product 1",
21+
"default_code": "1234567",
22+
}
23+
)
24+
25+
def setUp(self):
26+
super().setUp()
27+
self.confirm_conf = self.env.ref(
28+
"edi_purchase_oca.edi_conf_button_confirm_purchase_order"
29+
)
30+
self.partner.write({"edi_purchase_conf_ids": [(4, self.confirm_conf.id)]})
31+
32+
def test_edi_configuration_snippet_before_do(self):
33+
order = self.purchase_order.create(
34+
{
35+
"partner_id": self.partner.id,
36+
"order_line": [
37+
(
38+
0,
39+
0,
40+
{
41+
"product_id": self.product.id,
42+
"product_qty": 10,
43+
"price_unit": 100.0,
44+
},
45+
)
46+
],
47+
}
48+
)
49+
self.assertTrue(order)
50+
self.assertEqual(order.state, "draft")
51+
52+
# Replace snippet_before_do for test
53+
self.confirm_conf.snippet_before_do = "record.button_cancel()"
54+
order.button_confirm()
55+
# After purchase order is confirmed
56+
# it will be automatically canceled due to edi_configuration execution.
57+
self.assertEqual(order.state, "cancel")
58+
59+
def test_edi_configuration_snippet_do(self):
60+
self.confirm_conf.write(
61+
{
62+
"backend_id": self.backend.id,
63+
"type_id": self.exchange_type_out.id,
64+
"snippet_do": "record._edi_send_via_edi(conf.type_id)",
65+
}
66+
)
67+
with mock.patch.object(
68+
type(self.backend), "exchange_generate", return_value=True
69+
):
70+
71+
with self.assertRaises(UserError) as err:
72+
order = self.purchase_order.create(
73+
{
74+
"partner_id": self.partner.id,
75+
"order_line": [
76+
(
77+
0,
78+
0,
79+
{
80+
"product_id": self.product.id,
81+
"product_qty": 10,
82+
"price_unit": 100.0,
83+
},
84+
)
85+
],
86+
}
87+
)
88+
order.button_confirm()
89+
self.assertRegex(
90+
err.exception.args[0], r"Record ID=\d+ has no file to send!"
91+
)

edi_purchase_oca/views/res_partner_view.xml

+12-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8" ?>
22
<odoo>
33
<record id="view_partner_form_inherit" model="ir.ui.view">
44
<field name="name">res.partner.form.inherit.sales_purchases</field>
55
<field name="model">res.partner</field>
66
<field name="inherit_id" ref="base.view_partner_form" />
77
<field name="arch" type="xml">
8-
<xpath expr="//page[@name='sales_purchases']" position="inside">
9-
<group string="Edi Configuration">
10-
<field name="edi_purchase_conf_ids"
11-
context="{'default_model_name': 'purchase.order', 'default_partner_ids': active_ids}" />
12-
</group>
8+
<xpath expr="//notebook" position="inside">
9+
<page name="edi_configuration" string="Edi Configurations">
10+
<group string="EDI Purchase Configurations">
11+
<field
12+
name="edi_purchase_conf_ids"
13+
widget="one2many_list"
14+
nolabel="1"
15+
readonly="1"
16+
/>
17+
</group>
18+
</page>
1319
</xpath>
1420
</field>
1521
</record>

0 commit comments

Comments
 (0)