94 lines
3.1 KiB
C#
94 lines
3.1 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 Gutschriften ermöglicht
|
|
/// </summary>
|
|
public class CreditNoteService : ServiceBase<CreditNote>, ICreditNoteService
|
|
{
|
|
/// <summary>
|
|
/// Erstellt eine Instanz
|
|
/// </summary>
|
|
/// <param name="unitOfWork">Instanz eines IUnitOfWork</param>
|
|
public CreditNoteService(IUnitOfWork unitOfWork) : base(unitOfWork)
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// Erstellen einer Gutschrift
|
|
/// </summary>
|
|
/// <returns>Gutschrift</returns>
|
|
public CreditNote Create()
|
|
{
|
|
var item = new CreditNote()
|
|
{
|
|
UniqueId = Guid.NewGuid(),
|
|
Created = DateTime.UtcNow
|
|
};
|
|
return item;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Erstellen einer Gutschrift
|
|
/// </summary>
|
|
/// <param name="orderId">Id der Bestellung</param>
|
|
/// <param name="invoiceNumber">Rechnungsnummer</param>
|
|
/// <returns>Gutschrift</returns>
|
|
public async Task<CreditNote> CreateAsync(long orderId, string invoiceNumber)
|
|
{
|
|
var item = Create();
|
|
item.OrderId = orderId;
|
|
item.InvoiceNumber = invoiceNumber;
|
|
item.Number = await GetNextNumberAsync();
|
|
item.Counter = await GetNextCounterAsync();
|
|
|
|
return item;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt eine Gutschrift basierend auf der Bestellung zurück
|
|
/// </summary>
|
|
/// <param name="orderId">Id der Bestellung</param>
|
|
/// <returns>Gutschrift</returns>
|
|
public async Task<CreditNote> GetByOrderAsync(long orderId)
|
|
{
|
|
var item = await Repository.FirstOrDefaultAsync(c => c.OrderId == orderId).ConfigureAwait(false);
|
|
return item;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt den nächsten freien Gutschrift-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 Gutschrift als Gutschriftennummer zurück
|
|
/// </summary>
|
|
/// <returns>Gutschrift-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}";
|
|
}
|
|
}
|
|
}
|