using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;
using MimPortal.Data;
using System.Diagnostics;
namespace MimPortal.Pages.Inventarios
{
public class InventariosModel : PageModel
{
private readonly MimDbContext _context;
public InventariosModel(MimDbContext context)
{
_context = context;
}
[BindProperty(SupportsGet = true)]
public string? Codigo { get; set; }
[BindProperty(SupportsGet = true)]
public string? Descripcion { get; set; }
[BindProperty(SupportsGet = true)]
public int CurrentPage { get; set; } = 1;
public int PageSize { get; set; } = 25;
public int TotalPages { get; set; }
public int TotalRecords { get; set; }
public long ElapsedMs { get; set; }
public List<InventarioItem> Items { get; set; } = new();
public class InventarioItem
{
public string? Codigo { get; set; }
public string? CodigoSAT { get; set; }
public string? Descripcion { get; set; }
public string? UDM { get; set; }
public decimal Disponible { get; set; }
public decimal EmbarqueD { get; set; }
public decimal Apartado { get; set; }
public decimal RSEM { get; set; }
public decimal PrecioLista { get; set; }
public string? Moneda { get; set; }
public string? Ubicacion { get; set; }
public decimal Peso { get; set; }
public DateTime? FechaLista { get; set; }
}
public async Task OnGetAsync()
{
var sucursal = HttpContext.Session.GetString("Sucursal") ?? "MERIDA";
if (string.IsNullOrEmpty(Codigo) && string.IsNullOrEmpty(Descripcion))
{
return;
}
var sw = Stopwatch.StartNew();
// Build the query similar to the original ASP:
// Select Artmul as Codigo, ClaveProd as CodigoSAT, Nominv As Descripcion,
// (exirmul - capmul + embdire - rsem) as Disponible, embdire as EmbarqueD,
// capmul as Apartado, Rsem as RSEM, udminv as UDM, plainv as Precio_Lista,
// Desdiv as Moneda, marbmul as Marbete, ctlmul as Ubicacion, Pesoinv as Peso,
// convert(date,FechaLista) as FechaLista
// from multialmacen, Inventarios, Lineas, Monedas
// where Artmul like '%...%' and Nominv like '%...%'
// And cveinv = artmul and almmul = 1 and lininv = clalin and mplinv = cladiv
// order by artmul
var codigoFilter = Codigo?.ToUpper() ?? "";
var descripcionFilter = Descripcion?.ToUpper() ?? "";
var query = from m in _context.MULTIALMACEN
where m.Sucursal == sucursal
join i in _context.INVENTARIOS on m.ARTMUL equals i.CVEINV
join l in _context.LINEAS on i.LININV equals l.ClaLin
join mon in _context.MONEDAS on i.MPLINV equals mon.ClaDiv
where m.ALMMUL == 1
&& (string.IsNullOrEmpty(codigoFilter) || m.ARTMUL!.Contains(codigoFilter))
&& (string.IsNullOrEmpty(descripcionFilter) || i.NOMINV!.Contains(descripcionFilter))
orderby m.ARTMUL
select new InventarioItem
{
Codigo = m.ARTMUL,
CodigoSAT = i.ClaveProd,
Descripcion = i.NOMINV,
UDM = i.UDMINV,
Disponible = (m.EXIRMUL ?? 0) - (m.CAPMUL ?? 0) + (m.EMBDIRE ?? 0) - (m.RSEM ?? 0),
EmbarqueD = m.EMBDIRE ?? 0,
Apartado = m.CAPMUL ?? 0,
RSEM = m.RSEM ?? 0,
PrecioLista = i.PLAINV ?? 0,
Moneda = mon.DesDiv,
Ubicacion = m.CTLMUL,
Peso = i.PESOINV ?? 0,
FechaLista = i.FECHALISTA
};
TotalRecords = await query.CountAsync();
TotalPages = (int)Math.Ceiling(TotalRecords / (double)PageSize);
if (CurrentPage < 1) CurrentPage = 1;
if (CurrentPage > TotalPages && TotalPages > 0) CurrentPage = TotalPages;
Items = await query
.Skip((CurrentPage - 1) * PageSize)
.Take(PageSize)
.ToListAsync();
sw.Stop();
ElapsedMs = sw.ElapsedMilliseconds;
}
}
}