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.

54 lines
1.9 KiB

  1. # Copyright 2016 Tecnativa - Pedro M. Baeza
  2. # Copyright 2018 Tecnativa - Carlos Dauden
  3. # Copyright 2018 ACSONE SA/NV
  4. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  5. from odoo import api, models
  6. from odoo.tools import float_is_zero
  7. from odoo.tools.safe_eval import safe_eval
  8. class AccountAnalyticInvoiceLine(models.Model):
  9. _inherit = 'account.analytic.invoice.line'
  10. @api.multi
  11. def _prepare_invoice_line(self, invoice_id=False):
  12. vals = super(AccountAnalyticInvoiceLine, self)._prepare_invoice_line(
  13. invoice_id=invoice_id
  14. )
  15. if self.qty_type == 'variable':
  16. eval_context = {
  17. 'env': self.env,
  18. 'context': self.env.context,
  19. 'user': self.env.user,
  20. 'line': self,
  21. 'contract': self.contract_id,
  22. 'invoice': self.env['account.invoice'].browse(invoice_id),
  23. }
  24. safe_eval(
  25. self.qty_formula_id.code.strip(),
  26. eval_context,
  27. mode="exec",
  28. nocopy=True,
  29. ) # nocopy for returning result
  30. qty = eval_context.get('result', 0)
  31. if self.contract_id.skip_zero_qty and float_is_zero(
  32. qty,
  33. self.env['decimal.precision'].precision_get(
  34. 'Product Unit of Measure'
  35. ),
  36. ):
  37. # Return empty dict to skip line create
  38. vals = {}
  39. else:
  40. vals['quantity'] = qty
  41. # Re-evaluate price with this new quantity
  42. vals['price_unit'] = self.with_context(
  43. contract_line_qty=qty
  44. ).price_unit
  45. else:
  46. if vals.get('quantity') and vals['quantity'] == 0:
  47. # Skip zero should ignore lines with qty zero even for fixed
  48. # qty
  49. vals = {}
  50. return vals