using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using ODCWeb.DataAccess.Repository.IRepository; using ODCWeb.Models; using ODCWeb.Models.ViewModels; using System.Data; namespace ODCWeb.Areas.Staff.Controllers {/////////////////////////////////////// DISCONTINUED [Area("Staff")] [Authorize(Roles = "Admin")] public class BQItemController : Controller { private readonly IUnitOfWork _unitOfWork; public BQItemController(IUnitOfWork unitOfWork) { _unitOfWork = unitOfWork; } public IActionResult Index(int sectionId) { var section = _unitOfWork.BQSection.Get( s => s.BQSectionId == sectionId, includeProperties: "Template" ); if (section == null) return NotFound(); ViewBag.Section = section; ViewBag.Template = section.Template; var items = _unitOfWork.BQItem .Where(i => i.BQSectionId == sectionId) .OrderBy(i => i.OrderIndex) .ToList(); return View(items); } [HttpGet] public IActionResult Create(int sectionId) { var section = _unitOfWork.BQSection.Get(s => s.BQSectionId == sectionId, includeProperties: "Template"); if (section == null) return NotFound(); var viewModel = new BQItemViewModel { Item = new BQItem { BQSectionId = sectionId }, AvailableTasks = _unitOfWork.Task.GetAll().ToList(), // Get actual tasks TaskMappings = new List() }; ViewBag.Section = section; ViewBag.Template = section.Template; return View(viewModel); } [HttpPost] [ValidateAntiForgeryToken] public IActionResult Create(BQItemViewModel viewModel, List selectedTasks, string quantityFormula) { if (ModelState.IsValid) { viewModel.Item.OrderIndex = _unitOfWork.BQItem.GetAll() .Where(i => i.BQSectionId == viewModel.Item.BQSectionId) .Count() + 1; viewModel.Item.QuantityFormula = quantityFormula; _unitOfWork.BQItem.Add(viewModel.Item); _unitOfWork.save(); foreach (var taskId in selectedTasks) { var mapping = new BQTaskMapping { BQItemId = viewModel.Item.BQItemId, TaskId = taskId }; _unitOfWork.BQTaskMapping.Add(mapping); } _unitOfWork.save(); TempData["success"] = "BQ Item created successfully"; return RedirectToAction(nameof(Index), new { sectionId = viewModel.Item.BQSectionId }); } var section = _unitOfWork.BQSection.Get(s => s.BQSectionId == viewModel.Item.BQSectionId, includeProperties: "Template"); ViewBag.Section = section; ViewBag.Template = section.Template; viewModel.AvailableTasks = _unitOfWork.Task.GetAll().ToList(); return View(viewModel); } [HttpGet] public JsonResult GetTaskInfo(int taskId) { var task = _unitOfWork.Task.Get(t => t.TaskId == taskId); if (task == null) return Json(null); var taskInfo = new TaskFormulaInfo { TaskId = task.TaskId, TaskName = task.Name, ReferenceId = task.Reference_Id, CurrentValue = task.Measured_Work }; return Json(taskInfo); } [HttpGet] public IActionResult Edit(int id) { var item = _unitOfWork.BQItem.Get( i => i.BQItemId == id, includeProperties: "Section,Section.Template,TaskMappings" ); if (item == null) return NotFound(); // Get existing task mappings var selectedTaskIds = item.TaskMappings.Select(m => m.TaskId).ToList(); var viewModel = new BQItemViewModel { Item = item, AvailableTasks = _unitOfWork.Task.GetAll().ToList(), TaskMappings = item.TaskMappings.ToList(), QuantityFormula = item.QuantityFormula }; ViewBag.Section = item.Section; ViewBag.Template = item.Section.Template; return View("Create", viewModel); // Reuse Create view } [HttpPost] [ValidateAntiForgeryToken] public IActionResult Edit(int BQItemId, List selectedTasks, string quantityFormula) // Change parameters { var item = _unitOfWork.BQItem.Get(u => u.BQItemId == BQItemId); if (item == null) return NotFound(); var existingMappings = new List { }; if (ModelState.IsValid) { // Update basic item info item.QuantityFormula = quantityFormula; _unitOfWork.BQItem.update(item); // Remove existing task mappings existingMappings = _unitOfWork.BQTaskMapping.GetAll() .Where(m => m.BQItemId == BQItemId) .ToList(); foreach (var mapping in existingMappings) { _unitOfWork.BQTaskMapping.Remove(mapping); } // Add new task mappings foreach (var taskId in selectedTasks) { var mapping = new BQTaskMapping { BQItemId = BQItemId, TaskId = taskId }; _unitOfWork.BQTaskMapping.Add(mapping); } _unitOfWork.save(); TempData["success"] = "BQ Item updated successfully"; return RedirectToAction(nameof(Index), new { sectionId = item.BQSectionId }); } // If we get here, something failed, redisplay form var viewModel = new BQItemViewModel { Item = item, AvailableTasks = _unitOfWork.Task.GetAll().ToList(), TaskMappings = existingMappings }; var section = _unitOfWork.BQSection.Get(s => s.BQSectionId == item.BQSectionId, includeProperties: "Template"); ViewBag.Section = section; ViewBag.Template = section.Template; return View("Create", viewModel); } [HttpGet] public IActionResult UpdatePropertyMappings(int id) { var bqItem = _unitOfWork.BQItem.Get(i => i.BQItemId == id); if (bqItem == null) { TempData["error"] = "BQ Item not found"; return RedirectToAction("Index"); } // Get tasks that are mapped to this BQ Item var taskMappings = _unitOfWork.BQTaskMapping.Where(m => m.BQItemId == id).ToList(); if (!taskMappings.Any()) { TempData["error"] = "No tasks mapped to this BQ Item. Please select tasks in Edit BQ Item first."; return RedirectToAction("Edit", new { id = id }); } // Get the actual tasks from the mappings var availableTasks = new List(); foreach (var mapping in taskMappings) { var task = _unitOfWork.Task.Get(t => t.TaskId == mapping.TaskId); if (task != null) { availableTasks.Add(task); } } var existingMappings = _unitOfWork.BQPropertyMapping .Where(m => m.BQItemId == id) .ToList() ?? new List(); var viewModel = new PropertyMappingViewModel { BQItemId = id, AvailableTasks = availableTasks, ExistingMappings = existingMappings, Formula = bqItem.QuantityFormula ?? "", OriginalFormula = bqItem.QuantityFormula ?? "" }; return View(viewModel); } private List GetAvailableTasks() { try { // Get all tasks from all projects var tasks = new List(); var floors = _unitOfWork.Floor.GetAll(); foreach (var floor in floors) { var actions = _unitOfWork.Action.Where(a => a.FloorId == floor.FloorId); if (actions != null) { foreach (var action in actions) { var actionTasks = _unitOfWork.Task.Where(t => t.ActionId == action.ActionId); if (actionTasks != null) { tasks.AddRange(actionTasks); } } } } return tasks; } catch (Exception ex) { // Log the exception if you have logging configured return new List(); } } [HttpPost] [ValidateAntiForgeryToken] public IActionResult UpdatePropertyMappings(PropertyMappingViewModel viewModel) { if (!ModelState.IsValid) return View(viewModel); var bqItem = _unitOfWork.BQItem.Get(i => i.BQItemId == viewModel.BQItemId); if (bqItem == null) return NotFound(); // Remove existing mappings var existingMappings = _unitOfWork.BQPropertyMapping .Where(m => m.BQItemId == viewModel.BQItemId).ToList(); foreach (var mapping in existingMappings) { _unitOfWork.BQPropertyMapping.Remove(mapping); } // Add new mappings from the form foreach (var mapping in viewModel.ExistingMappings) { var newMapping = new BQPropertyMapping { BQItemId = viewModel.BQItemId, TaskId = mapping.TaskId, PropertyId = mapping.PropertyId, PropertyAlias = mapping.PropertyAlias }; _unitOfWork.BQPropertyMapping.Add(newMapping); } // Update formula bqItem.QuantityFormula = viewModel.Formula; _unitOfWork.BQItem.update(bqItem); _unitOfWork.save(); TempData["success"] = "Property mappings updated successfully"; return RedirectToAction("Edit", new { id = viewModel.BQItemId }); } [HttpGet] public JsonResult GetTaskProperties(int taskId) { var properties = _unitOfWork.Property .Where(p => p.TaskId == taskId && p.Visible == true) .Select(p => new PropertyFormulaViewModel { PropertyId = p.PropertyId, Name = p.Name, Value = p.Value, TaskId = p.TaskId, TaskName = _unitOfWork.Task.Get(t => t.TaskId == p.TaskId).Name }) .ToList(); return Json(properties); } [HttpPost] public JsonResult TestFormula([FromBody] Dictionary formulaData) { try { var formula = formulaData["formula"]; var testValues = new Dictionary(); foreach (var pair in formulaData) { if (pair.Key != "formula" && double.TryParse(pair.Value, out double value)) { testValues.Add(pair.Key, value); } } foreach (var alias in testValues.Keys) { formula = formula.Replace(alias, testValues[alias].ToString()); } DataTable dt = new DataTable(); var result = dt.Compute(formula, ""); return Json(new { success = true, result = Convert.ToDouble(result) }); } catch (Exception ex) { return Json(new { success = false, error = ex.Message }); } } } }