| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- <?php
- namespace App;
- use App\Traits\DateTimeTrait;
- use App\Traits\SoftDeleteTrait;
- use Exception;
- /**
- * Class Product
- * @property int $id
- * @property int $supplier_id
- * @property string $name
- * @property string $sku
- * @property int $type
- * @property string $price
- * @property string $url
- */
- class Product extends Model implements \JsonSerializable {
- protected static $table = "product";
- protected static $idField = "id";
- protected static $fillableFields = [
- "supplier_id",
- "name", "type", "sku", "price", "url",
- "dts_created", "dts_updated", "dts_deleted"
- ];
- use DateTimeTrait;
- use SoftDeleteTrait;
- public const TYPE_MACHINE = 1;
- public const TYPE_SPARE_PART = 2;
- public const TYPE_CONSUMABLE = 3;
- // Static methods
- public static function getTypeOptions(): array {
- return [
- self::TYPE_MACHINE => __("Machine"),
- self::TYPE_SPARE_PART => __("Spare Part"),
- self::TYPE_CONSUMABLE => __("Consumable"),
- ];
- }
- // Object methods
- /**
- * @return Supplier
- * @throws Exception
- */
- public function getSupplier(): Supplier {
- return Supplier::get($this->supplier_id);
- }
- public function getSlug(): string {
- return Util::slug("$this->name $this->id");
- }
- public function getUrl(): string {
- return sprintf("/products/%s", $this->getSlug());
- }
- public function getWebsiteUrl(): ?string {
- return $this->url;
- }
- public function getDisplayType(): string {
- return self::getTypeOptions()[$this->type] ?? "";
- }
- public function getDisplayPrice(): string {
- return number_format($this->price, 2);
- }
- public function jsonSerialize(): array {
- return [
- "id" => (int) $this->id,
- "supplierId" => (int) $this->supplier_id,
- "name" => $this->name,
- "sku" => $this->sku,
- "type" => (int) $this->type,
- "price" => $this->price,
- ];
- }
- public function getBrochurePath(): string {
- return $_ENV["DIR_ROOT"] . "/files/product/" . $this->id . ".pdf";
- }
- public function hasBrochure(): bool {
- return file_exists($this->getBrochurePath());
- }
- public function deleteBrochure(): bool {
- return !$this->hasBrochure() || unlink($this->getBrochurePath());
- }
- }
|