Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add pending fulfillment management command #321

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from commerce_coordinator.apps.commercetools.management.commands._ct_api_client_command import (
CommercetoolsAPIClientCommand
)
from commerce_coordinator.apps.commercetools.utils import has_refund_transaction


class Command(CommercetoolsAPIClientCommand):
help = "Find Pending Fulfillment Non Refunded Orders in commercetools"

def handle(self, *args, **options):
order_state = "Complete"
line_item_state_id = "bb686576-3bd5-4457-890a-d63c3d31b2ab"

# TODO: Change for your use case
last_modified_at = "2024-10-23T00:00:00"
limit = 500
offset = 0

orders_result = self.ct_api_client.base_client.orders.query(
where=[
f'orderState="{order_state}"',
f'lastModifiedAt>"{last_modified_at}"',
f'lineItems(state(state(id="{line_item_state_id}")))',
],
sort=["completedAt desc", "lastModifiedAt desc"],
limit=limit,
offset=offset,
expand=["paymentInfo.payments[*]"],
)

orders = orders_result.results
non_refunded_orders = []

print(f"Total Orders: {orders_result.total}")

for order in orders:
if order.payment_info and order.payment_info.payments:
payments = order.payment_info.payments
refund_found = False

for payment_ref in payments:
refund_found = has_refund_transaction(payment_ref.obj)

if refund_found:
break

if not refund_found:
non_refunded_orders.append(order)
else:
# Orders with no payment info are considered non-refunded
non_refunded_orders.append(order)
aht007 marked this conversation as resolved.
Show resolved Hide resolved

print(f"Non-Refunded Orders: {len(non_refunded_orders)}")

for order in non_refunded_orders:
print(f"Order ID: {order.id}")
Loading