Bladeren bron

Add support for setting an invoice date for a project

Alan Lam 11 maanden geleden
bovenliggende
commit
8b7462a9dc

+ 5 - 1
controllers/project_add.php

@@ -50,6 +50,7 @@ if($submit) {
 		$date_start = HTTP::value("date_start");
 		$date_deadline = HTTP::value("date_deadline");
 		$date_report = HTTP::value("date_report");
+		$date_invoice = HTTP::value("date_invoice", "date");
 
 		$form_exception = new FormException;
 
@@ -81,7 +82,9 @@ if($submit) {
 			if(!$dt_report) $form_exception->addError("Report Month is invalid.");
 		}
 
-		if($dt_start && $dt_deadline && $dt_start > $dt_deadline) $form_exception->addError("Start Date cannot be after the Deadline.");
+		if($dt_start && $dt_deadline && $dt_start > $dt_deadline) {
+			$form_exception->addError("Start Date cannot be after the Deadline.");
+		}
 
 		// Verify location IDs
 		Location::load($location_ids);
@@ -97,6 +100,7 @@ if($submit) {
 		$project->dts_deadline = (string) $dt_deadline->setTime(23, 59, 59);
 		$project->dts_created = DB::dts();
 		$project->dts_report = $dt_report ? (string) $dt_report->setTime(12, 0, 0) : null;
+		$project->invoice_date = $date_invoice ?: null;
 
 		$success = $project->commit();
 		if(!$success) throw new Exception("Project could not be added at this time. Please try again later.");

+ 2 - 0
controllers/project_edit.php

@@ -57,6 +57,7 @@ if($submit) {
 		$date_start = HTTP::value("date_start");
 		$date_deadline = HTTP::value("date_deadline");
 		$date_report = HTTP::value("date_report");
+		$date_invoice = HTTP::value("date_invoice");
 
 		$form_exception = new FormException;
 
@@ -104,6 +105,7 @@ if($submit) {
 		$project->dts_start = (string) $dt_start->setTime(0, 0);
 		$project->dts_deadline = (string) $dt_deadline->setTime(23, 59, 59);
 		$project->dts_report = $dt_report ? (string) $dt_report->setTime(12, 0, 0) : null;
+		$project->invoice_date = $date_invoice ?: null;
 
 		$success = $project->commit();
 		if(!$success) throw new Exception("Project could not be edited at this time. Please try again later.");

+ 13 - 2
controllers/project_invoice.php

@@ -4,6 +4,7 @@
  * @var int $id
  */
 
+use Carbon\CarbonImmutable;
 use \Mpdf\Mpdf;
 
 $project = Project::get($id);
@@ -22,6 +23,16 @@ $vat = $invoice->getTax();
 
 $total_inc_vat = $invoice->getTotalIncTax();
 
+$dt_invoice = CarbonImmutable::now();
+if($project->invoice_date) {
+	$dt_invoice = new CarbonImmutable($project->invoice_date);
+}
+
+$dt_due = $dt_invoice->addDays(30);
+
+$invoice_date = $dt_invoice->format("d/m/Y");
+$due_date = $dt_due->format("d/m/Y");
+
 // Filename
 $reference = $invoice->getReference();
 
@@ -134,11 +145,11 @@ ob_start();
 				<table width="100%" cellspacing="0">
 					<tr>
 						<td width="50%" align="right" color="gray">Date:</td>
-						<td width="50%" align="right"><?=date("d/m/Y")?></td>
+						<td width="50%" align="right"><?=$invoice_date?></td>
 					</tr>
 					<tr>
 						<td align="right" color="gray">Due Date:</td>
-						<td align="right"><?=date("d/m/Y", time() + (30 * 86400))?></td>
+						<td align="right"><?=$due_date?></td>
 					</tr>
 					<tr bgcolor="#eee">
 						<td align="right">Balance Due:</td>

+ 11 - 0
db/migrations/20251130103149_add_invoice_date_field_to_project_table.php

@@ -0,0 +1,11 @@
+<?php
+
+use Phinx\Migration\AbstractMigration;
+
+class AddInvoiceDateFieldToProjectTable extends AbstractMigration {
+	public function change() {
+		$this->table("project")
+			->addColumn("invoice_date", "date", ["null" => true, "after" => "template_overrides_json"])
+			->update();
+	}
+}

+ 8 - 0
engine/http.php

@@ -153,6 +153,14 @@ class HTTP {
 				}, $value) : number_format($value, 2, ".", "");
 			}
 
+			case "date": {
+				if(preg_match("/^\d{4}-\d{2}-\d{2}$/", $value)) {
+					return $value;
+				}
+
+				return null;
+			}
+
 			default: {
 				throw new RuntimeException("Unknown validation type: $validation");
 			}

+ 2 - 1
engine/project.php

@@ -20,6 +20,7 @@ use Carbon\CarbonImmutable;
  * @property int $play_money
  * @property int $play_time
  * @property string $template_overrides_json
+ * @property string $invoice_date
  * @property string $dts_start
  * @property string $dts_deadline
  * @property string $dts_created
@@ -37,7 +38,7 @@ class Project extends Model {
 
 	protected static $table = "project";
 	protected static $id_field = "id";
-	protected static $fillable_fields = ["customer_id", "template_id", "template_keys", "name", "test_count", "min_age", "max_age", "default_customer_fee", "default_retest_fee", "default_tester_fee", "time_of_day", "tester_can_claim", "browse_time", "play_money", "play_time", "template_overrides_json", "dts_start", "dts_deadline", "dts_created", "dts_updated", "dts_report", "dts_finished", "user_id_finished"];
+	protected static $fillable_fields = ["customer_id", "template_id", "template_keys", "name", "test_count", "min_age", "max_age", "default_customer_fee", "default_retest_fee", "default_tester_fee", "time_of_day", "tester_can_claim", "browse_time", "play_money", "play_time", "template_overrides_json", "invoice_date", "dts_start", "dts_deadline", "dts_created", "dts_updated", "dts_report", "dts_finished", "user_id_finished"];
 
 	/**
 	 * @var Test[] $tests

+ 8 - 0
views/project_form.php

@@ -37,6 +37,8 @@ $dt_deadline = $project->getDateTimeDeadline();
 $dt_report = $project->getDateTimeReport() ?: null;
 $date_report = $dt_report ? $dt_report->format("Y-m-d") : "";
 
+$date_invoice = $project->invoice_date;
+
 $name_readonly = $project->id ? "" : "readonly";
 $autogen_checked = $project->id ? "" : "checked";
 
@@ -294,6 +296,12 @@ $output_locations = static function(array $locations, bool $selected) use ($loca
 						<input class="form-control" type="date" name="date_report" id="date-report" value="<?=$date_report?>" />
 					</div>
 				</div>
+				<div class="row mb-1">
+					<label class="col-sm-4 col-form-label" for="date-invoice">Invoice Date</label>
+					<div class="col-sm">
+						<input class="form-control" type="date" name="date_invoice" id="date-invoice" value="<?=$date_invoice?>" />
+					</div>
+				</div>
 
 				<div class="row mb-1">
 					<label class="col-sm-4 col-form-label" for="browse-time">Browse Time (Mins)</label>