1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # Copyright (c) 2004 TINY SPRL. (http://tiny.be) All Rights Reserved.
5 # Fabien Pinckaers <fp@tiny.Be>
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 ##############################################################################
30 from osv import osv, fields
32 class account_analytic_account(osv.osv):
33 _name = "account.analytic.account"
34 _inherit = "account.analytic.account"
36 def _ca_invoiced_calc(self, cr, uid, ids, name, arg, context={}):
37 ids2 = self.search(cr, uid, [('parent_id', 'child_of', ids)])
38 acc_set = ",".join(map(str, ids2))
39 cr.execute("select account_analytic_line.account_id, sum(amount) from account_analytic_line join account_analytic_journal on account_analytic_line.journal_id = account_analytic_journal.id where account_analytic_line.account_id IN (%s) and account_analytic_journal.type = 'sale' group by account_analytic_line.account_id" % acc_set)
41 for account_id, sum in cr.fetchall():
42 res[account_id] = round(sum,2)
44 res[id] = round(res.get(id, 0.0),2)
47 def _ca_to_invoice_calc(self, cr, uid, ids, name, arg, context={}):
48 ids2 = self.search(cr, uid, [('parent_id', 'child_of', ids)])
49 # Amount uninvoiced hours to invoice at sale price
50 acc_set = ",".join(map(str, ids2))
51 cr.execute("""SELECT account_analytic_account.id,(sum (product_template.list_price * account_analytic_line.unit_amount)
52 - sum(product_template.list_price * account_analytic_line.unit_amount * (hr_timesheet_invoice_factor.factor/100)))
54 FROM product_template join product_product on product_template.id = product_product.product_tmpl_id
55 JOIN account_analytic_line on account_analytic_line.product_id = product_product.id
56 JOIN account_analytic_journal on account_analytic_line.journal_id = account_analytic_journal.id
57 JOIN account_analytic_account on account_analytic_account.id = account_analytic_line.account_id
58 JOIN hr_timesheet_invoice_factor on hr_timesheet_invoice_factor.id = account_analytic_account.to_invoice
59 WHERE account_analytic_account.id IN (%s)
60 AND account_analytic_journal.type='general'
61 AND account_analytic_line.invoice_id is null
62 GROUP BY account_analytic_account.id;"""%acc_set)
64 for account_id, sum in cr.fetchall():
65 res[account_id] = round(sum,2)
67 # Expense amount and purchase invoice
68 acc_set = ",".join(map(str, ids2))
69 cr.execute ("select account_analytic_line.account_id,sum(amount) from account_analytic_line join account_analytic_journal on account_analytic_line.journal_id = account_analytic_journal.id where account_analytic_line.account_id IN (%s) and account_analytic_journal.type = 'purchase' GROUP BY account_analytic_line.account_id;"%acc_set)
71 for account_id, sum in cr.fetchall():
72 res2[account_id] = round(sum,2)
73 # sum both result on account_id
75 res[id] = round(res.get(id, 0.0),2) + round(res2.get(id, 0.0),2)
78 def _hours_qtt_non_invoiced_calc (self, cr, uid, ids, name, arg, context={}):
79 ids2 = self.search(cr, uid, [('parent_id', 'child_of', ids)])
80 acc_set = ",".join(map(str, ids2))
81 cr.execute("select account_analytic_line.account_id,sum(unit_amount) from account_analytic_line join account_analytic_journal on account_analytic_line.journal_id = account_analytic_journal.id where account_analytic_line.account_id IN (%s) and account_analytic_journal.type='general' and invoice_id is null GROUP BY account_analytic_line.account_id;"%acc_set)
83 for account_id, sum in cr.fetchall():
84 res[account_id] = round(sum,2)
86 res[id] = round(res.get(id, 0.0),2)
89 def _hours_quantity_calc(self, cr, uid, ids, name, arg, context={}):
90 ids2 = self.search(cr, uid, [('parent_id', 'child_of', ids)])
91 acc_set = ",".join(map(str, ids2))
92 cr.execute("select account_analytic_line.account_id,sum(unit_amount) from account_analytic_line join account_analytic_journal on account_analytic_line.journal_id = account_analytic_journal.id where account_analytic_line.account_id IN (%s) and account_analytic_journal.type='general' GROUP BY account_analytic_line.account_id"%acc_set)
94 for account_id, sum in cr.fetchall():
95 res[account_id] = round(sum,2)
97 res[id] = round(res.get(id, 0.0),2)
100 def _total_cost_calc(self, cr, uid, ids, name, arg, context={}):
101 ids2 = self.search(cr, uid, [('parent_id', 'child_of', ids)])
102 acc_set = ",".join(map(str, ids2))
104 account_analytic_line.account_id,sum(amount)
106 account_analytic_line join account_analytic_journal on account_analytic_line.journal_id = account_analytic_journal.id
108 account_analytic_line.account_id IN (%s) and amount<0
110 account_analytic_line.account_id"""%acc_set)
112 for account_id, sum in cr.fetchall():
113 res[account_id] = round(sum,2)
115 res[id] = round(res.get(id, 0.0),2)
118 def _ca_theorical_calc(self, cr, uid, ids, name, arg, context={}):
119 ids2 = self.search(cr, uid, [('parent_id', 'child_of', ids)])
120 acc_set = ",".join(map(str, ids2))
121 # First part with expense and purchase
123 account_analytic_line.account_id,sum(amount)
125 account_analytic_line join account_analytic_journal on account_analytic_line.journal_id = account_analytic_journal.id
127 account_analytic_line.account_id IN (%s) and account_analytic_journal.type = 'purchase'
129 account_analytic_line.account_id"""%acc_set)
131 for account_id, sum in cr.fetchall():
132 res[account_id] = round(sum,2)
133 # Second part with timesheet (with invoice factor)
134 acc_set = ",".join(map(str, ids2))
137 account_analytic_line.account_id as account_id,sum((account_analytic_line.unit_amount * pt.list_price)-(account_analytic_line.unit_amount * pt.list_price*hr.factor)) as somme
139 account_analytic_line join account_analytic_journal on account_analytic_line.journal_id = account_analytic_journal.id
140 join product_product pp on (account_analytic_line.product_id = pp.id)
141 join product_template pt on (pp.product_tmpl_id = pt.id)
142 join account_analytic_account a on (a.id=account_analytic_line.account_id)
143 join hr_timesheet_invoice_factor hr on (hr.id=a.to_invoice)
145 account_analytic_line.account_id IN (%s) and account_analytic_journal.type='general' and a.to_invoice IS NOT NULL
147 account_analytic_line.account_id"""%acc_set)
149 for account_id, sum in cr.fetchall():
150 res2[account_id] = round(sum,2)
151 # sum both result on account_id
153 res[id] = round(res.get(id, 0.0),2) + round(res2.get(id, 0.0),2)
156 def _last_worked_date_calc (self, cr, uid, ids, name, arg, context={}):
157 ids2 = self.search(cr, uid, [('parent_id', 'child_of', ids)])
158 acc_set = ",".join(map(str, ids2))
159 cr.execute("select account_analytic_line.account_id, max(date) from account_analytic_line where account_id IN (%s) and invoice_id is null GROUP BY account_analytic_line.account_id;"%acc_set)
161 for account_id, sum in cr.fetchall():
162 res[account_id] = sum
164 res[id] = res.get(id, '')
167 def _last_invoice_date_calc (self, cr, uid, ids, name, arg, context={}):
168 ids2 = self.search(cr, uid, [('parent_id', 'child_of', ids)])
169 acc_set = ",".join(map(str, ids2))
170 cr.execute ("select account_analytic_line.account_id,date(max(account_invoice.date_invoice)) from account_analytic_line join account_invoice on account_analytic_line.invoice_id = account_invoice.id where account_analytic_line.account_id IN (%s) and account_analytic_line.invoice_id is not null GROUP BY account_analytic_line.account_id"%acc_set)
172 for account_id, sum in cr.fetchall():
173 res[account_id] = sum
175 res[id] = res.get(id, '')
178 def _last_worked_invoiced_date_calc (self, cr, uid, ids, name, arg, context={}):
179 ids2 = self.search(cr, uid, [('parent_id', 'child_of', ids)])
180 acc_set = ",".join(map(str, ids2))
181 cr.execute("select account_analytic_line.account_id, max(date) from account_analytic_line where account_id IN (%s) and invoice_id is not null GROUP BY account_analytic_line.account_id;"%acc_set)
183 for account_id, sum in cr.fetchall():
184 res[account_id] = sum
186 res[id] = res.get(id, '')
189 def _remaining_hours_calc(self, cr, uid, ids, name, arg, context={}):
191 for account in self.browse(cr, uid, ids):
192 if account.quantity_max <> 0:
193 res[account.id] = account.quantity_max - account.hours_quantity
197 res[id] = round(res.get(id, 0.0),2)
199 def _hours_qtt_invoiced_calc(self, cr, uid, ids, name, arg, context={}):
201 for account in self.browse(cr, uid, ids):
202 res[account.id] = account.hours_quantity - account.hours_qtt_non_invoiced
203 if res[account.id] < 0:
206 res[id] = round(res.get(id, 0.0),2)
208 def _revenue_per_hour_calc(self, cr, uid, ids, name, arg, context={}):
210 for account in self.browse(cr, uid, ids):
211 if account.hours_qtt_invoiced == 0:
214 res[account.id] = account.ca_invoiced / account.hours_qtt_invoiced
216 res[id] = round(res.get(id, 0.0),2)
218 def _real_margin_rate_calc(self, cr, uid, ids, name, arg, context={}):
220 for account in self.browse(cr, uid, ids):
221 if account.ca_invoiced == 0:
224 res[account.id] = (account.real_margin / account.ca_invoiced) * 100
226 res[id] = round(res.get(id, 0.0),2)
228 def _remaining_ca_calc(self, cr, uid, ids, name, arg, context={}):
230 for account in self.browse(cr, uid, ids):
231 if account.amount_max <> 0:
232 res[account.id] = account.amount_max - account.ca_invoiced
236 res[id] = round(res.get(id, 0.0),2)
238 def _real_margin_calc(self, cr, uid, ids, name, arg, context={}):
240 for account in self.browse(cr, uid, ids):
241 res[account.id] = account.ca_invoiced + account.total_cost
243 res[id] = round(res.get(id, 0.0),2)
245 def _theorical_margin_calc(self, cr, uid, ids, name, arg, context={}):
247 for account in self.browse(cr, uid, ids):
248 res[account.id] = account.ca_theorical + account.total_cost
250 res[id] = round(res.get(id, 0.0),2)
254 'ca_invoiced': fields.function(_ca_invoiced_calc, method=True, type='float', string='Invoiced amount'),
255 'total_cost': fields.function(_total_cost_calc, method=True, type='float', string='Total cost'),
256 'ca_to_invoice': fields.function(_ca_to_invoice_calc, method=True, type='float', string='Uninvoiced amount'),
257 'ca_theorical': fields.function(_ca_theorical_calc, method=True, type='float', string='Theorical revenue'),
258 'hours_quantity': fields.function(_hours_quantity_calc, method=True, type='float', string='Hours tot'),
259 'last_invoice_date': fields.function(_last_invoice_date_calc, method=True, type='date', string='Last invoice date'),
260 'last_worked_invoiced_date': fields.function(_last_worked_invoiced_date_calc, method=True, type='date', string='Last invoiced worked date'),
261 'last_worked_date': fields.function(_last_worked_date_calc, method=True, type='date', string='Last worked date'),
262 'hours_qtt_non_invoiced': fields.function(_hours_qtt_non_invoiced_calc, method=True, type='float', string='Uninvoiced hours'),
263 'hours_qtt_invoiced': fields.function(_hours_qtt_invoiced_calc, method=True, type='float', string='Invoiced hours'),
264 'remaining_hours': fields.function(_remaining_hours_calc, method=True, type='float', string='Remaining hours'),
265 'remaining_ca': fields.function(_remaining_ca_calc, method=True, type='float', string='Remaining revenue'),
266 'revenue_per_hour': fields.function(_revenue_per_hour_calc, method=True, type='float', string='Revenue per hours (real)'),
267 'real_margin': fields.function(_real_margin_calc, method=True, type='float', string='Real margin'),
268 'theorical_margin': fields.function(_theorical_margin_calc, method=True, type='float', string='Theorical margin'),
269 'real_margin_rate': fields.function(_real_margin_rate_calc, method=True, type='float', string='Real margin rate (%)'),
270 'month_ids': fields.one2many('account_analytic_analysis.summary.month', 'account_id', 'Month', readonly=True),
271 'user_ids': fields.one2many('account_analytic_analysis.summary.user', 'account_id', 'User', readonly=True),
273 account_analytic_account()
275 class account_analytic_account_summary_user(osv.osv):
276 _name = "account_analytic_analysis.summary.user"
277 _description = "Hours summary by user"
281 'account_id': fields.many2one('account.analytic.account', 'Analytic Account', readonly=True),
282 'unit_amount': fields.float('Total Time', digits=(16,2), readonly=True),
283 'name' : fields.many2one('res.users','User'),
287 create or replace view account_analytic_analysis_summary_user as (
294 min(account_analytic_line.id) as id,
297 sum(unit_amount) as unit_amount
299 account_analytic_line
301 account_analytic_journal on account_analytic_line.journal_id = account_analytic_journal.id
303 account_analytic_journal.type = 'general'
307 user_id,account_id asc )as
308 sous_account_analytic_analysis_summary_user
311 account_analytic_account_summary_user()
313 class account_analytic_account_summary_month(osv.osv):
314 _name = "account_analytic_analysis.summary.month"
315 _description = "Hours summary by month"
318 'account_id': fields.many2one('account.analytic.account', 'Analytic Account', readonly=True),
319 'unit_amount': fields.float('Total Time', digits=(16,2), readonly=True),
320 'name': fields.char('Month', size=25, readonly=True),
323 cr.execute("""create or replace view account_analytic_analysis_summary_month as (
324 select id, unit_amount,account_id, sort_month,month as name from (
326 min(account_analytic_line.id) as id,
327 date_trunc('month', date) as sort_month,
329 to_char(date,'Mon YYYY') as month,
330 sum(unit_amount) as unit_amount
332 account_analytic_line join account_analytic_journal on account_analytic_line.journal_id = account_analytic_journal.id
334 account_analytic_journal.type = 'general'
336 sort_month, month, account_id
338 sort_month,account_id asc
339 )as sous_account_analytic_analysis_summary_month order by sort_month,account_id)""")
341 account_analytic_account_summary_month()