diff --git a/dist/ROI.php b/dist/ROI.php index f1e99f2..43f68ff 100644 --- a/dist/ROI.php +++ b/dist/ROI.php @@ -318,7 +318,7 @@ $allYears = []; // Auftragsbasierte "Slots" (direct/incl) pro Produkt $slotAgg = []; -function ensureMeta(Epirent $Epi, int $productPk, int $productNo, string $title): void { +function ensureMeta(Epirent $Epi, int $productPk, int $productNo, string $title, string $invoiceDate = ''): void { global $meta; if (!isset($meta[$productPk])) { $p = getProduct($Epi, $productPk); @@ -326,8 +326,19 @@ function ensureMeta(Epirent $Epi, int $productPk, int $productNo, string $title) 'product_pk' => $productPk, 'product_no' => $productNo ?: (int)($p->product_no ?? 0), 'title' => $title ?: (string)($p->name ?? ''), + // titles: [title => letztes invoice_date, an dem dieser Titel auftauchte] + 'titles' => [], ]; } + + // Titel-Historie: nur Titel erfassen, die tatsächlich auf einer Rechnung standen, + // damit Umbenennungen (gleiche Artikelnummer, neuer Name) sichtbar bleiben. + if ($title !== '' && $invoiceDate !== '') { + $prev = $meta[$productPk]['titles'][$title] ?? ''; + if ($invoiceDate > $prev) { + $meta[$productPk]['titles'][$title] = $invoiceDate; + } + } } function addRevenue(array &$pivotRef, int $productPk, int $year, float $revenueNet): void { @@ -617,7 +628,7 @@ function processLineItem( $dateStart = safeYmd((string)($li->date_start ?? '')) ?: null; $dateEnd = safeYmd((string)($li->date_end ?? '')) ?: null; - ensureMeta($Epi, $productPk, $productNo, $title); + ensureMeta($Epi, $productPk, $productNo, $title, $invoiceDate); // Jahr: Rechnungsdatum $year = $invoiceYear; @@ -828,10 +839,23 @@ $pivotRows = []; foreach ($meta as $productPk => $m) { $productPk = (int)$productPk; + // Titel-Historie auswerten: neuester Titel als Hauptname, restliche als "früher"-Liste + $titlesMap = $m['titles'] ?? []; + if (!empty($titlesMap)) { + arsort($titlesMap); // nach letztem invoice_date absteigend + $orderedTitles = array_keys($titlesMap); + $newestTitle = (string)$orderedTitles[0]; + $olderTitles = array_slice($orderedTitles, 1); + } else { + $newestTitle = (string)($m['title'] ?? ''); + $olderTitles = []; + } + $row = [ 'product_pk' => $productPk, 'product_no' => (int)($m['product_no'] ?? 0), - 'title' => (string)($m['title'] ?? ''), + 'title' => $newestTitle, + 'older_titles' => $olderTitles, 'years' => [], 'years_ext' => [], 'total_direct'=> 0.0, @@ -1179,7 +1203,22 @@ function slotsToDisplay(array $slotMap): array {