diff --git a/app/App.php b/app/App.php index 2394056..d190d4f 100644 --- a/app/App.php +++ b/app/App.php @@ -3,3 +3,54 @@ declare(strict_types = 1); // Your Code + +function getFile() { + $fileOpen = fopen("../transaction_files/sample_1.csv", "r"); + + while (($data = fgetcsv($fileOpen, 1000, ",")) !== FALSE) { + $array[] = $data; + }; + + fclose($fileOpen); + + return $array; +} + +function getTotals() { + $data = getFile(); + $dataLen = count($data); + $incomeSum = 0; + $expenseSum = 0; + + for ($i=1; $i<$dataLen; $i++) { + $lastInd = count($data[$i]) - 1; + $arrVal = $data[$i][$lastInd]; + if (!str_starts_with($arrVal, "-")) { // check if positive value + $incomeVal = trim_data($arrVal); + $incomeSum += $incomeVal; + } else { + $expenseVal = trim_data($arrVal); + $expenseSum += $expenseVal; + } + } + $netTotal = $incomeSum + $expenseSum; + + $incomeSum = number_format($incomeSum, 2); // round up to 2 d.p + + $expenseSum = number_format($expenseSum, 2); + $expenseSum = str_replace('-', '', $expenseSum); // remove the '-' from expenses + + $netTotal = number_format($netTotal, 2); + + $totalsArr = array($incomeSum, $expenseSum, $netTotal); + + return $totalsArr; +} + +function trim_data($data) { + $data = str_replace('$', '', $data); // remove the dollar sign + $data = str_replace(',', '', $data); // remove ',' + $data = (float)$data; // convert to a number + + return $data; +} \ No newline at end of file diff --git a/public/index.php b/public/index.php index cd29608..38e9c0f 100644 --- a/public/index.php +++ b/public/index.php @@ -2,6 +2,8 @@ declare(strict_types = 1); +include '../views/transactions.php'; + $root = dirname(__DIR__) . DIRECTORY_SEPARATOR; define('APP_PATH', $root . 'app' . DIRECTORY_SEPARATOR); diff --git a/views/transactions.php b/views/transactions.php index 4db21ed..6c04782 100644 --- a/views/transactions.php +++ b/views/transactions.php @@ -1,3 +1,11 @@ + @@ -21,6 +29,12 @@ tfoot tr th { text-align: right; } + .t-red { + color: red; + } + .t-green { + color: green; + } @@ -35,19 +49,44 @@ + ". $setDate . ""; + } + elseif ($j !== $lastIndex) { // check if it's not the last column + $singleRow .= "". $data[$i][$j].""; + } else { + if (str_starts_with($data[$i][$j], '-')) { // check if it's an expense and add red color if true + $singleRow .= "". $data[$i][$j].""; + } else { // check if it's an income and add green color if true + $singleRow .= "". $data[$i][$j].""; + } + } + } + echo "". $singleRow . ""; + } + ?> Total Income: - + $". $totalIncome . ""; ?> Total Expense: - + -$". $totalExpense . ""; ?> Net Total: - + $" . $netTotal . "" ?>