1 # -*- encoding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 ##############################################################################
23 from osv import fields,osv
27 class account_analytic_default(osv.osv):
28 _name = 'account.analytic.default'
29 _description = 'Analytic Distributions'
30 _rec_name = 'analytic_id'
33 'sequence': fields.integer('Sequence'),
34 'analytic_id': fields.many2one('account.analytic.account', 'Analytic Account'),
35 'product_id': fields.many2one('product.product', 'Product', ondelete='cascade'),
36 'partner_id': fields.many2one('res.partner', 'Partner', ondelete='cascade'),
37 'user_id': fields.many2one('res.users', 'User', ondelete='cascade'),
38 'company_id': fields.many2one('res.company', 'Company', ondelete='cascade'),
39 'date_start': fields.date('Start Date'),
40 'date_stop': fields.date('End Date'),
42 def account_get(self, cr, uid, product_id=None, partner_id=None, user_id=None, date=None, context={}):
45 domain += ['|',('product_id','=',product_id)]
46 domain += [('product_id','=',False)]
48 domain += ['|',('partner_id','=',partner_id)]
49 domain += [('partner_id','=',False)]
51 domain += ['|',('user_id','=',uid)]
52 domain += [('user_id','=',False)]
54 domain += ['|',('date_start','<=',date),('date_start','=',False)]
55 domain += ['|',('date_stop','>=',date),('date_stop','=',False)]
58 for rec in self.browse(cr, uid, self.search(cr, uid, domain, context=context), context=context):
60 if rec.product_id: index+=1
61 if rec.partner_id: index+=1
62 if rec.user_id: index+=1
63 if rec.date_start: index+=1
64 if rec.date_stop: index+=1
69 account_analytic_default()
71 class account_invoice_line(osv.osv):
72 _inherit = 'account.invoice.line'
73 _description = 'account invoice line'
75 def product_id_change(self, cr, uid, ids, product, uom, qty=0, name='', type='out_invoice', partner_id=False, fposition=False, price_unit=False, address_invoice_id=False, context={}):
76 res_prod = super(account_invoice_line,self).product_id_change(cr, uid, ids, product, uom, qty, name, type, partner_id, fposition, price_unit, address_invoice_id, context)
77 rec = self.pool.get('account.analytic.default').account_get(cr, uid, product, partner_id, uid, time.strftime('%Y-%m-%d'), context)
79 res_prod['value'].update({'account_analytic_id':rec.analytic_id.id})
81 res_prod['value'].update({'account_analytic_id':False})
83 account_invoice_line()
86 class stock_picking(osv.osv):
87 _inherit = "stock.picking"
89 def _get_account_analytic_invoice(self, cursor, user, picking, move_line):
90 partner_id = picking.address_id and picking.address_id.partner_id or False
91 rec = self.pool.get('account.analytic.default').account_get(cursor, user, move_line.product_id.id, partner_id and partner_id.id, user, time.strftime('%Y-%m-%d'), context={})
94 return rec.analytic_id.id
96 return super(stock_picking, self)._get_account_analytic_invoice(cursor,
97 user, picking, move_line)
101 class sale_order_line(osv.osv):
102 _inherit = 'sale.order.line'
104 # Method overridden to set the analytic account by default on criterion match
105 def invoice_line_create(self, cr, uid, ids, context={}):
106 create_ids = super(sale_order_line,self).invoice_line_create(cr, uid, ids, context)
107 sale_line_obj = self.browse(cr, uid, ids[0], context)
108 pool_inv_line = self.pool.get('account.invoice.line')
110 for line in pool_inv_line.browse(cr, uid, create_ids, context):
111 rec = self.pool.get('account.analytic.default').account_get(cr, uid, line.product_id.id, sale_line_obj.order_id.partner_id.id, uid, time.strftime('%Y-%m-%d'), context)
114 pool_inv_line.write(cr, uid, [line.id], {'account_analytic_id':rec.analytic_id.id}, context=context)
121 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: