You are here

public static function PHPExcel_Calculation_Financial::DDB in Loft Data Grids 6.2

Same name and namespace in other branches
  1. 7.2 vendor/phpoffice/phpexcel/Classes/PHPExcel/Calculation/Financial.php \PHPExcel_Calculation_Financial::DDB()

* DDB * * Returns the depreciation of an asset for a specified period using the * double-declining balance method or some other method you specify. * * Excel Function: * DDB(cost,salvage,life,period[,factor]) * * @access public * @category Financial Functions *

Parameters

float cost Initial cost of the asset.: * @param float salvage Value at the end of the depreciation. * (Sometimes called the salvage value of the asset) * @param integer life Number of periods over which the asset is depreciated. * (Sometimes called the useful life of the asset) * @param integer period The period for which you want to calculate the * depreciation. Period must use the same units as life. * @param float factor The rate at which the balance declines. * If factor is omitted, it is assumed to be 2 (the * double-declining balance method). * @return float

File

vendor/phpoffice/phpexcel/Classes/PHPExcel/Calculation/Financial.php, line 975

Class

PHPExcel_Calculation_Financial
PHPExcel_Calculation_Financial

Code

public static function DDB($cost, $salvage, $life, $period, $factor = 2.0) {
  $cost = PHPExcel_Calculation_Functions::flattenSingleValue($cost);
  $salvage = PHPExcel_Calculation_Functions::flattenSingleValue($salvage);
  $life = PHPExcel_Calculation_Functions::flattenSingleValue($life);
  $period = PHPExcel_Calculation_Functions::flattenSingleValue($period);
  $factor = PHPExcel_Calculation_Functions::flattenSingleValue($factor);

  //	Validate
  if (is_numeric($cost) && is_numeric($salvage) && is_numeric($life) && is_numeric($period) && is_numeric($factor)) {
    $cost = (double) $cost;
    $salvage = (double) $salvage;
    $life = (int) $life;
    $period = (int) $period;
    $factor = (double) $factor;
    if ($cost <= 0 || $salvage / $cost < 0 || $life <= 0 || $period < 1 || $factor <= 0.0 || $period > $life) {
      return PHPExcel_Calculation_Functions::NaN();
    }

    //	Set Fixed Depreciation Rate
    $fixedDepreciationRate = 1 - pow($salvage / $cost, 1 / $life);
    $fixedDepreciationRate = round($fixedDepreciationRate, 3);

    //	Loop through each period calculating the depreciation
    $previousDepreciation = 0;
    for ($per = 1; $per <= $period; ++$per) {
      $depreciation = min(($cost - $previousDepreciation) * ($factor / $life), $cost - $salvage - $previousDepreciation);
      $previousDepreciation += $depreciation;
    }
    if (PHPExcel_Calculation_Functions::getCompatibilityMode() == PHPExcel_Calculation_Functions::COMPATIBILITY_GNUMERIC) {
      $depreciation = round($depreciation, 2);
    }
    return $depreciation;
  }
  return PHPExcel_Calculation_Functions::VALUE();
}