| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- <?php
- namespace App;
- use App\Enums\DocumentTypes;
- use App\Enums\InvoiceStatuses;
- use App\Traits\DateTimeTrait;
- use App\Traits\SoftDeleteTrait;
- use Exception;
- use NumberFormatter;
- use RuntimeException;
- /**
- * @property int $id
- * @property int $user_id User who created this document
- * @property int $sale_id Sale that this document relates to
- * @property int $supplier_id Used for goods-in documents
- * @property int $location_id
- * @property int $related_document_id Used to link a delivery note to an invoice
- * @property int $type
- * @property int $invoice_status
- * @property string $currency
- * @property string $tax_rate
- * @property string $billing_address
- * @property string $delivery_address
- * @property int $number
- * @property string $date
- * @property string $po_number Used for invoices
- * @property string $terms Used for invoices
- * @property string $delivery_company
- * @property string $tracking_number
- * @property string $delivery_cost
- * @property string $notes
- * @property string $dts_finalised
- * @property string $dts_paid
- */
- class Document extends Model {
- protected static $table = "document";
- protected static $idField = "id";
- protected static $fillableFields = [
- "user_id", "sale_id", "supplier_id", "location_id", "related_document_id",
- "type",
- "invoice_status",
- "currency", "tax_rate",
- "billing_address", "delivery_address",
- "number", "date", "po_number", "terms",
- "delivery_company", "tracking_number", "delivery_cost",
- "notes",
- "dts_finalised", "dts_paid",
- "dts_created", "dts_updated", "dts_deleted"
- ];
- use DateTimeTrait;
- use SoftDeleteTrait;
- // Static methods
- /**
- * @param int $document_type
- * @return int
- * @throws Exception
- */
- public static function getNextNumber(int $document_type): int {
- $document = self::queryOne(["type" => $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);
- }
- }
|