APYDataGridBundle icon indicating copy to clipboard operation
APYDataGridBundle copied to clipboard

get export with external filters

Open charfeddine-mahmoud opened this issue 7 years ago • 7 comments

Hello, I made a form that allows me to filter my data to display in the grid (a date field and 3 drop-down list) so I have the ability to do multiple sorting, My filters work well for display in the grid but when i try to do an export I do not have the right data Thank you in advance for your returns In my controller I have the following code :

            $agent = null;
            $agence = null;
            $date = null;
            $societe = null;
            $state = "terminer";
            $modePaiement = null;
            if ('POST' == $request->getMethod()) {
                if ($request->request->get('agent')) {
                    $agent = $em->getRepository('AppBundle:User')->find($request->request->get('agent'));
                }
                if ($request->request->get('agence')) {
                    $agence = $em->getRepository('AppBundle:Agence')->find($request->request->get('agence'));
                }
                if ($request->request->get('date')) {
                    $date = date_create_from_format('d/m/Y', $request->request->get('date'));
                }
                if ($request->request->get('modePaiement')) {
                    $modePaiement = $request->request->get('modePaiement');
                }
            }
            $abonnements = $em->getRepository('AppBundle:Abonnement')->findAbonnementsByCriteria($agent, $agence, $date, $societe, $state, $modePaiement);
            foreach ($abonnements as $abonnement) {
                if ($abonnement->getSalarie()->getSociete()->getPaiement() == 1){
                    $modePaiementText = "Facturé à l’employeur";
                } else {
                    if ($abonnement->getModePayement() == 0) {
                    $modePaiementText = "Prélèvement automatique";
                    } else if ($abonnement->getModePayement() == 1) {
                        $modePaiementText = "Comptant";
                    }
                }
                $data[] = array(
                    'id' => $abonnement->getId(),
                    'nom_salarie' => $abonnement->getSalarie()->getUser()->getNom(),
                    'prenom_salarie' => $abonnement->getSalarie()->getUser()->getPrenom(),
                    'num_client' => $abonnement->getSalarie()->getNumClient(),
                    'modePaiement' => $modePaiementText,
                    'typeContrat' => $abonnement->getTarif()->getNom(),
                    'agence' => (!empty($abonnement->getAgence())) ? $abonnement->getAgence()->getNom() : "",
                    'nomCompletAgent' => $abonnement->getAgent()->getPrenom()." ".$abonnement->getAgent()->getNom(),
                    'date_validation' => $abonnement->getDateAgent(),
                    'matricule' => $abonnement->getSalarie()->getSociete()->getNumMatricule(),
                    'societe' => $abonnement->getSalarie()->getSociete()->getNom()
                );
            }

            // Les colonnes de notre grid avec les parametres
            $columns = array(
            new Column\NumberColumn(array(
                    'id' => 'id',
                    'field' => 'id', 
                    'source' => true, 
                    'primary' => true, 
                    'title' => 'Réf',
                    'operatorsVisible' => false
            )),
            new Column\TextColumn(array(
                    'id' => 'nom_salarie', 
                    'field' => 'nom_salarie', 
                    'source' => true, 
                    'title' => 'Nom',
                    // 'align' => 'right',
                    'operatorsVisible' => false
            )),
            new Column\TextColumn(array(
                    'id' => 'prenom_salarie', 
                    'field' => 'prenom_salarie', 
                    'source' => true, 
                    'title' => 'Prénom',
                    // 'align' => 'right',
                    'operatorsVisible' => false
            )),
            new Column\TextColumn(array(
                    'id' => 'num_client', 
                    'field' => 'num_client', 
                    'source' => true, 
                    'title' => 'Num client',
                    'operatorsVisible' => false
            )),
            new Column\TextColumn(array(
                    'id' => 'modePaiement', 
                    'field' => 'modePaiement', 
                    'source' => true, 
                    'title' => 'Paiement',
                    // 'align' => 'right',
                    'operatorsVisible' => false
            )),
            new Column\TextColumn(array(
                    'id' => 'typeContrat', 
                    'field' => 'typeContrat', 
                    'source' => true, 
                    'title' => 'Contrat',
                    // 'align' => 'right',
                    'operatorsVisible' => false
            )),
            new Column\TextColumn(array(
                    'id' => 'agence', 
                    'field' => 'agence', 
                    'source' => true, 
                    'title' => 'Agence',
                    // 'align' => 'right',
                    'operatorsVisible' => false
            )),
            new Column\TextColumn(array(
                    'id' => 'nomCompletAgent', 
                    'field' => 'nomCompletAgent',
                    'source' => true, 
                    'title' => 'Agent',
                    // 'align' => 'right',
                    'operatorsVisible' => false
            )),
            new Column\DateColumn(array(
                    'id' => 'date_validation', 
                    'field' => 'date_validation', 
                    'source' => true, 
                    'title' => 'Date validation (agence)', 
                    'format' => 'd-m-Y',
                    'operatorsVisible' => false
            )),
            new Column\TextColumn(array(
                    'id' => 'matricule',
                    'field' => 'matricule', 
                    'source' => true, 
                    'title' => 'N matricule',
                    'operatorsVisible' => false
            )),
            new Column\TextColumn(array(
                    'id' => 'societe',
                    'field' => 'societe', 
                    'source' => true, 
                    'title' => 'Société',
                    'operatorsVisible' => false
            )),
        );

        $source = new Vector($data, $columns);
        $source->setId('id');
        // Récupération du service Grid
        $grid = $this->get('grid');
        $grid->setSource($source);
        $grid->getColumn('id')->setFilterable(false);
        $grid->getColumn('societe')->setFilterable(false);
        $grid->getColumn('date_validation')->setFilterable(false);
        $grid->getColumn('nomCompletAgent')->setFilterable(false);
        $grid->getColumn('agence')->setFilterable(false);
        $grid->getColumn('modePaiement')->setFilterable(false);
        $grid->getColumn('matricule')->setFilterable(false);
        $export = new ExcelExport('Exporter au format Excel');
        $export97 = new PHPExcel5Export('Exporter au format Excel 5 (97-2003)');
        $export2003 = new PHPExcel2003Export('Exporter au format Excel 2003');
        $export2007 = new PHPExcel2007Export('Exporter au format Excel 2007');
        $grid->addExport($export);
        $grid->addExport($export97);
        $grid->addExport($export2003);
        $grid->addExport($export2007);
        return $grid->getGridResponse('agent/etatVente.html.twig',array(
            'user' => $user,
            'agentSelected' => $request->request->get('agent'),
            'agenceSelected' => $request->request->get('agence'),
            'modePaiementSelected' => $request->request->get('modePaiement'),
            'dateSelected' => $request->request->get('date'),
            'agences' => $agences,
            'user' => $user,
            'agents' => $agents,
            'typeDevice' => $typeDevice
        ));

