Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- date_default_timezone_set('Europe/Sofia');
- $today = new DateTime($_GET['today']);
- $invoices = [];
- foreach ($_GET['invoices'] as $key => $row) {
- $invoices[] = preg_split('/\s*\|\s*/', $row, -1, PREG_SPLIT_NO_EMPTY);
- $invoices[$key][0] = DateTime::createFromFormat('d/m/Y', $invoices[$key][0]);
- preg_match('/\d+\.*\d*/', $invoices[$key][3], $invoices[$key][3]);
- $invoices[$key][3] = $invoices[$key][3][0];
- }
- //clean unnecessary dates:
- foreach ($invoices as $key => $value) {
- $diffBetweenDates = $today->diff($value[0]);
- if ($diffBetweenDates->days >= 1826) {
- unset($invoices[$key]);
- }
- }
- // Obtain a list of keys for sorting:
- foreach ($invoices as $key => $row) {
- $sortDate[$key] = $row[0];
- $sortCompany[$key] = $row[1];
- }
- // Sort the data:
- array_multisort($sortDate, SORT_ASC, $sortCompany, SORT_ASC, $invoices );
- $invoiceResultArr = fillResultArray($invoices);
- //print result:
- echo '<ul>';
- foreach ($invoiceResultArr as $date => $value) {
- echo '<li><p>' . $date . '</p>';
- echo '<ul>';
- foreach ($invoiceResultArr[$date] as $company => $order) {
- echo '<li><p>' . $company . '</p>';
- echo '<ul>';
- echo '<li><p>' . implode(',', $order['products']) . '-' . $order['total'] . 'lv</p></li>';
- echo '</ul></li>';
- }
- echo '</ul>';
- echo '</li>';
- }
- echo '</ul>';
- function fillResultArray($array) {
- $resultArray = [];
- foreach ($array as $key => $value) {
- $date = $value[0]->format('d/m/Y');
- if (!key_exists($date, $resultArray)) {
- $resultArray[$date] = [];
- }
- if (!key_exists($value[1], $resultArray[$date])) {
- $resultArray[$date][$value[1]] = array('products' => [], 'total' => 0);
- }
- $resultArray[$date][$value[1]]['products'][] = $value[2];
- $resultArray[$date][$value[1]]['total'] += $value[3];
- }
- return $resultArray;
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement