laatzen/LaaProductionWeb/LaaProduction.Web/Controllers/ShipmentsController.cs

182 lines
5.4 KiB
C#

namespace LaaProduction.Web.Controllers
{
using LaaProduction.Personalization;
using LaaProduction.Web.App_Infrastructure;
using LaaProduction.Services.Interfaces;
using LaaProduction.Services.Models;
using LaaProductionDI;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net;
using System.Threading.Tasks;
using System.Web.Mvc;
using SystemFile = System.IO.File;
[AllowedRoles(EmployeesRoles.WEB_Shipment)]
public class ShipmentsController : Controller
{
private readonly IShipmentsService shipmentsService;
private readonly IHttpService httpService;
public ShipmentsController()
{
this.shipmentsService = LaaServiceProvider.GetService<IShipmentsService>();
this.httpService = LaaServiceProvider.GetService<IHttpService>();
}
[HttpGet]
public ActionResult Default()
{
return this.View();
}
[HttpGet]
public ActionResult Index()
{
var shipmentModel = new ShipmentModel();
var orderScanModel = this.HttpContext.GetUserData<OrderScanModel>();
if (orderScanModel != null)
{
shipmentModel = new ShipmentModel(orderScanModel);
}
return this.View(shipmentModel);
}
[HttpPost]
public ActionResult Index(ShipmentModel model)
{
this.HttpContext.SetUserData(model.OrderScanModel);
return this.View(model);
}
[HttpGet]
public ActionResult Pallet(OrderScanModel model)
{
this.HttpContext.SetUserData(model);
return this.View(nameof(this.Index), new ShipmentModel(model));
}
public ActionResult Batch(OrderScanModel model)
{
var batchModel = this.shipmentsService.LoadBatchModel(model);
this.UpdateClientName(batchModel);
return this.PartialView(batchModel);
}
public ActionResult Scan(OrderScanModel model)
{
model = this.shipmentsService.LoadPallets(model);
return this.PartialView(model);
}
[HttpPost]
public async Task<ActionResult> ScanPost(OrderScanModel model)
{
if (this.ModelState.IsValid)
{
model.Host = this.HttpContext.Request.UserHostAddress;
model.Place = this.HttpContext.Request.UserHostName;
model.SerialNr = model.SerialNr?.Replace(" ", string.Empty);
var result = this.shipmentsService.UpdateBatchModel(model);
if (result)
{
await this.httpService.GetAsync($"/Shipment/Scanned/Serial/{WebUtility.UrlEncode(model.SerialNr)}");
model.SerialNr = default(string);
this.HttpContext.SetUserData(model);
}
else
{
this.TempData[nameof(model.SerialNr)] = result;
}
}
else if (this.TempData[nameof(model.PositionNr)] is int p && p != model.PositionNr)
{
this.TempData[nameof(model.PositionNr)] = model.PositionNr;
return this.Pallet(model);
}
this.TempData[nameof(model.PositionNr)] = model.PositionNr;
return this.View(nameof(this.Index), new ShipmentModel(model));
}
[HttpGet]
public ActionResult Delete(int id)
{
var orderScanModel = this.shipmentsService.DeletePalletEntry(id);
return this.View(nameof(this.Index), new ShipmentModel(orderScanModel));
}
[HttpGet]
public ActionResult Missing()
{
var model = this.shipmentsService.MissingItems();
return this.View(model);
}
[HttpGet]
public ActionResult Print(OrderScanModel model)
{
var batchModel = this.shipmentsService.LoadBatchModel(model);
var batchPrintModel = new BatchPrintModel(batchModel);
return this.View(batchPrintModel);
}
[HttpGet]
public ActionResult Download(OrderScanModel model)
{
var batchBuffer = this.shipmentsService.LoadBatchBuffer(model);
var fileName = $"{model.OrderNr}_{model.PositionNr}_{model.PalletsCount}.csv";
this.Response.AddHeader("Content-Disposition", $"attachment;filename={fileName}");
return this.File(batchBuffer, "application/vnd.ms-excel");
}
private void UpdateClientName(PalletsBatchModel batchModel)
{
try
{
var path = this.Server.MapPath("~/App_Data/client_map.json");
var jsonContent = SystemFile.ReadAllText(path);
var clientMap = JsonConvert.DeserializeObject<IDictionary<string, string>>(jsonContent);
var additionalText = batchModel.AdditionalText ?? string.Empty;
foreach (var kvp in clientMap)
{
if (additionalText.Contains(kvp.Key))
{
batchModel.Customer = kvp.Value;
break;
}
}
}
catch (Exception e)
{
Debug.WriteLine(e);
}
}
}
}