Document.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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 $company_id Company issuing this document
  13. * @property int $user_id User who created this document
  14. * @property int $sale_id Sale that this document relates to
  15. * @property int $supplier_id Used for goods-in documents
  16. * @property int $location_id
  17. * @property int $related_document_id Used to link a delivery note to an invoice
  18. * @property int $type
  19. * @property int $invoice_status
  20. * @property string $currency
  21. * @property string $tax_rate
  22. * @property string $billing_address
  23. * @property string $delivery_address
  24. * @property int $number
  25. * @property string $date
  26. * @property string $po_number Used for invoices
  27. * @property string $terms Used for invoices
  28. * @property string $delivery_company
  29. * @property string $tracking_number
  30. * @property string $delivery_cost
  31. * @property string $notes
  32. * @property string $dts_finalised
  33. * @property string $dts_paid
  34. */
  35. class Document extends Model {
  36. protected static $table = "document";
  37. protected static $idField = "id";
  38. protected static $fillableFields = [
  39. "company_id", "user_id", "sale_id", "supplier_id", "location_id", "related_document_id",
  40. "type",
  41. "invoice_status",
  42. "currency", "tax_rate",
  43. "billing_address", "delivery_address",
  44. "number", "date", "po_number", "terms",
  45. "delivery_company", "tracking_number", "delivery_cost",
  46. "notes",
  47. "dts_finalised", "dts_paid",
  48. "dts_created", "dts_updated", "dts_deleted"
  49. ];
  50. use DateTimeTrait;
  51. use SoftDeleteTrait;
  52. // Static methods
  53. /**
  54. * @param int $document_type
  55. * @return int
  56. * @throws Exception
  57. */
  58. public static function getNextNumber(int $document_type): int {
  59. $document = self::queryOne(["type" => $document_type], ["sort" => "id", "order" => "desc"]);
  60. if($document) {
  61. return $document->number + 1;
  62. }
  63. return 1000;
  64. }
  65. // Object methods
  66. /**
  67. * @return Company|null
  68. * @throws Exception
  69. */
  70. public function getCompany(): ?Company {
  71. return Company::get($this->company_id);
  72. }
  73. /**
  74. * @return Sale|null
  75. * @throws Exception
  76. */
  77. public function getSale(): ?Sale {
  78. return Sale::get($this->sale_id);
  79. }
  80. /**
  81. * @return Supplier|null
  82. * @throws Exception
  83. */
  84. public function getSupplier(): ?Supplier {
  85. return Supplier::get($this->supplier_id);
  86. }
  87. /**
  88. * @return Location|null
  89. * @throws Exception
  90. */
  91. public function getLocation(): ?Location {
  92. return Location::get($this->location_id);
  93. }
  94. /**
  95. * @return string
  96. * @throws Exception
  97. */
  98. public function getUrl(): string {
  99. if($this->isInvoice()) {
  100. // Sale Invoice
  101. /** @var Sale $sale */
  102. $sale = $this->getSale();
  103. if($sale) {
  104. return sprintf("%s/invoices/%s", $sale->getUrl(), $this->number);
  105. }
  106. // ARU invoice
  107. $document = $this->getRelatedDocument();
  108. if($document) {
  109. if($document->isAru()) {
  110. return sprintf("%s/invoices/%s", $document->getUrl(), $this->number);
  111. }
  112. }
  113. }
  114. if($this->isDeliveryNote()) {
  115. // Sale delivery note
  116. $sale = $this->getSale();
  117. if($sale) {
  118. return sprintf("%s/delivery-notes/%s", $sale->getUrl(), $this->number);
  119. }
  120. // ARU delivery note
  121. $document = $this->getRelatedDocument();
  122. if($document) {
  123. if($document->isAru()) {
  124. return sprintf("%s/delivery-notes/%s", $document->getUrl(), $this->number);
  125. }
  126. }
  127. }
  128. if($this->isGoodsIn()) {
  129. return sprintf("/goods-in/%s", $this->number);
  130. }
  131. if($this->isAru()) {
  132. return sprintf("/arus/%s", $this->number);
  133. }
  134. throw new RuntimeException(__("Unimplemented getUrl for document type: $this->type"));
  135. }
  136. public function isInvoice(): bool {
  137. return (int) $this->type === DocumentTypes::INVOICE;
  138. }
  139. public function isDeliveryNote(): bool {
  140. return (int) $this->type === DocumentTypes::DELIVERY_NOTE;
  141. }
  142. public function isGoodsIn(): bool {
  143. return (int) $this->type === DocumentTypes::GOODS_IN;
  144. }
  145. public function getDisplayInvoiceStatus(): string {
  146. return InvoiceStatuses::enumToText($this->invoice_status);
  147. }
  148. public function getDisplayTotal(): string {
  149. $formatter = new NumberFormatter("en_GB", NumberFormatter::CURRENCY);
  150. $subtotal = "0.00";
  151. foreach($this->getItems() as $item) {
  152. $subtotal = bcadd($item->getAmount(), $subtotal, 2);
  153. }
  154. $tax = bcmul($subtotal, $this->tax_rate / 100, 2);
  155. $total = bcadd($subtotal, $tax, 2);
  156. return $formatter->formatCurrency($total, $this->currency);
  157. }
  158. /**
  159. * @return DocumentItem[]
  160. */
  161. public function getItems(array $nvp = []): array {
  162. $nvp = array_merge($nvp, [
  163. "document_id" => $this->id,
  164. "dts_deleted" => null
  165. ]);
  166. return DocumentItem::query($nvp);
  167. }
  168. public function getTitle(): string {
  169. return __("%s %d", $this->getDisplayType(), $this->number);
  170. }
  171. /**
  172. * @return Document|null
  173. * @throws Exception
  174. */
  175. public function getRelatedDocument(): ?Document {
  176. return self::get($this->related_document_id);
  177. }
  178. /**
  179. * @return User
  180. * @throws Exception
  181. */
  182. public function getUser(): User {
  183. return User::get($this->user_id);
  184. }
  185. public function getType(): int {
  186. return $this->type;
  187. }
  188. public function isCreditNote(): bool {
  189. return $this->type === DocumentTypes::CREDIT_NOTE;
  190. }
  191. public function getDisplayType(): string {
  192. return DocumentTypes::enumToText($this->type);
  193. }
  194. public function getBillingAddressHtml(): string {
  195. return nl2br(htmlspecialchars($this->billing_address));
  196. }
  197. public function getDeliveryAddressHtml(): string {
  198. return nl2br(htmlspecialchars($this->delivery_address));
  199. }
  200. public function getAccessToken(): string {
  201. return sha1($this->dts_created . $this->number);
  202. }
  203. public function getPdfFilename(): string {
  204. $type_slug = Util::slug($this->getDisplayType());
  205. return sprintf("%s-%d.pdf", $type_slug, $this->number);
  206. }
  207. /**
  208. * @param string $variant
  209. * @return string
  210. * @throws Exception
  211. */
  212. public function getDownloadPdfUrl(string $variant = ""): string {
  213. return sprintf("%s/download-pdf?access_token=%s&variant=%s", $this->getUrl(), $this->getAccessToken(), $variant);
  214. }
  215. public function isAru(): bool {
  216. return $this->getType() === DocumentTypes::ADVANCE_REPLACEMENT_UNIT;
  217. }
  218. public function hasCosts(): bool {
  219. return ($this->isInvoice() || $this->isAru());
  220. }
  221. /**
  222. * Get delivery notes related to this invoice or ARU
  223. * @return Document[]
  224. */
  225. public function getDeliveryNotes(): array {
  226. return self::query([
  227. "type" => DocumentTypes::DELIVERY_NOTE,
  228. "related_document_id" => $this->id,
  229. "dts_deleted" => null
  230. ]);
  231. }
  232. /**
  233. * Get invoices related to this ARU
  234. * @return array
  235. */
  236. public function getInvoices(): array {
  237. return self::query([
  238. "type" => DocumentTypes::INVOICE,
  239. "related_document_id" => $this->id,
  240. "dts_deleted" => null
  241. ]);
  242. }
  243. /**
  244. * @return bool
  245. * @throws Exception
  246. */
  247. public function isPartOfAru(): bool {
  248. $related = $this->getRelatedDocument();
  249. return $related && $related->isAru();
  250. }
  251. }