{
 "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": [
    "orders = BigApi.adminGet(f\"/admin/shop/orders?origin=Shopify\")\n",
    "print(len(orders[\"items\"]))\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "customer_locales = {}\n",
    "\n",
    "for order in orders[\"items\"]:\n",
    "    if order[\"shopifyOrderSnapshot\"] is None:\n",
    "        continue\n",
    "    locale = order[\"shopifyOrderSnapshot\"].get(\"customer_locale\", \"\")\n",
    "    if locale is None:\n",
    "        continue\n",
    "    if \"JA\" in locale or \"JP\" in locale:\n",
    "        #print(order[\"shopifyOrderSnapshot\"][\"customer_locale\"])\n",
    "        print(order[\"shopifyOrderSnapshot\"])\n",
    "    # if order[\"shopifyOrderSnapshot\"][\"customer_locale\"] contains the locale \"JA\" or \"JP\" then print it out\n",
    "    #if order[\"shopifyOrderSnapshot\"][\"customer_locale\"] == \"ja-JP\":\n",
    "        #print(order[\"shopifyOrderSnapshot\"][\"customer_locale\"])\n",
    "    #if order[\"shopifyOrderSnapshot\"][\"customer_locale\"] == \"en-JP\":\n",
    "        #print(order[\"shopifyOrderSnapshot\"][\"customer_locale\"])\n",
    "    #if  order[\"shopifyOrderSnapshot\"][\"customer_locale\"] == \"ja-JP\":\n",
    "        #print(order[\"shopifyOrderSnapshot\"][\"customer_locale\"])\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Go through all orders and find any with two orders per customer, where customer is defined by the ownerBigscreenAccountId\n",
    "\n",
    "duplicateOrders = []\n",
    "weirdDuplicateOrders = []\n",
    "otherDuplicateOrders = []\n",
    "\n",
    "for order1 in orders[\"items\"]:\n",
    "    for order2 in orders[\"items\"]:\n",
    "        if order1[\"id\"] == order2[\"id\"]:\n",
    "            continue\n",
    "\n",
    "        #if order1[\"shopifyOrderId\"] != order2[\"shopifyOrderId\"]:\n",
    "        #    continue\n",
    "\n",
    "        # Go through \"currentLineItems\" and append all the ids to a string\n",
    "        order1LineItems = \"\"\n",
    "        if len(order1[\"currentLineItems\"]) == 0 and len(order2[\"currentLineItems\"]) == 0:\n",
    "            continue\n",
    "\n",
    "        for lineItem in order1[\"currentLineItems\"]:\n",
    "            if \"shopifyProductId\" not in lineItem:\n",
    "                continue\n",
    "            order1LineItems += str(lineItem[\"shopifyProductId\"])\n",
    "        order2LineItems = \"\"\n",
    "        for lineItem in order2[\"currentLineItems\"]:\n",
    "            if \"shopifyProductId\" not in lineItem:\n",
    "                continue\n",
    "            order2LineItems += str(lineItem[\"shopifyProductId\"])\n",
    "\n",
    "        # If the line items are the same, then we have a duplicate order\n",
    "        if order1LineItems == order2LineItems:\n",
    "            orderId1 = order1[\"id\"]\n",
    "            orderId2 = order2[\"id\"]\n",
    "            if order1[\"ownerBigscreenAccountId\"] == order2[\"ownerBigscreenAccountId\"]:\n",
    "                duplicateOrders.append([order1[\"state\"], f\"https://main-ocean-arda.bigscreencloud.com/shop/order/{orderId1}\", order2[\"state\"], f\"https://main-ocean-arda.bigscreencloud.com/shop/order/{orderId2}\"])\n",
    "            #else:\n",
    "                #weirdDuplicateOrders.append([order1[\"state\"], f\"https://main-ocean-arda.bigscreencloud.com/shop/order/{orderId1}\", order2[\"state\"], f\"https://main-ocean-arda.bigscreencloud.com/shop/order/{orderId2}\"])\n",
    "\n",
    "            #print(order1)\n",
    "            #if order1[\"state\"] == \"Shipped\" and order2[\"state\"] == \"Shipped\":\n",
    "            #    continue\n",
    "            #if order1[\"state\"] != \"Shipped\" and order2[\"state\"] != \"Shipped\":\n",
    "            #    orderId1 = order1[\"id\"]\n",
    "            #    orderId2 = order2[\"id\"]\n",
    "            #    duplicateOrders.append([order1[\"state\"], f\"https://main-ocean-arda.bigscreencloud.com/shop/order/{orderId1}\", order2[\"state\"], f\"https://main-ocean-arda.bigscreencloud.com/shop/order/{orderId2}\"])\n",
    "            #    break\n",
    "\n",
    "            #otherDuplicateOrders.append([order1[\"state\"], f\"https://main-ocean-arda.bigscreencloud.com/shop/order/{orderId1}\", order2[\"state\"], f\"https://main-ocean-arda.bigscreencloud.com/shop/order/{orderId2}\"])\n",
    "\n",
    "print(\"Other duplicate orders\")\n",
    "print(len(duplicateOrders))\n",
    "print(len(weirdDuplicateOrders))\n",
    "print(len(otherDuplicateOrders))\n",
    "\n",
    "#for order in duplicateOrders:\n",
    "    #print(order)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for order in duplicateOrders:\n",
    "    print(order)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#print(weirdDuplicateOrders[0])"
   ]
  }
 ],
 "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
}
