2009-10-13 05:58:37 +00:00
# -*- coding: utf-8 -*-
2008-08-24 14:45:43 +00:00
##############################################################################
2010-03-15 04:48:15 +00:00
#
2008-11-06 12:02:36 +00:00
# OpenERP, Open Source Management Solution
2010-01-12 09:18:39 +00:00
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
2008-08-24 14:45:43 +00:00
#
2008-11-03 19:18:56 +00:00
# This program is free software: you can redistribute it and/or modify
2009-10-14 11:15:34 +00:00
# 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.
2008-08-24 14:45:43 +00:00
#
2008-11-03 19:18:56 +00:00
# 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
2009-10-14 11:15:34 +00:00
# GNU Affero General Public License for more details.
2008-08-24 14:45:43 +00:00
#
2009-10-14 11:15:34 +00:00
# You should have received a copy of the GNU Affero General Public License
2010-03-15 04:48:15 +00:00
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2008-08-24 14:45:43 +00:00
#
##############################################################################
2010-10-11 11:25:50 +00:00
2008-08-24 14:45:43 +00:00
import time
2010-10-11 11:25:50 +00:00
from lxml import etree
2008-08-24 14:45:43 +00:00
2012-12-06 14:56:32 +00:00
from openerp . osv import fields , osv
from openerp import tools
from openerp . tools . translate import _
2008-08-24 14:45:43 +00:00
class one2many_mod2 ( fields . one2many ) :
def get ( self , cr , obj , ids , name , user = None , offset = 0 , context = None , values = None ) :
2010-11-23 07:05:05 +00:00
if context is None :
2008-08-24 14:45:43 +00:00
context = { }
res = { }
for id in ids :
res [ id ] = [ ]
ids2 = None
if ' journal_id ' in context :
2010-10-11 11:25:50 +00:00
journal = obj . pool . get ( ' account.journal ' ) . browse ( cr , user , context [ ' journal_id ' ] , context = context )
2008-08-24 14:45:43 +00:00
pnum = int ( name [ 7 ] ) - 1
plan = journal . plan_id
2010-10-11 11:25:50 +00:00
if plan and len ( plan . plan_ids ) > pnum :
2008-08-24 14:45:43 +00:00
acc_id = plan . plan_ids [ pnum ] . root_analytic_id . id
ids2 = obj . pool . get ( 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 . get ( self . _obj ) . search ( cr , user , [ ( self . _fields_id , ' in ' , ids ) ] , limit = self . _limit )
for r in obj . pool . get ( self . _obj ) . _read_flat ( cr , user , ids2 , [ self . _fields_id ] , context = context , load = ' _classic_write ' ) :
res [ r [ self . _fields_id ] ] . append ( r [ ' id ' ] )
return res
2011-04-14 06:36:06 +00:00
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 ) :
2011-05-06 06:05:07 +00:00
amount = line . move_id and line . move_id . amount_currency * ( line . percentage / 100 ) or 0.0
2011-05-06 05:21:05 +00:00
res [ line . id ] = amount
2011-04-14 06:36:06 +00:00
return res
_columns = {
2011-07-01 23:41:24 +00:00
' 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 ) ,
2011-05-06 05:21:05 +00:00
' percentage ' : fields . float ( ' Percentage ' )
2011-04-14 06:36:06 +00:00
}
account_analytic_line ( )
2008-08-24 14:45:43 +00:00
class account_analytic_plan ( osv . osv ) :
_name = " account.analytic.plan "
2010-05-19 18:32:32 +00:00
_description = " Analytic Plan "
2008-08-24 14:45:43 +00:00
_columns = {
2010-10-11 11:25:50 +00:00
' name ' : fields . char ( ' Analytic Plan ' , size = 64 , required = True , select = True ) ,
' plan_ids ' : fields . one2many ( ' account.analytic.plan.line ' , ' plan_id ' , ' Analytic Plans ' ) ,
2008-08-24 14:45:43 +00:00
}
2010-08-17 11:13:35 +00:00
2008-08-24 14:45:43 +00:00
account_analytic_plan ( )
class account_analytic_plan_line ( osv . osv ) :
_name = " account.analytic.plan.line "
2010-05-19 18:32:32 +00:00
_description = " Analytic Plan Line "
2010-07-02 14:09:05 +00:00
_order = " sequence, id "
2008-08-24 14:45:43 +00:00
_columns = {
2012-06-05 17:54:11 +00:00
' plan_id ' : fields . many2one ( ' account.analytic.plan ' , ' Analytic Plan ' , required = True ) ,
2008-08-24 14:45:43 +00:00
' name ' : fields . char ( ' Plan Name ' , size = 64 , required = True , select = True ) ,
2010-10-11 11:25:50 +00:00
' sequence ' : fields . integer ( ' Sequence ' ) ,
' root_analytic_id ' : fields . many2one ( ' account.analytic.account ' , ' Root Account ' , help = " Root account of this plan. " , required = False ) ,
2008-08-24 14:45:43 +00:00
' min_required ' : fields . float ( ' Minimum Allowed ( % ) ' ) ,
' max_required ' : fields . float ( ' Maximum Allowed ( % ) ' ) ,
}
_defaults = {
2010-07-02 14:09:05 +00:00
' min_required ' : 100.0 ,
' max_required ' : 100.0 ,
2008-08-24 14:45:43 +00:00
}
2010-08-17 11:13:35 +00:00
2008-08-24 14:45:43 +00:00
account_analytic_plan_line ( )
class account_analytic_plan_instance ( osv . osv ) :
2010-10-11 11:25:50 +00:00
_name = " account.analytic.plan.instance "
_description = " Analytic Plan Instance "
2010-07-02 14:09:05 +00:00
_columns = {
2010-10-11 11:25:50 +00:00
' name ' : fields . char ( ' Analytic Distribution ' , size = 64 ) ,
' code ' : fields . char ( ' Distribution Code ' , size = 16 ) ,
2010-03-12 10:14:25 +00:00
' journal_id ' : fields . many2one ( ' account.analytic.journal ' , ' Analytic Journal ' ) ,
2010-10-11 11:25:50 +00:00
' account_ids ' : fields . one2many ( ' account.analytic.plan.instance.line ' , ' plan_id ' , ' Account Id ' ) ,
' 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 " ) ,
2008-08-24 14:45:43 +00:00
}
2010-03-15 06:55:20 +00:00
def search ( self , cr , user , args , offset = 0 , limit = None , order = None , context = None , count = False ) :
2010-06-08 12:47:44 +00:00
if context is None :
context = { }
2010-10-11 11:25:50 +00:00
journal_obj = self . pool . get ( ' account.journal ' )
2010-03-15 06:55:20 +00:00
if context . get ( ' journal_id ' , False ) :
2010-10-11 11:25:50 +00:00
journal = journal_obj . browse ( cr , user , [ context [ ' journal_id ' ] ] , context = context ) [ 0 ]
2010-03-15 06:55:20 +00:00
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
2008-08-24 14:45:43 +00:00
def copy ( self , cr , uid , id , default = None , context = None ) :
if not default :
default = { }
2010-10-11 11:25:50 +00:00
default . update ( { ' account1_ids ' : False , ' account2_ids ' : False , ' account3_ids ' : False ,
2008-08-24 14:45:43 +00:00
' account4_ids ' : False , ' account5_ids ' : False , ' account6_ids ' : False } )
2010-10-11 11:25:50 +00:00
return super ( account_analytic_plan_instance , self ) . copy ( cr , uid , id , default , context = context )
2008-08-24 14:45:43 +00:00
2010-07-02 14:09:05 +00:00
def _default_journal ( self , cr , uid , context = None ) :
2010-11-23 07:05:05 +00:00
if context is None :
2010-07-02 14:09:05 +00:00
context = { }
2010-10-11 11:25:50 +00:00
journal_obj = self . pool . get ( ' account.journal ' )
2008-11-06 12:02:36 +00:00
if context . has_key ( ' journal_id ' ) and context [ ' journal_id ' ] :
2010-10-11 11:25:50 +00:00
journal = journal_obj . browse ( cr , uid , context [ ' journal_id ' ] , context = context )
2008-11-06 12:02:36 +00:00
if journal . analytic_journal_id :
return journal . analytic_journal_id . id
return False
2008-08-24 14:45:43 +00:00
_defaults = {
2010-07-02 14:09:05 +00:00
' plan_id ' : False ,
2008-11-06 12:02:36 +00:00
' journal_id ' : _default_journal ,
2008-08-24 14:45:43 +00:00
}
2010-07-02 14:09:05 +00:00
def name_get ( self , cr , uid , ids , context = None ) :
2008-08-24 14:45:43 +00:00
res = [ ]
2010-10-11 11:25:50 +00:00
for inst in self . browse ( cr , uid , ids , context = context ) :
2008-08-24 14:45:43 +00:00
name = inst . name or ' / '
if name and inst . code :
name = name + ' ( ' + inst . code + ' ) '
res . append ( ( inst . id , name ) )
return res
2009-12-09 11:43:34 +00:00
def name_search ( self , cr , uid , name , args = None , operator = ' ilike ' , context = None , limit = 100 ) :
2010-10-11 11:25:50 +00:00
args = args or [ ]
2008-08-24 14:45:43 +00:00
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 { } )
2009-09-24 10:46:21 +00:00
def fields_view_get ( self , cr , uid , view_id = None , view_type = ' form ' , context = None , toolbar = False , submenu = False ) :
2010-11-19 13:48:01 +00:00
if context is None :
context = { }
2010-10-11 11:25:50 +00:00
wiz_id = self . pool . get ( ' ir.actions.act_window ' ) . search ( cr , uid , [ ( " name " , " = " , " analytic.plan.create.model.action " ) ] , context = context )
2009-09-24 10:46:21 +00:00
res = super ( account_analytic_plan_instance , self ) . fields_view_get ( cr , uid , view_id , view_type , context , toolbar = toolbar , submenu = submenu )
2010-10-11 11:25:50 +00:00
journal_obj = self . pool . get ( ' account.journal ' )
analytic_plan_obj = self . pool . get ( ' account.analytic.plan ' )
2008-08-24 14:45:43 +00:00
if ( res [ ' type ' ] == ' form ' ) :
plan_id = False
2010-10-11 11:25:50 +00:00
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 )
2008-11-19 13:55:32 +00:00
2008-08-24 14:45:43 +00:00
if plan_id :
i = 1
res [ ' arch ' ] = """ <form string= " %s " >
< field name = " name " / >
< field name = " code " / >
< field name = " journal_id " / >
< button name = " %d " string = " Save This Distribution as a Model " type = " action " colspan = " 2 " / >
""" % (tools.to_xml(plan_id.name), wiz_id[0])
for line in plan_id . plan_ids :
res [ ' arch ' ] + = """
2010-12-22 11:05:57 +00:00
< field name = " account %d _ids " string = " %s " nolabel = " 1 " colspan = " 4 " >
2008-08-24 14:45:43 +00:00
< tree string = " %s " editable = " bottom " >
< field name = " rate " / >
2012-04-25 13:40:18 +00:00
< field name = " analytic_account_id " domain = " [( ' parent_id ' , ' child_of ' ,[ %d ])] " groups = " analytic.group_analytic_accounting " / >
2008-08-24 14:45:43 +00:00
< / tree >
< / field >
< newline / > """ % (i,tools.to_xml(line.name),tools.to_xml(line.name),line.root_analytic_id and line.root_analytic_id.id or 0)
i + = 1
res [ ' arch ' ] + = " </form> "
2009-11-04 10:16:28 +00:00
doc = etree . fromstring ( res [ ' arch ' ] . encode ( ' utf8 ' ) )
2009-01-23 16:56:02 +00:00
xarch , xfields = self . _view_look_dom_arch ( cr , uid , doc , view_id , context = context )
2008-08-24 14:45:43 +00:00
res [ ' arch ' ] = xarch
res [ ' fields ' ] = xfields
return res
else :
return res
def create ( self , cr , uid , vals , context = None ) :
2010-10-11 11:25:50 +00:00
journal_obj = self . pool . get ( ' account.journal ' )
ana_plan_instance_obj = self . pool . get ( ' account.analytic.plan.instance ' )
acct_anal_acct = self . pool . get ( ' account.analytic.account ' )
acct_anal_plan_line_obj = self . pool . get ( ' account.analytic.plan.line ' )
2008-08-24 14:45:43 +00:00
if context and ' journal_id ' in context :
2010-10-11 11:25:50 +00:00
journal = journal_obj . browse ( cr , uid , context [ ' journal_id ' ] , context = context )
2008-08-24 14:45:43 +00:00
2010-10-11 11:25:50 +00:00
pids = ana_plan_instance_obj . search ( cr , uid , [ ( ' name ' , ' = ' , vals [ ' name ' ] ) , ( ' code ' , ' = ' , vals [ ' code ' ] ) , ( ' plan_id ' , ' <> ' , False ) ] , context = context )
2008-08-24 14:45:43 +00:00
if pids :
2012-08-06 15:44:10 +00:00
raise osv . except_osv ( _ ( ' Error! ' ) , _ ( ' A model with this name and code already exists. ' ) )
2010-08-17 11:13:35 +00:00
2010-10-11 11:25:50 +00:00
res = acct_anal_plan_line_obj . search ( cr , uid , [ ( ' plan_id ' , ' = ' , journal . plan_id . id ) ] , context = context )
2008-08-24 14:45:43 +00:00
for i in res :
total_per_plan = 0
2010-10-11 11:25:50 +00:00
item = acct_anal_plan_line_obj . browse ( cr , uid , i , context = context )
temp_list = [ ' account1_ids ' , ' account2_ids ' , ' account3_ids ' , ' account4_ids ' , ' account5_ids ' , ' account6_ids ' ]
2008-08-24 14:45:43 +00:00
for l in temp_list :
if vals . has_key ( l ) :
for tempo in vals [ l ] :
2010-10-11 11:25:50 +00:00
if acct_anal_acct . search ( cr , uid , [ ( ' parent_id ' , ' child_of ' , [ item . root_analytic_id . id ] ) , ( ' id ' , ' = ' , tempo [ 2 ] [ ' analytic_account_id ' ] ) ] , context = context ) :
2008-08-24 14:45:43 +00:00
total_per_plan + = tempo [ 2 ] [ ' rate ' ]
if total_per_plan < item . min_required or total_per_plan > item . max_required :
2012-08-07 11:06:16 +00:00
raise osv . except_osv ( _ ( ' Error! ' ) , _ ( ' The total should be between %s and %s . ' ) % ( str ( item . min_required ) , str ( item . max_required ) ) )
2008-08-24 14:45:43 +00:00
2010-10-11 11:25:50 +00:00
return super ( account_analytic_plan_instance , self ) . create ( cr , uid , vals , context = context )
2008-08-24 14:45:43 +00:00
2010-07-02 14:09:05 +00:00
def write ( self , cr , uid , ids , vals , context = None , check = True , update_check = True ) :
2010-11-19 13:48:01 +00:00
if context is None :
context = { }
2010-10-11 11:25:50 +00:00
this = self . browse ( cr , uid , ids [ 0 ] , context = context )
invoice_line_obj = self . pool . get ( ' account.invoice.line ' )
2008-08-27 14:02:45 +00:00
if this . plan_id and not vals . has_key ( ' plan_id ' ) :
#this instance is a model, so we have to create a new plan instance instead of modifying it
#copy the existing model
2010-10-11 11:25:50 +00:00
temp_id = self . copy ( cr , uid , this . id , None , context = context )
2008-08-27 14:02:45 +00:00
#get the list of the invoice line that were linked to the model
2010-10-11 11:25:50 +00:00
lists = invoice_line_obj . search ( cr , uid , [ ( ' analytics_id ' , ' = ' , this . id ) ] , context = context )
2008-08-27 14:02:45 +00:00
#make them link to the copy
2010-10-11 11:25:50 +00:00
invoice_line_obj . write ( cr , uid , lists , { ' analytics_id ' : temp_id } , context = context )
2008-08-27 14:02:45 +00:00
#and finally modify the old model to be not a model anymore
2008-09-12 09:48:09 +00:00
vals [ ' plan_id ' ] = False
if not vals . has_key ( ' name ' ) :
2008-11-20 11:03:41 +00:00
vals [ ' name ' ] = this . name and ( str ( this . name ) + ' * ' ) or " * "
2008-09-12 09:48:09 +00:00
if not vals . has_key ( ' code ' ) :
2008-11-20 11:03:41 +00:00
vals [ ' code ' ] = this . code and ( str ( this . code ) + ' * ' ) or " * "
2010-10-11 11:25:50 +00:00
return super ( account_analytic_plan_instance , self ) . write ( cr , uid , ids , vals , context = context )
2008-08-27 14:02:45 +00:00
2008-08-24 14:45:43 +00:00
account_analytic_plan_instance ( )
class account_analytic_plan_instance_line ( osv . osv ) :
2010-10-11 11:25:50 +00:00
_name = " account.analytic.plan.instance.line "
_description = " Analytic Instance Line "
2012-09-26 07:22:39 +00:00
_rec_name = " analytic_account_id "
2010-10-11 11:25:50 +00:00
_columns = {
' plan_id ' : fields . many2one ( ' account.analytic.plan.instance ' , ' Plan Id ' ) ,
2010-12-10 19:27:51 +00:00
' analytic_account_id ' : fields . many2one ( ' account.analytic.account ' , ' Analytic Account ' , required = True , domain = [ ( ' type ' , ' <> ' , ' view ' ) ] ) ,
2010-10-11 11:25:50 +00:00
' rate ' : fields . float ( ' Rate ( % ) ' , required = True ) ,
2008-08-24 14:45:43 +00:00
}
2010-04-20 13:56:18 +00:00
_defaults = {
2010-07-02 14:09:05 +00:00
' rate ' : 100.0
2010-04-20 13:56:18 +00:00
}
2010-07-02 14:09:05 +00:00
def name_get ( self , cr , uid , ids , context = None ) :
2010-10-11 10:23:00 +00:00
if not ids :
2008-08-24 14:45:43 +00:00
return [ ]
2010-10-11 11:25:50 +00:00
reads = self . read ( cr , uid , ids , [ ' analytic_account_id ' ] , context = context )
2008-08-24 14:45:43 +00:00
res = [ ]
for record in reads :
res . append ( ( record [ ' id ' ] , record [ ' analytic_account_id ' ] ) )
return res
account_analytic_plan_instance_line ( )
class account_journal ( osv . osv ) :
2010-10-11 11:25:50 +00:00
_inherit = " account.journal "
_name = " account.journal "
2008-08-24 14:45:43 +00:00
_columns = {
2010-10-11 11:25:50 +00:00
' plan_id ' : fields . many2one ( ' account.analytic.plan ' , ' Analytic Plans ' ) ,
2008-08-24 14:45:43 +00:00
}
2010-08-17 11:13:35 +00:00
2008-08-24 14:45:43 +00:00
account_journal ( )
class account_invoice_line ( osv . osv ) :
2010-10-11 11:25:50 +00:00
_inherit = " account.invoice.line "
_name = " account.invoice.line "
2008-08-24 14:45:43 +00:00
_columns = {
2010-10-11 11:25:50 +00:00
' analytics_id ' : fields . many2one ( ' account.analytic.plan.instance ' , ' Analytic Distribution ' ) ,
2008-08-24 14:45:43 +00:00
}
def create ( self , cr , uid , vals , context = None ) :
2010-10-11 11:25:50 +00:00
if ' analytics_id ' in vals and isinstance ( vals [ ' analytics_id ' ] , tuple ) :
2008-08-24 14:45:43 +00:00
vals [ ' analytics_id ' ] = vals [ ' analytics_id ' ] [ 0 ]
2010-10-11 11:25:50 +00:00
return super ( account_invoice_line , self ) . create ( cr , uid , vals , context = context )
2008-08-24 14:45:43 +00:00
2009-11-26 13:54:00 +00:00
def move_line_get_item ( self , cr , uid , line , context = None ) :
2010-10-11 11:25:50 +00:00
res = super ( account_invoice_line , self ) . move_line_get_item ( cr , uid , line , context = context )
res [ ' analytics_id ' ] = line . analytics_id and line . analytics_id . id or False
2008-08-24 14:45:43 +00:00
return res
2013-02-26 08:30:29 +00:00
def product_id_change ( self , cr , uid , ids , product , uom_id , qty = 0 , name = ' ' , type = ' out_invoice ' , partner_id = False , fposition_id = False , price_unit = False , currency_id = False , context = None , company_id = None ) :
res_prod = super ( account_invoice_line , self ) . product_id_change ( cr , uid , ids , product , uom_id , qty , name , type , partner_id , fposition_id , price_unit , currency_id , context = context , company_id = company_id )
2010-10-11 11:25:50 +00:00
rec = self . pool . get ( ' account.analytic.default ' ) . account_get ( cr , uid , product , partner_id , uid , time . strftime ( ' % Y- % m- %d ' ) , context = context )
2008-09-03 13:47:19 +00:00
if rec and rec . analytics_id :
2010-10-11 11:25:50 +00:00
res_prod [ ' value ' ] . update ( { ' analytics_id ' : rec . analytics_id . id } )
2008-08-24 14:45:43 +00:00
return res_prod
2010-08-17 11:13:35 +00:00
2008-08-24 14:45:43 +00:00
account_invoice_line ( )
class account_move_line ( osv . osv ) :
2008-09-12 09:48:09 +00:00
2010-10-11 11:25:50 +00:00
_inherit = " account.move.line "
_name = " account.move.line "
2008-08-24 14:45:43 +00:00
_columns = {
2010-06-16 11:51:39 +00:00
' analytics_id ' : fields . many2one ( ' account.analytic.plan.instance ' , ' Analytic Distribution ' ) ,
2008-08-24 14:45:43 +00:00
}
2008-09-12 09:48:09 +00:00
2008-11-26 17:07:16 +00:00
def _default_get_move_form_hook ( self , cursor , user , data ) :
data = super ( account_move_line , self ) . _default_get_move_form_hook ( cursor , user , data )
if data . has_key ( ' analytics_id ' ) :
del ( data [ ' analytics_id ' ] )
return data
2010-07-02 14:09:05 +00:00
def create_analytic_lines ( self , cr , uid , ids , context = None ) :
2010-11-19 13:48:01 +00:00
if context is None :
context = { }
2010-10-11 11:25:50 +00:00
super ( account_move_line , self ) . create_analytic_lines ( cr , uid , ids , context = context )
2010-05-11 09:49:15 +00:00
analytic_line_obj = self . pool . get ( ' account.analytic.line ' )
2010-10-11 11:25:50 +00:00
for line in self . browse ( cr , uid , ids , context = context ) :
2008-09-12 09:48:09 +00:00
if line . analytics_id :
2010-03-12 10:14:25 +00:00
if not line . journal_id . analytic_journal_id :
2013-06-07 11:38:29 +00:00
raise osv . except_osv ( _ ( ' No Analytic Journal! ' ) , _ ( " You have to define an analytic journal on the ' %s ' journal. " ) % ( line . journal_id . name , ) )
2010-03-12 10:14:25 +00:00
2010-05-11 09:49:15 +00:00
toremove = analytic_line_obj . search ( cr , uid , [ ( ' move_id ' , ' = ' , line . id ) ] , context = context )
2008-09-12 09:48:09 +00:00
if toremove :
2010-05-11 09:49:15 +00:00
analytic_line_obj . unlink ( cr , uid , toremove , context = context )
2008-09-12 09:48:09 +00:00
for line2 in line . analytics_id . account_ids :
2008-11-24 17:00:14 +00:00
val = ( line . credit or 0.0 ) - ( line . debit or 0.0 )
2008-09-12 09:48:09 +00:00
amt = val * ( line2 . rate / 100 )
al_vals = {
' name ' : line . name ,
' date ' : line . date ,
' account_id ' : line2 . analytic_account_id . id ,
2008-11-26 17:07:16 +00:00
' unit_amount ' : line . quantity ,
' product_id ' : line . product_id and line . product_id . id or False ,
' product_uom_id ' : line . product_uom_id and line . product_uom_id . id or False ,
2008-09-12 09:48:09 +00:00
' amount ' : amt ,
' general_account_id ' : line . account_id . id ,
' move_id ' : line . id ,
2010-03-12 10:14:25 +00:00
' journal_id ' : line . journal_id . analytic_journal_id . id ,
2008-09-12 09:48:09 +00:00
' ref ' : line . ref ,
2011-05-06 05:21:05 +00:00
' percentage ' : line2 . rate
2008-09-12 09:48:09 +00:00
}
2010-10-05 07:00:21 +00:00
analytic_line_obj . create ( cr , uid , al_vals , context = context )
2008-11-24 17:00:14 +00:00
return True
2008-09-12 09:48:09 +00:00
2010-11-19 13:48:01 +00:00
def fields_view_get ( self , cr , uid , view_id = None , view_type = ' form ' , context = None , toolbar = False , submenu = False ) :
if context is None :
context = { }
2011-08-16 10:32:48 +00:00
result = super ( account_move_line , self ) . fields_view_get ( cr , uid , view_id , view_type , context , toolbar = toolbar , submenu = submenu )
2010-10-12 10:52:58 +00:00
return result
2008-08-24 14:45:43 +00:00
account_move_line ( )
class account_invoice ( osv . osv ) :
_name = " account.invoice "
2010-10-11 11:25:50 +00:00
_inherit = " account.invoice "
2008-08-24 14:45:43 +00:00
2010-07-02 14:09:05 +00:00
def line_get_convert ( self , cr , uid , x , part , date , context = None ) :
2010-10-11 11:25:50 +00:00
res = super ( account_invoice , self ) . line_get_convert ( cr , uid , x , part , date , context = context )
res [ ' analytics_id ' ] = x . get ( ' analytics_id ' , False )
2008-08-24 14:45:43 +00:00
return res
2011-12-15 09:07:30 +00:00
def _get_analytic_lines ( self , cr , uid , id , context = None ) :
2008-08-24 14:45:43 +00:00
inv = self . browse ( cr , uid , [ id ] ) [ 0 ]
cur_obj = self . pool . get ( ' res.currency ' )
2010-10-11 11:25:50 +00:00
invoice_line_obj = self . pool . get ( ' account.invoice.line ' )
acct_ins_obj = self . pool . get ( ' account.analytic.plan.instance ' )
2008-08-24 14:45:43 +00:00
company_currency = inv . company_id . currency_id . id
if inv . type in ( ' out_invoice ' , ' in_refund ' ) :
sign = 1
else :
sign = - 1
2011-12-15 09:07:30 +00:00
iml = invoice_line_obj . move_line_get ( cr , uid , inv . id , context = context )
2008-08-24 14:45:43 +00:00
for il in iml :
2010-10-04 04:46:07 +00:00
if il . get ( ' analytics_id ' , False ) :
2008-08-24 14:45:43 +00:00
if inv . type in ( ' in_invoice ' , ' in_refund ' ) :
ref = inv . reference
else :
ref = self . _convert_ref ( cr , uid , inv . number )
2012-01-03 12:33:39 +00:00
obj_move_line = acct_ins_obj . browse ( cr , uid , il [ ' analytics_id ' ] , context = context )
ctx = context . copy ( )
ctx . update ( { ' date ' : inv . date_invoice } )
amount_calc = cur_obj . compute ( cr , uid , inv . currency_id . id , company_currency , il [ ' price ' ] , context = ctx ) * sign
2010-07-06 14:58:03 +00:00
qty = il [ ' quantity ' ]
2010-10-04 04:46:07 +00:00
il [ ' analytic_lines ' ] = [ ]
2008-08-24 14:45:43 +00:00
for line2 in obj_move_line . account_ids :
2010-07-06 14:58:03 +00:00
amt = amount_calc * ( line2 . rate / 100 )
qtty = qty * ( line2 . rate / 100 )
al_vals = {
2008-08-24 14:45:43 +00:00
' name ' : il [ ' name ' ] ,
' date ' : inv [ ' date_invoice ' ] ,
2010-10-11 11:25:50 +00:00
' unit_amount ' : qtty ,
' product_id ' : il [ ' product_id ' ] ,
2008-08-24 14:45:43 +00:00
' account_id ' : line2 . analytic_account_id . id ,
' amount ' : amt ,
' product_uom_id ' : il [ ' uos_id ' ] ,
' general_account_id ' : il [ ' account_id ' ] ,
' journal_id ' : self . _get_journal_analytic ( cr , uid , inv . type ) ,
' ref ' : ref ,
}
2010-10-11 11:25:50 +00:00
il [ ' analytic_lines ' ] . append ( ( 0 , 0 , al_vals ) )
2008-08-24 14:45:43 +00:00
return iml
account_invoice ( )
class account_analytic_plan ( osv . osv ) :
_inherit = " account.analytic.plan "
_columns = {
' default_instance_id ' : fields . many2one ( ' account.analytic.plan.instance ' , ' Default Entries ' ) ,
}
account_analytic_plan ( )
2008-09-03 13:47:19 +00:00
class analytic_default ( osv . osv ) :
2010-10-11 11:25:50 +00:00
_inherit = " account.analytic.default "
2008-08-24 14:45:43 +00:00
_columns = {
2008-09-03 13:47:19 +00:00
' analytics_id ' : fields . many2one ( ' account.analytic.plan.instance ' , ' Analytic Distribution ' ) ,
}
2010-08-17 11:13:35 +00:00
2008-09-03 13:47:19 +00:00
analytic_default ( )
2008-08-24 14:45:43 +00:00
2009-12-16 11:38:59 +00:00
class sale_order_line ( osv . osv ) :
2010-10-11 11:25:50 +00:00
_inherit = " sale.order.line "
2009-12-16 11:38:59 +00:00
# Method overridden to set the analytic account by default on criterion match
2010-07-02 14:09:05 +00:00
def invoice_line_create ( self , cr , uid , ids , context = None ) :
2010-10-11 11:25:50 +00:00
create_ids = super ( sale_order_line , self ) . invoice_line_create ( cr , uid , ids , context = context )
inv_line_obj = self . pool . get ( ' account.invoice.line ' )
acct_anal_def_obj = self . pool . get ( ' account.analytic.default ' )
2009-12-16 11:38:59 +00:00
if ids :
2010-10-11 11:25:50 +00:00
sale_line = self . browse ( cr , uid , ids [ 0 ] , context = context )
for line in inv_line_obj . browse ( cr , uid , create_ids , context = context ) :
2010-07-06 14:58:03 +00:00
rec = acct_anal_def_obj . account_get ( cr , uid , line . product_id . id , sale_line . order_id . partner_id . id , uid , time . strftime ( ' % Y- % m- %d ' ) , context )
2009-12-16 11:38:59 +00:00
if rec :
2010-10-11 11:25:50 +00:00
inv_line_obj . write ( cr , uid , [ line . id ] , { ' analytics_id ' : rec . analytics_id . id } , context = context )
2009-12-16 11:38:59 +00:00
return create_ids
sale_order_line ( )
2010-10-12 10:52:58 +00:00
class account_bank_statement ( osv . osv ) :
_inherit = " account.bank.statement "
_name = " account.bank.statement "
2011-10-07 11:42:37 +00:00
2010-10-12 10:52:58 +00:00
def create_move_from_st_line ( self , cr , uid , st_line_id , company_currency_id , st_line_number , context = None ) :
account_move_line_pool = self . pool . get ( ' account.move.line ' )
account_bank_statement_line_pool = self . pool . get ( ' account.bank.statement.line ' )
2010-11-19 13:48:01 +00:00
st_line = account_bank_statement_line_pool . browse ( cr , uid , st_line_id , context = context )
2010-10-12 10:52:58 +00:00
result = super ( account_bank_statement , self ) . create_move_from_st_line ( cr , uid , st_line_id , company_currency_id , st_line_number , context = context )
move = st_line . move_ids and st_line . move_ids [ 0 ] or False
if move :
for line in move . line_id :
account_move_line_pool . write ( cr , uid , [ line . id ] , { ' analytics_id ' : st_line . analytics_id . id } , context = context )
return result
def button_confirm_bank ( self , cr , uid , ids , context = None ) :
super ( account_bank_statement , self ) . button_confirm_bank ( cr , uid , ids , context = context )
2010-12-13 06:43:09 +00:00
for st in self . browse ( cr , uid , ids , context = context ) :
2010-10-12 10:52:58 +00:00
for st_line in st . line_ids :
if st_line . analytics_id :
if not st . journal_id . analytic_journal_id :
2013-06-07 11:38:29 +00:00
raise osv . except_osv ( _ ( ' No Analytic Journal! ' ) , _ ( " You have to define an analytic journal on the ' %s ' journal. " ) % ( st . journal_id . name , ) )
2010-10-12 10:52:58 +00:00
if not st_line . amount :
continue
return True
2011-10-07 11:42:37 +00:00
2010-10-12 10:52:58 +00:00
account_bank_statement ( )
class account_bank_statement_line ( osv . osv ) :
_inherit = " account.bank.statement.line "
_name = " account.bank.statement.line "
_columns = {
' analytics_id ' : fields . many2one ( ' account.analytic.plan.instance ' , ' Analytic Distribution ' ) ,
}
account_bank_statement_line ( )
2011-08-16 10:32:48 +00:00
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: