Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //comprova que s'executi desde una associacio
- //$idAsso = getIdAssociacioByApiKey($apiKey);
- if (isset($idBotiga)) {
- $idBotiga = $idBotiga;
- } else {
- $idBotiga = "";
- }
- $idBotiga = escapa($idBotiga);
- if (isset($idAssociacio)) {
- $idAssociacio = $idAssociacio;
- } else {
- $idAssociacio = "";
- }
- $idAssociacio = escapa($idAssociacio);
- if (($idBotiga == '') && ($idAssociacio == '')) {
- $arr["cfg"]["error"] = $i18n->_('Falta especificar un valor en la variable') . ' \'idBotiga\' o \'idAssociacio\'';
- $arr["cfg"]["errorNum"] = $numApi . '001';
- if (modeDevelopment())
- $arr["cfg"]["debugTrace"] = "" . __FILE__ . ":" . __LINE__ . "";
- exit(printData($arr));
- }
- if (isset($cercador)) {
- $cercador = $cercador;
- } else {
- $cercador = "";
- }
- if (isset($excel)) {
- $excel = $excel;
- } else {
- $excel = "";
- }
- if (isset($idFiltre)) {
- $idFiltre = $idFiltre;
- } else {
- $idFiltre = "";
- }
- if ($idFiltre == "") {
- $arr["cfg"]["error"] = $i18n->_('Falta especificar un valor en la variable') . ' \'idFiltre\'';
- $arr["cfg"]["errorNum"] = $numApi . '002';
- if (modeDevelopment())
- $arr["cfg"]["debugTrace"] = "" . __FILE__ . ":" . __LINE__ . "";
- exit(printData($arr));
- }
- $idFiltre = escapa($idFiltre);
- if ($idAssociacio != '') {
- $sqlAux = " AND idAssociacio = $idAssociacio ";
- $sqlAux2 = " cb.idAssociacio = '$idAssociacio' ";
- }
- if ($idBotiga != "") {
- $sqlAux = " AND idBotiga = $idBotiga";
- $sqlAux2 = " cb.idBotiga = '$idBotiga'";
- }
- $query = " SELECT * FROM filtres_com WHERE idFiltre = $idFiltre $sqlAux";
- $result = $connexio_mysqli->query($query);
- if ($result->num_rows == 0) {
- $arr["cfg"]["error"] = $i18n->_("No s'ha trobat el filtre");
- $arr["cfg"]["errorNum"] = $numApi . '002';
- if (modeDevelopment())
- $arr["cfg"]["debugTrace"] = "" . __FILE__ . ":" . __LINE__ . "";
- exit(printData($arr));
- }
- $filtre = $result->fetch_array();
- if ($debug)
- printr2($filtre);
- $sqlBot = '';
- if (($idBotiga != '') && ($idBotiga != 0)) {
- $sqlBot = " WHERE cb.idBotiga = '$idBotiga' ";
- }
- if (($idAssociacio != '') && ($idAssociacio != 0)) {
- $sqlBot = " WHERE c.idAssociacio = '$idAssociacio' ";
- }
- if ($sqlBot != '') {
- $sqlAux1 = ' AND ';
- } else {
- $sqlAux1 = '';
- }
- if ($filtre['nom'] != '') {
- $sqlAux1 .= " CONCAT( c.nom, ' ', c.cognom1, ' ', c.cognom2 ) LIKE '%$filtre[nom]%' AND \n";
- }
- if ($filtre['sexe'] != '') {
- $sqlAux1 .= " c.sexe = '$filtre[sexe]' AND \n";
- }
- if ($filtre['diaAniversari'] != '') {
- $sqlAux1 .= " c.data_naixament LIKE '%-$filtre[diaAniversari]' AND \n";
- }
- if ($filtre['mesAniversari'] != '') {
- $sqlAux1 .= " c.data_naixament LIKE '%-$filtre[mesAniversari]-%' AND \n";
- }
- if (($filtre['edatMin'] != 0) && ($filtre['edatMin'] != '')) {
- //buscar any actual i restali la edat minima aixi
- $dataMin = (date('Y') - $filtre['edatMin']) . '-' . date('m-d');
- $sqlAux1 .= " c.data_naixament <= '$dataMin' AND \n";
- }
- if (($filtre['edatMax'] != 0) && ($filtre['edatMax'] != '') && ($filtre['edatMax'] != 120)) {
- //buscar any actual i restali la edat minima aixi
- $dataMin = (date('Y') - $filtre['edatMax']) . '-' . date('m-d');
- $sqlAux1 .= " c.data_naixament >= '$dataMin' AND \n";
- }
- if ($filtre['registratAbans'] != '') {
- $sqlAux1 .= " c.tempsEstampat <= '$filtre[registratAbans]' AND \n";
- }
- if ($filtre['registratDespres'] != '') {
- $sqlAux1 .= " c.tempsEstampat >= '$filtre[registratDespres]' AND \n";
- }
- if ($filtre['cp'] != '') {
- $sqlAux1 .= " c.cp = '$filtre[cp]' AND \n";
- }
- if ($filtre['correu'] != '') {
- if ($filtre['correu'] == 0) {
- //no tenen correu
- $sqlAux1 .= " c.email = '' AND \n";
- } else {
- //tenen correu
- $sqlAux1 .= " c.email != '' AND \n";
- }
- }
- $join = '';
- if (($filtre['operacio'] != '') || ($filtre['hanComprat'] != '') || ($filtre['noHanComprat'] != '')) {
- if (($filtre['hanComprat'] != '') || ($filtre['operacio'] != '')) {
- $join = "INNER JOIN ultima_operacio uo ON uo.idClient = c.idClient ";
- }
- if ($filtre['noHanComprat'] != '') {
- if ($idAssociacio != '') {
- $join = " LEFT JOIN (SELECT idClient, idBotiga, idAssociacio";
- if($filtre['operacio'] != ''){
- $join .= ", operacio ";
- }
- $join .= " , MAX(data) as data FROM ultima_operacio WHERE idAssociacio = '$idAssociacio' ";
- if($filtre['operacio'] != ''){
- $join .= " AND operacio = '$filtre[operacio]' ";
- }
- $join .=" GROUP BY idClient, idAssociacio";
- if($filtre['operacio'] != ''){
- $join .= ",operacio";
- }
- $join .=") as uo ON uo.idClient = c.idClient";
- } else {
- $join = "INNER JOIN ultima_operacio uo ON uo.idClient = c.idClient ";
- }
- }
- }
- $sqlAux2 = '';
- if ($filtre['operacio'] != '') {
- $sqlAux2 .= " uo.operacio = '$filtre[operacio]' AND \n";
- }
- if ($filtre['hanComprat'] != '') {
- $sqlAux2 .= " uo.data >= '$filtre[hanComprat]' AND \n";
- }
- if ($filtre['noHanComprat'] != '') {
- $sqlAux2 .= "(uo.data <= '$filtre[noHanComprat]' OR uo.data is null ) AND \n";
- }
- if ($idBotiga != '') {
- $campCom = 'idBotiga';
- }
- if ($idAssociacio != '') {
- $campCom = 'idAssociacio';
- }
- $join3= '';
- if ($filtre['comunicacio'] != '') {
- $join3 = " LEFT JOIN (SELECT * FROM baixes_com WHERE $campCom = ".$$campCom.") as bc ON bc.idClient = c.idClient ";
- if ($filtre['comunicacio'] == 1) {
- //volen rebre comunicacio
- $sqlAux2 .= " bc.idBaixa IS NULL AND \n";
- } else {
- //no volen rebre comunicacio
- $sqlAux2 .= " bc.idBaixa IS NOT NULL AND \n";
- }
- }
- if(($filtre['sector'] != '')&&($filtre['sector'] != 0)){
- $sqlAux2 .= " cbs.sector = '$filtre[sector]' AND \n";
- }
- if ($sqlAux2 != '') {
- $sqlAux2 = substr($sqlAux2, 0, -5);
- }
- if ($sqlAux != '') {
- $sqlAux1 = substr($sqlAux1, 0, -5);
- }
- if (isset($start)) {
- $start = $start;
- } else {
- $start = 0;
- }
- if (isset($pagina)) {
- $sqlPag = "LIMIT $start , 25";
- } else {
- $sqlPag = '';
- }
- $and = '';
- if ((($sqlAux1 != '') && ($sqlAux2 != '')) || (($sqlBot != '') && ($sqlAux2 != ''))) {
- $and = ' AND ';
- }
- $sqlCercador = '';
- if ($cercador != '') {
- if (($sqlBot != '') && ($sqlAux1 != '') && ($sqlAux2 != '')) {
- if (($sqlBot != '') && ($sqlAux1 == '') && ($sqlAux2 == '')) {
- $sqlCercador .= ' AND ';
- }
- } else {
- $sqlCercador .= ' AND ';
- }
- $sqlCercador .= " (
- CONCAT( c.nom, ' ', c.cognom1, ' ', c.cognom2 ) LIKE '%$cercador%'
- OR t.tokenCurt LIKE '%$cercador%'
- OR c.dni LIKE '%$cercador%'
- OR c.tel_fix LIKE '%$cercador%'
- OR c.email LIKE '%$cercador%'
- OR g.name LIKE '%$cercador%'
- ) ";
- }
- $sqlAux3 = '';
- if (($idBotiga != 0) && ($idBotiga != '')) {
- $sqlAux3 = " LEFT JOIN clients_botigues cb
- ON cb.idClient = c.idClient";
- }
- $join2 = '';
- if(($filtre['sector'] != '')&&($filtre['sector'] != '0') &&($idAssociacio != '')){
- $join2 = " LEFT JOIN (SELECT cb.idClient, b.sector FROM clients_botigues cb LEFT JOIN botigues b ON b.idBotiga = cb.idBotiga WHERE cb.idAssociacio = '$idAssociacio' AND b.sector = '$filtre[sector]') as cbs ON cbs.idClient = c.idClient ";
- }
- //
- //$sqlAux4 = '';
- //if(($idAssociacio != '') &&($idAssociacio != 0)){
- // $sqlAux4 = " c.idAssociacio = $idAssociacio "
- //}
- //consulta cache
- $cache = Cache::get($test . date('Y_m_d_h') . "_filtre_$idFiltre");
- if ($debug)
- var_dump($cache);
- if (!$cache) {
- $query = "SELECT
- c.idClient
- FROM clients c
- LEFT JOIN geo_name g
- ON g.geonameid = c.poblacio
- $sqlAux3
- LEFT JOIN targetes t ON t.idTargeta = c.idTargeta
- $join3
- $join
- $join2
- $sqlBot
- $sqlAux1
- $and
- $sqlAux2
- GROUP BY c.idClient";
- if ($debug)
- printr2($query);
- $ress = $connexio_mysqli->query($query);
- $cache = $ress->num_rows;
- Cache::put($test . date('Y_m_d') . "_filtre_$idFiltre", $cache);
- }
- $total = $cache;
- if ($debug)
- printr2("TOTAL:: $total");
- $query = "SELECT
- c.idClient,
- CONCAT( c.nom, ' ', c.cognom1, ' ', c.cognom2 ) AS nomClient,
- c.email,
- c.tel_fix,
- g.name AS poblacio,
- c.tempsEstampat,
- c.usuari,
- t.tokenCurt,
- c.tempsEstampat
- FROM clients c
- LEFT JOIN geo_name g
- ON g.geonameid = c.poblacio
- $sqlAux3
- LEFT JOIN targetes t ON t.idTargeta = c.idTargeta
- $join3
- $join
- $join2
- $sqlBot
- $sqlAux1
- $and
- $sqlAux2
- $sqlCercador
- GROUP BY c.idClient
- $sqlPag
- ";
- if ($debug)
- printr2($query);
- $result = $connexio_mysqli->query($query);
- if ($result->num_rows == 0) {
- $arr["cfg"]["error"] = $i18n->_("No hi ha clients que passin el filtre");
- $arr["cfg"]["errorNum"] = $numApi . '002';
- if (modeDevelopment())
- $arr["cfg"]["debugTrace"] = "" . __FILE__ . ":" . __LINE__ . "";
- exit(printData($arr));
- }
- while ($row = $result->fetch_array()) {
- $clients[$row['idClient']] = $row;
- }
- $arr['cfg']['data']['total'] = $total;
- $arr['cfg']['data']['mails'] = $clients;
- $arr['cfg']['getClientsFromFiltre'] = true;
- $arr["cfg"]["error"] = "false";
- if (modeDevelopment())
- $arr["cfg"]["debugTrace"] = "" . __FILE__ . ":" . __LINE__ . "";
- exit(printData($arr));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement