Browse Source

[REF] - Use method _get_quantity_to_invoice.

pull/238/head
sbejaoui 6 years ago
parent
commit
c4a76debe3
  1. 4
      contract_variable_quantity/__manifest__.py
  2. 9
      contract_variable_quantity/models/abstract_contract_line.py
  3. 50
      contract_variable_quantity/models/contract_line.py
  4. 10
      contract_variable_quantity/models/contract_line_formula.py
  5. 83
      contract_variable_quantity/tests/test_contract_variable_quantity.py

4
contract_variable_quantity/__manifest__.py

@ -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': [
'contract',
],
'depends': ['contract'],
'data': [ 'data': [
'security/ir.model.access.csv', 'security/ir.model.access.csv',
'views/abstract_contract_view.xml', 'views/abstract_contract_view.xml',

9
contract_variable_quantity/models/abstract_contract_line.py

@ -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"
)

50
contract_variable_quantity/models/contract_line.py

@ -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):
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': 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)
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( 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

10
contract_variable_quantity/models/contract_line_formula.py

@ -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.'))

83
contract_variable_quantity/tests/test_contract_variable_quantity.py

@ -12,44 +12,51 @@ 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({
'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): def test_check_invalid_code(self):
with self.assertRaises(exceptions.ValidationError): with self.assertRaises(exceptions.ValidationError):

Loading…
Cancel
Save