-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinvoice_automation.py
More file actions
1312 lines (1105 loc) · 61.3 KB
/
Copy pathinvoice_automation.py
File metadata and controls
1312 lines (1105 loc) · 61.3 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""
Stripe Customer Meeting Invoice Automation Script
This script:
1. Fetches customer list from Stripe
2. Checks Google Calendar for recent meetings with those customers
3. Shows interactive interface to select meetings to invoice
4. Tracks which meetings have already been invoiced
5. Creates draft invoices for selected meetings with custom synopses
Required packages:
pip install stripe google-auth google-auth-oauthlib google-auth-httplib2 google-api-python-client python-dateutil
Setup Instructions:
1. Set customer hourly rates in Stripe:
- Go to Stripe Dashboard > Customers > [Customer] > Edit
- Add metadata: key="hourly_rate", value="200.00"
- Or use the set_customer_hourly_rate() method in this script
2. For customers without a specific rate, the DEFAULT_HOURLY_RATE will be used
Example usage to set rates:
invoicer = StripeCalendarInvoicer(stripe_api_key="sk_...")
invoicer.set_customer_hourly_rate("cus_ABC123", 200.00) # $200/hour for premium client
invoicer.set_customer_hourly_rate("cus_DEF456", 125.00) # $125/hour for standard client
"""
import stripe
import os
import argparse
import re
from datetime import datetime, timedelta
from dateutil import parser
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
import json
import logging
import hashlib
from dotenv import load_dotenv
# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
class StripeCalendarInvoicer:
def __init__(self, stripe_api_key, calendar_credentials_file='credentials.json',
token_file='token.json', days_back=7):
"""
Initialize the invoicing automation
Args:
stripe_api_key: Your Stripe secret API key
calendar_credentials_file: Path to Google Calendar API credentials JSON
token_file: Path to store Google OAuth token
days_back: Number of days back to check for meetings
"""
self.stripe_api_key = stripe_api_key
self.calendar_credentials_file = calendar_credentials_file
self.token_file = token_file
self.days_back = days_back
self.calendar_scopes = ['https://www.googleapis.com/auth/calendar.readonly']
# Initialize Stripe
stripe.api_key = self.stripe_api_key
# Initialize Google Calendar service
self.calendar_service = self._get_calendar_service()
if not self.calendar_service:
raise Exception("Failed to initialize Google Calendar service. Please check your credentials and try again.")
def _get_calendar_service(self):
"""Authenticate and return Google Calendar service"""
creds = None
# Load existing token
if os.path.exists(self.token_file):
try:
creds = Credentials.from_authorized_user_file(self.token_file, self.calendar_scopes)
except Exception as e:
logger.warning(f"Error loading existing token: {e}")
creds = None
# If no valid credentials, get new ones
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
try:
creds.refresh(Request())
except Exception as e:
logger.error(f"Token refresh failed: {e}")
print(f"\n❌ Google Calendar token has expired and cannot be refreshed.")
print(f"This usually happens when the token has been expired for too long.")
print(f"Current token file: {self.token_file}")
while True:
choice = input("\nWould you like to remove the expired token and re-authenticate? (y/n): ").strip().lower()
if choice in ['y', 'yes']:
try:
os.remove(self.token_file)
print(f"✅ Removed expired token file: {self.token_file}")
print("🔄 Starting fresh authentication...")
creds = None
break
except Exception as remove_error:
logger.error(f"Error removing token file: {remove_error}")
print(f"❌ Could not remove token file. Please manually delete: {self.token_file}")
return None
elif choice in ['n', 'no']:
print("❌ Authentication cancelled. Cannot proceed without valid credentials.")
return None
else:
print("Please enter 'y' or 'n'")
# If we still don't have valid credentials, initiate fresh authentication
if not creds or not creds.valid:
try:
flow = InstalledAppFlow.from_client_secrets_file(
self.calendar_credentials_file, self.calendar_scopes)
creds = flow.run_local_server(port=0)
except Exception as e:
logger.error(f"Authentication failed: {e}")
print(f"\n❌ Google Calendar authentication failed.")
print(f"Please ensure your credentials.json file is valid and try again.")
return None
# Save credentials for next run
try:
with open(self.token_file, 'w') as token:
token.write(creds.to_json())
logger.info(f"✅ Saved new authentication token to: {self.token_file}")
except Exception as e:
logger.error(f"Error saving token file: {e}")
print(f"❌ Could not save authentication token. You may need to re-authenticate next time.")
try:
return build('calendar', 'v3', credentials=creds)
except Exception as e:
logger.error(f"Error building calendar service: {e}")
print(f"❌ Failed to initialize Google Calendar service.")
return None
def generate_meeting_id(self, customer_email, start_time, summary):
"""Generate a unique identifier for a meeting"""
# Create a hash from customer email, start time, and summary
meeting_string = f"{customer_email}|{start_time}|{summary}"
return hashlib.md5(meeting_string.encode()).hexdigest()[:12]
def get_customer_invoices(self, customer_id):
"""Get all invoices for a customer"""
try:
invoices = stripe.Invoice.list(customer=customer_id, limit=100)
return invoices.data
except Exception as e:
logger.error(f"Error fetching invoices for customer {customer_id}: {e}")
return []
def check_meeting_invoice_status(self, customer_id, meeting_id):
"""Check if a meeting has been invoiced and return status"""
invoices = self.get_customer_invoices(customer_id)
for invoice in invoices:
# Check line items for this meeting ID - line items are in the lines property
if hasattr(invoice, 'lines') and invoice.lines:
for item in invoice.lines.data:
if meeting_id in (item.description or ""):
if invoice.status == 'draft':
return 'drafted'
elif invoice.status in ['open', 'paid', 'uncollectible']:
return 'sent'
return 'not_invoiced'
def set_customer_hourly_rate(self, customer_id, hourly_rate):
"""Set hourly rate for a specific customer in Stripe metadata"""
try:
stripe.Customer.modify(
customer_id,
metadata={'hourly_rate': str(hourly_rate)}
)
logger.info(f"Set hourly rate for customer {customer_id}: ${hourly_rate}/hour")
return True
except Exception as e:
logger.error(f"Error setting hourly rate for customer {customer_id}: {e}")
return False
def get_customer_hourly_rate(self, customer, default_rate):
"""Get hourly rate for a customer from metadata or use default"""
try:
# Check for hourly_rate in customer metadata
rate = customer.get('metadata', {}).get('hourly_rate')
if rate:
return float(rate)
# Fallback to default rate
logger.info(f"No hourly rate set for {customer.get('name', 'Unknown')}, using default: ${default_rate}")
return default_rate
except (ValueError, TypeError):
logger.warning(f"Invalid hourly rate in metadata for {customer.get('name', 'Unknown')}, using default: ${default_rate}")
return default_rate
def parse_time_input(self, time_str):
"""Parse various time formats into datetime object"""
if not time_str or time_str.strip() == "":
return None
time_str = time_str.strip()
# Try different time formats
formats = [
"%I:%M %p", # 2:30 PM
"%I:%M%p", # 2:30PM
"%H:%M", # 14:30
"%I %p", # 2 PM
"%I%p", # 2PM
"%H" # 14
]
for fmt in formats:
try:
return datetime.strptime(time_str, fmt).time()
except ValueError:
continue
raise ValueError(f"Unable to parse time: {time_str}")
def parse_duration_input(self, duration_str):
"""Parse duration input into hours (float)"""
if not duration_str or duration_str.strip() == "":
return None
duration_str = duration_str.strip().lower()
# Remove common suffixes (order matters - longer patterns first)
duration_str = duration_str.replace('hours', '').replace('hour', '').replace('hr', '').replace('h', '').strip()
try:
# Handle formats like "1.5", "2", "0.5"
duration = float(duration_str)
if duration <= 0 or duration > 24:
raise ValueError("Duration must be between 0 and 24 hours")
return duration
except ValueError as e:
# Re-raise our specific error messages
if "Duration must be between" in str(e):
raise
# Otherwise it's a parsing error
raise ValueError(f"Unable to parse duration: {duration_str}")
def validate_hourly_rate(self, rate_str):
"""Validate and parse hourly rate input"""
if not rate_str or rate_str.strip() == "":
return None
rate_str = rate_str.strip().replace('$', '').replace(',', '')
try:
rate = float(rate_str)
if rate <= 0 or rate > 10000:
raise ValueError("Rate must be between $0 and $10,000")
return rate
except ValueError as e:
# Re-raise our specific error messages
if "Rate must be between" in str(e):
raise
# Otherwise it's a parsing error
raise ValueError(f"Unable to parse rate: {rate_str}")
def extract_emails_from_text(self, text):
"""Extract email addresses from text using regex"""
if not text:
return set()
# Email regex pattern
email_pattern = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b'
emails = re.findall(email_pattern, text, re.IGNORECASE)
# Return lowercase set of unique emails
return set(email.lower() for email in emails)
def find_customer_mentions_in_text(self, text, customers):
"""Find customer names mentioned in text along with their emails"""
if not text:
return set()
found_emails = set()
text_lower = text.lower()
# Check for each customer's name and email in the text
for customer in customers:
customer_name = customer.get('name', '').lower()
customer_email = customer.get('email', '').lower()
# Skip if no name
if not customer_name or customer_name == 'unknown':
continue
# Check if both name and email appear close to each other
# Look for patterns like "Name email@domain.com" or "email@domain.com (Name)"
if customer_name in text_lower and customer_email in text_lower:
# Simple proximity check - if both exist in text, consider it a match
name_pos = text_lower.find(customer_name)
email_pos = text_lower.find(customer_email)
# If name and email are within 100 characters of each other
if abs(name_pos - email_pos) < 100:
found_emails.add(customer_email)
return found_emails
def search_customers(self, customers, query):
"""Search customers by name or email
Args:
customers: List of customer dictionaries
query: Search query string
Returns:
List of matching customers
"""
if not query:
return []
query_lower = query.lower()
matches = []
for customer in customers:
customer_email = customer.get('email', '').lower()
customer_name = customer.get('name', '').lower()
if query_lower in customer_email or query_lower in customer_name:
matches.append(customer)
return matches
def get_stripe_customers(self):
"""Fetch all customers from Stripe with email addresses"""
logger.info("Fetching Stripe customers...")
customers = []
try:
# Fetch customers in batches
has_more = True
starting_after = None
while has_more:
params = {'limit': 100}
if starting_after:
params['starting_after'] = starting_after
response = stripe.Customer.list(**params)
for customer in response.data:
if customer.email: # Only include customers with email addresses
customers.append({
'id': customer.id,
'email': customer.email.lower(),
'name': customer.name or 'Unknown',
'created': customer.created,
'metadata': customer.metadata or {}
})
has_more = response.has_more
if has_more:
starting_after = response.data[-1].id
logger.info(f"Found {len(customers)} customers with email addresses")
return customers
except Exception as e:
logger.error(f"Error fetching Stripe customers: {e}")
return []
def get_calendar_events(self, start_date, end_date):
"""Fetch calendar events within date range"""
logger.info(f"Fetching calendar events from {start_date} to {end_date}")
try:
events_result = self.calendar_service.events().list(
calendarId='primary',
timeMin=start_date.isoformat() + 'Z',
timeMax=end_date.isoformat() + 'Z',
singleEvents=True,
orderBy='startTime'
).execute()
events = events_result.get('items', [])
logger.info(f"Found {len(events)} calendar events")
return events
except HttpError as e:
logger.error(f"Error fetching calendar events: {e}")
return []
def calculate_meeting_duration(self, start_time, end_time):
"""Calculate meeting duration in hours"""
try:
start = parser.parse(start_time)
end = parser.parse(end_time)
duration = end - start
return round(duration.total_seconds() / 3600, 2) # Convert to hours
except:
return 1.0 # Default to 1 hour if calculation fails
def find_customers_with_meetings(self, customers, events, include_all_meetings=False):
"""Find customers who had meetings and return meeting details with invoice status
Args:
customers: List of Stripe customers
events: List of calendar events
include_all_meetings: If True, also return unassociated meetings
Returns:
Tuple of (customers_with_meetings, unassociated_meetings)
"""
customers_with_meetings = {}
unassociated_meetings = []
# Create a mapping of email to customer
customer_by_email = {customer['email']: customer for customer in customers}
for event in events:
# Extract meeting details
start_time = event.get('start', {}).get('dateTime', event.get('start', {}).get('date'))
end_time = event.get('end', {}).get('dateTime', event.get('end', {}).get('date'))
summary = event.get('summary', 'Meeting')
if not start_time or not end_time:
continue
# Calculate duration
duration = self.calculate_meeting_duration(start_time, end_time)
# Format date for display
try:
meeting_date = parser.parse(start_time).strftime('%Y-%m-%d')
meeting_time = parser.parse(start_time).strftime('%I:%M %p')
except:
meeting_date = start_time[:10] if len(start_time) >= 10 else start_time
meeting_time = "Unknown time"
# Check all attendees and organizer
participant_emails = set()
detection_sources = {} # Track where each email was found
# Check attendees
attendees = event.get('attendees', [])
for attendee in attendees:
if attendee.get('email'):
email = attendee['email'].lower()
participant_emails.add(email)
detection_sources[email] = 'attendee'
# Check organizer
organizer = event.get('organizer', {})
if organizer.get('email'):
email = organizer['email'].lower()
participant_emails.add(email)
detection_sources[email] = 'organizer'
# Check description for customer emails
description = event.get('description', '')
if description:
# Extract all emails from description
description_emails = self.extract_emails_from_text(description)
# Also check for customer name mentions
name_mention_emails = self.find_customer_mentions_in_text(description, customers)
# Add all found emails from description
for email in description_emails.union(name_mention_emails):
if email not in participant_emails: # Only add if not already found
participant_emails.add(email)
detection_sources[email] = 'description'
# Find matching customers
customer_found = False
for email in participant_emails:
if email in customer_by_email:
customer_found = True
customer = customer_by_email[email]
customer_id = customer['id']
# Generate unique meeting ID
meeting_id = self.generate_meeting_id(email, start_time, summary)
# Check invoice status
invoice_status = self.check_meeting_invoice_status(customer_id, meeting_id)
if customer_id not in customers_with_meetings:
customers_with_meetings[customer_id] = {
'customer': customer,
'meetings': []
}
# Add meeting details
meeting_info = {
'id': meeting_id,
'summary': summary,
'date': meeting_date,
'time': meeting_time,
'duration': duration,
'start_time': start_time,
'end_time': end_time,
'invoice_status': invoice_status,
'selected': invoice_status == 'not_invoiced', # Default selection
'synopsis': '', # Will be filled in during interactive session
# New fields for override functionality
'edited_start_time': None,
'edited_duration': None,
'custom_rate': None,
'is_edited': False,
'detection_source': detection_sources.get(email, 'unknown')
}
customers_with_meetings[customer_id]['meetings'].append(meeting_info)
# If no customer found and include_all_meetings is True, add to unassociated list
if not customer_found and include_all_meetings:
# Generate unique ID for unassociated meeting
meeting_id = self.generate_meeting_id('unassociated', start_time, summary)
unassociated_meeting = {
'id': meeting_id,
'summary': summary,
'date': meeting_date,
'time': meeting_time,
'duration': duration,
'start_time': start_time,
'end_time': end_time,
'attendees': list(participant_emails),
'description': description[:200] if description else '', # Truncate long descriptions
'selected': False,
'synopsis': '',
'assigned_customer': None, # Will be set during assignment
'is_manually_assigned': False
}
unassociated_meetings.append(unassociated_meeting)
# Log results
for customer_id, data in customers_with_meetings.items():
customer = data['customer']
meeting_count = len(data['meetings'])
logger.info(f"Found {meeting_count} meeting(s) for customer: {customer['name']} ({customer['email']})")
logger.info(f"Total customers with recent meetings: {len(customers_with_meetings)}")
if include_all_meetings:
logger.info(f"Total unassociated meetings: {len(unassociated_meetings)}")
return customers_with_meetings, unassociated_meetings
def edit_meeting_details(self, meeting, customer_data):
"""Interactive function to edit meeting start time and duration"""
print(f"\n📝 EDITING MEETING: {meeting['summary']}")
print(f"📅 Original: {meeting['date']} at {meeting['time']} ({meeting['duration']}h)")
if meeting['is_edited']:
try:
current_time = meeting['edited_start_time'].strftime("%I:%M %p") if meeting['edited_start_time'] else meeting['time']
except (AttributeError, ValueError):
current_time = meeting['time']
current_duration = meeting['edited_duration'] if meeting['edited_duration'] else meeting['duration']
print(f"📅 Current: {meeting['date']} at {current_time} ({current_duration}h)")
print("\nEnter new values (press Enter to keep current):")
# Edit start time
while True:
try:
try:
current_time_display = meeting['edited_start_time'].strftime("%I:%M %p") if meeting['edited_start_time'] else meeting['time']
except (AttributeError, ValueError):
current_time_display = meeting['time']
time_input = input(f"Start time [{current_time_display}]: ").strip()
if not time_input:
# Keep current time
break
elif time_input.lower() == 'original':
# Reset to original
meeting['edited_start_time'] = None
break
else:
# Parse new time
parsed_time = self.parse_time_input(time_input)
meeting['edited_start_time'] = parsed_time
break
except ValueError as e:
print(f"❌ {e}")
print("Examples: '2:30 PM', '14:30', '2 PM', or 'original' to reset")
# Edit duration
while True:
try:
current_duration = meeting['edited_duration'] if meeting['edited_duration'] else meeting['duration']
duration_input = input(f"Duration in hours [{current_duration}]: ").strip()
if not duration_input:
# Keep current duration
break
elif duration_input.lower() == 'original':
# Reset to original
meeting['edited_duration'] = None
break
else:
# Parse new duration
parsed_duration = self.parse_duration_input(duration_input)
meeting['edited_duration'] = parsed_duration
break
except ValueError as e:
print(f"❌ {e}")
print("Examples: '1.5', '2', '0.5', or 'original' to reset")
# Update is_edited flag
meeting['is_edited'] = (meeting['edited_start_time'] is not None or
meeting['edited_duration'] is not None)
# Show final result
try:
display_time = meeting['edited_start_time'].strftime("%I:%M %p") if meeting['edited_start_time'] else meeting['time']
except (AttributeError, ValueError):
display_time = meeting['time']
display_duration = meeting['edited_duration'] if meeting['edited_duration'] else meeting['duration']
print(f"\n✅ Meeting updated:")
print(f"📅 {meeting['date']} at {display_time} ({display_duration}h)")
if meeting['is_edited']:
print("✏️ This meeting has been edited")
return True
def display_meetings_interactive(self, customers_with_meetings, default_hourly_rate, unassociated_meetings=None, all_customers=None):
"""Interactive session to select meetings and enter synopses
Args:
customers_with_meetings: Dict of customers with their meetings
default_hourly_rate: Default hourly rate
unassociated_meetings: List of meetings not associated with any customer
all_customers: List of all customers (for assignment)
"""
if unassociated_meetings is None:
unassociated_meetings = []
def display_meeting_list():
"""Helper function to display the current meeting list with selection status"""
print("\n" + "="*80)
print("CUSTOMER MEETINGS - INVOICE SELECTION")
print("="*80)
# Display all meetings with status
meeting_index = 0
meeting_map = {} # Map index to (customer_id, meeting_index)
unassociated_map = {} # Map Ux index to unassociated meeting index
for customer_id, data in customers_with_meetings.items():
customer = data['customer']
hourly_rate = self.get_customer_hourly_rate(customer, default_hourly_rate)
print(f"\n📧 {customer['name']} ({customer['email']}) - ${hourly_rate}/hour")
print("-" * 60)
for i, meeting in enumerate(data['meetings']):
meeting_index += 1
meeting_map[meeting_index] = (customer_id, i)
# Status symbols
status_symbol = {
'not_invoiced': '⭕',
'drafted': '📄',
'sent': '✅'
}[meeting['invoice_status']]
status_text = {
'not_invoiced': 'Not invoiced',
'drafted': 'Draft created',
'sent': 'Invoice sent'
}[meeting['invoice_status']]
selected_symbol = '[✓]' if meeting['selected'] else '[ ]'
# Use edited values if available
display_duration = meeting['edited_duration'] if meeting['edited_duration'] is not None else meeting['duration']
try:
display_time = meeting['edited_start_time'].strftime("%I:%M %p") if meeting['edited_start_time'] else meeting['time']
except (AttributeError, ValueError):
display_time = meeting['time'] # Fallback to original time
# Use custom rate if available
rate_to_use = meeting['custom_rate'] if meeting['custom_rate'] is not None else hourly_rate
amount = display_duration * rate_to_use
# Build meeting title with indicators
meeting_title = f"{meeting['summary']}"
if meeting['is_edited']:
meeting_title += " ✏️"
if meeting['custom_rate'] is not None:
meeting_title += f" 💰${meeting['custom_rate']}/h"
if meeting.get('detection_source') == 'description':
meeting_title += " 📝"
if meeting.get('is_manually_assigned'):
meeting_title += " 🔗"
print(f"{meeting_index:2}. {selected_symbol} {status_symbol} {meeting_title}")
print(f" 📅 {meeting['date']} at {display_time} ({display_duration}h) - ${amount:.2f}")
# Show original values if meeting was edited
if meeting['is_edited']:
print(f" 📅 Original: {meeting['date']} at {meeting['time']} ({meeting['duration']}h)")
print(f" 📊 Status: {status_text}")
print()
# Display unassociated meetings if any
if unassociated_meetings:
print(f"\n🔍 UNASSOCIATED MEETINGS ({len(unassociated_meetings)} found)")
print("-" * 60)
for i, meeting in enumerate(unassociated_meetings):
unassociated_index = f"U{i+1}"
unassociated_map[unassociated_index] = i
selected_symbol = '[✓]' if meeting['selected'] else '[ ]'
# Show assigned customer if any
if meeting.get('assigned_customer'):
customer = meeting['assigned_customer']
print(f"{unassociated_index:3}. {selected_symbol} {meeting['summary']} 🔗")
print(f" 📅 {meeting['date']} at {meeting['time']} ({meeting['duration']}h)")
print(f" 👤 Assigned to: {customer['name']} ({customer['email']})")
else:
print(f"{unassociated_index:3}. {selected_symbol} {meeting['summary']}")
print(f" 📅 {meeting['date']} at {meeting['time']} ({meeting['duration']}h)")
# Show attendees
if meeting['attendees']:
attendee_list = ', '.join(meeting['attendees'][:3])
if len(meeting['attendees']) > 3:
attendee_list += f" (+{len(meeting['attendees']) - 3} more)"
print(f" 👥 Attendees: {attendee_list}")
# Show description preview if available
if meeting['description']:
desc_preview = meeting['description'][:60] + "..." if len(meeting['description']) > 60 else meeting['description']
print(f" 📝 Description: {desc_preview}")
print()
return meeting_map, unassociated_map
# Initial display
meeting_map, unassociated_map = display_meeting_list()
# Interactive selection
def show_commands():
"""Helper function to display available commands"""
print("\nCommands:")
print(" [number] - Toggle selection for meeting")
print(" 'all' - Select all uninvoiced meetings")
print(" 'none' - Deselect all meetings")
print(" 'edit [number]' - Edit meeting time/duration")
print(" 'time [number]' - Quick edit meeting time/duration")
print(" 'rate [number] [amount]' - Set custom rate for meeting")
print(" 'setrate [email] [amount]' - Update customer's default rate")
if unassociated_meetings:
print("\nUnassociated Meeting Commands:")
print(" 'U[number]' - Toggle selection for unassociated meeting")
print(" 'assign U[number] [email]' - Assign meeting to customer")
print(" 'search [query]' - Search for customer by name or email")
print("\nGeneral Commands:")
print(" 'continue' - Continue to synopsis entry")
print(" 'quit' - Exit program")
print(" '?' - Show this help message")
print("\nIcons:")
print(" ✏️ = Meeting time/duration edited")
print(" 💰 = Custom rate applied")
print(" 📝 = Customer found in meeting description")
print(" 🔗 = Manually assigned to customer")
show_commands()
while True:
command = input("\nEnter command: ").strip().lower()
if command == 'quit':
print("Exiting...")
exit(0)
elif command == 'continue':
break
elif command == '?' or command == 'help':
show_commands()
elif command == 'all':
for customer_id, data in customers_with_meetings.items():
for meeting in data['meetings']:
if meeting['invoice_status'] == 'not_invoiced':
meeting['selected'] = True
print("✓ Selected all uninvoiced meetings")
# Refresh display after change
meeting_map, unassociated_map = display_meeting_list()
elif command == 'none':
for customer_id, data in customers_with_meetings.items():
for meeting in data['meetings']:
meeting['selected'] = False
print("✓ Deselected all meetings")
# Refresh display after change
meeting_map, unassociated_map = display_meeting_list()
elif command.isdigit():
meeting_num = int(command)
if meeting_num in meeting_map:
customer_id, meeting_idx = meeting_map[meeting_num]
meeting = customers_with_meetings[customer_id]['meetings'][meeting_idx]
if meeting['invoice_status'] != 'not_invoiced':
print(f"❌ Cannot select meeting #{meeting_num} - already invoiced")
else:
meeting['selected'] = not meeting['selected']
action = "Selected" if meeting['selected'] else "Deselected"
print(f"✓ {action} meeting #{meeting_num}")
# Refresh display after change
meeting_map, unassociated_map = display_meeting_list()
else:
print(f"❌ Invalid meeting number: {meeting_num}")
show_commands()
elif command.startswith('edit '):
# Edit meeting details (time/duration)
try:
meeting_num = int(command.split()[1])
if meeting_num in meeting_map:
customer_id, meeting_idx = meeting_map[meeting_num]
meeting = customers_with_meetings[customer_id]['meetings'][meeting_idx]
customer_data = customers_with_meetings[customer_id]
self.edit_meeting_details(meeting, customer_data)
# Refresh display after edit
meeting_map, unassociated_map = display_meeting_list()
else:
print(f"❌ Invalid meeting number: {meeting_num}")
except (ValueError, IndexError):
print("❌ Usage: edit [meeting_number]")
print("Example: edit 1")
elif command.startswith('time '):
# Quick time edit shortcut
try:
meeting_num = int(command.split()[1])
if meeting_num in meeting_map:
customer_id, meeting_idx = meeting_map[meeting_num]
meeting = customers_with_meetings[customer_id]['meetings'][meeting_idx]
customer_data = customers_with_meetings[customer_id]
self.edit_meeting_details(meeting, customer_data)
# Refresh display after edit
meeting_map, unassociated_map = display_meeting_list()
else:
print(f"❌ Invalid meeting number: {meeting_num}")
except (ValueError, IndexError):
print("❌ Usage: time [meeting_number]")
print("Example: time 1")
elif command.startswith('rate '):
# Set per-meeting rate override
try:
parts = command.split()
if len(parts) >= 3:
meeting_num = int(parts[1])
rate_str = ' '.join(parts[2:])
if meeting_num in meeting_map:
customer_id, meeting_idx = meeting_map[meeting_num]
meeting = customers_with_meetings[customer_id]['meetings'][meeting_idx]
try:
rate = self.validate_hourly_rate(rate_str)
meeting['custom_rate'] = rate
print(f"✓ Set custom rate for meeting #{meeting_num}: ${rate}/hour")
# Refresh display after change
meeting_map, unassociated_map = display_meeting_list()
except ValueError as e:
print(f"❌ {e}")
else:
print(f"❌ Invalid meeting number: {meeting_num}")
else:
print("❌ Usage: rate [meeting_number] [rate]")
print("Example: rate 1 250")
except (ValueError, IndexError):
print("❌ Usage: rate [meeting_number] [rate]")
print("Example: rate 1 250")
elif command.startswith('setrate '):
# Set customer default rate
try:
parts = command.split()
if len(parts) >= 3:
customer_email = parts[1]
rate_str = ' '.join(parts[2:])
# Find customer by email
customer_found = False
for customer_id, data in customers_with_meetings.items():
if data['customer']['email'].lower() == customer_email.lower():
try:
rate = self.validate_hourly_rate(rate_str)
# Update customer rate in Stripe
if self.set_customer_hourly_rate(customer_id, rate):
print(f"✓ Updated hourly rate for {customer_email}: ${rate}/hour")
# Refresh display after change
meeting_map, unassociated_map = display_meeting_list()
else:
print(f"❌ Failed to update rate for {customer_email}")
customer_found = True
break
except ValueError as e:
print(f"❌ {e}")
customer_found = True
break
if not customer_found:
print(f"❌ Customer not found: {customer_email}")
else:
print("❌ Usage: setrate [customer_email] [rate]")
print("Example: setrate john@company.com 250")
except (ValueError, IndexError):
print("❌ Usage: setrate [customer_email] [rate]")
print("Example: setrate john@company.com 250")
elif command.upper().startswith('U') and command[1:].isdigit():
# Toggle unassociated meeting selection
unassoc_idx = command.upper()
if unassoc_idx in unassociated_map:
meeting_idx = unassociated_map[unassoc_idx]
meeting = unassociated_meetings[meeting_idx]
# Can only select if assigned to a customer
if meeting.get('assigned_customer'):
meeting['selected'] = not meeting['selected']
action = "Selected" if meeting['selected'] else "Deselected"
print(f"✓ {action} unassociated meeting {unassoc_idx}")
else:
print(f"❌ Cannot select meeting {unassoc_idx} - must assign to customer first")
# Refresh display after change
meeting_map, unassociated_map = display_meeting_list()
else:
print(f"❌ Invalid unassociated meeting number: {unassoc_idx}")
elif command.startswith('assign '):
# Assign unassociated meeting to customer
try:
parts = command.split()
if len(parts) >= 3:
unassoc_idx = parts[1].upper()
customer_email = parts[2].lower()
if unassoc_idx in unassociated_map:
meeting_idx = unassociated_map[unassoc_idx]
meeting = unassociated_meetings[meeting_idx]
# Find customer by email
customer_found = None
for cust in all_customers:
if cust['email'].lower() == customer_email:
customer_found = cust
break
if customer_found:
# Assign the meeting
meeting['assigned_customer'] = customer_found
meeting['is_manually_assigned'] = True
# Move meeting to customer's meeting list
customer_id = customer_found['id']
if customer_id not in customers_with_meetings:
customers_with_meetings[customer_id] = {
'customer': customer_found,
'meetings': []
}
# Create proper meeting structure for customer
customer_meeting = {
'id': meeting['id'],
'summary': meeting['summary'],
'date': meeting['date'],
'time': meeting['time'],
'duration': meeting['duration'],
'start_time': meeting['start_time'],
'end_time': meeting['end_time'],
'invoice_status': 'not_invoiced',
'selected': True, # Auto-select assigned meetings
'synopsis': meeting['synopsis'],
'edited_start_time': None,
'edited_duration': None,
'custom_rate': None,
'is_edited': False,
'detection_source': 'manual_assignment',
'is_manually_assigned': True
}
customers_with_meetings[customer_id]['meetings'].append(customer_meeting)
print(f"✅ Assigned '{meeting['summary']}' to {customer_found['name']} ({customer_email})")
# Refresh display
meeting_map, unassociated_map = display_meeting_list()
else:
print(f"❌ Customer not found: {customer_email}")
print("Use 'search' command to find customers")