public PalletSync DispatchPalletsFromHandheld(PalletSync currentDispatch, int userId) { int?resourceId = _userService.GetResourceIdByUserId(userId); if (resourceId == 0) { resourceId = null; } var item = new PalletsDispatch { CompletedBy = userId, MarketVehicleID = currentDispatch.PalletDispatchInfo.MarketVehicleID, VehicleDriverResourceID = resourceId, ProofOfDeliveryImageFilenames = currentDispatch.PalletDispatchInfo.ProofOfDeliveryImageFilenames, DispatchNotes = currentDispatch.PalletDispatchInfo.DispatchNotes, CreatedBy = userId, DateCreated = DateTime.UtcNow, TrackingReference = currentDispatch.PalletDispatchInfo.TrackingReference, SentMethodID = currentDispatch.PalletDispatchInfo.SentMethodID > 0 ? currentDispatch.PalletDispatchInfo.SentMethodID : (int?)null, DateCompleted = currentDispatch.DateCompleted, DispatchStatus = PalletDispatchStatusEnum.Created, OrderProcessID = currentDispatch.OrderProcessID }; if (item.MarketVehicleID < 1) { var vehicle = _marketServices.GetMarketVehicleByVehicleNumber(currentDispatch.PalletDispatchInfo.VehicleIdentifier); if (vehicle == null) { vehicle = _marketServices.SaveMarketVehicle(new MarketVehicle() { CreatedBy = userId, TenantId = currentDispatch.TenantId, DateCreated = currentDispatch.DateCreated, Name = currentDispatch.PalletDispatchInfo.CustomVehicleModel, VehicleIdentifier = currentDispatch.PalletDispatchInfo.VehicleIdentifier }, userId); } item.MarketVehicleID = vehicle.Id; } _currentDbContext.PalletsDispatches.Add(item); _currentDbContext.SaveChanges(); foreach (var palletId in currentDispatch.SelectedPallets) { var pallet = _currentDbContext.Pallets.Find(palletId); if (pallet != null) { pallet.DateCompleted = DateTime.UtcNow; pallet.DateUpdated = DateTime.UtcNow; pallet.CompletedBy = userId; pallet.PalletsDispatch = item; _currentDbContext.Entry(pallet).State = EntityState.Modified; _currentDbContext.SaveChanges(); UpdatePalletProof(pallet.PalletNumber, currentDispatch.ProofOfLoadingImageBytes); } } currentDispatch.IsDispatched = true; currentDispatch.CompletedBy = userId; currentDispatch.DateCompleted = DateTime.UtcNow; //update order process status to dispatched as well var orderProcess = _currentDbContext.OrderProcess.Find(currentDispatch.OrderProcessID); orderProcess.OrderProcessStatusId = (int)OrderProcessStatusEnum.Dispatched; orderProcess.DateUpdated = DateTime.UtcNow; orderProcess.UpdatedBy = userId; _currentDbContext.Entry(orderProcess).State = EntityState.Modified; _currentDbContext.SaveChanges(); currentDispatch.PalletDispatchInfo = AutoMapper.Mapper.Map <PalletsDispatch, PalletDispatchSync>(item); return(currentDispatch); }