2 1

charfeddine-mahmoud avatar Jun 20 '17 14:06 charfeddine-mahmoud

Please, format your code in a proper way. Thank you.

DonCallisto avatar Jun 20 '17 14:06 DonCallisto

What do you mean with " I do not have the right data", which data do you retrieve? What do you expected instead? @charfeddine-mahmoud

AlessandroMinoccheri avatar Jun 20 '17 15:06 AlessandroMinoccheri

In the excel file I have all the data as if there are no filters, I just added two images to show 1 2

charfeddine-mahmoud avatar Jun 20 '17 15:06 charfeddine-mahmoud

I do not understand because every time the query is played, that's what builds the source for grid or export, why for the grid it works and not for export?

charfeddine-mahmoud avatar Jun 20 '17 15:06 charfeddine-mahmoud

If you print $data before you return the response what do you retrieve?

AlessandroMinoccheri avatar Jun 20 '17 19:06 AlessandroMinoccheri

If i print $data before i return the response i retrieve only one line, This means that it is good for the grid, but the export will have 3 lines which are that for first query without filters :

Array ( [0] => Array ( [id] => 1 [nom_salarie] => charfeddine [prenom_salarie] => mahmoud [num_client] => 111111 [modePaiement] => Facturé à l’employeur [typeContrat] => PASS SALARIE [agence] => Grand place [nomCompletAgent] => agent agent [date_validation] => DateTime Object ( [date] => 2017-06-15 12:38:38.000000 [timezone_type] => 3 [timezone] => UTC ) [matricule] => 31222 [societe] => Alteca ) )

What I understood is that the grid is initialized by the first query that keeps for export

charfeddine-mahmoud avatar Jun 21 '17 06:06 charfeddine-mahmoud

@charfeddine-mahmoud could you replicate this issue in a separate repository so I can check? I can't reproduce it.

DonCallisto avatar Jun 29 '17 08:06 DonCallisto