2017-02-01 17:52:16 +01:00
|
|
|
import uuid
|
2017-02-26 12:03:18 +01:00
|
|
|
import logging
|
2017-01-03 19:26:51 +01:00
|
|
|
from django.db import models
|
2017-02-04 06:51:09 +01:00
|
|
|
from django.contrib.auth.models import User
|
2017-02-06 20:11:20 +01:00
|
|
|
from datetime import datetime, timezone
|
2017-02-11 11:31:27 +01:00
|
|
|
from billard import utils
|
2017-02-06 19:11:32 +01:00
|
|
|
from django.contrib.auth.models import User
|
|
|
|
from django.db.models.signals import post_save
|
2017-02-10 20:49:38 +01:00
|
|
|
from django.dispatch import receiver
|
2017-02-10 21:02:07 +01:00
|
|
|
from celery import shared_task
|
2017-02-11 18:59:42 +01:00
|
|
|
from .tasks import request_process_location_data
|
2017-01-03 19:26:51 +01:00
|
|
|
|
2017-02-26 12:03:18 +01:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2017-01-03 19:26:51 +01:00
|
|
|
|
2017-02-01 13:51:15 +01:00
|
|
|
class LocationData(models.Model):
|
2017-02-10 20:49:38 +01:00
|
|
|
client_id = models.UUIDField(blank=False, null=False, verbose_name="Client-ID")
|
2017-02-10 19:55:31 +01:00
|
|
|
desk_no = models.IntegerField(blank=False, null=False, verbose_name="Tischnummer")
|
2017-02-06 20:27:11 +01:00
|
|
|
tst = models.DateTimeField(blank=False, null=False, verbose_name="Zeitstempel")
|
|
|
|
on_off = models.BooleanField(blank=False, null=False, verbose_name="Ein/Ausgebucht")
|
|
|
|
processed = models.BooleanField(default=False, verbose_name="Verarbeitet")
|
2017-02-06 20:29:35 +01:00
|
|
|
error_msg = models.TextField(blank=True, null=True, verbose_name="Fehlermeldung")
|
2017-01-03 19:26:51 +01:00
|
|
|
|
2017-01-06 18:53:26 +01:00
|
|
|
def __str__(self):
|
2017-02-10 20:49:38 +01:00
|
|
|
return str(self.client_id)
|
2017-01-06 18:53:26 +01:00
|
|
|
|
2017-02-06 19:19:28 +01:00
|
|
|
class Meta:
|
|
|
|
verbose_name = "Standortlog"
|
|
|
|
verbose_name_plural = "Standortlogs"
|
2017-01-03 20:13:47 +01:00
|
|
|
|
2017-02-10 18:37:36 +01:00
|
|
|
|
2017-02-01 13:51:15 +01:00
|
|
|
class Location(models.Model):
|
2017-02-06 20:27:11 +01:00
|
|
|
users = models.ManyToManyField(User, related_name='locations', verbose_name="Benutzer")
|
|
|
|
code = models.CharField(max_length=16, unique=True, verbose_name="Code")
|
|
|
|
name = models.CharField(max_length=64, unique=True, verbose_name="Name")
|
|
|
|
street = models.CharField(max_length=64, blank=True, null=True, verbose_name="Straße")
|
|
|
|
plz = models.CharField(max_length=8, blank=True, null=True, verbose_name="Postleitzahl")
|
|
|
|
city = models.CharField(max_length=64, blank=True, null=True, verbose_name="Stadt")
|
|
|
|
phone = models.CharField(max_length=64, blank=True, null=True, verbose_name="Telefon")
|
|
|
|
email = models.EmailField(blank=True, null=True, verbose_name="Email")
|
2017-02-25 18:34:28 +01:00
|
|
|
url = models.URLField(blank=True, null=True, verbose_name="URL")
|
|
|
|
happy_hour_start = models.TimeField(blank=True, null=True, verbose_name='Happy Hour Start')
|
|
|
|
happy_hour_end = models.TimeField(blank=True, null=True, verbose_name='Happy Hour Ende')
|
2017-01-03 19:26:51 +01:00
|
|
|
|
2017-01-06 18:53:26 +01:00
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
2017-02-06 19:19:28 +01:00
|
|
|
class Meta:
|
|
|
|
verbose_name = "Standort"
|
|
|
|
verbose_name_plural = "Standorte"
|
2017-01-06 18:53:26 +01:00
|
|
|
|
2017-01-03 20:13:47 +01:00
|
|
|
|
2017-02-01 13:51:15 +01:00
|
|
|
class Client(models.Model):
|
2017-02-06 20:27:11 +01:00
|
|
|
uuid = models.UUIDField(unique=True, default=uuid.uuid4, verbose_name="Identifier")
|
|
|
|
location = models.ForeignKey(Location, verbose_name="Standort")
|
2017-03-02 20:59:10 +01:00
|
|
|
report_user = models.ForeignKey(User, blank=True, null=True, verbose_name="Reporting Benutzer", related_name='reporting_clients')
|
2017-02-01 20:25:33 +01:00
|
|
|
|
|
|
|
def __str__(self):
|
2017-02-04 12:39:28 +01:00
|
|
|
return '{}, {}'.format(self.location.name, self.uuid)
|
2017-02-01 20:25:33 +01:00
|
|
|
|
2017-02-06 19:19:28 +01:00
|
|
|
class Meta:
|
|
|
|
verbose_name = "Client"
|
|
|
|
verbose_name_plural = "Clienten"
|
2017-02-01 20:25:33 +01:00
|
|
|
|
2017-02-10 18:37:36 +01:00
|
|
|
|
|
|
|
class Desk(models.Model):
|
2017-02-10 19:39:21 +01:00
|
|
|
client = models.ForeignKey(Client, verbose_name='Client', related_name='desks')
|
2017-02-10 18:37:36 +01:00
|
|
|
desk_no = models.IntegerField(verbose_name='Tischnummer')
|
|
|
|
name = models.CharField(max_length=32, blank=True, null=True, verbose_name='Tischbezeichnung')
|
|
|
|
enabled = models.BooleanField(verbose_name='Tisch aktiv')
|
2017-02-20 18:49:55 +01:00
|
|
|
prize = models.DecimalField(max_digits=5, decimal_places=2, default=0.0, verbose_name="Normalpreis")
|
2017-02-10 18:37:36 +01:00
|
|
|
prize_hh = models.DecimalField(max_digits=5, decimal_places=2, blank=True, null=True,
|
|
|
|
verbose_name="Preis Happy Hour")
|
|
|
|
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return '{}, {}'.format(self.client.uuid, self.name)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
verbose_name = "Tisch"
|
|
|
|
verbose_name_plural = "Tische"
|
|
|
|
|
|
|
|
|
2017-02-01 20:25:33 +01:00
|
|
|
class Accounting(models.Model):
|
2017-02-11 10:23:03 +01:00
|
|
|
desk = models.ForeignKey(Desk, verbose_name="Tisch")
|
2017-02-06 20:27:11 +01:00
|
|
|
time_from = models.DateTimeField(verbose_name="Beginn")
|
|
|
|
time_to = models.DateTimeField(blank=True, null=True, verbose_name="Ende")
|
|
|
|
prize = models.DecimalField(max_digits=5, decimal_places=2, blank=True, null=True, verbose_name="Preis")
|
2017-02-11 10:23:03 +01:00
|
|
|
billed = models.BooleanField(default=False, verbose_name="Abgerechnet")
|
2017-02-27 07:58:06 +01:00
|
|
|
reporter_uuid = models.UUIDField(blank=True, null=True, verbose_name='Reporter UUID')
|
2017-03-02 20:51:43 +01:00
|
|
|
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")
|
2017-02-01 20:25:33 +01:00
|
|
|
|
2017-01-06 18:53:26 +01:00
|
|
|
def __str__(self):
|
2017-02-11 10:23:03 +01:00
|
|
|
return '{}: {} -> {}, {}, {}'.format(self.desk, self.time_from, self.time_to, self.prize, self.billed)
|
2017-01-06 18:53:26 +01:00
|
|
|
|
2017-02-01 20:25:33 +01:00
|
|
|
class Meta:
|
|
|
|
ordering = ['-time_from']
|
2017-02-06 19:19:28 +01:00
|
|
|
verbose_name = "Buchhaltungseintrag"
|
2017-02-06 19:11:32 +01:00
|
|
|
verbose_name_plural = "Buchhaltungseinträge"
|
|
|
|
|
|
|
|
|
2017-02-10 20:49:38 +01:00
|
|
|
@receiver(post_save, sender=LocationData)
|
|
|
|
def test(sender, **kwargs):
|
2017-02-11 18:59:42 +01:00
|
|
|
request_process_location_data.delay()
|
2017-02-10 21:02:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
@shared_task
|
|
|
|
def process_location_data():
|
|
|
|
data = LocationData.objects.filter(processed=False)
|
|
|
|
for ld in data:
|
2017-02-11 11:00:14 +01:00
|
|
|
cli = Client.objects.filter(uuid=ld.client_id, desks__desk_no=ld.desk_no)
|
2017-02-10 21:02:07 +01:00
|
|
|
if cli.count() < 1:
|
|
|
|
ld.processed = True
|
2017-02-17 18:49:19 +01:00
|
|
|
ld.error_msg = 'No client object found. Stopp processing! {}, {}'.format(ld.client_id, ld.desk_no)
|
2017-02-10 21:02:07 +01:00
|
|
|
ld.save()
|
2017-02-26 12:03:18 +01:00
|
|
|
log.error(ld.error_msg)
|
2017-02-10 21:02:07 +01:00
|
|
|
else:
|
|
|
|
cli = cli[0]
|
2017-02-17 18:49:19 +01:00
|
|
|
desk = cli.desks.filter(desk_no=ld.desk_no, enabled=True)
|
|
|
|
if desk.count() != 1:
|
|
|
|
ld.processed = True
|
|
|
|
ld.error_msg = 'No desk object found. Stopp processing! {}, {}'.format(ld.client_id, ld.desk_no)
|
|
|
|
ld.save()
|
2017-02-26 12:03:18 +01:00
|
|
|
log.error(ld.error_msg)
|
2017-02-17 18:49:19 +01:00
|
|
|
desk = desk[0]
|
2017-02-11 10:23:03 +01:00
|
|
|
ac = desk.accounting_set.order_by('time_from').reverse()
|
2017-02-10 21:02:07 +01:00
|
|
|
if ld.on_off:
|
2017-02-17 18:56:40 +01:00
|
|
|
acc = None
|
2017-02-10 21:02:07 +01:00
|
|
|
if ac.count() > 0 and ac[0].time_to is None:
|
2017-02-26 12:03:18 +01:00
|
|
|
log.error('Vorheriges Accounting nicht abgeschlossen: Desk_id {}, Accounting_id {}'
|
|
|
|
.format(desk.id, ac[0].id))
|
2017-02-17 18:56:40 +01:00
|
|
|
acc = ac[0]
|
|
|
|
if acc is None:
|
|
|
|
acc = Accounting(
|
|
|
|
desk=desk,
|
|
|
|
time_from=ld.tst,
|
|
|
|
)
|
2017-02-10 21:02:07 +01:00
|
|
|
acc.save()
|
|
|
|
ld.delete()
|
|
|
|
else:
|
2017-02-26 12:37:47 +01:00
|
|
|
if len(ac) > 0:
|
2017-02-26 12:03:18 +01:00
|
|
|
acc = ac[0]
|
|
|
|
acc.time_to = ld.tst
|
2017-03-02 20:51:43 +01:00
|
|
|
acc.prize, acc.prize_normal, acc.prize_hh = utils.get_prize_for(
|
2017-02-26 12:03:18 +01:00
|
|
|
start=acc.time_from,
|
|
|
|
end=ld.tst,
|
|
|
|
pph=desk.prize,
|
|
|
|
hh_start=cli.location.happy_hour_start,
|
|
|
|
hh_end=cli.location.happy_hour_end,
|
|
|
|
pphh=desk.prize_hh,
|
|
|
|
)
|
2017-02-27 08:01:58 +01:00
|
|
|
acc.reporter_uuid = ld.client_id
|
2017-02-26 12:03:18 +01:00
|
|
|
acc.save()
|
|
|
|
ld.delete()
|
|
|
|
else:
|
|
|
|
ld.processed = True
|
|
|
|
ld.error_msg = 'No existing accountings found. Stopp processing! {}, {}'.format(ld.client_id, ld.desk_no)
|
|
|
|
ld.save()
|
2017-03-02 20:25:40 +01:00
|
|
|
log.error(ld.error_msg)
|