Вы сказали:
почему в таблице не применяются изменения pfixsum и percentfix после загрузки таблицы
$(document).ready(function() {
// Сохранение формы через AJAX
$('.nosy-field').click(function() {
$('.nosy-error').hide();
var formData = $('#dynamicForm').serialize();
$.ajax({
url: '../nosy/save_form.php',
type: 'POST',
data: formData,
dataType: 'json',//text only
success: function(response) {
//console.log(response);
//alert('Форма успешно сохранена!');
//$('#htmlret').html(response);
$('#htmlret').html(response.HTML);
$('.htmltable').remove();
$('#htmlret').append(
<div class="htmltable"><h6 style="margin-top: 30px;">Таблица цен и маржи</h6>
<table>
<thead>
<tr>
<th rowspan="2">Цена, Юани ¥</th>
<th rowspan="2">Наша себ-сть, ₽</th>
<th colspan="2">Маржа, %</th>
<th colspan="2">Маржа, свой %</th>
</tr>
<tr class="sub-header">
<th>45</th>
<th>25</th>
<th><input type="number" id="percentfix" style=" width: 80px;"> %</th>
</tr>
</thead><tbody>
<tr>
<td>${response[0]} ¥</td>
<td id="cost_price">${response[1]} ₽</td>
<td>${response[2]} ₽</td>
<td>${response[3]} ₽</td>
<td><input type="number" id="pfixsum" style=" width: 100px;"> ₽</td>
</tr>
</tbody>
</table></div>);
$('.nosy-error').show();
//$('.nosy-field').hide();
}
});
});
// Функция для форматирования числа с пробелами
function formatNumber(num) {
return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ' ');
}
// Функция для извлечения числового значения из строки с валютой
function extractNumber(currencyString) {
return parseFloat(currencyString.replace(/[^\d.]/g, ''));
}
// Получаем себестоимость
const costPrice = extractNumber($('#cost_price').text());
// Рассчитываем маржу для 45% и 25%
const margin45 = costPrice * 1.45;
const margin25 = costPrice * 1.25;
// Обновляем значения в таблице
$('#margin45').text(formatNumber(Math.round(margin45)) + ' ₽');
$('#margin25').text(formatNumber(Math.round(margin25)) + ' ₽');
function calculateMarginPrice(cost, marginPercent) {
return Math.round(cost / (1 - marginPercent / 100));//Math.round //Math.floor
}
// Формула наценки
function calculateMarkupPrice(cost, markupPercent) {
if (markupPercent > 1e12) return Infinity;
return markupPercent = ((markupPercent - costPrice) / costPrice) * 100
//return Math.round(cost * (1 + markupPercent / 100));//Math.floor
}
function calculateMarginPercent(cost, price) {
return Math.round(((price - cost) / price) * 100);
}
// Для 25%:
//calculateMarkupPrice(53220, 25); // → 66 525 ₽
// Обработчик изменения процента
$('#percentfix').on('input', function() {
const percent = parseFloat($(this).val()) || 0;
//const sum = parseFloat($('#pfixsum').val()) || 0;
//const customMargin = Math.round(costPrice * (1 - percent / 100));
//если нужна простая наценка (1 + percent / 100) //если нужна маржа от итоговой цены (1 - percent / 100)
const customMargin = calculateMarginPrice(costPrice, percent);
$('#pfixsum').val(customMargin);
//$('#pfixsum').val(Math.round(customMargin));
});
// Обработчик изменения суммы
$('#pfixsum').on('input', function() {
// const sum = parseFloat($(this).val()) || 0;
// const percent = parseFloat($('#percentfix').val()) || 0;
// //const percent = ((sum / costPrice) - 1) * 100;
// const customPercent = calculateMarkupPrice(costPrice, percent);
// //$('#percentfix').val(percent.toFixed(2));
// $('#percentfix').val(customPercent.toFixed(2));
// //const sum = extractNumber($(this).val()) || 0;
// const sum = extractNumber($('#percentfix').val()) || 0;
// const percent = ((sum - costPrice) / costPrice) * 100;
// // Ограничим нереально большие значения
// const safePercent = Math.min(percent, 1e12);
// $('#percentfix').val(safePercent.toFixed(2));
const sum = parseFloat($(this).val()) || 0;
const customPercent = calculateMarginPercent(costPrice, sum);
$('#percentfix').val(customPercent.toFixed(2));
});