[REF] - Use method _get_quantity_to_invoice.

This commit is contained in:
sbejaoui
2019-03-11 23:39:40 +01:00
committed by Víctor Martínez
parent 78a42762b8
commit 8d73c776fd
5 changed files with 90 additions and 66 deletions

View File

@@ -10,9 +10,7 @@
'author': "Tecnativa," 'author': "Tecnativa,"
"Odoo Community Association (OCA)", "Odoo Community Association (OCA)",
'website': 'https://www.tecnativa.com', 'website': 'https://www.tecnativa.com',
'depends': [ 'depends': ['contract'],
'contract',
],
'data': [ 'data': [
'security/ir.model.access.csv', 'security/ir.model.access.csv',
'views/abstract_contract_view.xml', 'views/abstract_contract_view.xml',

View File

@@ -13,6 +13,11 @@ class AccountAbstractAnalyticContractLine(models.AbstractModel):
selection=[ selection=[
('fixed', 'Fixed quantity'), ('fixed', 'Fixed quantity'),
('variable', 'Variable quantity'), ('variable', 'Variable quantity'),
], required=True, default='fixed', string="Qty. type") ],
required=True,
default='fixed',
string="Qty. type",
)
qty_formula_id = fields.Many2one( qty_formula_id = fields.Many2one(
comodel_name="contract.line.qty.formula", string="Qty. formula") comodel_name="contract.line.qty.formula", string="Qty. formula"
)

View File

@@ -12,18 +12,27 @@ class AccountAnalyticInvoiceLine(models.Model):
_inherit = 'account.analytic.invoice.line' _inherit = 'account.analytic.invoice.line'
@api.multi @api.multi
def _prepare_invoice_line(self, invoice_id=False): def _get_quantity_to_invoice(
vals = super(AccountAnalyticInvoiceLine, self)._prepare_invoice_line( self, period_first_date, period_last_date, invoice_date
invoice_id=invoice_id ):
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': if self.qty_type == 'variable':
eval_context = { eval_context = {
'env': self.env, 'env': self.env,
'context': self.env.context, 'context': self.env.context,
'user': self.env.user, 'user': self.env.user,
'line': self, 'line': self,
'quantity': quantity,
'period_first_date': period_first_date,
'period_last_date': period_last_date,
'invoice_date': invoice_date,
'contract': self.contract_id, 'contract': self.contract_id,
'invoice': self.env['account.invoice'].browse(invoice_id),
} }
safe_eval( safe_eval(
self.qty_formula_id.code.strip(), self.qty_formula_id.code.strip(),
@@ -31,24 +40,23 @@ class AccountAnalyticInvoiceLine(models.Model):
mode="exec", mode="exec",
nocopy=True, nocopy=True,
) # nocopy for returning result ) # nocopy for returning result
qty = eval_context.get('result', 0) quantity = eval_context.get('result', 0)
if self.contract_id.skip_zero_qty and float_is_zero( return quantity
qty,
@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( self.env['decimal.precision'].precision_get(
'Product Unit of Measure' '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 return vals

View File

@@ -23,12 +23,18 @@ class ContractLineFormula(models.Model):
'line': self.env['account.analytic.invoice.line'], 'line': self.env['account.analytic.invoice.line'],
'contract': self.env['account.analytic.account'], 'contract': self.env['account.analytic.account'],
'invoice': self.env['account.invoice'], 'invoice': self.env['account.invoice'],
'quantity': 0,
'period_first_date': False,
'period_last_date': False,
'invoice_date': False,
} }
try: try:
safe_eval( safe_eval(
self.code.strip(), eval_context, mode="exec", nocopy=True) self.code.strip(), eval_context, mode="exec", nocopy=True
)
except Exception as e: except Exception as e:
raise exceptions.ValidationError( raise exceptions.ValidationError(
_('Error evaluating code.\nDetails: %s') % e) _('Error evaluating code.\nDetails: %s') % e
)
if 'result' not in eval_context: if 'result' not in eval_context:
raise exceptions.ValidationError(_('No valid result returned.')) raise exceptions.ValidationError(_('No valid result returned.'))

View File

@@ -12,19 +12,20 @@ from odoo import exceptions
class TestContractVariableQuantity(odoo.tests.HttpCase): class TestContractVariableQuantity(odoo.tests.HttpCase):
def setUp(self): def setUp(self):
super(TestContractVariableQuantity, self).setUp() super(TestContractVariableQuantity, self).setUp()
self.partner = self.env['res.partner'].create({ self.partner = self.env['res.partner'].create({'name': 'Test partner'})
'name': 'Test partner', self.product = self.env['product.product'].create(
}) {'name': 'Test product'}
self.product = self.env['product.product'].create({ )
'name': 'Test product', self.contract = self.env['account.analytic.account'].create(
}) {
self.contract = self.env['account.analytic.account'].create({
'name': 'Test Contract', 'name': 'Test Contract',
'partner_id': self.partner.id, 'partner_id': self.partner.id,
'pricelist_id': self.partner.property_product_pricelist.id, 'pricelist_id': self.partner.property_product_pricelist.id,
'recurring_invoices': True, 'recurring_invoices': True,
}) }
self.formula = self.env['contract.line.qty.formula'].create({ )
self.formula = self.env['contract.line.qty.formula'].create(
{
'name': 'Test formula', 'name': 'Test formula',
# For testing each of the possible variables # For testing each of the possible variables
'code': 'env["res.users"]\n' 'code': 'env["res.users"]\n'
@@ -32,10 +33,15 @@ class TestContractVariableQuantity(odoo.tests.HttpCase):
'user.id\n' 'user.id\n'
'line.qty_type\n' 'line.qty_type\n'
'contract.id\n' 'contract.id\n'
'invoice.id\n' 'quantity\n'
'period_first_date\n'
'period_last_date\n'
'invoice_date\n'
'result = 12', 'result = 12',
}) }
self.contract_line = self.env['account.analytic.invoice.line'].create({ )
self.contract_line = self.env['account.analytic.invoice.line'].create(
{
'contract_id': self.contract.id, 'contract_id': self.contract.id,
'product_id': self.product.id, 'product_id': self.product.id,
'name': 'Test', 'name': 'Test',
@@ -49,7 +55,8 @@ class TestContractVariableQuantity(odoo.tests.HttpCase):
'recurring_interval': 1, 'recurring_interval': 1,
'date_start': '2016-02-15', 'date_start': '2016-02-15',
'recurring_next_date': '2016-02-29', 'recurring_next_date': '2016-02-29',
}) }
)
def test_check_invalid_code(self): def test_check_invalid_code(self):
with self.assertRaises(exceptions.ValidationError): with self.assertRaises(exceptions.ValidationError):