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.

41 lines
1.6 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):
  12. vals = super(AccountAnalyticInvoiceLine, self)._prepare_invoice_line(
  13. 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. 'invoice': self.env['account.invoice'].browse(invoice_id),
  22. }
  23. safe_eval(self.qty_formula_id.code.strip(), eval_context,
  24. mode="exec", nocopy=True) # nocopy for returning result
  25. qty = eval_context.get('result', 0)
  26. if self.contract_id.skip_zero_qty and float_is_zero(
  27. qty, self.env['decimal.precision'].precision_get(
  28. 'Product Unit of Measure')):
  29. # Return empty dict to skip line create
  30. vals = {}
  31. else:
  32. vals['quantity'] = qty
  33. # Re-evaluate price with this new quantity
  34. vals['price_unit'] = self.with_context(
  35. contract_line_qty=qty,
  36. ).price_unit
  37. return vals