{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from api import BigApi\n",
    "BigApi.init(\".admin.env\")\n",
    "BigApi.adminLogin()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from enum import Enum\n",
    "\n",
    "schemas = BigApi.adminGet(\"/admin/shop/schemas\")\n",
    "BigOrderAction = Enum(\"BigOrderAction\", dict([(v, k) for k, v in schemas[\"BigOrderAction\"].items()]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Get big orders\n",
    "def getBigOrders():\n",
    "    cursorId = None\n",
    "    orders = []\n",
    "    while(True):\n",
    "        url = \"/admin/shop/orders?limit=200\"\n",
    "        if cursorId != None:\n",
    "            url += \"&cursorId=\" + cursorId\n",
    "        data = BigApi.adminGet(url)\n",
    "        orders.extend(data[\"items\"])\n",
    "\n",
    "        if \"cursorId\" not in data or len(data[\"items\"]) < 200:\n",
    "            break\n",
    "        else:\n",
    "            cursorId = data[\"cursorId\"]\n",
    "    return orders\n",
    "\n",
    "bigOrders = getBigOrders()\n",
    "print(\"Big orders: \" + str(len(bigOrders)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Get shopify orders\n",
    "def getShopifyOrders(status, fulfillment_status = None):\n",
    "    nextCursor = None\n",
    "    orders = []\n",
    "    while(True):\n",
    "        url = f\"/admin/shop/shopify_orders?limit=200\"\n",
    "        if status != None:\n",
    "            url += \"&status=\" + status\n",
    "        if nextCursor != None:\n",
    "            url += \"&cursor=\" + nextCursor\n",
    "        if fulfillment_status != None:\n",
    "            url += \"&fulfillment_status=\" + fulfillment_status\n",
    "        data = BigApi.adminGet(url)\n",
    "        orders.extend(data[\"orders\"])\n",
    "\n",
    "        if \"nextCursor\" in data:\n",
    "            nextCursor = data[\"nextCursor\"]\n",
    "\n",
    "        if (len(data[\"orders\"]) < 200):\n",
    "            break\n",
    "    return orders\n",
    "\n",
    "cancelledShopifyOrders = getShopifyOrders(\"cancelled\")\n",
    "shippedOrders = getShopifyOrders(None, \"shipped\")\n",
    "\n",
    "#print(\"Open Shopify orders: \" + str(len(openShopifyOrders)))\n",
    "print(\"Cancelled Shopify orders: \" + str(len(cancelledShopifyOrders)))\n",
    "print(\"Shipped Shopify orders: \" + str(len(shippedOrders)))\n",
    "\n",
    "# For each big order, find the shopify order\n",
    "for bigOrder in bigOrders:\n",
    "    foundCancelled = False\n",
    "    foundShipped = False\n",
    "\n",
    "    for shopifyOrder in cancelledShopifyOrders:\n",
    "        if bigOrder[\"shopifyOrderId\"] == shopifyOrder[\"shopifyOrder\"][\"id\"]:\n",
    "            foundCancelled = True\n",
    "            break\n",
    "\n",
    "    for shopifyOrder in shippedOrders:\n",
    "        if bigOrder[\"shopifyOrderId\"] == shopifyOrder[\"shopifyOrder\"][\"id\"]:\n",
    "            foundShipped = True\n",
    "            break\n",
    "\n",
    "    if bigOrder[\"origin\"] == \"TestData\":\n",
    "        continue\n",
    "\n",
    "    if foundCancelled == True:\n",
    "        print(bigOrder[\"id\"] + \" cancelled\") \n",
    "\n",
    "    if foundShipped == True:\n",
    "        print(bigOrder[\"id\"] + \" shipped\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.11"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
