Fix auto pricelist resolution and latest-price selection; update Bible
This commit is contained in:
@@ -433,6 +433,11 @@ let selectedPricelistIds = {
|
||||
warehouse: null,
|
||||
competitor: null
|
||||
};
|
||||
let resolvedAutoPricelistIds = {
|
||||
estimate: null,
|
||||
warehouse: null,
|
||||
competitor: null
|
||||
};
|
||||
let disablePriceRefresh = false;
|
||||
let onlyInStock = false;
|
||||
let activePricelistsBySource = {
|
||||
@@ -498,6 +503,22 @@ function formatDelta(abs, pct) {
|
||||
return sign + formatMoney(absValue) + ' (' + pctSign + Math.round(Math.abs(pct)) + '%)';
|
||||
}
|
||||
|
||||
function getEffectivePricelistID(source) {
|
||||
const explicit = selectedPricelistIds[source];
|
||||
if (Number.isFinite(explicit) && explicit > 0) {
|
||||
return Number(explicit);
|
||||
}
|
||||
const resolvedAuto = resolvedAutoPricelistIds[source];
|
||||
if (Number.isFinite(resolvedAuto) && resolvedAuto > 0) {
|
||||
return Number(resolvedAuto);
|
||||
}
|
||||
const fallback = activePricelistsBySource[source]?.[0]?.id;
|
||||
if (Number.isFinite(fallback) && fallback > 0) {
|
||||
return Number(fallback);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
async function refreshPriceLevels(options = {}) {
|
||||
const force = options.force === true;
|
||||
const noCache = options.noCache === true;
|
||||
@@ -543,12 +564,10 @@ async function refreshPriceLevels(options = {}) {
|
||||
if (data.resolved_pricelist_ids) {
|
||||
['estimate', 'warehouse', 'competitor'].forEach(source => {
|
||||
if (!selectedPricelistIds[source] && data.resolved_pricelist_ids[source]) {
|
||||
selectedPricelistIds[source] = data.resolved_pricelist_ids[source];
|
||||
resolvedAutoPricelistIds[source] = Number(data.resolved_pricelist_ids[source]);
|
||||
}
|
||||
});
|
||||
syncPriceSettingsControls();
|
||||
renderPricelistSettingsSummary();
|
||||
persistLocalPriceSettings();
|
||||
}
|
||||
} catch(e) {
|
||||
console.error('Failed to refresh price levels', e);
|
||||
@@ -581,11 +600,7 @@ function schedulePriceLevelsRefresh(options = {}) {
|
||||
}
|
||||
|
||||
function currentWarehousePricelistID() {
|
||||
const id = selectedPricelistIds.warehouse;
|
||||
if (Number.isFinite(id) && id > 0) return Number(id);
|
||||
const fallback = activePricelistsBySource.warehouse?.[0]?.id;
|
||||
if (Number.isFinite(fallback) && fallback > 0) return Number(fallback);
|
||||
return null;
|
||||
return getEffectivePricelistID('warehouse');
|
||||
}
|
||||
|
||||
async function loadWarehouseInStockLots() {
|
||||
@@ -823,9 +838,7 @@ async function loadActivePricelists(force = false) {
|
||||
if (existing && activePricelistsBySource[source].some(pl => Number(pl.id) === Number(existing))) {
|
||||
return;
|
||||
}
|
||||
selectedPricelistIds[source] = activePricelistsBySource[source].length > 0
|
||||
? Number(activePricelistsBySource[source][0].id)
|
||||
: null;
|
||||
selectedPricelistIds[source] = null;
|
||||
} catch (e) {
|
||||
activePricelistsBySource[source] = [];
|
||||
selectedPricelistIds[source] = null;
|
||||
@@ -961,6 +974,15 @@ function applyPriceSettings() {
|
||||
selectedPricelistIds.estimate = Number.isFinite(estimateVal) && estimateVal > 0 ? estimateVal : null;
|
||||
selectedPricelistIds.warehouse = Number.isFinite(warehouseVal) && warehouseVal > 0 ? warehouseVal : null;
|
||||
selectedPricelistIds.competitor = Number.isFinite(competitorVal) && competitorVal > 0 ? competitorVal : null;
|
||||
if (selectedPricelistIds.estimate) {
|
||||
resolvedAutoPricelistIds.estimate = null;
|
||||
}
|
||||
if (selectedPricelistIds.warehouse) {
|
||||
resolvedAutoPricelistIds.warehouse = null;
|
||||
}
|
||||
if (selectedPricelistIds.competitor) {
|
||||
resolvedAutoPricelistIds.competitor = null;
|
||||
}
|
||||
disablePriceRefresh = disableVal;
|
||||
onlyInStock = inStockVal;
|
||||
|
||||
@@ -1861,7 +1883,8 @@ async function previewArticle() {
|
||||
if (!el) return;
|
||||
|
||||
const model = serverModelForQuote.trim();
|
||||
if (!model || !selectedPricelistIds.estimate || cart.length === 0) {
|
||||
const estimatePricelistID = getEffectivePricelistID('estimate');
|
||||
if (!model || !estimatePricelistID || cart.length === 0) {
|
||||
currentArticle = '';
|
||||
el.textContent = 'Артикул: —';
|
||||
return;
|
||||
@@ -1874,7 +1897,7 @@ async function previewArticle() {
|
||||
body: JSON.stringify({
|
||||
server_model: serverModelForQuote,
|
||||
support_code: supportCode,
|
||||
pricelist_id: selectedPricelistIds.estimate,
|
||||
pricelist_id: estimatePricelistID,
|
||||
items: cart.map(item => ({
|
||||
lot_name: item.lot_name,
|
||||
quantity: item.quantity,
|
||||
@@ -2408,13 +2431,19 @@ async function refreshPrices() {
|
||||
updatePriceUpdateDate(config.price_updated_at);
|
||||
}
|
||||
if (config.pricelist_id) {
|
||||
selectedPricelistIds.estimate = config.pricelist_id;
|
||||
if (selectedPricelistIds.estimate) {
|
||||
selectedPricelistIds.estimate = config.pricelist_id;
|
||||
} else {
|
||||
resolvedAutoPricelistIds.estimate = Number(config.pricelist_id);
|
||||
}
|
||||
if (!activePricelistsBySource.estimate.some(opt => Number(opt.id) === Number(config.pricelist_id))) {
|
||||
await loadActivePricelists();
|
||||
}
|
||||
syncPriceSettingsControls();
|
||||
renderPricelistSettingsSummary();
|
||||
persistLocalPriceSettings();
|
||||
if (selectedPricelistIds.estimate) {
|
||||
persistLocalPriceSettings();
|
||||
}
|
||||
}
|
||||
|
||||
// Re-render UI
|
||||
|
||||
Reference in New Issue
Block a user