Product.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace App;
  3. use App\Traits\DateTimeTrait;
  4. use App\Traits\SoftDeleteTrait;
  5. use Exception;
  6. /**
  7. * Class Product
  8. * @property int $id
  9. * @property int $supplier_id
  10. * @property string $name
  11. * @property string $sku
  12. * @property int $type
  13. * @property string $price
  14. * @property string $url
  15. * @property int $archived
  16. */
  17. class Product extends Model implements \JsonSerializable {
  18. protected static $table = "product";
  19. protected static $idField = "id";
  20. protected static $fillableFields = [
  21. "supplier_id",
  22. "name", "type", "sku", "price", "url", "archived",
  23. "dts_created", "dts_updated", "dts_deleted"
  24. ];
  25. use DateTimeTrait;
  26. use SoftDeleteTrait;
  27. public const TYPE_MACHINE = 1;
  28. public const TYPE_SPARE_PART = 2;
  29. public const TYPE_CONSUMABLE = 3;
  30. public const TYPE_DELIVERY_CHARGE = 4;
  31. // Static methods
  32. public static function getTypeOptions(): array {
  33. return [
  34. self::TYPE_MACHINE => __("Machine"),
  35. self::TYPE_SPARE_PART => __("Spare Part"),
  36. self::TYPE_CONSUMABLE => __("Consumable"),
  37. self::TYPE_DELIVERY_CHARGE => __("Delivery Charge"),
  38. ];
  39. }
  40. public static function getSkuPrefix(int $type): string {
  41. $map = [
  42. self::TYPE_MACHINE => "M",
  43. self::TYPE_SPARE_PART => "S",
  44. self::TYPE_CONSUMABLE => "C",
  45. self::TYPE_DELIVERY_CHARGE => "D",
  46. ];
  47. return $map[$type] ?? "";
  48. }
  49. // Object methods
  50. /**
  51. * @return Supplier
  52. * @throws Exception
  53. */
  54. public function getSupplier(): Supplier {
  55. return Supplier::get($this->supplier_id);
  56. }
  57. public function getSlug(): string {
  58. return Util::slug("$this->name $this->id");
  59. }
  60. public function getUrl(): string {
  61. return sprintf("/products/%s", $this->getSlug());
  62. }
  63. public function getUpdateUrl(): string {
  64. return sprintf("/products/%s/update", $this->getSlug());
  65. }
  66. public function getWebsiteUrl(): ?string {
  67. return $this->url;
  68. }
  69. public function getDisplayType(): string {
  70. return self::getTypeOptions()[$this->type] ?? "";
  71. }
  72. public function getDisplayPrice(): string {
  73. return number_format($this->price, 2);
  74. }
  75. public function jsonSerialize(): array {
  76. return [
  77. "id" => (int) $this->id,
  78. "supplierId" => (int) $this->supplier_id,
  79. "name" => $this->name,
  80. "sku" => $this->sku,
  81. "type" => (int) $this->type,
  82. "price" => $this->price,
  83. "archived" => $this->isArchived(),
  84. ];
  85. }
  86. public function getBrochurePath(): string {
  87. return $_ENV["DIR_ROOT"] . "/files/product/" . $this->id . ".pdf";
  88. }
  89. public function hasBrochure(): bool {
  90. return file_exists($this->getBrochurePath());
  91. }
  92. public function deleteBrochure(): bool {
  93. return !$this->hasBrochure() || unlink($this->getBrochurePath());
  94. }
  95. public function isArchived(): bool {
  96. return (bool) $this->archived;
  97. }
  98. }