# Import the functions and reporting class
from servicepytan import Report, auth
from servicepytan.reports import get_report_categories, get_report_list
# Set your config file path
st_conn = servicepytan.auth.servicepytan_connect(config_file="./servicepytan_config.json")
# Retrieve a list of report categories
categories = get_report_categories(config_file_path, conn=st_conn)
print(categories)
# Retrieve a list of reports included in the category
category_report_list = get_report_list("operations", config_file_path, conn=st_conn)
print(category_report_list)
# Configure a specific report
# NOTE: this is a standard ServiceTitan report in the Operations category
custom_report = Report("operations", "94428310", conn=st_conn)
# Look at the reporting metadata
# This inclued the filter options and field information
print(custom_report.metadata)
# Get list of parameters
custom_report.show_param_types()
# NOTE: [*] indicates a required parameter
# [*] - DateType: Number, (dynamicSetId: job-date-filter-type)
# [*] - From: Date,
# [*] - To: Date,
# [ ] - BusinessUnitId: Number, (dynamicSetId: business-units)
# [ ] - IncludeAdjustmentInvoices: Boolean,
# Get dynamic set information if needed
dynamic_sets = get_dynamic_set_list(dynamic_set_id='job-date-filter-type',conn=st_conn)
print(dynamic_sets)
# [0, 'Invoice Date'],
# [1, 'Job Completion Date'],
# [2, 'Job Creation Date'],
# [3, 'Job Start Date'],
# [4, 'Last Paid On Date'],
# [5, 'First Dispatch'],
# [6, 'Jobs with Appt Date'],
# [7, 'Jobs with Next Appt Start Date']
# Add Report Parameters
custom_report.add_params("DateType","3")
custom_report.add_params("From", "2022-11-03")
custom_report.add_params("To", "2022-11-03")
print(custom_report.get_params())
# Get individual page of data
custom_report_page_1_data = custom_report.get_data() #defaults to page 1
print(custom_report_page_1_data)
# Get all data
# This will run requests until all pages have been retrieved.
# NOTE: There is a 5min wait between subsequent requests from the same report.
# The function will automatically wait during the delay, however if it expects to
# run for more than 1 hour. The function will exit and ask to reduce size of request.
custom_report_all_data = custom_report.get_all_data()