Exemplo n.º 1
0
        public void UndoManualRefundRequest(RefundRequest refundRequest)
        {
            var order = _orderRepository.GetOrder(refundRequest.OrderId);

            var cancelledOrderItemIds    = order.OrderDetails.Where(od => od.DetailType == OrderItemType.RefundItem && !od.IsCompleted).Select(od => od.OrderItemId).ToArray();
            var manualRefundOrderDetails = order.OrderDetails.Where(od => od.DetailType == OrderItemType.RefundItem && od.IsCompleted && !cancelledOrderItemIds.Contains(od.OrderItemId))
                                           .Select(od => od).OrderByDescending(od => od.Id).ToList();

            if (manualRefundOrderDetails.IsNullOrEmpty())
            {
                throw new Exception("No Manual Refund item found in the order.");
            }

            IUniqueItemRepository <Refund> refundItemRepository = new RefundRepository();

            var forOrganizationRoleUser     = new OrganizationRoleUser(manualRefundOrderDetails.First().ForOrganizationRoleUserId);
            var creatorOrganizationRoleUser = new OrganizationRoleUser(_sessionContext.UserSession.CurrentOrganizationRole.OrganizationRoleUserId);

            if (manualRefundOrderDetails.Count() == 1)
            {
                var manualRefundOrderDetail = manualRefundOrderDetails.FirstOrDefault(mrod => mrod.Price == refundRequest.RequestedRefundAmount);

                if (manualRefundOrderDetail == null)
                {
                    throw new Exception("No Manual Refund item found for request amount.");
                }

                var refundItem = refundItemRepository.GetById(manualRefundOrderDetail.OrderItem.ItemId);
                refundItem.Price = -1 * refundItem.Price;
                _orderService.CancelRefundOrder(new[] { refundItem }, order, creatorOrganizationRoleUser, forOrganizationRoleUser);
            }
            else
            {
                decimal totalAmount = 0;

                var orderables = new List <IOrderable>();
                foreach (var manualRefundOrderDetail in manualRefundOrderDetails)
                {
                    var refundItem = refundItemRepository.GetById(manualRefundOrderDetail.OrderItem.ItemId);

                    if ((totalAmount + refundItem.Price) > refundRequest.RequestedRefundAmount)
                    {
                        break;
                    }

                    totalAmount     += refundItem.Price;
                    refundItem.Price = -1 * refundItem.Price;
                    orderables.Add(refundItem);
                }
                if (totalAmount != refundRequest.RequestedRefundAmount || orderables.IsNullOrEmpty())
                {
                    throw new Exception("No Manual Refund items found for request amount.");
                }

                _orderService.CancelRefundOrder(orderables, order, creatorOrganizationRoleUser, forOrganizationRoleUser);
            }

            var repository = ((IRepository <RefundRequest>)_refundRequestRepository);

            refundRequest.RequestStatus       = (long)RequestStatus.Reverted;
            refundRequest.RefundRequestResult = new RefundRequestResult
            {
                ProcessedOn = DateTime.Now,
                ProcessedByOrgRoleUserId = _sessionContext.UserSession.CurrentOrganizationRole.OrganizationRoleUserId
            };
            repository.Save(refundRequest);
        }