diff --git a/app/Models/Depreciable.php b/app/Models/Depreciable.php index 721135873..6139cda08 100644 --- a/app/Models/Depreciable.php +++ b/app/Models/Depreciable.php @@ -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() diff --git a/app/Models/License.php b/app/Models/License.php index d8bc3f03b..2680424a8 100755 --- a/app/Models/License.php +++ b/app/Models/License.php @@ -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', diff --git a/tests/Feature/Licenses/Ui/CreateLicenseTest.php b/tests/Feature/Licenses/Ui/CreateLicenseTest.php new file mode 100644 index 000000000..f24c3bd2c --- /dev/null +++ b/tests/Feature/Licenses/Ui/CreateLicenseTest.php @@ -0,0 +1,42 @@ +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()); + + } +} diff --git a/tests/Feature/Licenses/Ui/LicenseViewTest.php b/tests/Feature/Licenses/Ui/LicenseViewTest.php new file mode 100644 index 000000000..3b1f7830d --- /dev/null +++ b/tests/Feature/Licenses/Ui/LicenseViewTest.php @@ -0,0 +1,31 @@ +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); + } +}