[FIX] account, general ledger report: fixed the strip_name function
[odoo/odoo.git] / addons / account / account_analytic_line.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 import time
23
24 from osv import fields
25 from osv import osv
26 from tools.translate import _
27
28 class account_analytic_line(osv.osv):
29     _inherit = 'account.analytic.line'
30     _description = 'Analytic Line'
31     _columns = {
32         'product_uom_id': fields.many2one('product.uom', 'UoM'),
33         'product_id': fields.many2one('product.product', 'Product'),
34         'general_account_id': fields.many2one('account.account', 'General Account', required=True, ondelete='cascade'),
35         'move_id': fields.many2one('account.move.line', 'Move Line', ondelete='cascade', select=True),
36         'journal_id': fields.many2one('account.analytic.journal', 'Analytic Journal', required=True, ondelete='cascade', select=True),
37         'code': fields.char('Code', size=8),
38         'ref': fields.char('Ref.', size=64),
39         'currency_id': fields.related('move_id', 'currency_id', type='many2one', relation='res.currency', string='Account currency', store=True, help="The related account currency if not equal to the company one.", readonly=True),
40         'amount_currency': fields.related('move_id', 'amount_currency', type='float', string='Amount currency', store=True, help="The amount expressed in the related account currency if not equal to the company one.", readonly=True),
41     }
42
43     _defaults = {
44         'date': lambda *a: time.strftime('%Y-%m-%d'),
45         'company_id': lambda self,cr,uid,c: self.pool.get('res.company')._company_default_get(cr, uid, 'account.analytic.line', context=c),
46     }
47     _order = 'date desc'
48
49     def search(self, cr, uid, args, offset=0, limit=None, order=None, context=None, count=False):
50         if context is None:
51             context = {}
52         if context.get('from_date',False):
53             args.append(['date', '>=',context['from_date']])
54
55         if context.get('to_date',False):
56             args.append(['date','<=',context['to_date']])
57
58         return super(account_analytic_line, self).search(cr, uid, args, offset, limit,
59                 order, context=context, count=count)
60
61     def _check_company(self, cr, uid, ids):
62         lines = self.browse(cr, uid, ids)
63         for l in lines:
64             if l.move_id and not l.account_id.company_id.id == l.move_id.account_id.company_id.id:
65                 return False
66         return True
67
68     # Compute the cost based on the price type define into company
69     # property_valuation_price_type property
70     def on_change_unit_amount(self, cr, uid, id, prod_id, quantity, company_id,
71             unit=False, journal_id=False, context=None):
72         if context==None:
73             context={}
74         if not journal_id:
75             j_ids = self.pool.get('account.analytic.journal').search(cr, uid, [('type','=','purchase')])
76             journal_id = j_ids and j_ids[0] or False
77         if not journal_id or not prod_id:
78             return {}
79         product_obj = self.pool.get('product.product')
80         analytic_journal_obj =self.pool.get('account.analytic.journal')
81         product_price_type_obj = self.pool.get('product.price.type')
82         j_id = analytic_journal_obj.browse(cr, uid, journal_id, context=context)
83         prod = product_obj.browse(cr, uid, prod_id)
84         result = 0.0
85
86         if j_id.type <> 'sale':
87             a = prod.product_tmpl_id.property_account_expense.id
88             if not a:
89                 a = prod.categ_id.property_account_expense_categ.id
90             if not a:
91                 raise osv.except_osv(_('Error !'),
92                         _('There is no expense account defined ' \
93                                 'for this product: "%s" (id:%d)') % \
94                                 (prod.name, prod.id,))
95         else:
96             a = prod.product_tmpl_id.property_account_income.id
97             if not a:
98                 a = prod.categ_id.property_account_income_categ.id
99             if not a:
100                 raise osv.except_osv(_('Error !'),
101                         _('There is no income account defined ' \
102                                 'for this product: "%s" (id:%d)') % \
103                                 (prod.name, prod_id,))
104
105         flag = False
106         # Compute based on pricetype
107         product_price_type_ids = product_price_type_obj.search(cr, uid, [('field','=','standard_price')], context=context)
108         pricetype = product_price_type_obj.browse(cr, uid, product_price_type_ids, context)[0]
109         if journal_id:
110             journal = analytic_journal_obj.browse(cr, uid, journal_id)
111             if journal.type == 'sale':
112                 product_price_type_ids = product_price_type_obj.search(cr, uid, [('field','=','list_price')], context)
113                 if product_price_type_ids:
114                     pricetype = product_price_type_obj.browse(cr, uid, product_price_type_ids, context)[0]
115         # Take the company currency as the reference one
116         if pricetype.field == 'list_price':
117             flag = True
118         ctx = context.copy()
119         if unit:
120             # price_get() will respect a 'uom' in its context, in order
121             # to return a default price for those units
122             ctx['uom'] = unit
123         amount_unit = prod.price_get(pricetype.field, context=ctx)[prod.id]
124         prec = self.pool.get('decimal.precision').precision_get(cr, uid, 'Account')
125         amount = amount_unit * quantity or 1.0
126         result = round(amount, prec)
127         if not flag:
128             result *= -1
129
130         return {'value': {
131             'amount': result,
132             'general_account_id': a,
133             }
134         }
135
136     def view_header_get(self, cr, user, view_id, view_type, context):
137         if context.get('account_id', False):
138             # account_id in context may also be pointing to an account.account.id
139             cr.execute('select name from account_analytic_account where id=%s', (context['account_id'],))
140             res = cr.fetchone()
141             if res:
142                 res = _('Entries: ')+ (res[0] or '')
143             return res
144         return False
145
146 account_analytic_line()
147
148 class res_partner(osv.osv):
149     """ Inherits partner and adds contract information in the partner form """
150     _inherit = 'res.partner'
151
152     _columns = {
153         'contract_ids': fields.one2many('account.analytic.account', \
154                                                     'partner_id', 'Contracts', readonly=True),
155     }
156
157 res_partner()
158
159 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: