Skip to content

Commit a23910e

Browse files
committed
update forecasting logic, overview logic
1 parent 13b5b11 commit a23910e

File tree

1 file changed

+35
-7
lines changed

1 file changed

+35
-7
lines changed

components/ui/forecasting-card.tsx

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ interface ForecastData {
4242
month: string;
4343
demand: number;
4444
trend: "up" | "down" | "stable";
45+
isFutureMonth: boolean;
4546
}>;
4647
}
4748

@@ -165,6 +166,10 @@ export function ForecastingCard({ products, className }: ForecastingCardProps) {
165166
? new Date(products[0].createdAt).getUTCFullYear()
166167
: new Date().getUTCFullYear();
167168

169+
const currentDate = new Date();
170+
const currentMonth = currentDate.getUTCMonth() + 1; // 1-based month
171+
const currentYear = currentDate.getUTCFullYear();
172+
168173
const seasonalTrends = months.map((month, index) => {
169174
const monthKey = `${dataYear}-${String(index + 1).padStart(2, "0")}`;
170175
const productsThisMonth = productsByMonth.get(monthKey) || 0;
@@ -173,14 +178,28 @@ export function ForecastingCard({ products, className }: ForecastingCardProps) {
173178
const demand = productsThisMonth;
174179

175180
let trend: "up" | "down" | "stable" = "stable";
176-
if (index > 0) {
181+
182+
// Only calculate trends for months that have occurred (not future months)
183+
const monthNumber = index + 1;
184+
const isCurrentYear = dataYear === currentYear;
185+
const isPastMonth = !isCurrentYear || monthNumber <= currentMonth;
186+
187+
if (isPastMonth && index > 0) {
177188
const prevMonthKey = `${dataYear}-${String(index).padStart(2, "0")}`;
178189
const prevDemand = productsByMonth.get(prevMonthKey) || 0;
179-
if (demand > prevDemand) trend = "up";
180-
else if (demand < prevDemand) trend = "down";
190+
191+
// Only calculate trend if both months have actual data or are both zero
192+
if (demand > prevDemand) {
193+
trend = "up";
194+
} else if (demand < prevDemand && prevDemand > 0) {
195+
// Only show "down" if previous month had actual demand
196+
trend = "down";
197+
} else {
198+
trend = "stable";
199+
}
181200
}
182201

183-
return { month, demand, trend };
202+
return { month, demand, trend, isFutureMonth: !isPastMonth };
184203
});
185204

186205
return {
@@ -220,7 +239,11 @@ export function ForecastingCard({ products, className }: ForecastingCardProps) {
220239
}
221240
};
222241

223-
const getTrendIcon = (trend: string) => {
242+
const getTrendIcon = (trend: string, isFutureMonth: boolean = false) => {
243+
if (isFutureMonth) {
244+
return <Clock className="h-4 w-4 text-gray-400" />;
245+
}
246+
224247
switch (trend) {
225248
case "up":
226249
return <TrendingUp className="h-4 w-4 text-green-500" />;
@@ -335,11 +358,16 @@ export function ForecastingCard({ products, className }: ForecastingCardProps) {
335358
</h4>
336359
<div className="grid grid-cols-4 md:grid-cols-6 lg:grid-cols-12 gap-2">
337360
{forecastData.seasonalTrends.map((trend, index) => (
338-
<div key={index} className="text-center p-2 border rounded">
361+
<div
362+
key={index}
363+
className={`text-center p-2 border rounded ${
364+
trend.isFutureMonth ? "bg-gray-50 opacity-75" : ""
365+
}`}
366+
>
339367
<div className="text-xs font-medium">{trend.month}</div>
340368
<div className="text-lg font-bold">{trend.demand}</div>
341369
<div className="flex justify-center mt-1">
342-
{getTrendIcon(trend.trend)}
370+
{getTrendIcon(trend.trend, trend.isFutureMonth)}
343371
</div>
344372
</div>
345373
))}

0 commit comments

Comments
 (0)