public BusinessObject.ExpenseTransaction UpdateTransactionItemAndTotalAmount(BusinessObject.TransactionItem transactionItem) { try { //Re-Map Object to Entity Object var myEntityObj = TransactionItemMapper.Map <BusinessObject.TransactionItem, TransactionItem>(transactionItem); if (myEntityObj == null) { return(new BusinessObject.ExpenseTransaction()); } using (var db = new ExpenseManagerDBEntities()) { db.TransactionItems.Attach(myEntityObj); db.ObjectStateManager.ChangeObjectState(myEntityObj, EntityState.Modified); db.SaveChanges(); var transactionAmount = db.TransactionItems.Where(m => m.ExpenseTransactionId == myEntityObj.ExpenseTransactionId).Sum(m => m.RequestedQuantity * m.RequestedUnitPrice); var transaction = db.ExpenseTransactions.Single(m => m.ExpenseTransactionId == myEntityObj.ExpenseTransactionId); transaction.TotalTransactionAmount = transactionAmount; if (transaction.Status == 2) { transaction.Status = 0; } db.ObjectStateManager.ChangeObjectState(myEntityObj, EntityState.Modified); db.SaveChanges(); var myBusinessObj = ExpenseTransactionMapper.Map <ExpenseTransaction, BusinessObject.ExpenseTransaction>(transaction); return(myBusinessObj); } } catch (Exception ex) { ErrorManager.LogApplicationError(ex.StackTrace, ex.Source, ex.Message); return(new BusinessObject.ExpenseTransaction()); } }
public int AddTransactionItem(BusinessObject.TransactionItem transactionItem) { try { //Re-Map Object to Entity Object var myEntityObj = TransactionItemMapper.Map <xPlug.BusinessObject.TransactionItem, TransactionItem>(transactionItem); if (myEntityObj == null) { return(-2); } using (var db = new ExpenseManagerDBEntities()) { db.AddToTransactionItems(myEntityObj); db.SaveChanges(); transactionItem.TransactionItemId = myEntityObj.TransactionItemId; return(transactionItem.TransactionItemId); } } catch (Exception ex) { ErrorManager.LogApplicationError(ex.StackTrace, ex.Source, ex.Message); return(0); } }
public bool UpdatePendingTransactionAndItem(BusinessObject.ExpenseTransaction transaction, BusinessObject.TransactionItem updatedTransactionItem) { try { //Re-Map Object to Entity Object using (var db = new ExpenseManagerDBEntities()) { var myEntityObj = TransactionItemMapper.Map <BusinessObject.TransactionItem, TransactionItem>(updatedTransactionItem); if (myEntityObj == null) { return(false); } db.TransactionItems.Attach(myEntityObj); db.ObjectStateManager.ChangeObjectState(myEntityObj, EntityState.Modified); db.SaveChanges(); var totalPrice = db.TransactionItems.Where(x => x.ExpenseTransactionId == transaction.ExpenseTransactionId).ToList().Sum(m => m.RequestedQuantity * m.RequestedUnitPrice); transaction.TotalTransactionAmount = totalPrice; var transactionEntityToUpdate = ExpenseTransactionMapper.Map <BusinessObject.ExpenseTransaction, ExpenseTransaction>(transaction); db.ExpenseTransactions.Attach(transactionEntityToUpdate); db.ObjectStateManager.ChangeObjectState(transactionEntityToUpdate, EntityState.Modified); db.SaveChanges(); } return(true); } catch (Exception ex) { ErrorManager.LogApplicationError(ex.StackTrace, ex.Source, ex.Message); return(false); } }