| [dfe03b8] | 1 | using Microsoft.AspNetCore.Mvc;
|
|---|
| 2 | using StockMaster.Data;
|
|---|
| 3 | using StockMaster.Models;
|
|---|
| 4 | using StockMaster.Services;
|
|---|
| 5 | using StockMaster.ViewModels;
|
|---|
| 6 | using Microsoft.AspNetCore.Http;
|
|---|
| 7 | using System.Linq;
|
|---|
| 8 | using System.Threading.Tasks;
|
|---|
| 9 | using System;
|
|---|
| 10 | using System.Collections.Generic;
|
|---|
| 11 |
|
|---|
| 12 | namespace StockMaster.Controllers
|
|---|
| 13 | {
|
|---|
| 14 | public class SaleController : Controller
|
|---|
| 15 | {
|
|---|
| 16 | private readonly ISaleService _saleService;
|
|---|
| 17 | private readonly StockDbContext _context;
|
|---|
| 18 |
|
|---|
| 19 | public SaleController(ISaleService saleService, StockDbContext context)
|
|---|
| 20 | {
|
|---|
| 21 | _saleService = saleService;
|
|---|
| 22 | _context = context;
|
|---|
| 23 | }
|
|---|
| 24 |
|
|---|
| 25 | public async Task<IActionResult> Index()
|
|---|
| 26 | {
|
|---|
| 27 | var sales = await _saleService.GetAllSalesAsync();
|
|---|
| 28 | return View(sales);
|
|---|
| 29 | }
|
|---|
| 30 |
|
|---|
| 31 | [HttpGet]
|
|---|
| 32 | public IActionResult Create()
|
|---|
| 33 | {
|
|---|
| 34 | ViewBag.Customers = _context.Customers.OrderBy(c => c.Name).ToList();
|
|---|
| 35 | ViewBag.Warehouses = _context.Warehouses.OrderBy(w => w.Name).ToList();
|
|---|
| 36 | ViewBag.Products = _context.Products.Where(p => p.IsActive).OrderBy(p => p.Name).ToList();
|
|---|
| 37 | return View(new SaleCreateViewModel());
|
|---|
| 38 | }
|
|---|
| 39 |
|
|---|
| 40 | [HttpPost]
|
|---|
| 41 | [ValidateAntiForgeryToken]
|
|---|
| 42 | public async Task<IActionResult> Create(SaleCreateViewModel model)
|
|---|
| 43 | {
|
|---|
| 44 | if (model.Items == null || !model.Items.Any())
|
|---|
| 45 | {
|
|---|
| 46 | ModelState.AddModelError("", "Please add at least one product to the sale.");
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | if (ModelState.IsValid)
|
|---|
| 50 | {
|
|---|
| 51 | try
|
|---|
| 52 | {
|
|---|
| 53 | var userId = HttpContext.Session.GetInt32("UserId");
|
|---|
| 54 | if (userId == null)
|
|---|
| 55 | {
|
|---|
| 56 | ModelState.AddModelError("", "Your session has expired. Please log in again.");
|
|---|
| 57 | }
|
|---|
| 58 | else
|
|---|
| 59 | {
|
|---|
| 60 | var sale = new Sale
|
|---|
| 61 | {
|
|---|
| 62 | CustomerId = model.CustomerId,
|
|---|
| 63 | WarehouseId = model.WarehouseId,
|
|---|
| 64 | UserId = userId,
|
|---|
| 65 | TotalAmount = model.Items.Sum(i => i.Quantity * i.UnitPrice),
|
|---|
| 66 | DateTime = DateTime.Now,
|
|---|
| 67 | SaleItems = model.Items.Select(i => new SaleItem
|
|---|
| 68 | {
|
|---|
| 69 | ProductId = i.ProductId,
|
|---|
| 70 | Quantity = i.Quantity,
|
|---|
| 71 | UnitPriceAtSale = i.UnitPrice
|
|---|
| 72 | }).ToList()
|
|---|
| 73 | };
|
|---|
| 74 |
|
|---|
| 75 | var result = await _saleService.CreateSaleAsync(sale);
|
|---|
| 76 | if (result)
|
|---|
| 77 | {
|
|---|
| 78 | TempData["Success"] = "Sale created successfully!";
|
|---|
| 79 | return RedirectToAction("Index");
|
|---|
| 80 | }
|
|---|
| 81 | else
|
|---|
| 82 | {
|
|---|
| 83 | ModelState.AddModelError("", "The sale could not be completed. Check stock levels or database rules.");
|
|---|
| 84 | }
|
|---|
| 85 | }
|
|---|
| 86 | }
|
|---|
| 87 | catch (Exception ex)
|
|---|
| 88 | {
|
|---|
| 89 | var realErrorMessage = ex.Message;
|
|---|
| 90 | if (ex.InnerException != null)
|
|---|
| 91 | {
|
|---|
| 92 | realErrorMessage = ex.InnerException.Message;
|
|---|
| 93 | if (ex.InnerException.InnerException != null)
|
|---|
| 94 | {
|
|---|
| 95 | realErrorMessage = ex.InnerException.InnerException.Message;
|
|---|
| 96 | }
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | ModelState.AddModelError("", "REAL DATABASE ERROR: " + realErrorMessage);
|
|---|
| 100 | }
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | ViewBag.Customers = _context.Customers.OrderBy(c => c.Name).ToList();
|
|---|
| 104 | ViewBag.Warehouses = _context.Warehouses.OrderBy(w => w.Name).ToList();
|
|---|
| 105 | ViewBag.Products = _context.Products.Where(p => p.IsActive).OrderBy(p => p.Name).ToList();
|
|---|
| 106 | return View(model);
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | public async Task<IActionResult> Details(int id)
|
|---|
| 110 | {
|
|---|
| 111 | var sale = await _saleService.GetSaleByIdAsync(id);
|
|---|
| 112 | if (sale == null) return NotFound();
|
|---|
| 113 | return View(sale);
|
|---|
| 114 | }
|
|---|
| 115 | }
|
|---|
| 116 | }
|
|---|