/// <summary> /// Returns an instance of <see cref="CashboxViewModel"/> reflecting the present and any pending state /// and settings of a <see cref="CashBox"/> for customer id of <paramref name="customerId"/> /// and a cashbox id of <paramref name="cashboxId"/>. /// </summary> /// <param name="customerId">The customer id</param> /// <param name="cashboxId">The cashbox id</param> /// <returns>An instance of <see cref="CashboxViewModel"/></returns> public CashboxViewModel GetPendingViewModel(int customerId, int cashboxId) { CashboxViewModel model = GetViewModel(customerId, cashboxId); var cashbox = PemsEntities.CashBoxes.FirstOrDefault(m => m.CustomerID == model.CustomerId && m.CashBoxSeq == cashboxId); // Get the pending changes from [AssetPending] var assetPending = PemsEntities.AssetPendings.FirstOrDefault(m => m.CustomerId == model.CustomerId && m.AreaId == model.AreaId && m.MeterMapCashBoxId == cashbox.CashBoxID); if (assetPending.AssetModel.HasValue) { model.AssetModel = PemsEntities.MechanismMasterCustomers.FirstOrDefault(m => m.CustomerId == model.CustomerId && m.MechanismId == assetPending.AssetModel.Value && m.IsDisplay) == null ? "" : PemsEntities.MechanismMasterCustomers.FirstOrDefault(m => m.CustomerId == model.CustomerId && m.MechanismId == assetPending.AssetModel.Value && m.IsDisplay).MechanismDesc; } if (!string.IsNullOrWhiteSpace(assetPending.AssetName)) { model.Name = assetPending.AssetName; } // Preventive maint. dates. if (assetPending.LastPreventativeMaintenance.HasValue) { model.LastPrevMaint = assetPending.LastPreventativeMaintenance.Value.ToShortDateString(); } if (assetPending.NextPreventativeMaintenance.HasValue) { model.NextPrevMaint = assetPending.NextPreventativeMaintenance.Value.ToShortDateString(); } if (assetPending.WarrantyExpiration.HasValue) { model.WarrantyExpiration = assetPending.WarrantyExpiration.Value.ToShortDateString(); } // Cashbox install date. if (assetPending.DateInstalled.HasValue) { model.InstallDate = assetPending.DateInstalled.Value.ToShortDateString(); } // Operational status of cashbox. if (assetPending.OperationalStatus.HasValue) { model.Status = PemsEntities.OperationalStatus.FirstOrDefault(m => m.OperationalStatusId == assetPending.OperationalStatus.Value).OperationalStatusDesc; model.StatusDate = assetPending.OperationalStatusTime; } // Is asset active? model.State = AssetStateType.Pending; // Get target ActivationDate model.ActivationDate = assetPending.RecordMigrationDateTime.HasValue ? assetPending.RecordMigrationDateTime.Value.ToShortDateString() : ""; // Get last update by information CreateAndLastUpdate(model); return(model); }
/// <summary> /// Gets a <see cref="List{CashboxViewModel}"/> of <see cref="CashboxViewModel"/> representing the known history of a /// cashbox referenced by cashbox id of <paramref name="assetId"/> and customer id of <paramref name="customerId"/>. /// Return only a list that are between <paramref name="startDateTicks"/> and <paramref name="endDateTicks"/> /// </summary> /// <param name="request">Instance of <see cref="DataSourceRequest"/> from Kendo UI</param> /// <param name="total">Out parameter used to return total number of records being returned</param> /// <param name="customerId">The customer id</param> /// <param name="areaId">The area id</param> /// <param name="assetId">The cashbox id</param> /// <param name="startDateTicks">From date in ticks</param> /// <param name="endDateTicks">To date in ticks</param> /// <returns><see cref="List{CashboxViewModel}"/> instance</returns> public List <CashboxViewModel> GetHistory([DataSourceRequest] DataSourceRequest request, out int total, int customerId, int areaId, Int64 assetId, long startDateTicks, long endDateTicks) { DateTime startDate; DateTime endDate; IQueryable <CashBoxAudit> query = null; if (startDateTicks > 0 && endDateTicks > 0) { startDate = (new DateTime(startDateTicks, DateTimeKind.Utc)).ToLocalTime(); endDate = (new DateTime(endDateTicks, DateTimeKind.Utc)).ToLocalTime(); query = PemsEntities.CashBoxAudits.Where(m => m.CustomerID == customerId && m.CashBoxID == assetId && m.UpdateDateTime >= startDate && m.UpdateDateTime <= endDate).OrderBy(m => m.UpdateDateTime); } else if (startDateTicks > 0) { startDate = (new DateTime(startDateTicks, DateTimeKind.Utc)).ToLocalTime(); query = PemsEntities.CashBoxAudits.Where(m => m.CustomerID == customerId && m.CashBoxID == assetId && m.UpdateDateTime >= startDate).OrderBy(m => m.UpdateDateTime); } else if (endDateTicks > 0) { endDate = (new DateTime(endDateTicks, DateTimeKind.Utc)).ToLocalTime(); query = PemsEntities.CashBoxAudits.Where(m => m.CustomerID == customerId && m.CashBoxID == assetId && m.UpdateDateTime <= endDate).OrderBy(m => m.UpdateDateTime); } else { query = PemsEntities.CashBoxAudits.Where(m => m.CustomerID == customerId && m.CashBoxID == assetId).OrderBy(m => m.UpdateDateTime); } // Get the CashboxViewModel for each audit record. var list = new List <CashboxViewModel>(); // Get present data var presentModel = GetViewModel(customerId, (int)assetId); presentModel.RecordDate = Now; list.Add(presentModel); // Get historical data view models CashboxViewModel previousModel = null; foreach (var cashbox in query) { var activeModel = CreateHistoricViewModel(cashbox); list.Add(activeModel); if (previousModel != null) { previousModel.RecordSuperceededDate = activeModel.RecordDate; } previousModel = activeModel; } // Now filter, sort and page. var items = list.AsQueryable(); items = items.ApplyFiltering(request.Filters); total = items.Count(); items = items.ApplySorting(request.Groups, request.Sorts); items = items.ApplyPaging(request.Page, request.PageSize); return(items.ToList()); }
/// <summary> /// Creates a clone (copy) of the <see cref="CashBox"/> refrenced by <paramref name="model"/>. This cloned /// <see cref="CashBox"/> is written to the [CashBox] table and the [AssetPending] table. /// </summary> /// <param name="model">An instance of <see cref="CashboxViewModel"/> referencinf cashbox to be cloned</param> /// <returns>Integer representing the new cashbox id. <see cref="CashBox.CashBoxID"/></returns> public int Clone(CashboxViewModel model) { return(Clone(model.CustomerId, model.AreaId, model.AssetId)); }
/// <summary> /// Private method to create a <see cref="CashboxViewModel"/> from a <see cref="CashBoxAudit"/> record. /// </summary> /// <param name="auditModel">An instance of <see cref="CashBoxAudit"/></param> /// <returns>An instance of <see cref="CashboxViewModel"/></returns> private CashboxViewModel CreateHistoricViewModel(CashBoxAudit auditModel) { var model = new CashboxViewModel() { CustomerId = auditModel.CustomerID, AreaId = 0, AssetId = auditModel.CashBoxID, GlobalId = auditModel.CashBoxID.ToString() }; model.Type = auditModel.CashBoxType == null ? CashboxViewModel.DefaultType : PemsEntities.MeterGroups.FirstOrDefault(m => m.MeterGroupId == auditModel.CashBoxType).MeterGroupDesc ?? CashboxViewModel.DefaultType; model.TypeId = auditModel.CashBoxType ?? (int)MeterGroups.Cashbox; model.AssetModel = auditModel.CashBoxModel == null ? "" : PemsEntities.MechanismMasterCustomers.FirstOrDefault(m => m.MechanismId == auditModel.CashBoxModel && m.IsDisplay) == null ? "" : PemsEntities.MechanismMasterCustomers.FirstOrDefault(m => m.MechanismId == auditModel.CashBoxModel && m.IsDisplay).MechanismDesc; model.Name = auditModel.CashBoxName ?? " "; model.Location = auditModel.CashBoxLocationTypeId == null ? "" : PemsEntities.CashBoxLocationTypes.FirstOrDefault(m => m.CashBoxLocationTypeId == auditModel.CashBoxLocationTypeId).CashBoxLocationTypeDesc ?? ""; // Note: These values are not recorded in an audit table. model.CurrentMeterName = ""; // Preventive maint. dates. model.LastPrevMaint = auditModel.LastPreventativeMaintenance.HasValue ? auditModel.LastPreventativeMaintenance.Value.ToShortDateString() : ""; model.NextPrevMaint = auditModel.NextPreventativeMaintenance.HasValue ? auditModel.NextPreventativeMaintenance.Value.ToShortDateString() : ""; // Warranty Expiration model.WarrantyExpiration = auditModel.WarrantyExpiration.HasValue ? auditModel.WarrantyExpiration.Value.ToShortDateString() : ""; // Cashbox install date. model.InstallDate = auditModel.InstallDate.HasValue ? auditModel.InstallDate.Value.ToShortDateString() : ""; // Operational status of cashbox. model.Status = auditModel.OperationalStatus == null ? "" : PemsEntities.OperationalStatus.FirstOrDefault(m => m.OperationalStatusId == auditModel.OperationalStatus).OperationalStatusDesc ?? ""; model.StatusDate = auditModel.OperationalStatusTime; // Get last meter associated with this cash box. // Note: These values are not recorded in an audit table. model.LastMeterId = 0; model.LastCollected = null; model.LastCollectedValue = 0; // Asset state model.State = (AssetStateType)auditModel.CashBoxState; // Updated by model.LastUpdatedById = auditModel.UserId; model.LastUpdatedBy = (new UserFactory()).GetUserById(model.LastUpdatedById).FullName(); // Get the full-spell change reason. model.LastUpdatedReason = (AssetPendingReasonType)(auditModel.AssetPendingReasonId ?? 0); model.LastUpdatedReasonDisplay = PemsEntities.AssetPendingReasons.FirstOrDefault(m => m.AssetPendingReasonId == (int)model.LastUpdatedReason).AssetPendingReasonDesc; // Record date model.RecordDate = auditModel.UpdateDateTime; // Audit record id model.AuditId = auditModel.CashBoxAuditId; return(model); }
/// <summary> /// Returns an instance of <see cref="CashboxViewModel"/> reflecting the present state /// and settings of a <see cref="CashBox"/> for customer id of <paramref name="customerId"/> /// and a cashbox id of <paramref name="cashboxId"/> /// </summary> /// <param name="customerId">The customer id</param> /// <param name="cashboxId">The cashbox id</param> /// <returns>An instance of <see cref="CashboxViewModel"/></returns> public CashboxViewModel GetViewModel(int customerId, int cashboxId) { var model = new CashboxViewModel { CustomerId = customerId, AreaId = 0, AssetId = cashboxId }; var cashbox = PemsEntities.CashBoxes.FirstOrDefault(m => m.CustomerID == customerId && m.CashBoxSeq == cashboxId); model.GlobalId = cashbox.CashBoxID.ToString(); model.Type = cashbox.MeterGroup == null ? CashboxViewModel.DefaultType : cashbox.MeterGroup.MeterGroupDesc ?? CashboxViewModel.DefaultType; model.TypeId = cashbox.CashBoxType ?? (int)MeterGroups.Cashbox; model.AssetModel = cashbox.MechanismMaster == null ? " " : PemsEntities.MechanismMasterCustomers.FirstOrDefault(m => m.CustomerId == model.CustomerId && m.MechanismId == cashbox.MechanismMaster.MechanismId && m.IsDisplay) == null ? "" : PemsEntities.MechanismMasterCustomers.FirstOrDefault(m => m.CustomerId == model.CustomerId && m.MechanismId == cashbox.MechanismMaster.MechanismId && m.IsDisplay).MechanismDesc; model.Name = cashbox.CashBoxName ?? " "; model.Location = cashbox.CashBoxLocationType == null ? "" : cashbox.CashBoxLocationType.CashBoxLocationTypeDesc ?? ""; // Current meter associated with cashbox var collDataSumm = PemsEntities.CollDataSumms .Where(m => m.NewCashBoxID == cashbox.CashBoxName && m.CustomerId == customerId).OrderBy(m => m.CollDateTime).FirstOrDefault(); model.CurrentMeterName = collDataSumm == null ? "" : (collDataSumm.Meter == null ? "" : collDataSumm.Meter.Location ?? ""); // Preventive maint. dates. model.LastPrevMaint = cashbox.LastPreventativeMaintenance.HasValue ? cashbox.LastPreventativeMaintenance.Value.ToShortDateString() : ""; model.NextPrevMaint = cashbox.NextPreventativeMaintenance.HasValue ? cashbox.NextPreventativeMaintenance.Value.ToShortDateString() : ""; // Warranty Expiration model.WarrantyExpiration = cashbox.WarrantyExpiration.HasValue ? cashbox.WarrantyExpiration.Value.ToShortDateString() : ""; // Cashbox install date. model.InstallDate = cashbox.InstallDate.HasValue ? cashbox.InstallDate.Value.ToShortDateString() : ""; // Operational status of cashbox. model.Status = cashbox.OperationalStatu == null ? "" : cashbox.OperationalStatu.OperationalStatusDesc ?? ""; model.StatusDate = cashbox.OperationalStatusTime; // Get last meter associated with this cash box. collDataSumm = PemsEntities.CollDataSumms .Where(m => m.OldCashBoxID == cashbox.CashBoxName && m.CustomerId == customerId).OrderBy(m => m.CollDateTime).FirstOrDefault(); if (collDataSumm != null) { model.LastMeterId = collDataSumm.MeterId; model.LastCollected = collDataSumm.CollDateTime.ToString(); model.LastCollectedValue = collDataSumm.Amount; } // See if cashbox has pending changes. model.HasPendingChanges = (new PendingFactory(ConnectionStringName, Now)).Pending(model); // Is asset active? model.State = (AssetStateType)cashbox.CashBoxState; // Get last update by information CreateAndLastUpdate(model); return(model); }