274 lines
12 KiB
C#
274 lines
12 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.Routing;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using System.Linq;
|
|
using AyaNova.Models;
|
|
using AyaNova.Api.ControllerHelpers;
|
|
using AyaNova.Biz;
|
|
using System.Threading.Tasks;
|
|
using System;
|
|
|
|
namespace AyaNova.Api.Controllers
|
|
{
|
|
|
|
[ApiController]
|
|
[ApiVersion("8.0")]
|
|
[Route("api/v{version:apiVersion}/global-biz-setting")]
|
|
[Produces("application/json")]
|
|
[Authorize]
|
|
public class GlobalBizSettingsController : ControllerBase
|
|
{
|
|
private readonly AyContext ct;
|
|
private readonly ILogger<GlobalBizSettingsController> log;
|
|
private readonly ApiServerState serverState;
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// ctor
|
|
/// </summary>
|
|
/// <param name="dbcontext"></param>
|
|
/// <param name="logger"></param>
|
|
/// <param name="apiServerState"></param>
|
|
public GlobalBizSettingsController(AyContext dbcontext, ILogger<GlobalBizSettingsController> logger, ApiServerState apiServerState)
|
|
{
|
|
ct = dbcontext;
|
|
log = logger;
|
|
serverState = apiServerState;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get GlobalBizSettings
|
|
/// </summary>
|
|
/// <returns>Global settings object</returns>
|
|
[HttpGet]
|
|
public async Task<IActionResult> GetGlobalBizSettings()
|
|
{
|
|
if (serverState.IsClosed)
|
|
return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
|
|
|
|
//Instantiate the business object handler
|
|
GlobalBizSettingsBiz biz = GlobalBizSettingsBiz.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();
|
|
if (o == null)
|
|
return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND));
|
|
|
|
return Ok(ApiOkResponse.Response(o));
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// PUT Global biz settings
|
|
/// </summary>
|
|
/// <param name="updatedObject"></param>
|
|
/// <returns>New concurrency token</returns>
|
|
[HttpPut]
|
|
public async Task<IActionResult> ReplaceGlobalBizSettings([FromBody] GlobalBizSettings updatedObject)
|
|
{
|
|
if (serverState.IsClosed)
|
|
return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
|
|
|
|
if (!ModelState.IsValid)
|
|
return BadRequest(new ApiErrorResponse(ModelState));
|
|
|
|
//Instantiate the business object handler
|
|
GlobalBizSettingsBiz biz = GlobalBizSettingsBiz.GetBiz(ct, HttpContext);
|
|
|
|
if (!Authorized.HasModifyRole(HttpContext.Items, biz.BizType))
|
|
return StatusCode(403, new ApiNotAuthorizedResponse());
|
|
|
|
var o = await biz.PutAsync(updatedObject);
|
|
if (o == null)
|
|
return StatusCode(409, new ApiErrorResponse(biz.Errors));
|
|
return Ok(ApiOkResponse.Response(new { Concurrency = o.Concurrency }));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get Client app relevant GlobalBizSettings
|
|
/// </summary>
|
|
/// <returns>Global settings object</returns>
|
|
[HttpGet("client")]
|
|
public ActionResult GetClientGlobalBizSettings()
|
|
{
|
|
//## NOTE: these are settings that the Client needs to see for standard operations
|
|
//NOT the settings that the user changes in the global settings form which is fetched above
|
|
//so do not include anything here unless the client needs it
|
|
if (serverState.IsClosed)
|
|
{
|
|
//Exception for SuperUser account to handle licensing issues
|
|
if (UserIdFromContext.Id(HttpContext.Items) != 1)
|
|
return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
|
|
}
|
|
|
|
var ret = new
|
|
{
|
|
//Actual global settings:
|
|
SearchCaseSensitiveOnly = AyaNova.Util.ServerGlobalBizSettings.Cache.SearchCaseSensitiveOnly,
|
|
UseInventory = AyaNova.Util.ServerGlobalBizSettings.Cache.UseInventory,
|
|
DefaultTaxPartSaleId = AyaNova.Util.ServerGlobalBizSettings.Cache.TaxPartSaleId,
|
|
DefaultTaxPartPurchaseId = AyaNova.Util.ServerGlobalBizSettings.Cache.TaxPartPurchaseId,
|
|
DefaultTaxRateSaleId = AyaNova.Util.ServerGlobalBizSettings.Cache.TaxRateSaleId,
|
|
WorkOrderTravelDefaultMinutes = AyaNova.Util.ServerGlobalBizSettings.Cache.WorkOrderTravelDefaultMinutes,
|
|
WorkLaborScheduleDefaultMinutes = AyaNova.Util.ServerGlobalBizSettings.Cache.WorkLaborScheduleDefaultMinutes,
|
|
SignatureTitle = AyaNova.Util.ServerGlobalBizSettings.Cache.SignatureTitle,
|
|
SignatureHeader = AyaNova.Util.ServerGlobalBizSettings.Cache.SignatureHeader,
|
|
SignatureFooter = AyaNova.Util.ServerGlobalBizSettings.Cache.SignatureFooter,
|
|
|
|
|
|
//used to drive UI in case of unlicensed or attention required
|
|
LicenseStatus = AyaNova.Core.License.ActiveKey.Status,
|
|
MaintenanceExpired = AyaNova.Core.License.ActiveKey.MaintenanceExpired,
|
|
ServerDbId = AyaNova.Core.License.ServerDbId,
|
|
Company = AyaNova.Core.License.ActiveKey.RegisteredTo
|
|
|
|
// ,
|
|
// TestTSDaysWMS=new TimeSpan(22,10,15,22,33),
|
|
// TestTSHMS=new TimeSpan(5,16,33),
|
|
// TestTS_DHMS=new TimeSpan(5,10,15,33),
|
|
// TestTS_MS=new TimeSpan(0,15,33),
|
|
// TestTS_S=new TimeSpan(0,0,33),
|
|
// TestTS_D=new TimeSpan(22,0,0,0,0)
|
|
|
|
|
|
|
|
};
|
|
|
|
return Ok(ApiOkResponse.Response(ret));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get next serial numbers for serialized objects
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[HttpGet("seeds")]
|
|
public ActionResult GetSeeds()
|
|
{
|
|
if (serverState.IsClosed)
|
|
return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
|
|
|
|
if (!Authorized.HasReadFullRole(HttpContext.Items, AyaType.Global))
|
|
return StatusCode(403, new ApiNotAuthorizedResponse());
|
|
//select last_value from aworkorder_serial_seq
|
|
long WONext = 0, PONext = 0, QuoteNext = 0, PMNext = 0;
|
|
using (var command = ct.Database.GetDbConnection().CreateCommand())
|
|
{
|
|
ct.Database.OpenConnection();
|
|
command.CommandText = "select last_value from apurchaseorder_serial_seq;";
|
|
using (var dr = command.ExecuteReader())
|
|
if (dr.HasRows)
|
|
PONext = dr.Read() ? dr.GetInt64(0) : 0;
|
|
|
|
command.CommandText = "select last_value from aworkorder_serial_seq;";
|
|
using (var dr = command.ExecuteReader())
|
|
if (dr.HasRows)
|
|
WONext = dr.Read() ? dr.GetInt64(0) : 0;
|
|
|
|
command.CommandText = "select last_value from aquote_serial_seq;";
|
|
using (var dr = command.ExecuteReader())
|
|
if (dr.HasRows)
|
|
QuoteNext = dr.Read() ? dr.GetInt64(0) : 0;
|
|
|
|
command.CommandText = "select last_value from apm_serial_seq;";
|
|
using (var dr = command.ExecuteReader())
|
|
if (dr.HasRows)
|
|
PMNext = dr.Read() ? dr.GetInt64(0) : 0;
|
|
|
|
ct.Database.CloseConnection();
|
|
}
|
|
|
|
var ret = new
|
|
{
|
|
//Actual global settings:
|
|
PurchaseOrderNextSerial = ++PONext,
|
|
WorkorderNextSerial = ++WONext,
|
|
QuoteNextSerial = ++QuoteNext,
|
|
PMNextSerial = ++PMNext
|
|
};
|
|
|
|
return Ok(ApiOkResponse.Response(ret));
|
|
}
|
|
|
|
/// <summary>
|
|
/// PUT New serial start seed
|
|
/// </summary>
|
|
/// <param name="aType"> one of PurchaseOrder, Workorder, PM, Quote serialized object types</param>
|
|
/// <param name="nextSeed">Next value to use</param>
|
|
/// <returns>OK or error response if not a valid value (seed number already in use)</returns>
|
|
[HttpPut("seeds/{aType}/{nextSeed}")]
|
|
public async Task<IActionResult> SetNextSeed([FromRoute] AyaType aType, [FromRoute] long nextSeed)
|
|
{
|
|
if (serverState.IsClosed)
|
|
return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
|
|
|
|
if (!ModelState.IsValid)
|
|
return BadRequest(new ApiErrorResponse(ModelState));
|
|
|
|
if (!Authorized.HasModifyRole(HttpContext.Items, AyaType.Global))
|
|
return StatusCode(403, new ApiNotAuthorizedResponse());
|
|
|
|
//this is random and arbitrary but is meant as a sanity check
|
|
//in v7 and int identity was used for serial numbers so the maximum possible value could be 2,147,483,647
|
|
//assuming there is some weird edge case where people want to move beyond that max value for some reason I'm settling on a sanity check of 3 billion 3,000,000,000
|
|
if (nextSeed == 0 || nextSeed > 3000000000)
|
|
return BadRequest(new ApiErrorResponse(ApiErrorCode.VALIDATION_INVALID_VALUE, "nextSeed", "> 0 or < 3,000,000,000"));
|
|
|
|
string SequenceName = string.Empty;
|
|
//confirm this number is not in use and set correct sequence name to alter
|
|
switch (aType)
|
|
{
|
|
case AyaType.PurchaseOrder:
|
|
|
|
if (await ct.PurchaseOrder.AnyAsync(z => z.Serial == nextSeed))
|
|
return BadRequest(new ApiErrorResponse(ApiErrorCode.VALIDATION_NOT_UNIQUE, "nextSeed"));
|
|
SequenceName = "apurchaseorder_serial_seq";
|
|
break;
|
|
case AyaType.WorkOrder:
|
|
if (await ct.WorkOrder.AnyAsync(z => z.Serial == nextSeed))
|
|
return BadRequest(new ApiErrorResponse(ApiErrorCode.VALIDATION_NOT_UNIQUE, "nextSeed"));
|
|
SequenceName = "aworkorder_serial_seq";
|
|
break;
|
|
case AyaType.Quote:
|
|
if (await ct.Quote.AnyAsync(z => z.Serial == nextSeed))
|
|
return BadRequest(new ApiErrorResponse(ApiErrorCode.VALIDATION_NOT_UNIQUE, "nextSeed"));
|
|
SequenceName = "aquote_serial_seq";
|
|
break;
|
|
case AyaType.PM:
|
|
if (await ct.PM.AnyAsync(z => z.Serial == nextSeed))
|
|
return BadRequest(new ApiErrorResponse(ApiErrorCode.VALIDATION_NOT_UNIQUE, "nextSeed"));
|
|
SequenceName = "apm_serial_seq";
|
|
|
|
break;
|
|
default:
|
|
return BadRequest(new ApiErrorResponse(ApiErrorCode.VALIDATION_INVALID_VALUE, "aType", "Not a serialized type"));
|
|
|
|
}
|
|
try
|
|
{
|
|
using (var command = ct.Database.GetDbConnection().CreateCommand())
|
|
{
|
|
command.CommandText = $"ALTER SEQUENCE {SequenceName} RESTART WITH {nextSeed};";
|
|
await ct.Database.OpenConnectionAsync();
|
|
await command.ExecuteNonQueryAsync();
|
|
await ct.Database.CloseConnectionAsync();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return BadRequest(new ApiErrorResponse(ApiErrorCode.API_SERVER_ERROR, "generalerror", ex.Message));
|
|
}
|
|
|
|
return Ok(ApiOkResponse.Response(new { AType = aType, NextSeed = nextSeed }));
|
|
}
|
|
|
|
|
|
}//eoc
|
|
}//ens |