Skip to content

Commit

Permalink
[MIG] companyweb_payment_info: Migration to 17.0
Browse files Browse the repository at this point in the history
  • Loading branch information
mathisjacoby committed Dec 6, 2023
1 parent fc520ef commit 8c630b0
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 34 deletions.
2 changes: 1 addition & 1 deletion companyweb_payment_info/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
17 changes: 16 additions & 1 deletion companyweb_payment_info/tests/test_cweb_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "[email protected]"
Expand Down
53 changes: 22 additions & 31 deletions companyweb_payment_info/wizards/payment_info_wizard.py
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand All @@ -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 "
Expand All @@ -69,38 +71,30 @@ 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(
dict(
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")
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 = _(
"<h2>Companyweb upload</h2>"
Expand All @@ -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,
)
Expand Down Expand Up @@ -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,
Expand All @@ -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,
)
)

Expand Down Expand Up @@ -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,
Expand All @@ -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,
)
)
Expand Down
2 changes: 1 addition & 1 deletion companyweb_payment_info/wizards/payment_info_wizard.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<form>
<field name="wizard_text" readonly="1" />
<field name="wizard_step" invisible="1" />
<div attrs="{'invisible': [('wizard_step', '!=','step2')]}">
<div invisible="wizard_step !='step2'">
If you want to receive the summary on another email please change it here :
<field name="wizard_email" />
</div>
Expand Down

0 comments on commit 8c630b0

Please sign in to comment.