Procházet zdrojové kódy

Add ability to create entities for customers

Alan Lam před 2 roky
rodič
revize
b36b1b6efa

+ 64 - 0
controllers/customer_entity_form.php

@@ -0,0 +1,64 @@
+<?php /** @noinspection PhpUnhandledExceptionInspection */
+
+namespace App;
+
+use RuntimeException;
+
+/**
+ * @var int $customer_id
+ * @var int $id
+ */
+
+$customer = Customer::require($customer_id);
+
+$core = Core::getInstance();
+$core->addBreadcrumb("/customers", __("Customers"));
+$core->addBreadcrumb($customer->getUrl(), $customer->name);
+
+$user = $core->getUser();
+
+if(isset($id)) {
+	$customer_entity = CustomerEntity::require($id);
+
+	$customer_entity->dts_updated = Database::dts();
+
+	$core->addBreadcrumb($customer_entity->getUrl(), $customer_entity->name);
+
+	$title = __("Update Customer Entity");
+}
+else {
+	$customer_entity = new CustomerEntity();
+	$customer_entity->customer_id = $customer_id;
+	$customer_entity->dts_created = Database::dts();
+
+	$title = __("Add Customer Entity");
+}
+
+$core->addBreadcrumb($_SERVER["REQUEST_URI"], $title);
+
+$request = Request::fromPost();
+
+$submit = $request->int("submit");
+if($submit) {
+	try {
+		$user->requireValidCsrfToken();
+
+		$customer_entity = CustomerEntity::updateFromRequest($request, $customer_entity);
+
+		Session::addAlert("success", __("Customer entity saved successfully."));
+		Http::redirect($customer_entity->getUrl());
+	}
+	catch(RuntimeException $e) {
+		Session::addAlert("danger", $e->getMessage());
+	}
+}
+
+$core->addScript("/js/entity-form.js");
+
+View::include("header");
+View::include("entity_form", [
+	"title" => $title,
+	"model" => $customer_entity,
+	"address" => $customer_entity->getAddress()
+]);
+View::include("footer");

+ 23 - 0
db/migrations/20240812165025_add_customer_entity_table.php

@@ -0,0 +1,23 @@
+<?php
+declare(strict_types=1);
+
+use Phinx\Migration\AbstractMigration;
+
+final class AddCustomerEntityTable extends AbstractMigration {
+	public function change(): void {
+		$this->table("customer_entity", ["signed" => false])
+			->addColumn("customer_id", "integer", ["signed" => false])
+			->addColumn("address_id", "integer", ["signed" => false])
+			->addColumn("name", "string")
+			->addColumn("phone", "string", ["null" => true])
+			->addColumn("fax", "string", ["null" => true])
+			->addColumn("email", "string", ["null" => true])
+			->addColumn("vat_number", "string", ["null" => true])
+			->addColumn("dts_created", "datetime")
+			->addColumn("dts_updated", "datetime", ["null" => true])
+			->addColumn("dts_deleted", "datetime", ["null" => true])
+			->addForeignKey("customer_id", "customer", "id", ["update" => "CASCADE", "delete" => "CASCADE"])
+			->addForeignKey("address_id", "address", "id", ["update" => "CASCADE", "delete" => "CASCADE"])
+			->create();
+	}
+}

+ 12 - 0
engine/Customer.php

@@ -99,6 +99,18 @@ class Customer extends Model {
 		return sprintf("/customers/%s", $this->getSlug());
 	}
 
+	/**
+	 * @return CustomerEntity[]
+	 */
+	public function getEntities(): array {
+		return CustomerEntity::query([
+			"customer_id" => $this->id,
+			"dts_deleted" => null
+		], [
+			"sort" => "name"
+		]);
+	}
+
 	/**
 	 * @param array $nvp
 	 * @return Location[]

+ 112 - 0
engine/CustomerEntity.php

@@ -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"
+		]);
+	}
+
+}

+ 3 - 0
routes/default.php

@@ -24,6 +24,9 @@ $this->addRoute("/^customers\/add$/", "customer_form", [], ["requireUser" => tru
 $this->addRoute("/^customers\/$slug_pattern$/", "customer", ["id"], ["requireUser" => true]);
 $this->addRoute("/^customers\/$slug_pattern\/update$/", "customer_form", ["id"], ["requireUser" => true]);
 
+$this->addRoute("/^customers\/$slug_pattern\/entities\/add$/", "customer_entity_form", ["customer_id"], ["requireUser" => true]);
+$this->addRoute("/^customers\/$slug_pattern\/entities\/$slug_pattern\/update$/", "customer_entity_form", ["customer_id", "id"], ["requireUser" => true]);
+
 $this->addRoute("/^companies$/", "companies", [], ["requireUser" => true]);
 $this->addRoute("/^companies\/add$/", "company_form", [], ["requireUser" => true]);
 $this->addRoute("/^companies\/$slug_pattern\/update$/", "company_form", ["id"], ["requireUser" => true]);

+ 67 - 11
views/customer.php

@@ -10,6 +10,7 @@ namespace App;
  */
 
 $locations = $customer->getLocations();
