/var/www/hkosl.com/imusiccircle/webadmin/libraies/phpoffice/phpexcel/Classes/PHPExcel/Calculation/Financial.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
<?php
/**
 * PHPExcel
 *
 * Copyright (c) 2006 - 2014 PHPExcel
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 *
 * @category    PHPExcel
 * @package        PHPExcel_Calculation
 * @copyright    Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
 * @license        http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
 * @version        ##VERSION##, ##DATE##
 */


/** PHPExcel root directory */
if (!defined('PHPEXCEL_ROOT')) {
    
/**
     * @ignore
     */
    
define('PHPEXCEL_ROOT'dirname(__FILE__) . '/../../');
    require(
PHPEXCEL_ROOT 'PHPExcel/Autoloader.php');
}


/** FINANCIAL_MAX_ITERATIONS */
define('FINANCIAL_MAX_ITERATIONS'128);

/** FINANCIAL_PRECISION */
define('FINANCIAL_PRECISION'1.0e-08);


/**
 * PHPExcel_Calculation_Financial
 *
 * @category    PHPExcel
 * @package        PHPExcel_Calculation
 * @copyright    Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
 */
class PHPExcel_Calculation_Financial {

    
/**
     * _lastDayOfMonth
     *
     * Returns a boolean TRUE/FALSE indicating if this date is the last date of the month
     *
     * @param    DateTime    $testDate    The date for testing
     * @return    boolean
     */
    
private static function _lastDayOfMonth($testDate)
    {
        return (
$testDate->format('d') == $testDate->format('t'));
    }    
//    function _lastDayOfMonth()


    /**
     * _firstDayOfMonth
     *
     * Returns a boolean TRUE/FALSE indicating if this date is the first date of the month
     *
     * @param    DateTime    $testDate    The date for testing
     * @return    boolean
     */
    
private static function _firstDayOfMonth($testDate)
    {
        return (
$testDate->format('d') == 1);
    }    
//    function _firstDayOfMonth()


    
private static function _coupFirstPeriodDate($settlement$maturity$frequency$next)
    {
        
$months 12 $frequency;

        
$result PHPExcel_Shared_Date::ExcelToPHPObject($maturity);
        
$eom self::_lastDayOfMonth($result);

        while (
$settlement PHPExcel_Shared_Date::PHPToExcel($result)) {
            
$result->modify('-'.$months.' months');
        }
        if (
$next) {
            
$result->modify('+'.$months.' months');
        }

        if (
$eom) {
            
$result->modify('-1 day');
        }

        return 
PHPExcel_Shared_Date::PHPToExcel($result);
    }    
//    function _coupFirstPeriodDate()


    
private static function _validFrequency($frequency)
    {
        if ((
$frequency == 1) || ($frequency == 2) || ($frequency == 4)) {
            return 
true;
        }
        if ((
PHPExcel_Calculation_Functions::getCompatibilityMode() == PHPExcel_Calculation_Functions::COMPATIBILITY_GNUMERIC) &&
            ((
$frequency == 6) || ($frequency == 12))) {
            return 
true;
        }
        return 
false;
    }    
//    function _validFrequency()


    /**
     * _daysPerYear
     *
     * Returns the number of days in a specified year, as defined by the "basis" value
     *
     * @param    integer        $year    The year against which we're testing
     * @param   integer        $basis    The type of day count:
     *                                    0 or omitted US (NASD)    360
     *                                    1                        Actual (365 or 366 in a leap year)
     *                                    2                        360
     *                                    3                        365
     *                                    4                        European 360
     * @return    integer
     */
    
private static function _daysPerYear($year$basis=0)
    {
        switch (
$basis) {
            case 
:
            case 
:
            case 
:
                
$daysPerYear 360;
                break;
            case 
:
                
$daysPerYear 365;
                break;
            case 
:
                
$daysPerYear = (PHPExcel_Calculation_DateTime::_isLeapYear($year)) ? 366 365;
                break;
            default    :
                return 
PHPExcel_Calculation_Functions::NaN();
        }
        return 
$daysPerYear;
    }    
//    function _daysPerYear()


    
private static function _interestAndPrincipal($rate=0$per=0$nper=0$pv=0$fv=0$type=0)
    {
        
$pmt self::PMT($rate$nper$pv$fv$type);
        
$capital $pv;
        for (
$i 1$i<= $per; ++$i) {
            
$interest = ($type && $i == 1) ? : -$capital $rate;
            
$principal $pmt $interest;
            
$capital += $principal;
        }
        return array(
$interest$principal);
    }    
//    function _interestAndPrincipal()


    /**
     * ACCRINT
     *
     * Returns the accrued interest for a security that pays periodic interest.
     *
     * Excel Function:
     *        ACCRINT(issue,firstinterest,settlement,rate,par,frequency[,basis])
     *
     * @access    public
     * @category Financial Functions
     * @param    mixed    $issue            The security's issue date.
     * @param    mixed    $firstinterest    The security's first interest date.
     * @param    mixed    $settlement        The security's settlement date.
     *                                    The security settlement date is the date after the issue date
     *                                    when the security is traded to the buyer.
     * @param    float    $rate            The security's annual coupon rate.
     * @param    float    $par            The security's par value.
     *                                    If you omit par, ACCRINT uses $1,000.
     * @param    integer    $frequency        the number of coupon payments per year.
     *                                    Valid frequency values are:
     *                                        1    Annual
     *                                        2    Semi-Annual
     *                                        4    Quarterly
     *                                    If working in Gnumeric Mode, the following frequency options are
     *                                    also available
     *                                        6    Bimonthly
     *                                        12    Monthly
     * @param    integer    $basis            The type of day count to use.
     *                                        0 or omitted    US (NASD) 30/360
     *                                        1                Actual/actual
     *                                        2                Actual/360
     *                                        3                Actual/365
     *                                        4                European 30/360
     * @return    float
     */
    
public static function ACCRINT($issue$firstinterest$settlement$rate$par=1000$frequency=1$basis=0)
    {
        
$issue        PHPExcel_Calculation_Functions::flattenSingleValue($issue);
        
$firstinterest    PHPExcel_Calculation_Functions::flattenSingleValue($firstinterest);
        
$settlement    PHPExcel_Calculation_Functions::flattenSingleValue($settlement);
        
$rate        PHPExcel_Calculation_Functions::flattenSingleValue($rate);
        
$par        = (is_null($par))        ? 1000 :    PHPExcel_Calculation_Functions::flattenSingleValue($par);
        
$frequency    = (is_null($frequency))    ? 1    :         PHPExcel_Calculation_Functions::flattenSingleValue($frequency);
        
$basis        = (is_null($basis))        ? 0    :        PHPExcel_Calculation_Functions::flattenSingleValue($basis);

        
//    Validate
        
if ((is_numeric($rate)) && (is_numeric($par))) {
            
$rate    = (float) $rate;
            
$par    = (float) $par;
            if ((
$rate <= 0) || ($par <= 0)) {
                return 
PHPExcel_Calculation_Functions::NaN();
            }
            
$daysBetweenIssueAndSettlement PHPExcel_Calculation_DateTime::YEARFRAC($issue$settlement$basis);
            if (!
is_numeric($daysBetweenIssueAndSettlement)) {
                
//    return date error
                
return $daysBetweenIssueAndSettlement;
            }

            return 
$par $rate $daysBetweenIssueAndSettlement;
        }
        return 
PHPExcel_Calculation_Functions::VALUE();
    }    
//    function ACCRINT()


    /**
     * ACCRINTM
     *
     * Returns the accrued interest for a security that pays interest at maturity.
     *
     * Excel Function:
     *        ACCRINTM(issue,settlement,rate[,par[,basis]])
     *
     * @access    public
     * @category Financial Functions
     * @param    mixed    issue        The security's issue date.
     * @param    mixed    settlement    The security's settlement (or maturity) date.
     * @param    float    rate        The security's annual coupon rate.
     * @param    float    par            The security's par value.
     *                                    If you omit par, ACCRINT uses $1,000.
     * @param    integer    basis        The type of day count to use.
     *                                        0 or omitted    US (NASD) 30/360
     *                                        1                Actual/actual
     *                                        2                Actual/360
     *                                        3                Actual/365
     *                                        4                European 30/360
     * @return    float
     */
    
public static function ACCRINTM($issue$settlement$rate$par=1000$basis=0) {
        
$issue        PHPExcel_Calculation_Functions::flattenSingleValue($issue);
        
$settlement    PHPExcel_Calculation_Functions::flattenSingleValue($settlement);
        
$rate        PHPExcel_Calculation_Functions::flattenSingleValue($rate);
        
$par        = (is_null($par))    ? 1000 :    PHPExcel_Calculation_Functions::flattenSingleValue($par);
        
$basis        = (is_null($basis))    ? :        PHPExcel_Calculation_Functions::flattenSingleValue($basis);

        
//    Validate
        
if ((is_numeric($rate)) && (is_numeric($par))) {
            
$rate    = (float) $rate;
            
$par    = (float) $par;
            if ((
$rate <= 0) || ($par <= 0)) {
                return 
PHPExcel_Calculation_Functions::NaN();
            }
            
$daysBetweenIssueAndSettlement PHPExcel_Calculation_DateTime::YEARFRAC($issue$settlement$basis);
            if (!
is_numeric($daysBetweenIssueAndSettlement)) {
                
//    return date error
                
return $daysBetweenIssueAndSettlement;
            }
            return 
$par $rate $daysBetweenIssueAndSettlement;
        }
        return 
PHPExcel_Calculation_Functions::VALUE();
    }    
//    function ACCRINTM()


    /**
     * AMORDEGRC
     *
     * Returns the depreciation for each accounting period.
     * This function is provided for the French accounting system. If an asset is purchased in
     * the middle of the accounting period, the prorated depreciation is taken into account.
     * The function is similar to AMORLINC, except that a depreciation coefficient is applied in
     * the calculation depending on the life of the assets.
     * This function will return the depreciation until the last period of the life of the assets
     * or until the cumulated value of depreciation is greater than the cost of the assets minus
     * the salvage value.
     *
     * Excel Function:
     *        AMORDEGRC(cost,purchased,firstPeriod,salvage,period,rate[,basis])
     *
     * @access    public
     * @category Financial Functions
     * @param    float    cost        The cost of the asset.
     * @param    mixed    purchased    Date of the purchase of the asset.
     * @param    mixed    firstPeriod    Date of the end of the first period.
     * @param    mixed    salvage        The salvage value at the end of the life of the asset.
     * @param    float    period        The period.
     * @param    float    rate        Rate of depreciation.
     * @param    integer    basis        The type of day count to use.
     *                                        0 or omitted    US (NASD) 30/360
     *                                        1                Actual/actual
     *                                        2                Actual/360
     *                                        3                Actual/365
     *                                        4                European 30/360
     * @return    float
     */
    
public static function AMORDEGRC($cost$purchased$firstPeriod$salvage$period$rate$basis=0) {
        
$cost            PHPExcel_Calculation_Functions::flattenSingleValue($cost);
        
$purchased        PHPExcel_Calculation_Functions::flattenSingleValue($purchased);
        
$firstPeriod    PHPExcel_Calculation_Functions::flattenSingleValue($firstPeriod);
        
$salvage        PHPExcel_Calculation_Functions::flattenSingleValue($salvage);
        
$period            floor(PHPExcel_Calculation_Functions::flattenSingleValue($period));
        
$rate            PHPExcel_Calculation_Functions::flattenSingleValue($rate);
        
$basis            = (is_null($basis))    ? :    (int) PHPExcel_Calculation_Functions::flattenSingleValue($basis);

        
//    The depreciation coefficients are:
        //    Life of assets (1/rate)        Depreciation coefficient
        //    Less than 3 years            1
        //    Between 3 and 4 years        1.5
        //    Between 5 and 6 years        2
        //    More than 6 years            2.5
        
$fUsePer 1.0 $rate;
        if (
$fUsePer 3.0) {
            
$amortiseCoeff 1.0;
        } elseif (
$fUsePer 5.0) {
            
$amortiseCoeff 1.5;
        } elseif (
$fUsePer <= 6.0) {
            
$amortiseCoeff 2.0;
        } else {
            
$amortiseCoeff 2.5;
        }

        
$rate *= $amortiseCoeff;
        
$fNRate round(PHPExcel_Calculation_DateTime::YEARFRAC($purchased$firstPeriod$basis) * $rate $cost,0);
        
$cost -= $fNRate;
        
$fRest $cost $salvage;

        for (
$n 0$n $period; ++$n) {
            
$fNRate round($rate $cost,0);
            
$fRest -= $fNRate;

            if (
$fRest 0.0) {
                switch (
$period $n) {
                    case 
0    :
                    case 
1    : return round($cost 0.50);
                              break;
                    default    : return 
0.0;
                              break;
                }
            }
            
$cost -= $fNRate;
        }
        return 
$fNRate;
    }    
//    function AMORDEGRC()


