snipe-it/app/Models/Depreciable.php

188 lines
5.5 KiB
PHP
Raw Normal View History

2016-03-25 01:18:05 -07:00
<?php
2016-03-25 01:18:05 -07:00
namespace App\Models;
Partialize forms (#2884) * Consolidate edit form elements into reusable partials. This is a large code change that doesn't do much immediately. It refactors all of the various edit.blade.php files to reference standardized partials, so that they all reference the same base html layout. This has the side effect of moving everything to the new fancy "required" indicators, and making things look consistent. In addition, I've gone ahead and renamed a few database fields. We had Assetmodel::modelno and Consumable::model_no, I've renamed both to model_number. We had items using ::note and ::notes, I've standardized on ::notes. Component used total_qty where consumables and accessories used qty, so I've moved everything to qty (And fixed a few bugs in the helper file in the process. TODO includes looking at how/where to place the modal javascripts to allow for on the fly creation from all places, rather than just the asset page. Rename assetmodel::modelno to model_number for clarity and consistency Rename consumable::model_no to model_number for clarity and consistency Rename assetmodel::note to notes for clarity and consistency Port asset and assetmodel to new partials layout. Adapt all code to the renamed model_number and notes database changes. Fix some stying. * Share a settings variable with all views. * Allow editing the per_page setting. We showed the value, but we never showed it on the edit page.. * use snipeSettings in all views instead of the long ugly path. * War on partials. Centralize all bootstrap table javascript * Use model_number instead of modelno in importer * Codacy fix. * More unification/deduplication. Create an edit form template layout that we use as the base for all edit forms. This gives the same interface for editing everything and makes the edit.blade.* files much easier to read. * Use a ViewComposer instead of sharing the variable directly. Fixes artisan optimize trying to hit the db--which ruins new installs * Fix DB seeder. * Base sql dump and csv's to import data from for tests. * Start some functional tests for creating items. * Add functional tests for all create methods. Still need to do tests for edits, deletes, and lots of other things * Improvements to functional tests. Use the built in DB seeding mechanism instead of doing it ourselves. Break the tests into multiple units, rather than testing everything in each function. * Some improvements to acceptance tests. Make sure we're only looking at the "trs" within the bootstrap table. Creation of assets is now tested at the functional level (and is faster) so ignore it here. I'm testing acceptance tests with the IMPORT_{ASSETS,ACCESSORIES,CONSUMABLES}.csv in the tests/_data folder imported. * A few things to make acceptance tests work. Add a name to the companies table, and make the locations table have the correct name * Use a .env.tests file for testing functional and unit to allow a separate database. * Add functional tests for compoents, groups, and licenses. * Now that the config is in the functional.yml, this just confuses things. * Start some functional tests for creating items. * Add functional tests for all create methods. Still need to do tests for edits, deletes, and lots of other things * Improvements to functional tests. Use the built in DB seeding mechanism instead of doing it ourselves. Break the tests into multiple units, rather than testing everything in each function. * Some improvements to acceptance tests. Make sure we're only looking at the "trs" within the bootstrap table. Creation of assets is now tested at the functional level (and is faster) so ignore it here. I'm testing acceptance tests with the IMPORT_{ASSETS,ACCESSORIES,CONSUMABLES}.csv in the tests/_data folder imported. * update db dump * Update tests to new reality * env for the test setup * only load the database at beginning of tests, not between each Functional test. * Fix a miss from renaming note to notes. * Set Termination date when creating an asset. It was only set on edit. * Rename serial_number to serial in components for consistency. * Update validation rules to match limits in database. Currently we just accepted the values and they were truncated when adding to DB. * Much more detailed functional testing of creating items. This checks to make sure all values on form have been successfully persisted to database.
2016-11-16 16:56:57 -08:00
class Depreciable extends SnipeModel
2016-03-25 01:18:05 -07:00
{
/**
* Depreciation Relation, and associated helper methods
*/
2016-03-25 01:18:05 -07:00
//REQUIRES a purchase_date field
// and a purchase_cost field
//REQUIRES a get_depreciation method,
//which will return the deprecation.
//this is needed because assets get
//their depreciation from a model,
//whereas licenses have deprecations
//directly associated with them.
//assets will override the following
//two methods in order to inherit from
//their model instead of directly (like
//here)
public function depreciation()
{
return $this->belongsTo(\App\Models\Depreciation::class, 'depreciation_id');
2016-03-25 01:18:05 -07:00
}
public function get_depreciation()
{
return $this->depreciation;
}
/**
* @return float|int
*/
public function getDepreciatedValue()
{
if (! $this->get_depreciation()) { // will never happen
2016-03-25 01:18:05 -07:00
return $this->purchase_cost;
}
if ($this->get_depreciation()->months <= 0) {
return $this->purchase_cost;
}
$depreciation = 0;
$setting = Setting::getSettings();
switch ($setting->depreciation_method) {
case 'half_1':
$depreciation = $this->getHalfYearDepreciatedValue(true);
break;
case 'half_2':
$depreciation = $this->getHalfYearDepreciatedValue(false);
break;
2016-03-25 01:18:05 -07:00
default:
$depreciation = $this->getLinearDepreciatedValue();
}
return $depreciation;
}
/**
* @return float|int
*/
public function getLinearDepreciatedValue() // TODO - for testing it might be nice to have an optional $relative_to param here, defaulted to 'now'
{
if (($this->get_depreciation()) && ($this->purchase_date)) {
$months_passed = ($this->purchase_date->diff(now())->m)+($this->purchase_date->diff(now())->y*12);
} else {
return null;
}
if ($months_passed >= $this->get_depreciation()->months){
//if there is a floor use it
2022-10-17 13:15:10 -07:00
if(!$this->get_depreciation()->depreciation_min == null) {
2016-03-25 01:18:05 -07:00
$current_value = $this->get_depreciation()->depreciation_min;
}else{
$current_value = 0;
}
}
else {
// The equation here is (Purchase_Cost-Floor_min)*(Months_passed/Months_til_depreciated)
$current_value = round(($this->purchase_cost-($this->purchase_cost - ($this->get_depreciation()->depreciation_min)) * ($months_passed / $this->get_depreciation()->months)), 2);
2016-03-25 01:18:05 -07:00
}
2016-03-25 01:18:05 -07:00
return $current_value;
}
public function getMonthlyDepreciation(){
return ($this->purchase_cost-$this->get_depreciation()->depreciation_min)/$this->get_depreciation()->months;
}
/**
* @param onlyHalfFirstYear Boolean always applied only second half of the first year
* @return float|int
*/
public function getHalfYearDepreciatedValue($onlyHalfFirstYear = false)
{
// @link http://www.php.net/manual/en/class.dateinterval.php
$current_date = $this->getDateTime();
$purchase_date = date_create($this->purchase_date);
$currentYear = $this->get_fiscal_year($current_date);
$purchaseYear = $this->get_fiscal_year($purchase_date);
$yearsPast = $currentYear - $purchaseYear;
$deprecationYears = ceil($this->get_depreciation()->months / 12);
if ($onlyHalfFirstYear) {
$yearsPast -= 0.5;
} elseif (! $this->is_first_half_of_year($purchase_date)) {
$yearsPast -= 0.5;
}
if (! $this->is_first_half_of_year($current_date)) {
$yearsPast += 0.5;
}
if ($yearsPast >= $deprecationYears) {
$yearsPast = $deprecationYears;
} elseif ($yearsPast < 0) {
$yearsPast = 0;
}
return $this->purchase_cost - round($yearsPast / $deprecationYears * $this->purchase_cost, 2);
}
/**
* @param \DateTime $date
* @return int
*/
protected function get_fiscal_year($date)
{
$year = intval($date->format('Y'));
// also, maybe it'll have to set fiscal year date
if ($date->format('nj') === '1231') {
return $year;
} else {
return $year - 1;
}
}
/**
* @param \DateTime $date
* @return bool
*/
protected function is_first_half_of_year($date)
{
$date0m0d = intval($date->format('md'));
return ($date0m0d < 601) || ($date0m0d >= 1231);
}
2016-03-25 01:18:05 -07:00
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) {
2016-03-25 01:18:05 -07:00
return $interval;
} else {
return new \DateInterval('PT0S'); //null interval (zero seconds from now)
2016-03-25 01:18:05 -07:00
}
}
public function depreciated_date()
{
$date = date_create($this->purchase_date);
date_add($date, date_interval_create_from_date_string($this->get_depreciation()->months.' months'));
2016-03-25 01:18:05 -07:00
return $date; //date_format($date, 'Y-m-d'); //don't bake-in format, for internationalization
}
// it's necessary for unit tests
protected function getDateTime($time = null)
{
return new \DateTime($time);
}
2016-03-25 01:18:05 -07:00
}