public async Task <IActionResult> CreatePaymentVoucher([FromBody] PaymentVoucherForCreationDto creationDto) { var accountId = User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value; var result = await _paymentService.CreatePaymentVoucherAsync(creationDto, accountId); return(StatusCode((int)result.Code, result)); }
public async Task <ApiResult <string> > CreatePaymentVoucherAsync(PaymentVoucherForCreationDto creationDto, string accountId) { var employee = await _context.Employees.Where(x => x.AppuserId.ToString() == accountId) .SingleOrDefaultAsync(); if (employee == null) { return(new ApiResult <string>(HttpStatusCode.NotFound, $"Lỗi tài khoản đăng nhập")); } if (!string.IsNullOrEmpty(creationDto.CustomerId) && !string.IsNullOrEmpty(creationDto.SupplierId)) { return(new ApiResult <string>(HttpStatusCode.BadRequest, $"Một phiếu chi chỉ thuộc về một đối tượng.")); } if (string.IsNullOrEmpty(creationDto.CustomerId) && string.IsNullOrEmpty(creationDto.SupplierId)) { return(new ApiResult <string>(HttpStatusCode.BadRequest, $"Phiếu chi phải thuộc về một đối tượng")); } if (!string.IsNullOrEmpty(creationDto.CustomerId)) { var checkCustomer = await _context.Customers.Where(x => x.Id == creationDto.CustomerId) .SingleOrDefaultAsync(); if (checkCustomer == null) { return(new ApiResult <string>(HttpStatusCode.NotFound, $"Không tìm thấy khách hàng có mã: {creationDto.CustomerId}")); } } else { var checkSupplier = await _context.Suppliers.Where(x => x.Id == creationDto.SupplierId) .SingleOrDefaultAsync(); if (checkSupplier == null) { return(new ApiResult <string>(HttpStatusCode.NotFound, $"Không tìm thấy nhà cung cấp có mã: {creationDto.SupplierId}")); } } if (creationDto.Paid <= 0) { return(new ApiResult <string>(HttpStatusCode.BadRequest, $"Số tiền chi phải là một số dương")); } var sequenceNumber = await _context.PaymentVouchers.CountAsync(); var paymentVoucherId = IdentifyGenerator.GeneratePaymentVoucherId(sequenceNumber + 1); var paymentVoucher = new PaymentVoucher() { Description = creationDto.Description, Id = paymentVoucherId, Paid = creationDto.Paid, BranchId = employee.BranchId, CustomerId = string.IsNullOrEmpty(creationDto.CustomerId)?null:creationDto.CustomerId, SupplierId = string.IsNullOrEmpty(creationDto.SupplierId)?null:creationDto.SupplierId, EmployeeId = employee.Id, DateCreated = DateTime.Now, IsDelete = false, PaymentDate = creationDto.PaymentDate, PaymentMethodId = creationDto.PaymentMethodId }; await _context.PaymentVouchers.AddAsync(paymentVoucher); await _context.SaveChangesAsync(); return(new ApiResult <string>(HttpStatusCode.OK) { ResultObj = paymentVoucherId, Message = "Tạo phiếu chi thành công" }); }