Skip to content

Instantly share code, notes, and snippets.

@Matt-Jensen
Last active August 29, 2015 14:14
Show Gist options
  • Save Matt-Jensen/9dc5b00bae4e167b31c3 to your computer and use it in GitHub Desktop.
Save Matt-Jensen/9dc5b00bae4e167b31c3 to your computer and use it in GitHub Desktop.
Expensify Invoice Constructor
function Invoicerizer(date, duration, tasks) {
this.date = date;
this.duration = duration;
this.tasks = tasks;
}
Invoicerizer.prototype = {
init: function() {
$('.expensicons.expensicons-plus').trigger('click');
this.wait(2000)
.then( this.createExpense.bind(this) )
.then( this.openTimeTab.bind(this) )
.then( this.fillInInputs.bind(this) )
.then( this.fillInDate.bind(this) )
.then( function() {
$('.dialog_wrapper .js_save.expenseFormSave:last').trigger('click');
});
},
createExpense: function() {
$('#createexpense_button').trigger('click');
return this.wait(1000);
},
openTimeTab: function() {
$('li[data-mode=time]').trigger('click');
return this.wait(500);
},
fillInInputs: function() {
$('form[data-mode=time] input[name=hour]:first').val(this.duration);
$('form[data-mode=time] input[name=rate]:first').val(50);
$('form[data-mode=time] input[name=reimbursable]').prop('checked', true);
$('form[data-mode=time] [name=category]').val('21');
$('form[data-mode=time] input[name=comment]:first').val(this.tasks);
return this.wait(0);
},
fillInDate: function() {
var self = this;
var monthIndex = this._getDateMonthIndex();
$('form[data-mode=time] [name=created]').focus();
var setTheMonth = function() {
window.setTimeout(function() {
var diff = self._currentMonthDiff( monthIndex );
if( diff === 0 ) return;
else diff > 0 ? self._goToPrevMonth() : self._goToNextMonth();
return setTheMonth();
});
};
setTheMonth();
return this.wait(1000)
.then(function() {
var day = self._getDateDay();
$('#ui-datepicker-div td:contains('+ day +')')
.filter(function(i, el) { return $(el).text() === day; })
.trigger('click');
})
.then(function() { return self.wait(); });
},
wait: function(time) {
return new Promise(function(response, rej) {
if( !time ) time = 1000;
window.setTimeout(function() {
response.resolve();
}, time);
});
},
_getDateDay: function() {
return this.date.split('/')[1];
},
_getDateMonthIndex: function() {
return parseInt(this.date.split('/')[0], 10) - 1;
},
_currentMonthDiff: function(monthIndex) {
return this._getMonthIndex( $('#ui-datepicker-div .ui-datepicker-month').text() ) - monthIndex;
},
_goToNextMonth: function() {
return window.setTimeout(function() {
$('#ui-datepicker-div .ui-datepicker-next').click();
});
},
_goToPrevMonth: function() {
return window.setTimeout(function() {
$('#ui-datepicker-div .ui-datepicker-prev').click();
});
},
_getMonthIndex: function(month) {
month = month.toLowerCase().trim();
months = ['january', 'february', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december'];
return months.indexOf(month);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment