-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add pending fulfillment management command
- Loading branch information
Showing
1 changed file
with
59 additions
and
0 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
...apps/commercetools/management/commands/find_ct_pending_fulfillment_non_refunded_orders.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
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: | ||
payment = ( | ||
payment_ref.obj | ||
) # Expand the payment reference to access the payment object | ||
refund_found = has_refund_transaction(payment) | ||
|
||
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) | ||
|
||
print(f"Non-Refunded Orders: {len(non_refunded_orders)}") | ||
|
||
for order in non_refunded_orders: | ||
print(f"Order ID: {order.id}") |