update calculating prize for accounting

This commit is contained in:
Robert Einsle 2017-03-02 20:51:43 +01:00
parent d4a68683fa
commit 4df4a095cb
3 changed files with 25 additions and 28 deletions

View File

@ -71,24 +71,6 @@ class Desk(models.Model):
prize_hh = models.DecimalField(max_digits=5, decimal_places=2, blank=True, null=True, prize_hh = models.DecimalField(max_digits=5, decimal_places=2, blank=True, null=True,
verbose_name="Preis Happy Hour") verbose_name="Preis Happy Hour")
def accounting_for(self):
t = Accounting.objects.filter(client=self.client, desk_no=self.desk_no)[:3][::-1]
client = self.client
location = client.location
if t.__len__() > 0:
a = t[t.__len__() - 1]
if a.time_to is None:
prize = utils.get_prize_for(
start=a.time_from,
end=datetime.now(timezone.utc),
pph=self.prize,
hh_start=location.happy_hour_start,
hh_end=location.happy_hour_end,
pphh=self.prize_hh,
)
if prize != a.prize:
a.prize = prize
return t
def __str__(self): def __str__(self):
return '{}, {}'.format(self.client.uuid, self.name) return '{}, {}'.format(self.client.uuid, self.name)
@ -105,6 +87,8 @@ class Accounting(models.Model):
prize = models.DecimalField(max_digits=5, decimal_places=2, blank=True, null=True, verbose_name="Preis") prize = models.DecimalField(max_digits=5, decimal_places=2, blank=True, null=True, verbose_name="Preis")
billed = models.BooleanField(default=False, verbose_name="Abgerechnet") billed = models.BooleanField(default=False, verbose_name="Abgerechnet")
reporter_uuid = models.UUIDField(blank=True, null=True, verbose_name='Reporter UUID') reporter_uuid = models.UUIDField(blank=True, null=True, verbose_name='Reporter UUID')
prize_normal = models.DecimalField(max_digits=5, decimal_places=2, blank=True, null=True, verbose_name="Preis Normalzeit")
prize_hh = models.DecimalField(max_digits=5, decimal_places=2, blank=True, null=True, verbose_name="Preis Happy Hour")
def __str__(self): def __str__(self):
return '{}: {} -> {}, {}, {}'.format(self.desk, self.time_from, self.time_to, self.prize, self.billed) return '{}: {} -> {}, {}, {}'.format(self.desk, self.time_from, self.time_to, self.prize, self.billed)
@ -157,7 +141,7 @@ def process_location_data():
if len(ac) > 0: if len(ac) > 0:
acc = ac[0] acc = ac[0]
acc.time_to = ld.tst acc.time_to = ld.tst
acc.prize = utils.get_prize_for( acc.prize, acc.prize_normal, acc.prize_hh = utils.get_prize_for(
start=acc.time_from, start=acc.time_from,
end=ld.tst, end=ld.tst,
pph=desk.prize, pph=desk.prize,

View File

@ -22,7 +22,7 @@ def display_client(client, desk_no):
a = acc[-1] a = acc[-1]
if a.time_to is None: if a.time_to is None:
alert = 'alert-info' alert = 'alert-info'
prize = utils.get_prize_for( prize, u1, u2 = utils.get_prize_for(
start=a.time_from, start=a.time_from,
end=datetime.now(), end=datetime.now(),
pph=desk.prize, pph=desk.prize,

View File

@ -16,6 +16,8 @@ def get_prize_for(start, end, pph=0, hh_start=None, hh_end=None, pphh=0):
if end <= start: if end <= start:
raise ValueError('end date must be after start date') raise ValueError('end date must be after start date')
prize = 0 prize = 0
prize_normal = 0
prize_hh = 0
if hh_start is not None and hh_end is not None and pphh is not None: if hh_start is not None and hh_end is not None and pphh is not None:
d = start.date() d = start.date()
t = start.time() t = start.time()
@ -24,35 +26,46 @@ def get_prize_for(start, end, pph=0, hh_start=None, hh_end=None, pphh=0):
while True: while True:
if t < hh_start: if t < hh_start:
if end_time < hh_start and d == end_date: if end_time < hh_start and d == end_date:
prize += calculate_prize_for(start=t, end=end_time, pph=pph) p = calculate_prize_for(start=t, end=end_time, pph=pph)
prize += p
prize_normal += p
break break
else: else:
prize += calculate_prize_for(start=t, end=hh_start, pph=pph) p += calculate_prize_for(start=t, end=hh_start, pph=pph)
prize += p
prize_normal += p
t = hh_start t = hh_start
elif hh_start <= t < hh_end: elif hh_start <= t < hh_end:
if end_time < hh_end and d == end_date: if end_time < hh_end and d == end_date:
prize += calculate_prize_for(start=t, end=end_time, pph=pphh) p += calculate_prize_for(start=t, end=end_time, pph=pphh)
prize += p
prize_hh += p
break break
else: else:
prize += calculate_prize_for(start=t, end=hh_end, pph=pphh) p += calculate_prize_for(start=t, end=hh_end, pph=pphh)
prize += p
prize_hh += p
t = hh_end t = hh_end
else: else:
if d == end_date: if d == end_date:
prize += calculate_prize_for(start=t, end=end_time, pph=pph) p += calculate_prize_for(start=t, end=end_time, pph=pph)
prize += p
prize_normal += p
break break
else: else:
prize += calculate_prize_for(start=t, end=datetime.strptime('23:59:59', '%H:%M:%S').time(), pph=pph) p += calculate_prize_for(start=t, end=datetime.strptime('23:59:59', '%H:%M:%S').time(), pph=pph)
prize += p
prize_normal += p
t = datetime.strptime('00:00:00', '%H:%M:%S').time() t = datetime.strptime('00:00:00', '%H:%M:%S').time()
d = (datetime.combine(d, t) + timedelta(days=1)).date() d = (datetime.combine(d, t) + timedelta(days=1)).date()
else: else:
prize = calculate_prize_for(start=start, end=end, pph=pph) prize = calculate_prize_for(start=start, end=end, pph=pph)
return prize return prize, prize_normal, prize_hh
def calculate_prize_for(start, end, pph=0): def calculate_prize_for(start, end, pph=0):
pps = pph / 3600 pps = pph / 3600
d = date.today() d = date.today()
seconds = 0
if isinstance(start, datetime): if isinstance(start, datetime):
seconds = (end - start).seconds seconds = (end - start).seconds
else: else: