Allow edit or delete of supplier part price breaks from part pricing overview page

This commit is contained in:
Oliver Walters 2023-02-21 22:30:05 +11:00
parent 1afe982241
commit 4028ab1837
4 changed files with 92 additions and 27 deletions

View File

@ -295,7 +295,9 @@ $("#barcode-unlink").click(function() {
{% endif %} {% endif %}
loadSupplierPriceBreakTable({ loadSupplierPriceBreakTable({
part: {{ part.pk }} part: {{ part.pk }},
allowEdit: {% js_bool roles.purchase_order.change %},
allowDelete: {% js_bool roles.purchase_order.delete %},
}); });
$('#new-price-break').click(function() { $('#new-price-break').click(function() {

View File

@ -41,6 +41,8 @@ loadPurchasePriceHistoryTable({
// Supplier pricing information // Supplier pricing information
loadPartSupplierPricingTable({ loadPartSupplierPricingTable({
part: {{ part.pk }}, part: {{ part.pk }},
allowEdit: {% js_bool roles.purchase_order.change %},
allowDelete: {% js_bool roles.purchase_order.delete %},
}); });
{% endif %} {% endif %}

View File

@ -1144,33 +1144,43 @@ function loadSupplierPartTable(table, url, options) {
*/ */
function loadSupplierPriceBreakTable(options={}) { function loadSupplierPriceBreakTable(options={}) {
if (!options.part) {
console.error('No part provided to loadPurchasePriceHistoryTable');
return;
}
var table = options.table || $('#price-break-table'); var table = options.table || $('#price-break-table');
// Setup button callbacks once table is loaded // Setup button callbacks once table is loaded
function setupCallbacks() { function setupCallbacks() {
table.find('.button-price-break-delete').click(function() {
var pk = $(this).attr('pk');
constructForm(`/api/company/price-break/${pk}/`, { if (options.allowDelete) {
method: 'DELETE', table.find('.button-price-break-delete').click(function() {
title: '{% trans "Delete Price Break" %}', var pk = $(this).attr('pk');
onSuccess: function() {
table.bootstrapTable('refresh'); constructForm(`/api/company/price-break/${pk}/`, {
}, method: 'DELETE',
title: '{% trans "Delete Price Break" %}',
onSuccess: function() {
table.bootstrapTable('refresh');
},
});
}); });
}); }
table.find('.button-price-break-edit').click(function() { if (options.allowDelete) {
var pk = $(this).attr('pk'); table.find('.button-price-break-edit').click(function() {
var pk = $(this).attr('pk');
constructForm(`/api/company/price-break/${pk}/`, { constructForm(`/api/company/price-break/${pk}/`, {
fields: supplierPartPriceBreakFields(), fields: supplierPartPriceBreakFields(),
title: '{% trans "Edit Price Break" %}', title: '{% trans "Edit Price Break" %}',
onSuccess: function() { onSuccess: function() {
table.bootstrapTable('refresh'); table.bootstrapTable('refresh');
} }
});
}); });
}); }
} }
setupFilterList('supplierpricebreak', table, '#filter-list-supplierpricebreak'); setupFilterList('supplierpricebreak', table, '#filter-list-supplierpricebreak');
@ -1211,14 +1221,21 @@ function loadSupplierPriceBreakTable(options={}) {
}, },
{ {
field: 'updated', field: 'updated',
title: '{% trans "Last updated" %}', title: '{% trans "Last Updated" %}',
sortable: true, sortable: true,
formatter: function(value, row) { formatter: function(value, row) {
var html = renderDate(value); var html = renderDate(value);
html += `<div class='btn-group float-right' role='group'>`; html += `<div class='btn-group float-right' role='group'>`;
html += makeIconButton('fa-edit icon-blue', 'button-price-break-edit', row.pk, '{% trans "Edit price break" %}');
html += makeIconButton('fa-trash-alt icon-red', 'button-price-break-delete', row.pk, '{% trans "Delete price break" %}'); if (options.allowEdit) {
html += makeIconButton('fa-edit icon-blue', 'button-price-break-edit', row.pk, '{% trans "Edit price break" %}');
}
if (options.allowDelete) {
html += makeIconButton('fa-trash-alt icon-red', 'button-price-break-delete', row.pk, '{% trans "Delete price break" %}');
}
html += `</div>`; html += `</div>`;
return html; return html;

View File

@ -427,6 +427,39 @@ function loadPartSupplierPricingTable(options={}) {
options.params.base_part = part; options.params.base_part = part;
options.params.supplier_detail = true; options.params.supplier_detail = true;
options.params.part_detail = true; options.params.part_detail = true;
options.params.ordering = '-price';
// Setup button callbacks once table is loaded
function setupCallbacks() {
if (options.allowDelete) {
table.find('.button-price-break-delete').click(function() {
var pk = $(this).attr('pk');
constructForm(`/api/company/price-break/${pk}/`, {
method: 'DELETE',
title: '{% trans "Delete Price Break" %}',
onSuccess: function() {
table.bootstrapTable('refresh');
},
});
});
}
if (options.allowDelete) {
table.find('.button-price-break-edit').click(function() {
var pk = $(this).attr('pk');
constructForm(`/api/company/price-break/${pk}/`, {
fields: supplierPartPriceBreakFields(),
title: '{% trans "Edit Price Break" %}',
onSuccess: function() {
table.bootstrapTable('refresh');
}
});
});
}
}
table.inventreeTable({ table.inventreeTable({
url: '{% url "api-part-supplier-price-list" %}', url: '{% url "api-part-supplier-price-list" %}',
@ -441,6 +474,7 @@ function loadPartSupplierPricingTable(options={}) {
formatNoMatches: function() { formatNoMatches: function() {
return '{% trans "No supplier pricing data available" %}'; return '{% trans "No supplier pricing data available" %}';
}, },
onPostBody: setupCallbacks,
onLoadSuccess: function(data) { onLoadSuccess: function(data) {
// Update supplier pricing chart // Update supplier pricing chart
@ -511,16 +545,26 @@ function loadPartSupplierPricingTable(options={}) {
} }
// Convert to unit pricing // Convert to unit pricing
var unit_price = row.price / row.part_detail.pack_size; let unit_price = row.price / row.part_detail.pack_size;
var html = formatCurrency(unit_price, { let html = formatCurrency(unit_price, {
currency: row.price_currency currency: row.price_currency
}); });
if (row.updated != null) { if (options.allowEdit || options.allowDelete) {
html += `<span class='badge badge-right rounded-pill bg-dark'>${renderDate(row.updated)}</span>`;
}
html += `<div class='btn-group float-right' role='group'>`;
if (options.allowEdit) {
html += makeIconButton('fa-edit icon-blue', 'button-price-break-edit', row.pk, '{% trans "Edit price break" %}');
}
if (options.allowDelete) {
html += makeIconButton('fa-trash-alt icon-red', 'button-price-break-delete', row.pk, '{% trans "Delete price break" %}');
}
html += `</div>`;
}
return html; return html;
} }