address.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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. /**
  20. * \file htdocs/comm/address.php
  21. * \ingroup societe
  22. * \brief Tab address of thirdparty
  23. */
  24. require '../main.inc.php';
  25. require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
  26. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php';
  27. require_once DOL_DOCUMENT_ROOT.'/societe/class/address.class.php';
  28. // Load translation files required by the page
  29. $langs->loadLangs(array('companies', 'commercial'));
  30. $id = GETPOST('id','int');
  31. $action = GETPOST('action','alpha');
  32. $confirm = GETPOST('confirm','alpha');
  33. $backtopage = GETPOST('backtopage','alpha');
  34. $origin = GETPOST('origin','alpha');
  35. $originid = GETPOST('originid','int');
  36. $socid = GETPOST('socid','int');
  37. if (! $socid && ($action != 'create' && $action != 'add' && $action != 'update')) accessforbidden();
  38. // Security check
  39. if ($user->societe_id) $socid=$user->societe_id;
  40. $result = restrictedArea($user, 'societe', $socid);
  41. $object = new Address($db);
  42. /*
  43. * Actions
  44. */
  45. // Cancel
  46. if (GETPOST('cancel','alpha') && ! empty($backtopage))
  47. {
  48. header("Location: ".$backtopage);
  49. exit;
  50. }
  51. if ($action == 'add' || $action == 'update')
  52. {
  53. $object->socid = $socid;
  54. $object->label = ($_POST["label"]!=$langs->trans('RequiredField')?$_POST["label"]:'');
  55. $object->name = ($_POST["name"]!=$langs->trans('RequiredField')?$_POST["name"]:'');
  56. $object->address = $_POST["address"];
  57. $object->zip = $_POST["zipcode"];
  58. $object->town = $_POST["town"];
  59. $object->country_id = $_POST["country_id"];
  60. $object->phone = $_POST["phone"];
  61. $object->fax = $_POST["fax"];
  62. $object->note = $_POST["note"];
  63. // Add new address
  64. if ($action == 'add')
  65. {
  66. $result = $object->create($socid, $user);
  67. if ($result >= 0)
  68. {
  69. if (! empty($backtopage))
  70. {
  71. header("Location: ".$backtopage);
  72. exit;
  73. }
  74. else if ($origin == 'commande')
  75. {
  76. header("Location: ../commande/contact.php?action=editdelivery_adress&socid=".$socid."&id=".$originid);
  77. exit;
  78. }
  79. elseif ($origin == 'propal')
  80. {
  81. header("Location: ../comm/propal/contact.php?action=editdelivery_adress&socid=".$socid."&id=".$originid);
  82. exit;
  83. }
  84. elseif ($origin == 'shipment')
  85. {
  86. header("Location: ../expedition/card.php?id=".$originid);
  87. exit;
  88. }
  89. else
  90. {
  91. header("Location: ".$_SERVER['PHP_SELF']."?socid=".$socid);
  92. exit;
  93. }
  94. }
  95. else
  96. {
  97. setEventMessages($object->error, $object->errors, 'errors');
  98. $action='create';
  99. }
  100. }
  101. // Update address
  102. else if ($action == 'update')
  103. {
  104. $result = $object->update($id, $socid, $user);
  105. if ($result >= 0)
  106. {
  107. if (! empty($backtopage))
  108. {
  109. header("Location: ".$backtopage);
  110. exit;
  111. }
  112. else if ($origin == 'commande')
  113. {
  114. header("Location: ../commande/contact.php?id=".$originid);
  115. exit;
  116. }
  117. elseif ($origin == 'propal')
  118. {
  119. header("Location: ../comm/propal/contact.php?id=".$originid);
  120. exit;
  121. }
  122. elseif ($origin == 'shipment')
  123. {
  124. header("Location: ../expedition/card.php?id=".$originid);
  125. exit;
  126. }
  127. else
  128. {
  129. header("Location: ".$_SERVER['PHP_SELF']."?socid=".$socid);
  130. exit;
  131. }
  132. }
  133. else
  134. {
  135. $reload = 0;
  136. setEventMessages($object->error, $object->errors, 'errors');
  137. $action= "edit";
  138. }
  139. }
  140. }
  141. else if ($action == 'confirm_delete' && $confirm == 'yes' && $user->rights->societe->supprimer)
  142. {
  143. $result = $object->delete($id, $socid);
  144. if ($result == 0)
  145. {
  146. header("Location: ".$_SERVER['PHP_SELF']."?socid=".$socid);
  147. exit ;
  148. }
  149. else
  150. {
  151. $reload = 0;
  152. $action='';
  153. }
  154. }
  155. /**
  156. *
  157. *
  158. */
  159. llxHeader();
  160. $form = new Form($db);
  161. $formcompany = new FormCompany($db);
  162. $countrynotdefined=$langs->trans("ErrorSetACountryFirst").' ('.$langs->trans("SeeAbove").')';
  163. if ($action == 'create')
  164. {
  165. if ($user->rights->societe->creer)
  166. {
  167. /*
  168. * Creation
  169. */
  170. if ($_POST["label"] && $_POST["name"])
  171. {
  172. $object->socid = $socid;
  173. $object->label = $_POST["label"];
  174. $object->name = $_POST["name"];
  175. $object->address = $_POST["address"];
  176. $object->zip = $_POST["zipcode"];
  177. $object->town = $_POST["town"];
  178. $object->phone = $_POST["phone"];
  179. $object->fax = $_POST["fax"];
  180. $object->note = $_POST["note"];
  181. }
  182. // On positionne country_id, country_code and label of the chosen country
  183. $object->country_id = (GETPOST('country_id','int') ? GETPOST('country_id','int') : $mysoc->country_id);
  184. if ($object->country_id)
  185. {
  186. $tmparray=getCountry($object->country_id,'all');
  187. $object->country_code = $tmparray['code'];
  188. $object->country = $tmparray['label'];
  189. }
  190. print load_fiche_titre($langs->trans("AddAddress"));
  191. print "<br>\n";
  192. // If javascript enabled, we add interactivity on mandatory fields
  193. if ($conf->use_javascript_ajax)
  194. {
  195. print "\n".'<script type="text/javascript" language="javascript">';
  196. print '$(document).ready(function () {
  197. $("#label").focus(function() {
  198. hideMessage("label","'.$langs->trans('RequiredField').'");
  199. });
  200. $("#label").blur(function() {
  201. displayMessage("label","'.$langs->trans('RequiredField').'");
  202. });
  203. $("#name").focus(function() {
  204. hideMessage("name","'.$langs->trans('RequiredField').'");
  205. });
  206. $("#name").blur(function() {
  207. displayMessage("name","'.$langs->trans('RequiredField').'");
  208. });
  209. displayMessage("label","'.$langs->trans('RequiredField').'");
  210. displayMessage("name","'.$langs->trans('RequiredField').'");
  211. $("#label").css("color","grey");
  212. $("#name").css("color","grey");
  213. })';
  214. print '</script>'."\n";
  215. }
  216. print '<form action="'.$_SERVER['PHP_SELF'].'" method="POST" name="formsoc">';
  217. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'"/>';
  218. print '<input type="hidden" name="socid" value="'.$socid.'"/>';
  219. print '<input type="hidden" name="backtopage" value="'.$backtopage.'"/>';
  220. print '<input type="hidden" name="origin" value="'.$origin.'"/>';
  221. print '<input type="hidden" name="originid" value="'.$originid.'"/>';
  222. print '<input type="hidden" name="action" value="add"/>';
  223. print '<table class="border" width="100%">';
  224. 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>';
  225. 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>';
  226. print '<tr><td class="tdtop">'.$langs->trans('Address').'</td><td colspan="3"><textarea name="address" class="quatrevingtpercent" rows="3" wrap="soft">';
  227. print $object->address;
  228. print '</textarea></td></tr>';
  229. // Zip
  230. print '<tr><td>'.$langs->trans('Zip').'</td><td>';
  231. print $formcompany->select_ziptown($object->zip,'zipcode',array('town','selectcountry_id'),6);
  232. print '</td></tr>';
  233. // Town
  234. print '<tr><td>'.$langs->trans('Town').'</td><td>';
  235. print $formcompany->select_ziptown($object->town,'town',array('zipcode','selectcountry_id'));
  236. print '</td></tr>';
  237. print '<tr><td width="25%">'.$langs->trans('Country').'</td><td colspan="3">';
  238. print $form->select_country($object->country_id,'selectcountry_id');
  239. print '</td></tr>';
  240. print '<tr><td>'.$langs->trans('Phone').'</td><td><input type="text" name="phone" value="'.$object->phone.'"></td></tr>';
  241. print '<tr><td>'.$langs->trans('Fax').'</td><td><input type="text" name="fax" value="'.$object->fax.'"></td></tr>';
  242. print '<tr><td>'.$langs->trans('Note').'</td><td colspan="3"><textarea name="note" cols="40" rows="6" wrap="soft">';
  243. print $object->note;
  244. print '</textarea></td></tr>';
  245. print '</table>'."\n";
  246. print '<br><div class="center">';
  247. print '<input type="submit" class="button" value="'.$langs->trans('Add').'">';
  248. if (! empty($backtopage))
  249. {
  250. print '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
  251. print '<input type="submit" class="button" name="cancel" value="'.$langs->trans("Cancel").'">';
  252. }
  253. print '</div>'."\n";
  254. print '</form>'."\n";
  255. }
  256. }
  257. elseif ($action == 'edit')
  258. {
  259. /*
  260. * Fiche societe en mode edition
  261. */
  262. $societe=new Societe($db);
  263. $societe->fetch($socid);
  264. $head = societe_prepare_head($societe);
  265. dol_fiche_head($head, 'card', $societe->name);
  266. print load_fiche_titre($langs->trans("EditAddress"));
  267. print "<br>\n";
  268. if ($socid)
  269. {
  270. if ($reload || ! $_POST["name"])
  271. {
  272. $object->socid = $socid;
  273. $object->fetch_address($id);
  274. }
  275. else
  276. {
  277. $object->id = $id;
  278. $object->socid = $socid;
  279. $object->label = $_POST["label"];
  280. $object->name = $_POST["name"];
  281. $object->address = $_POST["address"];
  282. $object->zip = $_POST["zipcode"];
  283. $object->town = $_POST["town"];
  284. $object->country_id = $_POST["country_id"]?$_POST["country_id"]:$mysoc->country_id;
  285. $object->phone = $_POST["phone"];
  286. $object->fax = $_POST["fax"];
  287. $object->note = $_POST["note"];
  288. // On positionne country_id, country_code and label of the chosen country
  289. if ($object->country_id)
  290. {
  291. $tmparray=getCountry($object->country_id,'all');
  292. $object->country_code = $tmparray['code'];
  293. $object->country = $tmparray['label'];
  294. }
  295. }
  296. print '<form action="'.$_SERVER['PHP_SELF'].'?socid='.$object->socid.'" method="POST" name="formsoc">';
  297. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'"/>';
  298. print '<input type="hidden" name="action" value="update"/>';
  299. print '<input type="hidden" name="socid" value="'.$object->socid.'"/>';
  300. print '<input type="hidden" name="backtopage" value="'.$backtopage.'"/>';
  301. print '<input type="hidden" name="origin" value="'.$origin.'"/>';
  302. print '<input type="hidden" name="originid" value="'.$originid.'"/>';
  303. print '<input type="hidden" name="id" value="'.$object->id.'"/>';
  304. print '<table class="border" width="100%">';
  305. print '<tr><td>'.$langs->trans('AddressLabel').'</td><td colspan="3"><input type="text" size="40" name="label" value="'.$object->label.'"></td></tr>';
  306. print '<tr><td>'.$langs->trans('Name').'</td><td colspan="3"><input type="text" size="40" name="name" value="'.$object->name.'"></td></tr>';
  307. print '<tr><td class="tdtop">'.$langs->trans('Address').'</td><td colspan="3"><textarea name="address" class="quatrevingtpercent" rows="3" wrap="soft">';
  308. print $object->address;
  309. print '</textarea></td></tr>';
  310. // Zip
  311. print '<tr><td>'.$langs->trans('Zip').'</td><td>';
  312. print $formcompany->select_ziptown($object->zip,'zipcode',array('town','selectcountry_id'),6);
  313. print '</td></tr>';
  314. // Town
  315. print '<tr><td>'.$langs->trans('Town').'</td><td>';
  316. print $formcompany->select_ziptown($object->town,'town',array('zipcode','selectcountry_id'));
  317. print '</td></tr>';
  318. print '<tr><td>'.$langs->trans('Country').'</td><td colspan="3">';
  319. print $form->select_country($object->country_id,'country_id');
  320. print '</td></tr>';
  321. print '<tr><td>'.$langs->trans('Phone').'</td><td><input type="text" name="phone" value="'.$object->phone.'"></td></tr>';
  322. print '<tr><td>'.$langs->trans('Fax').'</td><td><input type="text" name="fax" value="'.$object->fax.'"></td></tr>';
  323. print '<tr><td>'.$langs->trans('Note').'</td><td colspan="3"><textarea name="note" cols="40" rows="6" wrap="soft">';
  324. print $object->note;
  325. print '</textarea></td></tr>';
  326. print '</table><br>';
  327. print '<div class="center">';
  328. print '<input type="submit" class="button" name="save" value="'.$langs->trans("Save").'">';
  329. print '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
  330. print '<input type="submit" class="button" name="cancel" value="'.$langs->trans("Cancel").'">';
  331. print '</div>';
  332. print '</form>';
  333. }
  334. }
  335. else
  336. {
  337. /*
  338. * Fiche societe en mode visu
  339. */
  340. $result=$object->fetch_lines($socid);
  341. if ($result < 0)
  342. {
  343. dol_print_error($db,$object->error);
  344. exit;
  345. }
  346. $societe=new Societe($db);
  347. $societe->fetch($object->socid);
  348. $head = societe_prepare_head($societe);
  349. dol_fiche_head($head, 'customer', $societe->name);
  350. // Confirmation delete
  351. if ($action == 'delete')
  352. {
  353. print $form->formconfirm($_SERVER['PHP_SELF']."?socid=".$object->socid."&amp;id=".$id,$langs->trans("DeleteAddress"),$langs->trans("ConfirmDeleteAddress"),"confirm_delete");
  354. }
  355. $nblines = count($object->lines);
  356. if ($nblines)
  357. {
  358. for ($i = 0 ; $i < $nblines ; $i++)
  359. {
  360. print '<table class="border" width="100%">';
  361. print '<tr><td width="20%">'.$langs->trans('AddressLabel').'</td><td colspan="3">'.$object->lines[$i]->label.'</td>';
  362. print '<td valign="top" colspan="2" width="50%" rowspan="6">'.$langs->trans('Note').' :<br>'.nl2br($object->lines[$i]->note).'</td></tr>';
  363. print '<tr><td width="20%">'.$langs->trans('Name').'</td><td colspan="3">'.$object->lines[$i]->name.'</td></tr>';
  364. print "<tr><td valign=\"top\">".$langs->trans('Address')."</td><td colspan=\"3\">".nl2br($object->lines[$i]->address)."</td></tr>";
  365. print '<tr><td width="25%">'.$langs->trans('Zip').'</td><td width="25%">'.$object->lines[$i]->zip."</td></tr>";
  366. print '<tr><td width="25%">'.$langs->trans('Town').'</td><td width="25%">'.$object->lines[$i]->town."</td></tr>";
  367. print '<tr><td>'.$langs->trans('Country').'</td><td colspan="3">'.$object->lines[$i]->country.'</td>';
  368. 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>';
  369. 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>';
  370. print '</td></tr>';
  371. print '</table>';
  372. /*
  373. *
  374. */
  375. print '<div class="tabsAction">';
  376. if ($user->rights->societe->creer)
  377. {
  378. 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>';
  379. }
  380. if ($user->rights->societe->supprimer)
  381. {
  382. 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>';
  383. }
  384. print '</div>';
  385. print '<br>';
  386. }
  387. }
  388. else
  389. {
  390. print $langs->trans("None");
  391. }
  392. print '</div>';
  393. /*
  394. * Bouton actions
  395. */
  396. if ($action == '')
  397. {
  398. print '<div class="tabsAction">';
  399. if ($user->rights->societe->creer)
  400. {
  401. print '<div class="inline-block divButAction"><a class="butAction" href="'.$_SERVER['PHP_SELF'].'?socid='.$object->socid.'&amp;action=create">'.$langs->trans("Add").'</a></div>';
  402. }
  403. print '</div>';
  404. }
  405. }
  406. // End of page
  407. llxFooter();
  408. $db->close();