Depreciable.php
Go to the documentation of this file.
1 <?php
2 namespace App\Models;
3 
6 
7 class Depreciable extends Model
8 {
13  //REQUIRES a purchase_date field
14  // and a purchase_cost field
15 
16  //REQUIRES a get_depreciation method,
17  //which will return the deprecation.
18  //this is needed because assets get
19  //their depreciation from a model,
20  //whereas licenses have deprecations
21  //directly associated with them.
22 
23  //assets will override the following
24  //two methods in order to inherit from
25  //their model instead of directly (like
26  //here)
27 
28  public function depreciation()
29  {
30  return $this->belongsTo('\App\Models\Depreciation', 'depreciation_id');
31  }
32 
33  public function get_depreciation()
34  {
35  return $this->depreciation;
36  }
37 
42  public function getDepreciatedValue()
43  {
44  if (!$this->get_depreciation()) { // will never happen
45  return $this->purchase_cost;
46  }
47 
48  if ($this->get_depreciation()->months <= 0) {
49  return $this->purchase_cost;
50  }
51 
52  // fraction of value left
53  $months_remaining = $this->time_until_depreciated()->m + 12*$this->time_until_depreciated()->y; //UGlY
54  $current_value = round(($months_remaining/ $this->get_depreciation()->months) * $this->purchase_cost, 2);
55 
56  if ($current_value < 0) {
57  $current_value = 0;
58  }
59  return $current_value;
60  }
61 
62  public function time_until_depreciated()
63  {
64  // @link http://www.php.net/manual/en/class.datetime.php
65  $d1 = new \DateTime();
66  $d2 = $this->depreciated_date();
67 
68  // @link http://www.php.net/manual/en/class.dateinterval.php
69  $interval = $d1->diff($d2);
70  if (!$interval->invert) {
71  return $interval;
72  } else {
73  return new \DateInterval("PT0S"); //null interval (zero seconds from now)
74  }
75  }
76 
77  public function depreciated_date()
78  {
79  $date = date_create($this->purchase_date);
80  date_add($date, date_interval_create_from_date_string($this->get_depreciation()->months . ' months'));
81  return $date; //date_format($date, 'Y-m-d'); //don't bake-in format, for internationalization
82  }
83 }
depreciation()
Depreciation Relation, and associated helper methods.
Definition: Depreciable.php:28