272 lines
14 KiB
C#

using System.Threading.Tasks;
using AutoMapper;
using gehGassi.Core.Interfaces;
using gehGassi.Core.Services;
using gehGassi.Domain.Common;
using gehGassi.Web.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Localization;
namespace gehGassi.Web.Controllers
{
/// <summary>
/// Controller der Funktionen für das Erstellen von PDF-Dokumenten zur Verfügung stellt
/// </summary>
[Authorize]
public class PrintController : BaseController
{
private readonly IMapper _mapper;
private readonly IStringLocalizer<PrintController> _localizer;
private readonly IOrderService _orderService;
private readonly IInvoiceService _invoiceService;
private readonly IListingService _listingService;
private readonly IAdvertisementService _advertisementService;
private readonly IBannerService _bannerService;
private readonly IPinService _pinService;
private readonly ICountryService _countryService;
private readonly ICreditNoteService _creditNoteService;
private readonly ICustomerService _customerService;
/// <summary>
/// Erstellt eine Instanz
/// </summary>
/// <param name="mapper">Instanz eines IMapper</param>
/// <param name="localizer">Instanz eines IStringLocalizer</param>
/// <param name="orderService">Instanz eines IOrderService</param>
/// <param name="invoiceService">Instanz eines IInvoiceService</param>
/// <param name="listingService">Instanz eines IListingService</param>
/// <param name="advertisementService">Instanz eines IAdvertisementService</param>
/// <param name="bannerService">Instanz eines IBannerService</param>
/// <param name="pinService">Instanz eines IPinService</param>
/// <param name="countryService">Instanz eines ICountryService</param>
/// <param name="creditNoteService">Instanz eines ICreditNoteService</param>
/// <param name="customerService">Instanz eines ICustomerService</param>
public PrintController(IMapper mapper, IStringLocalizer<PrintController> localizer, IOrderService orderService, IInvoiceService invoiceService, IListingService listingService,
IAdvertisementService advertisementService, IBannerService bannerService, IPinService pinService, ICountryService countryService, ICreditNoteService creditNoteService, ICustomerService customerService)
{
_mapper = mapper;
_localizer = localizer;
_orderService = orderService;
_invoiceService = invoiceService;
_listingService = listingService;
_advertisementService = advertisementService;
_bannerService = bannerService;
_pinService = pinService;
_countryService = countryService;
_creditNoteService = creditNoteService;
_customerService = customerService;
}
/// <summary>
/// View für eine Rechnung.
/// Wird für das Erstellen einer PDF-Rechnung verwendet
/// </summary>
/// <param name="orderId">Id der Bestellung</param>
/// <param name="lanuage">Sprache ISO2</param>
/// <returns>View</returns>
[AllowAnonymous]
public async Task<IActionResult> Invoice(long orderId, string lanuage)
{
var order = await _orderService.GetAsync(orderId);
if (order != null)
{
var invoice = await _invoiceService.GetByOrderAsync(orderId);
if (invoice != null)
{
var customer = await _customerService.GetAsync(order.CustomerId);
ViewBag.CustomerNumber = customer.Number;
var model = _mapper.Map<OrderVm>(order);
var orderItems = await _orderService.GetItemsAsync(order.Id);
foreach (var orderItem in orderItems)
{
var orderItemVm = _mapper.Map<OrderItemVm>(orderItem);
if (orderItem.ProductType == ProductType.Listing)
{
var listing = await _listingService.GetByOrderItemAsync(orderItem.Id);
if (listing != null)
{
var country = _countryService.GetCountry(listing.Address.CountryCode);
orderItemVm.ProductSpecialInfo = $"{country.Name} | {listing.StartDate.Value.Date.ToShortDateString()} - {listing.EndDate.Value.Date.ToShortDateString()}";
}
}
else if (orderItem.ProductType == ProductType.Advertisement)
{
var advertisement = await _advertisementService.GetByOrderItemAsync(orderItem.Id);
if (advertisement != null)
{
var country = _countryService.GetCountry(advertisement.Address.CountryCode);
orderItemVm.ProductSpecialInfo = $"{country.Name} | {advertisement.StartDate.Value.Date.ToShortDateString()} - {advertisement.EndDate.Value.Date.ToShortDateString()}";
}
}
else if (orderItem.ProductType == ProductType.Banner)
{
var banner = await _bannerService.GetByOrderItemAsync(orderItem.Id);
if (banner != null)
{
var country = _countryService.GetCountry(banner.Address.CountryCode);
orderItemVm.ProductSpecialInfo = $"{country.Name} | {banner.StartDate.Value.Date.ToShortDateString()}";
}
}
else if (orderItem.ProductType == ProductType.Pin)
{
var pin = await _pinService.GetByOrderItemAsync(orderItem.Id);
if (pin != null)
{
orderItemVm.ProductSpecialInfo = $"Lat: {pin.Location.Y.ToString("N6")} Lng: {pin.Location.X.ToString("N6")} Rad: {pin.Radius.ToString("N2")}km | {pin.StartDate.Value.Date.ToShortDateString()} - {pin.EndDate.Value.Date.ToShortDateString()}";
}
}
model.Items.Add(orderItemVm);
}
ViewBag.Invoice = invoice;
return View(model);
}
}
return View("Error");
}
/// <summary>
/// View für eine Proforma Rechnung.
/// Wird für das Erstellen einer PDF-Rechnung verwendet
/// </summary>
/// <param name="orderId">Id der Bestellung</param>
/// <param name="lanuage">Sprache ISO2</param>
/// <returns>View</returns>
[AllowAnonymous]
public async Task<IActionResult> InvoiceProforma(long orderId, string lanuage)
{
var order = await _orderService.GetAsync(orderId);
if (order != null)
{
var customer = await _customerService.GetAsync(order.CustomerId);
ViewBag.CustomerNumber = customer.Number;
var model = _mapper.Map<OrderVm>(order);
var orderItems = await _orderService.GetItemsAsync(order.Id);
foreach (var orderItem in orderItems)
{
var orderItemVm = _mapper.Map<OrderItemVm>(orderItem);
if (orderItem.ProductType == ProductType.Listing)
{
var listing = await _listingService.GetByOrderItemAsync(orderItem.Id);
if (listing != null)
{
var country = _countryService.GetCountry(listing.Address.CountryCode);
orderItemVm.ProductSpecialInfo = $"{country.Name} | {listing.StartDate.Value.Date.ToShortDateString()} - {listing.EndDate.Value.Date.ToShortDateString()}";
}
}
else if (orderItem.ProductType == ProductType.Advertisement)
{
var advertisement = await _advertisementService.GetByOrderItemAsync(orderItem.Id);
if (advertisement != null)
{
var country = _countryService.GetCountry(advertisement.Address.CountryCode);
orderItemVm.ProductSpecialInfo = $"{country.Name} | {advertisement.StartDate.Value.Date.ToShortDateString()} - {advertisement.EndDate.Value.Date.ToShortDateString()}";
}
}
else if (orderItem.ProductType == ProductType.Banner)
{
var banner = await _bannerService.GetByOrderItemAsync(orderItem.Id);
if (banner != null)
{
var country = _countryService.GetCountry(banner.Address.CountryCode);
orderItemVm.ProductSpecialInfo = $"{country.Name} | {banner.StartDate.Value.Date.ToShortDateString()}";
}
}
else if (orderItem.ProductType == ProductType.Pin)
{
var pin = await _pinService.GetByOrderItemAsync(orderItem.Id);
if (pin != null)
{
orderItemVm.ProductSpecialInfo = $"Lat: {pin.Location.Y.ToString("N6")} Lng: {pin.Location.X.ToString("N6")} Rad: {pin.Radius.ToString("N2")}km | {pin.StartDate.Value.Date.ToShortDateString()} - {pin.EndDate.Value.Date.ToShortDateString()}";
}
}
model.Items.Add(orderItemVm);
}
return View(model);
}
return View("Error");
}
/// <summary>
/// View für eine Gutschrift.
/// Wird für das Erstellen einer PDF-gutschrift verwendet
/// </summary>
/// <param name="orderId">Id der Bestellung</param>
/// <param name="lanuage">Sprache ISO2</param>
/// <returns>View</returns>
[AllowAnonymous]
public async Task<IActionResult> CreditNote(long orderId, string lanuage)
{
var order = await _orderService.GetAsync(orderId);
if (order != null)
{
var creditNote = await _creditNoteService.GetByOrderAsync(orderId);
if (creditNote != null)
{
var customer = await _customerService.GetAsync(order.CustomerId);
ViewBag.CustomerNumber = customer.Number;
var model = _mapper.Map<OrderVm>(order);
var orderItems = await _orderService.GetItemsAsync(order.Id);
foreach (var orderItem in orderItems)
{
var orderItemVm = _mapper.Map<OrderItemVm>(orderItem);
if (orderItem.ProductType == ProductType.Listing)
{
var listing = await _listingService.GetByOrderItemAsync(orderItem.Id);
if (listing != null)
{
var country = _countryService.GetCountry(listing.Address.CountryCode);
orderItemVm.ProductSpecialInfo = $"{country.Name} | {listing.StartDate.Value.Date.ToShortDateString()} - {listing.EndDate.Value.Date.ToShortDateString()}";
}
}
else if (orderItem.ProductType == ProductType.Advertisement)
{
var advertisement = await _advertisementService.GetByOrderItemAsync(orderItem.Id);
if (advertisement != null)
{
var country = _countryService.GetCountry(advertisement.Address.CountryCode);
orderItemVm.ProductSpecialInfo = $"{country.Name} | {advertisement.StartDate.Value.Date.ToShortDateString()} - {advertisement.EndDate.Value.Date.ToShortDateString()}";
}
}
else if (orderItem.ProductType == ProductType.Banner)
{
var banner = await _bannerService.GetByOrderItemAsync(orderItem.Id);
if (banner != null)
{
var country = _countryService.GetCountry(banner.Address.CountryCode);
orderItemVm.ProductSpecialInfo = $"{country.Name} | {banner.StartDate.Value.Date.ToShortDateString()}";
}
}
else if (orderItem.ProductType == ProductType.Pin)
{
var pin = await _pinService.GetByOrderItemAsync(orderItem.Id);
if (pin != null)
{
orderItemVm.ProductSpecialInfo = $"Lat: {pin.Location.Y.ToString("N6")} Lng: {pin.Location.X.ToString("N6")} Rad: {pin.Radius.ToString("N2")}km | {pin.StartDate.Value.Date.ToShortDateString()} - {pin.EndDate.Value.Date.ToShortDateString()}";
}
}
model.Items.Add(orderItemVm);
}
ViewBag.CreditNote = creditNote;
return View(model);
}
}
return View("Error");
}
}
}