From 8a761721d1e4b1db9ad74882c5649c577aeaecf0 Mon Sep 17 00:00:00 2001 From: Joren Van Onder Date: Thu, 28 Apr 2016 09:13:51 +0200 Subject: [PATCH] [IMP] payment: add options to automatically confirm SO's through website Introduces two new options: - confirm_so: to confirm the sale order on acquirer confirmation - generate_and_pay_invoice: confirm_so + generation of invoice and payment registration --- addons/account/models/account_payment.py | 2 +- addons/payment/__openerp__.py | 1 + addons/payment/models/__init__.py | 1 + addons/payment/models/account_payment.py | 8 ++++++ addons/payment/models/payment_acquirer.py | 7 ++--- addons/payment/views/account_payment.xml | 15 +++++++++++ addons/payment/views/payment_acquirer.xml | 1 + addons/website_payment/models/payment.py | 26 +++++++++++++++++-- addons/website_quote/controllers/main.py | 3 --- addons/website_sale/controllers/main.py | 4 --- .../views/report_shop_saleorder.xml | 2 +- addons/website_sale/views/templates.xml | 2 +- 12 files changed, 57 insertions(+), 15 deletions(-) create mode 100644 addons/payment/models/account_payment.py create mode 100644 addons/payment/views/account_payment.xml diff --git a/addons/account/models/account_payment.py b/addons/account/models/account_payment.py index 9e47e4b4268..b2587234999 100644 --- a/addons/account/models/account_payment.py +++ b/addons/account/models/account_payment.py @@ -42,7 +42,7 @@ class account_abstract_payment(models.AbstractModel): currency_id = fields.Many2one('res.currency', string='Currency', required=True, default=lambda self: self.env.user.company_id.currency_id) payment_date = fields.Date(string='Payment Date', default=fields.Date.context_today, required=True, copy=False) communication = fields.Char(string='Memo') - journal_id = fields.Many2one('account.journal', string='Payment Method', required=True, domain=[('type', 'in', ('bank', 'cash'))]) + journal_id = fields.Many2one('account.journal', string='Payment Journal', required=True, domain=[('type', 'in', ('bank', 'cash'))]) company_id = fields.Many2one('res.company', related='journal_id.company_id', string='Company', readonly=True) hide_payment_method = fields.Boolean(compute='_compute_hide_payment_method', diff --git a/addons/payment/__openerp__.py b/addons/payment/__openerp__.py index b472a4460c6..d547a2f5568 100644 --- a/addons/payment/__openerp__.py +++ b/addons/payment/__openerp__.py @@ -13,6 +13,7 @@ 'views/assets.xml', 'views/res_config_view.xml', 'views/res_partner_view.xml', + 'views/account_payment.xml', 'security/ir.model.access.csv', 'security/payment_security.xml', ], diff --git a/addons/payment/models/__init__.py b/addons/payment/models/__init__.py index e9c1546ea12..e5b3f93737c 100644 --- a/addons/payment/models/__init__.py +++ b/addons/payment/models/__init__.py @@ -2,3 +2,4 @@ import payment_acquirer import res_partner +import account_payment diff --git a/addons/payment/models/account_payment.py b/addons/payment/models/account_payment.py new file mode 100644 index 00000000000..20bc9c9cd28 --- /dev/null +++ b/addons/payment/models/account_payment.py @@ -0,0 +1,8 @@ +# coding: utf-8 + +from odoo import fields, models + +class AccountPayment(models.Model): + _inherit = 'account.payment' + + payment_transaction_id = fields.Many2one('payment.transaction', string="Payment Transaction") diff --git a/addons/payment/models/payment_acquirer.py b/addons/payment/models/payment_acquirer.py index 7d44d2d16fa..474ccb5aaea 100644 --- a/addons/payment/models/payment_acquirer.py +++ b/addons/payment/models/payment_acquirer.py @@ -79,8 +79,8 @@ class PaymentAcquirer(osv.Model): help="Make this payment acquirer available (Customer invoices, etc.)"), 'auto_confirm': fields.selection( [('none', 'No automatic confirmation'), - ('at_pay_confirm', 'At payment with acquirer confirmation'), - ('at_pay_now', 'At payment no acquirer confirmation needed')], + ('confirm_so', 'Confirm the SO on acquirer confirmation'), + ('generate_and_pay_invoice', 'On acquirer confirmation, confirm the SO, generate the invoice and pay it')], string='Order Confirmation', required=True), 'pending_msg': fields.html('Pending Message', translate=True, help='Message displayed, if order is in pending state after having done the payment process.'), 'done_msg': fields.html('Done Message', translate=True, help='Message displayed, if order is done successfully after having done the payment process.'), @@ -96,6 +96,7 @@ class PaymentAcquirer(osv.Model): 'module_id': fields.many2one('ir.module.module', string='Corresponding Module'), 'module_state': fields.related('module_id', 'state', type='char', string='Installation State'), 'description': fields.html('Description'), + 'journal_id': fields.many2one('account.journal', 'Accounting Journal', help="Account journal used for automatic payment reconciliation."), } image = openerp.fields.Binary("Image", attachment=True, @@ -113,7 +114,7 @@ class PaymentAcquirer(osv.Model): 'company_id': lambda self, cr, uid, obj, ctx=None: self.pool['res.users'].browse(cr, uid, uid).company_id.id, 'environment': 'test', 'website_published': False, - 'auto_confirm': 'at_pay_confirm', + 'auto_confirm': 'confirm_so', 'pending_msg': 'Pending, Your online payment has been successfully processed. But your order is not validated yet.', 'done_msg': 'Done, Your online payment has been successfully processed. Thank you for your order.', 'cancel_msg': 'Cancel, Your payment has been cancelled.', diff --git a/addons/payment/views/account_payment.xml b/addons/payment/views/account_payment.xml new file mode 100644 index 00000000000..00c25c4af8f --- /dev/null +++ b/addons/payment/views/account_payment.xml @@ -0,0 +1,15 @@ + + + + + view.account.payment.form.inherit.payment + account.payment + + + + + + + + + diff --git a/addons/payment/views/payment_acquirer.xml b/addons/payment/views/payment_acquirer.xml index 3c6ec8d88c5..21740a2715c 100644 --- a/addons/payment/views/payment_acquirer.xml +++ b/addons/payment/views/payment_acquirer.xml @@ -78,6 +78,7 @@ + diff --git a/addons/website_payment/models/payment.py b/addons/website_payment/models/payment.py index ed0a453e1af..7231bda4147 100644 --- a/addons/website_payment/models/payment.py +++ b/addons/website_payment/models/payment.py @@ -11,13 +11,32 @@ class PaymentAcquirer(models.Model): _name = 'payment.acquirer' _inherit = ['payment.acquirer','website.published.mixin'] - class PaymentTransaction(models.Model): _inherit = 'payment.transaction' # link with the sale order sale_order_id = fields.Many2one('sale.order', string='Sale Order') + def _generate_and_pay_invoice(self, tx, acquirer_name): + created_invoice = tx.sale_order_id.action_invoice_create() + created_invoice = self.env['account.invoice'].browse(created_invoice) + + if created_invoice: + _logger.info('<%s> transaction completed, auto-generated invoice %s (ID %s) for %s (ID %s)', + acquirer_name, created_invoice.name, created_invoice.id, tx.sale_order_id.name, tx.sale_order_id.id) + + created_invoice.signal_workflow('invoice_open') + if tx.acquirer_id.journal_id: + created_invoice.pay_and_reconcile(tx.acquirer_id.journal_id, pay_amount=created_invoice.amount_total) + if created_invoice.payment_ids: + created_invoice.payment_ids[0].payment_transaction_id = tx + else: + _logger.warning('<%s> transaction completed, could not auto-generate payment for %s (ID %s) (no journal set on acquirer)', + acquirer_name, tx.sale_order_id.name, tx.sale_order_id.id) + else: + _logger.warning('<%s> transaction completed, could not auto-generate invoice for %s (ID %s)', + acquirer_name, tx.sale_order_id.name, tx.sale_order_id.id) + @api.model def form_feedback(self, data, acquirer_name): """ Override to confirm the sale order, if defined, and if the transaction @@ -36,9 +55,12 @@ class PaymentTransaction(models.Model): # verify SO/TX match, excluding tx.fees which are currently not included in SO amount_matches = float_compare(tx.amount, tx.sale_order_id.amount_total, 2) == 0 if amount_matches: - if tx.state == 'done' and tx.acquirer_id.auto_confirm == 'at_pay_confirm': + if tx.state == 'done' and tx.acquirer_id.auto_confirm in ['confirm_so', 'generate_and_pay_invoice']: _logger.info('<%s> transaction completed, auto-confirming order %s (ID %s)', acquirer_name, tx.sale_order_id.name, tx.sale_order_id.id) tx.sale_order_id.with_context(send_email=True).action_confirm() + + if tx.acquirer_id.auto_confirm == 'generate_and_pay_invoice': + self._generate_and_pay_invoice(tx, acquirer_name) elif tx.state not in ['cancel', 'error'] and tx.sale_order_id.state == 'draft': _logger.info('<%s> transaction pending/to confirm manually, sending quote email for order %s (ID %s)', acquirer_name, tx.sale_order_id.name, tx.sale_order_id.id) tx.sale_order_id.force_quotation_send() diff --git a/addons/website_quote/controllers/main.py b/addons/website_quote/controllers/main.py index 7a3f74bb90e..42b5b0b2dac 100644 --- a/addons/website_quote/controllers/main.py +++ b/addons/website_quote/controllers/main.py @@ -219,9 +219,6 @@ class sale_quote(http.Controller): 'payment_tx_id': tx_id }, context=context) - # confirm the quotation - if tx.acquirer_id.auto_confirm == 'at_pay_now': - request.registry['sale.order'].action_confirm(cr, SUPERUSER_ID, [order.id], context=dict(request.context, send_email=True)) return payment_obj.render( request.cr, SUPERUSER_ID, tx.acquirer_id.id, tx.reference, diff --git a/addons/website_sale/controllers/main.py b/addons/website_sale/controllers/main.py index c059ca2692c..c82a16abf80 100644 --- a/addons/website_sale/controllers/main.py +++ b/addons/website_sale/controllers/main.py @@ -778,10 +778,6 @@ class WebsiteSale(http.Controller): 'payment_tx_id': request.session['sale_transaction_id'] }) - # confirm the quotation - if tx.acquirer_id.auto_confirm == 'at_pay_now': - order.with_context(send_email=True).action_confirm() - return tx.acquirer_id.with_context(submit_class='btn btn-primary', submit_txt=_('Pay Now')).sudo().render( tx.reference, order.amount_total, diff --git a/addons/website_sale/views/report_shop_saleorder.xml b/addons/website_sale/views/report_shop_saleorder.xml index 74519e6b78a..6c0954f6c20 100644 --- a/addons/website_sale/views/report_shop_saleorder.xml +++ b/addons/website_sale/views/report_shop_saleorder.xml @@ -18,7 +18,7 @@ - + diff --git a/addons/website_sale/views/templates.xml b/addons/website_sale/views/templates.xml index 4f9aa1043df..04abfcd9737 100644 --- a/addons/website_sale/views/templates.xml +++ b/addons/website_sale/views/templates.xml @@ -1454,7 +1454,7 @@ - +