This repository was archived by the owner on Mar 15, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLead Tracker
More file actions
401 lines (339 loc) · 16.7 KB
/
Lead Tracker
File metadata and controls
401 lines (339 loc) · 16.7 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
import tkinter as tk
from tkinter import ttk, messagebox, simpledialog, filedialog
import csv
from datetime import datetime
import os
from typing import List
try:
import openpyxl # For exporting to Excel
except ImportError:
pass # Will handle ImportError when trying to export to Excel
class LeadEntry:
"""Represents a single lead entry."""
def __init__(self, date: str, name: str, phone: str = "", email: str = "", contact_type: str = "",
role: str = "", source: str = "", location: str = "", importance_level: str = "", notes: str = ""):
self.date = date
self.name = name
self.phone = phone
self.email = email
self.contact_type = contact_type
self.role = role
self.source = source
self.location = location
self.importance_level = importance_level
self.notes = notes
def as_list(self):
"""Return lead data as a list."""
return [self.date, self.name, self.phone, self.email, self.contact_type, self.role, self.source,
self.location, self.importance_level, self.notes]
class LeadTracker:
"""Main class for the Lead Tracker Program."""
CONTACT_TYPES = [
"", "Friend", "Realtor", "Neighbor / Farm Contact", "Buyer", "Seller", "Renter",
"Investor", "Vendor", "Colleague", "Family Member", "Prospect", "Past Client",
"Service Provider", "Other Agent", "Referral Partner"
]
ROLES = ["", "Lead Source", "Buyer", "Seller", "Investor", "Renter", "Vendor", "Referral Partner"]
LEAD_SOURCES = [
"", "Referral", "Referral from Another Agent", "Social Media", "Farm Area",
"Open House", "Online Listing", "Cold Call", "Networking Event",
"Website Inquiry", "Past Client", "Walk-in", "Advertisement", "Direct Mail", "Email Campaign"
]
IMPORTANCE_LEVELS = ["", "High", "Medium", "Low"]
def __init__(self, master):
"""Initialize the lead tracker GUI."""
self.master = master
master.title("Real Estate Lead Tracker")
master.geometry("1100x750")
self.create_widgets()
self.leads: List[LeadEntry] = []
self.load_leads()
self.update_summary()
def create_widgets(self):
"""Create and arrange GUI widgets."""
main_frame = ttk.Frame(self.master, padding="10")
main_frame.grid(row=0, column=0, sticky=(tk.W, tk.E, tk.N, tk.S))
self.master.columnconfigure(0, weight=1)
self.master.rowconfigure(0, weight=1)
# Date (optional, default to current date)
ttk.Label(main_frame, text="Date (YYYY-MM-DD):").grid(row=0, column=0, sticky=tk.E, padx=5, pady=5)
self.date_entry = ttk.Entry(main_frame, width=40)
self.date_entry.grid(row=0, column=1, padx=5, pady=5)
self.date_entry.insert(0, datetime.now().strftime("%Y-%m-%d"))
# Name (required)
ttk.Label(main_frame, text="Name:*").grid(row=1, column=0, sticky=tk.E, padx=5, pady=5)
self.name_entry = ttk.Entry(main_frame, width=40)
self.name_entry.grid(row=1, column=1, padx=5, pady=5)
# Phone
ttk.Label(main_frame, text="Phone:").grid(row=2, column=0, sticky=tk.E, padx=5, pady=5)
self.phone_entry = ttk.Entry(main_frame, width=40)
self.phone_entry.grid(row=2, column=1, padx=5, pady=5)
# Email
ttk.Label(main_frame, text="Email:").grid(row=3, column=0, sticky=tk.E, padx=5, pady=5)
self.email_entry = ttk.Entry(main_frame, width=40)
self.email_entry.grid(row=3, column=1, padx=5, pady=5)
# Contact Type
ttk.Label(main_frame, text="Contact Type:").grid(row=4, column=0, sticky=tk.E, padx=5, pady=5)
self.contact_type = ttk.Combobox(main_frame, values=self.CONTACT_TYPES, width=37)
self.contact_type.grid(row=4, column=1, padx=5, pady=5)
self.contact_type.set(self.CONTACT_TYPES[0])
# Role
ttk.Label(main_frame, text="Role:").grid(row=5, column=0, sticky=tk.E, padx=5, pady=5)
self.role = ttk.Combobox(main_frame, values=self.ROLES, width=37)
self.role.grid(row=5, column=1, padx=5, pady=5)
self.role.set(self.ROLES[0])
# Lead Source
ttk.Label(main_frame, text="Lead Source:").grid(row=6, column=0, sticky=tk.E, padx=5, pady=5)
self.lead_source = ttk.Combobox(main_frame, values=self.LEAD_SOURCES, width=37)
self.lead_source.grid(row=6, column=1, padx=5, pady=5)
self.lead_source.set(self.LEAD_SOURCES[0])
# Location
ttk.Label(main_frame, text="Location:").grid(row=7, column=0, sticky=tk.E, padx=5, pady=5)
self.location_entry = ttk.Entry(main_frame, width=40)
self.location_entry.grid(row=7, column=1, padx=5, pady=5)
# Importance Level
ttk.Label(main_frame, text="Importance Level:").grid(row=8, column=0, sticky=tk.E, padx=5, pady=5)
self.importance_level = ttk.Combobox(main_frame, values=self.IMPORTANCE_LEVELS, width=37)
self.importance_level.grid(row=8, column=1, padx=5, pady=5)
self.importance_level.set(self.IMPORTANCE_LEVELS[0])
# Notes
ttk.Label(main_frame, text="Notes:").grid(row=9, column=0, sticky=tk.NE, padx=5, pady=5)
self.notes_text = tk.Text(main_frame, width=40, height=5)
self.notes_text.grid(row=9, column=1, padx=5, pady=5)
# Buttons Frame
buttons_frame = ttk.Frame(main_frame)
buttons_frame.grid(row=10, column=0, columnspan=2, pady=10)
# Submit Button
self.submit_button = ttk.Button(buttons_frame, text="Submit", command=self.submit_lead)
self.submit_button.grid(row=0, column=0, padx=5)
# Edit Lead Button
self.edit_lead_button = ttk.Button(buttons_frame, text="Edit Lead", command=self.edit_lead)
self.edit_lead_button.grid(row=0, column=1, padx=5)
# Delete Lead Button
self.delete_item_button = ttk.Button(buttons_frame, text="Delete Lead", command=self.delete_lead)
self.delete_item_button.grid(row=0, column=2, padx=5)
# Export to CSV Button
self.export_button = ttk.Button(buttons_frame, text="Export to CSV", command=self.export_to_csv)
self.export_button.grid(row=0, column=3, padx=5)
# Export to Excel Button
self.export_excel_button = ttk.Button(buttons_frame, text="Export to Excel", command=self.export_to_excel)
self.export_excel_button.grid(row=0, column=4, padx=5)
# Leads Treeview
columns = ("Date", "Name", "Phone", "Email", "Contact Type", "Role", "Source", "Location", "Importance Level", "Notes")
self.leads_tree = ttk.Treeview(main_frame, columns=columns, show='headings')
self.leads_tree.grid(row=11, column=0, columnspan=2, padx=5, pady=5, sticky='nsew')
# Define headings
for col in columns:
self.leads_tree.heading(col, text=col, command=lambda _col=col: self.treeview_sort_column(self.leads_tree, _col, False))
self.leads_tree.column(col, width=100)
# Configure column and row weights
main_frame.columnconfigure(1, weight=1)
main_frame.rowconfigure(11, weight=1)
def submit_lead(self):
"""Handle lead submission."""
date = self.date_entry.get().strip()
name = self.name_entry.get().strip()
phone = self.phone_entry.get().strip()
email = self.email_entry.get().strip()
contact_type = self.contact_type.get()
role = self.role.get()
source = self.lead_source.get()
location = self.location_entry.get().strip()
importance_level = self.importance_level.get()
notes = self.notes_text.get("1.0", tk.END).strip()
if not self.validate_input(name, date):
return
new_lead = LeadEntry(date, name, phone, email, contact_type, role, source,
location, importance_level, notes)
self.leads.append(new_lead)
self.save_leads()
self.clear_form()
self.update_summary()
messagebox.showinfo("Success", "Lead recorded successfully!")
def validate_input(self, name: str, date_str: str) -> bool:
"""Validate user input."""
if not name:
messagebox.showerror("Error", "Name is required!")
return False
try:
datetime.strptime(date_str, "%Y-%m-%d")
except ValueError:
messagebox.showerror("Error", "Date must be in YYYY-MM-DD format.")
return False
return True
def clear_form(self):
"""Clear input fields."""
self.date_entry.delete(0, tk.END)
self.date_entry.insert(0, datetime.now().strftime("%Y-%m-%d"))
self.name_entry.delete(0, tk.END)
self.phone_entry.delete(0, tk.END)
self.email_entry.delete(0, tk.END)
self.contact_type.set(self.CONTACT_TYPES[0])
self.role.set(self.ROLES[0])
self.lead_source.set(self.LEAD_SOURCES[0])
self.location_entry.delete(0, tk.END)
self.importance_level.set(self.IMPORTANCE_LEVELS[0])
self.notes_text.delete("1.0", tk.END)
# Reset submit button if in 'Update' mode
self.submit_button.config(text="Submit", command=self.submit_lead)
def update_summary(self):
"""Update the lead summary display."""
# Clear the Treeview
for item in self.leads_tree.get_children():
self.leads_tree.delete(item)
# Insert leads into the Treeview
for index, lead in enumerate(self.leads):
self.leads_tree.insert("", "end", iid=index, values=lead.as_list())
def save_leads(self):
"""Save leads to a CSV file."""
with open('leads.csv', 'w', newline='', encoding='utf-8') as file:
writer = csv.writer(file)
writer.writerow(['Date', 'Name', 'Phone', 'Email', 'Contact Type', 'Role', 'Source', 'Location', 'Importance Level', 'Notes'])
for lead in self.leads:
writer.writerow(lead.as_list())
def load_leads(self):
"""Load leads from the CSV file."""
if not os.path.exists('leads.csv'):
return
with open('leads.csv', 'r', newline='', encoding='utf-8') as file:
reader = csv.reader(file)
next(reader) # Skip header
for row in reader:
if len(row) == 10:
date, name, phone, email, contact_type, role, source, location, importance_level, notes = row
self.leads.append(LeadEntry(date, name, phone, email, contact_type, role, source, location, importance_level, notes))
def export_to_csv(self):
"""Export leads to a new CSV file."""
filepath = filedialog.asksaveasfilename(defaultextension=".csv",
filetypes=[("CSV files", "*.csv")],
title="Save CSV File")
if filepath:
try:
with open(filepath, 'w', newline='', encoding='utf-8') as file:
writer = csv.writer(file)
writer.writerow(['Date', 'Name', 'Phone', 'Email', 'Contact Type', 'Role', 'Source', 'Location', 'Importance Level', 'Notes'])
for lead in self.leads:
writer.writerow(lead.as_list())
messagebox.showinfo("Export Successful", f"Data exported to {filepath}")
except IOError:
messagebox.showerror("Export Failed", "An error occurred while exporting the data.")
def export_to_excel(self):
"""Export leads to an Excel file."""
try:
from openpyxl import Workbook
except ImportError:
messagebox.showerror("Module Not Found", "openpyxl module not found. Please install it using 'pip install openpyxl'")
return
filepath = filedialog.asksaveasfilename(defaultextension=".xlsx",
filetypes=[("Excel files", "*.xlsx")],
title="Save Excel File")
if filepath:
try:
wb = Workbook()
ws = wb.active
ws.title = "Leads"
# Write header
headers = ['Date', 'Name', 'Phone', 'Email', 'Contact Type', 'Role', 'Source', 'Location', 'Importance Level', 'Notes']
ws.append(headers)
# Write data
for lead in self.leads:
ws.append(lead.as_list())
wb.save(filepath)
messagebox.showinfo("Export Successful", f"Data exported to {filepath}")
except Exception as e:
messagebox.showerror("Export Failed", f"An error occurred while exporting the data.\n{e}")
def delete_lead(self):
"""Delete a selected lead."""
selected_item = self.leads_tree.selection()
if not selected_item:
messagebox.showinfo("No Selection", "Please select a lead to delete.")
return
confirm = messagebox.askyesno("Confirm Delete", "Are you sure you want to delete the selected lead?")
if confirm:
index = int(selected_item[0])
del self.leads[index]
self.save_leads()
self.update_summary()
messagebox.showinfo("Lead Deleted", "Lead has been deleted.")
def edit_lead(self):
"""Edit a selected lead."""
selected_item = self.leads_tree.selection()
if not selected_item:
messagebox.showinfo("No Selection", "Please select a lead to edit.")
return
index = int(selected_item[0])
lead = self.leads[index]
# Populate the form with lead data
self.date_entry.delete(0, tk.END)
self.date_entry.insert(0, lead.date)
self.name_entry.delete(0, tk.END)
self.name_entry.insert(0, lead.name)
self.phone_entry.delete(0, tk.END)
self.phone_entry.insert(0, lead.phone)
self.email_entry.delete(0, tk.END)
self.email_entry.insert(0, lead.email)
self.contact_type.set(lead.contact_type)
self.role.set(lead.role)
self.lead_source.set(lead.source)
self.location_entry.delete(0, tk.END)
self.location_entry.insert(0, lead.location)
self.importance_level.set(lead.importance_level)
self.notes_text.delete("1.0", tk.END)
self.notes_text.insert("1.0", lead.notes)
# Change the submit button to 'Update'
self.submit_button.config(text="Update", command=lambda: self.update_lead(index))
def update_lead(self, index):
"""Update a lead after editing."""
date = self.date_entry.get().strip()
name = self.name_entry.get().strip()
phone = self.phone_entry.get().strip()
email = self.email_entry.get().strip()
contact_type = self.contact_type.get()
role = self.role.get()
source = self.lead_source.get()
location = self.location_entry.get().strip()
importance_level = self.importance_level.get()
notes = self.notes_text.get("1.0", tk.END).strip()
if not self.validate_input(name, date):
return
lead = self.leads[index]
lead.date = date
lead.name = name
lead.phone = phone
lead.email = email
lead.contact_type = contact_type
lead.role = role
lead.source = source
lead.location = location
lead.importance_level = importance_level
lead.notes = notes
self.save_leads()
self.clear_form()
self.update_summary()
# Reset submit button
self.submit_button.config(text="Submit", command=self.submit_lead)
messagebox.showinfo("Success", "Lead updated successfully!")
def treeview_sort_column(self, tv, col, reverse):
"""Sort Treeview column when header is clicked."""
data_list = [(tv.set(k, col), k) for k in tv.get_children('')]
# Try to convert data to appropriate types for sorting
try:
if col == "Date":
data_list.sort(key=lambda t: datetime.strptime(t[0], "%Y-%m-%d"), reverse=reverse)
elif col in ["Name", "Phone", "Email", "Contact Type", "Role", "Source", "Location", "Importance Level"]:
data_list.sort(key=lambda t: t[0].lower(), reverse=reverse)
else:
data_list.sort(key=lambda t: t[0], reverse=reverse)
except Exception as e:
data_list.sort(key=lambda t: t[0], reverse=reverse)
# Rearrange items in sorted positions
for index, (val, k) in enumerate(data_list):
tv.move(k, '', index)
# Reverse sort next time
tv.heading(col, command=lambda: self.treeview_sort_column(tv, col, not reverse))
def main():
root = tk.Tk()
lead_tracker = LeadTracker(root)
root.mainloop()
if __name__ == "__main__":
main()