PageRenderTime 54ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/compta/facture/contact.php

https://github.com/asterix14/dolibarr
PHP | 363 lines | 244 code | 59 blank | 60 comment | 40 complexity | 1b952ce5f7e2e3738564c868b02eeb0c MD5 | raw file
Possible License(s): LGPL-2.0
  1. <?php
  2. /* Copyright (C) 2005 Patrick Rouillon <patrick@rouillon.net>
  3. * Copyright (C) 2005-2009 Destailleur Laurent <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 2 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 <http://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file htdocs/compta/facture/contact.php
  20. * \ingroup facture
  21. * \brief Onglet de gestion des contacts des factures
  22. */
  23. require("../../main.inc.php");
  24. require_once(DOL_DOCUMENT_ROOT."/compta/facture/class/facture.class.php");
  25. require_once(DOL_DOCUMENT_ROOT."/contact/class/contact.class.php");
  26. require_once(DOL_DOCUMENT_ROOT.'/core/class/discount.class.php');
  27. require_once(DOL_DOCUMENT_ROOT.'/core/lib/invoice.lib.php');
  28. require_once(DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php');
  29. $langs->load("bills");
  30. $langs->load("companies");
  31. $facid = isset($_GET["facid"])?$_GET["facid"]:'';
  32. // Security check
  33. if ($user->societe_id) $socid=$user->societe_id;
  34. $result = restrictedArea($user, 'facture', $facid);
  35. /*
  36. * Ajout d'un nouveau contact
  37. */
  38. if ($_POST["action"] == 'addcontact' && $user->rights->facture->creer)
  39. {
  40. $result = 0;
  41. $facture = new Facture($db);
  42. $result = $facture->fetch($_GET["facid"]);
  43. if ($result > 0 && $_GET["facid"] > 0)
  44. {
  45. $result = $facture->add_contact($_POST["contactid"], $_POST["type"], $_POST["source"]);
  46. }
  47. if ($result >= 0)
  48. {
  49. Header("Location: contact.php?facid=".$facture->id);
  50. exit;
  51. }
  52. else
  53. {
  54. if ($facture->error == 'DB_ERROR_RECORD_ALREADY_EXISTS')
  55. {
  56. $langs->load("errors");
  57. $mesg = '<div class="error">'.$langs->trans("ErrorThisContactIsAlreadyDefinedAsThisType").'</div>';
  58. }
  59. else
  60. {
  61. $mesg = '<div class="error">'.$facture->error.'</div>';
  62. }
  63. }
  64. }
  65. // bascule du statut d'un contact
  66. if ($_GET["action"] == 'swapstatut' && $user->rights->facture->creer)
  67. {
  68. $facture = new Facture($db);
  69. if ($facture->fetch(GETPOST("facid")))
  70. {
  71. $result=$facture->swapContactStatus(GETPOST('ligne'));
  72. }
  73. else
  74. {
  75. dol_print_error($db);
  76. }
  77. }
  78. // Efface un contact
  79. if ($_GET["action"] == 'deleteline' && $user->rights->facture->creer)
  80. {
  81. $facture = new Facture($db);
  82. $facture->fetch($_GET["facid"]);
  83. $result = $facture->delete_contact($_GET["lineid"]);
  84. if ($result >= 0)
  85. {
  86. Header("Location: contact.php?facid=".$facture->id);
  87. exit;
  88. }
  89. else {
  90. dol_print_error($db);
  91. }
  92. }
  93. /*
  94. * View
  95. */
  96. llxHeader('', $langs->trans("Bill"), "Facture");
  97. $form = new Form($db);
  98. $formcompany = new FormCompany($db);
  99. $contactstatic=new Contact($db);
  100. $userstatic=new User($db);
  101. /* *************************************************************************** */
  102. /* */
  103. /* Mode vue et edition */
  104. /* */
  105. /* *************************************************************************** */
  106. dol_htmloutput_mesg($mesg);
  107. $id = $_GET['facid'];
  108. $ref= $_GET['ref'];
  109. if ($id > 0 || ! empty($ref))
  110. {
  111. $facture = new Facture($db);
  112. if ($facture->fetch($id, $ref) > 0)
  113. {
  114. $facture->fetch_thirdparty();
  115. $head = facture_prepare_head($facture);
  116. dol_fiche_head($head, 'contact', $langs->trans('InvoiceCustomer'), 0, 'bill');
  117. /*
  118. * Facture synthese pour rappel
  119. */
  120. print '<table class="border" width="100%">';
  121. // Ref
  122. print '<tr><td width="20%">'.$langs->trans('Ref').'</td>';
  123. print '<td colspan="3">';
  124. $morehtmlref='';
  125. $discount=new DiscountAbsolute($db);
  126. $result=$discount->fetch(0,$facture->id);
  127. if ($result > 0)
  128. {
  129. $morehtmlref=' ('.$langs->trans("CreditNoteConvertedIntoDiscount",$discount->getNomUrl(1,'discount')).')';
  130. }
  131. if ($result < 0)
  132. {
  133. dol_print_error('',$discount->error);
  134. }
  135. print $form->showrefnav($facture,'ref','',1,'facnumber','ref',$morehtmlref);
  136. print '</td></tr>';
  137. // Customer
  138. print "<tr><td>".$langs->trans("Company")."</td>";
  139. print '<td colspan="3">'.$facture->client->getNomUrl(1,'compta').'</td></tr>';
  140. print "</table>";
  141. print '</div>';
  142. /*
  143. * Lignes de contacts
  144. */
  145. echo '<br><table class="noborder" width="100%">';
  146. /*
  147. * Ajouter une ligne de contact
  148. * Non affiche en mode modification de ligne
  149. */
  150. if ($_GET["action"] != 'editline' && $user->rights->facture->creer)
  151. {
  152. print '<tr class="liste_titre">';
  153. print '<td>'.$langs->trans("Source").'</td>';
  154. print '<td>'.$langs->trans("Company").'</td>';
  155. print '<td>'.$langs->trans("Contacts").'</td>';
  156. print '<td>'.$langs->trans("ContactType").'</td>';
  157. print '<td colspan="3">&nbsp;</td>';
  158. print "</tr>\n";
  159. $var = false;
  160. print '<form action="contact.php?facid='.$id.'" method="post">';
  161. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  162. print '<input type="hidden" name="action" value="addcontact">';
  163. print '<input type="hidden" name="source" value="internal">';
  164. print '<input type="hidden" name="id" value="'.$id.'">';
  165. // Line to add an internal contact
  166. print "<tr ".$bc[$var].">";
  167. print '<td nowrap="nowrap">';
  168. print img_object('','user').' '.$langs->trans("Users");
  169. print '</td>';
  170. print '<td colspan="1">';
  171. print $mysoc->name;
  172. print '</td>';
  173. print '<td colspan="1">';
  174. // Ge get ids of alreadey selected users
  175. //$userAlreadySelected = $facture->getListContactId('internal'); // On ne doit pas desactiver un contact deja selectionner car on doit pouvoir le seclectionner une deuxieme fois pour un autre type
  176. $form->select_users($user->id,'contactid',0,$userAlreadySelected);
  177. print '</td>';
  178. print '<td>';
  179. $formcompany->selectTypeContact($facture, '', 'type','internal');
  180. print '</td>';
  181. print '<td align="right" colspan="3" ><input type="submit" class="button" value="'.$langs->trans("Add").'"></td>';
  182. print '</tr>';
  183. print '</form>';
  184. print '<form action="contact.php?facid='.$id.'" method="post">';
  185. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  186. print '<input type="hidden" name="action" value="addcontact">';
  187. print '<input type="hidden" name="source" value="external">';
  188. print '<input type="hidden" name="id" value="'.$id.'">';
  189. // Line to add an external contact
  190. $var=!$var;
  191. print "<tr ".$bc[$var].">";
  192. print '<td nowrap="nowrap">';
  193. print img_object('','contact').' '.$langs->trans("ThirdPartyContacts");
  194. print '</td>';
  195. print '<td nowrap="nowrap">';
  196. $selectedCompany = isset($_GET["newcompany"])?$_GET["newcompany"]:$facture->client->id;
  197. $selectedCompany = $formcompany->selectCompaniesForNewContact($facture, 'facid', $selectedCompany, $htmlname = 'newcompany');
  198. print '</td>';
  199. print '<td>';
  200. $nbofcontacts=$form->select_contacts($selectedCompany, '', 'contactid');
  201. if ($nbofcontacts == 0) print $langs->trans("NoContactDefined");
  202. print '</td>';
  203. print '<td>';
  204. $formcompany->selectTypeContact($facture, '', 'type','external');
  205. print '</td>';
  206. print '<td align="right" colspan="3"><input type="submit" class="button" value="'.$langs->trans("Add").'"';
  207. if (! $nbofcontacts) print ' disabled="disabled"';
  208. print '></td>';
  209. print '</tr>';
  210. print "</form>";
  211. print '<tr><td colspan="6">&nbsp;</td></tr>';
  212. }
  213. // List of linked contacts
  214. print '<tr class="liste_titre">';
  215. print '<td>'.$langs->trans("Source").'</td>';
  216. print '<td>'.$langs->trans("Company").'</td>';
  217. print '<td>'.$langs->trans("Contacts").'</td>';
  218. print '<td>'.$langs->trans("ContactType").'</td>';
  219. print '<td align="center">'.$langs->trans("Status").'</td>';
  220. print '<td colspan="2">&nbsp;</td>';
  221. print "</tr>\n";
  222. $companystatic = new Societe($db);
  223. $var = true;
  224. foreach(array('internal','external') as $source)
  225. {
  226. $tab = $facture->liste_contact(-1,$source);
  227. $num=count($tab);
  228. $i = 0;
  229. while ($i < $num)
  230. {
  231. $var = !$var;
  232. print '<tr '.$bc[$var].' valign="top">';
  233. // Source
  234. print '<td align="left">';
  235. if ($tab[$i]['source']=='internal') print $langs->trans("User");
  236. if ($tab[$i]['source']=='external') print $langs->trans("ThirdPartyContact");
  237. print '</td>';
  238. // Third party
  239. print '<td align="left">';
  240. if ($tab[$i]['socid'] > 0)
  241. {
  242. $companystatic->fetch($tab[$i]['socid']);
  243. print $companystatic->getNomUrl(1);
  244. }
  245. if ($tab[$i]['socid'] < 0)
  246. {
  247. print $mysoc->name;
  248. }
  249. if (! $tab[$i]['socid'])
  250. {
  251. print '&nbsp;';
  252. }
  253. print '</td>';
  254. // Contact
  255. print '<td>';
  256. if ($tab[$i]['source']=='internal')
  257. {
  258. $userstatic->id=$tab[$i]['id'];
  259. $userstatic->nom=$tab[$i]['nom'];
  260. $userstatic->prenom=$tab[$i]['firstname'];
  261. print $userstatic->getNomUrl(1);
  262. }
  263. if ($tab[$i]['source']=='external')
  264. {
  265. $contactstatic->id=$tab[$i]['id'];
  266. $contactstatic->name=$tab[$i]['nom'];
  267. $contactstatic->firstname=$tab[$i]['firstname'];
  268. print $contactstatic->getNomUrl(1);
  269. }
  270. print '</td>';
  271. // Type of contact
  272. print '<td>'.$tab[$i]['libelle'].'</td>';
  273. // Status
  274. print '<td align="center">';
  275. // Activate/Unactivate contact
  276. if ($facture->statut >= 0) print '<a href="contact.php?facid='.$facture->id.'&amp;action=swapstatut&amp;ligne='.$tab[$i]['rowid'].'">';
  277. print $contactstatic->LibStatut($tab[$i]['status'],3);
  278. if ($facture->statut >= 0) print '</a>';
  279. print '</td>';
  280. // Icon update et delete
  281. print '<td align="center" nowrap>';
  282. if ($user->rights->facture->creer)
  283. {
  284. print '&nbsp;';
  285. print '<a href="contact.php?facid='.$facture->id.'&amp;action=deleteline&amp;lineid='.$tab[$i]['rowid'].'">';
  286. print img_delete();
  287. print '</a>';
  288. }
  289. print '</td>';
  290. print "</tr>\n";
  291. $i ++;
  292. }
  293. }
  294. print "</table>";
  295. }
  296. else
  297. {
  298. // Record not found
  299. print "ErrorRecordNotFound";
  300. }
  301. }
  302. $db->close();
  303. llxFooter();
  304. ?>