Controllers.prototype.agencyReporting = (function(){ 'use strict'; var template = new Template(), schedDlg = new ReportDialog('sched'), downloadDropdownOpts = new DropdownObj([ {title: 'Download', value: '-', placeholder: true}, {title: 'Excel Spreadsheet', value: 'xlsx'}, {title: 'JSON', value: 'json'}, {title: 'CSV (zip)', value: 'zip'}, {title: 'Schedule Report', value: 'SR'}, {title: 'Billing Excel Spreadsheet', value: 'billing'}, ], '-', 'cool', 'cool', 'cool', true); return newAgencyReporting; function newAgencyReporting(core, target, data, returnCB){ return new AgencyReporting(core, target, data, returnCB); } function AgencyReporting(core, target, data, returnCB){ var events = new EventManager, element = getElementFromString(template.download), elements = new Elements(element, { 'from' : {}, 'to' : {}, 'downloadAnchor' : { 'target' : new Dropdown(element.querySelector('.downloadAnchor'), downloadDropdownOpts, downloadReport) } }); this.exit = exit; init(); function init(){ elements.from = new CalendarInput(element.querySelector('.fromInput'), null, updateFromDate); elements.to = new CalendarInput(element.querySelector('.toInput'), null, updateToDate); returnCB(); target.appendChild(element); } function updateFromDate(e){ if(isDefined(e)){ data.fromDate = e.year + '-' + getFormattedDate(e.monthValue) + '-' + getFormattedDate(e.date); } } function updateToDate(e){ if(isDefined(e)){ data.toDate = e.year + '-' + getFormattedDate(e.monthValue) + '-' + getFormattedDate(e.date); } } function downloadReport(e){ if(e === '-') return; if(e === 'SR') { schedDlg.show(null, function(data, canceled) { if(canceled) return true; new HttpRequest('POST', '/api/v1/scheduledReports/' + core.userID, new scheduledReportOpts(core.userID, null, 'agency', data.frequency, data.format, data.recipient), 'json', 'json', done); return true; }); return; } if(isValidReportRange(data)){ ga('send', 'event', 'Agency Reporting', 'Download report', 'AgencyId ' + core.userID + ': ' + data.fromDate + ' to ' + data.toDate); if(e === 'billing') { new HttpRequest('GET', getDownloadSource(core.userID, data, 'xlsx', true), null, null, 'json', done); } else { new HttpRequest('GET', getDownloadSource(core.userID, data, e), null, null, 'json', done); } } function done(v) { if(v.status === 'success') return pop('Pop!', 'Downloads', '/dashboard/downloads'); if(v.error) return core.notifications.setError('Error: ' + v.message); } } function exit(){ if(!!element) removeChild(target, element); if(!!events) events.reset(); if(!!elements) elements.exit(); housekeeping(); } function housekeeping(){ events = element = elements = null; } } function Template(){ var dlspan = 'Download detailed report', fromDate = 'from:
', toDate = 'to:
', anch = '
'; this.download = '
' + dlspan + fromDate + toDate + anch + '
'; } function isValidReportRange(d){ return d.fromDate !== '' && d.toDate !== ''; } function getFormattedDate(v){ if(v > 9) return v; else return '0' + v; } function getDownloadSource(uid, d, fmt, billing){ var fd = d.fromDate, td = d.toDate; return '/api/v1/agency' + (billing ? 'Billing' : '') + 'Report/' + uid + '/' + fd + '/' + td + '/' + fd + '_' + td + '_report.' + fmt; } })();