/// <summary> /// Amends the last material batch transaction. /// </summary> /// <param name="batchId">The ID of the batch.</param> /// <param name="transactionId">The ID of the transaction.</param> /// <param name="quantity">The quantity to set.</param> /// <param name="userId">The ID of the user attempting to amend the transaction.</param> /// <exception cref="OpenMTS.Services.Exceptions.NotLastLogEntryException">Thrown if the passed transaction ID doesn't match the last transaction of this batch.</exception> /// <exception cref="UnauthorizedAccessException">Thrown if the last transaction was performed by a different user.</exception> public void AmendLastMaterialBatchTransaction(Guid batchId, Guid transactionId, double quantity, string userId) { MaterialBatch batch = GetBatchOrThrowNotFoundException(batchId); // Get and validate last transaction Transaction transaction = TransactionLogService.GetLastTransactionLogEntry(batchId); if (transaction.Id != transactionId) { throw new NotLastLogEntryException(transactionId); } // Validate user if (transaction.UserId != userId) { throw new UnauthorizedAccessException("The last transaction was performed by a different user."); } // Calculate new quantity differential quantity = RoundMaterialQuantity(quantity); double newBatchQuantity = batch.Quantity - transaction.Quantity + quantity; // Attempt to amend last transaction TransactionLogService.AmendLastTransactionLogEntry(batchId, transactionId, quantity); // No exception thrown - update material batch batch.Quantity = newBatchQuantity; MaterialBatchRepository.UpdateMaterialBatch(batch); }
/// <summary> /// Gets the last material batch transaction. /// </summary> /// <param name="batchId">The ID of the batch to get the last transaction for.</param> /// <exception cref="MaterialBatchNotFoundException">Thrown if the batch could be found.</exception> /// <returns>Returns the trnasaction</returns> public Transaction GetLastMaterialBatchTransaction(Guid batchId) { GetBatchOrThrowNotFoundException(batchId); return(TransactionLogService.GetLastTransactionLogEntry(batchId)); }