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.
 
 

126 lines
4.1 KiB

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
// include mysql database configuration file
include_once 'db.php';
$action=$_GET['action'];
if (isset($_GET['id']))
{
$id=$_GET['id'];
}
if ($action=='load')
{
if (isset($_POST['submit']))
{
// Allowed mime types
$fileMimes = array(
'text/x-comma-separated-values',
'text/comma-separated-values',
'application/octet-stream',
'application/vnd.ms-excel',
'application/x-csv',
'text/x-csv',
'text/csv',
'application/csv',
'application/excel',
'application/vnd.msexcel',
'text/plain'
);
// Validate whether selected file is a CSV file
if (!empty($_FILES['file']['name']) && in_array($_FILES['file']['type'], $fileMimes))
{
//ajout du fichier dans la table importation_compta
$sql="INSERT INTO importation (date_import, filename,count) VALUES (NOW(), '" . $_FILES['file']['name'] . "',0)";
mysqli_query($conn, $sql);
//recuperation de l'id de la ligne ajoutée
$sql="select id from importation order by id desc LIMIT 1";
$result=mysqli_query($conn, $sql);
foreach ($result as $row)
{
$id=$row['id'];
}
//mise à jour du champ pour identifier le dernier import
$sql="UPDATE importation set last=False";
mysqli_query($conn, $sql);
$sql="UPDATE importation set last=True where id=$id";
mysqli_query($conn, $sql);
header("Location: index.php");
// Open uploaded CSV file with read-only mode
$csvFile = fopen($_FILES['file']['tmp_name'], 'r');
// Skip the first line
fgetcsv($csvFile);
// Parse data from CSV file line by line
// Parse data from CSV file line by line
while (($getData = fgetcsv($csvFile, 10000, ",")) !== FALSE)
{
// Get row data
$no_compte = $getData[0];
$lib_compte = $getData[1];
$code_JNL = $getData[2];
$No_piece = $getData[3];
$date_piece = $getData[4];
$compte_contrepartie = $getData[5];
$lib_mouvement = $getData[6];
$debit = $getData[7];
$credit = $getData[8];
$solde = $getData[9];
$sens = $getData[10];
$code_activite = $getData[11];
$lib_activite = $getData[12];
// If user already exists in the database with the same email
$query = "SELECT id FROM users WHERE email = '" . $getData[1] . "'";
$check = mysqli_query($conn, $query);
if ($check->num_rows > 0)
{
mysqli_query($conn, "UPDATE users SET name = '" . $name . "', phone = '" . $phone . "', status = '" . $status . "', created_at = NOW() WHERE email = '" . $email . "'");
}
else
{
mysqli_query($conn, "INSERT INTO users (name, email, phone, created_at, updated_at, status) VALUES ('" . $name . "', '" . $email . "', '" . $phone . "', NOW(), NOW(), '" . $status . "')");
}
}
// Close opened CSV file
fclose($csvFile);
header("Location: index.php");
}
else
{
echo "Please select valid file";
}
}
}
elseif ($action=='remove')
{
$sql="DELETE from importation where id=".$id;
mysqli_query($conn, $sql);
header("Location: index.php");
}
?>