75 lines
2.1 KiB
C#
75 lines
2.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Database.Models;
|
|
using Database.Repository;
|
|
using Pos.Models;
|
|
|
|
namespace Pos.Service
|
|
{
|
|
public class SaleHistoryService
|
|
{
|
|
private readonly SaleRepository _saleRepository =
|
|
new SaleRepository();
|
|
|
|
public List<ReceiptSummaryModel> Search(
|
|
DateTime startDate,
|
|
DateTime endDate)
|
|
{
|
|
if (endDate.Date < startDate.Date)
|
|
throw new ArgumentException(
|
|
"Slutdatoen må ikke ligge før startdatoen.");
|
|
|
|
List<SaleEntity> sales =
|
|
_saleRepository.GetByDateRange(startDate, endDate);
|
|
|
|
List<ReceiptSummaryModel> receipts =
|
|
new List<ReceiptSummaryModel>();
|
|
|
|
foreach (SaleEntity sale in sales)
|
|
{
|
|
decimal total = _saleRepository
|
|
.GetSaleLineBySaleId(sale.Id)
|
|
.Sum(line => line.Total);
|
|
|
|
receipts.Add(new ReceiptSummaryModel
|
|
{
|
|
SaleId = sale.Id,
|
|
SaleTime = sale.Time,
|
|
Total = total
|
|
});
|
|
}
|
|
|
|
return receipts;
|
|
}
|
|
|
|
public ReceiptDetailModel GetReceipt(int saleId)
|
|
{
|
|
if (saleId <= 0)
|
|
throw new ArgumentOutOfRangeException(nameof(saleId));
|
|
|
|
List<SaleLineEntity> saleLines =
|
|
_saleRepository.GetSaleLineBySaleId(saleId);
|
|
|
|
ReceiptDetailModel receipt = new ReceiptDetailModel
|
|
{
|
|
SaleId = saleId
|
|
};
|
|
|
|
foreach (SaleLineEntity saleLine in saleLines)
|
|
{
|
|
receipt.Lines.Add(new ReceiptLineModel
|
|
{
|
|
Product = saleLine.Product,
|
|
Pieces = saleLine.Pieces,
|
|
Price = saleLine.Price,
|
|
Total = saleLine.Total
|
|
});
|
|
}
|
|
|
|
receipt.Total = receipt.Lines.Sum(line => line.Total);
|
|
return receipt;
|
|
}
|
|
}
|
|
}
|