public async Task <IActionResult> CreateOrderPayment(string orderId, OrderPaymenForCreationDto creationDto) { var accountId = User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value; var result = await _paymentService.OrderReceiveAsync(accountId, orderId, creationDto); return(StatusCode((int)result.Code, result)); }
public async Task <ApiResult <string> > OrderReceiveAsync(string accountId, string orderId, OrderPaymenForCreationDto creationDto) { var checkEmployee = await _context.Employees.Where(x => x.AppuserId.ToString() == accountId) .SingleOrDefaultAsync(); if (checkEmployee == null) { return(new ApiResult <string>(HttpStatusCode.NotFound, $"Lỗi tài khoản đăng nhập")); } var checkOrder = await _context.Orders.FindAsync(orderId); if (checkOrder == null) { return(new ApiResult <string>(HttpStatusCode.NotFound, ($"Không tìm thấy đơn hàng có mã: {orderId}"))); } if (checkOrder.TransactionStatusId != GlobalProperties.FinishedTransactionId && checkOrder.TransactionStatusId != GlobalProperties.WaitingTransactionId && checkOrder.TransactionStatusId != GlobalProperties.CancelTransactionId) { if (checkEmployee.BranchId == checkOrder.BranchId) { var receiptVoucher = ObjectMapper.Mapper.Map <ReceiptVoucher>(creationDto); var sequenceNumber = await _context.ReceiptVouchers.CountAsync(); var receiptVoucherId = IdentifyGenerator.GenerateReceiptVoucherId(sequenceNumber + 1); receiptVoucher.Id = receiptVoucherId; receiptVoucher.OrderId = checkOrder.Id; receiptVoucher.DateCreated = DateTime.Now; receiptVoucher.CustomerId = checkOrder.CustomerId; receiptVoucher.IsDelete = false; receiptVoucher.BranchId = checkOrder.BranchId; receiptVoucher.EmployeeId = checkEmployee.Id; await _context.ReceiptVouchers.AddAsync(receiptVoucher); await _context.SaveChangesAsync(); var totalPaid = await(from rv in _context.ReceiptVouchers where rv.OrderId == checkOrder.Id select rv.Received).SumAsync(); var checkExportStock = await _context.GoodsDeliveryNotes.Where(x => x.OrderId == checkOrder.Id) .SingleOrDefaultAsync(); if (totalPaid == checkOrder.TotalAmount) { if (checkExportStock != null) { checkOrder.TransactionStatusId = GlobalProperties.FinishedTransactionId; } checkOrder.PaymentStatusId = GlobalProperties.PaidPaymentId; } else if (totalPaid < checkOrder.TotalAmount) { checkOrder.PaymentStatusId = GlobalProperties.PartialPaymentId; } else { return(new ApiResult <string>(HttpStatusCode.BadRequest, $"Giá trị thanh toán lớn hơn giá trị đơn hàng")); } await _context.SaveChangesAsync(); return(new ApiResult <string>(HttpStatusCode.OK) { ResultObj = receiptVoucherId, Message = "Đã tạo phiếu thu cho đơn hàng" }); } return(new ApiResult <string>(HttpStatusCode.Forbidden, $"Tài khoản đăng nhập hiện tại không có quyền chỉnh sửa phiếu thu tại chi nhánh này")); } return(new ApiResult <string>(HttpStatusCode.BadRequest, "Chỉ được tạo phiếu thu cho đơn hàng đang trọng trạng thái giao dịch.")); }