mirror of
https://github.com/OCA/contract.git
synced 2025-02-13 17:57:24 +02:00
[REF] - Use method _get_quantity_to_invoice.
This commit is contained in:
committed by
Víctor Martínez
parent
0caeffffa6
commit
ba3e49c6b8
@@ -13,6 +13,11 @@ class AccountAbstractAnalyticContractLine(models.AbstractModel):
|
||||
selection=[
|
||||
('fixed', 'Fixed quantity'),
|
||||
('variable', 'Variable quantity'),
|
||||
], required=True, default='fixed', string="Qty. type")
|
||||
],
|
||||
required=True,
|
||||
default='fixed',
|
||||
string="Qty. type",
|
||||
)
|
||||
qty_formula_id = fields.Many2one(
|
||||
comodel_name="contract.line.qty.formula", string="Qty. formula")
|
||||
comodel_name="contract.line.qty.formula", string="Qty. formula"
|
||||
)
|
||||
|
||||
@@ -12,18 +12,27 @@ class AccountAnalyticInvoiceLine(models.Model):
|
||||
_inherit = 'account.analytic.invoice.line'
|
||||
|
||||
@api.multi
|
||||
def _prepare_invoice_line(self, invoice_id=False):
|
||||
vals = super(AccountAnalyticInvoiceLine, self)._prepare_invoice_line(
|
||||
invoice_id=invoice_id
|
||||
def _get_quantity_to_invoice(
|
||||
self, period_first_date, period_last_date, invoice_date
|
||||
):
|
||||
quantity = super(
|
||||
AccountAnalyticInvoiceLine, self
|
||||
)._get_quantity_to_invoice(
|
||||
period_first_date, period_last_date, invoice_date
|
||||
)
|
||||
if not period_first_date or not period_last_date or not invoice_date:
|
||||
return quantity
|
||||
if self.qty_type == 'variable':
|
||||
eval_context = {
|
||||
'env': self.env,
|
||||
'context': self.env.context,
|
||||
'user': self.env.user,
|
||||
'line': self,
|
||||
'quantity': quantity,
|
||||
'period_first_date': period_first_date,
|
||||
'period_last_date': period_last_date,
|
||||
'invoice_date': invoice_date,
|
||||
'contract': self.contract_id,
|
||||
'invoice': self.env['account.invoice'].browse(invoice_id),
|
||||
}
|
||||
safe_eval(
|
||||
self.qty_formula_id.code.strip(),
|
||||
@@ -31,24 +40,23 @@ class AccountAnalyticInvoiceLine(models.Model):
|
||||
mode="exec",
|
||||
nocopy=True,
|
||||
) # nocopy for returning result
|
||||
qty = eval_context.get('result', 0)
|
||||
if self.contract_id.skip_zero_qty and float_is_zero(
|
||||
qty,
|
||||
quantity = eval_context.get('result', 0)
|
||||
return quantity
|
||||
|
||||
@api.multi
|
||||
def _prepare_invoice_line(self, invoice_id=False):
|
||||
vals = super(AccountAnalyticInvoiceLine, self)._prepare_invoice_line(
|
||||
invoice_id=invoice_id
|
||||
)
|
||||
if (
|
||||
'quantity' in vals
|
||||
and self.contract_id.skip_zero_qty
|
||||
and float_is_zero(
|
||||
vals['quantity'],
|
||||
self.env['decimal.precision'].precision_get(
|
||||
'Product Unit of Measure'
|
||||
),
|
||||
):
|
||||
# Return empty dict to skip line create
|
||||
vals = {}
|
||||
else:
|
||||
vals['quantity'] = qty
|
||||
# Re-evaluate price with this new quantity
|
||||
vals['price_unit'] = self.with_context(
|
||||
contract_line_qty=qty
|
||||
).price_unit
|
||||
else:
|
||||
if 'quantity' in vals and vals['quantity'] == 0:
|
||||
# Skip zero should ignore lines with qty zero even for fixed
|
||||
# qty
|
||||
vals = {}
|
||||
)
|
||||
):
|
||||
vals = {}
|
||||
return vals
|
||||
|
||||
@@ -23,12 +23,18 @@ class ContractLineFormula(models.Model):
|
||||
'line': self.env['account.analytic.invoice.line'],
|
||||
'contract': self.env['account.analytic.account'],
|
||||
'invoice': self.env['account.invoice'],
|
||||
'quantity': 0,
|
||||
'period_first_date': False,
|
||||
'period_last_date': False,
|
||||
'invoice_date': False,
|
||||
}
|
||||
try:
|
||||
safe_eval(
|
||||
self.code.strip(), eval_context, mode="exec", nocopy=True)
|
||||
self.code.strip(), eval_context, mode="exec", nocopy=True
|
||||
)
|
||||
except Exception as e:
|
||||
raise exceptions.ValidationError(
|
||||
_('Error evaluating code.\nDetails: %s') % e)
|
||||
_('Error evaluating code.\nDetails: %s') % e
|
||||
)
|
||||
if 'result' not in eval_context:
|
||||
raise exceptions.ValidationError(_('No valid result returned.'))
|
||||
|
||||
Reference in New Issue
Block a user