using CashLineERP.SharedKernel.Tax;

namespace CashLineERP.Domain.Procurement;

public sealed class AccountsPayableBillLine
{
    public long Id { get; private set; }

    public long AccountsPayableBillId { get; private set; }

    public int LineNumber { get; private set; }

    public long? PurchaseOrderLineId { get; private set; }

    public long? ItemId { get; private set; }

    public decimal? QuantityBilled { get; private set; }

    public string? UnitOfMeasureCode { get; private set; }

    public decimal UnitPrice { get; private set; }

    public string? TaxCode { get; private set; }

    public decimal LineNetAmount { get; private set; }

    public decimal LineTaxAmount { get; private set; }

    public decimal LineTotalAmount { get; private set; }

    public AccountsPayableBillLine(
        long accountsPayableBillId,
        int lineNumber,
        long? purchaseOrderLineId,
        long? itemId,
        decimal? quantityBilled,
        string? unitOfMeasureCode,
        decimal unitPrice,
        string? taxCode)
    {
        AccountsPayableBillId = accountsPayableBillId;
        LineNumber = lineNumber;
        PurchaseOrderLineId = purchaseOrderLineId;
        ItemId = itemId;
        QuantityBilled = quantityBilled;
        UnitOfMeasureCode = unitOfMeasureCode;
        UnitPrice = unitPrice;
        TaxCode = taxCode;
        LineNetAmount = (quantityBilled ?? 0m) * unitPrice;
        LineTaxAmount = StatutoryTaxPolicy.CalculateTax(LineNetAmount, TaxCode);
        LineTotalAmount = LineNetAmount + LineTaxAmount;
    }

    private AccountsPayableBillLine()
    {
    }
}
