From 6fa6e57321cbef9950b4dcdf6c314dbd0d8db951 Mon Sep 17 00:00:00 2001 From: Carlos Dauden Date: Mon, 24 Oct 2016 19:36:56 +0200 Subject: [PATCH] [9.0][ADD] partner_sale_risk: New module --- partner_sale_risk/README.rst | 60 +++++++++++++ partner_sale_risk/__init__.py | 3 + partner_sale_risk/__openerp__.py | 19 ++++ partner_sale_risk/i18n/es.po | 89 +++++++++++++++++++ partner_sale_risk/models/__init__.py | 4 + partner_sale_risk/models/res_partner.py | 42 +++++++++ partner_sale_risk/models/sale.py | 60 +++++++++++++ partner_sale_risk/tests/__init__.py | 3 + .../tests/test_partner_sale_risk.py | 63 +++++++++++++ partner_sale_risk/views/res_partner_view.xml | 21 +++++ partner_sale_risk/views/sale_view.xml | 16 ++++ 11 files changed, 380 insertions(+) create mode 100644 partner_sale_risk/README.rst create mode 100644 partner_sale_risk/__init__.py create mode 100644 partner_sale_risk/__openerp__.py create mode 100644 partner_sale_risk/i18n/es.po create mode 100644 partner_sale_risk/models/__init__.py create mode 100644 partner_sale_risk/models/res_partner.py create mode 100644 partner_sale_risk/models/sale.py create mode 100644 partner_sale_risk/tests/__init__.py create mode 100644 partner_sale_risk/tests/test_partner_sale_risk.py create mode 100644 partner_sale_risk/views/res_partner_view.xml create mode 100644 partner_sale_risk/views/sale_view.xml diff --git a/partner_sale_risk/README.rst b/partner_sale_risk/README.rst new file mode 100644 index 000000000..500925870 --- /dev/null +++ b/partner_sale_risk/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 + +================= +Partner Sale Risk +================= + +Extends Partner Financial Risk to manage sales orders. + +If any limit is exceed the partner gets forbidden to confirm sale orders. + +Usage +===== + +To use this module, you need to: + +#. Go to *Customers > Financial Risk* +#. Set limits and choose options to compute in credit limit. +#. Go to *Sales -> Sales Orders* and create a new Sales Orders. + + +.. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas + :alt: Try me on Runbot + :target: https://runbot.odoo-community.org/runbot/134/9.0 + + +Bug Tracker +=========== + +Bugs are tracked on `GitHub Issues +`_. In case of trouble, please +check there if your issue has already been reported. If you spotted it first, +help us smashing it by providing a detailed and welcomed feedback. + + +Credits +======= + +Contributors +------------ + +* Carlos Dauden +* Pedro M. Baeza + + +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/partner_sale_risk/__init__.py b/partner_sale_risk/__init__.py new file mode 100644 index 000000000..cde864bae --- /dev/null +++ b/partner_sale_risk/__init__.py @@ -0,0 +1,3 @@ +# -*- coding: utf-8 -*- + +from . import models diff --git a/partner_sale_risk/__openerp__.py b/partner_sale_risk/__openerp__.py new file mode 100644 index 000000000..da484210f --- /dev/null +++ b/partner_sale_risk/__openerp__.py @@ -0,0 +1,19 @@ +# -*- coding: utf-8 -*- +# © 2016 Carlos Dauden +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +{ + 'name': 'Partner Sale Risk', + 'summary': 'Manage partner risk in sales orders', + 'version': '9.0.1.0.0', + 'category': 'Sales Management', + 'license': 'AGPL-3', + 'author': 'Tecnativa, Odoo Community Association (OCA)', + 'website': 'https://www.tecnativa.com', + 'depends': ['sale', 'partner_financial_risk'], + 'data': [ + 'views/res_partner_view.xml', + 'views/sale_view.xml', + ], + 'installable': True, +} diff --git a/partner_sale_risk/i18n/es.po b/partner_sale_risk/i18n/es.po new file mode 100644 index 000000000..caa01bb64 --- /dev/null +++ b/partner_sale_risk/i18n/es.po @@ -0,0 +1,89 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * partner_sale_risk +# +# Translators: +# Carlos Dauden , 2017 +msgid "" +msgstr "" +"Project-Id-Version: partner-contact (9.0)\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2017-05-29 13:25+0200\n" +"PO-Revision-Date: 2017-05-29 13:31+0200\n" +"Last-Translator: Carlos Dauden \n" +"Language-Team: Spanish (http://www.transifex.com/oca/OCA-partner-contact-9-0/" +"language/es/)\n" +"Language: es\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" +"Plural-Forms: nplurals=2; plural=(n != 1);\n" +"X-Generator: Poedit 1.8.7.1\n" + +#. module: partner_sale_risk +#: code:addons/partner_sale_risk/models/sale.py:42 +#, python-format +msgid "Financial risk exceeded.\n" +msgstr "Riesgo financiero excedido.\n" + +#. module: partner_sale_risk +#: model:ir.model.fields,help:partner_sale_risk.field_res_partner_risk_sale_order_include +msgid "Full risk computation" +msgstr "Cómputo de riesgo total" + +#. module: partner_sale_risk +#: model:ir.model.fields,field_description:partner_sale_risk.field_res_partner_risk_sale_order_include +msgid "Include Sales Orders" +msgstr "Incluir pedidos de venta" + +#. module: partner_sale_risk +#: model:ir.model.fields,field_description:partner_sale_risk.field_sale_order_invoice_amount +msgid "Invoice amount" +msgstr "Importe factura" + +#. module: partner_sale_risk +#: model:ir.model.fields,field_description:partner_sale_risk.field_sale_order_invoice_pending_amount +msgid "Invoice pending amount" +msgstr "Pendiente de facturar" + +#. module: partner_sale_risk +#: model:ir.model.fields,field_description:partner_sale_risk.field_res_partner_risk_sale_order_limit +msgid "Limit Sales Orders" +msgstr "Límite en pedidos" + +#. module: partner_sale_risk +#: model:ir.model,name:partner_sale_risk.model_res_partner +msgid "Partner" +msgstr "Empresa" + +#. module: partner_sale_risk +#: model:ir.model,name:partner_sale_risk.model_sale_order +msgid "Sales Order" +msgstr "Pedido de venta" + +#. module: partner_sale_risk +#: model:ir.model.fields,help:partner_sale_risk.field_res_partner_risk_sale_order_limit +msgid "Set 0 if it is not locked" +msgstr "Establece 0 si no está bloqueado" + +#. module: partner_sale_risk +#: code:addons/partner_sale_risk/models/sale.py:51 +#, python-format +msgid "This sale order exceeds the financial risk.\n" +msgstr "Este pedido excede el riesgo financiero.\n" + +#. module: partner_sale_risk +#: code:addons/partner_sale_risk/models/sale.py:46 +#, python-format +msgid "This sale order exceeds the sales orders risk.\n" +msgstr "Este pedido excede el riesgo en pedidos.\n" + +#. module: partner_sale_risk +#: model:ir.model.fields,field_description:partner_sale_risk.field_res_partner_risk_sale_order +msgid "Total Sales Orders Not Invoiced" +msgstr "Total de pedidos de venta no facturados" + +#. module: partner_sale_risk +#: model:ir.model.fields,help:partner_sale_risk.field_res_partner_risk_sale_order +msgid "Total not invoiced of sales orders in Sale Order state" +msgstr "Total no facturado" diff --git a/partner_sale_risk/models/__init__.py b/partner_sale_risk/models/__init__.py new file mode 100644 index 000000000..d078fbbd3 --- /dev/null +++ b/partner_sale_risk/models/__init__.py @@ -0,0 +1,4 @@ +# -*- coding: utf-8 -*- + +from . import sale +from . import res_partner diff --git a/partner_sale_risk/models/res_partner.py b/partner_sale_risk/models/res_partner.py new file mode 100644 index 000000000..1f5a3703a --- /dev/null +++ b/partner_sale_risk/models/res_partner.py @@ -0,0 +1,42 @@ +# -*- coding: utf-8 -*- +# © 2016 Carlos Dauden +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from openerp import api, fields, models + + +class ResPartner(models.Model): + _inherit = 'res.partner' + + risk_sale_order_include = fields.Boolean( + string='Include Sales Orders', help='Full risk computation') + risk_sale_order_limit = fields.Monetary( + string='Limit Sales Orders', help='Set 0 if it is not locked') + risk_sale_order = fields.Monetary( + compute='_compute_risk_sale_order', store=True, + string='Total Sales Orders Not Invoiced', + help='Total not invoiced of sales orders in Sale Order state') + + @api.multi + @api.depends('sale_order_ids', 'sale_order_ids.invoice_pending_amount', + 'child_ids.sale_order_ids', + 'child_ids.sale_order_ids.invoice_pending_amount') + def _compute_risk_sale_order(self): + customers = self.filtered('customer') + partners = customers | customers.mapped('child_ids') + orders_group = self.env['sale.order'].read_group( + [('state', '=', 'sale'), ('partner_id', 'in', partners.ids)], + ['partner_id', 'invoice_pending_amount'], + ['partner_id']) + for partner in customers: + partner_ids = (partner | partner.child_ids).ids + partner.risk_sale_order = sum( + [x['invoice_pending_amount'] + for x in orders_group if x['partner_id'][0] in partner_ids]) + + @api.model + def _risk_field_list(self): + res = super(ResPartner, self)._risk_field_list() + res.append(('risk_sale_order', 'risk_sale_order_limit', + 'risk_sale_order_include')) + return res diff --git a/partner_sale_risk/models/sale.py b/partner_sale_risk/models/sale.py new file mode 100644 index 000000000..962b9aef8 --- /dev/null +++ b/partner_sale_risk/models/sale.py @@ -0,0 +1,60 @@ +# -*- coding: utf-8 -*- +# © 2016 Carlos Dauden +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from openerp import api, fields, models, _ + + +class SaleOrder(models.Model): + _inherit = 'sale.order' + + invoice_amount = fields.Monetary( + compute='_compute_invoice_amount', store=True) + invoice_pending_amount = fields.Monetary( + compute='_compute_invoice_amount', store=True) + + @api.multi + @api.depends('state', 'order_line.invoice_lines.invoice_id.amount_total') + def _compute_invoice_amount(self): + AccountInvoice = self.env['account.invoice'] + for order in self.filtered(lambda x: x.state == 'sale'): + invoice_ids = order.order_line.mapped( + 'invoice_lines.invoice_id').ids + if not invoice_ids: + order.invoice_pending_amount = order.amount_total + continue + amount = AccountInvoice.read_group( + [('id', 'in', invoice_ids), + ('type', 'in', ['out_invoice', 'out_refund'])], + ['amount_total'], + [] + )[0]['amount_total'] + order.invoice_amount = amount + if order.amount_total > amount: + order.invoice_pending_amount = order.amount_total - amount + + @api.multi + def action_confirm(self): + if not self.env.context.get('bypass_risk', False): + partner = self.partner_id.commercial_partner_id + exception_msg = "" + if partner.risk_exception: + exception_msg = _("Financial risk exceeded.\n") + elif partner.risk_sale_order_limit and ( + (partner.risk_sale_order + self.amount_total) > + partner.risk_sale_order_limit): + exception_msg = _( + "This sale order exceeds the sales orders risk.\n") + elif partner.risk_sale_order_include and ( + (partner.risk_total + self.amount_total) > + partner.credit_limit): + exception_msg = _( + "This sale order exceeds the financial risk.\n") + if exception_msg: + return self.env['partner.risk.exceeded.wiz'].create({ + 'exception_msg': exception_msg, + 'partner_id': partner.id, + 'origin_reference': '%s,%s' % (self._model, self.id), + 'continue_method': 'action_confirm', + }).action_show() + return super(SaleOrder, self).action_confirm() diff --git a/partner_sale_risk/tests/__init__.py b/partner_sale_risk/tests/__init__.py new file mode 100644 index 000000000..9d92890e1 --- /dev/null +++ b/partner_sale_risk/tests/__init__.py @@ -0,0 +1,3 @@ +# -*- coding: utf-8 -*- + +from . import test_partner_sale_risk diff --git a/partner_sale_risk/tests/test_partner_sale_risk.py b/partner_sale_risk/tests/test_partner_sale_risk.py new file mode 100644 index 000000000..fac2f475f --- /dev/null +++ b/partner_sale_risk/tests/test_partner_sale_risk.py @@ -0,0 +1,63 @@ +# -*- coding: utf-8 -*- +# © 2016 Carlos Dauden +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from openerp.tests.common import SavepointCase + + +class TestPartnerSaleRisk(SavepointCase): + @classmethod + def setUpClass(cls): + super(TestPartnerSaleRisk, cls).setUpClass() + cls.env.user.groups_id |= cls.env.ref('base.group_sale_manager') + cls.partner = cls.env['res.partner'].create({ + 'name': 'Partner test', + 'customer': True, + }) + cls.product = cls.env.ref('product.product_product_2') + cls.product.invoice_policy = 'order' + cls.sale_order = cls.env['sale.order'].create({ + 'partner_id': cls.partner.id, + 'pricelist_id': cls.env.ref('product.list0').id, + 'order_line': [(0, 0, { + 'name': cls.product.name, + 'product_id': cls.product.id, + 'product_uom_qty': 1, + 'product_uom': cls.product.uom_id.id, + 'price_unit': 100.0})], + }) + cls.env.user.lang = 'en_US' + + def test_sale_order(self): + self.sale_order.action_confirm() + self.assertAlmostEqual(self.partner.risk_sale_order, 100.0) + self.assertFalse(self.partner.risk_exception) + self.partner.risk_sale_order_limit = 99.0 + self.assertTrue(self.partner.risk_exception) + sale_order2 = self.sale_order.copy() + wiz_dic = sale_order2.action_confirm() + wiz = self.env[wiz_dic['res_model']].browse(wiz_dic['res_id']) + self.assertEqual(wiz.exception_msg, "Financial risk exceeded.\n") + self.partner.risk_sale_order_limit = 150.0 + wiz_dic = sale_order2.action_confirm() + wiz = self.env[wiz_dic['res_model']].browse(wiz_dic['res_id']) + self.assertEqual(wiz.exception_msg, + "This sale order exceeds the sales orders risk.\n") + self.partner.risk_sale_order_limit = 0.0 + self.partner.risk_sale_order_include = True + self.partner.credit_limit = 100.0 + wiz_dic = sale_order2.action_confirm() + wiz = self.env[wiz_dic['res_model']].browse(wiz_dic['res_id']) + self.assertEqual(wiz.exception_msg, + "This sale order exceeds the financial risk.\n") + self.assertTrue(self.partner.risk_allow_edit) + wiz.button_continue() + self.assertAlmostEqual(self.partner.risk_sale_order, 200.0) + + def test_invoice_amount(self): + self.sale_order.action_confirm() + self.assertAlmostEqual(self.sale_order.invoice_pending_amount, 100.0) + self.assertAlmostEqual(self.sale_order.invoice_amount, 0.0) + self.sale_order.action_invoice_create() + self.assertAlmostEqual(self.sale_order.invoice_pending_amount, 0.0) + self.assertAlmostEqual(self.sale_order.invoice_amount, 100.0) diff --git a/partner_sale_risk/views/res_partner_view.xml b/partner_sale_risk/views/res_partner_view.xml new file mode 100644 index 000000000..a4c24f13b --- /dev/null +++ b/partner_sale_risk/views/res_partner_view.xml @@ -0,0 +1,21 @@ + + + + + res.partner.view.risk + res.partner + + + + + + + + + + + + diff --git a/partner_sale_risk/views/sale_view.xml b/partner_sale_risk/views/sale_view.xml new file mode 100644 index 000000000..3fbf2e1aa --- /dev/null +++ b/partner_sale_risk/views/sale_view.xml @@ -0,0 +1,16 @@ + + + + + sale.order.form.invoice.amount + sale.order + + + + + + + + +