Spaces:
Sleeping
Sleeping
File size: 3,231 Bytes
c2908d2 a19fd25 c2908d2 a19fd25 c2908d2 a19fd25 c2908d2 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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using OilShopManagement.Models;
using OilShopManagement.Repositories;
using OilShopManagement.ViewModels;
using System.Security.Claims;
namespace OilShopManagement.Controllers;
[Authorize]
public class StockController : Controller
{
private readonly IStockRepository _stock;
private readonly IProductRepository _products;
public StockController(IStockRepository stock, IProductRepository products)
{
_stock = stock;
_products = products;
}
public async Task<IActionResult> Index()
{
var transactions = await _stock.GetRecentAsync(50);
return View(transactions);
}
public async Task<IActionResult> Product(int id)
{
var product = await _products.GetWithCategoryAsync(id);
if (product == null) return NotFound();
var transactions = await _stock.GetByProductAsync(id);
ViewBag.Product = product;
return View(transactions);
}
[Authorize(Roles = "Admin")]
public async Task<IActionResult> Adjust()
{
var vm = new StockAdjustViewModel
{
Products = new SelectList(await _products.GetAllWithCategoryAsync(), "Id", "Name")
};
return View(vm);
}
[HttpPost]
[ValidateAntiForgeryToken]
[Authorize(Roles = "Admin")]
public async Task<IActionResult> Adjust(StockAdjustViewModel vm)
{
if (ModelState.IsValid)
{
var product = await _products.GetByIdAsync(vm.ProductId);
if (product == null) return NotFound();
var before = product.CurrentStock;
if (vm.Type == TransactionType.Purchase || vm.Type == TransactionType.Adjustment || vm.Type == TransactionType.Return)
product.CurrentStock += vm.Quantity;
else
product.CurrentStock -= vm.Quantity;
if (product.CurrentStock < 0)
{
ModelState.AddModelError("Quantity", "ط§ظ„ظƒظ…ظٹط© ظ„ط§ ظٹظ…ظƒظ† ط£ظ† طھظƒظˆظ† ط£ظ‚ظ„ ظ…ظ† طµظپط±");
vm.Products = new SelectList(await _products.GetAllWithCategoryAsync(), "Id", "Name");
return View(vm);
}
await _stock.AddAsync(new StockTransaction
{
ProductId = product.Id,
Type = vm.Type,
Quantity = vm.Quantity,
QuantityBefore = before,
QuantityAfter = product.CurrentStock,
UnitPrice = vm.UnitPrice,
Notes = vm.Notes,
UserId = User.FindFirstValue(ClaimTypes.NameIdentifier),
CreatedAt = DateTime.UtcNow
});
_products.Update(product);
await _stock.SaveChangesAsync();
TempData["Success"] = "طھظ… طھط¹ط¯ظٹظ„ ط§ظ„ظ…ط®ط²ظˆظ† ط¨ظ†ط¬ط§ط";
return RedirectToAction(nameof(Index));
}
vm.Products = new SelectList(await _products.GetAllWithCategoryAsync(), "Id", "Name");
return View(vm);
}
}
|