Skip to content

Commit 6a0d782

Browse files
carlosdaudenrjaraspearhead
authored andcommitted
[IMP] partner_financial_risk: New fields in view. Improve code.
1 parent e6895ec commit 6a0d782

File tree

4 files changed

+43
-28
lines changed

4 files changed

+43
-28
lines changed

account_financial_risk/models/account_invoice.py

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,31 @@ class AccountInvoice(models.Model):
1010

1111
@api.multi
1212
def action_invoice_open(self):
13-
if not self.env.context.get('bypass_risk', False):
14-
for invoice in self:
15-
partner = invoice.partner_id.commercial_partner_id
16-
exception_msg = ""
17-
if partner.risk_exception:
18-
exception_msg = _("Financial risk exceeded.\n")
19-
elif partner.risk_invoice_open_limit and (
20-
(partner.risk_invoice_open + invoice.amount_total) >
21-
partner.risk_invoice_open_limit):
22-
exception_msg = _(
23-
"This invoice exceeds the open invoices risk.\n")
24-
elif not partner.risk_invoice_draft_include and (
25-
partner.risk_invoice_open_include and
26-
(partner.risk_total + invoice.amount_total) >
27-
partner.credit_limit):
28-
exception_msg = _(
29-
"This invoice exceeds the financial risk.\n")
30-
if exception_msg:
31-
return self.env['partner.risk.exceeded.wiz'].create({
32-
'exception_msg': exception_msg,
33-
'partner_id': partner.id,
34-
'origin_reference':
35-
'%s,%s' % ('account.invoice', invoice.id),
36-
'continue_method': 'action_invoice_open',
37-
}).action_show()
13+
if self.env.context.get('bypass_risk', False):
14+
return super(AccountInvoice, self).action_invoice_open()
15+
for invoice in self:
16+
partner = invoice.partner_id.commercial_partner_id
17+
exception_msg = ""
18+
if partner.risk_exception:
19+
exception_msg = _("Financial risk exceeded.\n")
20+
elif partner.risk_invoice_open_limit and (
21+
(partner.risk_invoice_open + invoice.amount_total) >
22+
partner.risk_invoice_open_limit):
23+
exception_msg = _(
24+
"This invoice exceeds the open invoices risk.\n")
25+
# If risk_invoice_draft_include this invoice included in risk_total
26+
elif not partner.risk_invoice_draft_include and (
27+
partner.risk_invoice_open_include and
28+
(partner.risk_total + invoice.amount_total) >
29+
partner.credit_limit):
30+
exception_msg = _(
31+
"This invoice exceeds the financial risk.\n")
32+
if exception_msg:
33+
return self.env['partner.risk.exceeded.wiz'].create({
34+
'exception_msg': exception_msg,
35+
'partner_id': partner.id,
36+
'origin_reference':
37+
'%s,%s' % ('account.invoice', invoice.id),
38+
'continue_method': 'action_invoice_open',
39+
}).action_show()
3840
return super(AccountInvoice, self).action_invoice_open()

account_financial_risk/models/res_partner.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,19 +114,21 @@ def _compute_risk_allow_edit(self):
114114

115115
@api.multi
116116
@api.depends(
117-
'invoice_ids', 'invoice_ids.state',
117+
'customer', 'invoice_ids', 'invoice_ids.state',
118118
'invoice_ids.amount_total',
119119
'child_ids.invoice_ids', 'child_ids.invoice_ids.state',
120120
'child_ids.invoice_ids.amount_total')
121121
def _compute_risk_invoice(self):
122122
all_partners_and_children = {}
123123
all_partner_ids = []
124124
for partner in self.filtered('customer'):
125+
if not partner.id:
126+
continue
125127
all_partners_and_children[partner] = self.with_context(
126128
active_test=False).search([('id', 'child_of', partner.id)]).ids
127129
all_partner_ids += all_partners_and_children[partner]
128130
if not all_partner_ids:
129-
return # pragma: no cover
131+
return
130132
total_group = self.env['account.invoice'].sudo().read_group(
131133
[('type', 'in', ['out_invoice', 'out_refund']),
132134
('state', 'in', ['draft', 'proforma', 'proforma2']),

account_financial_risk/tests/test_partner_financial_risk.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ def setUpClass(cls):
7070
'invoice_line_tax_ids': [(6, 0, [cls.tax.id])],
7171
})],
7272
})
73+
cls.env.user.lang = False
7374

7475
def test_invoices(self):
7576
self.partner.risk_invoice_draft_include = True
@@ -142,3 +143,8 @@ def test_other_account_amount(self):
142143
line.date_maturity = '2017-01-01'
143144
self.assertAlmostEqual(self.partner.risk_account_amount, 0.0)
144145
self.assertAlmostEqual(self.partner.risk_account_amount_unpaid, 100.0)
146+
147+
def test_recompute_newid(self):
148+
"""Computing risk shouldn't fail if record is a NewId."""
149+
new = self.env['res.partner'].new({'customer': True})
150+
new._compute_risk_invoice()

account_financial_risk/views/res_partner_view.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,13 @@
2828
<field name="risk_account_amount_include"
2929
attrs="{'readonly': [('risk_allow_edit', '=', False)]}"/>
3030
<field name="risk_account_amount" nolabel="1"/>
31+
<field name="risk_account_amount_unpaid_include"
32+
attrs="{'readonly': [('risk_allow_edit', '=', False)]}"/>
33+
<field name="risk_account_amount_unpaid" nolabel="1"/>
3134
<field name="risk_total" colspan="3" class="oe_subtotal_footer_separator"/>
3235
</group>
3336
</group>
34-
<group string="Specific Limits" name="risk_limits" col="1">
37+
<group string="Specific Limits" name="risk_limits" col="2">
3538
<group class="oe_subtotal_footer">
3639
<field name="risk_invoice_draft_limit"
3740
attrs="{'readonly': [('risk_allow_edit', '=', False)]}"/>
@@ -41,6 +44,8 @@
4144
attrs="{'readonly': [('risk_allow_edit', '=', False)]}"/>
4245
<field name="risk_account_amount_limit"
4346
attrs="{'readonly': [('risk_allow_edit', '=', False)]}"/>
47+
<field name="risk_account_amount_unpaid_limit"
48+
attrs="{'readonly': [('risk_allow_edit', '=', False)]}"/>
4449
<field name="risk_allow_edit" invisible="1"/>
4550
</group>
4651
</group>

0 commit comments

Comments
 (0)