/// <summary> /// Creates a new material batch. /// </summary> /// <param name="material">The material the batch is composed of..</param> /// <param name="expirationDate">The expiration date of the material.</param> /// <param name="storageLocation">The storage location of the material.</param> /// <param name="batchNumber">The manufacturer provided batch number.</param> /// <param name="quantity">The quantity of the batch.</param> /// <param name="customProps">The custom prop values for this batch.</param> /// <param name="isLocked">Whether the batch should be locked.</param> /// <param name="userId">The ID of the user checking in the new batch..</param> /// <returns>Returns the newly created batch.</returns> public MaterialBatch CreateMaterialBatch(Material material, DateTime expirationDate, StorageLocation storageLocation, long batchNumber, double quantity, Dictionary <Guid, string> customProps, bool isLocked, string userId) { // Create batch MaterialBatch batch = MaterialBatchRepository.CreateMaterialBatch(material, expirationDate, storageLocation, batchNumber, quantity, customProps, isLocked); // Log transaction Transaction transaction = new Transaction() { Id = Guid.NewGuid(), MaterialBatchId = batch.Id, Quantity = batch.Quantity, Timestamp = DateTime.UtcNow, UserId = userId }; TransactionLogService.LogTransaction(transaction); // Done - return newly create 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); }