dolibarr  13.0.2
ziptown.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2010 Regis Houssin <regis.houssin@inodbox.com>
3  * Copyright (C) 2011-2014 Laurent Destailleur <eldy@users.sourceforge.net>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <https://www.gnu.org/licenses/>.
17  */
18 
25 if (!defined('NOTOKENRENEWAL')) define('NOTOKENRENEWAL', 1); // Disables token renewal
26 if (!defined('NOREQUIREMENU')) define('NOREQUIREMENU', '1');
27 if (!defined('NOREQUIREHTML')) define('NOREQUIREHTML', '1');
28 if (!defined('NOREQUIREAJAX')) define('NOREQUIREAJAX', '1');
29 if (!defined('NOREQUIRESOC')) define('NOREQUIRESOC', '1');
30 if (!defined('NOCSRFCHECK')) define('NOCSRFCHECK', '1');
31 
32 require '../../main.inc.php';
33 require_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php';
34 
35 
36 
37 /*
38  * View
39  */
40 
41 // Ajout directives pour resoudre bug IE
42 //header('Cache-Control: Public, must-revalidate');
43 //header('Pragma: public');
44 
45 //top_htmlhead("", "", 1); // Replaced with top_httphead. An ajax page does not need html header.
46 top_httphead();
47 
48 //print '<!-- Ajax page called with url '.dol_escape_htmltag($_SERVER["PHP_SELF"]).'?'.dol_escape_htmltag($_SERVER["QUERY_STRING"]).' -->'."\n";
49 
50 dol_syslog("GET is ".join(',', $_GET).', MAIN_USE_ZIPTOWN_DICTIONNARY='.(empty($conf->global->MAIN_USE_ZIPTOWN_DICTIONNARY) ? '' : $conf->global->MAIN_USE_ZIPTOWN_DICTIONNARY));
51 //var_dump($_GET);
52 
53 // Generation of list of zip-town
54 if (!empty($_GET['zipcode']) || !empty($_GET['town']))
55 {
56  $return_arr = array();
57  $formcompany = new FormCompany($db);
58 
59  // Define filter on text typed
60  $zipcode = $_GET['zipcode'] ? $_GET['zipcode'] : '';
61  $town = $_GET['town'] ? $_GET['town'] : '';
62 
63  if (!empty($conf->global->MAIN_USE_ZIPTOWN_DICTIONNARY)) // Use zip-town table
64  {
65  $sql = "SELECT z.rowid, z.zip, z.town, z.fk_county, z.fk_pays as fk_country";
66  $sql .= ", c.rowid as fk_country, c.code as country_code, c.label as country";
67  $sql .= ", d.rowid as fk_county, d.code_departement as county_code, d.nom as county";
68  $sql .= " FROM ".MAIN_DB_PREFIX."c_ziptown as z";
69  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_departements as d ON z.fk_county = d.rowid";
70  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_regions as r ON d.fk_region = r.code_region,";
71  $sql .= " ".MAIN_DB_PREFIX."c_country as c";
72  $sql .= " WHERE z.fk_pays = c.rowid";
73  $sql .= " AND z.active = 1 AND c.active = 1";
74  if ($zipcode) $sql .= " AND z.zip LIKE '".$db->escape($zipcode)."%'";
75  if ($town) $sql .= " AND z.town LIKE '%".$db->escape($town)."%'";
76  $sql .= " ORDER BY z.zip, z.town";
77  $sql .= $db->plimit(100); // Avoid pb with bad criteria
78  } else // Use table of third parties
79  {
80  $sql = "SELECT DISTINCT s.zip, s.town, s.fk_departement as fk_county, s.fk_pays as fk_country";
81  $sql .= ", c.code as country_code, c.label as country";
82  $sql .= ", d.code_departement as county_code , d.nom as county";
83  $sql .= " FROM ".MAIN_DB_PREFIX.'societe as s';
84  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_departements as d ON s.fk_departement = d.rowid";
85  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX.'c_country as c ON s.fk_pays = c.rowid';
86  $sql .= " WHERE";
87  if ($zipcode) $sql .= " s.zip LIKE '".$db->escape($zipcode)."%'";
88  if ($town) $sql .= " s.town LIKE '%".$db->escape($town)."%'";
89  $sql .= " ORDER BY s.fk_pays, s.zip, s.town";
90  $sql .= $db->plimit(100); // Avoid pb with bad criteria
91  }
92 
93  //print $sql;
94  $resql = $db->query($sql);
95  //var_dump($db);
96  if ($resql)
97  {
98  while ($row = $db->fetch_array($resql))
99  {
100  $country = $row['fk_country'] ? ($langs->transnoentitiesnoconv('Country'.$row['country_code']) != 'Country'.$row['country_code'] ? $langs->transnoentitiesnoconv('Country'.$row['country_code']) : $row['country']) : '';
101  $county = $row['fk_county'] ? ($langs->transnoentitiesnoconv($row['county_code']) != $row['county_code'] ? $langs->transnoentitiesnoconv($row['county_code']) : ($row['county'] != '-' ? $row['county'] : '')) : '';
102 
103  $row_array['label'] = $row['zip'].' '.$row['town'];
104  $row_array['label'] .= ($county || $country) ? ' (' : '';
105  $row_array['label'] .= $county;
106  $row_array['label'] .= ($county && $country ? ' - ' : '');
107  $row_array['label'] .= $country;
108  $row_array['label'] .= ($county || $country) ? ')' : '';
109  if ($zipcode)
110  {
111  $row_array['value'] = $row['zip'];
112  $row_array['town'] = $row['town'];
113  }
114  if ($town)
115  {
116  $row_array['value'] = $row['town'];
117  $row_array['zipcode'] = $row['zip'];
118  }
119  $row_array['selectcountry_id'] = $row['fk_country'];
120  $row_array['state_id'] = $row['fk_county'];
121 
122  // TODO Use a cache here to avoid to make select_state in each pass (this make a SQL and lot of logs)
123  $row_array['states'] = $formcompany->select_state('', $row['fk_country'], '');
124 
125  array_push($return_arr, $row_array);
126  }
127  }
128 
129  echo json_encode($return_arr);
130 } else {
131 }
132 
133 $db->close();
if(!defined('NOREQUIREMENU')) if(!function_exists("llxHeader")) top_httphead($contenttype= 'text/html', $forcenocache=0)
Show HTTP header.
Definition: main.inc.php:1214
Class to build HTML component for third parties management Only common components are here...
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1232