$document_type], ["sort" => "id", "order" => "desc"]); if($document) { return $document->number + 1; } return 1000; } // Object methods /** * @return Sale|null * @throws Exception */ public function getSale(): ?Sale { return Sale::get($this->sale_id); } /** * @return string * @throws Exception */ public function getUrl(): string { if($this->isInvoice()) { /** @var Sale $sale */ $sale = $this->getSale(); return sprintf("%s/invoices/%s", $sale->getUrl(), $this->number); } if($this->isDeliveryNote()) { $sale = $this->getSale(); return sprintf("%s/delivery-notes/%s", $sale->getUrl(), $this->number); } throw new RuntimeException(__("Unimplemented getUrl for document type: $this->type")); } public function isInvoice(): bool { return (int) $this->type === DocumentTypes::INVOICE; } public function isDeliveryNote(): bool { return (int) $this->type === DocumentTypes::DELIVERY_NOTE; } public function isGoodsIn(): bool { return (int) $this->type === DocumentTypes::GOODS_IN; } public function getDisplayInvoiceStatus(): string { return InvoiceStatuses::enumToText($this->invoice_status); } public function getDisplayTotal(): string { $formatter = new NumberFormatter("en_GB", NumberFormatter::CURRENCY); $subtotal = "0.00"; foreach($this->getItems() as $item) { $subtotal = bcadd($item->getAmount(), $subtotal, 2); } $tax = bcmul($subtotal, $this->tax_rate / 100, 2); $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 { if($this->isInvoice()) { return __("Invoice %d", $this->number); } if($this->isDeliveryNote()) { return __("Delivery Note %d", $this->number); } return __("Document %d", $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); } }