This commit is contained in:
2022-03-29 17:43:38 +00:00
parent 55fa485ba1
commit de6cac92c2
2 changed files with 84 additions and 2 deletions

View File

@@ -7,10 +7,11 @@ using AyaNova.Api.ControllerHelpers;
using AyaNova.Models;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using Newtonsoft.Json;
namespace AyaNova.Biz
{
internal class PartInventoryBiz : BizObject, ISearchAbleObject, IReportAbleObject, IExportAbleObject
internal class PartInventoryBiz : BizObject, ISearchAbleObject, IReportAbleObject, IExportAbleObject, IImportAbleObject
{
internal PartInventoryBiz(AyContext dbcontext, long currentUserId, long userTranslationId, AuthorizationRoles UserRoles)
{
@@ -342,6 +343,87 @@ namespace AyaNova.Biz
public async Task<List<string>> ImportData(AyImportData importData)
{
List<string> ImportResult = new List<string>();
if (importData.DoUpdate)
{
ImportResult.Add($"❌ Inventory transactions can only be Imported, not Updated");
return ImportResult;
}
string ImportTag = ImportUtil.GetImportTag();
foreach (JObject j in importData.Data)
{
try
{
long ImportPartId = -1;//default meaning not included / don't set
if (j["PartViz"] != null)
{
if (!JsonUtil.JTokenIsNullOrEmpty(j["PartViz"]))
{
//a name was specified so attempt to find it
ImportPartId = await ct.Part.AsNoTracking().Where(z => z.Name == (string)j["PartViz"]).Select(x => x.Id).FirstOrDefaultAsync();
if (ImportPartId == 0)
AddError(ApiErrorCode.NOT_FOUND, "PartViz", $"'{(string)j["PartViz"]}'");
}
}
else
AddError(ApiErrorCode.VALIDATION_REQUIRED, "PartViz");
long ImportPartWarehouseId = -1;//default meaning not included / don't set
if (j["PartWarehouseViz"] != null)
{
if (!JsonUtil.JTokenIsNullOrEmpty(j["PartWarehouseViz"]))
{
//a name was specified so attempt to find it
ImportPartWarehouseId = await ct.PartWarehouse.AsNoTracking().Where(z => z.Name == (string)j["PartWarehouseViz"]).Select(x => x.Id).FirstOrDefaultAsync();
if (ImportPartWarehouseId == 0)
AddError(ApiErrorCode.NOT_FOUND, "PartWarehouseViz", $"'{(string)j["PartWarehouseViz"]}'");
}
}
else
AddError(ApiErrorCode.VALIDATION_REQUIRED, "PartWarehouseViz");
var ImportDescription = (string)j["Description"];
decimal ImportQuantity = 0;
if (j["Quantity"] != null)
{
ImportQuantity = (decimal)j["Quantity"];
}
else
AddError(ApiErrorCode.VALIDATION_REQUIRED, "Quantity");
//import this record
if (string.IsNullOrWhiteSpace(ImportDescription))
ImportDescription = ImportTag;
var Target = new dtPartInventory() { Description = ImportDescription, PartId = ImportPartId, PartWarehouseId = ImportPartWarehouseId, Quantity = ImportQuantity };
//var Target = j.ToObject<dtPartInventory>();
var nameviz = $"{Target.Description} - part {(string)j["PartViz"]}, quantity: {(decimal)j["Quantity"]}";
var res = await CreateAsync(Target);
if (res == null)
{
ImportResult.Add($"❌ {nameviz}\r\n{this.GetErrorsAsString()}");
this.ClearErrors();
}
else
{
ImportResult.Add($"✔️ {nameviz}");
}
}
catch (Exception ex)
{
ImportResult.Add($"❌ Exception processing import\n record:{j.ToString()}\nError:{ex.Message}\nSource:{ex.Source}\nStack:{ex.StackTrace.ToString()}");
}
}
return ImportResult;
}