Skip to content
This repository has been archived by the owner on Aug 24, 2022. It is now read-only.

Commit

Permalink
invoicing bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
risufaj committed Jan 9, 2019
1 parent 567a331 commit 452b4ed
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 25 deletions.
91 changes: 68 additions & 23 deletions invoicing/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from decimal import Decimal
from functools import reduce
from collections import Counter

import datetime
from django.apps import apps
from django.db.models import Prefetch

Expand All @@ -22,6 +22,9 @@

logger = logging.getLogger('db')

def trunc_datetime(someDate):
return someDate.replace(day=1, hour=0, minute=0, second=0, microsecond=0)


class InvoicingSerializer(ModelSerializer):
request = SerializerMethodField()
Expand All @@ -32,6 +35,7 @@ class InvoicingSerializer(ModelSerializer):
percentage = SerializerMethodField()
read_length = SerializerMethodField()
num_libraries_samples = SerializerMethodField()
num_libraries_samples_show = SerializerMethodField()
library_protocol = SerializerMethodField()
fixed_costs = SerializerMethodField()
sequencing_costs = SerializerMethodField()
Expand All @@ -42,7 +46,7 @@ class InvoicingSerializer(ModelSerializer):
class Meta:
model = Request
fields = ('request', 'cost_unit', 'sequencer', 'flowcell', 'pool',
'percentage', 'read_length', 'num_libraries_samples',
'percentage', 'read_length', 'num_libraries_samples','num_libraries_samples_show',
'library_protocol', 'fixed_costs', 'sequencing_costs',
'preparation_costs', 'variable_costs', 'total_costs',)

Expand All @@ -63,6 +67,9 @@ def __init__(self, instance=None, data=empty, **kwargs):
'sequencing_depth',
)

curr_month = int(self.context['curr_month'])
curr_year = int(self.context['curr_year'])

pool_ids = instance.values_list('flowcell__lanes__pool')
pools = Pool.objects.filter(pk__in=pool_ids).prefetch_related(
Prefetch('libraries', queryset=libraries_qs),
Expand Down Expand Up @@ -92,6 +99,8 @@ def __init__(self, instance=None, data=empty, **kwargs):
'fixed_costs': fixed_costs,
'preparation_costs': preparation_costs,
'sequencing_costs': sequencing_costs,
'curr_month': curr_month,
'curr_year': curr_year,
})

def get_request(self, obj):
Expand Down Expand Up @@ -121,13 +130,14 @@ def get_percentage(self, obj):


for flowcell in obj.flowcell.all():
#pprint(vars(flowcell))

flowcell_dict = {
'flowcell_id': flowcell.flowcell_id,
'sequencer': flowcell.sequencer.pk,
'pools': [],
'flowcell_create_month':int(flowcell.create_time.strftime('%m')),
'flowcell_create_year': int(flowcell.create_time.strftime('%Y')),
'flowcell_create_time': flowcell.create_time
}

count = Counter(flowcell.lanes.values_list('pool', flat=True))
Expand Down Expand Up @@ -172,38 +182,67 @@ def get_percentage(self, obj):
def get_read_length(self, obj):
return set([x.read_length.pk for x in obj.records])

def get_num_libraries_samples_show(self,obj):

num_libraries = obj.libraries.count()
num_samples = obj.samples.count()

if num_libraries > 0:
return f'{num_libraries} libraries'
else:
return f'{num_samples} samples'

def get_num_libraries_samples(self, obj):

flowcells = self.get_percentage(obj)

mindt = min([d['flowcell_create_time'] for d in flowcells])

num_libraries = obj.libraries.count()
#min_date = trunc_datetime(datetime.datetime(minYear, minMonth, 1))
min_date = trunc_datetime(datetime.datetime(mindt.year,mindt.month,1))


curr_date = trunc_datetime(datetime.datetime(self.context['curr_year'], self.context['curr_month'], 1))


num_libraries = obj.libraries.count()
num_samples = obj.samples.count()

if num_libraries > 0:



libcount = 0
flowcells = self.get_percentage(obj)
maxYear = max([d['flowcell_create_year'] for d in flowcells])
maxMonth = max([d['flowcell_create_month'] for d in flowcells])
for flowcell in flowcells:
if flowcell['flowcell_create_month'] == maxMonth and flowcell['flowcell_create_year'] == maxYear:
for pool in flowcell['pools']:
libcount = libcount + len(pool['libraries'])

#for flowcell in flowcells:
# flowcell_dt = trunc_datetime(datetime.datetime(flowcell['flowcell_create_year'],flowcell['flowcell_create_month'],1))
# #if flowcell['flowcell_create_month'] == minMonth and flowcell['flowcell_create_year'] == minYear:
# if flowcell_dt == minDt:
# for pool in flowcell['pools']:
# libcount = libcount + len(pool['libraries'])

