This commit is contained in:
90
server/AyaNova/Controllers/PartInventoryController.cs
Normal file
90
server/AyaNova/Controllers/PartInventoryController.cs
Normal file
@@ -0,0 +1,90 @@
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Routing;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using AyaNova.Models;
|
||||
using AyaNova.Api.ControllerHelpers;
|
||||
using AyaNova.Biz;
|
||||
|
||||
|
||||
|
||||
namespace AyaNova.Api.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[ApiVersion("8.0")]
|
||||
[Route("api/v{version:apiVersion}/part-inventory")]
|
||||
[Produces("application/json")]
|
||||
[Authorize]
|
||||
public class PartInventoryController : ControllerBase
|
||||
{
|
||||
private readonly AyContext ct;
|
||||
private readonly ILogger<PartInventoryController> log;
|
||||
private readonly ApiServerState serverState;
|
||||
|
||||
/// <summary>
|
||||
/// ctor
|
||||
/// </summary>
|
||||
/// <param name="dbcontext"></param>
|
||||
/// <param name="logger"></param>
|
||||
/// <param name="apiServerState"></param>
|
||||
public PartInventoryController(AyContext dbcontext, ILogger<PartInventoryController> logger, ApiServerState apiServerState)
|
||||
{
|
||||
ct = dbcontext;
|
||||
log = logger;
|
||||
serverState = apiServerState;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create PartInventory (manual adjustment entry)
|
||||
/// </summary>
|
||||
/// <param name="newObject"></param>
|
||||
/// <param name="apiVersion">From route path</param>
|
||||
/// <returns></returns>
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> PostPartInventory([FromBody] dtPartInventory newObject, ApiVersion apiVersion)
|
||||
{
|
||||
if (!serverState.IsOpen)
|
||||
return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
|
||||
PartInventoryBiz biz = PartInventoryBiz.GetBiz(ct, HttpContext);
|
||||
if (!Authorized.HasCreateRole(HttpContext.Items, biz.BizType))
|
||||
return StatusCode(403, new ApiNotAuthorizedResponse());
|
||||
if (!ModelState.IsValid)
|
||||
return BadRequest(new ApiErrorResponse(ModelState));
|
||||
PartInventory o = await biz.CreateAsync(newObject);
|
||||
if (o == null)
|
||||
return BadRequest(new ApiErrorResponse(biz.Errors));
|
||||
else
|
||||
return CreatedAtAction(nameof(PartInventoryController.GetPartInventory), new { id = o.Id, version = apiVersion.ToString() }, new ApiCreatedResponse(o));
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Get PartInventory
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <returns>PartInventory</returns>
|
||||
[HttpGet("{id}")]
|
||||
public async Task<IActionResult> GetPartInventory([FromRoute] long id)
|
||||
{
|
||||
if (!serverState.IsOpen)
|
||||
return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
|
||||
PartInventoryBiz biz = PartInventoryBiz.GetBiz(ct, HttpContext);
|
||||
if (!Authorized.HasReadFullRole(HttpContext.Items, biz.BizType))
|
||||
return StatusCode(403, new ApiNotAuthorizedResponse());
|
||||
if (!ModelState.IsValid)
|
||||
return BadRequest(new ApiErrorResponse(ModelState));
|
||||
var o = await biz.GetAsync(id);
|
||||
if (o == null) return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND));
|
||||
return Ok(ApiOkResponse.Response(o));
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
//------------
|
||||
|
||||
|
||||
}//eoc
|
||||
}//eons
|
||||
@@ -145,10 +145,26 @@ namespace AyaNova.Biz
|
||||
Select = AuthorizationRoles.All
|
||||
});
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
//PartInventory
|
||||
//
|
||||
roles.Add(AyaType.PartInventory, new BizRoleSet()
|
||||
{
|
||||
Change = AuthorizationRoles.InventoryFull
|
||||
| AuthorizationRoles.BizAdminFull
|
||||
| AuthorizationRoles.AccountingFull,
|
||||
ReadFullRecord = AuthorizationRoles.DispatchFull
|
||||
| AuthorizationRoles.InventoryLimited
|
||||
| AuthorizationRoles.BizAdminLimited
|
||||
| AuthorizationRoles.DispatchLimited,
|
||||
Select = AuthorizationRoles.All
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
//Part
|
||||
//PartWarehouse
|
||||
//
|
||||
roles.Add(AyaType.PartWarehouse, new BizRoleSet()
|
||||
{
|
||||
|
||||
@@ -39,7 +39,49 @@ namespace AyaNova.Biz
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//CREATE
|
||||
//CREATE (adjustment version)
|
||||
//
|
||||
internal async Task<PartInventory> CreateAsync(dtPartInventory newDtObject)
|
||||
{
|
||||
using (var transaction = await ct.Database.BeginTransactionAsync())
|
||||
{
|
||||
//get the last record if exists (may 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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//CREATE (internal version)
|
||||
//
|
||||
internal async Task<PartInventory> CreateAsync(PartInventory newObject)
|
||||
{
|
||||
@@ -58,8 +100,6 @@ namespace AyaNova.Biz
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//GET
|
||||
//
|
||||
|
||||
@@ -23,7 +23,7 @@ namespace AyaNova.Models
|
||||
public DateTime? LastEntryDate { get; set; }
|
||||
[Required]
|
||||
public long PartId { get; set; }
|
||||
[Required]
|
||||
[Required]
|
||||
public long PartWarehouseId { get; set; }
|
||||
[Required]
|
||||
public long SourceId { get; set; }
|
||||
@@ -34,22 +34,39 @@ namespace AyaNova.Models
|
||||
[Required]
|
||||
public decimal Balance { get; set; }
|
||||
public decimal? LastBalance { get; set; }
|
||||
|
||||
|
||||
|
||||
|
||||
public PartInventory()
|
||||
{
|
||||
EntryDate = DateTime.UtcNow;
|
||||
SourceId=0;
|
||||
SourceType=AyaType.NoType;
|
||||
SourceId = 0;
|
||||
SourceType = AyaType.NoType;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}//eoc
|
||||
|
||||
//public facing class for manual adjustment via ui
|
||||
public class dtPartInventory
|
||||
{
|
||||
public long Id { get; set; }
|
||||
public uint Concurrency { 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; }
|
||||
|
||||
|
||||
}//eoc
|
||||
|
||||
|
||||
}//eons
|
||||
|
||||
Reference in New Issue
Block a user