1
0

Location.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. namespace App;
  3. use App\Constraint\MoreThanOrEqualsConstraint;
  4. use App\Constraint\NotInConstraint;
  5. use App\Enums\SalesOrderStatuses;
  6. use App\Interfaces\OptionInterface;
  7. use App\Traits\DateTimeTrait;
  8. use App\Traits\SoftDeleteTrait;
  9. use Carbon\CarbonImmutable;
  10. use Exception;
  11. use RuntimeException;
  12. /**
  13. * Class Location
  14. * @property int $id
  15. * @property int $customer_id
  16. * @property int $address_id
  17. * @property int $billing_entity_id
  18. * @property string $name
  19. * @property string $nickname
  20. * @property string $phone
  21. * @property string $fax
  22. * @property string $email
  23. * @property string $contact_name
  24. * @property string $contact_phone
  25. * @property string $contact_email
  26. */
  27. class Location extends Model implements OptionInterface {
  28. protected static $table = "location";
  29. protected static $idField = "id";
  30. protected static $fillableFields = [
  31. "customer_id", "address_id", "billing_entity_id",
  32. "name", "nickname", "phone", "fax", "email",
  33. "contact_name", "contact_phone", "contact_email",
  34. "dts_created", "dts_updated", "dts_deleted"
  35. ];
  36. use DateTimeTrait;
  37. use SoftDeleteTrait;
  38. /**
  39. * @return string[]
  40. * @throws Exception
  41. */
  42. public static function getOptions(): array {
  43. $options = [];
  44. foreach(Customer::getAll() as $customer) {
  45. foreach($customer->getLocations() as $location) {
  46. $options[$location->id] = $location->getDisplayName();
  47. }
  48. }
  49. return $options;
  50. }
  51. /**
  52. * @param Request $request
  53. * @param Location $location
  54. * @param Customer|null $customer_override
  55. * @return Location
  56. * @throws Exception
  57. */
  58. public static function updateFromRequest(Request $request, Location $location, ?Customer $customer_override = null): Location {
  59. if($customer_override) {
  60. $customer = $customer_override;
  61. }
  62. else {
  63. $customer_id = $request->int("customer_id");
  64. $customer = Customer::require($customer_id);
  65. }
  66. $billing_entity_id = $request->int("billing_entity_id");
  67. if($billing_entity_id) {
  68. $billing_entity = CustomerEntity::require($billing_entity_id);
  69. if(!$billing_entity->belongsToCustomer($customer)) {
  70. throw new RuntimeException(__("The billing entity does not belong to the selected customer."));
  71. }
  72. $location->billing_entity_id = $billing_entity_id;
  73. }
  74. else {
  75. $location->billing_entity_id = null;
  76. }
  77. $location->customer_id = $customer->id;
  78. $location->name = $request->string("name");
  79. $location->nickname = $request->string("nickname");
  80. $location->phone = $request->string("phone");
  81. $location->email = $request->email("email");
  82. $location->contact_name = $request->string("contact_name");
  83. $location->contact_phone = $request->string("contact_phone");
  84. $location->contact_email = $request->email("contact_email");
  85. $address = $location->getAddress();
  86. $address->address_1 = $request->string("address_1");
  87. $address->address_2 = $request->string("address_2");
  88. $address->city = $request->string("city");
  89. $address->county = $request->string("county");
  90. $address->postal_code = $request->string("postal_code");
  91. $address->country = $request->string("country");
  92. $address->latitude = $request->float("latitude");
  93. $address->longitude = $request->float("longitude");
  94. if(!$location->name) {
  95. throw new RuntimeException(__("Name is required."));
  96. }
  97. if(isset($address->postal_code, $address->country) && !$address->hasGeolocation()) {
  98. $address->geocode();
  99. }
  100. $address->commit();
  101. $location->address_id = $address->id;
  102. $location->commit();
  103. return $location;
  104. }
  105. // Object methods
  106. /**
  107. * @return Customer
  108. * @throws Exception
  109. */
  110. public function getCustomer(): Customer {
  111. return Customer::get($this->customer_id);
  112. }
  113. /**
  114. * @return Address
  115. * @throws Exception
  116. */
  117. public function getAddress(): Address {
  118. $address = Address::get($this->address_id);
  119. return $address ?: new Address();
  120. }
  121. /**
  122. * @return CustomerEntity|null
  123. * @throws Exception
  124. */
  125. public function getBillingEntity(): ?CustomerEntity {
  126. return CustomerEntity::get($this->billing_entity_id);
  127. }
  128. public function getSlug(): string {
  129. return Util::slug("$this->name $this->id");
  130. }
  131. /**
  132. * @return string
  133. * @throws Exception
  134. */
  135. public function getUrl(): string {
  136. return sprintf("/locations/%s/update", $this->getSlug());
  137. }
  138. /**
  139. * @return string
  140. * @throws Exception
  141. */
  142. public function getDisplayName(): string {
  143. $customer = $this->getCustomer();
  144. return sprintf("%s - %s", $customer->name, $this->name);
  145. }
  146. public function getRecentSalesOrders(): array {
  147. $dt_threshold = CarbonImmutable::now($_ENV["TIMEZONE"])->subYear();
  148. return Document::query([
  149. "location_id" => $this->id,
  150. "date" => new MoreThanOrEqualsConstraint($dt_threshold->toDateString()),
  151. "invoice_status" => new NotInConstraint(SalesOrderStatuses::VOID, SalesOrderStatuses::UNCOLLECTIBLE),
  152. "dts_deleted" => null,
  153. ], [
  154. "sort" => "date",
  155. "order" => "desc",
  156. ]);
  157. }
  158. public function getOptionValue(): string {
  159. return $this->getIdString();
  160. }
  161. public function getOptionLabel(): string {
  162. return $this->name ?? "";
  163. }
  164. }