67 global $conf, $langs, $user;
71 if ($managedfor !=
'member') {
76 $MAXPERCALL = (empty($conf->global->PARTNERSHIP_MAX_EXPIRATION_CANCEL_PER_CALL) ? 25 : $conf->global->PARTNERSHIP_MAX_EXPIRATION_CANCEL_PER_CALL);
78 $langs->loadLangs(array(
"partnership",
"member"));
84 $partnershipsprocessed = array();
86 $gracedelay = $conf->global->PARTNERSHIP_NBDAYS_AFTER_MEMBER_EXPIRATION_BEFORE_CANCEL;
87 if ($gracedelay < 1) {
88 $this->error =
'BadValueForDelayBeforeCancelCheckSetup';
92 dol_syslog(get_class($this).
"::doCancelStatusOfMemberPartnership cancel expired partnerships with grace delay of ".$gracedelay);
99 $sql =
"SELECT p.rowid, p.fk_member, p.status";
100 $sql .=
", d.datefin, d.fk_adherent_type, dty.subscription";
101 $sql .=
" FROM ".MAIN_DB_PREFIX.
"partnership as p";
102 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"adherent as d on (d.rowid = p.fk_member)";
103 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"adherent_type as dty on (dty.rowid = d.fk_adherent_type)";
104 $sql .=
" WHERE fk_member > 0";
105 $sql .=
" AND (d.datefin < '".$this->db->idate($datetotest).
"' AND dty.subscription = 1)";
106 $sql .=
" AND p.status = ".((int) $partnership::STATUS_APPROVED);
107 $sql .= $this->db->order(
'd.rowid',
'ASC');
110 $resql = $this->db->query($sql);
112 $numofexpiredmembers = $this->db->num_rows($resql);
114 $somethingdoneonpartnership = 0;
116 while ($ifetchpartner < $numofexpiredmembers) {
119 $obj = $this->db->fetch_object($resql);
121 if (!empty($partnershipsprocessed[$obj->rowid]))
continue;
123 if ($somethingdoneonpartnership >= $MAXPERCALL) {
124 dol_syslog(
"We reach the limit of ".$MAXPERCALL.
" partnership processed, so we quit loop for this batch doCancelStatusOfMemberPartnership to avoid to reach email quota.", LOG_WARNING);
129 $object->fetch($obj->rowid);
132 $expirationdate = $obj->datefin;
134 if ($expirationdate && $expirationdate < $now) {
135 $somethingdoneonpartnership++;
137 $result = $object->cancel($user, 0);
141 $this->error = $object->error;
142 if (is_array($object->errors) && count($object->errors)) {
143 if (is_array($this->errors)) $this->errors = array_merge($this->errors, $object->errors);
144 else $this->errors = $object->errors;
147 $partnershipsprocessed[$object->id] = $object->ref;
150 $labeltemplate =
'('.getDolGlobalString(
'PARTNERSHIP_SENDMAIL_IF_AUTO_CANCEL',
'SendingEmailOnPartnershipCanceled').
')';
152 dol_syslog(
"Now we will send an email to member id=".$object->fk_member.
" with label ".$labeltemplate);
155 include_once DOL_DOCUMENT_ROOT.
'/core/class/html.formmail.class.php';
156 include_once DOL_DOCUMENT_ROOT.
'/core/class/CMailFile.class.php';
157 $formmail =
new FormMail($this->db);
160 $outputlangs = $langs;
163 if (!empty($newlang)) {
165 $outputlangs->setDefaultLang($newlang);
166 $outputlangs->loadLangs(array(
'main',
'member',
'partnership'));
169 $arraydefaultmessage = $formmail->getEMailTemplate($this->db,
'partnership_send', $user, $outputlangs, 0, 1, $labeltemplate);
174 $subject =
make_substitutions($arraydefaultmessage->topic, $substitutionarray, $outputlangs);
175 $msg =
make_substitutions($arraydefaultmessage->content, $substitutionarray, $outputlangs);
176 $from =
dol_string_nospecial($conf->global->MAIN_INFO_SOCIETE_NOM,
' ', array(
",")).
' <'.$conf->global->MAIN_INFO_SOCIETE_MAIL.
'>';
179 $fk_partner = $object->fk_member;
181 $adherent =
new Adherent($this->db);
182 $adherent->fetch($object->fk_member);
183 $sendto = $adherent->email;
185 $trackid =
'par'.$object->id;
186 $sendcontext =
'standard';
188 $cmail =
new CMailFile($subject, $sendto, $from, $msg, array(), array(), array(),
'',
'', 0, 1,
'',
'', $trackid,
'', $sendcontext);
190 $result = $cmail->sendfile();
192 if (!$result || !empty($cmail->error) || !empty($cmail->errors)) {
193 $erroremail .= ($erroremail ?
', ' :
'').$cmail->error;
194 $this->errors[] = $cmail->error;
195 if (is_array($cmail->errors) && count($cmail->errors) > 0) $this->errors += $cmail->errors;
198 if (is_object($object)) {
199 $actiontypecode =
'AC_OTH_AUTO';
200 $attachedfiles = array();
202 $object->actiontypecode = $actiontypecode;
203 $object->actionmsg = $arraydefaultmessage->topic.
"\n".$arraydefaultmessage->content;
204 $object->actionmsg2 = $langs->transnoentities(
"PartnershipSentByEMail", $object->ref);;
205 if (!empty($conf->global->MAIN_MAIL_REPLACE_EVENT_TITLE_BY_EMAIL_SUBJECT)) {
206 $object->actionmsg2 = $subject;
209 $object->trackid = $trackid;
210 $object->fk_element = $object->id;
211 $object->elementtype = $object->element;
212 if (is_array($attachedfiles) && count($attachedfiles) > 0) {
213 $object->attachedfiles = $attachedfiles;
216 $object->email_from = $from;
217 $object->email_subject = $subject;
218 $object->email_to = $sendto;
219 $object->email_subject = $subject;
221 $triggersendname =
'PARTNERSHIP_SENTBYMAIL';
223 if (!empty($triggersendname)) {
224 $result = $object->call_trigger($triggersendname, $user);
238 $this->error = $this->db->lasterror();
243 $this->output = $numofexpiredmembers.
' expired partnership members found'.
"\n";
244 if ($erroremail) $this->output .=
'. Got errors when sending some email : '.$erroremail;
246 $this->db->rollback();
247 $this->output =
"Rollback after error\n";
248 $this->output .= $numofexpiredmembers.
' expired partnership members found'.
"\n";
249 if ($erroremail) $this->output .=
'. Got errors when sending some email : '.$erroremail;
266 global $conf, $langs, $user;
271 if (empty($maxpercall)) {
272 $maxpercall =
getDolGlobalInt(
'PARTNERSHIP_MAX_WARNING_BACKLINK_PER_CALL', 10);
275 $langs->loadLangs(array(
"partnership",
"member"));
281 $partnershipsprocessed = array();
283 $websitenotfound =
'';
291 $fk_partner = ($managedfor ==
'member') ?
'fk_member' :
'fk_soc';
293 dol_syslog(get_class($this).
"::doWarningOfPartnershipIfDolibarrBacklinkNotfound Warning of partnership");
300 $sql =
"SELECT p.rowid, p.status, p.".$fk_partner;
301 $sql .=
", p.url_to_check, p.last_check_backlink";
302 $sql .=
', partner.url, partner.email';
303 $sql .=
" FROM ".MAIN_DB_PREFIX.
"partnership as p";
304 if ($managedfor ==
'member') {
305 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"adherent as partner on (partner.rowid = p.fk_member)";
307 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe as partner on (partner.rowid = p.fk_soc)";
309 $sql .=
" WHERE p.".$fk_partner.
" > 0";
310 $sql .=
" AND p.status = ".((int) $partnership::STATUS_APPROVED);
311 $sql .=
" AND (p.last_check_backlink IS NULL OR p.last_check_backlink <= '".$this->db->idate($now - 24 * 3600).
"')";
312 $sql .= $this->db->order(
'p.rowid',
'ASC');
315 $resql = $this->db->query($sql);
317 $numofexpiredmembers = $this->db->num_rows($resql);
318 $somethingdoneonpartnership = 0;
320 while ($ifetchpartner < $numofexpiredmembers) {
323 $obj = $this->db->fetch_object($resql);
325 if (!empty($partnershipsprocessed[$obj->rowid])) {
329 if ($somethingdoneonpartnership >= $maxpercall) {
330 dol_syslog(
"We reach the limit of ".$maxpercall.
" partnership processed, so we quit loop for this batch doWarningOfPartnershipIfDolibarrBacklinkNotfound to avoid to reach email quota.", LOG_WARNING);
337 $object->fetch($obj->rowid);
339 if ($managedfor ==
'member') {
340 $fk_partner = $object->fk_member;
342 $fk_partner = $object->fk_soc;
345 $website = (empty($obj->url_to_check) ? $obj->url : $obj->url_to_check);
347 if (empty($website)) {
348 $websitenotfound .= ($websitenotfound ?
', ' :
'').
'Website not found for id="'.$fk_partner.
'"'.
"\n";
353 if (!$backlinkfound) {
354 $tmpcount = $object->count_last_url_check_error + 1;
356 $nbminbacklinkerrorforcancel =
getDolGlobalString(
'PARTNERSHIP_MIN_BACKLINK_ERROR_FOR_CANCEL', 3);
357 $nbmaxbacklinkerrorforcancel =
getDolGlobalString(
'PARTNERSHIP_MAX_BACKLINK_ERROR_FOR_CANCEL', $nbminbacklinkerrorforcancel + 2);
360 if ($nbminbacklinkerrorforcancel > 0) {
361 if ($tmpcount > $nbminbacklinkerrorforcancel && $tmpcount <= $nbmaxbacklinkerrorforcancel) {
362 if (!empty($obj->email)) {
363 $emailnotfound .= ($emailnotfound ?
', ' :
'').
'Email not found for id="'.$fk_partner.
'"'.
"\n";
366 $labeltemplate =
'('.getDolGlobalString(
'PARTNERSHIP_SENDMAIL_IF_NO_LINK',
'SendingEmailOnPartnershipWillSoonBeCanceled').
')';
368 dol_syslog(
"Now we will send an email to partner id=".$fk_partner.
" with label ".$labeltemplate);
371 include_once DOL_DOCUMENT_ROOT.
'/core/class/html.formmail.class.php';
372 include_once DOL_DOCUMENT_ROOT.
'/core/class/CMailFile.class.php';
373 $formmail =
new FormMail($this->db);
376 $outputlangs = $langs;
379 if (!empty($newlang)) {
381 $outputlangs->setDefaultLang($newlang);
382 $outputlangs->loadLangs(array(
'main',
'member',
'partnership'));
385 $arraydefaultmessage = $formmail->getEMailTemplate($this->db,
'partnership_send', $user, $outputlangs, 0, 1, $labeltemplate);
390 $subject =
make_substitutions($arraydefaultmessage->topic, $substitutionarray, $outputlangs);
391 $msg =
make_substitutions($arraydefaultmessage->content, $substitutionarray, $outputlangs);
392 $from =
dol_string_nospecial($conf->global->MAIN_INFO_SOCIETE_NOM,
' ', array(
",")).
' <'.$conf->global->MAIN_INFO_SOCIETE_MAIL.
'>';
394 $sendto = $obj->email;
396 $trackid =
'par'.$object->id;
397 $sendcontext =
'standard';
399 $cmail =
new CMailFile($subject, $sendto, $from, $msg, array(), array(), array(),
'',
'', 0, 1,
'',
'', $trackid,
'', $sendcontext);
401 $result = $cmail->sendfile();
403 if (!$result || !empty($cmail->error) || !empty($cmail->errors)) {
404 $erroremail .= ($erroremail ?
', ' :
'').$cmail->error;
405 $this->errors[] = $cmail->error;
406 if (is_array($cmail->errors) && count($cmail->errors) > 0) $this->errors += $cmail->errors;
409 if (is_object($object)) {
410 $actiontypecode =
'AC_OTH_AUTO';
411 $attachedfiles = array();
413 if ($managedfor !=
'member') {
414 $object->socid = $fk_partner;
416 $object->actiontypecode = $actiontypecode;
417 $object->actionmsg = $arraydefaultmessage->topic.
"\n".$arraydefaultmessage->content;
418 $object->actionmsg2 = $langs->transnoentities(
"PartnershipSentByEMail", $object->ref);;
419 if (!empty($conf->global->MAIN_MAIL_REPLACE_EVENT_TITLE_BY_EMAIL_SUBJECT)) {
420 $object->actionmsg2 = $subject;
423 $object->trackid = $trackid;
424 $object->fk_element = $object->id;
425 $object->elementtype = $object->element;
426 if (is_array($attachedfiles) && count($attachedfiles) > 0) {
427 $object->attachedfiles = $attachedfiles;
430 $object->email_from = $from;
431 $object->email_subject = $subject;
432 $object->email_to = $sendto;
433 $object->email_subject = $subject;
435 $triggersendname =
'PARTNERSHIP_SENTBYMAIL';
437 if (!empty($triggersendname)) {
438 $result = $object->call_trigger($triggersendname, $user);
447 } elseif ($tmpcount > $nbmaxbacklinkerrorforcancel) {
448 $object->status = $object::STATUS_CANCELED;
449 $object->reason_decline_or_cancel = $langs->trans(
'BacklinkNotFoundOnPartnerWebsite');
453 $object->count_last_url_check_error = $tmpcount;
455 $object->count_last_url_check_error = 0;
456 $object->reason_decline_or_cancel =
'';
459 $partnershipsprocessed[$object->id] = $object->ref;
461 $object->last_check_backlink = $now;
463 $object->update($user);
468 $this->error = $this->db->lasterror();
475 $this->db->rollback();
476 $this->output =
"Rollback after error\n";
478 $this->output .= $numofexpiredmembers.
' partnership checked'.
"\n";
479 if ($erroremail) $this->output .=
'. Got errors when sending some email : '.$erroremail.
"\n";
480 if ($emailnotfound) $this->output .=
'. Email not found for some partner : '.$emailnotfound.
"\n";
481 if ($websitenotfound) $this->output .=
'. Website not found for some partner : '.$websitenotfound.
"\n";
482 $this->output .=
"\nSQL used to find partnerships to scan: ".$sql;