Skip to content

Commit

Permalink
baraled patched, stats improved
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzobracciale committed Nov 8, 2013
1 parent 3877397 commit 3dff9df
Show file tree
Hide file tree
Showing 9 changed files with 754 additions and 335 deletions.
6 changes: 3 additions & 3 deletions fusolab2_0/apps/bar/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@

def get_payment_display(key):
d = dict(PAYMENT_SUBTYPES)
return d[key] if key in d else None
return d[key] if key in d else ""

def get_deposit_display(key):
d = dict(DEPOSIT_SUBTYPES)
return d[key] if key in d else None
return d[key] if key in d else ""

class BalanceManager(models.Manager):

Expand All @@ -48,7 +48,7 @@ def is_open(self,time=datetime.now):
except ObjectDoesNotExist:
return False

def get_parent_t(self, current_time):
def get_parent_t(self, current_time=datetime.now):
try:
return super(BalanceManager, self).get_query_set().filter(Q(operation__in=[OPENING,CASHPOINT]) & Q(date__lt=current_time)).latest('date')
except ObjectDoesNotExist:
Expand Down
6 changes: 2 additions & 4 deletions fusolab2_0/apps/baraled/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,10 @@ def send_ledbar_cmd(sender, instance, **kwargs):
'''
After being saved, each LedString sends an UPD message to the Arduino that controls the led bar
'''
BARALED_IP = settings.IP_OPENER
BARALED_PORT = settings.PORT_OPENER
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.sendto(instance.coded_sentence, (BARALED_IP, BARALED_PORT))
sock.sendto("baraled:" + instance.coded_sentence, (settings.BARALED_IP, settings.BARALED_PORT))
sock.close()


post_save.connect(send_ledbar_cmd, sender=LedString, dispatch_uid="send_ledbar_cmd")
post_save.connect(send_ledbar_cmd, sender=LedString)

2 changes: 1 addition & 1 deletion fusolab2_0/apps/ingresso/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def get_entrance_summary(closing):

notes.append("apertura "+ str(closing.parent.amount))
if closing.parent.note:
notes.append(closing.parent.note.replace("\r\n"," ")+"\n")
notes.append(" "+closing.parent.note.replace("\r\n"," ")+"\n")
else:
notes.append("\n")

Expand Down
84 changes: 59 additions & 25 deletions fusolab2_0/apps/reports/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from django.db.models import Sum
from django.db.models import Count
from bar.models import *
from ingresso.models import *
from django.conf import settings
from django.utils import simplejson
from decimal import Decimal
Expand All @@ -27,36 +28,69 @@ def default(self, obj):
return simplejson.JSONEncoder.default(self, obj)

@staff_member_required
def get_bar_stats(request):
def get_stats(request,what,yyyy=None,mm=None,dd=None):

start_time = BarBalance.objects.get_parent_t()
end_time = datetime.now()
if all((yyyy,mm,dd)):
end_time = datetime(int(yyyy),int(mm),int(dd)+1,12,00,00)
start_time = datetime(int(yyyy),int(mm),int(dd),12,00,00)

end_time = datetime(2013,10,26,00,00,00)
start_time = datetime(2013,10,24,00,00,00)

data = []
series = {}
counter = {}
products = Product.objects.all()
#initialize counter and data series dicts
for pa in products:
counter[pa.name] = 0
series[pa.name] = []
data = []

if what == "bar":
series = {}
counter = {}
products = Product.objects.all()

#initialize counter and data series dicts
for pa in products:
counter[pa.name] = 0
series[pa.name] = []

#fill data series dict
for r in Receipt.objects.receipts_between(start_time,end_time):
for pp in PurchasedProduct.objects.filter(receipt=r):
counter[pp.name]+=1
for pb in products:
series[pb.name].append( [ int(r.date.strftime('%s')+'000') , counter[pb.name] * pb.cost ] )
# series[pb.name].append( [ r.date , counter[pb.name] * pb.cost ] )
#fill data series dict
for r in Receipt.objects.receipts_between(start_time,end_time):
for pp in PurchasedProduct.objects.filter(receipt=r):
counter[pp.name]+=1
for pb in products:
series[pb.name].append( [ int(r.date.strftime('%s')+'000') , counter[pb.name] * pb.cost ] )

