/// <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> /// Updates the status of a material batch (whether it is locked or not). /// </summary> /// <param name="batchId">The ID of the batch to lock or unlock.</param> /// <param name="isLocked">Whether the batch should be locked..</param> /// <exception cref="MaterialBatchNotFoundException">Thrown if no matching batch could be found.</exception> public void UpdateBatchStatus(Guid batchId, bool isLocked) { MaterialBatch batch = GetBatchOrThrowNotFoundException(batchId); batch.IsLocked = isLocked; MaterialBatchRepository.UpdateMaterialBatch(batch); }
/// <summary> /// Updates a material batch. /// </summary> /// <param name="batchId">The ID of the batch to update.</param> /// <param name="material">The material this batch consists of.</param> /// <param name="expirationDate">The expiration date of the material.</param> /// <param name="storageLocation">The storage location of the batch.</param> /// <param name="batchNumber">The batch number.</param> /// <param name="customProps">The custom prop values for this batch.</param> /// <exception cref="MaterialBatchNotFoundException">Thrown if no matching batch could be found.</exception> /// <returns>Returns the updated batch.</returns> public MaterialBatch UpdateMaterialBatch(Guid batchId, Material material, DateTime expirationDate, StorageLocation storageLocation, long batchNumber, Dictionary <Guid, string> customProps) { // Get batch MaterialBatch batch = GetBatchOrThrowNotFoundException(batchId); // Validate expirationd date IEnumerable <Transaction> log = TransactionLogService.GetTransactionLog(batchId); DateTime originalCheckIn = log.Last().Timestamp.Date; if (expirationDate <= originalCheckIn) { throw new ArgumentException("The expiration date cannot be set prior to the original check in date of the material batch."); } // Proceed with update batch.Material = material; batch.ExpirationDate = expirationDate; batch.StorageLocation = storageLocation; batch.BatchNumber = batchNumber; batch.CustomProps = customProps; MaterialBatchRepository.UpdateMaterialBatch(batch); return(batch); }
/// <summary> /// Performs a material transaction: checking material in or out of storage. /// </summary> /// <param name="batchId">The ID of the batch to perform a transaction on.</param> /// <param name="quantity">The quantity to check out or in. Negative numbers indicate a check-out, positive numbers a check-in.</param> /// <param name="userId">The ID of the user performing the transaction.</param> /// <returns>Returns the transcation.</returns> /// <exception cref="ArgumentException">Thrown if the new computed quantity of material is less than 0.</exception> public Transaction PerformMaterialTransaction(Guid batchId, double quantity, string userId) { MaterialBatch batch = GetBatchOrThrowNotFoundException(batchId); // Check whether batch is locked if (batch.IsLocked) { throw new UnauthorizedAccessException("This batch is locked!"); } // Compute and validate new quantity double newQuantity = RoundMaterialQuantity(batch.Quantity + quantity); if (newQuantity < 0) { throw new ArgumentException("The quantity of a batch cannot be less than 0. You cannot check out more material than there is in the inventory!"); } // Update quantity batch.Quantity = newQuantity; if (batch.Quantity == 0) { batch.IsArchived = true; } // Generate transaction Transaction transaction = new Transaction() { Id = Guid.NewGuid(), MaterialBatchId = batchId, Quantity = quantity, Timestamp = DateTime.UtcNow, UserId = userId }; // Persist batch and log transaction MaterialBatchRepository.UpdateMaterialBatch(batch); TransactionLogService.LogTransaction(transaction); // Done - return transaction! return(transaction); }