def reportmaker(fp, type): file1 = addpath1(f'reports/pagestart.pdf') file2 = addpath1(f'reports/background.pdf') file3 = addpath1(f'reports/headers.pdf') c = canvas.Canvas(file1, pagesize=letter) c.setLineWidth(1) c.drawImage(logoi, 185, 680, mask='auto') c.showPage() c.save() docref = None if type == 'introduction': report_background(file2, 'introduction') report_headers(file3, 'introduction') report_contents(fp, 'introduction') docref = pagemerger([file1, file2, file3, fp]) if type == 'driver_list': file4 = addpath1(f'reports/driverlist.pdf') report_background(file2, 'drivers') report_headers(file3, 'drivers') report_contents(fp, 'drivers') docref = pagemerger([file1, file2, file3, fp]) if type == 'truck_list': file4 = addpath1(f'reports/trucklist.pdf') report_background(file2, 'trucks') report_headers(file3, 'trucks') report_contents(fp, 'trucks') docref = pagemerger([file1, file2, file3, fp]) return docref
def make_each_sumover(rep, each, subrep): if rep == 'driver_sections': driver = each.replace(' ', '_') if subrep == 'summary': fp = addpath1(f'reports/{driver}_Summary.pdf') bm = f'{each}' if subrep == 'mvr': fp = addpath1(f'reports/{driver}_MVR.pdf') bm = f'{each} MVR' if subrep == 'hos': fp = addpath1(f'reports/hos_{driver}.pdf') bm = f'{each} Hours of Service' if subrep == 'hosval': fp = addpath1(f'reports/hos_val{driver}.pdf') bm = f'{each} HOS Validation Data' if rep == 'inspection_sections': fp = addpath1(f'reports/Annual_Inspection_Unit_{each}.pdf') bm = f'{each} Annual Inspection' if rep == 'drug_test_sections': driver = each.replace(' ', '_') if subrep == 'ccf': fp = addpath1(f'reports/{driver}_PreEmp_CCF.pdf') bm = f'{each} PreEmployment CCF' if subrep == 'results': fp = addpath1(f'reports/{driver}_PreEmp_Results.pdf') bm = f'{each} PreEmployment Results' return fp, bm
def pagemerger(filelist): lfs = len(filelist)-1 print('lfs has value:', lfs) for j in range(lfs): if j == 0: firstfile = filelist[0] else: firstfile = addpath1(f'reports/temp'+str(j-1)+'.pdf') reader = PdfFileReader(open(firstfile, 'rb')) first_page = reader.getPage(0) sup_reader = PdfFileReader(open(filelist[j+1], 'rb')) sup_page = sup_reader.getPage(0) # This is the first page, can pick any page of document #translated_page = PageObject.createBlankPage(None, sup_page.mediaBox.getWidth(), sup_page.mediaBox.getHeight()) # translated_page.mergeScaledTranslatedPage(sup_page, 1, 0, 0) # -400 is approximate mid-page # translated_page.mergePage(invoice_page) sup_page.mergePage(first_page) writer = PdfFileWriter() # writer.addPage(translated_page) writer.addPage(sup_page) if j == lfs-1: outfile = addpath1(f'reports/report'+'.pdf') else: outfile = addpath1(f'reports/temp'+str(j)+'.pdf') print('lfs and j are:', j, lfs) print('firstfile=', firstfile) print('supfile=', filelist[j+1]) print('outfile=', outfile) with open(outfile, 'wb') as f: writer.write(f) f.close() docref = f'reports/report.pdf' return docref
def subreportmaker(fp, type, each, subtype): file1 = addpath1(f'reports/pagestart.pdf') file2 = addpath1(f'reports/background.pdf') file3 = addpath1(f'reports/headers.pdf') c = canvas.Canvas(file1, pagesize=letter) c.setLineWidth(1) c.drawImage(logoi, 185, 680, mask='auto') c.showPage() c.save() if subtype == 'hos' or subtype == 'hosval': report_background(file2, subtype) report_headers(file3, subtype) pages, multioutput = subreport_contents(fp, type, each, subtype) if len(pages) > 1: docref = pagemergermp([file1, file2, file3, fp], pages, multioutput) else: docref = pagemerger([file1, file2, file3, fp]) return docref else: return None
def mergerbook(pdfs, bookmarks): # merge page by page output_pdf_stream = PdfFileWriter() j = 0 for kx, pdf in enumerate(pdfs): f = PdfFileReader(open(pdf, "rb")) for i in range(f.numPages): output_pdf_stream.addPage(f.getPage(i)) if i == 0: output_pdf_stream.addBookmark(bookmarks[kx], j) j = j + 1 # create output pdf file ofile = addpath1('reports/fmcsa.pdf') try: output_pdf_file = open(ofile, "wb") output_pdf_stream.write(output_pdf_file) finally: output_pdf_file.close()
for subrep in subreps: fp, bm = make_each_sumover(rep, each, subrep) fpstatic = fp.replace('reports/', 'reports/static/') print(f'Looking for static file: {fpstatic}') if os.path.isfile(fpstatic): pdfs.append(fpstatic) bookmarks.append(bm) else: docref = subreportmaker(fp, rep, each, subrep) if docref is not None: if os.path.isfile(docref): pdfs.append(fp) bookmarks.append(bm) shutil.move(docref, fp) else: fp = addpath1(f'reports/{scac}_{rep.title()}.pdf') bm = rep.replace('_', ' ') fpstatic = fp.replace('reports/', 'reports/static/') print(f'Looking for static file: {fpstatic}') if os.path.isfile(fpstatic): pdfs.append(fpstatic) bookmarks.append(bm.title()) else: docref = reportmaker(fp, rep) if docref is not None: pdfs.append(fp) bookmarks.append(bm.title()) shutil.move(docref, fp) mergerbook(pdfs, bookmarks)
import datetime from datetime import timedelta from viewfuncs import stripper, newjo, hasinput import os movem = 0 daybackfrom = 10 daybackto = 0 printif = 0 today = datetime.datetime.today() cutoffdate = today - timedelta(3) print(f'Running pdf builder for SCAC {scac} off Machine {machine}') print(addpath1('tmp/')) odata = Orders.query.filter(Orders.Date > cutoffdate).all() for odat in odata: if hasinput(odat.Container): idata = Interchange.query.filter( Interchange.Container == odat.Container).all() for idat in idata: print(idat.Container, idat.Original) if movem == 1: pythonline = websites[ 'ssh_data'] + f'vinterchange/{idat.Original}' placefile = addpath3(f'interchange/{idat.Original}') copyline1 = f'scp {pythonline} {placefile}' print(copyline1) os.system(copyline1)
try: body = body.decode('utf-8') pdflinks = get_links(body) dispatch_links.append(pdflinks) dispatch_subjs.append(subject) dispatch_dates.append(thisdate.strftime('%Y-%m-%d')) order = subject.split('Order ID:', 1)[1] order = order.split() order = order[0] orderlist.append(order.strip()) company = subject.split('ACCEPTED by', 1)[1] companylist.append(company.strip()) except: print('Bad decode on', getdate) txt_file = addpath1('tmp/dispatches.txt') print('txt_file=', txt_file) print('Initial Orderlist: ', orderlist) if dispatch == 2: ot = 'w+' with open(txt_file, ot) as f: for j, mylink in enumerate(dispatch_links): print('Adding', mylink) f.write(dispatch_dates[j] + ' ' + dispatch_subjs[j] + '\n') if dispatch == 1: poplist = [] with open(txt_file, 'w+') as f: for line in f: for j, order in enumerate(orderlist): company = companylist[j] if order in line and company in line:
def subreport_contents(fp, rep, each, item): cut60 = datetime.now() - timedelta(60) ltm, rtm, bump, tb, ctrall, left_ctr, right_ctr, dl, dh, tdl, hls, m1, m2, m3, m4, m5, m6, m7, n1, n2, n3 = reportsettings( 1) pages = [fp] page = 1 c = canvas.Canvas(fp, pagesize=letter) c.setLineWidth(1) #Main Items Listing mid = int(ltm + rtm) / 2 c.line(ltm, 550, rtm, 550) c.line(ltm, 530, rtm, 530) if rep == 'driver_sections': driver = each tlogs = Trucklog.query.filter((Trucklog.DriverStart == driver) & (Trucklog.Date > cut60)).order_by( Trucklog.Date).all() try: # get last 30 times in truck tlogs = tlogs[-30:] exemptnumber = 1 except: print('Tlogs shorter than 30') if item == 'hos': c.setFont('Helvetica-Bold', 14, leading=None) c.drawCentredString( mid, 535, f'Hours of Service For Driver {each} Last 30 Drive Days') c.setFont('Helvetica', 10, leading=None) top = 530 leftw1 = ltm + 10 leftw2 = ltm + 170 leftw3 = ltm + 220 c.setFont('Helvetica', 10, leading=None) top = top - dl * .9 for dd in reversed(tlogs): duty_hours = dd.Shift try: hrs = float(duty_hours) except: hrs = 0.0 if hrs > 12.0 and hrs < 12.25: hrs = 12.0 airmiles = dd.Rdist try: airmiles = float(airmiles) except: airmiles = 0.0 logmiles = dd.Distance try: logmiles = float(logmiles) except: logmiles = 0.0 if hrs > 1.0: if hrs > 12.0 or airmiles > 100: if hrs > 12.0 and airmiles < 100: exempt = '**Shift hours exceed 12.0**' if hrs < 12.0 and airmiles > 100: exempt = '**Airmiles exceed 100.0**' if hrs > 12.0 and airmiles > 100: exempt = '**Shift hours exceed 12.0 and Airmiles exceed 100.0**' else: exempt = '100 mile exemption' thisdate = dd.GPSin thisdate = thisdate.date() c.drawString(leftw1, top, f'Shift Start: {dd.GPSin}') c.drawString(leftw2, top, f'Unit {dd.Unit}') c.drawString(leftw3, top, f'{dd.Locationstart}') top = top - dl * .7 c.drawString(leftw1, top, f'Shift End : {dd.GPSout}') c.drawString(leftw2, top, f'Unit {dd.Unit}') c.drawString(leftw3, top, f'{dd.Locationstop}') top = top - dl * .7 c.drawString(leftw1, top, f'Duty Hours: {hrs}') c.drawString(leftw2, top, f'Truck Miles: {dd.Distance}') c.drawString(leftw2 + 120, top, f'Air Miles: {dd.Rdist}') top = top - dl * .7 c.drawString(leftw2, top, f'Farthest Location: {dd.Rloc}') #top = top - dl * .7 if '**' in exempt: c.setFillColorRGB(1, 0, 0) c.drawString(leftw1, top, f'Paper Log Required') top = top - dl * .7 c.drawString(leftw1, top, f'{exempt}') c.drawString( leftw2, top, f'Exemption #{exemptnumber} of 8 allowed last 30 days' ) exemptnumber = exemptnumber + 1 c.setFillColorRGB(0, 0, 0) else: c.drawString(leftw1, top, f'Logging: {exempt}') top = top - dl * 1.2 if top < n3: c, page, top, pages = topcheck(fp, n2, dh, c, page, pages) c.drawCentredString( mid, 535, f'HOS Data For Driver {each} (cont.) ') c.line(ltm, 550, rtm, 550) c.line(ltm, 530, rtm, 530) c.setFont('Helvetica', 10, leading=None) if item == 'hosval': c.drawCentredString(mid, 535, f'HOS Validation Data For Driver {each}') top = 510 leftw1 = ltm + 10 leftw2 = ltm + 70 leftw3 = ltm + 110 leftw4 = ltm + 190 leftw5 = ltm + 290 c.drawString(leftw1, top, f'Interchange Tickets from Port') top = top - dl * .8 c.drawString(leftw1, top, f'Date') c.drawString(leftw2, top, f'Time') c.drawString(leftw3, top, f'Plate') c.drawString(leftw4, top, f'Container') c.drawString(leftw5, top, f'Gross Weight') top = top - dl * .8 c.setFont('Helvetica', 10, leading=None) for dd in reversed(tlogs): thisdate = dd.GPSin thisdate = thisdate.date() tdat = Vehicles.query.filter(Vehicles.Unit == dd.Unit).first() plate = tdat.Plate valticket = Interchange.query.filter( (Interchange.Date == thisdate) & (Interchange.TruckNumber == plate)).all() for val in valticket: c.drawString(leftw1, top, f'{val.Date}') c.drawString(leftw2, top, f'{val.Time}') c.drawString(leftw3, top, f'{val.TruckNumber}') c.drawString(leftw4, top, f'{val.Container}') c.drawString(leftw5, top, f'{val.GrossWt}') top = top - dl * .6 if top < n3: c, page, top, pages = topcheck(fp, n2, dh, c, page, pages) c.drawCentredString( mid, 535, f'HOS Validation Data For Driver ') c.line(ltm, 550, rtm, 550) c.line(ltm, 530, rtm, 530) c.setFont('Helvetica', 10, leading=None) top = top - dl * .6 leftw1 = ltm + 10 leftw2 = ltm + 130 leftw3 = ltm + 180 leftw4 = ltm + 240 c.drawString(leftw1, top, f'Toll Plaza Data') top = top - dl * .8 c.drawString(leftw1, top, f'Date/Time') c.drawString(leftw2, top, f'Unit') c.drawString(leftw3, top, f'Plaza') c.drawString(leftw4, top, f'Amount') top = top - dl * .8 for dd in reversed(tlogs): thisdate = dd.GPSin thisdate = thisdate.date() unit = dd.Unit print(thisdate, unit) tolldata = Tolls.query.filter((Tolls.Date == thisdate) & (Tolls.Unit == unit)).all() for toll in tolldata: c.drawString(leftw1, top, f'{toll.Datetm}') c.drawString(leftw2, top, f'{toll.Unit}') c.drawString(leftw3, top, f'{toll.Plaza}') c.drawString(leftw4, top, f'{toll.Amount}') top = top - dl * .6 if top < n3: c, page, top, pages = topcheck(fp, n2, dh, c, page, pages) c.drawCentredString( mid, 535, f'HOS Validation Data For Driver ') c.line(ltm, 550, rtm, 550) c.line(ltm, 530, rtm, 530) c.setFont('Helvetica', 10, leading=None) c.showPage() c.save() if len(pages) > 1: pdfcommand = ['pdfunite'] for page in pages: pdfcommand.append(page) multioutput = addpath1(f'reports/multioutput' + '.pdf') pdfcommand.append(multioutput) tes = subprocess.check_output(pdfcommand) else: multioutput = ' ' return pages, multioutput
def report_contents(file4, item): ltm, rtm, bump, tb, ctrall, left_ctr, right_ctr, dl, dh, tdl, hls, m1, m2, m3, m4, m5, m6, m7, n1, n2, n3 = reportsettings( 1) pages = [file4] page = 1 c = canvas.Canvas(file4, pagesize=letter) c.setLineWidth(1) #Main Items Listing c.setFont('Helvetica-Bold', 14, leading=None) mid = int(ltm + rtm) / 2 if item == 'drivers': c.drawCentredString(mid, 535, 'Active Driver Data') if item == 'trucks': c.drawCentredString(mid, 535, 'Active DOT Truck Data') c.line(ltm, 550, rtm, 550) c.line(ltm, 530, rtm, 530) c.setFont('Helvetica', 10, leading=None) top = 510 leftw1 = ltm + 10 leftw2 = ltm + 110 leftw3 = ltm + 320 leftw4 = ltm + 490 if item == 'introduction': c.drawCentredString(mid, 535, 'Introduction') ddata = KeyInfo.query.all() presents = [ 'overview', 'focus', 'truckstart', 'logging', 'rod', 'maintenance', 'testing', 'accidents' ] for present in presents: for dd in ddata: if dd.Type == present: c.drawString(leftw1, top, dd.Type.title()) slist = parseline(dd.Description, 90) for sl in slist: c.drawString(leftw2, top, sl) top = top - dl * .9 top = top - dl * .5 if top < n3: c, page, top, pages = topcheck(file4, n2, dh, c, page, pages) if item == 'drivers': ddata = Drivers.query.filter(Drivers.JobEnd > today).all() for dd in ddata: c.drawString(leftw1, top, dd.Name) c.drawString(leftw2, top, f'{dd.Addr1}, {dd.Addr2}') top = top - dl c.drawString(leftw2, top, f'{dd.Phone} {dd.Email}') top = top - dl c.drawString(leftw2, top, f'Started with Company: {dd.JobStart}') c.drawString(leftw3, top, f'DOB: {dd.DOB}') top = top - dl c.drawString( leftw2, top, f'CDL Information: {dd.CDLstate} #{dd.CDLnum} Expires:{dd.CDLexpire}' ) top = top - dl c.drawString( leftw2, top, f'Medical Expires: {dd.MedExpire} TWIC Expires: {dd.TwicExpire}' ) top = top - dl c.drawString( leftw2, top, f'Pre-Employment Screening: {dd.PreScreen} Last Screening Completed: {dd.LastTested}' ) top = top - dl * 2 if top < n3: c, page, top, pages = topcheck(file4, n2, dh, c, page, pages) if item == 'trucks': ddata = Vehicles.query.filter(Vehicles.DOTNum != None).all() for dd in ddata: c.drawString(leftw1, top, f'Unit #{dd.Unit}') c.drawString( leftw2, top, f'{dd.Year} {dd.Make} {dd.Model} Place in Service:{dd.ServStr}' ) top = top - dl c.drawString( leftw2, top, f'Color: {dd.Color} VIN: {dd.VIN} MD Plate:{dd.Plate}') top = top - dl c.drawString( leftw2, top, f'MD Title: {dd.Title} EmptyWt: {dd.EmpWeight} GrossWt: {dd.GrossWt}' ) top = top - dl c.drawString( leftw2, top, f'EzPass Transponder: {dd.Ezpassxponder} Port Transponer: {dd.Portxponder}' ) top = top - dl * 2 if top < n3: c, page, top, pages = topcheck(file4, n2, dh, c, page, pages) addtickets = 0 if addtickets == 1: first = 0 last = 0 for ix, val in enumerate(valticket): if ix == 0: tlist = val.Time.split(':') firsttime = int(tlist[0]) lasttime = firsttime else: tlist = val.Time.split(':') thistime = int(tlist[0]) if thistime < firsttime: first = ix firsttime = thistime elif thistime >= lasttime: last = ix lasttime = thistime for ix, val in enumerate(valticket): if ix == first or ix == last: placefile = addpath3(f'interchange/{val.Original}') if os.path.isfile(placefile): print( f'Have interchange ticket for {thisdate} {plate}' ) else: pythonline = websites[ 'ssh_data'] + f'vinterchange/{val.Original}' placefile = addpath3(f'interchange/{val.Original}') copyline1 = f'scp {pythonline} {placefile}' print(copyline1) os.system(copyline1) if ix == first: placefile1 = placefile if ix == last: placefile2 = placefile blendfile = addpath3(f'interchange/BLEND_{val.Original}') if os.path.isfile(blendfile): print('Have this blend file') else: blendticks(placefile1, placefile2, blendfile) valpdfs.append(blendfile) c.showPage() c.save() if len(pages) > 1: pdfcommand = ['pdfunite'] for page in pages: pdfcommand.append(page) multioutput = addpath1(f'reports/multioutput' + '.pdf') pdfcommand.append(multioutput) tes = subprocess.check_output(pdfcommand) else: multioutput = ' ' return pages, multioutput
return year, make, model, error # Main part of running code client = Client() try: longs = open('incoming/whatsapp/vins.txt').read() vlist = longs.split() os.remove('incoming/whatsapp/vins.txt') sessionph = vlist[0] vlist.remove(sessionph) except IOError: longs = '' vlist = [] file1 = open(addpath1('whatsapp/vinrun.txt'), 'a+') file1.write('Error in opening incoming/vins.txt\n') file1.close() for vin in vlist: message = client.messages.create(body=f'Looking up VIN: {vin}', from_=from_phone, to=sessionph) now = datetime.datetime.now() date_time = now.strftime("%m/%d/%Y, %H:%M:%S") file1 = open(addpath1('whatsapp/vinrun.txt'), 'a+') file1.write('At ' + date_time + ' running task for VIN:' + vin + '\n') file1.close() year, make, model, error = quickvinloader(vin)
def pagemergermp(filelist, pages, multioutput): lfs = len(filelist)-1 print('lfs has value:', lfs) for j in range(lfs): if j == 0: firstfile = filelist[0] else: firstfile = addpath1(f'reports/temp'+str(j-1)+'.pdf') reader = PdfFileReader(open(firstfile, 'rb')) first_page = reader.getPage(0) sup_reader = PdfFileReader(open(filelist[j+1], 'rb')) sup_page = sup_reader.getPage(0) # This is the first page, can pick any page of document #translated_page = PageObject.createBlankPage(None, sup_page.mediaBox.getWidth(), sup_page.mediaBox.getHeight()) # translated_page.mergeScaledTranslatedPage(sup_page, 1, 0, 0) # -400 is approximate mid-page # translated_page.mergePage(invoice_page) sup_page.mergePage(first_page) writer = PdfFileWriter() # writer.addPage(translated_page) writer.addPage(sup_page) if j == lfs-1: outfile = addpath1(f'reports/reportx.pdf') else: outfile = addpath1(f'reports/temp'+str(j)+'.pdf') print('lfs and j are:', j, lfs) print('firstfile=', firstfile) print('supfile=', filelist[j+1]) print('outfile=', outfile) with open(outfile, 'wb') as f: writer.write(f) f.close() # This gives us the merges backdrop pdf file on which we will place the contents. # Now place the mulitpage content on this file for each page and assemble newpages = [] for j, page in enumerate(pages): print('outfilehere=', firstfile) reader = PdfFileReader(open(firstfile, 'rb')) first_page = reader.getPage(0) sup_reader = PdfFileReader(open(multioutput, 'rb')) sup_page = sup_reader.getPage(j) sup_page.mergePage(first_page) writer = PdfFileWriter() writer.addPage(sup_page) newoutfile = addpath1('reports/multipage'+str(j)+'.pdf') with open(newoutfile, 'wb') as f: writer.write(f) f.close() newpages.append(newoutfile) pdfcommand = ['pdfunite'] for page in newpages: print('page is:',page) pdfcommand.append(page) newmultioutput = addpath1(f'reports/newmultioutput'+'.pdf') pdfcommand.append(newmultioutput) tes = subprocess.check_output(pdfcommand) docref = f'reports/report.pdf' shutil.copy(newmultioutput, addpath1(docref)) return docref