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