Пример #1
0
        public async Task <IActionResult> CreateReceiptVoucher([FromBody] ReceiptVoucherForCreationDto creationDto)
        {
            var accountId = User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value;
            var result    = await _paymentService.CreateReceiptVoucherAsync(creationDto, accountId);

            return(StatusCode((int)result.Code, result));
        }
Пример #2
0
        public async Task <ApiResult <string> > CreateReceiptVoucherAsync(ReceiptVoucherForCreationDto 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 thu 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 thu 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.Received <= 0)
            {
                return(new ApiResult <string>(HttpStatusCode.BadRequest, $"Số tiền thu phải là một số dương"));
            }
            var sequenceNumber = await _context.ReceiptVouchers.CountAsync();

            var receiptVoucherId = IdentifyGenerator.GenerateReceiptVoucherId(sequenceNumber + 1);
            var receiptVoucher   = new ReceiptVoucher()
            {
                Description     = creationDto.Description,
                Id              = receiptVoucherId,
                Received        = creationDto.Received,
                BranchId        = employee.BranchId,
                CustomerId      = string.IsNullOrEmpty(creationDto.CustomerId)?null:creationDto.CustomerId,
                DateCreated     = DateTime.Now,
                EmployeeId      = employee.Id,
                IsDelete        = false,
                PaymentMethodId = creationDto.PaymentMethodId,
                ReceivedDate    = creationDto.ReceivedDate,
                SupplierId      = string.IsNullOrEmpty(creationDto.SupplierId)?null:creationDto.SupplierId
            };
            await _context.ReceiptVouchers.AddAsync(receiptVoucher);

            await _context.SaveChangesAsync();

            return(new ApiResult <string>(HttpStatusCode.OK)
            {
                ResultObj = receiptVoucherId,
                Message = "Tạo phiếu thu thành công"
            });
        }