Brak opisu

models.py 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. from __future__ import annotations
  2. from django.db import models
  3. from django.utils import timezone
  4. # Import domain models with light coupling
  5. from orgs.models import Organization
  6. from recycle_core.models import Customer, PickupOrder, Material
  7. class TimestampedModel(models.Model):
  8. created_at = models.DateTimeField(auto_now_add=True)
  9. updated_at = models.DateTimeField(auto_now=True)
  10. class Meta:
  11. abstract = True
  12. class Invoice(TimestampedModel):
  13. STATUS_DRAFT = "draft"
  14. STATUS_ISSUED = "issued"
  15. STATUS_PAID = "paid"
  16. STATUS_VOID = "void"
  17. STATUS_CHOICES = (
  18. (STATUS_DRAFT, "Draft"),
  19. (STATUS_ISSUED, "Issued"),
  20. (STATUS_PAID, "Paid"),
  21. (STATUS_VOID, "Void"),
  22. )
  23. organization = models.ForeignKey(Organization, on_delete=models.CASCADE, related_name="invoices")
  24. customer = models.ForeignKey(Customer, on_delete=models.PROTECT, related_name="invoices")
  25. pickup = models.ForeignKey(PickupOrder, on_delete=models.SET_NULL, null=True, blank=True, related_name="invoices")
  26. currency_code = models.CharField(max_length=8, default="USD")
  27. total_amount = models.DecimalField(max_digits=14, decimal_places=2, default=0)
  28. status = models.CharField(max_length=8, choices=STATUS_CHOICES, default=STATUS_DRAFT, db_index=True)
  29. issued_at = models.DateTimeField(null=True, blank=True)
  30. due_at = models.DateTimeField(null=True, blank=True)
  31. class InvoiceLine(TimestampedModel):
  32. invoice = models.ForeignKey(Invoice, on_delete=models.CASCADE, related_name="lines")
  33. description = models.CharField(max_length=255)
  34. material = models.ForeignKey(Material, on_delete=models.SET_NULL, null=True, blank=True)
  35. quantity = models.DecimalField(max_digits=12, decimal_places=3)
  36. unit = models.CharField(max_length=8, choices=Material.UNIT_CHOICES, default=Material.UNIT_KG)
  37. unit_price = models.DecimalField(max_digits=12, decimal_places=2)
  38. line_total = models.DecimalField(max_digits=14, decimal_places=2)
  39. class Payment(TimestampedModel):
  40. invoice = models.ForeignKey(Invoice, on_delete=models.CASCADE, related_name="payments")
  41. amount = models.DecimalField(max_digits=14, decimal_places=2)
  42. currency_code = models.CharField(max_length=8, default="USD")
  43. received_at = models.DateTimeField(default=timezone.now)
  44. reference = models.CharField(max_length=128, blank=True)
  45. class Payout(TimestampedModel):
  46. organization = models.ForeignKey(Organization, on_delete=models.CASCADE, related_name="payouts")
  47. customer = models.ForeignKey(Customer, on_delete=models.PROTECT, related_name="payouts")
  48. pickup = models.ForeignKey(PickupOrder, on_delete=models.SET_NULL, null=True, blank=True, related_name="payouts")
  49. amount = models.DecimalField(max_digits=14, decimal_places=2)
  50. currency_code = models.CharField(max_length=8, default="USD")
  51. paid_at = models.DateTimeField(default=timezone.now)
  52. reference = models.CharField(max_length=128, blank=True)