236 lines
9.2 KiB
C#
236 lines
9.2 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.CodeTable;
|
|
using Microsoft.AspNetCore.Identity;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.VisualStudio.Debugger.Contracts;
|
|
using System.Globalization;
|
|
|
|
namespace EveryThing.Pages.CodeTableItems
|
|
{
|
|
[Authorize(Roles = "Administrator,InvoicingUser,ProjecThingUser")]
|
|
public class IndexModel : PageModel
|
|
{
|
|
public class AddEditCodeTableItem
|
|
{
|
|
public CodeTableItem Item { get; set; }
|
|
public bool Edit { get; set; }
|
|
public int IdCodeTableItem { get; set; }
|
|
public IList<File> Files { get; set; }
|
|
public string MaterialPrice { get; set; }
|
|
public string WorkPrice { get; set; }
|
|
public string TotalPrice { get; set; }
|
|
public string SellingPrice { get; set; }
|
|
public string DifferenceInPricePercentage { get; set; }
|
|
public string MaterialDimensions { get; set; }
|
|
public string Material { get; set; }
|
|
}
|
|
|
|
private readonly ApplicationDbContext _context;
|
|
private readonly UserManager<IdentityApplicationUser> _userManager;
|
|
private readonly SignInManager<IdentityApplicationUser> _loginManager;
|
|
private readonly RoleManager<IdentityApplicationRole> _roleManager;
|
|
|
|
public IndexModel(ApplicationDbContext context, UserManager<IdentityApplicationUser> userManager, SignInManager<IdentityApplicationUser> loginManager, RoleManager<IdentityApplicationRole> roleManager)
|
|
{
|
|
_context = context;
|
|
_userManager = userManager;
|
|
_loginManager = loginManager;
|
|
_roleManager = roleManager;
|
|
}
|
|
|
|
public IList<CodeTableItem> Item { get;set; }
|
|
|
|
public async Task OnGetAsync(string searchString)
|
|
{
|
|
var user = _userManager.GetUserAsync(User).Result;
|
|
|
|
ViewData["SearchString"] = searchString ?? "";
|
|
|
|
var search = !string.IsNullOrEmpty(searchString);
|
|
|
|
Item = await _context.CodeTableItems
|
|
.Where(x => x.IdCompanyFk == user.IdCompanyFk
|
|
&& EF.Functions.Like(x.Title, $"%{searchString}%")
|
|
|| EF.Functions.Like(x.Description, $"%{searchString}%"))
|
|
.Include(j => j.Company)
|
|
.OrderByDescending(x => x.IdItem).Take(100).ToListAsync();
|
|
|
|
}
|
|
|
|
public IActionResult OnGetCodeTableItemModal(bool edit, int idCodeTableItem, int codeTableItemType)
|
|
{
|
|
var user = _userManager.GetUserAsync(User).Result;
|
|
|
|
CodeTableItem item = null;
|
|
IList<File> files = null;
|
|
var materialPrice = 0d;
|
|
var workPrice = 0d;
|
|
var sellingPrice = 0d;
|
|
var differenceInPricePercentage = 0d;
|
|
var materialDimensions = "";
|
|
var material = "";
|
|
|
|
if (edit)
|
|
{
|
|
item = _context.CodeTableItems
|
|
.Where(x => x.IdCompanyFk == user.IdCompanyFk)
|
|
.FirstOrDefault(x => x.IdItem == idCodeTableItem);
|
|
|
|
files = _context.Files
|
|
.Where(x => x.IdCompanyFk == user.IdCompanyFk
|
|
&& x.IdReferenceFk == item.IdItem
|
|
&& x.FileType == FileType.CodeTableItem)
|
|
.ToList();
|
|
}
|
|
|
|
if (item == null)
|
|
{
|
|
item = new CodeTableItem();
|
|
item.Active = true;
|
|
item.CodeTableItemType = (CodeTableItemType)codeTableItemType;
|
|
}
|
|
else
|
|
{
|
|
var projectPartItem = _context.ProjectPartItems
|
|
.Include(x => x.Material)
|
|
.OrderByDescending(x => x.DateModified)
|
|
.ThenByDescending(x => x.IdProjectPartItem)
|
|
.FirstOrDefault(x => x.IdItemFk == item.IdItem);
|
|
|
|
if (projectPartItem != null)
|
|
{
|
|
materialPrice = projectPartItem.MaterialPrice;
|
|
workPrice = projectPartItem.WorkPrice;
|
|
sellingPrice = projectPartItem.SellingPrice;
|
|
differenceInPricePercentage = projectPartItem.DifferenceInPricePercentage;
|
|
materialDimensions = projectPartItem.MaterialDimensions;
|
|
material = projectPartItem?.Material?.Title;
|
|
}
|
|
}
|
|
|
|
|
|
return Partial("AddEditItemModal", new AddEditCodeTableItem
|
|
{
|
|
Item = item,
|
|
Edit = edit,
|
|
IdCodeTableItem = idCodeTableItem,
|
|
Files = files,
|
|
MaterialPrice = materialPrice.ToString("#,###,##0.00", new CultureInfo("sl-SI")),
|
|
SellingPrice = sellingPrice.ToString("#,###,##0.00", new CultureInfo("sl-SI")),
|
|
TotalPrice = (materialPrice + workPrice).ToString("#,###,##0.00", new CultureInfo("sl-SI")),
|
|
WorkPrice = workPrice.ToString("#,###,##0.00", new CultureInfo("sl-SI")),
|
|
DifferenceInPricePercentage = differenceInPricePercentage.ToString("#,###,##0.00", new CultureInfo("sl-SI")),
|
|
MaterialDimensions = materialDimensions,
|
|
Material = material
|
|
});
|
|
}
|
|
|
|
public IActionResult OnGetCodeTableItem(int idCodeTableItem)
|
|
{
|
|
var user = _userManager.GetUserAsync(User).Result;
|
|
|
|
bool successful = true;
|
|
string error = "";
|
|
bool itemInUse = false;
|
|
|
|
CodeTableItem item = _context.CodeTableItems
|
|
.Where(x => x.IdCompanyFk == user.IdCompanyFk)
|
|
.Include(x => x.ItemProjectPartItem)
|
|
.Include(x => x.ItemProjectPartItemMaterial)
|
|
.Include(x => x.InvoiceItem)
|
|
.FirstOrDefault(x => x.IdItem == idCodeTableItem);
|
|
|
|
if (item == null)
|
|
{
|
|
successful = false;
|
|
error = $"Codetable item with ID: {idCodeTableItem} not found";
|
|
}
|
|
else
|
|
{
|
|
itemInUse = item.ItemProjectPartItem.Count > 0 || item.ItemProjectPartItemMaterial.Count > 0 || item.InvoiceItem.Count > 0;
|
|
//Cene se json zacikla neki IDK.
|
|
item.ItemProjectPartItem = null;
|
|
item.ItemProjectPartItemMaterial = null;
|
|
item.InvoiceItem = null;
|
|
}
|
|
|
|
return new JsonResult(new { item = item, error = error, successful = successful, itemInUse = itemInUse });
|
|
}
|
|
|
|
public IActionResult OnPostCodeTableItem(string title, string description, bool active, bool edit, int idCodeTableItem, int codeTableItemType)
|
|
{
|
|
var user = _userManager.GetUserAsync(User).Result;
|
|
bool successful = true;
|
|
string error = "";
|
|
if (edit)
|
|
{
|
|
var item = _context.CodeTableItems
|
|
.Where(x => x.IdCompanyFk == user.IdCompanyFk)
|
|
.FirstOrDefault(x => x.IdItem == idCodeTableItem);
|
|
if (item != null)
|
|
{
|
|
item.Title = title;
|
|
item.Description = description;
|
|
item.Active = active;
|
|
item.CodeTableItemType = (CodeTableItemType)codeTableItemType;
|
|
_context.SaveChanges();
|
|
}
|
|
else
|
|
{
|
|
successful = false;
|
|
error = $"Codetable item with ID: {idCodeTableItem} not found";
|
|
}
|
|
}
|
|
else
|
|
{
|
|
var item = new CodeTableItem
|
|
{
|
|
Title = title,
|
|
Description = description,
|
|
Active = active,
|
|
IdCompanyFk = user.IdCompanyFk,
|
|
CodeTableItemType = (CodeTableItemType)codeTableItemType
|
|
};
|
|
_context.CodeTableItems.Add(item);
|
|
_context.SaveChanges();
|
|
|
|
idCodeTableItem = item.IdItem;
|
|
}
|
|
|
|
return new JsonResult(new { idCodeTableItem = idCodeTableItem, error = error, successful = successful });
|
|
}
|
|
|
|
public IActionResult OnDeleteCodeTableItem(int idCodeTableItem)
|
|
{
|
|
var user = _userManager.GetUserAsync(User).Result;
|
|
bool successful = true;
|
|
string error = "";
|
|
|
|
var item = _context.CodeTableItems
|
|
.Where(x => x.IdCompanyFk == user.IdCompanyFk)
|
|
.FirstOrDefault(x => x.IdItem == idCodeTableItem);
|
|
if (item != null)
|
|
{
|
|
_context.CodeTableItems.Remove(item);
|
|
_context.SaveChanges();
|
|
}
|
|
else
|
|
{
|
|
successful = false;
|
|
error = $"Codetable item with ID: {idCodeTableItem} not found";
|
|
}
|
|
|
|
return new JsonResult(new { idCodeTableItem = idCodeTableItem, error = error, successful = successful });
|
|
}
|
|
|
|
}
|
|
}
|