From 5439e72a4ebd7716b813c7ec24fd59058151568f Mon Sep 17 00:00:00 2001 From: Laurent Smet Date: Tue, 27 Mar 2018 14:33:12 +0200 Subject: [PATCH] [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 --- addons/payment/models/__init__.py | 1 + addons/payment/models/chart_template.py | 14 +++++ addons/payment/models/payment_acquirer.py | 72 ++++++++++++++++++++++- addons/payment/views/payment_views.xml | 4 +- addons/payment_adyen/__init__.py | 1 + addons/payment_adyen/__manifest__.py | 1 + addons/payment_authorize/__init__.py | 1 + addons/payment_authorize/__manifest__.py | 1 + addons/payment_buckaroo/__init__.py | 1 + addons/payment_buckaroo/__manifest__.py | 1 + addons/payment_ogone/__init__.py | 1 + addons/payment_ogone/__manifest__.py | 1 + addons/payment_paypal/__init__.py | 1 + addons/payment_paypal/__manifest__.py | 1 + addons/payment_payumoney/__init__.py | 1 + addons/payment_payumoney/__manifest__.py | 1 + addons/payment_sips/__init__.py | 1 + addons/payment_sips/__manifest__.py | 1 + addons/payment_stripe/__init__.py | 1 + addons/payment_stripe/__manifest__.py | 1 + addons/payment_transfer/__init__.py | 1 + addons/payment_transfer/__manifest__.py | 1 + addons/payment_transfer/models/payment.py | 13 ++++ 23 files changed, 118 insertions(+), 4 deletions(-) create mode 100644 addons/payment/models/chart_template.py diff --git a/addons/payment/models/__init__.py b/addons/payment/models/__init__.py index 3a1c7ce052a..ed704df85d1 100644 --- a/addons/payment/models/__init__.py +++ b/addons/payment/models/__init__.py @@ -3,3 +3,4 @@ from . import payment_acquirer from . import res_partner from . import account_payment +from . import chart_template diff --git a/addons/payment/models/chart_template.py b/addons/payment/models/chart_template.py new file mode 100644 index 00000000000..515103316be --- /dev/null +++ b/addons/payment/models/chart_template.py @@ -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) diff --git a/addons/payment/models/payment_acquirer.py b/addons/payment/models/payment_acquirer.py index 691de438072..efd6573c778 100644 --- a/addons/payment/models/payment_acquirer.py +++ b/addons/payment/models/payment_acquirer.py @@ -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 diff --git a/addons/payment/views/payment_views.xml b/addons/payment/views/payment_views.xml index e5b29fb7b75..7d0b10ea5a1 100644 --- a/addons/payment/views/payment_views.xml +++ b/addons/payment/views/payment_views.xml @@ -81,7 +81,7 @@ - + @@ -113,7 +113,7 @@ - + diff --git a/addons/payment_adyen/__init__.py b/addons/payment_adyen/__init__.py index 8c67f893b37..f2ff55f0433 100644 --- a/addons/payment_adyen/__init__.py +++ b/addons/payment_adyen/__init__.py @@ -3,3 +3,4 @@ from . import models from . import controllers +from odoo.addons.payment.models.payment_acquirer import create_missing_journal_for_acquirers diff --git a/addons/payment_adyen/__manifest__.py b/addons/payment_adyen/__manifest__.py index 1e26a8808ca..8b04a9834e2 100644 --- a/addons/payment_adyen/__manifest__.py +++ b/addons/payment_adyen/__manifest__.py @@ -13,4 +13,5 @@ 'data/payment_acquirer_data.xml', ], 'installable': True, + 'post_init_hook': 'create_missing_journal_for_acquirers', } diff --git a/addons/payment_authorize/__init__.py b/addons/payment_authorize/__init__.py index 8c67f893b37..f2ff55f0433 100644 --- a/addons/payment_authorize/__init__.py +++ b/addons/payment_authorize/__init__.py @@ -3,3 +3,4 @@ from . import models from . import controllers +from odoo.addons.payment.models.payment_acquirer import create_missing_journal_for_acquirers diff --git a/addons/payment_authorize/__manifest__.py b/addons/payment_authorize/__manifest__.py index 4a8eafa8b73..af6c1e946b9 100644 --- a/addons/payment_authorize/__manifest__.py +++ b/addons/payment_authorize/__manifest__.py @@ -13,4 +13,5 @@ 'data/payment_acquirer_data.xml', ], 'installable': True, + 'post_init_hook': 'create_missing_journal_for_acquirers', } diff --git a/addons/payment_buckaroo/__init__.py b/addons/payment_buckaroo/__init__.py index 8c67f893b37..f2ff55f0433 100644 --- a/addons/payment_buckaroo/__init__.py +++ b/addons/payment_buckaroo/__init__.py @@ -3,3 +3,4 @@ from . import models from . import controllers +from odoo.addons.payment.models.payment_acquirer import create_missing_journal_for_acquirers diff --git a/addons/payment_buckaroo/__manifest__.py b/addons/payment_buckaroo/__manifest__.py index 3e56a451c52..f08e9152473 100644 --- a/addons/payment_buckaroo/__manifest__.py +++ b/addons/payment_buckaroo/__manifest__.py @@ -13,4 +13,5 @@ 'data/payment_acquirer_data.xml', ], 'installable': True, + 'post_init_hook': 'create_missing_journal_for_acquirers', } diff --git a/addons/payment_ogone/__init__.py b/addons/payment_ogone/__init__.py index 8c67f893b37..f2ff55f0433 100644 --- a/addons/payment_ogone/__init__.py +++ b/addons/payment_ogone/__init__.py @@ -3,3 +3,4 @@ from . import models from . import controllers +from odoo.addons.payment.models.payment_acquirer import create_missing_journal_for_acquirers diff --git a/addons/payment_ogone/__manifest__.py b/addons/payment_ogone/__manifest__.py index 1fd1ee9c55b..3866d1e115b 100644 --- a/addons/payment_ogone/__manifest__.py +++ b/addons/payment_ogone/__manifest__.py @@ -13,4 +13,5 @@ 'data/payment_acquirer_data.xml', ], 'installable': True, + 'post_init_hook': 'create_missing_journal_for_acquirers', } diff --git a/addons/payment_paypal/__init__.py b/addons/payment_paypal/__init__.py index 8c67f893b37..f2ff55f0433 100644 --- a/addons/payment_paypal/__init__.py +++ b/addons/payment_paypal/__init__.py @@ -3,3 +3,4 @@ from . import models from . import controllers +from odoo.addons.payment.models.payment_acquirer import create_missing_journal_for_acquirers diff --git a/addons/payment_paypal/__manifest__.py b/addons/payment_paypal/__manifest__.py index 352b130e051..bf2e9e90b51 100644 --- a/addons/payment_paypal/__manifest__.py +++ b/addons/payment_paypal/__manifest__.py @@ -13,4 +13,5 @@ 'data/payment_acquirer_data.xml', ], 'installable': True, + 'post_init_hook': 'create_missing_journal_for_acquirers', } diff --git a/addons/payment_payumoney/__init__.py b/addons/payment_payumoney/__init__.py index 8c67f893b37..f2ff55f0433 100644 --- a/addons/payment_payumoney/__init__.py +++ b/addons/payment_payumoney/__init__.py @@ -3,3 +3,4 @@ from . import models from . import controllers +from odoo.addons.payment.models.payment_acquirer import create_missing_journal_for_acquirers diff --git a/addons/payment_payumoney/__manifest__.py b/addons/payment_payumoney/__manifest__.py index 794ba6b6de1..66f83456fcb 100644 --- a/addons/payment_payumoney/__manifest__.py +++ b/addons/payment_payumoney/__manifest__.py @@ -16,4 +16,5 @@ 'views/payment_payumoney_templates.xml', 'data/payment_acquirer_data.xml', ], + 'post_init_hook': 'create_missing_journal_for_acquirers', } diff --git a/addons/payment_sips/__init__.py b/addons/payment_sips/__init__.py index f7209b17100..c7d74c9c782 100644 --- a/addons/payment_sips/__init__.py +++ b/addons/payment_sips/__init__.py @@ -1,2 +1,3 @@ from . import models from . import controllers +from odoo.addons.payment.models.payment_acquirer import create_missing_journal_for_acquirers diff --git a/addons/payment_sips/__manifest__.py b/addons/payment_sips/__manifest__.py index 6039f962009..2764807c5ea 100644 --- a/addons/payment_sips/__manifest__.py +++ b/addons/payment_sips/__manifest__.py @@ -20,4 +20,5 @@ payments acquirer using Worldline SIPS.""", 'data/payment_acquirer_data.xml', ], 'installable': True, + 'post_init_hook': 'create_missing_journal_for_acquirers', } diff --git a/addons/payment_stripe/__init__.py b/addons/payment_stripe/__init__.py index 8c67f893b37..f2ff55f0433 100644 --- a/addons/payment_stripe/__init__.py +++ b/addons/payment_stripe/__init__.py @@ -3,3 +3,4 @@ from . import models from . import controllers +from odoo.addons.payment.models.payment_acquirer import create_missing_journal_for_acquirers diff --git a/addons/payment_stripe/__manifest__.py b/addons/payment_stripe/__manifest__.py index 6eb664791cb..247fe74859b 100644 --- a/addons/payment_stripe/__manifest__.py +++ b/addons/payment_stripe/__manifest__.py @@ -14,4 +14,5 @@ ], 'images': ['static/description/icon.png'], 'installable': True, + 'post_init_hook': 'create_missing_journal_for_acquirers', } diff --git a/addons/payment_transfer/__init__.py b/addons/payment_transfer/__init__.py index 8c67f893b37..f2ff55f0433 100644 --- a/addons/payment_transfer/__init__.py +++ b/addons/payment_transfer/__init__.py @@ -3,3 +3,4 @@ from . import models from . import controllers +from odoo.addons.payment.models.payment_acquirer import create_missing_journal_for_acquirers diff --git a/addons/payment_transfer/__manifest__.py b/addons/payment_transfer/__manifest__.py index 9f8704467dc..530fe49ede4 100644 --- a/addons/payment_transfer/__manifest__.py +++ b/addons/payment_transfer/__manifest__.py @@ -14,4 +14,5 @@ ], 'installable': True, 'auto_install': True, + 'post_init_hook': 'create_missing_journal_for_acquirers', } diff --git a/addons/payment_transfer/models/payment.py b/addons/payment_transfer/models/payment.py index 00a34ac0ad4..18c74d2246d 100644 --- a/addons/payment_transfer/models/payment.py +++ b/addons/payment_transfer/models/payment.py @@ -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'