Może spróbuj użyć zamiast kilku tablic np.: tablicę asocjacyjną.
przykład
<?php
$products = [
[
'name' => 'Produkt1',
'quantity' => 0,
'price' => 0,
'discount' => 0,
'vat_rate' => 0
],
[
'name' => 'Produkt2',
'quantity' => 0,
'price' => 0,
'discount' => 0,
'vat_rate' => 0
],
[
'name' => 'Produkt3',
'quantity' => 0,
'price' => 0,
'discount' => 0,
'vat_rate' => 0
],
[
'name' => 'Produkt4',
'quantity' => 0,
'price' => 0,
'discount' => 0,
'vat_rate' => 0
],
[
'name' => 'Produkt5',
'quantity' => 0,
'price' => 0,
'discount' => 0,
'vat_rate' => 0
]
];
// Wypełnianie losowymi danymi
$pennies_arr = [0, 10, 30, 50, 70, 99];
$discount_arr = [0, 5, 0, 10, 0, 15, 0, 20];
$vat_rate_arr = [0, 5, 8, 23];
foreach ($products as &$row) {
$row['quantity'] = rand(1, 5);
$row['price'] = number_format((rand(10, 150).'.'.$pennies_arr[array_rand($pennies_arr)]), 2);
$row['discount'] = number_format($discount_arr[array_rand($discount_arr)]);
$row['vat_rate'] = number_format($vat_rate_arr[array_rand($vat_rate_arr)]);
}
unset($row);
// Generowanie wierszy do tablicy
$summary = [];
$rows = '';
foreach ($products as $key => $row) {
$key++;
$net_price_after_discount = $row['price'] - (($row['price'] / 100) * $row['discount']);
$net_price = $net_price_after_discount * $row['quantity'];
$vat_value = ($net_price / 100) * $row['vat_rate'];
$gross_price = $net_price + $vat_value;
$rows .= '<tr>'
. "<td>{$key}.</td>"
. "<td>{$row['name']}</td>"
. "<td>{$row['quantity']}</td>"
. "<td>{$row['price']}</td>"
. "<td>{$row['discount']}</td>"
. '<td>'.number_format($net_price_after_discount, 2).'</td>'
. "<td>{$row['vat_rate']}</td>"
. '<td>'.number_format($net_price, 2).'</td>'
. '<td>'.number_format($vat_value, 2).'</td>'
. '<td>'.number_format($gross_price, 2).'</td>'
. '</tr>';
if (! isset($summary[$row['vat_rate']])){
$summary += [$row['vat_rate'] => [$net_price, $vat_value, $gross_price]];
} else {
$summary[$row['vat_rate']][0] += $net_price;
$summary[$row['vat_rate']][1] += $vat_value;
$summary[$row['vat_rate']][2] += $gross_price;
}
}
// var_dump($summary);
// Generowanie wierszy do tablicy summary
$net_price_total = $vat_value_total = $gross_price_total = 0;
ksort($summary, SORT_NUMERIC);
$rows_summary = '';
foreach ($summary as $key => $row) {
$net_price_total += $row[0];
$vat_value_total += $row[1];
$gross_price_total += $row[2];
$rows_summary .= '<tr>'
. "<td>{$key}%</td>"
. '<td>'.number_format($row[0], 2).'</td>'
. '<td>'.number_format($row[1], 2).'</td>'
. '<td>'.number_format($row[2], 2).'</td>'
. '</tr>';
}
$summary_foot = '<td>'.number_format($net_price_total, 2).'</td>'
. '<td>'.number_format($vat_value_total, 2).'</td>'
. '<td class="to_pay">'.number_format($gross_price_total, 2).'</td>';
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<style>
.container {
width: 800px;
}
table {
box-sizing: border-box;
width: 100%;
border-spacing: 0;
border-collapse: separate;
}
th, td {
border: 1px solid white;
}
thead tr {
background-color: rgba(0,0,0,0.7);
color: white;
}
th {
font: 900 1.05em/1.6 monospace;
padding: 0 0.25em;
}
th.vat {
padding: 0 0.35em;
}
th.name {
width: 30%;
}
tbody tr {
transition: background-color 0.5s;
}
tbody tr:nth-child(odd) {
background-color: rgba(0,0,0,0.1);
}
tbody tr:nth-child(even) {
background-color: rgba(0,0,0,0.2);
}
tbody tr:hover {
background-color: rgba(0,0,0,0.4);
}
td {
font: 400 1em/1.6 monospace;
text-align: center;
}
table.summary {
width: 50%;
}
table.summary tfoot {
background-color: rgba(0,0,0,0.6);
}
table.summary tfoot td {
font: 700 1.05em/1.55 monospace;
}
table.summary .to_pay {
color: limegreen;
}
</style>
</head>
<body>
<div class="container">
<table>
<thead>
<tr>
<th>Lp.</th>
<th class="name">Nazwa towaru / usługi</th>
<th>Ilość</th>
<th>Cena netto</th>
<th>Rabat %</th>
<th>Cena netto po rabacie</th>
<th>Stawka Vat</th>
<th>Kwota netto</th>
<th>Kwota Vat</th>
<th>Kwota brutto</th>
</tr>
</thead>
<tbody>
<?php echo $rows; ?>
</tbody>
</table>
<br />
<table class="summary">
<thead>
<tr>
<th>Stawka Vat</th>
<th>Kwota netto</th>
<th>Kwota Vat</th>
<th>Kwota brutto</th>
</tr>
</thead>
<tbody>
<?php echo $rows_summary; ?>
</tbody>
<tfoot>
<tr>
<td>Razem</td>
<?php echo $summary_foot; ?>
</tr>
</tfoot>
</table>
</div>
</body>
</html>