feat: refactor post filtering logic and automate contract post population based on context counts
This commit is contained in:
parent
e2eb20536d
commit
cde2c7e52f
4 changed files with 115 additions and 46 deletions
|
|
@ -1382,14 +1382,66 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
// ==================== Affichage de tous les postes ====================
|
// ==================== Affichage de tous les postes ====================
|
||||||
async function showAllContractPosts() {
|
function updateShowAllButtonUI() {
|
||||||
|
const showAllBtn = document.querySelector(CONFIG.selectors.showAllBtn);
|
||||||
|
if (!showAllBtn) return;
|
||||||
if (appState.isShowingAllPosts) {
|
if (appState.isShowingAllPosts) {
|
||||||
// Revenir aux postes de l'intervention
|
showAllBtn.innerHTML = `
|
||||||
appState.currentData = appState.originalData.map(item => ({...item}));
|
<i class="bi bi-list-check"></i> ${gettext('Afficher uniquement les postes de l\'intervention')}
|
||||||
appState.isShowingAllPosts = false;
|
`;
|
||||||
document.querySelector(CONFIG.selectors.showAllBtn).innerHTML = `
|
} else {
|
||||||
|
showAllBtn.innerHTML = `
|
||||||
<i class="bi bi-list-ul"></i> ${gettext('Afficher tous les postes du contrat')}
|
<i class="bi bi-list-ul"></i> ${gettext('Afficher tous les postes du contrat')}
|
||||||
`;
|
`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function populateWithAllContractPosts() {
|
||||||
|
const currentPostIds = new Set((appState.currentData || []).map(item => String(item.contract_post_id)));
|
||||||
|
const allPosts = [...(appState.currentData || [])];
|
||||||
|
|
||||||
|
(appState.contractPosts || []).forEach(post => {
|
||||||
|
if (!currentPostIds.has(String(post.id))) {
|
||||||
|
allPosts.push({
|
||||||
|
contract_post_id: post.id,
|
||||||
|
order_number: post.order_number,
|
||||||
|
code: post.code,
|
||||||
|
description: post.description_fr || post.description_nl,
|
||||||
|
quantity: '',
|
||||||
|
comment: '',
|
||||||
|
unit: post.unit || '',
|
||||||
|
unit_price: post.unit_price,
|
||||||
|
total: '',
|
||||||
|
control_status: '',
|
||||||
|
control_comment: '',
|
||||||
|
remaining_quantity: '',
|
||||||
|
post_type: post.post_type || '',
|
||||||
|
is_coefficient_multipliable: post.is_coefficient_multipliable || false,
|
||||||
|
is_amount_to_be_justified: post.is_amount_to_be_justified || false,
|
||||||
|
coeff_amount_to_be_justified: post.coeff_amount_to_be_justified || 1,
|
||||||
|
deleted: false,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
appState.currentData = allPosts;
|
||||||
|
appState.isShowingAllPosts = true;
|
||||||
|
updateShowAllButtonUI();
|
||||||
|
}
|
||||||
|
|
||||||
|
async function showAllContractPosts() {
|
||||||
|
if (appState.isShowingAllPosts) {
|
||||||
|
// Revenir aux postes de l'intervention (postes originaux + postes ayant une quantité > 0 ou un commentaire)
|
||||||
|
const originalPostIds = new Set((appState.originalData || []).map(item => String(item.contract_post_id)));
|
||||||
|
appState.currentData = (appState.currentData || []).filter(item => {
|
||||||
|
const isOriginal = originalPostIds.has(String(item.contract_post_id));
|
||||||
|
const qty = parseFloat(String(item.quantity || '').replace(',', '.'));
|
||||||
|
const hasQty = !Number.isNaN(qty) && qty !== 0;
|
||||||
|
const hasComment = Boolean(item.comment && item.comment.trim());
|
||||||
|
return isOriginal || hasQty || hasComment;
|
||||||
|
});
|
||||||
|
appState.isShowingAllPosts = false;
|
||||||
|
updateShowAllButtonUI();
|
||||||
} else {
|
} else {
|
||||||
// Vérifier qu'un contrat est disponible
|
// Vérifier qu'un contrat est disponible
|
||||||
const currentContractId = window.contractId || contractId;
|
const currentContractId = window.contractId || contractId;
|
||||||
|
|
@ -1399,48 +1451,17 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
// Charger tous les postes
|
// Charger tous les postes
|
||||||
if (appState.contractPosts.length === 0) {
|
if (!appState.contractPosts || appState.contractPosts.length === 0) {
|
||||||
appState.contractPosts = await loadContractPosts();
|
appState.contractPosts = await loadContractPosts();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Vérifier que des postes ont été chargés
|
// Vérifier que des postes ont été chargés
|
||||||
if (appState.contractPosts.length === 0) {
|
if (!appState.contractPosts || appState.contractPosts.length === 0) {
|
||||||
showAlert(gettext('Aucun poste trouvé dans le contrat.'), 'info');
|
showAlert(gettext('Aucun poste trouvé dans le contrat.'), 'info');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Merger avec les données actuelles
|
populateWithAllContractPosts();
|
||||||
const currentPostIds = new Set(appState.originalData.map(item => item.contract_post_id));
|
|
||||||
const allPosts = [...appState.originalData];
|
|
||||||
|
|
||||||
appState.contractPosts.forEach(post => {
|
|
||||||
if (!currentPostIds.has(post.id.toString())) {
|
|
||||||
allPosts.push({
|
|
||||||
contract_post_id: post.id,
|
|
||||||
order_number: post.order_number,
|
|
||||||
code: post.code,
|
|
||||||
description: post.description_fr || post.description_nl,
|
|
||||||
quantity: '',
|
|
||||||
comment: '',
|
|
||||||
unit_price: post.unit_price,
|
|
||||||
total: '',
|
|
||||||
control_status: '',
|
|
||||||
control_comment: '',
|
|
||||||
remaining_quantity: '',
|
|
||||||
post_type: post.post_type || '',
|
|
||||||
is_coefficient_multipliable: post.is_coefficient_multipliable || false,
|
|
||||||
is_amount_to_be_justified: post.is_amount_to_be_justified || false,
|
|
||||||
coeff_amount_to_be_justified: post.coeff_amount_to_be_justified || 1,
|
|
||||||
deleted: false,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
appState.currentData = allPosts;
|
|
||||||
appState.isShowingAllPosts = true;
|
|
||||||
document.querySelector(CONFIG.selectors.showAllBtn).innerHTML = `
|
|
||||||
<i class="bi bi-list-check"></i> ${gettext('Afficher uniquement les postes de l\'intervention')}
|
|
||||||
`;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
renderTable();
|
renderTable();
|
||||||
|
|
@ -1482,12 +1503,7 @@
|
||||||
|
|
||||||
if (appState.isShowingAllPosts) {
|
if (appState.isShowingAllPosts) {
|
||||||
appState.isShowingAllPosts = false;
|
appState.isShowingAllPosts = false;
|
||||||
const showAllBtn = document.querySelector(CONFIG.selectors.showAllBtn);
|
updateShowAllButtonUI();
|
||||||
if (showAllBtn) {
|
|
||||||
showAllBtn.innerHTML = `
|
|
||||||
<i class="bi bi-list-ul"></i> ${gettext('Afficher tous les postes du contrat')}
|
|
||||||
`;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -2679,11 +2695,38 @@
|
||||||
appState.quoteItems = Array.isArray(data.quote_items) ? data.quote_items : [];
|
appState.quoteItems = Array.isArray(data.quote_items) ? data.quote_items : [];
|
||||||
|
|
||||||
// Mettre à jour le contractId global si fourni par le serveur
|
// Mettre à jour le contractId global si fourni par le serveur
|
||||||
|
const previousContractId = window.contractId;
|
||||||
if (data.contract_id) {
|
if (data.contract_id) {
|
||||||
window.contractId = data.contract_id;
|
window.contractId = data.contract_id;
|
||||||
|
window.contractPostsCount = data.contract_posts_count !== undefined ? data.contract_posts_count : 0;
|
||||||
|
if (previousContractId !== data.contract_id) {
|
||||||
|
appState.contractPosts = [];
|
||||||
|
}
|
||||||
} else if (!orderId) {
|
} else if (!orderId) {
|
||||||
// Si on désélectionne la commande, réinitialiser contractId
|
// Si on désélectionne la commande, réinitialiser contractId
|
||||||
window.contractId = null;
|
window.contractId = null;
|
||||||
|
window.contractPostsCount = 0;
|
||||||
|
appState.contractPosts = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (appState.originalData.length === 0) {
|
||||||
|
if (window.contractId && window.contractPostsCount > 0 && window.contractPostsCount < 1000) {
|
||||||
|
if (!appState.contractPosts || appState.contractPosts.length === 0) {
|
||||||
|
appState.contractPosts = await loadContractPosts();
|
||||||
|
}
|
||||||
|
if (appState.contractPosts && appState.contractPosts.length > 0) {
|
||||||
|
appState.currentData = [];
|
||||||
|
populateWithAllContractPosts();
|
||||||
|
} else {
|
||||||
|
appState.currentData = [];
|
||||||
|
appState.isShowingAllPosts = false;
|
||||||
|
updateShowAllButtonUI();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
appState.currentData = [];
|
||||||
|
appState.isShowingAllPosts = false;
|
||||||
|
updateShowAllButtonUI();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mettre à jour l'affichage dans la vue consultation
|
// Mettre à jour l'affichage dans la vue consultation
|
||||||
|
|
@ -2831,6 +2874,11 @@
|
||||||
|
|
||||||
appState.modal = new bootstrap.Modal(modalEl, { backdrop: 'static', keyboard: false });
|
appState.modal = new bootstrap.Modal(modalEl, { backdrop: 'static', keyboard: false });
|
||||||
|
|
||||||
|
// Initialiser contractPostsCount depuis le contexte serveur
|
||||||
|
if (typeof contractPostsCount !== 'undefined') {
|
||||||
|
window.contractPostsCount = contractPostsCount;
|
||||||
|
}
|
||||||
|
|
||||||
// Initialiser hasCoefficientPosts depuis le contexte serveur
|
// Initialiser hasCoefficientPosts depuis le contexte serveur
|
||||||
if (typeof hasCoefficientPostsInitial !== 'undefined') {
|
if (typeof hasCoefficientPostsInitial !== 'undefined') {
|
||||||
appState.hasCoefficientPosts = hasCoefficientPostsInitial;
|
appState.hasCoefficientPosts = hasCoefficientPostsInitial;
|
||||||
|
|
@ -2848,10 +2896,27 @@
|
||||||
appState.originalData = extractDataFromSourceTable();
|
appState.originalData = extractDataFromSourceTable();
|
||||||
appState.currentData = appState.originalData.map(item => ({...item}));
|
appState.currentData = appState.originalData.map(item => ({...item}));
|
||||||
normalizeStatusForPeriodValidation();
|
normalizeStatusForPeriodValidation();
|
||||||
appState.isShowingAllPosts = false;
|
|
||||||
appState.hasUnsavedChanges = false;
|
appState.hasUnsavedChanges = false;
|
||||||
appState.statusChanges = {};
|
appState.statusChanges = {};
|
||||||
|
|
||||||
|
const currentContractId = window.contractId || contractId;
|
||||||
|
const currentPostsCount = window.contractPostsCount !== undefined ? window.contractPostsCount : (typeof contractPostsCount !== 'undefined' ? contractPostsCount : 0);
|
||||||
|
|
||||||
|
if (appState.originalData.length === 0 && currentContractId && currentPostsCount > 0 && currentPostsCount < 1000) {
|
||||||
|
if (!appState.contractPosts || appState.contractPosts.length === 0) {
|
||||||
|
appState.contractPosts = await loadContractPosts();
|
||||||
|
}
|
||||||
|
if (appState.contractPosts && appState.contractPosts.length > 0) {
|
||||||
|
populateWithAllContractPosts();
|
||||||
|
} else {
|
||||||
|
appState.isShowingAllPosts = false;
|
||||||
|
updateShowAllButtonUI();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
appState.isShowingAllPosts = false;
|
||||||
|
updateShowAllButtonUI();
|
||||||
|
}
|
||||||
|
|
||||||
await fetchPeriodQuantities();
|
await fetchPeriodQuantities();
|
||||||
if (autoShowPeriods) {
|
if (autoShowPeriods) {
|
||||||
appState.showPeriods = true;
|
appState.showPeriods = true;
|
||||||
|
|
|
||||||
|
|
@ -362,6 +362,7 @@
|
||||||
data-contract="{{ order.contract.contract_number }}"
|
data-contract="{{ order.contract.contract_number }}"
|
||||||
data-order-code="{{ order.order_code }}"
|
data-order-code="{{ order.order_code }}"
|
||||||
data-has-quote="{% if order.quote %}true{% else %}false{% endif %}"
|
data-has-quote="{% if order.quote %}true{% else %}false{% endif %}"
|
||||||
|
data-contract-posts-count="{{ order.contract.posts.count }}"
|
||||||
{% if intervention.order and order.id == intervention.order.id %}selected{% endif %}>
|
{% if intervention.order and order.id == intervention.order.id %}selected{% endif %}>
|
||||||
{{ order.contract.contract_number }} - {{ order.order_code }}
|
{{ order.contract.contract_number }} - {{ order.order_code }}
|
||||||
</option>
|
</option>
|
||||||
|
|
|
||||||
|
|
@ -48,6 +48,7 @@
|
||||||
const photosRequiredSince = "{{ photos_required_since }}";
|
const photosRequiredSince = "{{ photos_required_since }}";
|
||||||
const contractWarnNoPrestationOnProcessed = {{ contract_warn_no_prestation_on_processed|yesno:"true,false" }};
|
const contractWarnNoPrestationOnProcessed = {{ contract_warn_no_prestation_on_processed|yesno:"true,false" }};
|
||||||
const hasPrestationsEncoded = {{ has_prestations_encoded|yesno:"true,false" }};
|
const hasPrestationsEncoded = {{ has_prestations_encoded|yesno:"true,false" }};
|
||||||
|
var contractPostsCount = {{ contract_posts_count|default:0 }};
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
|
|
|
||||||
|
|
@ -632,6 +632,7 @@ def intervention_detail(request, intervention_id):
|
||||||
'operations_asset_groups': asset_operations_groups,
|
'operations_asset_groups': asset_operations_groups,
|
||||||
'orders': orders,
|
'orders': orders,
|
||||||
'contract_id': contract_id,
|
'contract_id': contract_id,
|
||||||
|
'contract_posts_count': contract.posts.count() if contract else 0,
|
||||||
'can_control': can_control,
|
'can_control': can_control,
|
||||||
'is_draft': is_draft,
|
'is_draft': is_draft,
|
||||||
'providers': providers,
|
'providers': providers,
|
||||||
|
|
@ -2226,6 +2227,7 @@ def intervention_link_order(request, intervention_id):
|
||||||
return JsonResponse({
|
return JsonResponse({
|
||||||
'success': True,
|
'success': True,
|
||||||
'contract_id': intervention.order.contract.id if intervention.order else None,
|
'contract_id': intervention.order.contract.id if intervention.order else None,
|
||||||
|
'contract_posts_count': intervention.order.contract.posts.count() if intervention.order and intervention.order.contract else 0,
|
||||||
'quote_items': quote_items,
|
'quote_items': quote_items,
|
||||||
'quote_total': quote_total,
|
'quote_total': quote_total,
|
||||||
'can_manage_posts': 'posts' in allowed_update_fields,
|
'can_manage_posts': 'posts' in allowed_update_fields,
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue