Files
tryton/modules/project_revenue/work.py
T

217 lines
7.6 KiB
Python
Raw Normal View History

2014-12-02 12:55:45 +01: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.
2015-09-24 16:08:08 +02:00
from collections import defaultdict
2021-12-11 17:58:43 +01:00
from decimal import Decimal
2015-09-22 19:27:26 +02:00
from sql.aggregate import Sum
from sql.conditionals import Coalesce
from sql.functions import Extract
2015-09-24 16:08:08 +02:00
from sql.operators import Concat
2013-09-20 17:34:12 +02:00
from trytond import backend
2012-09-24 22:18:08 +02:00
from trytond.model import fields
2021-08-21 09:09:56 +02:00
from trytond.modules.currency.fields import Monetary
2020-04-23 21:44:00 +02:00
from trytond.modules.product import price_digits, round_price
2021-12-11 17:58:43 +01:00
from trytond.pool import Pool, PoolMeta
2026-02-25 15:25:02 +01:00
from trytond.pyson import Bool, Eval, If
from trytond.tools import sqlite_apply_types
2021-12-11 17:58:43 +01:00
from trytond.transaction import Transaction
2015-10-01 22:27:18 +02:00
2009-06-04 11:19:01 +02:00
2018-06-26 19:11:51 +02:00
class Work(metaclass=PoolMeta):
2012-09-24 22:18:08 +02:00
__name__ = 'project.work'
2009-06-04 11:19:01 +02:00
product = fields.Many2One('product.product', 'Product',
2013-05-27 17:55:27 +02:00
domain=[
('type', '=', 'service'),
2026-02-25 15:25:02 +01:00
If(Eval('origin', None),
('id', '=', -1),
()),
],
2026-02-25 15:25:02 +01:00
states={
'invisible': Bool(Eval('origin', None)),
},
context={
'company': Eval('company', -1),
},
depends={'company'})
2021-08-21 09:09:56 +02:00
list_price = Monetary(
"List Price", currency='currency', digits=price_digits)
revenue = fields.Function(Monetary(
"Revenue", currency='currency', digits='currency'),
'get_total')
cost = fields.Function(Monetary(
"Cost", currency='currency', digits='currency'),
'get_total')
2020-10-09 19:09:05 +02:00
currency = fields.Function(fields.Many2One('currency.currency',
'Currency'), 'on_change_with_currency')
2009-06-04 11:19:01 +02:00
@classmethod
def get_total(cls, works, names):
result = super().get_total(works, names)
currencies = []
for name in names:
field = getattr(cls, name)
if getattr(field, 'digits', None) == 'currency':
currencies.append(name)
if currencies:
for work in works:
for name in currencies:
result[name][work.id] = work.company.currency.round(
result[name][work.id])
return result
2012-09-24 22:18:08 +02:00
@classmethod
2015-09-17 11:19:59 +02:00
def _get_cost(cls, works):
costs = defaultdict(Decimal)
for work_id, cost in cls._timesheet_cost(works):
costs[work_id] += cost
return costs
@classmethod
def _timesheet_cost(cls, works):
2013-09-20 17:34:12 +02:00
pool = Pool()
Line = pool.get('timesheet.line')
Work = pool.get('timesheet.work')
transaction = Transaction()
cursor = transaction.connection.cursor()
2015-09-17 09:31:09 +02:00
table = cls.__table__()
work = Work.__table__()
2013-09-20 17:34:12 +02:00
line = Line.__table__()
cost = line.cost_price * Extract('EPOCH', line.duration) / (60 * 60)
2026-03-22 21:05:26 +01:00
where = fields.SQL_OPERATORS['in'](table.id, map(int, works))
query = (table.join(work,
2026-03-22 21:05:26 +01:00
condition=(
Concat(cls.__name__ + ',', table.id) == work.origin)
).join(line, condition=line.work == work.id
).select(table.id, Sum(Coalesce(cost, 0)).as_('cost'),
2026-03-22 21:05:26 +01:00
where=where,
group_by=[table.id]))
if backend.name == 'sqlite':
sqlite_apply_types(query, [None, 'NUMERIC'])
cursor.execute(*query)
yield from cursor
2009-06-04 11:19:01 +02:00
2015-09-24 16:08:08 +02:00
@classmethod
def _get_revenue(cls, works):
revenues = defaultdict(Decimal)
for work in works:
if not work.list_price:
continue
if work.price_list_hour:
revenue = work.list_price * Decimal(str(work.effort_hours))
else:
revenue = work.list_price
revenues[work.id] = revenue
return revenues
@fields.depends('company')
def on_change_with_currency(self, name=None):
return self.company.currency if self.company else None
@fields.depends('product')
def on_change_product(self):
pool = Pool()
ModelData = pool.get('ir.model.data')
Uom = pool.get('product.uom')
if not self.product:
return
list_price = self.product.list_price_used
if list_price is not None:
if self.price_list_hour:
hour_uom = Uom(ModelData.get_id('product', 'uom_hour'))
list_price = Uom.compute_price(
self.product.default_uom, list_price, hour_uom)
list_price = round_price(list_price)
self.list_price = list_price
@property
def price_list_hour(self):
pool = Pool()
ModelData = pool.get('ir.model.data')
Category = pool.get('product.uom.category')
if not self.product:
return
time = Category(ModelData.get_id('product', 'uom_cat_time'))
return self.product.default_uom_category == time
class Work_Purchase(metaclass=PoolMeta):
__name__ = 'project.work'
purchase_lines = fields.One2Many(
'purchase.line', 'work', "Purchase Lines",
domain=[
('purchase.company', '=', Eval('company', -1)),
('type', '=', 'line'),
])
@classmethod
def _get_cost(cls, works):
costs = super()._get_cost(works)
for work_id, cost in cls._purchase_cost(works):
costs[work_id] += cost
return costs
@classmethod
def _purchase_cost(cls, works):
"Compute direct purchase cost"
2015-09-24 16:08:08 +02:00
pool = Pool()
Currency = pool.get('currency.currency')
PurchaseLine = pool.get('purchase.line')
InvoiceLine = pool.get('account.invoice.line')
Invoice = pool.get('account.invoice')
Company = pool.get('company.company')
cursor = Transaction().connection.cursor()
2015-09-24 16:08:08 +02:00
table = cls.__table__()
purchase_line = PurchaseLine.__table__()
invoice_line = InvoiceLine.__table__()
invoice = Invoice.__table__()
company = Company.__table__()
work2currency = {}
iline2work = {}
2026-03-22 21:05:26 +01:00
where = fields.SQL_OPERATORS['in'](table.id, map(int, works))
cursor.execute(*table.join(purchase_line,
condition=purchase_line.work == table.id
).join(invoice_line,
condition=invoice_line.origin == Concat(
'purchase.line,', purchase_line.id)
).join(invoice,
condition=invoice_line.invoice == invoice.id
).select(invoice_line.id, table.id,
where=where & ~invoice.state.in_(['draft', 'cancelled'])))
iline2work.update(cursor)
2015-09-24 16:08:08 +02:00
2026-03-22 21:05:26 +01:00
cursor.execute(*table.join(company,
condition=table.company == company.id
).select(table.id, company.currency,
where=where))
work2currency.update(cursor)
2015-09-24 16:08:08 +02:00
2018-06-26 19:11:51 +02:00
currencies = Currency.browse(set(work2currency.values()))
2015-09-24 16:08:08 +02:00
id2currency = {c.id: c for c in currencies}
2018-06-26 19:11:51 +02:00
invoice_lines = InvoiceLine.browse(list(iline2work.keys()))
2015-09-24 16:08:08 +02:00
for invoice_line in invoice_lines:
invoice = invoice_line.invoice
work_id = iline2work[invoice_line.id]
currency_id = work2currency[work_id]
currency = id2currency[currency_id]
if currency != invoice.currency:
with Transaction().set_context(date=invoice.currency_date):
amount = Currency.compute(invoice.currency,
invoice_line.amount, currency)
else:
amount = invoice_line.amount
yield work_id, amount
2015-09-24 16:08:08 +02:00
2022-06-16 14:31:01 +02:00
@classmethod
def copy(cls, records, default=None):
default = default.copy() if default is not None else {}
default.setdefault('purchase_lines', None)
2022-06-16 14:31:01 +02:00
return super().copy(records, default=default)