    /**
     * AMORLINC
     *
     * Returns the depreciation for each accounting period.
     * This function is provided for the French accounting system. If an asset is purchased in
     * the middle of the accounting period, the prorated depreciation is taken into account.
     *
     * Excel Function:
     *        AMORLINC(cost,purchased,firstPeriod,salvage,period,rate[,basis])
     *
     * @access    public
     * @category Financial Functions
     * @param    float    cost        The cost of the asset.
     * @param    mixed    purchased    Date of the purchase of the asset.
     * @param    mixed    firstPeriod    Date of the end of the first period.
     * @param    mixed    salvage        The salvage value at the end of the life of the asset.
     * @param    float    period        The period.
     * @param    float    rate        Rate of depreciation.
     * @param    integer    basis        The type of day count to use.
     *                                        0 or omitted    US (NASD) 30/360
     *                                        1                Actual/actual
     *                                        2                Actual/360
     *                                        3                Actual/365
     *                                        4                European 30/360
     * @return    float
     */
    
public static function AMORLINC($cost$purchased$firstPeriod$salvage$period$rate$basis=0) {
        
$cost            PHPExcel_Calculation_Functions::flattenSingleValue($cost);
        
$purchased        PHPExcel_Calculation_Functions::flattenSingleValue($purchased);
        
$firstPeriod    PHPExcel_Calculation_Functions::flattenSingleValue($firstPeriod);
        
$salvage        PHPExcel_Calculation_Functions::flattenSingleValue($salvage);
        
$period            PHPExcel_Calculation_Functions::flattenSingleValue($period);
        
$rate            PHPExcel_Calculation_Functions::flattenSingleValue($rate);
        
$basis            = (is_null($basis))    ? :    (int) PHPExcel_Calculation_Functions::flattenSingleValue($basis);

        
$fOneRate $cost $rate;
        
$fCostDelta $cost $salvage;
        
//    Note, quirky variation for leap years on the YEARFRAC for this function
        
$purchasedYear PHPExcel_Calculation_DateTime::YEAR($purchased);
        
$yearFrac PHPExcel_Calculation_DateTime::YEARFRAC($purchased$firstPeriod$basis);

        if ((
$basis == 1) && ($yearFrac 1) && (PHPExcel_Calculation_DateTime::_isLeapYear($purchasedYear))) {
            
$yearFrac *= 365 366;
        }

        
$f0Rate $yearFrac $rate $cost;
        
$nNumOfFullPeriods intval(($cost $salvage $f0Rate) / $fOneRate);

        if (
$period == 0) {
            return 
$f0Rate;
        } elseif (
$period <= $nNumOfFullPeriods) {
            return 
$fOneRate;
        } elseif (
$period == ($nNumOfFullPeriods 1)) {
            return (
$fCostDelta $fOneRate $nNumOfFullPeriods $f0Rate);
        } else {
            return 
0.0;
        }
    }    
//    function AMORLINC()


    /**
     * COUPDAYBS
     *
     * Returns the number of days from the beginning of the coupon period to the settlement date.
     *
     * Excel Function:
     *        COUPDAYBS(settlement,maturity,frequency[,basis])
     *
     * @access    public
     * @category Financial Functions
     * @param    mixed    settlement    The security's settlement date.
     *                                The security settlement date is the date after the issue
     *                                date when the security is traded to the buyer.
     * @param    mixed    maturity    The security's maturity date.
     *                                The maturity date is the date when the security expires.
     * @param    mixed    frequency    the number of coupon payments per year.
     *                                    Valid frequency values are:
     *                                        1    Annual
     *                                        2    Semi-Annual
     *                                        4    Quarterly
     *                                    If working in Gnumeric Mode, the following frequency options are
     *                                    also available
     *                                        6    Bimonthly
     *                                        12    Monthly
     * @param    integer        basis        The type of day count to use.
     *                                        0 or omitted    US (NASD) 30/360
     *                                        1                Actual/actual
     *                                        2                Actual/360
     *                                        3                Actual/365
     *                                        4                European 30/360
     * @return    float
     */
    
public static function COUPDAYBS($settlement$maturity$frequency$basis=0) {
        
$settlement    PHPExcel_Calculation_Functions::flattenSingleValue($settlement);
        
$maturity    PHPExcel_Calculation_Functions::flattenSingleValue($maturity);
        
$frequency    = (int) PHPExcel_Calculation_Functions::flattenSingleValue($frequency);
        
$basis        = (is_null($basis))    ? :    (int) PHPExcel_Calculation_Functions::flattenSingleValue($basis);

        if (
is_string($settlement PHPExcel_Calculation_DateTime::_getDateValue($settlement))) {
            return 
PHPExcel_Calculation_Functions::VALUE();
        }
        if (
is_string($maturity PHPExcel_Calculation_DateTime::_getDateValue($maturity))) {
            return 
PHPExcel_Calculation_Functions::VALUE();
        }

        if ((
$settlement $maturity) ||
            (!
self::_validFrequency($frequency)) ||
            ((
$basis 0) || ($basis 4))) {
            return 
PHPExcel_Calculation_Functions::NaN();
        }

        
$daysPerYear self::_daysPerYear(PHPExcel_Calculation_DateTime::YEAR($settlement),$basis);
        
$prev self::_coupFirstPeriodDate($settlement$maturity$frequencyFalse);

        return 
PHPExcel_Calculation_DateTime::YEARFRAC($prev$settlement$basis) * $daysPerYear;
    }    
//    function COUPDAYBS()


    /**
     * COUPDAYS
     *
     * Returns the number of days in the coupon period that contains the settlement date.
     *
     * Excel Function:
     *        COUPDAYS(settlement,maturity,frequency[,basis])
     *
     * @access    public
     * @category Financial Functions
     * @param    mixed    settlement    The security's settlement date.
     *                                The security settlement date is the date after the issue
     *                                date when the security is traded to the buyer.
     * @param    mixed    maturity    The security's maturity date.
     *                                The maturity date is the date when the security expires.
     * @param    mixed    frequency    the number of coupon payments per year.
     *                                    Valid frequency values are:
     *                                        1    Annual
     *                                        2    Semi-Annual
     *                                        4    Quarterly
     *                                    If working in Gnumeric Mode, the following frequency options are
     *                                    also available
     *                                        6    Bimonthly
     *                                        12    Monthly
     * @param    integer        basis        The type of day count to use.
     *                                        0 or omitted    US (NASD) 30/360
     *                                        1                Actual/actual
     *                                        2                Actual/360
     *                                        3                Actual/365
     *                                        4                European 30/360
     * @return    float
     */
    
public static function COUPDAYS($settlement$maturity$frequency$basis=0) {
        
$settlement    PHPExcel_Calculation_Functions::flattenSingleValue($settlement);
        
$maturity    PHPExcel_Calculation_Functions::flattenSingleValue($maturity);
        
$frequency    = (int) PHPExcel_Calculation_Functions::flattenSingleValue($frequency);
        
$basis        = (is_null($basis))    ? :    (int) PHPExcel_Calculation_Functions::flattenSingleValue($basis);

        if (
is_string($settlement PHPExcel_Calculation_DateTime::_getDateValue($settlement))) {
            return 
PHPExcel_Calculation_Functions::VALUE();
        }
        if (
is_string($maturity PHPExcel_Calculation_DateTime::_getDateValue($maturity))) {
            return 
PHPExcel_Calculation_Functions::VALUE();
        }

        if ((
$settlement $maturity) ||
            (!
self::_validFrequency($frequency)) ||
            ((
$basis 0) || ($basis 4))) {
            return 
PHPExcel_Calculation_Functions::NaN();
        }

        switch (
$basis) {
            case 
3// Actual/365
                    
return 365 $frequency;
            case 
1// Actual/actual
                    
if ($frequency == 1) {
                        
$daysPerYear self::_daysPerYear(PHPExcel_Calculation_DateTime::YEAR($maturity),$basis);
                        return (
$daysPerYear $frequency);
                    } else {
                        
$prev self::_coupFirstPeriodDate($settlement$maturity$frequencyFalse);
                        
$next self::_coupFirstPeriodDate($settlement$maturity$frequencyTrue);
                        return (
$next $prev);
                    }
            default: 
// US (NASD) 30/360, Actual/360 or European 30/360
                    
return 360 $frequency;
        }
        return 
PHPExcel_Calculation_Functions::VALUE();
    }    
//    function COUPDAYS()


    /**
     * COUPDAYSNC
     *
     * Returns the number of days from the settlement date to the next coupon date.
     *
     * Excel Function:
     *        COUPDAYSNC(settlement,maturity,frequency[,basis])
     *
     * @access    public
     * @category Financial Functions
     * @param    mixed    settlement    The security's settlement date.
     *                                The security settlement date is the date after the issue
     *                                date when the security is traded to the buyer.
     * @param    mixed    maturity    The security's maturity date.
     *                                The maturity date is the date when the security expires.
     * @param    mixed    frequency    the number of coupon payments per year.
     *                                    Valid frequency values are:
     *                                        1    Annual
     *                                        2    Semi-Annual
     *                                        4    Quarterly
     *                                    If working in Gnumeric Mode, the following frequency options are
     *                                    also available
     *                                        6    Bimonthly
     *                                        12    Monthly
     * @param    integer        basis        The type of day count to use.
     *                                        0 or omitted    US (NASD) 30/360
     *                                        1                Actual/actual
     *                                        2                Actual/360
     *                                        3                Actual/365
     *                                        4                European 30/360
     * @return    float
     */
    
public static function COUPDAYSNC($settlement$maturity$frequency$basis=0) {
        
$settlement    PHPExcel_Calculation_Functions::flattenSingleValue($settlement);
        
$maturity    PHPExcel_Calculation_Functions::flattenSingleValue($maturity);
        
$frequency    = (int) PHPExcel_Calculation_Functions::flattenSingleValue($frequency);
        
$basis        = (is_null($basis))    ? :    (int) PHPExcel_Calculation_Functions::flattenSingleValue($basis);

        if (
is_string($settlement PHPExcel_Calculation_DateTime::_getDateValue($settlement))) {
            return 
PHPExcel_Calculation_Functions::VALUE();
        }
        if (
is_string($maturity PHPExcel_Calculation_DateTime::_getDateValue($maturity))) {
            return 
PHPExcel_Calculation_Functions::VALUE();
        }

        if ((
$settlement $maturity) ||
            (!
self::_validFrequency($frequency)) ||
            ((
$basis 0) || ($basis 4))) {
            return 
PHPExcel_Calculation_Functions::NaN();
        }

        
$daysPerYear self::_daysPerYear(PHPExcel_Calculation_DateTime::YEAR($settlement),$basis);
        
$next self::_coupFirstPeriodDate($settlement$maturity$frequencyTrue);

        return 
PHPExcel_Calculation_DateTime::YEARFRAC($settlement$next$basis) * $daysPerYear;
    }    
//    function COUPDAYSNC()


