[IMP] contract_variable_quantity: Add option to don't create zero qty invoice lines

This commit is contained in:
Carlos Dauden
2018-03-27 13:55:05 +02:00
committed by Carlos Roca
parent 7b0d4ee154
commit 295c7c1ee7
5 changed files with 56 additions and 22 deletions

View File

@@ -1,13 +1,20 @@
# © 2016 Pedro M. Baeza <pedro.baeza@tecnativa.com>
# Copyright 2016 Tecnativa - Pedro M. Baeza
# Copyright 2018 Tecnativa - Carlos Dauden
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo import _, api, fields, models, exceptions
from odoo.tools import float_is_zero
from odoo.tools.safe_eval import safe_eval
class AccountAnalyticAccount(models.Model):
_inherit = "account.analytic.account"
skip_zero_qty = fields.Boolean(
string='Skip Zero Qty Lines',
help="If checked, contract lines with 0 qty don't create invoice line",
)
@api.model
def _prepare_invoice_line(self, line, invoice_id):
vals = super(AccountAnalyticAccount, self)._prepare_invoice_line(
@@ -23,7 +30,14 @@ class AccountAnalyticAccount(models.Model):
}
safe_eval(line.qty_formula_id.code.strip(), eval_context,
mode="exec", nocopy=True) # nocopy for returning result
vals['quantity'] = eval_context.get('result', 0)
qty = eval_context.get('result', 0)
if self.skip_zero_qty and float_is_zero(
qty, self.env['decimal.precision'].precision_get(
'Product Unit of Measure')):
# Return empty dict to skip line create
vals = {}
else:
vals['quantity'] = qty
return vals