public async Task <IActionResult> Account([FromBody] AccountDto accountDto) { var transaction = await _context.Database.BeginTransactionAsync(); try { Receipt receipt = new Receipt() { IsPay = accountDto.IsPay, About = accountDto.About, CreatedBy = AuthoticateUserName(), ClientId = accountDto.ClinetId, Date = DateTime.Now, Amount = accountDto.Amount, Manager = accountDto.Manager, Note = accountDto.Note, }; await this._context.AddAsync(receipt); await this._context.SaveChangesAsync(); var treasuer = await _context.Treasuries.FindAsync(AuthoticateUserId()); treasuer.Total += accountDto.Amount; _context.Update(treasuer); var history = new TreasuryHistory() { Amount = accountDto.Amount, ReceiptId = receipt.Id, TreasuryId = treasuer.Id, CreatedOnUtc = DateTime.Now, }; await _context.AddAsync(history); await _context.SaveChangesAsync(); await transaction.CommitAsync(); return(Ok(receipt.Id)); } catch (Exception ex) { await transaction.RollbackAsync(); return(BadRequest()); } }
public async Task <ErrorRepsonse <TreasuryHistoryDto> > DecreaseAmount(int id, decimal amount) { var treausry = await _uintOfWork.Repository <Treasury>().GetById(id); await _uintOfWork.BegeinTransaction(); try { var cashMovment = new CashMovment() { Amount = -amount, CreatedBy = _userService.AuthoticateUserName(), CreatedOnUtc = DateTime.UtcNow, TreasuryId = treausry.Id, }; await _uintOfWork.Add(cashMovment); var history = new TreasuryHistory() { CreatedOnUtc = cashMovment.CreatedOnUtc, Amount = -amount, CashMovment = cashMovment, CashMovmentId = cashMovment.Id, TreasuryId = id, }; await _uintOfWork.Add(history); treausry.Total -= amount; await _uintOfWork.Update(treausry); await _uintOfWork.Commit(); return(new ErrorRepsonse <TreasuryHistoryDto>(_mapper.Map <TreasuryHistoryDto>(history))); } catch (Exception ex) { await _uintOfWork.RoleBack(); return(new ErrorRepsonse <TreasuryHistoryDto>("حدث خطأ ما ")); } }
public async Task IncreaseAmountByOrderFromAgent(ReceiptOfTheOrderStatus receiptOfTheOrderStatus) { var receiptOfTheOrderStatusDetalis = receiptOfTheOrderStatus.ReceiptOfTheOrderStatusDetalis.Where(c => c.MoneyPlacedId != (int)MoneyPalcedEnum.WithAgent && c.MoneyPlacedId != (int)MoneyPalcedEnum.OutSideCompany).ToList(); if (receiptOfTheOrderStatusDetalis.Any()) { var treaury = await _repository.GetById(_userService.AuthoticateUserId()); var total = receiptOfTheOrderStatusDetalis.Sum(c => c.Cost - c.AgentCost); treaury.Total += total; await _uintOfWork.Update(treaury); var history = new TreasuryHistory() { Amount = total, ReceiptOfTheOrderStatusId = receiptOfTheOrderStatus.Id, TreasuryId = treaury.Id, CreatedOnUtc = DateTime.UtcNow, }; await _uintOfWork.Add(history); } }