# -*- coding: utf-8 -*- ############################################################################## # # OpenERP, Open Source Management Solution # Copyright (C) 2004-2010 Tiny SPRL (). # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as # published by the Free Software Foundation, either version 3 of the # License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . # ############################################################################## import time from lxml import etree from openerp.osv import fields, osv from openerp import tools from openerp.tools.translate import _ class one2many_mod2(fields.one2many): def get(self, cr, obj, ids, name, user=None, offset=0, context=None, values=None): if context is None: context = {} res = {} for id in ids: res[id] = [] ids2 = None if 'journal_id' in context: journal = obj.pool.get('account.journal').browse(cr, user, context['journal_id'], context=context) pnum = int(name[7]) -1 plan = journal.plan_id if plan and len(plan.plan_ids) > pnum: acc_id = plan.plan_ids[pnum].root_analytic_id.id ids2 = obj.pool[self._obj].search(cr, user, [(self._fields_id,'in',ids),('analytic_account_id','child_of',[acc_id])], limit=self._limit) if ids2 is None: ids2 = obj.pool[self._obj].search(cr, user, [(self._fields_id,'in',ids)], limit=self._limit) for r in obj.pool[self._obj].read(cr, user, ids2, [self._fields_id], context=context, load='_classic_write'): key = r[self._fields_id] if isinstance(key, tuple): # Read return a tuple in the case where the field is a many2one # but we want to get the id of this field. key = key[0] res[key].append( r['id'] ) return res class account_analytic_line(osv.osv): _inherit = 'account.analytic.line' _description = 'Analytic Line' def _get_amount(self, cr, uid, ids, name, args, context=None): res = {} for id in ids: res.setdefault(id, 0.0) for line in self.browse(cr, uid, ids, context=context): amount = line.move_id and line.move_id.amount_currency * (line.percentage / 100) or 0.0 res[line.id] = amount return res _columns = { 'amount_currency': fields.function(_get_amount, string="Amount Currency", type="float", store=True, help="The amount expressed in the related account currency if not equal to the company one.", readonly=True), 'percentage': fields.float('Percentage') } class account_analytic_plan(osv.osv): _name = "account.analytic.plan" _description = "Analytic Plan" _columns = { 'name': fields.char('Analytic Plan', required=True, select=True), 'plan_ids': fields.one2many('account.analytic.plan.line', 'plan_id', 'Analytic Plans', copy=True), } class account_analytic_plan_line(osv.osv): _name = "account.analytic.plan.line" _description = "Analytic Plan Line" _order = "sequence, id" _columns = { 'plan_id': fields.many2one('account.analytic.plan','Analytic Plan',required=True), 'name': fields.char('Axis Name', required=True, select=True), 'sequence': fields.integer('Sequence'), 'root_analytic_id': fields.many2one('account.analytic.account', 'Root Account', help="Root account of this plan.", required=False), 'min_required': fields.float('Minimum Allowed (%)'), 'max_required': fields.float('Maximum Allowed (%)'), } _defaults = { 'min_required': 100.0, 'max_required': 100.0, } class account_analytic_plan_instance(osv.osv): _name = "account.analytic.plan.instance" _description = "Analytic Plan Instance" _columns = { 'name': fields.char('Analytic Distribution'), 'code': fields.char('Distribution Code', size=16), 'journal_id': fields.many2one('account.analytic.journal', 'Analytic Journal' ), 'account_ids': fields.one2many('account.analytic.plan.instance.line', 'plan_id', 'Account Id', copy=True), 'account1_ids': one2many_mod2('account.analytic.plan.instance.line', 'plan_id', 'Account1 Id'), 'account2_ids': one2many_mod2('account.analytic.plan.instance.line', 'plan_id', 'Account2 Id'), 'account3_ids': one2many_mod2('account.analytic.plan.instance.line', 'plan_id', 'Account3 Id'), 'account4_ids': one2many_mod2('account.analytic.plan.instance.line', 'plan_id', 'Account4 Id'), 'account5_ids': one2many_mod2('account.analytic.plan.instance.line', 'plan_id', 'Account5 Id'), 'account6_ids': one2many_mod2('account.analytic.plan.instance.line', 'plan_id', 'Account6 Id'), 'plan_id': fields.many2one('account.analytic.plan', "Model's Plan"), } def search(self, cr, user, args, offset=0, limit=None, order=None, context=None, count=False): if context is None: context = {} journal_obj = self.pool.get('account.journal') if context.get('journal_id', False): journal = journal_obj.browse(cr, user, [context['journal_id']], context=context)[0] analytic_journal = journal.analytic_journal_id and journal.analytic_journal_id.id or False args.append('|') args.append(('journal_id', '=', analytic_journal)) args.append(('journal_id', '=', False)) res = super(account_analytic_plan_instance, self).search(cr, user, args, offset=offset, limit=limit, order=order, context=context, count=count) return res def _default_journal(self, cr, uid, context=None): if context is None: context = {} journal_obj = self.pool.get('account.journal') if context.has_key('journal_id') and context['journal_id']: journal = journal_obj.browse(cr, uid, context['journal_id'], context=context) if journal.analytic_journal_id: return journal.analytic_journal_id.id return False _defaults = { 'plan_id': False, 'journal_id': _default_journal, } def name_get(self, cr, uid, ids, context=None): res = [] for inst in self.browse(cr, uid, ids, context=context): name = inst.name or '/' if name and inst.code: name=name+' ('+inst.code+')' res.append((inst.id, name)) return res def name_search(self, cr, uid, name, args=None, operator='ilike', context=None, limit=100): args = args or [] if name: ids = self.search(cr, uid, [('code', '=', name)] + args, limit=limit, context=context or {}) if not ids: ids = self.search(cr, uid, [('name', operator, name)] + args, limit=limit, context=context or {}) else: ids = self.search(cr, uid, args, limit=limit, context=context or {}) return self.name_get(cr, uid, ids, context or {}) def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False): if context is None: context = {} wiz_id = self.pool.get('ir.actions.act_window').search(cr, uid, [("name","=","analytic.plan.create.model.action")], context=context) res = super(account_analytic_plan_instance,self).fields_view_get(cr, uid, view_id, view_type, context=context, toolbar=toolbar, submenu=submenu) journal_obj = self.pool.get('account.journal') analytic_plan_obj = self.pool.get('account.analytic.plan') if (res['type']=='form'): plan_id = False if context.get('journal_id', False): plan_id = journal_obj.browse(cr, uid, int(context['journal_id']), context=context).plan_id elif context.get('plan_id', False): plan_id = analytic_plan_obj.browse(cr, uid, int(context['plan_id']), context=context) if plan_id: i=1 res['arch'] = """