From 8c630b0e5ef67eb1bacbec6e6976febaa93eb4e9 Mon Sep 17 00:00:00 2001 From: Mathis Jacoby Date: Wed, 6 Dec 2023 15:03:42 +0100 Subject: [PATCH] [MIG] companyweb_payment_info: Migration to 17.0 --- companyweb_payment_info/__manifest__.py | 2 +- .../tests/test_cweb_upload.py | 17 +++++- .../wizards/payment_info_wizard.py | 53 ++++++++----------- .../wizards/payment_info_wizard.xml | 2 +- 4 files changed, 40 insertions(+), 34 deletions(-) diff --git a/companyweb_payment_info/__manifest__.py b/companyweb_payment_info/__manifest__.py index 66e319404..01a24207b 100644 --- a/companyweb_payment_info/__manifest__.py +++ b/companyweb_payment_info/__manifest__.py @@ -5,7 +5,7 @@ "summary": ("Send your customer payment information to Companyweb"), "author": "ACSONE SA/NV,Odoo Community Association (OCA)", "website": "https://github.com/OCA/l10n-belgium", - "version": "16.0.1.0.4", + "version": "17.0.1.0.0", "development_status": "Production/Stable", "license": "AGPL-3", "installable": True, diff --git a/companyweb_payment_info/tests/test_cweb_upload.py b/companyweb_payment_info/tests/test_cweb_upload.py index b2b6bb5a2..dfe0d3bdc 100644 --- a/companyweb_payment_info/tests/test_cweb_upload.py +++ b/companyweb_payment_info/tests/test_cweb_upload.py @@ -2,16 +2,31 @@ # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). import os +import requests +import werkzeug from freezegun import freeze_time +from requests import PreparedRequest, Session from vcr_unittest import VCRMixin from odoo.exceptions import UserError from odoo.tests.common import TransactionCase +_super_send = requests.Session.send + class TestUpload(VCRMixin, TransactionCase): + @classmethod + def _request_handler(cls, s: Session, r: PreparedRequest, /, **kw): + """ + Override to allow requests to the companyweb API + """ + url = werkzeug.urls.url_parse(r.url) + if url.host in ("payex.companyweb.be",): + return _super_send(s, r, **kw) + return super()._request_handler(s=s, r=r, **kw) + def setUp(self, *args, **kwargs): - super(TestUpload, self).setUp(*args, **kwargs) + super().setUp(*args, **kwargs) self.demo_user = self.env.ref("base.user_demo") self.env.user.company_id.vat = False self.demo_user.partner_id.email = "test@odoo.com" diff --git a/companyweb_payment_info/wizards/payment_info_wizard.py b/companyweb_payment_info/wizards/payment_info_wizard.py index 1dc8f281b..0a756ccf3 100644 --- a/companyweb_payment_info/wizards/payment_info_wizard.py +++ b/companyweb_payment_info/wizards/payment_info_wizard.py @@ -27,6 +27,8 @@ class CompanyWebPaymentInfoWizard(models.TransientModel): wizard_text = fields.Html("wizard_text") wizard_step = fields.Char(default="step1") wizard_email = fields.Char("wizard_email") + last_sent_month = fields.Integer() + last_sent_year = fields.Integer() def payment_info_entry_point(self): if self.wizard_step == "step1": @@ -51,8 +53,8 @@ def _check_group(self): def _cweb_payment_info_step1(self): self._check_group() - supplierVat = self.env.user.company_id.vat - if not supplierVat or not supplierVat.startswith("BE"): + supplier_vat = self.env.user.company_id.vat + if not supplier_vat or not supplier_vat.startswith("BE"): raise UserError( _( "Companyweb : You need to set a valid belgian's vat " @@ -69,31 +71,21 @@ def _cweb_payment_info_step1(self): "https://payex.companyweb.be/v1/PaymentExperienceService.asmx" ) - response_previous_period = self._get_previous_period(client, supplierVat) + response_previous_period = self._get_previous_period(client, supplier_vat) # 11/15/16 means bad credentials if response_previous_period["StatusCode"] in [11, 15, 16]: return self._cweb_call_wizard_credentials(_("Bad Credentials")) - last_date_sent = "%02d/%d" % ( - response_previous_period["PreviousMonth"], - response_previous_period["PreviousYear"], - ) + + self.last_sent_month = response_previous_period["PreviousMonth"] + self.last_sent_year = response_previous_period["PreviousYear"] period_to_send = self._get_period_to_send() - if ( - period_to_send.month == response_previous_period["PreviousMonth"] - and period_to_send.year == response_previous_period["PreviousYear"] - ): - raise UserError( - _("Companyweb : You already submitted invoices for {last_date}").format( - last_date=last_date_sent - ) - ) invoices_to_send = self._create_invoices_to_send(client) if len(invoices_to_send) == 0: raise UserError(_("Companyweb : No Invoices to send")) summary = self._create_step1_summary( - response_previous_period, invoices_to_send, last_date_sent, period_to_send + response_previous_period, invoices_to_send, period_to_send ) wizard = self.create( @@ -101,6 +93,8 @@ def _cweb_payment_info_step1(self): wizard_text=summary, wizard_step="step2", wizard_email=self.env.user.partner_id.email, + last_sent_month=self.last_sent_month, + last_sent_year=self.last_sent_year, ) ) return dict(wizard.get_formview_action(), target="new") @@ -133,9 +127,7 @@ def _cweb_payment_info_step2(self): ) transaction_key = result_start_transaction["TransactionKey"] self._cweb_send_batch(client, invoices_to_send, transaction_key) - result_summary = self._cweb_step3_summary( - client, transaction_key, invoices_to_send - ) + result_summary = self._cweb_step3_summary(client, transaction_key) result_commit = self._cweb_commit_tran(client, transaction_key) if result_commit["StatusCode"] != 0: @@ -165,7 +157,7 @@ def _cweb_commit_tran(self, client, transaction_key): ) return response_tran - def _cweb_step3_summary(self, client, transaction_key, invoices_to_send): + def _cweb_step3_summary(self, client, transaction_key): response_summary = client.service.Step3_GetSummary( dict( CompanyWebLogin=self.env.user.cweb_login, @@ -214,13 +206,16 @@ def _create_step1_summary( self, response_previous_period, invoice_to_send, - last_period_sent, period_to_send, ): print_period_to_send = "%02d/%d" % ( period_to_send.month, period_to_send.year, ) + print_last_period_sent = "%02d/%d" % ( + self.last_sent_month, + self.last_sent_year, + ) if response_previous_period["PreviousPeriodExists"]: summary = _( "

