Merge pull request #15161 from snipe/fixes/500_when_depreciation_is_active_but_no_purchase_date

Fixes 500 when depreciation is active but no purchase date
This commit is contained in:
snipe 2024-07-24 20:20:10 +01:00 committed by GitHub
commit 02bd8d7ea1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 86 additions and 10 deletions

View file

@ -158,17 +158,20 @@ class Depreciable extends SnipeModel
public function time_until_depreciated()
{
// @link http://www.php.net/manual/en/class.datetime.php
$d1 = new \DateTime();
$d2 = $this->depreciated_date();
if ($this->depreciated_date()) {
// @link http://www.php.net/manual/en/class.datetime.php
$d1 = new \DateTime();
$d2 = $this->depreciated_date();
// @link http://www.php.net/manual/en/class.dateinterval.php
$interval = $d1->diff($d2);
if (! $interval->invert) {
return $interval;
} else {
return new \DateInterval('PT0S'); //null interval (zero seconds from now)
// @link http://www.php.net/manual/en/class.dateinterval.php
$interval = $d1->diff($d2);
if (! $interval->invert) {
return $interval;
} else {
return new \DateInterval('PT0S'); //null interval (zero seconds from now)
}
}
return false;
}
public function depreciated_date()

View file

@ -50,7 +50,7 @@ class License extends Depreciable
'category_id' => 'required|exists:categories,id',
'company_id' => 'integer|nullable',
'purchase_cost'=> 'numeric|nullable|gte:0',
'purchase_date' => 'date_format:Y-m-d|nullable|max:10',
'purchase_date' => 'date_format:Y-m-d|nullable|max:10|required_with:depreciation_id',
'expiration_date' => 'date_format:Y-m-d|nullable|max:10',
'termination_date' => 'date_format:Y-m-d|nullable|max:10',
'min_amt' => 'numeric|nullable|gte:0',

View file

@ -0,0 +1,42 @@
<?php
namespace Tests\Feature\Consumables\Ui;
use App\Models\AssetModel;
use App\Models\Category;
use App\Models\License;
use App\Models\Depreciation;
use App\Models\User;
use Tests\TestCase;
class CreateLicenseTest extends TestCase
{
public function testPermissionRequiredToViewLicense()
{
$license = License::factory()->create();
$this->actingAs(User::factory()->create())
->get(route('licenses.create', $license))
->assertForbidden();
}
public function testLicenseWithoutPurchaseDateFailsValidation()
{
$response = $this->actingAs(User::factory()->superuser()->create())
->from(route('licenses.create'))
->post(route('licenses.store'), [
'name' => 'Test Invalid License',
'seats' => '10',
'category_id' => Category::factory()->forLicenses()->create()->id,
'depreciation_id' => Depreciation::factory()->create()->id
]);
$response->assertStatus(302);
$response->assertRedirect(route('licenses.create'));
$response->assertInvalid(['purchase_date']);
$response->assertSessionHasErrors(['purchase_date']);
$this->followRedirects($response)->assertSee(trans('general.error'));
$this->assertFalse(AssetModel::where('name', 'Test Invalid License')->exists());
}
}

View file

@ -0,0 +1,31 @@
<?php
namespace Tests\Feature\Consumables\Ui;
use App\Models\License;
use App\Models\Depreciation;
use App\Models\User;
use Tests\TestCase;
class LicenseViewTest extends TestCase
{
public function testPermissionRequiredToViewLicense()
{
$license = License::factory()->create();
$this->actingAs(User::factory()->create())
->get(route('licenses.show', $license))
->assertForbidden();
}
public function testLicenseWithPurchaseDateDepreciatesCorrectly()
{
$depreciation = Depreciation::factory()->create(['months' => 12]);
$license = License::factory()->create(['depreciation_id' => $depreciation->id, 'purchase_date' => '2020-01-01']);
$this->actingAs(User::factory()->superuser()->create())
->get(route('licenses.show', $license))
->assertOk()
->assertSee([
'2021-01-01'
], false);
}
}