    /**
     * COUPNCD
     *
     * Returns the next coupon date after the settlement date.
     *
     * Excel Function:
     *        COUPNCD(settlement,maturity,frequency[,basis])
     *
     * @access    public
     * @category Financial Functions
     * @param    mixed    settlement    The security's settlement date.
     *                                The security settlement date is the date after the issue
     *                                date when the security is traded to the buyer.
     * @param    mixed    maturity    The security's maturity date.
     *                                The maturity date is the date when the security expires.
     * @param    mixed    frequency    the number of coupon payments per year.
     *                                    Valid frequency values are:
     *                                        1    Annual
     *                                        2    Semi-Annual
     *                                        4    Quarterly
     *                                    If working in Gnumeric Mode, the following frequency options are
     *                                    also available
     *                                        6    Bimonthly
     *                                        12    Monthly
     * @param    integer        basis        The type of day count to use.
     *                                        0 or omitted    US (NASD) 30/360
     *                                        1                Actual/actual
     *                                        2                Actual/360
     *                                        3                Actual/365
     *                                        4                European 30/360
     * @return    mixed    Excel date/time serial value, PHP date/time serial value or PHP date/time object,
     *                        depending on the value of the ReturnDateType flag
     */
    
public static function COUPNCD($settlement$maturity$frequency$basis=0) {
        
$settlement    PHPExcel_Calculation_Functions::flattenSingleValue($settlement);
        
$maturity    PHPExcel_Calculation_Functions::flattenSingleValue($maturity);
        
$frequency    = (int) PHPExcel_Calculation_Functions::flattenSingleValue($frequency);
        
$basis        = (is_null($basis))    ? :    (int) PHPExcel_Calculation_Functions::flattenSingleValue($basis);

        if (
is_string($settlement PHPExcel_Calculation_DateTime::_getDateValue($settlement))) {
            return 
PHPExcel_Calculation_Functions::VALUE();
        }
        if (
is_string($maturity PHPExcel_Calculation_DateTime::_getDateValue($maturity))) {
            return 
PHPExcel_Calculation_Functions::VALUE();
        }

        if ((
$settlement $maturity) ||
            (!
self::_validFrequency($frequency)) ||
            ((
$basis 0) || ($basis 4))) {
            return 
PHPExcel_Calculation_Functions::NaN();
        }

        return 
self::_coupFirstPeriodDate($settlement$maturity$frequencyTrue);
    }    
//    function COUPNCD()


    /**
     * COUPNUM
     *
     * Returns the number of coupons payable between the settlement date and maturity date,
     * rounded up to the nearest whole coupon.
     *
     * Excel Function:
     *        COUPNUM(settlement,maturity,frequency[,basis])
     *
     * @access    public
     * @category Financial Functions
     * @param    mixed    settlement    The security's settlement date.
     *                                The security settlement date is the date after the issue
     *                                date when the security is traded to the buyer.
     * @param    mixed    maturity    The security's maturity date.
     *                                The maturity date is the date when the security expires.
     * @param    mixed    frequency    the number of coupon payments per year.
     *                                    Valid frequency values are:
     *                                        1    Annual
     *                                        2    Semi-Annual
     *                                        4    Quarterly
     *                                    If working in Gnumeric Mode, the following frequency options are
     *                                    also available
     *                                        6    Bimonthly
     *                                        12    Monthly
     * @param    integer        basis        The type of day count to use.
     *                                        0 or omitted    US (NASD) 30/360
     *                                        1                Actual/actual
     *                                        2                Actual/360
     *                                        3                Actual/365
     *                                        4                European 30/360
     * @return    integer
     */
    
public static function COUPNUM($settlement$maturity$frequency$basis=0) {
        
$settlement    PHPExcel_Calculation_Functions::flattenSingleValue($settlement);
        
$maturity    PHPExcel_Calculation_Functions::flattenSingleValue($maturity);
        
$frequency    = (int) PHPExcel_Calculation_Functions::flattenSingleValue($frequency);
        
$basis        = (is_null($basis))    ? :    (int) PHPExcel_Calculation_Functions::flattenSingleValue($basis);

        if (
is_string($settlement PHPExcel_Calculation_DateTime::_getDateValue($settlement))) {
            return 
PHPExcel_Calculation_Functions::VALUE();
        }
        if (
is_string($maturity PHPExcel_Calculation_DateTime::_getDateValue($maturity))) {
            return 
PHPExcel_Calculation_Functions::VALUE();
        }

        if ((
$settlement $maturity) ||
            (!
self::_validFrequency($frequency)) ||
            ((
$basis 0) || ($basis 4))) {
            return 
PHPExcel_Calculation_Functions::NaN();
        }

        
$settlement self::_coupFirstPeriodDate($settlement$maturity$frequencyTrue);
        
$daysBetweenSettlementAndMaturity PHPExcel_Calculation_DateTime::YEARFRAC($settlement$maturity$basis) * 365;

        switch (
$frequency) {
            case 
1// annual payments
                    
return ceil($daysBetweenSettlementAndMaturity 360);
            case 
2// half-yearly
                    
return ceil($daysBetweenSettlementAndMaturity 180);
            case 
4// quarterly
                    
return ceil($daysBetweenSettlementAndMaturity 90);
            case 
6// bimonthly
                    
return ceil($daysBetweenSettlementAndMaturity 60);
            case 
12// monthly
                    
return ceil($daysBetweenSettlementAndMaturity 30);
        }
        return 
PHPExcel_Calculation_Functions::VALUE();
    }    
//    function COUPNUM()


    /**
     * COUPPCD
     *
     * Returns the previous coupon date before the settlement date.
     *
     * Excel Function:
     *        COUPPCD(settlement,maturity,frequency[,basis])
     *
     * @access    public
     * @category Financial Functions
     * @param    mixed    settlement    The security's settlement date.
     *                                The security settlement date is the date after the issue
     *                                date when the security is traded to the buyer.
     * @param    mixed    maturity    The security's maturity date.
     *                                The maturity date is the date when the security expires.
     * @param    mixed    frequency    the number of coupon payments per year.
     *                                    Valid frequency values are:
     *                                        1    Annual
     *                                        2    Semi-Annual
     *                                        4    Quarterly
     *                                    If working in Gnumeric Mode, the following frequency options are
     *                                    also available
     *                                        6    Bimonthly
     *                                        12    Monthly
     * @param    integer        basis        The type of day count to use.
     *                                        0 or omitted    US (NASD) 30/360
     *                                        1                Actual/actual
     *                                        2                Actual/360
     *                                        3                Actual/365
     *                                        4                European 30/360
     * @return    mixed    Excel date/time serial value, PHP date/time serial value or PHP date/time object,
     *                        depending on the value of the ReturnDateType flag
     */
    
public static function COUPPCD($settlement$maturity$frequency$basis=0) {
        
$settlement    PHPExcel_Calculation_Functions::flattenSingleValue($settlement);
        
$maturity    PHPExcel_Calculation_Functions::flattenSingleValue($maturity);
        
$frequency    = (int) PHPExcel_Calculation_Functions::flattenSingleValue($frequency);
        
$basis        = (is_null($basis))    ? :    (int) PHPExcel_Calculation_Functions::flattenSingleValue($basis);

        if (
is_string($settlement PHPExcel_Calculation_DateTime::_getDateValue($settlement))) {
            return 
PHPExcel_Calculation_Functions::VALUE();
        }
        if (
is_string($maturity PHPExcel_Calculation_DateTime::_getDateValue($maturity))) {
            return 
PHPExcel_Calculation_Functions::VALUE();
        }

        if ((
$settlement $maturity) ||
            (!
self::_validFrequency($frequency)) ||
            ((
$basis 0) || ($basis 4))) {
            return 
PHPExcel_Calculation_Functions::NaN();
        }

        return 
self::_coupFirstPeriodDate($settlement$maturity$frequencyFalse);
    }    
//    function COUPPCD()


    /**
     * CUMIPMT
     *
     * Returns the cumulative interest paid on a loan between the start and end periods.
     *
     * Excel Function:
     *        CUMIPMT(rate,nper,pv,start,end[,type])
     *
     * @access    public
     * @category Financial Functions
     * @param    float    $rate    The Interest rate
     * @param    integer    $nper    The total number of payment periods
     * @param    float    $pv        Present Value
     * @param    integer    $start    The first period in the calculation.
     *                            Payment periods are numbered beginning with 1.
     * @param    integer    $end    The last period in the calculation.
     * @param    integer    $type    A number 0 or 1 and indicates when payments are due:
     *                                0 or omitted    At the end of the period.
     *                                1                At the beginning of the period.
     * @return    float
     */
    
public static function CUMIPMT($rate$nper$pv$start$end$type 0) {
        
$rate    PHPExcel_Calculation_Functions::flattenSingleValue($rate);
        
$nper    = (int) PHPExcel_Calculation_Functions::flattenSingleValue($nper);
        
$pv        PHPExcel_Calculation_Functions::flattenSingleValue($pv);
        
$start    = (int) PHPExcel_Calculation_Functions::flattenSingleValue($start);
        
$end    = (int) PHPExcel_Calculation_Functions::flattenSingleValue($end);
        
$type    = (int) PHPExcel_Calculation_Functions::flattenSingleValue($type);

        
// Validate parameters
        
if ($type != && $type != 1) {
            return 
PHPExcel_Calculation_Functions::NaN();
        }
        if (
$start || $start $end) {
            return 
PHPExcel_Calculation_Functions::VALUE();
        }

        
// Calculate
        
$interest 0;
        for (
$per $start$per <= $end; ++$per) {
            
$interest += self::IPMT($rate$per$nper$pv0$type);
        }

        return 
$interest;
    }    
//    function CUMIPMT()


    /**
     * CUMPRINC
     *
     * Returns the cumulative principal paid on a loan between the start and end periods.
     *
     * Excel Function:
     *        CUMPRINC(rate,nper,pv,start,end[,type])
     *
     * @access    public
     * @category Financial Functions
     * @param    float    $rate    The Interest rate
     * @param    integer    $nper    The total number of payment periods
     * @param    float    $pv        Present Value
     * @param    integer    $start    The first period in the calculation.
     *                            Payment periods are numbered beginning with 1.
     * @param    integer    $end    The last period in the calculation.
     * @param    integer    $type    A number 0 or 1 and indicates when payments are due:
     *                                0 or omitted    At the end of the period.
     *                                1                At the beginning of the period.
     * @return    float
     */
    
public static function CUMPRINC($rate$nper$pv$start$end$type 0) {
        
$rate    PHPExcel_Calculation_Functions::flattenSingleValue($rate);
        
$nper    = (int) PHPExcel_Calculation_Functions::flattenSingleValue($nper);
        
$pv        PHPExcel_Calculation_Functions::flattenSingleValue($pv);
        
$start    = (int) PHPExcel_Calculation_Functions::flattenSingleValue($start);
        
$end    = (int) PHPExcel_Calculation_Functions::flattenSingleValue($end);
        
$type    = (int) PHPExcel_Calculation_Functions::flattenSingleValue($type);

        
// Validate parameters
        
if ($type != && $type != 1) {
            return 
PHPExcel_Calculation_Functions::NaN();
        }
        if (
$start || $start $end) {
            return 
PHPExcel_Calculation_Functions::VALUE();
        }

        
// Calculate
        
$principal 0;
        for (
$per $start$per <= $end; ++$per) {
            
$principal += self::PPMT($rate$per$nper$pv0$type);
        }

        return 
$principal;
    }    
//    function CUMPRINC()


