diff --git a/server/AyaNova/biz/WorkOrderBiz.cs b/server/AyaNova/biz/WorkOrderBiz.cs index c0e187bc..37f64039 100644 --- a/server/AyaNova/biz/WorkOrderBiz.cs +++ b/server/AyaNova/biz/WorkOrderBiz.cs @@ -346,12 +346,12 @@ namespace AyaNova.Biz private async Task AutoSetAddressAsync(WorkOrder newObj) { - if (newObj.CustomerId == 0) - return; + if (newObj.CustomerId == 0) + return; var cust = await ct.Customer.AsNoTracking().Where(z => z.Id == newObj.CustomerId).FirstOrDefaultAsync(); - if (cust == null) - return; + if (cust == null) + return; newObj.PostAddress = cust.PostAddress; newObj.PostCity = cust.PostCity; @@ -377,7 +377,7 @@ namespace AyaNova.Biz newObj.PostCountry = head.PostCountry; newObj.PostCode = head.PostCode; - } + } } private async Task AutoSetContractAsync(WorkOrder newObj) @@ -387,23 +387,26 @@ namespace AyaNova.Biz mFetchedContractAlready = false; //CONTRACT AUTO SET + //failsafe + newObj.ContractId = null; + if (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(); + //precedence: unit->customer->headoffice + var cust = await ct.Customer.AsNoTracking().Where(z => z.Id == newObj.CustomerId).Select(z => new { headofficeId = z.HeadOfficeId, contractId = z.ContractId, contractExpires = z.ContractExpires }).FirstOrDefaultAsync(); + + //first set it to the customer one if available in case the ho one has expired then set the ho if applicable + if (cust.contractId != null && cust.contractExpires < DateTime.UtcNow) + newObj.ContractId = cust.contractId; + 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;//if ultimately no contract for this customer or ho then it's set to null anyway which is what we want - } - else - { - newObj.ContractId = null;//no customer then contract + var head = await ct.HeadOffice.AsNoTracking().Where(z => z.Id == cust.headofficeId).Select(z => new { contractId = z.ContractId, contractExpires = z.ContractExpires }).FirstOrDefaultAsync(); + if (head.contractId != null && head.contractExpires < DateTime.UtcNow) + newObj.ContractId = head.contractId; + } } + //RESPONSE TIME / COMPLETE BY AUTO SET //precedence: manually pre-set -> contract -> global biz