+$entities = $customer->getEntities();
 
 ?>
 
@@ -25,19 +26,20 @@ $locations = $customer->getLocations();
 	</div>
 </div>
 
-<div class="row mb-3 align-items-center">
-	<div class="col">
-		<h4 class="mb-0"><?=htmlspecialchars(__("Locations"))?></h4>
-	</div>
-	<div class="col-auto">
-		<a class="btn btn-success" href="/locations/add?customer_id=<?=$customer->id?>">
-			<i class="fa fa-fw fa-plus"></i>
-			<?=htmlspecialchars(__("Add Location"))?>
-		</a>
+<section class="mb-4">
+
+	<div class="row mb-3 align-items-center">
+		<div class="col">
+			<h4 class="mb-0"><?=htmlspecialchars(__("Locations"))?></h4>
+		</div>
+		<div class="col-auto">
+			<a class="btn btn-success" href="/locations/add?customer_id=<?=$customer->id?>">
+				<i class="fa fa-fw fa-plus"></i>
+				<?=htmlspecialchars(__("Add Location"))?>
+			</a>
+		</div>
 	</div>
-</div>
 
-<section class="mb-4">
 	<?php
 	if(empty($locations)) {
 		?>
@@ -117,3 +119,57 @@ View::include("sale_invoices_section", ["sales_orders" => $customer->getActiveSa
 	}
 	?>
 </section>
+
+<section class="mb-4">
+	<div class="row mb-3 align-items-center">
+		<div class="col">
+			<h4 class="mb-0"><?=htmlspecialchars(__("Entities"))?></h4>
+		</div>
+		<div class="col-auto">
+			<a class="btn btn-success" href="/customers/<?=htmlspecialchars($customer->getSlug())?>/entities/add">
+				<i class="fa fa-fw fa-plus"></i>
+				<?=htmlspecialchars(__("Add Entity"))?>
+			</a>
+		</div>
+	</div>
+
+	<?php
+	if(empty($entities)) {
+		?>
+		<div class="alert alert-info mb-3">
+			<i class="fa fa-fw fa-info-circle"></i>
+			<?=htmlspecialchars(__("No entities to display."))?>
+		</div>
+		<?php
+	}
+	else {
+		?>
+		<table class="table table-bordered table-hover mb-3">
+			<thead>
+			<tr>
+				<th><?=htmlspecialchars(__("Name"))?></th>
+				<th><?=htmlspecialchars(__("Actions"))?></th>
+			</tr>
+			</thead>
+			<tbody>
+			<?php
+			foreach($entities as $entity) {
+				?>
+				<tr>
+					<td><?=htmlspecialchars($entity->name)?></td>
+					<td class="p-1">
+						<a class="btn btn-sm btn-primary" href="<?=htmlspecialchars($entity->getUrl())?>">
+							<?=htmlspecialchars(__("Update"))?>
+						</a>
+					</td>
+				</tr>
+				<?php
+			}
+			?>
+			</tbody>
+		</table>
+		<?php
+	}
+	?>
+
+</section>

+ 5 - 1
views/entity_form.php

@@ -6,7 +6,7 @@ use App\Helpers\HtmlHelper;
 
 /**
  * @var string $title
- * @var Company|Customer|Location|Supplier $model
+ * @var Company|Customer|CustomerEntity|Location|Supplier $model
  * @var Address $address
  */
 
@@ -15,6 +15,7 @@ $user = $core->getUser();
 
 $is_company = $model instanceof Company;
 $is_customer = $model instanceof Customer;
+$is_entity = $model instanceof CustomerEntity;
 $is_location = $model instanceof Location;
 $is_supplier = $model instanceof Supplier;
 
@@ -28,6 +29,9 @@ if($is_location) {
 if($is_supplier) {
 	$name_label = "Supplier Name";
 }
+if($is_entity) {
+	$name_label = "Entity Name";
+}
 
 ?>