    /**
     * DB
     *
     * Returns the depreciation of an asset for a specified period using the
     * fixed-declining balance method.
     * This form of depreciation is used if you want to get a higher depreciation value
     * at the beginning of the depreciation (as opposed to linear depreciation). The
     * depreciation value is reduced with every depreciation period by the depreciation
     * already deducted from the initial cost.
     *
     * Excel Function:
     *        DB(cost,salvage,life,period[,month])
     *
     * @access    public
     * @category Financial Functions
     * @param    float    cost        Initial cost of the asset.
     * @param    float    salvage        Value at the end of the depreciation.
     *                                (Sometimes called the salvage value of the asset)
     * @param    integer    life        Number of periods over which the asset is depreciated.
     *                                (Sometimes called the useful life of the asset)
     * @param    integer    period        The period for which you want to calculate the
     *                                depreciation. Period must use the same units as life.
     * @param    integer    month        Number of months in the first year. If month is omitted,
     *                                it defaults to 12.
     * @return    float
     */
    
public static function DB($cost$salvage$life$period$month=12) {
        
$cost        PHPExcel_Calculation_Functions::flattenSingleValue($cost);
        
$salvage    PHPExcel_Calculation_Functions::flattenSingleValue($salvage);
        
$life        PHPExcel_Calculation_Functions::flattenSingleValue($life);
        
$period        PHPExcel_Calculation_Functions::flattenSingleValue($period);
        
$month        PHPExcel_Calculation_Functions::flattenSingleValue($month);

        
//    Validate
        
if ((is_numeric($cost)) && (is_numeric($salvage)) && (is_numeric($life)) && (is_numeric($period)) && (is_numeric($month))) {
            
$cost        = (float) $cost;
            
$salvage    = (float) $salvage;
            
$life        = (int) $life;
            
$period        = (int) $period;
            
$month        = (int) $month;
            if (
$cost == 0) {
                return 
0.0;
            } elseif ((
$cost 0) || (($salvage $cost) < 0) || ($life <= 0) || ($period 1) || ($month 1)) {
                return 
PHPExcel_Calculation_Functions::NaN();
            }
            
//    Set Fixed Depreciation Rate
            
$fixedDepreciationRate pow(($salvage $cost), ($life));
            
$fixedDepreciationRate round($fixedDepreciationRate3);

            
//    Loop through each period calculating the depreciation
            
$previousDepreciation 0;
            for (
$per 1$per <= $period; ++$per) {
                if (
$per == 1) {
                    
$depreciation $cost $fixedDepreciationRate $month 12;
                } elseif (
$per == ($life 1)) {
                    
$depreciation = ($cost $previousDepreciation) * $fixedDepreciationRate * (12 $month) / 12;
                } else {
                    
$depreciation = ($cost $previousDepreciation) * $fixedDepreciationRate;
                }
                
$previousDepreciation += $depreciation;
            }
            if (
PHPExcel_Calculation_Functions::getCompatibilityMode() == PHPExcel_Calculation_Functions::COMPATIBILITY_GNUMERIC) {
                
$depreciation round($depreciation,2);
            }
            return 
$depreciation;
        }
        return 
PHPExcel_Calculation_Functions::VALUE();
    }    
//    function DB()


