public bool Insert(ReceiptNoteSelfMadeModels model, RecipeIngredientUsageModels _objIngredientDependent, ref string msg) { NSLog.Logger.Info("InsertSelfMade", _objIngredientDependent); using (var cxt = new NuWebContext()) { using (var transaction = cxt.Database.BeginTransaction()) { try { ResultModels resultModels = new ResultModels(); List <InventoryModels> lstInventory = new List <InventoryModels>(); var item = new I_ReceiptNoteForSeftMade(); string ReceiptNoteForSeftMadeId = Guid.NewGuid().ToString(); item.Id = ReceiptNoteForSeftMadeId; item.ReceiptNo = CommonHelper.GetGenNo(Commons.ETableZipCode.ReceiptNoteSelfMade, model.StoreId); item.Status = (int)Commons.EReceiptNoteStatus.Closed; item.CreatedBy = model.CreatedBy; item.CreatedDate = model.CreatedDate; item.UpdatedBy = model.UpdatedBy; item.UpdatedDate = model.UpdatedDate; item.StoreId = model.StoreId; item.ReceiptBy = model.ReceiptBy; item.ReceiptDate = model.ReceiptDate; cxt.I_ReceiptNoteForSeftMade.Add(item); //======== Insert Receipt Work Order List <string> ListWOId = new List <string>(); List <I_ReceiptSelfMade_Work_Order> RWOModels = new List <I_ReceiptSelfMade_Work_Order>(); foreach (var RNDetailParent in model.ListWorkOrder) { RWOModels.Add(new I_ReceiptSelfMade_Work_Order { Id = Guid.NewGuid().ToString(), WorkOrderId = RNDetailParent.Id, RNSelfMadeId = ReceiptNoteForSeftMadeId, CreatedBy = model.CreatedBy, CreatedDate = model.CreatedDate, ModifierBy = model.UpdatedBy, ModifierDate = model.UpdatedDate, IsActived = true }); ListWOId.Add(RNDetailParent.Id); //Update WO detail var childs = model.ListItem.Where(ww => ww.WOId == RNDetailParent.Id).ToList(); foreach (var RNDetailChild in childs) { var itemUpdate = (from tb in cxt.I_Work_Order_Detail where tb.Id == RNDetailChild.Id select tb).FirstOrDefault(); if (itemUpdate != null) { itemUpdate.ReceiptNoteQty += RNDetailChild.ReceivingQty; //RNDetailChild.ReturnReceiptNoteQty = itemUpdate.ReturnReceiptNoteQty.HasValue ? itemUpdate.ReturnReceiptNoteQty.Value : 0; //RNDetailChild.ReceiptNoteQty = itemUpdate.ReceiptNoteQty.HasValue ? itemUpdate.ReceiptNoteQty.Value : 0; } } } if (RWOModels != null && RWOModels.Any()) { cxt.I_ReceiptSelfMade_Work_Order.AddRange(RWOModels); } //========= Change Status WO var lstObj = cxt.I_Work_Order.Where(ww => ListWOId.Contains(ww.Id)).ToList(); if (lstObj != null && lstObj.Count > 0) { //check if approve before set in progress foreach (var purchase in lstObj) { if (purchase.Status == (int)Commons.EPOStatus.Approved) { purchase.Status = (int)Commons.EPOStatus.InProgress; } } } List <I_ReceiptNoteForSeftMadeDetail> ListInsertRND = new List <I_ReceiptNoteForSeftMadeDetail>(); I_ReceiptNoteForSeftMadeDetail detail = null; List <I_ReceiptNoteForSeftMadeDependentDetail> ListDetailDependent = new List <I_ReceiptNoteForSeftMadeDependentDetail>(); I_ReceiptNoteForSeftMadeDependentDetail detailDepen = null; foreach (var RNSelfMadeDetail in model.ListItem) { detail = new I_ReceiptNoteForSeftMadeDetail(); detail.Id = Guid.NewGuid().ToString(); detail.ReceiptNoteId = ReceiptNoteForSeftMadeId; detail.ReceivingQty = RNSelfMadeDetail.ReceivingQty; detail.IsActived = true; detail.Status = (int)Commons.EStatus.Actived; detail.IngredientId = RNSelfMadeDetail.IngredientId; detail.BaseReceivingQty = (RNSelfMadeDetail.ReceivingQty * RNSelfMadeDetail.BaseQty); detail.WOId = RNSelfMadeDetail.WOId; ListInsertRND.Add(detail); if (_objIngredientDependent.ListChilds == null) { _objIngredientDependent.ListChilds = new List <RecipeIngredientUsageModels>(); } var lstChild = _objIngredientDependent.ListChilds.Where(ww => ww.MixtureIngredientId == RNSelfMadeDetail.IngredientId).ToList(); foreach (var subitem in lstChild) { detailDepen = new I_ReceiptNoteForSeftMadeDependentDetail(); detailDepen.Id = Guid.NewGuid().ToString(); detailDepen.IngredientId = subitem.Id; detailDepen.RNSelfMadeDetailId = detail.Id; detailDepen.StockOutQty = subitem.TotalUsage; detailDepen.IsActived = true; ListDetailDependent.Add(detailDepen); _objIngredientDependent.ListChilds.Remove(subitem); } //detail dependent //ListInsertRND.Add(new I_ReceiptNoteForSeftMadeDetail //{ // Id = Guid.NewGuid().ToString(), // ReceiptNoteId = ReceiptNoteForSeftMadeId, // ReceivingQty = RNSelfMadeDetail.ReceivingQty, // IsActived = true, // Status = (int)Commons.EStatus.Actived, // IngredientId = RNSelfMadeDetail.IngredientId, // BaseReceivingQty = (RNSelfMadeDetail.ReceivingQty * RNSelfMadeDetail.BaseQty) //}); if (RNSelfMadeDetail.IsSelfMode && RNSelfMadeDetail.IsStockAble) { lstInventory.Add(new InventoryModels() { StoreId = model.StoreId, IngredientId = RNSelfMadeDetail.IngredientId, Price = 0, Quantity = (RNSelfMadeDetail.ReceivingQty * RNSelfMadeDetail.BaseQty) }); } } cxt.I_ReceiptNoteForSeftMadeDetail.AddRange(ListInsertRND); cxt.I_ReceiptNoteForSeftMadeDependentDetail.AddRange(ListDetailDependent); //============= cxt.SaveChanges(); transaction.Commit(); //Auto change statu for list PO if (model.ListWorkOrder != null && model.ListWorkOrder.Any()) { _inventoryFactory.CloseWOAuto(ListWOId); } NSLog.Logger.Info("Save RNForSeftMade", model); if (lstInventory.Count > 0) { //Update List Ingredient have Properties Self-made = True and StockAble = true _inventoryFactory.UpdateInventoryForSeftMadeReceiptNoteWhenStockAbleOn(lstInventory, _objIngredientDependent, ReceiptNoteForSeftMadeId, ref resultModels); NSLog.Logger.Info(string.Format("UpdateInventoryForSeftMadeReceiptNoteWhenStockAbleOn: [{0}] - [{1}]- [{2}]", resultModels.IsOk, ReceiptNoteForSeftMadeId, resultModels.Message)); } return(true); } catch (Exception ex) { _logger.Error(ex); transaction.Rollback(); return(false); } finally { if (cxt != null) { cxt.Dispose(); } } } } }