Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes 500 when depreciation is active but no purchase date #15161

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions app/Models/Depreciable.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,17 +158,20 @@ protected function is_first_half_of_year($date)

public function time_until_depreciated()
{
// @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)
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)
}
}
return false;
}

public function depreciated_date()
Expand Down
2 changes: 1 addition & 1 deletion app/Models/License.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
42 changes: 42 additions & 0 deletions tests/Feature/Licenses/Ui/CreateLicenseTest.php
Original file line number Diff line number Diff line change
@@ -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());

}
}
31 changes: 31 additions & 0 deletions tests/Feature/Licenses/Ui/LicenseViewTest.php
Original file line number Diff line number Diff line change
@@ -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);
}
}
Loading