public async Task TriggerOfflineTableSync() { lock (thisLock) { var spoilTrt = GetLastRetreivedSpoil(); var freshSpoils = GetUnSyncedSpoilAfter(spoilTrt); freshSpoils.ForEach(trt => { Spoil dbTrt = this.GetSpoilBy(trt.TransactionRefNo); //_spoilSyncSvc.FirstOrDefault(t => t.TransactionRefNo == trt.TransactionRefNo); if (dbTrt == null) { trt.CreatedOnUtc = DateTime.UtcNow; trt.ModifiedOnUtc = DateTime.UtcNow; Log.Warning(String.Format("Spoil with stock ref no ({0}) couldn't be retreive for update.", trt.StockRefNo)); Log.Information(String.Format("Switching to adding Spoil with stock ref no ({0}) to sync table as newly created.", trt.StockRefNo)); //_spoilSyncSvc.Add(trt); _syncStoreProvider.AddSpoilToSync( Guid.NewGuid(), trt.TransactionRefNo, trt.StockRefNo, trt.StockDetails, trt.StockUnit, trt.StockUnitLeft, trt.IsSyncReady, trt.SyncStatus, trt.SpoilDetails, trt.ReasonSyncFailed, trt.ReportedBy, trt.SyncRefCreatedOn, trt.SyncRefModifiedOn, trt.MachineName, trt.SyncRefNo, trt.SyncDate, trt.SyncFailedCount, trt.RefCreatedDate, trt.RefModifiedDate, trt.Cost, trt.CreatedOnUtc, trt.ModifiedOnUtc, trt.IsDeleted); } else { Spoil.Extend(trt, dbTrt); _syncStoreProvider.UpdateSyncedSpoil(dbTrt.StockRefNo, dbTrt.StockDetails, dbTrt.StockUnit, dbTrt.StockUnitLeft, dbTrt.IsSyncReady, dbTrt.SyncStatus, dbTrt.SpoilDetails, dbTrt.ReportedBy, dbTrt.MachineName, dbTrt.RefCreatedDate, dbTrt.RefModifiedDate, dbTrt.Cost, dbTrt.Id); //_spoilSyncSvc.Update(dbTrt); } }); } await Task.FromResult <int>(1); }
private Spoil GetUnSyncSpoilBy(Guid?id) { var searchedSpoil = _storeProvider.GetUnSyncedSpoilDetailBy(id); if (searchedSpoil == null) { return(null); } var trt = Spoil.Create(searchedSpoil); trt.MachineName = Environment.MachineName; return(trt); }
private Spoil GetSpoilBy(string transactionRefNo) { var reader = _syncStoreProvider.GetSpoiltBy(transactionRefNo); if (!reader.Read()) { return(null); } Spoil spoil = new Spoil(); DataReaderHelper.DataReaderToObject(reader, spoil); return(spoil); }
public List <Spoil> GetUnSyncedSpoilAfter(Spoil lastSpoil) { List <SpoilDto> dtoSpoils; if (lastSpoil == null) { dtoSpoils = this.GetUnSyncedSpoil(null); } else { dtoSpoils = this.GetUnSyncedSpoil(lastSpoil.CreatedOnUtc); } var spoilList = new List <Spoil>(); dtoSpoils.ForEach(c => { var trt = Spoil.Create(c); trt.MachineName = Environment.MachineName; spoilList.Add(trt); }); return(spoilList); }
private void SyncSpoilChanges(SpoilDto spoilDto, TableDependency.Enums.ChangeType changeType) { var unsyncedSpoil = GetUnSyncSpoilBy(spoilDto.SpoilId); if (unsyncedSpoil == null && changeType != ChangeType.Delete) { return; } switch (changeType) { case ChangeType.Delete: { String spoilId = spoilDto.SpoilId.ToString(); var existingSpoil = _spoilSyncSvc.FirstOrDefault(t => t.TransactionRefNo == spoilId); if (existingSpoil != null) { existingSpoil.IsDeleted = true; Spoil.Extend(unsyncedSpoil, existingSpoil); _spoilSyncSvc.Update(existingSpoil); } break; } case TableDependency.Enums.ChangeType.Insert: { unsyncedSpoil.RefModifiedDate = unsyncedSpoil.RefCreatedDate; var dbTrt = _spoilSyncSvc.FirstOrDefault(t => t.TransactionRefNo == unsyncedSpoil.TransactionRefNo); if (dbTrt == null) { unsyncedSpoil.ModifiedOnUtc = unsyncedSpoil.CreatedOnUtc = DateTime.UtcNow; _spoilSyncSvc.Add(unsyncedSpoil); } else { Spoil.Extend(unsyncedSpoil, dbTrt); _spoilSyncSvc.Update(dbTrt); } break; } case TableDependency.Enums.ChangeType.Update: { if (spoilDto.IsDeleted) { goto case ChangeType.Delete; } var dbTrt = _spoilSyncSvc.FirstOrDefault(t => t.TransactionRefNo == unsyncedSpoil.TransactionRefNo); if (dbTrt == null) { unsyncedSpoil.CreatedOnUtc = DateTime.UtcNow; unsyncedSpoil.ModifiedOnUtc = DateTime.UtcNow; Log.Warning(String.Format("Spoil with TransactionRefNo ({0}) couldn't be retreived for update.", unsyncedSpoil.TransactionRefNo)); Log.Information(String.Format("Adding Spoil with TransactionRefNo ({0}) to sync table as newly created.", unsyncedSpoil.StockRefNo)); _spoilSyncSvc.Add(unsyncedSpoil); } else { Spoil.Extend(unsyncedSpoil, dbTrt); _spoilSyncSvc.Update(dbTrt); } break; } case TableDependency.Enums.ChangeType.None: default: break; } }
public HttpResponseMessage CreateWasteStock(SpoilViewModel spoilVM) { var response = new ApiResultViewModel <dynamic>(); if (spoilVM == null || spoilVM.Id == 0) { response.errorStatus = true; response.errorMessage = "Invalid request."; return(Request.CreateResponse(response)); } try { using (var uow = _spoilSvc.UnitOfWork) { uow.BeginTransaction(); var product = _prodSvc.GetProductById(spoilVM.Id); if (product == null) { response.errorStatus = true; response.errorMessage = "Product was not found."; return(Request.CreateResponse(response)); } if (product.Quantity < 0 || product.Quantity - spoilVM.Quantity < 0) { response.errorStatus = true; response.errorMessage = "Cannot report waste for a negative product."; return(Request.CreateResponse(response)); } var newSpoil = new Spoil(); var membershipId = IposConfig.UseMembership ? (Guid?)IposMembershipService.GetUserId(User.Identity.Name) : null; var identityUserId = User.Identity.GetUserId <int>(); newSpoil.Title = product.Name; newSpoil.Description = spoilVM.Description; newSpoil.Quantity = spoilVM.Quantity; newSpoil.Product_Id = product.ProductId; newSpoil.CreatedBy_Id = identityUserId; newSpoil.User_Id = membershipId; _spoilSvc.NewWaste(newSpoil); if (newSpoil.HasErrors) { response.errorStatus = newSpoil.HasErrors; response.errorMessage = newSpoil.ValidationErrors.FirstOrDefault() != null? newSpoil.ValidationErrors.FirstOrDefault().ErrorMessage : String.Empty; } else { product.Quantity -= newSpoil.Quantity; _prodSvc.Update(product); var eventDescription = String.Format("{0} quantity of {1} was entered as a waste.", newSpoil.Quantity, product.Name); _auditSvc.LogEvent(eventDescription, AuditType.NEW_WASTE, membershipId, identityUserId); uow.Commit(); response.result = new { product.Quantity, Id = product.ProductId }; response.message = "Waste has now been reported."; } } } catch (Exception ex) { _logger.Log(ex); #if DEBUG response.errorMessage = ex.Message; response.errorStatus = true; #else response.errorMessage = "Error occured, please contact admin."; response.errorStatus = true; #endif } return(Request.CreateResponse(response)); }