File size: 2,202 Bytes
c2908d2
a19fd25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8ded86b
 
 
a19fd25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c2908d2
8ded86b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using OilShopManagement.Data;
using OilShopManagement.Models;
using OilShopManagement.Repositories;
using OilShopManagement.ViewModels;

namespace OilShopManagement.Controllers;

[Authorize]
public class DashboardController : Controller
{
    private readonly IProductRepository _products;
    private readonly IInvoiceRepository _invoices;
    private readonly IStockRepository _stock;
    private readonly ApplicationDbContext _context;

    public DashboardController(IProductRepository products, IInvoiceRepository invoices,
        IStockRepository stock, ApplicationDbContext context)
    {
        _products = products;
        _invoices = invoices;
        _stock = stock;
        _context = context;
    }

    public async Task<IActionResult> Index()
    {
        var now = DateTime.UtcNow;
        var today = new DateTime(now.Year, now.Month, now.Day, 0, 0, 0, DateTimeKind.Utc);
        var monthStart = new DateTime(now.Year, now.Month, 1, 0, 0, 0, DateTimeKind.Utc);
        var tomorrow = today.AddDays(1);

        var vm = new DashboardViewModel
        {
            TotalProducts = await _products.CountAsync(p => p.IsActive),
            TotalCustomers = await _context.Customers.CountAsync(c => c.IsActive),
            TotalInvoicesToday = await _invoices.CountAsync(i => i.InvoiceDate >= today && i.InvoiceDate < tomorrow && i.Status == InvoiceStatus.Completed),
            LowStockCount = (await _products.GetLowStockProductsAsync()).Count(),
            SalesToday = await _invoices.GetTotalSalesAsync(today, tomorrow),
            SalesThisMonth = await _invoices.GetTotalSalesAsync(monthStart, tomorrow),
            ProfitToday = await _invoices.GetTotalProfitAsync(today, tomorrow),
            ProfitThisMonth = await _invoices.GetTotalProfitAsync(monthStart, tomorrow),
            RecentInvoices = await _invoices.GetByDateRangeAsync(today.AddDays(-7), tomorrow),
            LowStockProducts = await _products.GetLowStockProductsAsync(),
            RecentStockMovements = await _stock.GetRecentAsync(10)
        };

        return View(vm);
    }
}