Skip to content

Commit

Permalink
merging danni
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzobracciale committed Oct 21, 2013
2 parents b656bd4 + 7af28ad commit 739a8ff
Show file tree
Hide file tree
Showing 12 changed files with 377 additions and 88 deletions.
7 changes: 7 additions & 0 deletions fusolab2_0/apps/bar/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,15 @@ def formfield_for_foreignkey(self, db_field, request, **kwargs):
kwargs['queryset'] = User.objects.filter(groups__name='turnisti')
return super(BarBalanceAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)

class SmallBalanceAdmin(admin.ModelAdmin):
def formfield_for_foreignkey(self, db_field, request, **kwargs):
if db_field.name == 'cashier':
kwargs['queryset'] = User.objects.filter(groups__name='turnisti')
return super(SmallBalanceAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)

# Re-register UserAdmin
admin.site.register(PurchasedProduct)
admin.site.register(Product)
admin.site.register(Receipt)
admin.site.register(BarBalance, BarBalanceAdmin)
admin.site.register(SmallBalance, SmallBalanceAdmin)
22 changes: 17 additions & 5 deletions fusolab2_0/apps/bar/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def __init__(self, *args, **kwargs):

class Meta:
model = BarBalance
exclude = ('parent','subtype', 'date')
exclude = ('parent', 'date')
widgets = {
'cashier': HiddenInput(),
}
Expand All @@ -29,30 +29,42 @@ class Meta:
class BarOpeningModelForm(BarBalanceModelForm):
def __init__(self, *args, **kwargs):
super(BarOpeningModelForm, self).__init__(*args, **kwargs)
self.fields['operation'] = forms.CharField(widget=forms.HiddenInput(),initial=OPENING)
self.fields['operation'] = forms.CharField(widget=forms.HiddenInput(),initial=OPENING)
self.fields['promoter'] = forms.CharField(required=False)
self.fields['name'] = forms.CharField(required=False)
self.fields['subtype'] = forms.CharField(widget=forms.HiddenInput(), required=False)

class BarClosingModelForm(BarBalanceModelForm):
def __init__(self, *args, **kwargs):
super(BarClosingModelForm, self).__init__(*args, **kwargs)
self.fields['operation'] = forms.CharField(widget=forms.HiddenInput(),initial=CLOSING)

self.fields['operation'] = forms.CharField(widget=forms.HiddenInput(),initial=CLOSING)
self.fields['subtype'] = forms.CharField(widget=forms.HiddenInput(), required=False)
self.fields['promoter'] = forms.CharField(widget=forms.HiddenInput(), required=False)
self.fields['name'] = forms.CharField(widget=forms.HiddenInput(), required=False)

class BarPaymentModelForm(BarBalanceModelForm):
def __init__(self, *args, **kwargs):
super(BarPaymentModelForm, self).__init__(*args, **kwargs)
self.fields['operation'] = forms.CharField(widget=forms.HiddenInput(),initial=PAYMENT)
self.fields['subtype'] = forms.CharField(widget=forms.Select(choices=PAYMENT_SUBTYPES))

self.fields['promoter'] = forms.CharField(widget=forms.HiddenInput(), required=False)
self.fields['name'] = forms.CharField(widget=forms.HiddenInput(), required=False)


class BarDepositModelForm(BarBalanceModelForm):
def __init__(self, *args, **kwargs):
super(BarDepositModelForm, self).__init__(*args, **kwargs)
self.fields['operation'] = forms.CharField(widget=forms.HiddenInput(),initial=DEPOSIT)
self.fields['subtype'] = forms.CharField(widget=forms.Select(choices=DEPOSIT_SUBTYPES))
self.fields['promoter'] = forms.CharField(widget=forms.HiddenInput(), required=False)
self.fields['name'] = forms.CharField(widget=forms.HiddenInput(), required=False)

