This commit is contained in:
2021-02-16 19:29:57 +00:00
parent 13a71d643f
commit 5e1b846059
3 changed files with 96 additions and 39 deletions

View File

@@ -40,14 +40,63 @@ namespace AyaNova.Biz
} }
//////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////
//CREATE (adjustment version) //CREATE (public facing adjustment version)
// //
internal async Task<PartInventory> CreateAsync(dtPartInventory newDtObject, IDbContextTransaction parentTransaction = null) internal async Task<PartInventory> CreateAsync(dtPartInventory newDtObject)
{ {
IDbContextTransaction transaction = null; using (var transaction = await ct.Database.BeginTransactionAsync())
if (parentTransaction == null) {
transaction = await ct.Database.BeginTransactionAsync(); 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)
{
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 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;
}
}
}
////////////////////////////////////////////////////////////////////////////////////////////////
//CREATE (internal version)
//
internal async Task<PartInventory> CreateAsync(dtPOPartInventory newDtObject)
{
try try
{ {
//get the last record if exists (will not if opening balance) //get the last record if exists (will not if opening balance)
@@ -57,8 +106,8 @@ namespace AyaNova.Biz
newObject.EntryDate = DateTime.UtcNow; newObject.EntryDate = DateTime.UtcNow;
newObject.PartId = newDtObject.PartId; newObject.PartId = newDtObject.PartId;
newObject.PartWarehouseId = newDtObject.PartWarehouseId; newObject.PartWarehouseId = newDtObject.PartWarehouseId;
newObject.SourceId = null; newObject.SourceId = newDtObject.SourceId;
newObject.SourceType = null; newObject.SourceType = newDtObject.SourceType;
newObject.Quantity = newDtObject.Quantity; newObject.Quantity = newDtObject.Quantity;
if (LastEntry != null) if (LastEntry != null)
@@ -79,9 +128,6 @@ namespace AyaNova.Biz
{ {
await ct.PartInventory.AddAsync(newObject); await ct.PartInventory.AddAsync(newObject);
await ct.SaveChangesAsync(); 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 EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, newObject.Id, BizType, AyaEvent.Created), ct);
await SearchIndexAsync(newObject, true); await SearchIndexAsync(newObject, true);
return newObject; return newObject;
@@ -96,26 +142,6 @@ namespace AyaNova.Biz
} }
////////////////////////////////////////////////////////////////////////////////////////////////
//CREATE (internal version)
//
internal async Task<PartInventory> CreateAsync(PartInventory newObject)
{
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;
}
}
//////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////
//GET //GET
// //

View File

@@ -8,7 +8,7 @@ using Microsoft.Extensions.Logging;
using AyaNova.Models; using AyaNova.Models;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using System.Collections.Generic; using System.Collections.Generic;
using Newtonsoft.Json; using Microsoft.EntityFrameworkCore.Storage;
namespace AyaNova.Biz namespace AyaNova.Biz
{ {
@@ -263,7 +263,7 @@ namespace AyaNova.Biz
//BIZ ACTIONS //BIZ ACTIONS
// //
private async Task BizActionsAsync(AyaEvent ayaEvent, PurchaseOrder proposedObj, PurchaseOrder currentObj, Microsoft.EntityFrameworkCore.Storage.IDbContextTransaction transaction) private async Task BizActionsAsync(AyaEvent ayaEvent, PurchaseOrder proposedObj, PurchaseOrder currentObj, IDbContextTransaction transaction)
{ {
//TODO: BIZ ACTIONS TO FIXUP INVENTORY ON CHANGES ETC //TODO: BIZ ACTIONS TO FIXUP INVENTORY ON CHANGES ETC
@@ -274,13 +274,23 @@ namespace AyaNova.Biz
//if received on woitempartrequest then need to update woitempartrequest (notification separate not a concern here) //if received on woitempartrequest then need to update woitempartrequest (notification separate not a concern here)
//if workorderitempartrequest item removed, need to fixup woitempartrequest //if workorderitempartrequest item removed, need to fixup woitempartrequest
PartInventoryBiz pib = new PartInventoryBiz(ct, UserId, UserTranslationId, CurrentUserRoles);
switch (ayaEvent) switch (ayaEvent)
{ {
case AyaEvent.Created: case AyaEvent.Created:
//any received go into inventory //any received go into inventory
var inventoryAffectingItems = proposedObj.Items.Where(z => z.QuantityReceived > 0).ToList(); var inventoryAffectingItems = proposedObj.Items.Where(z => z.QuantityReceived > 0).ToList();
foreach(var poItem in inventoryAffectingItems){ foreach (var poItem in inventoryAffectingItems)
{
//make inventory adjustment here //make inventory adjustment here
dtPOPartInventory i = new dtPOPartInventory();
i.PartId = poItem.PartId;
i.PartWarehouseId = poItem.PartWarehouseId;
i.Quantity = poItem.QuantityReceived;
i.SourceType = AyaType.PurchaseOrder;
i.SourceId = proposedObj.Id;
await pib.CreateAsync(i);
} }
break; break;

View File

@@ -50,7 +50,7 @@ namespace AyaNova.Models
public class dtPartInventory public class dtPartInventory
{ {
public long Id { get; set; } public long Id { get; set; }
public uint Concurrency { get; set; } public uint Concurrency { get; set; }//wtf? This is never an update, shouldn't this be not here?
[Required] [Required]
public string Description { get; set; } public string Description { get; set; }
@@ -66,6 +66,27 @@ namespace AyaNova.Models
}//eoc }//eoc
//internal purchase order version
public class dtPOPartInventory
{
public long Id { get; set; }
[Required]
public string Description { get; set; }
[Required]
public long PartId { get; set; }
[Required]
public long PartWarehouseId { get; set; }
[Required]
public decimal Quantity { get; set; }
public long? SourceId { get; set; }
public AyaType? SourceType { get; set; }
}//eoc
}//eons }//eons
/* /*