def METHOD_NAME(
    order: models.Order,
    input_lines_data: dict[uuid.UUID, models.OrderGrantedRefundLine],
    errors: list[dict[str, Any]],
    error_code: str,
    granted_refund_lines_to_exclude: Optional[list[int]] = None,
):
    all_granted_refund_ids = order.granted_refunds.all().values_list("id", flat=True)
    all_granted_refund_lines = models.OrderGrantedRefundLine.objects.filter(
        granted_refund_id__in=all_granted_refund_ids
    )
    if granted_refund_lines_to_exclude:
        all_granted_refund_lines.exclude(pk__in=granted_refund_lines_to_exclude)
    lines_with_quantity_already_refunded: dict[uuid.UUID, int] = defaultdict(int)
    for line in all_granted_refund_lines:
        lines_with_quantity_already_refunded[line.order_line_id] += line.quantity
    for granted_refund_line in input_lines_data.values():
        if not granted_refund_line.order_line:
            continue
        quantity_already_refunded = lines_with_quantity_already_refunded[
            granted_refund_line.order_line.pk
        ]
        if (
            granted_refund_line.quantity + quantity_already_refunded
            > granted_refund_line.order_line.quantity
        ):
            available_quantity = (
                granted_refund_line.order_line.quantity - quantity_already_refunded
            )
            errors.append(
                {
                    "line_id": graphene.Node.to_global_id(
                        "OrderLine", granted_refund_line.order_line.pk
                    ),
                    "field": "quantity",
                    "message": (
                        "Cannot grant refund for more than the available quantity "
                        f"of the line ({available_quantity})."
                    ),
                    "code": error_code,
                }
            )