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 18, 2024
1 parent 0a923bb commit a65062e
Show file tree
Hide file tree
Showing 12 changed files with 160 additions and 18 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
19 changes: 19 additions & 0 deletions edi_purchase_oca/components/listener_purchase_order.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.addons.component.core import Component


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

def on_button_confirm_purchase_order(self, record):
trigger = "on_button_confirm_purchase_order"
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)
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

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
92 changes: 92 additions & 0 deletions edi_purchase_oca/tests/test_edi_configuration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# 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 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")

confirm_conf = self.env.ref(
"edi_purchase_oca.edi_conf_button_confirm_purchase_order"
)
# Replace snippet_before_do for test
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.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"),
"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!"
)
16 changes: 10 additions & 6 deletions edi_purchase_oca/views/res_partner_view.xml
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
<?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="Configurations">
<field
name="edi_purchase_conf_ids"
context="{'default_model_name': 'purchase.order', 'default_partner_ids': active_ids}"
/>
</group>
</page>
</xpath>
</field>
</record>
Expand Down

0 comments on commit a65062e

Please sign in to comment.