Exemplo n.º 1
0
        public async Task <ApiResult <string> > PurchaseOrderPaymentAsync(string accountId, string purchaseOrderId,
                                                                          PurchaseOrderPaymentForCreationDto 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 checkPurchaseOrder = await _context.PurchaseOrders.FindAsync(purchaseOrderId);

            if (checkPurchaseOrder == null)
            {
                return(new ApiResult <string>(HttpStatusCode.NotFound));
            }
            if (checkEmployee.BranchId == checkPurchaseOrder.BrandId)
            {
                var sequenceNumber = await _context.PaymentVouchers.CountAsync();

                var paymentVoucherId = IdentifyGenerator.GeneratePaymentVoucherId(sequenceNumber + 1);
                var paymentVoucher   = ObjectMapper.Mapper.Map <PaymentVoucher>(creationDto);
                paymentVoucher.Id = paymentVoucherId;
                paymentVoucher.PurchaseOrderId = purchaseOrderId;
                paymentVoucher.DateCreated     = DateTime.Now;
                paymentVoucher.SupplierId      = checkPurchaseOrder.SupplierId;
                paymentVoucher.EmployeeId      = checkEmployee.Id;
                paymentVoucher.BranchId        = checkEmployee.BranchId;
                await _context.PaymentVouchers.AddAsync(paymentVoucher);

                var totalPayment = await(from pv in _context.PaymentVouchers
                                         where pv.PurchaseOrderId == purchaseOrderId
                                         select pv.Paid).SumAsync();
                if (totalPayment - checkPurchaseOrder.TotalAmount >= 0)
                {
                    checkPurchaseOrder.TransactionStatusId = GlobalProperties.FinishedTransactionId;
                    checkPurchaseOrder.PaymentStatusId     = GlobalProperties.PaidPaymentId;
                }
                else if (checkPurchaseOrder.TotalAmount - totalPayment > 0)
                {
                    checkPurchaseOrder.TransactionStatusId = GlobalProperties.TradingTransactionId;
                    checkPurchaseOrder.PaymentStatusId     = GlobalProperties.PartialPaymentId;
                }
                await _context.SaveChangesAsync();

                return(new ApiResult <string>(HttpStatusCode.OK)
                {
                    ResultObj = paymentVoucherId, Message = "Tạo phiếu chi cho đơn hàng thành công"
                });
            }
            return(new ApiResult <string>(HttpStatusCode.Forbidden, $"Tài khoản hiện tại không có quyền chỉnh sửa phiếu chi tại chi nhánh này"));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> CreatePurchaseOrderPayment(string purchaseOrderId, PurchaseOrderPaymentForCreationDto creationDto)
        {
            var accountId = User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value;
            var result    = await _paymentService.PurchaseOrderPaymentAsync(accountId, purchaseOrderId, creationDto);

            return(StatusCode((int)result.Code, result));
        }