1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
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.
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.
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/>.
20 ##############################################################################
24 from osv import fields
26 from tools.translate import _
29 class account_analytic_line(osv.osv):
30 _name = 'account.analytic.line'
31 _description = 'Analytic lines'
33 'name' : fields.char('Description', size=256, required=True),
34 'date' : fields.date('Date', required=True),
35 'amount' : fields.float('Amount', required=True, help='Calculated by multiplying the quantity and the price given in the Product\'s cost price.'),
36 'unit_amount' : fields.float('Quantity', help='Specifies the amount of quantity to count.'),
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('Reference', size=32),
48 'date': lambda *a: time.strftime('%Y-%m-%d'),
53 def search(self, cr, uid, args, offset=0, limit=None, order=None, context=None, count=False):
57 if context.get('from_date',False):
58 args.append(['date', '>=',context['from_date']])
60 if context.get('to_date',False):
61 args.append(['date','<=',context['to_date']])
63 return super(account_analytic_line, self).search(cr, uid, args, offset, limit,
64 order, context=context, count=count)
66 def _check_company(self, cr, uid, ids):
67 lines = self.browse(cr, uid, ids)
69 if l.move_id and not l.account_id.company_id.id == l.move_id.account_id.company_id.id:
73 # (_check_company, 'You can not create analytic line that is not in the same company than the account line', ['account_id'])
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:
82 prod = product_obj.browse(cr, uid, prod_id)
83 a = prod.product_tmpl_id.property_account_expense.id
85 a = prod.categ_id.property_account_expense_categ.id
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)
94 'amount': - round(amount, 2),
95 'general_account_id': a,
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'],))
105 res = _('Entries: ')+ (res[0] or '')
109 account_analytic_line()
112 class timesheet_invoice(osv.osv):
113 _name = "report.hr.timesheet.invoice.journal"
114 _description = "Analytic account costs and revenues"
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),
126 _order = 'name desc, account_id'
128 tools.drop_view_if_exists(cr, 'report_hr_timesheet_invoice_journal')
130 create or replace view report_hr_timesheet_invoice_journal as (
133 to_char(l.date, 'YYYY') as name,
134 to_char(l.date,'MM') as month,
136 CASE WHEN l.amount>0 THEN 0 ELSE l.amount
140 CASE WHEN l.amount>0 THEN l.amount ELSE 0
143 sum(l.unit_amount* COALESCE(u.factor, 1)) as quantity,
146 from account_analytic_line l
147 LEFT OUTER join product_uom u on (u.id=l.product_uom_id)
149 to_char(l.date, 'YYYY'),
150 to_char(l.date,'MM'),
156 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: