public void AddReceivable(ref dtoReceivable t) { var receivable = new tbl_receivable() { referenceNumber = t.referenceNumber, cashAmount = t.chequeAmount, chequeNumber = t.chequeNumber, chequeDate = t.chequeDate, chequeBank = t.chequeBank, dateCreated = DateTime.Now, createdBy = t.createdBy, clientId = t.clientId }; DBContext.tbl_receivable.Add(receivable); DBContext.SaveChanges(); t.receivableId = receivable.receivableId; }
public void SaveTransaction(dtoReceivable header, List<dtoReceivableDetail> details) { if (header.receivableId == 0) { tbl_receivable entiry = new tbl_receivable() { receivableId = header.receivableId, clientId = header.clientId, isNewPayment = header.isNewPayment, referenceNumber = header.referenceNumber, isCash = header.isCash, cashAmount = header.cashAmount, chequeNumber = header.chequeNumber, chequeDate = header.chequeDate, chequeBank = header.chequeBank, dateCreated = DateTime.Now }; DBContext.tbl_receivable.Add(entiry); DBContext.SaveChanges(); header.receivableId = entiry.receivableId; } else { var entity = DBContext.tbl_receivable.FirstOrDefault(d => d.receivableId == header.receivableId); entity.clientId = header.clientId; entity.isNewPayment = header.isNewPayment; entity.referenceNumber = header.referenceNumber; entity.isCash = header.isCash; entity.cashAmount = header.cashAmount; entity.chequeNumber = header.chequeNumber; entity.chequeDate = header.chequeDate; entity.chequeBank = header.chequeBank; entity.dateLastModified = DateTime.Now; var arr = details.Where(c=>c.receivableDetailsId != 0).Select(c => c.receivableDetailsId).ToArray<int>(); var deleteList = DBContext.tbl_receivableDetails.Where(d => !arr.Contains(d.receivableDetailsId) && d.receivableId == header.receivableId); foreach (var item in deleteList) { DBContext.tbl_receivableDetails.Remove(item); } } foreach (var item in details.Where(d => d.receivableDetailsId == 0)) { DBContext.tbl_receivableDetails.Add(new tbl_receivableDetails { documentId = item.documentId, receivableId = header.receivableId, paymentPrice = item.paymentPrice, dateCreated = DateTime.Now, }); } DBContext.SaveChanges(); }