Financial.php 96 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheet\Calculation;
  3. use PhpOffice\PhpSpreadsheet\Shared\Date;
  4. class Financial
  5. {
  6. const FINANCIAL_MAX_ITERATIONS = 32;
  7. const FINANCIAL_PRECISION = 1.0e-08;
  8. /**
  9. * isLastDayOfMonth.
  10. *
  11. * Returns a boolean TRUE/FALSE indicating if this date is the last date of the month
  12. *
  13. * @param \DateTime $testDate The date for testing
  14. *
  15. * @return bool
  16. */
  17. private static function isLastDayOfMonth(\DateTime $testDate)
  18. {
  19. return $testDate->format('d') == $testDate->format('t');
  20. }
  21. private static function couponFirstPeriodDate($settlement, $maturity, $frequency, $next)
  22. {
  23. $months = 12 / $frequency;
  24. $result = Date::excelToDateTimeObject($maturity);
  25. $eom = self::isLastDayOfMonth($result);
  26. while ($settlement < Date::PHPToExcel($result)) {
  27. $result->modify('-' . $months . ' months');
  28. }
  29. if ($next) {
  30. $result->modify('+' . $months . ' months');
  31. }
  32. if ($eom) {
  33. $result->modify('-1 day');
  34. }
  35. return Date::PHPToExcel($result);
  36. }
  37. private static function isValidFrequency($frequency)
  38. {
  39. if (($frequency == 1) || ($frequency == 2) || ($frequency == 4)) {
  40. return true;
  41. }
  42. if ((Functions::getCompatibilityMode() == Functions::COMPATIBILITY_GNUMERIC) &&
  43. (($frequency == 6) || ($frequency == 12))) {
  44. return true;
  45. }
  46. return false;
  47. }
  48. /**
  49. * daysPerYear.
  50. *
  51. * Returns the number of days in a specified year, as defined by the "basis" value
  52. *
  53. * @param int|string $year The year against which we're testing
  54. * @param int|string $basis The type of day count:
  55. * 0 or omitted US (NASD) 360
  56. * 1 Actual (365 or 366 in a leap year)
  57. * 2 360
  58. * 3 365
  59. * 4 European 360
  60. *
  61. * @return int
  62. */
  63. private static function daysPerYear($year, $basis = 0)
  64. {
  65. switch ($basis) {
  66. case 0:
  67. case 2:
  68. case 4:
  69. $daysPerYear = 360;
  70. break;
  71. case 3:
  72. $daysPerYear = 365;
  73. break;
  74. case 1:
  75. $daysPerYear = (DateTime::isLeapYear($year)) ? 366 : 365;
  76. break;
  77. default:
  78. return Functions::NAN();
  79. }
  80. return $daysPerYear;
  81. }
  82. private static function interestAndPrincipal($rate = 0, $per = 0, $nper = 0, $pv = 0, $fv = 0, $type = 0)
  83. {
  84. $pmt = self::PMT($rate, $nper, $pv, $fv, $type);
  85. $capital = $pv;
  86. for ($i = 1; $i <= $per; ++$i) {
  87. $interest = ($type && $i == 1) ? 0 : -$capital * $rate;
  88. $principal = $pmt - $interest;
  89. $capital += $principal;
  90. }
  91. return [$interest, $principal];
  92. }
  93. /**
  94. * ACCRINT.
  95. *
  96. * Returns the accrued interest for a security that pays periodic interest.
  97. *
  98. * Excel Function:
  99. * ACCRINT(issue,firstinterest,settlement,rate,par,frequency[,basis])
  100. *
  101. * @category Financial Functions
  102. *
  103. * @param mixed $issue the security's issue date
  104. * @param mixed $firstinterest the security's first interest date
  105. * @param mixed $settlement The security's settlement date.
  106. * The security settlement date is the date after the issue date
  107. * when the security is traded to the buyer.
  108. * @param float $rate the security's annual coupon rate
  109. * @param float $par The security's par value.
  110. * If you omit par, ACCRINT uses $1,000.
  111. * @param int $frequency the number of coupon payments per year.
  112. * Valid frequency values are:
  113. * 1 Annual
  114. * 2 Semi-Annual
  115. * 4 Quarterly
  116. * If working in Gnumeric Mode, the following frequency options are
  117. * also available
  118. * 6 Bimonthly
  119. * 12 Monthly
  120. * @param int $basis The type of day count to use.
  121. * 0 or omitted US (NASD) 30/360
  122. * 1 Actual/actual
  123. * 2 Actual/360
  124. * 3 Actual/365
  125. * 4 European 30/360
  126. *
  127. * @return float|string
  128. */
  129. public static function ACCRINT($issue, $firstinterest, $settlement, $rate, $par = 1000, $frequency = 1, $basis = 0)
  130. {
  131. $issue = Functions::flattenSingleValue($issue);
  132. $firstinterest = Functions::flattenSingleValue($firstinterest);
  133. $settlement = Functions::flattenSingleValue($settlement);
  134. $rate = Functions::flattenSingleValue($rate);
  135. $par = ($par === null) ? 1000 : Functions::flattenSingleValue($par);
  136. $frequency = ($frequency === null) ? 1 : Functions::flattenSingleValue($frequency);
  137. $basis = ($basis === null) ? 0 : Functions::flattenSingleValue($basis);
  138. // Validate
  139. if ((is_numeric($rate)) && (is_numeric($par))) {
  140. $rate = (float) $rate;
  141. $par = (float) $par;
  142. if (($rate <= 0) || ($par <= 0)) {
  143. return Functions::NAN();
  144. }
  145. $daysBetweenIssueAndSettlement = DateTime::YEARFRAC($issue, $settlement, $basis);
  146. if (!is_numeric($daysBetweenIssueAndSettlement)) {
  147. // return date error
  148. return $daysBetweenIssueAndSettlement;
  149. }
  150. return $par * $rate * $daysBetweenIssueAndSettlement;
  151. }
  152. return Functions::VALUE();
  153. }
  154. /**
  155. * ACCRINTM.
  156. *
  157. * Returns the accrued interest for a security that pays interest at maturity.
  158. *
  159. * Excel Function:
  160. * ACCRINTM(issue,settlement,rate[,par[,basis]])
  161. *
  162. * @category Financial Functions
  163. *
  164. * @param mixed $issue The security's issue date
  165. * @param mixed $settlement The security's settlement (or maturity) date
  166. * @param float $rate The security's annual coupon rate
  167. * @param float $par The security's par value.
  168. * If you omit par, ACCRINT uses $1,000.
  169. * @param int $basis The type of day count to use.
  170. * 0 or omitted US (NASD) 30/360
  171. * 1 Actual/actual
  172. * 2 Actual/360
  173. * 3 Actual/365
  174. * 4 European 30/360
  175. *
  176. * @return float|string
  177. */
  178. public static function ACCRINTM($issue, $settlement, $rate, $par = 1000, $basis = 0)
  179. {
  180. $issue = Functions::flattenSingleValue($issue);
  181. $settlement = Functions::flattenSingleValue($settlement);
  182. $rate = Functions::flattenSingleValue($rate);
  183. $par = ($par === null) ? 1000 : Functions::flattenSingleValue($par);
  184. $basis = ($basis === null) ? 0 : Functions::flattenSingleValue($basis);
  185. // Validate
  186. if ((is_numeric($rate)) && (is_numeric($par))) {
  187. $rate = (float) $rate;
  188. $par = (float) $par;
  189. if (($rate <= 0) || ($par <= 0)) {
  190. return Functions::NAN();
  191. }
  192. $daysBetweenIssueAndSettlement = DateTime::YEARFRAC($issue, $settlement, $basis);
  193. if (!is_numeric($daysBetweenIssueAndSettlement)) {
  194. // return date error
  195. return $daysBetweenIssueAndSettlement;
  196. }
  197. return $par * $rate * $daysBetweenIssueAndSettlement;
  198. }
  199. return Functions::VALUE();
  200. }
  201. /**
  202. * AMORDEGRC.
  203. *
  204. * Returns the depreciation for each accounting period.
  205. * This function is provided for the French accounting system. If an asset is purchased in
  206. * the middle of the accounting period, the prorated depreciation is taken into account.
  207. * The function is similar to AMORLINC, except that a depreciation coefficient is applied in
  208. * the calculation depending on the life of the assets.
  209. * This function will return the depreciation until the last period of the life of the assets
  210. * or until the cumulated value of depreciation is greater than the cost of the assets minus
  211. * the salvage value.
  212. *
  213. * Excel Function:
  214. * AMORDEGRC(cost,purchased,firstPeriod,salvage,period,rate[,basis])
  215. *
  216. * @category Financial Functions
  217. *
  218. * @param float $cost The cost of the asset
  219. * @param mixed $purchased Date of the purchase of the asset
  220. * @param mixed $firstPeriod Date of the end of the first period
  221. * @param mixed $salvage The salvage value at the end of the life of the asset
  222. * @param float $period The period
  223. * @param float $rate Rate of depreciation
  224. * @param int $basis The type of day count to use.
  225. * 0 or omitted US (NASD) 30/360
  226. * 1 Actual/actual
  227. * 2 Actual/360
  228. * 3 Actual/365
  229. * 4 European 30/360
  230. *
  231. * @return float
  232. */
  233. public static function AMORDEGRC($cost, $purchased, $firstPeriod, $salvage, $period, $rate, $basis = 0)
  234. {
  235. $cost = Functions::flattenSingleValue($cost);
  236. $purchased = Functions::flattenSingleValue($purchased);
  237. $firstPeriod = Functions::flattenSingleValue($firstPeriod);
  238. $salvage = Functions::flattenSingleValue($salvage);
  239. $period = floor(Functions::flattenSingleValue($period));
  240. $rate = Functions::flattenSingleValue($rate);
  241. $basis = ($basis === null) ? 0 : (int) Functions::flattenSingleValue($basis);
  242. // The depreciation coefficients are:
  243. // Life of assets (1/rate) Depreciation coefficient
  244. // Less than 3 years 1
  245. // Between 3 and 4 years 1.5
  246. // Between 5 and 6 years 2
  247. // More than 6 years 2.5
  248. $fUsePer = 1.0 / $rate;
  249. if ($fUsePer < 3.0) {
  250. $amortiseCoeff = 1.0;
  251. } elseif ($fUsePer < 5.0) {
  252. $amortiseCoeff = 1.5;
  253. } elseif ($fUsePer <= 6.0) {
  254. $amortiseCoeff = 2.0;
  255. } else {
  256. $amortiseCoeff = 2.5;
  257. }
  258. $rate *= $amortiseCoeff;
  259. $fNRate = round(DateTime::YEARFRAC($purchased, $firstPeriod, $basis) * $rate * $cost, 0);
  260. $cost -= $fNRate;
  261. $fRest = $cost - $salvage;
  262. for ($n = 0; $n < $period; ++$n) {
  263. $fNRate = round($rate * $cost, 0);
  264. $fRest -= $fNRate;
  265. if ($fRest < 0.0) {
  266. switch ($period - $n) {
  267. case 0:
  268. case 1:
  269. return round($cost * 0.5, 0);
  270. default:
  271. return 0.0;
  272. }
  273. }
  274. $cost -= $fNRate;
  275. }
  276. return $fNRate;
  277. }
  278. /**
  279. * AMORLINC.
  280. *
  281. * Returns the depreciation for each accounting period.
  282. * This function is provided for the French accounting system. If an asset is purchased in
  283. * the middle of the accounting period, the prorated depreciation is taken into account.
  284. *
  285. * Excel Function:
  286. * AMORLINC(cost,purchased,firstPeriod,salvage,period,rate[,basis])
  287. *
  288. * @category Financial Functions
  289. *
  290. * @param float $cost The cost of the asset
  291. * @param mixed $purchased Date of the purchase of the asset
  292. * @param mixed $firstPeriod Date of the end of the first period
  293. * @param mixed $salvage The salvage value at the end of the life of the asset
  294. * @param float $period The period
  295. * @param float $rate Rate of depreciation
  296. * @param int $basis The type of day count to use.
  297. * 0 or omitted US (NASD) 30/360
  298. * 1 Actual/actual
  299. * 2 Actual/360
  300. * 3 Actual/365
  301. * 4 European 30/360
  302. *
  303. * @return float
  304. */
  305. public static function AMORLINC($cost, $purchased, $firstPeriod, $salvage, $period, $rate, $basis = 0)
  306. {
  307. $cost = Functions::flattenSingleValue($cost);
  308. $purchased = Functions::flattenSingleValue($purchased);
  309. $firstPeriod = Functions::flattenSingleValue($firstPeriod);
  310. $salvage = Functions::flattenSingleValue($salvage);
  311. $period = Functions::flattenSingleValue($period);
  312. $rate = Functions::flattenSingleValue($rate);
  313. $basis = ($basis === null) ? 0 : (int) Functions::flattenSingleValue($basis);
  314. $fOneRate = $cost * $rate;
  315. $fCostDelta = $cost - $salvage;
  316. // Note, quirky variation for leap years on the YEARFRAC for this function
  317. $purchasedYear = DateTime::YEAR($purchased);
  318. $yearFrac = DateTime::YEARFRAC($purchased, $firstPeriod, $basis);
  319. if (($basis == 1) && ($yearFrac < 1) && (DateTime::isLeapYear($purchasedYear))) {
  320. $yearFrac *= 365 / 366;
  321. }
  322. $f0Rate = $yearFrac * $rate * $cost;
  323. $nNumOfFullPeriods = (int) (($cost - $salvage - $f0Rate) / $fOneRate);
  324. if ($period == 0) {
  325. return $f0Rate;
  326. } elseif ($period <= $nNumOfFullPeriods) {
  327. return $fOneRate;
  328. } elseif ($period == ($nNumOfFullPeriods + 1)) {
  329. return $fCostDelta - $fOneRate * $nNumOfFullPeriods - $f0Rate;
  330. }
  331. return 0.0;
  332. }
  333. /**
  334. * COUPDAYBS.
  335. *
  336. * Returns the number of days from the beginning of the coupon period to the settlement date.
  337. *
  338. * Excel Function:
  339. * COUPDAYBS(settlement,maturity,frequency[,basis])
  340. *
  341. * @category Financial Functions
  342. *
  343. * @param mixed $settlement The security's settlement date.
  344. * The security settlement date is the date after the issue
  345. * date when the security is traded to the buyer.
  346. * @param mixed $maturity The security's maturity date.
  347. * The maturity date is the date when the security expires.
  348. * @param int $frequency the number of coupon payments per year.
  349. * Valid frequency values are:
  350. * 1 Annual
  351. * 2 Semi-Annual
  352. * 4 Quarterly
  353. * If working in Gnumeric Mode, the following frequency options are
  354. * also available
  355. * 6 Bimonthly
  356. * 12 Monthly
  357. * @param int $basis The type of day count to use.
  358. * 0 or omitted US (NASD) 30/360
  359. * 1 Actual/actual
  360. * 2 Actual/360
  361. * 3 Actual/365
  362. * 4 European 30/360
  363. *
  364. * @return float|string
  365. */
  366. public static function COUPDAYBS($settlement, $maturity, $frequency, $basis = 0)
  367. {
  368. $settlement = Functions::flattenSingleValue($settlement);
  369. $maturity = Functions::flattenSingleValue($maturity);
  370. $frequency = (int) Functions::flattenSingleValue($frequency);
  371. $basis = ($basis === null) ? 0 : (int) Functions::flattenSingleValue($basis);
  372. if (is_string($settlement = DateTime::getDateValue($settlement))) {
  373. return Functions::VALUE();
  374. }
  375. if (is_string($maturity = DateTime::getDateValue($maturity))) {
  376. return Functions::VALUE();
  377. }
  378. if (($settlement > $maturity) ||
  379. (!self::isValidFrequency($frequency)) ||
  380. (($basis < 0) || ($basis > 4))) {
  381. return Functions::NAN();
  382. }
  383. $daysPerYear = self::daysPerYear(DateTime::YEAR($settlement), $basis);
  384. $prev = self::couponFirstPeriodDate($settlement, $maturity, $frequency, false);
  385. return DateTime::YEARFRAC($prev, $settlement, $basis) * $daysPerYear;
  386. }
  387. /**
  388. * COUPDAYS.
  389. *
  390. * Returns the number of days in the coupon period that contains the settlement date.
  391. *
  392. * Excel Function:
  393. * COUPDAYS(settlement,maturity,frequency[,basis])
  394. *
  395. * @category Financial Functions
  396. *
  397. * @param mixed $settlement The security's settlement date.
  398. * The security settlement date is the date after the issue
  399. * date when the security is traded to the buyer.
  400. * @param mixed $maturity The security's maturity date.
  401. * The maturity date is the date when the security expires.
  402. * @param mixed $frequency the number of coupon payments per year.
  403. * Valid frequency values are:
  404. * 1 Annual
  405. * 2 Semi-Annual
  406. * 4 Quarterly
  407. * If working in Gnumeric Mode, the following frequency options are
  408. * also available
  409. * 6 Bimonthly
  410. * 12 Monthly
  411. * @param int $basis The type of day count to use.
  412. * 0 or omitted US (NASD) 30/360
  413. * 1 Actual/actual
  414. * 2 Actual/360
  415. * 3 Actual/365
  416. * 4 European 30/360
  417. *
  418. * @return float|string
  419. */
  420. public static function COUPDAYS($settlement, $maturity, $frequency, $basis = 0)
  421. {
  422. $settlement = Functions::flattenSingleValue($settlement);
  423. $maturity = Functions::flattenSingleValue($maturity);
  424. $frequency = (int) Functions::flattenSingleValue($frequency);
  425. $basis = ($basis === null) ? 0 : (int) Functions::flattenSingleValue($basis);
  426. if (is_string($settlement = DateTime::getDateValue($settlement))) {
  427. return Functions::VALUE();
  428. }
  429. if (is_string($maturity = DateTime::getDateValue($maturity))) {
  430. return Functions::VALUE();
  431. }
  432. if (($settlement > $maturity) ||
  433. (!self::isValidFrequency($frequency)) ||
  434. (($basis < 0) || ($basis > 4))) {
  435. return Functions::NAN();
  436. }
  437. switch ($basis) {
  438. case 3:
  439. // Actual/365
  440. return 365 / $frequency;
  441. case 1:
  442. // Actual/actual
  443. if ($frequency == 1) {
  444. $daysPerYear = self::daysPerYear(DateTime::YEAR($settlement), $basis);
  445. return $daysPerYear / $frequency;
  446. }
  447. $prev = self::couponFirstPeriodDate($settlement, $maturity, $frequency, false);
  448. $next = self::couponFirstPeriodDate($settlement, $maturity, $frequency, true);
  449. return $next - $prev;
  450. default:
  451. // US (NASD) 30/360, Actual/360 or European 30/360
  452. return 360 / $frequency;
  453. }
  454. }
  455. /**
  456. * COUPDAYSNC.
  457. *
  458. * Returns the number of days from the settlement date to the next coupon date.
  459. *
  460. * Excel Function:
  461. * COUPDAYSNC(settlement,maturity,frequency[,basis])
  462. *
  463. * @category Financial Functions
  464. *
  465. * @param mixed $settlement The security's settlement date.
  466. * The security settlement date is the date after the issue
  467. * date when the security is traded to the buyer.
  468. * @param mixed $maturity The security's maturity date.
  469. * The maturity date is the date when the security expires.
  470. * @param mixed $frequency the number of coupon payments per year.
  471. * Valid frequency values are:
  472. * 1 Annual
  473. * 2 Semi-Annual
  474. * 4 Quarterly
  475. * If working in Gnumeric Mode, the following frequency options are
  476. * also available
  477. * 6 Bimonthly
  478. * 12 Monthly
  479. * @param int $basis The type of day count to use.
  480. * 0 or omitted US (NASD) 30/360
  481. * 1 Actual/actual
  482. * 2 Actual/360
  483. * 3 Actual/365
  484. * 4 European 30/360
  485. *
  486. * @return float|string
  487. */
  488. public static function COUPDAYSNC($settlement, $maturity, $frequency, $basis = 0)
  489. {
  490. $settlement = Functions::flattenSingleValue($settlement);
  491. $maturity = Functions::flattenSingleValue($maturity);
  492. $frequency = (int) Functions::flattenSingleValue($frequency);
  493. $basis = ($basis === null) ? 0 : (int) Functions::flattenSingleValue($basis);
  494. if (is_string($settlement = DateTime::getDateValue($settlement))) {
  495. return Functions::VALUE();
  496. }
  497. if (is_string($maturity = DateTime::getDateValue($maturity))) {
  498. return Functions::VALUE();
  499. }
  500. if (($settlement > $maturity) ||
  501. (!self::isValidFrequency($frequency)) ||
  502. (($basis < 0) || ($basis > 4))) {
  503. return Functions::NAN();
  504. }
  505. $daysPerYear = self::daysPerYear(DateTime::YEAR($settlement), $basis);
  506. $next = self::couponFirstPeriodDate($settlement, $maturity, $frequency, true);
  507. return DateTime::YEARFRAC($settlement, $next, $basis) * $daysPerYear;
  508. }
  509. /**
  510. * COUPNCD.
  511. *
  512. * Returns the next coupon date after the settlement date.
  513. *
  514. * Excel Function:
  515. * COUPNCD(settlement,maturity,frequency[,basis])
  516. *
  517. * @category Financial Functions
  518. *
  519. * @param mixed $settlement The security's settlement date.
  520. * The security settlement date is the date after the issue
  521. * date when the security is traded to the buyer.
  522. * @param mixed $maturity The security's maturity date.
  523. * The maturity date is the date when the security expires.
  524. * @param mixed $frequency the number of coupon payments per year.
  525. * Valid frequency values are:
  526. * 1 Annual
  527. * 2 Semi-Annual
  528. * 4 Quarterly
  529. * If working in Gnumeric Mode, the following frequency options are
  530. * also available
  531. * 6 Bimonthly
  532. * 12 Monthly
  533. * @param int $basis The type of day count to use.
  534. * 0 or omitted US (NASD) 30/360
  535. * 1 Actual/actual
  536. * 2 Actual/360
  537. * 3 Actual/365
  538. * 4 European 30/360
  539. *
  540. * @return mixed Excel date/time serial value, PHP date/time serial value or PHP date/time object,
  541. * depending on the value of the ReturnDateType flag
  542. */
  543. public static function COUPNCD($settlement, $maturity, $frequency, $basis = 0)
  544. {
  545. $settlement = Functions::flattenSingleValue($settlement);
  546. $maturity = Functions::flattenSingleValue($maturity);
  547. $frequency = (int) Functions::flattenSingleValue($frequency);
  548. $basis = ($basis === null) ? 0 : (int) Functions::flattenSingleValue($basis);
  549. if (is_string($settlement = DateTime::getDateValue($settlement))) {
  550. return Functions::VALUE();
  551. }
  552. if (is_string($maturity = DateTime::getDateValue($maturity))) {
  553. return Functions::VALUE();
  554. }
  555. if (($settlement > $maturity) ||
  556. (!self::isValidFrequency($frequency)) ||
  557. (($basis < 0) || ($basis > 4))) {
  558. return Functions::NAN();
  559. }
  560. return self::couponFirstPeriodDate($settlement, $maturity, $frequency, true);
  561. }
  562. /**
  563. * COUPNUM.
  564. *
  565. * Returns the number of coupons payable between the settlement date and maturity date,
  566. * rounded up to the nearest whole coupon.
  567. *
  568. * Excel Function:
  569. * COUPNUM(settlement,maturity,frequency[,basis])
  570. *
  571. * @category Financial Functions
  572. *
  573. * @param mixed $settlement The security's settlement date.
  574. * The security settlement date is the date after the issue
  575. * date when the security is traded to the buyer.
  576. * @param mixed $maturity The security's maturity date.
  577. * The maturity date is the date when the security expires.
  578. * @param mixed $frequency the number of coupon payments per year.
  579. * Valid frequency values are:
  580. * 1 Annual
  581. * 2 Semi-Annual
  582. * 4 Quarterly
  583. * If working in Gnumeric Mode, the following frequency options are
  584. * also available
  585. * 6 Bimonthly
  586. * 12 Monthly
  587. * @param int $basis The type of day count to use.
  588. * 0 or omitted US (NASD) 30/360
  589. * 1 Actual/actual
  590. * 2 Actual/360
  591. * 3 Actual/365
  592. * 4 European 30/360
  593. *
  594. * @return int|string
  595. */
  596. public static function COUPNUM($settlement, $maturity, $frequency, $basis = 0)
  597. {
  598. $settlement = Functions::flattenSingleValue($settlement);
  599. $maturity = Functions::flattenSingleValue($maturity);
  600. $frequency = (int) Functions::flattenSingleValue($frequency);
  601. $basis = ($basis === null) ? 0 : (int) Functions::flattenSingleValue($basis);
  602. if (is_string($settlement = DateTime::getDateValue($settlement))) {
  603. return Functions::VALUE();
  604. }
  605. if (is_string($maturity = DateTime::getDateValue($maturity))) {
  606. return Functions::VALUE();
  607. }
  608. if (($settlement > $maturity) ||
  609. (!self::isValidFrequency($frequency)) ||
  610. (($basis < 0) || ($basis > 4))) {
  611. return Functions::NAN();
  612. }
  613. $settlement = self::couponFirstPeriodDate($settlement, $maturity, $frequency, true);
  614. $daysBetweenSettlementAndMaturity = DateTime::YEARFRAC($settlement, $maturity, $basis) * 365;
  615. switch ($frequency) {
  616. case 1: // annual payments
  617. return ceil($daysBetweenSettlementAndMaturity / 360);
  618. case 2: // half-yearly
  619. return ceil($daysBetweenSettlementAndMaturity / 180);
  620. case 4: // quarterly
  621. return ceil($daysBetweenSettlementAndMaturity / 90);
  622. case 6: // bimonthly
  623. return ceil($daysBetweenSettlementAndMaturity / 60);
  624. case 12: // monthly
  625. return ceil($daysBetweenSettlementAndMaturity / 30);
  626. }
  627. return Functions::VALUE();
  628. }
  629. /**
  630. * COUPPCD.
  631. *
  632. * Returns the previous coupon date before the settlement date.
  633. *
  634. * Excel Function:
  635. * COUPPCD(settlement,maturity,frequency[,basis])
  636. *
  637. * @category Financial Functions
  638. *
  639. * @param mixed $settlement The security's settlement date.
  640. * The security settlement date is the date after the issue
  641. * date when the security is traded to the buyer.
  642. * @param mixed $maturity The security's maturity date.
  643. * The maturity date is the date when the security expires.
  644. * @param mixed $frequency the number of coupon payments per year.
  645. * Valid frequency values are:
  646. * 1 Annual
  647. * 2 Semi-Annual
  648. * 4 Quarterly
  649. * If working in Gnumeric Mode, the following frequency options are
  650. * also available
  651. * 6 Bimonthly
  652. * 12 Monthly
  653. * @param int $basis The type of day count to use.
  654. * 0 or omitted US (NASD) 30/360
  655. * 1 Actual/actual
  656. * 2 Actual/360
  657. * 3 Actual/365
  658. * 4 European 30/360
  659. *
  660. * @return mixed Excel date/time serial value, PHP date/time serial value or PHP date/time object,
  661. * depending on the value of the ReturnDateType flag
  662. */
  663. public static function COUPPCD($settlement, $maturity, $frequency, $basis = 0)
  664. {
  665. $settlement = Functions::flattenSingleValue($settlement);
  666. $maturity = Functions::flattenSingleValue($maturity);
  667. $frequency = (int) Functions::flattenSingleValue($frequency);
  668. $basis = ($basis === null) ? 0 : (int) Functions::flattenSingleValue($basis);
  669. if (is_string($settlement = DateTime::getDateValue($settlement))) {
  670. return Functions::VALUE();
  671. }
  672. if (is_string($maturity = DateTime::getDateValue($maturity))) {
  673. return Functions::VALUE();
  674. }
  675. if (($settlement > $maturity) ||
  676. (!self::isValidFrequency($frequency)) ||
  677. (($basis < 0) || ($basis > 4))) {
  678. return Functions::NAN();
  679. }
  680. return self::couponFirstPeriodDate($settlement, $maturity, $frequency, false);
  681. }
  682. /**
  683. * CUMIPMT.
  684. *
  685. * Returns the cumulative interest paid on a loan between the start and end periods.
  686. *
  687. * Excel Function:
  688. * CUMIPMT(rate,nper,pv,start,end[,type])
  689. *
  690. * @category Financial Functions
  691. *
  692. * @param float $rate The Interest rate
  693. * @param int $nper The total number of payment periods
  694. * @param float $pv Present Value
  695. * @param int $start The first period in the calculation.
  696. * Payment periods are numbered beginning with 1.
  697. * @param int $end the last period in the calculation
  698. * @param int $type A number 0 or 1 and indicates when payments are due:
  699. * 0 or omitted At the end of the period.
  700. * 1 At the beginning of the period.
  701. *
  702. * @return float|string
  703. */
  704. public static function CUMIPMT($rate, $nper, $pv, $start, $end, $type = 0)
  705. {
  706. $rate = Functions::flattenSingleValue($rate);
  707. $nper = (int) Functions::flattenSingleValue($nper);
  708. $pv = Functions::flattenSingleValue($pv);
  709. $start = (int) Functions::flattenSingleValue($start);
  710. $end = (int) Functions::flattenSingleValue($end);
  711. $type = (int) Functions::flattenSingleValue($type);
  712. // Validate parameters
  713. if ($type != 0 && $type != 1) {
  714. return Functions::NAN();
  715. }
  716. if ($start < 1 || $start > $end) {
  717. return Functions::VALUE();
  718. }
  719. // Calculate
  720. $interest = 0;
  721. for ($per = $start; $per <= $end; ++$per) {
  722. $interest += self::IPMT($rate, $per, $nper, $pv, 0, $type);
  723. }
  724. return $interest;
  725. }
  726. /**
  727. * CUMPRINC.
  728. *
  729. * Returns the cumulative principal paid on a loan between the start and end periods.
  730. *
  731. * Excel Function:
  732. * CUMPRINC(rate,nper,pv,start,end[,type])
  733. *
  734. * @category Financial Functions
  735. *
  736. * @param float $rate The Interest rate
  737. * @param int $nper The total number of payment periods
  738. * @param float $pv Present Value
  739. * @param int $start The first period in the calculation.
  740. * Payment periods are numbered beginning with 1.
  741. * @param int $end the last period in the calculation
  742. * @param int $type A number 0 or 1 and indicates when payments are due:
  743. * 0 or omitted At the end of the period.
  744. * 1 At the beginning of the period.
  745. *
  746. * @return float|string
  747. */
  748. public static function CUMPRINC($rate, $nper, $pv, $start, $end, $type = 0)
  749. {
  750. $rate = Functions::flattenSingleValue($rate);
  751. $nper = (int) Functions::flattenSingleValue($nper);
  752. $pv = Functions::flattenSingleValue($pv);
  753. $start = (int) Functions::flattenSingleValue($start);
  754. $end = (int) Functions::flattenSingleValue($end);
  755. $type = (int) Functions::flattenSingleValue($type);
  756. // Validate parameters
  757. if ($type != 0 && $type != 1) {
  758. return Functions::NAN();
  759. }
  760. if ($start < 1 || $start > $end) {
  761. return Functions::VALUE();
  762. }
  763. // Calculate
  764. $principal = 0;
  765. for ($per = $start; $per <= $end; ++$per) {
  766. $principal += self::PPMT($rate, $per, $nper, $pv, 0, $type);
  767. }
  768. return $principal;
  769. }
  770. /**
  771. * DB.
  772. *
  773. * Returns the depreciation of an asset for a specified period using the
  774. * fixed-declining balance method.
  775. * This form of depreciation is used if you want to get a higher depreciation value
  776. * at the beginning of the depreciation (as opposed to linear depreciation). The
  777. * depreciation value is reduced with every depreciation period by the depreciation
  778. * already deducted from the initial cost.
  779. *
  780. * Excel Function:
  781. * DB(cost,salvage,life,period[,month])
  782. *
  783. * @category Financial Functions
  784. *
  785. * @param float $cost Initial cost of the asset
  786. * @param float $salvage Value at the end of the depreciation.
  787. * (Sometimes called the salvage value of the asset)
  788. * @param int $life Number of periods over which the asset is depreciated.
  789. * (Sometimes called the useful life of the asset)
  790. * @param int $period The period for which you want to calculate the
  791. * depreciation. Period must use the same units as life.
  792. * @param int $month Number of months in the first year. If month is omitted,
  793. * it defaults to 12.
  794. *
  795. * @return float|string
  796. */
  797. public static function DB($cost, $salvage, $life, $period, $month = 12)
  798. {
  799. $cost = Functions::flattenSingleValue($cost);
  800. $salvage = Functions::flattenSingleValue($salvage);
  801. $life = Functions::flattenSingleValue($life);
  802. $period = Functions::flattenSingleValue($period);
  803. $month = Functions::flattenSingleValue($month);
  804. // Validate
  805. if ((is_numeric($cost)) && (is_numeric($salvage)) && (is_numeric($life)) && (is_numeric($period)) && (is_numeric($month))) {
  806. $cost = (float) $cost;
  807. $salvage = (float) $salvage;
  808. $life = (int) $life;
  809. $period = (int) $period;
  810. $month = (int) $month;
  811. if ($cost == 0) {
  812. return 0.0;
  813. } elseif (($cost < 0) || (($salvage / $cost) < 0) || ($life <= 0) || ($period < 1) || ($month < 1)) {
  814. return Functions::NAN();
  815. }
  816. // Set Fixed Depreciation Rate
  817. $fixedDepreciationRate = 1 - pow(($salvage / $cost), (1 / $life));
  818. $fixedDepreciationRate = round($fixedDepreciationRate, 3);
  819. // Loop through each period calculating the depreciation
  820. $previousDepreciation = 0;
  821. for ($per = 1; $per <= $period; ++$per) {
  822. if ($per == 1) {
  823. $depreciation = $cost * $fixedDepreciationRate * $month / 12;
  824. } elseif ($per == ($life + 1)) {
  825. $depreciation = ($cost - $previousDepreciation) * $fixedDepreciationRate * (12 - $month) / 12;
  826. } else {
  827. $depreciation = ($cost - $previousDepreciation) * $fixedDepreciationRate;
  828. }
  829. $previousDepreciation += $depreciation;
  830. }
  831. if (Functions::getCompatibilityMode() == Functions::COMPATIBILITY_GNUMERIC) {
  832. $depreciation = round($depreciation, 2);
  833. }
  834. return $depreciation;
  835. }
  836. return Functions::VALUE();
  837. }
  838. /**
  839. * DDB.
  840. *
  841. * Returns the depreciation of an asset for a specified period using the
  842. * double-declining balance method or some other method you specify.
  843. *
  844. * Excel Function:
  845. * DDB(cost,salvage,life,period[,factor])
  846. *
  847. * @category Financial Functions
  848. *
  849. * @param float $cost Initial cost of the asset
  850. * @param float $salvage Value at the end of the depreciation.
  851. * (Sometimes called the salvage value of the asset)
  852. * @param int $life Number of periods over which the asset is depreciated.
  853. * (Sometimes called the useful life of the asset)
  854. * @param int $period The period for which you want to calculate the
  855. * depreciation. Period must use the same units as life.
  856. * @param float $factor The rate at which the balance declines.
  857. * If factor is omitted, it is assumed to be 2 (the
  858. * double-declining balance method).
  859. *
  860. * @return float|string
  861. */
  862. public static function DDB($cost, $salvage, $life, $period, $factor = 2.0)
  863. {
  864. $cost = Functions::flattenSingleValue($cost);
  865. $salvage = Functions::flattenSingleValue($salvage);
  866. $life = Functions::flattenSingleValue($life);
  867. $period = Functions::flattenSingleValue($period);
  868. $factor = Functions::flattenSingleValue($factor);
  869. // Validate
  870. if ((is_numeric($cost)) && (is_numeric($salvage)) && (is_numeric($life)) && (is_numeric($period)) && (is_numeric($factor))) {
  871. $cost = (float) $cost;
  872. $salvage = (float) $salvage;
  873. $life = (int) $life;
  874. $period = (int) $period;
  875. $factor = (float) $factor;
  876. if (($cost <= 0) || (($salvage / $cost) < 0) || ($life <= 0) || ($period < 1) || ($factor <= 0.0) || ($period > $life)) {
  877. return Functions::NAN();
  878. }
  879. // Set Fixed Depreciation Rate
  880. $fixedDepreciationRate = 1 - pow(($salvage / $cost), (1 / $life));
  881. $fixedDepreciationRate = round($fixedDepreciationRate, 3);
  882. // Loop through each period calculating the depreciation
  883. $previousDepreciation = 0;
  884. for ($per = 1; $per <= $period; ++$per) {
  885. $depreciation = min(($cost - $previousDepreciation) * ($factor / $life), ($cost - $salvage - $previousDepreciation));
  886. $previousDepreciation += $depreciation;
  887. }
  888. if (Functions::getCompatibilityMode() == Functions::COMPATIBILITY_GNUMERIC) {
  889. $depreciation = round($depreciation, 2);
  890. }
  891. return $depreciation;
  892. }
  893. return Functions::VALUE();
  894. }
  895. /**
  896. * DISC.
  897. *
  898. * Returns the discount rate for a security.
  899. *
  900. * Excel Function:
  901. * DISC(settlement,maturity,price,redemption[,basis])
  902. *
  903. * @category Financial Functions
  904. *
  905. * @param mixed $settlement The security's settlement date.
  906. * The security settlement date is the date after the issue
  907. * date when the security is traded to the buyer.
  908. * @param mixed $maturity The security's maturity date.
  909. * The maturity date is the date when the security expires.
  910. * @param int $price The security's price per $100 face value
  911. * @param int $redemption The security's redemption value per $100 face value
  912. * @param int $basis The type of day count to use.
  913. * 0 or omitted US (NASD) 30/360
  914. * 1 Actual/actual
  915. * 2 Actual/360
  916. * 3 Actual/365
  917. * 4 European 30/360
  918. *
  919. * @return float|string
  920. */
  921. public static function DISC($settlement, $maturity, $price, $redemption, $basis = 0)
  922. {
  923. $settlement = Functions::flattenSingleValue($settlement);
  924. $maturity = Functions::flattenSingleValue($maturity);
  925. $price = Functions::flattenSingleValue($price);
  926. $redemption = Functions::flattenSingleValue($redemption);
  927. $basis = Functions::flattenSingleValue($basis);
  928. // Validate
  929. if ((is_numeric($price)) && (is_numeric($redemption)) && (is_numeric($basis))) {
  930. $price = (float) $price;
  931. $redemption = (float) $redemption;
  932. $basis = (int) $basis;
  933. if (($price <= 0) || ($redemption <= 0)) {
  934. return Functions::NAN();
  935. }
  936. $daysBetweenSettlementAndMaturity = DateTime::YEARFRAC($settlement, $maturity, $basis);
  937. if (!is_numeric($daysBetweenSettlementAndMaturity)) {
  938. // return date error
  939. return $daysBetweenSettlementAndMaturity;
  940. }
  941. return (1 - $price / $redemption) / $daysBetweenSettlementAndMaturity;
  942. }
  943. return Functions::VALUE();
  944. }
  945. /**
  946. * DOLLARDE.
  947. *
  948. * Converts a dollar price expressed as an integer part and a fraction
  949. * part into a dollar price expressed as a decimal number.
  950. * Fractional dollar numbers are sometimes used for security prices.
  951. *
  952. * Excel Function:
  953. * DOLLARDE(fractional_dollar,fraction)
  954. *
  955. * @category Financial Functions
  956. *
  957. * @param float $fractional_dollar Fractional Dollar
  958. * @param int $fraction Fraction
  959. *
  960. * @return float|string
  961. */
  962. public static function DOLLARDE($fractional_dollar = null, $fraction = 0)
  963. {
  964. $fractional_dollar = Functions::flattenSingleValue($fractional_dollar);
  965. $fraction = (int) Functions::flattenSingleValue($fraction);
  966. // Validate parameters
  967. if ($fractional_dollar === null || $fraction < 0) {
  968. return Functions::NAN();
  969. }
  970. if ($fraction == 0) {
  971. return Functions::DIV0();
  972. }
  973. $dollars = floor($fractional_dollar);
  974. $cents = fmod($fractional_dollar, 1);
  975. $cents /= $fraction;
  976. $cents *= pow(10, ceil(log10($fraction)));
  977. return $dollars + $cents;
  978. }
  979. /**
  980. * DOLLARFR.
  981. *
  982. * Converts a dollar price expressed as a decimal number into a dollar price
  983. * expressed as a fraction.
  984. * Fractional dollar numbers are sometimes used for security prices.
  985. *
  986. * Excel Function:
  987. * DOLLARFR(decimal_dollar,fraction)
  988. *
  989. * @category Financial Functions
  990. *
  991. * @param float $decimal_dollar Decimal Dollar
  992. * @param int $fraction Fraction
  993. *
  994. * @return float|string
  995. */
  996. public static function DOLLARFR($decimal_dollar = null, $fraction = 0)
  997. {
  998. $decimal_dollar = Functions::flattenSingleValue($decimal_dollar);
  999. $fraction = (int) Functions::flattenSingleValue($fraction);
  1000. // Validate parameters
  1001. if ($decimal_dollar === null || $fraction < 0) {
  1002. return Functions::NAN();
  1003. }
  1004. if ($fraction == 0) {
  1005. return Functions::DIV0();
  1006. }
  1007. $dollars = floor($decimal_dollar);
  1008. $cents = fmod($decimal_dollar, 1);
  1009. $cents *= $fraction;
  1010. $cents *= pow(10, -ceil(log10($fraction)));
  1011. return $dollars + $cents;
  1012. }
  1013. /**
  1014. * EFFECT.
  1015. *
  1016. * Returns the effective interest rate given the nominal rate and the number of
  1017. * compounding payments per year.
  1018. *
  1019. * Excel Function:
  1020. * EFFECT(nominal_rate,npery)
  1021. *
  1022. * @category Financial Functions
  1023. *
  1024. * @param float $nominal_rate Nominal interest rate
  1025. * @param int $npery Number of compounding payments per year
  1026. *
  1027. * @return float|string
  1028. */
  1029. public static function EFFECT($nominal_rate = 0, $npery = 0)
  1030. {
  1031. $nominal_rate = Functions::flattenSingleValue($nominal_rate);
  1032. $npery = (int) Functions::flattenSingleValue($npery);
  1033. // Validate parameters
  1034. if ($nominal_rate <= 0 || $npery < 1) {
  1035. return Functions::NAN();
  1036. }
  1037. return pow((1 + $nominal_rate / $npery), $npery) - 1;
  1038. }
  1039. /**
  1040. * FV.
  1041. *
  1042. * Returns the Future Value of a cash flow with constant payments and interest rate (annuities).
  1043. *
  1044. * Excel Function:
  1045. * FV(rate,nper,pmt[,pv[,type]])
  1046. *
  1047. * @category Financial Functions
  1048. *
  1049. * @param float $rate The interest rate per period
  1050. * @param int $nper Total number of payment periods in an annuity
  1051. * @param float $pmt The payment made each period: it cannot change over the
  1052. * life of the annuity. Typically, pmt contains principal
  1053. * and interest but no other fees or taxes.
  1054. * @param float $pv present Value, or the lump-sum amount that a series of
  1055. * future payments is worth right now
  1056. * @param int $type A number 0 or 1 and indicates when payments are due:
  1057. * 0 or omitted At the end of the period.
  1058. * 1 At the beginning of the period.
  1059. *
  1060. * @return float|string
  1061. */
  1062. public static function FV($rate = 0, $nper = 0, $pmt = 0, $pv = 0, $type = 0)
  1063. {
  1064. $rate = Functions::flattenSingleValue($rate);
  1065. $nper = Functions::flattenSingleValue($nper);
  1066. $pmt = Functions::flattenSingleValue($pmt);
  1067. $pv = Functions::flattenSingleValue($pv);
  1068. $type = Functions::flattenSingleValue($type);
  1069. // Validate parameters
  1070. if ($type != 0 && $type != 1) {
  1071. return Functions::NAN();
  1072. }
  1073. // Calculate
  1074. if ($rate !== null && $rate != 0) {
  1075. return -$pv * pow(1 + $rate, $nper) - $pmt * (1 + $rate * $type) * (pow(1 + $rate, $nper) - 1) / $rate;
  1076. }
  1077. return -$pv - $pmt * $nper;
  1078. }
  1079. /**
  1080. * FVSCHEDULE.
  1081. *
  1082. * Returns the future value of an initial principal after applying a series of compound interest rates.
  1083. * Use FVSCHEDULE to calculate the future value of an investment with a variable or adjustable rate.
  1084. *
  1085. * Excel Function:
  1086. * FVSCHEDULE(principal,schedule)
  1087. *
  1088. * @param float $principal the present value
  1089. * @param float[] $schedule an array of interest rates to apply
  1090. *
  1091. * @return float
  1092. */
  1093. public static function FVSCHEDULE($principal, $schedule)
  1094. {
  1095. $principal = Functions::flattenSingleValue($principal);
  1096. $schedule = Functions::flattenArray($schedule);
  1097. foreach ($schedule as $rate) {
  1098. $principal *= 1 + $rate;
  1099. }
  1100. return $principal;
  1101. }
  1102. /**
  1103. * INTRATE.
  1104. *
  1105. * Returns the interest rate for a fully invested security.
  1106. *
  1107. * Excel Function:
  1108. * INTRATE(settlement,maturity,investment,redemption[,basis])
  1109. *
  1110. * @param mixed $settlement The security's settlement date.
  1111. * The security settlement date is the date after the issue date when the security is traded to the buyer.
  1112. * @param mixed $maturity The security's maturity date.
  1113. * The maturity date is the date when the security expires.
  1114. * @param int $investment the amount invested in the security
  1115. * @param int $redemption the amount to be received at maturity
  1116. * @param int $basis The type of day count to use.
  1117. * 0 or omitted US (NASD) 30/360
  1118. * 1 Actual/actual
  1119. * 2 Actual/360
  1120. * 3 Actual/365
  1121. * 4 European 30/360
  1122. *
  1123. * @return float|string
  1124. */
  1125. public static function INTRATE($settlement, $maturity, $investment, $redemption, $basis = 0)
  1126. {
  1127. $settlement = Functions::flattenSingleValue($settlement);
  1128. $maturity = Functions::flattenSingleValue($maturity);
  1129. $investment = Functions::flattenSingleValue($investment);
  1130. $redemption = Functions::flattenSingleValue($redemption);
  1131. $basis = Functions::flattenSingleValue($basis);
  1132. // Validate
  1133. if ((is_numeric($investment)) && (is_numeric($redemption)) && (is_numeric($basis))) {
  1134. $investment = (float) $investment;
  1135. $redemption = (float) $redemption;
  1136. $basis = (int) $basis;
  1137. if (($investment <= 0) || ($redemption <= 0)) {
  1138. return Functions::NAN();
  1139. }
  1140. $daysBetweenSettlementAndMaturity = DateTime::YEARFRAC($settlement, $maturity, $basis);
  1141. if (!is_numeric($daysBetweenSettlementAndMaturity)) {
  1142. // return date error
  1143. return $daysBetweenSettlementAndMaturity;
  1144. }
  1145. return (($redemption / $investment) - 1) / ($daysBetweenSettlementAndMaturity);
  1146. }
  1147. return Functions::VALUE();
  1148. }
  1149. /**
  1150. * IPMT.
  1151. *
  1152. * Returns the interest payment for a given period for an investment based on periodic, constant payments and a constant interest rate.
  1153. *
  1154. * Excel Function:
  1155. * IPMT(rate,per,nper,pv[,fv][,type])
  1156. *
  1157. * @param float $rate Interest rate per period
  1158. * @param int $per Period for which we want to find the interest
  1159. * @param int $nper Number of periods
  1160. * @param float $pv Present Value
  1161. * @param float $fv Future Value
  1162. * @param int $type Payment type: 0 = at the end of each period, 1 = at the beginning of each period
  1163. *
  1164. * @return float|string
  1165. */
  1166. public static function IPMT($rate, $per, $nper, $pv, $fv = 0, $type = 0)
  1167. {
  1168. $rate = Functions::flattenSingleValue($rate);
  1169. $per = (int) Functions::flattenSingleValue($per);
  1170. $nper = (int) Functions::flattenSingleValue($nper);
  1171. $pv = Functions::flattenSingleValue($pv);
  1172. $fv = Functions::flattenSingleValue($fv);
  1173. $type = (int) Functions::flattenSingleValue($type);
  1174. // Validate parameters
  1175. if ($type != 0 && $type != 1) {
  1176. return Functions::NAN();
  1177. }
  1178. if ($per <= 0 || $per > $nper) {
  1179. return Functions::VALUE();
  1180. }
  1181. // Calculate
  1182. $interestAndPrincipal = self::interestAndPrincipal($rate, $per, $nper, $pv, $fv, $type);
  1183. return $interestAndPrincipal[0];
  1184. }
  1185. /**
  1186. * IRR.
  1187. *
  1188. * Returns the internal rate of return for a series of cash flows represented by the numbers in values.
  1189. * These cash flows do not have to be even, as they would be for an annuity. However, the cash flows must occur
  1190. * at regular intervals, such as monthly or annually. The internal rate of return is the interest rate received
  1191. * for an investment consisting of payments (negative values) and income (positive values) that occur at regular
  1192. * periods.
  1193. *
  1194. * Excel Function:
  1195. * IRR(values[,guess])
  1196. *
  1197. * @param float[] $values An array or a reference to cells that contain numbers for which you want
  1198. * to calculate the internal rate of return.
  1199. * Values must contain at least one positive value and one negative value to
  1200. * calculate the internal rate of return.
  1201. * @param float $guess A number that you guess is close to the result of IRR
  1202. *
  1203. * @return float|string
  1204. */
  1205. public static function IRR($values, $guess = 0.1)
  1206. {
  1207. if (!is_array($values)) {
  1208. return Functions::VALUE();
  1209. }
  1210. $values = Functions::flattenArray($values);
  1211. $guess = Functions::flattenSingleValue($guess);
  1212. // create an initial range, with a root somewhere between 0 and guess
  1213. $x1 = 0.0;
  1214. $x2 = $guess;
  1215. $f1 = self::NPV($x1, $values);
  1216. $f2 = self::NPV($x2, $values);
  1217. for ($i = 0; $i < self::FINANCIAL_MAX_ITERATIONS; ++$i) {
  1218. if (($f1 * $f2) < 0.0) {
  1219. break;
  1220. }
  1221. if (abs($f1) < abs($f2)) {
  1222. $f1 = self::NPV($x1 += 1.6 * ($x1 - $x2), $values);
  1223. } else {
  1224. $f2 = self::NPV($x2 += 1.6 * ($x2 - $x1), $values);
  1225. }
  1226. }
  1227. if (($f1 * $f2) > 0.0) {
  1228. return Functions::VALUE();
  1229. }
  1230. $f = self::NPV($x1, $values);
  1231. if ($f < 0.0) {
  1232. $rtb = $x1;
  1233. $dx = $x2 - $x1;
  1234. } else {
  1235. $rtb = $x2;
  1236. $dx = $x1 - $x2;
  1237. }
  1238. for ($i = 0; $i < self::FINANCIAL_MAX_ITERATIONS; ++$i) {
  1239. $dx *= 0.5;
  1240. $x_mid = $rtb + $dx;
  1241. $f_mid = self::NPV($x_mid, $values);
  1242. if ($f_mid <= 0.0) {
  1243. $rtb = $x_mid;
  1244. }
  1245. if ((abs($f_mid) < self::FINANCIAL_PRECISION) || (abs($dx) < self::FINANCIAL_PRECISION)) {
  1246. return $x_mid;
  1247. }
  1248. }
  1249. return Functions::VALUE();
  1250. }
  1251. /**
  1252. * ISPMT.
  1253. *
  1254. * Returns the interest payment for an investment based on an interest rate and a constant payment schedule.
  1255. *
  1256. * Excel Function:
  1257. * =ISPMT(interest_rate, period, number_payments, PV)
  1258. *
  1259. * interest_rate is the interest rate for the investment
  1260. *
  1261. * period is the period to calculate the interest rate. It must be betweeen 1 and number_payments.
  1262. *
  1263. * number_payments is the number of payments for the annuity
  1264. *
  1265. * PV is the loan amount or present value of the payments
  1266. */
  1267. public static function ISPMT(...$args)
  1268. {
  1269. // Return value
  1270. $returnValue = 0;
  1271. // Get the parameters
  1272. $aArgs = Functions::flattenArray($args);
  1273. $interestRate = array_shift($aArgs);
  1274. $period = array_shift($aArgs);
  1275. $numberPeriods = array_shift($aArgs);
  1276. $principleRemaining = array_shift($aArgs);
  1277. // Calculate
  1278. $principlePayment = ($principleRemaining * 1.0) / ($numberPeriods * 1.0);
  1279. for ($i = 0; $i <= $period; ++$i) {
  1280. $returnValue = $interestRate * $principleRemaining * -1;
  1281. $principleRemaining -= $principlePayment;
  1282. // principle needs to be 0 after the last payment, don't let floating point screw it up
  1283. if ($i == $numberPeriods) {
  1284. $returnValue = 0;
  1285. }
  1286. }
  1287. return $returnValue;
  1288. }
  1289. /**
  1290. * MIRR.
  1291. *
  1292. * Returns the modified internal rate of return for a series of periodic cash flows. MIRR considers both
  1293. * the cost of the investment and the interest received on reinvestment of cash.
  1294. *
  1295. * Excel Function:
  1296. * MIRR(values,finance_rate, reinvestment_rate)
  1297. *
  1298. * @param float[] $values An array or a reference to cells that contain a series of payments and
  1299. * income occurring at regular intervals.
  1300. * Payments are negative value, income is positive values.
  1301. * @param float $finance_rate The interest rate you pay on the money used in the cash flows
  1302. * @param float $reinvestment_rate The interest rate you receive on the cash flows as you reinvest them
  1303. *
  1304. * @return float|string
  1305. */
  1306. public static function MIRR($values, $finance_rate, $reinvestment_rate)
  1307. {
  1308. if (!is_array($values)) {
  1309. return Functions::VALUE();
  1310. }
  1311. $values = Functions::flattenArray($values);
  1312. $finance_rate = Functions::flattenSingleValue($finance_rate);
  1313. $reinvestment_rate = Functions::flattenSingleValue($reinvestment_rate);
  1314. $n = count($values);
  1315. $rr = 1.0 + $reinvestment_rate;
  1316. $fr = 1.0 + $finance_rate;
  1317. $npv_pos = $npv_neg = 0.0;
  1318. foreach ($values as $i => $v) {
  1319. if ($v >= 0) {
  1320. $npv_pos += $v / pow($rr, $i);
  1321. } else {
  1322. $npv_neg += $v / pow($fr, $i);
  1323. }
  1324. }
  1325. if (($npv_neg == 0) || ($npv_pos == 0) || ($reinvestment_rate <= -1)) {
  1326. return Functions::VALUE();
  1327. }
  1328. $mirr = pow((-$npv_pos * pow($rr, $n))
  1329. / ($npv_neg * ($rr)), (1.0 / ($n - 1))) - 1.0;
  1330. return is_finite($mirr) ? $mirr : Functions::VALUE();
  1331. }
  1332. /**
  1333. * NOMINAL.
  1334. *
  1335. * Returns the nominal interest rate given the effective rate and the number of compounding payments per year.
  1336. *
  1337. * @param float $effect_rate Effective interest rate
  1338. * @param int $npery Number of compounding payments per year
  1339. *
  1340. * @return float|string
  1341. */
  1342. public static function NOMINAL($effect_rate = 0, $npery = 0)
  1343. {
  1344. $effect_rate = Functions::flattenSingleValue($effect_rate);
  1345. $npery = (int) Functions::flattenSingleValue($npery);
  1346. // Validate parameters
  1347. if ($effect_rate <= 0 || $npery < 1) {
  1348. return Functions::NAN();
  1349. }
  1350. // Calculate
  1351. return $npery * (pow($effect_rate + 1, 1 / $npery) - 1);
  1352. }
  1353. /**
  1354. * NPER.
  1355. *
  1356. * Returns the number of periods for a cash flow with constant periodic payments (annuities), and interest rate.
  1357. *
  1358. * @param float $rate Interest rate per period
  1359. * @param int $pmt Periodic payment (annuity)
  1360. * @param float $pv Present Value
  1361. * @param float $fv Future Value
  1362. * @param int $type Payment type: 0 = at the end of each period, 1 = at the beginning of each period
  1363. *
  1364. * @return float|string
  1365. */
  1366. public static function NPER($rate = 0, $pmt = 0, $pv = 0, $fv = 0, $type = 0)
  1367. {
  1368. $rate = Functions::flattenSingleValue($rate);
  1369. $pmt = Functions::flattenSingleValue($pmt);
  1370. $pv = Functions::flattenSingleValue($pv);
  1371. $fv = Functions::flattenSingleValue($fv);
  1372. $type = Functions::flattenSingleValue($type);
  1373. // Validate parameters
  1374. if ($type != 0 && $type != 1) {
  1375. return Functions::NAN();
  1376. }
  1377. // Calculate
  1378. if ($rate !== null && $rate != 0) {
  1379. if ($pmt == 0 && $pv == 0) {
  1380. return Functions::NAN();
  1381. }
  1382. return log(($pmt * (1 + $rate * $type) / $rate - $fv) / ($pv + $pmt * (1 + $rate * $type) / $rate)) / log(1 + $rate);
  1383. }
  1384. if ($pmt == 0) {
  1385. return Functions::NAN();
  1386. }
  1387. return (-$pv - $fv) / $pmt;
  1388. }
  1389. /**
  1390. * NPV.
  1391. *
  1392. * Returns the Net Present Value of a cash flow series given a discount rate.
  1393. *
  1394. * @return float
  1395. */
  1396. public static function NPV(...$args)
  1397. {
  1398. // Return value
  1399. $returnValue = 0;
  1400. // Loop through arguments
  1401. $aArgs = Functions::flattenArray($args);
  1402. // Calculate
  1403. $rate = array_shift($aArgs);
  1404. $countArgs = count($aArgs);
  1405. for ($i = 1; $i <= $countArgs; ++$i) {
  1406. // Is it a numeric value?
  1407. if (is_numeric($aArgs[$i - 1])) {
  1408. $returnValue += $aArgs[$i - 1] / pow(1 + $rate, $i);
  1409. }
  1410. }
  1411. // Return
  1412. return $returnValue;
  1413. }
  1414. /**
  1415. * PDURATION.
  1416. *
  1417. * Calculates the number of periods required for an investment to reach a specified value.
  1418. *
  1419. * @param float $rate Interest rate per period
  1420. * @param float $pv Present Value
  1421. * @param float $fv Future Value
  1422. *
  1423. * @return float|string
  1424. */
  1425. public static function PDURATION($rate = 0, $pv = 0, $fv = 0)
  1426. {
  1427. $rate = Functions::flattenSingleValue($rate);
  1428. $pv = Functions::flattenSingleValue($pv);
  1429. $fv = Functions::flattenSingleValue($fv);
  1430. // Validate parameters
  1431. if (!is_numeric($rate) || !is_numeric($pv) || !is_numeric($fv)) {
  1432. return Functions::VALUE();
  1433. } elseif ($rate <= 0.0 || $pv <= 0.0 || $fv <= 0.0) {
  1434. return Functions::NAN();
  1435. }
  1436. return (log($fv) - log($pv)) / log(1 + $rate);
  1437. }
  1438. /**
  1439. * PMT.
  1440. *
  1441. * Returns the constant payment (annuity) for a cash flow with a constant interest rate.
  1442. *
  1443. * @param float $rate Interest rate per period
  1444. * @param int $nper Number of periods
  1445. * @param float $pv Present Value
  1446. * @param float $fv Future Value
  1447. * @param int $type Payment type: 0 = at the end of each period, 1 = at the beginning of each period
  1448. *
  1449. * @return float
  1450. */
  1451. public static function PMT($rate = 0, $nper = 0, $pv = 0, $fv = 0, $type = 0)
  1452. {
  1453. $rate = Functions::flattenSingleValue($rate);
  1454. $nper = Functions::flattenSingleValue($nper);
  1455. $pv = Functions::flattenSingleValue($pv);
  1456. $fv = Functions::flattenSingleValue($fv);
  1457. $type = Functions::flattenSingleValue($type);
  1458. // Validate parameters
  1459. if ($type != 0 && $type != 1) {
  1460. return Functions::NAN();
  1461. }
  1462. // Calculate
  1463. if ($rate !== null && $rate != 0) {
  1464. return (-$fv - $pv * pow(1 + $rate, $nper)) / (1 + $rate * $type) / ((pow(1 + $rate, $nper) - 1) / $rate);
  1465. }
  1466. return (-$pv - $fv) / $nper;
  1467. }
  1468. /**
  1469. * PPMT.
  1470. *
  1471. * Returns the interest payment for a given period for an investment based on periodic, constant payments and a constant interest rate.
  1472. *
  1473. * @param float $rate Interest rate per period
  1474. * @param int $per Period for which we want to find the interest
  1475. * @param int $nper Number of periods
  1476. * @param float $pv Present Value
  1477. * @param float $fv Future Value
  1478. * @param int $type Payment type: 0 = at the end of each period, 1 = at the beginning of each period
  1479. *
  1480. * @return float
  1481. */
  1482. public static function PPMT($rate, $per, $nper, $pv, $fv = 0, $type = 0)
  1483. {
  1484. $rate = Functions::flattenSingleValue($rate);
  1485. $per = (int) Functions::flattenSingleValue($per);
  1486. $nper = (int) Functions::flattenSingleValue($nper);
  1487. $pv = Functions::flattenSingleValue($pv);
  1488. $fv = Functions::flattenSingleValue($fv);
  1489. $type = (int) Functions::flattenSingleValue($type);
  1490. // Validate parameters
  1491. if ($type != 0 && $type != 1) {
  1492. return Functions::NAN();
  1493. }
  1494. if ($per <= 0 || $per > $nper) {
  1495. return Functions::VALUE();
  1496. }
  1497. // Calculate
  1498. $interestAndPrincipal = self::interestAndPrincipal($rate, $per, $nper, $pv, $fv, $type);
  1499. return $interestAndPrincipal[1];
  1500. }
  1501. public static function PRICE($settlement, $maturity, $rate, $yield, $redemption, $frequency, $basis = 0)
  1502. {
  1503. $settlement = Functions::flattenSingleValue($settlement);
  1504. $maturity = Functions::flattenSingleValue($maturity);
  1505. $rate = (float) Functions::flattenSingleValue($rate);
  1506. $yield = (float) Functions::flattenSingleValue($yield);
  1507. $redemption = (float) Functions::flattenSingleValue($redemption);
  1508. $frequency = (int) Functions::flattenSingleValue($frequency);
  1509. $basis = ($basis === null) ? 0 : (int) Functions::flattenSingleValue($basis);
  1510. if (is_string($settlement = DateTime::getDateValue($settlement))) {
  1511. return Functions::VALUE();
  1512. }
  1513. if (is_string($maturity = DateTime::getDateValue($maturity))) {
  1514. return Functions::VALUE();
  1515. }
  1516. if (($settlement > $maturity) ||
  1517. (!self::isValidFrequency($frequency)) ||
  1518. (($basis < 0) || ($basis > 4))) {
  1519. return Functions::NAN();
  1520. }
  1521. $dsc = self::COUPDAYSNC($settlement, $maturity, $frequency, $basis);
  1522. $e = self::COUPDAYS($settlement, $maturity, $frequency, $basis);
  1523. $n = self::COUPNUM($settlement, $maturity, $frequency, $basis);
  1524. $a = self::COUPDAYBS($settlement, $maturity, $frequency, $basis);
  1525. $baseYF = 1.0 + ($yield / $frequency);
  1526. $rfp = 100 * ($rate / $frequency);
  1527. $de = $dsc / $e;
  1528. $result = $redemption / pow($baseYF, (--$n + $de));
  1529. for ($k = 0; $k <= $n; ++$k) {
  1530. $result += $rfp / (pow($baseYF, ($k + $de)));
  1531. }
  1532. $result -= $rfp * ($a / $e);
  1533. return $result;
  1534. }
  1535. /**
  1536. * PRICEDISC.
  1537. *
  1538. * Returns the price per $100 face value of a discounted security.
  1539. *
  1540. * @param mixed $settlement The security's settlement date.
  1541. * The security settlement date is the date after the issue date when the security is traded to the buyer.
  1542. * @param mixed $maturity The security's maturity date.
  1543. * The maturity date is the date when the security expires.
  1544. * @param int $discount The security's discount rate
  1545. * @param int $redemption The security's redemption value per $100 face value
  1546. * @param int $basis The type of day count to use.
  1547. * 0 or omitted US (NASD) 30/360
  1548. * 1 Actual/actual
  1549. * 2 Actual/360
  1550. * 3 Actual/365
  1551. * 4 European 30/360
  1552. *
  1553. * @return float
  1554. */
  1555. public static function PRICEDISC($settlement, $maturity, $discount, $redemption, $basis = 0)
  1556. {
  1557. $settlement = Functions::flattenSingleValue($settlement);
  1558. $maturity = Functions::flattenSingleValue($maturity);
  1559. $discount = (float) Functions::flattenSingleValue($discount);
  1560. $redemption = (float) Functions::flattenSingleValue($redemption);
  1561. $basis = (int) Functions::flattenSingleValue($basis);
  1562. // Validate
  1563. if ((is_numeric($discount)) && (is_numeric($redemption)) && (is_numeric($basis))) {
  1564. if (($discount <= 0) || ($redemption <= 0)) {
  1565. return Functions::NAN();
  1566. }
  1567. $daysBetweenSettlementAndMaturity = DateTime::YEARFRAC($settlement, $maturity, $basis);
  1568. if (!is_numeric($daysBetweenSettlementAndMaturity)) {
  1569. // return date error
  1570. return $daysBetweenSettlementAndMaturity;
  1571. }
  1572. return $redemption * (1 - $discount * $daysBetweenSettlementAndMaturity);
  1573. }
  1574. return Functions::VALUE();
  1575. }
  1576. /**
  1577. * PRICEMAT.
  1578. *
  1579. * Returns the price per $100 face value of a security that pays interest at maturity.
  1580. *
  1581. * @param mixed $settlement The security's settlement date.
  1582. * The security's settlement date is the date after the issue date when the security is traded to the buyer.
  1583. * @param mixed $maturity The security's maturity date.
  1584. * The maturity date is the date when the security expires.
  1585. * @param mixed $issue The security's issue date
  1586. * @param int $rate The security's interest rate at date of issue
  1587. * @param int $yield The security's annual yield
  1588. * @param int $basis The type of day count to use.
  1589. * 0 or omitted US (NASD) 30/360
  1590. * 1 Actual/actual
  1591. * 2 Actual/360
  1592. * 3 Actual/365
  1593. * 4 European 30/360
  1594. *
  1595. * @return float
  1596. */
  1597. public static function PRICEMAT($settlement, $maturity, $issue, $rate, $yield, $basis = 0)
  1598. {
  1599. $settlement = Functions::flattenSingleValue($settlement);
  1600. $maturity = Functions::flattenSingleValue($maturity);
  1601. $issue = Functions::flattenSingleValue($issue);
  1602. $rate = Functions::flattenSingleValue($rate);
  1603. $yield = Functions::flattenSingleValue($yield);
  1604. $basis = (int) Functions::flattenSingleValue($basis);
  1605. // Validate
  1606. if (is_numeric($rate) && is_numeric($yield)) {
  1607. if (($rate <= 0) || ($yield <= 0)) {
  1608. return Functions::NAN();
  1609. }
  1610. $daysPerYear = self::daysPerYear(DateTime::YEAR($settlement), $basis);
  1611. if (!is_numeric($daysPerYear)) {
  1612. return $daysPerYear;
  1613. }
  1614. $daysBetweenIssueAndSettlement = DateTime::YEARFRAC($issue, $settlement, $basis);
  1615. if (!is_numeric($daysBetweenIssueAndSettlement)) {
  1616. // return date error
  1617. return $daysBetweenIssueAndSettlement;
  1618. }
  1619. $daysBetweenIssueAndSettlement *= $daysPerYear;
  1620. $daysBetweenIssueAndMaturity = DateTime::YEARFRAC($issue, $maturity, $basis);
  1621. if (!is_numeric($daysBetweenIssueAndMaturity)) {
  1622. // return date error
  1623. return $daysBetweenIssueAndMaturity;
  1624. }
  1625. $daysBetweenIssueAndMaturity *= $daysPerYear;
  1626. $daysBetweenSettlementAndMaturity = DateTime::YEARFRAC($settlement, $maturity, $basis);
  1627. if (!is_numeric($daysBetweenSettlementAndMaturity)) {
  1628. // return date error
  1629. return $daysBetweenSettlementAndMaturity;
  1630. }
  1631. $daysBetweenSettlementAndMaturity *= $daysPerYear;
  1632. return (100 + (($daysBetweenIssueAndMaturity / $daysPerYear) * $rate * 100)) /
  1633. (1 + (($daysBetweenSettlementAndMaturity / $daysPerYear) * $yield)) -
  1634. (($daysBetweenIssueAndSettlement / $daysPerYear) * $rate * 100);
  1635. }
  1636. return Functions::VALUE();
  1637. }
  1638. /**
  1639. * PV.
  1640. *
  1641. * Returns the Present Value of a cash flow with constant payments and interest rate (annuities).
  1642. *
  1643. * @param float $rate Interest rate per period
  1644. * @param int $nper Number of periods
  1645. * @param float $pmt Periodic payment (annuity)
  1646. * @param float $fv Future Value
  1647. * @param int $type Payment type: 0 = at the end of each period, 1 = at the beginning of each period
  1648. *
  1649. * @return float
  1650. */
  1651. public static function PV($rate = 0, $nper = 0, $pmt = 0, $fv = 0, $type = 0)
  1652. {
  1653. $rate = Functions::flattenSingleValue($rate);
  1654. $nper = Functions::flattenSingleValue($nper);
  1655. $pmt = Functions::flattenSingleValue($pmt);
  1656. $fv = Functions::flattenSingleValue($fv);
  1657. $type = Functions::flattenSingleValue($type);
  1658. // Validate parameters
  1659. if ($type != 0 && $type != 1) {
  1660. return Functions::NAN();
  1661. }
  1662. // Calculate
  1663. if ($rate !== null && $rate != 0) {
  1664. return (-$pmt * (1 + $rate * $type) * ((pow(1 + $rate, $nper) - 1) / $rate) - $fv) / pow(1 + $rate, $nper);
  1665. }
  1666. return -$fv - $pmt * $nper;
  1667. }
  1668. /**
  1669. * RATE.
  1670. *
  1671. * Returns the interest rate per period of an annuity.
  1672. * RATE is calculated by iteration and can have zero or more solutions.
  1673. * If the successive results of RATE do not converge to within 0.0000001 after 20 iterations,
  1674. * RATE returns the #NUM! error value.
  1675. *
  1676. * Excel Function:
  1677. * RATE(nper,pmt,pv[,fv[,type[,guess]]])
  1678. *
  1679. * @category Financial Functions
  1680. *
  1681. * @param float $nper The total number of payment periods in an annuity
  1682. * @param float $pmt The payment made each period and cannot change over the life
  1683. * of the annuity.
  1684. * Typically, pmt includes principal and interest but no other
  1685. * fees or taxes.
  1686. * @param float $pv The present value - the total amount that a series of future
  1687. * payments is worth now
  1688. * @param float $fv The future value, or a cash balance you want to attain after
  1689. * the last payment is made. If fv is omitted, it is assumed
  1690. * to be 0 (the future value of a loan, for example, is 0).
  1691. * @param int $type A number 0 or 1 and indicates when payments are due:
  1692. * 0 or omitted At the end of the period.
  1693. * 1 At the beginning of the period.
  1694. * @param float $guess Your guess for what the rate will be.
  1695. * If you omit guess, it is assumed to be 10 percent.
  1696. *
  1697. * @return float
  1698. */
  1699. public static function RATE($nper, $pmt, $pv, $fv = 0.0, $type = 0, $guess = 0.1)
  1700. {
  1701. $nper = (int) Functions::flattenSingleValue($nper);
  1702. $pmt = Functions::flattenSingleValue($pmt);
  1703. $pv = Functions::flattenSingleValue($pv);
  1704. $fv = ($fv === null) ? 0.0 : Functions::flattenSingleValue($fv);
  1705. $type = ($type === null) ? 0 : (int) Functions::flattenSingleValue($type);
  1706. $guess = ($guess === null) ? 0.1 : Functions::flattenSingleValue($guess);
  1707. $rate = $guess;
  1708. if (abs($rate) < self::FINANCIAL_PRECISION) {
  1709. $y = $pv * (1 + $nper * $rate) + $pmt * (1 + $rate * $type) * $nper + $fv;
  1710. } else {
  1711. $f = exp($nper * log(1 + $rate));
  1712. $y = $pv * $f + $pmt * (1 / $rate + $type) * ($f - 1) + $fv;
  1713. }
  1714. $y0 = $pv + $pmt * $nper + $fv;
  1715. $y1 = $pv * $f + $pmt * (1 / $rate + $type) * ($f - 1) + $fv;
  1716. // find root by secant method
  1717. $i = $x0 = 0.0;
  1718. $x1 = $rate;
  1719. while ((abs($y0 - $y1) > self::FINANCIAL_PRECISION) && ($i < self::FINANCIAL_MAX_ITERATIONS)) {
  1720. $rate = ($y1 * $x0 - $y0 * $x1) / ($y1 - $y0);
  1721. $x0 = $x1;
  1722. $x1 = $rate;
  1723. if (($nper * abs($pmt)) > ($pv - $fv)) {
  1724. $x1 = abs($x1);
  1725. }
  1726. if (abs($rate) < self::FINANCIAL_PRECISION) {
  1727. $y = $pv * (1 + $nper * $rate) + $pmt * (1 + $rate * $type) * $nper + $fv;
  1728. } else {
  1729. $f = exp($nper * log(1 + $rate));
  1730. $y = $pv * $f + $pmt * (1 / $rate + $type) * ($f - 1) + $fv;
  1731. }
  1732. $y0 = $y1;
  1733. $y1 = $y;
  1734. ++$i;
  1735. }
  1736. return $rate;
  1737. }
  1738. /**
  1739. * RECEIVED.
  1740. *
  1741. * Returns the price per $100 face value of a discounted security.
  1742. *
  1743. * @param mixed $settlement The security's settlement date.
  1744. * The security settlement date is the date after the issue date when the security is traded to the buyer.
  1745. * @param mixed $maturity The security's maturity date.
  1746. * The maturity date is the date when the security expires.
  1747. * @param int $investment The amount invested in the security
  1748. * @param int $discount The security's discount rate
  1749. * @param int $basis The type of day count to use.
  1750. * 0 or omitted US (NASD) 30/360
  1751. * 1 Actual/actual
  1752. * 2 Actual/360
  1753. * 3 Actual/365
  1754. * 4 European 30/360
  1755. *
  1756. * @return float
  1757. */
  1758. public static function RECEIVED($settlement, $maturity, $investment, $discount, $basis = 0)
  1759. {
  1760. $settlement = Functions::flattenSingleValue($settlement);
  1761. $maturity = Functions::flattenSingleValue($maturity);
  1762. $investment = (float) Functions::flattenSingleValue($investment);
  1763. $discount = (float) Functions::flattenSingleValue($discount);
  1764. $basis = (int) Functions::flattenSingleValue($basis);
  1765. // Validate
  1766. if ((is_numeric($investment)) && (is_numeric($discount)) && (is_numeric($basis))) {
  1767. if (($investment <= 0) || ($discount <= 0)) {
  1768. return Functions::NAN();
  1769. }
  1770. $daysBetweenSettlementAndMaturity = DateTime::YEARFRAC($settlement, $maturity, $basis);
  1771. if (!is_numeric($daysBetweenSettlementAndMaturity)) {
  1772. // return date error
  1773. return $daysBetweenSettlementAndMaturity;
  1774. }
  1775. return $investment / (1 - ($discount * $daysBetweenSettlementAndMaturity));
  1776. }
  1777. return Functions::VALUE();
  1778. }
  1779. /**
  1780. * RRI.
  1781. *
  1782. * Calculates the interest rate required for an investment to grow to a specified future value .
  1783. *
  1784. * @param float $nper The number of periods over which the investment is made
  1785. * @param float $pv Present Value
  1786. * @param float $fv Future Value
  1787. *
  1788. * @return float|string
  1789. */
  1790. public static function RRI($nper = 0, $pv = 0, $fv = 0)
  1791. {
  1792. $nper = Functions::flattenSingleValue($nper);
  1793. $pv = Functions::flattenSingleValue($pv);
  1794. $fv = Functions::flattenSingleValue($fv);
  1795. // Validate parameters
  1796. if (!is_numeric($nper) || !is_numeric($pv) || !is_numeric($fv)) {
  1797. return Functions::VALUE();
  1798. } elseif ($nper <= 0.0 || $pv <= 0.0 || $fv < 0.0) {
  1799. return Functions::NAN();
  1800. }
  1801. return pow($fv / $pv, 1 / $nper) - 1;
  1802. }
  1803. /**
  1804. * SLN.
  1805. *
  1806. * Returns the straight-line depreciation of an asset for one period
  1807. *
  1808. * @param mixed $cost Initial cost of the asset
  1809. * @param mixed $salvage Value at the end of the depreciation
  1810. * @param mixed $life Number of periods over which the asset is depreciated
  1811. *
  1812. * @return float|string
  1813. */
  1814. public static function SLN($cost, $salvage, $life)
  1815. {
  1816. $cost = Functions::flattenSingleValue($cost);
  1817. $salvage = Functions::flattenSingleValue($salvage);
  1818. $life = Functions::flattenSingleValue($life);
  1819. // Calculate
  1820. if ((is_numeric($cost)) && (is_numeric($salvage)) && (is_numeric($life))) {
  1821. if ($life < 0) {
  1822. return Functions::NAN();
  1823. }
  1824. return ($cost - $salvage) / $life;
  1825. }
  1826. return Functions::VALUE();
  1827. }
  1828. /**
  1829. * SYD.
  1830. *
  1831. * Returns the sum-of-years' digits depreciation of an asset for a specified period.
  1832. *
  1833. * @param mixed $cost Initial cost of the asset
  1834. * @param mixed $salvage Value at the end of the depreciation
  1835. * @param mixed $life Number of periods over which the asset is depreciated
  1836. * @param mixed $period Period
  1837. *
  1838. * @return float|string
  1839. */
  1840. public static function SYD($cost, $salvage, $life, $period)
  1841. {
  1842. $cost = Functions::flattenSingleValue($cost);
  1843. $salvage = Functions::flattenSingleValue($salvage);
  1844. $life = Functions::flattenSingleValue($life);
  1845. $period = Functions::flattenSingleValue($period);
  1846. // Calculate
  1847. if ((is_numeric($cost)) && (is_numeric($salvage)) && (is_numeric($life)) && (is_numeric($period))) {
  1848. if (($life < 1) || ($period > $life)) {
  1849. return Functions::NAN();
  1850. }
  1851. return (($cost - $salvage) * ($life - $period + 1) * 2) / ($life * ($life + 1));
  1852. }
  1853. return Functions::VALUE();
  1854. }
  1855. /**
  1856. * TBILLEQ.
  1857. *
  1858. * Returns the bond-equivalent yield for a Treasury bill.
  1859. *
  1860. * @param mixed $settlement The Treasury bill's settlement date.
  1861. * The Treasury bill's settlement date is the date after the issue date when the Treasury bill is traded to the buyer.
  1862. * @param mixed $maturity The Treasury bill's maturity date.
  1863. * The maturity date is the date when the Treasury bill expires.
  1864. * @param int $discount The Treasury bill's discount rate
  1865. *
  1866. * @return float
  1867. */
  1868. public static function TBILLEQ($settlement, $maturity, $discount)
  1869. {
  1870. $settlement = Functions::flattenSingleValue($settlement);
  1871. $maturity = Functions::flattenSingleValue($maturity);
  1872. $discount = Functions::flattenSingleValue($discount);
  1873. // Use TBILLPRICE for validation
  1874. $testValue = self::TBILLPRICE($settlement, $maturity, $discount);
  1875. if (is_string($testValue)) {
  1876. return $testValue;
  1877. }
  1878. if (is_string($maturity = DateTime::getDateValue($maturity))) {
  1879. return Functions::VALUE();
  1880. }
  1881. if (Functions::getCompatibilityMode() == Functions::COMPATIBILITY_OPENOFFICE) {
  1882. ++$maturity;
  1883. $daysBetweenSettlementAndMaturity = DateTime::YEARFRAC($settlement, $maturity) * 360;
  1884. } else {
  1885. $daysBetweenSettlementAndMaturity = (DateTime::getDateValue($maturity) - DateTime::getDateValue($settlement));
  1886. }
  1887. return (365 * $discount) / (360 - $discount * $daysBetweenSettlementAndMaturity);
  1888. }
  1889. /**
  1890. * TBILLPRICE.
  1891. *
  1892. * Returns the yield for a Treasury bill.
  1893. *
  1894. * @param mixed $settlement The Treasury bill's settlement date.
  1895. * The Treasury bill's settlement date is the date after the issue date when the Treasury bill is traded to the buyer.
  1896. * @param mixed $maturity The Treasury bill's maturity date.
  1897. * The maturity date is the date when the Treasury bill expires.
  1898. * @param int $discount The Treasury bill's discount rate
  1899. *
  1900. * @return float
  1901. */
  1902. public static function TBILLPRICE($settlement, $maturity, $discount)
  1903. {
  1904. $settlement = Functions::flattenSingleValue($settlement);
  1905. $maturity = Functions::flattenSingleValue($maturity);
  1906. $discount = Functions::flattenSingleValue($discount);
  1907. if (is_string($maturity = DateTime::getDateValue($maturity))) {
  1908. return Functions::VALUE();
  1909. }
  1910. // Validate
  1911. if (is_numeric($discount)) {
  1912. if ($discount <= 0) {
  1913. return Functions::NAN();
  1914. }
  1915. if (Functions::getCompatibilityMode() == Functions::COMPATIBILITY_OPENOFFICE) {
  1916. ++$maturity;
  1917. $daysBetweenSettlementAndMaturity = DateTime::YEARFRAC($settlement, $maturity) * 360;
  1918. if (!is_numeric($daysBetweenSettlementAndMaturity)) {
  1919. // return date error
  1920. return $daysBetweenSettlementAndMaturity;
  1921. }
  1922. } else {
  1923. $daysBetweenSettlementAndMaturity = (DateTime::getDateValue($maturity) - DateTime::getDateValue($settlement));
  1924. }
  1925. if ($daysBetweenSettlementAndMaturity > 360) {
  1926. return Functions::NAN();
  1927. }
  1928. $price = 100 * (1 - (($discount * $daysBetweenSettlementAndMaturity) / 360));
  1929. if ($price <= 0) {
  1930. return Functions::NAN();
  1931. }
  1932. return $price;
  1933. }
  1934. return Functions::VALUE();
  1935. }
  1936. /**
  1937. * TBILLYIELD.
  1938. *
  1939. * Returns the yield for a Treasury bill.
  1940. *
  1941. * @param mixed $settlement The Treasury bill's settlement date.
  1942. * The Treasury bill's settlement date is the date after the issue date when the Treasury bill is traded to the buyer.
  1943. * @param mixed $maturity The Treasury bill's maturity date.
  1944. * The maturity date is the date when the Treasury bill expires.
  1945. * @param int $price The Treasury bill's price per $100 face value
  1946. *
  1947. * @return float
  1948. */
  1949. public static function TBILLYIELD($settlement, $maturity, $price)
  1950. {
  1951. $settlement = Functions::flattenSingleValue($settlement);
  1952. $maturity = Functions::flattenSingleValue($maturity);
  1953. $price = Functions::flattenSingleValue($price);
  1954. // Validate
  1955. if (is_numeric($price)) {
  1956. if ($price <= 0) {
  1957. return Functions::NAN();
  1958. }
  1959. if (Functions::getCompatibilityMode() == Functions::COMPATIBILITY_OPENOFFICE) {
  1960. ++$maturity;
  1961. $daysBetweenSettlementAndMaturity = DateTime::YEARFRAC($settlement, $maturity) * 360;
  1962. if (!is_numeric($daysBetweenSettlementAndMaturity)) {
  1963. // return date error
  1964. return $daysBetweenSettlementAndMaturity;
  1965. }
  1966. } else {
  1967. $daysBetweenSettlementAndMaturity = (DateTime::getDateValue($maturity) - DateTime::getDateValue($settlement));
  1968. }
  1969. if ($daysBetweenSettlementAndMaturity > 360) {
  1970. return Functions::NAN();
  1971. }
  1972. return ((100 - $price) / $price) * (360 / $daysBetweenSettlementAndMaturity);
  1973. }
  1974. return Functions::VALUE();
  1975. }
  1976. public static function XIRR($values, $dates, $guess = 0.1)
  1977. {
  1978. if ((!is_array($values)) && (!is_array($dates))) {
  1979. return Functions::VALUE();
  1980. }
  1981. $values = Functions::flattenArray($values);
  1982. $dates = Functions::flattenArray($dates);
  1983. $guess = Functions::flattenSingleValue($guess);
  1984. if (count($values) != count($dates)) {
  1985. return Functions::NAN();
  1986. }
  1987. // create an initial range, with a root somewhere between 0 and guess
  1988. $x1 = 0.0;
  1989. $x2 = $guess;
  1990. $f1 = self::XNPV($x1, $values, $dates);
  1991. $f2 = self::XNPV($x2, $values, $dates);
  1992. for ($i = 0; $i < self::FINANCIAL_MAX_ITERATIONS; ++$i) {
  1993. if (($f1 * $f2) < 0.0) {
  1994. break;
  1995. } elseif (abs($f1) < abs($f2)) {
  1996. $f1 = self::XNPV($x1 += 1.6 * ($x1 - $x2), $values, $dates);
  1997. } else {
  1998. $f2 = self::XNPV($x2 += 1.6 * ($x2 - $x1), $values, $dates);
  1999. }
  2000. }
  2001. if (($f1 * $f2) > 0.0) {
  2002. return Functions::VALUE();
  2003. }
  2004. $f = self::XNPV($x1, $values, $dates);
  2005. if ($f < 0.0) {
  2006. $rtb = $x1;
  2007. $dx = $x2 - $x1;
  2008. } else {
  2009. $rtb = $x2;
  2010. $dx = $x1 - $x2;
  2011. }
  2012. for ($i = 0; $i < self::FINANCIAL_MAX_ITERATIONS; ++$i) {
  2013. $dx *= 0.5;
  2014. $x_mid = $rtb + $dx;
  2015. $f_mid = self::XNPV($x_mid, $values, $dates);
  2016. if ($f_mid <= 0.0) {
  2017. $rtb = $x_mid;
  2018. }
  2019. if ((abs($f_mid) < self::FINANCIAL_PRECISION) || (abs($dx) < self::FINANCIAL_PRECISION)) {
  2020. return $x_mid;
  2021. }
  2022. }
  2023. return Functions::VALUE();
  2024. }
  2025. /**
  2026. * XNPV.
  2027. *
  2028. * Returns the net present value for a schedule of cash flows that is not necessarily periodic.
  2029. * To calculate the net present value for a series of cash flows that is periodic, use the NPV function.
  2030. *
  2031. * Excel Function:
  2032. * =XNPV(rate,values,dates)
  2033. *
  2034. * @param float $rate the discount rate to apply to the cash flows
  2035. * @param array of float $values A series of cash flows that corresponds to a schedule of payments in dates.
  2036. * The first payment is optional and corresponds to a cost or payment that occurs at the beginning of the investment.
  2037. * If the first value is a cost or payment, it must be a negative value. All succeeding payments are discounted based on a 365-day year.
  2038. * The series of values must contain at least one positive value and one negative value.
  2039. * @param array of mixed $dates A schedule of payment dates that corresponds to the cash flow payments.
  2040. * The first payment date indicates the beginning of the schedule of payments.
  2041. * All other dates must be later than this date, but they may occur in any order.
  2042. *
  2043. * @return float
  2044. */
  2045. public static function XNPV($rate, $values, $dates)
  2046. {
  2047. $rate = Functions::flattenSingleValue($rate);
  2048. if (!is_numeric($rate)) {
  2049. return Functions::VALUE();
  2050. }
  2051. if ((!is_array($values)) || (!is_array($dates))) {
  2052. return Functions::VALUE();
  2053. }
  2054. $values = Functions::flattenArray($values);
  2055. $dates = Functions::flattenArray($dates);
  2056. $valCount = count($values);
  2057. if ($valCount != count($dates)) {
  2058. return Functions::NAN();
  2059. }
  2060. if ((min($values) > 0) || (max($values) < 0)) {
  2061. return Functions::VALUE();
  2062. }
  2063. $xnpv = 0.0;
  2064. for ($i = 0; $i < $valCount; ++$i) {
  2065. if (!is_numeric($values[$i])) {
  2066. return Functions::VALUE();
  2067. }
  2068. $xnpv += $values[$i] / pow(1 + $rate, DateTime::DATEDIF($dates[0], $dates[$i], 'd') / 365);
  2069. }
  2070. return (is_finite($xnpv)) ? $xnpv : Functions::VALUE();
  2071. }
  2072. /**
  2073. * YIELDDISC.
  2074. *
  2075. * Returns the annual yield of a security that pays interest at maturity.
  2076. *
  2077. * @param mixed $settlement The security's settlement date.
  2078. * The security's settlement date is the date after the issue date when the security is traded to the buyer.
  2079. * @param mixed $maturity The security's maturity date.
  2080. * The maturity date is the date when the security expires.
  2081. * @param int $price The security's price per $100 face value
  2082. * @param int $redemption The security's redemption value per $100 face value
  2083. * @param int $basis The type of day count to use.
  2084. * 0 or omitted US (NASD) 30/360
  2085. * 1 Actual/actual
  2086. * 2 Actual/360
  2087. * 3 Actual/365
  2088. * 4 European 30/360
  2089. *
  2090. * @return float
  2091. */
  2092. public static function YIELDDISC($settlement, $maturity, $price, $redemption, $basis = 0)
  2093. {
  2094. $settlement = Functions::flattenSingleValue($settlement);
  2095. $maturity = Functions::flattenSingleValue($maturity);
  2096. $price = Functions::flattenSingleValue($price);
  2097. $redemption = Functions::flattenSingleValue($redemption);
  2098. $basis = (int) Functions::flattenSingleValue($basis);
  2099. // Validate
  2100. if (is_numeric($price) && is_numeric($redemption)) {
  2101. if (($price <= 0) || ($redemption <= 0)) {
  2102. return Functions::NAN();
  2103. }
  2104. $daysPerYear = self::daysPerYear(DateTime::YEAR($settlement), $basis);
  2105. if (!is_numeric($daysPerYear)) {
  2106. return $daysPerYear;
  2107. }
  2108. $daysBetweenSettlementAndMaturity = DateTime::YEARFRAC($settlement, $maturity, $basis);
  2109. if (!is_numeric($daysBetweenSettlementAndMaturity)) {
  2110. // return date error
  2111. return $daysBetweenSettlementAndMaturity;
  2112. }
  2113. $daysBetweenSettlementAndMaturity *= $daysPerYear;
  2114. return (($redemption - $price) / $price) * ($daysPerYear / $daysBetweenSettlementAndMaturity);
  2115. }
  2116. return Functions::VALUE();
  2117. }
  2118. /**
  2119. * YIELDMAT.
  2120. *
  2121. * Returns the annual yield of a security that pays interest at maturity.
  2122. *
  2123. * @param mixed $settlement The security's settlement date.
  2124. * The security's settlement date is the date after the issue date when the security is traded to the buyer.
  2125. * @param mixed $maturity The security's maturity date.
  2126. * The maturity date is the date when the security expires.
  2127. * @param mixed $issue The security's issue date
  2128. * @param int $rate The security's interest rate at date of issue
  2129. * @param int $price The security's price per $100 face value
  2130. * @param int $basis The type of day count to use.
  2131. * 0 or omitted US (NASD) 30/360
  2132. * 1 Actual/actual
  2133. * 2 Actual/360
  2134. * 3 Actual/365
  2135. * 4 European 30/360
  2136. *
  2137. * @return float
  2138. */
  2139. public static function YIELDMAT($settlement, $maturity, $issue, $rate, $price, $basis = 0)
  2140. {
  2141. $settlement = Functions::flattenSingleValue($settlement);
  2142. $maturity = Functions::flattenSingleValue($maturity);
  2143. $issue = Functions::flattenSingleValue($issue);
  2144. $rate = Functions::flattenSingleValue($rate);
  2145. $price = Functions::flattenSingleValue($price);
  2146. $basis = (int) Functions::flattenSingleValue($basis);
  2147. // Validate
  2148. if (is_numeric($rate) && is_numeric($price)) {
  2149. if (($rate <= 0) || ($price <= 0)) {
  2150. return Functions::NAN();
  2151. }
  2152. $daysPerYear = self::daysPerYear(DateTime::YEAR($settlement), $basis);
  2153. if (!is_numeric($daysPerYear)) {
  2154. return $daysPerYear;
  2155. }
  2156. $daysBetweenIssueAndSettlement = DateTime::YEARFRAC($issue, $settlement, $basis);
  2157. if (!is_numeric($daysBetweenIssueAndSettlement)) {
  2158. // return date error
  2159. return $daysBetweenIssueAndSettlement;
  2160. }
  2161. $daysBetweenIssueAndSettlement *= $daysPerYear;
  2162. $daysBetweenIssueAndMaturity = DateTime::YEARFRAC($issue, $maturity, $basis);
  2163. if (!is_numeric($daysBetweenIssueAndMaturity)) {
  2164. // return date error
  2165. return $daysBetweenIssueAndMaturity;
  2166. }
  2167. $daysBetweenIssueAndMaturity *= $daysPerYear;
  2168. $daysBetweenSettlementAndMaturity = DateTime::YEARFRAC($settlement, $maturity, $basis);
  2169. if (!is_numeric($daysBetweenSettlementAndMaturity)) {
  2170. // return date error
  2171. return $daysBetweenSettlementAndMaturity;
  2172. }
  2173. $daysBetweenSettlementAndMaturity *= $daysPerYear;
  2174. return ((1 + (($daysBetweenIssueAndMaturity / $daysPerYear) * $rate) - (($price / 100) + (($daysBetweenIssueAndSettlement / $daysPerYear) * $rate))) /
  2175. (($price / 100) + (($daysBetweenIssueAndSettlement / $daysPerYear) * $rate))) *
  2176. ($daysPerYear / $daysBetweenSettlementAndMaturity);
  2177. }
  2178. return Functions::VALUE();
  2179. }
  2180. }