[dyad] Fixed total customers calculation and added debugging logs - wrote 1 file(s) + extra files edited outside of Dyad
This commit is contained in:
parent
dbe224d67e
commit
961604be6c
1 changed files with 27 additions and 13 deletions
|
|
@ -7,7 +7,7 @@ interface DashboardStats {
|
|||
totalItemsInStock: string;
|
||||
stockOutLast30Days: string;
|
||||
lowStockAlerts: string;
|
||||
totalCustomers: string; // Added totalCustomers property
|
||||
totalCustomers: string;
|
||||
}
|
||||
|
||||
interface MonthlyActivity {
|
||||
|
|
@ -22,7 +22,7 @@ export function useDashboardData() {
|
|||
totalItemsInStock: "0",
|
||||
stockOutLast30Days: "0",
|
||||
lowStockAlerts: "0",
|
||||
totalCustomers: "0", // Initialize totalCustomers
|
||||
totalCustomers: "0",
|
||||
});
|
||||
const [monthlyActivityData, setMonthlyActivityData] = useState<MonthlyActivity[]>([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
|
|
@ -50,41 +50,51 @@ export function useDashboardData() {
|
|||
}
|
||||
|
||||
// Fetch all stock items
|
||||
console.log('Fetching stock items...');
|
||||
const stockRecords = await pb.collection('stock_items').getFullList({
|
||||
sort: '-created',
|
||||
signal: abortSignal,
|
||||
});
|
||||
console.log(`Fetched ${stockRecords.length} stock items`);
|
||||
|
||||
let calculatedTotalStockValue = 0;
|
||||
let calculatedTotalItemsInStock = 0;
|
||||
let calculatedLowStockAlerts = 0;
|
||||
|
||||
stockRecords.forEach((item: any) => {
|
||||
calculatedTotalStockValue += (item.quantity * item.cost_price);
|
||||
calculatedTotalItemsInStock += item.quantity;
|
||||
if (item.quantity < 10) {
|
||||
const quantity = item.quantity || 0;
|
||||
const costPrice = item.cost_price || 0;
|
||||
calculatedTotalStockValue += (quantity * costPrice);
|
||||
calculatedTotalItemsInStock += quantity;
|
||||
if (quantity < 10) {
|
||||
calculatedLowStockAlerts++;
|
||||
}
|
||||
});
|
||||
|
||||
// Fetch book out records for the last 30 days
|
||||
const thirtyDaysAgo = subDays(new Date(), 30).toISOString();
|
||||
console.log('Fetching book out records for last 30 days...');
|
||||
const bookOutRecordsLast30Days = await pb.collection('book_out_records').getFullList({
|
||||
filter: `date_booked_out >= "${thirtyDaysAgo}"`,
|
||||
signal: abortSignal,
|
||||
});
|
||||
console.log(`Fetched ${bookOutRecordsLast30Days.length} book out records for last 30 days`);
|
||||
|
||||
let calculatedStockOutLast30Days = 0;
|
||||
bookOutRecordsLast30Days.forEach((record: any) => {
|
||||
calculatedStockOutLast30Days += record.quantity;
|
||||
calculatedStockOutLast30Days += record.quantity || 0;
|
||||
});
|
||||
|
||||
// Fetch total customers
|
||||
const totalCustomersRecord = await pb.collection('customers').getFirstListItem('', {
|
||||
// Fetch total customers using getList to get totalItems
|
||||
console.log('Fetching total customers...');
|
||||
const customersList = await pb.collection('customers').getList(1, 1, {
|
||||
signal: abortSignal,
|
||||
});
|
||||
const totalCustomers = totalCustomersRecord.total;
|
||||
const totalCustomers = customersList.totalItems;
|
||||
|
||||
console.log(`Total customers: ${totalCustomers}`);
|
||||
|
||||
// Update stats state with fetched data
|
||||
setStats({
|
||||
totalStockValue: new Intl.NumberFormat("en-ZA", {
|
||||
style: "currency",
|
||||
|
|
@ -113,24 +123,27 @@ export function useDashboardData() {
|
|||
monthlyDataMap.set(monthKey, { stockIn: 0, stockOut: 0 });
|
||||
});
|
||||
|
||||
// Process stock items for 'stockIn'
|
||||
// Process stock items for 'stockIn' (based on creation date)
|
||||
console.log('Processing stock items for monthly activity...');
|
||||
stockRecords.forEach((item: any) => {
|
||||
const createdDate = new Date(item.created);
|
||||
if (createdDate >= currentYearStart) {
|
||||
const monthKey = format(createdDate, 'MMM');
|
||||
if (monthlyDataMap.has(monthKey)) {
|
||||
const current = monthlyDataMap.get(monthKey)!;
|
||||
current.stockIn += item.quantity;
|
||||
current.stockIn += item.quantity || 0;
|
||||
monthlyDataMap.set(monthKey, current);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Fetch all book out records for 'stockOut'
|
||||
const allBookOutRecords = await pb.collection('book_out_records').getFullList({ // Declared allBookOutRecords here
|
||||
console.log('Fetching all book out records for monthly activity...');
|
||||
const allBookOutRecords = await pb.collection('book_out_records').getFullList({
|
||||
sort: '-created',
|
||||
signal: abortSignal,
|
||||
});
|
||||
console.log(`Fetched ${allBookOutRecords.length} total book out records`);
|
||||
|
||||
allBookOutRecords.forEach((record: any) => {
|
||||
const bookedOutDate = new Date(record.date_booked_out);
|
||||
|
|
@ -138,7 +151,7 @@ export function useDashboardData() {
|
|||
const monthKey = format(bookedOutDate, 'MMM');
|
||||
if (monthlyDataMap.has(monthKey)) {
|
||||
const current = monthlyDataMap.get(monthKey)!;
|
||||
current.stockOut += record.quantity;
|
||||
current.stockOut += record.quantity || 0;
|
||||
monthlyDataMap.set(monthKey, current);
|
||||
}
|
||||
}
|
||||
|
|
@ -153,6 +166,7 @@ export function useDashboardData() {
|
|||
});
|
||||
|
||||
setMonthlyActivityData(sortedMonthlyData);
|
||||
console.log('Dashboard data loaded successfully');
|
||||
|
||||
} catch (err: any) {
|
||||
if (err.name === 'AbortError') {
|
||||
|
|
|
|||
Loading…
Reference in a new issue