Supplier.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. public static function getOptions(): array {
  27. $options = [];
  28. foreach(self::getAll() as $supplier) {
  29. $options[$supplier->id] = $supplier->name;
  30. }
  31. return $options;
  32. }
  33. // Object methods
  34. /**
  35. * @return Address
  36. * @throws \Exception
  37. */
  38. public function getAddress(): Address {
  39. $address = Address::get($this->address_id);
  40. return $address ?: new Address();
  41. }
  42. public function getSlug(): string {
  43. return Util::slug("$this->name $this->id");
  44. }
  45. public function getUrl(): string {
  46. return sprintf("/suppliers/%s", $this->getSlug());
  47. }
  48. /**
  49. * @return Product[]
  50. */
  51. public function getProducts(): array {
  52. return Product::query([
  53. "supplier_id" => $this->id,
  54. "dts_deleted" => null
  55. ], ["sort" => "name"]);
  56. }
  57. }