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 authored and xavier-bouquiaux committed Mar 11, 2024
1 parent fc520ef commit c6d7d75
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 46 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
22 changes: 21 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,33 @@
# 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
because odoo17 only permit calls to localhost
(see https://github.com/odoo/odoo/blob/17.0/odoo/tests/common.py#L265 )
"""
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 Expand Up @@ -58,6 +75,9 @@ def _init_invoice(self):
{
"name": "Bisnode SA",
"vat": "BE0458.662.817",
"country_id": False,
"peppol_eas": False,
"peppol_endpoint": False,
}
)
self.out_invoice_1 = self.env["account.move"].create(
Expand Down
93 changes: 50 additions & 43 deletions companyweb_payment_info/wizards/payment_info_wizard.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import re

import zeep
from dateutil.relativedelta import relativedelta

from odoo import _, api, fields, models
from odoo.exceptions import UserError
Expand All @@ -27,6 +28,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 +54,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,46 +72,49 @@ 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"))
invoices_to_send = self._create_invoices_to_send(client, period_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")

def _get_period_to_send(self):
today = datetime.date.today()
first = today.replace(day=1)
return first - datetime.timedelta(days=1)
# We have to use last sent period
# The period to send is the month after the last period sent
return_date = fields.Date.today()

if not self.last_sent_year:
today = datetime.date.today()
first = today.replace(day=1)
return_date = first - datetime.timedelta(days=1)
else:
last_period_sent = "%d-%02d-01" % (
self.last_sent_year,
self.last_sent_month,
)
last_date_sent = fields.Date.from_string(last_period_sent)
return_date = last_date_sent + relativedelta(months=1)
return return_date

@api.model
def _cweb_payment_info_step2(self):
Expand All @@ -121,9 +127,12 @@ def _cweb_payment_info_step2(self):
wizard_email = (
self.wizard_email if self.wizard_email else self.env.user.partner_id.email
)
period_to_send = self._get_period_to_send()

invoices_to_send = self._create_invoices_to_send(client)
result_start_transaction = self._cweb_start_transaction(client, wizard_email)
invoices_to_send = self._create_invoices_to_send(client, period_to_send)
result_start_transaction = self._cweb_start_transaction(
client, wizard_email, period_to_send
)
if result_start_transaction["StatusCode"] != 0:
raise UserError(
_("Error from Companyweb : {status} : {message}").format(
Expand All @@ -133,9 +142,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 +172,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 +221,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 +244,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 All @@ -259,8 +269,8 @@ def _create_step1_summary(

return summary

def _create_invoices_to_send(self, client):
open_invoices = self._get_open_invoices()
def _create_invoices_to_send(self, client, to_date):
open_invoices = self._get_open_invoices(to_date)
if not open_invoices:
return []

Expand All @@ -283,17 +293,18 @@ def _create_invoices_to_send(self, client):
invoice_to_send.append(to_send)
return invoice_to_send

def _get_open_invoices(self):
def _get_open_invoices(self, to_date):
return self.env["account.move"].search(
[
("payment_state", "=", "not_paid"),
("state", "=", "posted"),
("move_type", "in", ["out_invoice"]),
("company_id", "=", self.env.user.company_id.id),
("date", "<=", to_date),
]
)

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 +315,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 @@ -357,11 +368,7 @@ 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)

def _cweb_start_transaction(self, client, email, perdiod_to_send):
return client.service.Step1_StartTransaction(
dict(
CompanyWebLogin=self.env.user.cweb_login,
Expand All @@ -374,8 +381,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(perdiod_to_send.year),
PeriodMonth="%02d" % perdiod_to_send.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 c6d7d75

Please sign in to comment.