public int CreateBill(Bill bill, IList <BillingItem> billingItems) { try { bill.CreatedDate = DateTime.Now; Bills.Add(bill); //SaveChanges(); foreach (var item in billingItems) { //item.BillingItemId = 0; item.CreatedDate = DateTime.Now; item.Bill = bill; item.BillId = bill.BillId; BillingItems.Add(item); } SaveChanges(); return(bill.BillId); }catch (Exception ex) { throw; } return(0); }
public int UpdateBill(Bill bill, IList <BillingItem> billingItems) { try{ var oldbill = Bills.Single(b => b.BillId == bill.BillId); if (oldbill == null) { throw new Exception("Bill not available"); } BillingAudits.Add(Map(oldbill)); oldbill.OutStandingAmount = bill.OutStandingAmount; oldbill.Date = bill.Date; oldbill.UpdatedDate = bill.UpdatedDate; oldbill.ShopCustomerId = bill.ShopCustomerId; oldbill.TotalPrice = bill.TotalPrice; oldbill.TotalQuantity = bill.TotalQuantity; // SaveChanges(); foreach (var newItem in billingItems) { var oldItem = BillingItems.FirstOrDefault(bi => bi.BillingItemId == newItem.BillingItemId); var temp = BillingItems.First(); if (oldItem == null) { newItem.BillId = oldbill.BillId; newItem.CreatedDate = DateTime.Now; newItem.BillingItemId = 0; BillingItems.Add(newItem); } else { BillingItemAudits.Add(Map(oldItem)); oldItem.CGST = newItem.CGST; oldItem.SGST = newItem.SGST; oldItem.ItemId = newItem.ItemId; oldItem.OriginalPrice = newItem.OriginalPrice; oldItem.SellingPrice = newItem.SellingPrice; oldItem.Quantity = newItem.Quantity; oldItem.TotalPrice = newItem.TotalPrice; oldItem.UpdatedDate = DateTime.Now; oldItem.BillId = bill.BillId; //BillingItems.Add(oldItem); } } SaveChanges(); } catch (Exception ex) { return(0); } return(bill.BillId); }