2023-08-02 19:42:43 +02:00
|
|
|
from pathlib import Path
|
2023-08-03 09:29:11 +02:00
|
|
|
import datetime
|
|
|
|
|
from typing import List, Dict
|
2023-08-03 09:42:22 +02:00
|
|
|
import logging
|
2023-08-02 19:42:43 +02:00
|
|
|
|
|
|
|
|
from dotenv import dotenv_values
|
|
|
|
|
from woocommerce import API
|
|
|
|
|
|
2023-08-03 14:55:41 +02:00
|
|
|
from camisatoshi_wordpress_reports.order import Order
|
|
|
|
|
|
2023-08-02 19:42:43 +02:00
|
|
|
API_CONFIG = dotenv_values(
|
|
|
|
|
dotenv_path=Path.home() / Path(".camisatoshi-wordpress-reports/.env")
|
|
|
|
|
)
|
2023-08-02 20:09:28 +02:00
|
|
|
WC_API = API(
|
|
|
|
|
url=API_CONFIG["URL"],
|
|
|
|
|
consumer_key=API_CONFIG["CONSUMER_KEY"],
|
|
|
|
|
consumer_secret=API_CONFIG["CONSUMER_SECRET"],
|
|
|
|
|
version=API_CONFIG["VERSION"],
|
|
|
|
|
)
|
2023-08-02 19:56:59 +02:00
|
|
|
|
2023-08-03 09:42:22 +02:00
|
|
|
logging.basicConfig(level=logging.INFO)
|
|
|
|
|
logger = logging.getLogger()
|
|
|
|
|
|
2023-08-02 19:56:59 +02:00
|
|
|
|
2023-08-02 19:49:36 +02:00
|
|
|
def check_health():
|
2023-08-03 09:42:22 +02:00
|
|
|
logger.info(
|
2023-08-03 14:34:49 +02:00
|
|
|
f"Connecting to the configured WooCommerce at {API_CONFIG['URL']}"
|
2023-08-03 09:42:22 +02:00
|
|
|
)
|
2023-08-02 19:56:59 +02:00
|
|
|
|
|
|
|
|
try:
|
2023-08-02 20:09:28 +02:00
|
|
|
api_reported_version = WC_API.get("").json()["namespace"]
|
2023-08-02 19:56:59 +02:00
|
|
|
except:
|
|
|
|
|
raise ConnectionError(
|
2023-08-03 14:34:49 +02:00
|
|
|
"There was an issue connecting to the WooCommerce API."
|
2023-08-02 19:56:59 +02:00
|
|
|
)
|
|
|
|
|
|
2023-08-03 09:42:22 +02:00
|
|
|
logger.info(f"Informed version of the API: {API_CONFIG['VERSION']}")
|
|
|
|
|
logger.info(f"Version reported by the API itself: {api_reported_version}")
|
2023-08-02 19:56:59 +02:00
|
|
|
|
2023-08-03 09:42:22 +02:00
|
|
|
logger.info("Connection successful. The API is reachable.")
|
2023-08-02 20:09:28 +02:00
|
|
|
|
|
|
|
|
|
2023-08-03 09:29:11 +02:00
|
|
|
def generate_um_report(start_date: str, end_date: str):
|
2023-08-03 09:42:22 +02:00
|
|
|
start_date = datetime.datetime.strptime(start_date, "%Y-%m-%d").isoformat()
|
|
|
|
|
end_date = datetime.datetime.strptime(end_date, "%Y-%m-%d").isoformat()
|
|
|
|
|
|
|
|
|
|
logger.info(f"Fetching orders between {start_date} and {end_date}.")
|
|
|
|
|
|
2023-08-03 09:29:11 +02:00
|
|
|
orders_in_date_range = WC_API.get(
|
|
|
|
|
endpoint="orders",
|
|
|
|
|
params={
|
2023-08-03 09:42:22 +02:00
|
|
|
"after": start_date,
|
|
|
|
|
"before": end_date,
|
2023-08-03 09:29:11 +02:00
|
|
|
"per_page": 100,
|
|
|
|
|
"status": "processing,completed",
|
|
|
|
|
},
|
2023-08-03 09:42:22 +02:00
|
|
|
).json()
|
|
|
|
|
|
2023-08-03 14:55:41 +02:00
|
|
|
orders_in_date_range = [Order.from_api_response(order_raw_data) for order_raw_data in orders_in_date_range]
|
|
|
|
|
|
2023-08-03 09:42:22 +02:00
|
|
|
logger.info(f"Received {len(orders_in_date_range)} orders.")
|
|
|
|
|
|
|
|
|
|
skus_to_keep = ["TEE-05-BBO-BLACK"]
|
|
|
|
|
|
|
|
|
|
logger.info(f"Filtering by SKUs: {skus_to_keep}")
|
|
|
|
|
relevant_orders = filter_orders_by_sku(
|
|
|
|
|
orders_in_date_range, skus=skus_to_keep
|
2023-08-02 20:09:28 +02:00
|
|
|
)
|
2023-08-03 09:42:22 +02:00
|
|
|
logger.info(f"Kept {len(relevant_orders)} orders.")
|
|
|
|
|
|
2023-08-03 14:50:39 +02:00
|
|
|
logger.info("Checking if all orders have the sats_received entry filled in.")
|
|
|
|
|
orders_without_sats_received = find_orders_without_sats_received(relevant_orders)
|
|
|
|
|
|
|
|
|
|
if orders_without_sats_received:
|
|
|
|
|
logger.warning(
|
|
|
|
|
f"There are {len(orders_without_sats_received)} orders without a properly filled sats_received entry.")
|
|
|
|
|
logger.warning(f"See details below.")
|
|
|
|
|
logger.warning(orders_without_sats_received)
|
|
|
|
|
raise ValueError("Not all orders have sats_received. Can't compute sats owed without that.")
|
|
|
|
|
|
2023-08-03 09:42:22 +02:00
|
|
|
# Fetch orders:
|
|
|
|
|
# - Between specific dates
|
|
|
|
|
# - That contain the hardcoded products
|
|
|
|
|
# - That have been paid, hence status is either processing or completed
|
|
|
|
|
# - That have not been settled yet (is_settled_with_um: 0)
|
|
|
|
|
|
|
|
|
|
# Print to screen:
|
|
|
|
|
# - Orders that do not have the `sats_received` metadata informed
|
|
|
|
|
# - The unit count for each product
|
|
|
|
|
# - The sales sum for each product
|
|
|
|
|
# - The sats sum for each product
|
|
|
|
|
# - The corresponding payment owed to UM
|
|
|
|
|
# - The list of order ids that have been taken into account
|
|
|
|
|
|
|
|
|
|
# Update orders:
|
|
|
|
|
# - Add metadata entry: is_settled_with_um: 1
|
|
|
|
|
|
|
|
|
|
|
2023-08-03 14:58:26 +02:00
|
|
|
def filter_orders_by_sku(orders: List[Order], skus: List[str]) -> List[Order]:
|
2023-08-03 09:42:22 +02:00
|
|
|
filtered_orders = []
|
|
|
|
|
|
|
|
|
|
for order in orders:
|
2023-08-03 14:58:26 +02:00
|
|
|
for sku in skus:
|
|
|
|
|
if order.contains_sku(sku):
|
2023-08-03 09:42:22 +02:00
|
|
|
filtered_orders.append(order)
|
|
|
|
|
|
|
|
|
|
return filtered_orders
|
2023-08-03 14:50:39 +02:00
|
|
|
|
|
|
|
|
|
2023-08-03 15:01:27 +02:00
|
|
|
def find_orders_without_sats_received(orders: List[Order]) -> List[Order]:
|
2023-08-03 14:50:39 +02:00
|
|
|
orders_without_sats_received = []
|
|
|
|
|
|
|
|
|
|
for order in orders:
|
2023-08-03 15:01:27 +02:00
|
|
|
if not order.contains_meta_data_entry("sats_received"):
|
2023-08-03 14:50:39 +02:00
|
|
|
orders_without_sats_received.append(order)
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
return orders_without_sats_received
|