Product.php 1.3 KB

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