You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

150 lines
6.5 KiB

  1. # -*- coding: utf-8 -*-
  2. # © 2016 Carlos Dauden <carlos.dauden@tecnativa.com>
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  4. from datetime import datetime
  5. from dateutil.relativedelta import relativedelta
  6. from openerp import api, fields, models
  7. class ResPartner(models.Model):
  8. _inherit = 'res.partner'
  9. risk_invoice_draft_include = fields.Boolean(
  10. string='Include Draft Invoices', help='Full risk computation')
  11. risk_invoice_draft_limit = fields.Monetary(
  12. string='Limit In Draft Invoices', help='Set 0 if it is not locked')
  13. risk_invoice_draft = fields.Monetary(
  14. compute='_compute_risk_invoice', store=True,
  15. string='Total Draft Invoices',
  16. help='Total amount of invoices in Draft or Pro-forma state')
  17. risk_invoice_open_include = fields.Boolean(
  18. string='Include Open Invoices', help='Full risk computation')
  19. risk_invoice_open_limit = fields.Monetary(
  20. string='Limit In Open Invoices', help='Set 0 if it is not locked')
  21. risk_invoice_open = fields.Monetary(
  22. compute='_compute_risk_invoice', store=True,
  23. string='Total Open Invoices',
  24. help='Residual amount of invoices in Open state and the date due is '
  25. 'not exceeded, considering Due Margin set in account '
  26. 'settings')
  27. risk_invoice_unpaid_include = fields.Boolean(
  28. string='Include Unpaid Invoices', help='Full risk computation')
  29. risk_invoice_unpaid_limit = fields.Monetary(
  30. string='Limit In Unpaid Invoices', help='Set 0 if it is not locked')
  31. risk_invoice_unpaid = fields.Monetary(
  32. compute='_compute_risk_invoice', store=True,
  33. string='Total Unpaid Invoices',
  34. help='Residual amount of invoices in Open state and the date due is '
  35. 'exceeded, considering Unpaid Margin set in account settings')
  36. risk_account_amount_include = fields.Boolean(
  37. string='Include Other Account Amount', help='Full risk computation')
  38. risk_account_amount_limit = fields.Monetary(
  39. string='Limit Other Account Amount', help='Set 0 if it is not locked')
  40. risk_account_amount = fields.Monetary(
  41. compute='_compute_risk_account_amount',
  42. string='Other Account Amount',
  43. help='Difference between accounting credit and rest of totals')
  44. risk_total = fields.Monetary(
  45. compute='_compute_risk_exception',
  46. string='Total Risk', help='Sum of total risk included')
  47. risk_exception = fields.Boolean(
  48. compute='_compute_risk_exception',
  49. string='Risk Exception',
  50. help='It Indicate if partner risk exceeded')
  51. credit_policy = fields.Char()
  52. risk_allow_edit = fields.Boolean(compute='_compute_risk_allow_edit')
  53. @api.multi
  54. def _compute_risk_allow_edit(self):
  55. is_editable = self.env.user.has_group(
  56. 'base.group_sale_manager') or self.env.user.has_group(
  57. 'account.group_account_manager')
  58. for partner in self:
  59. partner.risk_allow_edit = is_editable
  60. @api.multi
  61. @api.depends('invoice_ids', 'invoice_ids.state',
  62. 'invoice_ids.amount_total', 'invoice_ids.residual',
  63. 'invoice_ids.company_id.invoice_unpaid_margin')
  64. def _compute_risk_invoice(self):
  65. max_date = self._max_risk_date_due()
  66. for partner in self:
  67. invoices_out = partner.invoice_ids.filtered(
  68. lambda x: x.type in ['out_invoice', 'out_refund'])
  69. invoices = invoices_out.filtered(
  70. lambda x: x.state in ['draft', 'proforma', 'proforma2'])
  71. partner.risk_invoice_draft = sum(invoices.mapped('amount_total'))
  72. invoices = invoices_out.filtered(
  73. lambda x: x.state == 'open' and x.date_due >= max_date)
  74. partner.risk_invoice_open = sum(invoices.mapped('residual'))
  75. invoices = invoices_out.filtered(
  76. lambda x: x.state == 'open' and x.date_due < max_date)
  77. partner.risk_invoice_unpaid = sum(invoices.mapped('residual'))
  78. @api.multi
  79. @api.depends('credit', 'risk_invoice_open', 'risk_invoice_unpaid')
  80. def _compute_risk_account_amount(self):
  81. for partner in self:
  82. partner.risk_account_amount = (
  83. partner.credit - partner.risk_invoice_open -
  84. partner.risk_invoice_unpaid)
  85. @api.multi
  86. @api.depends(lambda x: x._get_depends_compute_risk_exception())
  87. def _compute_risk_exception(self):
  88. risk_field_list = self._risk_field_list()
  89. for partner in self:
  90. amount = 0.0
  91. for risk_field in risk_field_list:
  92. field_value = getattr(partner, risk_field[0], 0.0)
  93. max_value = getattr(partner, risk_field[1], 0.0)
  94. if max_value and field_value > max_value:
  95. partner.risk_exception = True
  96. if getattr(partner, risk_field[2], False):
  97. amount += field_value
  98. partner.risk_total = amount
  99. if amount > partner.credit_limit:
  100. partner.risk_exception = True
  101. @api.model
  102. def _max_risk_date_due(self):
  103. return fields.Date.to_string(datetime.today().date() - relativedelta(
  104. days=self.env.user.company_id.invoice_unpaid_margin))
  105. @api.model
  106. def _risk_field_list(self):
  107. return [
  108. ('risk_invoice_draft', 'risk_invoice_draft_limit',
  109. 'risk_invoice_draft_include'),
  110. ('risk_invoice_open', 'risk_invoice_open_limit',
  111. 'risk_invoice_open_include'),
  112. ('risk_invoice_unpaid', 'risk_invoice_unpaid_limit',
  113. 'risk_invoice_unpaid_include'),
  114. ('risk_account_amount', 'risk_account_amount_limit',
  115. 'risk_account_amount_include'),
  116. ]
  117. @api.model
  118. def _get_depends_compute_risk_exception(self):
  119. # TODO: Improve code without performance loss
  120. tuple_list = self._risk_field_list()
  121. res = [x[0] for x in tuple_list]
  122. res.extend([x[1] for x in tuple_list])
  123. res.extend([x[2] for x in tuple_list])
  124. res.append('credit_limit')
  125. return res
  126. @api.model
  127. def process_unpaid_invoices(self):
  128. today = fields.Date.today()
  129. ConfigParameter = self.env['ir.config_parameter']
  130. last_check = ConfigParameter.get_param(
  131. 'partner_financial_risk.last_check', default='2016-01-01')
  132. invoices = self.env['account.invoice'].search([
  133. ('date_due', '>=', last_check), ('date_due', '<', today)])
  134. invoices.mapped('partner_id')._compute_risk_invoice()
  135. ConfigParameter.set_param('partner_financial_risk.last_check', today)
  136. return True