|
|
@@ -0,0 +1,112 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace App;
|
|
|
+
|
|
|
+use App\Constraint\NotInConstraint;
|
|
|
+use App\Enums\SalesOrderStatuses;
|
|
|
+use App\Traits\DateTimeTrait;
|
|
|
+use App\Traits\SoftDeleteTrait;
|
|
|
+use Exception;
|
|
|
+use RuntimeException;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @property int $id
|
|
|
+ * @property int $customer_id
|
|
|
+ * @property int $address_id
|
|
|
+ * @property string $name
|
|
|
+ * @property string $phone
|
|
|
+ * @property string $fax
|
|
|
+ * @property string $email
|
|
|
+ * @property string $vat_number
|
|
|
+ */
|
|
|
+
|
|
|
+class CustomerEntity extends Model {
|
|
|
+
|
|
|
+ protected static $table = "customer_entity";
|
|
|
+ protected static $idField = "id";
|
|
|
+ protected static $fillableFields = ["customer_id", "address_id", "name", "phone", "fax", "email", "vat_number", "dts_created", "dts_updated", "dts_deleted"];
|
|
|
+
|
|
|
+ use DateTimeTrait;
|
|
|
+ use SoftDeleteTrait;
|
|
|
+
|
|
|
+ // Static methods
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param Request $request
|
|
|
+ * @param CustomerEntity $entity
|
|
|
+ * @return CustomerEntity
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public static function updateFromRequest(Request $request, CustomerEntity $entity): CustomerEntity {
|
|
|
+ $address = $entity->getAddress();
|
|
|
+
|
|
|
+ $entity->name = $request->string("name");
|
|
|
+ $entity->phone = $request->string("phone");
|
|
|
+ $entity->email = $request->email("email");
|
|
|
+ $entity->vat_number = $request->string("vat_number");
|
|
|
+
|
|
|
+ $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(!$entity->name) {
|
|
|
+ throw new RuntimeException(__("Name is required."));
|
|
|
+ }
|
|
|
+
|
|
|
+ $address->commit();
|
|
|
+
|
|
|
+ $entity->address_id = $address->id;
|
|
|
+ $entity->commit();
|
|
|
+
|
|
|
+ return $entity;
|
|
|
+ }
|
|
|
+
|
|
|
+ // Object methods
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @return Customer
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public function getCustomer(): Customer {
|
|
|
+ return Customer::require($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");
|
|
|
+ }
|
|
|
+
|
|
|
+ public function getUrl(): string {
|
|
|
+ return sprintf("/customers/%s/entities/%s/update", $this->getCustomer()->getSlug(), $this->getSlug());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param array $nvp
|
|
|
+ * @return Location[]
|
|
|
+ */
|
|
|
+ public function getLocations(array $nvp = []): array {
|
|
|
+ $nvp = array_merge($nvp, [
|
|
|
+ "customer_entity_id" => $this->id,
|
|
|
+ "dts_deleted" => null
|
|
|
+ ]);
|
|
|
+
|
|
|
+ return Location::query($nvp, [
|
|
|
+ "sort" => "name"
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|