Skip to content

Instantly share code, notes, and snippets.

@garrettjoecox
Last active November 8, 2019 15:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save garrettjoecox/bcbbbebe9922fe420c30129254274144 to your computer and use it in GitHub Desktop.
Save garrettjoecox/bcbbbebe9922fe420c30129254274144 to your computer and use it in GitHub Desktop.
try {
if(window._xProducts) {
window._xProducts.print_barcodes = function (skus, type = 'barcode') {
var _this = this;
var title = 'How many barcodes do you want to print?';
var names = {};
_xDatatables.api.data().toArray().forEach(function (item) {
names[item.sku] = item.name;
});
if (type == 'location') {
title = 'How many location barcodes do you want to print?';
}
var containerEl = document.createElement('div');
skus.forEach(function (sku, index) {
var itemEl = document.createElement('div');
itemEl.setAttribute('style', 'display: flex; align-items: baseline; justify-content: space-between; padding: 8px 0px; border-bottom: 1px solid #DDD;');
var labelEl = document.createElement('label');
labelEl.innerHTML = names[sku] + '(' + sku + ')';
itemEl.appendChild(labelEl);
var inputEl = document.createElement('input');
inputEl.setAttribute('id', 'bootbox-input-' + index);
inputEl.setAttribute('class', 'form-control');
inputEl.setAttribute('type', 'number');
inputEl.setAttribute('value', 0);
itemEl.appendChild(inputEl);
containerEl.appendChild(itemEl);
});
bootbox.dialog({
title: title,
message: containerEl.outerHTML,
buttons: {
cancel: {
label: "Cancel",
className: 'btn btn-default',
callback: function () { }
},
ok: {
label: 'OK',
className: 'btn btn-primary',
callback: function () {
var products = skus.map(function (sku, index) {
var inputEl = document.querySelector('#bootbox-input-' + index);
return {
quantity: inputEl ? inputEl.value : 0,
sku: encodeURI(sku),
id: _this.selected_products[index],
};
}).filter(function (product) {
return !isNaN(product.quantity) && product.quantity > 0;
});
if (products.length) {
$.post('/dashboard/products/printBarcode', {
products: JSON.stringify(products),
type: type
})
.done(function (json) {
$('button[data-loading-text]').button('reset');
if (json.status == 'error') {
growl_error('Error Printing Barcode', json.message);
} else {
growl_success('Barcode Printed', 'Your barcode has been sent to the printer');
}
})
.fail(function (jqxhr, textStatus, error) {
$('button[data-loading-text]').button('reset');
growl_error('Error Printing Barcode', 'Something went wrong. Please try again.');
});
} else {
$('button[data-loading-text]').button('reset');
}
}
}
}
});
};
console.log('[ShipHero Ext]: Loaded');
}
if (document.querySelector('.span3 .widget:last-child .order_details_buttons') && document.querySelectorAll('.span9 .widget:last-child tbody tr').length) {
var idBySku = {};
var poRowEls = document.querySelectorAll('table.table.po-order-items tbody tr');
for (var i = 0; i < poRowEls.length; i++) {
poRowEl = poRowEls[i];
poRowInputEls = poRowEl.querySelectorAll('input');
poRowHref = poRowEl.querySelector('a');
if (poRowInputEls.length >= 3) {
idBySku[poRowInputEls[1].value] = poRowHref.href.split('/').pop();
}
}
var dataByDate = {};
var rowEls = document.querySelectorAll('.span9 .widget:last-child tbody tr');
for (var i = 0; i < rowEls.length; i++) {
var rowEl = rowEls[i];
var dateEl = rowEl.children[0];
if (!dateEl) continue;
var date = moment(dateEl.textContent).format('YYYY-MM-DD');
var updateEls = rowEl.children[2].children;
for (var j = 0; j < updateEls.length; j++) {
var updateEl = updateEls[j];
if (!updateEl) continue;
if (updateEl.children.length && updateEl.children[0].textContent === 'Quantity Received') {
var before = parseInt(updateEl.children[1].textContent, 10);
var after = parseInt(updateEl.children[2].textContent, 10);
var sku = updateEl.children[3].textContent;
if (!dataByDate.hasOwnProperty(date)) dataByDate[date] = {};
if (!dataByDate[date].hasOwnProperty(sku)) dataByDate[date][sku] = 0;
dataByDate[date][sku] += (after - before);
}
}
}
var printBtnEl = document.createElement('button');
printBtnEl.setAttribute('class', 'btn btn-small');
printBtnEl.innerHTML = 'Print Recieved By Day';
document.querySelector('.span3 .widget:last-child .order_details_buttons').appendChild(printBtnEl);
printBtnEl.addEventListener('click', function () {
bootbox.prompt({
title: 'Select the day to print for',
inputType: 'date',
callback: function (selectedDate) {
if (!selectedDate) return;
var products = Object.keys(dataByDate[selectedDate] || []).map(function (sku) {
return {
quantity: '' + dataByDate[selectedDate][sku] + '',
sku: encodeURI(sku),
id: idBySku[sku],
};
}).filter(function (product) {
return !isNaN(product.quantity) && product.quantity > 0;
});
if (products.length) {
$.post('/dashboard/products/printBarcode', {
products: JSON.stringify(products),
type: 'barcode'
})
.done(function (json) {
if (json.status == 'error') {
growl_error('Error Printing Barcode', json.message);
} else {
growl_success('Barcode Printed', 'Your barcode has been sent to the printer');
}
})
.fail(function (jqxhr, textStatus, error) {
growl_error('Error Printing Barcode', 'Something went wrong. Please try again.');
});
} else {
growl_error('Error Printing Barcode', 'Nothing was available for print on the selected date');
}
}
});
});
console.log('[ShipHero Ext]: Loaded');
}
var isKit = Array.prototype.slice.call(document.querySelectorAll('strong'))
.filter(function (el) {
return el.textContent === 'Build Kit:'
})[0];
isKit = !!isKit && isKit.nextSibling && isKit.nextSibling.textContent === ' Yes';
if (isKit) {
window._xShiphero.load_modal = function(url) {
$('.modal-backdrop').remove();
$('#shModal').removeData('modal');
$('#shModal .modal-body').html('');
$('#shModal').modal({
remote: url
});
$('#shModal').on('shown.bs.modal', function(e) {
var modal = e.target;
var data = _xShiphero.datatables[0].data();
var sumPrice = Math.round(data.reduce(function(a, i) {
var quantity = parseInt(i.quantity, 10);
return a + (parseFloat(i.price) * quantity);
}, 0) * 100) / 100;
var sumValue = Math.round(data.reduce(function(a, i) {
var quantity = parseInt(i.quantity, 10);
return a + (parseFloat(i.value) * quantity);
}, 0) * 100) / 100;
var priceInput = modal.querySelector('input[name=price]');
var priceHelpText = priceInput.parentElement.querySelector('.text-info')
if (!priceHelpText) {
priceHelpText = document.createElement('small');
priceHelpText.setAttribute('class', 'help-text text-info');
priceInput.parentElement.appendChild(priceHelpText);
}
priceHelpText.textContent = "(" + sumPrice + ")";
var valueInput = modal.querySelector('input[name=value]');
var valueHelpText = valueInput.parentElement.querySelector('.text-info')
if (!valueHelpText) {
valueHelpText = document.createElement('small');
valueHelpText.setAttribute('class', 'help-text text-info');
valueInput.parentElement.appendChild(valueHelpText);
}
valueHelpText.textContent = "(" + sumValue + ")";
});
};
console.log('[ShipHero Ext]: Loaded');
}
} catch (error) {
console.error('[ShipHero Ext]: Error loading script');
console.error(error);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment