Files
tryton/work.py
T

170 lines
5.8 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
2012-09-24 22:18:08 +02:00
from trytond.model import 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
2012-09-24 22:18:08 +02:00
from trytond.pool import Pool, PoolMeta
__all__ = ['TimesheetLine', 'Work']
__metaclass__ = PoolMeta
2010-08-19 22:17:42 +02:00
2009-06-04 11:19:01 +02:00
2012-09-24 22:18:08 +02:00
class TimesheetLine:
__name__ = 'timesheet.line'
2009-06-04 19:52:24 +02:00
2012-09-24 22:18:08 +02:00
def compute_cost(self):
Currency = Pool().get('currency.currency')
2009-06-04 19:52:24 +02:00
2012-09-24 22:18:08 +02:00
cost_price = self.employee.compute_cost_price(date=self.date)
2009-06-04 19:52:24 +02:00
2012-09-24 22:18:08 +02:00
line_company = self.employee.company
work_company = self.work.company
if (line_company != work_company and
line_company.currency != work_company.currency):
with Transaction().set_context(date=self.date):
cost_price = Currency.compute(line_company.currency,
cost_price, work_company.currency)
2009-06-04 19:52:24 +02:00
2012-09-24 22:18:08 +02:00
return Decimal(str(self.hours)) * cost_price
2009-06-04 11:19:01 +02:00
2012-04-03 20:07:30 +02:00
2012-09-24 22:18:08 +02:00
class Work:
__name__ = 'project.work'
2009-06-04 11:19:01 +02:00
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',
states={
'invisible': ~Eval('timesheet_available'),
},
digits=(16, Eval('currency_digits', 2)),
depends=['currency_digits', 'timesheet_available']), 'get_cost')
2010-03-11 16:52:56 +01:00
currency_digits = fields.Function(fields.Integer('Currency Digits',
2012-09-24 22:18:08 +02:00
on_change_with=['company']), 'on_change_with_currency_digits')
2009-06-04 11:19:01 +02:00
2012-09-24 22:18:08 +02:00
@classmethod
def get_cost(cls, works, name):
2012-10-17 16:30:06 +02:00
works += cls.search([
2012-09-24 22:18:08 +02:00
('parent', 'child_of', [w.id for w in works]),
('active', '=', True)]) + works
2012-10-17 16:30:06 +02:00
works = set(works)
2012-09-24 22:18:08 +02:00
costs = {}
2009-06-04 11:19:01 +02:00
id2work = {}
leafs = set()
2012-10-17 16:30:06 +02:00
for work in works:
2009-06-04 11:19:01 +02:00
id2work[work.id] = work
if not work.children:
leafs.add(work.id)
2012-09-24 22:18:08 +02:00
costs[work.id] = Decimal('0')
2009-06-04 11:19:01 +02:00
for ts_line in work.timesheet_lines:
2012-09-24 22:18:08 +02:00
costs[work.id] += ts_line.compute_cost()
2009-06-04 11:19:01 +02:00
while leafs:
for work_id in leafs:
work = id2work[work_id]
2012-10-17 16:30:06 +02:00
works.remove(work)
2009-06-04 11:19:01 +02:00
if not work.active:
continue
2012-09-24 22:18:08 +02:00
if work.parent and work.parent.id in costs:
costs[work.parent.id] += costs[work_id]
2012-10-17 16:30:06 +02:00
next_leafs = set(w.id for w in works)
for work in works:
if not work.parent:
continue
if work.parent.id in next_leafs and work.parent in works:
next_leafs.remove(work.parent.id)
leafs = next_leafs
2012-09-24 22:18:08 +02:00
return costs
2009-06-04 11:19:01 +02:00
2012-09-24 22:18:08 +02:00
@classmethod
def get_revenue(cls, works, name):
2012-10-17 16:30:06 +02:00
works = cls.search([
2012-09-24 22:18:08 +02:00
('parent', 'child_of', [w.id for w in works]),
('active', '=', True)]) + works
2012-10-17 16:30:06 +02:00
works = set(works)
2012-09-24 22:18:08 +02:00
revenues = {}
2009-06-04 11:19:01 +02:00
id2work = {}
leafs = set()
2012-10-17 16:30:06 +02:00
for work in works:
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-09-24 22:18:08 +02:00
revenues[work.id] = (work.list_price
2012-04-03 20:07:30 +02:00
* Decimal(str(work.total_effort)))
2009-06-04 11:19:01 +02:00
else:
2012-09-24 22:18:08 +02:00
revenues[work.id] = Decimal('0')
2009-06-04 11:19:01 +02:00
while leafs:
for work_id in leafs:
work = id2work[work_id]
2012-10-17 16:30:06 +02:00
works.remove(work)
2009-06-04 11:19:01 +02:00
if not work.active:
continue
2012-09-24 22:18:08 +02:00
if work.parent and work.parent.id in revenues:
revenues[work.parent.id] += revenues[work_id]
2012-10-17 16:30:06 +02:00
next_leafs = set(w.id for w in works)
for work in works:
if not work.parent:
continue
if work.parent.id in next_leafs and work.parent in works:
next_leafs.remove(work.parent.id)
leafs = next_leafs
2012-09-24 22:18:08 +02:00
return revenues
2009-06-04 11:19:01 +02:00
2012-09-24 22:18:08 +02:00
def on_change_with_currency_digits(self, name=None):
if self.company:
return self.company.currency.digits
2009-06-04 13:29:31 +02:00
return 2
2012-09-24 22:18:08 +02:00
@staticmethod
def default_currency_digits():
Company = Pool().get('company.company')
2010-08-19 22:17:42 +02:00
company = Transaction().context.get('company')
if company:
2012-09-24 22:18:08 +02:00
company = Company(company)
2009-06-04 13:29:31 +02:00
return company.currency.digits
return 2
2012-09-24 22:18:08 +02:00
def on_change_product(self):
2011-07-03 13:19:08 +02:00
pool = Pool()
2012-09-24 22:18:08 +02:00
User = pool.get('res.user')
ModelData = pool.get('ir.model.data')
Uom = pool.get('product.uom')
Currency = pool.get('currency.currency')
2009-06-04 11:19:01 +02:00
2012-09-24 22:18:08 +02:00
if not self.product:
2009-06-04 11:19:01 +02:00
return {}
2010-08-19 22:17:42 +02:00
context = {}
2009-06-04 11:19:01 +02:00
2012-09-24 22:18:08 +02:00
if self.party:
context['customer'] = self.party.id
2009-06-04 11:19:01 +02:00
2012-09-24 22:18:08 +02:00
hour_uom = Uom(ModelData.get_id('product', 'uom_hour'))
2009-06-04 11:19:01 +02:00
2010-08-19 22:17:42 +02:00
with Transaction().set_context(context):
2012-09-24 22:18:08 +02:00
list_price = Uom.compute_price(self.product.default_uom,
self.product.list_price, hour_uom)
2009-06-04 11:19:01 +02:00
2012-09-24 22:18:08 +02:00
if self.company:
user = User(Transaction().user)
if user.company != self.company:
if user.company.currency != self.company.currency:
list_price = Currency.compute(user.company.currency,
list_price, self.company.currency)
2009-06-04 11:19:01 +02:00
return {'list_price': list_price}