]> git.agnieray.net Git - galette.git/blobdiff - galette/lib/Galette/Controllers/Crud/ContributionsController.php
Disable events from mass changes; closes #1733
[galette.git] / galette / lib / Galette / Controllers / Crud / ContributionsController.php
index 7f72d6a8ded71ccfa1fe2a22b1a529ea40635982..ebee1834eb064bbc4f0be95c21d57fe0960c5c1b 100644 (file)
@@ -37,8 +37,6 @@
 namespace Galette\Controllers\Crud;
 
 use Galette\Features\BatchList;
-use Galette\Filters\ContributionsList;
-use Throwable;
 use Analog\Analog;
 use Galette\Controllers\CrudController;
 use Slim\Psr7\Request;
@@ -198,7 +196,7 @@ class ContributionsController extends CrudController
             $contrib = new Contribution(
                 $this->zdb,
                 $this->login,
-                (count($cparams) > 0 ? $cparams : null)
+                $cparams
             );
 
             if (isset($cparams['adh'])) {
@@ -274,19 +272,23 @@ class ContributionsController extends CrudController
     {
         $post = $request->getParsedBody();
         $filters = $this->session->filter_members;
-        $contribution = new Contribution($this->zdb, $this->login);
-
         $type = $post['type'];
+
+        $ct = new ContributionsTypes($this->zdb);
+        $contributions_types = $ct->getList($type === Contribution::TYPE_FEE);
+
+        $contribution = new Contribution(
+            $this->zdb,
+            $this->login,
+            ['type' => array_keys($contributions_types)[0]]
+        );
+
         $data = [
             'id'            => $filters->selected,
             'redirect_uri'  => $this->routeparser->urlFor('members'),
             'type'          => $type
         ];
 
-        // contribution types
-        $ct = new ContributionsTypes($this->zdb);
-        $contributions_types = $ct->getList($type === Contribution::TYPE_FEE);
-
         // display page
         $this->view->render(
             $response,
@@ -335,6 +337,7 @@ class ContributionsController extends CrudController
         foreach ($members_ids as $member_id) {
             $post[Adherent::PK] = (int)$member_id;
             $contrib = new Contribution($this->zdb, $this->login);
+            $contrib->disableEvents();
 
             // regular fields
             $valid = $contrib->check($post, $contrib->getRequired(), $disabled);
@@ -397,14 +400,6 @@ class ContributionsController extends CrudController
         $ajax = false;
         $get = $request->getQueryParams();
 
-        if (
-            ($request->getHeaderLine('X-Requested-With') === 'XMLHttpRequest')
-            || isset($get['ajax'])
-            && $get['ajax'] == 'true'
-        ) {
-            $ajax = true;
-        }
-
         switch ($type) {
             case 'transactions':
                 $raw_type = 'transactions';
@@ -426,6 +421,14 @@ class ContributionsController extends CrudController
         }
 
         $filter_name = 'filter_' . $raw_type;
+        if (
+            ($request->getHeaderLine('X-Requested-With') === 'XMLHttpRequest')
+            || isset($get['ajax'])
+            && $get['ajax'] == 'true'
+        ) {
+            $ajax = true;
+            $filter_name .= '_ajax';
+        }
 
         if (isset($this->session->$filter_name)) {
             $filters = $this->session->$filter_name;
@@ -440,7 +443,6 @@ class ContributionsController extends CrudController
         }
 
         if ($type === 'contributions') {
-            $filters->filtre_transactions = false;
             if (isset($request->getQueryParams()['max_amount'])) {
                 $filters->filtre_transactions = true;
                 $filters->max_amount = (int)$request->getQueryParams()['max_amount'];
@@ -477,7 +479,7 @@ class ContributionsController extends CrudController
                 );
                 if (
                     !$member->hasParent() ||
-                    $member->hasParent() && $member->parent->id != $this->login->id
+                    $member->parent->id != $this->login->id
                 ) {
                     $value = $this->login->id;
                     Analog::log(
@@ -530,6 +532,7 @@ class ContributionsController extends CrudController
 
         if ($filters->filtre_cotis_adh != null) {
             $member = new Adherent($this->zdb);
+            $member->enableDep('children');
             $member->load($filters->filtre_cotis_adh);
             $tpl_vars['member'] = $member;
         }
@@ -597,7 +600,13 @@ class ContributionsController extends CrudController
      */
     public function filter(Request $request, Response $response, string $type = null): Response
     {
+        $ajax = false;
         $filter_name = 'filter_' . $type;
+        if ($request->getHeaderLine('X-Requested-With') === 'XMLHttpRequest') {
+            $ajax = true;
+            $filter_name .= '_ajax';
+        }
+
         $post = $request->getParsedBody();
         $error_detected = [];
 
@@ -609,9 +618,9 @@ class ContributionsController extends CrudController
         }
 
         if (isset($post['clear_filter'])) {
-            $filters->reinit();
+            $filters->reinit($ajax);
         } else {
-            if (isset($post['max_amount'])) {
+            if (!isset($post['max_amount'])) {
                 $filters->max_amount = null;
             }
 
@@ -626,15 +635,11 @@ class ContributionsController extends CrudController
             }
 
             if (isset($post['end_date_filter']) || isset($post['start_date_filter'])) {
-                try {
-                    if (isset($post['start_date_filter'])) {
-                        $filters->start_date_filter = $post['start_date_filter'];
-                    }
-                    if (isset($post['end_date_filter'])) {
-                        $filters->end_date_filter = $post['end_date_filter'];
-                    }
-                } catch (Throwable $e) {
-                    $error_detected[] = $e->getMessage();
+                if (isset($post['start_date_filter'])) {
+                    $filters->start_date_filter = $post['start_date_filter'];
+                }
+                if (isset($post['end_date_filter'])) {
+                    $filters->end_date_filter = $post['end_date_filter'];
                 }
             }
 
@@ -688,7 +693,8 @@ class ContributionsController extends CrudController
         $post = $request->getParsedBody();
 
         if (isset($post['entries_sel'])) {
-            $filters = $this->session->$filter_name ?? new ContributionsList();
+            $filter_class = '\\Galette\\Filters\\' . ucwords($type . 'List');
+            $filters = $this->session->$filter_name ?? new $filter_class();
             $filters->selected = $post['entries_sel'];
             $this->session->$filter_name = $filters;
 
@@ -788,16 +794,16 @@ class ContributionsController extends CrudController
     public function store(Request $request, Response $response, $action, string $type, $id = null): Response
     {
         $post = $request->getParsedBody();
-        $args = [
+        $url_args = [
             'action'    => $action,
             'type'      => $type
         ];
         if ($id !== null) {
-            $args['id'] = $id;
+            $url_args['id'] = $id;
         }
 
         if ($action == 'edit' && isset($post['btnreload'])) {
-            $redirect_url = $this->routeparser->urlFor($action . 'Contribution', $args);
+            $redirect_url = $this->routeparser->urlFor($action . 'Contribution', $url_args);
             $redirect_url .= '?' . Adherent::PK . '=' . $post[Adherent::PK] . '&' .
                 ContributionsTypes::PK . '=' . $post[ContributionsTypes::PK] . '&' .
                 'montant_cotis=' . $post['montant_cotis'];
@@ -860,7 +866,7 @@ class ContributionsController extends CrudController
                 $redirect_url = $this->routeparser->urlFor(
                     'addContribution',
                     [
-                        'type'      => $type
+                        'type'      => $post['contrib_type'] ?? $type
                     ]
                 ) . '?' . Transaction::PK . '=' . $contrib->transaction->id .
                 '&' . Adherent::PK . '=' . $contrib->member;
@@ -877,7 +883,7 @@ class ContributionsController extends CrudController
             //something went wrong.
             //store entity in session
             $this->session->contribution = $contrib;
-            $redirect_url = $this->routeparser->urlFor($action . 'Contribution', $args);
+            $redirect_url = $this->routeparser->urlFor($action . 'Contribution', $url_args);
 
             //report errors
             foreach ($error_detected as $error) {