{
 "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": [
    "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"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "openShopifyOrders = getShopifyOrders(\"open\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "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": [
    "print(len(openShopifyOrders))\n",
    "print(openShopifyOrders[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "blockedTags = [\n",
    "    \"leap-era\",\n",
    "    \"competitor\",\n",
    "    \"staff\",\n",
    "    \"early-access\",\n",
    "    \"test-order\"\n",
    "]\n",
    "\n",
    "def containsBeyondItem(shopifyOrder):\n",
    "    for lineItem in shopifyOrder[\"line_items\"]:\n",
    "        if lineItem[\"product_id\"] == 7693929054425:\n",
    "            return True\n",
    "    return False\n",
    "\n",
    "def isJapanBeyondOrder(shopifyOrder):\n",
    "    try:\n",
    "        if shopifyOrder[\"shipping_address\"][\"country_code\"] != \"JP\":\n",
    "            return False\n",
    "        \n",
    "        if shopifyOrder[\"shipping_address\"] == None:\n",
    "            print(f\"Skipping {shopifyOrder['name']} because it has no shipping address\")\n",
    "            return False\n",
    "        \n",
    "        tags = []\n",
    "        if \"tags\" in shopifyOrder:\n",
    "            tags += [t.strip() for t in shopifyOrder[\"tags\"].split(\",\")]\n",
    "        if \"customer\" in shopifyOrder:\n",
    "            if \"tags\" in shopifyOrder[\"customer\"]:\n",
    "                tags += [t.strip() for t in shopifyOrder[\"customer\"][\"tags\"].split(\",\")]\n",
    "\n",
    "        for tag in tags:\n",
    "            if tag in blockedTags:\n",
    "                print(f\"Skipping {shopifyOrder['name']} because it contains a blocked tag ({tag} in \\\"{tags}\\\")\")\n",
    "                return False\n",
    "\n",
    "        return True\n",
    "    except:\n",
    "        print(\"Error processing order:\")\n",
    "        print(shopifyOrder)\n",
    "        return False\n",
    "\n",
    "japanOrderCount = 0\n",
    "for shopifyOrder in openShopifyOrders:\n",
    "    if shopifyOrder.get(\"bigOrder\") == None:\n",
    "        continue\n",
    "    if isJapanBeyondOrder(shopifyOrder[\"shopifyOrder\"]) and containsBeyondItem(shopifyOrder[\"shopifyOrder\"]):\n",
    "        japanOrderCount += 1\n",
    "print(\"Japan orders: \" + str(japanOrderCount))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "state = \"WaitingForScanRequest\"\n",
    "countryCode = \"JP\"\n",
    "queryResult = BigApi.adminGet(f\"/admin/shop/orders?countryCode={countryCode}&origin=Shopify\")\n",
    "\n",
    "ipds = {}\n",
    "\n",
    "print(\"total items: \" + str(len(queryResult[\"items\"])))\n",
    "cushionCount = 0\n",
    "lineItemBeyondCount = 0\n",
    "beyondCount = 0\n",
    "\n",
    "def containsBeyondItem(shopifyOrder):\n",
    "    for lineItem in shopifyOrder[\"line_items\"]:\n",
    "        if lineItem[\"product_id\"] == 7693929054425:\n",
    "            return True\n",
    "    return False\n",
    "\n",
    "for bigOrder in queryResult[\"items\"]:\n",
    "    if bigOrder[\"shopifyOrderSnapshot\"]:\n",
    "        if containsBeyondItem(bigOrder[\"shopifyOrderSnapshot\"]):\n",
    "            lineItemBeyondCount = lineItemBeyondCount + 1\n",
    "    if containsBeyondItem(bigOrder[\"shopifyOrderSnapshot\"]) and any(lineItem[\"type\"] == \"BigscreenBeyondV1\" for lineItem in bigOrder[\"currentLineItems\"]) == False:\n",
    "        print(\"no beyond line item for order: \" + str(bigOrder[\"id\"]))\n",
    "    for lineItem in bigOrder[\"currentLineItems\"]:\n",
    "        if lineItem[\"type\"] == \"BigscreenCushionV1\":\n",
    "            cushionCount = cushionCount + 1\n",
    "    if any(lineItem[\"type\"] == \"BigscreenBeyondV1\" for lineItem in bigOrder[\"currentLineItems\"]):\n",
    "        beyondCount = beyondCount + 1\n",
    "        if ipds.get(bigOrder[\"ipd\"]) is None:\n",
    "            ipds[bigOrder[\"ipd\"]] = 0\n",
    "        ipds[bigOrder[\"ipd\"]] = ipds[bigOrder[\"ipd\"]] + 1\n",
    "\n",
    "print(\"total cushions: \" + str(cushionCount))\n",
    "print(\"total line item beyonds: \" + str(lineItemBeyondCount))\n",
    "print(\"total beyonds: \" + str(beyondCount))\n",
    "\n",
    "sorted_keys = sorted(ipds.keys())\n",
    "for key in sorted_keys:\n",
    "    print(f\"{key}\\t{ipds[key]}\")"
   ]
  }
 ],
 "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"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
