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.

48 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. if self.qty_type == 'variable':
  15. eval_context = {
  16. 'env': self.env,
  17. 'context': self.env.context,
  18. 'user': self.env.user,
  19. 'line': self,
  20. 'contract': self.contract_id,
  21. }
  22. if invoice_id:
  23. eval_context['invoice'] = self.env['account.invoice'].browse(
  24. invoice_id),
  25. safe_eval(self.qty_formula_id.code.strip(), eval_context,
  26. mode="exec", nocopy=True) # nocopy for returning result
  27. qty = eval_context.get('result', 0)
  28. if self.contract_id.skip_zero_qty and float_is_zero(
  29. qty, self.env['decimal.precision'].precision_get(
  30. 'Product Unit of Measure')):
  31. # Return empty dict to skip line create
  32. vals = {}
  33. else:
  34. vals['quantity'] = qty
  35. # Re-evaluate price with this new quantity
  36. vals['price_unit'] = self.with_context(
  37. contract_line_qty=qty,
  38. ).price_unit
  39. else:
  40. if vals.get('quantity') and vals['quantity'] == 0:
  41. # Skip zero should ignore lines with qty zero even for fixed
  42. # qty
  43. vals = {}
  44. return vals