class BarWithdrawModelForm(BarBalanceModelForm):
def __init__(self, *args, **kwargs):
super(BarWithdrawModelForm, self).__init__(*args, **kwargs)
self.fields['operation'] = forms.CharField(widget=forms.HiddenInput(),initial=WITHDRAW)
self.fields['promoter'] = forms.CharField(widget=forms.HiddenInput(), required=False)
self.fields['name'] = forms.CharField(widget=forms.HiddenInput(), required=False)

#
# SMALL FORMS
Expand Down
11 changes: 10 additions & 1 deletion fusolab2_0/apps/bar/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@
('do','donazione')
)

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

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

class BalanceManager(models.Manager):

Expand Down Expand Up @@ -76,7 +83,7 @@ def get_withdraws_for(self, current_opening):
return super(BalanceManager, self).get_query_set().filter(Q(parent=current_opening.id) & Q(operation=WITHDRAW)).aggregate(Sum('amount'))['amount__sum']

def get_opening_times(self,start_date,end_date):
list = super(BalanceManager, self).get_query_set().filter(Q(date__range=[start_date,end_date]) & Q(parent__isnull=True)).select_related()
list = super(BalanceManager, self).get_query_set().filter(Q(date__range=[start_date,end_date]) & Q(parent__isnull=True)).select_related().order_by('date')
ret = []
for l in list:
ret.append([l.date,super(BalanceManager, self).get_query_set().filter(Q(parent=l.id) & Q(operation=CLOSING)).get().date])
Expand All @@ -91,6 +98,8 @@ def get_checkpoint_before(self,saved_balance):
def get_last_n(self,n):
return super(BalanceManager, self).get_query_set().order_by('-date')[:n]



class ReceiptManager(models.Manager):

def total_amount(self):
Expand Down
47 changes: 25 additions & 22 deletions fusolab2_0/apps/bar/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class Meta:
#assegna l'id automaticamente durante il salvataggio per raggruppare gli eventi
def save(self, *args, **kwargs):
if self.operation != CASHPOINT:
self.parent = SmallBalance.objects.get_parent(self.date)
self.parent = SmallBalance.objects.get_parent_t(self.date)
super(SmallBalance, self).save(*args, **kwargs)

def get_bar_summary(closing):
Expand All @@ -125,23 +125,24 @@ def get_bar_summary(closing):
if closing.parent.note:
notes.append("apertura "+str(closing.parent.amount)+" "+closing.parent.note)
d['last_closing_amount'] = BarBalance.objects.get_last_closing(closing.parent)

d['closing_amount'] = closing.amount

#calcolo del valore atteso della chiusura
d['expected_balance'] = 0
d['expected_balance']+=d['opening_amount']

try:
opening_transactions = BarBalance.objects.get_transactions_for(closing.parent)
except(IndexError, BarBalance.DoesNotExist):
pass
#send_mail('allarme chiusura bar', 'errore tragico #1', '[email protected]',NOTIFICATION_ADDRESS_LIST, fail_silently=False)

if Receipt.objects.total_between(closing.parent.date,closing.date):
d['receipt_count'] = Receipt.objects.total_between(closing.parent.date,closing.date)
else:
d['receipt_count'] = 0
d['expected_balance']+=d['receipt_count']

try:
opening_transactions = BarBalance.objects.get_transactions_for(closing.parent)
except(IndexError, BarBalance.DoesNotExist):
pass
#send_mail('allarme chiusura bar', 'errore tragico #1', '[email protected]',NOTIFICATION_ADDRESS_LIST, fail_silently=False)


d[DEPOSIT] = 0
d[PAYMENT] = 0
Expand All @@ -157,7 +158,7 @@ def get_bar_summary(closing):
d['expected_balance']-=transaction.amount

if transaction.note:
notes.append(transaction.get_operation_display()+" "+str(transaction.amount)+": "+transaction.note)
notes.append(transaction.get_operation_display()+" "+str(transaction.amount)+" "+transaction.note+"\n")

