public void Save(PurchaseOrder po) { bool poExist = context.PurchaseOrders.Count(p => p.No == po.No) > 0; int id; if (!poExist) { po.ShipToCompany = null; po.Vendor = null; po.No = GeneratePoNumber(po.Date); context.PurchaseOrders.Add(po); context.SaveChanges(); id = po.Id; foreach (ItemPriceLogs poItem in po.ItemPriceLogs.ToList()) { poItem.PurchaseOrderId = id; InsertStockCard(id, poItem.ItemId, poItem.UnitCost, poItem.Qty); } } else { PurchaseOrder dbEntry = context.PurchaseOrders.FirstOrDefault(p => p.No == po.No); if (dbEntry != null) { dbEntry.No = po.No; dbEntry.Date = po.Date; dbEntry.VendorId = po.VendorId; dbEntry.ShipTo = po.ShipTo; dbEntry.Terms = vendorRepo.GetById(po.VendorId).Terms; dbEntry.PreparedBy = Common.GetCurrentUser.Id; dbEntry.ApprovedBy = Common.GetCurrentUser.GeneralManagerId; dbEntry.CheckedBy = po.CheckedBy; } id = dbEntry.Id; //Delete previous items foreach (PurchaseOrderItem poItem in dbEntry.PurchaseOrderItems.ToList()) { context.PurchaseOrderItems.Remove(poItem); } foreach (PurchaseOrderItem poItem in po.PurchaseOrderItems.ToList()) { poItem.PurchaseOrderId = id; context.PurchaseOrderItems.Add(poItem); } //Delete previous items foreach (ItemPriceLogs poItem in dbEntry.ItemPriceLogs.ToList()) { StockCard sc = context.StockCards.FirstOrDefault(p => p.ReferenceId == poItem.PurchaseOrderId); context.StockCards.Remove(sc); context.ItemPriceLogs.Remove(poItem); } foreach (ItemPriceLogs poItem in po.ItemPriceLogs.ToList()) { poItem.PurchaseOrderId = id; InsertStockCard(id, poItem.ItemId, poItem.UnitCost, poItem.Qty); context.ItemPriceLogs.Add(poItem); } } context.SaveChanges(); }
public PurchaseOrder GetByNo(string poNo) { PurchaseOrder po = context.PurchaseOrders.FirstOrDefault(p => p.No == poNo); return(po); }
public PurchaseOrder GetById(int poId) { PurchaseOrder po = context.PurchaseOrders.FirstOrDefault(p => p.Id == poId); return(po); }
public PurchaseOrder Find(int poId) { PurchaseOrder po = List().FirstOrDefault(p => p.Id == poId); return(po); }