mirror of
https://github.com/nottinghamtec/PyRIGS.git
synced 2026-01-18 14:02:15 +00:00
Recollected static
This commit is contained in:
@@ -1,128 +1,130 @@
|
||||
function setupItemTable(items_json) {
|
||||
objectitems = JSON.parse(items_json)
|
||||
$.each(objectitems, function (key, val) {
|
||||
objectitems[key] = JSON.parse(val);
|
||||
})
|
||||
newitem = -1;
|
||||
}
|
||||
|
||||
function nl2br (str, is_xhtml) {
|
||||
var breakTag = (is_xhtml || typeof is_xhtml === 'undefined') ? '<br />' : '<br>';
|
||||
return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1'+ breakTag +'$2');
|
||||
}
|
||||
|
||||
function updatePrices() {
|
||||
var sum = 0;
|
||||
$('.sub-total').each(function() {
|
||||
sum += Number($(this).data('subtotal'));
|
||||
});
|
||||
$('#sumtotal').text(parseFloat(sum).toFixed(2));
|
||||
var vat = sum * Number($('#vat-rate').data('rate'));
|
||||
$('#vat').text(parseFloat(vat).toFixed(2));
|
||||
$('#total').text(parseFloat(sum+vat).toFixed(2));
|
||||
}
|
||||
// individual rows
|
||||
var sum = 0;
|
||||
for (var pk in objectitems) {
|
||||
var fields = objectitems[pk].fields;
|
||||
var sub = fields.cost * fields.quantity;
|
||||
$('#item-' + pk + ' .sub-total').html(parseFloat(sub).toFixed(2)).data('subtotal', sub);
|
||||
|
||||
function updateItemRow(pk) {
|
||||
$row = $('#item-'+pk)
|
||||
url = $row.data('url');
|
||||
$.ajax({
|
||||
url:url,
|
||||
success:function(r) {
|
||||
$row.replaceWith(r);
|
||||
updatePrices();
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
function addItemRow(url) {
|
||||
$tbody = $('#item-table tbody');
|
||||
$.ajax({
|
||||
url:url,
|
||||
success:function(r) {
|
||||
$tbody.append(r);
|
||||
updatePrices();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
var csrftoken = $.cookie('csrftoken');
|
||||
function csrfSafeMethod(method) {
|
||||
// these HTTP methods do not require CSRF protection
|
||||
return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
|
||||
}
|
||||
$.ajaxSetup({
|
||||
crossDomain: false, // obviates need for sameOrigin test
|
||||
beforeSend: function(xhr, settings) {
|
||||
if (!csrfSafeMethod(settings.type)) {
|
||||
xhr.setRequestHeader("X-CSRFToken", csrftoken);
|
||||
}
|
||||
sum += Number(sub);
|
||||
}
|
||||
});
|
||||
|
||||
$('#item-table').on('click', '.item-delete', function() {
|
||||
var row = $('#item-'+$(this).data('pk'));
|
||||
$.ajax({
|
||||
type:"POST",
|
||||
url:$(this).data('url'),
|
||||
success:function(r) {
|
||||
row.remove();
|
||||
updatePrices();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
$('#item-table').on('click', '.item-add', function() {
|
||||
$.ajax({
|
||||
url:$(this).data('url'),
|
||||
success:function(r) {
|
||||
$('#itemModal .modal-content').html(r);
|
||||
$('#item-table tbody').sortable('refresh');
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
$('#item-table').on('click', '.item-edit', function() {
|
||||
var url = $(this).data('url');
|
||||
$('#itemModal').data('pk', $(this).data('pk'));
|
||||
$.ajax({
|
||||
url:url,
|
||||
success:function(r) {
|
||||
$('#itemModal .modal-content').html(r);
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
$('#itemModal').on('hidden.bs.modal', function() {
|
||||
pk = $(this).data('pk');
|
||||
updateItemRow(pk);
|
||||
$('#itemModal .modal-content').html('');
|
||||
})
|
||||
|
||||
$('body').on('submit','.item-form', function(e) {
|
||||
e.preventDefault();
|
||||
url = $(this).data('url');
|
||||
data = $(this).serialize();
|
||||
$.ajax({
|
||||
type:'POST',
|
||||
url:url,
|
||||
data:data,
|
||||
success: function(r) {
|
||||
$('#itemModal .modal-content').html(r)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
function addItem(url) {
|
||||
$.get(url, function(r) {
|
||||
$('#item-table tbody').append(r);
|
||||
});
|
||||
$('#sumtotal').text(parseFloat(sum).toFixed(2));
|
||||
var vat = sum * Number($('#vat-rate').data('rate'));
|
||||
$('#vat').text(parseFloat(vat).toFixed(2));
|
||||
$('#total').text(parseFloat(sum + vat).toFixed(2));
|
||||
}
|
||||
|
||||
$('#item-table').on('click', '.item-delete', function () {
|
||||
delete objectitems[$(this).data('pk')]
|
||||
$('#item-' + $(this).data('pk')).remove();
|
||||
updatePrices();
|
||||
});
|
||||
|
||||
$('#item-table').on('click', '.item-add', function () {
|
||||
$('#item-form').data('pk', newitem);
|
||||
|
||||
// Set the form values
|
||||
$('#item_name').val('');
|
||||
$('#item_description').val('');
|
||||
$('#item_quantity').val('');
|
||||
$('#item_cost').val('');
|
||||
});
|
||||
|
||||
$('#item-table').on('click', '.item-edit', function () {
|
||||
// set the pk as we will need this later
|
||||
var pk = $(this).data('pk');
|
||||
$('#item-form').data('pk', pk);
|
||||
|
||||
// Set the form values
|
||||
var fields = objectitems[pk].fields;
|
||||
$('#item_name').val(fields.name);
|
||||
$('#item_description').val(fields.description);
|
||||
$('#item_quantity').val(fields.quantity);
|
||||
$('#item_cost').val(fields.cost);
|
||||
});
|
||||
|
||||
$('body').on('submit', '#item-form', function (e) {
|
||||
e.preventDefault();
|
||||
var pk = $(this).data('pk');
|
||||
$('#itemModal').modal('hide');
|
||||
|
||||
var fields;
|
||||
if (pk == newitem--) {
|
||||
// Create the new data structure and add it on.
|
||||
fields = new Object();
|
||||
fields['name'] = $('#item_name').val()
|
||||
fields['description'] = $('#item_description').val();
|
||||
fields['cost'] = $('#item_cost').val();
|
||||
fields['quantity'] = $('#item_quantity').val();
|
||||
|
||||
var order = 0;
|
||||
for (item in objectitems) {
|
||||
order++;
|
||||
}
|
||||
|
||||
fields['order'] = order;
|
||||
|
||||
objectitems[pk] = new Object();
|
||||
objectitems[pk]['fields'] = fields;
|
||||
|
||||
// Add the new table
|
||||
$('#new-item-row').clone().attr('id', 'item-' + pk).data('pk', pk).appendTo('#item-table-body');
|
||||
} else {
|
||||
// Existing item
|
||||
// update data structure
|
||||
fields = objectitems[pk].fields;
|
||||
fields.name = $('#item_name').val()
|
||||
fields.description = $('#item_description').val();
|
||||
fields.cost = $('#item_cost').val();
|
||||
fields.quantity = $('#item_quantity').val();
|
||||
objectitems[pk].fields = fields;
|
||||
|
||||
}
|
||||
// update the table
|
||||
$row = $('#item-' + pk);
|
||||
$row.find('.name').html(fields.name);
|
||||
$row.find('.description').html(nl2br(fields.description));
|
||||
$row.find('.cost').html(parseFloat(fields.cost).toFixed(2));
|
||||
$row.find('.quantity').html(fields.quantity);
|
||||
|
||||
updatePrices();
|
||||
});
|
||||
|
||||
$('body').on('submit', '.itemised_form', function (e) {
|
||||
$('#id_items_json').val(JSON.stringify(objectitems));
|
||||
});
|
||||
|
||||
// Return a helper with preserved width of cells
|
||||
var fixHelper = function(e, ui) {
|
||||
ui.children().each(function() {
|
||||
$(this).width($(this).width());
|
||||
});
|
||||
return ui;
|
||||
var fixHelper = function (e, ui) {
|
||||
ui.children().each(function () {
|
||||
$(this).width($(this).width());
|
||||
});
|
||||
return ui;
|
||||
};
|
||||
|
||||
$("#item-table tbody").sortable({
|
||||
helper: fixHelper,
|
||||
update: function(e, ui) {
|
||||
info = $(this).sortable("toArray");
|
||||
itemorder = new Array();
|
||||
$.each(info, function(key, value) {
|
||||
pk = $('#'+value).data('pk');
|
||||
itemorder[key] = pk;
|
||||
});
|
||||
data = JSON.stringify(itemorder);
|
||||
$.post($('#item-table').data('orderurl'), data);
|
||||
}
|
||||
helper: fixHelper,
|
||||
update: function (e, ui) {
|
||||
info = $(this).sortable("toArray");
|
||||
itemorder = new Array();
|
||||
$.each(info, function (key, value) {
|
||||
pk = $('#' + value).data('pk');
|
||||
objectitems[pk].fields.order = key;
|
||||
});
|
||||
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user