Repository navigation
Expand file tree
/
Copy pathindex.php
More file actions
112 lines (100 loc) · 3.64 KB
/
Copy pathindex.php
File metadata and controls
112 lines (100 loc) · 3.64 KB
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
<?php
require_once __DIR__ . '/vendor/autoload.php';
use App\FileProcessor;
use App\Helpers;
use Dotenv\Dotenv;
$dotenv = Dotenv::createImmutable(__DIR__);
$dotenv->load();
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['file'])) {
$filename = __DIR__ . '/uploads/' . $_FILES['file']['name'];
move_uploaded_file($_FILES['file']['tmp_name'], $filename);
$fileProcessor = new FileProcessor();
try {
$data = $fileProcessor->processFile($filename);
if (empty($data)) {
throw new Exception('No data found in file');
}
} catch (Exception $e) {
$error = $e->getMessage();
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="css/style.css">
<title>File Processor</title>
</head>
<body>
<main>
<h1>File Processor</h1>
<form action="" method="post" enctype="multipart/form-data">
<p>Upload your file to be processed:</p>
<input required type="file" name="file" accept=".csv">
<input class="submit-button" type="submit" value="Process">
</form>
<?php if (isset($error)): ?>
<p class="error"><?= $error ?></p>
<?php endif; ?>
<?php if (isset($fileProcessor) && isset($data)): ?>
<table>
<!-- Header -->
<tr>
<th>SKU</th>
<th>Cost</th>
<th>Price</th>
<th>QTY</th>
<th>Profit Margin</th>
<th>Total Profit (USD)</th>
<th>Total Profit (CAD)</th>
</tr>
<!-- Body -->
<?php foreach ($data as $row): ?>
<tr>
<td><?= $row['sku'] ?></td>
<td><?= Helpers::valueToDollars($row['cost']) ?></td>
<td><?= Helpers::valueToDollars($row['price']) ?></td>
<td class="<?= Helpers::isNegative($row['qty']) ? 'negative' : 'positive' ?>">
<?= $row['qty'] ?>
</td>
<td class="<?= Helpers::isNegative($row['profitMargin']) ? 'negative' : 'positive' ?>">
<?= Helpers::valueToPercentage($row['profitMargin']) ?>
</td>
<td class="<?= Helpers::isNegative($row['totalProfitUSD']) ? 'negative' : 'positive' ?>">
<?= Helpers::valueToDollars($row['totalProfitUSD']) ?>
</td>
<td class="<?= Helpers::isNegative($row['totalProfitCAD']) ? 'negative' : 'positive' ?>">
<?= Helpers::valueToDollars($row['totalProfitCAD']) ?>
</td>
</tr>
<?php endforeach; ?>
<!-- Footer-->
<?php
$totalQty = $fileProcessor->getTotalQty();
$averageProfitMargin = $fileProcessor->getAverageProfitMargin();
$totalProfitUSD = $fileProcessor->getTotalProfitUSD();
$totalProfitCAD = $fileProcessor->getTotalProfitCAD();
?>
<tr>
<td colspan="2"></td>
<td><?= Helpers::valueToDollars($fileProcessor->getAveragePrice()) ?></td>
<td class="<?= Helpers::isNegative($totalQty) ? 'negative' : 'positive' ?>">
<?= $totalQty ?>
</td>
<td class="<?= Helpers::isNegative($averageProfitMargin) ? 'negative' : 'positive' ?>">
<?= Helpers::valueToPercentage($averageProfitMargin) ?>
</td>
<td class="<?= Helpers::isNegative($totalProfitUSD) ? 'negative' : 'positive' ?>">
<?= Helpers::valueToDollars($totalProfitUSD) ?>
</td>
<td class="<?= Helpers::isNegative($totalProfitCAD) ? 'negative' : 'positive' ?>">
<?= Helpers::valueToDollars($totalProfitCAD) ?>
</td>
</tr>
</table>
<?php endif; ?>
</main>
</body>
</html>