NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

library(pdftools)
library(knitr)
library(kableExtra)
library(sendmailR)
library(stringr)
library(dplyr)

# Load utility functions
source(.stUtilityFunctions)
source(.msGraphFunctions)

estJobsStartFrom <- '2024-05-08'
toDate <- '2024-05-09'

getSTInstallWorkOrderForm <- function(jobId, formName) {
cookies <- c(
'st_tenant' = 'logan',
'st_authenticated_user' = '1'
)

auditTrail <- getJobAuditTrail(jobId)
formName <- str_replace_all(formName, c("\(" = "", "\)" = ""))
auditTrail$History$Entries$FormName <- str_replace_all(auditTrail$History$Entries$FormName, c("\(" = "", "\)" = ""))

entry <- auditTrail$History$Entries %>%
filter(str_detect(FormName, formName)) %>%
coerceTypes(all = 'character') %>%
# Latest contract usually has the best information
filter(!is.na(FormId)) %>%
filter(CreatedOn == max(CreatedOn))

waitForToken(paste0(.bucketDir, '/stBucket.rds'),
'stWebTokens',
nextUpdate = FALSE)

res <- GET(url = modify_url('https://go.servicetitan.com/Form/Print',
query = list(ownerId = entry$JobId,
formId = entry$FormId,
submittingId = entry$SubmittingId,
forMobile = FALSE,
isForms20 = entry$IsForm20)),
set_cookies(cookies),
authenticate('ddriver', .getCred('ServiceTitanPassword')))

pdfUrl <- res$url
waitForToken(paste0(.bucketDir, '/stBucket.rds'),
'stWebTokens',
nextUpdate = FALSE)

outResp <- GET(pdfUrl,
set_cookies(cookies),
authenticate('ddriver', .getCred('ServiceTitanPassword')))

tf <- tempfile(fileext = '.pdf')
writeBin(outResp$content, tf)
return(tf)
}

jobs <- getReports20(url = 'https://go.servicetitan.com/app/api/reporting/CustomReport/QueryExport?format=1&id=148072380',
critStr = '{
"DateType": "3",
"BusinessUnitId": "",
"IncludeAdjustmentInvoices": "false",
"RecipientId": "",
"PrimaryKeys": "",
"From": "%FROMDT%",
"To": "%TODT%",
"VisibleFields": "JobType,JobStatus,JobBusinessUnit,JobId,JobNumber,Total,PrimaryTechnician,CampaignCategory,ScheduledDate,CustomerId,LocationId,LocationName,LocationAddress,LocationZip,Tags,SoldBy,ProjectNumber,Estimates",
"Fields": "JobType,JobStatus,JobBusinessUnit,JobId,JobNumber,Total,PrimaryTechnician,CampaignCategory,ScheduledDate,CustomerId,LocationId,LocationName,LocationAddress,LocationZip,Tags,SoldBy,ProjectNumber,Estimates",
"Sort": "[]",
"Filters": "{\"filters\":[{\"logic\":\"and\",\"filters\":[{\"field\":\"JobStatus\",\"operator\":\"notin\",\"value\":[\"Canceled\",\"Hold\"]}]}],\"logic\":\"and\"}",
"Group": "[]",
"ColumnExportOptions": "",
"AggregatesOnly": "false"
}' %>%
str_replace('%FROMDT%', as.character(estJobsStartFrom)) %>%
str_replace('%TODT%', as.character(toDate))) %>%
clean_names('small_camel') %>%
filter(str_detect(businessUnit, '19|20|21|22|23|24|50')) %>% select(jobId,jobNumber)

#getTechnicians
technicians <- getTechnicians()
techsDf <- technicians %>% select(userId,Technicians = `name`,email,loginName)
#Form Number 260847 is For Install Work HVAC 2024 Form
allHvacForms <- searchForms(260847, as.Date(estJobsStartFrom), as.Date(toDate))
df <- allHvacForms %>% select(jobNumber = JobNumber,jobCompletedOn = `Job Completed On`,Technicians)
combinedDf <- merge(x = df, y = jobs, by = "jobNumber")

combinedTechs <- merge(x = combinedDf, y = techsDf, by = "Technicians")

mainCombinedDf <- merge(x=combinedDf, y=combinedTechs, by = "jobNumber")

for(jobId in unique(mainCombinedDf$jobId))
{
browser()
email_address <- mainCombinedDf$email[mainCombinedDf$jobId == jobId]
# Attempt to fetch the PDF and capture the response
pdf_file <- getSTInstallWorkOrderForm(
jobId = jobId,
formName = 'Install Work Order (HVAC) - 2024'
)

pdf_file2 <- getSTInstallWorkOrderForm(
jobId = jobId,
formName = 'Install Work Order (HVAC) - Measurements'
)
# Send the email
sendMail(
from = "[email protected]",
to = "[email protected]",
subject = paste(jobId, "- Install Work Order (HVAC) - 2024 Test Mail."),
body = paste0('
<h1>Install Work Order (HVAC) - 2024</h1>
<p>Dear Technician,</p>
<p>Please find attached the documents for the HVAC installation:</p>
<ul>
<li>Install Work Order (HVAC) - 2024</li>
<li>Install Work Order (HVAC) - Measurements</li>
</ul>
<p>Thank you for choosing our services.</p>
<p>Best Regards,<br>
Gaurav Pal<br>
Logan A/C & Heat Services.<br>
<br><br>'),
attachmentFiles = c(pdf_file,pdf_file2),
send = TRUE
)

}





     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.