Browse Source

[9.0][ADD] partner_sale_risk: New module

pull/391/head
Carlos Dauden 8 years ago
committed by Andhitia Rama
parent
commit
6e0ea65680
  1. 60
      partner_sale_risk/README.rst
  2. 3
      partner_sale_risk/__init__.py
  3. 19
      partner_sale_risk/__openerp__.py
  4. 4
      partner_sale_risk/models/__init__.py
  5. 32
      partner_sale_risk/models/res_partner.py
  6. 50
      partner_sale_risk/models/sale.py
  7. 53
      partner_sale_risk/tests/test_partner_sale_risk.py
  8. 21
      partner_sale_risk/views/res_partner_view.xml
  9. 16
      partner_sale_risk/views/sale_view.xml

60
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
<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>
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.

3
partner_sale_risk/__init__.py

@ -0,0 +1,3 @@
# -*- coding: utf-8 -*-
from . import models

19
partner_sale_risk/__openerp__.py

@ -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': '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,
}

4
partner_sale_risk/models/__init__.py

@ -0,0 +1,4 @@
# -*- coding: utf-8 -*-
from . import res_partner
from . import sale

32
partner_sale_risk/models/res_partner.py

@ -0,0 +1,32 @@
# -*- 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.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

50
partner_sale_risk/models/sale.py

@ -0,0 +1,50 @@
# -*- 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.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()

53
partner_sale_risk/tests/test_partner_sale_risk.py

@ -0,0 +1,53 @@
# -*- 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})],
})
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)

21
partner_sale_risk/views/res_partner_view.xml

@ -0,0 +1,21 @@
<?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). -->
<odoo>
<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>
</odoo>

16
partner_sale_risk/views/sale_view.xml

@ -0,0 +1,16 @@
<?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). -->
<odoo>
<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_id" position="after">
<field name="invoice_amount"/>
<field name="invoice_pending_amount"/>
</field>
</field>
</record>
</odoo>
Loading…
Cancel
Save