Location.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. * @param Request $request
  35. * @param Location $location
  36. * @return Location
  37. * @throws Exception
  38. */
  39. public static function updateFromRequest(Request $request, Location $location, ?Customer $customer_override = null): Location {
  40. if($customer_override) {
  41. $customer = $customer_override;
  42. }
  43. else {
  44. $customer_id = $request->int("customer_id");
  45. $customer = Customer::require($customer_id);
  46. }
  47. $location->customer_id = $customer->id;
  48. $location->name = $request->string("name");
  49. $location->nickname = $request->string("nickname");
  50. $location->phone = $request->string("phone");
  51. $location->email = $request->email("email");
  52. $location->contact_name = $request->string("contact_name");
  53. $location->contact_phone = $request->string("contact_phone");
  54. $location->contact_email = $request->email("contact_email");
  55. $address = $location->getAddress();
  56. $address->address_1 = $request->string("address_1");
  57. $address->address_2 = $request->string("address_2");
  58. $address->city = $request->string("city");
  59. $address->county = $request->string("county");
  60. $address->postal_code = $request->string("postal_code");
  61. $address->country = $request->string("country");
  62. $address->latitude = $request->float("latitude");
  63. $address->longitude = $request->float("longitude");
  64. if(!$location->name) {
  65. throw new RuntimeException(__("Name is required."));
  66. }
  67. if(isset($address->postal_code, $address->country) && !$address->hasGeolocation()) {
  68. $address->geocode();
  69. }
  70. $address->commit();
  71. $location->address_id = $address->id;
  72. $location->commit();
  73. return $location;
  74. }
  75. // Object methods
  76. /**
  77. * @return Customer
  78. * @throws Exception
  79. */
  80. public function getCustomer(): Customer {
  81. return Customer::get($this->customer_id);
  82. }
  83. /**
  84. * @return Address
  85. * @throws Exception
  86. */
  87. public function getAddress(): Address {
  88. $address = Address::get($this->address_id);
  89. return $address ?: new Address();
  90. }
  91. public function getSlug(): string {
  92. return Util::slug("$this->name $this->id");
  93. }
  94. /**
  95. * @return string
  96. * @throws Exception
  97. */
  98. public function getUrl(): string {
  99. return sprintf("/locations/%s/update", $this->getSlug());
  100. }
  101. /**
  102. * @return string
  103. * @throws Exception
  104. */
  105. public function getDisplayName(): string {
  106. $customer = $this->getCustomer();
  107. return sprintf("%s - %s", $customer->name, $this->name);
  108. }
  109. }