-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclockify_api_invoice.py
More file actions
53 lines (42 loc) · 1.52 KB
/
Copy pathclockify_api_invoice.py
File metadata and controls
53 lines (42 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from dotenv import load_dotenv
import os
from clockify_sdk import Clockify
from datetime import datetime, timedelta
import pandas as pd
# Load environment variables from .env file
load_dotenv()
rate_per_hour = 70 # euros per hour
# Initialize the client
client = Clockify(
api_key=os.getenv("CLOCKIFY_API_KEY"),
# workspace_id=os.getenv("CLOCKIFY_WORKSPACE_ID") # Optional
)
# Get time entries for a specific date range
time_entries = client.time_entries.get_by_user_id(
user_id=client.user_id,
# 7 days ago
start=datetime.now().replace(hour=0, minute=0, second=0, microsecond=0) - timedelta(days=7),
end=datetime.now()
)
# Store invoice data
invoice_data = []
# Print the time entries
for entry in time_entries:
print(f"Time Entry: {entry['timeInterval']['start']} - {entry['timeInterval']['end']}")
x = entry['timeInterval']['start']
y = entry['timeInterval']['end']
# making start time TO datetime object !!!!
x_time_object = datetime.fromisoformat(x)
y_time_object = datetime.fromisoformat(y)
billable_duration = y_time_object - x_time_object
total_billable_hours = billable_duration.total_seconds() / 3600
final_invoice_amount = total_billable_hours * rate_per_hour
print(f"Bill this amount {final_invoice_amount:.2f} euros")
# Add to invoice data
invoice_data.append({
'date': x_time_object.date(),
'invoice_amount': round(final_invoice_amount, 2)
})
# Save to CSV
df = pd.DataFrame(invoice_data)
df.to_csv("invoice.csv", index=False)