/// <summary> /// Update the stock of item as per the Allocated for the items that are re-recommended. /// </summary> /// <param name="alloc"></param> /// <param name="quantToReduce">Quantity to reduce.</param> private static void UpdateItemStock(OimsDataModel.Allocated alloc, float quantToReduce) { // Get item matching with item id in 'allocated' from 'items' var item = OimsDataContext.Items.FirstOrDefault(i => i.I_Id.Equals(alloc.ItemId)); if (item == null) { return; } item.I_Quantity = item.I_Quantity - quantToReduce; OimsDataContext.FlushChanges(); }
/// <summary> /// Save request by moving each request details into 'allocated'. Also subtract same item quantities from stock too. /// </summary> /// <param name="requestorId">Id of the requestor who created the request.</param> /// <param name="updatorId">Id of the updator who is updating the request.</param> /// <param name="statusId">Status id for the request.</param> /// <param name="requestId">Request id assigned to current request.</param> public static void SaveRequest(int requestorId, int updatorId, int statusId, int requestId) { #region 1. If item (oi) in request, save items in table 'allocated' using 'orderitem' var orderItems = OimsDataContext.Itemsrequests.Where(o => o.O_Id.Equals(requestId) && o.OI_CreatedBy.Equals(requestorId)); foreach (var oi in orderItems) { if (oi == null) { continue; } var iAlloc = OimsDataContext.Allocateds.FirstOrDefault(a => a.UserId.Equals(requestorId) && a.OrderId.Equals(requestId) && a.ItemId.Equals(oi.I_Id)); // If item already in allocated for same request if (iAlloc != null) { var itemRecom = oi.OI_ItemRecom ?? 0; // Set mark quantity in allocated var markQty = iAlloc.MarkQty; iAlloc.MarkQty = markQty + itemRecom; UpdateItemStock(iAlloc, itemRecom); } else { var alloc = new OimsDataModel.Allocated { OrderId = requestId, UserId = requestorId, ItemId = oi.I_Id, MarkQty = oi.OI_ItemRecom != null ? (float)oi.OI_ItemRecom : 0 }; OimsDataContext.Add(alloc); UpdateItemStock(alloc); } } #endregion #region 2. If shelter (op) in request, save products in table 'allocated' using 'orderproduct' var orderProducts = OimsDataContext.Sheltersrequests.Where(o => o.O_Id.Equals(requestId) && o.OP_CreatedBy.Equals(requestorId)); foreach (var op in orderProducts) { if (op == null) { continue; } // Get respective item's var pId = op.P_Id; var prodItems = ShelterRepository.ShelterItems.Where(p => p.P_Id.Equals(pId)); foreach (var pi in prodItems) { var pAlloc = OimsDataContext.Allocateds.FirstOrDefault(a => a.UserId.Equals(requestorId) && a.OrderId.Equals(requestId) && a.ItemId.Equals(pi.I_Id)); if (pAlloc == null) { if (op.OP_ProdRecom == null) { continue; } var newAlloc = new OimsDataModel.Allocated { OrderId = requestId, UserId = requestorId, ItemId = pi.I_Id, MarkQty = pi.I_Qty * (int)op.OP_ProdRecom }; OimsDataContext.Add(newAlloc); UpdateItemStock(newAlloc); } else { // If re-recommending, adjust the stock taking previous one // and later one in context var prodRecom = op.OP_ProdRecom ?? 0; // Set mark quantity in allocated var markQty = pAlloc.MarkQty; prodRecom = pi.I_Qty * prodRecom; pAlloc.MarkQty = markQty + prodRecom; UpdateItemStock(pAlloc, prodRecom); } } } #endregion #region 3. Save status for current request var order = OimsDataContext.Requests.FirstOrDefault(o => o.O_Id.Equals(requestId)); if (order == null) { return; } order.O_Status = statusId; order.O_UpdatedBy = updatorId; order.O_UpdatedDate = DateTime.Now; OimsDataContext.FlushChanges(); #endregion // 4. Finally save all changes OimsDataContext.SaveChanges(); }