123 lines
6.5 KiB
C#
123 lines
6.5 KiB
C#
using Leiren.Erp.Application.Billing;
|
|
using Leiren.Erp.Contracts;
|
|
|
|
namespace Leiren.Erp.Api;
|
|
|
|
public static class BillingEndpoints
|
|
{
|
|
public static IEndpointRouteBuilder MapBillingEndpoints(this IEndpointRouteBuilder app)
|
|
{
|
|
var notices = app.MapGroup("/api/billing/notices");
|
|
|
|
notices.MapGet("/", (Guid? shippingBatchId, Guid? purchaseContractId, Guid? supplierId, string? status,
|
|
string? search, int? page, int? pageSize, BillingService service, CancellationToken ct) =>
|
|
Handle(async () => Results.Ok(await service.ListNoticesAsync(
|
|
shippingBatchId, purchaseContractId, supplierId, status, search, page ?? 1, pageSize ?? 50, ct))));
|
|
|
|
notices.MapGet("/{id:guid}", (Guid id, BillingService service, CancellationToken ct) =>
|
|
Handle(async () => Results.Ok(await service.GetNoticeAsync(id, ct))));
|
|
|
|
notices.MapPost("/", (CreateSupplierInvoiceNoticeRequest request, BillingService service, CancellationToken ct) =>
|
|
Handle(async () =>
|
|
{
|
|
var created = await service.CreateNoticeAsync(request, ct);
|
|
return Results.Created($"/api/billing/notices/{created.Id}", created);
|
|
}));
|
|
|
|
// One notice per supplier from a confirmed shipping batch.
|
|
notices.MapPost("/generate-from-shipping-batch", (GenerateBillingNoticesRequest request, BillingService service, CancellationToken ct) =>
|
|
Handle(async () => Results.Ok(await service.GenerateFromShippingBatchAsync(request, ct))));
|
|
|
|
notices.MapPut("/{id:guid}", (Guid id, UpdateSupplierInvoiceNoticeRequest request, BillingService service, CancellationToken ct) =>
|
|
Handle(async () => Results.Ok(await service.UpdateNoticeAsync(id, request, ct))));
|
|
|
|
notices.MapPost("/{id:guid}/notify", (Guid id, BillingService service, CancellationToken ct) =>
|
|
Handle(async () => Results.Ok(await service.NotifyAsync(id, ct))));
|
|
|
|
notices.MapPost("/{id:guid}/await-invoice", (Guid id, BillingService service, CancellationToken ct) =>
|
|
Handle(async () => Results.Ok(await service.AwaitInvoiceAsync(id, ct))));
|
|
|
|
notices.MapPost("/{id:guid}/void", (Guid id, VoidBillingRequest? request, BillingService service, CancellationToken ct) =>
|
|
Handle(async () =>
|
|
{
|
|
await service.VoidNoticeAsync(id, request, ct);
|
|
return Results.NoContent();
|
|
}));
|
|
|
|
notices.MapGet("/{id:guid}/contract", (Guid id, BillingService service, CancellationToken ct) =>
|
|
Handle(async () => Results.Ok(await service.GetBillingContractAsync(id, ct))));
|
|
|
|
notices.MapPut("/{id:guid}/contract", (Guid id, UpdateSupplierBillingContractRequest request, BillingService service, CancellationToken ct) =>
|
|
Handle(async () => Results.Ok(await service.UpdateBillingContractAsync(id, request, ct))));
|
|
|
|
// documentKind: SupplierInvoiceNotice (开票通知) or SupplierBillingContract (开票购销合同).
|
|
notices.MapGet("/{id:guid}/documents/{documentKind}", (Guid id, string documentKind, BillingDocumentExportService service, CancellationToken ct) =>
|
|
Handle(async () =>
|
|
{
|
|
var (bytes, fileName) = await service.RenderAsync(id, documentKind, ct);
|
|
return Results.File(bytes, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", fileName);
|
|
}));
|
|
|
|
// Batch level: split quantities between the batch's suppliers, then confirm all notices at once.
|
|
notices.MapPut("/shipping-batches/{batchId:guid}/allocation", (Guid batchId, ReallocateBillingRequest request, BillingService service, CancellationToken ct) =>
|
|
Handle(async () => Results.Ok(await service.ReallocateBatchAsync(batchId, request, ct))));
|
|
notices.MapPost("/shipping-batches/{batchId:guid}/confirm", (Guid batchId, BillingService service, CancellationToken ct) =>
|
|
Handle(async () => Results.Ok(await service.ConfirmBatchAsync(batchId, ct))));
|
|
|
|
notices.MapPost("/{id:guid}/invoices", (Guid id, RegisterSupplierInvoiceRequest request, BillingService service, CancellationToken ct) =>
|
|
Handle(async () =>
|
|
{
|
|
var created = await service.RegisterInvoiceAsync(id, request, ct);
|
|
return Results.Created($"/api/billing/invoices/{created.Id}", created);
|
|
}));
|
|
|
|
var invoices = app.MapGroup("/api/billing/invoices");
|
|
|
|
invoices.MapGet("/", (Guid? noticeId, string? status, string? search, int? page, int? pageSize,
|
|
BillingService service, CancellationToken ct) =>
|
|
Handle(async () => Results.Ok(await service.ListInvoicesAsync(noticeId, status, search, page ?? 1, pageSize ?? 50, ct))));
|
|
|
|
invoices.MapGet("/{id:guid}", (Guid id, BillingService service, CancellationToken ct) =>
|
|
Handle(async () => Results.Ok(await service.GetInvoiceAsync(id, ct))));
|
|
|
|
invoices.MapPost("/{id:guid}/hand-to-finance", (Guid id, HandSupplierInvoiceToFinanceRequest? request, BillingService service, CancellationToken ct) =>
|
|
Handle(async () => Results.Ok(await service.HandInvoiceToFinanceAsync(id, request, ct))));
|
|
|
|
invoices.MapPost("/{id:guid}/complete", (Guid id, BillingService service, CancellationToken ct) =>
|
|
Handle(async () => Results.Ok(await service.CompleteInvoiceAsync(id, ct))));
|
|
|
|
invoices.MapPost("/{id:guid}/void", (Guid id, VoidBillingRequest? request, BillingService service, CancellationToken ct) =>
|
|
Handle(async () =>
|
|
{
|
|
await service.VoidInvoiceAsync(id, request, ct);
|
|
return Results.NoContent();
|
|
}));
|
|
|
|
return app;
|
|
}
|
|
|
|
private static async Task<IResult> Handle(Func<Task<IResult>> action)
|
|
{
|
|
try
|
|
{
|
|
return await action();
|
|
}
|
|
catch (BillingValidationException ex)
|
|
{
|
|
return Results.ValidationProblem(ex.Errors);
|
|
}
|
|
catch (BillingNotFoundException ex)
|
|
{
|
|
return Results.Problem(title: "资源不存在", detail: ex.Message, statusCode: StatusCodes.Status404NotFound);
|
|
}
|
|
catch (BillingConflictException ex)
|
|
{
|
|
return Results.Problem(title: "操作冲突", detail: ex.Message, statusCode: StatusCodes.Status409Conflict);
|
|
}
|
|
catch (InvalidOperationException ex)
|
|
{
|
|
return Results.Problem(title: "操作冲突", detail: ex.Message, statusCode: StatusCodes.Status409Conflict);
|
|
}
|
|
}
|
|
}
|