Просмотр исходного кода

Add ability to set billing entity for a location

Alan Lam 2 лет назад
Родитель
Сommit
6ef3a2c8a0

+ 16 - 4
controllers/sale.php

@@ -249,7 +249,18 @@ $controller = new class($number) {
 		$sale = $this->requireSale();
 		$location = $sale->getLocation();
 		$customer = $location->getCustomer();
-		$address = $location->getAddress();
+		$billing_entity = $location->getBillingEntity();
+
+		$delivery_address = $location->getAddress();
+
+		if($billing_entity) {
+			$billing_address = $billing_entity->getAddress();
+			$billing_name = $billing_entity->name;
+		}
+		else {
+			$billing_address = $delivery_address;
+			$billing_name = $location->name;
+		}
 
 		$item_ids = $this->request->int("sale_item_ids");
 		if(empty($item_ids) || !is_array($item_ids)) {
@@ -258,7 +269,8 @@ $controller = new class($number) {
 
 		$sale_items = $this->validateItemIds($item_ids);
 
-		$address_string = $location->name . "\n" . $address;
+		$billing_address_string = $billing_name . "\n" . $billing_address;
+		$delivery_address_string = $location->name . "\n" . $delivery_address;
 
 		// OK
 		$document = new Document();
@@ -270,8 +282,8 @@ $controller = new class($number) {
 		$document->invoice_status = SalesOrderStatuses::DRAFT;
 		$document->currency = $sale->currency;
 		$document->tax_rate = $sale->tax_rate;
-		$document->billing_address = $address_string;
-		$document->delivery_address = $address_string;
+		$document->billing_address = $billing_address_string;
+		$document->delivery_address = $delivery_address_string;
 		$document->number = Document::getNextNumber(DocumentTypes::SALES_ORDER);
 		$document->date = date("Y-m-d");
 		$document->vat_number = $customer->vat_number;

+ 13 - 0
db/migrations/20240812171148_add_billing_entity_id_field_to_location_table.php

@@ -0,0 +1,13 @@
+<?php
+declare(strict_types=1);
+
+use Phinx\Migration\AbstractMigration;
+
+final class AddBillingEntityIdFieldToLocationTable extends AbstractMigration {
+	public function change(): void {
+		$this->table("location")
+			->addColumn("billing_entity_id", "integer", ["signed" => false, "null" => true, "after" => "address_id"])
+			->addForeignKey("billing_entity_id", "customer_entity", "id", ["update" => "CASCADE", "delete" => "SET NULL"])
+			->update();
+	}
+}

+ 4 - 0
engine/CustomerEntity.php

@@ -109,4 +109,8 @@ class CustomerEntity extends Model {
 		]);
 	}
 
+	public function belongsToCustomer(Customer $customer): bool {
+		return $customer->is($this->customer_id);
+	}
+
 }

+ 22 - 1
engine/Location.php

@@ -13,6 +13,7 @@ use RuntimeException;
  * @property int $id
  * @property int $customer_id
  * @property int $address_id
+ * @property int $billing_entity_id
  * @property string $name
  * @property string $nickname
  * @property string $phone
@@ -28,7 +29,7 @@ class Location extends Model {
 	protected static $table = "location";
 	protected static $idField = "id";
 	protected static $fillableFields = [
-		"customer_id", "address_id",
+		"customer_id", "address_id", "billing_entity_id",
 		"name", "nickname", "phone", "fax", "email",
 		"contact_name", "contact_phone", "contact_email",
 		"dts_created", "dts_updated", "dts_deleted"
@@ -68,6 +69,18 @@ class Location extends Model {
 			$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");
@@ -123,6 +136,14 @@ class Location extends Model {
 		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");
 	}

+ 20 - 0
views/entity_form.php

@@ -122,6 +122,26 @@ if($is_entity) {
 				<input class="form-control" type="email" id="email" name="email" placeholder="<?=htmlspecialchars(__("Type email"))?>" value="<?=htmlspecialchars($model->email)?>" />
 			</div>
 
+			<?php
+			if($is_location) {
+				$customer = $model->getCustomer();
+				$entity_options = [];
+				foreach($customer->getEntities() as $entity) {
+					$entity_options[$entity->id] = $entity->name;
+				}
+				?>
+				<div class="mb-3">
+					<label class="form-label" for="billing-entity-id"><?=htmlspecialchars(__("Default Billing Entity"))?></label>
+					<select class="form-control" name="billing_entity_id" id="billing-entity-id">
+						<option value=""><?=htmlspecialchars($customer->name)?></option>
+						<?php
+						HtmlHelper::renderOptions($entity_options, $model->billing_entity_id);
+						?>
+					</select>
+				</div>
+				<?php
+			}
+			?>
 
 			<div class="row">
 				<?php