Customer.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. namespace App;
  3. /**
  4. * Class Customer
  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 Customer extends Model {
  16. protected static $table = "customer";
  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 Customer[]
  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 $customer) {
  29. $options[$customer->id] = $customer->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("/customers/%s", $this->getSlug());
  47. }
  48. /**
  49. * @param array $nvp
  50. * @return Location[]
  51. */
  52. public function getLocations(array $nvp = []): array {
  53. $nvp = array_merge($nvp, [
  54. "customer_id" => $this->id,
  55. "dts_deleted" => null
  56. ]);
  57. return Location::query($nvp, [
  58. "sort" => "name"
  59. ]);
  60. }
  61. }