| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- <?php
- namespace App;
- use App\Enums\AruStatuses;
- use App\Traits\DateTimeTrait;
- use App\Traits\SoftDeleteTrait;
- use Exception;
- use RuntimeException;
- /**
- * Class Location
- * @property int $id
- * @property int $customer_id
- * @property int $address_id
- * @property string $name
- * @property string $nickname
- * @property string $phone
- * @property string $fax
- * @property string $email
- * @property string $contact_name
- * @property string $contact_phone
- * @property string $contact_email
- */
- class Location extends Model {
- protected static $table = "location";
- protected static $idField = "id";
- protected static $fillableFields = [
- "customer_id", "address_id",
- "name", "nickname", "phone", "fax", "email",
- "contact_name", "contact_phone", "contact_email",
- "dts_created", "dts_updated", "dts_deleted"
- ];
- use DateTimeTrait;
- use SoftDeleteTrait;
- /**
- * @param Request $request
- * @param Location $location
- * @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);
- }
- $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();
- }
- 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);
- }
- }
|