d['notes'] = notes
d['opening_check'] = d['opening_amount'] - d['last_closing_amount']
Expand All @@ -178,25 +179,27 @@ def get_small_summary(checkpoint):
d = {}
d['date'] = checkpoint.date.strftime("%d/%m/%Y")
d['checkpoint'] = checkpoint.amount
d['cashier'] = checkpoint.cashier
l = SmallBalance.objects.get_checkpoint_before(checkpoint)
if l:
d['last_checkpoint'] = SmallBalance.objects.get_checkpoint_before(checkpoint).amount
start_date = SmallBalance.objects.get_checkpoint_before(checkpoint).date
try:
checkpoint_transactions = SmallBalance.objects.get_transactions_for(l)
except(IndexError, SmallBalance.DoesNotExist):
pass
checkpoint_transactions = None
else:
d['last_checkpoint'] = 0
start_date = datetime(2012,11,01,00,00,00)
checkpoint_transactions = None

d['expected_checkpoint'] = d['last_checkpoint']
receipts = Receipts.objects.filter(date__range=[l.date,checkpoint.date])
receipts = Receipt.objects.filter(date__range=[start_date,checkpoint.date]).order_by('date')
for o in BarBalance.objects.get_opening_times(start_date,checkpoint.date):
receipts.exclude(date__range=[o[0],o[1]])
receipts = receipts.exclude(date__range=[o[0],o[1]])

if receipts:
d['receipt_count'] = receipts.total_amount()
d['receipt_count'] = receipts.aggregate(Sum('total'))['total__sum']
else:
d['receipt_count'] = 0

Expand All @@ -205,16 +208,16 @@ def get_small_summary(checkpoint):
d[DEPOSIT] = 0
d[PAYMENT] = 0
d[WITHDRAW] = 0

for transaction in checkpoint_transactions:
d[transaction.operation]+=transaction.amount
if transaction.operation in [DEPOSIT]:
d['expected_checkpoint']+=transaction.amount
elif transaction.operation in [PAYMENT,WITHDRAW]:
d['expected_checkpoint']-=transaction.amount
if checkpoint_transactions:
for transaction in checkpoint_transactions:
d[transaction.operation]+=transaction.amount
if transaction.operation in [DEPOSIT]:
d['expected_checkpoint']+=transaction.amount
elif transaction.operation in [PAYMENT,WITHDRAW]:
d['expected_checkpoint']-=transaction.amount

if transaction.note:
notes.append(transaction.get_operation_display()+" "+str(transaction.amount)+": "+transaction.note)
if transaction.note:
notes.append(transaction.get_operation_display()+" "+str(transaction.amount)+": "+transaction.note)

d['notes'] = notes
d['check'] = d['checkpoint'] - d['expected_checkpoint']
Expand Down
21 changes: 21 additions & 0 deletions fusolab2_0/apps/bar/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,24 @@ def bar_handler(sender, **kwargs):
msg = EmailMessage(subject, content, '[email protected]', to=settings.EMAIL_NOTIFICATION_LIST)
msg.content_subtype = "html"
msg.send()

@receiver(post_save, sender=SmallBalance)
def bar_handler(sender, **kwargs):
saved_balance = kwargs['instance']

if saved_balance.operation == CASHPOINT:
subject = ''
template = get_template('base/smallbalance_mail.html')

print "saved_balance parent:" , saved_balance.parent
d = get_small_summary(saved_balance)
context = Context(d)
content = template.render(context)

if ('warning' in d):
subject += 'WARNING '
subject += 'riepilogo interregno '+saved_balance.date.strftime("%d/%m/%Y")

msg = EmailMessage(subject, content, '[email protected]', to=settings.EMAIL_NOTIFICATION_LIST)
msg.content_subtype = "html"
msg.send()
2 changes: 1 addition & 1 deletion fusolab2_0/apps/reports/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
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/(?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'^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'^stats/$', 'stats.stats.stats', name='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'),
Expand Down
Loading

0 comments on commit 739a8ff

Please sign in to comment.