improve
[odoo/odoo.git] / addons / account / account_analytic_line.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution   
5 #    Copyright (C) 2004-2008 Tiny SPRL (<http://tiny.be>). All Rights Reserved
6 #    $Id$
7 #
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.
12 #
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.
17 #
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/>.
20 #
21 ##############################################################################
22
23 import time
24
25 from osv import fields
26 from osv import osv
27 from tools.translate import _
28
29 class account_analytic_line(osv.osv):
30     _name = 'account.analytic.line'
31     _description = 'Analytic lines'
32     _columns = {
33         'name' : fields.char('Description', size=256, required=True),
34         'date' : fields.date('Date', required=True),
35         'amount' : fields.float('Amount', required=True),
36         'unit_amount' : fields.float('Quantity'),
37         'product_uom_id' : fields.many2one('product.uom', 'UoM'),
38         'product_id' : fields.many2one('product.product', 'Product'),
39         'account_id' : fields.many2one('account.analytic.account', 'Analytic Account', required=True, ondelete='cascade', select=True),
40         'general_account_id' : fields.many2one('account.account', 'General Account', required=True, ondelete='cascade'),
41         'move_id' : fields.many2one('account.move.line', 'Move Line', ondelete='cascade', select=True),
42         'journal_id' : fields.many2one('account.analytic.journal', 'Analytic Journal', required=True, ondelete='cascade', select=True),
43         'code' : fields.char('Code', size=8),
44         'user_id' : fields.many2one('res.users', 'User',),
45         'ref': fields.char('Ref.', size=32),
46     }
47     _defaults = {
48         'date': lambda *a: time.strftime('%Y-%m-%d'),
49     }
50     _order = 'date'
51     def _check_company(self, cr, uid, ids):
52         lines = self.browse(cr, uid, ids)
53         for l in lines:
54             if l.move_id and not l.account_id.company_id.id == l.move_id.account_id.company_id.id:
55                 return False
56         return True
57     _constraints = [
58         (_check_company, 'You can not create analytic line that is not in the same company than the account line', ['account_id'])
59     ]
60     
61     def on_change_unit_amount(self, cr, uid, id, prod_id, unit_amount,
62             unit=False, context=None):
63         uom_obj = self.pool.get('product.uom')
64         product_obj = self.pool.get('product.product')
65         if unit_amount and prod_id:
66             prod = product_obj.browse(cr, uid, prod_id)
67             a = prod.product_tmpl_id.property_account_expense.id
68             if not a:
69                 a = prod.categ_id.property_account_expense_categ.id
70             if not a:
71                 raise osv.except_osv(_('Error !'),
72                         _('There is no expense account define ' \
73                                 'for this product: "%s" (id:%d)') % \
74                                 (prod.name, prod.id,))
75             amount = unit_amount * uom_obj._compute_price(cr, uid,
76                     prod.uom_id.id, prod.standard_price, unit)
77             return {'value': {
78                 'amount': - round(amount, 2),
79                 'general_account_id': a,
80                 }}
81         return {}
82
83     def view_header_get(self, cr, user, view_id, view_type, context):
84         if context.get('account_id', False):
85             cr.execute('select name from account_analytic_account where id=%d', (context['account_id'],))
86             res = cr.fetchone()
87             res = _('Entries: ')+ (res[0] or '')
88             return res
89         return False
90
91 account_analytic_line()
92
93
94 class timesheet_invoice(osv.osv):
95     _name = "report.hr.timesheet.invoice.journal"
96     _description = "Analytic account costs and revenues"
97     _auto = False
98     _columns = {
99         'name': fields.date('Month', readonly=True),
100         'account_id':fields.many2one('account.analytic.account', 'Analytic Account', readonly=True, select=True),
101         'journal_id': fields.many2one('account.analytic.journal', 'Journal', readonly=True),
102         'quantity': fields.float('Quantities', readonly=True),
103         'cost': fields.float('Credit', readonly=True),
104         'revenue': fields.float('Debit', readonly=True)
105     }
106     _order = 'name desc, account_id'
107     def init(self, cr):
108         cr.execute("""
109         create or replace view report_hr_timesheet_invoice_journal as (
110             select
111                 min(l.id) as id,
112                 date_trunc('month', l.date)::date as name,
113                 sum(
114                     CASE WHEN l.amount>0 THEN 0 ELSE l.amount
115                     END
116                 ) as cost,
117                 sum(
118                     CASE WHEN l.amount>0 THEN l.amount ELSE 0
119                     END
120                 ) as revenue,
121                 sum(l.unit_amount* COALESCE(u.factor, 1)) as quantity,
122                 journal_id,
123                 account_id
124             from account_analytic_line l
125                 LEFT OUTER join product_uom u on (u.id=l.product_uom_id)
126             group by
127                 date_trunc('month', l.date),
128                 journal_id,
129                 account_id
130         )""")
131 timesheet_invoice()
132
133 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
134