It is using an already existing field that wasn't used anymore ('account_type') but as a lot of domains on analytic accounts were
- wrong (still using old fields state and type that have been removed as well in the same commit),
- or defined directly in the views
so, an update of some modules may be needed in order to avoid further problems.
110 lines
5.1 KiB
Python
110 lines
5.1 KiB
Python
# -*- coding: utf-8 -*-
|
|
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
|
|
|
from openerp import api, fields, models
|
|
|
|
|
|
class account_analytic_tag(models.Model):
|
|
_name = 'account.analytic.tag'
|
|
_description = 'Analytic Tags'
|
|
name = fields.Char(string='Analytic Tag', index=True, required=True)
|
|
color = fields.Integer('Color Index')
|
|
|
|
|
|
class account_analytic_account(models.Model):
|
|
_name = 'account.analytic.account'
|
|
_inherit = ['mail.thread']
|
|
_description = 'Analytic Account'
|
|
_order = 'code, name asc'
|
|
|
|
@api.multi
|
|
def _compute_debit_credit_balance(self):
|
|
analytic_line_obj = self.env['account.analytic.line']
|
|
domain = [('account_id', 'in', self.mapped('id'))]
|
|
if self._context.get('from_date', False):
|
|
domain.append(('date', '>=', self._context['from_date']))
|
|
if self._context.get('to_date', False):
|
|
domain.append(('date', '<=', self._context['to_date']))
|
|
|
|
account_amounts = analytic_line_obj.search_read(domain, ['account_id', 'amount'])
|
|
account_ids = set([line['account_id'][0] for line in account_amounts])
|
|
data_debit = {account_id: 0.0 for account_id in account_ids}
|
|
data_credit = {account_id: 0.0 for account_id in account_ids}
|
|
for account_amount in account_amounts:
|
|
if account_amount['amount'] < 0.0:
|
|
data_debit[account_amount['account_id'][0]] += account_amount['amount']
|
|
else:
|
|
data_credit[account_amount['account_id'][0]] += account_amount['amount']
|
|
|
|
for account in self:
|
|
account.debit = abs(data_debit.get(account.id, 0.0))
|
|
account.credit = data_credit.get(account.id, 0.0)
|
|
account.balance = account.credit - account.debit
|
|
|
|
name = fields.Char(string='Analytic Account', index=True, required=True, track_visibility='onchange')
|
|
code = fields.Char(string='Reference', index=True, track_visibility='onchange')
|
|
# FIXME: we reused account_type to implement the closed accounts (feature removed by mistake on release of v9) without modifying the schemas on already released v9, but it would be more clean to rename it
|
|
account_type = fields.Selection([
|
|
('normal', 'Active'),
|
|
('closed', 'Archived')
|
|
], string='State', required=True, default='normal')
|
|
|
|
tag_ids = fields.Many2many('account.analytic.tag', 'account_analytic_account_tag_rel', 'account_id', 'tag_id', string='Tags', copy=True)
|
|
line_ids = fields.One2many('account.analytic.line', 'account_id', string="Analytic Lines")
|
|
|
|
company_id = fields.Many2one('res.company', string='Company', required=True, default=lambda self: self.env.user.company_id)
|
|
|
|
# use auto_join to speed up name_search call
|
|
partner_id = fields.Many2one('res.partner', string='Customer', auto_join=True)
|
|
|
|
balance = fields.Monetary(compute='_compute_debit_credit_balance', string='Balance')
|
|
debit = fields.Monetary(compute='_compute_debit_credit_balance', string='Debit')
|
|
credit = fields.Monetary(compute='_compute_debit_credit_balance', string='Credit')
|
|
|
|
currency_id = fields.Many2one(related="company_id.currency_id", string="Currency", readonly=True)
|
|
|
|
@api.multi
|
|
def name_get(self):
|
|
res = []
|
|
for analytic in self:
|
|
name = analytic.name
|
|
if analytic.code:
|
|
name = '['+analytic.code+'] '+name
|
|
if analytic.partner_id:
|
|
name = name +' - '+analytic.partner_id.commercial_partner_id.name
|
|
res.append((analytic.id, name))
|
|
return res
|
|
|
|
@api.model
|
|
def name_search(self, name='', args=None, operator='ilike', limit=100):
|
|
args = args or []
|
|
domain = ['|', ('code', operator, name), ('name', operator, name)]
|
|
partners = self.env['res.partner'].search([('name', operator, name)], limit=limit)
|
|
if partners:
|
|
domain = ['|'] + domain + [('partner_id', 'in', partners.ids)]
|
|
recs = self.search(domain + args, limit=limit)
|
|
return recs.name_get()
|
|
|
|
|
|
class account_analytic_line(models.Model):
|
|
_name = 'account.analytic.line'
|
|
_description = 'Analytic Line'
|
|
_order = 'date desc, id desc'
|
|
|
|
@api.model
|
|
def _default_user(self):
|
|
return self.env.user.id
|
|
|
|
name = fields.Char('Description', required=True)
|
|
date = fields.Date('Date', required=True, index=True, default=fields.Date.context_today)
|
|
amount = fields.Monetary('Amount', required=True, default=0.0)
|
|
unit_amount = fields.Float('Quantity', default=0.0)
|
|
account_id = fields.Many2one('account.analytic.account', 'Analytic Account', required=True, ondelete='restrict', index=True)
|
|
partner_id = fields.Many2one('res.partner', string='Partner')
|
|
user_id = fields.Many2one('res.users', string='User', default=_default_user)
|
|
|
|
tag_ids = fields.Many2many('account.analytic.tag', 'account_analytic_line_tag_rel', 'line_id', 'tag_id', string='Tags', copy=True)
|
|
|
|
company_id = fields.Many2one(related='account_id.company_id', string='Company', store=True, readonly=True)
|
|
currency_id = fields.Many2one(related="company_id.currency_id", string="Currency", readonly=True)
|