{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Prerequisites:\n",
    "#!pip install requests\n",
    "#!pip install python-dotenv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. Login\n",
    "from api import BigApi\n",
    "\n",
    "BigApi.init(\".admin.env\")\n",
    "BigApi.adminLogin()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 2. Enumerate JobStates (needed!)\n",
    "from enum import Enum\n",
    "\n",
    "schemas = BigApi.adminGet(f\"/admin/fabricator/schemas2\")\n",
    "\n",
    "# print(schemas['JobStates'])\n",
    "# derive JobStates variable from schemas['JobStates'], but swap the keys and values\n",
    "JobStates = {v: k for k, v in schemas['JobStates'].items()}\n",
    "JobStates"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# (Basic API tests)\n",
    "\n",
    "data = BigApi.adminGet(f\"/admin/shop/shopify_orders?search=%23BS01119623\")\n",
    "# data = BigApi.adminGet(f\"/admin/fabricator/job/MdIg4W1z6KjdVrYBVuhE4BuffaY\")\n",
    "\n",
    "data\n",
    "# output = {\n",
    "#   'jobState': JobStates[data['jobState']],\n",
    "#   'nfcTagId': data['nfcTagId'],\n",
    "# }\n",
    "# output"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Recompile counts of lens insert orders by email -> save as JSON file\n",
    "import json\n",
    "# import os\n",
    "import time\n",
    "\n",
    "def orderExcludesTags(orderDict, tagsArr):\n",
    "  for tag in tagsArr:\n",
    "    if(tag in orderDict['shopifyOrder']['tags']):\n",
    "      return False\n",
    "  return True\n",
    "\n",
    "def orderValid(orderDict):\n",
    "  # return orderDict['shopifyOrder']['cancel_reason'] == None and orderDict['shopifyOrder']['fulfillment_status'] == None\n",
    "  return True\n",
    "\n",
    "def isLateEnough(orderDict):\n",
    "  earliestDate = \"2023-02-13T00:06:00-08:00\"\n",
    "  return orderDict['shopifyOrder']['created_at'] > earliestDate\n",
    "\n",
    "# function that accepts a list of properties formatted {name: 'name', value: 'value'} and accepts a name and returns the value\n",
    "def getSiblingProp(props, findVal, findKey = 'name', returnKey = 'value'):\n",
    "  for prop in props:\n",
    "    if(prop[findKey] == findVal):\n",
    "      return prop[returnKey]\n",
    "  return None\n",
    "\n",
    "# function that accepts a key and value, and returns the index of the first object in an array of objects that has that key/value pair\n",
    "def getIndexByProp(props, propKey, matchVal):\n",
    "  for i in range(len(props)):\n",
    "    if(props[i][propKey] == matchVal):\n",
    "      return i\n",
    "  return None\n",
    "\n",
    "\n",
    "\n",
    "excludedTags = ['team-bigscreen', 'competitor']\n",
    "lensInsertOrdersByEmail = [] #item format {email: '', orderName: ''}\n",
    "ordersProcessed = 0\n",
    "skipCount = 0\n",
    "cursor = ''\n",
    "debugOut = ''\n",
    "\n",
    "print(f\"START: Compiling array of all Beyond orders lens insert orders\")\n",
    "\n",
    "while True:\n",
    "  data = BigApi.adminGet(f\"/admin/shop/shopify_orders?status=any&limit=250\" + ('' if cursor == '' else '&cursor=' + cursor))\n",
    "  orders = data['orders']\n",
    "\n",
    "  for order in orders:\n",
    "    # debugOut = order #debug\n",
    "    # break #debug\n",
    "    if not isLateEnough(order):\n",
    "      continue\n",
    "    ordersProcessed += 1\n",
    "    if(orderValid(order) and orderExcludesTags(order, excludedTags)):\n",
    "      items = order['shopifyOrder']['line_items']\n",
    "      for item in items:\n",
    "        if(item['name'] == 'Prescription Lenses'):\n",
    "          existingIndex = getIndexByProp(lensInsertOrdersByEmail, 'email', order['shopifyOrder']['email'])\n",
    "          if(existingIndex != None):\n",
    "            lensInsertOrdersByEmail[existingIndex]['lensOrdersQty'] += 1\n",
    "          else:\n",
    "            lensInsertOrdersByEmail.append({'email': order['shopifyOrder']['email'], 'orderName': order['shopifyOrder']['name'], 'lensOrdersQty': 1})\n",
    "    else:\n",
    "      skipCount += 1\n",
    "    if(ordersProcessed % 100 == 0):\n",
    "      print(f\"{ordersProcessed} total orders processed...\")\n",
    "    if(ordersProcessed % 500 == 0):\n",
    "      # five second time delay to avoid rate limiting\n",
    "      time.sleep(5)\n",
    "      BigApi.adminLogin()\n",
    "\n",
    "  # break if no more nextCursor\n",
    "  if('nextCursor' not in data):\n",
    "    print(f\"### Finished iterating through {ordersProcessed} total orders ###\")\n",
    "    print(f\"There are a grand total of {len(lensInsertOrdersByEmail)} lens insert orders\")\n",
    "    print(f\"{skipCount} orders have been skipped (canceled or fulfilled)\")\n",
    "    break\n",
    "  else:\n",
    "    cursor = data['nextCursor']\n",
    "\n",
    "# for row in csv:\n",
    "#   email = row[4]\n",
    "#   orderName = getBeyondOrderName(beyondOrdersByEmail, email)\n",
    "#   row.append(orderName)\n",
    "\n",
    "lensOrdersFile = 'C:/Users/decid/Documents/Bigscreen/lensOrders.json'\n",
    "\n",
    "# Write the modified dictionary back to the JSON file\n",
    "with open(lensOrdersFile, 'w') as file:\n",
    "    json.dump(lensInsertOrdersByEmail, file, indent=4)  # indent=4 for pretty printing\n",
    "\n",
    "print(f'Lens insert list written to file \"{lensOrdersFile}\":', lensInsertOrdersByEmail)\n",
    "\n",
    "# debugOut #debug\n",
    "# data #debug"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 3. Set up IPD quotas, excluded orders\n",
    "forcedOrderIds = [\n",
    "  # '#BS01119623', # [91] Clifford Zernicek 《60mm》 - 10/5 leftover\n",
    "]\n",
    "excludedOrderIds = [\n",
    "  \"#BS01160423\", # Jonathan Y, who needs to re-do his face scan - 10/3\n",
    "]\n",
    "ipdQuotas = {\n",
    "  '55mm': 3,\n",
    "  '56mm': 2,\n",
    "  '57mm': 5,\n",
    "  '58mm': 10,\n",
    "  '59mm': 12,\n",
    "  '60mm': 18,\n",
    "  '61mm': 18,\n",
    "  '62mm': 28,\n",
    "  '63mm': 33,\n",
    "  '64mm': 21,\n",
    "  '65mm': 35,\n",
    "  '66mm': 30,\n",
    "  '67mm': 33,\n",
    "  '68mm': 21,\n",
    "  '69mm': 31,\n",
    "  # '70mm': 0,\n",
    "  '71mm': 8,\n",
    "  '72mm': 12,\n",
    "  # '73mm': 2,\n",
    "  # '74mm': 2,\n",
    "  # '75mm': 2,\n",
    "}\n",
    "maximumOrders = len(forcedOrderIds)\n",
    "for key in ipdQuotas:\n",
    "  maximumOrders += ipdQuotas[key]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 4. Compile order data for unfulfilled US orders shipping in Q3 2023 → csv variable\n",
    "import json\n",
    "import os\n",
    "import hashlib\n",
    "import urllib.parse\n",
    "\n",
    "def orderExcludesTags(orderDict, tagsArr):\n",
    "  for tag in tagsArr:\n",
    "    if(tag in orderDict['shopifyOrder']['tags']):\n",
    "      return False\n",
    "  return True\n",
    "\n",
    "def orderIncludesTags(orderDict, tagsArr):\n",
    "  for tag in tagsArr:\n",
    "    if(tag not in orderDict['shopifyOrder']['tags']):\n",
    "      return False\n",
    "  return True\n",
    "\n",
    "def orderValid(orderDict):\n",
    "  return orderDict['shopifyOrder']['billing_address']['country'] == 'United States' and orderDict['shopifyOrder']['cancel_reason'] == None and orderDict['shopifyOrder']['fulfillment_status'] == None and order['shopifyOrder']['name'] not in excludedOrderIds\n",
    "\n",
    "def isLateEnough(orderDict):\n",
    "  earliestDate = \"2023-02-13T00:06:00-08:00\"\n",
    "  return orderDict['shopifyOrder']['created_at'] > earliestDate\n",
    "\n",
    "def isPartOfQ3(orderDict):\n",
    "  cutoffDate = \"2023-07-22T00:00:00-08:00\"\n",
    "  return orderDict['shopifyOrder']['created_at'] < cutoffDate\n",
    "\n",
    "def actualIpd(ipd):\n",
    "  # seems redundant but keep it\n",
    "  ipdString = f\"{ipd}\"\n",
    "  ipdInt = int(ipdString)\n",
    "  if ipdInt < 55:\n",
    "    ipdInt = 55\n",
    "  elif ipdInt == 70:\n",
    "    ipdInt = 69\n",
    "  elif ipdInt > 72:\n",
    "    ipdInt = 72\n",
    "  return f\"{ipdInt}\"\n",
    "\n",
    "# function that accepts a list of properties formatted {name: 'name', value: 'value'} and accepts a name and returns the value\n",
    "def getSiblingProp(props, findVal, findKey = 'name', returnKey = 'value'):\n",
    "  for prop in props:\n",
    "    if(prop[findKey] == findVal):\n",
    "      return prop[returnKey]\n",
    "  return None\n",
    "\n",
    "# function that accepts a key and value, and returns the index of the first object in an array of objects that has that key/value pair\n",
    "def getIndexByProp(props, propKey, matchVal):\n",
    "  for i in range(len(props)):\n",
    "    if(props[i][propKey] == matchVal):\n",
    "      return i\n",
    "  return None\n",
    "\n",
    "# function that accepts order dict, and returns order.bigOrder.lineItems[i].currentJobId where lineItems[i].type == 'BeyondCushionV1'\n",
    "# IMPORTANT: Ensure JobStates variable is populated by running the second cell above\n",
    "def getCushionInfo(orderDict):\n",
    "  retFallback = {\n",
    "    'jobId': None,\n",
    "    'jobState': None,\n",
    "    'cushionIndex': None,\n",
    "    'originalIpd': '-1',\n",
    "  }\n",
    "  items = orderDict['bigOrder']['lineItems']\n",
    "  for item in items:\n",
    "    if(item['type'] == 'BeyondCushionV1'):\n",
    "      jobId = item['currentJobId']\n",
    "      if jobId == None:\n",
    "        break\n",
    "      try:\n",
    "        result = BigApi.adminGet(f\"/admin/fabricator/job/{jobId}\")\n",
    "      except:\n",
    "        print(f\"## API ERROR GETTING CUSHION INFO FOR JOB {jobId} ##\")\n",
    "        return retFallback\n",
    "      cushionIndex = None\n",
    "      if 'nfcTagId' in result and result['nfcTagId'] != None:\n",
    "        hash_object = hashlib.md5(jobId.encode())\n",
    "        cushionIndex = (int(hash_object.hexdigest(), 16) % 1000)/10\n",
    "      return {\n",
    "        'jobId': jobId,\n",
    "        'jobState': JobStates[result['jobState']],\n",
    "        'cushionIndex': cushionIndex,\n",
    "        'originalIpd': result['ipd'],\n",
    "      }\n",
    "  return retFallback\n",
    "\n",
    "\n",
    "\n",
    "orderLibFile = 'C:/Users/decid/Documents/Bigscreen/orderLib.json'\n",
    "# If file exists, read it. If not, quit\n",
    "if os.path.exists(orderLibFile):\n",
    "    try:\n",
    "        with open(orderLibFile, 'r') as file:\n",
    "            orderLib = json.load(file)\n",
    "        # print('Original data:', orderLib) #debug\n",
    "    except json.JSONDecodeError:\n",
    "        assert False, f\"Error: {orderLibFile} contains invalid JSON. Please check formatting.\"\n",
    "else:\n",
    "    print(f\"{orderLibFile} does not exist. Initializing an empty dictionary.\")\n",
    "    orderLib = {\n",
    "      'latestShippingId': 58,\n",
    "      'orderDetails': [], #item format {orderName: '#BS00000', shippingId: 59}\n",
    "    }\n",
    "\n",
    "lensOrdersFile = 'C:/Users/decid/Documents/Bigscreen/lensOrders.json'\n",
    "# If file exists, read it. If not, quit\n",
    "if os.path.exists(lensOrdersFile):\n",
    "    try:\n",
    "        with open(lensOrdersFile, 'r') as file:\n",
    "            lensInsertOrdersByEmail = json.load(file)\n",
    "        # print('Original data:', orderLib) #debug\n",
    "    except json.JSONDecodeError:\n",
    "        assert False, f\"Error: {lensOrdersFile} contains invalid JSON. Please check formatting.\"\n",
    "else:\n",
    "    print(f\"{lensOrdersFile} does not exist. Populate and write it using the cell above.\")\n",
    "\n",
    "\n",
    "\n",
    "# different from quotas. ongoing count of how many orders have been assigned to each IPD\n",
    "ipdCounts = {}\n",
    "for i in range(55, 73):\n",
    "  ipdCounts[f\"{i}mm\"] = 0\n",
    "\n",
    "orders = []\n",
    "excludedTags = ['team-bigscreen', 'competitor']\n",
    "csv = [['Date', 'Email', 'Shipping ID', 'Name', 'ID', 'FINAL IPD', 'BigOrder URL', 'Needs Rx Inserts?']]\n",
    "cushionsInfo = []\n",
    "ordersProcessed = 0\n",
    "newOrdersIndexed = 0\n",
    "ordersToFulfill = 0\n",
    "skipCount = 0\n",
    "inDateRange = True\n",
    "cursor = ''\n",
    "debugOut = ''\n",
    "\n",
    "print(f\"START: Compiling array of {maximumOrders} unfulfilled Q3 Beyond orders\")\n",
    "\n",
    "while True:\n",
    "  if len(orders) == 0:\n",
    "    for orderId in forcedOrderIds:\n",
    "      data = BigApi.adminGet(f\"/admin/shop/shopify_orders?search={urllib.parse.quote(orderId)}\")\n",
    "      if(len(data['orders']) > 0):\n",
    "        data['orders'][0]['force'] = True\n",
    "        orderIpdKey = f\"{actualIpd(data['orders'][0]['bigOrder']['ipd'])}mm\"\n",
    "        if not orderIpdKey in ipdQuotas:\n",
    "          ipdQuotas[orderIpdKey] = 0\n",
    "        ipdQuotas[orderIpdKey] += 1\n",
    "      orders.extend(data['orders'])\n",
    "  else:\n",
    "    orders = []\n",
    "\n",
    "  data = BigApi.adminGet(f\"/admin/shop/shopify_orders\" + ('' if cursor == '' else '?cursor=' + cursor))\n",
    "  orders.extend(data['orders'])\n",
    "\n",
    "  for order in orders:\n",
    "    # debugOut = order #debug\n",
    "    # break #debug\n",
    "    isForcedOrder = 'force' in order\n",
    "    inDateRange = isPartOfQ3(order)\n",
    "    if not isLateEnough(order) and not isForcedOrder:\n",
    "      continue\n",
    "    if not inDateRange and not isForcedOrder:\n",
    "      break\n",
    "    ordersProcessed += 1\n",
    "    if(isForcedOrder or (orderValid(order) and orderExcludesTags(order, excludedTags) and 'bigOrder' in order and order['bigOrder']['ipd'] != -1)):\n",
    "      hasBeyond = False\n",
    "      items = order['shopifyOrder']['line_items']\n",
    "      for item in items:\n",
    "        if(item['name'] == 'Bigscreen Beyond - Measure Me'):\n",
    "          hasBeyond = True\n",
    "      if(hasBeyond):\n",
    "        baseOrderIpd = actualIpd(order['bigOrder']['ipd'])\n",
    "        orderIpdKey = f\"{baseOrderIpd}mm\"\n",
    "        if(isForcedOrder or (orderIpdKey in ipdQuotas and ipdCounts[orderIpdKey] < ipdQuotas[orderIpdKey])):\n",
    "\n",
    "          ordersToFulfill += 1\n",
    "          ipdCounts[orderIpdKey] += 1\n",
    "          shippingId = orderLib['latestShippingId'] + 1\n",
    "          orderLibId = getIndexByProp(orderLib['orderDetails'], 'orderName', order['shopifyOrder']['name'])\n",
    "          if(orderLibId == None):\n",
    "            newOrdersIndexed += 1\n",
    "            orderLib['latestShippingId'] = shippingId\n",
    "            orderLibId = len(orderLib['orderDetails'])\n",
    "            orderLib['orderDetails'].append({'orderName': order['shopifyOrder']['name'], 'shippingId': shippingId})\n",
    "          else:\n",
    "            shippingId = orderLib['orderDetails'][orderLibId]['shippingId']\n",
    "          lensOrdersQty = getSiblingProp(lensInsertOrdersByEmail, order['shopifyOrder']['email'], 'email', 'lensOrdersQty')\n",
    "          if(lensOrdersQty == None):\n",
    "            lensOrdersQty = ''\n",
    "          orderIpd = \"CHECK IPD\" if orderIncludesTags(order, ['IPD Change Requested']) else f\"{order['bigOrder']['ipd']}\"\n",
    "          cushionInfo = getCushionInfo(order)\n",
    "          cushionInfo['shippingId'] = shippingId\n",
    "          cushionsInfo.append(cushionInfo)\n",
    "          csv.append([order['shopifyOrder']['created_at'], order['shopifyOrder']['email'], f\"{shippingId}\", order['shopifyOrder']['billing_address']['name'], order['shopifyOrder']['name'], orderIpd, f\"https://main-ocean-arda.bigscreencloud.com/shop/order/{order['bigOrder']['id']}\", f\"{lensOrdersQty}\"])\n",
    "          \n",
    "    elif('bigOrder' not in order or order['bigOrder']['ipd'] == -1):\n",
    "      skipCount += 1\n",
    "    if(ordersToFulfill >= maximumOrders):\n",
    "      break\n",
    "    if(ordersProcessed % 25 == 0):\n",
    "      print(f\"{ordersProcessed} total orders processed...\")\n",
    "    if(ordersProcessed % 300 == 0):\n",
    "      BigApi.adminLogin()\n",
    "\n",
    "  # Write the modified dictionary back to the JSON file\n",
    "  with open(orderLibFile, 'w') as file:\n",
    "      json.dump(orderLib, file, indent=4)  # indent=4 for pretty printing\n",
    "\n",
    "  # break if no more nextCursor\n",
    "  if(ordersToFulfill >= maximumOrders or 'nextCursor' not in data or not inDateRange):\n",
    "    print(f\"### Finished iterating through {ordersProcessed} total orders ###\")\n",
    "    print(f\"{len(orderLib['orderDetails'])} total Beyond orders have permanently indexed shipping IDs\")\n",
    "    print(f\"During this run, {newOrdersIndexed} new Beyond orders have been assigned shipping IDs\")\n",
    "    print(f\"The latest shipping ID is now {orderLib['latestShippingId']}\")\n",
    "    print(f'The order library file has been updated: \"{orderLibFile}\"')\n",
    "    print(\"--------------------------------------------------\")\n",
    "    print(f\"CSV contains a grand total of {len(csv) - 1} Beyond orders\")\n",
    "    print(f\"{skipCount} orders have been skipped due to missing IPD (no face scan done)\")\n",
    "    print(f\"IPD counts: {ipdCounts}\")\n",
    "    break\n",
    "  else:\n",
    "    cursor = data['nextCursor']\n",
    "\n",
    "# for row in csv:\n",
    "#   email = row[4]\n",
    "#   orderName = getBeyondOrderName(beyondOrdersByEmail, email)\n",
    "#   row.append(orderName)\n",
    "\n",
    "# debugOut #debug\n",
    "# csv #debug\n",
    "# data #debug"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 5. Sort csv by shipping ID ascending + print markdown for discord\n",
    "fabJobUrl = 'https://main-ocean-arda.bigscreencloud.com/fabricator/job/'\n",
    "shopifyAdminUrl = 'https://admin.shopify.com/store/bigscreenvr/orders/'\n",
    "\n",
    "sortedCsv = sorted(csv[1:], key=lambda x: int(x[2]))\n",
    "sortedCushionsInfo = sorted(cushionsInfo, key=lambda x: x['shippingId'])\n",
    "rxInsertPrepick = [[\n",
    "  'SID',\n",
    "  'Name',\n",
    "  'Email',\n",
    "  'Beyond Order ID',\n",
    "  'Job Status',\n",
    "  'Cushion Index',\n",
    "  'Cushion IPD',\n",
    "  'Job ID',\n",
    "  'Job URL',\n",
    "  'BigOrder URL',\n",
    "]]\n",
    "\n",
    "for i, row in enumerate(sortedCsv):\n",
    "  lensInsertString = ''\n",
    "  cushionInfo = sortedCushionsInfo[i]\n",
    "  infoString = ''\n",
    "  if(row[7] != ''):\n",
    "    lensInsertString = f\" **:3DGlasses:x{row[7]}**\"\n",
    "    rxInsertPrepick.append([\n",
    "      f\"{row[2]}\", # SID\n",
    "      f\"{row[3]}\", # Name\n",
    "      f\"{row[1]}\", # Email\n",
    "      f\"{row[4]}\", # Beyond Order ID\n",
    "      f\"{cushionInfo['jobState']}\", # Job Status\n",
    "      f\"{cushionInfo['cushionIndex']}\", # Cushion Index\n",
    "      f\"{cushionInfo['originalIpd']}mm\", # Cushion IPD\n",
    "      f\"{cushionInfo['jobId']}\", # Job ID\n",
    "      f\"{fabJobUrl}{cushionInfo['jobId']}\", # Job URL\n",
    "      f\"{row[6].replace('melon', 'ocean')}\", # BigOrder URL\n",
    "    ])\n",
    "    # vvv ADDS TIME. uncomment only if you need lens order URLs vvv\n",
    "    # lensOrderId = getSiblingProp(lensInsertOrdersByEmail, row[1], 'email', 'orderName')\n",
    "    # data = BigApi.adminGet(f\"/admin/shop/shopify_orders?search={urllib.parse.quote(lensOrderId)}\")\n",
    "    # if(len(data['orders']) > 0):\n",
    "    #   lensOrderUrl = f\"{shopifyAdminUrl}{data['orders'][0]['shopifyOrder']['id']}\"\n",
    "    #   lensInsertString = f\" **:3DGlasses:[x{row[7]}]({lensOrderUrl})**\"\n",
    "  else: # to exclude prescription lens-less orders\n",
    "    continue\n",
    "  if(cushionInfo['jobState'] == 'AwaitingFulfilment'):\n",
    "    infoString = (f\" ---> **{cushionInfo['cushionIndex']}**\")\n",
    "    infoString += f\".....verify: ||**{cushionInfo['originalIpd']}mm, \\\"{cushionInfo['jobId'][-4:]}\\\"**||\"\n",
    "  ## DEBUG - surface verification failed jobs for Philip ##\n",
    "  # if(cushionInfo['jobState'] == 'ScanVerificationFailed'):\n",
    "  #   print(cushionInfo['jobId'])\n",
    "  ## END DEBUG ##\n",
    "  print(f\"- [[**{row[2]}**]]({row[6]}) **{row[3]}** 《{row[5]}mm》 ||`{row[4]}`||{lensInsertString}\")\n",
    "  print(f\" - *`Cushion is` [{cushionInfo['jobState']}]({fabJobUrl}{cushionInfo['jobId']}){infoString}*\")\n",
    "\n",
    "# add header row back to sorted csv\n",
    "sortedCsv.insert(0, csv[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 6. Print in proper CSV format --END\n",
    "\n",
    "# for row in sortedCsv:\n",
    "#   print('\"' + '\",\"'.join(row), end='\"\\n')\n",
    "\n",
    "for row in rxInsertPrepick:\n",
    "  print('\"' + '\",\"'.join(row), end='\"\\n')\n",
    "\n",
    "# debugOut #debug\n",
    "# moddedCsv #debug\n",
    "# beyondOrdersByEmail #debug\n",
    "# data #debug"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# [decommissioned] Append \"Needs Rx Inserts?\" value to every csv row\n",
    "\n",
    "def customerOrderedRxInserts(email):\n",
    "  data = BigApi.adminGet(f\"/admin/shop/shopify_orders?search={email}\")\n",
    "  orders = data['orders']\n",
    "\n",
    "  for order in orders:\n",
    "      for item in order['shopifyOrder']['line_items']:\n",
    "        if(item['name'] == 'Prescription Lenses'):\n",
    "          return True\n",
    "  return False\n",
    "\n",
    "for row in csv:\n",
    "  moddedRow = row.copy()\n",
    "  email = moddedRow[1]\n",
    "  if('Email' not in email):\n",
    "    value = 'x' if customerOrderedRxInserts(email) else ''\n",
    "    moddedRow.append(value)\n",
    "  print('\"' + '\",\"'.join(moddedRow), end='\"\\n')\n",
    "\n",
    "# debugOut #debug\n",
    "# moddedCsv #debug\n",
    "# beyondOrdersByEmail #debug\n",
    "# data #debug"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Print IPD counts as CSV rows\n",
    "\n",
    "for value in ipdCounts.values():\n",
    "  print(f'\"{value}\"', end='\\n')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# [arbitrary] merge csv files on matched column values\n",
    "import pandas as pd\n",
    "\n",
    "bob_df = pd.read_csv(\"C:/Users/decid/Documents/Bigscreen/DVT-2_VIDs_CSV_Final.csv\", header=None, names=['A'])\n",
    "fred_df = pd.read_csv(\"C:/Users/decid/Documents/Bigscreen/DVT-2_VIDs_CSV.csv\", header=None, names=['A', 'B'])\n",
    "\n",
    "# Merging the two DataFrames on column 'A'\n",
    "merged_df = bob_df.merge(fred_df, on='A', how='left')\n",
    "\n",
    "# Saving the merged DataFrame to a new CSV file\n",
    "merged_df.to_csv('merged.csv', index=False)\n",
    "\n",
    "print(\"Data merged and saved to 'merged.csv'\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Load lens insert order CSV\n",
    "import pandas as pd\n",
    "\n",
    "van_df = pd.read_csv(\"C:/Users/decid/Documents/Bigscreen/20230918_lens_insert_order_batch_2.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Check csv for cancelled orders\n",
    "ordersChecked = 0\n",
    "\n",
    "for orderName in van_df['ID']:\n",
    "  # print(f\"[DEBUG] Search url: {adminApiUrl}/admin/shop/shopify_orders?limit=5&search=%23{orderName[1:]}\") #debug\n",
    "  data = BigApi.adminGet(f\"/admin/shop/shopify_orders?limit=5&search=%23{orderName[1:]}\")\n",
    "\n",
    "  order = data['orders'][0]['shopifyOrder']\n",
    "  ordersChecked += 1\n",
    "\n",
    "  # print(order['cancel_reason']) #debug\n",
    "  if order['cancel_reason'] != None:\n",
    "    print(f\"Order {order['name']} is cancelled\")\n",
    "  else:\n",
    "    continue\n",
    "    # print(f\"Order {order['name']} is not cancelled\")\n",
    "\n",
    "print(f\"Cancellation checks complete. {ordersChecked} orders processed.\")"
   ]
  }
 ],
 "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
}
