From e4bc0caa51d62eb3e6ec139899aa4a2207192751 Mon Sep 17 00:00:00 2001
From: Neil <55785687+carkom@users.noreply.github.com>
Date: Wed, 29 May 2024 19:32:36 +0100
Subject: [PATCH] Spending report error fix with some visual changes (#2809)

* spending error fix with some visual changes

* notes
---
 .../reports/graphs/SpendingGraph.tsx          |  2 +-
 .../components/reports/reports/Spending.tsx   | 33 ++++---------------
 .../reports/reports/SpendingCard.tsx          | 21 +++++++++---
 .../spreadsheets/spending-spreadsheet.ts      |  4 ++-
 upcoming-release-notes/2809.md                |  6 ++++
 5 files changed, 33 insertions(+), 33 deletions(-)
 create mode 100644 upcoming-release-notes/2809.md

diff --git a/packages/desktop-client/src/components/reports/graphs/SpendingGraph.tsx b/packages/desktop-client/src/components/reports/graphs/SpendingGraph.tsx
index 616315fd2..2aadf407d 100644
--- a/packages/desktop-client/src/components/reports/graphs/SpendingGraph.tsx
+++ b/packages/desktop-client/src/components/reports/graphs/SpendingGraph.tsx
@@ -200,7 +200,7 @@ export function SpendingGraph({
                 height={height}
                 data={data.intervalData}
                 margin={{
-                  top: 0,
+                  top: 10,
                   right: 0,
                   left: 0,
                   bottom: 0,
diff --git a/packages/desktop-client/src/components/reports/reports/Spending.tsx b/packages/desktop-client/src/components/reports/reports/Spending.tsx
index c60c2f3b3..5b6dc4aee 100644
--- a/packages/desktop-client/src/components/reports/reports/Spending.tsx
+++ b/packages/desktop-client/src/components/reports/reports/Spending.tsx
@@ -61,6 +61,10 @@ export function Spending() {
     data.intervalData[27].months[
       monthUtils.subMonths(monthUtils.currentDay(), 3)
     ].daily !== 0;
+  const todayDay =
+    monthUtils.getDay(monthUtils.currentDay()) - 1 >= 28
+      ? 27
+      : monthUtils.getDay(monthUtils.currentDay()) - 1;
 
   return (
     <Page
@@ -173,14 +177,7 @@ export function Spending() {
                       <Text>
                         <PrivacyFilter blurIntensity={5}>
                           {amountToCurrency(
-                            Math.abs(
-                              data.intervalData[
-                                monthUtils.getDay(monthUtils.currentDay()) >= 29
-                                  ? 28
-                                  : monthUtils.getDay(monthUtils.currentDay()) -
-                                    1
-                              ].thisMonth,
-                            ),
+                            Math.abs(data.intervalData[todayDay].thisMonth),
                           )}
                         </PrivacyFilter>
                       </Text>
@@ -192,14 +189,7 @@ export function Spending() {
                       <Text>
                         <PrivacyFilter blurIntensity={5}>
                           {amountToCurrency(
-                            Math.abs(
-                              data.intervalData[
-                                monthUtils.getDay(monthUtils.currentDay()) >= 29
-                                  ? 28
-                                  : monthUtils.getDay(monthUtils.currentDay()) -
-                                    1
-                              ].lastMonth,
-                            ),
+                            Math.abs(data.intervalData[todayDay].lastMonth),
                           )}
                         </PrivacyFilter>
                       </Text>
@@ -212,16 +202,7 @@ export function Spending() {
                         <Text>
                           <PrivacyFilter blurIntensity={5}>
                             {amountToCurrency(
-                              Math.abs(
-                                data.intervalData[
-                                  monthUtils.getDay(monthUtils.currentDay()) >=
-                                  29
-                                    ? 28
-                                    : monthUtils.getDay(
-                                        monthUtils.currentDay(),
-                                      ) - 1
-                                ].average,
-                              ),
+                              Math.abs(data.intervalData[todayDay].average),
                             )}
                           </PrivacyFilter>
                         </Text>
diff --git a/packages/desktop-client/src/components/reports/reports/SpendingCard.tsx b/packages/desktop-client/src/components/reports/reports/SpendingCard.tsx
index 0eda99dd3..8a11dd342 100644
--- a/packages/desktop-client/src/components/reports/reports/SpendingCard.tsx
+++ b/packages/desktop-client/src/components/reports/reports/SpendingCard.tsx
@@ -5,6 +5,7 @@ import { amountToCurrency } from 'loot-core/src/shared/util';
 
 import { useCategories } from '../../../hooks/useCategories';
 import { styles } from '../../../style/styles';
+import { theme } from '../../../style/theme';
 import { Block } from '../../common/Block';
 import { View } from '../../common/View';
 import { PrivacyFilter } from '../../PrivacyFilter';
@@ -27,11 +28,14 @@ export function SpendingCard() {
   }, [categories]);
 
   const data = useReport('default', getGraphData);
+  const todayDay =
+    monthUtils.getDay(monthUtils.currentDay()) - 1 >= 28
+      ? 27
+      : monthUtils.getDay(monthUtils.currentDay()) - 1;
   const difference =
     data &&
-    data.intervalData[monthUtils.getDay(monthUtils.currentDay()) - 1].average -
-      data.intervalData[monthUtils.getDay(monthUtils.currentDay()) - 1]
-        .thisMonth;
+    data.intervalData[todayDay].lastMonth -
+      data.intervalData[todayDay].thisMonth;
 
   return (
     <ReportCard flex="1" to="/reports/spending">
@@ -60,10 +64,17 @@ export function SpendingCard() {
                   ...styles.mediumText,
                   fontWeight: 500,
                   marginBottom: 5,
+                  color: !difference
+                    ? 'inherit'
+                    : difference <= 0
+                      ? theme.noticeTextLight
+                      : theme.errorText,
                 }}
               >
                 <PrivacyFilter activationFilters={[!isCardHovered]}>
-                  {data && amountToCurrency(difference)}
+                  {data &&
+                    (difference && difference > 0 ? '+' : '') +
+                      amountToCurrency(difference)}
                 </PrivacyFilter>
               </Block>
             </View>
@@ -75,7 +86,7 @@ export function SpendingCard() {
             style={{ flex: 1 }}
             compact={true}
             data={data}
-            mode="average"
+            mode="lastMonth"
           />
         ) : (
           <LoadingIndicator />
diff --git a/packages/desktop-client/src/components/reports/spreadsheets/spending-spreadsheet.ts b/packages/desktop-client/src/components/reports/spreadsheets/spending-spreadsheet.ts
index e844aa63c..9cc953c40 100644
--- a/packages/desktop-client/src/components/reports/spreadsheets/spending-spreadsheet.ts
+++ b/packages/desktop-client/src/components/reports/spreadsheets/spending-spreadsheet.ts
@@ -151,10 +151,12 @@ export function createSpendingSpreadsheet({
           a.cumulative < b.cumulative ? a : b,
         ).cumulative;
 
+        const totalDaily = data.reduce((a, v) => (a = a + v.totalTotals), 0);
+
         return {
           date: data[0].date,
           cumulative: maxCumulative,
-          daily: data[0].totalTotals,
+          daily: totalDaily,
           month: month.month,
         };
       });
diff --git a/upcoming-release-notes/2809.md b/upcoming-release-notes/2809.md
new file mode 100644
index 000000000..09e24ad29
--- /dev/null
+++ b/upcoming-release-notes/2809.md
@@ -0,0 +1,6 @@
+---
+category: Bugfix
+authors: [carkom]
+---
+
+Fixes a crashing bug that happens when the current day of the month is greater than 28.
-- 
GitLab