$document_type], ["sort" => "id", "order" => "desc"]); if($document) { return $document->number + 1; } return 1000; } /** * @param int $number * @param int $document_type * @return Document|null * @throws Exception */ public static function getByNumber(int $number, int $document_type): ?Document { return self::queryOne([ "type" => $document_type, "number" => $number, "dts_deleted" => null ]); } // Object methods /** * @return Company|null * @throws Exception */ public function getCompany(): ?Company { return Company::get($this->company_id); } /** * @return Sale|null * @throws Exception */ public function getSale(): ?Sale { return Sale::get($this->sale_id); } /** * @return Supplier|null * @throws Exception */ public function getSupplier(): ?Supplier { return Supplier::get($this->supplier_id); } /** * @return Location|null * @throws Exception */ public function getLocation(): ?Location { return Location::get($this->location_id); } public function getReference(): string { switch($this->getType()) { case DocumentTypes::GOODS_IN: return "G" . $this->number; case DocumentTypes::ADVANCE_REPLACEMENT_UNIT: return "A" . $this->number; case DocumentTypes::SALES_ORDER: return "S" . $this->number; case DocumentTypes::DELIVERY_NOTE: return "D" . $this->number; default: return $this->number; } } /** * @return string * @throws Exception */ public function getUrl(): string { if($this->isSalesOrder()) { // Lead Sales Order /** @var Sale $sale */ $sale = $this->getSale(); if($sale) { return sprintf("%s/sales-orders/%s", $sale->getUrl(), $this->number); } // ARU Sales Order $document = $this->getRelatedDocument(); if($document) { if($document->isAru()) { return sprintf("%s/sales-orders/%s", $document->getUrl(), $this->number); } } } if($this->isDeliveryNote()) { // Sale delivery note $sale = $this->getSale(); if($sale) { return sprintf("%s/delivery-notes/%s", $sale->getUrl(), $this->number); } // ARU delivery note $document = $this->getRelatedDocument(); if($document) { if($document->isAru()) { return sprintf("%s/delivery-notes/%s", $document->getUrl(), $this->number); } } } if($this->isGoodsIn()) { return sprintf("/goods-in/%s", $this->number); } if($this->isAru()) { return sprintf("/arus/%s", $this->number); } throw new RuntimeException(__("Unimplemented getUrl for document type: $this->type")); } public function isSalesOrder(): bool { return (int) $this->type === DocumentTypes::SALES_ORDER; } public function isDeliveryNote(): bool { return (int) $this->type === DocumentTypes::DELIVERY_NOTE; } public function isGoodsIn(): bool { return (int) $this->type === DocumentTypes::GOODS_IN; } public function getDisplaySalesOrderStatus(): string { return SalesOrderStatuses::enumToText($this->invoice_status); } public function getDisplayAruStatus(): string { return AruStatuses::enumToText($this->aru_status); } public function getDisplaySubtotal(): string { return (new NumberFormatter("en_GB", NumberFormatter::CURRENCY))->formatCurrency($this->getSubtotal(), $this->currency); } public function getDisplayTax(): string { return (new NumberFormatter("en_GB", NumberFormatter::CURRENCY))->formatCurrency($this->getTax(), $this->currency); } public function getDisplayTotal(): string { $formatter = new NumberFormatter("en_GB", NumberFormatter::CURRENCY); $subtotal = $this->getSubtotal(); $tax = $this->getTax(); $total = bcadd($subtotal, $tax, 2); return $formatter->formatCurrency($total, $this->currency); } /** * @return DocumentItem[] */ public function getItems(array $nvp = []): array { $nvp = array_merge($nvp, [ "document_id" => $this->id, "dts_deleted" => null ]); return DocumentItem::query($nvp); } public function getTitle(): string { return __("%s %d", $this->getDisplayType(), $this->number); } /** * @return Document|null * @throws Exception */ public function getRelatedDocument(): ?Document { return self::get($this->related_document_id); } /** * @return User * @throws Exception */ public function getUser(): User { return User::get($this->user_id); } public function getType(): int { return $this->type; } public function isCreditNote(): bool { return $this->type === DocumentTypes::CREDIT_NOTE; } public function getDisplayType(): string { return DocumentTypes::enumToText($this->type); } public function getBillingAddressHtml(): string { return nl2br(htmlspecialchars($this->billing_address)); } public function getDeliveryAddressHtml(): string { return nl2br(htmlspecialchars($this->delivery_address)); } public function getAccessToken(): string { return sha1($this->dts_created . $this->number); } public function getPdfFilename(): string { $type_slug = Util::slug($this->getDisplayType()); return sprintf("%s-%d.pdf", $type_slug, $this->number); } /** * @param string $variant * @return string * @throws Exception */ public function getDownloadPdfUrl(string $variant = ""): string { return sprintf("%s/download-pdf?access_token=%s&variant=%s", $this->getUrl(), $this->getAccessToken(), $variant); } public function isAru(): bool { return $this->getType() === DocumentTypes::ADVANCE_REPLACEMENT_UNIT; } public function hasCosts(): bool { return ($this->isSalesOrder() || $this->isAru()); } /** * Get delivery notes related to this invoice or ARU * @return Document[] */ public function getDeliveryNotes(): array { return self::query([ "type" => DocumentTypes::DELIVERY_NOTE, "related_document_id" => $this->id, "dts_deleted" => null ]); } /** * Get invoices related to this ARU * @return Document[] */ public function getSalesOrders(): array { return self::query([ "type" => DocumentTypes::SALES_ORDER, "related_document_id" => $this->id, "dts_deleted" => null ]); } /** * @return bool * @throws Exception */ public function isPartOfAru(): bool { $related = $this->getRelatedDocument(); return $related && $related->isAru(); } /** * @return Document[] */ public function getGoodsInDocuments(): array { return self::query([ "type" => DocumentTypes::GOODS_IN, "related_document_id" => $this->id, "dts_deleted" => null ]); } public function getDisplayDate(string $format = "d/m/Y"): string { return CarbonImmutable::parse($this->date)->format($format); } public function getSubtotal(): string { $subtotal = "0.00"; foreach($this->getItems() as $item) { $subtotal = bcadd($subtotal, $item->getAmount(), 2); } return $subtotal; } public function getTax(): string { return bcmul($this->getSubtotal(), $this->tax_rate / 100, 2); } public function getNotesHtml(): string { return nl2br(htmlspecialchars($this->notes)); } /** * @return Ticket[] */ public function getTickets(): array { return Ticket::query([ "aru_document_id" => $this->id, "dts_deleted" => null ]); } /** * @param string|null $variant * @return string * @throws Exception */ public function generatePdf(?string $variant): string { $host = $_SERVER["HTTP_HOST"]; $render_url = "https://" . $host . $this->getUrl() . "/render-pdf?" . http_build_query([ "access_token" => $this->getAccessToken(), "variant" => $variant ]); chdir($_ENV["DIR_ROOT"]); $output_filename = $this->id . ".pdf"; $output_path = "pdf-output/" . $output_filename; $command = sprintf('node pdf.js "%s" "%s"', $render_url, $output_filename ); shell_exec($command); return $output_path; } /** * @return $this * @throws Exception */ public function getRootDocument(): Document { $related_document = $this->getRelatedDocument(); if($related_document) { return $related_document->getRootDocument(); } return $this; } /** * @return Document[] */ public function getChildDocuments(): array { return self::query([ "related_document_id" => $this->id, "dts_deleted" => null ], [ "sort" => "type", ]); } /** * @return Product[] * @throws Exception */ public function getProducts(): array { $products = []; foreach($this->getItems(["product_id" => new NotNullConstraint()]) as $item) { $products[$item->product_id] = $item->getProduct(); } return array_values($products); } }