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
{
///
/// Controller der Funktionen für das Erstellen von PDF-Dokumenten zur Verfügung stellt
///
[Authorize]
public class PrintController : BaseController
{
private readonly IMapper _mapper;
private readonly IStringLocalizer _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;
///
/// Erstellt eine Instanz
///
/// Instanz eines IMapper
/// Instanz eines IStringLocalizer
/// Instanz eines IOrderService
/// Instanz eines IInvoiceService
/// Instanz eines IListingService
/// Instanz eines IAdvertisementService
/// Instanz eines IBannerService
/// Instanz eines IPinService
/// Instanz eines ICountryService
/// Instanz eines ICreditNoteService
/// Instanz eines ICustomerService
public PrintController(IMapper mapper, IStringLocalizer 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;
}
///
/// View für eine Rechnung.
/// Wird für das Erstellen einer PDF-Rechnung verwendet
///
/// Id der Bestellung
/// Sprache ISO2
/// View
[AllowAnonymous]
public async Task 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(order);
var orderItems = await _orderService.GetItemsAsync(order.Id);
foreach (var orderItem in orderItems)
{
var orderItemVm = _mapper.Map(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");
}
///
/// View für eine Proforma Rechnung.
/// Wird für das Erstellen einer PDF-Rechnung verwendet
///
/// Id der Bestellung
/// Sprache ISO2
/// View
[AllowAnonymous]
public async Task 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(order);
var orderItems = await _orderService.GetItemsAsync(order.Id);
foreach (var orderItem in orderItems)
{
var orderItemVm = _mapper.Map(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");
}
///
/// View für eine Gutschrift.
/// Wird für das Erstellen einer PDF-gutschrift verwendet
///
/// Id der Bestellung
/// Sprache ISO2
/// View
[AllowAnonymous]
public async Task 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(order);
var orderItems = await _orderService.GetItemsAsync(order.Id);
foreach (var orderItem in orderItems)
{
var orderItemVm = _mapper.Map(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");
}
}
}