Merge from stable
[odoo/odoo.git] / addons / account_analytic_default / account_analytic_default.py
1 # -*- coding: utf-8 -*-
2 ###############################################################################
3 #    
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.     
19 #
20 ##############################################################################
21
22 from osv import fields,osv
23 from osv import orm
24 import time
25
26 class account_analytic_default(osv.osv):
27     _name = 'account.analytic.default'
28     _description = 'Analytic Distributions'
29     _rec_name = 'analytic_id'
30     _order = 'sequence'
31     _columns = {
32         'sequence': fields.integer('Sequence', help="Gives the sequence order when displaying a list of analytic distribution."),
33         'analytic_id': fields.many2one('account.analytic.account', 'Analytic Account'),
34         'product_id': fields.many2one('product.product', 'Product', ondelete='cascade'),
35         'partner_id': fields.many2one('res.partner', 'Partner', ondelete='cascade'),
36         'user_id': fields.many2one('res.users', 'User', ondelete='cascade'),
37         'company_id': fields.many2one('res.company', 'Company', ondelete='cascade'),
38         'date_start': fields.date('Start Date'),
39         'date_stop': fields.date('End Date'),
40     }
41     def account_get(self, cr, uid, product_id=None, partner_id=None, user_id=None, date=None, context={}):
42         domain = []
43         if product_id:
44             domain += ['|',('product_id','=',product_id)]
45         domain += [('product_id','=',False)]
46         if partner_id:
47             domain += ['|',('partner_id','=',partner_id)]
48         domain += [('partner_id','=',False)]
49         if user_id:
50             domain += ['|',('user_id','=',uid)]
51         domain += [('user_id','=',False)]
52         if date:
53             domain += ['|',('date_start','<=',date),('date_start','=',False)]
54             domain += ['|',('date_stop','>=',date),('date_stop','=',False)]
55         best_index = -1
56         res = False
57         for rec in self.browse(cr, uid, self.search(cr, uid, domain, context=context), context=context):
58             index = 0
59             if rec.product_id: index+=1
60             if rec.partner_id: index+=1
61             if rec.user_id: index+=1
62             if rec.date_start: index+=1
63             if rec.date_stop: index+=1
64             if index>best_index:
65                 res = rec
66                 best_index = index
67         return res
68 account_analytic_default()
69
70 class account_invoice_line(osv.osv):
71     _inherit = 'account.invoice.line'
72     _description = 'account invoice line'
73     
74     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={}):
75         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)
76         rec = self.pool.get('account.analytic.default').account_get(cr, uid, product, partner_id, uid, time.strftime('%Y-%m-%d'), context)
77         if rec:
78             res_prod['value'].update({'account_analytic_id':rec.analytic_id.id})
79         else:
80             res_prod['value'].update({'account_analytic_id':False})       
81         return res_prod
82 account_invoice_line()
83
84
85 class stock_picking(osv.osv):
86     _inherit = "stock.picking"
87     
88     def _get_account_analytic_invoice(self, cursor, user, picking, move_line):
89         partner_id = picking.address_id and picking.address_id.partner_id or False
90         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={})
91         
92         if rec:
93             return rec.analytic_id.id
94         
95         return super(stock_picking, self)._get_account_analytic_invoice(cursor,
96                 user, picking, move_line)
97         
98 stock_picking()
99
100 class sale_order_line(osv.osv):
101     _inherit = 'sale.order.line'
102     
103     # Method overridden to set the analytic account by default on criterion match
104     def invoice_line_create(self, cr, uid, ids, context={}):
105         create_ids = super(sale_order_line,self).invoice_line_create(cr, uid, ids, context)
106         sale_line_obj = self.browse(cr, uid, ids[0], context)
107         pool_inv_line = self.pool.get('account.invoice.line')
108         
109         for line in pool_inv_line.browse(cr, uid, create_ids, context):
110             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)
111             
112             if rec:
113                 pool_inv_line.write(cr, uid, [line.id], {'account_analytic_id':rec.analytic_id.id}, context=context)
114         return create_ids
115     
116 sale_order_line()    
117      
118
119
120 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: