Skip to content
Open
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
11 changes: 9 additions & 2 deletions edi_core_oca/models/edi_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ class EDIBackend(models.Model):
Usecase: the web service you send the file to processes it on the fly.
"""
)
output_cancel_cannot_send_auto = fields.Boolean(
string="Automatically cancel the records that cannot be sent"
)
active = fields.Boolean(default=True)
company_id = fields.Many2one("res.company", string="Company")
auto_archive_records_after_days = fields.Integer(
Expand Down Expand Up @@ -225,7 +228,11 @@ def exchange_send(self, exchange_record):
# In case already sent: skip sending and check the state
check = self._output_check_send(exchange_record)
if not check:
return self._failed_output_check_send_msg()
message = self._failed_output_check_send_msg(exchange_record=exchange_record)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would work only for exchanges implementing check handlers.
Is that what you want?

if self.output_cancel_cannot_send_auto:
exchange_record.edi_exchange_state = "output_cancel"
exchange_record._notify_cancel(message)
return message
state = exchange_record.edi_exchange_state
error = traceback = False
message = None
Expand Down Expand Up @@ -675,7 +682,7 @@ def _is_valid_edi_action(self, action, raise_if_not=False):
raise
return False

def _failed_output_check_send_msg(self):
def _failed_output_check_send_msg(self, exchange_record=None):
return "Nothing to do. Likely already sent."

def _get_exec_handler(self, exchange_record, action):
Expand Down
8 changes: 8 additions & 0 deletions edi_core_oca/models/edi_exchange_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ class EDIExchangeRecord(models.Model):
("output_sent", "Sent"),
("output_sent_and_processed", "Sent and processed"),
("output_sent_and_error", "Sent and error"),
("output_cancel", "Sending canceled"),
# input exchange states
("input_pending", "Waiting to be received"),
("input_received", "Received"),
Expand Down Expand Up @@ -423,6 +424,7 @@ def _exchange_status_messages(self):
),
"ack_received_error": self.env._("ACK file received but contains errors."),
"validate_ko": self.env._("Exchange not valid"),
"cancel": self.env._("Exchange canceled"),
}

def _exchange_status_message(self, key):
Expand Down Expand Up @@ -558,6 +560,12 @@ def _notify_error(self, message_key):
)
self._trigger_edi_event("error")

def _notify_cancel(self, message=None):
if message is None:
message = self._exchange_status_message("cancel")
self._notify_related_record(message)
self._trigger_edi_event("cancel")

def _notify_ack_received(self):
self._notify_related_record(self._exchange_status_message("ack_received"))
self._trigger_edi_event("done", suffix="ack_received")
Expand Down
8 changes: 8 additions & 0 deletions edi_core_oca/tests/test_backend_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,11 @@ def test_send_record_with_operational_error(self):
).exchange_send(self.record)
self.assertRecordValues(self.record, [{"edi_exchange_state": "output_pending"}])
self.assertFalse(self.record.exchange_error)

def test_send_auto_cancel(self):
self.backend.write({"output_cancel_cannot_send_auto": True})
self.record.write({"edi_exchange_state": "output_pending"})
with mock.patch.object(type(self.backend), "_output_check_send") as mocked:
mocked.return_value = False
self.backend.exchange_send(self.record)
self.assertRecordValues(self.record, [{"edi_exchange_state": "output_cancel"}])
1 change: 1 addition & 0 deletions edi_core_oca/views/edi_backend_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
<!-- can be used to hide elements based on the backend type, e.g. notebook pages -->
<field name="backend_type_code" invisible="1" />
<field name="output_sent_processed_auto" />
<field name="output_cancel_cannot_send_auto" />
<field name="active" invisible="1" />
<field name="company_id" groups="base.group_multi_company" />
</group>
Expand Down
Loading