carom-server/billard/models.py

90 lines
3.4 KiB
Python

import uuid
from django.db import models
from django.contrib.auth.models import User
from datetime import datetime
from . import utils
class LocationData(models.Model):
location_id = models.CharField(max_length=32, 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)
def __str__(self):
return str(self.location_id)
def __unicode__(self):
return str(self.location_id)
class Location(models.Model):
users = models.ManyToManyField(User, related_name='locations')
code = models.CharField(max_length=16, unique=True)
name = models.CharField(max_length=64, unique=True)
street = models.CharField(max_length=64, blank=True, null=True)
plz = models.CharField(max_length=8, blank=True, null=True)
city = models.CharField(max_length=64, blank=True, null=True)
phone = models.CharField(max_length=64, blank=True, null=True)
email = models.EmailField(blank=True, null=True)
url = models.URLField(blank=True,null=True)
def __str__(self):
return self.name
def __unicode__(self):
return self.name
class Client(models.Model):
uuid = models.UUIDField(unique=True, default=uuid.uuid4)
location = models.ForeignKey(Location)
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)
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
def accounting_1(self):
return self.accounting_for(1, self.desk1_prize_ht, self.desk1_prize_nt)
def accounting_2(self):
return self.accounting_for(2, self.desk2_prize_ht, self.desk2_prize_nt)
def __str__(self):
return '{}, {}'.format(self.location.name, self.uuid)
def __unicode__(self):
return '{}, {}'.format(self.location.name, self.uuid)
class Accounting(models.Model):
client = models.ForeignKey(Client)
desk_no = models.IntegerField()
time_from = models.DateTimeField()
time_to = models.DateTimeField(blank=True, null=True)
prize = models.DecimalField(max_digits=5, decimal_places=2, blank=True, null=True)
def __str__(self):
return '{}: {} -> {}, {}'.format(self.client.uuid, self.time_from, self.time_to, self.prize)
def __unicode__(self):
return '{}: {} -> {}, {}'.format(self.client.uuid, self.time_from, self.time_to, self.prize)
class Meta:
ordering = ['-time_from']