163 lines
6.3 KiB
C#
163 lines
6.3 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.CodeTable;
|
|
using EveryThing.Models.Project;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace EveryThing.Pages.Projects
|
|
{
|
|
[Authorize(Roles = "Administrator,ProjecThingUser")]
|
|
public class CreateEditPartItemModel : PageModel
|
|
{
|
|
private readonly ApplicationDbContext _context;
|
|
private readonly UserManager<IdentityApplicationUser> _userManager;
|
|
private readonly SignInManager<IdentityApplicationUser> _loginManager;
|
|
private readonly RoleManager<IdentityApplicationRole> _roleManager;
|
|
|
|
public CreateEditPartItemModel(ApplicationDbContext context, UserManager<IdentityApplicationUser> userManager, SignInManager<IdentityApplicationUser> loginManager, RoleManager<IdentityApplicationRole> roleManager)
|
|
{
|
|
_context = context;
|
|
_userManager = userManager;
|
|
_loginManager = loginManager;
|
|
_roleManager = roleManager;
|
|
}
|
|
|
|
public IActionResult OnGet(int idProject, int idProjectPart, bool edit, int? idProjectPartItem, int? idArticleCopyFrom)
|
|
{
|
|
var user = _userManager.GetUserAsync(User).Result;
|
|
|
|
IdProject = idProject;
|
|
ViewData["IdProject"] = idProject;
|
|
ViewData["IdItemFk"] = new SelectList(_context.CodeTableItems
|
|
.Where(x => x.IdCompanyFk == user.IdCompanyFk && x.Active && x.CodeTableItemType == CodeTableItemType.Product)
|
|
.OrderBy(x => x.Title), "IdItem", "Title");
|
|
ViewData["IdMaterialFk"] = new SelectList(_context.CodeTableItems
|
|
.Where(x => x.IdCompanyFk == user.IdCompanyFk && x.Active && x.CodeTableItemType == CodeTableItemType.Material)
|
|
.OrderBy(x => x.Title), "IdItem", "Title");
|
|
ViewData["IdMaterialSupplierFk"] = new SelectList(_context.CodeTablePartners
|
|
.Where(x => x.IdCompanyFk == user.IdCompanyFk && x.Active && x.Supplier)
|
|
.OrderBy(x => x.Title), "IdPartner", "Title");
|
|
|
|
ViewData["Edit"] = edit;
|
|
|
|
if (edit)
|
|
{
|
|
ProjectPartItem = _context.ProjectPartItems
|
|
.Include(x => x.ProjectPart)
|
|
.ThenInclude(x => x.Project)
|
|
.FirstOrDefault(x => x.IdProjectPartItem == idProjectPartItem);
|
|
|
|
if (ProjectPartItem == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
ProjectPartItem = new ProjectPartItem
|
|
{
|
|
NumberOfSets = 1,
|
|
IdProjectPartFk = idProjectPart
|
|
};
|
|
}
|
|
|
|
if (idArticleCopyFrom != null)
|
|
{
|
|
var item = _context.ProjectPartItems
|
|
.OrderByDescending(x => x.DateModified)
|
|
.ThenByDescending(x => x.IdProjectPartItem)
|
|
.FirstOrDefault(x => x.IdItemFk == idArticleCopyFrom && (!edit || x.IdProjectPartFk != ProjectPartItem.IdProjectPartFk));
|
|
|
|
if (item != null)
|
|
{
|
|
ProjectPartItem.IdItemFk = item.IdItemFk;
|
|
ProjectPartItem.NumberOfItems = item.NumberOfItems;
|
|
ProjectPartItem.NumberOfSets = item.NumberOfSets;
|
|
ProjectPartItem.IdMaterialFk = item.IdMaterialFk;
|
|
ProjectPartItem.MaterialDimensions = item.MaterialDimensions;
|
|
ProjectPartItem.IdMaterialSupplierFk = item.IdMaterialSupplierFk;
|
|
ProjectPartItem.MaterialPrice = item.MaterialPrice;
|
|
ProjectPartItem.WorkPrice = item.WorkPrice;
|
|
ProjectPartItem.SellingPrice = item.SellingPrice;
|
|
}
|
|
}
|
|
|
|
|
|
return Page();
|
|
}
|
|
|
|
[BindProperty]
|
|
public Models.Project.ProjectPartItem ProjectPartItem { get; set; }
|
|
|
|
[BindProperty]
|
|
public int IdProject { get; set; }
|
|
|
|
public async Task<IActionResult> OnPostAsync(bool edit)
|
|
{
|
|
if (!ModelState.IsValid)
|
|
{
|
|
return Page();
|
|
}
|
|
ProjectPartItem.DateModified = DateTime.Now;
|
|
var user = _userManager.GetUserAsync(User).Result;
|
|
if (!edit)
|
|
{
|
|
ProjectPartItem.Status = ProjectPartItemStatus.Opened;
|
|
|
|
var items = _context.ProjectPartItems
|
|
.Where(x => x.IdProjectPartFk == ProjectPartItem.IdProjectPartFk).ToList();
|
|
|
|
ProjectPartItem.ProjectPartItemNumber = items.Count <= 0 ? 1 : items.Max(x => x.ProjectPartItemNumber) + 1;
|
|
_context.ProjectPartItems.Add(ProjectPartItem);
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
else
|
|
{
|
|
_context.Attach(ProjectPartItem).State = EntityState.Modified;
|
|
|
|
try
|
|
{
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
catch (DbUpdateConcurrencyException)
|
|
{
|
|
if (!ProjectPartItemExists(ProjectPartItem.IdProjectPartItem))
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
throw;
|
|
}
|
|
}
|
|
|
|
return RedirectToPage("./Edit", new { id = IdProject });
|
|
}
|
|
|
|
private bool ProjectPartItemExists(int id)
|
|
{
|
|
return _context.ProjectPartItems.Any(e => e.IdProjectPartItem == id);
|
|
}
|
|
|
|
public IActionResult OnGetCodeTableItems(int type)
|
|
{
|
|
var user = _userManager.GetUserAsync(User).Result;
|
|
var codeTableItemType = (CodeTableItemType)type;
|
|
|
|
var items = new SelectList(_context.CodeTableItems
|
|
.Where(x => x.IdCompanyFk == user.IdCompanyFk && x.Active && x.CodeTableItemType == codeTableItemType)
|
|
.OrderBy(x => x.Title), "IdItem", "Title");
|
|
|
|
return new JsonResult(new { items = items });
|
|
}
|
|
}
|
|
}
|