Exemple #1
0
        private async Task <bool> RemoveFromInventory(PurchaseDetail purchaseDetail, DateTime date)
        {
            var productInventory = await _context.ProductInventory.FirstOrDefaultAsync(m =>
                                                                                       m.ProductId == purchaseDetail.ProductId &&
                                                                                       m.LocationId == purchaseDetail.ArrivedAtLocationId);

            decimal currentBalance = 0;

            if (productInventory != null)
            {
                currentBalance                = productInventory.Balance;
                productInventory.Balance      = productInventory.Balance - purchaseDetail.Amount;
                productInventory.ModifiedDate = date;
            }
            else
            {
                await _context.ProductInventory.AddAsync(
                    new ProductInventory
                {
                    Balance      = -1 * purchaseDetail.Amount,
                    BinCode      = "",
                    LocationId   = purchaseDetail.ArrivedAtLocationId.Value,
                    ModifiedDate = date,
                    ProductId    = purchaseDetail.ProductId
                });
            }

            var productInventoryHistory = new ProductInventoryHistory
            {
                ChangedBalance  = currentBalance - purchaseDetail.Amount,
                Balance         = -1 * purchaseDetail.Amount,
                Notes           = $"Purchase Id: {purchaseDetail.PurchaseId} Deleted.",
                BinCode         = "",
                LocationId      = purchaseDetail.ArrivedAtLocationId.Value,
                CreatedByUserId = purchaseDetail.CreatedByUserId,
                ModifiedDate    = date,
                ProductId       = purchaseDetail.ProductId,
                TransactionType = "Purchase Deleted"
            };

            _context.ProductInventoryHistory.Add(productInventoryHistory);
            return(true);
        }
        public async Task <IActionResult> PostProductInventoryHistory([FromBody] ProductInventoryHistory productInventoryHistory)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            decimal currentBalance = 0;

            System.Security.Claims.ClaimsPrincipal currentUser = this.User;
            var userId = _userManager.GetUserId(User);

            productInventoryHistory.CreatedByUserId = userId;
            var date = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTime.UtcNow, "Pacific Standard Time");

            productInventoryHistory.ModifiedDate = date;

            // Update Product Inventory
            var productInventory = await _context.ProductInventory.FirstOrDefaultAsync(m =>
                                                                                       m.ProductId == productInventoryHistory.ProductId &&
                                                                                       m.LocationId == productInventoryHistory.LocationId);

            if (productInventory != null)
            {
                currentBalance                = productInventory.Balance;
                productInventory.Balance      = productInventoryHistory.Balance;
                productInventory.BinCode      = productInventoryHistory.BinCode;
                productInventory.ModifiedDate = productInventoryHistory.ModifiedDate;
            }
            else
            {
                var newProductInventory = new ProductInventory
                {
                    Balance      = productInventoryHistory.Balance,
                    BinCode      = productInventoryHistory.BinCode,
                    LocationId   = productInventoryHistory.LocationId,
                    ModifiedDate = productInventoryHistory.ModifiedDate,
                    ProductId    = productInventoryHistory.ProductId
                };
                _context.ProductInventory.Add(newProductInventory);
            }

            // Calculate if the inventory is up or down
            productInventoryHistory.Balance = productInventoryHistory.Balance - currentBalance;
            var operationType = "";

            operationType = productInventoryHistory.Balance > 0 ? "Stock Up - " : "Stock Down - ";
            productInventoryHistory.TransactionType = productInventoryHistory.Balance > 0 ? "Stock Up" : "Stock Down";
            productInventoryHistory.Notes           = operationType + productInventoryHistory.Notes;

            if (productInventoryHistory.Balance != 0)
            {
                _context.ProductInventoryHistory.Add(productInventoryHistory);
            }

            await _context.SaveChangesAsync();

            if (productInventoryHistory.Balance != 0)
            {
                var product = await _context.Product.FirstOrDefaultAsync(p => p.ProductId == productInventoryHistory.ProductId);

                var location = await _context.Location.FirstOrDefaultAsync(p => p.LocationId == productInventoryHistory.LocationId);

                var subject = $"Inventory {productInventoryHistory.TransactionType} - {location.LocationName}";
                var message = $"Product: {product.ProductCode} - {product.ProductName}.\n";
                message += $"Inventory {productInventoryHistory.TransactionType}: {productInventoryHistory.Balance}.\n";
                message += $"Changed From: {currentBalance} To:{productInventory.Balance}. {productInventoryHistory.TransactionType}.\n";
                message += $"Date: {productInventoryHistory.ModifiedDate}.\n";
                message += $"Location: {location.LocationName}.\n";
                message += $"User: {userId}.\n";

                await _emailSender.SendEmailAsync(null, subject, null, message, null, null);
            }

            return(CreatedAtAction("GetProductInventoryHistory", new { id = productInventoryHistory.ProductInventoryHistoryId }, productInventoryHistory));
        }
        public async Task <IActionResult> PutProductInventoryHistory([FromRoute] int id, [FromBody] ProductInventoryHistory productInventoryHistory)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != productInventoryHistory.ProductInventoryHistoryId)
            {
                return(BadRequest());
            }

            _context.Entry(productInventoryHistory).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ProductInventoryHistoryExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        public async Task <IActionResult> PostTransferInventory([FromBody] TransferInventory transferInventory)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            System.Security.Claims.ClaimsPrincipal currentUser = this.User;
            var userId = _userManager.GetUserId(User);
            var date   = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTime.UtcNow, "Pacific Standard Time");
            var fromProductInventoryHistory = new ProductInventoryHistory
            {
                CreatedByUserId = userId,
                ModifiedDate    = date,
                ProductId       = transferInventory.ProductId,
                Balance         = transferInventory.TransferQuantity * -1,
                LocationId      = transferInventory.FromLocationId,
                BinCode         = "",
                Notes           = "Transfer - " + transferInventory.TransferNotes,
                TransactionType = "Transfer",
            };
            var toProductInventoryHistory = new ProductInventoryHistory
            {
                CreatedByUserId = userId,
                ModifiedDate    = date,
                ProductId       = transferInventory.ProductId,
                Balance         = transferInventory.TransferQuantity,
                LocationId      = transferInventory.ToLocationId,
                BinCode         = "",
                Notes           = "Transfer - " + transferInventory.TransferNotes,
                TransactionType = "Transfer",
            };

            // Update Product Inventory
            var fromProductInventory = await _context.ProductInventory.FirstOrDefaultAsync(m =>
                                                                                           m.ProductId == transferInventory.ProductId &&
                                                                                           m.LocationId == transferInventory.FromLocationId);

            if (fromProductInventory != null)
            {
                fromProductInventory.Balance      = fromProductInventory.Balance - transferInventory.TransferQuantity;
                fromProductInventory.ModifiedDate = fromProductInventoryHistory.ModifiedDate;
            }
            else
            {
                var newProductInventory = new ProductInventory
                {
                    Balance      = -transferInventory.TransferQuantity,
                    BinCode      = "",
                    LocationId   = transferInventory.FromLocationId,
                    ModifiedDate = fromProductInventoryHistory.ModifiedDate,
                    ProductId    = transferInventory.ProductId
                };
                _context.ProductInventory.Add(newProductInventory);
            }

            var toProductInventory = await _context.ProductInventory.FirstOrDefaultAsync(m =>
                                                                                         m.ProductId == transferInventory.ProductId &&
                                                                                         m.LocationId == transferInventory.ToLocationId);

            if (toProductInventory != null)
            {
                toProductInventory.Balance      = toProductInventory.Balance + transferInventory.TransferQuantity;
                toProductInventory.ModifiedDate = toProductInventoryHistory.ModifiedDate;
            }
            else
            {
                var newProductInventory = new ProductInventory
                {
                    Balance      = transferInventory.TransferQuantity,
                    BinCode      = "",
                    LocationId   = transferInventory.ToLocationId,
                    ModifiedDate = toProductInventoryHistory.ModifiedDate,
                    ProductId    = transferInventory.ProductId
                };
                _context.ProductInventory.Add(newProductInventory);
            }


            var product = await _context.Product.FirstOrDefaultAsync(p => p.ProductId == transferInventory.ProductId);

            var fromLocation = await _context.Location.FirstOrDefaultAsync(p => p.LocationId == transferInventory.FromLocationId);

            var toLocation = await _context.Location.FirstOrDefaultAsync(p => p.LocationId == transferInventory.ToLocationId);

            var subject = $"Inventory Transfer From: {fromLocation.LocationName} To: {toLocation.LocationName}";
            var message = $"Product: {product.ProductCode} - {product.ProductName}.\n";

            message += $"Inventory Transfer.\n";
            message += $"From: {fromLocation.LocationName} To: {toLocation.LocationName}.\n";
            message += $"Amount: {transferInventory.TransferQuantity}.\n";
            message += $"Date: {toProductInventoryHistory.ModifiedDate}.\n";
            message += $"User: {userId}.\n";
            await _emailSender.SendEmailAsync(null, subject, null, message, null, null);

            _context.ProductInventoryHistory.Add(fromProductInventoryHistory);
            _context.ProductInventoryHistory.Add(toProductInventoryHistory);

            await _context.SaveChangesAsync();

            return(Ok(transferInventory.ProductId));
        }