getLocations() as $location) { $options[$location->id] = $location->getDisplayName(); } } return $options; } /** * @param Request $request * @param Location $location * @param Customer|null $customer_override * @return Location * @throws Exception */ public static function updateFromRequest(Request $request, Location $location, ?Customer $customer_override = null): Location { if($customer_override) { $customer = $customer_override; } else { $customer_id = $request->int("customer_id"); $customer = Customer::require($customer_id); } $billing_entity_id = $request->int("billing_entity_id"); if($billing_entity_id) { $billing_entity = CustomerEntity::require($billing_entity_id); if(!$billing_entity->belongsToCustomer($customer)) { throw new RuntimeException(__("The billing entity does not belong to the selected customer.")); } $location->billing_entity_id = $billing_entity_id; } else { $location->billing_entity_id = null; } $location->customer_id = $customer->id; $location->name = $request->string("name"); $location->nickname = $request->string("nickname"); $location->phone = $request->string("phone"); $location->email = $request->email("email"); $location->contact_name = $request->string("contact_name"); $location->contact_phone = $request->string("contact_phone"); $location->contact_email = $request->email("contact_email"); $address = $location->getAddress(); $address->address_1 = $request->string("address_1"); $address->address_2 = $request->string("address_2"); $address->city = $request->string("city"); $address->county = $request->string("county"); $address->postal_code = $request->string("postal_code"); $address->country = $request->string("country"); $address->latitude = $request->float("latitude"); $address->longitude = $request->float("longitude"); if(!$location->name) { throw new RuntimeException(__("Name is required.")); } if(isset($address->postal_code, $address->country) && !$address->hasGeolocation()) { $address->geocode(); } $address->commit(); $location->address_id = $address->id; $location->commit(); return $location; } // Object methods /** * @return Customer * @throws Exception */ public function getCustomer(): Customer { return Customer::get($this->customer_id); } /** * @return Address * @throws Exception */ public function getAddress(): Address { $address = Address::get($this->address_id); return $address ?: new Address(); } /** * @return CustomerEntity|null * @throws Exception */ public function getBillingEntity(): ?CustomerEntity { return CustomerEntity::get($this->billing_entity_id); } public function getSlug(): string { return Util::slug("$this->name $this->id"); } /** * @return string * @throws Exception */ public function getUrl(): string { return sprintf("/locations/%s/update", $this->getSlug()); } /** * @return string * @throws Exception */ public function getDisplayName(): string { $customer = $this->getCustomer(); return sprintf("%s - %s", $customer->name, $this->name); } }