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 Oct 25, 2024
1 parent 9b94f18 commit 3ac4a93
Show file tree
Hide file tree
Showing 12 changed files with 161 additions and 19 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
16 changes: 16 additions & 0 deletions edi_purchase_oca/components/listener_purchase_order.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# 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.listener.config.purchase.order"
_inherit = "base.event.listener"
_apply_on = ["purchase.order"]

def on_button_confirm_purchase_order(self, record):
trigger = "on_button_confirm_purchase_order"
confs = record.partner_id.edi_purchase_conf_ids.edi_get_conf(trigger)
for conf in confs:
conf.edi_exec_snippet_do(record)
10 changes: 5 additions & 5 deletions edi_purchase_oca/data/edi_configuration.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
<field name="name">Purchase Confirmation Config</field>
<field name="code">on_button_confirm_purchase_order</field>
<field name="trigger">on_button_confirm_purchase_order</field>
<field name="model" ref="purchase.model_purchase_order" />
<field name="model_id" 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
19 changes: 19 additions & 0 deletions edi_purchase_oca/models/edi_configuration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# 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"),
],
ondelete={
"on_button_confirm_purchase_order": "set default",
"send_via_email_rfq": "set default",
},
)
12 changes: 7 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,15 @@

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:
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

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
91 changes: 91 additions & 0 deletions edi_purchase_oca/tests/test_edi_configuration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# 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.confirm_conf = self.env.ref(
"edi_purchase_oca.edi_conf_button_confirm_purchase_order"
)
self.partner.write({"edi_purchase_conf_ids": [(4, self.confirm_conf.id)]})

def test_edi_configuration_snippet_before_do(self):
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, "draft")

# Replace snippet_before_do for test
self.confirm_conf.snippet_before_do = "record.button_cancel()"
order.button_confirm()
# After purchase order is confirmed
# it will be automatically canceled due to edi_configuration execution.
self.assertEqual(order.state, "cancel")

def test_edi_configuration_snippet_do(self):
self.confirm_conf.write(
{
"backend_id": self.backend.id,
"type_id": self.exchange_type_out.id,
"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:
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,
},
)
],
}
)
order.button_confirm()
self.assertRegex(
err.exception.args[0], r"Record ID=\d+ has no file to send!"
)
17 changes: 11 additions & 6 deletions edi_purchase_oca/views/res_partner_view.xml
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
<?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>
<field name="model">res.partner</field>
<field name="inherit_id" ref="base.view_partner_form" />
<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}" />
</group>
<xpath expr="//notebook" position="inside">
<page name="edi_configuration" string="Edi Configurations">
<group string="EDI Purchase Configurations">
<field
name="edi_purchase_conf_ids"
widget="one2many_list"
nolabel="1"
/>
</group>
</page>
</xpath>
</field>
</record>
Expand Down

0 comments on commit 3ac4a93

Please sign in to comment.