Document.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. /**
  73. * @param int $number
  74. * @param int $document_type
  75. * @return Document|null
  76. * @throws Exception
  77. */
  78. public static function getByNumber(int $number, int $document_type): ?Document {
  79. return self::queryOne([
  80. "type" => $document_type,
  81. "number" => $number,
  82. "dts_deleted" => null
  83. ]);
  84. }
  85. // Object methods
  86. /**
  87. * @return Company|null
  88. * @throws Exception
  89. */
  90. public function getCompany(): ?Company {
  91. return Company::get($this->company_id);
  92. }
  93. /**
  94. * @return Sale|null
  95. * @throws Exception
  96. */
  97. public function getSale(): ?Sale {
  98. return Sale::get($this->sale_id);
  99. }
  100. /**
  101. * @return Supplier|null
  102. * @throws Exception
  103. */
  104. public function getSupplier(): ?Supplier {
  105. return Supplier::get($this->supplier_id);
  106. }
  107. /**
  108. * @return Location|null
  109. * @throws Exception
  110. */
  111. public function getLocation(): ?Location {
  112. return Location::get($this->location_id);
  113. }
  114. /**
  115. * @return string
  116. * @throws Exception
  117. */
  118. public function getUrl(): string {
  119. if($this->isInvoice()) {
  120. // Sale Invoice
  121. /** @var Sale $sale */
  122. $sale = $this->getSale();
  123. if($sale) {
  124. return sprintf("%s/invoices/%s", $sale->getUrl(), $this->number);
  125. }
  126. // ARU invoice
  127. $document = $this->getRelatedDocument();
  128. if($document) {
  129. if($document->isAru()) {
  130. return sprintf("%s/invoices/%s", $document->getUrl(), $this->number);
  131. }
  132. }
  133. }
  134. if($this->isDeliveryNote()) {
  135. // Sale delivery note
  136. $sale = $this->getSale();
  137. if($sale) {
  138. return sprintf("%s/delivery-notes/%s", $sale->getUrl(), $this->number);
  139. }
  140. // ARU delivery note
  141. $document = $this->getRelatedDocument();
  142. if($document) {
  143. if($document->isAru()) {
  144. return sprintf("%s/delivery-notes/%s", $document->getUrl(), $this->number);
  145. }
  146. }
  147. }
  148. if($this->isGoodsIn()) {
  149. return sprintf("/goods-in/%s", $this->number);
  150. }
  151. if($this->isAru()) {
  152. return sprintf("/arus/%s", $this->number);
  153. }
  154. throw new RuntimeException(__("Unimplemented getUrl for document type: $this->type"));
  155. }
  156. public function isInvoice(): bool {
  157. return (int) $this->type === DocumentTypes::INVOICE;
  158. }
  159. public function isDeliveryNote(): bool {
  160. return (int) $this->type === DocumentTypes::DELIVERY_NOTE;
  161. }
  162. public function isGoodsIn(): bool {
  163. return (int) $this->type === DocumentTypes::GOODS_IN;
  164. }
  165. public function getDisplayInvoiceStatus(): string {
  166. return InvoiceStatuses::enumToText($this->invoice_status);
  167. }
  168. public function getDisplayAruStatus(): string {
  169. return AruStatuses::enumToText($this->aru_status);
  170. }
  171. public function getDisplayTotal(): string {
  172. $formatter = new NumberFormatter("en_GB", NumberFormatter::CURRENCY);
  173. $subtotal = "0.00";
  174. foreach($this->getItems() as $item) {
  175. $subtotal = bcadd($item->getAmount(), $subtotal, 2);
  176. }
  177. $tax = bcmul($subtotal, $this->tax_rate / 100, 2);
  178. $total = bcadd($subtotal, $tax, 2);
  179. return $formatter->formatCurrency($total, $this->currency);
  180. }
  181. /**
  182. * @return DocumentItem[]
  183. */
  184. public function getItems(array $nvp = []): array {
  185. $nvp = array_merge($nvp, [
  186. "document_id" => $this->id,
  187. "dts_deleted" => null
  188. ]);
  189. return DocumentItem::query($nvp);
  190. }
  191. public function getTitle(): string {
  192. return __("%s %d", $this->getDisplayType(), $this->number);
  193. }
  194. /**
  195. * @return Document|null
  196. * @throws Exception
  197. */
  198. public function getRelatedDocument(): ?Document {
  199. return self::get($this->related_document_id);
  200. }
  201. /**
  202. * @return User
  203. * @throws Exception
  204. */
  205. public function getUser(): User {
  206. return User::get($this->user_id);
  207. }
  208. public function getType(): int {
  209. return $this->type;
  210. }
  211. public function isCreditNote(): bool {
  212. return $this->type === DocumentTypes::CREDIT_NOTE;
  213. }
  214. public function getDisplayType(): string {
  215. return DocumentTypes::enumToText($this->type);
  216. }
  217. public function getBillingAddressHtml(): string {
  218. return nl2br(htmlspecialchars($this->billing_address));
  219. }
  220. public function getDeliveryAddressHtml(): string {
  221. return nl2br(htmlspecialchars($this->delivery_address));
  222. }
  223. public function getAccessToken(): string {
  224. return sha1($this->dts_created . $this->number);
  225. }
  226. public function getPdfFilename(): string {
  227. $type_slug = Util::slug($this->getDisplayType());
  228. return sprintf("%s-%d.pdf", $type_slug, $this->number);
  229. }
  230. /**
  231. * @param string $variant
  232. * @return string
  233. * @throws Exception
  234. */
  235. public function getDownloadPdfUrl(string $variant = ""): string {
  236. return sprintf("%s/download-pdf?access_token=%s&variant=%s", $this->getUrl(), $this->getAccessToken(), $variant);
  237. }
  238. public function isAru(): bool {
  239. return $this->getType() === DocumentTypes::ADVANCE_REPLACEMENT_UNIT;
  240. }
  241. public function hasCosts(): bool {
  242. return ($this->isInvoice() || $this->isAru());
  243. }
  244. /**
  245. * Get delivery notes related to this invoice or ARU
  246. * @return Document[]
  247. */
  248. public function getDeliveryNotes(): array {
  249. return self::query([
  250. "type" => DocumentTypes::DELIVERY_NOTE,
  251. "related_document_id" => $this->id,
  252. "dts_deleted" => null
  253. ]);
  254. }
  255. /**
  256. * Get invoices related to this ARU
  257. * @return Document[]
  258. */
  259. public function getInvoices(): array {
  260. return self::query([
  261. "type" => DocumentTypes::INVOICE,
  262. "related_document_id" => $this->id,
  263. "dts_deleted" => null
  264. ]);
  265. }
  266. /**
  267. * @return bool
  268. * @throws Exception
  269. */
  270. public function isPartOfAru(): bool {
  271. $related = $this->getRelatedDocument();
  272. return $related && $related->isAru();
  273. }
  274. /**
  275. * @return Document[]
  276. */
  277. public function getGoodsInDocuments(): array {
  278. return self::query([
  279. "type" => DocumentTypes::GOODS_IN,
  280. "related_document_id" => $this->id,
  281. "dts_deleted" => null
  282. ]);
  283. }
  284. public function getDisplayDate(string $format = "d/m/Y"): string {
  285. return CarbonImmutable::parse($this->date)->format($format);
  286. }
  287. public function getSubtotal(): string {
  288. $subtotal = "0.00";
  289. foreach($this->getItems() as $item) {
  290. $subtotal = bcadd($subtotal, $item->getAmount(), 2);
  291. }
  292. return $subtotal;
  293. }
  294. public function getNotesHtml(): string {
  295. return nl2br(htmlspecialchars($this->notes));
  296. }
  297. /**
  298. * @return Ticket[]
  299. */
  300. public function getTickets(): array {
  301. return Ticket::query([
  302. "aru_document_id" => $this->id,
  303. "dts_deleted" => null
  304. ]);
  305. }
  306. }