|
(function($) { |
|
var hostname = window.location.hostname; |
|
|
|
function getEnrollmentDate(distributorID) { |
|
var deferEnrollment = $.Deferred(); |
|
distributorID = distributorID || getDistributorID(); |
|
|
|
var requests = []; |
|
|
|
// Get the enrollment date for distributorID |
|
$.post( |
|
'https://' + hostname + '/index.cfm', |
|
{ |
|
Fuseaction: 'evo_Modules.DetailedGenealogy', |
|
TargetID: distributorID, |
|
DETAIL_FOCUSID: distributorID, |
|
DETAIL_START_LEVEL: 0, |
|
DETAIL_END_LEVEL: 0 |
|
} |
|
).done(function(data) { |
|
var doc = $(data); |
|
var row = $('#DetailGen' + distributorID, doc); |
|
var enrollDate = $('td', row).slice(3, 4).text().replace(/[^0-9]/g, ''); |
|
var year = parseInt(enrollDate.slice(-4)); |
|
var month = parseInt(enrollDate.slice(0,2)); |
|
deferEnrollment.resolve(year, month); |
|
}); |
|
|
|
return deferEnrollment; |
|
} |
|
|
|
function getVolumes(distributorID) { |
|
var deferVolumes = $.Deferred(); |
|
distributorID = distributorID || getDistributorID(); |
|
|
|
var requests = []; |
|
|
|
// Get the enrollment date for distributorID. |
|
getEnrollmentDate(distributorID).done(function(enrollYear, enrollMonth) { |
|
// Get the volume from the enrollment date until last month. |
|
var now = new Date(); |
|
// Use the last day of the month. |
|
var previousMonth = new Date(now.getFullYear(), now.getMonth(), -1); |
|
var currentMonth = new Date(enrollYear, enrollMonth, -1); |
|
|
|
while(currentMonth <= previousMonth) { |
|
console.log('Retrieving volume for ' + currentMonth); |
|
requests.push(getVolume(distributorID, currentMonth)); |
|
|
|
// Use the last day of the month. |
|
currentMonth = new Date( |
|
currentMonth.getFullYear(), |
|
currentMonth.getMonth() + 2, |
|
-1); |
|
} |
|
|
|
$.when.apply(this, requests).then(function() { |
|
var args = Array.prototype.slice.call(arguments); |
|
deferVolumes.resolve(args); |
|
}); |
|
}); |
|
|
|
return deferVolumes; |
|
} |
|
|
|
function getVolume(distributorID, period) { |
|
var deferVolume = $.Deferred(); |
|
var year = period.getFullYear(); |
|
var month = ('0' + (period.getMonth() + 1)).slice(-2); |
|
var jqxhr = $.post( |
|
'https://' + hostname + '/index.cfm', |
|
{ |
|
Fuseaction: 'evo_Modules.Qualifications', |
|
TargetID: distributorID, |
|
viewpvdate: year + month |
|
} |
|
); |
|
|
|
jqxhr.done(function(data, textStatus, jqXHR) { |
|
var doc = $(data); |
|
var stat = {}; |
|
|
|
stat.period = year + month; |
|
stat.lastDay = month + '/' + period.getDate() + '/' + year; |
|
|
|
stat.pv = parseFloat($('.PVClick table table', doc) |
|
.attr('title') |
|
.replace(/[^0-9\.]/g, '') |
|
.match(/([0-9]*[\.,][0-9]*)$/)[1]); |
|
|
|
$("font[color]", doc).each(function() { |
|
var ele = $(this); |
|
var onclick = ele.parents("[onclick]"); |
|
var action = onclick.attr('onclick'); |
|
if (action) { |
|
var text = ele.text().replace(/[^0-9\.]/g, ''); |
|
if (action.search(/currentfieldlist\=WRK31/) >= 0) { |
|
stat.tv = parseFloat(text); |
|
} else if (action.search(/currentfieldlist\=VOL3/) >= 0) { |
|
stat.ov = parseFloat(text); |
|
} |
|
} |
|
}); |
|
|
|
deferVolume.resolve(stat); |
|
}); |
|
|
|
return deferVolume; |
|
} |
|
|
|
function downloadVolumes(distributorID) { |
|
distributorID = distributorID || getDistributorID(); |
|
|
|
getVolumes(distributorID).done(function(volumes) { |
|
volumes.sort(function(a, b){ |
|
if(a.period < b.period) return -1; |
|
if(a.period > b.period) return 1; |
|
return 0; |
|
}); |
|
|
|
var data = [['Month', 'PV', 'TV', 'OV']]; |
|
|
|
volumes.forEach(function(stat) { |
|
data.push([stat.lastDay, stat.pv, stat.tv, stat.ov]); |
|
}); |
|
|
|
downloadCSV(distributorID, data); |
|
}); |
|
} |
|
|
|
function downloadCSV(distributorID, data) { |
|
var csvContent = "data:text/csv;charset=utf-8,"; |
|
var rows = []; |
|
data.forEach(function(infoArray, index){ |
|
rows.push(infoArray.join(",")); |
|
}); |
|
csvContent += rows.join('\n'); |
|
|
|
var encodedUri = encodeURI(csvContent); |
|
var link = document.createElement("a"); |
|
link.setAttribute("href", encodedUri); |
|
link.setAttribute("download", 'volume-' + distributorID + '.csv'); |
|
|
|
console.log('CSV file downloading...'); |
|
link.click(); |
|
console.log('CSV file downloaded'); |
|
} |
|
|
|
// Use the navigation links to return the distributorID |
|
function getDistributorID() { |
|
return $('#nav a') |
|
.attr('onclick') |
|
.match(/DistributorID\=([0-9]*)/i)[1]; |
|
} |
|
|
|
// Allow for calling externally. |
|
window.doTerraDownloadVolumes = downloadVolumes; |
|
})(jQuery); |