Browse Source

[IMP] partner_financial_risk: New fields in view. Improve code.

pull/470/head
Carlos Dauden 7 years ago
committed by David
parent
commit
c3e4acd957
  1. 52
      partner_financial_risk/models/account_invoice.py
  2. 6
      partner_financial_risk/models/res_partner.py
  3. 6
      partner_financial_risk/tests/test_partner_financial_risk.py
  4. 7
      partner_financial_risk/views/res_partner_view.xml

52
partner_financial_risk/models/account_invoice.py

@ -10,29 +10,31 @@ class AccountInvoice(models.Model):
@api.multi
def action_invoice_open(self):
if not self.env.context.get('bypass_risk', False):
for invoice in self:
partner = invoice.partner_id.commercial_partner_id
exception_msg = ""
if partner.risk_exception:
exception_msg = _("Financial risk exceeded.\n")
elif partner.risk_invoice_open_limit and (
(partner.risk_invoice_open + invoice.amount_total) >
partner.risk_invoice_open_limit):
exception_msg = _(
"This invoice exceeds the open invoices risk.\n")
elif not partner.risk_invoice_draft_include and (
partner.risk_invoice_open_include and
(partner.risk_total + invoice.amount_total) >
partner.credit_limit):
exception_msg = _(
"This invoice 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' % ('account.invoice', invoice.id),
'continue_method': 'action_invoice_open',
}).action_show()
if self.env.context.get('bypass_risk', False):
return super(AccountInvoice, self).action_invoice_open()
for invoice in self:
partner = invoice.partner_id.commercial_partner_id
exception_msg = ""
if partner.risk_exception:
exception_msg = _("Financial risk exceeded.\n")
elif partner.risk_invoice_open_limit and (
(partner.risk_invoice_open + invoice.amount_total) >
partner.risk_invoice_open_limit):
exception_msg = _(
"This invoice exceeds the open invoices risk.\n")
# If risk_invoice_draft_include this invoice included in risk_total
elif not partner.risk_invoice_draft_include and (
partner.risk_invoice_open_include and
(partner.risk_total + invoice.amount_total) >
partner.credit_limit):
exception_msg = _(
"This invoice 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' % ('account.invoice', invoice.id),
'continue_method': 'action_invoice_open',
}).action_show()
return super(AccountInvoice, self).action_invoice_open()

6
partner_financial_risk/models/res_partner.py

@ -114,7 +114,7 @@ class ResPartner(models.Model):
@api.multi
@api.depends(
'invoice_ids', 'invoice_ids.state',
'customer', 'invoice_ids', 'invoice_ids.state',
'invoice_ids.amount_total',
'child_ids.invoice_ids', 'child_ids.invoice_ids.state',
'child_ids.invoice_ids.amount_total')
@ -122,11 +122,13 @@ class ResPartner(models.Model):
all_partners_and_children = {}
all_partner_ids = []
for partner in self.filtered('customer'):
if not partner.id:
continue
all_partners_and_children[partner] = self.with_context(
active_test=False).search([('id', 'child_of', partner.id)]).ids
all_partner_ids += all_partners_and_children[partner]
if not all_partner_ids:
return # pragma: no cover
return
total_group = self.env['account.invoice'].sudo().read_group(
[('type', 'in', ['out_invoice', 'out_refund']),
('state', 'in', ['draft', 'proforma', 'proforma2']),

6
partner_financial_risk/tests/test_partner_financial_risk.py

@ -70,6 +70,7 @@ class TestPartnerFinancialRisk(SavepointCase):
'invoice_line_tax_ids': [(6, 0, [cls.tax.id])],
})],
})
cls.env.user.lang = False
def test_invoices(self):
self.partner.risk_invoice_draft_include = True
@ -142,3 +143,8 @@ class TestPartnerFinancialRisk(SavepointCase):
line.date_maturity = '2017-01-01'
self.assertAlmostEqual(self.partner.risk_account_amount, 0.0)
self.assertAlmostEqual(self.partner.risk_account_amount_unpaid, 100.0)
def test_recompute_newid(self):
"""Computing risk shouldn't fail if record is a NewId."""
new = self.env['res.partner'].new({'customer': True})
new._compute_risk_invoice()

7
partner_financial_risk/views/res_partner_view.xml

@ -28,10 +28,13 @@
<field name="risk_account_amount_include"
attrs="{'readonly': [('risk_allow_edit', '=', False)]}"/>
<field name="risk_account_amount" nolabel="1"/>
<field name="risk_account_amount_unpaid_include"
attrs="{'readonly': [('risk_allow_edit', '=', False)]}"/>
<field name="risk_account_amount_unpaid" nolabel="1"/>
<field name="risk_total" colspan="3" class="oe_subtotal_footer_separator"/>
</group>
</group>
<group string="Specific Limits" name="risk_limits" col="1">
<group string="Specific Limits" name="risk_limits" col="2">
<group class="oe_subtotal_footer">
<field name="risk_invoice_draft_limit"
attrs="{'readonly': [('risk_allow_edit', '=', False)]}"/>
@ -41,6 +44,8 @@
attrs="{'readonly': [('risk_allow_edit', '=', False)]}"/>
<field name="risk_account_amount_limit"
attrs="{'readonly': [('risk_allow_edit', '=', False)]}"/>
<field name="risk_account_amount_unpaid_limit"
attrs="{'readonly': [('risk_allow_edit', '=', False)]}"/>
<field name="risk_allow_edit" invisible="1"/>
</group>
</group>

Loading…
Cancel
Save