using CashLineERP.SharedKernel.Tax;

namespace CashLineERP.Domain.Procurement;

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

    public long PurchaseOrderId { get; private set; }

    public int LineNumber { get; private set; }

    public long ItemId { get; private set; }

    public long WarehouseId { get; private set; }

    public decimal OrderedQuantity { get; private set; }

    public decimal ReceivedQuantity { get; private set; }

    public string UnitOfMeasureCode { get; private set; } = string.Empty;

    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 PurchaseOrderLine(
        long purchaseOrderId,
        int lineNumber,
        long itemId,
        long warehouseId,
        decimal orderedQuantity,
        string unitOfMeasureCode,
        decimal unitPrice,
        string? taxCode)
    {
        PurchaseOrderId = purchaseOrderId;
        LineNumber = lineNumber;
        ItemId = itemId;
        WarehouseId = warehouseId;
        OrderedQuantity = orderedQuantity;
        UnitOfMeasureCode = unitOfMeasureCode;
        UnitPrice = unitPrice;
        TaxCode = taxCode;
        LineNetAmount = orderedQuantity * unitPrice;
        LineTaxAmount = StatutoryTaxPolicy.CalculateTax(LineNetAmount, TaxCode);
        LineTotalAmount = LineNetAmount + LineTaxAmount;
    }

    private PurchaseOrderLine()
    {
    }

    public void RegisterReceipt(decimal acceptedQuantity)
    {
        ReceivedQuantity += acceptedQuantity;
    }
}
