diff --git a/app/controller/admin.php b/app/controller/admin.php index 9ac4db2cc094bbe0b36dad6467ef1fcb4553e211..7135557a3b2893beac5b131870ddcc07e9380229 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']->getNextInsertedId(); + $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..ada08ea16574ee564f1334fd3462faada874e6c4 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']->getNextInsertedId(); 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/app/view/backend/contreparties.html b/app/view/backend/contreparties.html index 8f71fd0a135c033e4c6298644861d0b31befe959..b77647ddcbbce4991dc849e4facb3168e52f66f7 100644 --- a/app/view/backend/contreparties.html +++ b/app/view/backend/contreparties.html @@ -43,6 +43,7 @@ <table cellspacing="0" cellpadding="0"> <theader> <th>Id</th> + <th>Id du parent</th> <th>Email</th> <th>Date</th> <th>Quoi</th> @@ -56,7 +57,8 @@ <repeat group="{{ @data }}" value="{{ @row }}"> <tr> <td>{{ @row.id }}</td> - <td><a href="{{'user_info', 'id='.@row.user_id | alias }}">{{ @row.email }}</a></td> + <td>{{ @row.parent }}</td> + <td><a href="{{'user_info', 'id='.@row.user_id | alias }}">{{ @row.email }}</a></td> <td>{{ @row.datec }}</td> <td>{{ @row.quoi }}</td> <td> diff --git a/app/view/backend/user.html b/app/view/backend/user.html index c0ca52a8b1cd82afbc7d895d9dd1ae877c5d60a8..77aeb5fd429fb72aeedd00429176e1136a731fe7 100644 --- a/app/view/backend/user.html +++ b/app/view/backend/user.html @@ -95,6 +95,7 @@ <table cellspacing="0" cellpadding="0"> <theader> <th>Id</th> + <th>Id du parent</th> <th>Date</th> <th>Quoi</th> <th>Taille/Piplome</th> @@ -106,6 +107,7 @@ <repeat group="{{ @user.contreparties }}" value="{{ @row }}"> <tr> <td>{{ @row.id }}</td> + <td>{{ @row.parent }}</td> <td>{{ @row.datec }}</td> <td>{{ @row.quoi }}</td> <td> diff --git a/db/migrations/20181213125806_add_parent_field.php b/db/migrations/20181213125806_add_parent_field.php new file mode 100644 index 0000000000000000000000000000000000000000..c9364aa7c57e0631dd9eb1a549c4a9fc57cd693c --- /dev/null +++ b/db/migrations/20181213125806_add_parent_field.php @@ -0,0 +1,58 @@ +<?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 up() + { + $table = $this->table('contreparties'); + if (!$table->hasColumn('parent')) { + $table->addColumn('parent', 'integer'); + $table->save(); + } + + // Let's set some default value + $builder = $this->getQueryBuilder(); + $stmt = $builder->update('contreparties', 'c') + ->set('parent', 'id') +// ->where('parent = 0') + ->execute(); + } + + public function down() + { + $table = $this->table('contreparties'); + + if ($table->hasColumn('parent')) { + $table->removeColumn('parent') + ->save(); + } + } +} 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..16c4bbefa51da6e4014d9851e1409475bbb30856 100644 --- a/src/LQDN/Finder/CounterpartFinder.php +++ b/src/LQDN/Finder/CounterpartFinder.php @@ -57,6 +57,18 @@ EOQ; return $counterparts; } + /** + * return the id of the next item to be inserted + * + * @return int + */ + public function getNextInsertedId() + { + 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 =<<<EOF -INSERT INTO contreparties(datec, user_id, adresse_id, quoi, taille, status, commentaire) -VALUES (: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, :parent) EOF; $stmt = $this->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..0ae0a0de1422efdba87c394d231bdce807862a3e 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 id = 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()