#build return list of dicts
for pc in products:
data.append( {"key" : pc.name.encode('utf-8') , "values" : series[pc.name] } )
#build return list of dicts
for pc in products:
data.append( {"key" : pc.name.encode('utf-8') , "values" : series[pc.name] } )

buffer = simplejson.dumps( data, cls=FuckYeahEncoder )
return HttpResponse(buffer, mimetype = "application/json" )
buffer = simplejson.dumps( data, cls=FuckYeahEncoder )
return HttpResponse(buffer, mimetype = "application/json" )

if what == "ingresso":

counter_serie = []
counter = 0
amount_serie = [ ]
amount = 0

for e in Entrance.objects.filter(date__range=[start_time,end_time]):
counter += 1
counter_serie.append( [ int(e.date.strftime('%s')+'000') , counter ] )
amount_serie.append( [ int(e.date.strftime('%s')+'000') , e.cost ] )

data.append( {"key" : "Euro" ,"bar": true, "values" : amount_serie } )
data.append( {"key" : "Numerico" , "values" : counter_serie } )

buffer = simplejson.dumps( data, cls=FuckYeahEncoder )
return HttpResponse(buffer, mimetype = "application/json" )

if what == "total":

values = []
values.append( {"label": "Bar", "value" : Receipt.objects.total_between(start_time,end_time)})
values.append( {"label": "Ingresso", "value" : Entrance.objects.total_between(start_time,end_time)})
data = { "key" : "Cumulative Return" , "values" : values }
buffer = simplejson.dumps( data, cls=FuckYeahEncoder )
return HttpResponse(buffer, mimetype = "application/json" )
else:
raise Http404

# @staff_member_required
# def stats(request):
Expand Down
9 changes: 3 additions & 6 deletions fusolab2_0/apps/reports/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@
from django.views.generic import TemplateView

urlpatterns = patterns('',
url(r'^/daily_stats/$', 'reports.views.daily_stats', name='reports_daily_stats'),
#url(r'^/daily_stats/$', 'reports.views.daily_stats', name='reports_daily_stats'),
#url(r'^/make_balance/$', 'reports.views.make_balance', name='reports_make_balance'),

url(r'^excel_writer/(?P<what>\S+)/(?P<from_day>\d{2})/(?P<from_month>\d{2})/(?P<from_year>\d{4})/(?P<to_day>\d{2})/(?P<to_month>\d{2})/(?P<to_year>\d{4})/$', 'reports.views.excel_writer', name='excel_writer'),


url(r'^excel/(?P<what>\S+)/(?P<from_day>\d{2})/(?P<from_month>\d{2})/(?P<from_year>\d{4})/(?P<to_day>\d{2})/(?P<to_month>\d{2})/(?P<to_year>\d{4})/$', 'reports.views.excel', name='excel'),
#url(r'^stats/(?P<what>\S+)/(?P<interval>\S+)/(?P<dd>\d{2})/(?P<mm>\d{2})/(?P<yyyy>\d{4})/$', 'stats.stats.ajax_stats', name='ajax_stats'),
url(r'^get_daily_stats.json$', 'reports.stats.get_bar_stats', name='get_bar_stats'),
url(r'^(?P<yyyy>\d{4})/(?P<mm>\d{2})/(?P<dd>\d{2})/(?P<what>\S+).json$', 'reports.stats.get_stats', name='get_stats'),
#url(r'^devstats/(?P<what>\S+)/(?P<interval>\S+)/(?P<dd>\d{2})/(?P<mm>\d{2})/(?P<yyyy>\d{4})/$', 'stats.stats.ajax_stats_dev', name='ajax_stats_dev'),
#url(r'^trends/(?P<what>\S+)/(?P<interval>\S+)/(?P<dd>\d{2})/(?P<mm>\d{2})/(?P<yyyy>\d{4})/$', 'stats.trends.ajax_stats', name='ajax_stats'),
#url(r'^trends/$', 'stats.trends.trends', name='trends'),
Expand Down
Loading

0 comments on commit 3dff9df

Please sign in to comment.