dolibarr  9.0.0
address.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2001-2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3  * Copyright (C) 2004-2006 Laurent Destailleur <eldy@users.sourceforge.net>
4  * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
26 require '../main.inc.php';
27 require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
28 require_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php';
29 require_once DOL_DOCUMENT_ROOT.'/societe/class/address.class.php';
30 
31 // Load translation files required by the page
32 $langs->loadLangs(array('companies', 'commercial'));
33 
34 $id = GETPOST('id','int');
35 $action = GETPOST('action','alpha');
36 $confirm = GETPOST('confirm','alpha');
37 $backtopage = GETPOST('backtopage','alpha');
38 $origin = GETPOST('origin','alpha');
39 $originid = GETPOST('originid','int');
40 $socid = GETPOST('socid','int');
41 if (! $socid && ($action != 'create' && $action != 'add' && $action != 'update')) accessforbidden();
42 
43 // Security check
44 if ($user->societe_id) $socid=$user->societe_id;
45 $result = restrictedArea($user, 'societe', $socid);
46 
47 $object = new Address($db);
48 
49 
50 /*
51  * Actions
52  */
53 
54 // Cancel
55 if (GETPOST('cancel','alpha') && ! empty($backtopage))
56 {
57  header("Location: ".$backtopage);
58  exit;
59 }
60 
61 if ($action == 'add' || $action == 'update')
62 {
63  $object->socid = $socid;
64  $object->label = ($_POST["label"]!=$langs->trans('RequiredField')?$_POST["label"]:'');
65  $object->name = ($_POST["name"]!=$langs->trans('RequiredField')?$_POST["name"]:'');
66  $object->address = $_POST["address"];
67  $object->zip = $_POST["zipcode"];
68  $object->town = $_POST["town"];
69  $object->country_id = $_POST["country_id"];
70  $object->phone = $_POST["phone"];
71  $object->fax = $_POST["fax"];
72  $object->note = $_POST["note"];
73 
74  // Add new address
75  if ($action == 'add')
76  {
77  $result = $object->create($socid, $user);
78 
79  if ($result >= 0)
80  {
81  if (! empty($backtopage))
82  {
83  header("Location: ".$backtopage);
84  exit;
85  }
86  else if ($origin == 'commande')
87  {
88  header("Location: ../commande/contact.php?action=editdelivery_adress&socid=".$socid."&id=".$originid);
89  exit;
90  }
91  elseif ($origin == 'propal')
92  {
93  header("Location: ../comm/propal/contact.php?action=editdelivery_adress&socid=".$socid."&id=".$originid);
94  exit;
95  }
96  elseif ($origin == 'shipment')
97  {
98  header("Location: ../expedition/card.php?id=".$originid);
99  exit;
100  }
101  else
102  {
103  header("Location: ".$_SERVER['PHP_SELF']."?socid=".$socid);
104  exit;
105  }
106  }
107  else
108  {
109  setEventMessages($object->error, $object->errors, 'errors');
110  $action='create';
111  }
112  }
113 
114  // Update address
115  else if ($action == 'update')
116  {
117  $result = $object->update($id, $socid, $user);
118 
119  if ($result >= 0)
120  {
121  if (! empty($backtopage))
122  {
123  header("Location: ".$backtopage);
124  exit;
125  }
126  else if ($origin == 'commande')
127  {
128  header("Location: ../commande/contact.php?id=".$originid);
129  exit;
130  }
131  elseif ($origin == 'propal')
132  {
133  header("Location: ../comm/propal/contact.php?id=".$originid);
134  exit;
135  }
136  elseif ($origin == 'shipment')
137  {
138  header("Location: ../expedition/card.php?id=".$originid);
139  exit;
140  }
141  else
142  {
143  header("Location: ".$_SERVER['PHP_SELF']."?socid=".$socid);
144  exit;
145  }
146  }
147  else
148  {
149  $reload = 0;
150  setEventMessages($object->error, $object->errors, 'errors');
151  $action= "edit";
152  }
153  }
154 }
155 
156 else if ($action == 'confirm_delete' && $confirm == 'yes' && $user->rights->societe->supprimer)
157 {
158  $result = $object->delete($id, $socid);
159 
160  if ($result == 0)
161  {
162  header("Location: ".$_SERVER['PHP_SELF']."?socid=".$socid);
163  exit ;
164  }
165  else
166  {
167  $reload = 0;
168  $action='';
169  }
170 }
171 
177 llxHeader();
178 
179 $form = new Form($db);
180 $formcompany = new FormCompany($db);
181 $countrynotdefined=$langs->trans("ErrorSetACountryFirst").' ('.$langs->trans("SeeAbove").')';
182 
183 if ($action == 'create')
184 {
185  if ($user->rights->societe->creer)
186  {
187  /*
188  * Creation
189  */
190 
191  if ($_POST["label"] && $_POST["name"])
192  {
193  $object->socid = $socid;
194  $object->label = $_POST["label"];
195  $object->name = $_POST["name"];
196  $object->address = $_POST["address"];
197  $object->zip = $_POST["zipcode"];
198  $object->town = $_POST["town"];
199  $object->phone = $_POST["phone"];
200  $object->fax = $_POST["fax"];
201  $object->note = $_POST["note"];
202  }
203 
204  // On positionne country_id, country_code and label of the chosen country
205  $object->country_id = (GETPOST('country_id','int') ? GETPOST('country_id','int') : $mysoc->country_id);
206  if ($object->country_id)
207  {
208  $tmparray=getCountry($object->country_id,'all');
209  $object->country_code = $tmparray['code'];
210  $object->country = $tmparray['label'];
211  }
212 
213  print load_fiche_titre($langs->trans("AddAddress"));
214 
215  print "<br>\n";
216 
217  // If javascript enabled, we add interactivity on mandatory fields
218  if ($conf->use_javascript_ajax)
219  {
220  print "\n".'<script type="text/javascript" language="javascript">';
221  print '$(document).ready(function () {
222  $("#label").focus(function() {
223  hideMessage("label","'.$langs->trans('RequiredField').'");
224  });
225  $("#label").blur(function() {
226  displayMessage("label","'.$langs->trans('RequiredField').'");
227  });
228  $("#name").focus(function() {
229  hideMessage("name","'.$langs->trans('RequiredField').'");
230  });
231  $("#name").blur(function() {
232  displayMessage("name","'.$langs->trans('RequiredField').'");
233  });
234  displayMessage("label","'.$langs->trans('RequiredField').'");
235  displayMessage("name","'.$langs->trans('RequiredField').'");
236  $("#label").css("color","grey");
237  $("#name").css("color","grey");
238  })';
239  print '</script>'."\n";
240  }
241 
242  print '<form action="'.$_SERVER['PHP_SELF'].'" method="POST" name="formsoc">';
243  print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'"/>';
244  print '<input type="hidden" name="socid" value="'.$socid.'"/>';
245  print '<input type="hidden" name="backtopage" value="'.$backtopage.'"/>';
246  print '<input type="hidden" name="origin" value="'.$origin.'"/>';
247  print '<input type="hidden" name="originid" value="'.$originid.'"/>';
248  print '<input type="hidden" name="action" value="add"/>';
249 
250  print '<table class="border" width="100%">';
251 
252  print '<tr><td class="fieldrequired">'.$langs->trans('Label').'</td><td><input type="text" size="30" name="label" id="label" value="'.($object->label?$object->label:$langs->trans('RequiredField')).'"></td></tr>';
253  print '<tr><td class="fieldrequired">'.$langs->trans('Name').'</td><td><input type="text" size="30" name="name" id="name" value="'.($object->name?$object->name:$langs->trans('RequiredField')).'"></td></tr>';
254 
255  print '<tr><td class="tdtop">'.$langs->trans('Address').'</td><td colspan="3"><textarea name="address" class="quatrevingtpercent" rows="3" wrap="soft">';
256  print $object->address;
257  print '</textarea></td></tr>';
258 
259  // Zip
260  print '<tr><td>'.$langs->trans('Zip').'</td><td>';
261  print $formcompany->select_ziptown($object->zip,'zipcode',array('town','selectcountry_id'),6);
262  print '</td></tr>';
263 
264  // Town
265  print '<tr><td>'.$langs->trans('Town').'</td><td>';
266  print $formcompany->select_ziptown($object->town,'town',array('zipcode','selectcountry_id'));
267  print '</td></tr>';
268 
269  print '<tr><td width="25%">'.$langs->trans('Country').'</td><td colspan="3">';
270  print $form->select_country($object->country_id,'selectcountry_id');
271  print '</td></tr>';
272 
273  print '<tr><td>'.$langs->trans('Phone').'</td><td><input type="text" name="phone" value="'.$object->phone.'"></td></tr>';
274 
275  print '<tr><td>'.$langs->trans('Fax').'</td><td><input type="text" name="fax" value="'.$object->fax.'"></td></tr>';
276 
277  print '<tr><td>'.$langs->trans('Note').'</td><td colspan="3"><textarea name="note" cols="40" rows="6" wrap="soft">';
278  print $object->note;
279  print '</textarea></td></tr>';
280 
281  print '</table>'."\n";
282 
283  print '<br><div class="center">';
284  print '<input type="submit" class="button" value="'.$langs->trans('Add').'">';
285  if (! empty($backtopage))
286  {
287  print '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
288  print '<input type="submit" class="button" name="cancel" value="'.$langs->trans("Cancel").'">';
289  }
290  print '</div>'."\n";
291 
292  print '</form>'."\n";
293  }
294 }
295 elseif ($action == 'edit')
296 {
297  /*
298  * Fiche societe en mode edition
299  */
300 
301  $societe=new Societe($db);
302  $societe->fetch($socid);
303  $head = societe_prepare_head($societe);
304 
305  dol_fiche_head($head, 'card', $societe->name);
306 
307  print load_fiche_titre($langs->trans("EditAddress"));
308  print "<br>\n";
309 
310  if ($socid)
311  {
312  if ($reload || ! $_POST["name"])
313  {
314  $object->socid = $socid;
315  $object->fetch_address($id);
316  }
317  else
318  {
319  $object->id = $id;
320  $object->socid = $socid;
321  $object->label = $_POST["label"];
322  $object->name = $_POST["name"];
323  $object->address = $_POST["address"];
324  $object->zip = $_POST["zipcode"];
325  $object->town = $_POST["town"];
326  $object->country_id = $_POST["country_id"]?$_POST["country_id"]:$mysoc->country_id;
327  $object->phone = $_POST["phone"];
328  $object->fax = $_POST["fax"];
329  $object->note = $_POST["note"];
330 
331  // On positionne country_id, country_code and label of the chosen country
332  if ($object->country_id)
333  {
334  $tmparray=getCountry($object->country_id,'all');
335  $object->country_code = $tmparray['code'];
336  $object->country = $tmparray['label'];
337  }
338  }
339 
340  print '<form action="'.$_SERVER['PHP_SELF'].'?socid='.$object->socid.'" method="POST" name="formsoc">';
341  print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'"/>';
342  print '<input type="hidden" name="action" value="update"/>';
343  print '<input type="hidden" name="socid" value="'.$object->socid.'"/>';
344  print '<input type="hidden" name="backtopage" value="'.$backtopage.'"/>';
345  print '<input type="hidden" name="origin" value="'.$origin.'"/>';
346  print '<input type="hidden" name="originid" value="'.$originid.'"/>';
347  print '<input type="hidden" name="id" value="'.$object->id.'"/>';
348 
349  print '<table class="border" width="100%">';
350 
351  print '<tr><td>'.$langs->trans('AddressLabel').'</td><td colspan="3"><input type="text" size="40" name="label" value="'.$object->label.'"></td></tr>';
352  print '<tr><td>'.$langs->trans('Name').'</td><td colspan="3"><input type="text" size="40" name="name" value="'.$object->name.'"></td></tr>';
353 
354  print '<tr><td class="tdtop">'.$langs->trans('Address').'</td><td colspan="3"><textarea name="address" class="quatrevingtpercent" rows="3" wrap="soft">';
355  print $object->address;
356  print '</textarea></td></tr>';
357 
358  // Zip
359  print '<tr><td>'.$langs->trans('Zip').'</td><td>';
360  print $formcompany->select_ziptown($object->zip,'zipcode',array('town','selectcountry_id'),6);
361  print '</td></tr>';
362 
363  // Town
364  print '<tr><td>'.$langs->trans('Town').'</td><td>';
365  print $formcompany->select_ziptown($object->town,'town',array('zipcode','selectcountry_id'));
366  print '</td></tr>';
367 
368  print '<tr><td>'.$langs->trans('Country').'</td><td colspan="3">';
369  print $form->select_country($object->country_id,'country_id');
370  print '</td></tr>';
371 
372  print '<tr><td>'.$langs->trans('Phone').'</td><td><input type="text" name="phone" value="'.$object->phone.'"></td></tr>';
373 
374  print '<tr><td>'.$langs->trans('Fax').'</td><td><input type="text" name="fax" value="'.$object->fax.'"></td></tr>';
375 
376  print '<tr><td>'.$langs->trans('Note').'</td><td colspan="3"><textarea name="note" cols="40" rows="6" wrap="soft">';
377  print $object->note;
378  print '</textarea></td></tr>';
379 
380  print '</table><br>';
381 
382  print '<div class="center">';
383  print '<input type="submit" class="button" name="save" value="'.$langs->trans("Save").'">';
384  print '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
385  print '<input type="submit" class="button" name="cancel" value="'.$langs->trans("Cancel").'">';
386  print '</div>';
387 
388  print '</form>';
389  }
390 }
391 else
392 {
393  /*
394  * Fiche societe en mode visu
395  */
396 
397  $result=$object->fetch_lines($socid);
398  if ($result < 0)
399  {
400  dol_print_error($db,$object->error);
401  exit;
402  }
403 
404  $societe=new Societe($db);
405  $societe->fetch($object->socid);
406  $head = societe_prepare_head($societe);
407 
408  dol_fiche_head($head, 'customer', $societe->name);
409 
410 
411  // Confirmation delete
412  if ($action == 'delete')
413  {
414  print $form->formconfirm($_SERVER['PHP_SELF']."?socid=".$object->socid."&amp;id=".$id,$langs->trans("DeleteAddress"),$langs->trans("ConfirmDeleteAddress"),"confirm_delete");
415  }
416 
417  $nblines = count($object->lines);
418  if ($nblines)
419  {
420  for ($i = 0 ; $i < $nblines ; $i++)
421  {
422 
423  print '<table class="border" width="100%">';
424 
425  print '<tr><td width="20%">'.$langs->trans('AddressLabel').'</td><td colspan="3">'.$object->lines[$i]->label.'</td>';
426  print '<td valign="top" colspan="2" width="50%" rowspan="6">'.$langs->trans('Note').' :<br>'.nl2br($object->lines[$i]->note).'</td></tr>';
427  print '<tr><td width="20%">'.$langs->trans('Name').'</td><td colspan="3">'.$object->lines[$i]->name.'</td></tr>';
428 
429  print "<tr><td valign=\"top\">".$langs->trans('Address')."</td><td colspan=\"3\">".nl2br($object->lines[$i]->address)."</td></tr>";
430 
431  print '<tr><td width="25%">'.$langs->trans('Zip').'</td><td width="25%">'.$object->lines[$i]->zip."</td></tr>";
432  print '<tr><td width="25%">'.$langs->trans('Town').'</td><td width="25%">'.$object->lines[$i]->town."</td></tr>";
433 
434  print '<tr><td>'.$langs->trans('Country').'</td><td colspan="3">'.$object->lines[$i]->country.'</td>';
435 
436  print '<tr><td>'.$langs->trans('Phone').'</td><td>'.dol_print_phone($object->lines[$i]->phone,$object->lines[$i]->country_code,0,$object->socid,'AC_TEL').'</td></tr>';
437 
438  print '<tr><td>'.$langs->trans('Fax').'</td><td>'.dol_print_phone($object->lines[$i]->fax,$object->lines[$i]->country_code,0,$object->socid,'AC_FAX').'</td></tr>';
439 
440  print '</td></tr>';
441 
442  print '</table>';
443 
444 
445  /*
446  *
447  */
448 
449  print '<div class="tabsAction">';
450 
451  if ($user->rights->societe->creer)
452  {
453  print '<div class="inline-block divButAction"><a class="butAction" href="'.$_SERVER['PHP_SELF'].'?socid='.$object->socid.'&amp;id='.$object->lines[$i]->id.'&amp;action=edit">'.$langs->trans("Modify").'</a></div>';
454  }
455 
456  if ($user->rights->societe->supprimer)
457  {
458  print '<div class="inline-block divButAction"><a class="butActionDelete" href="'.$_SERVER['PHP_SELF'].'?socid='.$object->socid.'&amp;id='.$object->lines[$i]->id.'&amp;action=delete">'.$langs->trans("Delete").'</a></div>';
459  }
460 
461 
462  print '</div>';
463  print '<br>';
464  }
465  }
466  else
467  {
468  print $langs->trans("None");
469  }
470  print '</div>';
471 
472 
473  /*
474  * Bouton actions
475  */
476 
477  if ($action == '')
478  {
479  print '<div class="tabsAction">';
480 
481  if ($user->rights->societe->creer)
482  {
483  print '<div class="inline-block divButAction"><a class="butAction" href="'.$_SERVER['PHP_SELF'].'?socid='.$object->socid.'&amp;action=create">'.$langs->trans("Add").'</a></div>';
484  }
485  print '</div>';
486  }
487 }
488 
489 
490 // End of page
491 llxFooter();
492 $db->close();
llxFooter()
Empty footer.
Definition: wrapper.php:56
load_fiche_titre($titre, $morehtmlright='', $picto='title_generic.png', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
GETPOST($paramname, $check='none', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
print
Draft customers invoices.
Definition: index.php:91
setEventMessages($mesg, $mesgs, $style='mesgs')
Set event messages in dol_events session object.
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
getCountry($searchkey, $withcode='', $dbtouse=0, $outputlangs='', $entconv=1, $searchlabel='')
Return country label, code or id from an id, code or label.
Class to build HTML component for third parties management Only common components are here...
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0)
Show a message to say access is forbidden and stop program Calling this function terminate execution ...
Class to manage generation of HTML components Only common components must be here.
Class to manage third parties objects (customers, suppliers, prospects...)
llxHeader()
Empty header.
Definition: wrapper.php:44
societe_prepare_head(Societe $object)
Return array of tabs to used on pages for third parties cards.
Definition: company.lib.php:42
restrictedArea($user, $features, $objectid=0, $tableandshare='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $isdraft=0)
Check permissions of a user to show a page and an object.
dol_print_phone($phone, $countrycode='', $cid=0, $socid=0, $addlink='', $separ="&nbsp;", $withpicto='', $titlealt='', $adddivfloat=0)
Format phone numbers according to country.
Class to manage addresses.
dol_fiche_head($links=array(), $active='0', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='')
Show tab header of a card.