253 lines
10 KiB
C#
253 lines
10 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 Microsoft.AspNetCore.Mvc.Rendering;
|
|
using DocumentFormat.OpenXml.Bibliography;
|
|
|
|
namespace EveryThing.Pages.Projects
|
|
{
|
|
[Authorize(Roles = "Administrator,ProjecThingUser")]
|
|
public class IndexModel : PageModel
|
|
{
|
|
private readonly ApplicationDbContext _context;
|
|
private readonly UserManager<IdentityApplicationUser> _userManager;
|
|
|
|
public IndexModel(ApplicationDbContext context, UserManager<IdentityApplicationUser> userManager)
|
|
{
|
|
_context = context;
|
|
_userManager = userManager;
|
|
}
|
|
|
|
public IList<Project> Project { get;set; }
|
|
public string Year { get; set; }
|
|
|
|
|
|
public async Task OnGetAsync(string searchString, string finishedProjects, string openProjects, string offerProjects, string inProductionProjects, string year)
|
|
{
|
|
ViewData["SearchString"] = searchString;
|
|
ViewData["FinishedProjects"] = finishedProjects == "on" ? "checked" : "";
|
|
ViewData["OpenProjects"] = openProjects == "on" ? "checked" : "";
|
|
ViewData["OffersProjects"] = offerProjects == "on" ? "checked" : "";
|
|
ViewData["InProductionProjects"] = inProductionProjects == "on" ? "checked" : "";
|
|
|
|
Year = year;
|
|
Year ??= DateTime.Now.Year.ToString();
|
|
|
|
ViewData["Years"] = new SelectList(Enumerable.Range(2022, (DateTime.Now.Year - 2022) + 2));
|
|
|
|
Project = new List<Project>();
|
|
|
|
await _context.Projects
|
|
.Include(p => p.Company)
|
|
.Include(p => p.Partner)
|
|
.Include(x => x.ProjectProjectPart)
|
|
.ThenInclude(x => x.ProjectPartProjectPartItem)
|
|
.Where(x => x.ProjectYear == Convert.ToInt32(Year))
|
|
.ForEachAsync(x =>
|
|
{
|
|
x.FirstDeliveryDate = x.ProjectProjectPart.Max(y => y.ProjectPartProjectPartItem
|
|
.Where(a => a.Status == ProjectPartItemStatus.InProduction ||
|
|
a.Status == ProjectPartItemStatus.Opened).Max(z => z.DeliveryDate)) == null
|
|
? DateTime.MaxValue
|
|
: x.ProjectProjectPart.Max(y =>
|
|
y.ProjectPartProjectPartItem.Where(a =>
|
|
a.Status == ProjectPartItemStatus.InProduction ||
|
|
a.Status == ProjectPartItemStatus.Opened).Max(z => z.DeliveryDate));
|
|
Project.Add(x);
|
|
});
|
|
|
|
Project = Project.OrderBy(x => (int)x.Status)
|
|
.ThenBy(x => x.FirstDeliveryDate)
|
|
.ToList();
|
|
|
|
var listStatus = new List<ProjectStatus>();
|
|
// Active companies
|
|
if (!string.IsNullOrEmpty(finishedProjects)
|
|
&& finishedProjects == "on")
|
|
{
|
|
listStatus.Add(ProjectStatus.Finished);
|
|
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(openProjects)
|
|
&& openProjects == "on")
|
|
{
|
|
listStatus.Add(ProjectStatus.Opened);
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(offerProjects)
|
|
&& offerProjects == "on")
|
|
{
|
|
listStatus.Add(ProjectStatus.Offer);
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(inProductionProjects)
|
|
&& inProductionProjects == "on")
|
|
{
|
|
listStatus.Add(ProjectStatus.InProduction);
|
|
}
|
|
|
|
if (listStatus.Count > 0)
|
|
{
|
|
Project = Project.Where(s => listStatus.Contains(s.Status)).ToList();
|
|
}
|
|
|
|
// Search string
|
|
if (!string.IsNullOrEmpty(searchString))
|
|
{
|
|
Project = Project
|
|
.Where(s => s.Title.Contains(searchString, StringComparison.InvariantCultureIgnoreCase)
|
|
|| s.Partner.Title.Contains(searchString, StringComparison.InvariantCultureIgnoreCase)
|
|
|| (s.Description != null && s.Description.Contains(searchString, StringComparison.InvariantCultureIgnoreCase))
|
|
|| s.ProjectNumberFormatted.Contains(searchString, StringComparison.CurrentCultureIgnoreCase))
|
|
.ToList();
|
|
}
|
|
}
|
|
|
|
public IActionResult OnDeleteProject(int idProject)
|
|
{
|
|
var user = _userManager.GetUserAsync(User).Result;
|
|
bool successful = true;
|
|
string error = "";
|
|
|
|
var project = _context.Projects
|
|
.Include(x => x.ProjectProjectPart)
|
|
.ThenInclude(x => x.ProjectPartProjectPartItem)
|
|
.ThenInclude(x => x.InvoiceItem)
|
|
.Where(x => x.IdCompanyFk == user.IdCompanyFk)
|
|
.FirstOrDefault(x => x.IdProject == idProject);
|
|
|
|
|
|
if (project == null)
|
|
{
|
|
successful = false;
|
|
error = $"Project with ID: {idProject} not found";
|
|
}
|
|
else if (project.ProjectProjectPart.Any(x => x.ProjectPartProjectPartItem.Any(x => x.InvoiceItem.Count > 0)))
|
|
{
|
|
successful = false;
|
|
error = $"Projekt ima vezane dokumente fakturiranja!\nBrisanje ni možno!";
|
|
}
|
|
else
|
|
{
|
|
var projectParts = _context.ProjectParts.Where(x => x.IdProjectFk == idProject).ToList();
|
|
|
|
for (int i = 0; i < projectParts.Count(); i++)
|
|
{
|
|
var part = projectParts[i];
|
|
var projectPartItems = _context.ProjectPartItems.Where(x => x.IdProjectPartFk == part.IdProjectPart).ToList();
|
|
|
|
for (int j = 0; j < projectPartItems.Count; j++)
|
|
{
|
|
_context.ProjectPartItems.Remove(projectPartItems[j]);
|
|
}
|
|
|
|
_context.ProjectParts.Remove(part);
|
|
}
|
|
_context.Projects.Remove(project);
|
|
_context.SaveChanges();
|
|
}
|
|
|
|
return new JsonResult(new { idProject = idProject, error = error, successful = successful });
|
|
}
|
|
|
|
public IActionResult OnPostCopyProject(int idProject)
|
|
{
|
|
var user = _userManager.GetUserAsync(User).Result;
|
|
bool successful = true;
|
|
string error = "";
|
|
|
|
var project = _context.Projects
|
|
.FirstOrDefault(x => x.IdCompanyFk == user.IdCompanyFk && x.IdProject == idProject);
|
|
|
|
if (project != null)
|
|
{
|
|
var newProject = new Project
|
|
{
|
|
IdCompanyFk = user.IdCompanyFk,
|
|
Status = ProjectStatus.Opened,
|
|
BuyersOrderNumber = project.BuyersOrderNumber,
|
|
Description = project.Description,
|
|
IdPartnerFk = project.IdPartnerFk,
|
|
ProjectYear = DateTime.Now.Year,
|
|
ProjectNumber = _context.Projects.Where(x => x.IdCompanyFk == user.IdCompanyFk && x.ProjectYear == DateTime.Now.Year).Max(x => x.ProjectNumber) + 1,
|
|
Title = project.Title,
|
|
};
|
|
|
|
if (newProject.ProjectNumber <= 0)
|
|
newProject.ProjectNumber = 1;
|
|
|
|
_context.Projects.Add(newProject);
|
|
|
|
_context.SaveChanges();
|
|
|
|
var projectParts = _context.ProjectParts.Where(x => x.IdProjectFk == project.IdProject)
|
|
.OrderBy(x => x.ProjectPartNumber).ThenBy(x => x.IdProjectFk).ToList();
|
|
|
|
var projectPartCounter = 1;
|
|
foreach (var projectPart in projectParts)
|
|
{
|
|
var newProjectPart = new ProjectPart
|
|
{
|
|
IdProjectFk = newProject.IdProject,
|
|
Description = projectPart.Description,
|
|
//FinishedDate = item.FinishedDate,
|
|
PathOfPlans = projectPart.PathOfPlans,
|
|
ShippedDate = projectPart.ShippedDate,
|
|
Status = projectPart.Status,
|
|
Title = projectPart.Title,
|
|
ProjectPartNumber = projectPartCounter
|
|
};
|
|
|
|
_context.ProjectParts.Add(newProjectPart);
|
|
|
|
_context.SaveChanges();
|
|
|
|
var projectPartItemCounter = 1;
|
|
foreach (var projectPartItem in _context.ProjectPartItems.Where(x => x.IdProjectPartFk == projectPart.IdProjectPart).ToList())
|
|
{
|
|
var newProjectPartItem = new ProjectPartItem
|
|
{
|
|
IdItemFk = projectPartItem.IdItemFk,
|
|
IdMaterialFk = projectPartItem.IdMaterialFk,
|
|
IdMaterialSupplierFk = projectPartItem.IdMaterialSupplierFk,
|
|
IdProjectPartFk = newProjectPart.IdProjectPart,
|
|
MaterialDimensions = projectPartItem.MaterialDimensions,
|
|
MaterialPrice = projectPartItem.MaterialPrice,
|
|
NumberOfItems = projectPartItem.NumberOfItems,
|
|
NumberOfItemsFinished = projectPartItem.NumberOfItemsFinished,
|
|
WorkPrice = projectPartItem.WorkPrice,
|
|
NumberOfSets = projectPartItem.NumberOfSets,
|
|
Status = ProjectPartItemStatus.Opened,
|
|
SellingPrice = projectPartItem.SellingPrice,
|
|
ProjectPartItemNumber = projectPartItemCounter
|
|
};
|
|
_context.ProjectPartItems.Add(newProjectPartItem);
|
|
|
|
projectPartItemCounter++;
|
|
}
|
|
_context.SaveChanges();
|
|
|
|
projectPartCounter++;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
successful = false;
|
|
error = $"Project with ID: {idProject} not found";
|
|
}
|
|
|
|
return new JsonResult(new { error, successful });
|
|
}
|
|
}
|
|
}
|