Product.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 implements \JsonSerializable {
  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. public function jsonSerialize(): array {
  52. return [
  53. "id" => (int) $this->id,
  54. "supplierId" => (int) $this->supplier_id,
  55. "name" => $this->name,
  56. "sku" => $this->sku,
  57. "type" => (int) $this->type,
  58. "price" => $this->price,
  59. ];
  60. }
  61. }