Commit b4b8c8d5 authored by okhin's avatar okhin 🚴

Adding a parent fild to only use the most expensive counterpart from a batch to compute cumul

parent c64f4403
Pipeline #2327 passed with stages
in 19 seconds
...@@ -439,7 +439,9 @@ class Admin extends Controller ...@@ -439,7 +439,9 @@ class Admin extends Controller
} }
if ($user['cumul'] >= $needed) { if ($user['cumul'] >= $needed) {
// Assez de point, on crée // Assez de point, on crée
$f3->get('container')['command_handler']->handle(new CounterpartCreateCommand($adresse['id'], $user['id'], $quoi, 1, 2, date("Y-m-d H:i:s"), 'Imported from a file')); // On récupère le dernier ID inséré
$parent = $f3->get('container')['counterpart_finder']->getLastInsertedId() + 1;
$f3->get('container')['command_handler']->handle(new CounterpartCreateCommand($adresse['id'], $user['id'], $quoi, 1, 2, date("Y-m-d H:i:s"), 'Imported from a file', $parent));
// On mets à jour le cumul de l'utilisateur // On mets à jour le cumul de l'utilisateur
$f3->get('container')['command_handler']->handle(new UserUpdateCumulCommand($user['id'], $user['cumul'] - $needed)); $f3->get('container')['command_handler']->handle(new UserUpdateCumulCommand($user['id'], $user['cumul'] - $needed));
$total[$quoi] +=1; $total[$quoi] +=1;
......
...@@ -309,6 +309,7 @@ class Perso extends Controller ...@@ -309,6 +309,7 @@ class Perso extends Controller
$f3->error('401'); $f3->error('401');
} }
$db = $f3->get('DB');
$user = $f3->get('container')['user_finder']->findById($f3->get('SESSION.id')); $user = $f3->get('container')['user_finder']->findById($f3->get('SESSION.id'));
// Récupération des valeurs du formulaire // Récupération des valeurs du formulaire
...@@ -336,18 +337,19 @@ class Perso extends Controller ...@@ -336,18 +337,19 @@ class Perso extends Controller
// Ajout d'une demande de contrepartie pour chaque contrepartie // Ajout d'une demande de contrepartie pour chaque contrepartie
if ((int) $user['cumul'] >= $valeur) { if ((int) $user['cumul'] >= $valeur) {
$parent = $f3->get('container')['counterpart_finder']->getLastInsertedId() + 1;
switch ($quoi) { switch ($quoi) {
case 'piplome':
$f3->get('container')['command_handler']->handle(new CounterpartCreateCommand(\Utils::asl($adresse_id), $f3->get('SESSION.id'), 'piplome', \Utils::asl($piplome_id), 1, date("Y-m-d H:i:s"), \Utils::asl($commentaire)));
break;
case 'pibag':
$f3->get('container')['command_handler']->handle(new CounterpartCreateCommand(\Utils::asl($adresse_id), $f3->get('SESSION.id'), 'pibag', '', 1, date("Y-m-d H:i:s"), \Utils::asl($commentaire)));
break;
case 'pishirt':
$f3->get('container')['command_handler']->handle(new CounterpartCreateCommand(\Utils::asl($adresse_id), $f3->get('SESSION.id'), 'pishirt', \Utils::asl($taille), 1, date("Y-m-d H:i:s"), \Utils::asl($commentaire)));
break;
case 'hoodie': case 'hoodie':
$f3->get('container')['command_handler']->handle(new CounterpartCreateCommand(\Utils::asl($adresse_id), $f3->get('SESSION.id'), 'hoodie', \Utils::asl($taille_h), 1, date("Y-m-d H:i:s"), \Utils::asl($commentaire))); $f3->get('container')['command_handler']->handle(new CounterpartCreateCommand(\Utils::asl($adresse_id), $f3->get('SESSION.id'), 'hoodie', \Utils::asl($taille_h), 1, date("Y-m-d H:i:s"), \Utils::asl($commentaire), $parent));
// no break
case 'pishirt':
$f3->get('container')['command_handler']->handle(new CounterpartCreateCommand(\Utils::asl($adresse_id), $f3->get('SESSION.id'), 'pishirt', \Utils::asl($taille), 1, date("Y-m-d H:i:s"), \Utils::asl($commentaire), $parent));
// no break
case 'pibag':
$f3->get('container')['command_handler']->handle(new CounterpartCreateCommand(\Utils::asl($adresse_id), $f3->get('SESSION.id'), 'pibag', '', 1, date("Y-m-d H:i:s"), \Utils::asl($commentaire), $parent));
// no break
case 'piplome':
$f3->get('container')['command_handler']->handle(new CounterpartCreateCommand(\Utils::asl($adresse_id), $f3->get('SESSION.id'), 'piplome', \Utils::asl($piplome_id), 1, date("Y-m-d H:i:s"), \Utils::asl($commentaire), $parent));
break; break;
} }
// Puis diminution du cumul de la valeur des contreparties // Puis diminution du cumul de la valeur des contreparties
......
<?php
use Phinx\Migration\AbstractMigration;
class AddParentField extends AbstractMigration
{
/**
* Change Method.
*
* Write your reversible migrations using this method.
*
* More information on writing migrations is available here:
* http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
*
* The following commands can be used in this method and Phinx will
* automatically reverse them when rolling back:
*
* createTable
* renameTable
* addColumn
* addCustomColumn
* renameColumn
* addIndex
* addForeignKey
*
* Any other destructive changes will result in an error when trying to
* rollback the migration.
*
* Remember to call "create()" or "update()" and NOT "save()" when working
* with the Table class.
*/
public function change()
{
$table = $this->table('contreparties');
if (!$table->hasColumn('parent')) {
$table->addColumn('parent', 'integer');
$table->update();
}
$builder = $this->getQueryBuilder();
$stmt = $builder->update('contreparties')
->set('parent', 'id')
->where(['parent' => ''])
->execute();
}
}
...@@ -23,6 +23,7 @@ class CounterpartSeeder extends AbstractSeed ...@@ -23,6 +23,7 @@ class CounterpartSeeder extends AbstractSeed
'taille' => 8, 'taille' => 8,
'status' => 1, 'status' => 1,
'adresse_id' => 1, 'adresse_id' => 1,
'parent' => 1,
), ),
array( array(
'id' => 2, 'id' => 2,
...@@ -32,6 +33,7 @@ class CounterpartSeeder extends AbstractSeed ...@@ -32,6 +33,7 @@ class CounterpartSeeder extends AbstractSeed
'taille' => 2, 'taille' => 2,
'status' => 2, 'status' => 2,
'adresse_id' => null, 'adresse_id' => null,
'parent' => 4,
), ),
array( array(
'id' => 3, 'id' => 3,
...@@ -41,6 +43,7 @@ class CounterpartSeeder extends AbstractSeed ...@@ -41,6 +43,7 @@ class CounterpartSeeder extends AbstractSeed
'taille' => 2, 'taille' => 2,
'status' => 2, 'status' => 2,
'adresse_id' => null, 'adresse_id' => null,
'parent' => 4,
), ),
array( array(
'id' => 4, 'id' => 4,
...@@ -50,6 +53,7 @@ class CounterpartSeeder extends AbstractSeed ...@@ -50,6 +53,7 @@ class CounterpartSeeder extends AbstractSeed
'taille' => 2, 'taille' => 2,
'status' => 2, 'status' => 2,
'adresse_id' => null, 'adresse_id' => null,
'parent' => 4,
), ),
); );
......
...@@ -12,7 +12,7 @@ class CounterpartCreateCommand ...@@ -12,7 +12,7 @@ class CounterpartCreateCommand
private $status; private $status;
private $date; private $date;
public function __construct($addressId, $userId, $quoi, $taille, $status, $date, $commentaire) public function __construct($addressId, $userId, $quoi, $taille, $status, $date, $commentaire, $parent)
{ {
$this->userId = $userId; $this->userId = $userId;
$this->addressId = $addressId; $this->addressId = $addressId;
...@@ -21,6 +21,7 @@ class CounterpartCreateCommand ...@@ -21,6 +21,7 @@ class CounterpartCreateCommand
$this->status = $status; $this->status = $status;
$this->date = $date; $this->date = $date;
$this->commentaire = $commentaire; $this->commentaire = $commentaire;
$this->parent = $parent;
} }
public function getUserId() public function getUserId()
...@@ -57,4 +58,9 @@ class CounterpartCreateCommand ...@@ -57,4 +58,9 @@ class CounterpartCreateCommand
{ {
return $this->commentaire; return $this->commentaire;
} }
public function getParent()
{
return $this->parent;
}
} }
...@@ -57,6 +57,18 @@ EOQ; ...@@ -57,6 +57,18 @@ EOQ;
return $counterparts; return $counterparts;
} }
/**
* return the last inserted id
*
* @return int
*/
public function getLastInsertedId()
{
return (int) $this->connection->fetchColumn(
"SELECT AUTO_INCREMENT FROM information_schema.tables WHERE table_name = 'contreparties' AND table_schema = DATABASE()"
);
}
/** /**
* Return all counterparts in a specific status * Return all counterparts in a specific status
* *
......
...@@ -43,8 +43,8 @@ class CounterpartHandler ...@@ -43,8 +43,8 @@ class CounterpartHandler
$adresseId = $command->getAddressId(); $adresseId = $command->getAddressId();
$query =<<<EOF $query =<<<EOF
INSERT INTO contreparties(datec, user_id, adresse_id, quoi, taille, status, commentaire) INSERT INTO contreparties(datec, user_id, adresse_id, quoi, taille, status, commentaire, parent)
VALUES (:datec, :user_id, :adresse_id, :quoi, :taille, :status, :commentaire) VALUES (:datec, :user_id, :adresse_id, :quoi, :taille, :status, :commentaire, :parent)
EOF; EOF;
$stmt = $this->connection->prepare($query); $stmt = $this->connection->prepare($query);
$stmt->bindValue('datec', $command->getDateCreation()); $stmt->bindValue('datec', $command->getDateCreation());
...@@ -54,6 +54,7 @@ EOF; ...@@ -54,6 +54,7 @@ EOF;
$stmt->bindValue('taille', $command->getTaille()); $stmt->bindValue('taille', $command->getTaille());
$stmt->bindValue('status', $command->getStatus()); $stmt->bindValue('status', $command->getStatus());
$stmt->bindValue('commentaire', $command->getCommentaire()); $stmt->bindValue('commentaire', $command->getCommentaire());
$stmt->bindValue('parent', $command->getParent());
$stmt->execute(); $stmt->execute();
} }
......
...@@ -100,12 +100,13 @@ class UserHandler ...@@ -100,12 +100,13 @@ class UserHandler
])->fetchAll(\PDO::FETCH_COLUMN)[0]; ])->fetchAll(\PDO::FETCH_COLUMN)[0];
// Let's compute the cumul too // Let's compute the cumul too
// We only want line where id == parent, others are children of one claim
// quoi = 'hoodie' somme = 250 // quoi = 'hoodie' somme = 250
// quoi = 'pishirt' somme = 100 // quoi = 'pishirt' somme = 100
// quoi = 'pibag' somme = 50 // quoi = 'pibag' somme = 50
// quoi = 'piplome' somme = 30 // quoi = 'piplome' somme = 30
$spent = $this->connection->executeQuery( $spent = $this->connection->executeQuery(
"SELECT sum(IF(quoi = 'hoodie',250,0)) + sum(IF(quoi = 'pishirt', 100, 0)) + sum(IF(quoi = 'pibag', 50, 0)) + sum(IF(quoi = 'piplome', 30, 0)) AS spent FROM contreparties WHERE user_id = :user_id", "SELECT sum(IF(quoi = 'hoodie',250,0)) + sum(IF(quoi = 'pishirt', 100, 0)) + sum(IF(quoi = 'pibag', 50, 0)) + sum(IF(quoi = 'piplome', 30, 0)) AS spent FROM contreparties WHERE user_id = :user_id AND contreparties.id = contreparties.parent",
[ 'user_id' => $user_id] [ 'user_id' => $user_id]
)->fetchAll(\PDO::FETCH_COLUMN)[0]; )->fetchAll(\PDO::FETCH_COLUMN)[0];
$this->connection->executeUpdate('UPDATE users SET total = :total, cumul = :cumul WHERE id = :user_id', ['total' => (int) $total, 'cumul' => (int) $total - (int) $spent, 'user_id' => $user_id]); $this->connection->executeUpdate('UPDATE users SET total = :total, cumul = :cumul WHERE id = :user_id', ['total' => (int) $total, 'cumul' => (int) $total - (int) $spent, 'user_id' => $user_id]);
......
...@@ -26,6 +26,7 @@ class CounterpartFinderTest extends FunctionalTest ...@@ -26,6 +26,7 @@ class CounterpartFinderTest extends FunctionalTest
'pdf_nom' => 'Main', 'pdf_nom' => 'Main',
'pdf_url' => 'pdf', 'pdf_url' => 'pdf',
'commentaire' => '', 'commentaire' => '',
'parent' => '4',
]; ];
$this->assertEquals($expectedCounterpart, $firstCounterpart); $this->assertEquals($expectedCounterpart, $firstCounterpart);
} }
...@@ -49,6 +50,7 @@ class CounterpartFinderTest extends FunctionalTest ...@@ -49,6 +50,7 @@ class CounterpartFinderTest extends FunctionalTest
'pdf_nom' => 'Main', 'pdf_nom' => 'Main',
'pdf_url' => 'pdf', 'pdf_url' => 'pdf',
'commentaire' => '', 'commentaire' => '',
'parent' => '4',
]; ];
$this->assertEquals($expectedCounterpart, $firstCounterpart); $this->assertEquals($expectedCounterpart, $firstCounterpart);
...@@ -72,6 +74,7 @@ class CounterpartFinderTest extends FunctionalTest ...@@ -72,6 +74,7 @@ class CounterpartFinderTest extends FunctionalTest
'pdf_nom' => '', 'pdf_nom' => '',
'pdf_url' => '', 'pdf_url' => '',
'commentaire' => '', 'commentaire' => '',
'parent' => '1',
]; ];
$this->assertEquals($expectedCounterpart, $firstCounterpart); $this->assertEquals($expectedCounterpart, $firstCounterpart);
} }
......
...@@ -21,7 +21,7 @@ class CounterpartHandlerTest extends FunctionalTest ...@@ -21,7 +21,7 @@ class CounterpartHandlerTest extends FunctionalTest
{ {
$this->assertFalse($this->counterpartExists(5)); $this->assertFalse($this->counterpartExists(5));
$this->container['command_handler']->handle(new CounterpartCreateCommand(1, 1, 'pishirt', 4, 1, date("Y-m-d H:i:s"), '')); $this->container['command_handler']->handle(new CounterpartCreateCommand(1, 1, 'pishirt', 4, 1, date("Y-m-d H:i:s"), '', 1));
} }
public function testCounterpartChangeState() public function testCounterpartChangeState()
......
...@@ -16,6 +16,8 @@ class UserHandlerTest extends FunctionalTest ...@@ -16,6 +16,8 @@ class UserHandlerTest extends FunctionalTest
$this->container['command_handler']->handle(new AdminUpdateTotalUsersCommand()); $this->container['command_handler']->handle(new AdminUpdateTotalUsersCommand());
$this->assertSame(1000, (int) $this->getUser(1)['total']); $this->assertSame(1000, (int) $this->getUser(1)['total']);
$this->assertSame(900, (int) $this->getUser(1)['cumul']); $this->assertSame(900, (int) $this->getUser(1)['cumul']);
$this->assertSame(1000, (int) $this->getUser(2)['total']);
$this->assertSame(750, (int) $this->getUser(2)['cumul']);
} }
public function testUserUpdateFromAdmin() public function testUserUpdateFromAdmin()
......
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