Skip to content

Commit

Permalink
🎨 Update Finance Pages (#116)
Browse files Browse the repository at this point in the history
Co-authored-by: DasunDhananjaya-git< it22190420@my.sliit.lk>
  • Loading branch information
nmdra authored Oct 14, 2024
1 parent a12d047 commit 3420b05
Show file tree
Hide file tree
Showing 5 changed files with 492 additions and 280 deletions.
50 changes: 50 additions & 0 deletions backend/controllers/orderController.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,3 +248,53 @@ export const getTotalSales = async (req, res) => {
return res.status(500).json({ message: 'Error fetching total sales' })
}
}


export const getShopTotalIncome = async (req, res) => {
try {
const totalIncome = await Order.aggregate([
{
$group: {
_id: '$farmer.shopId', // Group by shop ID
totalIncome: { $sum: '$totalPrice' }, // Sum totalPrice for each shop
},
},
{
$lookup: {
from: 'shops', // Name of your shop collection
localField: '_id', // The shop ID from the previous group stage
foreignField: '_id', // The shop ID in the shops collection
as: 'shopDetails', // Output array field for joined shop details
},
},
{
$unwind: '$shopDetails', // Deconstruct the shopDetails array
},
{
$lookup: {
from: 'farmers', // Assuming the farmers collection holds owner information
localField: 'shopDetails.farmer', // Reference to farmer ID in the shopDetails
foreignField: '_id', // The farmer ID in the farmers collection
as: 'farmerDetails', // Output array field for joined farmer details
},
},
{
$unwind: '$farmerDetails', // Deconstruct the farmerDetails array
},
{
$project: {
_id: 0, // Exclude the default _id field
shopId: '$_id', // Include the shop ID
shopName: '$shopDetails.name', // Include the shop name
totalIncome: 1, // Include total income
ownerName: '$farmerDetails.name', // Include the owner's name
},
},
]);

res.status(200).json(totalIncome); // Send the total income response
} catch (error) {
console.error('Error fetching shop total income with owner:', error);
res.status(500).json({ message: 'Error fetching total income for shops with owner' });
}
}
2 changes: 2 additions & 0 deletions backend/routes/orderRoute.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
getOrderById,
getDailyOrders,
getTotalSales,
getShopTotalIncome,
} from '../controllers/orderController.js'
const orderRouter = express.Router()

Expand All @@ -22,6 +23,7 @@ orderRouter.get('/get-user-orders/:id', getOrdersByUserId)
orderRouter.put('/:id', updateOrderStatus)
orderRouter.delete('/:id', DeleteOrder)
orderRouter.get('/get-shop/:id', getShopByFarmerId)
orderRouter.get('/shop-total-income', getShopTotalIncome);

orderRouter.get('/:id', getOrderById) // Fetch order details by ID with populated farmer and user details

Expand Down
2 changes: 2 additions & 0 deletions frontend/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ import HelpLayout from './Layouts/HelpLayout'
import Help from './Pages/Help/Help'
import SupportTicket from './Pages/Help/SupportTicket'
import Feedback from './Pages/Help/Feedback'
import ManageShopIncome from './Pages/Admin/ManageShopIncome'

// Define all routes in a single Router
const router = createBrowserRouter(
Expand Down Expand Up @@ -218,6 +219,7 @@ const router = createBrowserRouter(
/>
<Route path="/coupens" element={<Coupens />} />
<Route path="/finance" element={<Finance />} />
<Route path="/manage-shop-income" element={<ManageShopIncome />} />
</Route>

{/*
Expand Down
Loading

0 comments on commit 3420b05

Please sign in to comment.