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/models/__init__.py b/partner_sale_risk/models/__init__.py new file mode 100644 index 000000000..b2be41d4d --- /dev/null +++ b/partner_sale_risk/models/__init__.py @@ -0,0 +1,4 @@ +# -*- coding: utf-8 -*- + +from . import res_partner +from . import sale diff --git a/partner_sale_risk/models/res_partner.py b/partner_sale_risk/models/res_partner.py new file mode 100644 index 000000000..f5d669ebd --- /dev/null +++ b/partner_sale_risk/models/res_partner.py @@ -0,0 +1,32 @@ +# -*- 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') + def _compute_risk_sale_order(self): + for partner in self: + partner.risk_sale_order = sum( + partner.sale_order_ids.mapped('invoice_pending_amount')) + + @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..c79494936 --- /dev/null +++ b/partner_sale_risk/models/sale.py @@ -0,0 +1,50 @@ +# -*- 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): + for order in self.filtered(lambda x: x.state == 'sale'): + order.invoice_amount = sum( + order.invoice_ids.mapped('amount_total')) + order.invoice_pending_amount = ( + order.amount_total > order.invoice_amount and + order.amount_total - order.invoice_amount or 0.0) + + @api.multi + def action_confirm(self): + if not self.env.context.get('bypass_risk', False): + partner = self.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/test_partner_sale_risk.py b/partner_sale_risk/tests/test_partner_sale_risk.py new file mode 100644 index 000000000..7be22c953 --- /dev/null +++ b/partner_sale_risk/tests/test_partner_sale_risk.py @@ -0,0 +1,53 @@ +# -*- coding: utf-8 -*- +# © 2016 Carlos Dauden +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from openerp.tests.common import TransactionCase + + +class TestPartnerSaleRisk(TransactionCase): + def setUp(self): + super(TestPartnerSaleRisk, self).setUp() + self.env.user.groups_id |= self.env.ref('base.group_sale_manager') + self.partner = self.env['res.partner'].create({ + 'name': 'Partner test', + 'customer': True, + }) + self.product = self.env.ref('product.product_product_2') + self.product.invoice_policy = 'order' + self.sale_order = self.env['sale.order'].create({ + 'partner_id': self.partner.id, + 'pricelist_id': self.env.ref('product.list0').id, + 'order_line': [(0, 0, { + 'name': self.product.name, + 'product_id': self.product.id, + 'product_uom_qty': 1, + 'product_uom': self.product.uom_id.id, + 'price_unit': 100.0})], + }) + + 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) 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 + + + + + + + + +