Browse Source

[FIX] - Onchange contract line don't reste contract lines

[FIX] - Rename _get_invoiced_period to _get_period_to_invoice

[ADD] - Add stop_at_date_end to _get_period_to_invoice

[FIX] - Remove useless filter
13.0-mig-contract
sbejaoui 6 years ago
committed by Administrator
parent
commit
b358f76d2f
  1. 26
      contract/models/abstract_contract_line.py
  2. 9
      contract/models/contract.py
  3. 18
      contract/models/contract_line.py
  4. 52
      contract/tests/test_contract.py
  5. 5
      contract/views/contract.xml

26
contract/models/abstract_contract_line.py

@ -116,17 +116,6 @@ class AccountAbstractAnalyticContractLine(models.AbstractModel):
oldname='analytic_account_id',
)
@api.multi
def _get_invoiced_period(self, last_date_invoiced, recurring_next_date):
return self.date_start, last_date_invoiced, recurring_next_date
@api.multi
def _get_quantity_to_invoice(
self, period_first_date, period_last_date, invoice_date
):
self.ensure_one()
return self.quantity
@api.depends(
'automatic_price',
'specific_price',
@ -141,13 +130,10 @@ class AccountAbstractAnalyticContractLine(models.AbstractModel):
"""
for line in self:
if line.automatic_price:
dates = line._get_invoiced_period(
line.last_date_invoiced, line.recurring_next_date
)
product = line.product_id.with_context(
quantity=line.env.context.get(
'contract_line_qty',
line._get_quantity_to_invoice(*dates),
line.quantity,
),
pricelist=line.contract_id.pricelist_id.id,
partner=line.contract_id.partner_id.id,
@ -170,10 +156,7 @@ class AccountAbstractAnalyticContractLine(models.AbstractModel):
@api.depends('quantity', 'price_unit', 'discount')
def _compute_price_subtotal(self):
for line in self:
dates = line._get_invoiced_period(
line.last_date_invoiced, line.recurring_next_date
)
subtotal = line._get_quantity_to_invoice(*dates) * line.price_unit
subtotal = line.quantity * line.price_unit
discount = line.discount / 100
subtotal *= 1 - discount
if line.contract_id.pricelist_id:
@ -210,13 +193,10 @@ class AccountAbstractAnalyticContractLine(models.AbstractModel):
date = self.recurring_next_date or fields.Date.context_today(self)
partner = self.contract_id.partner_id or self.env.user.partner_id
dates = self._get_invoiced_period(
self.last_date_invoiced, self.recurring_next_date
)
product = self.product_id.with_context(
lang=partner.lang,
partner=partner.id,
quantity=self._get_quantity_to_invoice(*dates),
quantity=self.quantity,
date=date,
pricelist=self.contract_id.pricelist_id.id,
uom=self.uom_id.id,

9
contract/models/contract.py

@ -171,7 +171,7 @@ class AccountAnalyticAccount(models.Model):
for field_name, field in contract_template_id._fields.items():
if field.name == 'recurring_invoice_line_ids':
lines = self._convert_contract_lines(contract_template_id)
self.recurring_invoice_line_ids = lines
self.recurring_invoice_line_ids += lines
elif not any(
(
field.compute,
@ -183,6 +183,7 @@ class AccountAnalyticAccount(models.Model):
)
):
self[field_name] = self.contract_template_id[field_name]
self.recurring_invoice_line_ids._onchange_date_start()
@api.onchange('partner_id')
def _onchange_partner_id(self):
@ -213,7 +214,8 @@ class AccountAnalyticAccount(models.Model):
@api.multi
def _convert_contract_lines(self, contract):
self.ensure_one()
new_lines = []
new_lines = self.env['account.analytic.invoice.line']
contract_line_model = self.env['account.analytic.invoice.line']
for contract_line in contract.recurring_invoice_line_ids:
vals = contract_line._convert_to_write(contract_line.read()[0])
# Remove template link field
@ -222,8 +224,7 @@ class AccountAnalyticAccount(models.Model):
vals['recurring_next_date'] = fields.Date.context_today(
contract_line
)
self.recurring_invoice_line_ids._onchange_date_start()
new_lines.append((0, 0, vals))
new_lines += contract_line_model.new(vals)
return new_lines
@api.multi

18
contract/models/contract_line.py

@ -486,7 +486,7 @@ class AccountAnalyticInvoiceLine(models.Model):
@api.multi
def _prepare_invoice_line(self, invoice_id=False):
self.ensure_one()
dates = self._get_invoiced_period(
dates = self._get_period_to_invoice(
self.last_date_invoiced, self.recurring_next_date
)
invoice_line_vals = {
@ -515,7 +515,9 @@ class AccountAnalyticInvoiceLine(models.Model):
return invoice_line_vals
@api.multi
def _get_invoiced_period(self, last_date_invoiced, recurring_next_date):
def _get_period_to_invoice(
self, last_date_invoiced, recurring_next_date, stop_at_date_end=True
):
self.ensure_one()
first_date_invoiced = False
if not recurring_next_date:
@ -540,8 +542,9 @@ class AccountAnalyticInvoiceLine(models.Model):
last_date_invoiced = recurring_next_date - relativedelta(
days=1
)
if self.date_end and self.date_end < last_date_invoiced:
last_date_invoiced = self.date_end
if stop_at_date_end:
if self.date_end and self.date_end < last_date_invoiced:
last_date_invoiced = self.date_end
return first_date_invoiced, last_date_invoiced, recurring_next_date
@api.multi
@ -1078,3 +1081,10 @@ class AccountAnalyticInvoiceLine(models.Model):
_("Contract line must be canceled before delete")
)
return super(AccountAnalyticInvoiceLine, self).unlink()
@api.multi
def _get_quantity_to_invoice(
self, period_first_date, period_last_date, invoice_date
):
self.ensure_one()
return self.quantity

52
contract/tests/test_contract.py

@ -1324,27 +1324,27 @@ class TestContract(TestContractBase):
len(invoice_lines),
)
def test_get_invoiced_period_monthlylastday(self):
def test_get_period_to_invoice_monthlylastday(self):
self.acct_line.date_start = '2018-01-05'
self.acct_line.recurring_invoicing_type = 'post-paid'
self.acct_line.recurring_rule_type = 'monthlylastday'
self.acct_line.date_end = '2018-03-15'
self.acct_line._onchange_date_start()
first, last, recurring_next_date = self.acct_line._get_invoiced_period(
first, last, recurring_next_date = self.acct_line._get_period_to_invoice(
self.acct_line.last_date_invoiced,
self.acct_line.recurring_next_date,
)
self.assertEqual(first, to_date('2018-01-05'))
self.assertEqual(last, to_date('2018-01-31'))
self.contract.recurring_create_invoice()
first, last, recurring_next_date = self.acct_line._get_invoiced_period(
first, last, recurring_next_date = self.acct_line._get_period_to_invoice(
self.acct_line.last_date_invoiced,
self.acct_line.recurring_next_date,
)
self.assertEqual(first, to_date('2018-02-01'))
self.assertEqual(last, to_date('2018-02-28'))
self.contract.recurring_create_invoice()
first, last, recurring_next_date = self.acct_line._get_invoiced_period(
first, last, recurring_next_date = self.acct_line._get_period_to_invoice(
self.acct_line.last_date_invoiced,
self.acct_line.recurring_next_date,
)
@ -1352,150 +1352,150 @@ class TestContract(TestContractBase):
self.assertEqual(last, to_date('2018-03-15'))
self.acct_line.manual_renew_needed = True
def test_get_invoiced_period_monthly_pre_paid_2(self):
def test_get_period_to_invoice_monthly_pre_paid_2(self):
self.acct_line.date_start = '2018-01-05'
self.acct_line.recurring_invoicing_type = 'pre-paid'
self.acct_line.recurring_rule_type = 'monthly'
self.acct_line.date_end = '2018-08-15'
self.acct_line._onchange_date_start()
self.contract.recurring_create_invoice()
first, last, recurring_next_date = self.acct_line._get_invoiced_period(
first, last, recurring_next_date = self.acct_line._get_period_to_invoice(
self.acct_line.last_date_invoiced,
self.acct_line.recurring_next_date,
)
self.assertEqual(first, to_date('2018-02-05'))
self.assertEqual(last, to_date('2018-03-04'))
self.acct_line.recurring_next_date = '2018-06-05'
first, last, recurring_next_date = self.acct_line._get_invoiced_period(
first, last, recurring_next_date = self.acct_line._get_period_to_invoice(
self.acct_line.last_date_invoiced,
self.acct_line.recurring_next_date,
)
self.assertEqual(first, to_date('2018-02-05'))
self.assertEqual(last, to_date('2018-07-04'))
def test_get_invoiced_period_monthly_post_paid_2(self):
def test_get_period_to_invoice_monthly_post_paid_2(self):
self.acct_line.date_start = '2018-01-05'
self.acct_line.recurring_invoicing_type = 'post-paid'
self.acct_line.recurring_rule_type = 'monthly'
self.acct_line.date_end = '2018-08-15'
self.acct_line._onchange_date_start()
self.contract.recurring_create_invoice()
first, last, recurring_next_date = self.acct_line._get_invoiced_period(
first, last, recurring_next_date = self.acct_line._get_period_to_invoice(
self.acct_line.last_date_invoiced,
self.acct_line.recurring_next_date,
)
self.assertEqual(first, to_date('2018-02-05'))
self.assertEqual(last, to_date('2018-03-04'))
self.acct_line.recurring_next_date = '2018-06-05'
first, last, recurring_next_date = self.acct_line._get_invoiced_period(
first, last, recurring_next_date = self.acct_line._get_period_to_invoice(
self.acct_line.last_date_invoiced,
self.acct_line.recurring_next_date,
)
self.assertEqual(first, to_date('2018-02-05'))
self.assertEqual(last, to_date('2018-06-04'))
def test_get_invoiced_period_monthly_post_paid(self):
def test_get_period_to_invoice_monthly_post_paid(self):
self.acct_line.date_start = '2018-01-05'
self.acct_line.recurring_invoicing_type = 'post-paid'
self.acct_line.recurring_rule_type = 'monthly'
self.acct_line.date_end = '2018-03-15'
self.acct_line._onchange_date_start()
first, last, recurring_next_date = self.acct_line._get_invoiced_period(
first, last, recurring_next_date = self.acct_line._get_period_to_invoice(
self.acct_line.last_date_invoiced,
self.acct_line.recurring_next_date,
)
self.assertEqual(first, to_date('2018-01-05'))
self.assertEqual(last, to_date('2018-02-04'))
self.contract.recurring_create_invoice()
first, last, recurring_next_date = self.acct_line._get_invoiced_period(
first, last, recurring_next_date = self.acct_line._get_period_to_invoice(
self.acct_line.last_date_invoiced,
self.acct_line.recurring_next_date,
)
self.assertEqual(first, to_date('2018-02-05'))
self.assertEqual(last, to_date('2018-03-04'))
self.contract.recurring_create_invoice()
first, last, recurring_next_date = self.acct_line._get_invoiced_period(
first, last, recurring_next_date = self.acct_line._get_period_to_invoice(
self.acct_line.last_date_invoiced,
self.acct_line.recurring_next_date,
)
self.assertEqual(first, to_date('2018-03-05'))
self.assertEqual(last, to_date('2018-03-15'))
def test_get_invoiced_period_monthly_pre_paid(self):
def test_get_period_to_invoice_monthly_pre_paid(self):
self.acct_line.date_start = '2018-01-05'
self.acct_line.recurring_invoicing_type = 'pre-paid'
self.acct_line.recurring_rule_type = 'monthly'
self.acct_line.date_end = '2018-03-15'
self.acct_line._onchange_date_start()
first, last, recurring_next_date = self.acct_line._get_invoiced_period(
first, last, recurring_next_date = self.acct_line._get_period_to_invoice(
self.acct_line.last_date_invoiced,
self.acct_line.recurring_next_date,
)
self.assertEqual(first, to_date('2018-01-05'))
self.assertEqual(last, to_date('2018-02-04'))
self.contract.recurring_create_invoice()
first, last, recurring_next_date = self.acct_line._get_invoiced_period(
first, last, recurring_next_date = self.acct_line._get_period_to_invoice(
self.acct_line.last_date_invoiced,
self.acct_line.recurring_next_date,
)
self.assertEqual(first, to_date('2018-02-05'))
self.assertEqual(last, to_date('2018-03-04'))
self.contract.recurring_create_invoice()
first, last, recurring_next_date = self.acct_line._get_invoiced_period(
first, last, recurring_next_date = self.acct_line._get_period_to_invoice(
self.acct_line.last_date_invoiced,
self.acct_line.recurring_next_date,
)
self.assertEqual(first, to_date('2018-03-05'))
self.assertEqual(last, to_date('2018-03-15'))
def test_get_invoiced_period_yearly_post_paid(self):
def test_get_period_to_invoice_yearly_post_paid(self):
self.acct_line.date_start = '2018-01-05'
self.acct_line.recurring_invoicing_type = 'post-paid'
self.acct_line.recurring_rule_type = 'yearly'
self.acct_line.date_end = '2020-03-15'
self.acct_line._onchange_date_start()
first, last, recurring_next_date = self.acct_line._get_invoiced_period(
first, last, recurring_next_date = self.acct_line._get_period_to_invoice(
self.acct_line.last_date_invoiced,
self.acct_line.recurring_next_date,
)
self.assertEqual(first, to_date('2018-01-05'))
self.assertEqual(last, to_date('2019-01-04'))
self.contract.recurring_create_invoice()
first, last, recurring_next_date = self.acct_line._get_invoiced_period(
first, last, recurring_next_date = self.acct_line._get_period_to_invoice(
self.acct_line.last_date_invoiced,
self.acct_line.recurring_next_date,
)
self.assertEqual(first, to_date('2019-01-05'))
self.assertEqual(last, to_date('2020-01-04'))
self.contract.recurring_create_invoice()
first, last, recurring_next_date = self.acct_line._get_invoiced_period(
first, last, recurring_next_date = self.acct_line._get_period_to_invoice(
self.acct_line.last_date_invoiced,
self.acct_line.recurring_next_date,
)
self.assertEqual(first, to_date('2020-01-05'))
self.assertEqual(last, to_date('2020-03-15'))
def test_get_invoiced_period_yearly_pre_paid(self):
def test_get_period_to_invoice_yearly_pre_paid(self):
self.acct_line.date_start = '2018-01-05'
self.acct_line.recurring_invoicing_type = 'pre-paid'
self.acct_line.recurring_rule_type = 'yearly'
self.acct_line.date_end = '2020-03-15'
self.acct_line._onchange_date_start()
first, last, recurring_next_date = self.acct_line._get_invoiced_period(
first, last, recurring_next_date = self.acct_line._get_period_to_invoice(
self.acct_line.last_date_invoiced,
self.acct_line.recurring_next_date,
)
self.assertEqual(first, to_date('2018-01-05'))
self.assertEqual(last, to_date('2019-01-04'))
self.contract.recurring_create_invoice()
first, last, recurring_next_date = self.acct_line._get_invoiced_period(
first, last, recurring_next_date = self.acct_line._get_period_to_invoice(
self.acct_line.last_date_invoiced,
self.acct_line.recurring_next_date,
)
self.assertEqual(first, to_date('2019-01-05'))
self.assertEqual(last, to_date('2020-01-04'))
self.contract.recurring_create_invoice()
first, last, recurring_next_date = self.acct_line._get_invoiced_period(
first, last, recurring_next_date = self.acct_line._get_period_to_invoice(
self.acct_line.last_date_invoiced,
self.acct_line.recurring_next_date,
)

5
contract/views/contract.xml

@ -184,11 +184,6 @@
<field name="inherit_id"
ref="analytic.view_account_analytic_account_search"/>
<field name="arch" type="xml">
<field name="partner_id" position="after">
<field name="partner_id"
filter_domain="[('partner_id', 'child_of', self)]"
string="Partner and dependents"/>
</field>
<field name="name" position="after">
<field name="journal_id"/>
<field name="pricelist_id"/>

Loading…
Cancel
Save