dolibarr  7.0.0-beta
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@capnetworks.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 $langs->load("companies");
32 $langs->load("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 
157 else if ($action == 'confirm_delete' && $confirm == 'yes' && $user->rights->societe->supprimer)
158 {
159  $result = $object->delete($id, $socid);
160 
161  if ($result == 0)
162  {
163  header("Location: ".$_SERVER['PHP_SELF']."?socid=".$socid);
164  exit ;
165  }
166  else
167  {
168  $reload = 0;
169  $action='';
170  }
171 }
172 
178 llxHeader();
179 
180 $form = new Form($db);
181 $formcompany = new FormCompany($db);
182 $countrynotdefined=$langs->trans("ErrorSetACountryFirst").' ('.$langs->trans("SeeAbove").')';
183 
184 if ($action == 'create')
185 {
186  if ($user->rights->societe->creer)
187  {
188  /*
189  * Creation
190  */
191 
192  if ($_POST["label"] && $_POST["name"])
193  {
194  $object->socid = $socid;
195  $object->label = $_POST["label"];
196  $object->name = $_POST["name"];
197  $object->address = $_POST["address"];
198  $object->zip = $_POST["zipcode"];
199  $object->town = $_POST["town"];
200  $object->phone = $_POST["phone"];
201  $object->fax = $_POST["fax"];
202  $object->note = $_POST["note"];
203  }
204 
205  // On positionne country_id, country_code and label of the chosen country
206  $object->country_id = (GETPOST('country_id','int') ? GETPOST('country_id','int') : $mysoc->country_id);
207  if ($object->country_id)
208  {
209  $tmparray=getCountry($object->country_id,'all');
210  $object->country_code = $tmparray['code'];
211  $object->country = $tmparray['label'];
212  }
213 
214  print load_fiche_titre($langs->trans("AddAddress"));
215 
216  print "<br>\n";
217 
218  // If javascript enabled, we add interactivity on mandatory fields
219  if ($conf->use_javascript_ajax)
220  {
221  print "\n".'<script type="text/javascript" language="javascript">';
222  print '$(document).ready(function () {
223  $("#label").focus(function() {
224  hideMessage("label","'.$langs->trans('RequiredField').'");
225  });
226  $("#label").blur(function() {
227  displayMessage("label","'.$langs->trans('RequiredField').'");
228  });
229  $("#name").focus(function() {
230  hideMessage("name","'.$langs->trans('RequiredField').'");
231  });
232  $("#name").blur(function() {
233  displayMessage("name","'.$langs->trans('RequiredField').'");
234  });
235  displayMessage("label","'.$langs->trans('RequiredField').'");
236  displayMessage("name","'.$langs->trans('RequiredField').'");
237  $("#label").css("color","grey");
238  $("#name").css("color","grey");
239  })';
240  print '</script>'."\n";
241  }
242 
243  print '<form action="'.$_SERVER['PHP_SELF'].'" method="POST" name="formsoc">';
244  print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'"/>';
245  print '<input type="hidden" name="socid" value="'.$socid.'"/>';
246  print '<input type="hidden" name="backtopage" value="'.$backtopage.'"/>';
247  print '<input type="hidden" name="origin" value="'.$origin.'"/>';
248  print '<input type="hidden" name="originid" value="'.$originid.'"/>';
249  print '<input type="hidden" name="action" value="add"/>';
250 
251  print '<table class="border" width="100%">';
252 
253  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>';
254  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>';
255 
256  print '<tr><td class="tdtop">'.$langs->trans('Address').'</td><td colspan="3"><textarea name="address" class="quatrevingtpercent" rows="3" wrap="soft">';
257  print $object->address;
258  print '</textarea></td></tr>';
259 
260  // Zip
261  print '<tr><td>'.$langs->trans('Zip').'</td><td>';
262  print $formcompany->select_ziptown($object->zip,'zipcode',array('town','selectcountry_id'),6);
263  print '</td></tr>';
264 
265  // Town
266  print '<tr><td>'.$langs->trans('Town').'</td><td>';
267  print $formcompany->select_ziptown($object->town,'town',array('zipcode','selectcountry_id'));
268  print '</td></tr>';
269 
270  print '<tr><td width="25%">'.$langs->trans('Country').'</td><td colspan="3">';
271  print $form->select_country($object->country_id,'selectcountry_id');
272  print '</td></tr>';
273 
274  print '<tr><td>'.$langs->trans('Phone').'</td><td><input type="text" name="phone" value="'.$object->phone.'"></td></tr>';
275 
276  print '<tr><td>'.$langs->trans('Fax').'</td><td><input type="text" name="fax" value="'.$object->fax.'"></td></tr>';
277 
278  print '<tr><td>'.$langs->trans('Note').'</td><td colspan="3"><textarea name="note" cols="40" rows="6" wrap="soft">';
279  print $object->note;
280  print '</textarea></td></tr>';
281 
282  print '</table>'."\n";
283 
284  print '<br><div class="center">';
285  print '<input type="submit" class="button" value="'.$langs->trans('Add').'">';
286  if (! empty($backtopage))
287  {
288  print '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
289  print '<input type="submit" class="button" name="cancel" value="'.$langs->trans("Cancel").'">';
290  }
291  print '</div>'."\n";
292 
293  print '</form>'."\n";
294 
295  }
296 }
297 elseif ($action == 'edit')
298 {
299  /*
300  * Fiche societe en mode edition
301  */
302 
303  $societe=new Societe($db);
304  $societe->fetch($socid);
305  $head = societe_prepare_head($societe);
306 
307  dol_fiche_head($head, 'card', $societe->name);
308 
309  print load_fiche_titre($langs->trans("EditAddress"));
310  print "<br>\n";
311 
312  if ($socid)
313  {
314  if ($reload || ! $_POST["name"])
315  {
316  $object->socid = $socid;
317  $object->fetch_address($id);
318  }
319  else
320  {
321  $object->id = $id;
322  $object->socid = $socid;
323  $object->label = $_POST["label"];
324  $object->name = $_POST["name"];
325  $object->address = $_POST["address"];
326  $object->zip = $_POST["zipcode"];
327  $object->town = $_POST["town"];
328  $object->country_id = $_POST["country_id"]?$_POST["country_id"]:$mysoc->country_id;
329  $object->phone = $_POST["phone"];
330  $object->fax = $_POST["fax"];
331  $object->note = $_POST["note"];
332 
333  // On positionne country_id, country_code and label of the chosen country
334  if ($object->country_id)
335  {
336  $tmparray=getCountry($object->country_id,'all');
337  $object->country_code = $tmparray['code'];
338  $object->country = $tmparray['label'];
339  }
340  }
341 
342  print '<form action="'.$_SERVER['PHP_SELF'].'?socid='.$object->socid.'" method="POST" name="formsoc">';
343  print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'"/>';
344  print '<input type="hidden" name="action" value="update"/>';
345  print '<input type="hidden" name="socid" value="'.$object->socid.'"/>';
346  print '<input type="hidden" name="backtopage" value="'.$backtopage.'"/>';
347  print '<input type="hidden" name="origin" value="'.$origin.'"/>';
348  print '<input type="hidden" name="originid" value="'.$originid.'"/>';
349  print '<input type="hidden" name="id" value="'.$object->id.'"/>';
350 
351  print '<table class="border" width="100%">';
352 
353  print '<tr><td>'.$langs->trans('AddressLabel').'</td><td colspan="3"><input type="text" size="40" name="label" value="'.$object->label.'"></td></tr>';
354  print '<tr><td>'.$langs->trans('Name').'</td><td colspan="3"><input type="text" size="40" name="name" value="'.$object->name.'"></td></tr>';
355 
356  print '<tr><td class="tdtop">'.$langs->trans('Address').'</td><td colspan="3"><textarea name="address" class="quatrevingtpercent" rows="3" wrap="soft">';
357  print $object->address;
358  print '</textarea></td></tr>';
359 
360  // Zip
361  print '<tr><td>'.$langs->trans('Zip').'</td><td>';
362  print $formcompany->select_ziptown($object->zip,'zipcode',array('town','selectcountry_id'),6);
363  print '</td></tr>';
364 
365  // Town
366  print '<tr><td>'.$langs->trans('Town').'</td><td>';
367  print $formcompany->select_ziptown($object->town,'town',array('zipcode','selectcountry_id'));
368  print '</td></tr>';
369 
370  print '<tr><td>'.$langs->trans('Country').'</td><td colspan="3">';
371  print $form->select_country($object->country_id,'country_id');
372  print '</td></tr>';
373 
374  print '<tr><td>'.$langs->trans('Phone').'</td><td><input type="text" name="phone" value="'.$object->phone.'"></td></tr>';
375 
376  print '<tr><td>'.$langs->trans('Fax').'</td><td><input type="text" name="fax" value="'.$object->fax.'"></td></tr>';
377 
378  print '<tr><td>'.$langs->trans('Note').'</td><td colspan="3"><textarea name="note" cols="40" rows="6" wrap="soft">';
379  print $object->note;
380  print '</textarea></td></tr>';
381 
382  print '</table><br>';
383 
384  print '<div class="center">';
385  print '<input type="submit" class="button" name="save" value="'.$langs->trans("Save").'">';
386  print '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
387  print '<input type="submit" class="button" name="cancel" value="'.$langs->trans("Cancel").'">';
388  print '</div>';
389 
390  print '</form>';
391  }
392 }
393 else
394 {
395  /*
396  * Fiche societe en mode visu
397  */
398 
399  $result=$object->fetch_lines($socid);
400  if ($result < 0)
401  {
402  dol_print_error($db,$object->error);
403  exit;
404  }
405 
406  $societe=new Societe($db);
407  $societe->fetch($object->socid);
408  $head = societe_prepare_head($societe);
409 
410  dol_fiche_head($head, 'customer', $societe->name);
411 
412 
413  // Confirmation delete
414  if ($action == 'delete')
415  {
416  print $form->formconfirm($_SERVER['PHP_SELF']."?socid=".$object->socid."&amp;id=".$id,$langs->trans("DeleteAddress"),$langs->trans("ConfirmDeleteAddress"),"confirm_delete");
417  }
418 
419  $nblines = count($object->lines);
420  if ($nblines)
421  {
422  for ($i = 0 ; $i < $nblines ; $i++)
423  {
424 
425  print '<table class="border" width="100%">';
426 
427  print '<tr><td width="20%">'.$langs->trans('AddressLabel').'</td><td colspan="3">'.$object->lines[$i]->label.'</td>';
428  print '<td valign="top" colspan="2" width="50%" rowspan="6">'.$langs->trans('Note').' :<br>'.nl2br($object->lines[$i]->note).'</td></tr>';
429  print '<tr><td width="20%">'.$langs->trans('Name').'</td><td colspan="3">'.$object->lines[$i]->name.'</td></tr>';
430 
431  print "<tr><td valign=\"top\">".$langs->trans('Address')."</td><td colspan=\"3\">".nl2br($object->lines[$i]->address)."</td></tr>";
432 
433  print '<tr><td width="25%">'.$langs->trans('Zip').'</td><td width="25%">'.$object->lines[$i]->zip."</td></tr>";
434  print '<tr><td width="25%">'.$langs->trans('Town').'</td><td width="25%">'.$object->lines[$i]->town."</td></tr>";
435 
436  print '<tr><td>'.$langs->trans('Country').'</td><td colspan="3">'.$object->lines[$i]->country.'</td>';
437 
438  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>';
439 
440  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>';
441 
442  print '</td></tr>';
443 
444  print '</table>';
445 
446 
447  /*
448  *
449  */
450 
451  print '<div class="tabsAction">';
452 
453  if ($user->rights->societe->creer)
454  {
455  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>';
456  }
457 
458  if ($user->rights->societe->supprimer)
459  {
460  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>';
461  }
462 
463 
464  print '</div>';
465  print '<br>';
466  }
467  }
468  else
469  {
470  print $langs->trans("None");
471  }
472  print '</div>';
473 
474 
475  /*
476  * Bouton actions
477  */
478 
479  if ($action == '')
480  {
481  print '<div class="tabsAction">';
482 
483  if ($user->rights->societe->creer)
484  {
485  print '<div class="inline-block divButAction"><a class="butAction" href="'.$_SERVER['PHP_SELF'].'?socid='.$object->socid.'&amp;action=create">'.$langs->trans("Add").'</a></div>';
486  }
487  print '</div>';
488  }
489 
490 }
491 
492 
493 // End of page
494 llxFooter();
495 $db->close();
llxFooter()
Empty footer.
Definition: wrapper.php:58
setEventMessages($mesg, $mesgs, $style='mesgs')
Set event messages in dol_events session object.
dol_fiche_head($links=array(), $active='0', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='')
Show tab header of a card.
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.
if(empty($reshook)) $form
View.
Definition: perms.php:103
GETPOST($paramname, $check='none', $method=0, $filter=NULL, $options=NULL, $noreplace=0)
Return value of a param into GET or POST supervariable.
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...)
load_fiche_titre($titre, $morehtmlright='', $picto='title_generic.png', $pictoisfullpath=0, $id=0, $morecssontable='', $morehtmlcenter='')
Load a title with picto.
llxHeader()
Empty header.
Definition: wrapper.php:46
societe_prepare_head(Societe $object)
Return array of tabs to used on pages for third parties cards.
Definition: company.lib.php:40
print
Draft customers invoices.
Definition: index.php:91
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.
restrictedArea($user, $features, $objectid=0, $tableandshare='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $objcanvas=null)
Check permissions of a user to show a page and an object.