Mango.Nop.Plugins/Nop.Plugin.Misc.AIPlugin/Areas/Admin/Controllers/ShippingController.cs

392 lines
16 KiB
C#

using FruitBank.Common.Entities;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Nop.Plugin.Misc.FruitBankPlugin.Areas.Admin.Models;
using Nop.Plugin.Misc.FruitBankPlugin.Domains.DataLayer;
using Nop.Services.Messages;
using Nop.Services.Security;
using Nop.Web.Areas.Admin.Controllers;
using Nop.Web.Areas.Admin.Models.Orders;
using Nop.Web.Framework;
using Nop.Web.Framework.Models;
using Nop.Web.Framework.Models.Extensions;
using Nop.Web.Framework.Mvc.Filters;
namespace Nop.Plugin.Misc.FruitBankPlugin.Areas.Admin.Controllers
{
[Area(AreaNames.ADMIN)]
[AuthorizeAdmin]
public class ShippingController : BaseAdminController
{
private readonly IPermissionService _permissionService;
protected readonly INotificationService _notificationService;
protected readonly ShippingItemDbTable _shippingItemDbTable;
protected readonly ShippingDbTable _shippingDbTable;
protected readonly ShippingDocumentDbTable _shippingDocumentDbTable;
protected readonly FruitBankDbContext _dbContext;
//private readonly IFruitBankShippingModelFactory _shippingModelFactory;
// TODO: Add your shipment and document services here
// private readonly IShipmentService _shipmentService;
// private readonly IShipmentDocumentService _documentService;
public ShippingController(IPermissionService permissionService, INotificationService notificationService, ShippingItemDbTable shippingItemDbTable, ShippingDbTable shippingDbTable, ShippingDocumentDbTable shippingDocumentDbTable, FruitBankDbContext dbContext)
{
_permissionService = permissionService;
_notificationService = notificationService;
_shippingItemDbTable = shippingItemDbTable;
_shippingDbTable = shippingDbTable;
_shippingDocumentDbTable = shippingDocumentDbTable;
_dbContext = dbContext;
//_shippingModelFactory = shippingModelFactory;
}
[HttpGet]
public async Task<IActionResult> List()
{
if (!await _permissionService.AuthorizeAsync(StandardPermission.Security.ACCESS_ADMIN_PANEL))
return AccessDeniedView();
// Create model and load data
var model = new ShippingListModel();
// TODO: Replace with your actual service call
// model.ShippingList = await _shippingService.GetAllShippingsAsync();
// Mock data for now
model.ShippingList = _dbContext.Shippings.GetAll(true).ToList();
var valami = model;
//model. = await _dbContext.GetShippingDocumentsByShippingIdAsync(shippingId);
return View("~/Plugins/Misc.FruitBankPlugin/Areas/Admin/Views/Shipping/List.cshtml", model);
}
[HttpPost]
public async Task<IActionResult> List(ShippingListModel model)
{
if (!await _permissionService.AuthorizeAsync(StandardPermission.Security.ACCESS_ADMIN_PANEL))
return AccessDeniedView();
// Apply filters to mock data
model.ShippingList = _shippingDbTable.GetAll().ToList();
return View("~/Plugins/Misc.FruitBankPlugin/Areas/Admin/Views/Shipping/List.cshtml", model);
}
[HttpGet]
public async Task<IActionResult> ShippingDocumentList()
{
if (!await _permissionService.AuthorizeAsync(StandardPermission.Security.ACCESS_ADMIN_PANEL))
return AccessDeniedView();
// Create model and load data
var model = _shippingDocumentDbTable.GetAll().ToList();
// TODO: Replace with your actual service call
// model.ShippingList = await _shippingService.GetAllShippingsAsync();
// Mock data for now
return View("~/Plugins/Misc.FruitBankPlugin/Areas/Admin/Views/Shipping/List.cshtml", model);
}
[HttpPost]
public async Task<IActionResult> ShippingDocumentList(int shippingId)
{
if (!await _permissionService.AuthorizeAsync(StandardPermission.Security.ACCESS_ADMIN_PANEL))
return AccessDeniedView();
// Apply filters to mock data
var model = await _dbContext.GetShippingDocumentsByShippingIdAsync(shippingId);
return View("~/Plugins/Misc.FruitBankPlugin/Areas/Admin/Views/Shipping/List.cshtml", model);
}
[HttpPost]
public async Task<IActionResult> Delete(int id)
{
if (!await _permissionService.AuthorizeAsync(StandardPermission.Security.ACCESS_ADMIN_PANEL))
return Json(new { success = false, message = "Access denied" });
try
{
// TODO: Implement actual deletion
return Json(new { success = true, message = "Shipment deleted successfully" });
}
catch (Exception ex)
{
return Json(new { success = false, message = ex.Message });
}
}
[HttpGet]
public async Task<IActionResult> Create()
{
if (!await _permissionService.AuthorizeAsync(StandardPermission.Security.ACCESS_ADMIN_PANEL))
return AccessDeniedView();
var model = new CreateShippingModel
{
ShippingDate = DateTime.Now
};
return View("~/Plugins/Misc.FruitBankPlugin/Areas/Admin/Views/Shipping/Create.cshtml", model);
}
[HttpPost]
public async Task<IActionResult> Create(CreateShippingModel model)
{
if (!await _permissionService.AuthorizeAsync(StandardPermission.Security.ACCESS_ADMIN_PANEL))
return AccessDeniedView();
if (!ModelState.IsValid)
{
return View("~/Plugins/Misc.FruitBankPlugin/Areas/Admin/Views/Shipping/Create.cshtml", model);
}
string licencePlate = "";
if (model.LicencePlate.Length > 3)
{
licencePlate = model.LicencePlate?.Trim().ToUpper() ?? string.Empty;
}
try
{
var shipment = new Shipping
{
LicencePlate = licencePlate,
ShippingDate = model.ShippingDate,
IsAllMeasured = false,
};
await _shippingDbTable.InsertAsync(shipment);
_notificationService.SuccessNotification($"Shipment created successfully. You can now upload documents. Shipping Id: {shipment.Id}");
// Redirect to Edit action where user can upload files
return RedirectToAction("Edit", new { id = shipment.Id });
}
catch (Exception ex)
{
_notificationService.ErrorNotification($"Error creating shipment: {ex.Message}");
return View("~/Plugins/Misc.FruitBankPlugin/Areas/Admin/Views/Shipping/Create.cshtml", model);
}
}
[HttpGet]
public async Task<IActionResult> Edit(int id)
{
if (!await _permissionService.AuthorizeAsync(StandardPermission.Security.ACCESS_ADMIN_PANEL))
return AccessDeniedView();
try
{
// TODO: Load shipment from database
var shipment = await _shippingDbTable.GetByIdAsync(id);
if (shipment == null)
return RedirectToAction("List");
// TODO: Load existing documents
// var documents = await _documentService.GetShipmentDocumentsAsync(id);
// For now, create a mock model
var model = new EditShippingModel
{
Id = shipment.Id,
ShippingDate = shipment.ShippingDate, // Replace with: shipment.ShipmentDate
LicencePlate = shipment.LicencePlate, // Replace with: shipment.TrackingNumber
ExistingDocuments = new List<ShippingDocumentModel>()
// Replace with: documents.Select(d => new ShipmentDocumentModel { ... }).ToList()
};
return View("~/Plugins/Misc.FruitBankPlugin/Areas/Admin/Views/Shipping/Edit.cshtml", model);
}
catch (Exception ex)
{
_notificationService.ErrorNotification($"Error loading shipment: {ex.Message}");
return RedirectToAction("List");
}
}
[HttpPost]
public async Task<IActionResult> Edit(EditShippingModel model)
{
if (!await _permissionService.AuthorizeAsync(StandardPermission.Security.ACCESS_ADMIN_PANEL))
return AccessDeniedView();
if (!ModelState.IsValid)
{
// Reload existing documents if validation fails
// model.ExistingDocuments = await LoadExistingDocuments(model.Id);
return View("~/Plugins/Misc.FruitBankPlugin/Areas/Admin/Views/Shipping/Edit.cshtml", model);
}
try
{
// TODO: Update shipment in database
// var shipment = await _shipmentService.GetShipmentByIdAsync(model.Id);
// shipment.Name = model.ShipmentName;
// shipment.Description = model.Description;
// shipment.ShipmentDate = model.ShipmentDate;
// shipment.TrackingNumber = model.TrackingNumber;
// await _shipmentService.UpdateShippingAsync(shipment);
_notificationService.SuccessNotification("Shipment updated successfully");
return RedirectToAction("Edit", new { id = model.Id });
}
catch (Exception ex)
{
_notificationService.ErrorNotification($"Error updating shipment: {ex.Message}");
return View("~/Plugins/Misc.FruitBankPlugin/Areas/Admin/Views/Shipping/Edit.cshtml", model);
}
}
[HttpPost]
public async Task<IActionResult> UploadFile(IFormFile file, int shipmentId)
{
try
{
if (!await _permissionService.AuthorizeAsync(StandardPermission.Security.ACCESS_ADMIN_PANEL))
return Json(new FileUploadResult { Success = false, ErrorMessage = "Access denied" });
if (file == null || file.Length == 0)
return Json(new FileUploadResult { Success = false, ErrorMessage = "No file selected" });
// Validate file type (PDF only)
if (!file.ContentType.Equals("application/pdf", StringComparison.OrdinalIgnoreCase))
return Json(new FileUploadResult { Success = false, ErrorMessage = "Only PDF files are allowed" });
// Validate file size (e.g., max 10MB)
if (file.Length > 10 * 1024 * 1024)
return Json(new FileUploadResult { Success = false, ErrorMessage = "File size must be less than 10MB" });
// Create upload directory if it doesn't exist
var uploadsPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "uploads", "shipments");
Directory.CreateDirectory(uploadsPath);
// Generate unique filename
var fileName = $"{Guid.NewGuid()}_{file.FileName}";
var filePath = Path.Combine(uploadsPath, fileName);
// Save file
using (var stream = new FileStream(filePath, FileMode.Create))
{
await file.CopyToAsync(stream);
}
// TODO: Save document record to database
// var document = new ShipmentDocument
// {
// ShipmentId = shipmentId,
// FileName = file.FileName,
// FilePath = $"/uploads/shipments/{fileName}",
// FileSize = (int)(file.Length / 1024), // Convert to KB
// ContentType = file.ContentType,
// UploadDate = DateTime.UtcNow,
// IsActive = true
// };
// var savedDocument = await _documentService.InsertDocumentAsync(document);
return Json(new FileUploadResult
{
Success = true,
FileName = file.FileName,
FilePath = $"/uploads/shippingDocuments/{fileName}",
FileSize = (int)(file.Length / 1024), // KB
DocumentId = 1 // Replace with: savedDocument.Id
});
}
catch (Exception ex)
{
return Json(new FileUploadResult { Success = false, ErrorMessage = ex.Message });
}
}
[HttpPost]
public async Task<IActionResult> DeleteUploadedFile(string filePath)
{
try
{
if (!await _permissionService.AuthorizeAsync(StandardPermission.Security.ACCESS_ADMIN_PANEL))
return Json(new { success = false, message = "Access denied" });
if (string.IsNullOrEmpty(filePath))
return Json(new { success = false, message = "Invalid file path" });
// TODO: Delete document record from database first
// var document = await _documentService.GetDocumentByFilePathAsync(filePath);
// if (document != null)
// await _documentService.DeleteDocumentAsync(document);
var fullPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", filePath.TrimStart('/'));
if (System.IO.File.Exists(fullPath))
{
System.IO.File.Delete(fullPath);
return Json(new { success = true });
}
return Json(new { success = false, message = "File not found" });
}
catch (Exception ex)
{
return Json(new { success = false, message = ex.Message });
}
}
[HttpPost]
public async Task<IActionResult> DeleteDocument(int documentId)
{
try
{
if (!await _permissionService.AuthorizeAsync(StandardPermission.Security.ACCESS_ADMIN_PANEL))
return Json(new DocumentOperationResult { Success = false, Message = "Access denied" });
// TODO: Implement document deletion
// var document = await _documentService.GetDocumentByIdAsync(documentId);
// if (document == null)
// return Json(new DocumentOperationResult { Success = false, Message = "Document not found" });
// Delete physical file
// var fullPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", document.FilePath.TrimStart('/'));
// if (System.IO.File.Exists(fullPath))
// System.IO.File.Delete(fullPath);
// Delete database record
// await _documentService.DeleteDocumentAsync(document);
return Json(new DocumentOperationResult
{
Success = true,
Message = "Document deleted successfully",
DocumentId = documentId
});
}
catch (Exception ex)
{
return Json(new DocumentOperationResult { Success = false, Message = ex.Message });
}
}
// Helper method for loading existing documents (to be implemented)
// private async Task<List<ShipmentDocumentModel>> LoadExistingDocuments(int shipmentId)
// {
// var documents = await _documentService.GetShipmentDocumentsAsync(shipmentId);
// return documents.Select(d => new ShipmentDocumentModel
// {
// Id = d.Id,
// ShipmentId = d.ShipmentId,
// FileName = d.FileName,
// FilePath = d.FilePath,
// FileSize = d.FileSize,
// UploadDate = d.UploadDate,
// ContentType = d.ContentType,
// IsActive = d.IsActive
// }).ToList();
// }
}
}