Supplier.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace App;
  3. /**
  4. * Class Supplier
  5. * @property int $id
  6. * @property int $address_id
  7. * @property string $name
  8. * @property string $phone
  9. * @property string $fax
  10. * @property string $email
  11. * @property string $sku_prefix
  12. * @property string $dts_created
  13. * @property string $dts_updated
  14. * @property string $dts_deleted
  15. */
  16. class Supplier extends Model {
  17. protected static $table = "supplier";
  18. protected static $idField = "id";
  19. protected static $fillableFields = ["address_id", "name", "phone", "fax", "email", "sku_prefix", "dts_created", "dts_updated", "dts_deleted"];
  20. // Static methods
  21. /**
  22. * @return Supplier[]
  23. */
  24. public static function getAll(): array {
  25. return self::query([], ["sort" => "name"]);
  26. }
  27. public static function getOptions(): array {
  28. $options = [];
  29. foreach(self::getAll() as $supplier) {
  30. $options[$supplier->id] = $supplier->name;
  31. }
  32. return $options;
  33. }
  34. // Object methods
  35. /**
  36. * @return Address
  37. * @throws \Exception
  38. */
  39. public function getAddress(): Address {
  40. $address = Address::get($this->address_id);
  41. return $address ?: new Address();
  42. }
  43. public function getSlug(): string {
  44. return Util::slug("$this->name $this->id");
  45. }
  46. public function getUrl(): string {
  47. return sprintf("/suppliers/%s", $this->getSlug());
  48. }
  49. /**
  50. * @return Product[]
  51. */
  52. public function getProducts(): array {
  53. return Product::query([
  54. "supplier_id" => $this->id,
  55. "dts_deleted" => null
  56. ], ["sort" => "name"]);
  57. }
  58. }