Customer.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace App;
  3. use App\Traits\DateTimeTrait;
  4. use App\Traits\SoftDeleteTrait;
  5. /**
  6. * Class Customer
  7. * @property int $id
  8. * @property int $address_id
  9. * @property string $name
  10. * @property string $phone
  11. * @property string $fax
  12. * @property string $email
  13. * @property string $vat_number
  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", "vat_number", "dts_created", "dts_updated", "dts_deleted"];
  19. use DateTimeTrait;
  20. use SoftDeleteTrait;
  21. // Static methods
  22. /**
  23. * @return Customer[]
  24. */
  25. public static function getAll(): array {
  26. return self::query([], ["sort" => "name"]);
  27. }
  28. public static function getOptions(): array {
  29. $options = [];
  30. foreach(self::getAll() as $customer) {
  31. $options[$customer->id] = $customer->name;
  32. }
  33. return $options;
  34. }
  35. // Object methods
  36. /**
  37. * @return Address
  38. * @throws \Exception
  39. */
  40. public function getAddress(): Address {
  41. $address = Address::get($this->address_id);
  42. return $address ?: new Address();
  43. }
  44. public function getSlug(): string {
  45. return Util::slug("$this->name $this->id");
  46. }
  47. public function getUrl(): string {
  48. return sprintf("/customers/%s", $this->getSlug());
  49. }
  50. /**
  51. * @param array $nvp
  52. * @return Location[]
  53. */
  54. public function getLocations(array $nvp = []): array {
  55. $nvp = array_merge($nvp, [
  56. "customer_id" => $this->id,
  57. "dts_deleted" => null
  58. ]);
  59. return Location::query($nvp, [
  60. "sort" => "name"
  61. ]);
  62. }
  63. }