/// <summary> /// Checks supply required restaurants and supply required products /// </summary> /// <param name="date">supplyDate</param> public void Supply(DateTime date) { var supplyRequiredRestaurants = _restaurantService.GetBySupplyDayOfWeek(new GetRestaurantsBySupplyDayOfWeekRequest { SupplyDayOfWeek = (byte)date.DayOfWeek }).Restaurants; var supplyRequiredRestaurantCount = supplyRequiredRestaurants.Count; //If today isn't a supply day for any restaurant if (supplyRequiredRestaurantCount == default(int)) { return; } foreach (var restaurant in supplyRequiredRestaurants) { //Parallel.ForEach(supplyRequiredRestaurants, restaurant => //{ //Get sales staff for current restaurant var responsibleStaff = GetResponsibleStaff(restaurant.ID); if (responsibleStaff == null) { //LOG return; } //Get Latest stock transactions var latestStockTransactionLookup = _productService.GetLatestStockTransactions(new GetLatestStockTransactionsRequest { RestaurantId = restaurant.ID, StartDate = date.Date.AddDays(-7) }).LastStockTransactions.ToLookup(x => x.ProductID); //Get products that have stock transaction for current restaurant var productIdList = latestStockTransactionLookup.Select(x => x.Key); foreach (var productId in productIdList) { //Get latest transactions for current restaurant - product pair. var latestProductTransactions = latestStockTransactionLookup[productId]; //Get Remaining Amount var lastTransaction = latestProductTransactions.OrderBy(x => x.CreatedDatetime).LastOrDefault(); if (lastTransaction == null) { // There is no transaction for this seven days. So, no order is reqired. } //Get consumed amount for current restaurant - product pair. var consumedAmount = latestProductTransactions.Where(x => x.TransactionAmount < 0).Sum(x => x.TransactionAmount); //Check if stock is enough for next week var requiredAmount = Math.Abs(consumedAmount) - lastTransaction.RemainingAmount; if (requiredAmount > 0) { //Give an order for this product var giveOrderResponse = _supplyService.GiveOrder(BuildGiveOrderRequest(restaurant.ID, responsibleStaff.ID, productId, requiredAmount, date)); //Given orders are provided by supplier. _supplyService.Provide(new ProvideProductOrderRequest { OrderID = giveOrderResponse.OrderID, UnitsInStock = lastTransaction.RemainingAmount, ExpirationDate = date.AddDays(RandomHelper.RandomInteger(Constants.ProductExpirationDayRange.Min, Constants.ProductExpirationDayRange.Max)), ReceivedDateTime = date }); } } //Parallel.ForEach(productIdList, productId => // { // //Get latest transactions for current restaurant - product pair. // var latestProductTransactions = latestStockTransactionLookup[productId]; // //Get Remaining Amount // var lastTransaction = latestProductTransactions.OrderBy(x => x.CreatedDatetime).LastOrDefault(); // if (lastTransaction == null) // { // // There is no transaction for this seven days. So, no order is reqired. // } // //Get consumed amount for current restaurant - product pair. // var consumedAmount = latestProductTransactions.Where(x => x.TransactionAmount < 0).Sum(x => x.TransactionAmount); // //Check if stock is enough for next week // var requiredAmount = consumedAmount - lastTransaction.RemainingAmount; // if (requiredAmount > 0) // { // //Give an order for this product // var giveOrderResponse = _supplyService.GiveOrder(BuildGiveOrderRequest(restaurant.ID, responsibleStaff.ID, productId, requiredAmount)); // //Given orders are provided by supplier. // _supplyService.Provide(new ProvideProductOrderRequest // { // OrderID = giveOrderResponse.OrderID, // UnitsInStock = lastTransaction.RemainingAmount, // ExpirationDate = date.AddDays(RandomHelper.RandomInteger(Constants.ProductExpirationDayRange.Min, Constants.ProductExpirationDayRange.Max)) // }); // } // }); // }); } }