/// <summary> /// Delete /// </summary> /// <param name="model">Account model</param> /// <returns>ResponseStatusCodeHelper</returns> public ResponseStatusCodeHelper Delete(AccountModel model) { try { using (var _context = new TDHEntities()) { MN_ACCOUNT _md = _context.MN_ACCOUNT.FirstOrDefault(m => m.id == model.ID && !m.deleted && m.create_by == model.CreateBy); if (_md == null) { throw new DataAccessException(FILE_NAME, "Delete", model.CreateBy); } _md.deleted = true; _md.delete_by = model.DeleteBy; _md.delete_date = DateTime.Now; _context.MN_ACCOUNT.Attach(_md); _context.Entry(_md).State = EntityState.Modified; _context.SaveChanges(); } } catch (DataAccessException fieldEx) { throw fieldEx; } catch (Exception ex) { throw new ServiceException(FILE_NAME, "Delete", model.CreateBy, ex); } Notifier.Notification(model.CreateBy, Message.DeleteSuccess, Notifier.TYPE.Success); return(ResponseStatusCodeHelper.Success); }
/// <summary> /// Save /// </summary> /// <param name="model">Account model</param> /// <returns>ResponseStatusCodeHelper</returns> public ResponseStatusCodeHelper Save(AccountModel model) { try { using (var _context = new TDHEntities()) { MN_ACCOUNT _md = new MN_ACCOUNT(); if (model.Insert) { _md.id = Guid.NewGuid(); } else { _md = _context.MN_ACCOUNT.FirstOrDefault(m => m.id == model.ID && !m.deleted && m.create_by == model.CreateBy); if (_md == null) { throw new DataAccessException(FILE_NAME, "Save", model.CreateBy); } } _md.account_type_id = model.AccountTypeID; _md.name = model.Name; _md.max_payment = model.MaxPayment; //Create or edit, only change the name and type if (model.Insert) { _md.create_by = model.CreateBy; _md.create_date = DateTime.Now; _context.MN_ACCOUNT.Add(_md); _context.Entry(_md).State = EntityState.Added; } else { _md.update_by = model.UpdateBy; _md.update_date = DateTime.Now; _context.MN_ACCOUNT.Attach(_md); _context.Entry(_md).State = EntityState.Modified; } _context.SaveChanges(); } } catch (DataAccessException fieldEx) { throw fieldEx; } catch (Exception ex) { throw new ServiceException(FILE_NAME, "Save", model.CreateBy, ex); } if (model.Insert) { Notifier.Notification(model.CreateBy, Message.InsertSuccess, Notifier.TYPE.Success); } else { Notifier.Notification(model.CreateBy, Message.UpdateSuccess, Notifier.TYPE.Success); } return(ResponseStatusCodeHelper.Success); }
/// <summary> /// Save payment /// </summary> /// <param name="model">Payment model</param> /// <returns>ResponseStatusCodeHelper</returns> public ResponseStatusCodeHelper SavePayment(PaymentModel model) { try { using (var _context = new TDHEntities()) { using (var trans = _context.Database.BeginTransaction()) { try { decimal _currentYearMonth = decimal.Parse(DateTime.Now.DateToString("yyyyMM")); MN_PAYMENT _md = new MN_PAYMENT(); _md.id = Guid.NewGuid(); _md.account_id = model.AccountID; _md.category_id = model.CategoryID; _md.title = model.Title; _md.date = model.Date; _md.purpose = model.Purpose; _md.notes = model.Notes; _md.money = model.Money; _md.create_by = model.CreateBy; _md.create_date = DateTime.Now; _context.MN_PAYMENT.Add(_md); _context.Entry(_md).State = EntityState.Added; _context.SaveChanges(); //MN_ACCOUNT. Update account payment money MN_ACCOUNT _acc = _context.MN_ACCOUNT.FirstOrDefault(m => m.id == _md.account_id); _acc.output += model.Money; _context.MN_ACCOUNT.Attach(_acc); _context.Entry(_acc).State = EntityState.Modified; _context.SaveChanges(); trans.Commit(); } catch (Exception ex) { trans.Rollback(); throw new ServiceException(FILE_NAME, "SavePayment", model.CreateBy, ex); } } } } catch (ServiceException serviceEx) { throw serviceEx; } catch (Exception ex) { throw new ServiceException(FILE_NAME, "SavePayment", model.CreateBy, ex); } Notifier.Notification(model.CreateBy, Message.InsertSuccess, Notifier.TYPE.Success); return(ResponseStatusCodeHelper.Success); }
/// <summary> /// Check Delete /// </summary> /// <param name="model">Account model</param> /// <returns>ResponseStatusCodeHelper</returns> public ResponseStatusCodeHelper CheckDelete(AccountModel model) { try { using (var _context = new TDHEntities()) { MN_ACCOUNT _md = _context.MN_ACCOUNT.FirstOrDefault(m => m.id == model.ID && !m.deleted && m.create_by == model.CreateBy); if (_md == null) { throw new DataAccessException(FILE_NAME, "CheckDelete", model.CreateBy); } var _accSetting = _context.MN_ACCOUNT_SETTING.FirstOrDefault(m => m.account_id == model.ID && !m.deleted); if (_accSetting != null) { Notifier.Notification(model.CreateBy, Message.CheckExists, Notifier.TYPE.Warning); return(ResponseStatusCodeHelper.NG); } var _payment = _context.MN_PAYMENT.FirstOrDefault(m => m.account_id == model.ID && !m.deleted); if (_payment != null) { Notifier.Notification(model.CreateBy, Message.CheckExists, Notifier.TYPE.Warning); return(ResponseStatusCodeHelper.NG); } var _income = _context.MN_INCOME.FirstOrDefault(m => m.account_id == model.ID && !m.deleted); if (_income != null) { Notifier.Notification(model.CreateBy, Message.CheckExists, Notifier.TYPE.Warning); return(ResponseStatusCodeHelper.NG); } var _transFrom = _context.MN_TRANSFER.FirstOrDefault(m => m.account_from == model.ID && !m.deleted); if (_transFrom != null) { Notifier.Notification(model.CreateBy, Message.CheckExists, Notifier.TYPE.Warning); return(ResponseStatusCodeHelper.NG); } var _transTo = _context.MN_TRANSFER.FirstOrDefault(m => m.account_to == model.ID && !m.deleted); if (_transTo != null) { Notifier.Notification(model.CreateBy, Message.CheckExists, Notifier.TYPE.Warning); return(ResponseStatusCodeHelper.NG); } } } catch (DataAccessException fieldEx) { throw fieldEx; } catch (Exception ex) { throw new ServiceException(FILE_NAME, "CheckDelete", model.CreateBy, ex); } return(ResponseStatusCodeHelper.OK); }
/// <summary> /// Get item /// </summary> /// <param name="model">Account model</param> /// <returns>MoneyAccountModel</returns> public AccountModel GetItemByID(AccountModel model) { try { using (var _context = new TDHEntities()) { MN_ACCOUNT _md = _context.MN_ACCOUNT.FirstOrDefault(m => m.id == model.ID && !m.deleted && m.create_by == model.CreateBy); if (_md == null) { throw new DataAccessException(FILE_NAME, "GetItemByID", model.CreateBy); } MN_ACCOUNT_TYPE _type = _context.MN_ACCOUNT_TYPE.FirstOrDefault(m => m.id == _md.account_type_id); var _return = new AccountModel() { ID = _md.id, Name = _md.name, MaxPayment = _md.max_payment, MaxPaymentString = _md.max_payment.NumberToString(), AccountType = _type.type, AccountTypeID = _md.account_type_id, AccountTypeName = _type.name, Total = _type.type == (short)AccountType.Borrow ? _md.input - _md.max_payment : _type.type == (short)AccountType.Loan ? _md.input - _md.max_payment : _md.input - _md.output }; return(_return); } } catch (DataAccessException fieldEx) { throw fieldEx; } catch (Exception ex) { throw new ServiceException(FILE_NAME, "GetItemByID", model.CreateBy, ex); } }
/// <summary> /// Save transfer /// </summary> /// <param name="model">Transfer model</param> /// <returns>ResponseStatusCodeHelper</returns> public ResponseStatusCodeHelper SaveTransfer(TransferModel model) { try { using (var _context = new TDHEntities()) { using (var trans = _context.Database.BeginTransaction()) { try { decimal _currentYearMonth = decimal.Parse(DateTime.Now.DateToString("yyyyMM")); MN_TRANSFER _md = new MN_TRANSFER(); _md.id = Guid.NewGuid(); _md.category_id = model.CategoryID; _md.account_from = model.AccountFrom; _md.account_to = model.AccountTo; _md.title = model.Title; _md.date = model.Date; _md.purpose = model.Purpose; _md.notes = model.Notes; _md.money = model.Money; _md.fee = model.Fee; _md.create_by = model.CreateBy; _md.create_date = DateTime.Now; _context.MN_TRANSFER.Add(_md); _context.Entry(_md).State = EntityState.Added; _context.SaveChanges(); //MN_ACCOUNT receive MN_ACCOUNT _accReceive = _context.MN_ACCOUNT.FirstOrDefault(m => m.id == _md.account_to); _accReceive.input += model.Money; _context.MN_ACCOUNT.Attach(_accReceive); _context.Entry(_accReceive).State = EntityState.Modified; _context.SaveChanges(); //MN_ACCOUNT spend MN_ACCOUNT _accSpend = _context.MN_ACCOUNT.FirstOrDefault(m => m.id == _md.account_from); _accSpend.input -= (model.Money + model.Fee); _context.MN_ACCOUNT.Attach(_accSpend); _context.Entry(_accSpend).State = EntityState.Modified; _context.SaveChanges(); // trans.Commit(); } catch (Exception ex) { trans.Rollback(); throw new ServiceException(FILE_NAME, "SaveTransfer", model.CreateBy, ex); } } } } catch (ServiceException serviceEx) { throw serviceEx; } catch (Exception ex) { throw new ServiceException(FILE_NAME, "SaveTransfer", model.CreateBy, ex); } Notifier.Notification(model.CreateBy, Message.InsertSuccess, Notifier.TYPE.Success); return(ResponseStatusCodeHelper.Success); }