191 lines
8.0 KiB
C#
191 lines
8.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.Mvc.RazorPages;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using EveryThing.Data;
|
|
using EveryThing.Models;
|
|
using EveryThing.Models.Project;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Identity;
|
|
using ClosedXML.Excel;
|
|
using System.IO;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
|
|
namespace EveryThing.Pages.Invoices
|
|
{
|
|
[Authorize(Roles = "Administrator,InvoicingUser,ProjecThingUser")]
|
|
public class IndexModel : PageModel
|
|
{
|
|
private readonly IWebHostEnvironment _hostingEnvironment;
|
|
private readonly ApplicationDbContext _context;
|
|
private readonly UserManager<IdentityApplicationUser> _userManager;
|
|
|
|
public IndexModel(ApplicationDbContext context, UserManager<IdentityApplicationUser> userManager, IWebHostEnvironment environment)
|
|
{
|
|
_context = context;
|
|
_userManager = userManager;
|
|
_hostingEnvironment = environment;
|
|
}
|
|
|
|
public IList<Models.Invoice.Invoice> Invoice { get;set; }
|
|
public bool ShowProjects { get; set; } = false;
|
|
|
|
public async Task OnGetAsync(int type, string searchString, string finishedProjects)
|
|
{
|
|
var user = _userManager.GetUserAsync(User).Result;
|
|
|
|
ShowProjects = User.IsInRole("ProjecThingUser") || User.IsInRole("Administrator");
|
|
|
|
var invoiceType = (Models.Invoice.Invoice.InvoiceType)type;
|
|
|
|
//Kako spraviti type preko osvezovanja
|
|
ViewData["Type"] = type;
|
|
ViewData["SearchString"] = searchString;
|
|
ViewData["FinishedProjects"] = finishedProjects == "on" ? "checked" : "";
|
|
|
|
if (ShowProjects)
|
|
{
|
|
Invoice = await _context.Invoices
|
|
.Include(p => p.Company)
|
|
.Include(p => p.Partner)
|
|
.Include(x => x.InvoiceInvoiceItem)
|
|
.ThenInclude(x => x.ProjectPartItem)
|
|
.ThenInclude(x => x.ProjectPart)
|
|
.ThenInclude(x => x.Project)
|
|
.Where(x => x.Type == invoiceType)
|
|
.OrderBy(x => x.InvoiceYear)
|
|
.ThenBy(x => x.InvoiceNumber)
|
|
.ToListAsync();
|
|
}
|
|
else
|
|
{
|
|
Invoice = await _context.Invoices
|
|
.Include(p => p.Company)
|
|
.Include(p => p.Partner)
|
|
.Where(x => x.Type == invoiceType)
|
|
.OrderBy(x => x.InvoiceYear)
|
|
.ThenBy(x => x.InvoiceNumber)
|
|
.ToListAsync();
|
|
}
|
|
|
|
// Search string
|
|
if (!string.IsNullOrEmpty(searchString))
|
|
{
|
|
Invoice = Invoice.Where(s => s.Partner.Title.Contains(searchString, StringComparison.OrdinalIgnoreCase)
|
|
|| s.InvoiceNumberFormatted.Contains(searchString, StringComparison.OrdinalIgnoreCase)).ToList();
|
|
}
|
|
}
|
|
|
|
public IActionResult OnDeleteInvoice(int idInvoice)
|
|
{
|
|
var user = _userManager.GetUserAsync(User).Result;
|
|
var successful = true;
|
|
var error = "";
|
|
|
|
var invoice = _context.Invoices
|
|
.Where(x => x.IdCompanyFk == user.IdCompanyFk)
|
|
.FirstOrDefault(x => x.IdInvoice == idInvoice);
|
|
if (invoice != null)
|
|
{
|
|
var invoiceItems = _context.InvoiceItems.Where(x => x.IdInvoiceFk == idInvoice).ToList();
|
|
|
|
for (var i = 0; i < invoiceItems.Count(); i++)
|
|
{
|
|
_context.InvoiceItems.Remove(invoiceItems[i]);
|
|
}
|
|
_context.Invoices.Remove(invoice);
|
|
_context.SaveChanges();
|
|
}
|
|
else
|
|
{
|
|
successful = false;
|
|
error = $"Invoice with ID: {idInvoice} not found";
|
|
}
|
|
|
|
return new JsonResult(new { idInvoice = idInvoice, error = error, successful = successful });
|
|
}
|
|
|
|
public IActionResult OnGetExcel(int idInvoice)
|
|
{
|
|
var user = _userManager.GetUserAsync(User).Result;
|
|
var successful = true;
|
|
var error = "";
|
|
var name = "";
|
|
var base64 = "";
|
|
|
|
var invoice = _context.Invoices
|
|
.Where(x => x.IdCompanyFk == user.IdCompanyFk)
|
|
.FirstOrDefault(x => x.IdInvoice == idInvoice);
|
|
if (invoice != null)
|
|
{
|
|
name = invoice.InvoiceNumberFull.Replace("-", "_") + ".xlsx";
|
|
|
|
var invoiceItems = _context.InvoiceItems
|
|
.Include(x => x.Item)
|
|
.Include(x => x.InvoiceItemJoin)
|
|
.ThenInclude(x=> x.Invoice)
|
|
.Where(x => x.IdInvoiceFk == idInvoice).ToList();
|
|
|
|
var workbook = new XLWorkbook();
|
|
var workSheet = workbook.Worksheets.Add("Dokument");
|
|
|
|
var counter = 1;
|
|
workSheet.Cell($"A{counter}").Value = "Artikel";
|
|
workSheet.Cell($"B{counter}").Value = "Količina";
|
|
workSheet.Cell($"C{counter}").Value = "Cena";
|
|
workSheet.Cell($"D{counter}").Value = "Rabat";
|
|
workSheet.Cell($"E{counter}").Value = "Vrednost";
|
|
workSheet.Cell($"F{counter}").Value = "Opis";
|
|
workSheet.Cell($"G{counter}").Value = "Št. kup. naročila";
|
|
workSheet.Cell($"H{counter}").Value = "Št. naročila";
|
|
workSheet.Cell($"I{counter}").Value = "Dobavni rok";
|
|
//workSheet.Cell($"J{counter}").Value = "DN Z";
|
|
//workSheet.Cell($"K{counter}").Value = "DN Z Vnos";
|
|
//workSheet.Cell($"L{counter}").Value = "DN P";
|
|
//workSheet.Cell($"M{counter}").Value = "DN P Vnos";
|
|
//workSheet.Cell($"N{counter}").Value = "DN N";
|
|
//workSheet.Cell($"O{counter}").Value = "DN N Vnos";
|
|
//workSheet.Cell($"P{counter}").Value = "Prihod/Odhod nepreračunan";
|
|
//workSheet.Cell($"Q{counter}").Value = "Skupaj nepreračunan";
|
|
|
|
foreach (var invoiceItem in invoiceItems)
|
|
{
|
|
counter++;
|
|
workSheet.Cell($"A{counter}").Value = invoiceItem.Item?.Title;
|
|
workSheet.Cell($"B{counter}").Value = invoiceItem.Quantity;
|
|
workSheet.Cell($"C{counter}").Value = invoiceItem.Price;
|
|
workSheet.Cell($"D{counter}").Value = invoiceItem.Discount;
|
|
workSheet.Cell($"E{counter}").Value = invoiceItem.TotalValue;
|
|
workSheet.Cell($"F{counter}").Value = invoiceItem.ItemDescription;
|
|
workSheet.Cell($"G{counter}").Value = "'" + invoiceItem.InvoiceItemJoin?.Invoice?.BuyersOrderNumber;
|
|
workSheet.Cell($"H{counter}").Value = "'" + invoiceItem.InvoiceItemJoin?.Invoice?.InvoiceNumberFormatted;
|
|
workSheet.Cell($"I{counter}").Value = invoice.DateOfDispatch == null ? "" : ((DateTime)invoice.DateOfDispatch).ToString("dd.MM.yyyy");
|
|
}
|
|
|
|
//var range = workSheet.Range(1, 1, counter - 1, 9);
|
|
|
|
//var table = range.CreateTable();
|
|
//table.Theme = XLTableTheme.TableStyleMedium5;
|
|
|
|
var tmpFilePath = Path.Combine(_hostingEnvironment.WebRootPath, "Uploads", Guid.NewGuid().ToString().Replace("-", "_") + ".xlsx");
|
|
workbook.SaveAs(tmpFilePath);
|
|
|
|
var tempByte = System.IO.File.ReadAllBytes(tmpFilePath);
|
|
base64 = Convert.ToBase64String(tempByte);
|
|
System.IO.File.Delete(tmpFilePath);
|
|
|
|
}
|
|
else
|
|
{
|
|
successful = false;
|
|
error = $"Invoice with ID: {idInvoice} not found";
|
|
}
|
|
|
|
return new JsonResult(new { idInvoice, error, successful, name, base64 });
|
|
}
|
|
}
|
|
}
|