Created
January 21, 2025 17:27
-
-
Save tbbooher/31b165aa2ba664a4a9498c38e1c61840 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import psycopg2 | |
import csv | |
from dotenv import load_dotenv | |
import os | |
from datetime import datetime | |
# Load environment variables | |
load_dotenv('../.env') | |
# Database connection parameters | |
conn_params = { | |
"host": os.getenv("DATABASE_HOST"), | |
"port": os.getenv("LOCAL_DATABASE_PORT"), | |
"database": "amzn", | |
"user": os.getenv("DATABASE_USER"), | |
"password": os.getenv("DATABASE_PASSWORD") | |
} | |
# Path to the CSV file | |
csv_file_path = "/Users/tim/code/amzn_dwn/amazon-invoice-downloader/downloads/2024/transactions-2024.csv" | |
# Helper function to convert date strings to datetime objects | |
def convert_to_date(value): | |
try: | |
return datetime.strptime(value, '%Y-%m-%d') | |
except ValueError: | |
return None | |
def clean_amount(amount_str): | |
if amount_str: | |
return float(amount_str.replace('$', '').replace(',', '').strip()) | |
return None | |
# Main import logic | |
try: | |
conn = psycopg2.connect(**conn_params) | |
conn.autocommit = True # Enable autocommit | |
print("Database connection successful") | |
cur = conn.cursor() | |
# Define the table structure | |
create_table_query = """ | |
CREATE TABLE IF NOT EXISTS invoice_data ( | |
date DATE, | |
amount NUMERIC, | |
card_type VARCHAR(50), | |
last_4_digits VARCHAR(4), | |
order_id VARCHAR(20), | |
order_type VARCHAR(50) | |
); | |
""" | |
cur.execute(create_table_query) | |
with open(csv_file_path, 'r', newline='', encoding='utf-8') as f: | |
reader = csv.DictReader(f) | |
for row_number, row in enumerate(reader, start=1): | |
# Convert and clean data | |
date = convert_to_date(row['Date']) | |
amount = clean_amount(row['Amount']) | |
card_type = row['Card Type'] | |
last_4_digits = row['Last 4 Digits'] | |
order_id = row['Order ID'] | |
order_type = row['Order Type'] | |
# Insert into the database | |
try: | |
cur.execute(""" | |
INSERT INTO invoice_data ( | |
date, amount, card_type, last_4_digits, order_id, order_type | |
) VALUES (%s, %s, %s, %s, %s, %s) | |
""", (date, amount, card_type, last_4_digits, order_id, order_type)) | |
except Exception as e: | |
print(f"Row {row_number} insertion error: {e}") | |
except Exception as e: | |
print(f"Error connecting to the database: {e}") | |
finally: | |
if 'cur' in locals() and cur: | |
cur.close() | |
if 'conn' in locals() and conn: | |
conn.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE TABLE public.invoice_data ( | |
date date, | |
amount numeric, | |
card_type character varying(50), | |
last_4_digits character varying(4), | |
order_id character varying(20), | |
order_type character varying(50) | |
); | |
ALTER TABLE public.invoice_data OWNER TO tim; | |
CREATE VIEW public.all_invoice_order_data AS | |
SELECT o.order_id, | |
(o.ship_date)::date AS ship_date, | |
sum(o.shipment_item_subtotal) AS total_shipment_item_subtotal, | |
sum(o.shipment_item_subtotal_tax) AS total_shipment_item_subtotal_tax, | |
sum(o.shipping_charge) AS total_shipping, | |
sum(o.total_owed) AS total_charge, | |
string_agg(o.product_name, ' | '::text) AS concatenated_product_names, | |
i.amount AS invoice_amount, | |
i.date AS invoice_date | |
FROM (public.orders o | |
LEFT JOIN public.invoice_data i ON ((o.order_id = (i.order_id)::text))) | |
WHERE ((o.order_status = 'Closed'::text) AND (((o.ship_date)::date >= '2024-01-01'::date) AND ((o.ship_date)::date <= '2024-12-31'::date))) | |
GROUP BY o.order_id, ((o.ship_date)::date), i.amount, i.date | |
ORDER BY (sum(o.total_owed)) DESC; | |
ALTER VIEW public.all_invoice_order_data OWNER TO tim; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment