205 lines
7.9 KiB
C#
205 lines
7.9 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.AspNetCore.Mvc.Rendering;
|
|
using EveryThing.Data;
|
|
using EveryThing.Models;
|
|
using EveryThing.Models.Project;
|
|
using Microsoft.AspNetCore.Http;
|
|
using System.IO;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using ClosedXML.Excel;
|
|
using EveryThing.Models.CodeTable;
|
|
|
|
namespace EveryThing.Pages.Projects
|
|
{
|
|
[Authorize(Roles = "Administrator,ProjecThingUser")]
|
|
public class CreatePartItemImportExcelModel : PageModel
|
|
{
|
|
private readonly IWebHostEnvironment _hostingEnvironment;
|
|
private readonly ApplicationDbContext _context;
|
|
private readonly UserManager<IdentityApplicationUser> _userManager;
|
|
private readonly SignInManager<IdentityApplicationUser> _loginManager;
|
|
private readonly RoleManager<IdentityApplicationRole> _roleManager;
|
|
|
|
public CreatePartItemImportExcelModel(ApplicationDbContext context, UserManager<IdentityApplicationUser> userManager, SignInManager<IdentityApplicationUser> loginManager, RoleManager<IdentityApplicationRole> roleManager, IWebHostEnvironment environment)
|
|
{
|
|
_context = context;
|
|
_userManager = userManager;
|
|
_loginManager = loginManager;
|
|
_roleManager = roleManager;
|
|
_hostingEnvironment = environment;
|
|
}
|
|
|
|
[BindProperty]
|
|
public int IdProject { get; set; }
|
|
[BindProperty]
|
|
public int IdProjectPart { get; set; }
|
|
[BindProperty]
|
|
public string FileName { get; set; }
|
|
[BindProperty]
|
|
public List<ExcelItem> ExcelItems { get; set; }
|
|
[BindProperty]
|
|
public string SelectedItems { get; set; }
|
|
|
|
public IActionResult OnGet(int idProject, int idProjectPart, string fileName)
|
|
{
|
|
var user = _userManager.GetUserAsync(User).Result;
|
|
|
|
IdProject = idProject;
|
|
IdProjectPart = idProjectPart;
|
|
FileName = fileName;
|
|
|
|
var tmpList = typeof(Models.Project.ProjectPartItem).GetProperties()
|
|
.Where(x => x.GetCustomAttributes(true).Length > 0 && x.GetCustomAttributes(true).Any(y => y.GetType() == typeof(System.ComponentModel.DataAnnotations.DisplayAttribute)))
|
|
.Select(x => new
|
|
{
|
|
Name = x.Name,
|
|
Display = ((System.ComponentModel.DataAnnotations.DisplayAttribute)x.GetCustomAttributes(true).First(y => y.GetType() == typeof(System.ComponentModel.DataAnnotations.DisplayAttribute))).Name
|
|
}).ToList();
|
|
tmpList.Insert(0, new { Name = "", Display = "Ni izbrano" });
|
|
|
|
ViewData["ProjectPartItems"] = new SelectList(tmpList, "Name", "Display");
|
|
|
|
ExcelItems = new List<ExcelItem>();
|
|
string path = Path.Combine(_hostingEnvironment.WebRootPath, "Uploads");
|
|
|
|
var xlWorkbook = new XLWorkbook(Path.Combine(path, fileName));
|
|
|
|
//ONLY FIRST LIST
|
|
var worksheet = xlWorkbook.Worksheet(1);
|
|
|
|
int i = 1;
|
|
if (!worksheet.Row(i).IsEmpty())
|
|
{
|
|
IXLRow row = worksheet.Row(i);
|
|
int j = 1;
|
|
while (!row.Cell(j).IsEmpty())
|
|
{
|
|
var cellData = row.Cell(j).Value;
|
|
|
|
ExcelItems.Add(new ExcelItem
|
|
{
|
|
CellIndex = j,
|
|
Name = cellData.ToString(),
|
|
});
|
|
|
|
j++;
|
|
}
|
|
}
|
|
|
|
return Page();
|
|
}
|
|
|
|
public async Task<IActionResult> OnPostAsync(string selectedItems)
|
|
{
|
|
if (selectedItems == "")
|
|
{
|
|
return Page(); //TODO Error
|
|
}
|
|
var user = _userManager.GetUserAsync(User).Result;
|
|
|
|
var excelItems = new List<ExcelItem>();
|
|
|
|
var items = selectedItems.Split('#');
|
|
foreach (var item in items)
|
|
{
|
|
var itemData = item.Split(';');
|
|
if (itemData.Length != 2)
|
|
continue;
|
|
|
|
excelItems.Add(new ExcelItem
|
|
{
|
|
CellIndex = Convert.ToInt32(itemData[0]),
|
|
Name = itemData[1],
|
|
});
|
|
}
|
|
|
|
string path = Path.Combine(_hostingEnvironment.WebRootPath, "Uploads");
|
|
|
|
var xlWorkbook = new XLWorkbook(Path.Combine(path, FileName));
|
|
|
|
//ONLY FIRST LIST
|
|
var worksheet = xlWorkbook.Worksheet(1);
|
|
|
|
var insertedParts = _context.ProjectPartItems.Where(x => x.IdProjectPartFk == IdProjectPart);
|
|
|
|
var currentPositionNumber = insertedParts.Any()
|
|
? insertedParts.Max(x => x.ProjectPartItemNumber)
|
|
: 0;
|
|
|
|
if (currentPositionNumber <= 0)
|
|
currentPositionNumber = 0;
|
|
|
|
int i = 2;//Skip header
|
|
while (!worksheet.Row(i).IsEmpty())
|
|
{
|
|
currentPositionNumber++;
|
|
IXLRow row = worksheet.Row(i);
|
|
var newPosition = new Models.Project.ProjectPartItem
|
|
{
|
|
IdProjectPartFk = IdProjectPart,
|
|
NumberOfItems = 1,
|
|
NumberOfSets = 1,
|
|
IdMaterialSupplierFk = _context.CodeTablePartners.First().IdPartner,
|
|
ProjectPartItemNumber = currentPositionNumber
|
|
};
|
|
|
|
foreach (var excelItem in excelItems)
|
|
{
|
|
if (row.Cell(excelItem.CellIndex) == null)
|
|
continue;
|
|
|
|
string value = row.Cell(excelItem.CellIndex).Value.ToString();
|
|
|
|
if (excelItem.Name == "IdItemFk"
|
|
|| excelItem.Name == "IdMaterialFk")
|
|
{
|
|
var completableItem = _context.CodeTableItems.FirstOrDefault(x => x.Title == value && x.Active == true);
|
|
if (completableItem == null)
|
|
{
|
|
//Add new if not exists
|
|
completableItem = new Models.CodeTable.CodeTableItem
|
|
{
|
|
Active = true,
|
|
IdCompanyFk = user.IdCompanyFk,
|
|
Title = value,
|
|
CodeTableItemType = excelItem.Name == "IdMaterialFk" ? CodeTableItemType.Material : CodeTableItemType.Product
|
|
};
|
|
_context.CodeTableItems.Add(completableItem);
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
if (excelItem.Name == "IdItemFk")
|
|
newPosition.IdItemFk = completableItem.IdItem;
|
|
else
|
|
newPosition.IdMaterialFk = completableItem.IdItem;
|
|
}
|
|
else
|
|
{
|
|
var propertyInfo = newPosition.GetType().GetProperties().First(x => x.Name == excelItem.Name);
|
|
object propertyValue = Convert.ChangeType(value, Nullable.GetUnderlyingType(propertyInfo.PropertyType) ?? propertyInfo.PropertyType);
|
|
propertyInfo.SetValue(newPosition, propertyValue);
|
|
}
|
|
}
|
|
_context.ProjectPartItems.Add(newPosition);
|
|
await _context.SaveChangesAsync();
|
|
i++;
|
|
}
|
|
|
|
|
|
return RedirectToPage("./Edit", new {id = IdProject});
|
|
}
|
|
|
|
public class ExcelItem
|
|
{
|
|
public int CellIndex { get; set; }
|
|
public string Name { get; set; }
|
|
}
|
|
}
|
|
}
|