This commit is contained in:
2021-05-17 18:34:29 +00:00
parent 16bee4eff8
commit cecb268351
2 changed files with 105 additions and 49 deletions

View File

@@ -12,14 +12,7 @@ using System.Collections.Generic;
namespace AyaNova.Biz
{
/*
###############
todo: remember, some users should not even have data sent from the server / scrubbed and not affect updating.
for example a user may not be able to see part costs so that should not even be sent over the wire
workorder will have to handle that as necessary and expect sometimes data is not forthcoming
*/
internal class WorkOrderBiz : BizObject, IJobObject, ISearchAbleObject, IReportAbleObject, IExportAbleObject
{
@@ -86,7 +79,7 @@ namespace AyaNova.Biz
//(seeder or api user, not something AyaNova front end would do)
if (newObject.Items.Count > 0)//our front end will post the header alone on new so this indicates a fully populated wo was saved
{
await GetCurrentContractFromContractIdAsync(newObject.ContractId);
await GetCurrentContractFromContractIdAsync(newObject.ContractId);
await ProcessChangeOfContractAsync(newObject.Id);
}
@@ -224,7 +217,7 @@ namespace AyaNova.Biz
}
bool contractChanged = false;
long? newContractId = null;
if (putObject.ContractId != dbObject.ContractId)
if (putObject.ContractId != dbObject.ContractId)//manual change of contract
{
contractChanged = true;
newContractId = putObject.ContractId;
@@ -328,38 +321,55 @@ namespace AyaNova.Biz
//CREATION ACTIONS
if (ayaEvent == AyaEvent.Created)
{ //applied at time of creation only
//CONTRACT AUTO SET
if (newObj.ContractId == null && newObj.CustomerId != 0)
{
//customer->headoffice
var cust = await ct.Customer.AsNoTracking().Where(z => z.Id == newObj.CustomerId).Select(z => new { headofficeId = z.HeadOfficeId, contractId = z.ContractId }).FirstOrDefaultAsync();
if (cust.contractId == null && cust.headofficeId != null)
{
var hoContractId = await ct.HeadOffice.AsNoTracking().Where(z => z.Id == cust.headofficeId).Select(z => z.ContractId).FirstOrDefaultAsync();
if (hoContractId != null)
newObj.ContractId = hoContractId;
}
else
newObj.ContractId = cust.contractId;
}
{
await AutoSetContractAsync(newObj);
return;
}
//RESPONSE TIME / COMPLETE BY AUTO SET
//precedence: manually pre-set -> contract -> global biz
if (newObj.CompleteByDate != null)
//MODIFIED ACTIONS
if (ayaEvent == AyaEvent.Modified)
{
//if customer changed then contractId must be re-checked
if (newObj.CustomerId != oldObj.CustomerId)
{
if (newObj.ContractId != null)
{
await GetCurrentContractFromContractIdAsync(newObj.ContractId);
if (mContractInEffect != null && mContractInEffect.ResponseTime != TimeSpan.Zero)
newObj.CompleteByDate = DateTime.UtcNow.Add(mContractInEffect.ResponseTime);
}
else
{
if(AyaNova.Util.ServerGlobalBizSettings.WorkOrderCompleteByAge!=TimeSpan.Zero)
await AutoSetContractAsync(newObj);
}
}
}
private async Task AutoSetContractAsync(WorkOrder newObj)
{
//CONTRACT AUTO SET
if (newObj.ContractId == null && newObj.CustomerId != 0)
{
//unit->customer->headoffice
var cust = await ct.Customer.AsNoTracking().Where(z => z.Id == newObj.CustomerId).Select(z => new { headofficeId = z.HeadOfficeId, contractId = z.ContractId }).FirstOrDefaultAsync();
if (cust.contractId == null && cust.headofficeId != null)
{
var hoContractId = await ct.HeadOffice.AsNoTracking().Where(z => z.Id == cust.headofficeId).Select(z => z.ContractId).FirstOrDefaultAsync();
if (hoContractId != null)
newObj.ContractId = hoContractId;
}
else
newObj.ContractId = cust.contractId;
}
//RESPONSE TIME / COMPLETE BY AUTO SET
//precedence: manually pre-set -> contract -> global biz
if (newObj.CompleteByDate != null)
{
if (newObj.ContractId != null)
{
await GetCurrentContractFromContractIdAsync(newObj.ContractId);
if (mContractInEffect != null && mContractInEffect.ResponseTime != TimeSpan.Zero)
newObj.CompleteByDate = DateTime.UtcNow.Add(mContractInEffect.ResponseTime);
}
else
{
if (AyaNova.Util.ServerGlobalBizSettings.WorkOrderCompleteByAge != TimeSpan.Zero)
newObj.CompleteByDate = DateTime.UtcNow.Add(AyaNova.Util.ServerGlobalBizSettings.WorkOrderCompleteByAge);
}
}
}
}
}
@@ -509,7 +519,7 @@ namespace AyaNova.Biz
//If Contract has response time then set CompleteByDate
if (mContractInEffect!=null && mContractInEffect.ResponseTime != TimeSpan.Zero)
if (mContractInEffect != null && mContractInEffect.ResponseTime != TimeSpan.Zero)
{
wo.CompleteByDate = DateTime.UtcNow.Add(mContractInEffect.ResponseTime);
}
@@ -4164,6 +4174,7 @@ namespace AyaNova.Biz
return null;
else
{
//TODO: In biz actions set contract if this unit has a contract, note that we are only here if there is no pre-existing unit with a contract on this workorder via validation above
newObject.Tags = TagBiz.NormalizeTags(newObject.Tags);
newObject.CustomFields = JsonUtil.CompactJson(newObject.CustomFields);
await ct.WorkOrderItemUnit.AddAsync(newObject);
@@ -4208,6 +4219,9 @@ namespace AyaNova.Biz
dbObject.CustomFields = JsonUtil.CompactJson(dbObject.CustomFields);
await UnitValidateAsync(putObject, dbObject);
if (HasErrors) return null;
//TODO: In biz actions set contract if this unit has a contract, note that we are only here if there is no pre-existing unit with a contract on this workorder via validation above
ct.Replace(dbObject, putObject);
try
{
@@ -4305,6 +4319,10 @@ namespace AyaNova.Biz
//skip validation if seeding
// if (ServerBootConfig.SEEDING) return;
//TODO: ADD VALIDATIONS:
// - A work order *MUST* have only one Unit with a Contract, if there is already a unit with a contract on this workorder then a new one cannot be added and it will reject with a validation error
//run validation and biz rules
bool isNew = currentObj == null;