source: StockMaster/Controllers/SaleController.cs@ dfe03b8

main
Last change on this file since dfe03b8 was dfe03b8, checked in by Ceyda <ceyda.huseini@…>, 3 days ago

Initialize StockMaster project

  • Property mode set to 100644
File size: 4.3 KB
Line 
1using Microsoft.AspNetCore.Mvc;
2using StockMaster.Data;
3using StockMaster.Models;
4using StockMaster.Services;
5using StockMaster.ViewModels;
6using Microsoft.AspNetCore.Http;
7using System.Linq;
8using System.Threading.Tasks;
9using System;
10using System.Collections.Generic;
11
12namespace 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}
Note: See TracBrowser for help on using the repository browser.