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 16:50:40 +02:00
|
|
|
from camisatoshi_wordpress_reports.order import Order, Orders
|
2023-08-03 16:36:27 +02:00
|
|
|
from camisatoshi_wordpress_reports.constants import custom_meta_data_keys
|
2023-08-03 14:55:41 +02:00
|
|
|
|
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 16:50:40 +02:00
|
|
|
logger.info(f"Connecting to the configured WooCommerce at {API_CONFIG['URL']}")
|
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:
|
2023-08-03 16:50:40 +02:00
|
|
|
raise ConnectionError("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 16:50:40 +02:00
|
|
|
def generate_um_report(
|
|
|
|
|
start_date: datetime.datetime, end_date: datetime.datetime
|
|
|
|
|
) -> None:
|
2023-08-03 09:42:22 +02:00
|
|
|
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 16:10:30 +02:00
|
|
|
"after": start_date.isoformat(),
|
|
|
|
|
"before": end_date.isoformat(),
|
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 16:50:40 +02:00
|
|
|
orders_in_date_range = Orders(
|
|
|
|
|
[
|
|
|
|
|
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}")
|
2023-08-03 16:50:40 +02:00
|
|
|
relevant_orders = orders_in_date_range.filter_orders_by_skus(skus=skus_to_keep)
|
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.")
|
2023-08-03 16:50:40 +02:00
|
|
|
orders_without_sats_received = relevant_orders.filter_orders_without_sats_received()
|
2023-08-03 14:50:39 +02:00
|
|
|
if orders_without_sats_received:
|
|
|
|
|
logger.warning(
|
2023-08-03 16:50:40 +02:00
|
|
|
f"There are {len(orders_without_sats_received)} orders without a properly filled sats_received entry."
|
|
|
|
|
)
|
2023-08-03 14:50:39 +02:00
|
|
|
logger.warning(f"See details below.")
|
|
|
|
|
logger.warning(orders_without_sats_received)
|
2023-08-03 16:50:40 +02:00
|
|
|
raise ValueError(
|
|
|
|
|
"Not all orders have sats_received. Can't compute sats owed without that."
|
|
|
|
|
)
|
2023-08-03 16:01:34 +02:00
|
|
|
logger.info("Success, all orders have sats_received filled in.")
|
2023-08-03 16:22:20 +02:00
|
|
|
|
|
|
|
|
logger.info("Removing settled orders.")
|
2023-08-03 16:50:40 +02:00
|
|
|
unsettled_orders = relevant_orders.filter_unsettled_orders()
|
2023-08-03 16:22:20 +02:00
|
|
|
logger.info(f"Kept {len(unsettled_orders)} unsettled orders.")
|
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
|