Merge branch 'mig/15.0/sale_credit_limit' into '15.0'

mig/15.0/sale_credit_limit into 15.0

See merge request hibou-io/hibou-odoo/suite!1212
This commit is contained in:
Jared Kipe
2021-12-06 17:51:49 +00:00
9 changed files with 181 additions and 0 deletions

View File

@@ -0,0 +1 @@
from . import models

View File

@@ -0,0 +1,29 @@
{
'name': 'Sale Credit Limit',
'summary': 'Uses credit limit on Partners to warn salespeople if they are over their limit.',
'version': '15.0.1.0.0',
'author': "Hibou Corp.",
'category': 'Sale',
'license': 'OPL-1',
'complexity': 'expert',
'images': [],
'website': "https://hibou.io",
'description': """
Uses credit limit on Partners to warn salespeople if they are over their limit.
When confirming a sale order, the current sale order total will be considered and a Sale Order Exception
will be created if the total would put them over their credit limit.
""",
'depends': [
'sale',
'account',
'sale_exception',
],
'demo': [],
'data': [
'data/sale_exceptions.xml',
'views/partner_views.xml',
],
'auto_install': False,
'installable': True,
}

View File

@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8" ?>
<odoo>
<record id="excep_sale_credit_limit" model="exception.rule">
<field name="name">Invoice Partner credit limit exceeded.</field>
<field name="description">The Customer or Invoice Address has a credit limit.
This sale order, or the customer has an outstanding balance that, exceeds their credit limit.</field>
<field name="sequence">50</field>
<field name="model">sale.order</field>
<field name="code">
partner = object.partner_invoice_id.commercial_partner_id
partner_balance = partner.credit + object.amount_total
if partner.credit_limit and partner.credit_limit &lt;= partner_balance:
failed = True
</field>
<field name="active" eval="True" />
</record>
<record id="excep_sale_credit_hold" model="exception.rule">
<field name="name">Customer On Credit Hold.</field>
<field name="description">The Customer is on Credit Hold.
Please have the customer contact accounting.</field>
<field name="sequence">50</field>
<field name="model">sale.order</field>
<field name="code">
partner = object.partner_invoice_id.commercial_partner_id
if partner.credit_hold:
failed = True
</field>
<field name="active" eval="True" />
</record>
<record id="excep_invoice_overdue" model="exception.rule">
<field name="name">Customer has Overdue Invoices.</field>
<field name="description">The Customer has unpaid overdue invoices.
Please have the customer contact accounting.</field>
<field name="sequence">55</field>
<field name="model">sale.order</field>
<field name="code">
partner = object.partner_invoice_id.commercial_partner_id
if partner.invoice_ids.filtered(lambda i: i.state == 'posted' and i.payment_state != 'paid' and (i.invoice_date_due and str(i.invoice_date_due) &lt; str(datetime.date.today())) and i.move_type == 'out_invoice'):
failed = True
</field>
<field name="active" eval="True" />
</record>
</odoo>

View File

@@ -0,0 +1,4 @@
# Part of Hibou Suite Professional. See LICENSE_PROFESSIONAL file for full copyright and licensing details.
from . import partner
from . import sale

View File

@@ -0,0 +1,18 @@
# Part of Hibou Suite Professional. See LICENSE_PROFESSIONAL file for full copyright and licensing details.
from odoo import api, fields, models
class ResPartner(models.Model):
_inherit = 'res.partner'
credit_remaining = fields.Float('Credit Remaining', compute='_compute_credit_remaining')
credit_hold = fields.Boolean('Credit Hold')
@api.depends('credit_limit', 'credit')
def _compute_credit_remaining(self):
for partner in self:
if partner.credit_limit:
partner.credit_remaining = partner.credit_limit - partner.credit
else:
partner.credit_remaining = 0.0

View File

@@ -0,0 +1,28 @@
# Part of Hibou Suite Professional. See LICENSE_PROFESSIONAL file for full copyright and licensing details.
from odoo import api, models, tools
from odoo.tools.safe_eval import datetime as wrapped_datetime
class SaleOrder(models.Model):
_inherit = 'sale.order'
# We need a way to be able to create a 'today' date to compare
@api.model
def _exception_rule_eval_context(self, rec):
res = super(SaleOrder, self)._exception_rule_eval_context(rec)
res["datetime"] = wrapped_datetime
return res
@api.onchange('partner_invoice_id')
def _onchange_partner_invoice_id(self):
for so in self:
partner = so.partner_invoice_id.commercial_partner_id
if partner.credit_limit and partner.credit_limit <= partner.credit:
m = 'Partner outstanding receivables %s is above their credit limit of %s' \
% (tools.format_amount(self.env, partner.credit, so.currency_id),
tools.format_amount(self.env, partner.credit_limit, so.currency_id))
return {
'warning': {'title': 'Sale Credit Limit',
'message': m}
}

View File

@@ -0,0 +1,3 @@
# Part of Hibou Suite Professional. See LICENSE_PROFESSIONAL file for full copyright and licensing details.
from . import test_sale_credit_exception

View File

@@ -0,0 +1,31 @@
# Part of Hibou Suite Professional. See LICENSE_PROFESSIONAL file for full copyright and licensing details.
from odoo.addons.sale_exception.tests.test_sale_exception import TestSaleException
class TestSaleCreditException(TestSaleException):
def setUp(self):
super(TestSaleCreditException, self).setUp()
def test_sale_order_credit_limit_exception(self):
self.sale_exception_confirm = self.env['sale.exception.confirm']
partner = self.env.ref('base.res_partner_12')
partner.credit_limit = 100.00
p = self.env.ref('product.product_product_25_product_template')
so1 = self.env['sale.order'].create({
'partner_id': partner.id,
'partner_invoice_id': partner.id,
'partner_shipping_id': partner.id,
'order_line': [(0, 0, {'name': p.name,
'product_id': p.id,
'product_uom_qty': 2,
'product_uom': p.uom_id.id,
'price_unit': p.list_price})],
'pricelist_id': self.env.ref('product.list0').id,
})
# confirm quotation
so1.action_confirm()
self.assertTrue(so1.state == 'draft')
self.assertFalse(so1.ignore_exception)

View File

@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8" ?>
<odoo>
<record id="view_partner_form_inherit" model="ir.ui.view">
<field name="name">res.partner.form.inherit</field>
<field name="model">res.partner</field>
<field name="inherit_id" ref="base.view_partner_form"/>
<field name="arch" type="xml">
<xpath expr="//group[@name='accounting_entries']" position="after">
<group string="Credit Limit" name="credit_limit" groups="account.group_account_invoice">
<field name="credit" widget="monetary" attrs="{'invisible': [('parent_id', '!=', False)]}" />
<field name="credit_limit" attrs="{'invisible': [('parent_id', '!=', False)]}" />
<field name="credit_remaining" widget="monetary" attrs="{'invisible': [('parent_id', '!=', False)]}" />
<field name="credit_hold" attrs="{'invisible': [('parent_id', '!=', False)]}" />
</group>
</xpath>
</field>
</record>
</odoo>