[IMP] payment*: auto-create journal for payment.acquirer

This commit adds the auto-creation journal for installed acquirers.
This is not easy because:
- The acquirers are created on the 'payment' module but are enabled only when the specific module
is installed. E.g. Paypal is enabled with 'payment_paypal'.
- To create a journal, a chart of accounts is required. However, the post_init_hook on the
'account' module makes the installation order harder. E.g install payment_paypal directly:
The module are installed in the order: account -> payment -> payment_paypal -> l10n_generic_coa.

To fix the problem, the journals are created at two moments:
- During the installation of the chart of accounts.
- At the installation of an acquirer module. E.g. payment_paypal.

Was PR #23904
Was task: 1831620
This commit is contained in:
Laurent Smet
2018-04-26 11:26:22 +02:00
committed by qdp-odoo
parent 5588c2e02b
commit 5439e72a4e
23 changed files with 118 additions and 4 deletions
+1
View File
@@ -3,3 +3,4 @@
from . import payment_acquirer
from . import res_partner
from . import account_payment
from . import chart_template
+14
View File
@@ -0,0 +1,14 @@
# -*- coding: utf-8 -*-
from odoo import api, fields, models, _
class WizardMultiChartsAccounts(models.TransientModel):
_inherit = 'wizard.multi.charts.accounts'
@api.multi
def _create_bank_journals_from_o2m(self, company, acc_template_ref):
res = super(WizardMultiChartsAccounts, self)._create_bank_journals_from_o2m(company, acc_template_ref)
# Try to generate the missing journals
return res + self.env['payment.acquirer']._create_missing_journal_for_acquirers(company=company)
+70 -2
View File
@@ -8,6 +8,7 @@ from odoo import api, exceptions, fields, models, _
from odoo.tools import consteq, float_round, image_resize_images, image_resize_image, ustr
from odoo.addons.base.module import module
from odoo.exceptions import ValidationError
from odoo import api, SUPERUSER_ID
_logger = logging.getLogger(__name__)
@@ -20,6 +21,11 @@ def _partner_split_name(partner_name):
return [' '.join(partner_name.split()[:-1]), ' '.join(partner_name.split()[-1:])]
def create_missing_journal_for_acquirers(cr, registry):
env = api.Environment(cr, SUPERUSER_ID, {})
env['payment.acquirer']._create_missing_journal_for_acquirers()
class PaymentAcquirer(models.Model):
""" Acquirer Model. Each specific acquirer can extend the model by adding
its own fields, using the acquirer_name as a prefix for the new fields.
@@ -51,6 +57,9 @@ class PaymentAcquirer(models.Model):
_description = 'Payment Acquirer'
_order = 'website_published desc, sequence, name'
def _get_default_view_template_id(self):
return self.env.ref('payment.default_acquirer_button', raise_if_not_found=False)
name = fields.Char('Name', required=True, translate=True)
description = fields.Html('Description')
sequence = fields.Integer('Sequence', default=10, help="Determine the display order")
@@ -61,7 +70,8 @@ class PaymentAcquirer(models.Model):
'res.company', 'Company',
default=lambda self: self.env.user.company_id.id, required=True)
view_template_id = fields.Many2one(
'ir.ui.view', 'Form Button Template', required=True)
'ir.ui.view', 'Form Button Template', required=True,
default=_get_default_view_template_id)
registration_view_template_id = fields.Many2one(
'ir.ui.view', 'S2S Form Template', domain=[('type', '=', 'qweb')],
help="Template for method registration")
@@ -196,6 +206,58 @@ class PaymentAcquirer(models.Model):
"""
return dict(authorize=[], tokenize=[], fees=[])
@api.multi
def _prepare_account_journal_vals(self):
'''Prepare the values to create the acquirer's journal.
:return: a dictionary to create a account.journal record.
'''
self.ensure_one()
account_vals = self.env['account.journal']._prepare_liquidity_account(
self.name, self.company_id, None, 'bank')
account_vals['user_type_id'] = self.env.ref('account.data_account_type_current_assets').id
account_vals['reconcile'] = True
account = self.env['account.account'].create(account_vals)
return {
'name': self.name,
'code': self.name.upper(),
'sequence': 999,
'type': 'bank',
'company_id': self.company_id.id,
'default_debit_account_id': account.id,
'default_credit_account_id': account.id,
# Show the journal on dashboard if the acquirer is published on the website.
'show_on_dashboard': self.website_published,
# Don't show payment methods in the backend.
'inbound_payment_method_ids': [],
'outbound_payment_method_ids': [],
}
@api.model
def _create_missing_journal_for_acquirers(self, company=None):
'''Create the journal for active acquirers.
We want one journal per acquirer. However, we can't create them during the 'create' of the payment.acquirer
because every acquirers are defined on the 'payment' module but is active only when installing their own module
(e.g. payment_paypal for Paypal). We can't do that in such modules because we have no guarantee the chart template
is already installed.
'''
# Search for installed acquirers modules.
# If this method is triggered by a post_init_hook, the module is 'to install'.
# If the trigger comes from the chart template wizard, the modules are already installed.
acquirer_modules = self.env['ir.module.module'].search(
[('name', 'like', 'payment_%'), ('state', 'in', ('to install', 'installed'))])
acquirer_names = [a.name.split('_')[1] for a in acquirer_modules]
# Search for acquirers having no journal
company = company or self.env.user.company_id
acquirers = self.env['payment.acquirer'].search(
[('provider', 'in', acquirer_names), ('journal_id', '=', False), ('company_id', '=', company.id)])
journals = self.env['account.journal']
for acquirer in acquirers.filtered(lambda l: not l.journal_id and l.company_id.chart_template_id):
acquirer.journal_id = self.env['account.journal'].create(acquirer._prepare_account_journal_vals())
journals += acquirer.journal_id
return journals
@api.model
def create(self, vals):
image_resize_images(vals)
@@ -208,7 +270,13 @@ class PaymentAcquirer(models.Model):
@api.multi
def toggle_website_published(self):
self.write({'website_published': not self.website_published})
''' When clicking on the website publish toggle button, the website_published is reversed and
the acquirer journal is set or not in favorite on the dashboard.
'''
self.ensure_one()
self.website_published = not self.website_published
if self.journal_id:
self.journal_id.show_on_dashboard = self.website_published
return True
@api.multi
+2 -2
View File
@@ -81,7 +81,7 @@
</page>
<page string="Configuration">
<group name="acquirer_config">
<field name="journal_id" context="{'default_type': 'bank'}" required="1"/>
<field name="journal_id" context="{'default_type': 'bank'}"/>
<field name="capture_manually" attrs="{'invisible': [('authorize_implemented', '=', False)]}"/>
<field name="save_token" widget="radio" attrs="{'invisible': ['|', ('token_implemented', '=', False), ('payment_flow', '=', 's2s')]}"/>
<field name="fees_active" attrs="{'invisible': [('fees_implemented', '=', False)]}"/>
@@ -113,7 +113,7 @@
</div>
<field name="registration_view_template_id" groups="base.group_no_one"/>
<field name="payment_icon_ids" widget="many2many_tags"/>
<field name="payment_flow" widget="radio"/>
<field name="payment_flow" widget="radio" attrs="{'invisible': [('token_implemented', '=', False)]}"/>
</group>
</page>
</notebook>
+1
View File
@@ -3,3 +3,4 @@
from . import models
from . import controllers
from odoo.addons.payment.models.payment_acquirer import create_missing_journal_for_acquirers
+1
View File
@@ -13,4 +13,5 @@
'data/payment_acquirer_data.xml',
],
'installable': True,
'post_init_hook': 'create_missing_journal_for_acquirers',
}
+1
View File
@@ -3,3 +3,4 @@
from . import models
from . import controllers
from odoo.addons.payment.models.payment_acquirer import create_missing_journal_for_acquirers
+1
View File
@@ -13,4 +13,5 @@
'data/payment_acquirer_data.xml',
],
'installable': True,
'post_init_hook': 'create_missing_journal_for_acquirers',
}
+1
View File
@@ -3,3 +3,4 @@
from . import models
from . import controllers
from odoo.addons.payment.models.payment_acquirer import create_missing_journal_for_acquirers
+1
View File
@@ -13,4 +13,5 @@
'data/payment_acquirer_data.xml',
],
'installable': True,
'post_init_hook': 'create_missing_journal_for_acquirers',
}
+1
View File
@@ -3,3 +3,4 @@
from . import models
from . import controllers
from odoo.addons.payment.models.payment_acquirer import create_missing_journal_for_acquirers
+1
View File
@@ -13,4 +13,5 @@
'data/payment_acquirer_data.xml',
],
'installable': True,
'post_init_hook': 'create_missing_journal_for_acquirers',
}
+1
View File
@@ -3,3 +3,4 @@
from . import models
from . import controllers
from odoo.addons.payment.models.payment_acquirer import create_missing_journal_for_acquirers
+1
View File
@@ -13,4 +13,5 @@
'data/payment_acquirer_data.xml',
],
'installable': True,
'post_init_hook': 'create_missing_journal_for_acquirers',
}
+1
View File
@@ -3,3 +3,4 @@
from . import models
from . import controllers
from odoo.addons.payment.models.payment_acquirer import create_missing_journal_for_acquirers
+1
View File
@@ -16,4 +16,5 @@
'views/payment_payumoney_templates.xml',
'data/payment_acquirer_data.xml',
],
'post_init_hook': 'create_missing_journal_for_acquirers',
}
+1
View File
@@ -1,2 +1,3 @@
from . import models
from . import controllers
from odoo.addons.payment.models.payment_acquirer import create_missing_journal_for_acquirers
+1
View File
@@ -20,4 +20,5 @@ payments acquirer using Worldline SIPS.""",
'data/payment_acquirer_data.xml',
],
'installable': True,
'post_init_hook': 'create_missing_journal_for_acquirers',
}
+1
View File
@@ -3,3 +3,4 @@
from . import models
from . import controllers
from odoo.addons.payment.models.payment_acquirer import create_missing_journal_for_acquirers
+1
View File
@@ -14,4 +14,5 @@
],
'images': ['static/description/icon.png'],
'installable': True,
'post_init_hook': 'create_missing_journal_for_acquirers',
}
+1
View File
@@ -3,3 +3,4 @@
from . import models
from . import controllers
from odoo.addons.payment.models.payment_acquirer import create_missing_journal_for_acquirers
+1
View File
@@ -14,4 +14,5 @@
],
'installable': True,
'auto_install': True,
'post_init_hook': 'create_missing_journal_for_acquirers',
}
+13
View File
@@ -15,6 +15,19 @@ class TransferPaymentAcquirer(models.Model):
provider = fields.Selection(selection_add=[('transfer', 'Wire Transfer')], default='transfer')
@api.model
def _create_missing_journal_for_acquirers(self, company=None):
# By default, the wire transfer method uses the default Bank journal.
company = company or self.env.user.company_id
acquirers = self.env['payment.acquirer'].search(
[('provider', '=', 'transfer'), ('journal_id', '=', False), ('company_id', '=', company.id)])
bank_journal = self.env['account.journal'].search(
[('type', '=', 'bank'), ('company_id', '=', company.id)], limit=1)
if bank_journal:
acquirers.write({'journal_id': bank_journal.id})
return super(TransferPaymentAcquirer, self)._create_missing_journal_for_acquirers(company=company)
def transfer_get_form_action_url(self):
return '/payment/transfer/feedback'