Skip to content

Commit

Permalink
Improve inheritance by removing duplicated code and move fields in co…
Browse files Browse the repository at this point in the history
…rrect modules
  • Loading branch information
ecino committed Oct 8, 2020
1 parent 4dd9124 commit d56e919
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 89 deletions.
20 changes: 11 additions & 9 deletions l10n_ch_fds_import_payment_return/models/fds_postfinance_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ class FdsPostfinanceFile(models.Model):
ondelete='restrict',
readonly=True,
)
payment_order = fields.Many2one(
'account.payment.order',
string='Payment order',
ondelete='restrict',
readonly=True,
)
file_type = fields.Selection(selection_add=[
('pain.002.001.03.ch.02',
'pain.002.001.03.ch.02 (payment return)')
Expand All @@ -45,24 +51,22 @@ def import_to_payment_return(self):
pr_import_obj = self.env['payment.return.import']
pr_wiz_imp = pr_import_obj.create(values)
import_result = pr_wiz_imp.import_file()
payment_return = self.env["payment.return"].browse(import_result['res_id'])
# Mark the file as imported, remove binary as it should be
# attached to the statement.
self.write({
'state': 'done',
'payment_return_id':
import_result['res_id'],
'payment_order': self.env['account.payment.order']
.search([('name', '=',
import_result['context']['order_name'])]).id,
'payment_return_id': payment_return.id,
'payment_order': payment_return.payment_order_id.id,
'error_message': False
})
# Automatically confirm payment returns
payment_return.action_confirm()
_logger.info("[OK] import file '%s'", self.filename)
except NoTransactionsError as e:
_logger.info(e.name, self.filename)
self.write({
'state': 'done',
'payment_order': self.env['account.payment.order']
.search([('name', '=', e.object[0]['order_name'])]).id,
'error_message': e.name
})
except FileAlreadyImported as e:
Expand All @@ -72,8 +76,6 @@ def import_to_payment_return(self):
.search([('line_ids.reference', 'in', references)])
self.write({
'state': 'done',
'payment_order': self.env['account.payment.order']
.search([('name', '=', e.object[0]['order_name'])]).id,
'payment_return_id': payment_return.id,
'error_message': e.name
})
Expand Down
13 changes: 3 additions & 10 deletions l10n_ch_fds_upload_sepa/models/fds_postfinance_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,6 @@ class FdsPostfinanceFile(models.Model):
"""
_inherit = 'fds.postfinance.file'

payment_order = fields.Many2one(
'account.payment.order',
string='Payment order',
ondelete='restrict',
readonly=True,
)

file_type = fields.Selection(selection_add=[
('pain.001.001.03.ch.02',
'pain.001.001.03.ch.02 (payment order)')
Expand All @@ -38,16 +31,16 @@ def import_to_bank_statements(self):
result = account_pain002.parse(decoded_file)

# Link the payment order to the file import.
pf_file.payment_order = self.env['account.payment.order'] \
.search([('name', '=', result['order_name'])])
pf_file.payment_order = result['payment_order_id']
if result['transactions']:
# Attach the file to the payment order.
self.env['ir.attachment'].create({
'datas_fname': pf_file.filename,
'res_model': 'account.payment.order',
'datas': pf_file.data,
'name': pf_file.filename,
'res_id': pf_file.payment_order.id})
'res_id': pf_file.payment_order.id
})

pf_file.write({
'state': 'done',
Expand Down
1 change: 1 addition & 0 deletions l10n_ch_payment_return_sepa/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from . import pain002_parser
from . import errors
from . import payment_return
27 changes: 14 additions & 13 deletions l10n_ch_payment_return_sepa/models/pain002_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,25 +76,26 @@ def parse(self, payment_return):
'./ns:OrgnlGrpInfAndSts/ns:OrgnlMsgId',
payment_return, 'order_name')

payment_order = self.env['account.payment.order'] \
.search([('name', '=', payment_return['order_name'])])
payment_order = self.env['account.payment.order'].search([
('name', '=', payment_return.pop('order_name'))])
payment_return["payment_order_id"] = payment_order.id
if 'account_number' not in payment_return:
payment_return['account_number'] = payment_order \
.company_partner_bank_id.acc_number

if payment_order.payment_mode_id.offsetting_account \
== 'transfer_account':
order_mode = payment_order.payment_mode_id
if order_mode.offsetting_account == 'transfer_account':
journal_obj = self.env["account.journal"]
if payment_order.payment_type == 'inbound':
payment_return['journal_id'] = self.env[
'account.journal'] \
.search([('default_credit_account_id.id', '=',
payment_order.payment_mode_id
.transfer_account_id.id)]).id
payment_return['journal_id'] = journal_obj.search([
('default_credit_account_id', '=',
order_mode.transfer_account_id.id)
], limit=1).id
elif payment_order.payment_type == 'outbound':
payment_return['journal_id'] = self.env['account.journal']\
.search([('default_debit_account_id.id', '=',
payment_order.payment_mode_id
.transfer_account_id.id)]).id
payment_return['journal_id'] = journal_obj.search([
('default_debit_account_id', '=',
order_mode.transfer_account_id.id)
], limit=1).id
else:
payment_return['journal_id'] = payment_order.journal_id.id

Expand Down
11 changes: 11 additions & 0 deletions l10n_ch_payment_return_sepa/models/payment_return.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Copyright 2020 Emanuel Cino <[email protected]>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import fields, models


class PaymentReturn(models.Model):
_inherit = "payment.return"

payment_order_id = fields.Many2one(
"account.payment.order", "Originating Payment order")
60 changes: 3 additions & 57 deletions l10n_ch_payment_return_sepa/wizards/payment_return_import.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
# Copyright 2016 Carlos Dauden <[email protected]>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

import logging
import base64

from odoo import api, models, _
from odoo.exceptions import UserError
from ..models.errors import NoPaymentReturnError, NoTransactionsError, \
ErrorOccurred

Expand All @@ -17,7 +14,8 @@ class PaymentReturnImport(models.TransientModel):

@api.model
def _check_parsed_data(self, payment_returns):
""" Basic and structural verifications """
""" Override base function to avoid raising errors when the statement has no
transaction """
if not payment_returns:
raise NoPaymentReturnError(_(
'This file doesn\'t contain any payment return.'))
Expand All @@ -33,58 +31,6 @@ def _check_parsed_data(self, payment_returns):
payment_returns
)

@api.multi
def import_file(self):
"""Process the file chosen in the wizard, create bank payment return(s)
and go to reconciliation."""
self.ensure_one()
data_file = base64.b64decode(self.data_file)
payment_returns, notifications, order_name = self.with_context(
active_id=self.id
)._import_file(data_file)
result = self.env.ref(
'account_payment_return.payment_return_action').read()[0]
if self.match_after_import:
payment_returns.button_match()
if len(payment_returns) != 1:
result['domain'] = "[('id', 'in', %s)]" % payment_returns.ids
else:
form_view = self.env.ref(
'account_payment_return.payment_return_form_view')
result.update({
'views': [(form_view.id, 'form')],
'res_id': payment_returns.id,
'context': {
'notifications': notifications,
'order_name': order_name
},
})
return result

@api.model
def _import_file(self, data_file):
""" Create bank payment return(s) from file."""
# The appropriate implementation module returns the required data
payment_returns = self.env['payment.return']
notifications = []
payment_return_raw_list = self._parse_all_files(data_file)
# Check raw data:
self._check_parsed_data(payment_return_raw_list)
order_name = ''
# Import all payment returns:
for payret_vals in payment_return_raw_list:
payret_vals = self._complete_payment_return(payret_vals)
payment_return, new_notifications = self._create_payment_return(
payret_vals)
if payment_return:
payment_return.action_confirm()
payment_returns += payment_return
notifications.extend(new_notifications)
order_name = payret_vals['order_name']
if not payment_returns:
raise UserError(_('You have already imported this file.'))
return payment_returns, notifications, order_name

@api.model
def _parse_file(self, data_file):
"""Parse a PAIN.002.001.03 XML file."""
Expand All @@ -93,7 +39,7 @@ def _parse_file(self, data_file):
return self.env['account.pain002.parser'].parse(data_file)
except ValueError:
# Not a valid file, returning super will call next candidate:
_logger.debug("Paymen return file was not a Direct Debit Unpaid "
_logger.debug("Payment return file was not a Direct Debit Unpaid "
"Report file.",
exc_info=True)
return super()._parse_file(data_file)

0 comments on commit d56e919

Please sign in to comment.