This commit is contained in:
2021-02-16 19:10:50 +00:00
parent ea356ba764
commit 13a71d643f
2 changed files with 128 additions and 85 deletions

View File

@@ -9,6 +9,7 @@ using AyaNova.Models;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using Newtonsoft.Json;
using Microsoft.EntityFrameworkCore.Storage;
namespace AyaNova.Biz
{
@@ -41,54 +42,57 @@ namespace AyaNova.Biz
////////////////////////////////////////////////////////////////////////////////////////////////
//CREATE (adjustment version)
//
internal async Task<PartInventory> CreateAsync(dtPartInventory newDtObject)
internal async Task<PartInventory> CreateAsync(dtPartInventory newDtObject, IDbContextTransaction parentTransaction = null)
{
using (var transaction = await ct.Database.BeginTransactionAsync())
IDbContextTransaction transaction = null;
if (parentTransaction == null)
transaction = await ct.Database.BeginTransactionAsync();
try
{
try
//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 = null;
newObject.SourceType = null;
newObject.Quantity = newDtObject.Quantity;
if (LastEntry != null)
{
//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 = null;
newObject.SourceType = null;
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;
}
newObject.LastEntryDate = LastEntry.EntryDate;
newObject.LastBalance = LastEntry.Balance;
newObject.Balance = LastEntry.Balance + newObject.Quantity;
}
catch
else
{
//Just re-throw for now, let exception handler deal, but in future may want to deal with this more here
throw;
newObject.Balance = newObject.Quantity;
}
await ValidateAsync(newObject);
if (HasErrors)
return null;
else
{
await ct.PartInventory.AddAsync(newObject);
await ct.SaveChangesAsync();
//all good do the commit if it's ours
if (parentTransaction == null)
await transaction.CommitAsync();
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, newObject.Id, BizType, AyaEvent.Created), ct);
await SearchIndexAsync(newObject, true);
return newObject;
}
}
catch
{
//Just re-throw for now, let exception handler deal, but in future may want to deal with this more here
throw;
}
}
@@ -183,7 +187,7 @@ namespace AyaNova.Biz
}
if (proposedObj.SourceType != null && proposedObj.SourceId != null && !await BizObjectExistsInDatabase.ExistsAsync((AyaType)proposedObj.SourceType, (long)proposedObj.SourceId, ct))
{
{
AddError(ApiErrorCode.NOT_FOUND, "generalerror", $"PartInventory source object causing inventory change specified doesn't exist [type:{proposedObj.SourceType}, id:{proposedObj.SourceId}]");
return;
}