Location.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 int $billing_entity_id
  14. * @property string $name
  15. * @property string $nickname
  16. * @property string $phone
  17. * @property string $fax
  18. * @property string $email
  19. * @property string $contact_name
  20. * @property string $contact_phone
  21. * @property string $contact_email
  22. */
  23. class Location extends Model {
  24. protected static $table = "location";
  25. protected static $idField = "id";
  26. protected static $fillableFields = [
  27. "customer_id", "address_id", "billing_entity_id",
  28. "name", "nickname", "phone", "fax", "email",
  29. "contact_name", "contact_phone", "contact_email",
  30. "dts_created", "dts_updated", "dts_deleted"
  31. ];
  32. use DateTimeTrait;
  33. use SoftDeleteTrait;
  34. /**
  35. * @return string[]
  36. * @throws Exception
  37. */
  38. public static function getOptions(): array {
  39. $options = [];
  40. foreach(Customer::getAll() as $customer) {
  41. foreach($customer->getLocations() as $location) {
  42. $options[$location->id] = $location->getDisplayName();
  43. }
  44. }
  45. return $options;
  46. }
  47. /**
  48. * @param Request $request
  49. * @param Location $location
  50. * @param Customer|null $customer_override
  51. * @return Location
  52. * @throws Exception
  53. */
  54. public static function updateFromRequest(Request $request, Location $location, ?Customer $customer_override = null): Location {
  55. if($customer_override) {
  56. $customer = $customer_override;
  57. }
  58. else {
  59. $customer_id = $request->int("customer_id");
  60. $customer = Customer::require($customer_id);
  61. }
  62. $billing_entity_id = $request->int("billing_entity_id");
  63. if($billing_entity_id) {
  64. $billing_entity = CustomerEntity::require($billing_entity_id);
  65. if(!$billing_entity->belongsToCustomer($customer)) {
  66. throw new RuntimeException(__("The billing entity does not belong to the selected customer."));
  67. }
  68. $location->billing_entity_id = $billing_entity_id;
  69. }
  70. else {
  71. $location->billing_entity_id = null;
  72. }
  73. $location->customer_id = $customer->id;
  74. $location->name = $request->string("name");
  75. $location->nickname = $request->string("nickname");
  76. $location->phone = $request->string("phone");
  77. $location->email = $request->email("email");
  78. $location->contact_name = $request->string("contact_name");
  79. $location->contact_phone = $request->string("contact_phone");
  80. $location->contact_email = $request->email("contact_email");
  81. $address = $location->getAddress();
  82. $address->address_1 = $request->string("address_1");
  83. $address->address_2 = $request->string("address_2");
  84. $address->city = $request->string("city");
  85. $address->county = $request->string("county");
  86. $address->postal_code = $request->string("postal_code");
  87. $address->country = $request->string("country");
  88. $address->latitude = $request->float("latitude");
  89. $address->longitude = $request->float("longitude");
  90. if(!$location->name) {
  91. throw new RuntimeException(__("Name is required."));
  92. }
  93. if(isset($address->postal_code, $address->country) && !$address->hasGeolocation()) {
  94. $address->geocode();
  95. }
  96. $address->commit();
  97. $location->address_id = $address->id;
  98. $location->commit();
  99. return $location;
  100. }
  101. // Object methods
  102. /**
  103. * @return Customer
  104. * @throws Exception
  105. */
  106. public function getCustomer(): Customer {
  107. return Customer::get($this->customer_id);
  108. }
  109. /**
  110. * @return Address
  111. * @throws Exception
  112. */
  113. public function getAddress(): Address {
  114. $address = Address::get($this->address_id);
  115. return $address ?: new Address();
  116. }
  117. /**
  118. * @return CustomerEntity|null
  119. * @throws Exception
  120. */
  121. public function getBillingEntity(): ?CustomerEntity {
  122. return CustomerEntity::get($this->billing_entity_id);
  123. }
  124. public function getSlug(): string {
  125. return Util::slug("$this->name $this->id");
  126. }
  127. /**
  128. * @return string
  129. * @throws Exception
  130. */
  131. public function getUrl(): string {
  132. return sprintf("/locations/%s/update", $this->getSlug());
  133. }
  134. /**
  135. * @return string
  136. * @throws Exception
  137. */
  138. public function getDisplayName(): string {
  139. $customer = $this->getCustomer();
  140. return sprintf("%s - %s", $customer->name, $this->name);
  141. }
  142. }