2021-03-21 15:50:50 +00:00
|
|
|
from django.contrib.gis.db import models
|
2021-04-19 10:51:50 +00:00
|
|
|
from django.utils import timezone
|
2021-05-30 12:19:40 +00:00
|
|
|
from django.contrib.gis.db.models.functions import Distance
|
2021-05-31 06:35:17 +00:00
|
|
|
from django.dispatch import receiver
|
|
|
|
from django.db.models.signals import post_save
|
2021-03-21 15:50:50 +00:00
|
|
|
|
2021-03-24 06:43:05 +00:00
|
|
|
from clients.models import ClientProfile
|
2021-04-20 06:36:59 +00:00
|
|
|
from partners.models import Establishment, RoomCategory
|
|
|
|
|
2021-05-30 15:47:56 +00:00
|
|
|
from dbsettings.functions import getValue
|
|
|
|
|
|
|
|
from datetime import timedelta
|
2021-03-21 15:50:50 +00:00
|
|
|
|
2021-04-08 11:38:10 +00:00
|
|
|
import uuid
|
|
|
|
|
2021-04-18 06:12:15 +00:00
|
|
|
class LengthChoices(models.IntegerChoices):
|
|
|
|
ANY = 0
|
|
|
|
SHORT = 1
|
|
|
|
LONG = 2
|
|
|
|
|
2021-03-21 15:50:50 +00:00
|
|
|
class Inquiry(models.Model):
|
2021-04-08 11:38:10 +00:00
|
|
|
uuid = models.UUIDField(default=uuid.uuid4, unique=True)
|
2021-04-08 09:30:19 +00:00
|
|
|
client = models.ForeignKey(ClientProfile, models.PROTECT, null=True, blank=True)
|
2021-03-21 15:50:50 +00:00
|
|
|
destination_name = models.CharField(max_length=128)
|
|
|
|
destination_coords = models.PointField()
|
|
|
|
destination_radius = models.IntegerField()
|
2021-03-24 09:52:56 +00:00
|
|
|
arrival = models.DateField()
|
2021-04-18 06:12:15 +00:00
|
|
|
min_nights = models.IntegerField(default=0, choices=LengthChoices.choices)
|
2021-03-21 15:50:50 +00:00
|
|
|
budget = models.DecimalField(max_digits=10, decimal_places=2)
|
|
|
|
adults = models.IntegerField()
|
|
|
|
children = models.IntegerField()
|
2021-03-24 09:44:52 +00:00
|
|
|
comment = models.TextField(null=True, blank=True)
|
2021-04-19 10:51:50 +00:00
|
|
|
activated = models.DateTimeField(null=True, blank=True)
|
2021-05-30 15:47:56 +00:00
|
|
|
bidding_end = models.DateTimeField(null=True, blank=True)
|
2021-10-28 07:26:17 +00:00
|
|
|
auction_end = models.DateTimeField(null=True, blank=True)
|
2021-05-31 06:35:17 +00:00
|
|
|
expiry = models.DateTimeField(null=True, blank=True)
|
2021-04-18 10:02:17 +00:00
|
|
|
gateway = models.CharField(max_length=128, null=True, blank=True)
|
2021-03-21 15:50:50 +00:00
|
|
|
|
2021-03-24 06:43:05 +00:00
|
|
|
@property
|
|
|
|
def is_paid(self):
|
2021-03-24 06:43:47 +00:00
|
|
|
if not self.invoice:
|
2021-03-24 06:43:05 +00:00
|
|
|
return False
|
|
|
|
|
2021-03-24 06:43:47 +00:00
|
|
|
return self.invoice.is_paid
|
2021-03-24 06:43:05 +00:00
|
|
|
|
2021-05-30 15:47:56 +00:00
|
|
|
def activate(self):
|
|
|
|
if self.activated:
|
|
|
|
return False
|
|
|
|
|
|
|
|
self.activated = timezone.now()
|
2021-10-28 07:26:17 +00:00
|
|
|
self.bidding_end = self.activated + timedelta(hours=int(getValue("auction.bidding_period", 24)))
|
|
|
|
self.auction_end = self.bidding_end + timedelta(hours=int(getValue("auction.selection_period", 24)))
|
2021-05-30 15:47:56 +00:00
|
|
|
self.save()
|
|
|
|
|
2021-04-19 10:51:50 +00:00
|
|
|
def process_payment(self):
|
2021-05-27 03:42:19 +00:00
|
|
|
if self.invoice.is_paid:
|
2021-05-30 15:47:56 +00:00
|
|
|
self.activate()
|
2021-04-20 06:36:59 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def expired(self):
|
|
|
|
return self.expiry and (self.expiry < timezone.now())
|
|
|
|
|
2021-10-28 07:26:17 +00:00
|
|
|
@property
|
|
|
|
def in_bidding(self):
|
2021-11-13 15:47:58 +00:00
|
|
|
return self.active and (self.bidding_end > timezone.now())
|
2021-10-28 07:26:17 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def in_selection(self):
|
2021-11-13 15:47:58 +00:00
|
|
|
return self.active and (self.auction_end > timezone.now()) and not self.in_bidding
|
2021-10-28 07:26:17 +00:00
|
|
|
|
2021-04-19 10:51:50 +00:00
|
|
|
@property
|
|
|
|
def active(self):
|
|
|
|
try:
|
2021-04-20 06:36:59 +00:00
|
|
|
return bool(self.activated) and not self.expired
|
2021-04-19 10:51:50 +00:00
|
|
|
except:
|
|
|
|
return False
|
|
|
|
|
2021-03-24 09:44:52 +00:00
|
|
|
@property
|
|
|
|
def accepted(self):
|
2021-04-20 11:37:36 +00:00
|
|
|
try:
|
|
|
|
return Offer.objects.get(inquiry=self, accepted__isnull=False)
|
|
|
|
except Offer.DoesNotExist:
|
|
|
|
return False
|
2021-03-24 09:44:52 +00:00
|
|
|
|
2021-03-24 06:43:05 +00:00
|
|
|
class Offer(models.Model):
|
2021-04-20 06:36:59 +00:00
|
|
|
uuid = models.UUIDField(default=uuid.uuid4, unique=True)
|
2021-03-24 06:43:05 +00:00
|
|
|
inquiry = models.ForeignKey(Inquiry, models.PROTECT)
|
2021-04-20 06:36:59 +00:00
|
|
|
roomcategory = models.ForeignKey(RoomCategory, models.PROTECT)
|
|
|
|
departure = models.DateField()
|
2021-03-24 09:44:52 +00:00
|
|
|
comment = models.TextField(null=True, blank=True)
|
2021-04-20 11:37:36 +00:00
|
|
|
accepted = models.DateTimeField(null=True, blank=True)
|
2021-04-20 06:36:59 +00:00
|
|
|
hidden = models.BooleanField(default=False)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def establishment(self):
|
|
|
|
return self.roomcategory.establishment
|
|
|
|
|
|
|
|
@property
|
|
|
|
def nights(self):
|
2021-05-30 12:19:40 +00:00
|
|
|
return (self.departure - self.inquiry.arrival).days
|
|
|
|
|
|
|
|
@property
|
|
|
|
def distance(self):
|
2021-05-31 06:35:17 +00:00
|
|
|
return self.inquiry.destination_coords.distance(self.roomcategory.establishment.coords)
|
|
|
|
|
|
|
|
@receiver(post_save, sender=Inquiry)
|
|
|
|
def inquiry_expiry(sender, instance, created, **kwargs):
|
|
|
|
if not instance.expiry:
|
2021-10-28 07:26:17 +00:00
|
|
|
instance.expiry = min(instance.arrival, (timezone.now() + timedelta(hours=int(getValue("auction.payment_period", 168)))).date())
|
2021-11-13 15:47:58 +00:00
|
|
|
instance.save()
|