Product.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace App;
  3. use Exception;
  4. /**
  5. * Class Product
  6. * @property int $id
  7. * @property int $supplier_id
  8. * @property string $name
  9. * @property string $sku
  10. * @property int $type
  11. * @property string $price
  12. * @property string $dts_created
  13. * @property string $dts_updated
  14. * @property string $dts_deleted
  15. */
  16. class Product extends Model {
  17. protected static $table = "product";
  18. protected static $idField = "id";
  19. protected static $fillableFields = ["supplier_id", "name", "type", "sku", "price", "dts_created", "dts_updated", "dts_deleted"];
  20. public const TYPE_MACHINE = 1;
  21. public const TYPE_SPARE_PART = 2;
  22. public const TYPE_CONSUMABLE = 3;
  23. // Static methods
  24. public static function getTypeOptions(): array {
  25. return [
  26. self::TYPE_MACHINE => __("Machine"),
  27. self::TYPE_SPARE_PART => __("Spare Part"),
  28. self::TYPE_CONSUMABLE => __("Consumable"),
  29. ];
  30. }
  31. // Object methods
  32. /**
  33. * @return Supplier
  34. * @throws Exception
  35. */
  36. public function getSupplier(): Supplier {
  37. return Supplier::get($this->supplier_id);
  38. }
  39. public function getSlug(): string {
  40. return Util::slug("$this->name $this->id");
  41. }
  42. public function getUrl(): string {
  43. return sprintf("/products/%s", $this->getSlug());
  44. }
  45. public function getDisplayType(): string {
  46. return self::getTypeOptions()[$this->type] ?? "";
  47. }
  48. public function getDisplayPrice(): string {
  49. return number_format($this->price, 2);
  50. }
  51. }