Files
BealeEngineering/BealeEngineering/BealeEngineering.Core/Model/Sale/Invoice.cs

108 lines
3.4 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BealeEngineering.Core.Model.Sale
{
public class Invoice : InvoiceHeader
{
public List<InvoiceLine> InvoiceLineList { get; set; } = new List<InvoiceLine>();
public bool InvoiceLineListIsSet
{
get
{
if (InvoiceLineList == null || !InvoiceLineList.Any()) { return false; }
else { return true; }
}
}
public class InvoiceLine
{
[Required(), Range(0, 255)]
public int LineNumber { get; set; }
[Required(), StringLength(50)]
public string InventoryItemCode { get; set; }
[StringLength(500)]
public string Description { get; set; }
[Required(), Range(0, 255)]
public decimal Quantity { get; set; }
[Required()]
public decimal UnitAmount { get; set; }
[Range(1, 100)]
public int? Discount { get; set; }
[Required(), StringLength(10)]
public string AccountCode { get; set; }
[Required(), StringLength(50)]
public string TaxType { get; set; }
[Required()]
public decimal TaxAmount { get; set; }
/// <summary>
/// Line amount is Tax Exclusive
/// </summary>
public decimal LineAmount
{
get
{
int discount;
if (Discount == null || Discount == 0) { discount = 100; }
else { discount = (int)Discount; }
return decimal.Round(((Quantity * UnitAmount) * (discount / 100m)),2, MidpointRounding.AwayFromZero);
}
}
}
public override IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
base.Validate(validationContext);
// additional validation
if (!InvoiceLineListIsSet)
{
var result = new ValidationResult("Invoice must have at least one line.");
ValidationResults.Add(result);
}
else
{
decimal lineTotal = 0;
decimal lineTaxTotal = 0;
foreach (var line in InvoiceLineList)
{
// checks on each line
if (line.Quantity <= 0)
{
var result = new ValidationResult("Quantity must be greater than zero.");
ValidationResults.Add(result);
}
lineTotal = lineTotal + line.LineAmount;
lineTaxTotal = lineTaxTotal + line.TaxAmount;
}
if (lineTotal + lineTaxTotal != InvoiceTotal)
{
var result = new ValidationResult("Line total is not equal to Invoice Total.");
ValidationResults.Add(result);
}
if (lineTaxTotal != TaxTotal)
{
var result = new ValidationResult("Line tax total is not equal to Invoice Tax Total.");
ValidationResults.Add(result);
}
}
return ValidationResults;
}
}
}