using NTP.Application.Common;
namespace NTP.Application.Refinery.Trips;
/// Configuration of the trips slice (section "Refinery:Trips").
public sealed class TripOptions
{
public const string SectionName = "Refinery:Trips";
///
/// Largest net wage a payment action accepts (legacy application.REFINERY_MAX_PAYMENT_AMOUNT, defined outside the repository;
/// the caption ERROR.PAYMENT_ISNOT_ALLOW_FOR_BIG_AMOUNT says 500 dinars). UNVERIFIED default.
///
public decimal MaxPaymentAmount { get; set; } = 500m;
}
/// Reads (Application has no Options package; implemented in Infrastructure).
public interface ITripSettingsService
{
TripOptions Value { get; }
}
public sealed record TripLock(string? StatusCode, int DieselCharge);
public sealed record CardInfo(long Id, decimal CurrentBalance);
/// Data access of the refinery trips (REFINERY_TRIPS and its view, cards, logs, DB functions).
public interface ITripRepository
{
// ---- reads
Task> SearchAsync(TripQuery query, PageRequest page, CancellationToken ct = default);
Task GetAsync(long id, CancellationToken ct = default);
/// SELECT ... FOR UPDATE of the trip (inside a transaction): serialises two operators acting on the same trip.
Task LockAsync(long id, CancellationToken ct = default);
Task GetActiveWalletByTruckAsync(string tn, CancellationToken ct = default);
Task GetWalletAsync(long walletId, CancellationToken ct = default);
/// REFINERY_VARIABLES AUTDIT_MODE = 1 (legacy checkAuditMode).
Task GetAuditModeAsync(CancellationToken ct = default);
/// FIRST_ADVANCE_PAYMENT valid today (legacy getFirstPaymentValue).
Task GetFirstPaymentValueAsync(CancellationToken ct = default);
/// All KEY_NAME / KEY_VALUE of the variables valid on the (truncated) loading date.
Task>> GetVariablesAsync(DateTime loadingDate, CancellationToken ct = default);
/// WATER_FLAG of the trip, or null when the column does not exist yet in this database / is empty.
Task GetWaterFlagAsync(long id, CancellationToken ct = default);
/// CalculateRefinaryDiesel(id); null when the function finds no price (ORA-01403).
Task CalculateDieselAsync(long id, CancellationToken ct = default);
/// CalculateRefinaryAmount(id); null when the function finds no price (ORA-01403).
Task CalculateAmountAsync(long id, CancellationToken ct = default);
Task HasTransferLogAsync(long id, CancellationToken ct = default);
Task> GetTripsOfPeriodAsync(DateTime from, DateTime to, CancellationToken ct = default);
// ---- diesel cards
Task GetActiveCardAsync(string tn, CancellationToken ct = default);
Task GetLastCardLogAmountAsync(string pn, string methodName, CancellationToken ct = default);
Task InsertCardLogAsync(long cardId, string methodName, decimal before, decimal after, decimal amount, string userId, string pn, CancellationToken ct = default);
Task UpdateCardBalanceAsync(long cardId, decimal balance, string userId, long tripId, CancellationToken ct = default);
// ---- writes on the trip
Task MarkFirstAdvanceAsync(long id, string userId, CancellationToken ct = default);
Task MarkSecondAdvanceAsync(long id, string userId, decimal secondPaymentValue, CancellationToken ct = default);
Task MarkAdvanceReversedAsync(long id, string userId, int dieselCharge, CancellationToken ct = default);
/// Legacy NetWageAmount: stores the computed figures on the trip.
Task SaveAmountsAsync(long id, string userId, TripAmounts amounts, CancellationToken ct = default);
/// RECEIPT (READY_TO_PAY) and EXCHANGE_AUDIT (PENDING_EXCHANGE): new status + received by / date.
Task SetStatusReceivedAsync(long id, string userId, string status, CancellationToken ct = default);
/// Status EXCHANGED. also fills RECEIVED_BY/DATE (not done by the bank-transfer PAYMENT).
Task MarkExchangedAsync(long id, string userId, string? remarks, string? paymentOptions, bool setReceived, long? walletId, CancellationToken ct = default);
Task BumpDinarakReferenceAsync(long id, string userId, CancellationToken ct = default);
Task SetVoucherSerialAsync(long id, long serial, CancellationToken ct = default);
/// Legacy DAO.CONFIRM_EXCHANGE: EXCHANGED with wallet / user / date taken from the last Dinarak log and EXCHANGE log rows.
Task ConfirmExchangeAsync(long id, string userId, CancellationToken ct = default);
// ---- history
Task InsertLogAsync(long tripId, string methodName, string userId, string oldJson, string newJson, CancellationToken ct = default);
Task> SearchLogAsync(long tripId, PageRequest page, CancellationToken ct = default);
// ---- reports
Task> ReportAsync(TripQuery query, CancellationToken ct = default);
Task> DieselReportAsync(DieselReportQuery query, CancellationToken ct = default);
Task> CardsAsync(string? tn, CancellationToken ct = default);
Task> BankReportAsync(BankReportQuery query, CancellationToken ct = default);
}
/// Voucher serial of the cash voucher (NTP.SYSTEM_VARIABLES VOUCHER_SERIAL_NO, legacy GenerateSerialNumber('VOUCHER')). NTP-schema connection.
public interface IVoucherSerialRepository
{
/// Takes the next number (locks the row until commit) and returns {yyyy}{seq:00000} as a number.
Task NextAsync(CancellationToken ct = default);
}