rename to projecting
This commit is contained in:
351
ProjecThing/Pages/Invoices/Print.cshtml.cs
Normal file
351
ProjecThing/Pages/Invoices/Print.cshtml.cs
Normal file
@@ -0,0 +1,351 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.RazorPages;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using MailKit.Net.Smtp;
|
||||
using MailKit.Security;
|
||||
using MimeKit;
|
||||
using ProjecThing.Data;
|
||||
using ProjecThing.Models;
|
||||
using ProjecThing.Models.Invoice;
|
||||
using ProjecThing.Models.Transport;
|
||||
using DocumentFormat.OpenXml.Wordprocessing;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace ProjecThing.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 Discount { get; set; } = "Rabat";
|
||||
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;
|
||||
private readonly IConfiguration _configuration;
|
||||
private readonly IWebHostEnvironment _environment;
|
||||
|
||||
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, IConfiguration configuration, IWebHostEnvironment environment)
|
||||
{
|
||||
_context = context;
|
||||
_userManager = userManager;
|
||||
_configuration = configuration;
|
||||
_environment = environment;
|
||||
}
|
||||
|
||||
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 async Task<IActionResult> OnPostSendDocumentAsync(IFormFile pdfFile, int idInvoice, bool withAttachment)
|
||||
{
|
||||
if (pdfFile == null || pdfFile.Length == 0)
|
||||
return new JsonResult(new { successful = false, error = "PDF datoteka je obvezna." });
|
||||
|
||||
var user = await _userManager.GetUserAsync(User);
|
||||
|
||||
try
|
||||
{
|
||||
var invoice = await _context.Invoices
|
||||
.Include(x => x.Partner)
|
||||
.Include(x => x.Company)
|
||||
.FirstOrDefaultAsync(x => x.IdInvoice == idInvoice && x.IdCompanyFk == user.IdCompanyFk);
|
||||
|
||||
if (invoice == null)
|
||||
return new JsonResult(new { successful = false, error = "Invoice not found." });
|
||||
|
||||
using var stream = pdfFile.OpenReadStream();
|
||||
var pdfBytes = new byte[pdfFile.Length];
|
||||
await stream.ReadAsync(pdfBytes, 0, (int)pdfFile.Length);
|
||||
|
||||
var attachments = new List<Models.File>();
|
||||
if (withAttachment)
|
||||
{
|
||||
var itemIds = await _context.InvoiceItems
|
||||
.Where(x => x.IdInvoiceFk == idInvoice && x.IdItemFk != null)
|
||||
.Select(x => x.IdItemFk.Value)
|
||||
.Distinct()
|
||||
.ToListAsync();
|
||||
|
||||
if (itemIds.Any())
|
||||
{
|
||||
attachments = await _context.Files
|
||||
.Where(x => x.IdCompanyFk == user.IdCompanyFk
|
||||
&& itemIds.Contains(x.IdReferenceFk)
|
||||
&& x.FileType == FileType.CodeTableItem)
|
||||
.ToListAsync();
|
||||
}
|
||||
}
|
||||
|
||||
var emailSettings = _configuration.GetSection("EmailSettings");
|
||||
var host = emailSettings["Host"];
|
||||
var port = int.Parse(emailSettings["Port"] ?? "587");
|
||||
var secureSocketOptions = Enum.Parse<SecureSocketOptions>(emailSettings["SecureSocketOptions"] ?? "StartTls");
|
||||
var username = emailSettings["Username"];
|
||||
var password = emailSettings["Password"];
|
||||
var fromEmail = emailSettings["FromEmail"];
|
||||
var fromName = emailSettings["FromName"];
|
||||
var bcc = emailSettings["Bcc"];
|
||||
|
||||
var mimeMessage = new MimeMessage();
|
||||
mimeMessage.From.Add(new MailboxAddress(fromName, fromEmail));
|
||||
mimeMessage.To.Add(new MailboxAddress(invoice.Partner.Title, invoice.Partner.Email));
|
||||
if (!string.IsNullOrWhiteSpace(bcc))
|
||||
mimeMessage.Bcc.Add(MailboxAddress.Parse(bcc));
|
||||
mimeMessage.Subject = invoice.InvoiceNumberFull;
|
||||
|
||||
var bodyBuilder = new BodyBuilder
|
||||
{
|
||||
TextBody = $"Spoštovani,\n\nv prilogi vam pošiljamo dokument {invoice.InvoiceNumberFull}.\n\nLep pozdrav,\n{invoice.Company.Title}"
|
||||
};
|
||||
|
||||
bodyBuilder.Attachments.Add(invoice.InvoiceNumberFull + ".pdf", pdfBytes, new ContentType("application", "pdf"));
|
||||
|
||||
var filesFolder = System.IO.Path.Combine(_environment.WebRootPath, "Uploads", "Files", ((int)FileType.CodeTableItem).ToString());
|
||||
foreach (var file in attachments)
|
||||
{
|
||||
var filePath = System.IO.Path.Combine(filesFolder, file.Guid + file.Extension);
|
||||
if (System.IO.File.Exists(filePath))
|
||||
{
|
||||
var fileBytes = await System.IO.File.ReadAllBytesAsync(filePath);
|
||||
bodyBuilder.Attachments.Add(file.Title + file.Extension, fileBytes);
|
||||
}
|
||||
}
|
||||
|
||||
mimeMessage.Body = bodyBuilder.ToMessageBody();
|
||||
|
||||
using var smtpClient = new SmtpClient();
|
||||
await smtpClient.ConnectAsync(host, port, secureSocketOptions);
|
||||
await smtpClient.AuthenticateAsync(username, password);
|
||||
await smtpClient.SendAsync(mimeMessage);
|
||||
await smtpClient.DisconnectAsync(true);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return new JsonResult(new { successful = false, error = ex.Message });
|
||||
}
|
||||
|
||||
return new JsonResult(new { successful = true });
|
||||
}
|
||||
|
||||
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;
|
||||
case Invoice.InvoiceType.Cooperation:
|
||||
Translation.InvoiceType = "Kooperacija";
|
||||
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)",
|
||||
Discount = "Rabatt",
|
||||
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;
|
||||
case Invoice.InvoiceType.Cooperation:
|
||||
Translation.InvoiceType = "Zusammenarbeit";
|
||||
break;
|
||||
default:
|
||||
Translation.InvoiceType = "Faktura";
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user