a2f95a1a04a934c72f6c303544099285c7d87274
[odoo/odoo.git] / addons / account_analytic_default / account_analytic_default.py
1 # -*- encoding: utf-8 -*-
2
3 ##############################################################################
4 #
5 # Copyright (c) 2007 TINY SPRL. (http://tiny.be) All Rights Reserved.
6 #
7 # WARNING: This program as such is intended to be used by professional
8 # programmers who take the whole responsability of assessing all potential
9 # consequences resulting from its eventual inadequacies and bugs
10 # End users who are looking for a ready-to-use solution with commercial
11 # garantees and support are strongly adviced to contract a Free Software
12 # Service Company
13 #
14 # This program is Free Software; you can redistribute it and/or
15 # modify it under the terms of the GNU General Public License
16 # as published by the Free Software Foundation; either version 2
17 # of the License, or (at your option) any later version.
18 #
19 # This program is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 # GNU General Public License for more details.
23 #
24 # You should have received a copy of the GNU General Public License
25 # along with this program; if not, write to the Free Software
26 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
27 #
28 ##############################################################################
29
30 from osv import fields,osv
31 from osv import orm
32 import time
33
34 class account_analytic_default(osv.osv):
35     _name = 'account.analytic.default'
36     _description = 'Analytic Distributions'
37     _rec_name = 'analytic_id'
38     _order = 'sequence'
39     _columns = {
40         'sequence': fields.integer('Sequence'),
41         'analytic_id': fields.many2one('account.analytic.account', 'Analytic Account'),
42         'product_id': fields.many2one('product.product', 'Product', ondelete='cascade'),
43         'partner_id': fields.many2one('res.partner', 'Partner', ondelete='cascade'),
44         'user_id': fields.many2one('res.users', 'User', ondelete='cascade'),
45         'company_id': fields.many2one('res.company', 'Company', ondelete='cascade'),
46         'date_start': fields.date('Start Date'),
47         'date_stop': fields.date('End Date'),
48     }
49     def account_get(self, cr, uid, product_id=None, partner_id=None, user_id=None, date=None, context={}):
50         domain = []
51         if product_id:
52             domain += ['|',('product_id','=',product_id)]
53         domain += [('product_id','=',False)]
54         if partner_id:
55             domain += ['|',('partner_id','=',partner_id)]
56         domain += [('partner_id','=',False)]
57         if user_id:
58             domain += ['|',('user_id','=',uid)]
59         domain += [('user_id','=',False)]
60         if date:
61             domain += ['|',('date_start','<=',date),('date_start','=',False)]
62             domain += ['|',('date_stop','>=',date),('date_stop','=',False)]
63         print 'DOMAIN', domain
64         best_index = -1
65         res = False
66         for rec in self.browse(cr, uid, self.search(cr, uid, domain, context=context), context=context):
67             index = 0
68             if rec.product_id: index+=1
69             if rec.partner_id: index+=1
70             if rec.user_id: index+=1
71             if rec.date_start: index+=1
72             if rec.date_stop: index+=1
73             if index>best_index:
74                 res = rec
75                 best_index = index
76         return res
77 account_analytic_default()
78
79 class account_invoice_line(osv.osv):
80     _inherit = 'account.invoice.line'
81     _description = 'account invoice line'
82     def product_id_change(self, cr, uid, ids, product, uom, qty=0, name='', type='out_invoice', partner_id=False, price_unit=False, address_invoice_id=False, context={}):
83         res_prod = super(account_invoice_line,self).product_id_change(cr, uid, ids, product, uom, qty, name, type, partner_id, price_unit, address_invoice_id, context)
84         rec = self.pool.get('account.analytic.default').account_get(cr, uid, product, partner_id, uid, time.strftime('%Y-%m-%d'), context)
85         if rec:
86             res_prod['value'].update({'account_analytic_id':rec.analytic_id.id})
87         return res_prod
88 account_invoice_line()
89