diff --git a/app/controller/admin.php b/app/controller/admin.php index 9ac4db2cc094bbe0b36dad6467ef1fcb4553e211..7b00620909c4408c1a022379012d70e49630cdeb 100644 --- a/app/controller/admin.php +++ b/app/controller/admin.php @@ -439,7 +439,9 @@ class Admin extends Controller } if ($user['cumul'] >= $needed) { // 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 $f3->get('container')['command_handler']->handle(new UserUpdateCumulCommand($user['id'], $user['cumul'] - $needed)); $total[$quoi] +=1; diff --git a/app/controller/perso.php b/app/controller/perso.php index dfccd26829515a4572f656a8b240943f04b74936..fd6b901a086b9f1c807abcd55780a1401a9a3d82 100644 --- a/app/controller/perso.php +++ b/app/controller/perso.php @@ -309,6 +309,7 @@ class Perso extends Controller $f3->error('401'); } + $db = $f3->get('DB'); $user = $f3->get('container')['user_finder']->findById($f3->get('SESSION.id')); // Récupération des valeurs du formulaire @@ -336,18 +337,19 @@ class Perso extends Controller // Ajout d'une demande de contrepartie pour chaque contrepartie if ((int) $user['cumul'] >= $valeur) { + $parent = $f3->get('container')['counterpart_finder']->getLastInsertedId() + 1; 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': - $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; } // Puis diminution du cumul de la valeur des contreparties diff --git a/db/migrations/20181213125806_add_parent_field.php b/db/migrations/20181213125806_add_parent_field.php new file mode 100644 index 0000000000000000000000000000000000000000..4b0061510792996a40bcef142b904c91d53cacb8 --- /dev/null +++ b/db/migrations/20181213125806_add_parent_field.php @@ -0,0 +1,46 @@ +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(); + } +} diff --git a/db/seeds/CounterpartSeeder.php b/db/seeds/CounterpartSeeder.php index 4d743dff616c84edd56c11c9d8b58f3ba1021856..b63ad95e68f1e990ba461744169c8ada7b5a83bd 100644 --- a/db/seeds/CounterpartSeeder.php +++ b/db/seeds/CounterpartSeeder.php @@ -23,6 +23,7 @@ class CounterpartSeeder extends AbstractSeed 'taille' => 8, 'status' => 1, 'adresse_id' => 1, + 'parent' => 1, ), array( 'id' => 2, @@ -32,6 +33,7 @@ class CounterpartSeeder extends AbstractSeed 'taille' => 2, 'status' => 2, 'adresse_id' => null, + 'parent' => 4, ), array( 'id' => 3, @@ -41,6 +43,7 @@ class CounterpartSeeder extends AbstractSeed 'taille' => 2, 'status' => 2, 'adresse_id' => null, + 'parent' => 4, ), array( 'id' => 4, @@ -50,6 +53,7 @@ class CounterpartSeeder extends AbstractSeed 'taille' => 2, 'status' => 2, 'adresse_id' => null, + 'parent' => 4, ), ); diff --git a/src/LQDN/Command/CounterpartCreateCommand.php b/src/LQDN/Command/CounterpartCreateCommand.php index 1a0a7e3ba44884b09be4609b7926039532762751..b3cdd6af93add5386f9a4a97c542f6597f99c73d 100644 --- a/src/LQDN/Command/CounterpartCreateCommand.php +++ b/src/LQDN/Command/CounterpartCreateCommand.php @@ -12,7 +12,7 @@ class CounterpartCreateCommand private $status; 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->addressId = $addressId; @@ -21,6 +21,7 @@ class CounterpartCreateCommand $this->status = $status; $this->date = $date; $this->commentaire = $commentaire; + $this->parent = $parent; } public function getUserId() @@ -57,4 +58,9 @@ class CounterpartCreateCommand { return $this->commentaire; } + + public function getParent() + { + return $this->parent; + } } diff --git a/src/LQDN/Finder/CounterpartFinder.php b/src/LQDN/Finder/CounterpartFinder.php index b566c55af7d5b1b250b2846a33d8388d0effdcf7..ac0dca00778d35ed59eeaaa3b5e93abf45d39afb 100644 --- a/src/LQDN/Finder/CounterpartFinder.php +++ b/src/LQDN/Finder/CounterpartFinder.php @@ -57,6 +57,18 @@ EOQ; 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 * diff --git a/src/LQDN/Handler/CounterpartHandler.php b/src/LQDN/Handler/CounterpartHandler.php index ef4b4af5812f3c83194bc22317880cda2d1a0009..8c228fc0cf78f9d2076660942a6c40ab138c56d4 100644 --- a/src/LQDN/Handler/CounterpartHandler.php +++ b/src/LQDN/Handler/CounterpartHandler.php @@ -43,8 +43,8 @@ class CounterpartHandler $adresseId = $command->getAddressId(); $query =<<connection->prepare($query); $stmt->bindValue('datec', $command->getDateCreation()); @@ -54,6 +54,7 @@ EOF; $stmt->bindValue('taille', $command->getTaille()); $stmt->bindValue('status', $command->getStatus()); $stmt->bindValue('commentaire', $command->getCommentaire()); + $stmt->bindValue('parent', $command->getParent()); $stmt->execute(); } diff --git a/src/LQDN/Handler/UserHandler.php b/src/LQDN/Handler/UserHandler.php index 5d4370e79b36847e6eeded77344dfcb24dc4d1cd..e9a4cbbd51ea5098eddaa66260401571f6c63485 100644 --- a/src/LQDN/Handler/UserHandler.php +++ b/src/LQDN/Handler/UserHandler.php @@ -100,12 +100,13 @@ class UserHandler ])->fetchAll(\PDO::FETCH_COLUMN)[0]; // Let's compute the cumul too + // We only want line where id == parent, others are children of one claim // quoi = 'hoodie' somme = 250 // quoi = 'pishirt' somme = 100 // quoi = 'pibag' somme = 50 // quoi = 'piplome' somme = 30 $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] )->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]); diff --git a/tests/functional/Finder/CounterpartFinderTest.php b/tests/functional/Finder/CounterpartFinderTest.php index 00a82e24aceae4a8f1e55e9ec42a4e4b1cdde0c3..f5b9da14ee32122f7cbd49c3fd74434c6cc0a2ec 100644 --- a/tests/functional/Finder/CounterpartFinderTest.php +++ b/tests/functional/Finder/CounterpartFinderTest.php @@ -26,6 +26,7 @@ class CounterpartFinderTest extends FunctionalTest 'pdf_nom' => 'Main', 'pdf_url' => 'pdf', 'commentaire' => '', + 'parent' => '4', ]; $this->assertEquals($expectedCounterpart, $firstCounterpart); } @@ -49,6 +50,7 @@ class CounterpartFinderTest extends FunctionalTest 'pdf_nom' => 'Main', 'pdf_url' => 'pdf', 'commentaire' => '', + 'parent' => '4', ]; $this->assertEquals($expectedCounterpart, $firstCounterpart); @@ -72,6 +74,7 @@ class CounterpartFinderTest extends FunctionalTest 'pdf_nom' => '', 'pdf_url' => '', 'commentaire' => '', + 'parent' => '1', ]; $this->assertEquals($expectedCounterpart, $firstCounterpart); } diff --git a/tests/functional/Handler/CounterpartHandlerTest.php b/tests/functional/Handler/CounterpartHandlerTest.php index 0aefcd5709d7e310bbb46d84df74c3f323cc2ea9..a40c099a9af302bf7231162bc592d065f9d35035 100644 --- a/tests/functional/Handler/CounterpartHandlerTest.php +++ b/tests/functional/Handler/CounterpartHandlerTest.php @@ -21,7 +21,7 @@ class CounterpartHandlerTest extends FunctionalTest { $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() diff --git a/tests/functional/Handler/UserHandlerTest.php b/tests/functional/Handler/UserHandlerTest.php index f245255b0aa1a0887a9615cd4b0bd0a02dfe90aa..61c6b9695fa0bfebd2e7ab4869c9d2964b7348f9 100644 --- a/tests/functional/Handler/UserHandlerTest.php +++ b/tests/functional/Handler/UserHandlerTest.php @@ -16,6 +16,8 @@ class UserHandlerTest extends FunctionalTest $this->container['command_handler']->handle(new AdminUpdateTotalUsersCommand()); $this->assertSame(1000, (int) $this->getUser(1)['total']); $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()