public bool DeleteReceivingDetail(Session session, int detailID) { ReceivingDetail detail = session.GetObjectByKey <ReceivingDetail>(detailID); int rowsAffected = 0; int itemID = 0; int quantity = 0; int locationID = 0; int units = 0; int? lpn = null; string lot = null; if (detail != null) { itemID = detail.ReceivDetItemID.ItemID; units = detail.intUnits; quantity = detail.ReceivDetQty; locationID = session.GetObjectByKey <Receiving>(detail.ReceivMainID.ReceivID).ReceivingLocation.Oid; lpn = detail.ReceivDetLPN; lot = detail.ReceivDetLot; session.Delete(detail); } ItemsBLL items = new ItemsBLL(); items.UpdateStock(session, itemID, quantity * -1, false, locationID, lot, lpn); return(true); }
private ReceivingDetail getEntityByModel(ReceivingDetailModel model) { if (model == null) { return(null); } ReceivingDetail entity = new ReceivingDetail(); if (model.Id == 0) { entity.CreateBy = AuthenticationHelper.UserId; entity.CreateDate = DateTime.Now; } else { entity.CreateBy = model.CreateBy; entity.CreateDate = model.CreateDate; } entity.Id = model.Id; entity.ItemId = model.ItemId; entity.LocatorId = model.LocatorId; entity.LotNoId = model.LotNoId; entity.Quantity = model.ThisPurchaseQty; entity.ReceiptId = model.ReceiptId; entity.SerialNo = model.SerialNo; entity.UpdateBy = model.UpdateBy; entity.UpdateDate = model.UpdateDate; entity.WarehouseId = model.WarehouseId; entity.PODetailId = model.PODetailId; return(entity); }
private void UpdateAuditTrail(ReceivingDetail ModifiedRecord, ReceivingDetail originalRecord) { //Dim builder As New StringBuilder(String.Empty) //Dim recordFields As Integer = (ModifiedRecord.ItemArray.Length - 1) //Dim i As Integer = 0 //Do While (i <= recordFields) // Try // If IsDBNull(originalRecord(i)) Then // If Not IsDBNull(ModifiedRecord.Item(i)) Then // builder.Append(String.Format("{0}:{1}({2}); ", ModifiedRecord.Table.Columns.Item(i).ColumnName, "NULL", ModifiedRecord.Item(i))) // End If // ElseIf IsDBNull(ModifiedRecord.Item(i)) Then // If Not IsDBNull(originalRecord(i)) Then // builder.Append(String.Format("{0}:{1}({2}); ", ModifiedRecord.Table.Columns.Item(i).ColumnName, originalRecord(i), "NULL")) // End If // ElseIf CStr(ModifiedRecord.Item(i)) <> CStr(originalRecord(i)) Then // builder.Append(String.Format("{0}:{1}({2}); ", ModifiedRecord.Table.Columns.Item(i).ColumnName, originalRecord(i), ModifiedRecord.Item(i))) // End If // Catch // End Try // i += 1 //Loop //If (builder.Length > 2) Then // builder.Length = (builder.Length - 2) // AuditTrailBLL.AddTrailEntry(My.Settings.UserName, CType(ModifiedRecord.Item(0), Integer), "ReceivingDetails", builder.ToString) //End If }
public string Update(ReceivingDetail entity) { var originalEntity = this.Context.ReceivingDetails.Find(entity.Id); this.Context.Entry(originalEntity).CurrentValues.SetValues(entity); this.Context.Entry(originalEntity).State = EntityState.Modified; this.Commit(); return(entity.Id.ToString()); }
public ReceivingDetailModel(ReceivingDetail entity) { if (entity != null) { this.CreateBy = entity.CreateBy; this.CreateDate = entity.CreateDate; this.Id = entity.Id; this.ItemId = entity.ItemId; this.LocatorId = entity.LocatorId; this.LotNoId = entity.LotNoId; this.ThisPurchaseQty = entity.Quantity; this.ReceiptId = entity.ReceiptId; this.SerialNo = entity.SerialNo; this.UpdateBy = entity.UpdateBy; this.UpdateDate = entity.UpdateDate; this.WarehouseId = entity.WarehouseId; this.PODetailId = entity.PODetailId; } }
public static List <ReceivingDetail> GetReceivingDetail(int rrid) { var dbUtil = new DatabaseManager(); var receivingDetails = new List <ReceivingDetail>(); using (var conn = new SqlConnection(dbUtil.getSQLConnectionString("MainDB"))) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = "spPurGetReceivingDetail"; cmd.CommandTimeout = 180; cmd.Parameters.Clear(); cmd.Parameters.AddWithValue("@intRRID", rrid); using (SqlDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { var receivingDetail = new ReceivingDetail { RRDetailID = ReferenceEquals(reader["intRRDetailID"], DBNull.Value) ? 0 : (int)reader["intRRDetailID"], RRID = ReferenceEquals(reader["intRRID"], DBNull.Value) ? 0 : (int)reader["intRRID"], ProductID = ReferenceEquals(reader["intProductID"], DBNull.Value) ? 0 : (int)reader["intProductID"], ProductCode = ReferenceEquals(reader["strProductCode"], DBNull.Value) ? String.Empty : (string)reader["strProductCode"], ProductDescription = ReferenceEquals(reader["strProductDescription"], DBNull.Value) ? String.Empty : (string)reader["strProductDescription"], Quantity = ReferenceEquals(reader["intQuantity"], DBNull.Value) ? 0 : (int)reader["intQuantity"], Price = ReferenceEquals(reader["curPrice"], DBNull.Value) ? 0 : (decimal)reader["curPrice"], Amount = ReferenceEquals(reader["curAmount"], DBNull.Value) ? 0 : (decimal)reader["curAmount"] }; receivingDetails.Add(receivingDetail); } return(receivingDetails); } } } }
public int InsertDetails(Session session, int receivingID, Items item, string lot, int?quantity, int?units, int?LPN, DateTime?expirationDate) { ReceivingDetail detail = new ReceivingDetail(session); detail.ReceivMainID = session.GetObjectByKey <Receiving>(receivingID); detail.ReceivDetItemID = item; detail.intUnits = units.Value; detail.ReceivDetQty = quantity.Value; detail.ExpirationDate = expirationDate; detail.ReceivDetLPN = LPN; detail.ReceivDetLot = lot; detail.Save(); ItemsBLL items = new ItemsBLL(); int locationID = session.GetObjectByKey <Receiving>(receivingID).ReceivingLocation.Oid; items.UpdateStock(session, item.ItemID, quantity.Value, false, locationID, lot, LPN, expirationDate); return(detail.ReceivDetID); }
private string deleteSerials(ReceivingDetailModel model) { ReceivingDetail savedDetail = service.GetSingleReceivingDetail(model.Id); if (!string.IsNullOrEmpty(savedDetail.SerialNo)) { List <string> savedSerials = savedDetail.SerialNo.Split(new char[] { ',' }).ToList(); bool isAllowed = true; List <SerialNumber> tobeDeleted = new List <SerialNumber>(); foreach (var serial in savedSerials) { SerialNumber currentSerial = lotService.GetSerialNo(serial, savedDetail.LotNoId.Value, AuthenticationHelper.CompanyId.Value, SessionHelper.SOBId); if (currentSerial != null) { isAllowed = lotService.CheckSerialNumAvailability(AuthenticationHelper.CompanyId.Value, savedDetail.LotNoId.Value, serial); if (!isAllowed) { return("Record can not be deleted!"); } else { tobeDeleted.Add(lotService.GetSerialNo(serial, savedDetail.LotNoId.Value, AuthenticationHelper.CompanyId.Value, SessionHelper.SOBId)); } } } if (isAllowed) { if (tobeDeleted.Count() > 0) { foreach (var item in tobeDeleted) { lotService.DeleteSerialNum(item.Id.ToString(), AuthenticationHelper.CompanyId.Value); } } } } return(""); }
private string updateLot(ReceivingDetailModel model) { if (!string.IsNullOrEmpty(model.LotNo)) { if (model.Id > 0) { ReceivingDetail savedDetail = service.GetSingleReceivingDetail(model.Id); LotNumber savedLot = lotService.GetSingle(model.LotNoId.ToString(), AuthenticationHelper.CompanyId.Value); if (savedLot.LotNo == model.LotNo) { savedLot.Qty = savedLot.Qty - savedDetail.Quantity + model.ThisPurchaseQty; return(lotService.Update(savedLot)); } else { List <LotNumber> savedLots = lotService.GetAllbyLotNo(AuthenticationHelper.CompanyId.Value, SessionHelper.SOBId, savedLot.LotNo, savedLot.ItemId).ToList(); if (savedLots.Count() > 1) { return("Lot can not be edited!"); } else { savedLot.Qty = savedLot.Qty - savedDetail.Quantity + model.ThisPurchaseQty; savedLot.LotNo = model.LotNo; return(lotService.Update(savedLot)); } } } else { LotNumber savedLot = lotService.GetLotbyItem(AuthenticationHelper.CompanyId.Value, SessionHelper.SOBId, model.ItemId, model.LotNo); if (savedLot != null) { savedLot.Qty = savedLot.Qty + model.ThisPurchaseQty; return(lotService.Update(savedLot)); } else { return(lotService.Insert(new LotNumber { CompanyId = AuthenticationHelper.CompanyId.Value, CreateBy = AuthenticationHelper.UserId, CreateDate = DateTime.Now, ItemId = model.ItemId, LotNo = model.LotNo, Qty = model.ThisPurchaseQty, SOBId = SessionHelper.SOBId, SourceId = 0, SourceType = "Receiving", UpdateBy = null, UpdateDate = null })); } } } else { if (model.Id > 0) { ReceivingDetail savedDetail = service.GetSingleReceivingDetail(model.Id); LotNumber lot = lotService.GetSingle(savedDetail.LotNoId.Value.ToString(), AuthenticationHelper.CompanyId.Value); List <LotNumber> savedLots = lotService.GetAllbyLotNo(AuthenticationHelper.CompanyId.Value, SessionHelper.SOBId, lot.LotNo, lot.ItemId).ToList(); if (savedDetail.LotNoId != null) { if (savedLots.Count() > 1) { return("Lot can not be deleted!"); } else { lotService.Delete(savedDetail.LotNoId.Value.ToString(), AuthenticationHelper.CompanyId.Value); } } return(""); } return(""); } }
private string updateSerials(ReceivingDetailModel model) { if (!string.IsNullOrEmpty(model.SerialNo)) { List <string> newSerials = model.SerialNo.Trim().Split(new char[] { ',' }).ToList(); if (newSerials.Count() != model.ThisPurchaseQty) { return("Serials must be according to the Quantity"); } } if (model.Id > 0) { ReceivingDetail savedDetail = service.GetSingleReceivingDetail(model.Id); if (!string.IsNullOrEmpty(model.SerialNo)) { List <string> unsavedSerials = model.SerialNo.Trim().Split(new char[] { ',' }).ToList(); if (!string.IsNullOrEmpty(savedDetail.SerialNo)) { List <string> savedSerials = savedDetail.SerialNo.Split(new char[] { ',' }).ToList(); bool isAvailable = true; foreach (var serial in savedSerials) { isAvailable = lotService.CheckSerialNumAvailability(AuthenticationHelper.CompanyId.Value, savedDetail.LotNoId.Value, serial); if (!isAvailable) { return("Serial is in use!"); } } if (isAvailable) { if (savedSerials.Count() > unsavedSerials.Count()) { List <string> tobeDeleted = savedSerials.Take(savedSerials.Count() - unsavedSerials.Count()).ToList(); if (tobeDeleted != null && tobeDeleted.Count() > 0) { foreach (var item in tobeDeleted) { SerialNumber serialNum = lotService.GetSerialNo(item, savedDetail.LotNoId.Value, AuthenticationHelper.CompanyId.Value, SessionHelper.SOBId); if (serialNum != null) { lotService.DeleteSerialNum(serialNum.Id.ToString(), AuthenticationHelper.CompanyId.Value); } } } } foreach (var serial in unsavedSerials) { SerialNumber entity = lotService.GetSerialNo(serial, savedDetail.LotNoId.Value, AuthenticationHelper.CompanyId.Value, SessionHelper.SOBId); if (entity != null) { entity.LotNo = model.LotNo; entity.SerialNo = serial; lotService.UpdateSerialNum(entity); } else { lotService.InsertSerialNum(new SerialNumber { CompanyId = AuthenticationHelper.CompanyId.Value, CreateBy = AuthenticationHelper.UserId, CreateDate = DateTime.Now, LotNo = model.LotNo, LotNoId = savedDetail.LotNoId.Value, SerialNo = serial, UpdateBy = null, UpdateDate = null }); } } } } else { foreach (var serial in unsavedSerials) { lotService.InsertSerialNum(new SerialNumber { CompanyId = AuthenticationHelper.CompanyId.Value, CreateBy = AuthenticationHelper.UserId, CreateDate = DateTime.Now, LotNo = model.LotNo, LotNoId = savedDetail.Id, SerialNo = serial, UpdateBy = null, UpdateDate = null }); } } } } else { if (!string.IsNullOrEmpty(model.SerialNo)) { if (!string.IsNullOrEmpty(model.LotNo)) { LotNumber lot = lotService.GetLotbyItem(AuthenticationHelper.CompanyId.Value, SessionHelper.SOBId, model.ItemId, model.LotNo); if (lot != null) { List <string> serials = model.SerialNo.Trim().Split(new char[] { ',' }).ToList(); bool notAvailable = true; foreach (var serial in serials) { notAvailable = lotService.CheckSerialNumAvailability(AuthenticationHelper.CompanyId.Value, lot.Id, serial); if (notAvailable) { return("Serial # " + serial + " is already defined"); } } if (!notAvailable) { foreach (var serial in serials) { lotService.InsertSerialNum(new SerialNumber { CompanyId = AuthenticationHelper.CompanyId.Value, CreateBy = AuthenticationHelper.UserId, CreateDate = DateTime.Now, LotNo = lot.LotNo, LotNoId = lot.Id, SerialNo = serial, UpdateBy = null, UpdateDate = null }); } } } else { return("Lot not found!"); } } else { return("Serials can not be defined without lot!"); } } } return(""); }
public string Insert(ReceivingDetail entity) { this.Context.ReceivingDetails.Add(entity); this.Commit(); return(entity.Id.ToString()); }
public string Update(ReceivingDetail entity) { return(this.repository.Update(entity)); }
public string Insert(ReceivingDetail entity) { return(this.repository.Insert(entity)); }
public int UpdateReceivingDetails(Session session, int?detailID, int receivingID, int?itemID, string lot, int?quantity, int?units, int?LPN, DateTime?expirationDate) { if (!itemID.HasValue) { throw new ApplicationException("You must provide receiving item."); } if (!quantity.HasValue || !units.HasValue) { throw new ApplicationException("You must provide the amount of quamtity\\units received."); } if (LPN == null) { throw new ApplicationException("LPN is blank."); } Items item = session.GetObjectByKey <Items>(itemID.Value, true); if (!LotCodeValidator.ValidateByItem(item, lot, true)) { throw new ApplicationException("Item " + item.ItemCode + " & lot # " + lot + " is invalid" + Environment.NewLine + "You must provide a valid lot."); } ReceivingDetail detail = session.GetObjectByKey <ReceivingDetail>(detailID); if (detail == null) { //It is a new Detail return(InsertDetails(session, receivingID, item, lot, quantity, units, LPN, expirationDate)); } bool itemChanged = false; ReceivingDetail originalDetail = null; Cloner cloner = new Cloner(); originalDetail = (ReceivingDetail)cloner.CloneTo(detail, typeof(ReceivingDetail)); if (itemID.HasValue && originalDetail.ReceivDetItemID.ItemID != itemID) { itemChanged = true; } detail.ReceivDetItemID = item; detail.intUnits = units.Value; detail.ReceivDetQty = quantity.Value; detail.ExpirationDate = expirationDate; detail.ReceivDetLPN = LPN; detail.ReceivDetLot = lot; if (!(originalDetail == null)) { UpdateAuditTrail(detail, originalDetail); } session.Save(detail); if (!detail.Equals(originalDetail)) { ItemsBLL items = new ItemsBLL(); int locationID = session.GetObjectByKey <Receiving>(receivingID).ReceivingLocation.Oid; items.UpdateStock(session, originalDetail.ReceivDetItemID.ItemID, null, originalDetail.ReceivDetQty * -1, false, locationID, originalDetail.ReceivDetLot, LPN); items.UpdateStock(session, itemID.Value, quantity.Value, false, locationID, lot, LPN, expirationDate); } return(detail.ReceivDetID); }
private string save(ReceivingModel model) { Receiving entity = getEntityByModel(model); if (model.Id > 0) { List <ReceivingDetailModel> receivingDetail = service.GetAllReceivingDetail(model.Id).Select(rec => new ReceivingDetailModel(rec, true)).ToList(); if (receivingDetail != null && receivingDetail.Count() > 0) { foreach (var item in receivingDetail) { if (!model.ReceivingDetail.Any(rec => rec.PODetailId == item.PODetailId)) { string serialResult = deleteSerials(item); if (string.IsNullOrEmpty(serialResult)) { service.DeleteReceivingDetail(item.Id); deleteLot(item); } else { return(serialResult); } } } } } List <ReceivingDetailModel> tobeUpdatedDetail = model.ReceivingDetail.Where(rec => rec.LocatorId > 0 && rec.WarehouseId > 0).ToList(); string result = string.Empty; if (entity.IsValid()) { bool goodToSave = false; foreach (var item in tobeUpdatedDetail) { ReceivingDetailModel updatedModel = item; string lotResult = updateLot(updatedModel); int outVal; bool isNumeric = int.TryParse(lotResult, out outVal); if (isNumeric || string.IsNullOrEmpty(lotResult)) { item.LotNoId = isNumeric ? (long?)Convert.ToInt64(lotResult) : null; string serialResult = updateSerials(updatedModel); if (string.IsNullOrEmpty(serialResult)) { goodToSave = true; } else { if (item.LotNoId != null) { LotNumber lot = lotService.GetSingle(item.LotNoId.Value.ToString(), AuthenticationHelper.CompanyId.Value); lot.Qty = lot.Qty - item.ThisPurchaseQty; lotService.Update(lot); } return(serialResult); } } else { return(lotResult); } } if (goodToSave) { if (model.Id > 0) { result = service.Update(entity); } else { result = service.Insert(entity); } if (!string.IsNullOrEmpty(result)) { var savedLines = getReceivingDetail(result); if (savedLines.Count() > tobeUpdatedDetail.Count()) { var tobeDeleted = savedLines.Take(savedLines.Count() - tobeUpdatedDetail.Count()); foreach (var item in tobeDeleted) { string serialResult = deleteSerials(item); if (string.IsNullOrEmpty(serialResult)) { string lotResult = deleteLot(item); if (string.IsNullOrEmpty(lotResult)) { service.DeleteReceivingDetail(item.Id); } else { return("Record can not be deleted"); } } else { return("Record can not be deleted"); } } savedLines = getReceivingDetail(result); } foreach (var detail in tobeUpdatedDetail) { ReceivingDetail detailEntity = getEntityByModel(detail); if (detailEntity.IsValid()) { detailEntity.ReceiptId = Convert.ToInt64(result); if (savedLines.Count() > 0) { detailEntity.Id = savedLines.FirstOrDefault().Id; savedLines.Remove(savedLines.FirstOrDefault(rec => rec.Id == detailEntity.Id)); string receivingDetailId = service.Update(detailEntity); if (detailEntity.LotNoId != null) { LotNumber lottobeUpdated = lotService.GetSingle(detailEntity.LotNoId.ToString(), AuthenticationHelper.CompanyId.Value); lottobeUpdated.SourceId = Convert.ToInt64(receivingDetailId); lotService.Update(lottobeUpdated); } } else { string receivingDetailId = service.Insert(detailEntity); if (detailEntity.LotNoId != null) { LotNumber lottobeUpdated = lotService.GetSingle(detailEntity.LotNoId.ToString(), AuthenticationHelper.CompanyId.Value); lottobeUpdated.SourceId = Convert.ToInt64(receivingDetailId); lotService.Update(lottobeUpdated); } } } } } } } return(""); }
public string AddOrUpdateReceiving(int id, int poID, DateTime date, string goodIssueNo, string freightInfo, int warehouseID, string notes, List <ReceivingDetailViewModel> detail) { var receivingDetails = context.ReceivingHeaders .Where(rcv => rcv.PurchaseOrderID == poID && rcv.ID != id && !rcv.VoidDate.HasValue) .SelectMany(x => x.ReceivingDetails) .ToList(); var order = context.PurchaseOrderHeaders.Single(po => po.ID == poID && !po.VoidWhen.HasValue); var orderDetails = order.PurchaseOrderDetails.ToList(); var receiveHeader = id == 0 ? new ReceivingHeader() : context.ReceivingHeaders.Single(rh => rh.ID == id); if (id == 0) { receiveHeader.DocumentNo = autoNumberProvider.Generate(order.BranchID, "RC", date.Month, date.Year); } receiveHeader.PurchaseOrderID = poID; receiveHeader.Date = date; receiveHeader.GoodIssueNo = goodIssueNo; receiveHeader.FreightInfo = freightInfo; receiveHeader.WarehouseID = warehouseID; receiveHeader.Notes = notes; receiveHeader.Status = 'O'; EntityHelper.SetAuditField(id, receiveHeader, CurrentUserName); context.Delete(receiveHeader.ReceivingDetails.ToList()); bool isFullReceived = true; foreach (var newReceivingDetail in detail) { var totalQtyOrder = orderDetails.Where(po => po.ItemID == newReceivingDetail.ItemID).Sum(po => po.Quantity); var totalQtyRcv = receivingDetails.Where(rcv => rcv.ItemID == newReceivingDetail.ItemID).Sum(rcv => rcv.QtyReceived); if (totalQtyOrder - totalQtyRcv - newReceivingDetail.QtyReceived < 0) { throw new Exception(String.Format("Qty for item {0} exceeds Qty in Purchase Order. Item:{1}, QtyPO:{2}, QtyRcv:{3}", newReceivingDetail.ItemCode, newReceivingDetail.ItemCode, Convert.ToInt32(totalQtyOrder), totalQtyRcv + newReceivingDetail.QtyReceived)); } if (totalQtyOrder - totalQtyRcv - newReceivingDetail.QtyReceived > 0) { isFullReceived = false; } var receiveDetail = new ReceivingDetail { ItemID = newReceivingDetail.ItemID, UnitName = newReceivingDetail.UnitName, QtyReceived = newReceivingDetail.QtyReceived, Notes = newReceivingDetail.Notes, ReceivingHeader = receiveHeader }; receiveHeader.ReceivingDetails.Add(receiveDetail); } if (id == 0) { context.Add(receiveHeader); autoNumberProvider.Increment("RC", order.BranchID, date.Year); } order.IsFullReceived = isFullReceived; order.StatusReceiving = isFullReceived ? 'F' : 'P'; context.SaveChanges(); return(receiveHeader.DocumentNo); }
public string AddOrUpdateReceiving(int id, int poID, DateTime date, string goodIssueNo, string freightInfo, string notes, List <ReceivingDetailViewModel> detail) { List <ReceivingDetail> receivingDetails = context.ReceivingHeaders .Where(rcv => rcv.PurchaseOrderID == poID && rcv.ID != id && !rcv.VoidDate.HasValue) .SelectMany(x => x.ReceivingDetails) .ToList(); var order = context.PurchaseOrderHeaders.Single(po => po.ID == poID && !po.VoidDate.HasValue); List <PurchaseOrderDetail> orderDetails = order.PurchaseOrderDetails.ToList(); var receiveHeader = id == 0 ? new ReceivingHeader() : context.ReceivingHeaders.Single(rh => rh.ID == id); var autoNo = new AutoNumberProvider(context, principal); var emp = new EmployeeProvider(context, principal); if (id == 0) { receiveHeader.DocumentNo = autoNo.GenerateReceivingRunningNumber(CurrentCompanyCode, date); } receiveHeader.PurchaseOrderID = poID; receiveHeader.Date = date; receiveHeader.GoodIssueNo = goodIssueNo; receiveHeader.FreightInfo = freightInfo; receiveHeader.Notes = notes; receiveHeader.Status = "O"; EntityHelper.SetAuditFields(id, receiveHeader, CurrentUserName); context.ReceivingDetails.DeleteAllOnSubmit(receiveHeader.ReceivingDetails); bool isFullReceived = true; foreach (var newReceivingDetail in detail) { var totalQtyPO = orderDetails.Where(po => po.ItemID == newReceivingDetail.ItemID).Sum(po => po.Quantity); var totalQtyRcv = receivingDetails.Where(rcv => rcv.ItemID == newReceivingDetail.ItemID).Sum(rcv => rcv.QtyReceived); if (totalQtyPO - totalQtyRcv - newReceivingDetail.QtyReceived < 0) { throw new Exception(String.Format("Qty for item {0} exceeds Qty in Purchase Order. Item:{1}, QtyPO:{2}, QtyRcv:{3}", newReceivingDetail.ItemCode, newReceivingDetail.ItemCode, Convert.ToInt32(totalQtyPO), totalQtyRcv + newReceivingDetail.QtyReceived)); } else if (totalQtyPO - totalQtyRcv - newReceivingDetail.QtyReceived > 0) { isFullReceived = false; } var receiveDetail = new ReceivingDetail { ItemID = newReceivingDetail.ItemID, UnitName = newReceivingDetail.UnitName, QtyReceived = newReceivingDetail.QtyReceived, Notes = newReceivingDetail.Notes, ReceivingHeader = receiveHeader }; context.ReceivingDetails.InsertOnSubmit(receiveDetail); } if (id == 0) { context.ReceivingHeaders.InsertOnSubmit(receiveHeader); } order.IsFullReceived = isFullReceived; order.StatusReceiving = isFullReceived ? "F" : "P"; //var receiveDetails = new List<ReceivingDetailViewModel>(); //foreach (var item in query) // receiveDetails.Add(new ReceivingDetailViewModel { ItemID = item.ItemID, QtyReceived = item.QtyReceived }); //foreach (var lineDetail in detail) // receiveDetails.Add(lineDetail); //var sumReceiveUnit = detail.GroupBy(x => x.ItemID).Select(y => new { item = y.Key, qty = receiveDetails.Where(x => x.ItemID == y.Key).Sum(x => x.QtyReceived) }); //var sumPOUnit = context.PurchaseOrderHeaders.Where(x => x.ID == poID).SelectMany(x => x.PurchaseOrderDetails). // GroupBy(x => x.ItemID).Select(z => new { item = z.Key, qty = z.Sum(p => p.Quantity) }); //bool isAllqtySame = true; //bool isItemCountSame = true; //foreach (var itemReceive in sumReceiveUnit) //{ // var itemPO = sumPOUnit.SingleOrDefault(x => x.item == itemReceive.item); // if (itemPO.qty != itemReceive.qty) // isAllqtySame = false; //} //if (sumReceiveUnit.Count() != sumPOUnit.Count()) // isItemCountSame = false; //PurchaseOrderHeader purchaseOrderHeader = context.PurchaseOrderHeaders.SingleOrDefault(x => x.ID == poID); //if (purchaseOrderHeader != null) //{ // if (isAllqtySame && isItemCountSame) // { // var allReceiving = context.ReceivingHeaders.Where(x => x.PurchaseOrderID == poID && x.Status == "O"); // foreach (var item in allReceiving) // { // ReceivingHeader headerReceive = context.ReceivingHeaders.SingleOrDefault(x => x.ID == item.ID); // headerReceive.Status = "F"; // EntityHelper.SetAuditFieldsForUpdate(headerReceive, CurrentUserName); // } // purchaseOrderHeader.IsFullReceived = true; // purchaseOrderHeader.StatusReceiving = "F"; // receiveHeader.Status = "F"; // EntityHelper.SetAuditFieldsForUpdate(purchaseOrderHeader, CurrentUserName); // } // else // { // purchaseOrderHeader.IsFullReceived = false; // purchaseOrderHeader.StatusReceiving = "P"; // } //} //#endregion context.SubmitChanges(); return(receiveHeader.DocumentNo); }