Browse Source

Merge pull request #295 from Tecnativa/9.0-partner_sale_risk

[9.0] [ADD] partner_sale_risk
pull/433/head
Pedro M. Baeza 8 years ago
committed by GitHub
parent
commit
6ffa883ce1
  1. 60
      partner_sale_risk/README.rst
  2. 3
      partner_sale_risk/__init__.py
  3. 19
      partner_sale_risk/__openerp__.py
  4. 89
      partner_sale_risk/i18n/es.po
  5. 4
      partner_sale_risk/models/__init__.py
  6. 42
      partner_sale_risk/models/res_partner.py
  7. 60
      partner_sale_risk/models/sale.py
  8. 3
      partner_sale_risk/tests/__init__.py
  9. 63
      partner_sale_risk/tests/test_partner_sale_risk.py
  10. 21
      partner_sale_risk/views/res_partner_view.xml
  11. 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,
}

89
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 <carlos.dauden@tecnativa.com>, 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 <carlos.dauden@tecnativa.com>\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"

4
partner_sale_risk/models/__init__.py

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

42
partner_sale_risk/models/res_partner.py

@ -0,0 +1,42 @@
# -*- 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',
'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

60
partner_sale_risk/models/sale.py

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

3
partner_sale_risk/tests/__init__.py

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

63
partner_sale_risk/tests/test_partner_sale_risk.py

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

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