説明なし

models.py 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. from django.db import models
  2. from django_google_maps import fields as map_fields
  3. from colorfield.fields import ColorField
  4. from smart_selects.db_fields import (
  5. ChainedForeignKey,
  6. ChainedManyToManyField,
  7. GroupedForeignKey,
  8. )
  9. from django.db.models import Q
  10. import googlemaps
  11. from django.contrib.gis.geos import fromstr
  12. from django.conf import settings
  13. import csv
  14. import haversine as hs
  15. from mptt.models import MPTTModel, TreeForeignKey
  16. from django.contrib.auth.models import User
  17. from django.db.models.signals import post_save
  18. from django.dispatch import receiver
  19. from shaqfindbed.utils import get_current_user
  20. import decimal
  21. from django.utils.html import escape, format_html
  22. # Create your models here.
  23. VAT = 0.07
  24. class GenericModel(models.Model):
  25. created_at = models.DateTimeField(auto_now_add=True, null=True)
  26. updated_at = models.DateTimeField(auto_now=True)
  27. created_by = models.ForeignKey(User, null=True, editable=False, related_name='%(class)s_created', on_delete=models.SET_NULL)
  28. modified_by = models.ForeignKey(User, null=True, editable=False, related_name='%(class)s_modified', on_delete=models.SET_NULL)
  29. '''
  30. def save(self, *args, **kwargs):
  31. user = get_current_user()
  32. print("user "+user.is_authenticated())
  33. if user and user.is_authenticated():
  34. self.modified_by = user
  35. if not self.id:
  36. self.created_by = user
  37. super(GenericModel, self).save(*args, **kwargs)
  38. '''
  39. class Meta:
  40. abstract = True
  41. class StoreCat(MPTTModel):
  42. name = models.CharField(max_length=50, unique=True)
  43. parent = TreeForeignKey('self', on_delete=models.CASCADE, null=True, blank=True, related_name='children')
  44. class MPTTMeta:
  45. order_insertion_by = ['name']
  46. def __str__(self):
  47. return f"{self.name}"
  48. class Store(GenericModel, models.Model):
  49. logo = models.ImageField(upload_to="uploads/%Y/%m/%d/", blank=False, null=True)
  50. name = models.CharField(max_length=200)
  51. summary = models.TextField(blank=True, null=True)
  52. address_text = models.TextField(blank=True, null=True)
  53. #address = models.CharField(max_length=100)
  54. address = map_fields.AddressField(max_length=200)
  55. geolocation = map_fields.GeoLocationField(max_length=100)
  56. tel = models.CharField(max_length=100, null=True, blank=True)
  57. line_id = models.CharField(max_length=100, null=True, blank=True)
  58. email = models.EmailField(null=True, blank=True)
  59. store_cat = TreeForeignKey('StoreCat', on_delete=models.SET_NULL, null=True)
  60. def logo_tag(self):
  61. return format_html('<img src="%s" width="300px"/>' % escape(self.logo.url))
  62. logo_tag.short_description = 'Image'
  63. logo_tag.allow_tags = True
  64. #created_at = models.DateTimeField(auto_now_add=True, null=True)
  65. #updated_at = models.DateTimeField(auto_now=True)
  66. def __str__(self):
  67. return f"{self.name}"
  68. class ProductType(MPTTModel):
  69. name = models.CharField(max_length=50, unique=True)
  70. parent = TreeForeignKey('self', on_delete=models.CASCADE, null=True, blank=True, related_name='children')
  71. class MPTTMeta:
  72. order_insertion_by = ['name']
  73. def __str__(self):
  74. return f"{self.name}"
  75. class Buyer(GenericModel, models.Model):
  76. name = models.CharField(max_length=300, null=False, blank=False)
  77. address_text = models.TextField(blank=False, null=False)
  78. address = map_fields.AddressField(max_length=200)
  79. geolocation = map_fields.GeoLocationField(max_length=100)
  80. tel = models.CharField(max_length=100, null=False, blank=False)
  81. line_id = models.CharField(max_length=100, null=True, blank=True)
  82. email = models.EmailField(null=True, blank=True)
  83. def __str__(self):
  84. return f"{self.name} Tel.:{self.tel} Email:{self.email}"
  85. class Inbox(GenericModel, models.Model):
  86. store = models.ForeignKey('Store', on_delete=models.SET_NULL, null=True, blank=False)
  87. product = models.ForeignKey('Product', on_delete=models.SET_NULL, null=True, blank=False)
  88. buyer = models.ForeignKey('Buyer', on_delete=models.SET_NULL, null=True, blank=True)
  89. subject = models.CharField(max_length=200)
  90. body = models.TextField(blank=False)
  91. tel = models.CharField(max_length=100, null=True, blank=True)
  92. line_id = models.CharField(max_length=100, null=True, blank=True)
  93. email = models.EmailField(null=True, blank=True)
  94. status = models.CharField(
  95. max_length=30,
  96. choices=(("request", "Request"), ("read", "Read"), ("process", "Process"), ("complete", "Complete")),
  97. default="request",
  98. )
  99. class Purchase(GenericModel, models.Model):
  100. product = models.ForeignKey('Product', on_delete=models.DO_NOTHING, null=False, blank=False)
  101. store = models.ForeignKey('Store', on_delete=models.DO_NOTHING, null=False, blank=False)
  102. vendor = models.ForeignKey('Vendor', on_delete=models.DO_NOTHING, null=False, blank=False)
  103. price = models.DecimalField(null=True, blank=True, decimal_places=2, max_digits=7)
  104. n_unit = models.DecimalField(blank=True, decimal_places=2, max_digits=7, null=True)
  105. unit_name = models.CharField(max_length=200, null=True)
  106. sub_total = models.DecimalField(null=True, blank=True, decimal_places=2, max_digits=10)
  107. vat = models.DecimalField(null=True, blank=True, decimal_places=2, max_digits=10)
  108. total = models.DecimalField(null=True, blank=True, decimal_places=2, max_digits=10)
  109. def save(self, *args, **kwargs):
  110. self.sub_total = self.price * self.n_unit
  111. self.vat = self.sub_total * decimal.Decimal(VAT)
  112. self.total = self.sub_total + self.vat
  113. super(Purchase, self).save(*args, **kwargs)
  114. class Sale(GenericModel, models.Model):
  115. product = models.ForeignKey('Product', on_delete=models.DO_NOTHING, null=False, blank=False)
  116. store = models.ForeignKey('Store', on_delete=models.DO_NOTHING, null=False, blank=False)
  117. sku = models.ForeignKey('ProductSKU', on_delete=models.DO_NOTHING, null=False, blank=False)
  118. price = models.DecimalField(null=True, blank=True, decimal_places=2, max_digits=7)
  119. n_unit = models.DecimalField(blank=True, decimal_places=2, max_digits=7, null=True)
  120. unit_name = models.CharField(max_length=200, null=True)
  121. buyer = models.ForeignKey('Buyer', on_delete=models.DO_NOTHING, null=False, blank=False)
  122. sub_total = models.DecimalField(null=True, blank=True, decimal_places=2, max_digits=10)
  123. vat = models.DecimalField(null=True, blank=True, decimal_places=2, max_digits=10)
  124. total = models.DecimalField(null=True, blank=True, decimal_places=2, max_digits=10)
  125. def save(self, *args, **kwargs):
  126. self.sub_total = self.price * self.n_unit
  127. self.vat = self.sub_total * decimal.Decimal(VAT)
  128. self.total = self.sub_total + self.vat
  129. super(Sale, self).save(*args, **kwargs)
  130. class Product(GenericModel, models.Model ):
  131. name = models.CharField(max_length=200)
  132. code = models.CharField(max_length=200)
  133. product_type = TreeForeignKey('ProductType', on_delete=models.SET_NULL, null=True)
  134. description = models.TextField(blank=True, null=True)
  135. store = models.ForeignKey('Store', on_delete=models.CASCADE, null=True, blank=True)
  136. price = models.DecimalField(null=True, blank=True, decimal_places=2, max_digits=10)
  137. details = models.JSONField(null=True, blank=True)
  138. n_unit = models.DecimalField(blank=True, decimal_places=2, max_digits=7, null=True)
  139. unit_name = models.CharField(max_length=200, null=True)
  140. def __str__(self):
  141. return f"{self.name} {self.code}"
  142. class VendorProduct(GenericModel, models.Model):
  143. product = models.ForeignKey('Product', on_delete=models.CASCADE)
  144. vendor = models.ForeignKey('Vendor', on_delete=models.CASCADE)
  145. price = models.DecimalField(null=True, blank=True, decimal_places=2, max_digits=7)
  146. n_unit = models.DecimalField(blank=True, decimal_places=2, max_digits=7, null=True)
  147. unit_name = models.CharField(max_length=200, null=True)
  148. def __str__(self):
  149. return f"{self.product.name} {self.price}/{self.unit_name}"
  150. class Vendor(GenericModel, models.Model ):
  151. name = models.CharField(max_length=200)
  152. code = models.CharField(max_length=200)
  153. #product_type = TreeForeignKey('ProductType', on_delete=models.SET_NULL, null=True)
  154. #product = models.ForeignKey('Product', on_delete=models.CASCADE)
  155. #products = models.ManyToManyField('Product',)
  156. description = models.TextField(blank=True, null=True)
  157. store = models.ForeignKey('Store', on_delete=models.CASCADE, null=True, blank=False)
  158. price = models.DecimalField(null=True, blank=True, decimal_places=2, max_digits=10)
  159. details = models.JSONField(null=True, blank=True)
  160. n_unit = models.DecimalField(blank=True, decimal_places=2, max_digits=7, null=True)
  161. unit_name = models.CharField(max_length=200, null=True)
  162. tel = models.CharField(max_length=100, null=True, blank=True)
  163. line_id = models.CharField(max_length=100, null=True, blank=True)
  164. email = models.EmailField(null=True, blank=True)
  165. address_text = models.TextField(blank=False, null=False)
  166. address = map_fields.AddressField(max_length=200)
  167. geolocation = map_fields.GeoLocationField(max_length=100)
  168. def __str__(self):
  169. return f"{self.name} {self.code}"
  170. class VendorOrder(GenericModel, models.Model):
  171. vendor = models.ForeignKey('Vendor', on_delete=models.CASCADE, null=False, blank=False)
  172. product = ChainedForeignKey(
  173. "VendorProduct",
  174. chained_field="vendor",
  175. chained_model_field="vendor",
  176. show_all=False,
  177. auto_choose=True,
  178. null=True
  179. )
  180. store = models.ForeignKey('Store', on_delete=models.CASCADE, null=False, blank=False)
  181. price = models.DecimalField(null=True, blank=True, decimal_places=2, max_digits=10)
  182. n_unit = models.DecimalField(blank=True, decimal_places=2, max_digits=7, null=True)
  183. unit_name = models.CharField(max_length=200, null=True)
  184. sub_total = models.DecimalField(null=True, blank=True, decimal_places=2, max_digits=10)
  185. vat = models.DecimalField(null=True, blank=True, decimal_places=2, max_digits=10)
  186. total = models.DecimalField(null=True, blank=True, decimal_places=2, max_digits=10)
  187. def save(self, *args, **kwargs):
  188. self.sub_total = self.price * self.n_unit
  189. self.vat = self.sub_total * decimal.Decimal(VAT)
  190. self.total = self.sub_total + self.vat
  191. super(VendorOrder, self).save(*args, **kwargs)
  192. class ProductSKU(GenericModel, models.Model ):
  193. sku = models.CharField(max_length=200)
  194. product = models.ForeignKey('Product', on_delete=models.CASCADE, null=True)
  195. description = models.TextField(blank=True, null=True)
  196. price = models.DecimalField(null=True, blank=True, decimal_places=2, max_digits=7)
  197. details = models.JSONField(null=True, blank=True)
  198. n_unit = models.DecimalField(blank=True, decimal_places=2, max_digits=7, null=True)
  199. unit_name = models.CharField(max_length=200, null=True)
  200. def __str__(self):
  201. return f"{self.sku} {self.product} @{self.price}(THB)"
  202. class Photo(GenericModel, models.Model):
  203. name = models.CharField(max_length=200, blank=True)
  204. order_n = models.IntegerField(default=0, blank=True)
  205. photo = models.ImageField(upload_to="uploads/%Y/%m/%d/", blank=False, verbose_name="Photo")
  206. product = models.ForeignKey('Product', on_delete=models.CASCADE, null=True)
  207. class Profile(models.Model):
  208. user = models.OneToOneField(User, on_delete=models.CASCADE)
  209. bio = models.TextField(max_length=500, blank=True)
  210. location = models.CharField(max_length=30, blank=True)
  211. roles = models.CharField(
  212. max_length=30,
  213. choices=(("seller", "Seller"), ("buyer", "Buyer"),),
  214. default="seller",
  215. )
  216. tel = models.CharField(max_length=100, null=True, blank=False)
  217. line_id = models.CharField(max_length=100, null=True, blank=False)
  218. facebook = models.CharField(max_length=200, null=True, blank=False)
  219. birth_date = models.DateField(null=True, blank=True)
  220. @receiver(post_save, sender=User)
  221. def create_user_profile(sender, instance, created, **kwargs):
  222. if created:
  223. store = Store()
  224. store.name = "Unname"
  225. store.created_by = instance
  226. store.save()
  227. Profile.objects.create(user=instance)
  228. @receiver(post_save, sender=User)
  229. def save_user_profile(sender, instance, **kwargs):
  230. try:
  231. instance.profile.save()
  232. except:
  233. Profile.objects.create(user=instance)