Document.php 7.9 KB

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