Document.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace App;
  3. use App\Enums\DocumentTypes;
  4. use App\Enums\InvoiceStatuses;
  5. use App\Traits\DateTimeTrait;
  6. use App\Traits\SoftDeleteTrait;
  7. use Exception;
  8. use NumberFormatter;
  9. use RuntimeException;
  10. /**
  11. * @property int $id
  12. * @property int $user_id User who created this document
  13. * @property int $sale_id Sale that this document relates to
  14. * @property int $supplier_id Used for goods-in documents
  15. * @property int $location_id
  16. * @property int $related_document_id Used to link a delivery note to an invoice
  17. * @property int $type
  18. * @property int $invoice_status
  19. * @property string $currency
  20. * @property string $tax_rate
  21. * @property string $billing_address
  22. * @property string $delivery_address
  23. * @property int $number
  24. * @property string $date
  25. * @property string $po_number Used for invoices
  26. * @property string $terms Used for invoices
  27. * @property string $delivery_company
  28. * @property string $tracking_number
  29. * @property string $delivery_cost
  30. * @property string $notes
  31. * @property string $dts_finalised
  32. * @property string $dts_paid
  33. */
  34. class Document extends Model {
  35. protected static $table = "document";
  36. protected static $idField = "id";
  37. protected static $fillableFields = [
  38. "user_id", "sale_id", "supplier_id", "location_id", "related_document_id",
  39. "type",
  40. "invoice_status",
  41. "currency", "tax_rate",
  42. "billing_address", "delivery_address",
  43. "number", "date", "po_number", "terms",
  44. "delivery_company", "tracking_number", "delivery_cost",
  45. "notes",
  46. "dts_finalised", "dts_paid",
  47. "dts_created", "dts_updated", "dts_deleted"
  48. ];
  49. use DateTimeTrait;
  50. use SoftDeleteTrait;
  51. // Static methods
  52. /**
  53. * @param int $document_type
  54. * @return int
  55. * @throws Exception
  56. */
  57. public static function getNextNumber(int $document_type): int {
  58. $document = self::queryOne(["type" => $document_type], ["sort" => "id", "order" => "desc"]);
  59. if($document) {
  60. return $document->number + 1;
  61. }
  62. return 1000;
  63. }
  64. // Object methods
  65. /**
  66. * @return Sale|null
  67. * @throws Exception
  68. */
  69. public function getSale(): ?Sale {
  70. return Sale::get($this->sale_id);
  71. }
  72. /**
  73. * @return string
  74. * @throws Exception
  75. */
  76. public function getUrl(): string {
  77. if($this->isInvoice()) {
  78. /** @var Sale $sale */
  79. $sale = $this->getSale();
  80. return sprintf("%s/invoices/%s", $sale->getUrl(), $this->number);
  81. }
  82. if($this->isDeliveryNote()) {
  83. $sale = $this->getSale();
  84. return sprintf("%s/delivery-notes/%s", $sale->getUrl(), $this->number);
  85. }
  86. throw new RuntimeException(__("Unimplemented getUrl for document type: $this->type"));
  87. }
  88. public function isInvoice(): bool {
  89. return (int) $this->type === DocumentTypes::INVOICE;
  90. }
  91. public function isDeliveryNote(): bool {
  92. return (int) $this->type === DocumentTypes::DELIVERY_NOTE;
  93. }
  94. public function isGoodsIn(): bool {
  95. return (int) $this->type === DocumentTypes::GOODS_IN;
  96. }
  97. public function getDisplayInvoiceStatus(): string {
  98. return InvoiceStatuses::enumToText($this->invoice_status);
  99. }
  100. public function getDisplayTotal(): string {
  101. $formatter = new NumberFormatter("en_GB", NumberFormatter::CURRENCY);
  102. $subtotal = "0.00";
  103. foreach($this->getItems() as $item) {
  104. $subtotal = bcadd($item->getAmount(), $subtotal, 2);
  105. }
  106. $tax = bcmul($subtotal, $this->tax_rate / 100, 2);
  107. $total = bcadd($subtotal, $tax, 2);
  108. return $formatter->formatCurrency($total, $this->currency);
  109. }
  110. /**
  111. * @return DocumentItem[]
  112. */
  113. public function getItems(array $nvp = []): array {
  114. $nvp = array_merge($nvp, [
  115. "document_id" => $this->id,
  116. "dts_deleted" => null
  117. ]);
  118. return DocumentItem::query($nvp);
  119. }
  120. public function getTitle(): string {
  121. if($this->isInvoice()) {
  122. return __("Invoice %d", $this->number);
  123. }
  124. if($this->isDeliveryNote()) {
  125. return __("Delivery Note %d", $this->number);
  126. }
  127. return __("Document %d", $this->number);
  128. }
  129. /**
  130. * @return Document|null
  131. * @throws Exception
  132. */
  133. public function getRelatedDocument(): ?Document {
  134. return self::get($this->related_document_id);
  135. }
  136. /**
  137. * @return User
  138. * @throws Exception
  139. */
  140. public function getUser(): User {
  141. return User::get($this->user_id);
  142. }
  143. }