/// <summary> /// Archives a Product by setting boolean IsArchived to True /// </summary> /// <param name="id"></param> /// <returns></returns> public ServiceResponse <Data.Models.Product> ArchiveProduct(int id) { try { var product = _db.Products.Find(id); product.IsArchived = true; _db.SaveChanges(); return(new ServiceResponse <Data.Models.Product> { Data = product, Time = DateTime.UtcNow, Message = "Archived Product", IsSuccess = true }); } catch (Exception e) { return(new ServiceResponse <Data.Models.Product> { Data = null, Time = DateTime.UtcNow, Message = e.StackTrace, IsSuccess = false }); } }
/// <summary> /// Creates an open SalesOrder /// </summary> /// <param name="order"></param> /// <returns></returns> public ServiceResponse <bool> GenerateOpenOrder(SalesOrder order) { var now = DateTime.UtcNow; _logger.LogInformation("Generating new order"); foreach (var item in order.SalesOrderItems) { item.Product = _productService .GetProductById(item.Product.Id); var inventoryId = _inventoryService .GetByProductId(item.Product.Id).Id; _inventoryService .UpdateUnitsAvailable(inventoryId, -item.Quantity); } try { _db.SalesOrders.Add(order); _db.SaveChanges(); return(new ServiceResponse <bool> { IsSuccess = true, Data = true, Message = "Open order created", Time = now }); } catch (Exception e) { return(new ServiceResponse <bool> { IsSuccess = false, Data = false, Message = e.StackTrace, Time = now }); } }
/// <summary> /// Adds a new Customer record /// </summary> /// <param name="customer">Customer instance</param> /// <returns></returns> public ServiceResponse <Data.Models.Customer> CreateCustomer(Data.Models.Customer customer) { try { _db.Customers.Add(customer); _db.SaveChanges(); return(new ServiceResponse <Data.Models.Customer> { IsSuccess = true, Message = "New customer added", Time = DateTime.UtcNow, Data = customer }); } catch (Exception e) { return(new ServiceResponse <Data.Models.Customer> { IsSuccess = false, Message = e.StackTrace, Time = DateTime.UtcNow, Data = customer }); } }
/// <summary> /// Updates number of units of the provided product id /// Adjusts QuantityOnHand by adjustment value /// </summary> /// <param name="id">productId</param> /// <param name="adjustment">number of units added / removed from inventory</param> /// <returns></returns> public ServiceResponse <ProductInventory> UpdateUnitsAvailable(int id, int adjustment) { var now = DateTime.UtcNow; try { var inventory = _db.ProductInventories .Include(inv => inv.Product) .First(inv => inv.Product.Id == id); inventory.QuantityOnHand += adjustment; try { CreateSnapshot(); } catch (Exception e) { _logger.LogError("Error creating inventory snapshot"); _logger.LogError(e.StackTrace); } _db.SaveChanges(); return(new ServiceResponse <ProductInventory> { IsSuccess = true, Data = inventory, Message = $"Product {id} inventory adjusted", Time = now }); } catch { return(new ServiceResponse <ProductInventory> { IsSuccess = false, Data = null, Message = $"Error updating Product QuantityOnHand", Time = now }); } }