1
##############################################################################
3
# Copyright (c) 2004-2006 TINY SPRL. (http://tiny.be) All Rights Reserved.
7
# WARNING: This program as such is intended to be used by professional
8
# programmers who take the whole responsability of assessing all potential
9
# consequences resulting from its eventual inadequacies and bugs
10
# End users who are looking for a ready-to-use solution with commercial
11
# garantees and support are strongly adviced to contract a Free Software
14
# This program is Free Software; you can redistribute it and/or
15
# modify it under the terms of the GNU General Public License
16
# as published by the Free Software Foundation; either version 2
17
# of the License, or (at your option) any later version.
19
# This program is distributed in the hope that it will be useful,
20
# but WITHOUT ANY WARRANTY; without even the implied warranty of
21
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22
# GNU General Public License for more details.
24
# You should have received a copy of the GNU General Public License
25
# along with this program; if not, write to the Free Software
26
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28
##############################################################################
33
from osv import fields
40
class account_analytic_account(osv.osv):
41
_name = 'account.analytic.account'
43
def _credit_calc(self, cr, uid, ids, name, arg, context={}):
45
for account in self.browse(cr, uid, ids):
46
node_balance = reduce(operator.add, [-line.amount for line in account.line_ids if line.amount<0], 0)
47
child_balance = reduce(operator.add, [child.credit for child in account.child_ids], 0)
48
res[account.id] = node_balance + child_balance
50
res[id] = round(res.get(id, 0.0),2)
53
def _debit_calc(self, cr, uid, ids, name, arg, context={}):
55
for account in self.browse(cr, uid, ids):
56
node_balance = reduce(operator.add, [line.amount for line in account.line_ids if line.amount>0], 0)
57
child_balance = reduce(operator.add, [child.debit for child in account.child_ids], 0)
58
res[account.id] = node_balance + child_balance
60
res[id] = round(res.get(id, 0.0),2)
63
def _balance_calc(self, cr, uid, ids, name, arg, context={}):
65
for account in self.browse(cr, uid, ids):
66
node_balance = reduce(operator.add, [line.amount for line in account.line_ids], 0)
67
child_balance = reduce(operator.add, [child.balance for child in account.child_ids], 0)
68
res[account.id] = node_balance + child_balance
70
res[id] = round(res.get(id, 0.0),2)
73
def _quantity_calc(self, cr, uid, ids, name, arg, context={}):
75
for account in self.browse(cr, uid, ids):
76
node_balance = reduce(operator.add, [line.unit_amount for line in account.line_ids], 0)
77
child_balance = reduce(operator.add, [child.quantity for child in account.child_ids], 0)
78
res[account.id] = node_balance + child_balance
80
res[id] = round(res.get(id, 0.0),2)
83
def name_get(self, cr, uid, ids, context={}):
86
reads = self.read(cr, uid, ids, ['name','parent_id'], context)
90
if record['parent_id']:
91
name = record['parent_id'][1]+' / '+name
92
res.append((record['id'], name))
95
def _complete_name_calc(self, cr, uid, ids, prop, unknow_none, unknow_dict):
96
res = self.name_get(cr, uid, ids)
101
'name' : fields.char('Account name', size=64, required=True),
102
'complete_name': fields.function(_complete_name_calc, method=True, type='char', string='Account Name'),
103
'code' : fields.char('Account code', size=8),
104
'active' : fields.boolean('Active'),
105
'type': fields.selection([('view','View'), ('normal','Normal')], 'type'),
106
'description' : fields.text('Description'),
107
'parent_id': fields.many2one('account.analytic.account', 'Parent Cost account', select=True),
108
'child_ids': fields.one2many('account.analytic.account', 'parent_id', 'Childs Accounts'),
109
'line_ids': fields.one2many('account.analytic.line', 'account_id', 'Analytic entries'),
110
'balance' : fields.function(_balance_calc, method=True, type='float', string='Balance'),
111
'debit' : fields.function(_debit_calc, method=True, type='float', string='Balance'),
112
'credit' : fields.function(_credit_calc, method=True, type='float', string='Balance'),
113
'quantity': fields.function(_quantity_calc, method=True, type='float', string='Quantity'),
114
'quantity_max': fields.float('Maximal quantity'),
115
'partner_id' : fields.many2one('res.partner', 'Associated partner'),
116
'contact_id' : fields.many2one('res.partner.address', 'Contact'),
117
'date_start': fields.date('Date Start'),
118
'date': fields.date('Date End'),
119
'stats_ids': fields.one2many('report.hr.timesheet.invoice.journal', 'account_id', string='Statistics', readonly=True),
123
'active' : lambda *a : True,
124
'type' : lambda *a : 'normal',
127
_order = 'parent_id desc,code'
129
def create(self, cr, uid, vals, ctx={}):
130
parent_id = vals.get('parent_id', 0)
131
if ('code' not in vals or not vals['code']) and not parent_id:
132
vals['code'] = self.pool.get('ir.sequence').get(cr, uid, 'account.analytic.account')
134
parent = self.read(cr, uid, [parent_id], ['parent_id'])[0]
135
childs = self.search(cr, uid, [('parent_id', '=', parent_id), ('active', '=', 1)]) + self.search(cr, uid, [('parent_id', '=', parent_id), ('active', '=', 0)])
136
vals['code'] = '%03d' % (len(childs) + 1,)
137
return super(account_analytic_account, self).create(cr, uid, vals, ctx)
140
def on_change_parent(self, cr, uid, id, parent_id):
142
return {'value': {'code': False, 'partner_id': ''}}
143
parent = self.read(cr, uid, [parent_id], ['partner_id'])[0]
144
childs = self.search(cr, uid, [('parent_id', '=', parent_id), ('active', '=', 1)]) + self.search(cr, uid, [('parent_id', '=', parent_id), ('active', '=', 0)])
145
numchild = len(childs)
146
if parent['partner_id']:
147
partner = parent['partner_id'][0]
150
return {'value' : {'code' : '%03d' % (numchild + 1,), 'partner_id' : partner}}
152
def name_search(self, cr, uid, name, args=[], operator='ilike', context={}):
153
codes = name.split('.')
157
current_code = codes.pop()
158
account = self.search(cr, uid, [('parent_id', '=', parent_code), ('code', '=', current_code)]+args)
160
parent_code = account[0]
162
account = self.search(cr, uid, [('name', 'ilike', '%%%s%%' % name)]+args)
164
return self.name_get(cr, uid, account)
166
account_analytic_account()
169
class account_analytic_journal(osv.osv):
170
_name = 'account.analytic.journal'
172
'name' : fields.char('Journal name', size=64, required=True),
173
'code' : fields.char('Journal code', size=8),
174
'active' : fields.boolean('Active'),
175
'type': fields.selection([('sale','Sale'), ('purchase','Purchase'), ('cash','Cash'), ('general','General'), ('situation','Situation')], 'Type', size=32, required=True, help="Gives the type of the analytic journal. When a document (eg: an invoice) needs to create analytic entries, Tiny ERP will look for a matching journal of the same type."),
176
'line_ids' : fields.one2many('account.analytic.line', 'journal_id', 'Lines'),
179
'active': lambda *a: True,
180
'type': lambda *a: 'general',
182
account_analytic_journal()
184
class account_analytic_line(osv.osv):
185
_name = 'account.analytic.line'
187
'name' : fields.char('Description', size=128, required=True),
188
'date' : fields.date('Date', required=True),
189
'amount' : fields.float('Amount', required=True),
190
'unit_amount' : fields.float('Quantity'),
191
'product_uom_id' : fields.many2one('product.uom', 'UoM'),
192
'product_id' : fields.many2one('product.product', 'Product'),
193
'account_id' : fields.many2one('account.analytic.account', 'Analytic Account', required=True, ondelete='cascade', select=True),
194
'general_account_id' : fields.many2one('account.account', 'General account', required=True, ondelete='cascade'),
195
'move_id' : fields.many2one('account.move.line', 'General entry', ondelete='cascade', select=True),
196
'journal_id' : fields.many2one('account.analytic.journal', 'Analytic journal', required=True, ondelete='cascade', select=True),
197
'code' : fields.char('Code', size=8),
198
'user_id' : fields.many2one('res.users', 'User',),
202
'date': lambda *a: time.strftime('%Y-%m-%d'),
206
def on_change_unit_amount(self, cr, uid, id, prod_id, unit_amount, unit=False, context={}):
207
if unit_amount and prod_id:
210
uom_id = self.pool.get('product.uom')
211
hunit = uom_id.browse(cr, uid, unit)
213
uom_id = self.pool.get('product.product')
214
prod = uom_id.browse(cr, uid, prod_id)
215
a = prod.product_tmpl_id.property_account_expense
217
a = prod.categ_id.property_account_expense_categ
218
return {'value' : {'amount' : -round(unit_amount * prod.standard_price * rate,2), 'general_account_id':a[0]}}
221
account_analytic_line()
223
class timesheet_invoice(osv.osv):
224
_name = "report.hr.timesheet.invoice.journal"
225
_description = "Analytic account costs and revenues"
228
'name': fields.date('Month', readonly=True),
229
'account_id':fields.many2one('account.analytic.account', 'Analytic Account', readonly=True, relate=True, select=True),
230
'journal_id': fields.many2one('account.analytic.journal', 'Journal', readonly=True),
231
'quantity': fields.float('Quantities', readonly=True),
232
'cost': fields.float('Credit', readonly=True),
233
'revenue': fields.float('Debit', readonly=True)
235
_order = 'name desc, account_id'
238
#create or replace view report_hr_timesheet_invoice_journal as (
241
# substring(l.create_date for 7)||'-01' as name,
242
# sum(greatest(-l.amount,0)) as cost,
243
# sum(greatest(l.amount,0)) as revenue,
244
# sum(l.unit_amount*u.factor) as quantity,
247
# from account_analytic_line l
248
# left join product_uom u on (u.id=l.product_uom_id)
250
# substring(l.create_date for 7),
255
create or replace view report_hr_timesheet_invoice_journal as (
258
substring(l.create_date for 7)||'-01' as name,
260
CASE WHEN -l.amount>0 THEN 0 ELSE -l.amount
264
CASE WHEN l.amount>0 THEN l.amount ELSE 0
267
sum(l.unit_amount*u.factor) as quantity,
270
from account_analytic_line l
271
left join product_uom u on (u.id=l.product_uom_id)
273
substring(l.create_date for 7),