docparse-dumpinvoices
Version:
takes a list of couchdb invoice ids and exports the data contained within those invoices to an Excel spreadsheet
304 lines (268 loc) • 7.94 kB
JavaScript
var shoe = require('shoe');
var domready = require('domready');
var dnode = require('dnode');
var last_selected_row;
var server;
domready(function() {
var result = document.getElementById('result');
var stream = shoe('/output/realtime');
var d = dnode(function() {
this.alert = function(s, cb) {
alert('s: ' + JSON.stringify(s, null, ' '));
},
this.update_invoices_table = update_invoices_table_handler
});
d.on('remote', function(remote) {
server = remote;
});
d.pipe(stream).pipe(d);
$('tr.invoice_row')
.filter(':has(:checkbox:checked)')
.addClass('selected')
.end()
.die('click')
.live('click', function(event)
{
var isShift = event.shiftKey;
var isCtrl = event.ctrlKey;
var isMeta = event.metaKey;
// console.log('isShift: ' + isShift);
// console.log('isCtrl: ' + isCtrl);
// console.log('isMeta: ' + isMeta);
if (!isCtrl && !isMeta) {
deselect_all();
}
if (isShift) {
select_multiple($(this));
}
else {
last_selected_row = $(this);
}
console.log(last_selected_row);
$(this).toggleClass("success");
if (event.target.type !== "checkbox")
{
var checkbox = $(":checkbox", this);
checkbox.attr("checked", checkbox.is(':not(:checked)'));
}
$('input:checkbox', $(this)).focus();
});
$("tr.invoice_row a").click(function(e) {
e.stopPropagation();
});
});
/**
* Called when the invoices table has changed
* @param {Object} reply should have the following fields set
* @param {Object} reply.invoices_table_html
*/
var update_invoices_table_handler = function(err, reply) {
if (err) {
alert_error(err);
return;
}
if (!reply.hasOwnProperty('invoices_table_html') || !reply.invoices_table_html) {
alert_error('failed to get invoices table. Please try again later');
return
}
$('#invoices_table').html(reply.invoices_table_html);
}
$('#export_button').on('click', function(event) {
// dynamically create a new form
event.preventDefault();
var invoice_ids = get_selected_invoice_ids();
if (!invoice_ids || invoice_ids.length === 0) {
alert_info('no invoices selected');
return false;
}
else {
// var query_string = $.param(params);
var output = JSON.stringify(invoice_ids);
var form = document.createElement('form');
form.action = '/output/export/';
form.method = 'post';
var field = document.createElement('input');
field.type = 'hidden';
field.name = 'invoice_ids';
field.value = output;
form.appendChild(field);
document.body.appendChild(form);
form.submit();
return false;
}
});
$('#paid-toggle-button').toggleButtons({
onChange: function ($el, status, e) {
update_table();
}
});
$('#unpaid-toggle-button').toggleButtons({
onChange: function ($el, status, e) {
update_table();
}
});
$('#exported-toggle-button').toggleButtons({
onChange: function ($el, status, e) {
update_table();
}
});
$('#unexported-toggle-button').toggleButtons({
onChange: function ($el, status, e) {
update_table();
}
});
/**
* Select all rows between current_row and last_selected_row
*/
function select_multiple(current_row) {
if (!last_selected_row) {
last_selected_row = current_row
return;
}
var current_row_index = current_row.index();
var last_selected_row_index = last_selected_row.index();
console.log('current_row index: ' + current_row_index);
console.log('last_selected_row index: ' + last_selected_row_index);
var rows;
// selecting upward
if (current_row_index < last_selected_row_index) {
var end = last_selected_row.next();
rows= current_row.nextUntil(end);
}
// selecting downward
else {
rows = last_selected_row.nextUntil(current_row).andSelf();
}
rows.each(function () {
var checkbox = $(this).find('input.invoice');
var rowHTML = $(this).html();
if (rowHTML.match(/showing/)) {
return;
}
if (!checkbox) {
return;
}
if (!$(this).hasClass('success')) {
$(this).addClass('success');
}
checkbox.prop('checked', true).change();
});
}
function deselect_all() {
$('tr.invoice_row.success').each(function() {
if ($(this).hasClass('success')) {
$(this).removeClass('success');
}
var checkbox = $(this).find('input.invoice');
checkbox.prop('checked', false).change();
});
}
function getParameterByName(name) {
var match = RegExp('[?&]' + name + '=([^&]*)')
.exec(window.location.search);
return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
}
$('#export_button').on('click', function(event) {
// dynamically create a new form
var invoice_ids = get_selected_invoice_ids();
event.preventDefault();
// var query_string = $.param(params);
var output = JSON.stringify(invoice_ids);
window.location = '/output/export/?invoice_ids='+output
console.log(output);
return false;
});
var get_selected_invoice_ids = function() {
var invoice_ids = [];
var obj = $('.invoice:checked').each(function (index) {
invoice_ids.push($(this).attr('rel'));
});
return invoice_ids;
}
var get_params = function() {
var params = new Object();
var ignore_paid, ignore_unpaid, ignore_exported, ignore_unexported;
if (!$('#paid-checkbox').attr('checked')) {
params.ignore_paid = true;
}
if (!$('#unpaid-checkbox').attr('checked')) {
params.ignore_unpaid = true;
}
if (!$('#exported-checkbox').attr('checked')) {
params.ignore_exported = true;
}
if (!$('#unexported-checkbox').attr('checked')) {
params.ignore_unexported = true;
}
var supplier_code = getParameterByName('supplier_code');
if (supplier_code) {
params.supplier_code = supplier_code;
}
return params;
}
var update_table = function() {
// build the url
if (!server) {
alert_error('not connected to server. Please reload the page and try again later');
}
var query_string,
url;
var params = get_params();
console.log('params: ' + JSON.stringify(params, null, ' '));
var data = {
params: params,
}
server.update_invoices_table(data, function (err, reply) {
console.log(err);
console.log(reply);
var invoices_table = $('#invoices_table');
invoices_table.html(reply.invoice_table_html);
});
// query_string = $.param(params);
// url = '/output/table/?'+query_string;
// console.log(url);
// $.ajax({
// type: 'GET',
// url: url,
// dataType: 'html',
// success: function(data) {
// $("#invoice_content").html(data);
// if (data.match(/No Invoices found/)) {
// $('#export_button').hide();
// }
// else {
// $('#export_button').show();
// $('#export_button').css('margin-bottom', '1em');
// }
// },
// error: function(error) {
// alert('an error occurred, error: ' + error);
}
function hide_all_alerts(callback) {
$('.alert').each(function() {
var alert = $(this)
$(this).slideUp(100).promise().done(function() {
alert.close();
})
}).promise().done(function () {
callback();
});
}
function alert_error(message) {
var alert_class = 'alert-error'
alert_write(message, alert_class);
}
function alert_success(message) {
var alert_class = 'alert-success'
alert_write(message, alert_class);
}
function alert_info(message) {
var alert_class = 'alert-info'
alert_write(message, alert_class);
}
function alert_write(message, alert_class) {
var alerts_div = $('#alerts_container');
var alert = $('<div class="alert '+alert_class+' fade in"><button type="button" class="close" data-dismiss="alert">x</button>'+message+'</div>');
alert.appendTo(alerts_div);
alerts_div.slideDown();
}