Supplier.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 $dts_created
  12. * @property string $dts_updated
  13. * @property string $dts_deleted
  14. */
  15. class Supplier extends Model {
  16. protected static $table = "supplier";
  17. protected static $idField = "id";
  18. protected static $fillableFields = ["address_id", "name", "phone", "fax", "email", "dts_created", "dts_updated", "dts_deleted"];
  19. // Static methods
  20. /**
  21. * @return Supplier[]
  22. */
  23. public static function getAll(): array {
  24. return self::query([], ["sort" => "name"]);
  25. }
  26. // Object methods
  27. /**
  28. * @return Address
  29. * @throws \Exception
  30. */
  31. public function getAddress(): Address {
  32. $address = Address::get($this->address_id);
  33. return $address ?: new Address();
  34. }
  35. public function getSlug(): string {
  36. return Util::slug("$this->name $this->id");
  37. }
  38. public function getUrl(): string {
  39. return sprintf("/suppliers/%s", $this->getSlug());
  40. }
  41. /**
  42. * @return Product[]
  43. */
  44. public function getProducts(): array {
  45. return Product::query([
  46. "supplier_id" => $this->id,
  47. "dts_deleted" => null
  48. ], ["sort" => "name"]);
  49. }
  50. }