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.

79 lines
2.9 KiB

  1. # Copyright 2016 Tecnativa - Pedro M. Baeza
  2. # Copyright 2018 Tecnativa - Carlos Dauden
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  4. from odoo import _, api, fields, models, exceptions
  5. from odoo.tools import float_is_zero
  6. from odoo.tools.safe_eval import safe_eval
  7. class AccountAnalyticAccount(models.Model):
  8. _inherit = "account.analytic.account"
  9. skip_zero_qty = fields.Boolean(
  10. string='Skip Zero Qty Lines',
  11. help="If checked, contract lines with 0 qty don't create invoice line",
  12. )
  13. @api.model
  14. def _prepare_invoice_line(self, line, invoice_id):
  15. vals = super(AccountAnalyticAccount, self)._prepare_invoice_line(
  16. line, invoice_id)
  17. if line.qty_type == 'variable':
  18. eval_context = {
  19. 'env': self.env,
  20. 'context': self.env.context,
  21. 'user': self.env.user,
  22. 'line': line,
  23. 'contract': line.analytic_account_id,
  24. 'invoice': self.env['account.invoice'].browse(invoice_id),
  25. }
  26. safe_eval(line.qty_formula_id.code.strip(), eval_context,
  27. mode="exec", nocopy=True) # nocopy for returning result
  28. qty = eval_context.get('result', 0)
  29. if self.skip_zero_qty and float_is_zero(
  30. qty, self.env['decimal.precision'].precision_get(
  31. 'Product Unit of Measure')):
  32. # Return empty dict to skip line create
  33. vals = {}
  34. else:
  35. vals['quantity'] = qty
  36. return vals
  37. class AccountAnalyticInvoiceLine(models.Model):
  38. _inherit = 'account.analytic.invoice.line'
  39. qty_type = fields.Selection(
  40. selection=[
  41. ('fixed', 'Fixed quantity'),
  42. ('variable', 'Variable quantity'),
  43. ], required=True, default='fixed', string="Qty. type")
  44. qty_formula_id = fields.Many2one(
  45. comodel_name="contract.line.qty.formula", string="Qty. formula")
  46. class ContractLineFormula(models.Model):
  47. _name = 'contract.line.qty.formula'
  48. name = fields.Char(required=True, translate=True)
  49. code = fields.Text(required=True, default="result = 0")
  50. @api.constrains('code')
  51. def _check_code(self):
  52. eval_context = {
  53. 'env': self.env,
  54. 'context': self.env.context,
  55. 'user': self.env.user,
  56. 'line': self.env['account.analytic.invoice.line'],
  57. 'contract': self.env['account.analytic.account'],
  58. 'invoice': self.env['account.invoice'],
  59. }
  60. try:
  61. safe_eval(
  62. self.code.strip(), eval_context, mode="exec", nocopy=True)
  63. except Exception as e:
  64. raise exceptions.ValidationError(
  65. _('Error evaluating code.\nDetails: %s') % e)
  66. if 'result' not in eval_context:
  67. raise exceptions.ValidationError(_('No valid result returned.'))