diff --git a/src/food-market.api/Controllers/Purchases/SupplierReturnsController.cs b/src/food-market.api/Controllers/Purchases/SupplierReturnsController.cs new file mode 100644 index 0000000..ac5f807 --- /dev/null +++ b/src/food-market.api/Controllers/Purchases/SupplierReturnsController.cs @@ -0,0 +1,412 @@ +using System.ComponentModel.DataAnnotations; +using foodmarket.Application.Common; +using foodmarket.Application.Inventory; +using foodmarket.Domain.Inventory; +using foodmarket.Domain.Purchases; +using foodmarket.Infrastructure.Persistence; +using foodmarket.Api.Infrastructure.Authorization; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; + +namespace foodmarket.Api.Controllers.Purchases; + +/// Возврат поставщику. Списывает товар со склада (как Supply, но +/// со знаком минус и movement type SupplierReturn). Опционально ссылается на +/// исходную приёмку через referenceSupplyId. +[ApiController] +[Authorize] +[Route("api/purchases/supplier-returns")] +public class SupplierReturnsController : ControllerBase +{ + private readonly AppDbContext _db; + private readonly IStockService _stock; + + public SupplierReturnsController(AppDbContext db, IStockService stock) + { + _db = db; + _stock = stock; + } + + public record SupplierReturnListRow( + Guid Id, string Number, DateTime Date, SupplierReturnStatus Status, + Guid SupplierId, string SupplierName, + Guid StoreId, string StoreName, + Guid CurrencyId, string CurrencyCode, + decimal Total, int LineCount, + DateTime? PostedAt, + Guid? ReferenceSupplyId, string? ReferenceSupplyNumber); + + public record SupplierReturnLineDto( + Guid? Id, Guid ProductId, string? ProductName, string? ProductArticle, string? UnitSymbol, + decimal Quantity, decimal UnitPrice, decimal LineTotal, int SortOrder, + decimal? StockAtStore); + + public record SupplierReturnDto( + Guid Id, string Number, DateTime Date, SupplierReturnStatus Status, + Guid SupplierId, string SupplierName, + Guid StoreId, string StoreName, + Guid CurrencyId, string CurrencyCode, + Guid? ReferenceSupplyId, string? ReferenceSupplyNumber, + string? Notes, + decimal Total, DateTime? PostedAt, + IReadOnlyList Lines); + + public record SupplierReturnLineInput( + Guid ProductId, + [Range(0, 1e10)] decimal Quantity, + [Range(0, 1e10)] decimal UnitPrice); + + public record SupplierReturnInput( + DateTime Date, + Guid SupplierId, Guid StoreId, Guid CurrencyId, + Guid? ReferenceSupplyId, + string? Notes, + IReadOnlyList Lines); + + [HttpGet] + public async Task>> List( + [FromQuery] PagedRequest req, + [FromQuery] SupplierReturnStatus? status, + [FromQuery] Guid? supplierId, + [FromQuery] Guid? storeId, + CancellationToken ct) + { + var q = from r in _db.SupplierReturns.AsNoTracking() + join cp in _db.Counterparties on r.SupplierId equals cp.Id + join st in _db.Stores on r.StoreId equals st.Id + join cu in _db.Currencies on r.CurrencyId equals cu.Id + select new { r, cp, st, cu }; + + if (status is not null) q = q.Where(x => x.r.Status == status); + if (supplierId is not null) q = q.Where(x => x.r.SupplierId == supplierId); + if (storeId is not null) q = q.Where(x => x.r.StoreId == storeId); + if (!string.IsNullOrWhiteSpace(req.Search)) + { + var s = req.Search.Trim().ToLower(); + q = q.Where(x => x.r.Number.ToLower().Contains(s) || x.cp.Name.ToLower().Contains(s)); + } + + var total = await q.CountAsync(ct); + q = (req.Sort, req.Desc) switch + { + ("number", false) => q.OrderBy(x => x.r.Number), + ("number", true) => q.OrderByDescending(x => x.r.Number), + ("supplier", false) => q.OrderBy(x => x.cp.Name).ThenByDescending(x => x.r.Date), + ("supplier", true) => q.OrderByDescending(x => x.cp.Name).ThenByDescending(x => x.r.Date), + ("status", false) => q.OrderBy(x => x.r.Status).ThenByDescending(x => x.r.Date), + ("status", true) => q.OrderByDescending(x => x.r.Status).ThenByDescending(x => x.r.Date), + ("total", false) => q.OrderBy(x => x.r.Total).ThenByDescending(x => x.r.Date), + ("total", true) => q.OrderByDescending(x => x.r.Total).ThenByDescending(x => x.r.Date), + ("date", false) => q.OrderBy(x => x.r.Date).ThenBy(x => x.r.Number), + _ => q.OrderByDescending(x => x.r.Date).ThenByDescending(x => x.r.Number), + }; + var items = await q + .Skip(req.Skip).Take(req.Take) + .Select(x => new SupplierReturnListRow( + x.r.Id, x.r.Number, x.r.Date, x.r.Status, + x.cp.Id, x.cp.Name, + x.st.Id, x.st.Name, + x.cu.Id, x.cu.Code, + x.r.Total, + x.r.Lines.Count, + x.r.PostedAt, + x.r.ReferenceSupplyId, + x.r.ReferenceSupplyId == null ? null : _db.Supplies.Where(s => s.Id == x.r.ReferenceSupplyId).Select(s => s.Number).FirstOrDefault())) + .ToListAsync(ct); + + return new PagedResult { Items = items, Total = total, Page = req.Page, PageSize = req.Take }; + } + + [HttpGet("{id:guid}")] + public async Task> Get(Guid id, CancellationToken ct) + { + var dto = await GetInternal(id, ct); + return dto is null ? NotFound() : Ok(dto); + } + + [HttpPost, RequiresPermission("SuppliesEdit")] + public async Task> Create([FromBody] SupplierReturnInput input, CancellationToken ct) + { + if (RequiredGuid.FirstMissing( + (nameof(input.SupplierId), input.SupplierId), + (nameof(input.StoreId), input.StoreId), + (nameof(input.CurrencyId), input.CurrencyId)) is { } missing) + return BadRequest(new { error = $"Поле {missing} обязательно.", field = missing }); + if (input.Lines is null || input.Lines.Count == 0) + return BadRequest(new { error = "Возврат должен содержать хотя бы одну позицию." }); + + if (input.ReferenceSupplyId is { } refId) + { + var refSupply = await _db.Supplies.AsNoTracking().FirstOrDefaultAsync(s => s.Id == refId, ct); + if (refSupply is null) + return BadRequest(new { error = "Исходная приёмка не найдена.", field = "referenceSupplyId" }); + if (refSupply.Status != SupplyStatus.Posted) + return BadRequest(new { error = "Можно возвращать только из проведённой приёмки.", field = "referenceSupplyId" }); + if (refSupply.SupplierId != input.SupplierId) + return BadRequest(new { error = "Поставщик возврата должен совпадать с поставщиком исходной приёмки.", field = "supplierId" }); + } + + var number = await GenerateNumberAsync(input.Date, ct); + var r = new SupplierReturn + { + Number = number, + Date = input.Date, + Status = SupplierReturnStatus.Draft, + SupplierId = input.SupplierId, + StoreId = input.StoreId, + CurrencyId = input.CurrencyId, + ReferenceSupplyId = input.ReferenceSupplyId, + Notes = input.Notes, + }; + var order = 0; + foreach (var l in input.Lines) + { + r.Lines.Add(new SupplierReturnLine + { + ProductId = l.ProductId, + Quantity = l.Quantity, + UnitPrice = l.UnitPrice, + LineTotal = l.Quantity * l.UnitPrice, + SortOrder = order++, + }); + } + r.Total = r.Lines.Sum(x => x.LineTotal); + + _db.SupplierReturns.Add(r); + if (await SaveOrFkErrorAsync(ct) is { } err) return err; + var dto = await GetInternal(r.Id, ct); + return CreatedAtAction(nameof(Get), new { id = r.Id }, dto); + } + + private async Task SaveOrFkErrorAsync(CancellationToken ct) + { + try + { + await _db.SaveChangesAsync(ct); + return null; + } + catch (DbUpdateException ex) when (ex.InnerException is Npgsql.PostgresException pg && pg.SqlState == "23503") + { + var name = pg.ConstraintName ?? ""; + string field = name.Contains("Supplier") ? "supplierId" + : name.Contains("Store") ? "storeId" + : name.Contains("Currency") ? "currencyId" + : name.Contains("Product") ? "productId" + : name.Contains("Supply") ? "referenceSupplyId" + : "(unknown)"; + return BadRequest(new { error = $"Связанная запись не найдена: {field}.", field, constraint = name }); + } + } + + [HttpPut("{id:guid}"), RequiresPermission("SuppliesEdit")] + public async Task Update(Guid id, [FromBody] SupplierReturnInput input, CancellationToken ct) + { + if (RequiredGuid.FirstMissing( + (nameof(input.SupplierId), input.SupplierId), + (nameof(input.StoreId), input.StoreId), + (nameof(input.CurrencyId), input.CurrencyId)) is { } missing) + return BadRequest(new { error = $"Поле {missing} обязательно.", field = missing }); + if (input.Lines is null || input.Lines.Count == 0) + return BadRequest(new { error = "Возврат должен содержать хотя бы одну позицию." }); + var r = await _db.SupplierReturns.Include(x => x.Lines).FirstOrDefaultAsync(x => x.Id == id, ct); + if (r is null) return NotFound(); + if (r.Status != SupplierReturnStatus.Draft) + return Conflict(new { error = "Только черновик может быть изменён. Сначала отмени проведение." }); + + r.Date = input.Date; + r.SupplierId = input.SupplierId; + r.StoreId = input.StoreId; + r.CurrencyId = input.CurrencyId; + r.ReferenceSupplyId = input.ReferenceSupplyId; + r.Notes = input.Notes; + + _db.SupplierReturnLines.RemoveRange(r.Lines); + r.Lines.Clear(); + var order = 0; + foreach (var l in input.Lines) + { + r.Lines.Add(new SupplierReturnLine + { + SupplierReturnId = r.Id, + ProductId = l.ProductId, + Quantity = l.Quantity, + UnitPrice = l.UnitPrice, + LineTotal = l.Quantity * l.UnitPrice, + SortOrder = order++, + }); + } + r.Total = r.Lines.Sum(x => x.LineTotal); + + if (await SaveOrFkErrorAsync(ct) is { } err) return err; + return NoContent(); + } + + [HttpDelete("{id:guid}"), RequiresPermission("SuppliesDelete")] + public async Task Delete(Guid id, CancellationToken ct) + { + var r = await _db.SupplierReturns.FirstOrDefaultAsync(x => x.Id == id, ct); + if (r is null) return NotFound(); + if (r.Status != SupplierReturnStatus.Draft) + return Conflict(new { error = "Нельзя удалить проведённый документ. Сначала отмени проведение." }); + _db.SupplierReturns.Remove(r); + await _db.SaveChangesAsync(ct); + return NoContent(); + } + + [HttpPost("{id:guid}/post"), RequiresPermission("SuppliesPost")] + public async Task Post(Guid id, CancellationToken ct) + { + var r = await _db.SupplierReturns.Include(x => x.Lines).FirstOrDefaultAsync(x => x.Id == id, ct); + if (r is null) return NotFound(); + if (r.Status == SupplierReturnStatus.Posted) return Conflict(new { error = "Документ уже проведён." }); + if (r.Lines.Count == 0) return BadRequest(new { error = "Нельзя провести документ без строк." }); + + // Защита от ухода в минус. + var byProduct = r.Lines.GroupBy(l => l.ProductId) + .Select(g => new { ProductId = g.Key, Quantity = g.Sum(x => x.Quantity) }).ToList(); + var productIds = byProduct.Select(x => x.ProductId).ToList(); + var stocks = await _db.Stocks + .Where(s => s.StoreId == r.StoreId && productIds.Contains(s.ProductId)) + .ToDictionaryAsync(s => s.ProductId, s => s.Quantity, ct); + var conflicts = new List(); + foreach (var x in byProduct) + { + stocks.TryGetValue(x.ProductId, out var available); + if (available < x.Quantity) + { + var name = await _db.Products.Where(p => p.Id == x.ProductId).Select(p => p.Name).FirstOrDefaultAsync(ct); + conflicts.Add(new + { + productId = x.ProductId, + productName = name, + returnQty = x.Quantity, + available, + }); + } + } + if (conflicts.Count > 0) + return Conflict(new { error = "Нельзя вернуть больше, чем есть на складе.", lines = conflicts }); + + await using var tx = await _db.Database.BeginTransactionAsync( + System.Data.IsolationLevel.Serializable, ct); + + foreach (var line in r.Lines) + { + await _stock.ApplyMovementAsync(new StockMovementDraft( + ProductId: line.ProductId, + StoreId: r.StoreId, + Quantity: -line.Quantity, + Type: MovementType.SupplierReturn, + DocumentType: "supplier-return", + DocumentId: r.Id, + DocumentNumber: r.Number, + UnitCost: line.UnitPrice, + OccurredAt: r.Date), ct); + } + + r.Status = SupplierReturnStatus.Posted; + r.PostedAt = DateTime.UtcNow; + try + { + await _db.SaveChangesAsync(ct); + await tx.CommitAsync(ct); + } + catch (Exception ex) when (IsSerializationConflict(ex)) + { + return Conflict(new { error = "Документ проводится параллельно. Повторите попытку." }); + } + return NoContent(); + } + + [HttpPost("{id:guid}/unpost"), RequiresPermission("SuppliesPost")] + public async Task Unpost(Guid id, CancellationToken ct) + { + var r = await _db.SupplierReturns.Include(x => x.Lines).FirstOrDefaultAsync(x => x.Id == id, ct); + if (r is null) return NotFound(); + if (r.Status != SupplierReturnStatus.Posted) return Conflict(new { error = "Документ не проведён." }); + + // Reverse — добавляем товар обратно, проверка на минус не нужна (только растёт). + foreach (var line in r.Lines) + { + await _stock.ApplyMovementAsync(new StockMovementDraft( + ProductId: line.ProductId, + StoreId: r.StoreId, + Quantity: line.Quantity, + Type: MovementType.SupplierReturn, + DocumentType: "supplier-return-reversal", + DocumentId: r.Id, + DocumentNumber: r.Number, + UnitCost: line.UnitPrice, + OccurredAt: DateTime.UtcNow, + Notes: $"Отмена возврата {r.Number}"), ct); + } + + r.Status = SupplierReturnStatus.Draft; + r.PostedAt = null; + await _db.SaveChangesAsync(ct); + return NoContent(); + } + + private static bool IsSerializationConflict(Exception ex) + { + for (Exception? e = ex; e is not null; e = e.InnerException) + { + if (e is System.Data.Common.DbException { SqlState: "40001" or "40P01" }) + return true; + } + return false; + } + + private async Task GenerateNumberAsync(DateTime date, CancellationToken ct) + { + var year = date.Year; + var prefix = $"ВП-{year}-"; + var lastNumber = await _db.SupplierReturns + .Where(r => r.Number.StartsWith(prefix)) + .OrderByDescending(r => r.Number) + .Select(r => r.Number) + .FirstOrDefaultAsync(ct); + + var seq = 1; + if (lastNumber is not null && int.TryParse(lastNumber[prefix.Length..], out var last)) + seq = last + 1; + return $"{prefix}{seq:D6}"; + } + + private async Task GetInternal(Guid id, CancellationToken ct) + { + var row = await (from r in _db.SupplierReturns.AsNoTracking() + join cp in _db.Counterparties on r.SupplierId equals cp.Id + join st in _db.Stores on r.StoreId equals st.Id + join cu in _db.Currencies on r.CurrencyId equals cu.Id + where r.Id == id + select new { r, cp, st, cu }).FirstOrDefaultAsync(ct); + if (row is null) return null; + + string? refNumber = row.r.ReferenceSupplyId is null ? null + : await _db.Supplies.Where(s => s.Id == row.r.ReferenceSupplyId).Select(s => s.Number).FirstOrDefaultAsync(ct); + + var lines = await (from l in _db.SupplierReturnLines.AsNoTracking() + join p in _db.Products on l.ProductId equals p.Id + join u in _db.UnitsOfMeasure on p.UnitOfMeasureId equals u.Id + where l.SupplierReturnId == id + orderby l.SortOrder + select new SupplierReturnLineDto( + l.Id, l.ProductId, p.Name, p.Article, u.Name, + l.Quantity, l.UnitPrice, l.LineTotal, l.SortOrder, + _db.Stocks.Where(s => s.ProductId == l.ProductId && s.StoreId == row.r.StoreId) + .Select(s => (decimal?)s.Quantity).FirstOrDefault())) + .ToListAsync(ct); + + return new SupplierReturnDto( + row.r.Id, row.r.Number, row.r.Date, row.r.Status, + row.cp.Id, row.cp.Name, + row.st.Id, row.st.Name, + row.cu.Id, row.cu.Code, + row.r.ReferenceSupplyId, refNumber, + row.r.Notes, + row.r.Total, row.r.PostedAt, + lines); + } +} diff --git a/src/food-market.domain/Purchases/SupplierReturn.cs b/src/food-market.domain/Purchases/SupplierReturn.cs new file mode 100644 index 0000000..f6d6595 --- /dev/null +++ b/src/food-market.domain/Purchases/SupplierReturn.cs @@ -0,0 +1,58 @@ +using foodmarket.Domain.Catalog; +using foodmarket.Domain.Common; + +namespace foodmarket.Domain.Purchases; + +public enum SupplierReturnStatus +{ + Draft = 0, + Posted = 1, +} + +/// Возврат поставщику. Отгрузка товара обратно поставщику (брак, излишек, +/// неликвид). При проведении создаёт с типом +/// и отрицательным Quantity. +/// Опционально ссылается на исходную приёмку через . +public class SupplierReturn : TenantEntity +{ + public string Number { get; set; } = ""; + public DateTime Date { get; set; } = DateTime.UtcNow; + public SupplierReturnStatus Status { get; set; } = SupplierReturnStatus.Draft; + + public Guid SupplierId { get; set; } + public Counterparty Supplier { get; set; } = null!; + + public Guid StoreId { get; set; } + public Store Store { get; set; } = null!; + + public Guid CurrencyId { get; set; } + public Currency Currency { get; set; } = null!; + + /// Опциональная ссылка на исходную приёмку. + public Guid? ReferenceSupplyId { get; set; } + public Supply? ReferenceSupply { get; set; } + + public string? Notes { get; set; } + + public decimal Total { get; set; } + + public DateTime? PostedAt { get; set; } + public Guid? PostedByUserId { get; set; } + + public ICollection Lines { get; set; } = new List(); +} + +public class SupplierReturnLine : TenantEntity +{ + public Guid SupplierReturnId { get; set; } + public SupplierReturn SupplierReturn { get; set; } = null!; + + public Guid ProductId { get; set; } + public Product Product { get; set; } = null!; + + public decimal Quantity { get; set; } + public decimal UnitPrice { get; set; } + public decimal LineTotal { get; set; } + + public int SortOrder { get; set; } +} diff --git a/src/food-market.infrastructure/Persistence/AppDbContext.cs b/src/food-market.infrastructure/Persistence/AppDbContext.cs index fa6cf43..f31695c 100644 --- a/src/food-market.infrastructure/Persistence/AppDbContext.cs +++ b/src/food-market.infrastructure/Persistence/AppDbContext.cs @@ -47,6 +47,9 @@ public AppDbContext(DbContextOptions options, ITenantContext tenan public DbSet Enters => Set(); public DbSet EnterLines => Set(); + public DbSet SupplierReturns => Set(); + public DbSet SupplierReturnLines => Set(); + public DbSet Losses => Set(); public DbSet LossLines => Set(); diff --git a/src/food-market.infrastructure/Persistence/Configurations/PurchasesConfigurations.cs b/src/food-market.infrastructure/Persistence/Configurations/PurchasesConfigurations.cs index 593b16c..89c4ff3 100644 --- a/src/food-market.infrastructure/Persistence/Configurations/PurchasesConfigurations.cs +++ b/src/food-market.infrastructure/Persistence/Configurations/PurchasesConfigurations.cs @@ -66,5 +66,38 @@ public static void ConfigurePurchases(this ModelBuilder b) e.HasIndex(x => new { x.OrganizationId, x.ProductId }); }); + + b.Entity(e => + { + e.ToTable("supplier_returns"); + e.Property(x => x.Number).HasMaxLength(50).IsRequired(); + e.Property(x => x.Notes).HasMaxLength(1000); + e.Property(x => x.Total).HasPrecision(18, 4); + + e.HasOne(x => x.Supplier).WithMany().HasForeignKey(x => x.SupplierId).OnDelete(DeleteBehavior.Restrict); + e.HasOne(x => x.Store).WithMany().HasForeignKey(x => x.StoreId).OnDelete(DeleteBehavior.Restrict); + e.HasOne(x => x.Currency).WithMany().HasForeignKey(x => x.CurrencyId).OnDelete(DeleteBehavior.Restrict); + e.HasOne(x => x.ReferenceSupply).WithMany().HasForeignKey(x => x.ReferenceSupplyId).OnDelete(DeleteBehavior.Restrict); + + e.HasMany(x => x.Lines).WithOne(l => l.SupplierReturn).HasForeignKey(l => l.SupplierReturnId).OnDelete(DeleteBehavior.Cascade); + + e.HasIndex(x => new { x.OrganizationId, x.Number }).IsUnique(); + e.HasIndex(x => new { x.OrganizationId, x.Date }); + e.HasIndex(x => new { x.OrganizationId, x.Status }); + e.HasIndex(x => new { x.OrganizationId, x.SupplierId }); + e.HasIndex(x => x.ReferenceSupplyId); + }); + + b.Entity(e => + { + e.ToTable("supplier_return_lines"); + e.Property(x => x.Quantity).HasPrecision(18, 4); + e.Property(x => x.UnitPrice).HasPrecision(18, 4); + e.Property(x => x.LineTotal).HasPrecision(18, 4); + + e.HasOne(x => x.Product).WithMany().HasForeignKey(x => x.ProductId).OnDelete(DeleteBehavior.Restrict); + + e.HasIndex(x => new { x.OrganizationId, x.ProductId }); + }); } } diff --git a/src/food-market.infrastructure/Persistence/Migrations/20260528050000_Phase6f_SupplierReturns.cs b/src/food-market.infrastructure/Persistence/Migrations/20260528050000_Phase6f_SupplierReturns.cs new file mode 100644 index 0000000..0b4d796 --- /dev/null +++ b/src/food-market.infrastructure/Persistence/Migrations/20260528050000_Phase6f_SupplierReturns.cs @@ -0,0 +1,82 @@ +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using foodmarket.Infrastructure.Persistence; + +#nullable disable + +namespace foodmarket.Infrastructure.Persistence.Migrations +{ + /// Phase6f — возврат поставщику (SupplierReturn). + /// + /// Документ возврата товара поставщику. При проведении создаёт + /// stock_movements тип SupplierReturn с отрицательным Quantity. + /// Опционально ссылается на исходную приёмку. + [DbContext(typeof(AppDbContext))] + [Migration("20260528050000_Phase6f_SupplierReturns")] + public partial class Phase6f_SupplierReturns : Migration + { + protected override void Up(MigrationBuilder b) + { + b.Sql(@" + CREATE TABLE IF NOT EXISTS public.supplier_returns ( + ""Id"" uuid PRIMARY KEY, + ""OrganizationId"" uuid NOT NULL, + ""Number"" varchar(50) NOT NULL, + ""Date"" timestamp with time zone NOT NULL, + ""Status"" integer NOT NULL, + ""SupplierId"" uuid NOT NULL, + ""StoreId"" uuid NOT NULL, + ""CurrencyId"" uuid NOT NULL, + ""ReferenceSupplyId"" uuid, + ""Notes"" varchar(1000), + ""Total"" numeric(18,4) NOT NULL, + ""PostedAt"" timestamp with time zone, + ""PostedByUserId"" uuid, + ""CreatedAt"" timestamp with time zone NOT NULL, + ""UpdatedAt"" timestamp with time zone, + CONSTRAINT ""FK_supplier_returns_counterparties_SupplierId"" FOREIGN KEY (""SupplierId"") REFERENCES public.counterparties(""Id"") ON DELETE RESTRICT, + CONSTRAINT ""FK_supplier_returns_stores_StoreId"" FOREIGN KEY (""StoreId"") REFERENCES public.stores(""Id"") ON DELETE RESTRICT, + CONSTRAINT ""FK_supplier_returns_currencies_CurrencyId"" FOREIGN KEY (""CurrencyId"") REFERENCES public.currencies(""Id"") ON DELETE RESTRICT, + CONSTRAINT ""FK_supplier_returns_supplies_ReferenceSupplyId"" FOREIGN KEY (""ReferenceSupplyId"") REFERENCES public.supplies(""Id"") ON DELETE RESTRICT + ); + + CREATE UNIQUE INDEX IF NOT EXISTS ""IX_supplier_returns_OrganizationId_Number"" ON public.supplier_returns (""OrganizationId"", ""Number""); + CREATE INDEX IF NOT EXISTS ""IX_supplier_returns_OrganizationId_Date"" ON public.supplier_returns (""OrganizationId"", ""Date""); + CREATE INDEX IF NOT EXISTS ""IX_supplier_returns_OrganizationId_Status"" ON public.supplier_returns (""OrganizationId"", ""Status""); + CREATE INDEX IF NOT EXISTS ""IX_supplier_returns_OrganizationId_SupplierId"" ON public.supplier_returns (""OrganizationId"", ""SupplierId""); + CREATE INDEX IF NOT EXISTS ""IX_supplier_returns_StoreId"" ON public.supplier_returns (""StoreId""); + CREATE INDEX IF NOT EXISTS ""IX_supplier_returns_CurrencyId"" ON public.supplier_returns (""CurrencyId""); + CREATE INDEX IF NOT EXISTS ""IX_supplier_returns_ReferenceSupplyId"" ON public.supplier_returns (""ReferenceSupplyId""); + + CREATE TABLE IF NOT EXISTS public.supplier_return_lines ( + ""Id"" uuid PRIMARY KEY, + ""OrganizationId"" uuid NOT NULL, + ""SupplierReturnId"" uuid NOT NULL, + ""ProductId"" uuid NOT NULL, + ""Quantity"" numeric(18,4) NOT NULL, + ""UnitPrice"" numeric(18,4) NOT NULL, + ""LineTotal"" numeric(18,4) NOT NULL, + ""SortOrder"" integer NOT NULL, + ""CreatedAt"" timestamp with time zone NOT NULL, + ""UpdatedAt"" timestamp with time zone, + CONSTRAINT ""FK_supplier_return_lines_supplier_returns_SupplierReturnId"" + FOREIGN KEY (""SupplierReturnId"") REFERENCES public.supplier_returns(""Id"") ON DELETE CASCADE, + CONSTRAINT ""FK_supplier_return_lines_products_ProductId"" + FOREIGN KEY (""ProductId"") REFERENCES public.products(""Id"") ON DELETE RESTRICT + ); + + CREATE INDEX IF NOT EXISTS ""IX_supplier_return_lines_SupplierReturnId"" ON public.supplier_return_lines (""SupplierReturnId""); + CREATE INDEX IF NOT EXISTS ""IX_supplier_return_lines_ProductId"" ON public.supplier_return_lines (""ProductId""); + CREATE INDEX IF NOT EXISTS ""IX_supplier_return_lines_OrganizationId_ProductId"" ON public.supplier_return_lines (""OrganizationId"", ""ProductId""); + "); + } + + protected override void Down(MigrationBuilder b) + { + b.Sql(@" + DROP TABLE IF EXISTS public.supplier_return_lines; + DROP TABLE IF EXISTS public.supplier_returns; + "); + } + } +} diff --git a/src/food-market.web/src/App.tsx b/src/food-market.web/src/App.tsx index 5051e0a..f0084be 100644 --- a/src/food-market.web/src/App.tsx +++ b/src/food-market.web/src/App.tsx @@ -36,6 +36,8 @@ import { TransfersPage } from '@/pages/TransfersPage' import { TransferEditPage } from '@/pages/TransferEditPage' import { InventoriesPage } from '@/pages/InventoriesPage' import { InventoryEditPage } from '@/pages/InventoryEditPage' +import { SupplierReturnsPage } from '@/pages/SupplierReturnsPage' +import { SupplierReturnEditPage } from '@/pages/SupplierReturnEditPage' import { RetailSalesPage } from '@/pages/RetailSalesPage' import { RetailSaleEditPage } from '@/pages/RetailSaleEditPage' import { AppLayout } from '@/components/AppLayout' @@ -120,6 +122,9 @@ export default function App() { } /> } /> } /> + } /> + } /> + } /> } /> } /> } /> diff --git a/src/food-market.web/src/components/AppLayout.tsx b/src/food-market.web/src/components/AppLayout.tsx index f632481..f12aa7f 100644 --- a/src/food-market.web/src/components/AppLayout.tsx +++ b/src/food-market.web/src/components/AppLayout.tsx @@ -7,7 +7,7 @@ import { cn } from '@/lib/utils' import { LayoutDashboard, Package, FolderTree, Ruler, Tag, Users, Warehouse, Store as StoreIcon, LogOut, Download, UserCog, Shield, ShieldCheck, - Boxes, History, TruckIcon, ShoppingCart, Settings, Menu, X, PackagePlus, PackageMinus, ArrowRightLeft, ClipboardCheck, + Boxes, History, TruckIcon, ShoppingCart, Settings, Menu, X, PackagePlus, PackageMinus, ArrowRightLeft, ClipboardCheck, Undo2, } from 'lucide-react' import { Logo } from './Logo' import { SuperAdminAsOrgBanner } from './SuperAdminAsOrgBanner' @@ -97,6 +97,7 @@ function buildNav(roles: string[]): NavSection[] { if (isAdmin || isStorekeeper) { sections.push({ group: 'Закупки', items: [ { to: '/purchases/supplies', icon: TruckIcon, label: 'Приёмки' }, + { to: '/purchases/supplier-returns', icon: Undo2, label: 'Возвраты поставщикам' }, ]}) } diff --git a/src/food-market.web/src/lib/types.ts b/src/food-market.web/src/lib/types.ts index 7c9d8b2..e758db1 100644 --- a/src/food-market.web/src/lib/types.ts +++ b/src/food-market.web/src/lib/types.ts @@ -224,6 +224,36 @@ export interface InventoryDto { lines: InventoryLineDto[]; } +export const SupplierReturnStatus = { Draft: 0, Posted: 1 } as const +export type SupplierReturnStatus = (typeof SupplierReturnStatus)[keyof typeof SupplierReturnStatus] + +export interface SupplierReturnListRow { + id: string; number: string; date: string; status: SupplierReturnStatus; + supplierId: string; supplierName: string; + storeId: string; storeName: string; + currencyId: string; currencyCode: string; + total: number; lineCount: number; postedAt: string | null; + referenceSupplyId: string | null; referenceSupplyNumber: string | null; +} + +export interface SupplierReturnLineDto { + id: string | null; productId: string; + productName: string | null; productArticle: string | null; unitSymbol: string | null; + quantity: number; unitPrice: number; lineTotal: number; sortOrder: number; + stockAtStore: number | null; +} + +export interface SupplierReturnDto { + id: string; number: string; date: string; status: SupplierReturnStatus; + supplierId: string; supplierName: string; + storeId: string; storeName: string; + currencyId: string; currencyCode: string; + referenceSupplyId: string | null; referenceSupplyNumber: string | null; + notes: string | null; + total: number; postedAt: string | null; + lines: SupplierReturnLineDto[]; +} + export const RetailSaleStatus = { Draft: 0, Posted: 1 } as const export type RetailSaleStatus = (typeof RetailSaleStatus)[keyof typeof RetailSaleStatus] diff --git a/src/food-market.web/src/pages/SupplierReturnEditPage.tsx b/src/food-market.web/src/pages/SupplierReturnEditPage.tsx new file mode 100644 index 0000000..5a2ca64 --- /dev/null +++ b/src/food-market.web/src/pages/SupplierReturnEditPage.tsx @@ -0,0 +1,366 @@ +import { useState, useEffect, type FormEvent } from 'react' +import { useNavigate, useParams, Link } from 'react-router-dom' +import { useQuery, useQueryClient, useMutation } from '@tanstack/react-query' +import { ArrowLeft, Plus, Trash2, Save, CheckCircle } from 'lucide-react' +import { api } from '@/lib/api' +import { Button } from '@/components/Button' +import { Field, TextArea, Select, AsyncSelect, MoneyInput, NumberInput, Checkbox } from '@/components/Field' +import { DateField } from '@/components/DateField' +import { ProductPicker } from '@/components/ProductPicker' +import { useStores, useCurrencies } from '@/lib/useLookups' +import { useOrgSettings } from '@/lib/useOrgSettings' +import { SupplierReturnStatus, type SupplierReturnDto, type Product } from '@/lib/types' + +interface LineRow { + productId: string + productName: string + productArticle: string | null + unitSymbol: string | null + quantity: number + unitPrice: number + stockAtStore: number | null +} + +interface Form { + date: string + supplierId: string + storeId: string + currencyId: string + referenceSupplyId: string | null + notes: string + lines: LineRow[] +} + +const todayIso = () => { + const d = new Date() + return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}` +} + +const emptyForm: Form = { + date: todayIso(), supplierId: '', storeId: '', currencyId: '', + referenceSupplyId: null, notes: '', lines: [], +} + +export function SupplierReturnEditPage() { + const { id } = useParams<{ id: string }>() + const isNew = !id || id === 'new' + const navigate = useNavigate() + const qc = useQueryClient() + + const stores = useStores() + const currencies = useCurrencies() + const org = useOrgSettings() + + const [form, setForm] = useState
(emptyForm) + const [pickerOpen, setPickerOpen] = useState(false) + const [error, setError] = useState(null) + + const existing = useQuery({ + queryKey: ['/api/purchases/supplier-returns', id], + queryFn: async () => (await api.get(`/api/purchases/supplier-returns/${id}`)).data, + enabled: !isNew, + }) + + useEffect(() => { + if (!isNew && existing.data) { + const s = existing.data + setForm({ + date: s.date.slice(0, 10), + supplierId: s.supplierId, + storeId: s.storeId, + currencyId: s.currencyId, + referenceSupplyId: s.referenceSupplyId, + notes: s.notes ?? '', + lines: s.lines.map((l) => ({ + productId: l.productId, + productName: l.productName ?? '', + productArticle: l.productArticle, + unitSymbol: l.unitSymbol, + quantity: l.quantity, + unitPrice: l.unitPrice, + stockAtStore: l.stockAtStore, + })), + }) + } + }, [isNew, existing.data]) + + useEffect(() => { + if (isNew) { + if (!form.storeId && stores.data?.length) { + const main = stores.data.find((s) => s.isMain) ?? stores.data[0] + setForm((f) => ({ ...f, storeId: main.id })) + } + if (!form.currencyId && currencies.data?.length) { + const def = org.data?.defaultCurrencyId + ? currencies.data.find((c) => c.id === org.data!.defaultCurrencyId) + : currencies.data.find((c) => c.code === 'KZT') + if (def) setForm((f) => ({ ...f, currencyId: def.id })) + } + } + }, [isNew, stores.data, currencies.data, org.data?.defaultCurrencyId, form.storeId, form.currencyId]) + + const isDraft = isNew || existing.data?.status === SupplierReturnStatus.Draft + const isPosted = existing.data?.status === SupplierReturnStatus.Posted + + const lineTotal = (l: LineRow) => l.quantity * l.unitPrice + const grandTotal = form.lines.reduce((s, l) => s + lineTotal(l), 0) + + const save = useMutation({ + mutationFn: async () => { + const payload = { + date: new Date(form.date).toISOString(), + supplierId: form.supplierId, + storeId: form.storeId, + currencyId: form.currencyId, + referenceSupplyId: form.referenceSupplyId, + notes: form.notes || null, + lines: form.lines.map((l) => ({ + productId: l.productId, quantity: l.quantity, unitPrice: l.unitPrice, + })), + } + if (isNew) return (await api.post('/api/purchases/supplier-returns', payload)).data + await api.put(`/api/purchases/supplier-returns/${id}`, payload) + return null + }, + onSuccess: (created) => { + qc.invalidateQueries({ queryKey: ['/api/purchases/supplier-returns'] }) + navigate(created ? `/purchases/supplier-returns/${created.id}` : `/purchases/supplier-returns/${id}`) + }, + onError: (e: Error) => setError(e.message), + }) + + const post = useMutation({ + mutationFn: async () => { await api.post(`/api/purchases/supplier-returns/${id}/post`) }, + onSuccess: () => { + qc.invalidateQueries({ queryKey: ['/api/purchases/supplier-returns'] }) + qc.invalidateQueries({ queryKey: ['/api/inventory/stock'] }) + qc.invalidateQueries({ queryKey: ['/api/inventory/movements'] }) + existing.refetch() + }, + onError: (e: Error) => { + const msg = (e as { response?: { data?: { error?: string } } }).response?.data?.error ?? e.message + setError(msg) + }, + }) + + const unpost = useMutation({ + mutationFn: async () => { await api.post(`/api/purchases/supplier-returns/${id}/unpost`) }, + onSuccess: () => { + qc.invalidateQueries({ queryKey: ['/api/purchases/supplier-returns'] }) + qc.invalidateQueries({ queryKey: ['/api/inventory/stock'] }) + existing.refetch() + }, + onError: (e: Error) => setError(e.message), + }) + + const remove = useMutation({ + mutationFn: async () => { await api.delete(`/api/purchases/supplier-returns/${id}`) }, + onSuccess: () => navigate('/purchases/supplier-returns'), + onError: (e: Error) => setError(e.message), + }) + + const onSubmit = (e: FormEvent) => { e.preventDefault(); save.mutate() } + + const addLineFromProduct = (p: Product) => { + setForm({ + ...form, + lines: [...form.lines, { + productId: p.id, + productName: p.name, + productArticle: p.article, + unitSymbol: p.unitName, + quantity: 1, + unitPrice: p.cost ?? p.referencePrice ?? 0, + stockAtStore: null, + }], + }) + } + const updateLine = (i: number, patch: Partial) => + setForm({ ...form, lines: form.lines.map((l, ix) => ix === i ? { ...l, ...patch } : l) }) + const removeLine = (i: number) => + setForm({ ...form, lines: form.lines.filter((_, ix) => ix !== i) }) + + const canSave = !!form.date && !!form.supplierId && !!form.storeId && !!form.currencyId + && form.lines.length > 0 && isDraft + + const fractional = org.data?.allowFractionalPrices ?? false + const moneyFmt = fractional + ? { minimumFractionDigits: 2, maximumFractionDigits: 2 } + : { maximumFractionDigits: 0 } + + return ( + +
+
+ + + +
+

+ {isNew ? 'Новый возврат поставщику' : existing.data?.number ?? 'Возврат поставщику'} +

+

+ {isPosted + ? Проведён {existing.data?.postedAt ? new Date(existing.data.postedAt).toLocaleString('ru') : ''} + : 'Черновик — товар не списан, пока не проведёшь'} +

+
+
+
+ {isDraft && !isNew && ( + + )} + {isDraft && ( + + )} +
+
+ +
+
+ {error && ( +
{error}
+ )} + +
+
+ + setForm({ ...form, date: iso ?? '' })} /> + + + setForm({ ...form, supplierId: v })} + placeholder="Выберите поставщика…" + /> + + + + + {org.data?.multiCurrencyEnabled && ( + + + + )} + +