Companyweb upload

" @@ -234,7 +229,7 @@ def _create_step1_summary( ).format( nb_invoice=len(invoice_to_send), company=html.escape(self.env.user.company_id.name), - last_period=last_period_sent, + last_period=print_last_period_sent, login=self.env.user.partner_id.email, period=print_period_to_send, ) @@ -293,7 +288,7 @@ def _get_open_invoices(self): ] ) - def _get_previous_period(self, client, supplierVat): + def _get_previous_period(self, client, supplier_vat): response_previous_period = client.service.GetPreviousPeriod( dict( CompanyWebLogin=self.env.user.cweb_login, @@ -304,7 +299,7 @@ def _get_previous_period(self, client, supplierVat): self.env.user.cweb_password, cweb_partner.SERVICE_INTEGRATOR_SECRET, ), - SupplierVat=supplierVat, + SupplierVat=supplier_vat, ) ) @@ -358,10 +353,6 @@ def _cweb_send_batch(self, client, invoices_to_send, transaction_key): ) def _cweb_start_transaction(self, client, email): - today = datetime.date.today() - first = today.replace(day=1) - lastMonth = first - datetime.timedelta(days=1) - return client.service.Step1_StartTransaction( dict( CompanyWebLogin=self.env.user.cweb_login, @@ -374,8 +365,8 @@ def _cweb_start_transaction(self, client, email): ), PackageVersion=self._get_module_version(), SupplierVat=self.env.user.company_id.vat, - PeriodYear=lastMonth.year, - PeriodMonth=lastMonth.month, + PeriodYear=str(self.last_sent_year), + PeriodMonth="%02d" % self.last_sent_month, EmailAddress=email, ) ) diff --git a/companyweb_payment_info/wizards/payment_info_wizard.xml b/companyweb_payment_info/wizards/payment_info_wizard.xml index 91ace62dd..6211e33da 100644 --- a/companyweb_payment_info/wizards/payment_info_wizard.xml +++ b/companyweb_payment_info/wizards/payment_info_wizard.xml @@ -7,7 +7,7 @@
-
+
If you want to receive the summary on another email please change it here :