You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

610 lines
24 KiB

<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
<section class="content-header">
<h1>
Gérer les
<small>Avances</small>
</h1>
<ol class="breadcrumb">
<li><a href="#"><i class="fa fa-dashboard"></i> Accueil</a></li>
<li class="active">Avances</li>
</ol>
</section>
<section class="content">
<div id="messages"></div>
<?php if (in_array('createAvance', $user_permission)): ?>
<button class="btn btn-primary" data-toggle="modal" data-target="#createModal">AJOUTER UNE AVANCE</button>
<br /><br />
<?php endif; ?>
<div class="row">
<div class="col-md-3">
<button id="avance_no_order" class="btn btn-warning w-100 rounded-pill shadow-sm py-2">
<i class="fas fa-hourglass-half me-2"></i> Avances Incomplètes
</button>
</div>
<div class="col-md-3">
<button id="avance_order" class="btn btn-success w-100 rounded-pill shadow-sm py-2">
<i class="fas fa-check-circle me-2"></i> Avances Complètes
</button>
</div>
<div class="col-md-3">
<button id="avance_expired" class="btn btn-outline-danger w-100 rounded-pill shadow-sm py-2 fw-bold border-2">
<i class="fas fa-exclamation-circle me-2"></i> Avances Expirées
</button>
</div>
</div>
<br>
<div class="box">
<div class="box-header"><h3 class="box-title">Liste des avances</h3></div>
<div class="box-body">
<table id="avanceTable" class="table table-bordered table-striped">
<thead>
<?php $session = session();
$users = $session->get('user');
$isAdmin = in_array($users['group_name'], ['Conseil', 'Direction']);
$isCommerciale = in_array($users['group_name'], ['COMMERCIALE']);
$isCaissier = in_array($users['group_name'], ['Caissier']);
if ($isAdmin): ?>
<tr>
<th>Client</th>
<th>Téléphone</th>
<th>Adresse</th>
<th>Produit</th>
<th>Prix</th>
<th>Avance</th>
<th>Reste à payer</th>
<th>Date</th>
<?php if (in_array('updateAvance', $user_permission) || in_array('deleteAvance', $user_permission)): ?>
<th>Action</th>
<?php endif;?>
</tr>
<?php endif;?>
<?php $session = session();
$users = $session->get('user');
$isAdmin = in_array($users['group_name'], ['Conseil', 'Direction']);
$isCommerciale = in_array($users['group_name'], ['COMMERCIALE']);
$isCaissier = in_array($users['group_name'], ['Caissier']);
if ($isCommerciale || $isCaissier): ?>
<tr>
<th>#</th>
<th>Produit</th>
<th>Avance</th>
<th>Reste à payer</th>
<th>Date</th>
<?php if (in_array('updateAvance', $user_permission) || in_array('deleteAvance', $user_permission) ||in_array('viewAvance', $user_permission)): ?>
<th>Action</th>
<?php endif;?>
</tr>
<?php endif;?>
</thead>
<tbody></tbody>
</table>
</div>
</div>
</section>
</div>
<!-- Modal Création -->
<?php if (in_array('createAvance', $user_permission)): ?>
<div class="modal fade" id="createModal" tabindex="-1" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<form id="create_avance_form">
<div class="modal-header">
<h4 class="modal-title">Ajouter une avance</h4>
<button type="button" class="close" data-dismiss="modal">&times;</button>
</div>
<div class="modal-body">
<div class="row">
<!-- Nom client -->
<div class="form-group col-md-6">
<label>Nom du client</label>
<input type="text" name="customer_name_avance" id="customer_name_avance" class="form-control" required>
</div>
<!-- Téléphone client -->
<div class="form-group col-md-6">
<label>Téléphone du client</label>
<input type="text" name="customer_phone_avance" id="customer_phone_avance" class="form-control" required>
</div>
</div>
<div class="row">
<!-- Adresse client -->
<div class="form-group col-md-6">
<label>Adresse du client</label>
<input type="text" name="customer_address_avance" id="customer_address_avance" class="form-control" required>
</div>
<!-- CIN client -->
<div class="form-group col-md-6">
<label>CIN du client</label>
<input type="text" name="customer_cin_avance" id="customer_cin_avance" class="form-control" required>
</div>
</div>
<div class="row">
<!-- Produit -->
<div class="form-group col-md-6">
<label for="id_product" class="form-label">Produit</label>
<select name="id_product" id="id_product" class="form-control " onchange="getProductDataCreate()" required>
<option value="">Sélectionnez un produit</option>
<?php foreach($products as $p): ?>
<option value="<?= $p['id'] ?>" <?= $p['product_sold'] ? 'disabled' : '' ?>>
<?= esc($p['name']) ?> <?= $p['product_sold'] ? '(Rupture)' : '' ?>
</option>
<?php endforeach; ?>
</select>
</div>
<!-- Prix brut -->
<div class="form-group col-md-6">
<label>Prix du produit</label>
<input type="text" name="gross_amount" id="gross_amount" class="form-control" readonly>
</div>
</div>
<div class="row">
<!-- Avance -->
<div class="form-group col-md-6">
<label>Avance</label>
<input type="number" name="avance_amount" id="avance_amount" class="form-control" onkeyup="updateDueCreate()" required>
</div>
<!-- Reste à payer -->
<div class="form-group col-md-6">
<label>Reste à payer</label>
<input type="text" name="amount_due" id="amount_due" class="form-control" readonly>
</div>
</div>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-primary">Enregistrer</button>
<button type="button" class="btn btn-default" data-dismiss="modal">Annuler</button>
</div>
</form>
</div>
</div>
</div>
<?php endif; ?>
<!-- Modal Modification -->
<?php if (in_array('updateAvance', $user_permission)): ?>
<div class="modal fade" id="updateModal" tabindex="-1" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<form id="update_avance_form" method="post">
<input type="hidden" name="id" id="avance_id_edit">
<div class="modal-header">
<h4 class="modal-title">Modifier une avance</h4>
<button type="button" class="close" data-dismiss="modal">&times;</button>
</div>
<div class="modal-body">
<div class="row">
<!-- Nom client -->
<div class="form-group col-md-6">
<label>Nom du client</label>
<input type="text" name="customer_name_avance_edit" id="customer_name_avance_edit" class="form-control" required>
</div>
<!-- Téléphone client -->
<div class="form-group col-md-6">
<label>Téléphone du client</label>
<input type="text" name="customer_phone_avance_edit" id="customer_phone_avance_edit" class="form-control" required>
</div>
<!-- Adresse client -->
<div class="form-group col-md-6">
<label>Adresse du client</label>
<input type="text" name="customer_address_avance_edit" id="customer_address_avance_edit" class="form-control" required>
</div>
<!-- CIN client -->
<div class="form-group col-md-6">
<label>CIN du client</label>
<input type="text" name="customer_cin_avance_edit" id="customer_cin_avance_edit" class="form-control" required>
</div>
<!-- Produit -->
<div class="form-group col-md-6">
<label class="form_label">Produit</label>
<select name="id_product_edit" id="id_product_edit" class="form-control" onchange="getProductDataUpdate()" required>
<option value="">Sélectionnez un produit</option>
<?php foreach($products as $p): ?>
<option value="<?= $p['id'] ?>" <?= $p['product_sold'] ? 'disabled' : '' ?>>
<?= esc($p['name']) ?> <?= $p['product_sold'] ? '(Rupture)' : '' ?>
</option>
<?php endforeach; ?>
</select>
</div>
<!-- Prix du produit -->
<div class="form-group col-md-6">
<label>Prix du produit</label>
<input type="text" name="gross_amount_edit" id="gross_amount_edit" class="form-control" readonly>
</div>
<!-- Avance -->
<div class="form-group col-md-6">
<label>Avance</label>
<input type="number" name="avance_amount_edit" id="avance_amount_edit" class="form-control" onkeyup="updateDueEdit()" required>
</div>
<!-- Reste à payer -->
<div class="form-group col-md-6">
<label>Reste à payer</label>
<input type="text" name="amount_due_edit" id="amount_due_edit" class="form-control" readonly>
</div>
</div>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-success">Modifier</button>
<button type="button" class="btn btn-default" data-dismiss="modal">Annuler</button>
</div>
</form>
</div>
</div>
</div>
<?php endif;?>
<?php if (in_array('deleteAvance', $user_permission)): ?>
<!-- remove brand modal -->
<div class="modal fade" tabindex="-1" role="dialog" id="removeModal">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<h4 class="modal-title">Supprimer cette avance</h4>
</div>
<form role="form" action="<?php echo base_url('avances/deleteAvance') ?>" method="post" id="removeForm">
<div class="modal-body">
<p>Voulez-vous vraiment supprimer ?</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Fermer</button>
<button type="submit" class="btn btn-primary">Oui</button>
</div>
</form>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
<?php endif; ?>
<script>
var base_url = "<?= base_url() ?>", brutCreate = 0, brutEdit = 0;
$(document).ready(function() {
$('#avance_menu').addClass("active");
$('.select2').select2();
<?php if ($isAdmin):?>
manageTable = $('#avanceTable').DataTable({
ajax: 'fetchAvanceData',
columns: [
{ title: "Client" },
{ title: "Téléphone" },
{ title: "Adresse" },
{ title: "Produit" },
{ title: "Prix" },
{ title: "Avance" },
{ title: "Reste à payer" },
{ title: "Date" }
<?php if (in_array('updateAvance', $user_permission) || in_array('deleteAvance', $user_permission)): ?>
,{ title: "Action", orderable: false, searchable: false }
<?php endif; ?>
]
});
$('#avance_order').on('click', function () {
$('#avanceTable').DataTable().destroy();
manageTable = $('#avanceTable').DataTable({
ajax: 'fetchAvanceBecameOrder',
columns: [
{ title: "Client" },
{ title: "Téléphone" },
{ title: "Adresse" },
{ title: "Produit" },
{ title: "Prix" },
{ title: "Avance" },
{ title: "Reste à payer" },
{ title: "Date" }
<?php if (in_array('updateAvance', $user_permission) || in_array('deleteAvance', $user_permission)): ?>
,{ title: "Action", orderable: false, searchable: false }
<?php endif; ?>
]
});
});
$('#avance_expired').on('click', function () {
$('#avanceTable').DataTable().destroy();
manageTable = $('#avanceTable').DataTable({
ajax: 'fetchExpiredAvance',
columns: [
{ title: "Client" },
{ title: "Téléphone" },
{ title: "Adresse" },
{ title: "Produit" },
{ title: "Prix" },
{ title: "Avance" },
{ title: "Reste à payer" },
{ title: "Date" }
<?php if (in_array('updateAvance', $user_permission) || in_array('deleteAvance', $user_permission)): ?>
,{ title: "Action", orderable: false, searchable: false }
<?php endif; ?>
]
});
});
$('#avance_no_order').on('click', function () {
$('#avanceTable').DataTable().destroy();
manageTable = $('#avanceTable').DataTable({
ajax: 'fetchAvanceData',
columns: [
{ title: "Client" },
{ title: "Téléphone" },
{ title: "Adresse" },
{ title: "Produit" },
{ title: "Prix" },
{ title: "Avance" },
{ title: "Reste à payer" },
{ title: "Date" }
<?php if (in_array('updateAvance', $user_permission) || in_array('deleteAvance', $user_permission)): ?>
,{ title: "Action", orderable: false, searchable: false }
<?php endif; ?>
]
});
});
<?php endif; ?>
<?php if ($isCaissier || $isCommerciale):?>
manageTable = $('#avanceTable').DataTable({
ajax: 'fetchAvanceData',
columns: [
{ title: "#" },
{ title: "Produit" },
{ title: "Avance" },
{ title: "Reste à payer" },
{ title: "Date" }
<?php if (in_array('updateAvance', $user_permission) || in_array('deleteAvance', $user_permission)): ?>
,{ title: "Action", orderable: false, searchable: false }
<?php endif; ?>
]
});
$('#avance_order').on('click', function () {
$('#avanceTable').DataTable().destroy();
manageTable = $('#avanceTable').DataTable({
ajax: 'fetchAvanceBecameOrder',
columns: [
{ title: "#" },
{ title: "Produit" },
{ title: "Avance" },
{ title: "Reste à payer" },
{ title: "Date" }
<?php if (in_array('updateAvance', $user_permission) || in_array('deleteAvance', $user_permission)): ?>
,{ title: "Action", orderable: false, searchable: false }
<?php endif; ?>
]
});
});
$('#avance_no_order').on('click', function () {
$('#avanceTable').DataTable().destroy();
manageTable = $('#avanceTable').DataTable({
ajax: 'fetchAvanceData',
columns: [
{ title: "#" },
{ title: "Produit" },
{ title: "Avance" },
{ title: "Reste à payer" },
{ title: "Date" }
<?php if (in_array('updateAvance', $user_permission) || in_array('deleteAvance', $user_permission)): ?>
,{ title: "Action", orderable: false, searchable: false }
<?php endif; ?>
]
});
});
<?php endif; ?>
// Création AJAX
$('#create_avance_form').on('submit', function(e) {
e.preventDefault();
const $form = $(this);
$.post('/avances/createAvance', $form.serialize(), function(res) {
if (res.success === true) {
manageTable.ajax.reload(null, false);
$("#messages").html(
`<div class="alert alert-success alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
<strong><span class="glyphicon glyphicon-ok-sign"></span></strong> ${res.messages}
</div>`
);
// Cacher le modal de création
$("#createModal").modal('hide');
$form[0].reset();
} else {
$("#createModal").modal('hide');
$("#messages").html(
`<div class="alert alert-warning alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
<strong><span class="glyphicon glyphicon-exclamation-sign"></span></strong> ${res.messages}
</div>`
);
}
}, 'json');
});
});
// Récupère prix pour création
function getProductDataCreate(){
var id = $('#id_product').val();
if(!id){ brutCreate=0; $('#gross_amount,#amount_due').val(''); return; }
else{
$.post(base_url+'orders/getProductValueById',{product_id:id}, function(r){
brutCreate = parseFloat(r.prix_vente)||0;
$('#gross_amount').val(brutCreate);
$('#amount_due').val(brutCreate);
updateDueCreate();
},'json');
}
}
function getProductDataUpdate(){
var id = $('#id_product_edit').val();
if(!id){ brutCreate=0; $('#gross_amount_edit,#amount_due_edit').val(''); return; }
else{
$.post(base_url+'orders/getProductValueById',{product_id:id}, function(r){
brutCreate = parseFloat(r.prix_vente)||0;
$('#gross_amount_edit').val(brutCreate);
$('#amount_due_edit').val(brutCreate);
updateDueCreate();
},'json');
}
}
function updateDueCreate(){
var av = parseFloat($('#avance_amount').val())||0;
var brutAmount = parseFloat($('#gross_amount').val())||0;
$('#amount_due').val(Math.max(brutAmount-av,0));
}
// Prépare et affiche modal édition
function editFunc(id) {
// Récupération des données de l'avance
$.getJSON(base_url + 'avances/fetchSingleAvance/' + id, function(r) {
// Préremplissage du modal
$('#customer_name_avance_edit').val(r.customer_name);
$('#customer_phone_avance_edit').val(r.customer_phone);
$('#customer_adress_avance_edit').val(r.customer_adress);
$('#customer_cin_avance_edit').val(r.customer_cin);
$('#gross_amount_edit').val(r.gross_amount);
$('#avance_amount_edit').val(r.avance_amount);
$('#amount_due_edit').val(r.amount_due);
const product_id = r.product_id;
const productSelect = $("#id_product_edit");
const optionExists = productSelect.find('option').filter(function() {
return $(this).val().trim().toLowerCase() === product_id.trim().toLowerCase();
}).length > 0;
if (optionExists) {
productSelect.val(product_id);
} else {
productSelect.val("");
}
// Calcul du montant brut initial
brutEdit = parseFloat(r.avance_amount) + parseFloat(r.amount_due);
// Affiche le modal
$('#updateModal').modal('show');
$('#update_avance_form').off('submit');
$('#update_avance_form').on('submit', function(e) {
e.preventDefault();
const actionUrl = base_url + 'avances/updateAvance/' + id;
// Envoi AJAX
$.post(actionUrl, $(this).serialize(), function(res) {
if (res.success) {
$('#updateModal').modal('hide');
manageTable.ajax.reload(null, false);
$("#messages").html(
`<div class="alert alert-success alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
<strong><span class="glyphicon glyphicon-ok-sign"></span></strong> ${res.messages}
</div>`
);
} else {
$("#messages").html(
`<div class="alert alert-danger alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
<strong><span class="glyphicon glyphicon-ok-sign"></span></strong> ${res.messages}
</div>`
);
alert(res.messages || 'Erreur lors de la modification.');
}
}, 'json');
});
});
}
function updateDueEdit(){
var av = parseFloat($('#avance_amount_edit').val())||0;
$('#amount_due_edit').val(Math.max(brutEdit-av,0).toFixed(2));
}
function removeFunc(id, product_id) {
// Affiche correctement le modal
$('#removeModal').modal('show');
if (id) {
// On évite d'attacher plusieurs fois le submit handler
$('#removeForm').off('submit').on('submit', function(e) {
e.preventDefault();
var form = $(this);
// Supprime les anciens messages d'erreur
$(".text-danger").remove();
$.ajax({
url: form.attr('action'),
type: form.attr('method'),
data: {
avance_id: id,
product_id: product_id
},
dataType: 'json',
success: function(response) {
manageTable.ajax.reload(null, false);
if (response.success === true) {
$("#messages").html('<div class="alert alert-success alert-dismissible" role="alert">' +
'<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>' +
'<strong><span class="glyphicon glyphicon-ok-sign"></span></strong> ' + response.messages +
'</div>');
// Cache le modal
$("#removeModal").modal('hide');
} else {
$("#messages").html('<div class="alert alert-warning alert-dismissible" role="alert">' +
'<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>' +
'<strong><span class="glyphicon glyphicon-exclamation-sign"></span></strong> ' + response.messages +
'</div>');
}
}
});
return false;
});
}
}
</script>