This commit is contained in:
@@ -45,40 +45,48 @@ namespace AyaNova.Biz
|
||||
{
|
||||
using (var transaction = await ct.Database.BeginTransactionAsync())
|
||||
{
|
||||
//get the last record if exists (will not if opening balance)
|
||||
var LastEntry = await ct.PartInventory.OrderByDescending(m => m.EntryDate).FirstOrDefaultAsync(m => m.PartId == newDtObject.PartId && m.PartWarehouseId == newDtObject.PartWarehouseId);
|
||||
PartInventory newObject = new PartInventory();
|
||||
newObject.Description = newDtObject.Description;
|
||||
newObject.EntryDate = DateTime.UtcNow;
|
||||
newObject.PartId = newDtObject.PartId;
|
||||
newObject.PartWarehouseId = newDtObject.PartWarehouseId;
|
||||
newObject.SourceId = 0;
|
||||
newObject.SourceType = AyaType.NoType;
|
||||
newObject.Quantity = newDtObject.Quantity;
|
||||
|
||||
if (LastEntry != null)
|
||||
try
|
||||
{
|
||||
newObject.LastEntryDate = LastEntry.EntryDate;
|
||||
newObject.LastBalance = LastEntry.Balance;
|
||||
newObject.Balance = LastEntry.Balance + newObject.Quantity;
|
||||
//get the last record if exists (will not if opening balance)
|
||||
var LastEntry = await ct.PartInventory.OrderByDescending(m => m.EntryDate).FirstOrDefaultAsync(m => m.PartId == newDtObject.PartId && m.PartWarehouseId == newDtObject.PartWarehouseId);
|
||||
PartInventory newObject = new PartInventory();
|
||||
newObject.Description = newDtObject.Description;
|
||||
newObject.EntryDate = DateTime.UtcNow;
|
||||
newObject.PartId = newDtObject.PartId;
|
||||
newObject.PartWarehouseId = newDtObject.PartWarehouseId;
|
||||
newObject.SourceId = 0;
|
||||
newObject.SourceType = AyaType.NoType;
|
||||
newObject.Quantity = newDtObject.Quantity;
|
||||
|
||||
if (LastEntry != null)
|
||||
{
|
||||
newObject.LastEntryDate = LastEntry.EntryDate;
|
||||
newObject.LastBalance = LastEntry.Balance;
|
||||
newObject.Balance = LastEntry.Balance + newObject.Quantity;
|
||||
}
|
||||
else
|
||||
{
|
||||
newObject.Balance = newObject.Quantity;
|
||||
}
|
||||
|
||||
await ValidateAsync(newObject);
|
||||
if (HasErrors)
|
||||
return null;
|
||||
else
|
||||
{
|
||||
|
||||
await ct.PartInventory.AddAsync(newObject);
|
||||
await ct.SaveChangesAsync();
|
||||
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, newObject.Id, BizType, AyaEvent.Created), ct);
|
||||
await SearchIndexAsync(newObject, true);
|
||||
await transaction.CommitAsync();
|
||||
return newObject;
|
||||
}
|
||||
}
|
||||
else
|
||||
catch
|
||||
{
|
||||
newObject.Balance = newObject.Quantity;
|
||||
}
|
||||
|
||||
await ValidateAsync(newObject);
|
||||
if (HasErrors)
|
||||
return null;
|
||||
else
|
||||
{
|
||||
|
||||
await ct.PartInventory.AddAsync(newObject);
|
||||
await ct.SaveChangesAsync();
|
||||
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, newObject.Id, BizType, AyaEvent.Created), ct);
|
||||
await SearchIndexAsync(newObject, true);
|
||||
|
||||
return newObject;
|
||||
//Just re-throw for now, let exception handler deal, but in future may want to deal with this more here
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -190,7 +198,7 @@ namespace AyaNova.Biz
|
||||
}
|
||||
|
||||
//values must add up
|
||||
if (proposedObj.Balance != (proposedObj.LastBalance ?? 0 + proposedObj.Quantity))
|
||||
if (proposedObj.Balance != (proposedObj.Quantity + (proposedObj.LastBalance ?? 0)))
|
||||
{
|
||||
AddError(ApiErrorCode.VALIDATION_INVALID_VALUE, "Balance", "Balance incorrect (LastBalance + Quantity not equal to Balance");
|
||||
return;
|
||||
|
||||
@@ -203,17 +203,17 @@ namespace AyaNova.Biz
|
||||
}
|
||||
|
||||
//values must add up
|
||||
if (proposedObj.IncidentsBalance != (proposedObj.LastIncidentsBalance ?? 0 + proposedObj.Incidents))
|
||||
if (proposedObj.IncidentsBalance != (proposedObj.Incidents + (proposedObj.LastIncidentsBalance ?? 0)))
|
||||
{
|
||||
AddError(ApiErrorCode.VALIDATION_INVALID_VALUE, "generalerror", "LT:ServiceBankIncidentsBalance");
|
||||
return;
|
||||
}
|
||||
if (proposedObj.CurrencyBalance != (proposedObj.LastCurrencyBalance ?? 0 + proposedObj.Currency))
|
||||
if (proposedObj.CurrencyBalance != (proposedObj.Currency + (proposedObj.LastCurrencyBalance ?? 0)))
|
||||
{
|
||||
AddError(ApiErrorCode.VALIDATION_INVALID_VALUE, "generalerror", "LT:ServiceBankCurrencyBalance");
|
||||
return;
|
||||
}
|
||||
if (proposedObj.HoursBalance != (proposedObj.LastHoursBalance ?? 0 + proposedObj.Hours))
|
||||
if (proposedObj.HoursBalance != (proposedObj.Hours + (proposedObj.LastHoursBalance ?? 0)))
|
||||
{
|
||||
AddError(ApiErrorCode.VALIDATION_INVALID_VALUE, "generalerror", "LT:ServiceBankHoursBalance");
|
||||
return;
|
||||
|
||||
@@ -1538,6 +1538,7 @@ namespace AyaNova.Util
|
||||
}
|
||||
|
||||
PartInventoryBiz PartInventoryBizNess = PartInventoryBiz.GetBiz(ct);
|
||||
int OpeningInventoryLevel = 1;
|
||||
// 50% chance it has serial numbers
|
||||
if (Fake.Random.Number() == 1)
|
||||
{
|
||||
@@ -1548,14 +1549,31 @@ namespace AyaNova.Util
|
||||
|
||||
}
|
||||
await ct.SaveChangesAsync();
|
||||
//add inventory to cover serials
|
||||
await PartInventoryBizNess.CreateAsync(new dtPartInventory() { PartId = NewObject.Id, PartWarehouseId = 1, Quantity = serialCount });
|
||||
//ensure add enough inventory to cover serials
|
||||
OpeningInventoryLevel = serialCount;
|
||||
}
|
||||
else
|
||||
|
||||
OpeningInventoryLevel += Fake.Random.Number(1, 1000);
|
||||
PartInventory partInventory = null;
|
||||
|
||||
//add opening inventory
|
||||
partInventory = await PartInventoryBizNess.CreateAsync(new dtPartInventory() { PartId = NewObject.Id, PartWarehouseId = 1, Quantity = OpeningInventoryLevel, Description = "New part opening inventory" });
|
||||
|
||||
//make two adjustments to have some testing data
|
||||
if (partInventory != null)
|
||||
partInventory = await PartInventoryBizNess.CreateAsync(new dtPartInventory() { PartId = NewObject.Id, PartWarehouseId = 1, Quantity = -1, Description = "example adjustment" });
|
||||
|
||||
if (partInventory != null)
|
||||
partInventory = await PartInventoryBizNess.CreateAsync(new dtPartInventory() { PartId = NewObject.Id, PartWarehouseId = 1, Quantity = Fake.Random.Number(1, 100), Description = "example adjustment" });
|
||||
|
||||
|
||||
if (partInventory == null)
|
||||
{
|
||||
//add random inventory level
|
||||
await PartInventoryBizNess.CreateAsync(new dtPartInventory() { PartId = NewObject.Id, PartWarehouseId = 1, Quantity = Fake.Random.Number(1, 100) });
|
||||
var err = $"Seeder::SeedPart - error creating {o.PartNumber} INVENTORY \r\n{PartInventoryBizNess.GetErrorsAsString()}";
|
||||
log.LogError(err);
|
||||
throw new System.Exception(err);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user