| 1 | using Microsoft.AspNetCore.Mvc;
|
|---|
| 2 | using StockMaster.Data;
|
|---|
| 3 | using StockMaster.Models;
|
|---|
| 4 | using StockMaster.Services;
|
|---|
| 5 | using StockMaster.ViewModels;
|
|---|
| 6 | using System.Linq;
|
|---|
| 7 | using System.Threading.Tasks;
|
|---|
| 8 |
|
|---|
| 9 | namespace StockMaster.Controllers
|
|---|
| 10 | {
|
|---|
| 11 | public class PurchaseOrderController : BaseController
|
|---|
| 12 | {
|
|---|
| 13 | private readonly IPurchaseOrderService _poService;
|
|---|
| 14 | private readonly StockDbContext _context;
|
|---|
| 15 |
|
|---|
| 16 | public PurchaseOrderController(IPurchaseOrderService poService, StockDbContext context)
|
|---|
| 17 | {
|
|---|
| 18 | _poService = poService;
|
|---|
| 19 | _context = context;
|
|---|
| 20 | }
|
|---|
| 21 |
|
|---|
| 22 | public async Task<IActionResult> Index()
|
|---|
| 23 | {
|
|---|
| 24 | var orders = await _poService.GetAllPurchaseOrdersAsync();
|
|---|
| 25 | return View(orders);
|
|---|
| 26 | }
|
|---|
| 27 |
|
|---|
| 28 | [HttpGet]
|
|---|
| 29 | public IActionResult Create()
|
|---|
| 30 | {
|
|---|
| 31 | ViewBag.Suppliers = _context.Suppliers.OrderBy(s => s.Name).ToList();
|
|---|
| 32 | ViewBag.Warehouses = _context.Warehouses.OrderBy(w => w.Name).ToList();
|
|---|
| 33 | ViewBag.Products = _context.Products.Where(p => p.IsActive).OrderBy(p => p.Name).ToList();
|
|---|
| 34 | return View(new PurchaseOrderCreateViewModel());
|
|---|
| 35 | }
|
|---|
| 36 |
|
|---|
| 37 | [HttpPost]
|
|---|
| 38 | [ValidateAntiForgeryToken]
|
|---|
| 39 | public async Task<IActionResult> Create(PurchaseOrderCreateViewModel model)
|
|---|
| 40 | {
|
|---|
| 41 |
|
|---|
| 42 | if (model.Items == null || !model.Items.Any())
|
|---|
| 43 | {
|
|---|
| 44 | ModelState.AddModelError("", "Please add at least one product");
|
|---|
| 45 | ViewBag.Suppliers = _context.Suppliers.OrderBy(s => s.Name).ToList();
|
|---|
| 46 | ViewBag.Warehouses = _context.Warehouses.OrderBy(w => w.Name).ToList();
|
|---|
| 47 | ViewBag.Products = _context.Products.Where(p => p.IsActive).OrderBy(p => p.Name).ToList();
|
|---|
| 48 | return View(model);
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 | if (ModelState.IsValid || model.Items.Any())
|
|---|
| 52 | {
|
|---|
| 53 | try
|
|---|
| 54 | {
|
|---|
| 55 | var po = new PurchaseOrder
|
|---|
| 56 | {
|
|---|
| 57 | SupplierId = model.SupplierId,
|
|---|
| 58 | WarehouseId = model.WarehouseId,
|
|---|
| 59 | ExpectedDeliveryDate = model.ExpectedDeliveryDate,
|
|---|
| 60 | Status = "Pending",
|
|---|
| 61 | PurchaseOrderItems = model.Items.Select(i => new PurchaseOrderItem
|
|---|
| 62 | {
|
|---|
| 63 | ProductId = i.ProductId,
|
|---|
| 64 | Quantity = i.Quantity,
|
|---|
| 65 | UnitCost = i.UnitCost
|
|---|
| 66 | }).ToList()
|
|---|
| 67 | };
|
|---|
| 68 |
|
|---|
| 69 | var result = await _poService.CreatePurchaseOrderAsync(po);
|
|---|
| 70 | if (result)
|
|---|
| 71 | {
|
|---|
| 72 | TempData["Success"] = "Purchase order created successfully";
|
|---|
| 73 | return RedirectToAction("Index");
|
|---|
| 74 | }
|
|---|
| 75 | ModelState.AddModelError("", "Failed to create purchase order");
|
|---|
| 76 | }
|
|---|
| 77 | catch (Exception ex)
|
|---|
| 78 | {
|
|---|
| 79 | ModelState.AddModelError("", $"Error: {ex.Message}");
|
|---|
| 80 | }
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | ViewBag.Suppliers = _context.Suppliers.OrderBy(s => s.Name).ToList();
|
|---|
| 84 | ViewBag.Warehouses = _context.Warehouses.OrderBy(w => w.Name).ToList();
|
|---|
| 85 | ViewBag.Products = _context.Products.Where(p => p.IsActive).OrderBy(p => p.Name).ToList();
|
|---|
| 86 | return View(model);
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | public async Task<IActionResult> Details(int id)
|
|---|
| 90 | {
|
|---|
| 91 | var po = await _poService.GetPurchaseOrderByIdAsync(id);
|
|---|
| 92 | if (po == null)
|
|---|
| 93 | return NotFound();
|
|---|
| 94 |
|
|---|
| 95 | return View(po);
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | [HttpPost]
|
|---|
| 99 | [ValidateAntiForgeryToken]
|
|---|
| 100 | public async Task<IActionResult> Receive(int id)
|
|---|
| 101 | {
|
|---|
| 102 | try
|
|---|
| 103 | {
|
|---|
| 104 | var result = await _poService.ReceivePurchaseOrderAsync(id);
|
|---|
| 105 | if (result)
|
|---|
| 106 | {
|
|---|
| 107 | TempData["Success"] = "Purchase order received and stock updated successfully";
|
|---|
| 108 | }
|
|---|
| 109 | else
|
|---|
| 110 | {
|
|---|
| 111 | TempData["Error"] = "Failed to receive purchase order. Order may already be received.";
|
|---|
| 112 | }
|
|---|
| 113 | }
|
|---|
| 114 | catch (Exception ex)
|
|---|
| 115 | {
|
|---|
| 116 | TempData["Error"] = $"Error: {ex.Message}";
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | return RedirectToAction("Details", new { id });
|
|---|
| 120 | }
|
|---|
| 121 | }
|
|---|
| 122 | } |
|---|