Commit 67bf68ce authored by okhin's avatar okhin 🚴

Fixing the CSV imports

parent d7f61fc3
Pipeline #2254 failed with stages
in 1 minute and 24 seconds
......@@ -7,6 +7,7 @@ use LQDN\Command\AdminChangePasswordCommand;
use LQDN\Command\DonationValidateCommand;
use LQDN\Command\DonationInvalidateCommand;
use LQDN\Command\DonationResetPdfCommand;
use LQDN\Command\DonationCreateCommand;
use LQDN\Command\UserUpdateByAdminCommand;
class Admin extends Controller
......@@ -68,15 +69,15 @@ class Admin extends Controller
{
if ($f3->get('VERB')=='POST' && $f3->exists('POST.action')) {
switch ($f3->get('POST.action')) {
case 'valid':
$f3->get('container')['command_handler']->handle(new DonationValidateCommand($f3->get('POST.id')));
break;
case 'invalid':
$f3->get('container')['command_handler']->handle(new DonationInvalidateCommand($f3->get('POST.id')));
break;
case 'pdf':
$f3->get('container')['command_handler']->handle(new DonationResetPdfCommand($f3->get('POST.id')));
break;
case 'valid':
$f3->get('container')['command_handler']->handle(new DonationValidateCommand($f3->get('POST.id')));
break;
case 'invalid':
$f3->get('container')['command_handler']->handle(new DonationInvalidateCommand($f3->get('POST.id')));
break;
case 'pdf':
$f3->get('container')['command_handler']->handle(new DonationResetPdfCommand($f3->get('POST.id')));
break;
}
}
......@@ -143,7 +144,7 @@ class Admin extends Controller
"UPDATE dons
SET status = $status
WHERE id = ".\Utils::asl($id)
);
);
$db->query("UPDATE users
SET total = $total,
cumul = $cumul
......@@ -193,7 +194,7 @@ class Admin extends Controller
"UPDATE dons
SET status = $status
WHERE id = ".\Utils::asl($id)
);
);
$db->query("UPDATE users
SET total = $total,
cumul = $cumul
......@@ -241,8 +242,8 @@ class Admin extends Controller
if ($id=='') {
// We need to check if there's already a user with this email
$result = $db->query("SELECT id
FROM users
WHERE email='".\Utils::asl($email)."'");
FROM users
WHERE email='".\Utils::asl($email)."'");
if ($result) {
// The user already exist, lets get the user_id
$user_id = $result->fetch(\PDO::FETCH_ASSOC);
......@@ -250,53 +251,53 @@ class Admin extends Controller
} else {
// Let's create the account first
$db->query("INSERT INTO users
(email,
pseudo,
cumul,
total)
VALUES('".\Utils::asl($email)."',
'".\Utils::asl($pseudo)."',
'".\Utils::asl($somme)."',
'0')");
(email,
pseudo,
cumul,
total)
VALUES('".\Utils::asl($email)."',
'".\Utils::asl($pseudo)."',
'".\Utils::asl($somme)."',
'0')");
$user_id = $db->lastInsertId();
}
$db->query("INSERT INTO dons
(datec,
user_id,
somme,
lang,
abo,
public,
status)
VALUES
(now(),
'".\Utils::asl($user_id)."',
'".\Utils::asl($somme)."',
'".\Utils::asl($lang)."',
'".\Utils::asl($abo)."',
'".\Utils::asl($public)."',
1)");
(datec,
user_id,
somme,
lang,
abo,
public,
status)
VALUES
(now(),
'".\Utils::asl($user_id)."',
'".\Utils::asl($somme)."',
'".\Utils::asl($lang)."',
'".\Utils::asl($abo)."',
'".\Utils::asl($public)."',
1)");
// Récupération de l'id pour passage en mode édition
$id = $db->lastInsertId();
} else {
// We will get the ids associated to the gift first
$result = $db->query("SELECT adresse_id,
user_id
FROM dons
WHERE id = ".\Utils::asl($f3->get('POST.id')));
user_id
FROM dons
WHERE id = ".\Utils::asl($f3->get('POST.id')));
$result = $result->fetch(\PDO::FETCH_ASSOC);
// Let's update the adresse first or create the adress
$db->query("UPDATE users
SET email='".\Utils::asl($email)."',
pseudo='".\Utils::asl($pseudo)."'
WHERE id={$result['user_id']}");
SET email='".\Utils::asl($email)."',
pseudo='".\Utils::asl($pseudo)."'
WHERE id={$result['user_id']}");
$db->query("UPDATE dons
SET somme='".\Utils::asl($somme)."',
lang='".\Utils::asl($lang)."',
abo='".\Utils::asl($abo)."',
public='".\Utils::asl($public)."',
adresse_id='".$result['adresse_id']."'
WHERE id='".\Utils::asl($_POST['id'])."'");
SET somme='".\Utils::asl($somme)."',
lang='".\Utils::asl($lang)."',
abo='".\Utils::asl($abo)."',
public='".\Utils::asl($public)."',
adresse_id='".$result['adresse_id']."'
WHERE id='".\Utils::asl($_POST['id'])."'");
}
}
}
......@@ -304,17 +305,17 @@ class Admin extends Controller
if ($id!='') {
// Récupération du don, de l'adresse et de l'utilisateur
$result = $db->query("SELECT d.id AS id,
u.email AS email,
u.pseudo AS pseudo,
d.somme AS somme,
d.lang AS lang,
d.abo AS abo,
d.public AS public,
d.adresse_id AS adresse_id,
d.user_id AS user_id
FROM dons d
JOIN users u ON u.id = d.user_id
WHERE d.id=".$id);
u.email AS email,
u.pseudo AS pseudo,
d.somme AS somme,
d.lang AS lang,
d.abo AS abo,
d.public AS public,
d.adresse_id AS adresse_id,
d.user_id AS user_id
FROM dons d
JOIN users u ON u.id = d.user_id
WHERE d.id=".$id);
$don = $result->fetch(\PDO::FETCH_ASSOC);
} else {
$don = array(
......@@ -350,10 +351,10 @@ class Admin extends Controller
}
// Calcul des quantités
$query = "SELECT quoi,
status,
COUNT(status) AS qte
FROM contreparties
GROUP BY quoi, status;";
status,
COUNT(status) AS qte
FROM contreparties
GROUP BY quoi, status;";
$result = $db->query($query);
if ($result) {
foreach ($result->fetchAll(\PDO::FETCH_ASSOC) as $row) {
......@@ -366,10 +367,10 @@ class Admin extends Controller
$f3->set('hoodie_t'.$i, 0);
}
$query = "SELECT quoi,
taille,
COUNT(taille) AS qte
FROM contreparties
GROUP BY quoi, taille;";
taille,
COUNT(taille) AS qte
FROM contreparties
GROUP BY quoi, taille;";
$result = $db->query($query);
if ($result) {
foreach ($result->fetchAll(\PDO::FETCH_ASSOC) as $row) {
......@@ -428,34 +429,34 @@ class Admin extends Controller
}
$query = "SELECT c.id,
c.datec as datec,
c.quoi as quoi,
c.taille as taille,
c.status as status,
c.user_id as user_id,
u.email as email,
u.pseudo as pseudo,
u.commentaire as commentaire,
a.nom as nom,
a.adresse as adresse,
a.adresse2 as adresse2,
a.codepostal as codepostal,
a.ville as ville,
a.pays as pays
FROM contreparties c
INNER JOIN users u ON c.user_id = u.id
INNER JOIN adresses a ON c.adresse_id = a.id
WHERE 1=1 ";
c.datec as datec,
c.quoi as quoi,
c.taille as taille,
c.status as status,
c.user_id as user_id,
u.email as email,
u.pseudo as pseudo,
u.commentaire as commentaire,
a.nom as nom,
a.adresse as adresse,
a.adresse2 as adresse2,
a.codepostal as codepostal,
a.ville as ville,
a.pays as pays
FROM contreparties c
INNER JOIN users u ON c.user_id = u.id
INNER JOIN adresses a ON c.adresse_id = a.id
WHERE 1=1 ";
if ($texte) {
$query .= " AND (c.id like '%".$texte."%'
OR u.pseudo like '%".$texte."%'
OR u.email like '%".$texte."%'
OR a.nom like '%".$texte."%'
OR a.ville like '%".$texte."%'
OR a.pays like '%".$texte."%'
OR a.codepostal like '%".$texte."%'
OR a.adresse2 like '%".$texte."%'
OR a.adresse like '%".$texte."%')";
OR u.pseudo like '%".$texte."%'
OR u.email like '%".$texte."%'
OR a.nom like '%".$texte."%'
OR a.ville like '%".$texte."%'
OR a.pays like '%".$texte."%'
OR a.codepostal like '%".$texte."%'
OR a.adresse2 like '%".$texte."%'
OR a.adresse like '%".$texte."%')";
}
switch ($quoi) {
case 0:
......@@ -474,19 +475,19 @@ class Admin extends Controller
break;
default:
$query .= " AND datec IN (
SELECT datec
FROM contreparties
WHERE status=1
GROUP BY user_id, datec
HAVING SUM(
CASE quoi
WHEN 'pibag' THEN 1
WHEN 'piplome' THEN 2
WHEN 'pishirt' THEN 4
WHEN 'hoodie' THEN 8
END
)=$quoi)
";
SELECT datec
FROM contreparties
WHERE status=1
GROUP BY user_id, datec
HAVING SUM(
CASE quoi
WHEN 'pibag' THEN 1
WHEN 'piplome' THEN 2
WHEN 'pishirt' THEN 4
WHEN 'hoodie' THEN 8
END
)=$quoi)
";
}
if ($status) {
$query .= " AND c.status IN (".\Utils::asl($status).") ";
......@@ -501,11 +502,11 @@ class Admin extends Controller
foreach ($result->fetchAll(\PDO::FETCH_ASSOC) as $row) {
if ($row['quoi']=='piplome') {
$pdf = $db->query("SELECT d.id AS id,
a.nom AS pseudo,
d.pdf AS pdf
FROM dons d
JOIN adresses a ON a.id = d.adresse_id
WHERE d.id='".$row['taille']."'");
a.nom AS pseudo,
d.pdf AS pdf
FROM dons d
JOIN adresses a ON a.id = d.adresse_id
WHERE d.id='".$row['taille']."'");
if ($pdf) {
$pdf = $pdf->fetch(\PDO::FETCH_ASSOC);
}
......@@ -635,38 +636,38 @@ class Admin extends Controller
if ($id!='') {
// First, let's retrieve the adresse from the adresse_id if it exists
$result = $db->query("SELECT adresse_id
FROM contreparties
WHERE id = '".\Utils::asl($f3->get('POST.id'))."'
AND adresse_id IS NOT NULL");
FROM contreparties
WHERE id = '".\Utils::asl($f3->get('POST.id'))."'
AND adresse_id IS NOT NULL");
$result = $result->fetch(\PDO::FETCH_ASSOC);
$adresse_id = $result['adresse_id'];
if (!$adresse_id or $adresse_id == 0) {
// We need to create an adresse_id
$db->query("INSERT INTO adresses
VALUES (nom='".\Utils::asl($nom)."',
adresse='".\Utils::asl($adresse)."',
adresse2='".\Utils::asl($adresse2)."',
codepostal='".\Utils::asl($codepostal)."',
ville='".\Utils::asl($ville)."',
pays='".\Utils::asl($pays)."')");
VALUES (nom='".\Utils::asl($nom)."',
adresse='".\Utils::asl($adresse)."',
adresse2='".\Utils::asl($adresse2)."',
codepostal='".\Utils::asl($codepostal)."',
ville='".\Utils::asl($ville)."',
pays='".\Utils::asl($pays)."')");
$adresse_id = $db->lastInsertId();
} else {
// Updating the adresses first
$db->query("UPDATE adresses
SET nom='".\Utils::asl($nom)."',
adresse='".\Utils::asl($adresse)."',
adresse2='".\Utils::asl($adresse2)."',
codepostal='".\Utils::asl($codepostal)."',
ville='".\Utils::asl($ville)."',
pays='".\Utils::asl($pays)."'
adresse='".\Utils::asl($adresse)."',
adresse2='".\Utils::asl($adresse2)."',
codepostal='".\Utils::asl($codepostal)."',
ville='".\Utils::asl($ville)."',
pays='".\Utils::asl($pays)."'
WHERE id = $adresse_id");
};
$db->query("UPDATE contreparties
SET quoi='".\Utils::asl($quoi)."',
taille='".\Utils::asl($taille)."',
status='".\Utils::asl($status)."'
WHERE id='".\Utils::asl($_POST['id'])."'");
SET quoi='".\Utils::asl($quoi)."',
taille='".\Utils::asl($taille)."',
status='".\Utils::asl($status)."'
WHERE id='".\Utils::asl($_POST['id'])."'");
// We will update the comment
$db->query("UPDATE users SET commentaire='".\Utils::asl($comment)."' WHERE id='".\Utils::asl($user_id)."'");
}
......@@ -693,13 +694,13 @@ class Admin extends Controller
$pdfs = array();
if ($contrepartie['quoi']=='piplome') {
$result = $db->query("SELECT d.id AS id,
d.pdf AS pdf,
a.nom AS nom
FROM dons d
INNER JOIN adresses a ON d.adresse_id = a.id
WHERE pdf<>''
AND d.status IN (1,4,101)
AND d.user_id='".$contrepartie['user_id']."'");
d.pdf AS pdf,
a.nom AS nom
FROM dons d
INNER JOIN adresses a ON d.adresse_id = a.id
WHERE pdf<>''
AND d.status IN (1,4,101)
AND d.user_id='".$contrepartie['user_id']."'");
if ($result) {
foreach ($result->fetchAll(\PDO::FETCH_ASSOC) as $pdf) {
$pdfs[] = $pdf;
......@@ -857,7 +858,7 @@ class Admin extends Controller
$query = "SELECT id, email, expiration, status FROM users WHERE identifier!='' AND identifier IS NOT NULL AND status != ''";
if ($texte!='') {
$query .= " AND (email like '%".$texte."%'
OR pseudo like '%".$texte."%')";
OR pseudo like '%".$texte."%')";
}
if ($status!='') {
$query .= " AND status='".$status."'";
......@@ -1037,12 +1038,12 @@ class Admin extends Controller
$db->query("UPDATE identifiers SET expiration='".$expire. "' WHERE identifier='".$identifier."'");
if ($expire_date<$today_date) {
$db->query("INSERT INTO dons SET
status = '103',
datec = NOW(),
somme = 0,
user_id = '" .$user_id ."',
identifier = '" . $identifier ."',
cumul = 0;");
status = '103',
datec = NOW(),
somme = 0,
user_id = '" .$user_id ."',
identifier = '" . $identifier ."',
cumul = 0;");
}
}
}
......@@ -1236,14 +1237,14 @@ class Admin extends Controller
));
// Pour un don récurrent, on recherche son équivalent qui peut ne pas exister
$result = $db->query("SELECT email
FROM users
JOIN identifiers
ON users.id = identifiers.user_id
AND identifiers.identifier='".$identifier. "'");
FROM users
JOIN identifiers
ON users.id = identifiers.user_id
AND identifiers.identifier='".$identifier. "'");
if ($result->rowCount() == 0) {
// Identifier does not exist, we must create it. We have either a pseudo or an email.
$user_field = explode("_", $identifier)[1];
$user = 'asfhjk';
$user = '';
if (strpos($user_field, '@')) {
// identifier is an email
......@@ -1271,35 +1272,50 @@ class Admin extends Controller
} else {
$emails[$email] = 1;
}
// On a besoin de l'utilisateur
$user = $f3->get('container')['user_finder']->findByEmail($email);
// Récupération de l'id du bon abonnement
$result = $db->query("SELECT d.id AS id
$stmt = $db->query("SELECT d.id AS id
FROM dons d
JOIN users u ON u.id = d.user_id
WHERE u.email='".$email. "'
WHERE u.email='".$email."'
AND d.status=101
AND d.id='".$transaction."' ");
print_r($result);
if ($result->rowCount() > 0) {
$cumul = $result->fetch(\PDO::FETCH_ASSOC);
$cumul = $result['id'];
if ($cumul!='') {
$result = $db->query("SELECT COUNT(1)
FROM dons d
JOIN users u ON u.id = d.user_id
WHERE u.email='".$email. "'
AND d.status=102
AND d.datec='".$new_effect."'
AND d.cumul=".$cumul."
");
if ($result) {
$combien = $result->fetch();
if ($combien[0]==0) {
$update ++;
// Don récurrent non comptabilisé, on l'ajoute
$user_id = $db->query("SELECT id FROM users WHERE email='$email'");
$user_id = $user_id->fetch(\PDO::FETCH_ASSOC);
$user_id = $user_id['id'];
$query = "INSERT INTO dons (status,
AND d.id=".$transaction);
$result = $stmt->fetch(\PDO::FETCH_ASSOC);
if (!$result) {
// don non trouvé en statut 101. On le crée (la banque à raison)
// statut = cumul
$f3->get('container')['command_handler']
->handle(new DonationCreateCommand(
$user['id'],
101,
$new_effect,
$amount,
0,
0
));
// Et on le récupère
$cumul = $db->lastInsertId();
$don = $f3->get('container')['donation_finder']->findById($cumul);
} else {
$cumul = $result->fetch(\PDO::FETCH_ASSOC);
$cumul = $result['id'];
}
// On regarde si le don n'as pas déjà été ajouté (en vérifiant la datec)
$result = $db->query("SELECT COUNT(1)
FROM dons d
JOIN users u ON u.id = d.user_id
WHERE u.email='".$email. "'
AND d.status=102
AND d.datec='".$new_effect."'
AND d.cumul=".$cumul."
");
if ($result) {
$combien = $result->fetch();
if ($combien[0]==0) {
$update ++;
// Don récurrent non comptabilisé, on l'ajoute
$query = "INSERT INTO dons (status,
datec,
somme,
user_id,
......@@ -1309,25 +1325,21 @@ class Admin extends Controller
102,
'".$new_effect."',
".$amount.",
'".$user_id."',
'".$user['id']."',
'".$cumul."',
'".$identifier."')";
if (!$testing) {
$db->query($query);
// On met alors à jour le cumul et le total de l'utilisateur concerné
$result = $db->query("UPDATE users
if (!$testing) {
$db->query($query);
// On met alors à jour le cumul et le total de l'utilisateur concerné
$result = $db->query("UPDATE users
SET cumul=cumul+".$amount.",
total=total+".$amount."
WHERE id='".$user_id."'");
}
} else {
$comptabilise[] = $identifier;
WHERE id='".$user['id']."'");
}
} else {
$comptabilise[] = $identifier;
}
} else {
$sans_cumul[] = $email;
}
}
}
}
fclose($handle);
......@@ -1339,6 +1351,7 @@ class Admin extends Controller
return $datas;
}
public function stats($f3, $args)
{
$startingDay = new \DateTime('2014-11-12');
......
......@@ -32,6 +32,24 @@ class DonationFinder
return $donations;
}
/**
* findById
*
* @param int $donationId
*
* @return []
*/
public function findById($donationId)
{
$donationId = (int) $donationId;
$donations = [];
$stmt = $this->connection->query("SELECT * FROM dons WHERE id='$donationId'");
while ($donation = $stmt->fetch()) {
$donations[$donationId] = $donation;
}
return $donations;
}
/**
* Return dons for admins.
*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment