1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2010 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 ##############################################################################
23 import decimal_precision as dp
25 from osv import fields
27 from tools.translate import _
29 from tools import config
31 class account_analytic_line(osv.osv):
32 _name = 'account.analytic.line'
33 _description = 'Analytic lines'
35 def _amount_currency(self, cr, uid, ids, field_name, arg, context={}):
37 for rec in self.browse(cr, uid, ids, context):
38 cmp_cur_id=rec.company_id.currency_id.id
39 aa_cur_id=rec.account_id.currency_id.id
40 # Always provide the amount in currency
41 if cmp_cur_id != aa_cur_id:
42 cur_obj = self.pool.get('res.currency')
44 if rec.date and rec.amount:
45 ctx['date'] = rec.date
46 result[rec.id] = cur_obj.compute(cr, uid, rec.company_id.currency_id.id,
47 rec.account_id.currency_id.id, rec.amount,
50 result[rec.id]=rec.amount
53 def _get_account_currency(self, cr, uid, ids, field_name, arg, context={}):
55 for rec in self.browse(cr, uid, ids, context):
56 # Always provide second currency
57 result[rec.id] = (rec.account_id.currency_id.id,rec.account_id.currency_id.code)
60 def _get_account_line(self, cr, uid, ids, context={}):
62 for acc in self.pool.get('account.analytic.account').browse(cr, uid, ids):
63 aac_ids[acc.id] = True
66 aal_ids = self.pool.get('account.analytic.line').search(cr, uid, [('account_id','in',aac_ids.keys())], context=context)
70 'name' : fields.char('Description', size=256, required=True),
71 'date' : fields.date('Date', required=True),
72 'amount' : fields.float('Amount', required=True, help='Calculated by multiplying the quantity and the price given in the Product\'s cost price.'),
73 'unit_amount' : fields.float('Quantity', help='Specifies the amount of quantity to count.'),
74 'product_uom_id' : fields.many2one('product.uom', 'UoM'),
75 'product_id' : fields.many2one('product.product', 'Product'),
76 'account_id' : fields.many2one('account.analytic.account', 'Analytic Account', required=True, ondelete='cascade', select=True),
77 'general_account_id' : fields.many2one('account.account', 'General Account', required=True, ondelete='cascade'),
78 'move_id' : fields.many2one('account.move.line', 'Move Line', ondelete='cascade', select=True),
79 'journal_id' : fields.many2one('account.analytic.journal', 'Analytic Journal', required=True, ondelete='cascade', select=True),
80 'code' : fields.char('Code', size=8),
81 'user_id' : fields.many2one('res.users', 'User',),
82 'currency_id': fields.function(_get_account_currency, method=True, type='many2one', relation='res.currency', string='Account currency',
84 'account.analytic.account': (_get_account_line, ['company_id'], 50),
85 'account.analytic.line': (lambda self,cr,uid,ids,c={}: ids, ['amount','unit_amount'],10),
87 help="The related account currency if not equal to the company one."),
88 'company_id': fields.many2one('res.company','Company',required=True),
89 'amount_currency': fields.function(_amount_currency, method=True, digits_compute= dp.get_precision('Account'), string='Amount currency',
91 'account.analytic.account': (_get_account_line, ['company_id'], 50),
92 'account.analytic.line': (lambda self,cr,uid,ids,c={}: ids, ['amount','unit_amount'],10),
94 help="The amount expressed in the related account currency if not equal to the company one."),
95 'ref': fields.char('Ref.', size=64),
98 'date': lambda *a: time.strftime('%Y-%m-%d'),
99 'company_id': lambda self,cr,uid,c: self.pool.get('res.company')._company_default_get(cr, uid, 'account.analytic.line', c),
103 def search(self, cr, uid, args, offset=0, limit=None, order=None, context=None, count=False):
107 if context.get('from_date',False):
108 args.append(['date', '>=',context['from_date']])
110 if context.get('to_date',False):
111 args.append(['date','<=',context['to_date']])
113 return super(account_analytic_line, self).search(cr, uid, args, offset, limit,
114 order, context=context, count=count)
116 def _check_company(self, cr, uid, ids):
117 lines = self.browse(cr, uid, ids)
119 if l.move_id and not l.account_id.company_id.id == l.move_id.account_id.company_id.id:
123 # (_check_company, 'You can not create analytic line that is not in the same company than the account line', ['account_id'])
126 # Compute the cost based on the price type define into company
127 # property_valuation_price_type property
128 def on_change_unit_amount(self, cr, uid, id, prod_id, unit_amount,company_id,
129 unit=False, context=None):
132 uom_obj = self.pool.get('product.uom')
133 product_obj = self.pool.get('product.product')
134 company_obj=self.pool.get('res.company')
136 prod = product_obj.browse(cr, uid, prod_id)
137 a = prod.product_tmpl_id.property_account_expense.id
139 a = prod.categ_id.property_account_expense_categ.id
141 raise osv.except_osv(_('Error !'),
142 _('There is no expense account defined ' \
143 'for this product: "%s" (id:%d)') % \
144 (prod.name, prod.id,))
146 company_id=company_obj._company_default_get(cr, uid, 'account.analytic.line', context)
148 # Compute based on pricetype
149 pricetype=self.pool.get('product.price.type').browse(cr,uid,company_obj.browse(cr,uid,company_id).property_valuation_price_type.id)
150 # Take the company currency as the reference one
151 context['currency_id']=company_obj.browse(cr,uid,company_id).currency_id.id
152 amount_unit=prod.price_get(pricetype.field, context)[prod.id]
153 amount=amount_unit*unit_amount or 1.0
155 'amount': - round(amount, 2),
156 'general_account_id': a,
160 def view_header_get(self, cr, user, view_id, view_type, context):
161 if context.get('account_id', False):
162 # account_id in context may also be pointing to an account.account.id
163 cr.execute('select name from account_analytic_account where id=%s', (context['account_id'],))
166 res = _('Entries: ')+ (res[0] or '')
170 account_analytic_line()
173 class timesheet_invoice(osv.osv):
174 _name = "report.hr.timesheet.invoice.journal"
175 _description = "Analytic account costs and revenues"
178 'name': fields.char('Year',size=64,required=False, readonly=True),
179 'account_id':fields.many2one('account.analytic.account', 'Analytic Account', readonly=True, select=True),
180 'journal_id': fields.many2one('account.analytic.journal', 'Journal', readonly=True),
181 'quantity': fields.float('Quantities', readonly=True),
182 'cost': fields.float('Credit', readonly=True),
183 'revenue': fields.float('Debit', readonly=True),
184 'month':fields.selection([('01','January'), ('02','February'), ('03','March'), ('04','April'), ('05','May'), ('06','June'),
185 ('07','July'), ('08','August'), ('09','September'), ('10','October'), ('11','November'), ('12','December')],'Month',readonly=True),
187 _order = 'name desc, account_id'
189 tools.drop_view_if_exists(cr, 'report_hr_timesheet_invoice_journal')
191 create or replace view report_hr_timesheet_invoice_journal as (
194 to_char(l.date, 'YYYY') as name,
195 to_char(l.date,'MM') as month,
197 CASE WHEN l.amount>0 THEN 0 ELSE l.amount
201 CASE WHEN l.amount>0 THEN l.amount ELSE 0
204 sum(l.unit_amount* COALESCE(u.factor, 1)) as quantity,
207 from account_analytic_line l
208 LEFT OUTER join product_uom u on (u.id=l.product_uom_id)
210 to_char(l.date, 'YYYY'),
211 to_char(l.date,'MM'),
217 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: