mirror of
https://github.com/OCA/contract.git
synced 2025-02-13 17:57:24 +02:00
[FIX] contract: Template lines handling (#92)
Update contract template lines handling to fix #80, and fix #59 #100
This commit is contained in:
committed by
Jean-Charles Drubay
parent
085d05917d
commit
6cd28264b4
@@ -4,4 +4,5 @@
|
||||
from . import account_analytic_contract
|
||||
from . import account_analytic_account
|
||||
from . import account_analytic_invoice_line
|
||||
from . import account_analytic_contract_line
|
||||
from . import account_invoice
|
||||
|
||||
@@ -23,6 +23,12 @@ class AccountAnalyticAccount(models.Model):
|
||||
string='Contract Template',
|
||||
comodel_name='account.analytic.contract',
|
||||
)
|
||||
recurring_invoice_line_ids = fields.One2many(
|
||||
string='Invoice Lines',
|
||||
comodel_name='account.analytic.invoice.line',
|
||||
inverse_name='analytic_account_id',
|
||||
copy=True,
|
||||
)
|
||||
date_start = fields.Date(default=fields.Date.context_today)
|
||||
recurring_invoices = fields.Boolean(
|
||||
string='Generate recurring invoices automatically',
|
||||
@@ -41,16 +47,28 @@ class AccountAnalyticAccount(models.Model):
|
||||
|
||||
@api.onchange('contract_template_id')
|
||||
def _onchange_contract_template_id(self):
|
||||
""" It updates contract fields with that of the template """
|
||||
"""Update the contract fields with that of the template.
|
||||
|
||||
Take special consideration with the `recurring_invoice_line_ids`,
|
||||
which must be created using the data from the contract lines. Cascade
|
||||
deletion ensures that any errant lines that are created are also
|
||||
deleted.
|
||||
"""
|
||||
|
||||
contract = self.contract_template_id
|
||||
|
||||
for field_name, field in contract._fields.iteritems():
|
||||
if any((
|
||||
|
||||
if field.name == 'recurring_invoice_line_ids':
|
||||
lines = self._convert_contract_lines(contract)
|
||||
self.recurring_invoice_line_ids = lines
|
||||
|
||||
elif not any((
|
||||
field.compute, field.related, field.automatic,
|
||||
field.readonly, field.company_dependent,
|
||||
field.name in self.NO_SYNC,
|
||||
)):
|
||||
continue
|
||||
self[field_name] = self.contract_template_id[field_name]
|
||||
self[field_name] = self.contract_template_id[field_name]
|
||||
|
||||
@api.onchange('recurring_invoices')
|
||||
def _onchange_recurring_invoices(self):
|
||||
@@ -61,6 +79,15 @@ class AccountAnalyticAccount(models.Model):
|
||||
def _onchange_partner_id(self):
|
||||
self.pricelist_id = self.partner_id.property_product_pricelist.id
|
||||
|
||||
@api.multi
|
||||
def _convert_contract_lines(self, contract):
|
||||
self.ensure_one()
|
||||
new_lines = []
|
||||
for contract_line in contract.recurring_invoice_line_ids:
|
||||
vals = contract_line._convert_to_write(contract_line.read()[0])
|
||||
new_lines.append((0, 0, vals))
|
||||
return new_lines
|
||||
|
||||
@api.model
|
||||
def get_relative_delta(self, recurring_rule_type, interval):
|
||||
if recurring_rule_type == 'daily':
|
||||
|
||||
@@ -25,7 +25,7 @@ class AccountAnalyticContract(models.Model):
|
||||
string='Pricelist',
|
||||
)
|
||||
recurring_invoice_line_ids = fields.One2many(
|
||||
comodel_name='account.analytic.invoice.line',
|
||||
comodel_name='account.analytic.contract.line',
|
||||
inverse_name='analytic_account_id',
|
||||
copy=True,
|
||||
string='Invoice Lines',
|
||||
|
||||
19
contract/models/account_analytic_contract_line.py
Normal file
19
contract/models/account_analytic_contract_line.py
Normal file
@@ -0,0 +1,19 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Copyright 2017 LasLabs Inc.
|
||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||
|
||||
from odoo import fields, models
|
||||
|
||||
|
||||
class AccountAnalyticContractLine(models.Model):
|
||||
|
||||
_name = 'account.analytic.contract.line'
|
||||
_description = 'Contract Lines'
|
||||
_inherit = 'account.analytic.invoice.line'
|
||||
|
||||
analytic_account_id = fields.Many2one(
|
||||
string='Contract',
|
||||
comodel_name='account.analytic.contract',
|
||||
required=True,
|
||||
ondelete='cascade',
|
||||
)
|
||||
@@ -3,7 +3,7 @@
|
||||
# © 2014 Angel Moya <angel.moya@domatix.com>
|
||||
# © 2015 Pedro M. Baeza <pedro.baeza@tecnativa.com>
|
||||
# © 2016 Carlos Dauden <carlos.dauden@tecnativa.com>
|
||||
# Copyright 2016 LasLabs Inc.
|
||||
# Copyright 2016-2017 LasLabs Inc.
|
||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||
|
||||
from odoo import api, fields, models
|
||||
@@ -16,23 +16,44 @@ class AccountAnalyticInvoiceLine(models.Model):
|
||||
_name = 'account.analytic.invoice.line'
|
||||
|
||||
product_id = fields.Many2one(
|
||||
'product.product', string='Product', required=True)
|
||||
'product.product',
|
||||
string='Product',
|
||||
required=True,
|
||||
)
|
||||
analytic_account_id = fields.Many2one(
|
||||
'account.analytic.account', string='Analytic Account')
|
||||
name = fields.Text(string='Description', required=True)
|
||||
quantity = fields.Float(default=1.0, required=True)
|
||||
'account.analytic.account',
|
||||
string='Analytic Account',
|
||||
required=True,
|
||||
ondelete='cascade',
|
||||
)
|
||||
name = fields.Text(
|
||||
string='Description',
|
||||
required=True,
|
||||
)
|
||||
quantity = fields.Float(
|
||||
default=1.0,
|
||||
required=True,
|
||||
)
|
||||
uom_id = fields.Many2one(
|
||||
'product.uom', string='Unit of Measure', required=True)
|
||||
price_unit = fields.Float('Unit Price', required=True)
|
||||
'product.uom',
|
||||
string='Unit of Measure',
|
||||
required=True,
|
||||
)
|
||||
price_unit = fields.Float(
|
||||
'Unit Price',
|
||||
required=True,
|
||||
)
|
||||
price_subtotal = fields.Float(
|
||||
compute='_compute_price_subtotal',
|
||||
digits=dp.get_precision('Account'),
|
||||
string='Sub Total')
|
||||
string='Sub Total',
|
||||
)
|
||||
discount = fields.Float(
|
||||
string='Discount (%)',
|
||||
digits=dp.get_precision('Discount'),
|
||||
help='Discount that is applied in generated invoices.'
|
||||
' It should be less or equal to 100')
|
||||
' It should be less or equal to 100',
|
||||
)
|
||||
|
||||
@api.multi
|
||||
@api.depends('quantity', 'price_unit', 'discount')
|
||||
@@ -68,14 +89,20 @@ class AccountAnalyticInvoiceLine(models.Model):
|
||||
self.uom_id.category_id.id):
|
||||
vals['uom_id'] = self.product_id.uom_id
|
||||
|
||||
date = (
|
||||
self.analytic_account_id.recurring_next_date or
|
||||
fields.Datetime.now()
|
||||
)
|
||||
if self.analytic_account_id._name == 'account.analytic.account':
|
||||
date = (
|
||||
self.analytic_account_id.recurring_next_date or
|
||||
fields.Datetime.now()
|
||||
)
|
||||
partner = self.analytic_account_id.partner_id
|
||||
|
||||
else:
|
||||
date = fields.Datetime.now()
|
||||
partner = self.env.user.partner_id
|
||||
|
||||
product = self.product_id.with_context(
|
||||
lang=self.analytic_account_id.partner_id.lang,
|
||||
partner=self.analytic_account_id.partner_id.id,
|
||||
lang=partner.lang,
|
||||
partner=partner.id,
|
||||
quantity=self.quantity,
|
||||
date=date,
|
||||
pricelist=self.analytic_account_id.pricelist_id.id,
|
||||
|
||||
Reference in New Issue
Block a user