carom-server/billard/models.py

99 lines
3.9 KiB
Python
Raw Normal View History

2017-02-01 17:52:16 +01:00
import uuid
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-05 17:07:42 +01:00
from datetime import datetime
from . import utils
2017-02-06 19:11:32 +01:00
from . import tasks
from django.contrib.auth.models import User
from django.db.models.signals import post_save
2017-01-03 19:26:51 +01:00
2017-02-01 13:51:15 +01:00
class LocationData(models.Model):
2017-02-06 19:58:32 +01:00
location_id = models.UUIDField(blank=False, null=False)
table_no = models.IntegerField(blank=False, null=False)
tst = models.DateTimeField(blank=False, null=False)
on_off = models.BooleanField(blank=False, null=False)
processed = models.BooleanField(default=False)
error_msg = models.CharField(max_length=16000, blank=True, null=True)
2017-01-03 19:26:51 +01:00
2017-01-06 18:53:26 +01:00
def __str__(self):
2017-02-01 13:51:15 +01:00
return str(self.location_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-01 13:51:15 +01:00
class Location(models.Model):
2017-02-06 19:19:28 +01:00
users = models.ManyToManyField(User, related_name='locations', name="Benutzer")
code = models.CharField(max_length=16, unique=True, name="Code")
name = models.CharField(max_length=64, unique=True, name="Name")
street = models.CharField(max_length=64, blank=True, null=True, name="Straße")
plz = models.CharField(max_length=8, blank=True, null=True, name="Postleitzahl")
city = models.CharField(max_length=64, blank=True, null=True, name="Stadt")
phone = models.CharField(max_length=64, blank=True, null=True, name="Telefon")
email = models.EmailField(blank=True, null=True, name="Email")
url = models.URLField(blank=True,null=True, name="URL")
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 19:19:28 +01:00
uuid = models.UUIDField(unique=True, default=uuid.uuid4, name="Identifier")
location = models.ForeignKey(Location, name="Standort")
2017-02-01 13:51:15 +01:00
desk1_enable = models.BooleanField()
desk1_name = models.CharField(max_length=32, blank=True, null=True)
desk1_prize_nt = models.DecimalField(max_digits=4, decimal_places=2, blank=True, null=True)
desk1_prize_ht = models.DecimalField(max_digits=4, decimal_places=2, blank=True, null=True)
desk2_enable = models.BooleanField()
desk2_name = models.CharField(max_length=32, blank=True, null=True)
desk2_prize_nt = models.DecimalField(max_digits=4, decimal_places=2, blank=True, null=True)
desk2_prize_ht = models.DecimalField(max_digits=4, decimal_places=2, blank=True, null=True)
2017-01-06 18:53:26 +01:00
2017-02-05 17:07:42 +01:00
def accounting_for(self, desk_no, pht, pnt):
t = Accounting.objects.filter(client_id=self.id, desk_no=desk_no)[:3][::-1]
a = t[t.__len__() - 1]
if a.time_to is None:
prize = utils.get_prize_for(start=a.time_from, end=datetime.now(), pph=pht)
if prize != a.prize:
a.prize = prize
return t
2017-02-01 20:25:33 +01:00
def accounting_1(self):
2017-02-05 17:07:42 +01:00
return self.accounting_for(1, self.desk1_prize_ht, self.desk1_prize_nt)
2017-02-01 20:25:33 +01:00
def accounting_2(self):
2017-02-05 17:07:42 +01:00
return self.accounting_for(2, self.desk2_prize_ht, self.desk2_prize_nt)
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
class Accounting(models.Model):
2017-02-06 19:19:28 +01:00
client = models.ForeignKey(Client, name="Client")
desk_no = models.IntegerField(name="Tischnummer")
time_from = models.DateTimeField(name="Beginn")
time_to = models.DateTimeField(blank=True, null=True, name="Ende")
prize = models.DecimalField(max_digits=5, decimal_places=2, blank=True, null=True, name="Preis")
2017-02-01 20:25:33 +01:00
2017-01-06 18:53:26 +01:00
def __str__(self):
2017-02-04 12:39:28 +01:00
return '{}: {} -> {}, {}'.format(self.client.uuid, self.time_from, self.time_to, self.prize)
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"
def update_location_data(sender, instance, **kwargs):
tasks.process_location_data.delay()
post_save.connect(update_location_data, sender=LocationData)