NTP_V2/src/NTP.Application/Refinery/Trips/TripQueryService.cs

84 lines
4.6 KiB
C#

using NTP.Application.Common;
using NTP.Application.Refinery.RemainingBalance;
namespace NTP.Application.Refinery.Trips;
public interface ITripQueryService
{
Task<PagedResult<TripGridDto>> SearchAsync(TripQuery query, PageRequest page, CancellationToken ct);
Task<TripDetailDto> GetAsync(long id, CancellationToken ct);
Task<PagedResult<TripLogDto>> LogAsync(long id, PageRequest page, CancellationToken ct);
}
/// <summary>Port of refinerytripFactory.search / getREFINERYTRIP / log (grid, trip form, history popup).</summary>
public sealed class TripQueryService(
ITripRepository trips,
ITripAmountsService amounts) : ITripQueryService
{
public static void ValidateRanges(TripQuery q)
{
if (q.LoadingDateFrom > q.LoadingDateTo || q.PaymentedDateFrom > q.PaymentedDateTo) throw TripErrors.FromAfterTo();
}
public async Task<PagedResult<TripGridDto>> SearchAsync(TripQuery query, PageRequest page, CancellationToken ct)
{
ValidateRanges(query);
var r = await trips.SearchAsync(query, page, ct);
var items = r.Items.Select(x => new TripGridDto(
x.Id, x.Pn, x.DispatchDate, x.LoadingNo, x.DischargeAuthNo, x.DischargeEntryDate, x.Tn, x.Trn, x.Nnpn,
x.WageClaimId, x.WageClaimNo, x.StatusCode, x.DieselCharge, x.HavePaymentMethod != "0",
(x.WaterAmount ?? 0) != 0, x.ReversePayment == 1, x.NetWage)).ToList();
return new PagedResult<TripGridDto>(items, r.Page, r.PageSize, r.TotalCount);
}
public async Task<TripDetailDto> GetAsync(long id, CancellationToken ct)
{
var t = await trips.GetAsync(id, ct) ?? throw new NotFoundException("RefineryTrip", id);
var status = t.StatusCode ?? string.Empty;
// DISCHARGED / READY_TO_PAY: figures are recomputed from today's variables (factory 129-134)
decimal? netWage = t.NetWage, tripNetWage = t.TripNetWage, remaining = t.RemainingBalance;
if (TripStatus.RecalculateAmounts.Contains(status) && t.LoadingDate is not null)
{
var a = await amounts.ComputeAsync(t, ct);
netWage = a.NetWage;
tripNetWage = a.TripNetWage;
remaining = a.RemainingBalance;
}
// CalculateRefinaryDiesel is both the "new balance" and, for the late statuses, the second payment value shown on the form
var newBalance = await trips.CalculateDieselAsync(id, ct);
var second = TripStatus.ShowSecondPayment.Contains(status) ? newBalance : null;
var first = await trips.GetFirstPaymentValueAsync(ct);
var audit = await trips.GetAuditModeAsync(ct);
var wallet = t.Tn is null ? null : await trips.GetActiveWalletByTruckAsync(t.Tn, ct);
// a trip that was already paid shows the wallet it was paid to (factory 172-182)
if (t.RefineryWalletId is { } wid) wallet = await trips.GetWalletAsync(wid, ct) ?? wallet;
var walletDto = wallet is null
? new TripWalletDto(null, null, null, null, null, null, null, t.DriverWalletNumber)
: new TripWalletDto(wallet.Epayment, wallet.WalletNumber, wallet.OwnerNnpn?.ToString(), wallet.OwnerName,
wallet.BankAccount, wallet.Iban, wallet.BankName, t.DriverWalletNumber);
var hasTransfer = (status is TripStatus.Discharged or TripStatus.PendingExchange) && await trips.HasTransferLogAsync(id, ct);
var actions = TripActionRules.Available(status, t.DieselCharge, wallet?.Epayment, audit, hasTransfer);
return new TripDetailDto(
t.Id, t.Pn, t.StatusCode, t.DispatchDate, t.ActualLoadingDate, t.ClientName, t.Tn, t.Trn, t.Nnpn, t.Drvr, t.LoadingNo,
t.GrossWeight, t.EmptyWeight, t.NetLoadingWeight, t.LitersWeight, t.DischargeAuthNo, t.DischargeEntryDate,
t.ActualGrossWeight, t.ActualEmptyWeight, t.NetDischargeWeight, t.WaterAmount, t.SedimentAmount, t.WeightDifference,
netWage, tripNetWage, remaining, t.DieselCharge, t.ReversePayment == 1, t.CardNumber, t.CurrentBalance, newBalance,
first, second, t.WageClaimId, t.WageClaimNo, t.Remarks, t.ReceivedByName,
t.ReceivedDate?.ToString("yyyy-MM-dd HH:mm"), t.PaymentedDate?.ToString("yyyy-MM-dd HH:mm"), t.PaymentOptions,
t.VoucherSerialNumber, audit, walletDto, actions);
}
public async Task<PagedResult<TripLogDto>> LogAsync(long id, PageRequest page, CancellationToken ct)
{
var r = await trips.SearchLogAsync(id, page, ct);
return new PagedResult<TripLogDto>(
r.Items.Select(x => new TripLogDto(x.Id, x.MethodName, x.LogDate, x.Login, x.OldData, x.NewData)).ToList(),
r.Page, r.PageSize, r.TotalCount);
}
}