    /**
     * DDB
     *
     * Returns the depreciation of an asset for a specified period using the
     * double-declining balance method or some other method you specify.
     *
     * Excel Function:
     *        DDB(cost,salvage,life,period[,factor])
     *
     * @access    public
     * @category Financial Functions
     * @param    float    cost        Initial cost of the asset.
     * @param    float    salvage        Value at the end of the depreciation.
     *                                (Sometimes called the salvage value of the asset)
     * @param    integer    life        Number of periods over which the asset is depreciated.
     *                                (Sometimes called the useful life of the asset)
     * @param    integer    period        The period for which you want to calculate the
     *                                depreciation. Period must use the same units as life.
     * @param    float    factor        The rate at which the balance declines.
     *                                If factor is omitted, it is assumed to be 2 (the
     *                                double-declining balance method).
     * @return    float
     */
    
public static function DDB($cost$salvage$life$period$factor=2.0) {
        
$cost        PHPExcel_Calculation_Functions::flattenSingleValue($cost);
        
$salvage    PHPExcel_Calculation_Functions::flattenSingleValue($salvage);
        
$life        PHPExcel_Calculation_Functions::flattenSingleValue($life);
        
$period        PHPExcel_Calculation_Functions::flattenSingleValue($period);
        
$factor        PHPExcel_Calculation_Functions::flattenSingleValue($factor);

        
//    Validate
        
if ((is_numeric($cost)) && (is_numeric($salvage)) && (is_numeric($life)) && (is_numeric($period)) && (is_numeric($factor))) {
            
$cost        = (float) $cost;
            
$salvage    = (float) $salvage;
            
$life        = (int) $life;
            
$period        = (int) $period;
            
$factor        = (float) $factor;
            if ((
$cost <= 0) || (($salvage $cost) < 0) || ($life <= 0) || ($period 1) || ($factor <= 0.0) || ($period $life)) {
                return 
PHPExcel_Calculation_Functions::NaN();
            }
            
//    Set Fixed Depreciation Rate
            
$fixedDepreciationRate pow(($salvage $cost), ($life));
            
$fixedDepreciationRate round($fixedDepreciationRate3);

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


    /**
     * DISC
     *
     * Returns the discount rate for a security.
     *
     * Excel Function:
     *        DISC(settlement,maturity,price,redemption[,basis])
     *
     * @access    public
     * @category Financial Functions
     * @param    mixed    settlement    The security's settlement date.
     *                                The security settlement date is the date after the issue
     *                                date when the security is traded to the buyer.
     * @param    mixed    maturity    The security's maturity date.
     *                                The maturity date is the date when the security expires.
     * @param    integer    price        The security's price per $100 face value.
     * @param    integer    redemption    The security's redemption value per $100 face value.
     * @param    integer    basis        The type of day count to use.
     *                                        0 or omitted    US (NASD) 30/360
     *                                        1                Actual/actual
     *                                        2                Actual/360
     *                                        3                Actual/365
     *                                        4                European 30/360
     * @return    float
     */
    
public static function DISC($settlement$maturity$price$redemption$basis=0) {
        
$settlement    PHPExcel_Calculation_Functions::flattenSingleValue($settlement);
        
$maturity    PHPExcel_Calculation_Functions::flattenSingleValue($maturity);
        
$price        PHPExcel_Calculation_Functions::flattenSingleValue($price);
        
$redemption    PHPExcel_Calculation_Functions::flattenSingleValue($redemption);
        
$basis        PHPExcel_Calculation_Functions::flattenSingleValue($basis);

        
//    Validate
        
if ((is_numeric($price)) && (is_numeric($redemption)) && (is_numeric($basis))) {
            
$price        = (float) $price;
            
$redemption    = (float) $redemption;
            
$basis        = (int) $basis;
            if ((
$price <= 0) || ($redemption <= 0)) {
                return 
PHPExcel_Calculation_Functions::NaN();
            }
            
$daysBetweenSettlementAndMaturity PHPExcel_Calculation_DateTime::YEARFRAC($settlement$maturity$basis);
            if (!
is_numeric($daysBetweenSettlementAndMaturity)) {
                
//    return date error
                
return $daysBetweenSettlementAndMaturity;
            }

            return ((
$price $redemption) / $daysBetweenSettlementAndMaturity);
        }
        return 
PHPExcel_Calculation_Functions::VALUE();
    }    
//    function DISC()


    /**
     * DOLLARDE
     *
     * Converts a dollar price expressed as an integer part and a fraction
     *        part into a dollar price expressed as a decimal number.
     * Fractional dollar numbers are sometimes used for security prices.
     *
     * Excel Function:
     *        DOLLARDE(fractional_dollar,fraction)
     *
     * @access    public
     * @category Financial Functions
     * @param    float    $fractional_dollar    Fractional Dollar
     * @param    integer    $fraction            Fraction
     * @return    float
     */
    
public static function DOLLARDE($fractional_dollar Null$fraction 0) {
        
$fractional_dollar    PHPExcel_Calculation_Functions::flattenSingleValue($fractional_dollar);
        
$fraction            = (int)PHPExcel_Calculation_Functions::flattenSingleValue($fraction);

        
// Validate parameters
        
if (is_null($fractional_dollar) || $fraction 0) {
            return 
PHPExcel_Calculation_Functions::NaN();
        }
        if (
$fraction == 0) {
            return 
PHPExcel_Calculation_Functions::DIV0();
        }

        
$dollars floor($fractional_dollar);
        
$cents fmod($fractional_dollar,1);
        
$cents /= $fraction;
        
$cents *= pow(10,ceil(log10($fraction)));
        return 
$dollars $cents;
    }    
//    function DOLLARDE()


    /**
     * DOLLARFR
     *
     * Converts a dollar price expressed as a decimal number into a dollar price
     *        expressed as a fraction.
     * Fractional dollar numbers are sometimes used for security prices.
     *
     * Excel Function:
     *        DOLLARFR(decimal_dollar,fraction)
     *
     * @access    public
     * @category Financial Functions
     * @param    float    $decimal_dollar        Decimal Dollar
     * @param    integer    $fraction            Fraction
     * @return    float
     */
    
public static function DOLLARFR($decimal_dollar Null$fraction 0) {
        
$decimal_dollar    PHPExcel_Calculation_Functions::flattenSingleValue($decimal_dollar);
        
$fraction        = (int)PHPExcel_Calculation_Functions::flattenSingleValue($fraction);

        
// Validate parameters
        
if (is_null($decimal_dollar) || $fraction 0) {
            return 
PHPExcel_Calculation_Functions::NaN();
        }
        if (
$fraction == 0) {
            return 
PHPExcel_Calculation_Functions::DIV0();
        }

        
$dollars floor($decimal_dollar);
        
$cents fmod($decimal_dollar,1);
        
$cents *= $fraction;
        
$cents *= pow(10,-ceil(log10($fraction)));
        return 
$dollars $cents;
    }    
//    function DOLLARFR()


    /**
     * EFFECT
     *
     * Returns the effective interest rate given the nominal rate and the number of
     *        compounding payments per year.
     *
     * Excel Function:
     *        EFFECT(nominal_rate,npery)
     *
     * @access    public
     * @category Financial Functions
     * @param    float    $nominal_rate        Nominal interest rate
     * @param    integer    $npery                Number of compounding payments per year
     * @return    float
     */
    
public static function EFFECT($nominal_rate 0$npery 0) {
        
$nominal_rate    PHPExcel_Calculation_Functions::flattenSingleValue($nominal_rate);
        
$npery            = (int)PHPExcel_Calculation_Functions::flattenSingleValue($npery);

        
// Validate parameters
        
if ($nominal_rate <= || $npery 1) {
            return 
PHPExcel_Calculation_Functions::NaN();
        }

        return 
pow(($nominal_rate $npery), $npery) - 1;
    }    
//    function EFFECT()


    /**
     * FV
     *
     * Returns the Future Value of a cash flow with constant payments and interest rate (annuities).
     *
     * Excel Function:
     *        FV(rate,nper,pmt[,pv[,type]])
     *
     * @access    public
     * @category Financial Functions
     * @param    float    $rate    The interest rate per period
     * @param    int        $nper    Total number of payment periods in an annuity
     * @param    float    $pmt    The payment made each period: it cannot change over the
     *                            life of the annuity. Typically, pmt contains principal
     *                            and interest but no other fees or taxes.
     * @param    float    $pv        Present Value, or the lump-sum amount that a series of
     *                            future payments is worth right now.
     * @param    integer    $type    A number 0 or 1 and indicates when payments are due:
     *                                0 or omitted    At the end of the period.
     *                                1                At the beginning of the period.
     * @return    float
     */
    
public static function FV($rate 0$nper 0$pmt 0$pv 0$type 0) {
        
$rate    PHPExcel_Calculation_Functions::flattenSingleValue($rate);
        
$nper    PHPExcel_Calculation_Functions::flattenSingleValue($nper);
        
$pmt    PHPExcel_Calculation_Functions::flattenSingleValue($pmt);
        
$pv        PHPExcel_Calculation_Functions::flattenSingleValue($pv);
        
$type    PHPExcel_Calculation_Functions::flattenSingleValue($type);

        
// Validate parameters
        
if ($type != && $type != 1) {
            return 
PHPExcel_Calculation_Functions::NaN();
        }

        
// Calculate
        
if (!is_null($rate) && $rate != 0) {
            return -
$pv pow($rate$nper) - $pmt * ($rate $type) * (pow($rate$nper) - 1) / $rate;
        } else {
            return -
$pv $pmt $nper;
        }
    }    
//    function FV()


    /**
     * FVSCHEDULE
     *
     * Returns the future value of an initial principal after applying a series of compound interest rates.
     * Use FVSCHEDULE to calculate the future value of an investment with a variable or adjustable rate.
     *
     * Excel Function:
     *        FVSCHEDULE(principal,schedule)
     *
     * @param    float    $principal    The present value.
     * @param    float[]    $schedule    An array of interest rates to apply.
     * @return    float
     */
    
public static function FVSCHEDULE($principal$schedule) {
        
$principal    PHPExcel_Calculation_Functions::flattenSingleValue($principal);
        
$schedule    PHPExcel_Calculation_Functions::flattenArray($schedule);

        foreach(
$schedule as $rate) {
            
$principal *= $rate;
        }

        return 
$principal;
    }    
//    function FVSCHEDULE()


    /**
     * INTRATE
     *
     * Returns the interest rate for a fully invested security.
     *
     * Excel Function:
     *        INTRATE(settlement,maturity,investment,redemption[,basis])
     *
     * @param    mixed    $settlement    The security's settlement date.
     *                                The security settlement date is the date after the issue date when the security is traded to the buyer.
     * @param    mixed    $maturity    The security's maturity date.
     *                                The maturity date is the date when the security expires.
     * @param    integer    $investment    The amount invested in the security.
     * @param    integer    $redemption    The amount to be received at maturity.
     * @param    integer    $basis        The type of day count to use.
     *                                        0 or omitted    US (NASD) 30/360
     *                                        1                Actual/actual
     *                                        2                Actual/360
     *                                        3                Actual/365
     *                                        4                European 30/360
     * @return    float
     */
    
public static function INTRATE($settlement$maturity$investment$redemption$basis=0) {
        
$settlement    PHPExcel_Calculation_Functions::flattenSingleValue($settlement);
        
$maturity    PHPExcel_Calculation_Functions::flattenSingleValue($maturity);
        
$investment    PHPExcel_Calculation_Functions::flattenSingleValue($investment);
        
$redemption    PHPExcel_Calculation_Functions::flattenSingleValue($redemption);
        
$basis        PHPExcel_Calculation_Functions::flattenSingleValue($basis);

        
//    Validate
        
if ((is_numeric($investment)) && (is_numeric($redemption)) && (is_numeric($basis))) {
            
$investment    = (float) $investment;
            
$redemption    = (float) $redemption;
            
$basis        = (int) $basis;
            if ((
$investment <= 0) || ($redemption <= 0)) {
                return 
PHPExcel_Calculation_Functions::NaN();
            }
            
$daysBetweenSettlementAndMaturity PHPExcel_Calculation_DateTime::YEARFRAC($settlement$maturity$basis);
            if (!
is_numeric($daysBetweenSettlementAndMaturity)) {
                
//    return date error
                
return $daysBetweenSettlementAndMaturity;
            }

            return ((
$redemption $investment) - 1) / ($daysBetweenSettlementAndMaturity);
        }
        return 
PHPExcel_Calculation_Functions::VALUE();
    }    
//    function INTRATE()


    /**
     * IPMT
     *
     * Returns the interest payment for a given period for an investment based on periodic, constant payments and a constant interest rate.
     *
     * Excel Function:
     *        IPMT(rate,per,nper,pv[,fv][,type])
     *
     * @param    float    $rate    Interest rate per period
     * @param    int        $per    Period for which we want to find the interest
     * @param    int        $nper    Number of periods
     * @param    float    $pv        Present Value
     * @param    float    $fv        Future Value
     * @param    int        $type    Payment type: 0 = at the end of each period, 1 = at the beginning of each period
     * @return    float
     */
    
public static function IPMT($rate$per$nper$pv$fv 0$type 0) {
        
$rate    PHPExcel_Calculation_Functions::flattenSingleValue($rate);
        
$per    = (int) PHPExcel_Calculation_Functions::flattenSingleValue($per);
        
$nper    = (int) PHPExcel_Calculation_Functions::flattenSingleValue($nper);
        
$pv        PHPExcel_Calculation_Functions::flattenSingleValue($pv);
        
$fv        PHPExcel_Calculation_Functions::flattenSingleValue($fv);
        
$type    = (int) PHPExcel_Calculation_Functions::flattenSingleValue($type);

        
// Validate parameters
        
if ($type != && $type != 1) {
            return 
PHPExcel_Calculation_Functions::NaN();
        }
        if (
$per <= || $per $nper) {
            return 
PHPExcel_Calculation_Functions::VALUE();
        }

        
// Calculate
        
$interestAndPrincipal self::_interestAndPrincipal($rate$per$nper$pv$fv$type);
        return 
$interestAndPrincipal[0];
    }    
//    function IPMT()

    /**
     * IRR
     *
     * Returns the internal rate of return for a series of cash flows represented by the numbers in values. 
     * These cash flows do not have to be even, as they would be for an annuity. However, the cash flows must occur 
     * at regular intervals, such as monthly or annually. The internal rate of return is the interest rate received
     * for an investment consisting of payments (negative values) and income (positive values) that occur at regular 
     * periods.
     *
     * Excel Function:
     *        IRR(values[,guess])
     *
     * @param    float[]    $values        An array or a reference to cells that contain numbers for which you want
     *                                    to calculate the internal rate of return.
     *                                Values must contain at least one positive value and one negative value to 
     *                                    calculate the internal rate of return.
     * @param    float    $guess        A number that you guess is close to the result of IRR
     * @return    float
     */
    
public static function IRR($values$guess 0.1) {
        if (!
is_array($values)) return PHPExcel_Calculation_Functions::VALUE();
        
$values PHPExcel_Calculation_Functions::flattenArray($values);
        
$guess PHPExcel_Calculation_Functions::flattenSingleValue($guess);

        
// create an initial range, with a root somewhere between 0 and guess
        
$x1 0.0;
        
$x2 $guess;
        
$f1 self::NPV($x1$values);
        
$f2 self::NPV($x2$values);
        for (
$i 0$i FINANCIAL_MAX_ITERATIONS; ++$i) {
            if ((
$f1 $f2) < 0.0) break;
            if (
abs($f1) < abs($f2)) {
                
$f1 self::NPV($x1 += 1.6 * ($x1 $x2), $values);
            } else {
                
$f2 self::NPV($x2 += 1.6 * ($x2 $x1), $values);
            }
        }
        if ((
$f1 $f2) > 0.0) return PHPExcel_Calculation_Functions::VALUE();

        
$f self::NPV($x1$values);
        if (
$f 0.0) {
            
$rtb $x1;
            
$dx $x2 $x1;
        } else {
            
$rtb $x2;
            
$dx $x1 $x2;
        }

        for (
$i 0;  $i FINANCIAL_MAX_ITERATIONS; ++$i) {
            
$dx *= 0.5;
            
$x_mid $rtb $dx;
            
$f_mid self::NPV($x_mid$values);
            if (
$f_mid <= 0.0
                
$rtb $x_mid;
            if ((
abs($f_mid) < FINANCIAL_PRECISION) || (abs($dx) < FINANCIAL_PRECISION)) 
                return 
$x_mid;
        }
        return 
PHPExcel_Calculation_Functions::VALUE();
    }    
//    function IRR()


    /**
     * ISPMT
     *
     * Returns the interest payment for an investment based on an interest rate and a constant payment schedule.
     *
     * Excel Function:
     *     =ISPMT(interest_rate, period, number_payments, PV)
     *
     * interest_rate is the interest rate for the investment
     *
     * period is the period to calculate the interest rate.  It must be betweeen 1 and number_payments.
     *
     * number_payments is the number of payments for the annuity
     *
     * PV is the loan amount or present value of the payments
     */
    
public static function ISPMT() {
        
// Return value
        
$returnValue 0;

        
// Get the parameters
        
$aArgs PHPExcel_Calculation_Functions::flattenArray(func_get_args());
        
$interestRate array_shift($aArgs);
        
$period array_shift($aArgs);
        
$numberPeriods array_shift($aArgs);
        
$principleRemaining array_shift($aArgs);

        
// Calculate
        
$principlePayment = ($principleRemaining 1.0) / ($numberPeriods 1.0);
        for(
$i=0$i <= $period; ++$i) {
            
$returnValue $interestRate $principleRemaining * -1;
            
$principleRemaining -= $principlePayment;
            
// principle needs to be 0 after the last payment, don't let floating point screw it up
            
if($i == $numberPeriods) {
                
$returnValue 0;
            }
        }
        return(
$returnValue);
    }    
//    function ISPMT()


    /**
     * MIRR
     *
     * Returns the modified internal rate of return for a series of periodic cash flows. MIRR considers both 
     *        the cost of the investment and the interest received on reinvestment of cash.
     *
     * Excel Function:
     *        MIRR(values,finance_rate, reinvestment_rate)
     *
     * @param    float[]    $values                An array or a reference to cells that contain a series of payments and
     *                                            income occurring at regular intervals.
     *                                        Payments are negative value, income is positive values.
     * @param    float    $finance_rate        The interest rate you pay on the money used in the cash flows
     * @param    float    $reinvestment_rate    The interest rate you receive on the cash flows as you reinvest them
     * @return    float
     */
    
public static function MIRR($values$finance_rate$reinvestment_rate) {
        if (!
is_array($values)) return PHPExcel_Calculation_Functions::VALUE();
        
$values                PHPExcel_Calculation_Functions::flattenArray($values);
        
$finance_rate        PHPExcel_Calculation_Functions::flattenSingleValue($finance_rate);
        
$reinvestment_rate    PHPExcel_Calculation_Functions::flattenSingleValue($reinvestment_rate);
        
$n count($values);

        
$rr 1.0 $reinvestment_rate;
        
$fr 1.0 $finance_rate;

        
$npv_pos $npv_neg 0.0;
        foreach(
$values as $i => $v) {
            if (
$v >= 0) {
                
$npv_pos += $v pow($rr$i);
            } else {
                
$npv_neg += $v pow($fr$i);
            }
        }

        if ((
$npv_neg == 0) || ($npv_pos == 0) || ($reinvestment_rate <= -1)) {
            return 
PHPExcel_Calculation_Functions::VALUE();
        }

        
$mirr pow((-$npv_pos pow($rr$n))
                / (
$npv_neg * ($rr)), (1.0 / ($n 1))) - 1.0;

        return (
is_finite($mirr) ? $mirr PHPExcel_Calculation_Functions::VALUE());
    }    
//    function MIRR()


    /**
     * NOMINAL
     *
     * Returns the nominal interest rate given the effective rate and the number of compounding payments per year.
     *
     * @param    float    $effect_rate    Effective interest rate
     * @param    int        $npery            Number of compounding payments per year
     * @return    float
     */
    
public static function NOMINAL($effect_rate 0$npery 0) {
        
$effect_rate    PHPExcel_Calculation_Functions::flattenSingleValue($effect_rate);
        
$npery            = (int)PHPExcel_Calculation_Functions::flattenSingleValue($npery);

        
// Validate parameters
        
if ($effect_rate <= || $npery 1) {
            return 
PHPExcel_Calculation_Functions::NaN();
        }

        
// Calculate
        
return $npery * (pow($effect_rate 1$npery) - 1);
    }    
//    function NOMINAL()


    /**
     * NPER
     *
     * Returns the number of periods for a cash flow with constant periodic payments (annuities), and interest rate.
     *
     * @param    float    $rate    Interest rate per period
     * @param    int        $pmt    Periodic payment (annuity)
     * @param    float    $pv        Present Value
     * @param    float    $fv        Future Value
     * @param    int        $type    Payment type: 0 = at the end of each period, 1 = at the beginning of each period
     * @return    float
     */
    
public static function NPER($rate 0$pmt 0$pv 0$fv 0$type 0) {
        
$rate    PHPExcel_Calculation_Functions::flattenSingleValue($rate);
        
$pmt    PHPExcel_Calculation_Functions::flattenSingleValue($pmt);
        
$pv        PHPExcel_Calculation_Functions::flattenSingleValue($pv);
        
$fv        PHPExcel_Calculation_Functions::flattenSingleValue($fv);
        
$type    PHPExcel_Calculation_Functions::flattenSingleValue($type);

        
// Validate parameters
        
if ($type != && $type != 1) {
            return 
PHPExcel_Calculation_Functions::NaN();
        }

        
// Calculate
        
if (!is_null($rate) && $rate != 0) {
            if (
$pmt == && $pv == 0) {
                return 
PHPExcel_Calculation_Functions::NaN();
            }
            return 
log(($pmt * ($rate $type) / $rate $fv) / ($pv $pmt * ($rate $type) / $rate)) / log($rate);
        } else {
            if (
$pmt == 0) {
                return 
PHPExcel_Calculation_Functions::NaN();
            }
            return (-
$pv -$fv) / $pmt;
        }
    }    
//    function NPER()

    /**
     * NPV
     *
     * Returns the Net Present Value of a cash flow series given a discount rate.
     *
     * @return    float
     */
    
public static function NPV() {
        
// Return value
        
$returnValue 0;

        
// Loop through arguments
        
$aArgs PHPExcel_Calculation_Functions::flattenArray(func_get_args());

        
// Calculate
        
$rate array_shift($aArgs);
        for (
$i 1$i <= count($aArgs); ++$i) {
            
// Is it a numeric value?
            
if (is_numeric($aArgs[$i 1])) {
                
$returnValue += $aArgs[$i 1] / pow($rate$i);
            }
        }

        
// Return
        
return $returnValue;
    }    
//    function NPV()

    /**
     * PMT
     *
     * Returns the constant payment (annuity) for a cash flow with a constant interest rate.
     *
     * @param    float    $rate    Interest rate per period
     * @param    int        $nper    Number of periods
     * @param    float    $pv        Present Value
     * @param    float    $fv        Future Value
     * @param    int        $type    Payment type: 0 = at the end of each period, 1 = at the beginning of each period
     * @return    float
     */
    
public static function PMT($rate 0$nper 0$pv 0$fv 0$type 0) {
        
$rate    PHPExcel_Calculation_Functions::flattenSingleValue($rate);
        
$nper    PHPExcel_Calculation_Functions::flattenSingleValue($nper);
        
$pv        PHPExcel_Calculation_Functions::flattenSingleValue($pv);
        
$fv        PHPExcel_Calculation_Functions::flattenSingleValue($fv);
        
$type    PHPExcel_Calculation_Functions::flattenSingleValue($type);

        
// Validate parameters
        
if ($type != && $type != 1) {
            return 
PHPExcel_Calculation_Functions::NaN();
        }

        
// Calculate
        
if (!is_null($rate) && $rate != 0) {
            return (-
$fv $pv pow($rate$nper)) / ($rate $type) / ((pow($rate$nper) - 1) / $rate);
        } else {
            return (-
$pv $fv) / $nper;
        }
    }    
//    function PMT()


    /**
     * PPMT
     *
     * Returns the interest payment for a given period for an investment based on periodic, constant payments and a constant interest rate.
     *
     * @param    float    $rate    Interest rate per period
     * @param    int        $per    Period for which we want to find the interest
     * @param    int        $nper    Number of periods
     * @param    float    $pv        Present Value
     * @param    float    $fv        Future Value
     * @param    int        $type    Payment type: 0 = at the end of each period, 1 = at the beginning of each period
     * @return    float
     */
    
public static function PPMT($rate$per$nper$pv$fv 0$type 0) {
        
$rate    PHPExcel_Calculation_Functions::flattenSingleValue($rate);
        
$per    = (int) PHPExcel_Calculation_Functions::flattenSingleValue($per);
        
$nper    = (int) PHPExcel_Calculation_Functions::flattenSingleValue($nper);
        
$pv        PHPExcel_Calculation_Functions::flattenSingleValue($pv);
        
$fv        PHPExcel_Calculation_Functions::flattenSingleValue($fv);
        
$type    = (int) PHPExcel_Calculation_Functions::flattenSingleValue($type);

        
// Validate parameters
        
if ($type != && $type != 1) {
            return 
PHPExcel_Calculation_Functions::NaN();
        }
        if (
$per <= || $per $nper) {
            return 
PHPExcel_Calculation_Functions::VALUE();
        }

        
// Calculate
        
$interestAndPrincipal self::_interestAndPrincipal($rate$per$nper$pv$fv$type);
        return 
$interestAndPrincipal[1];
    }    
//    function PPMT()


    
public static function PRICE($settlement$maturity$rate$yield$redemption$frequency$basis=0) {
        
$settlement    PHPExcel_Calculation_Functions::flattenSingleValue($settlement);
        
$maturity    PHPExcel_Calculation_Functions::flattenSingleValue($maturity);
        
$rate        = (float) PHPExcel_Calculation_Functions::flattenSingleValue($rate);
        
$yield        = (float) PHPExcel_Calculation_Functions::flattenSingleValue($yield);
        
$redemption    = (float) PHPExcel_Calculation_Functions::flattenSingleValue($redemption);
        
$frequency    = (int) PHPExcel_Calculation_Functions::flattenSingleValue($frequency);
        
$basis        = (is_null($basis))    ? :    (int) PHPExcel_Calculation_Functions::flattenSingleValue($basis);

        if (
is_string($settlement PHPExcel_Calculation_DateTime::_getDateValue($settlement))) {
            return 
PHPExcel_Calculation_Functions::VALUE();
        }
        if (
is_string($maturity PHPExcel_Calculation_DateTime::_getDateValue($maturity))) {
            return 
PHPExcel_Calculation_Functions::VALUE();
        }

        if ((
$settlement $maturity) ||
            (!
self::_validFrequency($frequency)) ||
            ((
$basis 0) || ($basis 4))) {
            return 
PHPExcel_Calculation_Functions::NaN();
        }

        
$dsc self::COUPDAYSNC($settlement$maturity$frequency$basis);
        
$e self::COUPDAYS($settlement$maturity$frequency$basis);
        
$n self::COUPNUM($settlement$maturity$frequency$basis);
        
$a self::COUPDAYBS($settlement$maturity$frequency$basis);

        
$baseYF    1.0 + ($yield $frequency);
        
$rfp    100 * ($rate $frequency);
        
$de    $dsc $e;

        
$result $redemption pow($baseYF, (--$n $de));
        for(
$k 0$k <= $n; ++$k) {
            
$result += $rfp / (pow($baseYF, ($k $de)));
        }
        
$result -= $rfp * ($a $e);

        return 
$result;
    }    
//    function PRICE()


    /**
     * PRICEDISC
     *
     * Returns the price per $100 face value of a discounted security.
     *
     * @param    mixed    settlement    The security's settlement date.
     *                                The security settlement date is the date after the issue date when the security is traded to the buyer.
     * @param    mixed    maturity    The security's maturity date.
     *                                The maturity date is the date when the security expires.
     * @param    int        discount    The security's discount rate.
     * @param    int        redemption    The security's redemption value per $100 face value.
     * @param    int        basis        The type of day count to use.
     *                                        0 or omitted    US (NASD) 30/360
     *                                        1                Actual/actual
     *                                        2                Actual/360
     *                                        3                Actual/365
     *                                        4                European 30/360
     * @return    float
     */
    
public static function PRICEDISC($settlement$maturity$discount$redemption$basis=0) {
        
$settlement    PHPExcel_Calculation_Functions::flattenSingleValue($settlement);
        
$maturity    PHPExcel_Calculation_Functions::flattenSingleValue($maturity);
        
$discount    = (float) PHPExcel_Calculation_Functions::flattenSingleValue($discount);
        
$redemption    = (float) PHPExcel_Calculation_Functions::flattenSingleValue($redemption);
        
$basis        = (int) PHPExcel_Calculation_Functions::flattenSingleValue($basis);

        
//    Validate
        
if ((is_numeric($discount)) && (is_numeric($redemption)) && (is_numeric($basis))) {
            if ((
$discount <= 0) || ($redemption <= 0)) {
                return 
PHPExcel_Calculation_Functions::NaN();
            }
            
$daysBetweenSettlementAndMaturity PHPExcel_Calculation_DateTime::YEARFRAC($settlement$maturity$basis);
            if (!
is_numeric($daysBetweenSettlementAndMaturity)) {
                
//    return date error
                
return $daysBetweenSettlementAndMaturity;
            }

            return 
$redemption * ($discount $daysBetweenSettlementAndMaturity);
        }
        return 
PHPExcel_Calculation_Functions::VALUE();
    }    
//    function PRICEDISC()


    /**
     * PRICEMAT
     *
     * Returns the price per $100 face value of a security that pays interest at maturity.
     *
     * @param    mixed    settlement    The security's settlement date.
     *                                The security's settlement date is the date after the issue date when the security is traded to the buyer.
     * @param    mixed    maturity    The security's maturity date.
     *                                The maturity date is the date when the security expires.
     * @param    mixed    issue        The security's issue date.
     * @param    int        rate        The security's interest rate at date of issue.
     * @param    int        yield        The security's annual yield.
     * @param    int        basis        The type of day count to use.
     *                                        0 or omitted    US (NASD) 30/360
     *                                        1                Actual/actual
     *                                        2                Actual/360
     *                                        3                Actual/365
     *                                        4                European 30/360
     * @return    float
     */
    
public static function PRICEMAT($settlement$maturity$issue$rate$yield$basis=0) {
        
$settlement    PHPExcel_Calculation_Functions::flattenSingleValue($settlement);
        
$maturity    PHPExcel_Calculation_Functions::flattenSingleValue($maturity);
        
$issue        PHPExcel_Calculation_Functions::flattenSingleValue($issue);
        
$rate        PHPExcel_Calculation_Functions::flattenSingleValue($rate);
        
$yield        PHPExcel_Calculation_Functions::flattenSingleValue($yield);
        
$basis        = (int) PHPExcel_Calculation_Functions::flattenSingleValue($basis);

        
//    Validate
        
if (is_numeric($rate) && is_numeric($yield)) {
            if ((
$rate <= 0) || ($yield <= 0)) {
                return 
PHPExcel_Calculation_Functions::NaN();
            }
            
$daysPerYear self::_daysPerYear(PHPExcel_Calculation_DateTime::YEAR($settlement),$basis);
            if (!
is_numeric($daysPerYear)) {
                return 
$daysPerYear;
            }
            
$daysBetweenIssueAndSettlement PHPExcel_Calculation_DateTime::YEARFRAC($issue$settlement$basis);
            if (!
is_numeric($daysBetweenIssueAndSettlement)) {
                
//    return date error
                
return $daysBetweenIssueAndSettlement;
            }
            
$daysBetweenIssueAndSettlement *= $daysPerYear;
            
$daysBetweenIssueAndMaturity PHPExcel_Calculation_DateTime::YEARFRAC($issue$maturity$basis);
            if (!
is_numeric($daysBetweenIssueAndMaturity)) {
                
//    return date error
                
return $daysBetweenIssueAndMaturity;
            }
            
$daysBetweenIssueAndMaturity *= $daysPerYear;
            
$daysBetweenSettlementAndMaturity PHPExcel_Calculation_DateTime::YEARFRAC($settlement$maturity$basis);
            if (!
is_numeric($daysBetweenSettlementAndMaturity)) {
                
//    return date error
                
return $daysBetweenSettlementAndMaturity;
            }
            
$daysBetweenSettlementAndMaturity *= $daysPerYear;

            return ((
100 + (($daysBetweenIssueAndMaturity $daysPerYear) * $rate 100)) /
                   (
+ (($daysBetweenSettlementAndMaturity $daysPerYear) * $yield)) -
                   ((
$daysBetweenIssueAndSettlement $daysPerYear) * $rate 100));
        }
        return 
PHPExcel_Calculation_Functions::VALUE();
    }    
//    function PRICEMAT()


    /**
     * PV
     *
     * Returns the Present Value of a cash flow with constant payments and interest rate (annuities).
     *
     * @param    float    $rate    Interest rate per period
     * @param    int        $nper    Number of periods
     * @param    float    $pmt    Periodic payment (annuity)
     * @param    float    $fv        Future Value
     * @param    int        $type    Payment type: 0 = at the end of each period, 1 = at the beginning of each period
     * @return    float
     */
    
public static function PV($rate 0$nper 0$pmt 0$fv 0$type 0) {
        
$rate    PHPExcel_Calculation_Functions::flattenSingleValue($rate);
        
$nper    PHPExcel_Calculation_Functions::flattenSingleValue($nper);
        
$pmt    PHPExcel_Calculation_Functions::flattenSingleValue($pmt);
        
$fv        PHPExcel_Calculation_Functions::flattenSingleValue($fv);
        
$type    PHPExcel_Calculation_Functions::flattenSingleValue($type);

        
// Validate parameters
        
if ($type != && $type != 1) {
            return 
PHPExcel_Calculation_Functions::NaN();
        }

        
// Calculate
        
if (!is_null($rate) && $rate != 0) {
            return (-
$pmt * ($rate $type) * ((pow($rate$nper) - 1) / $rate) - $fv) / pow($rate$nper);
        } else {
            return -
$fv $pmt $nper;
        }
    }    
//    function PV()


    /**
     * RATE
     *
     * Returns the interest rate per period of an annuity.
     * RATE is calculated by iteration and can have zero or more solutions.
     * If the successive results of RATE do not converge to within 0.0000001 after 20 iterations,
     * RATE returns the #NUM! error value.
     *
     * Excel Function:
     *        RATE(nper,pmt,pv[,fv[,type[,guess]]])
     *
     * @access    public
     * @category Financial Functions
     * @param    float    nper        The total number of payment periods in an annuity.
     * @param    float    pmt            The payment made each period and cannot change over the life
     *                                    of the annuity.
     *                                Typically, pmt includes principal and interest but no other
     *                                    fees or taxes.
     * @param    float    pv            The present value - the total amount that a series of future
     *                                    payments is worth now.
     * @param    float    fv            The future value, or a cash balance you want to attain after
     *                                    the last payment is made. If fv is omitted, it is assumed
     *                                    to be 0 (the future value of a loan, for example, is 0).
     * @param    integer    type        A number 0 or 1 and indicates when payments are due:
     *                                        0 or omitted    At the end of the period.
     *                                        1                At the beginning of the period.
     * @param    float    guess        Your guess for what the rate will be.
     *                                    If you omit guess, it is assumed to be 10 percent.
     * @return    float
     **/
    
public static function RATE($nper$pmt$pv$fv 0.0$type 0$guess 0.1) {
        
$nper    = (int) PHPExcel_Calculation_Functions::flattenSingleValue($nper);
        
$pmt    PHPExcel_Calculation_Functions::flattenSingleValue($pmt);
        
$pv        PHPExcel_Calculation_Functions::flattenSingleValue($pv);
        
$fv        = (is_null($fv))    ? 0.0    :    PHPExcel_Calculation_Functions::flattenSingleValue($fv);
        
$type    = (is_null($type))    ? 0        :    (int) PHPExcel_Calculation_Functions::flattenSingleValue($type);
        
$guess    = (is_null($guess))    ? 0.1    :    PHPExcel_Calculation_Functions::flattenSingleValue($guess);

        
$rate $guess;
        if (
abs($rate) < FINANCIAL_PRECISION) {
            
$y $pv * ($nper $rate) + $pmt * ($rate $type) * $nper $fv;
        } else {
            
$f exp($nper log($rate));
            
$y $pv $f $pmt * ($rate $type) * ($f 1) + $fv;
        }
        
$y0 $pv $pmt $nper $fv;
        
$y1 $pv $f $pmt * ($rate $type) * ($f 1) + $fv;

        
// find root by secant method
        
$i  $x0 0.0;
        
$x1 $rate;
        while ((
abs($y0 $y1) > FINANCIAL_PRECISION) && ($i FINANCIAL_MAX_ITERATIONS)) {
            
$rate = ($y1 $x0 $y0 $x1) / ($y1 $y0);
            
$x0 $x1;
            
$x1 $rate;
            if ((
$nper abs($pmt)) > ($pv $fv))
                
$x1 abs($x1);

            if (
abs($rate) < FINANCIAL_PRECISION) {
                
$y $pv * ($nper $rate) + $pmt * ($rate $type) * $nper $fv;
            } else {
                
$f exp($nper log($rate));
                
$y $pv $f $pmt * ($rate $type) * ($f 1) + $fv;
            }

            
$y0 $y1;
            
$y1 $y;
            ++
$i;
        }
        return 
$rate;
    }    
//    function RATE()


    /**
     * RECEIVED
     *
     * Returns the price per $100 face value of a discounted security.
     *
     * @param    mixed    settlement    The security's settlement date.
     *                                The security settlement date is the date after the issue date when the security is traded to the buyer.
     * @param    mixed    maturity    The security's maturity date.
     *                                The maturity date is the date when the security expires.
     * @param    int        investment    The amount invested in the security.
     * @param    int        discount    The security's discount rate.
     * @param    int        basis        The type of day count to use.
     *                                        0 or omitted    US (NASD) 30/360
     *                                        1                Actual/actual
     *                                        2                Actual/360
     *                                        3                Actual/365
     *                                        4                European 30/360
     * @return    float
     */
    
public static function RECEIVED($settlement$maturity$investment$discount$basis=0) {
        
$settlement    PHPExcel_Calculation_Functions::flattenSingleValue($settlement);
        
$maturity    PHPExcel_Calculation_Functions::flattenSingleValue($maturity);
        
$investment    = (float) PHPExcel_Calculation_Functions::flattenSingleValue($investment);
        
$discount    = (float) PHPExcel_Calculation_Functions::flattenSingleValue($discount);
        
$basis        = (int) PHPExcel_Calculation_Functions::flattenSingleValue($basis);

        
//    Validate
        
if ((is_numeric($investment)) && (is_numeric($discount)) && (is_numeric($basis))) {
            if ((
$investment <= 0) || ($discount <= 0)) {
                return 
PHPExcel_Calculation_Functions::NaN();
            }
            
$daysBetweenSettlementAndMaturity PHPExcel_Calculation_DateTime::YEARFRAC($settlement$maturity$basis);
            if (!
is_numeric($daysBetweenSettlementAndMaturity)) {
                
//    return date error
                
return $daysBetweenSettlementAndMaturity;
            }

            return 
$investment / ( - ($discount $daysBetweenSettlementAndMaturity));
        }
        return 
PHPExcel_Calculation_Functions::VALUE();
    }    
//    function RECEIVED()


    /**
     * SLN
     *
     * Returns the straight-line depreciation of an asset for one period
     *
     * @param    cost        Initial cost of the asset
     * @param    salvage        Value at the end of the depreciation
     * @param    life        Number of periods over which the asset is depreciated
     * @return    float
     */
    
public static function SLN($cost$salvage$life) {
        
$cost        PHPExcel_Calculation_Functions::flattenSingleValue($cost);
        
$salvage    PHPExcel_Calculation_Functions::flattenSingleValue($salvage);
        
$life        PHPExcel_Calculation_Functions::flattenSingleValue($life);

        
// Calculate
        
if ((is_numeric($cost)) && (is_numeric($salvage)) && (is_numeric($life))) {
            if (
$life 0) {
                return 
PHPExcel_Calculation_Functions::NaN();
            }
            return (
$cost $salvage) / $life;
        }
        return 
PHPExcel_Calculation_Functions::VALUE();
    }    
//    function SLN()


    /**
     * SYD
     *
     * Returns the sum-of-years' digits depreciation of an asset for a specified period.
     *
     * @param    cost        Initial cost of the asset
     * @param    salvage        Value at the end of the depreciation
     * @param    life        Number of periods over which the asset is depreciated
     * @param    period        Period
     * @return    float
     */
    
public static function SYD($cost$salvage$life$period) {
        
$cost        PHPExcel_Calculation_Functions::flattenSingleValue($cost);
        
$salvage    PHPExcel_Calculation_Functions::flattenSingleValue($salvage);
        
$life        PHPExcel_Calculation_Functions::flattenSingleValue($life);
        
$period        PHPExcel_Calculation_Functions::flattenSingleValue($period);

        
// Calculate
        
if ((is_numeric($cost)) && (is_numeric($salvage)) && (is_numeric($life)) && (is_numeric($period))) {
            if ((
$life 1) || ($period $life)) {
                return 
PHPExcel_Calculation_Functions::NaN();
            }
            return ((
$cost $salvage) * ($life $period 1) * 2) / ($life * ($life 1));
        }
        return 
PHPExcel_Calculation_Functions::VALUE();
    }    
//    function SYD()


    /**
     * TBILLEQ
     *
     * Returns the bond-equivalent yield for a Treasury bill.
     *
     * @param    mixed    settlement    The Treasury bill's settlement date.
     *                                The Treasury bill's settlement date is the date after the issue date when the Treasury bill is traded to the buyer.
     * @param    mixed    maturity    The Treasury bill's maturity date.
     *                                The maturity date is the date when the Treasury bill expires.
     * @param    int        discount    The Treasury bill's discount rate.
     * @return    float
     */
    
public static function TBILLEQ($settlement$maturity$discount) {
        
$settlement    PHPExcel_Calculation_Functions::flattenSingleValue($settlement);
        
$maturity    PHPExcel_Calculation_Functions::flattenSingleValue($maturity);
        
$discount    PHPExcel_Calculation_Functions::flattenSingleValue($discount);

        
//    Use TBILLPRICE for validation
        
$testValue self::TBILLPRICE($settlement$maturity$discount);
        if (
is_string($testValue)) {
            return 
$testValue;
        }

        if (
is_string($maturity PHPExcel_Calculation_DateTime::_getDateValue($maturity))) {
            return 
PHPExcel_Calculation_Functions::VALUE();
        }

        if (
PHPExcel_Calculation_Functions::getCompatibilityMode() == PHPExcel_Calculation_Functions::COMPATIBILITY_OPENOFFICE) {
            ++
$maturity;
            
$daysBetweenSettlementAndMaturity PHPExcel_Calculation_DateTime::YEARFRAC($settlement$maturity) * 360;
        } else {
            
$daysBetweenSettlementAndMaturity = (PHPExcel_Calculation_DateTime::_getDateValue($maturity) - PHPExcel_Calculation_DateTime::_getDateValue($settlement));
        }

        return (
365 $discount) / (360 $discount $daysBetweenSettlementAndMaturity);
    }    
//    function TBILLEQ()


    /**
     * TBILLPRICE
     *
     * Returns the yield for a Treasury bill.
     *
     * @param    mixed    settlement    The Treasury bill's settlement date.
     *                                The Treasury bill's settlement date is the date after the issue date when the Treasury bill is traded to the buyer.
     * @param    mixed    maturity    The Treasury bill's maturity date.
     *                                The maturity date is the date when the Treasury bill expires.
     * @param    int        discount    The Treasury bill's discount rate.
     * @return    float
     */
    
public static function TBILLPRICE($settlement$maturity$discount) {
        
$settlement    PHPExcel_Calculation_Functions::flattenSingleValue($settlement);
        
$maturity    PHPExcel_Calculation_Functions::flattenSingleValue($maturity);
        
$discount    PHPExcel_Calculation_Functions::flattenSingleValue($discount);

        if (
is_string($maturity PHPExcel_Calculation_DateTime::_getDateValue($maturity))) {
            return 
PHPExcel_Calculation_Functions::VALUE();
        }

        
//    Validate
        
if (is_numeric($discount)) {
            if (
$discount <= 0) {
                return 
PHPExcel_Calculation_Functions::NaN();
            }

            if (
PHPExcel_Calculation_Functions::getCompatibilityMode() == PHPExcel_Calculation_Functions::COMPATIBILITY_OPENOFFICE) {
                ++
$maturity;
                
$daysBetweenSettlementAndMaturity PHPExcel_Calculation_DateTime::YEARFRAC($settlement$maturity) * 360;
                if (!
is_numeric($daysBetweenSettlementAndMaturity)) {
                    
//    return date error
                    
return $daysBetweenSettlementAndMaturity;
                }
            } else {
                
$daysBetweenSettlementAndMaturity = (PHPExcel_Calculation_DateTime::_getDateValue($maturity) - PHPExcel_Calculation_DateTime::_getDateValue($settlement));
            }

            if (
$daysBetweenSettlementAndMaturity 360) {
                return 
PHPExcel_Calculation_Functions::NaN();
            }

            
$price 100 * (- (($discount $daysBetweenSettlementAndMaturity) / 360));
            if (
$price <= 0) {
                return 
PHPExcel_Calculation_Functions::NaN();
            }
            return 
$price;
        }
        return 
PHPExcel_Calculation_Functions::VALUE();
    }    
//    function TBILLPRICE()


    /**
     * TBILLYIELD
     *
     * Returns the yield for a Treasury bill.
     *
     * @param    mixed    settlement    The Treasury bill's settlement date.
     *                                The Treasury bill's settlement date is the date after the issue date when the Treasury bill is traded to the buyer.
     * @param    mixed    maturity    The Treasury bill's maturity date.
     *                                The maturity date is the date when the Treasury bill expires.
     * @param    int        price        The Treasury bill's price per $100 face value.
     * @return    float
     */
    
public static function TBILLYIELD($settlement$maturity$price) {
        
$settlement    PHPExcel_Calculation_Functions::flattenSingleValue($settlement);
        
$maturity    PHPExcel_Calculation_Functions::flattenSingleValue($maturity);
        
$price        PHPExcel_Calculation_Functions::flattenSingleValue($price);

        
//    Validate
        
if (is_numeric($price)) {
            if (
$price <= 0) {
                return 
PHPExcel_Calculation_Functions::NaN();
            }

            if (
PHPExcel_Calculation_Functions::getCompatibilityMode() == PHPExcel_Calculation_Functions::COMPATIBILITY_OPENOFFICE) {
                ++
$maturity;
                
$daysBetweenSettlementAndMaturity PHPExcel_Calculation_DateTime::YEARFRAC($settlement$maturity) * 360;
                if (!
is_numeric($daysBetweenSettlementAndMaturity)) {
                    
//    return date error
                    
return $daysBetweenSettlementAndMaturity;
                }
            } else {
                
$daysBetweenSettlementAndMaturity = (PHPExcel_Calculation_DateTime::_getDateValue($maturity) - PHPExcel_Calculation_DateTime::_getDateValue($settlement));
            }

            if (
$daysBetweenSettlementAndMaturity 360) {
                return 
PHPExcel_Calculation_Functions::NaN();
            }

            return ((
100 $price) / $price) * (360 $daysBetweenSettlementAndMaturity);
        }
        return 
PHPExcel_Calculation_Functions::VALUE();
    }    
//    function TBILLYIELD()


    
public static function XIRR($values$dates$guess 0.1) {
        if ((!
is_array($values)) && (!is_array($dates))) return PHPExcel_Calculation_Functions::VALUE();
        
$values    PHPExcel_Calculation_Functions::flattenArray($values);
        
$dates    PHPExcel_Calculation_Functions::flattenArray($dates);
        
$guess PHPExcel_Calculation_Functions::flattenSingleValue($guess);
        if (
count($values) != count($dates)) return PHPExcel_Calculation_Functions::NaN();

        
// create an initial range, with a root somewhere between 0 and guess
        
$x1 0.0;
        
$x2 $guess;
        
$f1 self::XNPV($x1$values$dates);
        
$f2 self::XNPV($x2$values$dates);
        for (
$i 0$i FINANCIAL_MAX_ITERATIONS; ++$i) {
            if ((
$f1 $f2) < 0.0) break;
            if (
abs($f1) < abs($f2)) {
                
$f1 self::XNPV($x1 += 1.6 * ($x1 $x2), $values$dates);
            } else {
                
$f2 self::XNPV($x2 += 1.6 * ($x2 $x1), $values$dates);
            }
        }
        if ((
$f1 $f2) > 0.0) return PHPExcel_Calculation_Functions::VALUE();

        
$f self::XNPV($x1$values$dates);
        if (
$f 0.0) {
            
$rtb $x1;
            
$dx $x2 $x1;
        } else {
            
$rtb $x2;
            
$dx $x1 $x2;
        }

        for (
$i 0;  $i FINANCIAL_MAX_ITERATIONS; ++$i) {
            
$dx *= 0.5;
            
$x_mid $rtb $dx;
            
$f_mid self::XNPV($x_mid$values$dates);
            if (
$f_mid <= 0.0$rtb $x_mid;
            if ((
abs($f_mid) < FINANCIAL_PRECISION) || (abs($dx) < FINANCIAL_PRECISION)) return $x_mid;
        }
        return 
PHPExcel_Calculation_Functions::VALUE();
    }


    
/**
     * XNPV
     *
     * Returns the net present value for a schedule of cash flows that is not necessarily periodic.
     * To calculate the net present value for a series of cash flows that is periodic, use the NPV function.
     *
     * Excel Function:
     *        =XNPV(rate,values,dates)
     *
     * @param    float            $rate        The discount rate to apply to the cash flows.
     * @param    array of float    $values        A series of cash flows that corresponds to a schedule of payments in dates. The first payment is optional and corresponds to a cost or payment that occurs at the beginning of the investment. 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. The series of values must contain at least one positive value and one negative value.
     * @param    array of mixed    $dates        A schedule of payment dates that corresponds to the cash flow payments. The first payment date indicates the beginning of the schedule of payments. All other dates must be later than this date, but they may occur in any order.
     * @return    float
     */
    
public static function XNPV($rate$values$dates) {
        
$rate PHPExcel_Calculation_Functions::flattenSingleValue($rate);
        if (!
is_numeric($rate)) return PHPExcel_Calculation_Functions::VALUE();
        if ((!
is_array($values)) || (!is_array($dates))) return PHPExcel_Calculation_Functions::VALUE();
        
$values    PHPExcel_Calculation_Functions::flattenArray($values);
        
$dates    PHPExcel_Calculation_Functions::flattenArray($dates);
        
$valCount count($values);
        if (
$valCount != count($dates)) return PHPExcel_Calculation_Functions::NaN();
        if ((
min($values) > 0) || (max($values) < 0)) return PHPExcel_Calculation_Functions::VALUE();

        
$xnpv 0.0;
        for (
$i 0$i $valCount; ++$i) {
            if (!
is_numeric($values[$i])) return PHPExcel_Calculation_Functions::VALUE();
            
$xnpv += $values[$i] / pow($ratePHPExcel_Calculation_DateTime::DATEDIF($dates[0],$dates[$i],'d') / 365);
        }
        return (
is_finite($xnpv)) ? $xnpv PHPExcel_Calculation_Functions::VALUE();
    }    
//    function XNPV()


    /**
     * YIELDDISC
     *
     * Returns the annual yield of a security that pays interest at maturity.
     *
     * @param    mixed    settlement    The security's settlement date.
     *                                The security's settlement date is the date after the issue date when the security is traded to the buyer.
     * @param    mixed    maturity    The security's maturity date.
     *                                The maturity date is the date when the security expires.
     * @param    int        price        The security's price per $100 face value.
     * @param    int        redemption    The security's redemption value per $100 face value.
     * @param    int        basis        The type of day count to use.
     *                                        0 or omitted    US (NASD) 30/360
     *                                        1                Actual/actual
     *                                        2                Actual/360
     *                                        3                Actual/365
     *                                        4                European 30/360
     * @return    float
     */
    
public static function YIELDDISC($settlement$maturity$price$redemption$basis=0) {
        
$settlement    PHPExcel_Calculation_Functions::flattenSingleValue($settlement);
        
$maturity    PHPExcel_Calculation_Functions::flattenSingleValue($maturity);
        
$price        PHPExcel_Calculation_Functions::flattenSingleValue($price);
        
$redemption    PHPExcel_Calculation_Functions::flattenSingleValue($redemption);
        
$basis        = (int) PHPExcel_Calculation_Functions::flattenSingleValue($basis);

        
//    Validate
        
if (is_numeric($price) && is_numeric($redemption)) {
            if ((
$price <= 0) || ($redemption <= 0)) {
                return 
PHPExcel_Calculation_Functions::NaN();
            }
            
$daysPerYear self::_daysPerYear(PHPExcel_Calculation_DateTime::YEAR($settlement),$basis);
            if (!
is_numeric($daysPerYear)) {
                return 
$daysPerYear;
            }
            
$daysBetweenSettlementAndMaturity PHPExcel_Calculation_DateTime::YEARFRAC($settlement$maturity,$basis);
            if (!
is_numeric($daysBetweenSettlementAndMaturity)) {
                
//    return date error
                
return $daysBetweenSettlementAndMaturity;
            }
            
$daysBetweenSettlementAndMaturity *= $daysPerYear;

            return ((
$redemption $price) / $price) * ($daysPerYear $daysBetweenSettlementAndMaturity);
        }
        return 
PHPExcel_Calculation_Functions::VALUE();
    }    
//    function YIELDDISC()


    /**
     * YIELDMAT
     *
     * Returns the annual yield of a security that pays interest at maturity.
     *
     * @param    mixed    settlement    The security's settlement date.
     *                                The security's settlement date is the date after the issue date when the security is traded to the buyer.
     * @param    mixed    maturity    The security's maturity date.
     *                                The maturity date is the date when the security expires.
     * @param    mixed    issue        The security's issue date.
     * @param    int        rate        The security's interest rate at date of issue.
     * @param    int        price        The security's price per $100 face value.
     * @param    int        basis        The type of day count to use.
     *                                        0 or omitted    US (NASD) 30/360
     *                                        1                Actual/actual
     *                                        2                Actual/360
     *                                        3                Actual/365
     *                                        4                European 30/360
     * @return    float
     */
    
public static function YIELDMAT($settlement$maturity$issue$rate$price$basis=0) {
        
$settlement    PHPExcel_Calculation_Functions::flattenSingleValue($settlement);
        
$maturity    PHPExcel_Calculation_Functions::flattenSingleValue($maturity);
        
$issue        PHPExcel_Calculation_Functions::flattenSingleValue($issue);
        
$rate        PHPExcel_Calculation_Functions::flattenSingleValue($rate);
        
$price        PHPExcel_Calculation_Functions::flattenSingleValue($price);
        
$basis        = (int) PHPExcel_Calculation_Functions::flattenSingleValue($basis);

        
//    Validate
        
if (is_numeric($rate) && is_numeric($price)) {
            if ((
$rate <= 0) || ($price <= 0)) {
                return 
PHPExcel_Calculation_Functions::NaN();
            }
            
$daysPerYear self::_daysPerYear(PHPExcel_Calculation_DateTime::YEAR($settlement),$basis);
            if (!
is_numeric($daysPerYear)) {
                return 
$daysPerYear;
            }
            
$daysBetweenIssueAndSettlement PHPExcel_Calculation_DateTime::YEARFRAC($issue$settlement$basis);
            if (!
is_numeric($daysBetweenIssueAndSettlement)) {
                
//    return date error
                
return $daysBetweenIssueAndSettlement;
            }
            
$daysBetweenIssueAndSettlement *= $daysPerYear;
            
$daysBetweenIssueAndMaturity PHPExcel_Calculation_DateTime::YEARFRAC($issue$maturity$basis);
            if (!
is_numeric($daysBetweenIssueAndMaturity)) {
                
//    return date error
                
return $daysBetweenIssueAndMaturity;
            }
            
$daysBetweenIssueAndMaturity *= $daysPerYear;
            
$daysBetweenSettlementAndMaturity PHPExcel_Calculation_DateTime::YEARFRAC($settlement$maturity$basis);
            if (!
is_numeric($daysBetweenSettlementAndMaturity)) {
                
//    return date error
                
return $daysBetweenSettlementAndMaturity;
            }
            
$daysBetweenSettlementAndMaturity *= $daysPerYear;

            return ((
+ (($daysBetweenIssueAndMaturity $daysPerYear) * $rate) - (($price 100) + (($daysBetweenIssueAndSettlement $daysPerYear) * $rate))) /
                   ((
$price 100) + (($daysBetweenIssueAndSettlement $daysPerYear) * $rate))) *
                   (
$daysPerYear $daysBetweenSettlementAndMaturity);
        }
        return 
PHPExcel_Calculation_Functions::VALUE();
    }    
//    function YIELDMAT()

}    //    class PHPExcel_Calculation_Financial