Files
everything2/EveryThing/Pages/Invoices/Print.cshtml.cs
David Štaleker 03b92525d7 Prvi commit
2023-05-12 09:00:07 +02:00

243 lines
10 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;
using EveryThing.Data;
using EveryThing.Models;
using EveryThing.Models.Invoice;
using EveryThing.Models.Transport;
using DocumentFormat.OpenXml.Wordprocessing;
using System.ComponentModel.DataAnnotations;
namespace EveryThing.Pages.Invoices
{
[Authorize(Roles = "Administrator,InvoicingUser,ProjecThingUser")]
public class PrintModel : PageModel
{
public class PrintTranslation
{
public string Email { get; set; } = "E-pošta";
public string Telephone { get; set; } = "Tel.";
public string IdTax { get; set; } = "ID za DDV";
public string Iban { get; set; } = "IBAN";
public string SwiftBic { get; set; } = "SWIFT/BIC";
public string InvoiceType { get; set; } = "Faktura";
public string Number { get; set; } = "št.";
public string Date { get; set; } = "Datum";
public string OrderNumber { get; set; } = "Številka naroèila";
public string DeliveryNote { get; set; } = "Številka dobavnice:";
public string DeliveryTime { get; set; } = "Dobavni rok";
public string DateOfDispatch { get; set; } = "Datum odpreme";
public string Article { get; set; } = "Artikel";
public string Quantity { get; set; } = "Kolièina";
public string Price { get; set; } = "Cena";
public string Amount { get; set; } = "Znesek";
public string Dimensions { get; set; } = "Dimenzije";
public string Total { get; set; } = "Skupaj";
public string Director { get; set; } = "Direktor";
public string Project { get; set; } = "Projekt";
public string Issued { get; set; } = "Izdal:";
public string Received { get; set; } = "Prejel:";
}
public enum PrintTranslationLanguage
{
[Display(Name = "Slovensko")]
Slovenian = 1,
[Display(Name = "Nemško")]
German = 2
}
private readonly ApplicationDbContext _context;
private readonly UserManager<IdentityApplicationUser> _userManager;
public Models.Invoice.Invoice Invoice { get; set; }
public PrintTranslation Translation { get; set; }
public IList<Models.Invoice.InvoiceItem> InvoiceItems { get; set; }
public string ProjectNumber { get; set; }
public PrintModel(ApplicationDbContext context, UserManager<IdentityApplicationUser> userManager)
{
_context = context;
_userManager = userManager;
}
public async Task<IActionResult> OnGetAsync(int id, int? printTranslationLanguage)
{
var user = _userManager.GetUserAsync(User).Result;
Invoice = await _context.Invoices
.Include(x => x.Company)
.Include(x => x.Company.Country)
.Include(x => x.Partner)
.Include(x => x.Partner.Country)
.FirstOrDefaultAsync(x => x.IdInvoice == id && x.IdCompanyFk == user.IdCompanyFk);
if (Invoice != null)
{
InvoiceItems = await _context.InvoiceItems
.Where(x => x.IdInvoiceFk == Invoice.IdInvoice)
.Include(x => x.Item)
.Include(x => x.InvoiceItemJoin)
.ThenInclude(x => x.Invoice)
.ToListAsync();
var translationLanguage = PrintTranslationLanguage.Slovenian;
if (printTranslationLanguage != null)
translationLanguage = (PrintTranslationLanguage)printTranslationLanguage;
SetTranslation(translationLanguage, Invoice.State);
var showProjects = User.IsInRole("ProjecThingUser") || User.IsInRole("Administrator");
if (showProjects)
{
var project = _context.InvoiceItems
.Include(x => x.ProjectPartItem)
.ThenInclude(x => x.ProjectPart)
.ThenInclude(x => x.Project).FirstOrDefault(x => x.IdInvoiceFk == Invoice.IdInvoice);
if (project != null && project.ProjectPartItem != null)
ProjectNumber = project.ProjectPartItem.ProjectPart.Project.ProjectNumberFormatted;
}
return Page();
}
return NotFound();
}
public IActionResult OnPostConfirmInvoice(int idInvoice)
{
var user = _userManager.GetUserAsync(User).Result;
bool successful = true;
string error = "";
var invoice = _context.Invoices
.Where(x => x.IdCompanyFk == user.IdCompanyFk)
.FirstOrDefault(x => x.IdInvoice == idInvoice);
if (invoice != null)
{
invoice.State = Models.Invoice.Invoice.InvoiceState.Confirmed;
_context.SaveChanges();
}
else
{
successful = false;
error = $"Invoice with ID: {idInvoice} not found";
}
return new JsonResult(new { idInvoice = idInvoice, error = error, successful = successful });
}
private void SetTranslation(PrintTranslationLanguage printTranslationLanguage, Invoice.InvoiceState invoiceState)
{
Translation = new PrintTranslation();
switch (printTranslationLanguage)
{
case PrintTranslationLanguage.Slovenian:
switch(Invoice.Type)
{
case Invoice.InvoiceType.Order:
case Invoice.InvoiceType.BuyersOrder:
if (Invoice.State == Invoice.InvoiceState.Offer)
{
Translation.InvoiceType = "Ponudba";
}
else if (Invoice.State == Invoice.InvoiceState.Inquiry)
{
Translation.InvoiceType = "Povpraševanje";
}
else if (Invoice.State == Invoice.InvoiceState.OfferConfirmation)
{
Translation.InvoiceType = "Potrditev naroèila";
}
else
{
Translation.InvoiceType = "Naroèilo";
}
break;
case Invoice.InvoiceType.Invoice:
Translation.InvoiceType = "Raèun";
break;
case Invoice.InvoiceType.DeliveryNote:
Translation.InvoiceType = "Dobavnica";
break;
default:
Translation.InvoiceType = "Faktura";
break;
}
if (invoiceState == Invoice.InvoiceState.Offer)
Translation.OrderNumber = "Št. povpraševanja.";
break;
case PrintTranslationLanguage.German:
Translation = new PrintTranslation
{
Amount = "Menge",
Article = "Artikel",
Date = "Datum",
DateOfDispatch = "Lieferdatum",
DeliveryTime = "Lieferdatum",
Dimensions = "Maße",
Director = "Direktor",
Email = "Email",
Iban = "IBAN",
IdTax = "Ihre UID Nr.",
Number = "Num.",
OrderNumber = invoiceState == Invoice.InvoiceState.Offer ? "Anfrage No." : "Bestellnummer",
Price = "Preis(€)",
Quantity = "Menge(Stk)",
SwiftBic = "SWIFT/BIC",
Total = "Gesamtsumme",
Project = "Projekt",
Issued = "Bearbeiter:",
Received = "Empfänger:",
DeliveryNote = "Lieferschein:"
};
switch (Invoice.Type)
{
case Invoice.InvoiceType.Order:
case Invoice.InvoiceType.BuyersOrder:
if (Invoice.State == Invoice.InvoiceState.Offer)
{
Translation.InvoiceType = "Angebot";
}
else if (Invoice.State == Invoice.InvoiceState.Inquiry)
{
Translation.InvoiceType = "Anfrage";
}
else if (Invoice.State == Invoice.InvoiceState.OfferConfirmation)
{
Translation.InvoiceType = "Auftragsbestätigung";
}
else
{
Translation.InvoiceType = "Bestellung";
}
break;
case Invoice.InvoiceType.Invoice:
Translation.InvoiceType = "Rechnung";
break;
case Invoice.InvoiceType.DeliveryNote:
Translation.InvoiceType = "Lieferschein";
break;
default:
Translation.InvoiceType = "Faktura";
break;
}
break;
}
}
}
}