Kundenentscheid 26.08.: Rohstoffe werden in Gramm geführt, dürfen aber in ml erfasst werden; über die Dichte ergeben sich die Gramm (100 ml Propolistinktur × 0,79 = 79 g). - Neues Feld ingredients.density (g/ml, optional) inkl. Feld im INCI-Formular. - Wareneingang: Einheiten-Umschalter g/ml am Mengenfeld (nur wenn Dichte hinterlegt; Live-Hinweis mit Gramm-Umrechnung, Dichte kommt aus der Rohstoff-Suche mit). Erfassung in ml wird über die Dichte in Gramm umgerechnet und kanonisch als Gramm gespeichert — InventoryService und alle Aggregationen bleiben unverändert. purchase_unit + density_used (Snapshot) protokollieren die ml-Erfassung für Anzeige/Bearbeitung; Detailseite zeigt „790 g (= 1.000 ml)". Wareneingang-Buchung rechnet gelieferte ml ebenfalls um. - Serverseitige Validierung: ml nur mit hinterlegter Dichte. Migrationen add_density_* auf Dev-/Live-DB. 4 Tests, Suite 318 grün. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
170 lines
6.4 KiB
PHP
170 lines
6.4 KiB
PHP
<script>
|
|
(function ($) {
|
|
function toggleBlocks() {
|
|
var t = $('#entry_type').val();
|
|
var isIng = t === 'ingredient';
|
|
$('#stock-entry-ingredient-block').toggle(isIng);
|
|
$('#stock-entry-quality-block').toggle(isIng);
|
|
$('#stock-entry-packaging-block').toggle(!isIng);
|
|
$('#price-per-kg-block').toggle(isIng);
|
|
$('#price-total-block').toggle(!isIng);
|
|
}
|
|
|
|
function initIngredientSelect2() {
|
|
var $el = $('#ingredient_id');
|
|
if ($el.data('select2')) {
|
|
$el.select2('destroy');
|
|
}
|
|
$el.select2({
|
|
theme: 'default',
|
|
width: '100%',
|
|
placeholder: '{{ __('Inhaltsstoff suchen…') }}',
|
|
allowClear: true,
|
|
ajax: {
|
|
url: $el.data('search-url'),
|
|
dataType: 'json',
|
|
delay: 250,
|
|
data: function (params) {
|
|
return {q: params.term || ''};
|
|
},
|
|
processResults: function (data) {
|
|
return {results: data.results || []};
|
|
},
|
|
cache: true
|
|
},
|
|
minimumInputLength: 1
|
|
});
|
|
}
|
|
|
|
function initPackagingSelect2() {
|
|
var $el = $('#packaging_item_id');
|
|
if ($el.data('select2')) {
|
|
$el.select2('destroy');
|
|
}
|
|
$el.select2({
|
|
theme: 'default',
|
|
width: '100%',
|
|
placeholder: '{{ __('Verpackungsartikel suchen…') }}',
|
|
allowClear: true,
|
|
ajax: {
|
|
url: $el.data('search-url'),
|
|
dataType: 'json',
|
|
delay: 250,
|
|
data: function (params) {
|
|
return {
|
|
q: params.term || '',
|
|
entry_type: $('#entry_type').val()
|
|
};
|
|
},
|
|
processResults: function (data) {
|
|
return {results: data.results || []};
|
|
},
|
|
cache: true
|
|
},
|
|
minimumInputLength: 0
|
|
});
|
|
}
|
|
|
|
// AP-42: aktuelle Dichte des gewählten Rohstoffs (aus dem AJAX-Ergebnis oder
|
|
// — bei vorbelegtem Rohstoff — aus der data-density-Option).
|
|
function currentDensity() {
|
|
var sel = $('#ingredient_id').select2('data');
|
|
if (sel && sel[0] && sel[0].density != null && sel[0].density !== '') {
|
|
return parseFloat(sel[0].density);
|
|
}
|
|
var d = $('#ingredient_id option:selected').data('density');
|
|
return (d != null && d !== '') ? parseFloat(d) : null;
|
|
}
|
|
|
|
function applyOrderedUnit() {
|
|
var isIng = $('#entry_type').val() === 'ingredient';
|
|
$('#ordered-unit-wrap').toggle(isIng);
|
|
|
|
var density = isIng ? currentDensity() : null;
|
|
var hasDensity = density != null && density > 0;
|
|
$('#ordered_unit option[value="ml"]').prop('disabled', !hasDensity);
|
|
if (!hasDensity && $('#ordered_unit').val() === 'ml') {
|
|
$('#ordered_unit').val('gram');
|
|
}
|
|
|
|
var $hint = $('#ordered-quantity-hint');
|
|
if (!isIng) {
|
|
$hint.text('{{ __('Bei Verpackung in Stück.') }}');
|
|
return;
|
|
}
|
|
if ($('#ordered_unit').val() === 'ml' && hasDensity) {
|
|
var ml = parseNumber($('#ordered_quantity').val());
|
|
var g = ml != null ? formatNumber(ml * density) : '?';
|
|
$hint.text('{{ __('Erfassung in ml — entspricht ca.') }} ' + g + ' {{ __('g (Dichte') }} ' + formatNumber(density) + ' g/ml).');
|
|
} else if (hasDensity) {
|
|
$hint.text('{{ __('In Gramm — oder Einheit auf ml stellen (Dichte hinterlegt).') }}');
|
|
} else {
|
|
$hint.text('{{ __('In Gramm. Für ml-Erfassung zuerst die Dichte am Rohstoff pflegen.') }}');
|
|
}
|
|
}
|
|
|
|
function parseNumber(value) {
|
|
if (value === null || value === undefined) {
|
|
return null;
|
|
}
|
|
var normalized = String(value).trim().replace(/\./g, '').replace(',', '.');
|
|
if (normalized === '' || isNaN(normalized)) {
|
|
return null;
|
|
}
|
|
return parseFloat(normalized);
|
|
}
|
|
|
|
function formatNumber(value) {
|
|
return value.toFixed(2).replace('.', ',');
|
|
}
|
|
|
|
function currentFactor() {
|
|
var percent = parseFloat($('#tax_rate_id option:selected').data('percent')) || 0;
|
|
return 1 + percent / 100;
|
|
}
|
|
|
|
function recalcFromNet() {
|
|
var net = parseNumber($('#price_per_kg').val());
|
|
if (net === null) {
|
|
return;
|
|
}
|
|
$('#price_per_kg_gross').val(formatNumber(net * currentFactor()));
|
|
}
|
|
|
|
function recalcFromGross() {
|
|
var gross = parseNumber($('#price_per_kg_gross').val());
|
|
if (gross === null) {
|
|
return;
|
|
}
|
|
var factor = currentFactor();
|
|
$('#price_per_kg').val(formatNumber(factor > 0 ? gross / factor : gross));
|
|
}
|
|
|
|
$(document).ready(function () {
|
|
toggleBlocks();
|
|
initIngredientSelect2();
|
|
initPackagingSelect2();
|
|
applyOrderedUnit();
|
|
|
|
$('#entry_type').on('change', function () {
|
|
toggleBlocks();
|
|
applyOrderedUnit();
|
|
$('#packaging_item_id').val(null).trigger('change');
|
|
initPackagingSelect2();
|
|
});
|
|
$('#ingredient_id').on('change', applyOrderedUnit);
|
|
$('#ordered_unit').on('change', applyOrderedUnit);
|
|
$('#ordered_quantity').on('input', applyOrderedUnit);
|
|
|
|
$('#price_per_kg').on('input', recalcFromNet);
|
|
$('#price_per_kg_gross').on('input', recalcFromGross);
|
|
$('#tax_rate_id').on('change', function () {
|
|
if (parseNumber($('#price_per_kg').val()) !== null) {
|
|
recalcFromNet();
|
|
} else {
|
|
recalcFromGross();
|
|
}
|
|
});
|
|
});
|
|
})(jQuery);
|
|
</script>
|