This commit is contained in:
2022-03-29 15:20:50 +00:00
parent 951ce8fb2c
commit 1c63816575
2 changed files with 54 additions and 31 deletions

View File

@@ -432,11 +432,9 @@ namespace AyaNova.Biz
try
{
//Compile linked objects if specified
long? ImportCustomerId = -1;//default meaning not included / don't set
long ImportCustomerId = 0; //customer is required, can't be null so set to a definite bad setting
if (j["CustomerViz"] != null)
{
//something was specified, may be deliberate attempt to null it out so default to that
ImportCustomerId = null;
if (!JsonUtil.JTokenIsNullOrEmpty(j["CustomerViz"]))
{
//a name was specified so attempt to find it
@@ -498,26 +496,25 @@ namespace AyaNova.Biz
}
}
long? ImportContractId = -1;
DateTime? ImportContractExpires = null;
if (!JsonUtil.JTokenIsNullOrEmpty(j["ContractViz"]))
if (j["ContractViz"] != null)
{
ImportContractId = await ct.Contract.AsNoTracking().Where(z => z.Name == (string)j["ContractViz"]).Select(x => x.Id).FirstOrDefaultAsync();
if (ImportContractId == 0)
AddError(ApiErrorCode.NOT_FOUND, "ContractViz", $"'{(string)j["ContractViz"]}'");
if (JsonUtil.JTokenIsNullOrEmpty(j["ContractExpires"]))
ImportContractExpires = DateTime.UtcNow.Subtract(new TimeSpan(0, 1, 0));//expired one minute ago to be safe, can't guess what the contract should be
else
ImportContractExpires = (DateTime)j["ContractExpires"];
ImportContractId = null;
if (!JsonUtil.JTokenIsNullOrEmpty(j["ContractViz"]))
{
ImportContractId = await ct.Contract.AsNoTracking().Where(z => z.Name == (string)j["ContractViz"]).Select(x => x.Id).FirstOrDefaultAsync();
if (ImportContractId == 0)
AddError(ApiErrorCode.NOT_FOUND, "ContractViz", $"'{(string)j["ContractViz"]}'");
if (JsonUtil.JTokenIsNullOrEmpty(j["ContractExpires"]))
ImportContractExpires = DateTime.UtcNow.Subtract(new TimeSpan(0, 1, 0));//expired one minute ago to be safe, can't guess what the contract should be
else
ImportContractExpires = (DateTime)j["ContractExpires"];
}
}
long existingId = await ct.Unit.AsNoTracking().Where(z => z.Name == (string)j["Name"]).Select(x => x.Id).FirstOrDefaultAsync();
long existingId = await ct.Unit.AsNoTracking().Where(z => z.Serial == (string)j["Serial"]).Select(x => x.Id).FirstOrDefaultAsync();
if (existingId == 0)
{
if (importData.DoImport)
@@ -526,24 +523,35 @@ namespace AyaNova.Biz
var Target = j.ToObject<Unit>(jsset);
Target.Tags.Add(ImportTag);
if (ImportCustomerId != -1)
Target.ManufacturerId = ImportCustomerId;
Target.CustomerId = ImportCustomerId;
if (ImportParentUnitId != -1)
Target.WholeSalerId = ImportParentUnitId;
Target.ParentUnitId = ImportParentUnitId;
if (ImportUnitModelId != -1)
Target.AlternativeWholeSalerId = ImportUnitModelId;
Target.UnitModelId = ImportUnitModelId;
if (ImportPurchasedFromVendorId != -1)
Target.PurchasedFromVendorId = ImportPurchasedFromVendorId;
if (ImportReplacedByUnitId != -1)
Target.ReplacedByUnitId = ImportReplacedByUnitId;
if (ImportContractId != -1)
{
Target.ContractId = ImportContractId;
Target.ContractExpires = ImportContractExpires;
}
var res = await CreateAsync(Target);
if (res == null)
{
ImportResult.Add($"❌ {Target.Name}\r\n{this.GetErrorsAsString()}");
ImportResult.Add($"❌ {Target.Serial}\r\n{this.GetErrorsAsString()}");
this.ClearErrors();
}
else
{
ImportResult.Add($"✔️ {Target.Name}");
ImportResult.Add($"✔️ {Target.Serial}");
}
}
}
@@ -556,26 +564,39 @@ namespace AyaNova.Biz
var Target = await GetAsync((long)existingId);
var Source = j.ToObject<Unit>(jsset);
var propertiesToUpdate = j.Properties().Select(p => p.Name).ToList();
propertiesToUpdate.Remove("Name");
propertiesToUpdate.Remove("Serial");
ImportUtil.Update(Source, Target, propertiesToUpdate);
if (ImportCustomerId != -1)
Target.ManufacturerId = ImportCustomerId;
Target.CustomerId = ImportCustomerId;
if (ImportParentUnitId != -1)
Target.WholeSalerId = ImportParentUnitId;
Target.ParentUnitId = ImportParentUnitId;
if (ImportUnitModelId != -1)
Target.AlternativeWholeSalerId = ImportUnitModelId;
Target.UnitModelId = ImportUnitModelId;
if (ImportPurchasedFromVendorId != -1)
Target.PurchasedFromVendorId = ImportPurchasedFromVendorId;
if (ImportReplacedByUnitId != -1)
Target.ReplacedByUnitId = ImportReplacedByUnitId;
if (ImportContractId != -1)
{
Target.ContractId = ImportContractId;
Target.ContractExpires = ImportContractExpires;
}
var res = await PutAsync(Target);
if (res == null)
{
ImportResult.Add($"❌ {Target.Name} - {this.GetErrorsAsString()}");
ImportResult.Add($"❌ {Target.Serial} - {this.GetErrorsAsString()}");
this.ClearErrors();
}
else
{
ImportResult.Add($"✔️ {Target.Name}");
ImportResult.Add($"✔️ {Target.Serial}");
}
}
}