Files
tryton/work.py
T

181 lines
6.1 KiB
Python
Raw Normal View History

2009-06-04 11:19:01 +02:00
#This file is part of Tryton. The COPYRIGHT file at the top level of
#this repository contains the full copyright notices and license terms.
2010-08-19 22:17:42 +02:00
from decimal import Decimal
2009-06-04 11:19:01 +02:00
from trytond.model import ModelView, ModelSQL, fields
2011-08-21 11:20:49 +02:00
from trytond.pyson import Eval
2010-08-19 22:17:42 +02:00
from trytond.transaction import Transaction
2011-07-03 13:19:08 +02:00
from trytond.pool import Pool
2010-08-19 22:17:42 +02:00
2009-06-04 11:19:01 +02:00
2009-06-04 19:52:24 +02:00
class TimesheetLine(ModelSQL, ModelView):
_name = 'timesheet.line'
2010-08-19 22:17:42 +02:00
def compute_cost(self, line):
2011-07-03 13:19:08 +02:00
employee_obj = Pool().get('company.employee')
currency_obj = Pool().get('currency.currency')
2009-06-04 19:52:24 +02:00
2010-08-19 22:17:42 +02:00
cost_price = employee_obj.compute_cost_price(line.employee.id,
date=line.date)
2009-06-04 19:52:24 +02:00
line_company = line.employee.company
work_company = line.work.company
if line_company.id != work_company.id and\
line_company.currency.id != work_company.currency.id:
with Transaction().set_context(date=line.date):
cost_price = currency_obj.compute(line_company.currency.id,
cost_price, work_company.currency.id)
2009-06-04 19:52:24 +02:00
return Decimal(str(line.hours)) * cost_price
TimesheetLine()
2009-06-04 11:19:01 +02:00
2012-04-03 20:07:30 +02:00
2009-06-04 11:19:01 +02:00
class Work(ModelSQL, ModelView):
'Work Effort'
_name = 'project.work'
product = fields.Many2One('product.product', 'Product',
2011-08-21 11:20:49 +02:00
states={
'invisible': ~Eval('timesheet_available'),
},
depends=['timesheet_available'],
on_change=['product', 'party', 'hours', 'company'])
2010-01-20 11:03:14 +01:00
list_price = fields.Numeric('List Price',
digits=(16, Eval('currency_digits', 2)), depends=['currency_digits'])
2011-08-21 11:20:49 +02:00
revenue = fields.Function(fields.Numeric('Revenue',
digits=(16, Eval('currency_digits', 2)),
depends=['currency_digits']), 'get_revenue')
2010-03-11 16:52:56 +01:00
cost = fields.Function(fields.Numeric('Cost',
digits=(16, Eval('currency_digits', 2)), depends=['currency_digits']),
'get_cost')
currency_digits = fields.Function(fields.Integer('Currency Digits',
on_change_with=['company']), 'get_currency_digits')
2009-06-04 11:19:01 +02:00
2010-08-19 22:17:42 +02:00
def get_cost(self, ids, name):
2011-07-03 13:19:08 +02:00
timesheet_line_obj = Pool().get('timesheet.line')
2010-08-19 22:17:42 +02:00
all_ids = self.search([
2009-06-04 11:19:01 +02:00
('parent', 'child_of', ids),
2010-08-19 22:17:42 +02:00
('active', '=', True)]) + ids
2009-06-04 11:19:01 +02:00
all_ids = list(set(all_ids))
res = {}
id2work = {}
leafs = set()
2010-08-19 22:17:42 +02:00
for work in self.browse(all_ids):
2009-06-04 11:19:01 +02:00
id2work[work.id] = work
if not work.children:
leafs.add(work.id)
2009-06-04 19:52:24 +02:00
res[work.id] = Decimal('0')
2009-06-04 11:19:01 +02:00
for ts_line in work.timesheet_lines:
2010-08-19 22:17:42 +02:00
res[work.id] += timesheet_line_obj.compute_cost(ts_line)
2009-06-04 11:19:01 +02:00
while leafs:
parents = set()
for work_id in leafs:
work = id2work[work_id]
if not work.active:
continue
if work.parent and work.parent.id in res:
res[work.parent.id] += res[work_id]
parents.add(work.parent.id)
leafs = parents
for id in all_ids:
if id not in ids:
del res[id]
return res
2010-08-19 22:17:42 +02:00
def get_revenue(self, ids, name):
all_ids = self.search([
2009-06-04 11:19:01 +02:00
('parent', 'child_of', ids),
2010-08-19 22:17:42 +02:00
('active', '=', True)]) + ids
2009-06-04 11:19:01 +02:00
all_ids = list(set(all_ids))
res = {}
id2work = {}
leafs = set()
2010-08-19 22:17:42 +02:00
for work in self.browse(all_ids):
2009-06-04 11:19:01 +02:00
id2work[work.id] = work
if not work.children:
leafs.add(work.id)
2012-03-30 19:20:18 +02:00
if work.type == 'task' and work.list_price:
2012-04-03 20:07:30 +02:00
res[work.id] = (work.list_price
* Decimal(str(work.total_effort)))
2009-06-04 11:19:01 +02:00
else:
2009-06-04 19:52:24 +02:00
res[work.id] = Decimal('0')
2009-06-04 11:19:01 +02:00
while leafs:
parents = set()
for work_id in leafs:
work = id2work[work_id]
if not work.active:
continue
if work.parent and work.parent.id in res:
res[work.parent.id] += res[work_id]
parents.add(work.parent.id)
leafs = parents
for id in all_ids:
if id not in ids:
del res[id]
return res
2010-08-19 22:17:42 +02:00
def get_currency_digits(self, ids, name):
2009-06-04 13:29:31 +02:00
res = {}
2010-08-19 22:17:42 +02:00
for work in self.browse(ids):
2009-06-04 13:29:31 +02:00
res[work.id] = work.company.currency.digits
return res
2010-08-19 22:17:42 +02:00
def on_change_with_currency_digits(self, vals):
2011-07-03 13:19:08 +02:00
company_obj = Pool().get('company.company')
2009-06-04 13:29:31 +02:00
if vals.get('company'):
2010-08-19 22:17:42 +02:00
company = company_obj.browse(vals['company'])
2009-06-04 13:29:31 +02:00
return company.currency.digits
return 2
2010-08-19 22:17:42 +02:00
def default_currency_digits(self):
2011-07-03 13:19:08 +02:00
company_obj = Pool().get('company.company')
2010-08-19 22:17:42 +02:00
company = Transaction().context.get('company')
if company:
company = company_obj.browse(company)
2009-06-04 13:29:31 +02:00
return company.currency.digits
return 2
2010-08-19 22:17:42 +02:00
def on_change_product(self, vals):
2011-07-03 13:19:08 +02:00
pool = Pool()
product_obj = pool.get('product.product')
user_obj = pool.get('res.user')
company_obj = pool.get('company.company')
model_data_obj = pool.get('ir.model.data')
uom_obj = pool.get('product.uom')
2011-11-02 16:12:17 +01:00
currency_obj = pool.get('currency.currency')
2009-06-04 11:19:01 +02:00
if not vals.get('product'):
return {}
2010-08-19 22:17:42 +02:00
context = {}
2009-06-04 11:19:01 +02:00
2010-08-19 22:17:42 +02:00
product = product_obj.browse(vals['product'])
2009-06-04 11:19:01 +02:00
if vals.get('party'):
2010-08-19 22:17:42 +02:00
context['customer'] = vals['party']
2009-06-04 11:19:01 +02:00
2010-08-19 22:17:42 +02:00
uom_id = model_data_obj.get_id('product', 'uom_hour')
hour_uom = uom_obj.browse(uom_id)
2009-06-04 11:19:01 +02:00
2010-08-19 22:17:42 +02:00
with Transaction().set_context(context):
list_price = uom_obj.compute_price(product.default_uom,
product.list_price, hour_uom)
2009-06-04 11:19:01 +02:00
if vals.get('company'):
2010-08-19 22:17:42 +02:00
user = user_obj.browse(Transaction().user)
if user.company.id != vals['company']:
company = company_obj.browse(vals['company'])
if user.company.currency.id != company.currency.id:
list_price = currency_obj.compute(user.company.currency,
list_price, company.currency)
2009-06-04 11:19:01 +02:00
return {'list_price': list_price}
Work()