Location.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace App;
  3. use App\Enums\AruStatuses;
  4. use App\Traits\DateTimeTrait;
  5. use App\Traits\SoftDeleteTrait;
  6. use Exception;
  7. use RuntimeException;
  8. /**
  9. * Class Location
  10. * @property int $id
  11. * @property int $customer_id
  12. * @property int $address_id
  13. * @property string $name
  14. * @property string $nickname
  15. * @property string $phone
  16. * @property string $fax
  17. * @property string $email
  18. * @property string $contact_name
  19. * @property string $contact_phone
  20. * @property string $contact_email
  21. */
  22. class Location extends Model {
  23. protected static $table = "location";
  24. protected static $idField = "id";
  25. protected static $fillableFields = [
  26. "customer_id", "address_id",
  27. "name", "nickname", "phone", "fax", "email",
  28. "contact_name", "contact_phone", "contact_email",
  29. "dts_created", "dts_updated", "dts_deleted"
  30. ];
  31. use DateTimeTrait;
  32. use SoftDeleteTrait;
  33. /**
  34. * @return string[]
  35. * @throws Exception
  36. */
  37. public static function getOptions(): array {
  38. $options = [];
  39. foreach(Customer::getAll() as $customer) {
  40. foreach($customer->getLocations() as $location) {
  41. $options[$location->id] = $location->getDisplayName();
  42. }
  43. }
  44. return $options;
  45. }
  46. /**
  47. * @param Request $request
  48. * @param Location $location
  49. * @param Customer|null $customer_override
  50. * @return Location
  51. * @throws Exception
  52. */
  53. public static function updateFromRequest(Request $request, Location $location, ?Customer $customer_override = null): Location {
  54. if($customer_override) {
  55. $customer = $customer_override;
  56. }
  57. else {
  58. $customer_id = $request->int("customer_id");
  59. $customer = Customer::require($customer_id);
  60. }
  61. $location->customer_id = $customer->id;
  62. $location->name = $request->string("name");
  63. $location->nickname = $request->string("nickname");
  64. $location->phone = $request->string("phone");
  65. $location->email = $request->email("email");
  66. $location->contact_name = $request->string("contact_name");
  67. $location->contact_phone = $request->string("contact_phone");
  68. $location->contact_email = $request->email("contact_email");
  69. $address = $location->getAddress();
  70. $address->address_1 = $request->string("address_1");
  71. $address->address_2 = $request->string("address_2");
  72. $address->city = $request->string("city");
  73. $address->county = $request->string("county");
  74. $address->postal_code = $request->string("postal_code");
  75. $address->country = $request->string("country");
  76. $address->latitude = $request->float("latitude");
  77. $address->longitude = $request->float("longitude");
  78. if(!$location->name) {
  79. throw new RuntimeException(__("Name is required."));
  80. }
  81. if(isset($address->postal_code, $address->country) && !$address->hasGeolocation()) {
  82. $address->geocode();
  83. }
  84. $address->commit();
  85. $location->address_id = $address->id;
  86. $location->commit();
  87. return $location;
  88. }
  89. // Object methods
  90. /**
  91. * @return Customer
  92. * @throws Exception
  93. */
  94. public function getCustomer(): Customer {
  95. return Customer::get($this->customer_id);
  96. }
  97. /**
  98. * @return Address
  99. * @throws Exception
  100. */
  101. public function getAddress(): Address {
  102. $address = Address::get($this->address_id);
  103. return $address ?: new Address();
  104. }
  105. public function getSlug(): string {
  106. return Util::slug("$this->name $this->id");
  107. }
  108. /**
  109. * @return string
  110. * @throws Exception
  111. */
  112. public function getUrl(): string {
  113. return sprintf("/locations/%s/update", $this->getSlug());
  114. }
  115. /**
  116. * @return string
  117. * @throws Exception
  118. */
  119. public function getDisplayName(): string {
  120. $customer = $this->getCustomer();
  121. return sprintf("%s - %s", $customer->name, $this->name);
  122. }
  123. }