فهرست منبع

Add ability to create location with same details when creating a customer

Alan Lam 3 سال پیش
والد
کامیت
81e7392c81
7فایلهای تغییر یافته به همراه151 افزوده شده و 76 حذف شده
  1. 10 25
      controllers/customer_form.php
  2. 2 37
      controllers/location_form.php
  3. 37 0
      engine/Customer.php
  4. 4 0
      engine/Helpers/HtmlHelper.php
  5. 55 0
      engine/Location.php
  6. 14 0
      public/js/entity-form.js
  7. 29 14
      views/entity_form.php

+ 10 - 25
controllers/customer_form.php

@@ -31,39 +31,22 @@ else {
 
 $core->addBreadcrumb($_SERVER["REQUEST_URI"], $title);
 
-$address = $customer->getAddress();
-
 $request = Request::fromPost();
 
 $submit = $request->int("submit");
 if($submit) {
-
 	try {
 		$user->requireValidCsrfToken();
 
-		$customer->name = $request->string("name");
-		$customer->phone = $request->string("phone");
-		$customer->fax = $request->string("fax");
-		$customer->email = $request->email("email");
-		$customer->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(!$customer->name) {
-			throw new RuntimeException(__("Name is required."));
-		}
+		$customer = Customer::updateFromRequest($request, $customer);
 
-		$address->commit();
+		$create_location = $request->int("create_location");
+		if($create_location) {
+			$location = new Location();
 
-		$customer->address_id = $address->id;
-		$customer->commit();
+			$location->dts_created = Database::dts();
+			Location::updateFromRequest($request, $location, $customer);
+		}
 
 		Session::addAlert("success", __("Customer saved successfully."));
 		Http::redirect($customer->getUrl());
@@ -73,10 +56,12 @@ if($submit) {
 	}
 }
 
+$core->addScript("/js/entity-form.js");
+
 View::include("header");
 View::include("entity_form", [
 	"title" => $title,
 	"model" => $customer,
-	"address" => $address
+	"address" => $customer->getAddress()
 ]);
 View::include("footer");

+ 2 - 37
controllers/location_form.php

@@ -36,48 +36,13 @@ else {
 
 $core->addBreadcrumb($_SERVER["REQUEST_URI"], $title);
 
-$address = $location->getAddress();
-
 $submit = $request->int("submit");
 if($submit) {
 
 	try {
 		$user->requireValidCsrfToken();
 
-		$customer_id = $request->int("customer_id");
-		$customer = Customer::require($customer_id);
-
-		$location->customer_id = $customer->id;
-		$location->name = $request->string("name");
-		$location->phone = $request->string("phone");
-		$location->fax = $request->string("fax");
-		$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->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();
+		Location::updateFromRequest($request, $location);
 
 		Session::addAlert("success", __("Location saved successfully."));
 		Http::redirect($location->getUrl());
@@ -91,6 +56,6 @@ View::include("header");
 View::include("entity_form", [
 	"title" => $title,
 	"model" => $location,
-	"address" => $address
+	"address" => $location->getAddress()
 ]);
 View::include("footer");

+ 37 - 0
engine/Customer.php

@@ -4,6 +4,7 @@ namespace App;
 
 use App\Traits\DateTimeTrait;
 use App\Traits\SoftDeleteTrait;
+use RuntimeException;
 
 /**
  * Class Customer
@@ -42,6 +43,42 @@ class Customer extends Model {
 		return $options;
 	}
 
+	/**
+	 * @param Request $request
+	 * @param Customer $customer
+	 * @return Customer
+	 * @throws \Exception
+	 */
+	public static function updateFromRequest(Request $request, Customer $customer): Customer {
+		$address = $customer->getAddress();
+
+		$customer->name = $request->string("name");
+		$customer->phone = $request->string("phone");
+		$customer->fax = $request->string("fax");
+		$customer->email = $request->email("email");
+		$customer->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(!$customer->name) {
+			throw new RuntimeException(__("Name is required."));
+		}
+
+		$address->commit();
+
+		$customer->address_id = $address->id;
+		$customer->commit();
+
+		return $customer;
+	}
+
 	// Object methods
 
 	/**

+ 4 - 0
engine/Helpers/HtmlHelper.php

@@ -28,4 +28,8 @@ class HtmlHelper {
 		return $value ? "readonly" : "";
 	}
 
+	public static function hidden($value): string {
+		return $value ? "hidden" : "";
+	}
+
 }

+ 55 - 0
engine/Location.php

@@ -6,6 +6,7 @@ use App\Enums\AruStatuses;
 use App\Traits\DateTimeTrait;
 use App\Traits\SoftDeleteTrait;
 use Exception;
+use RuntimeException;
 
 /**
  * Class Location
@@ -35,6 +36,60 @@ class Location extends Model {
 	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->phone = $request->string("phone");
+		$location->fax = $request->string("fax");
+		$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

+ 14 - 0
public/js/entity-form.js

@@ -0,0 +1,14 @@
+class EntityForm {
+
+	constructor() {
+
+		$("#create-location").on("change", event => {
+			const checked = event.currentTarget.checked;
+			$("#contact-section").prop("hidden", !checked);
+		});
+
+	}
+
+}
+
+$(() => new EntityForm());

+ 29 - 14
views/entity_form.php

@@ -2,6 +2,8 @@
 
 namespace App;
 
+use App\Helpers\HtmlHelper;
+
 /**
  * @var string $title
  * @var Company|Customer|Location|Supplier $model
@@ -120,23 +122,36 @@ if($is_location) {
 				</div>
 				<?php
 			}
-
-			if($is_location) {
+			
+			if($is_customer && $model->isNew()) {
 				?>
-				<h4><?=htmlspecialchars(__("Contact Person"))?></h4>
-
-				<div class="mb-3">
-					<label class="form-label" for="contact-name"><?=htmlspecialchars(__("Name"))?></label>
-					<input class="form-control" type="text" id="contact-name" name="contact_name" placeholder="<?=htmlspecialchars(__("Type contact name"))?>" value="<?=htmlspecialchars($model->contact_name)?>" />
-				</div>
-				<div class="mb-3">
-					<label class="form-label" for="contact-phone"><?=htmlspecialchars(__("Phone / Mobile"))?></label>
-					<input class="form-control" type="tel" id="contact-phone" name="contact_phone" placeholder="<?=htmlspecialchars(__("Type contact phone or mobile number"))?>" value="<?=htmlspecialchars($model->contact_phone)?>" />
-				</div>
 				<div class="mb-3">
-					<label class="form-label" for="contact-email"><?=htmlspecialchars(__("Email Address"))?></label>
-					<input class="form-control" type="email" id="contact-email" name="contact_email" placeholder="<?=htmlspecialchars(__("Type contact email address"))?>" value="<?=htmlspecialchars($model->contact_email)?>" />
+					<div class="form-check form-switch">
+						<input class="form-check-input" type="checkbox" role="switch" id="create-location" name="create_location" value="1" />
+						<label class="form-check-label" for="create-location"><?= htmlspecialchars(__("Create location with same details")) ?></label>
+					</div>
 				</div>
+				<?php	
+			}
+
+			if($is_location || $is_customer) {
+				?>
+				<section id="contact-section" <?=HtmlHelper::hidden($is_customer)?>>
+					<h4><?=htmlspecialchars(__("Location Contact Person"))?></h4>
+
+					<div class="mb-3">
+						<label class="form-label" for="contact-name"><?=htmlspecialchars(__("Name"))?></label>
+						<input class="form-control" type="text" id="contact-name" name="contact_name" placeholder="<?=htmlspecialchars(__("Type contact name"))?>" value="<?=htmlspecialchars($model->contact_name)?>" />
+					</div>
+					<div class="mb-3">
+						<label class="form-label" for="contact-phone"><?=htmlspecialchars(__("Phone / Mobile"))?></label>
+						<input class="form-control" type="tel" id="contact-phone" name="contact_phone" placeholder="<?=htmlspecialchars(__("Type contact phone or mobile number"))?>" value="<?=htmlspecialchars($model->contact_phone)?>" />
+					</div>
+					<div class="mb-3">
+						<label class="form-label" for="contact-email"><?=htmlspecialchars(__("Email Address"))?></label>
+						<input class="form-control" type="email" id="contact-email" name="contact_email" placeholder="<?=htmlspecialchars(__("Type contact email address"))?>" value="<?=htmlspecialchars($model->contact_email)?>" />
+					</div>
+				</section>
 				<?php
 			}
 			?>