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:
@@ -10,9 +10,7 @@
|
||||
'author': "Tecnativa,"
|
||||
"Odoo Community Association (OCA)",
|
||||
'website': 'https://www.tecnativa.com',
|
||||
'depends': [
|
||||
'contract',
|
||||
],
|
||||
'depends': ['contract'],
|
||||
'data': [
|
||||
'security/ir.model.access.csv',
|
||||
'views/abstract_contract_view.xml',
|
||||
|
||||
@@ -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.'))
|
||||
|
||||
@@ -12,44 +12,51 @@ from odoo import exceptions
|
||||
class TestContractVariableQuantity(odoo.tests.HttpCase):
|
||||
def setUp(self):
|
||||
super(TestContractVariableQuantity, self).setUp()
|
||||
self.partner = self.env['res.partner'].create({
|
||||
'name': 'Test partner',
|
||||
})
|
||||
self.product = self.env['product.product'].create({
|
||||
'name': 'Test product',
|
||||
})
|
||||
self.contract = self.env['account.analytic.account'].create({
|
||||
'name': 'Test Contract',
|
||||
'partner_id': self.partner.id,
|
||||
'pricelist_id': self.partner.property_product_pricelist.id,
|
||||
'recurring_invoices': True,
|
||||
})
|
||||
self.formula = self.env['contract.line.qty.formula'].create({
|
||||
'name': 'Test formula',
|
||||
# For testing each of the possible variables
|
||||
'code': 'env["res.users"]\n'
|
||||
'context.get("lang")\n'
|
||||
'user.id\n'
|
||||
'line.qty_type\n'
|
||||
'contract.id\n'
|
||||
'invoice.id\n'
|
||||
'result = 12',
|
||||
})
|
||||
self.contract_line = self.env['account.analytic.invoice.line'].create({
|
||||
'contract_id': self.contract.id,
|
||||
'product_id': self.product.id,
|
||||
'name': 'Test',
|
||||
'qty_type': 'variable',
|
||||
'qty_formula_id': self.formula.id,
|
||||
'quantity': 1,
|
||||
'uom_id': self.product.uom_id.id,
|
||||
'price_unit': 100,
|
||||
'discount': 50,
|
||||
'recurring_rule_type': 'monthly',
|
||||
'recurring_interval': 1,
|
||||
'date_start': '2016-02-15',
|
||||
'recurring_next_date': '2016-02-29',
|
||||
})
|
||||
self.partner = self.env['res.partner'].create({'name': 'Test partner'})
|
||||
self.product = self.env['product.product'].create(
|
||||
{'name': 'Test product'}
|
||||
)
|
||||
self.contract = self.env['account.analytic.account'].create(
|
||||
{
|
||||
'name': 'Test Contract',
|
||||
'partner_id': self.partner.id,
|
||||
'pricelist_id': self.partner.property_product_pricelist.id,
|
||||
'recurring_invoices': True,
|
||||
}
|
||||
)
|
||||
self.formula = self.env['contract.line.qty.formula'].create(
|
||||
{
|
||||
'name': 'Test formula',
|
||||
# For testing each of the possible variables
|
||||
'code': 'env["res.users"]\n'
|
||||
'context.get("lang")\n'
|
||||
'user.id\n'
|
||||
'line.qty_type\n'
|
||||
'contract.id\n'
|
||||
'quantity\n'
|
||||
'period_first_date\n'
|
||||
'period_last_date\n'
|
||||
'invoice_date\n'
|
||||
'result = 12',
|
||||
}
|
||||
)
|
||||
self.contract_line = self.env['account.analytic.invoice.line'].create(
|
||||
{
|
||||
'contract_id': self.contract.id,
|
||||
'product_id': self.product.id,
|
||||
'name': 'Test',
|
||||
'qty_type': 'variable',
|
||||
'qty_formula_id': self.formula.id,
|
||||
'quantity': 1,
|
||||
'uom_id': self.product.uom_id.id,
|
||||
'price_unit': 100,
|
||||
'discount': 50,
|
||||
'recurring_rule_type': 'monthly',
|
||||
'recurring_interval': 1,
|
||||
'date_start': '2016-02-15',
|
||||
'recurring_next_date': '2016-02-29',
|
||||
}
|
||||
)
|
||||
|
||||
def test_check_invalid_code(self):
|
||||
with self.assertRaises(exceptions.ValidationError):
|
||||
|
||||
Reference in New Issue
Block a user