Files
laatzen/LaaProductionWeb/LaaProductionWeb/Controllers/ShipmentsController.cs
T

129 lines
4.0 KiB
C#

namespace LaaProductionWeb.Controllers
{
using LaaProductionWeb.App_Infrastructure;
using LaaProductionWeb.Services.Interfaces;
using LaaProductionWeb.Services.Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Web.Mvc;
using SystemFile = System.IO.File;
[AllowedRoles("Shipment")]
public class ShipmentsController : Controller
{
private readonly IShipmentsService shipmentsService;
public ShipmentsController(IShipmentsService shipmentsService)
=> this.shipmentsService = shipmentsService;
[HttpGet]
public ActionResult Index()
=> this.View(new ShipmentModel());
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Index(ShipmentModel model)
=> this.View(model);
public ActionResult Batch(OrderScanModel model)
{
var batchModel = this.shipmentsService
.LoadBatchModel(model ?? new OrderScanModel());
this.UpdateClientName(batchModel);
return this.PartialView(batchModel);
}
public ActionResult Scan(OrderScanModel model)
{
var palletsCount = this.shipmentsService.GetPalletsCount(model);
model.UpdatePalletsCount(palletsCount);
return this.PartialView(model);
}
[HttpPost]
[ValidateAntiForgeryToken]
public 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)
{
model.SerialNr = default(string);
}
else
{
this.TempData[nameof(model.SerialNr)] = result;
}
}
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 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.ClientName = kvp.Value;
break;
}
}
}
catch (Exception e)
{
Debug.WriteLine(e);
}
}
}
}