Browse Source
Merge pull request #391 from open-synergy/8.0-partner_sale_risk
Merge pull request #391 from open-synergy/8.0-partner_sale_risk
[BACKPORT][8.0] partner_sale_riskpull/463/head
committed by
GitHub
10 changed files with 388 additions and 0 deletions
-
61partner_sale_risk/README.rst
-
3partner_sale_risk/__init__.py
-
19partner_sale_risk/__openerp__.py
-
4partner_sale_risk/models/__init__.py
-
43partner_sale_risk/models/res_partner.py
-
65partner_sale_risk/models/sale.py
-
5partner_sale_risk/tests/__init__.py
-
147partner_sale_risk/tests/test_partner_sale_risk.py
-
23partner_sale_risk/views/res_partner_view.xml
-
18partner_sale_risk/views/sale_view.xml
@ -0,0 +1,61 @@ |
|||||
|
.. 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/8.0 |
||||
|
|
||||
|
|
||||
|
Bug Tracker |
||||
|
=========== |
||||
|
|
||||
|
Bugs are tracked on `GitHub Issues |
||||
|
<https://github.com/OCA/partner-contact/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 <carlos.dauden@tecnativa.com> |
||||
|
* Pedro M. Baeza <pedro.baeza@tecnativa.com> |
||||
|
* Andhitia Rama <andhitia.r@gmail.com> |
||||
|
|
||||
|
|
||||
|
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. |
@ -0,0 +1,3 @@ |
|||||
|
# -*- coding: utf-8 -*- |
||||
|
|
||||
|
from . import models |
@ -0,0 +1,19 @@ |
|||||
|
# -*- coding: utf-8 -*- |
||||
|
# © 2016 Carlos Dauden <carlos.dauden@tecnativa.com> |
||||
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). |
||||
|
|
||||
|
{ |
||||
|
'name': 'Partner Sale Risk', |
||||
|
'summary': 'Manage partner risk in sales orders', |
||||
|
'version': '8.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, |
||||
|
} |
@ -0,0 +1,4 @@ |
|||||
|
# -*- coding: utf-8 -*- |
||||
|
|
||||
|
from . import res_partner |
||||
|
from . import sale |
@ -0,0 +1,43 @@ |
|||||
|
# -*- coding: utf-8 -*- |
||||
|
# © 2016 Carlos Dauden <carlos.dauden@tecnativa.com> |
||||
|
# 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.Float( |
||||
|
string='Limit Sales Orders', help='Set 0 if it is not locked') |
||||
|
risk_sale_order = fields.Float( |
||||
|
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', 'not in', ['draft', 'sent', 'cancel', 'done']), |
||||
|
('partner_id', 'in', partners.ids)], |
||||
|
['partner_id', 'invoice_pending_amount'], |
||||
|
['partner_id']) |
||||
|
for partner in customers: |
||||
|
partner_ids = (partner | partner.mapped('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 |
@ -0,0 +1,65 @@ |
|||||
|
# -*- coding: utf-8 -*- |
||||
|
# © 2016 Carlos Dauden <carlos.dauden@tecnativa.com> |
||||
|
# 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.Float( |
||||
|
compute='_compute_invoice_amount', store=True) |
||||
|
invoice_pending_amount = fields.Float( |
||||
|
compute='_compute_invoice_amount', store=True) |
||||
|
|
||||
|
@api.multi |
||||
|
@api.depends('state', |
||||
|
'invoice_ids', |
||||
|
'invoice_ids.amount_total', |
||||
|
'order_line.invoice_lines.invoice_id.amount_total') |
||||
|
def _compute_invoice_amount(self): |
||||
|
AccountInvoice = self.env['account.invoice'] |
||||
|
for order in self: |
||||
|
order.invoice_pending_amount = order.amount_total |
||||
|
invoice_ids = order.order_line.mapped('invoice_lines').mapped( |
||||
|
'invoice_id').ids |
||||
|
invoice_ids += order.mapped('invoice_ids').ids |
||||
|
if not invoice_ids: |
||||
|
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_button_confirm(self): |
||||
|
if not self.env.context.get('bypass_risk', False): |
||||
|
for order in self: |
||||
|
partner = order.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_button_confirm', |
||||
|
}).action_show() |
||||
|
return super(SaleOrder, self).action_button_confirm() |
@ -0,0 +1,5 @@ |
|||||
|
# -*- coding: utf-8 -*- |
||||
|
# Copyright 2016 Carlos Dauden <carlos.dauden@tecnativa.com> |
||||
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). |
||||
|
|
||||
|
from . import test_partner_sale_risk |
@ -0,0 +1,147 @@ |
|||||
|
# -*- coding: utf-8 -*- |
||||
|
# © 2016 Carlos Dauden <carlos.dauden@tecnativa.com> |
||||
|
# 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})], |
||||
|
}) |
||||
|
self.wizard = self.env[ |
||||
|
"sale.advance.payment.inv"] |
||||
|
|
||||
|
def test_sale_order_1(self): |
||||
|
""" |
||||
|
Scenario: |
||||
|
* 1 sale order @ 100 EUR |
||||
|
* Manual invoice policy |
||||
|
* Invoice all |
||||
|
* Risk sale order not include |
||||
|
* No invoice risk |
||||
|
|
||||
|
Expected result: |
||||
|
* Sale order can be confirm |
||||
|
* Invoice can be validate |
||||
|
""" |
||||
|
self.sale_order.action_button_confirm() |
||||
|
self.assertEqual( |
||||
|
self.sale_order.state, |
||||
|
"manual") |
||||
|
wizard = self.wizard.with_context({ |
||||
|
"active_ids": [self.sale_order.id]}).\ |
||||
|
create({ |
||||
|
"advance_payment_method": "all"}) |
||||
|
wizard.create_invoices() |
||||
|
self.sale_order.invoice_ids.signal_workflow("invoice_open") |
||||
|
|
||||
|
def test_sale_order_2(self): |
||||
|
""" |
||||
|
Scenario: |
||||
|
* 1 sale order @ 100 EUR |
||||
|
* Manual invoice policy |
||||
|
* Invoice all |
||||
|
* Sale Order Limit == 75 EUR |
||||
|
* Risk sale order not include |
||||
|
* No invoice risk |
||||
|
|
||||
|
Expected result: |
||||
|
* Sale order exceeds the sale order risk raised |
||||
|
""" |
||||
|
|
||||
|
self.partner.write({ |
||||
|
"risk_sale_order_limit": 75.0, |
||||
|
"credit_limit": 150.0, |
||||
|
}) |
||||
|
wiz_dic = self.sale_order.action_button_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") |
||||
|
|
||||
|
def test_sale_order_3(self): |
||||
|
""" |
||||
|
Scenario: |
||||
|
* 1 sale order @ 100 EUR |
||||
|
* Manual invoice policy |
||||
|
* Invoice all |
||||
|
* Sale Order Limit == 100 EUR |
||||
|
* Credit Limit == 75 EUR |
||||
|
* Risk sale order include |
||||
|
* No invoice risk |
||||
|
|
||||
|
Expected result: |
||||
|
* Sale order exceeds the financial risk raised |
||||
|
""" |
||||
|
|
||||
|
self.partner.write({ |
||||
|
"risk_sale_order_limit": 115.0, |
||||
|
"credit_limit": 75.0, |
||||
|
"risk_sale_order_include": True, |
||||
|
}) |
||||
|
wiz_dic = self.sale_order.action_button_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") |
||||
|
|
||||
|
def test_sale_order_4(self): |
||||
|
""" |
||||
|
Scenario: |
||||
|
* Sale Order Limit == 100 EUR |
||||
|
* Credit Limit == 75 EUR |
||||
|
* Risk sale order include |
||||
|
* Invoice draft include |
||||
|
* Sale order #1 @ 100 EUR |
||||
|
* Manual invoice policy |
||||
|
* Invoice percentace 0.75 |
||||
|
* Sale order #2 @ 100 EUR |
||||
|
* Confirm using bypass risk |
||||
|
* Sale order #3 @ 100 EUR |
||||
|
* Confirm using bypass risk |
||||
|
|
||||
|
Expected result: |
||||
|
* Financial risk exceeded raised |
||||
|
""" |
||||
|
|
||||
|
self.partner.write({ |
||||
|
"risk_sale_order_limit": 150.0, |
||||
|
"credit_limit": 100.0, |
||||
|
"risk_sale_order_include": True, |
||||
|
"risk_invoice_draft_include": True, |
||||
|
}) |
||||
|
self.sale_order.action_button_confirm() |
||||
|
self.assertEqual( |
||||
|
self.sale_order.state, |
||||
|
"manual") |
||||
|
sale_order2 = self.sale_order.copy() |
||||
|
sale_order2.order_line[0].write({'price_unit': 10.0}) |
||||
|
sale_order2.with_context(bypass_risk=True).action_button_confirm() |
||||
|
self.assertTrue( |
||||
|
self.sale_order.partner_id.risk_exception) |
||||
|
sale_order3 = self.sale_order.copy() |
||||
|
sale_order3.order_line[0].write({'price_unit': 10.0}) |
||||
|
wiz_dic = sale_order3.with_context( |
||||
|
bypass_risk=False).action_button_confirm() |
||||
|
wiz = self.env[wiz_dic['res_model']].browse(wiz_dic['res_id']) |
||||
|
self.assertEqual( |
||||
|
wiz.exception_msg, |
||||
|
"Financial risk exceeded.\n") |
@ -0,0 +1,23 @@ |
|||||
|
<?xml version="1.0" encoding="utf-8"?> |
||||
|
<!-- © 2016 Carlos Dauden <carlos.dauden@tecnativa.com> |
||||
|
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl-3). --> |
||||
|
<openerp> |
||||
|
<data> |
||||
|
<record id="res_partner_view_risk" model="ir.ui.view"> |
||||
|
<field name="name">res.partner.view.risk</field> |
||||
|
<field name="model">res.partner</field> |
||||
|
<field name="inherit_id" ref="partner_financial_risk.res_partner_view_risk"/> |
||||
|
<field name="arch" type="xml"> |
||||
|
<field name="risk_invoice_draft_include" position="before"> |
||||
|
<field name="risk_sale_order_include" |
||||
|
attrs="{'readonly': [('risk_allow_edit', '=', False)]}"/> |
||||
|
<field name="risk_sale_order" nolabel="1"/> |
||||
|
</field> |
||||
|
<field name="risk_invoice_draft_limit" position="before"> |
||||
|
<field name="risk_sale_order_limit" |
||||
|
attrs="{'readonly': [('risk_allow_edit', '=', False)]}"/> |
||||
|
</field> |
||||
|
</field> |
||||
|
</record> |
||||
|
</data> |
||||
|
</openerp> |
@ -0,0 +1,18 @@ |
|||||
|
<?xml version="1.0" encoding="utf-8"?> |
||||
|
<!-- © 2016 Carlos Dauden <carlos.dauden@tecnativa.com> |
||||
|
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl-3). --> |
||||
|
<openerp> |
||||
|
<data> |
||||
|
<record id="view_order_form_invoice_amount" model="ir.ui.view"> |
||||
|
<field name="name">sale.order.form.invoice.amount</field> |
||||
|
<field name="model">sale.order</field> |
||||
|
<field name="inherit_id" ref="sale.view_order_form"/> |
||||
|
<field name="arch" type="xml"> |
||||
|
<field name="fiscal_position" position="after"> |
||||
|
<field name="invoice_amount"/> |
||||
|
<field name="invoice_pending_amount"/> |
||||
|
</field> |
||||
|
</field> |
||||
|
</record> |
||||
|
</data> |
||||
|
</openerp> |
Write
Preview
Loading…
Cancel
Save
Reference in new issue