diff --git a/app/controller/admin.php b/app/controller/admin.php index 1eb7be1e94ecc484a5b05b51d28f359f65cc230a..d79b7a8c43875670fe15de2faa7e2ee4f901089c 100644 --- a/app/controller/admin.php +++ b/app/controller/admin.php @@ -4,12 +4,14 @@ namespace Controller; use LQDN\Command\AdminCreateCommand; use LQDN\Command\AdminDeleteCommand; use LQDN\Command\AdminChangePasswordCommand; +use LQDN\Command\CounterpartCreateCommand; use LQDN\Command\DonationValidateCommand; use LQDN\Command\DonationInvalidateCommand; use LQDN\Command\DonationResetPdfCommand; use LQDN\Command\DonationCreateCommand; use LQDN\Command\UserUpdateByAdminCommand; use LQDN\Command\UserCreateCommand; +use LQDN\Command\UserUpdateCumulCommand; use LQDN\Command\AdminUpdateTotalUsersCommand; class Admin extends Controller @@ -49,6 +51,7 @@ class Admin extends Controller )); $f3->set('TAILLES', array( + 0 => _('PlaceHolder -- ne pas utiliser') . ' S', 1 => _('Coupe Homme, Taille') . ' S', 2 => _('Coupe Homme, Taille') . ' M', 3 => _('Coupe Homme, Taille') . ' L', @@ -378,6 +381,78 @@ class Admin extends Controller public function counterparts_dashboard($f3, $args) { $db = $f3->get('DB'); + $f3->set('counterparts_import', ''); + $total = array("hoopie" => 0, "pishirt" => 0, "pibag" => 0, "piplome" => 0); + + + if ($f3->get('VERB') == 'POST') { + $separator = ';'; + // Si on a un POST sur cette page, c'est que l'on a un csv à parser + if ($_FILES['file']['tmp_name'] == '') { + $f3->push('SESSION.error', 'Veuillez uploader un fichier au format csv'); + } else { + // Lecture du fichier + if (($handle = fopen($_FILES['file']['tmp_name'], "r")) !== false) { + // D'abord le header : email / quoi + $fields = fgetcsv($handle, 1000, $separator); + $mail_idx = -1; + $quoi_idx = -1; + foreach ($fields as $key => $value) { + if ($value == "Quoi") { + $quoi_idx = $key; + } + if ($value == "Mail") { + $mail_idx = $key; + } + } + + // On tourne sur le fichier maintenant + while (($data = fgetcsv($handle, 1000, $separator)) !== false) { + // Récupération des données + $mail = $data[$mail_idx]; + $quoi = $data[$quoi_idx]; + + // On cherche l'utilisateur + $user = $f3->get('container')['user_finder']->findByEmail($mail); + // Et son addresse + $adresse = $f3->get('container')['address_finder']->findByUserId($user['id']); + + if (count($user) >= 1 and is_array($user)) { + // On a un utilisateur, cool + // On crée une contrepartie, si son cumul est suffisant + switch ($quoi) { + case 'hoodie': + $needed = 250; + break; + case 'pishirt': + $needed = 100; + break; + case 'pibag': + $needed = 50; + break; + case 'piplome': + $needed = 30; + break; + } + 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 mets à jour le cumul de l'utilisateur + $f3->get('container')['command_handler']->handle(new UserUpdateCumulCommand($user['id'], $user['cumul'] - $needed)); + $total[$quoi] +=1; + } else { + $f3->push('SESSION.error', 'Pas assez de cumul pour un '.$quoi.' avec l\'email'.$mail); + } + } else { + $f3->push('SESSION.error', 'Pas d\'utilisateur avec l\'adresse '.$mail); + } + } + fclose($handle); + } + } // Fin de l'import CSV + $f3->set('counterparts_import', $total); + } + // On définit des valeurs par défaut $query = "SELECT DISTINCT quoi FROM contreparties;"; $result = $db->query($query); diff --git a/app/routes.ini b/app/routes.ini index 7c235112e4f5ab58ac2353165089793ad7ae96d0..b774eb111176d64e2aab63a8e8c13a423e2228d0 100644 --- a/app/routes.ini +++ b/app/routes.ini @@ -29,7 +29,7 @@ GET @edit_support:/admin/support/edit/@id=Controller\Admin->support POST @modify_support:/admin/support/edit=Controller\Admin->support GET @invalidate_support:/admin/support/invalidate/@id=Controller\Admin->invalidate GET @validate_support:/admin/support/validate/@id=Controller\Admin->validate -GET @recompute:/admin/recompute=Controlle\Admin->recompute +GET @recompute:/admin/recompute=Controller\Admin->recompute GET|POST @admin_accounts:/admin/accounts=Controller\Admin->accounts @@ -47,7 +47,7 @@ POST @modify_user:/admin/users/@id=Controller\Admin->user POST @adresse:/admin/adresses=Controller\Admin->adresse -GET @counterparts_dashboard:/admin/counterparts_dashboard=Controller\Admin->counterparts_dashboard +GET|POST @counterparts_dashboard:/admin/counterparts_dashboard=Controller\Admin->counterparts_dashboard GET|POST @banque:/admin/banque=Controller\Admin->banque diff --git a/app/view/backend/contreparties_tableau.html b/app/view/backend/contreparties_tableau.html index 80e624bfdc62e8ea05f683718e74eb96bc168d06..8f71355a1fd90dfba7e78e35a02007ed37ea1cbf 100644 --- a/app/view/backend/contreparties_tableau.html +++ b/app/view/backend/contreparties_tableau.html @@ -1,4 +1,16 @@
+

Import des contreparties depuis un fichier csv

+

Veuillez préparer un fichier csv du format : Mail;quoi;

+
+ + +
+ +
+ {{ var_dump(@counterparts_import) }} + + +

Tableau de bord des contreparties

@@ -19,8 +31,8 @@ - - + +
Hoodies{{ @hoodie_s1 }}{{ @hoodie_s2 }}{{ @hoopie_s1 }}{{ @hoopie_s2 }}
diff --git a/db/seeds/CounterpartSeeder.php b/db/seeds/CounterpartSeeder.php index 9dced14093cd7d878a8de7446290130d2e0ac932..7c16e00543413b9e54764b000e406558b26b5928 100644 --- a/db/seeds/CounterpartSeeder.php +++ b/db/seeds/CounterpartSeeder.php @@ -33,6 +33,24 @@ class CounterpartSeeder extends AbstractSeed 'status' => 2, 'adresse_id' => null, ), + array( + 'id' => 3, + 'datec' => '2016-06-22 12:34', + 'user_id' => 2, + 'quoi' => 'pibag', // [piplome|pibag|pishirt|hoodie] + 'taille' => 2, + 'status' => 2, + 'adresse_id' => null, + ), + array( + 'id' => 4, + 'datec' => '2016-06-22 12:34', + 'user_id' => 2, + 'quoi' => 'hoopie', // [piplome|pibag|pishirt|hoodie] + 'taille' => 2, + 'status' => 2, + 'adresse_id' => null, + ), ); $this->table('contreparties')->insert($data)->save(); diff --git a/tests/functional/Finder/CounterpartFinderTest.php b/tests/functional/Finder/CounterpartFinderTest.php index 2eec470acfccbb66d06a9bfef9d6c73c4877af21..00a82e24aceae4a8f1e55e9ec42a4e4b1cdde0c3 100644 --- a/tests/functional/Finder/CounterpartFinderTest.php +++ b/tests/functional/Finder/CounterpartFinderTest.php @@ -9,7 +9,7 @@ class CounterpartFinderTest extends FunctionalTest public function testFindByUserId() { $counterparts = $this->container['counterpart_finder']->findByUserId(2); - $this->assertCount(1, $counterparts); + $this->assertCount(3, $counterparts); $firstCounterpart = reset($counterparts); diff --git a/tests/functional/Handler/CounterpartHandlerTest.php b/tests/functional/Handler/CounterpartHandlerTest.php index ecce1a68266e9bba61bcb9f6719752f8a9d25324..0aefcd5709d7e310bbb46d84df74c3f323cc2ea9 100644 --- a/tests/functional/Handler/CounterpartHandlerTest.php +++ b/tests/functional/Handler/CounterpartHandlerTest.php @@ -19,7 +19,7 @@ class CounterpartHandlerTest extends FunctionalTest public function testCounterpartCreate() { - $this->assertFalse($this->counterpartExists(3)); + $this->assertFalse($this->counterpartExists(5)); $this->container['command_handler']->handle(new CounterpartCreateCommand(1, 1, 'pishirt', 4, 1, date("Y-m-d H:i:s"), '')); }