Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions app/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 2 additions & 0 deletions public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

declare(strict_types = 1);

include '../views/transactions.php';

$root = dirname(__DIR__) . DIRECTORY_SEPARATOR;

define('APP_PATH', $root . 'app' . DIRECTORY_SEPARATOR);
Expand Down
45 changes: 42 additions & 3 deletions views/transactions.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
<?php
include '../app/App.php';
$data = getFile();
$dataLen = count($data);
$totalIncome = getTotals()[0];
$totalExpense = getTotals()[1];
$netTotal = getTotals()[2];
?>
<!DOCTYPE html>
<html>
<head>
Expand All @@ -21,6 +29,12 @@
tfoot tr th {
text-align: right;
}
.t-red {
color: red;
}
.t-green {
color: green;
}
</style>
</head>
<body>
Expand All @@ -35,19 +49,44 @@
</thead>
<tbody>
<!-- YOUR CODE -->
<?php
for($i = 1; $i<$dataLen; $i++) {
$itemLen = count($data[$i]);
$singleRow = "";
$lastIndex = $itemLen - 1;
for ($j = 0; $j<$itemLen; $j++) {
if ($j === 0) { // check if 1st column and change date format if true
$arrDate = $data[$i][$j];
$arrDate = explode('/', $arrDate);
$setDate = date("M d, Y", mktime(0,0,0,$arrDate[0],$arrDate[1],$arrDate[2]));
$singleRow .= "<td>". $setDate . "</td>";
}
elseif ($j !== $lastIndex) { // check if it's not the last column
$singleRow .= "<td>". $data[$i][$j]."</td>";
} else {
if (str_starts_with($data[$i][$j], '-')) { // check if it's an expense and add red color if true
$singleRow .= "<td class='t-red'>". $data[$i][$j]."</td>";
} else { // check if it's an income and add green color if true
$singleRow .= "<td class='t-green'>". $data[$i][$j]."</td>";
}
}
}
echo "<tr>". $singleRow . "</tr>";
}
?>
</tbody>
<tfoot>
<tr>
<th colspan="3">Total Income:</th>
<td><!-- YOUR CODE --></td>
<?php echo "<td>$". $totalIncome . "</td>"; ?>
</tr>
<tr>
<th colspan="3">Total Expense:</th>
<td><!-- YOUR CODE --></td>
<?php echo "<td>-$". $totalExpense . "</td>"; ?>
</tr>
<tr>
<th colspan="3">Net Total:</th>
<td><!-- YOUR CODE --></td>
<?php echo "<td>$" . $netTotal . "</td>" ?>
</tr>
</tfoot>
</table>
Expand Down