| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- <?php
- namespace App;
- 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 $dts_created
- * @property string $dts_updated
- * @property string $dts_deleted
- */
- class Product extends Model implements \JsonSerializable {
- protected static $table = "product";
- protected static $idField = "id";
- protected static $fillableFields = ["supplier_id", "name", "type", "sku", "price", "dts_created", "dts_updated", "dts_deleted"];
- 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 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,
- ];
- }
- }
|