Document.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 Supplier|null
  74. * @throws Exception
  75. */
  76. public function getSupplier(): ?Supplier {
  77. return Supplier::get($this->supplier_id);
  78. }
  79. /**
  80. * @return Location|null
  81. * @throws Exception
  82. */
  83. public function getLocation(): ?Location {
  84. return Location::get($this->location_id);
  85. }
  86. /**
  87. * @return string
  88. * @throws Exception
  89. */
  90. public function getUrl(): string {
  91. if($this->isInvoice()) {
  92. /** @var Sale $sale */
  93. $sale = $this->getSale();
  94. return sprintf("%s/invoices/%s", $sale->getUrl(), $this->number);
  95. }
  96. if($this->isDeliveryNote()) {
  97. $sale = $this->getSale();
  98. return sprintf("%s/delivery-notes/%s", $sale->getUrl(), $this->number);
  99. }
  100. if($this->isGoodsIn()) {
  101. return sprintf("/goods-in/%s", $this->number);
  102. }
  103. throw new RuntimeException(__("Unimplemented getUrl for document type: $this->type"));
  104. }
  105. public function isInvoice(): bool {
  106. return (int) $this->type === DocumentTypes::INVOICE;
  107. }
  108. public function isDeliveryNote(): bool {
  109. return (int) $this->type === DocumentTypes::DELIVERY_NOTE;
  110. }
  111. public function isGoodsIn(): bool {
  112. return (int) $this->type === DocumentTypes::GOODS_IN;
  113. }
  114. public function getDisplayInvoiceStatus(): string {
  115. return InvoiceStatuses::enumToText($this->invoice_status);
  116. }
  117. public function getDisplayTotal(): string {
  118. $formatter = new NumberFormatter("en_GB", NumberFormatter::CURRENCY);
  119. $subtotal = "0.00";
  120. foreach($this->getItems() as $item) {
  121. $subtotal = bcadd($item->getAmount(), $subtotal, 2);
  122. }
  123. $tax = bcmul($subtotal, $this->tax_rate / 100, 2);
  124. $total = bcadd($subtotal, $tax, 2);
  125. return $formatter->formatCurrency($total, $this->currency);
  126. }
  127. /**
  128. * @return DocumentItem[]
  129. */
  130. public function getItems(array $nvp = []): array {
  131. $nvp = array_merge($nvp, [
  132. "document_id" => $this->id,
  133. "dts_deleted" => null
  134. ]);
  135. return DocumentItem::query($nvp);
  136. }
  137. public function getTitle(): string {
  138. return __("%s %d", $this->getDisplayType(), $this->number);
  139. }
  140. /**
  141. * @return Document|null
  142. * @throws Exception
  143. */
  144. public function getRelatedDocument(): ?Document {
  145. return self::get($this->related_document_id);
  146. }
  147. /**
  148. * @return User
  149. * @throws Exception
  150. */
  151. public function getUser(): User {
  152. return User::get($this->user_id);
  153. }
  154. public function getType(): int {
  155. return $this->type;
  156. }
  157. public function isCreditNote(): bool {
  158. return $this->type === DocumentTypes::CREDIT_NOTE;
  159. }
  160. public function getDisplayType(): string {
  161. return DocumentTypes::enumToText($this->type);
  162. }
  163. public function getBillingAddressHtml(): string {
  164. return nl2br(htmlspecialchars($this->billing_address));
  165. }
  166. public function getDeliveryAddressHtml(): string {
  167. return nl2br(htmlspecialchars($this->delivery_address));
  168. }
  169. public function getAccessToken(): string {
  170. return sha1($this->dts_created . $this->number);
  171. }
  172. public function getPdfFilename(): string {
  173. $type_slug = Util::slug($this->getDisplayType());
  174. return sprintf("%s-%d.pdf", $type_slug, $this->number);
  175. }
  176. /**
  177. * @param string $variant
  178. * @return string
  179. * @throws Exception
  180. */
  181. public function getDownloadPdfUrl(string $variant = ""): string {
  182. return sprintf("%s/download-pdf?access_token=%s&variant=%s", $this->getUrl(), $this->getAccessToken(), $variant);
  183. }
  184. public function isAru(): bool {
  185. return $this->getType() === DocumentTypes::ADVANCE_REPLACEMENT_UNIT;
  186. }
  187. }