attendee_new.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  1. <?php
  2. /* Copyright (C) 2021 Dorian Vabre <dorian.vabre@gmail.com>
  3. * Copyright (C) 2023 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. /**
  19. * \file htdocs/public/eventorganization/attendee_new.php
  20. * \ingroup project
  21. * \brief Example of form to subscribe to an event
  22. */
  23. if (!defined('NOLOGIN')) {
  24. define("NOLOGIN", 1); // This means this output page does not require to be logged.
  25. }
  26. if (!defined('NOCSRFCHECK')) {
  27. define("NOCSRFCHECK", 1); // We accept to go on this page from external web site.
  28. }
  29. if (!defined('NOIPCHECK')) {
  30. define('NOIPCHECK', '1'); // Do not check IP defined into conf $dolibarr_main_restrict_ip
  31. }
  32. if (!defined('NOBROWSERNOTIF')) {
  33. define('NOBROWSERNOTIF', '1');
  34. }
  35. if (!defined('NOIPCHECK')) {
  36. define('NOIPCHECK', '1'); // Do not check IP defined into conf $dolibarr_main_restrict_ip
  37. }
  38. // For MultiCompany module.
  39. // Do not use GETPOST here, function is not defined and define must be done before including main.inc.php
  40. // TODO This should be useless. Because entity must be retrieve from object ref and not from url.
  41. $entity = (!empty($_GET['entity']) ? (int) $_GET['entity'] : (!empty($_POST['entity']) ? (int) $_POST['entity'] : 1));
  42. if (is_numeric($entity)) {
  43. define("DOLENTITY", $entity);
  44. }
  45. // Load Dolibarr environment
  46. require '../../main.inc.php';
  47. require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
  48. require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
  49. require_once DOL_DOCUMENT_ROOT.'/eventorganization/class/conferenceorbooth.class.php';
  50. require_once DOL_DOCUMENT_ROOT.'/eventorganization/class/conferenceorboothattendee.class.php';
  51. require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
  52. require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
  53. require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/paymentterm.class.php';
  54. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php';
  55. require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
  56. require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  57. global $dolibarr_main_url_root;
  58. // Init vars
  59. $errmsg = '';
  60. $errors = array();
  61. $error = 0;
  62. $backtopage = GETPOST('backtopage', 'alpha');
  63. $action = GETPOST('action', 'aZ09');
  64. $email = GETPOST("email");
  65. $societe = GETPOST("societe");
  66. $emailcompany = GETPOST("emailcompany");
  67. $note_public = GETPOST('note_public', "restricthtml");
  68. // Getting id from Post and decoding it
  69. $type = GETPOST('type', 'aZ09');
  70. if ($type == 'conf') {
  71. $id = GETPOST('id', 'int');
  72. } else {
  73. $id = GETPOST('fk_project', 'int') ? GETPOST('fk_project', 'int') : GETPOST('id', 'int');
  74. }
  75. $conference = new ConferenceOrBooth($db);
  76. $project = new Project($db);
  77. if ($type == 'conf') {
  78. $resultconf = $conference->fetch($id);
  79. if ($resultconf < 0) {
  80. print 'Bad value for parameter id';
  81. exit;
  82. }
  83. $resultproject = $project->fetch($conference->fk_project);
  84. if ($resultproject < 0) {
  85. $error++;
  86. $errmsg .= $project->error;
  87. $errors = array_merge($errors, $project->errors);
  88. }
  89. }
  90. $currentnbofattendees = 0;
  91. if ($type == 'global') {
  92. $resultproject = $project->fetch($id);
  93. if ($resultproject < 0) {
  94. $error++;
  95. $errmsg .= $project->error;
  96. $errors = array_merge($errors, $project->errors);
  97. } else {
  98. $sql = "SELECT COUNT(*) as nb FROM ".MAIN_DB_PREFIX."eventorganization_conferenceorboothattendee";
  99. $sql .= " WHERE fk_project = ".((int) $project->id);
  100. $resql = $db->query($sql);
  101. if ($resql) {
  102. $obj = $db->fetch_object($resql);
  103. if ($obj) {
  104. $currentnbofattendees = $obj->nb;
  105. } else {
  106. dol_print_error($db);
  107. }
  108. }
  109. }
  110. }
  111. // Security check
  112. $securekeyreceived = GETPOST('securekey', 'alpha');
  113. $securekeytocompare = dol_hash($conf->global->EVENTORGANIZATION_SECUREKEY.'conferenceorbooth'.$id, 'md5');
  114. // We check if the securekey collected is OK
  115. if ($securekeytocompare != $securekeyreceived) {
  116. print $langs->trans('MissingOrBadSecureKey');
  117. exit;
  118. }
  119. // Load translation files
  120. $langs->loadLangs(array("main", "companies", "install", "other", "eventorganization"));
  121. // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
  122. $hookmanager->initHooks(array('publicnewmembercard', 'globalcard'));
  123. $extrafields = new ExtraFields($db);
  124. $user->loadDefaultValues();
  125. // Security check
  126. if (empty($conf->eventorganization->enabled)) {
  127. httponly_accessforbidden('Module Event organization not enabled');
  128. }
  129. /**
  130. * Show header for new member
  131. *
  132. * @param string $title Title
  133. * @param string $head Head array
  134. * @param int $disablejs More content into html header
  135. * @param int $disablehead More content into html header
  136. * @param array $arrayofjs Array of complementary js files
  137. * @param array $arrayofcss Array of complementary css files
  138. * @return void
  139. */
  140. function llxHeaderVierge($title, $head = "", $disablejs = 0, $disablehead = 0, $arrayofjs = '', $arrayofcss = '')
  141. {
  142. global $user, $conf, $langs, $mysoc;
  143. top_htmlhead($head, $title, $disablejs, $disablehead, $arrayofjs, $arrayofcss); // Show html headers
  144. print '<body id="mainbody" class="publicnewmemberform">';
  145. // Define urllogo
  146. $urllogo = DOL_URL_ROOT.'/theme/common/login_logo.png';
  147. if (!empty($mysoc->logo_small) && is_readable($conf->mycompany->dir_output.'/logos/thumbs/'.$mysoc->logo_small)) {
  148. $urllogo = DOL_URL_ROOT.'/viewimage.php?cache=1&amp;modulepart=mycompany&amp;file='.urlencode('logos/thumbs/'.$mysoc->logo_small);
  149. } elseif (!empty($mysoc->logo) && is_readable($conf->mycompany->dir_output.'/logos/'.$mysoc->logo)) {
  150. $urllogo = DOL_URL_ROOT.'/viewimage.php?cache=1&amp;modulepart=mycompany&amp;file='.urlencode('logos/'.$mysoc->logo);
  151. } elseif (is_readable(DOL_DOCUMENT_ROOT.'/theme/dolibarr_logo.svg')) {
  152. $urllogo = DOL_URL_ROOT.'/theme/dolibarr_logo.svg';
  153. }
  154. print '<div class="center">';
  155. // Output html code for logo
  156. if ($urllogo) {
  157. print '<div class="backgreypublicpayment">';
  158. print '<div class="logopublicpayment">';
  159. print '<img id="dolpaymentlogo" src="'.$urllogo.'"';
  160. print '>';
  161. print '</div>';
  162. if (empty($conf->global->MAIN_HIDE_POWERED_BY)) {
  163. print '<div class="poweredbypublicpayment opacitymedium right"><a class="poweredbyhref" href="https://www.dolibarr.org?utm_medium=website&utm_source=poweredby" target="dolibarr" rel="noopener">'.$langs->trans("PoweredBy").'<br><img class="poweredbyimg" src="'.DOL_URL_ROOT.'/theme/dolibarr_logo.svg" width="80px"></a></div>';
  164. }
  165. print '</div>';
  166. }
  167. if (!empty($conf->global->EVENTORGANIZATION_IMAGE_PUBLIC_INTERFACE)) {
  168. print '<div class="backimagepubliceventorganizationsubscription">';
  169. print '<img id="idEVENTORGANIZATION_IMAGE_PUBLIC_INTERFACE" src="'.$conf->global->EVENTORGANIZATION_IMAGE_PUBLIC_INTERFACE.'">';
  170. print '</div>';
  171. }
  172. print '</div>';
  173. print '<div class="divmainbodylarge">';
  174. }
  175. /**
  176. * Show footer for new member
  177. *
  178. * @return void
  179. */
  180. function llxFooterVierge()
  181. {
  182. print '</div>';
  183. printCommonFooter('public');
  184. print "</body>\n";
  185. print "</html>\n";
  186. }
  187. /*
  188. * Actions
  189. */
  190. $parameters = array();
  191. // Note that $action and $object may have been modified by some hooks
  192. $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action);
  193. if ($reshook < 0) {
  194. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  195. }
  196. // Action called when page is submitted
  197. if (empty($reshook) && $action == 'add' && (!empty($conference->id) && $conference->status==2 || !empty($project->id) && $project->status == Project::STATUS_VALIDATED)) {
  198. $error = 0;
  199. $urlback = '';
  200. $db->begin();
  201. if (!GETPOST("email")) {
  202. $error++;
  203. $errmsg .= $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Email"))."<br>\n";
  204. }
  205. // If the price has been set, name is required for the invoice
  206. if (!GETPOST("societe") && !empty(floatval($project->price_registration))) {
  207. $error++;
  208. $errmsg .= $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Company"))."<br>\n";
  209. }
  210. if (GETPOST("email") && !isValidEmail(GETPOST("email"))) {
  211. $error++;
  212. $langs->load("errors");
  213. $errmsg .= $langs->trans("ErrorBadEMail", GETPOST("email"))."<br>\n";
  214. }
  215. if (!GETPOST("country_id")) {
  216. $error++;
  217. $errmsg .= $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Country"))."<br>\n";
  218. }
  219. if (!$error) {
  220. // Check if attendee already exists (by email and for this event)
  221. $confattendee = new ConferenceOrBoothAttendee($db);
  222. $filter = array();
  223. if ($type == 'global') {
  224. $filter = array('t.fk_project'=>((int) $id), 'customsql'=>'t.email="'.$db->escape($email).'"');
  225. }
  226. if ($type == 'conf') {
  227. $filter = array('t.fk_actioncomm'=>((int) $id), 'customsql'=>'t.email="'.$db->escape($email).'"');
  228. }
  229. // Check if there is already an attendee into table eventorganization_conferenceorboothattendee for same event (or conference/booth)
  230. $resultfetchconfattendee = $confattendee->fetchAll('', '', 0, 0, $filter);
  231. if (is_array($resultfetchconfattendee) && count($resultfetchconfattendee) > 0) {
  232. // Found confattendee
  233. $confattendee = array_shift($resultfetchconfattendee);
  234. } else {
  235. // Need to create a confattendee
  236. $confattendee->date_creation = dol_now();
  237. $confattendee->date_subscription = dol_now();
  238. $confattendee->email = $email;
  239. $confattendee->fk_project = $project->id;
  240. $confattendee->fk_actioncomm = $id;
  241. $confattendee->note_public = $note_public;
  242. $confattendee->ip = getUserRemoteIP();
  243. $nb_post_max = getDolGlobalInt("MAIN_SECURITY_MAX_POST_ON_PUBLIC_PAGES_BY_IP_ADDRESS", 200);
  244. $now = dol_now();
  245. $minmonthpost = dol_time_plus_duree($now, -1, "m");
  246. // Calculate nb of post for IP
  247. $nb_post_ip = 0;
  248. if ($nb_post_max > 0) { // Calculate only if there is a limit to check
  249. $sql = "SELECT COUNT(ref) as nb_attendee";
  250. $sql .= " FROM ".MAIN_DB_PREFIX."eventorganization_conferenceorboothattendee";
  251. $sql .= " WHERE ip = '".$db->escape($confattendee->ip)."'";
  252. $sql .= " AND date_creation > '".$db->idate($minmonthpost)."'";
  253. $resql = $db->query($sql);
  254. if ($resql) {
  255. $num = $db->num_rows($resql);
  256. $i = 0;
  257. while ($i < $num) {
  258. $i++;
  259. $obj = $db->fetch_object($resql);
  260. $nb_post_ip = $obj->nb_attendee;
  261. }
  262. }
  263. }
  264. $resultconforbooth = -1;
  265. if ($nb_post_max > 0 && $nb_post_ip >= $nb_post_max) {
  266. $error++;
  267. $errmsg .= $langs->trans("AlreadyTooMuchPostOnThisIPAdress");
  268. array_push($confattendee->errors, $langs->trans("AlreadyTooMuchPostOnThisIPAdress"));
  269. setEventMessage($errmsg, 'errors');
  270. } else {
  271. $resultconfattendee = $confattendee->create($user);
  272. }
  273. if ($resultconfattendee < 0) {
  274. $error++;
  275. $errmsg .= $confattendee->error;
  276. $errors = array_merge($errors, $confattendee->errors);
  277. }
  278. }
  279. // At this point, we have an existing $confattendee. It may not be linked to a thirdparty.
  280. //var_dump($confattendee);
  281. // If the registration has already been paid for this attendee
  282. if (!empty($confattendee->date_subscription) && !empty($confattendee->amount)) {
  283. $securekeyurl = dol_hash($conf->global->EVENTORGANIZATION_SECUREKEY.'conferenceorbooth'.$id, 'master');
  284. $redirection = $dolibarr_main_url_root.'/public/eventorganization/subscriptionok.php?id='.((int) $id).'&securekey='.urlencode($securekeyurl);
  285. $mesg = $langs->trans("RegistrationAndPaymentWereAlreadyRecorded", $email);
  286. setEventMessages($mesg, null, 'mesgs');
  287. $db->commit();
  288. Header("Location: ".$redirection);
  289. exit;
  290. }
  291. $resultfetchthirdparty = 0;
  292. $genericcompanyname = $langs->trans('EventParticipant').' '.($emailcompany ? $emailcompany : $email); // Keep this label simple so we can retreive same thirdparty for another event
  293. // Getting the thirdparty or creating it
  294. $thirdparty = new Societe($db);
  295. $contact = new Contact($db);
  296. // Fetch using fk_soc if the attendee was already found
  297. if (!empty($confattendee->fk_soc) && $confattendee->fk_soc > 0) {
  298. $resultfetchthirdparty = $thirdparty->fetch($confattendee->fk_soc);
  299. } else {
  300. if (empty($conf->global->EVENTORGANIZATION_DISABLE_RETREIVE_THIRDPARTY_FROM_NAME)) {
  301. // Fetch using the field input by end user if we have just created the attendee
  302. if ($resultfetchthirdparty <= 0 && !empty($societe) && !empty($emailcompany)) {
  303. $resultfetchthirdparty = $thirdparty->fetch('', $societe, '', '', '', '', '', '', '', '', $emailcompany);
  304. if ($resultfetchthirdparty > 0) {
  305. // We found a unique result with the name + emailcompany, so we set the fk_soc of attendee
  306. $confattendee->fk_soc = $thirdparty->id;
  307. $confattendee->update($user);
  308. } elseif ($resultfetchthirdparty == -2) {
  309. $thirdparty->error = $langs->trans("ErrorSeveralCompaniesWithNameContactUs", $mysoc->email);
  310. }
  311. }
  312. // Fetch using the field input by end user if we have just created the attendee
  313. if ($resultfetchthirdparty <= 0 && !empty($societe) && !empty($email) && $email != $emailcompany) {
  314. $resultfetchthirdparty = $thirdparty->fetch('', $societe, '', '', '', '', '', '', '', '', $email);
  315. if ($resultfetchthirdparty > 0) {
  316. // We found a unique result with the name + email, so we set the fk_soc of attendee
  317. $confattendee->fk_soc = $thirdparty->id;
  318. $confattendee->update($user);
  319. } elseif ($resultfetchthirdparty == -2) {
  320. $thirdparty->error = $langs->trans("ErrorSeveralCompaniesWithNameContactUs", $mysoc->email);
  321. }
  322. }
  323. }
  324. if ($resultfetchthirdparty <= 0 && !empty($emailcompany)) {
  325. // Try to find thirdparty from the email only
  326. $resultfetchthirdparty = $thirdparty->fetch('', '', '', '', '', '', '', '', '', '', $emailcompany);
  327. if ($resultfetchthirdparty > 0) {
  328. // We found a unique result with that email only, so we set the fk_soc of attendee
  329. $confattendee->fk_soc = $thirdparty->id;
  330. $confattendee->update($user);
  331. } elseif ($resultfetchthirdparty == -2) {
  332. $thirdparty->error = $langs->trans("ErrorSeveralCompaniesWithEmailContactUs", $mysoc->email);
  333. }
  334. }
  335. if ($resultfetchthirdparty <= 0 && !empty($email) && $email != $emailcompany) {
  336. // Try to find thirdparty from the email only
  337. $resultfetchthirdparty = $thirdparty->fetch('', '', '', '', '', '', '', '', '', '', $email);
  338. if ($resultfetchthirdparty > 0) {
  339. // We found a unique result with that email only, so we set the fk_soc of attendee
  340. $confattendee->fk_soc = $thirdparty->id;
  341. $confattendee->update($user);
  342. } elseif ($resultfetchthirdparty == -2) {
  343. $thirdparty->error = $langs->trans("ErrorSeveralCompaniesWithEmailContactUs", $mysoc->email);
  344. }
  345. }
  346. if ($resultfetchthirdparty <= 0 && !empty($genericcompanyname)) {
  347. // Try to find thirdparty from the generic mail only
  348. $resultfetchthirdparty = $thirdparty->fetch('', $genericcompanyname, '', '', '', '', '', '', '', '', '');
  349. if ($resultfetchthirdparty > 0) {
  350. // We found a unique result with that name + email, so we set the fk_soc of attendee
  351. $confattendee->fk_soc = $thirdparty->id;
  352. $confattendee->update($user);
  353. } elseif ($resultfetchthirdparty == -2) {
  354. $thirdparty->error = $langs->trans("ErrorSeveralCompaniesWithNameContactUs", $mysoc->email);
  355. }
  356. }
  357. // TODO Add more tests on a VAT number, profid or a name ?
  358. if ($resultfetchthirdparty <= 0 && !empty($email)) {
  359. // Try to find the thirdparty from the contact
  360. $resultfetchcontact = $contact->fetch('', null, '', $email);
  361. if ($resultfetchcontact > 0 && $contact->fk_soc > 0) {
  362. $thirdparty->fetch($contact->fk_soc);
  363. $confattendee->fk_soc = $thirdparty->id;
  364. $confattendee->update($user);
  365. $resultfetchthirdparty = 1;
  366. }
  367. }
  368. if ($resultfetchthirdparty <= 0 && !empty($societe)) {
  369. // Try to find thirdparty from the company name only
  370. $resultfetchthirdparty = $thirdparty->fetch('', $societe, '', '', '', '', '', '', '', '', '');
  371. if ($resultfetchthirdparty > 0) {
  372. // We found a unique result with that name only, so we set the fk_soc of attendee
  373. $confattendee->fk_soc = $thirdparty->id;
  374. $confattendee->update($user);
  375. } elseif ($resultfetchthirdparty == -2) {
  376. $thirdparty->error = "ErrorSeveralCompaniesWithNameContactUs";
  377. }
  378. }
  379. }
  380. // If price is empty, no need to create a thirdparty, so we force $resultfetchthirdparty as if we have already found thirdp party.
  381. if (empty(floatval($project->price_registration))) {
  382. $resultfetchthirdparty = 1;
  383. }
  384. if ($resultfetchthirdparty < 0) {
  385. // If an error was found
  386. $error++;
  387. $errmsg .= $thirdparty->error;
  388. $errors = array_merge($errors, $thirdparty->errors);
  389. } elseif ($resultfetchthirdparty == 0) { // No thirdparty found + a payment is expected
  390. // Creation of a new thirdparty
  391. if (!empty($societe)) {
  392. $thirdparty->name = $societe;
  393. } else {
  394. $thirdparty->name = $genericcompanyname;
  395. }
  396. $thirdparty->address = GETPOST("address");
  397. $thirdparty->zip = GETPOST("zipcode");
  398. $thirdparty->town = GETPOST("town");
  399. $thirdparty->client = $thirdparty::PROSPECT;
  400. $thirdparty->fournisseur = 0;
  401. $thirdparty->country_id = GETPOST("country_id", 'int');
  402. $thirdparty->state_id = GETPOST("state_id", 'int');
  403. $thirdparty->email = ($emailcompany ? $emailcompany : $email);
  404. // Load object modCodeTiers
  405. $module = (!empty($conf->global->SOCIETE_CODECLIENT_ADDON) ? $conf->global->SOCIETE_CODECLIENT_ADDON : 'mod_codeclient_leopard');
  406. if (substr($module, 0, 15) == 'mod_codeclient_' && substr($module, -3) == 'php') {
  407. $module = substr($module, 0, dol_strlen($module) - 4);
  408. }
  409. $dirsociete = array_merge(array('/core/modules/societe/'), $conf->modules_parts['societe']);
  410. foreach ($dirsociete as $dirroot) {
  411. $res = dol_include_once($dirroot.$module.'.php');
  412. if ($res) {
  413. break;
  414. }
  415. }
  416. $modCodeClient = new $module($db);
  417. if (empty($tmpcode) && !empty($modCodeClient->code_auto)) {
  418. $tmpcode = $modCodeClient->getNextValue($thirdparty, 0);
  419. }
  420. $thirdparty->code_client = $tmpcode;
  421. $readythirdparty = $thirdparty->create($user);
  422. if ($readythirdparty < 0) {
  423. $error++;
  424. $errmsg .= $thirdparty->error;
  425. $errors = array_merge($errors, $thirdparty->errors);
  426. } else {
  427. $thirdparty->country_code = getCountry($thirdparty->country_id, 2, $db, $langs);
  428. $thirdparty->country = getCountry($thirdparty->country_code, 0, $db, $langs);
  429. // Update attendee country to match country of thirdparty
  430. $confattendee->fk_soc = $thirdparty->id;
  431. $confattendee->update($user);
  432. }
  433. }
  434. }
  435. if (!$error) {
  436. // If the registration needs a payment
  437. if (!empty(floatval($project->price_registration))) {
  438. $outputlangs = $langs;
  439. // TODO Use default language of $thirdparty->default_lang to build $outputlang
  440. // Get product to use for invoice
  441. $productforinvoicerow = new Product($db);
  442. $productforinvoicerow->id = 0;
  443. $resultprod = 0;
  444. if ($conf->global->SERVICE_CONFERENCE_ATTENDEE_SUBSCRIPTION > 0) {
  445. $resultprod = $productforinvoicerow->fetch($conf->global->SERVICE_CONFERENCE_ATTENDEE_SUBSCRIPTION);
  446. }
  447. // Create the draft invoice for the payment
  448. if ($resultprod < 0) {
  449. $error++;
  450. $errmsg .= $productforinvoicerow->error;
  451. $errors = array_merge($errors, $productforinvoicerow->errors);
  452. } else {
  453. $facture = new Facture($db);
  454. if (empty($confattendee->fk_invoice)) {
  455. $facture->type = Facture::TYPE_STANDARD;
  456. $facture->socid = $thirdparty->id;
  457. $facture->paye = 0;
  458. $facture->date = dol_now();
  459. $facture->cond_reglement_id = $confattendee->cond_reglement_id;
  460. $facture->fk_project = $project->id;
  461. $facture->status = Facture::STATUS_DRAFT;
  462. if (empty($facture->cond_reglement_id)) {
  463. $paymenttermstatic = new PaymentTerm($confattendee->db);
  464. $facture->cond_reglement_id = $paymenttermstatic->getDefaultId();
  465. if (empty($facture->cond_reglement_id)) {
  466. $error++;
  467. $confattendee->error = 'ErrorNoPaymentTermRECEPFound';
  468. $confattendee->errors[] = $confattendee->error;
  469. }
  470. }
  471. $resultfacture = $facture->create($user);
  472. if ($resultfacture <= 0) {
  473. $confattendee->error = $facture->error;
  474. $confattendee->errors = $facture->errors;
  475. $error++;
  476. } else {
  477. $confattendee->fk_invoice = $resultfacture;
  478. $confattendee->update($user);
  479. }
  480. } else {
  481. $facture->fetch($confattendee->fk_invoice);
  482. }
  483. // Add link between invoice and the attendee registration
  484. /*if (!$error) {
  485. $facture->add_object_linked($confattendee->element, $confattendee->id);
  486. }*/
  487. }
  488. if (!$error) {
  489. // Add line to draft invoice
  490. $vattouse = get_default_tva($mysoc, $thirdparty, $productforinvoicerow->id);
  491. $labelforproduct = $outputlangs->trans("EventFee", $project->title);
  492. if ($project->location) {
  493. $labelforproduct .= ' - '.$project->location;
  494. }
  495. $date_start = $project->date_start_event;
  496. $date_end = $project->date_end_event;
  497. // If there is no lines yet, we add one
  498. if (empty($facture->lines)) {
  499. $pu_ttc = floatval($project->price_registration);
  500. $pu_ht = 0;
  501. $price_base_type = 'TTC';
  502. $result = $facture->addline($labelforproduct, $pu_ht, 1, $vattouse, 0, 0, $productforinvoicerow->id, 0, $date_start, $date_end, 0, 0, '', $price_base_type, $pu_ttc, 1);
  503. if ($result <= 0) {
  504. $confattendee->error = $facture->error;
  505. $confattendee->errors = $facture->errors;
  506. $error++;
  507. }
  508. }
  509. }
  510. if (!$error) {
  511. $db->commit();
  512. // Registration was recorded and invoice was generated, but payment not yet done.
  513. // TODO
  514. // Send an email to says registration shas been received and that we are waiting for the payment.
  515. // Should send email template (EventOrganizationEmailRegistrationEvent) saved into conf EVENTORGANIZATION_TEMPLATE_EMAIL_REGISTRATION_EVENT.
  516. // Now we redirect to the payment page
  517. $sourcetouse = 'organizedeventregistration';
  518. $reftouse = $facture->id;
  519. $redirection = $dolibarr_main_url_root.'/public/payment/newpayment.php?source='.urlencode($sourcetouse).'&ref='.urlencode($reftouse);
  520. if (!empty($conf->global->PAYMENT_SECURITY_TOKEN)) {
  521. if (!empty($conf->global->PAYMENT_SECURITY_TOKEN_UNIQUE)) {
  522. $redirection .= '&securekey='.dol_hash($conf->global->PAYMENT_SECURITY_TOKEN . $sourcetouse . $reftouse, 2); // Use the source in the hash to avoid duplicates if the references are identical
  523. } else {
  524. $redirection .= '&securekey='.urlencode($conf->global->PAYMENT_SECURITY_TOKEN);
  525. }
  526. }
  527. Header("Location: ".$redirection);
  528. exit;
  529. } else {
  530. $db->rollback();
  531. }
  532. } else {
  533. $db->commit();
  534. // No price has been set
  535. // Validating the subscription
  536. $confattendee->setStatut(1);
  537. // Sending mail
  538. require_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
  539. include_once DOL_DOCUMENT_ROOT.'/core/class/html.formmail.class.php';
  540. $formmail = new FormMail($db);
  541. // Set output language
  542. $outputlangs = new Translate('', $conf);
  543. $outputlangs->setDefaultLang(empty($thirdparty->default_lang) ? $mysoc->default_lang : $thirdparty->default_lang);
  544. // Load traductions files required by page
  545. $outputlangs->loadLangs(array("main", "members"));
  546. // Get email content from template
  547. $arraydefaultmessage = null;
  548. $labeltouse = $conf->global->EVENTORGANIZATION_TEMPLATE_EMAIL_AFT_SUBS_EVENT;
  549. if (!empty($labeltouse)) {
  550. $arraydefaultmessage = $formmail->getEMailTemplate($db, 'eventorganization_send', $user, $outputlangs, $labeltouse, 1, '');
  551. }
  552. if (!empty($labeltouse) && is_object($arraydefaultmessage) && $arraydefaultmessage->id > 0) {
  553. $subject = $arraydefaultmessage->topic;
  554. $msg = $arraydefaultmessage->content;
  555. }
  556. $substitutionarray = getCommonSubstitutionArray($outputlangs, 0, null, $thirdparty);
  557. complete_substitutions_array($substitutionarray, $outputlangs, $object);
  558. $subjecttosend = make_substitutions($subject, $substitutionarray, $outputlangs);
  559. $texttosend = make_substitutions($msg, $substitutionarray, $outputlangs);
  560. $sendto = $thirdparty->email;
  561. $from = $conf->global->MAILING_EMAIL_FROM;
  562. $urlback = $_SERVER["REQUEST_URI"];
  563. $ishtml = dol_textishtml($texttosend); // May contain urls
  564. $mailfile = new CMailFile($subjecttosend, $sendto, $from, $texttosend, array(), array(), array(), '', '', 0, $ishtml);
  565. $result = $mailfile->sendfile();
  566. if ($result) {
  567. dol_syslog("EMail sent to ".$sendto, LOG_DEBUG, 0, '_payment');
  568. } else {
  569. dol_syslog("Failed to send EMail to ".$sendto, LOG_ERR, 0, '_payment');
  570. }
  571. $securekeyurl = dol_hash($conf->global->EVENTORGANIZATION_SECUREKEY.'conferenceorbooth'.$id, 2);
  572. $redirection = $dolibarr_main_url_root.'/public/eventorganization/subscriptionok.php?id='.((int) $id).'&securekey='.urlencode($securekeyurl);
  573. Header("Location: ".$redirection);
  574. exit;
  575. }
  576. //Header("Location: ".$urlback);
  577. //exit;
  578. } else {
  579. $db->rollback();
  580. }
  581. }
  582. /*
  583. * View
  584. */
  585. $form = new Form($db);
  586. $formcompany = new FormCompany($db);
  587. llxHeaderVierge($langs->trans("NewRegistration"));
  588. print '<div align="center">';
  589. print '<div id="divsubscribe">';
  590. // Sub banner
  591. print '<div class="center subscriptionformbanner subbanner justify margintoponly paddingtop marginbottomonly padingbottom">';
  592. print load_fiche_titre($langs->trans("NewRegistration"), '', '', 0, 0, 'center');
  593. // Welcome message
  594. print '<span class="opacitymedium">'.$langs->trans("EvntOrgWelcomeMessage").'</span>';
  595. print '<br>';
  596. // Title
  597. print '<span class="eventlabel large">'.dol_escape_htmltag($project->title . ' '. $conference->label).'</span><br>';
  598. print '</div>';
  599. // Help text
  600. print '<div class="justify subscriptionformhelptext">';
  601. if ($project->date_start_event || $project->date_end_event) {
  602. print '<br><span class="fa fa-calendar pictofixedwidth opacitymedium"></span>';
  603. }
  604. if ($project->date_start_event) {
  605. $format = 'day';
  606. $tmparray = dol_getdate($project->date_start_event, false, '');
  607. if ($tmparray['hours'] || $tmparray['minutes'] || $tmparray['minutes']) {
  608. $format = 'dayhour';
  609. }
  610. print dol_print_date($project->date_start_event, $format);
  611. }
  612. if ($project->date_start_event && $project->date_end_event) {
  613. print ' - ';
  614. }
  615. if ($project->date_end_event) {
  616. $format = 'day';
  617. $tmparray = dol_getdate($project->date_end_event, false, '');
  618. if ($tmparray['hours'] || $tmparray['minutes'] || $tmparray['minutes']) {
  619. $format = 'dayhour';
  620. }
  621. print dol_print_date($project->date_end_event, $format);
  622. }
  623. if ($project->date_start_event || $project->date_end_event) {
  624. print '<br>';
  625. }
  626. if ($project->location) {
  627. print '<span class="fa fa-map-marked-alt pictofixedwidth opacitymedium"></span>'.dol_escape_htmltag($project->location).'<br>';
  628. }
  629. print '</div>';
  630. $maxattendees = 0;
  631. if ($conference->id > 0) {
  632. /* date of project is not date of event so commented
  633. print $langs->trans("Date").': ';
  634. print dol_print_date($conference->datep);
  635. if ($conference->date_end) {
  636. print ' - ';
  637. print dol_print_date($conference->datef);
  638. }*/
  639. } else {
  640. /* date of project is not date of event so commented
  641. print $langs->trans("Date").': ';
  642. print dol_print_date($project->date_start);
  643. if ($project->date_end) {
  644. print ' - ';
  645. print dol_print_date($project->date_end);
  646. }*/
  647. $maxattendees = $project->max_attendees; // Max attendeed for the project/event
  648. }
  649. if ($maxattendees && $currentnbofattendees >= $maxattendees) {
  650. print '<br>';
  651. print '<div class="warning">'.$langs->trans("MaxNbOfAttendeesReached").'</div>';
  652. print '<br>';
  653. }
  654. dol_htmloutput_errors($errmsg, $errors);
  655. if ((!empty($conference->id) && $conference->status == ConferenceOrBooth::STATUS_CONFIRMED) || (!empty($project->id) && $project->status == Project::STATUS_VALIDATED)) {
  656. if (empty($maxattendees) || $currentnbofattendees < $maxattendees) {
  657. // Print form
  658. print '<form action="' . $_SERVER["PHP_SELF"] . '" method="POST" name="newmember">' . "\n";
  659. print '<input type="hidden" name="token" value="' . newToken() . '" / >';
  660. print '<input type="hidden" name="entity" value="' . $entity . '" />';
  661. print '<input type="hidden" name="action" value="add" />';
  662. print '<input type="hidden" name="type" value="' . $type . '" />';
  663. print '<input type="hidden" name="id" value="' . $conference->id . '" />';
  664. print '<input type="hidden" name="fk_project" value="' . $project->id . '" />';
  665. print '<input type="hidden" name="securekey" value="' . $securekeyreceived . '" />';
  666. print '<br>';
  667. print '<br>';
  668. //print '<span class="opacitymedium">' . $langs->trans("FieldsWithAreMandatory", '*') . '</span><br>';
  669. //print $langs->trans("FieldsWithIsForPublic",'**').'<br>';
  670. print dol_get_fiche_head('');
  671. print '<script type="text/javascript">
  672. jQuery(document).ready(function () {
  673. jQuery(document).ready(function () {
  674. jQuery("#selectcountry_id").change(function() {
  675. document.newmember.action.value="create";
  676. document.newmember.submit();
  677. });
  678. });
  679. });
  680. </script>';
  681. print '<table class="border" summary="form to subscribe" id="tablesubscribe">' . "\n";
  682. // Email
  683. print '<tr><td><span class="fieldrequired">' . $langs->trans("EmailAttendee") . '</span></td><td>';
  684. print img_picto('', 'email', 'class="pictofixedwidth"');
  685. print '<input type="text" name="email" maxlength="255" class="minwidth200 widthcentpercentminusx maxwidth300" value="' . dol_escape_htmltag(GETPOST('email')) . '" required></td></tr>' . "\n";
  686. // Company
  687. print '<tr id="trcompany" class="trcompany"><td>';
  688. if (!empty(floatval($project->price_registration))) {
  689. print '<span class="fieldrequired">';
  690. }
  691. print $langs->trans("Company");
  692. if (!empty(floatval($project->price_registration))) {
  693. print '</span>';
  694. }
  695. print '</td><td>';
  696. print img_picto('', 'company', 'class="pictofixedwidth"');
  697. print '<input type="text" name="societe" class="minwidth200 widthcentpercentminusx maxwidth300" value="' . dol_escape_htmltag(GETPOST('societe')) . '"'.(empty(floatval($project->price_registration)) ? '' : ' required').'></td></tr>' . "\n";
  698. // Email company for invoice
  699. if ($project->price_registration) {
  700. print '<tr><td>' . $form->textwithpicto($langs->trans("EmailCompany"), $langs->trans("EmailCompanyForInvoice")) . '</td><td>';
  701. print img_picto('', 'email', 'class="pictofixedwidth"');
  702. print '<input type="text" name="emailcompany" maxlength="255" class="minwidth200 widthcentpercentminusx maxwidth300" value="' . dol_escape_htmltag(GETPOST('emailcompany')) . '"></td></tr>' . "\n";
  703. }
  704. // Address
  705. print '<tr><td>' . $langs->trans("Address") . '</td><td>' . "\n";
  706. print '<textarea name="address" id="address" wrap="soft" class="centpercent" rows="' . ROWS_2 . '">' . dol_escape_htmltag(GETPOST('address', 'restricthtml'), 0, 1) . '</textarea></td></tr>' . "\n";
  707. // Zip / Town
  708. print '<tr><td>' . $langs->trans('Zip') . ' / ' . $langs->trans('Town') . '</td><td>';
  709. print $formcompany->select_ziptown(GETPOST('zipcode'), 'zipcode', array('town', 'selectcountry_id', 'state_id'), 6, 1);
  710. print ' / ';
  711. print $formcompany->select_ziptown(GETPOST('town'), 'town', array('zipcode', 'selectcountry_id', 'state_id'), 0, 1);
  712. print '</td></tr>';
  713. // Country
  714. print '<tr><td><span class="fieldrequired">'.$langs->trans('Country').'</span></td><td>';
  715. print img_picto('', 'country', 'class="pictofixedwidth"');
  716. $country_id = GETPOST('country_id');
  717. if (!$country_id && !empty($conf->global->MEMBER_NEWFORM_FORCECOUNTRYCODE)) {
  718. $country_id = getCountry($conf->global->MEMBER_NEWFORM_FORCECOUNTRYCODE, 2, $db, $langs);
  719. }
  720. if (!$country_id && !empty($conf->geoipmaxmind->enabled)) {
  721. $country_code = dol_user_country();
  722. //print $country_code;
  723. if ($country_code) {
  724. $new_country_id = getCountry($country_code, 3, $db, $langs);
  725. //print 'xxx'.$country_code.' - '.$new_country_id;
  726. if ($new_country_id) {
  727. $country_id = $new_country_id;
  728. }
  729. }
  730. }
  731. $country_code = getCountry($country_id, 2, $db, $langs);
  732. print $form->select_country($country_id, 'country_id', '', 0, 'minwidth200 widthcentpercentminusx maxwidth300');
  733. print '</td></tr>';
  734. // State
  735. if (empty($conf->global->SOCIETE_DISABLE_STATE)) {
  736. print '<tr><td>' . $langs->trans('State') . '</td><td>';
  737. if ($country_code) {
  738. print img_picto('', 'state', 'class="pictofixedwidth"');
  739. print $formcompany->select_state(GETPOST("state_id"), $country_code);
  740. } else {
  741. print '';
  742. }
  743. print '</td></tr>';
  744. }
  745. if ($project->price_registration) {
  746. print '<tr><td>' . $langs->trans('Price') . '</td><td>';
  747. print '<span class="amount price-registration">'.price($project->price_registration, 1, $langs, 1, -1, -1, $conf->currency).'</span>';
  748. print '</td></tr>';
  749. }
  750. $notetoshow = $note_public;
  751. print '<tr><td>' . $langs->trans('Note') . '</td><td>';
  752. if (!empty($conf->global->EVENTORGANIZATION_DEFAULT_NOTE_ON_REGISTRATION)) {
  753. $notetoshow = str_replace('\n', "\n", $conf->global->EVENTORGANIZATION_DEFAULT_NOTE_ON_REGISTRATION);
  754. }
  755. print '<textarea name="note_public" class="centpercent" rows="'.ROWS_9.'">'.dol_escape_htmltag($notetoshow, 0, 1).'</textarea>';
  756. print '</td></tr>';
  757. print "</table>\n";
  758. print dol_get_fiche_end();
  759. // Save
  760. print '<div class="center">';
  761. print '<input type="submit" value="' . $langs->trans("Submit") . '" id="submitsave" class="button">';
  762. if (!empty($backtopage)) {
  763. print ' &nbsp; &nbsp; <input type="submit" value="' . $langs->trans("Cancel") . '" id="submitcancel" class="button button-cancel">';
  764. }
  765. print '</div>';
  766. print "</form>\n";
  767. print "<br>";
  768. }
  769. } else {
  770. print '<br><br>';
  771. print $langs->trans("ConferenceIsNotConfirmed");
  772. print '<br><br>';
  773. }
  774. print '</div></div>';
  775. llxFooterVierge();
  776. $db->close();