{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# import websockets\n",
    "import json\n",
    "import base64\n",
    "\n",
    "from api import BigApi\n",
    "BigApi.init(\".admin.env\")\n",
    "BigApi.adminLogin()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "BigApi.adminGet(\"/admin/shop/shopify_orders?limit=10&search=%23BS039172313\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from factoryKit import FactoryKit\n",
    "\n",
    "FactoryKit.init()\n",
    "FactoryKit.loadBs1Stock()\n",
    "print(json.dumps(FactoryKit.bs1Stock, indent=4))\n",
    "\n",
    "# BigApi.adminGetWithoutRetry(f\"/admin/inventory/items?limit=50&type=BigscreenBeyondV1&status=InStock&serialNumberLike=BS170\")\n",
    "# key = \"ipd55\"\n",
    "# print(key[-2:])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def containsAudioStrap(shopifyOrder):\n",
    "    for lineItem in shopifyOrder[\"line_items\"]:\n",
    "        if lineItem[\"product_id\"] == 8100195008729:\n",
    "            if lineItem[\"fulfillment_status\"] == None and lineItem[\"fulfillable_quantity\"] == 1:\n",
    "                return True\n",
    "    return False\n",
    "\n",
    "def containsBeyond(shopifyOrder):\n",
    "    for lineItem in shopifyOrder[\"line_items\"]:\n",
    "        if lineItem[\"product_id\"] == 7693929054425:\n",
    "            if lineItem[\"fulfillment_status\"] == None and lineItem[\"fulfillable_quantity\"] == 1:\n",
    "                return True\n",
    "    return False\n",
    "\n",
    "def containsRxLenses(shopifyOrder):\n",
    "    for lineItem in shopifyOrder[\"line_items\"]:\n",
    "        if lineItem[\"product_id\"] == 7761325916377:\n",
    "            if lineItem[\"fulfillment_status\"] == None and lineItem[\"fulfillable_quantity\"] == 1:\n",
    "                return True\n",
    "    return False\n",
    "\n",
    "def isValidOrder(shopifyOrder):\n",
    "    if shopifyOrder[\"cancelled_at\"] != None:\n",
    "        return False\n",
    "    if shopifyOrder[\"financial_status\"] == \"refunded\" or shopifyOrder[\"financial_status\"] == \"partially_refunded\":\n",
    "        return False\n",
    "    return True\n",
    "\n",
    "def BigOrderOnlyNeedsHMD(bigOrder):\n",
    "    if bigOrder[\"state\"] == \"WaitingForScanRequest\":\n",
    "        return False\n",
    "    if \"shipmentHold\" in bigOrder:\n",
    "        return False\n",
    "    bigOrderID = bigOrder[\"id\"]\n",
    "    # Perform inventory check on this order. If it is waiting on more than just the HMD, return false\n",
    "    url = f\"/admin/shop/order/{bigOrderID}/inventory\"\n",
    "    data = BigApi.adminGet(url)\n",
    "    # return false if any item where data[n][\"productType\"] != \"BigscreenBeyondV1\" has a status other than \"StockAvailable\"\n",
    "    for item in data:\n",
    "        if item[\"productType\"] != \"BigscreenBeyondV1\" and item[\"status\"] != \"StockAvailable\":\n",
    "            # print(f\"Ignoring BigOrder {bigOrderID} because it's waiting on more than just HMD\") #debug\n",
    "            return False\n",
    "        if item[\"productType\"] == \"BigscreenBeyondV1\" and item[\"status\"] == \"StockAvailable\":\n",
    "            # print(f\"Ignoring BigOrder {bigOrderID} because it's waiting on more than just HMD\") #debug\n",
    "            return False\n",
    "    return True\n",
    "\n",
    "def containsOneUnfulfilledItem(shopifyOrder):\n",
    "    unfulfilledLineItems = 0\n",
    "    for lineItem in shopifyOrder[\"line_items\"]:\n",
    "        if lineItem[\"fulfillment_status\"] == None:\n",
    "            unfulfilledLineItems += 1\n",
    "    return unfulfilledLineItems == 1\n",
    "\n",
    "def containsUnfulfilledItems(shopifyOrder):\n",
    "    hmdOrderItemIDs = [8100195008729, 7693929054425, 7761325916377]\n",
    "    hmdOrderItems = 0\n",
    "    unfulfilledHmdOrderItems = 0\n",
    "    for lineItem in shopifyOrder[\"line_items\"]:\n",
    "        if lineItem[\"product_id\"] in hmdOrderItemIDs:\n",
    "            hmdOrderItems += 1\n",
    "            if lineItem[\"fulfillment_status\"] == None:\n",
    "                unfulfilledHmdOrderItems += 1\n",
    "    return unfulfilledHmdOrderItems == hmdOrderItems\n",
    "\n",
    "def getShopifyOrders(status, fulfillment_status = None):\n",
    "    ordersReturnedLimit = 200 # max is 200, apparently\n",
    "    nextCursor = None\n",
    "    orders = {f\"ipd{i}\": [] for i in range(55, 73) if i != 70}\n",
    "    netOrderCount = 0\n",
    "\n",
    "    while(True):\n",
    "        url = f\"/admin/shop/shopify_orders?limit={ordersReturnedLimit}\"\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",
    "\n",
    "        for order in data[\"orders\"]:\n",
    "            if order.get(\"bigOrder\") and containsUnfulfilledItems(order['shopifyOrder']) and containsBeyond(order['shopifyOrder']):\n",
    "                if isValidOrder(order[\"shopifyOrder\"]) == False:\n",
    "                    #print(f\"Ignoring order {order['shopifyOrder']['id']} because it is not valid\")\n",
    "                    pass\n",
    "                elif BigOrderOnlyNeedsHMD(order[\"bigOrder\"]) == False:\n",
    "                    # print(f\"Ignoring order {order['shopifyOrder']['id']} because it's waiting on more than just HMD\")\n",
    "                    pass\n",
    "                else:\n",
    "                    bigOrderIPD = order[\"bigOrder\"][\"ipd\"]\n",
    "                    orders[f\"ipd{bigOrderIPD}\"].append(order)\n",
    "                    netOrderCount += 1\n",
    "\n",
    "        print(f\"Orders accumulated: {netOrderCount}\")\n",
    "        if \"nextCursor\" in data:\n",
    "            nextCursor = data[\"nextCursor\"]\n",
    "\n",
    "        if (len(data[\"orders\"]) < ordersReturnedLimit):\n",
    "            break\n",
    "        if (len(orders) > 100):\n",
    "            print(\"Breaking because order count passed the threshold\")\n",
    "            break   \n",
    "    return orders\n",
    "\n",
    "BigApi.adminLogin()\n",
    "shippedOrders = getShopifyOrders(\"open\", None)\n",
    "\n",
    "# check inventory status of all orders\n",
    "# for order in shippedOrders:\n",
    "#     boid = order[\"bigOrder\"][\"id\"]\n",
    "#     url = f\"/admin/shop/order/{boid}/inventory\"\n",
    "#     data = BigApi.adminGet(url)\n",
    "#     print(json.dumps(data, indent=4))\n",
    "\n",
    "# output a count of list entries for each key of shippedOrders\n",
    "for key in shippedOrders:\n",
    "    if len(shippedOrders[key]) > 0:\n",
    "        print(f\"- {key}: {len(shippedOrders[key])}\")\n",
    "        counter = 1\n",
    "        for order in shippedOrders[key]:\n",
    "            bigOrderID = order[\"bigOrder\"][\"id\"]\n",
    "            print(f\"  {counter}. ||https://main-ocean-arda.bigscreencloud.com/shop/order/{bigOrderID}||\")\n",
    "            counter += 1\n",
    "\n",
    "\n",
    "# output shippedOrders list as json\n",
    "# print(json.dumps(shippedOrders, indent=4))\n",
    "# print(len(shippedOrders))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "## Set bigOrderID, get the order data and print as json\n",
    "bigOrderID = \"kr3nITXwImkc9SM6\"\n",
    "BigApi.adminLogin()\n",
    "data = BigApi.adminGet(f\"/admin/shop/order/{bigOrderID}\")\n",
    "\n",
    "print(json.dumps(data, indent=4))\n",
    "# check if bigOrder[\"shipmentHold\"] exists\n",
    "# if \"shipmentHold\" in data:\n",
    "#     print(\"ShipmentHold exists\")\n",
    "#     print(data[\"shipmentHold\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "## Set bigOrderID, get the order data and print as json\n",
    "bigOrderID = \"kr3nITXwImkc9SM6\"\n",
    "BigApi.adminLogin()\n",
    "data = BigApi.adminGet(f\"/admin/shop/order/{bigOrderID}/inventory\")\n",
    "\n",
    "print(json.dumps(data, indent=4))\n",
    "# check if bigOrder[\"shipmentHold\"] exists\n",
    "# if \"shipmentHold\" in data:\n",
    "#     print(\"ShipmentHold exists\")\n",
    "#     print(data[\"shipmentHold\"])"
   ]
  }
 ],
 "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
}
