rename to projecting
This commit is contained in:
333
ProjecThing/Pages/Projects/CreateEditPartItem.cshtml.cs
Normal file
333
ProjecThing/Pages/Projects/CreateEditPartItem.cshtml.cs
Normal file
@@ -0,0 +1,333 @@
|
||||
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 ProjecThing.Data;
|
||||
using ProjecThing.Models;
|
||||
using ProjecThing.Models.CodeTable;
|
||||
using ProjecThing.Models.Project;
|
||||
using ProjecThing.Models.Invoice;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace ProjecThing.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["IdCodeTableOperationFk"] = new SelectList(_context.CodeTableOperations
|
||||
.OrderBy(x => x.Title), "Id", "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();
|
||||
}
|
||||
|
||||
ProjectPartItemOperations = _context.ProjectPartItemOperations
|
||||
.Include(x => x.Operation)
|
||||
.Where(x => x.IdProjectPartItemFk == ProjectPartItem.IdProjectPartItem)
|
||||
.OrderBy(x => x.Order)
|
||||
.ToList();
|
||||
|
||||
var usedOperationIds = ProjectPartItemOperations.Select(x => x.IdCodeTableOperationFk).ToList();
|
||||
ViewData["IdCodeTableOperationFk"] = new SelectList(_context.CodeTableOperations
|
||||
.Where(x => !usedOperationIds.Contains(x.Id))
|
||||
.OrderBy(x => x.Title), "Id", "Title");
|
||||
|
||||
CooperationInvoiceItems = _context.InvoiceItems
|
||||
.Include(x => x.Invoice)
|
||||
.ThenInclude(x => x.Partner)
|
||||
.Where(x => x.IdProjectPartItem == ProjectPartItem.IdProjectPartItem
|
||||
&& x.Invoice.Type == Invoice.InvoiceType.Cooperation)
|
||||
.ToList();
|
||||
}
|
||||
else
|
||||
{
|
||||
ProjectPartItem = new ProjectPartItem
|
||||
{
|
||||
NumberOfSets = 1,
|
||||
IdProjectPartFk = idProjectPart
|
||||
};
|
||||
ProjectPartItemOperations = new List<ProjectPartItemOperation>();
|
||||
CooperationInvoiceItems = new List<InvoiceItem>();
|
||||
}
|
||||
|
||||
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 IList<ProjectPartItemOperation> ProjectPartItemOperations { get; set; }
|
||||
|
||||
public IList<InvoiceItem> CooperationInvoiceItems { 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 });
|
||||
}
|
||||
|
||||
public IActionResult OnPostProjectPartItemOperation(int idProjectPartItem, int idCodeTableOperation)
|
||||
{
|
||||
var maxOrder = _context.ProjectPartItemOperations
|
||||
.Where(x => x.IdProjectPartItemFk == idProjectPartItem)
|
||||
.Select(x => (short?)x.Order)
|
||||
.Max() ?? 0;
|
||||
|
||||
var operation = new ProjectPartItemOperation
|
||||
{
|
||||
IdProjectPartItemFk = idProjectPartItem,
|
||||
IdCodeTableOperationFk = idCodeTableOperation,
|
||||
Finished = false,
|
||||
Order = (short)(maxOrder + 1)
|
||||
};
|
||||
|
||||
_context.ProjectPartItemOperations.Add(operation);
|
||||
_context.SaveChanges();
|
||||
|
||||
var title = _context.CodeTableOperations
|
||||
.Where(x => x.Id == idCodeTableOperation)
|
||||
.Select(x => x.Title)
|
||||
.FirstOrDefault();
|
||||
|
||||
return new JsonResult(new { successful = true, idProjectPartItemOperation = operation.Id, title });
|
||||
}
|
||||
|
||||
public IActionResult OnPostToggleProjectPartItemOperationFinished(int idProjectPartItemOperation)
|
||||
{
|
||||
var successful = true;
|
||||
var error = "";
|
||||
|
||||
var operation = _context.ProjectPartItemOperations
|
||||
.FirstOrDefault(x => x.Id == idProjectPartItemOperation);
|
||||
|
||||
if (operation != null)
|
||||
{
|
||||
operation.Finished = !operation.Finished;
|
||||
_context.SaveChanges();
|
||||
}
|
||||
else
|
||||
{
|
||||
successful = false;
|
||||
error = $"Operation with ID: {idProjectPartItemOperation} not found";
|
||||
}
|
||||
|
||||
return new JsonResult(new { successful, error, finished = operation?.Finished });
|
||||
}
|
||||
|
||||
public IActionResult OnDeleteProjectPartItemOperation(int idProjectPartItemOperation)
|
||||
{
|
||||
var successful = true;
|
||||
var error = "";
|
||||
|
||||
var operation = _context.ProjectPartItemOperations
|
||||
.FirstOrDefault(x => x.Id == idProjectPartItemOperation);
|
||||
|
||||
if (operation != null)
|
||||
{
|
||||
_context.ProjectPartItemOperations.Remove(operation);
|
||||
_context.SaveChanges();
|
||||
}
|
||||
else
|
||||
{
|
||||
successful = false;
|
||||
error = $"Operation with ID: {idProjectPartItemOperation} not found";
|
||||
}
|
||||
|
||||
return new JsonResult(new { successful, error, idProjectPartItemOperation });
|
||||
}
|
||||
|
||||
public IActionResult OnPostAddDefaultOperations(int idProjectPartItem)
|
||||
{
|
||||
var existingOperationIds = _context.ProjectPartItemOperations
|
||||
.Where(x => x.IdProjectPartItemFk == idProjectPartItem)
|
||||
.Select(x => x.IdCodeTableOperationFk)
|
||||
.ToList();
|
||||
|
||||
var defaultOperations = _context.CodeTableOperations
|
||||
.Where(x => x.Default && !existingOperationIds.Contains(x.Id))
|
||||
.OrderBy(x => x.Title)
|
||||
.ToList();
|
||||
|
||||
var added = new List<object>();
|
||||
|
||||
var maxOrder = _context.ProjectPartItemOperations
|
||||
.Where(x => x.IdProjectPartItemFk == idProjectPartItem)
|
||||
.Select(x => (short?)x.Order)
|
||||
.Max() ?? 0;
|
||||
|
||||
foreach (var op in defaultOperations)
|
||||
{
|
||||
maxOrder++;
|
||||
var newOp = new ProjectPartItemOperation
|
||||
{
|
||||
IdProjectPartItemFk = idProjectPartItem,
|
||||
IdCodeTableOperationFk = op.Id,
|
||||
Finished = false,
|
||||
Order = maxOrder
|
||||
};
|
||||
_context.ProjectPartItemOperations.Add(newOp);
|
||||
_context.SaveChanges();
|
||||
|
||||
added.Add(new { idProjectPartItemOperation = newOp.Id, idCodeTableOperation = op.Id, title = op.Title });
|
||||
}
|
||||
|
||||
return new JsonResult(new { successful = true, operations = added });
|
||||
}
|
||||
|
||||
public IActionResult OnPostMoveOperation(int idProjectPartItemOperation, string direction)
|
||||
{
|
||||
var operation = _context.ProjectPartItemOperations
|
||||
.FirstOrDefault(x => x.Id == idProjectPartItemOperation);
|
||||
|
||||
if (operation == null)
|
||||
return new JsonResult(new { successful = false, error = "Operation not found" });
|
||||
|
||||
ProjectPartItemOperation neighbor;
|
||||
|
||||
if (direction == "up")
|
||||
{
|
||||
neighbor = _context.ProjectPartItemOperations
|
||||
.Where(x => x.IdProjectPartItemFk == operation.IdProjectPartItemFk && x.Order < operation.Order)
|
||||
.OrderByDescending(x => x.Order)
|
||||
.FirstOrDefault();
|
||||
}
|
||||
else
|
||||
{
|
||||
neighbor = _context.ProjectPartItemOperations
|
||||
.Where(x => x.IdProjectPartItemFk == operation.IdProjectPartItemFk && x.Order > operation.Order)
|
||||
.OrderBy(x => x.Order)
|
||||
.FirstOrDefault();
|
||||
}
|
||||
|
||||
if (neighbor == null)
|
||||
return new JsonResult(new { successful = true });
|
||||
|
||||
var temp = operation.Order;
|
||||
operation.Order = neighbor.Order;
|
||||
neighbor.Order = temp;
|
||||
_context.SaveChanges();
|
||||
|
||||
return new JsonResult(new { successful = true });
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user