From 9a2defe9c523eb08e08c6a7d5c2cc624f9d07d32 Mon Sep 17 00:00:00 2001 From: John Cardinal Date: Thu, 21 Jan 2021 15:27:36 +0000 Subject: [PATCH] --- .../Controllers/PartInventoryController.cs | 90 +++++++++++++++++++ server/AyaNova/biz/BizRoles.cs | 18 +++- server/AyaNova/biz/PartInventoryBiz.cs | 46 +++++++++- server/AyaNova/models/PartInventory.cs | 27 ++++-- 4 files changed, 172 insertions(+), 9 deletions(-) create mode 100644 server/AyaNova/Controllers/PartInventoryController.cs diff --git a/server/AyaNova/Controllers/PartInventoryController.cs b/server/AyaNova/Controllers/PartInventoryController.cs new file mode 100644 index 00000000..f7741ddf --- /dev/null +++ b/server/AyaNova/Controllers/PartInventoryController.cs @@ -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 log; + private readonly ApiServerState serverState; + + /// + /// ctor + /// + /// + /// + /// + public PartInventoryController(AyContext dbcontext, ILogger logger, ApiServerState apiServerState) + { + ct = dbcontext; + log = logger; + serverState = apiServerState; + } + + /// + /// Create PartInventory (manual adjustment entry) + /// + /// + /// From route path + /// + [HttpPost] + public async Task 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)); + } + + + /// + /// Get PartInventory + /// + /// + /// PartInventory + [HttpGet("{id}")] + public async Task 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 \ No newline at end of file diff --git a/server/AyaNova/biz/BizRoles.cs b/server/AyaNova/biz/BizRoles.cs index daa83e3c..18113b86 100644 --- a/server/AyaNova/biz/BizRoles.cs +++ b/server/AyaNova/biz/BizRoles.cs @@ -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() { diff --git a/server/AyaNova/biz/PartInventoryBiz.cs b/server/AyaNova/biz/PartInventoryBiz.cs index 8a29af54..d24aa83a 100644 --- a/server/AyaNova/biz/PartInventoryBiz.cs +++ b/server/AyaNova/biz/PartInventoryBiz.cs @@ -39,7 +39,49 @@ namespace AyaNova.Biz } //////////////////////////////////////////////////////////////////////////////////////////////// - //CREATE + //CREATE (adjustment version) + // + internal async Task 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 CreateAsync(PartInventory newObject) { @@ -58,8 +100,6 @@ namespace AyaNova.Biz } } - - //////////////////////////////////////////////////////////////////////////////////////////////// //GET // diff --git a/server/AyaNova/models/PartInventory.cs b/server/AyaNova/models/PartInventory.cs index 8a892f2f..9298672c 100644 --- a/server/AyaNova/models/PartInventory.cs +++ b/server/AyaNova/models/PartInventory.cs @@ -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