'''de-coupling preparation costs from flowcell'''
if curr_date == min_date:
libcount = num_libraries


return f'{libcount} libraries'


else:
sampcount = 0
flowcells = self.get_percentage(obj)
maxYear = max([d['flowcell_create_year'] for d in flowcells])
maxMonth = max([d['flowcell_create_month'] for d in flowcells])
for flowcell in flowcells:
if flowcell['flowcell_create_month'] == maxMonth and flowcell['flowcell_create_year'] == maxYear:
for pool in flowcell['pools']:
sampcount = sampcount + len(pool['samples'])

#for flowcell in flowcells:

# if flowcell['flowcell_create_month'] == minMonth and flowcell['flowcell_create_year'] == minYear:

# for pool in flowcell['pools']:
# sampcount = sampcount + len(pool['samples'])


'''de-coupling preparation costs from flowcell'''
if curr_date == min_date:
sampcount = num_samples

return f'{sampcount} samples'

def get_library_protocol(self, obj):
Expand Down Expand Up @@ -241,14 +280,17 @@ def to_representation(self, instance):
Fix so samples of a request that have been sequenced in different months are not billed twice
Only bill if it has been sequenced in the latest month that is in the flowcell list
'''
maxYear = max([d['flowcell_create_year'] for d in percentage])
maxMonth = max([d['flowcell_create_month'] for d in percentage])

curr_date = trunc_datetime(datetime.datetime(self.context['curr_year'], self.context['curr_month'], 1))


# Calculate Fixed Costs
costs = 0
for flowcell in percentage:
if flowcell['flowcell_create_month'] == maxMonth and flowcell['flowcell_create_year'] == maxYear:
dt = flowcell['flowcell_create_time']
dt = trunc_datetime(datetime.datetime(dt.year,dt.month,1))
#if flowcell['flowcell_create_month'] == int(self.context['curr_month']) and flowcell['flowcell_create_year'] == int(self.context['curr_year']):
if dt == curr_date:

for pool in flowcell['pools']:
costs += fixed_costs.get(flowcell['sequencer'], 0) * \
Expand All @@ -259,7 +301,10 @@ def to_representation(self, instance):
# Calculate Sequencing Costs
costs = 0
for flowcell in percentage:
if flowcell['flowcell_create_month'] == maxMonth and flowcell['flowcell_create_year'] == maxYear:
dt = flowcell['flowcell_create_time']
dt = trunc_datetime(datetime.datetime(dt.year, dt.month, 1))
#if flowcell['flowcell_create_month'] == self.context['curr_month'] and flowcell['flowcell_create_year'] == self.context['curr_year']:
if dt == curr_date:
for pool in flowcell['pools']:
key = f"{flowcell['sequencer']}_{pool['read_length']}"
costs += sequencing_costs.get(key, 0) * \
Expand Down
14 changes: 14 additions & 0 deletions invoicing/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,22 @@

class InvoicingViewSet(viewsets.ReadOnlyModelViewSet):
permission_classes = [IsAdminUser]

serializer_class = InvoicingSerializer

def get_serializer_context(self):
today = datetime.date.today()
year = self.request.query_params.get('year', today.year)
month = self.request.query_params.get('month', today.month)
ctx = {'curr_month': month, 'curr_year': year, 'today': today}
return ctx

def get_queryset(self):
today = datetime.date.today()
year = self.request.query_params.get('year', today.year)
month = self.request.query_params.get('month', today.month)


flowcell_qs = Flowcell.objects.select_related(
'sequencer',
).order_by('flowcell_id')
Expand Down Expand Up @@ -90,6 +99,11 @@ def get_queryset(self):

def list(self,request):
queryset = self.filter_queryset(self.get_queryset())

today = datetime.date.today()
year = self.request.query_params.get('year', today.year)
month = self.request.query_params.get('month', today.month)
ctx = {'curr_month': month, 'curr_year': year, 'today': today}
serializer = self.get_serializer(queryset, many=True)

return Response(serializer.data)
Expand Down
2 changes: 1 addition & 1 deletion static/main-hub/app/model/invoicing/Request.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Ext.define('MainHub.model.invoicing.Request', {
type: 'auto'
},
{
name: 'num_libraries_samples',
name: 'num_libraries_samples_show',
type: 'string'
},
{
Expand Down
2 changes: 1 addition & 1 deletion static/main-hub/app/view/invoicing/Invoicing.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ Ext.define('MainHub.view.invoicing.Invoicing', {
},
{
text: '# of Libraries/Samples',
dataIndex: 'num_libraries_samples',
dataIndex: 'num_libraries_samples_show',
minWidth: 150
},
{
Expand Down

0 comments on commit 452b4ed

Please sign in to comment.