Skip to content

A free open source IT asset/license management system

Update models for cast and rules for datetime

snipe04e0d8298aae
diff --git a/app/Importer/AssetImporter.php b/app/Importer/AssetImporter.php
index bd8cce65a7..dd39507cd9 100644
--- a/app/Importer/AssetImporter.php
+++ b/app/Importer/AssetImporter.php
@@ -159,7 +159,9 @@ class AssetImporter extends ItemImporter
}
if ($this->item['expected_checkin'] != '') {
- $item['expected_checkin'] = $this->parseOrNullDate('expected_checkin');
+ // datetime format preserves any time portion in the CSV. Date-only
+ // values still parse fine and land as 00:00:00.
+ $item['expected_checkin'] = $this->parseOrNullDate('expected_checkin', 'datetime');
}
if ($this->item['last_audit_date'] != '') {
diff --git a/app/Models/Asset.php b/app/Models/Asset.php
index e623930c59..d91ad1d22e 100644
--- a/app/Models/Asset.php
+++ b/app/Models/Asset.php
@@ -94,7 +94,7 @@ class Asset extends Depreciable
'eol_explicit' => 'boolean',
'last_checkout' => 'datetime',
'last_checkin' => 'datetime',
- 'expected_checkin' => 'datetime:m-d-Y',
+ 'expected_checkin' => 'datetime',
'last_audit_date' => 'datetime',
'next_audit_date' => 'datetime:m-d-Y',
'model_id' => 'integer',
@@ -241,12 +241,19 @@ class Asset extends Depreciable
});
}
- // To properly set the expected checkin as Y-m-d
public function setExpectedCheckinAttribute($value)
{
if ($value == '') {
$value = null;
}
+ // Normalise to Y-m-d H:i:s so date-only strings ("2026-07-17") end
+ // up as valid datetime values in the DB. On MariaDB the DATETIME
+ // column would widen automatically, but SQLite (used in tests) is
+ // dynamically typed and stores the raw string, which then fails
+ // datetime BETWEEN comparisons in the DueForCheckin scope.
+ if ($value !== null && $value !== '') {
+ $value = Carbon::parse($value)->format('Y-m-d H:i:s');
+ }
$this->attributes['expected_checkin'] = $value;
}
@@ -419,7 +426,7 @@ class Asset extends Depreciable
protected function expectedCheckinFormattedDate(): Attribute
{
return Attribute::make(
- get: fn (mixed $value, array $attributes) => array_key_exists('expected_checkin', $attributes) ? Helper::getFormattedDateObject($attributes['expected_checkin'], 'date', false) : null,
+ get: fn (mixed $value, array $attributes) => array_key_exists('expected_checkin', $attributes) ? Helper::getFormattedDateObject($attributes['expected_checkin'], 'datetime', false) : null,
);
}
@@ -1666,11 +1673,15 @@ class Asset extends Depreciable
public function scopeDueForCheckin($query, $settings)
{
$interval = (int) $settings->due_checkin_days ?? 0;
- $today = Carbon::now();
- $interval_date = $today->copy()->addDays($interval)->format('Y-m-d');
+ // expected_checkin is a DATETIME. Use startOfDay/endOfDay explicitly so
+ // an asset due back on the last day of the interval at (say) 2:30 PM
+ // still falls inside the range. Formatting only Y-m-d silently widens
+ // to midnight and would exclude anything later that day.
+ $windowStart = Carbon::now()->startOfDay();
+ $windowEnd = Carbon::now()->addDays($interval)->endOfDay();
return $query->whereNotNull('assets.expected_checkin')
- ->whereBetween('assets.expected_checkin', [$today->format('Y-m-d'), $interval_date])
+ ->whereBetween('assets.expected_checkin', [$windowStart, $windowEnd])
->where('assets.archived', '=', 0)
->whereNotNull('assets.assigned_to')
->NotArchived();
@@ -1687,8 +1698,12 @@ class Asset extends Depreciable
*/
public function scopeOverdueForCheckin($query)
{
+ // expected_checkin is a DATETIME, so "overdue" is time-aware: an
+ // asset due back this morning at 09:00 is overdue by 09:01. Compare
+ // against Carbon::now() instead of today's midnight so mid-day
+ // dashboard views reflect what's actually late right now.
return $query->whereNotNull('assets.expected_checkin')
- ->where('assets.expected_checkin', '<', Carbon::now()->format('Y-m-d'))
+ ->where('assets.expected_checkin', '<', Carbon::now())
->where('assets.archived', '=', 0)
->whereNotNull('assets.assigned_to')
->NotArchived();
diff --git a/app/Models/CustomField.php b/app/Models/CustomField.php
index df109e3ca9..0c2cae8acb 100644
--- a/app/Models/CustomField.php
+++ b/app/Models/CustomField.php
@@ -31,6 +31,12 @@ class CustomField extends Model
'ALPHA-NUMERIC' => 'alpha_num',
'EMAIL' => 'email',
'DATE' => 'date',
+ // Distinct pattern so getFormatAttribute() can reverse-map back to
+ // 'DATETIME'. If both DATE and DATETIME shared 'date' the first-match
+ // loop would always return 'DATE' and the DATETIME picker never fires.
+ // The datetimepicker widget always outputs Y-m-d H:i:s, so this
+ // date_format is the exact shape we expect.
+ 'DATETIME' => 'date_format:Y-m-d H:i:s',
'URL' => 'url',
'IP' => 'ip',
'IPV4' => 'ipv4',
diff --git a/app/Models/Maintenance.php b/app/Models/Maintenance.php
index 1e83a6b1c1..381f048b9d 100644
--- a/app/Models/Maintenance.php
+++ b/app/Models/Maintenance.php
@@ -42,8 +42,8 @@ class Maintenance extends SnipeModel implements ICompanyableChild
'maintenance_type_id' => 'required|integer|exists:maintenance_types,id',
'name' => 'required|max:100',
'is_warranty' => 'boolean',
- 'start_date' => 'required|date_format:Y-m-d',
- 'completion_date' => 'date_format:Y-m-d|nullable|after_or_equal:start_date',
+ 'start_date' => 'required|date',
+ 'completion_date' => 'date|nullable|after_or_equal:start_date',
'notes' => 'string|nullable',
'cost' => 'numeric|nullable|gte:0|max:99999999999999999.99',
'url' => 'nullable|url|max:255',
@@ -51,6 +51,11 @@ class Maintenance extends SnipeModel implements ICompanyableChild
'completed_by' => 'nullable|integer|exists:users,id',
];
+ protected $casts = [
+ 'start_date' => 'datetime',
+ 'completion_date' => 'datetime',
+ ];
+
/**
* The attributes that are mass assignable.
*
@@ -167,7 +172,7 @@ class Maintenance extends SnipeModel implements ICompanyableChild
public function setCompletionDateAttribute($value)
{
- if ($value == '' || $value == '0000-00-00') {
+ if ($value == '' || $value == '0000-00-00' || $value == '0000-00-00 00:00:00') {
$value = null;
}
$this->attributes['completion_date'] = $value;