Przeglądaj źródła

Add predicted sold date field for sales

Alan Lam 3 lat temu
rodzic
commit
d299e468d2

+ 1 - 0
controllers/sale.php

@@ -123,6 +123,7 @@ $controller = new class($number) {
 			$sale->status = $this->request->int("status");
 			$sale->currency = $this->request->string("currency");
 			$sale->tax_rate = $this->request->decimal("tax_rate", 2);
+			$sale->date_predicted_sold = $this->request->date("date_predicted_sold");
 
 			if(!SaleStatuses::validate($sale->status)) {
 				throw new RuntimeException(__("Status is invalid."));

+ 12 - 0
db/migrations/20221224133703_add_date_predicted_sold_field_to_sale_table.php

@@ -0,0 +1,12 @@
+<?php
+declare(strict_types=1);
+
+use Phinx\Migration\AbstractMigration;
+
+final class AddDatePredictedSoldFieldToSaleTable extends AbstractMigration {
+	public function change(): void {
+		$this->table("sale")
+			->addColumn("date_predicted_sold", "date", ["null" => true, "after" => "tax_rate"])
+			->update();
+	}
+}

+ 9 - 0
engine/Request.php

@@ -200,5 +200,14 @@ class Request {
 		return null;
 	}
 
+	public function date(string $name, string $format = "Y-m-d"): ?string {
+		$value = $this->data[$name] ?? null;
+		if($value) {
+			$dt = CarbonImmutable::createFromFormat($format, $value);
+			return $dt ? (string) $dt->toDateString() : null;
+		}
+		return null;
+	}
+
 
 }

+ 12 - 0
engine/Sale.php

@@ -6,8 +6,10 @@ use App\Enums\DocumentTypes;
 use App\Enums\SaleStatuses;
 use App\Traits\DateTimeTrait;
 use App\Traits\SoftDeleteTrait;
+use Carbon\CarbonImmutable;
 
 /**
+ * Sale Lead
  * @property int $id
  * @property int $company_id
  * @property int $location_id
@@ -15,6 +17,7 @@ use App\Traits\SoftDeleteTrait;
  * @property int $status
  * @property string $currency
  * @property string $tax_rate
+ * @property string $date_predicted_sold
  */
 class Sale extends Model {
 
@@ -23,6 +26,7 @@ class Sale extends Model {
 	protected static $fillableFields = [
 		"company_id", "location_id", "user_id",
 		"status", "currency", "tax_rate",
+		"date_predicted_sold",
 		"dts_created", "dts_updated", "dts_deleted"
 	];
 
@@ -128,4 +132,12 @@ class Sale extends Model {
 		return __("Sale %d - %s", $this->getNumber(), $this->getLocation()->getDisplayName());
 	}
 
+	public function getDisplayPredictedSoldDate(string $format = "d/m/Y"): string {
+		if(!$this->date_predicted_sold) {
+			return "";
+		}
+
+		return CarbonImmutable::parse($this->date_predicted_sold)->format($format);
+	}
+
 }

+ 5 - 0
views/sale.php

@@ -70,6 +70,11 @@ $user = $core->getUser();
 					</select>
 				</div>
 
+				<div class="mb-3">
+					<label class="form-label" for="date-predicted-sold"><?=htmlspecialchars(__("Predicted Sold Date"))?></label>
+					<input class="form-control" type="date" name="date_predicted_sold" id="date-predicted-sold" value="<?=htmlspecialchars($sale->date_predicted_sold)?>" />
+				</div>
+
 				<dl>
 					<dt><?=htmlspecialchars(__("Location"))?></dt>
 					<dd><?=htmlspecialchars($sale->getLocation()->getDisplayName())?></dd>

+ 7 - 2
views/sales_table.php

@@ -8,8 +8,9 @@ namespace App;
 <table class="table table-bordered table-hover mb-3">
 	<colgroup>
 		<col style="width: 1%"/>
-		<col style="width: 50%"/>
-		<col style="width: 50%"/>
+		<col style="width: 33%"/>
+		<col style="width: 33%"/>
+		<col style="width: 33%"/>
 		<col style="width: 1%"/>
 	</colgroup>
 	<thead>
@@ -17,6 +18,7 @@ namespace App;
 		<th><?=htmlspecialchars(__("No."))?></th>
 		<th><?=htmlspecialchars(__("Location"))?></th>
 		<th><?=htmlspecialchars(__("Status"))?></th>
+		<th><?=htmlspecialchars(__("Predicted Sold Date"))?></th>
 		<th><?=htmlspecialchars(__("Actions"))?></th>
 	</tr>
 	</thead>
@@ -37,6 +39,9 @@ namespace App;
 			<td>
 				<?=htmlspecialchars($sale->getDisplayStatus())?>
 			</td>
+			<td>
+				<?=htmlspecialchars($sale->getDisplayPredictedSoldDate())?>
+			</td>
 			<td class="p-1">
 				<a class="btn btn-sm btn-primary" href="<?=htmlspecialchars($sale->getUrl())?>">
 					<?=htmlspecialchars(__("View"))?>