From db5418bf894bb6a277c81e1a207c3d2b001bd2b9 Mon Sep 17 00:00:00 2001 From: Guewen Baconnier Date: Tue, 23 Aug 2016 09:06:12 +0200 Subject: [PATCH] [WIP][9.0] Add sale_automatic_workflow_payment_mode (#297) [ADD] sale_automatic_workflow_payment_mode --- oca_dependencies.txt | 1 + .../README.rst | 60 +++++++++++++++++++ .../__init__.py | 5 ++ .../__openerp__.py | 20 +++++++ .../data/automatic_workflow_data.xml | 24 ++++++++ .../models/__init__.py | 8 +++ .../models/account_payment_mode.py | 14 +++++ .../models/automatic_workflow_job.py | 49 +++++++++++++++ .../models/sale_order.py | 17 ++++++ .../models/sale_workflow_process.py | 34 +++++++++++ .../views/account_payment_mode_views.xml | 26 ++++++++ .../views/sale_workflow_process_view.xml | 42 +++++++++++++ 12 files changed, 300 insertions(+) create mode 100644 sale_automatic_workflow_payment_mode/README.rst create mode 100644 sale_automatic_workflow_payment_mode/__init__.py create mode 100644 sale_automatic_workflow_payment_mode/__openerp__.py create mode 100644 sale_automatic_workflow_payment_mode/data/automatic_workflow_data.xml create mode 100644 sale_automatic_workflow_payment_mode/models/__init__.py create mode 100644 sale_automatic_workflow_payment_mode/models/account_payment_mode.py create mode 100644 sale_automatic_workflow_payment_mode/models/automatic_workflow_job.py create mode 100644 sale_automatic_workflow_payment_mode/models/sale_order.py create mode 100644 sale_automatic_workflow_payment_mode/models/sale_workflow_process.py create mode 100644 sale_automatic_workflow_payment_mode/views/account_payment_mode_views.xml create mode 100644 sale_automatic_workflow_payment_mode/views/sale_workflow_process_view.xml diff --git a/oca_dependencies.txt b/oca_dependencies.txt index 4cbc87802da..f1dfb48162e 100644 --- a/oca_dependencies.txt +++ b/oca_dependencies.txt @@ -1,3 +1,4 @@ +bank-payment stock-logistics-workflow stock-logistics-transport account-closing diff --git a/sale_automatic_workflow_payment_mode/README.rst b/sale_automatic_workflow_payment_mode/README.rst new file mode 100644 index 00000000000..92140659ca9 --- /dev/null +++ b/sale_automatic_workflow_payment_mode/README.rst @@ -0,0 +1,60 @@ +.. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 + +====================================== +Sale Automatic Workflow - Payment Mode +====================================== + +This module is a glue for **Account Payment Sale** (of the OCA/bank-payment +project) and **Sale Automatic Workflow**. + +When a payment mode is associated with an automatic workflow, this one +is automatically selected for the sales orders using this method. + +Installation +============ + +As soon as both **Account Payment Mode** and **Sale Automatic Workflow** +are installed, this module is installed. + +Configuration +============= + +The automatic workflow associated to a payment mode can be chosen in +the configuration of the payment modes in the Invoicing configuration menu. + +Usage +===== + +When a payment mode is selected on a sales order, if it has an +automatic workflow, the sales order will use it. + +Credits +======= + +Images +------ + +* Odoo Community Association: `Icon `_. + +Contributors +------------ + +* Guewen Baconnier +* Sodexis + +Maintainer +---------- + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +This module is maintained by the OCA. + +OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use. + +To contribute to this module, please visit https://odoo-community.org. diff --git a/sale_automatic_workflow_payment_mode/__init__.py b/sale_automatic_workflow_payment_mode/__init__.py new file mode 100644 index 00000000000..aec4252b662 --- /dev/null +++ b/sale_automatic_workflow_payment_mode/__init__.py @@ -0,0 +1,5 @@ +# -*- coding: utf-8 -*- +# © 2016 Camptocamp SA, Sodexis +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html) + +from . import models diff --git a/sale_automatic_workflow_payment_mode/__openerp__.py b/sale_automatic_workflow_payment_mode/__openerp__.py new file mode 100644 index 00000000000..98739b78796 --- /dev/null +++ b/sale_automatic_workflow_payment_mode/__openerp__.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# © 2016 Camptocamp SA, Sodexis +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html) + +{'name': 'Sale Automatic Workflow - Payment Mode', + 'version': '9.0.2.0.0', + 'author': 'Camptocamp,Sodexis,Odoo Community Association (OCA)', + 'license': 'AGPL-3', + 'category': 'Sales Management', + 'depends': ['sale_automatic_workflow', + 'account_payment_sale', # oca/bank-payment + ], + 'website': 'http://www.camptocamp.com', + 'data': ['data/automatic_workflow_data.xml', + 'views/account_payment_mode_views.xml', + 'views/sale_workflow_process_view.xml', + ], + 'installable': True, + 'auto_install': True, + } diff --git a/sale_automatic_workflow_payment_mode/data/automatic_workflow_data.xml b/sale_automatic_workflow_payment_mode/data/automatic_workflow_data.xml new file mode 100644 index 00000000000..a32f1bcc844 --- /dev/null +++ b/sale_automatic_workflow_payment_mode/data/automatic_workflow_data.xml @@ -0,0 +1,24 @@ + + + + + + Automatic Workflow Payment Filter + account.invoice + [('state', '=', 'open')] + + + + + + + + + + + + diff --git a/sale_automatic_workflow_payment_mode/models/__init__.py b/sale_automatic_workflow_payment_mode/models/__init__.py new file mode 100644 index 00000000000..71aca833bc6 --- /dev/null +++ b/sale_automatic_workflow_payment_mode/models/__init__.py @@ -0,0 +1,8 @@ +# -*- coding: utf-8 -*- +# © 2016 Camptocamp SA, Sodexis +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html) + +from . import account_payment_mode +from . import sale_order +from . import sale_workflow_process +from . import automatic_workflow_job diff --git a/sale_automatic_workflow_payment_mode/models/account_payment_mode.py b/sale_automatic_workflow_payment_mode/models/account_payment_mode.py new file mode 100644 index 00000000000..6b11e455c5f --- /dev/null +++ b/sale_automatic_workflow_payment_mode/models/account_payment_mode.py @@ -0,0 +1,14 @@ +# -*- coding: utf-8 -*- +# © 2016 Camptocamp SA, Sodexis +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html) + +from openerp import fields, models + + +class AccountPaymentMode(models.Model): + _inherit = 'account.payment.mode' + + workflow_process_id = fields.Many2one( + comodel_name='sale.workflow.process', + string='Automatic Workflow' + ) diff --git a/sale_automatic_workflow_payment_mode/models/automatic_workflow_job.py b/sale_automatic_workflow_payment_mode/models/automatic_workflow_job.py new file mode 100644 index 00000000000..70f0ed67e8b --- /dev/null +++ b/sale_automatic_workflow_payment_mode/models/automatic_workflow_job.py @@ -0,0 +1,49 @@ +# -*- coding: utf-8 -*- +# © 2016 Camptocamp SA, Sodexis +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html) + +import logging +from openerp import models, api, fields +from openerp.tools.safe_eval import safe_eval +from openerp.addons.sale_automatic_workflow.models.automatic_workflow_job \ + import commit + +_logger = logging.getLogger(__name__) + + +class AutomaticWorkflowJob(models.Model): + _inherit = 'automatic.workflow.job' + + @api.model + def run_with_workflow(self, sale_wkf): + workflow_domain = [('workflow_process_id', '=', sale_wkf.id)] + res = super(AutomaticWorkflowJob, self).run_with_workflow(sale_wkf) + if sale_wkf.register_payment: + self._register_payments( + safe_eval(sale_wkf.payment_filter_id.domain) + + workflow_domain) + return res + + @api.model + def _register_payments(self, payment_filter): + invoice_obj = self.env['account.invoice'] + invoices = invoice_obj.search(payment_filter) + _logger.debug('Invoices to Register Payment: %s', invoices.ids) + for invoice in invoices: + partner_type = invoice.type in ('out_invoice', 'out_refund') and \ + 'customer' or 'supplier' + payment_mode = invoice.payment_mode_id + with commit(self.env.cr): + payment = self.env['account.payment'].create({ + 'invoice_ids': [(6, 0, invoice.ids)], + 'amount': invoice.residual, + 'payment_date': fields.Date.context_today(self), + 'communication': invoice.reference or invoice.number, + 'partner_id': invoice.partner_id.id, + 'partner_type': partner_type, + 'payment_type': payment_mode.payment_type, + 'payment_method_id': payment_mode.payment_method_id.id, + 'journal_id': payment_mode.fixed_journal_id.id, + }) + payment.post() + return diff --git a/sale_automatic_workflow_payment_mode/models/sale_order.py b/sale_automatic_workflow_payment_mode/models/sale_order.py new file mode 100644 index 00000000000..91390e19b29 --- /dev/null +++ b/sale_automatic_workflow_payment_mode/models/sale_order.py @@ -0,0 +1,17 @@ +# -*- coding: utf-8 -*- +# © 2016 Camptocamp SA +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html) + +from openerp import api, models + + +class SaleOrder(models.Model): + _inherit = 'sale.order' + + @api.onchange('payment_mode_id') + def onchange_payment_mode_set_workflow(self): + if not self.payment_mode_id: + return + workflow = self.payment_mode_id.workflow_process_id + if workflow: + self.workflow_process_id = workflow diff --git a/sale_automatic_workflow_payment_mode/models/sale_workflow_process.py b/sale_automatic_workflow_payment_mode/models/sale_workflow_process.py new file mode 100644 index 00000000000..ba6ed176b84 --- /dev/null +++ b/sale_automatic_workflow_payment_mode/models/sale_workflow_process.py @@ -0,0 +1,34 @@ +# -*- coding: utf-8 -*- +# © 2016 Camptocamp SA, Sodexis +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html) + +from openerp import models, fields, api + + +class SaleWorkflowProcess(models.Model): + _inherit = "sale.workflow.process" + + @api.model + def _default_payment_filter_id(self): + xmlid = ('sale_automatic_workflow_payment_mode.' + 'automatic_workflow_payment_filter') + try: + return self.env.ref(xmlid) + except ValueError: + return self.env['ir.filters'].browse() + + payment_filter_id = fields.Many2one( + 'ir.filters', + string='Payment Filter', + default=_default_payment_filter_id, + ) + register_payment = fields.Boolean(string='Register Payment') + payment_filter_domain = fields.Char( + string='Payment Filter Domain', + default="[('state', '=', 'open')]" + ) + + @api.onchange('payment_filter_id') + def onchange_payment_filter_id(self): + if self.payment_filter_id: + self.payment_filter_domain = self.payment_filter_id.domain diff --git a/sale_automatic_workflow_payment_mode/views/account_payment_mode_views.xml b/sale_automatic_workflow_payment_mode/views/account_payment_mode_views.xml new file mode 100644 index 00000000000..8c17c6e35e0 --- /dev/null +++ b/sale_automatic_workflow_payment_mode/views/account_payment_mode_views.xml @@ -0,0 +1,26 @@ + + + + + account.payment.mode.form + account.payment.mode + + + + + + + + + + account.payment.mode.tree + account.payment.mode + + + + + + + + + diff --git a/sale_automatic_workflow_payment_mode/views/sale_workflow_process_view.xml b/sale_automatic_workflow_payment_mode/views/sale_workflow_process_view.xml new file mode 100644 index 00000000000..203f1364b56 --- /dev/null +++ b/sale_automatic_workflow_payment_mode/views/sale_workflow_process_view.xml @@ -0,0 +1,42 @@ + + + + + + sale.order.form.automatic.inherit + sale.workflow.process + + + + + + + + + sale_workflow_process.tree.inherit + sale.workflow.process + + + + + + + + \ No newline at end of file