Rework so employee dont use repos but servies
This commit is contained in:
74
PointOfSale/Pos.Service/SaleHistoryService.cs
Normal file
74
PointOfSale/Pos.Service/SaleHistoryService.cs
Normal file
@@ -0,0 +1,74 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user