[IMP]: Improved sale journal report views. Removed This Month/All Months menus.
[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-2009 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 import tools
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     
52     
53     def search(self, cr, uid, args, offset=0, limit=None, order=None, context=None, count=False):
54         if context is None:
55             context = {}
56
57         if context.get('from_date',False):
58             args.append(['date', '>=',context['from_date']])
59             
60         if context.get('to_date',False):
61             args.append(['date','<=',context['to_date']])
62             
63         return super(account_analytic_line, self).search(cr, uid, args, offset, limit,
64                 order, context=context, count=count)
65         
66     def _check_company(self, cr, uid, ids):
67         lines = self.browse(cr, uid, ids)
68         for l in lines:
69             if l.move_id and not l.account_id.company_id.id == l.move_id.account_id.company_id.id:
70                 return False
71         return True
72     _constraints = [
73 #        (_check_company, 'You can not create analytic line that is not in the same company than the account line', ['account_id'])
74     ]
75     
76     def on_change_unit_amount(self, cr, uid, id, prod_id, unit_amount,
77             unit=False, context=None):
78         uom_obj = self.pool.get('product.uom')
79         product_obj = self.pool.get('product.product')
80 #        if unit_amount and prod_id:
81         if  prod_id:
82             prod = product_obj.browse(cr, uid, prod_id)
83             a = prod.product_tmpl_id.property_account_expense.id
84             if not a:
85                 a = prod.categ_id.property_account_expense_categ.id
86             if not a:
87                 raise osv.except_osv(_('Error !'),
88                         _('There is no expense account defined ' \
89                                 'for this product: "%s" (id:%d)') % \
90                                 (prod.name, prod.id,))
91             amount = unit_amount * uom_obj._compute_price(cr, uid,
92                     prod.uom_id.id, prod.standard_price, unit)
93             return {'value': {
94                 'amount': - round(amount, 2),
95                 'general_account_id': a,
96                 }}
97         return {}
98
99     def view_header_get(self, cr, user, view_id, view_type, context):
100         if context.get('account_id', False):
101             # account_id in context may also be pointing to an account.account.id
102             cr.execute('select name from account_analytic_account where id=%s', (context['account_id'],))
103             res = cr.fetchone()
104             if res:
105                 res = _('Entries: ')+ (res[0] or '')
106             return res
107         return False
108
109 account_analytic_line()
110
111
112 class timesheet_invoice(osv.osv):
113     _name = "report.hr.timesheet.invoice.journal"
114     _description = "Analytic account costs and revenues"
115     _auto = False
116     _columns = {
117         'name': fields.char('Year',size=64,required=False, readonly=True),
118         'account_id':fields.many2one('account.analytic.account', 'Analytic Account', readonly=True, select=True),
119         'journal_id': fields.many2one('account.analytic.journal', 'Journal', readonly=True),
120         'quantity': fields.float('Quantities', readonly=True),
121         'cost': fields.float('Credit', readonly=True),
122         'revenue': fields.float('Debit', readonly=True),
123         'month':fields.selection([('01','January'), ('02','February'), ('03','March'), ('04','April'), ('05','May'), ('06','June'),
124                                   ('07','July'), ('08','August'), ('09','September'), ('10','October'), ('11','November'), ('12','December')],'Month',readonly=True),
125     }
126     _order = 'name desc, account_id'
127     def init(self, cr):
128         tools.drop_view_if_exists(cr, 'report_hr_timesheet_invoice_journal')
129         cr.execute("""
130         create or replace view report_hr_timesheet_invoice_journal as (
131             select
132                 min(l.id) as id,
133                 to_char(l.date, 'YYYY') as name,
134                 to_char(l.date,'MM') as month,
135                 sum(
136                     CASE WHEN l.amount>0 THEN 0 ELSE l.amount
137                     END
138                 ) as cost,
139                 sum(
140                     CASE WHEN l.amount>0 THEN l.amount ELSE 0
141                     END
142                 ) as revenue,
143                 sum(l.unit_amount* COALESCE(u.factor, 1)) as quantity,
144                 journal_id,
145                 account_id
146             from account_analytic_line l
147                 LEFT OUTER join product_uom u on (u.id=l.product_uom_id)
148             group by
149                 to_char(l.date, 'YYYY'),
150                 to_char(l.date,'MM'),
151                 journal_id,
152                 account_id
153         )""")
154 timesheet_invoice()
155
156 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
157