Location.php 4.7 KB

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