92 lines
3.0 KiB
C#
92 lines
3.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using gehGassi.Core.Interfaces;
|
|
using gehGassi.Domain.Shop;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace gehGassi.Core.Services
|
|
{
|
|
/// <summary>
|
|
/// Service der die Verwaltung von Rechnungen ermöglicht
|
|
/// </summary>
|
|
public class InvoiceService : ServiceBase<Invoice>, IInvoiceService
|
|
{
|
|
/// <summary>
|
|
/// Erstellt eine Instanz
|
|
/// </summary>
|
|
/// <param name="unitOfWork">Instanz eines IUnitOfWork</param>
|
|
public InvoiceService(IUnitOfWork unitOfWork) : base(unitOfWork)
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// Erstellen einer Rechnung
|
|
/// </summary>
|
|
/// <returns>Bestellung</returns>
|
|
public Invoice Create()
|
|
{
|
|
var item = new Invoice()
|
|
{
|
|
UniqueId = Guid.NewGuid(),
|
|
Created = DateTime.UtcNow
|
|
};
|
|
return item;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Erstellen einer Rechnung
|
|
/// </summary>
|
|
/// <param name="orderId">Id der Bestellung</param>
|
|
/// <returns>Bestellung</returns>
|
|
public async Task<Invoice> CreateAsync(long orderId)
|
|
{
|
|
var item = Create();
|
|
item.OrderId = orderId;
|
|
item.Number = await GetNextNumberAsync();
|
|
item.Counter = await GetNextCounterAsync();
|
|
|
|
return item;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt eine Rechnung basierend auf der Bestellung zurück
|
|
/// </summary>
|
|
/// <param name="orderId">Id der Bestellung</param>
|
|
/// <returns></returns>
|
|
public async Task<Invoice> GetByOrderAsync(long orderId)
|
|
{
|
|
var item = await Repository.FirstOrDefaultAsync(c => c.OrderId == orderId).ConfigureAwait(false);
|
|
return item;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt den nächsten freien Rechnungs-spezifischen Zähler zurück
|
|
/// </summary>
|
|
/// <returns>Nächste freie Nummer</returns>
|
|
public async Task<long> GetNextCounterAsync()
|
|
{
|
|
long number = 1;
|
|
var last = await Repository.Query(c => c.Id > 0).OrderByDescending(c => c.Counter).FirstOrDefaultAsync().ConfigureAwait(false);
|
|
if (last != null)
|
|
number = last.Counter + 1;
|
|
return number;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt die nächste freie Nummer für eine Rechnung als Rechnungsnummer zurück
|
|
/// </summary>
|
|
/// <returns>EndKunden-Nummer</returns>
|
|
public async Task<string> GetNextNumberAsync()
|
|
{
|
|
long number = 1;
|
|
var last = await Repository.Query(c => c.Id > 0).OrderByDescending(c => c.Id).FirstOrDefaultAsync().ConfigureAwait(false);
|
|
if (last != null)
|
|
number = last.Counter + 1;
|
|
return $"{DateTime.UtcNow.Year}{DateTime.UtcNow.Month}-{number:000000}";
|
|
}
|
|
}
|
|
}
|