Explorar el Código

Add `dts_created` field for locations; update location CSV export

Alan Lam hace 7 meses
padre
commit
308302d0cf

+ 1 - 0
controllers/location_add.php

@@ -13,6 +13,7 @@ Core::addBreadcrumb("/account/locations/add", $page_title);
 $location = new Location();
 $location->customer_id = $customer->id;
 $location->country = DEFAULT_COUNTRY;
+$location->dts_created = DB::dts();
 
 $submit = http::value("submit");
 if($submit) {

+ 11 - 0
db/migrations/20260402153903_add_dts_created_field_to_location_table.php

@@ -0,0 +1,11 @@
+<?php
+
+use Phinx\Migration\AbstractMigration;
+
+class AddDtsCreatedFieldToLocationTable extends AbstractMigration {
+	public function change() {
+		$this->table("location")
+			->addColumn("dts_created", "datetime", ["null" => true, "after" => "longitude"])
+			->update();
+	}
+}

+ 17 - 3
engine/exports/location_csv_export.php

@@ -2,15 +2,23 @@
 
 namespace Exports;
 
+use Exception;
+use Location;
+
 class LocationCsvExport {
 
-	/** @var \Location[] */
-	private $locations = [];
+	/** @var Location[] */
+	private array $locations;
 
 	public function __construct(array $locations) {
 		$this->locations = $locations;
 	}
 
+	/**
+	 * @param string $filename
+	 * @return void
+	 * @throws Exception
+	 */
 	public function export(string $filename): void {
 		header("Content-Type: text/csv; charset=utf-8");
 		header("Content-Disposition: attachment; filename=$filename");
@@ -18,10 +26,13 @@ class LocationCsvExport {
 		$handle = fopen("php://output", "wb");
 
 		fputcsv($handle, [
-			"ID", "Internal ID", "City", "Legal Entity", "Brand", "Region", "Area Manager", "Area Manager Email", "Open", "Close", "Name", "Address", "Postcode", "Email", "Latitude", "Longitude", "Archived", "Closed",
+			"ID", "Internal ID", "City", "Legal Entity", "Brand", "Region", "Area Manager", "Area Manager Email", "Open", "Close", "Name", "Address", "Postcode", "Email", "Latitude", "Longitude", "Archived", "Closed", "Created", "Updated", "Updated By"
 		]);
 
 		foreach($this->locations as $location) {
+			$entries = $location->getUpdateLogEntries([], ["limit" => 1]);
+			$entry = $entries[0] ?? null;
+
 			fputcsv($handle, [
 				$location->id,
 				$location->internal_id,
@@ -41,6 +52,9 @@ class LocationCsvExport {
 				$location->longitude,
 				$location->archived,
 				$location->closed,
+				$location->dts_created,
+				$entry ? $entry->dts : "",
+				$entry ? $entry->getUser()->getDisplayName() : "",
 			]);
 		}
 

+ 9 - 6
engine/location.php

@@ -39,6 +39,7 @@
  * @property bool $archived
  * @property double $latitude
  * @property double $longitude
+ * @property string $dts_created
  * @property string $dts_verified
  */
 
@@ -62,7 +63,7 @@ class Location extends Model {
 		"machine_count", "annual_test_count",
 		"do_not_test", "closed", "archived",
 		"latitude", "longitude",
-		"dts_verified"
+		"dts_created", "dts_verified"
 	];
 
 	// Object methods
@@ -195,15 +196,17 @@ class Location extends Model {
 	 * @return LocationUpdateLogEntry[]
 	 * @throws Exception
 	 */
-	public function getUpdateLogEntries() {
-
-		return LocationUpdateLogEntry::query([
+	public function getUpdateLogEntries(array $nvp = [], array $options = []): array {
+		$nvp = array_merge($nvp, [
 			"location_id" => $this->id,
-		], [
+		]);
+
+		$options = array_merge([
 			"sort" => "dts",
 			"order" => "desc",
-		]);
+		], $options);
 
+		return LocationUpdateLogEntry::query($nvp, $options);
 	}
 
 	/**

+ 1 - 0
tasks/import_venues.php

@@ -233,6 +233,7 @@ for($i = 0; $data = fgetcsv($handle); $i++) {
 		$location = new Location();
 		$location->customer_id = $customer_id;
 		$location->internal_id = $internal_id;
+		$location->dts_created = DB::dts();
 	}
 
 	if(!empty($email)) $location->email = $email;