79 global $conf, $langs, $user;
83 if ($managedfor !=
'member') {
88 $MAXPERCALL = (!
getDolGlobalString(
'PARTNERSHIP_MAX_EXPIRATION_CANCEL_PER_CALL') ? 25 : $conf->global->PARTNERSHIP_MAX_EXPIRATION_CANCEL_PER_CALL);
90 $langs->loadLangs(array(
"partnership",
"member"));
96 $partnershipsprocessed = array();
98 $gracedelay =
getDolGlobalString(
'PARTNERSHIP_NBDAYS_AFTER_MEMBER_EXPIRATION_BEFORE_CANCEL');
99 if ($gracedelay < 1) {
100 $this->error =
'BadValueForDelayBeforeCancelCheckSetup';
104 dol_syslog(get_class($this).
"::doCancelStatusOfMemberPartnership cancel expired partnerships with grace delay of ".$gracedelay);
111 $sql =
"SELECT p.rowid, p.fk_member, p.status";
112 $sql .=
", d.datefin, d.fk_adherent_type, dty.subscription";
113 $sql .=
" FROM ".MAIN_DB_PREFIX.
"partnership as p";
114 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"adherent as d on (d.rowid = p.fk_member)";
115 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"adherent_type as dty on (dty.rowid = d.fk_adherent_type)";
116 $sql .=
" WHERE fk_member > 0";
117 $sql .=
" AND (d.datefin < '".$this->db->idate($datetotest).
"' AND dty.subscription = 1)";
118 $sql .=
" AND p.status = ".((int) $partnership::STATUS_APPROVED);
119 $sql .= $this->db->order(
'd.rowid',
'ASC');
122 $numofexpiredmembers = 0;
123 $resql = $this->db->query($sql);
125 $numofexpiredmembers = $this->db->num_rows($resql);
127 $somethingdoneonpartnership = 0;
129 while ($ifetchpartner < $numofexpiredmembers) {
132 $obj = $this->db->fetch_object($resql);
134 if (!empty($partnershipsprocessed[$obj->rowid])) {
138 if ($somethingdoneonpartnership >= $MAXPERCALL) {
139 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);
147 $expirationdate = $obj->datefin;
149 if ($expirationdate && $expirationdate < $now) {
150 $somethingdoneonpartnership++;
152 $result =
$object->cancel($user, 0);
158 if (is_array($this->errors)) {
159 $this->errors = array_merge($this->errors,
$object->errors);
161 $this->errors =
$object->errors;
168 $labeltemplate =
'('.getDolGlobalString(
'PARTNERSHIP_SENDMAIL_IF_AUTO_CANCEL',
'SendingEmailOnPartnershipCanceled').
')';
170 dol_syslog(
"Now we will send an email to member id=".
$object->fk_member.
" with label ".$labeltemplate);
173 include_once DOL_DOCUMENT_ROOT.
'/core/class/html.formmail.class.php';
174 include_once DOL_DOCUMENT_ROOT.
'/core/class/CMailFile.class.php';
175 $formmail =
new FormMail($this->db);
178 $outputlangs = $langs;
181 $newlang =
GETPOST(
'lang_id',
'aZ09');
183 if (!empty($newlang)) {
185 $outputlangs->setDefaultLang($newlang);
186 $outputlangs->loadLangs(array(
'main',
'member',
'partnership'));
189 $arraydefaultmessage = $formmail->getEMailTemplate($this->db,
'partnership_send', $user, $outputlangs, 0, 1, $labeltemplate);
194 $subject =
make_substitutions($arraydefaultmessage->topic, $substitutionarray, $outputlangs);
195 $msg =
make_substitutions($arraydefaultmessage->content, $substitutionarray, $outputlangs);
199 $fk_partner =
$object->fk_member;
201 $adherent =
new Adherent($this->db);
202 $adherent->fetch(
$object->fk_member);
203 $sendto = $adherent->email;
205 $trackid =
'par'.$object->id;
206 $sendcontext =
'standard';
208 $cmail =
new CMailFile($subject, $sendto, $from, $msg, array(), array(), array(),
'',
'', 0, 1,
'',
'', $trackid,
'', $sendcontext);
210 $result = $cmail->sendfile();
212 if (!$result || !empty($cmail->error) || !empty($cmail->errors)) {
213 $erroremail .= ($erroremail ?
', ' :
'').$cmail->error;
214 $this->errors[] = $cmail->error;
215 if (is_array($cmail->errors) && count($cmail->errors) > 0) {
216 $this->errors += $cmail->errors;
221 $actiontypecode =
'AC_OTH_AUTO';
222 $attachedfiles = array();
224 $object->actiontypecode = $actiontypecode;
225 $object->actionmsg = $arraydefaultmessage->topic.
"\n".$arraydefaultmessage->content;
226 $object->actionmsg2 = $langs->transnoentities(
"PartnershipSentByEMail",
$object->ref);
229 $object->actionmsg2 = $subject;
235 if (is_array($attachedfiles) && count($attachedfiles) > 0) {
236 $object->attachedfiles = $attachedfiles;
240 $object->email_subject = $subject;
242 $object->email_subject = $subject;
244 $triggersendname =
'PARTNERSHIP_SENTBYMAIL';
246 if (!empty($triggersendname)) {
247 $result =
$object->call_trigger($triggersendname, $user);
261 $this->error = $this->db->lasterror();
266 $this->output = $numofexpiredmembers.
' expired partnership members found'.
"\n";
268 $this->output .=
'. Got errors when sending some email : '.$erroremail;
271 $this->db->rollback();
272 $this->output =
"Rollback after error\n";
273 $this->output .= $numofexpiredmembers.
' expired partnership members found'.
"\n";
275 $this->output .=
'. Got errors when sending some email : '.$erroremail;
293 global $conf, $langs, $user;
298 if (empty($maxpercall)) {
299 $maxpercall =
getDolGlobalInt(
'PARTNERSHIP_MAX_WARNING_BACKLINK_PER_CALL', 10);
302 $langs->loadLangs(array(
"partnership",
"member"));
308 $partnershipsprocessed = array();
310 $websitenotfound =
'';
318 $fk_partner = ($managedfor ==
'member') ?
'fk_member' :
'fk_soc';
320 dol_syslog(get_class($this).
"::doWarningOfPartnershipIfDolibarrBacklinkNotfound Warning of partnership");
327 $sql =
"SELECT p.rowid, p.status, p.".$fk_partner;
328 $sql .=
", p.url_to_check, p.last_check_backlink";
329 $sql .=
', partner.url, partner.email';
330 $sql .=
" FROM ".MAIN_DB_PREFIX.
"partnership as p";
331 if ($managedfor ==
'member') {
332 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"adherent as partner on (partner.rowid = p.fk_member)";
334 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe as partner on (partner.rowid = p.fk_soc)";
336 $sql .=
" WHERE p.".$fk_partner.
" > 0";
337 $sql .=
" AND p.status = ".((int) $partnership::STATUS_APPROVED);
338 $sql .=
" AND (p.last_check_backlink IS NULL OR p.last_check_backlink <= '".$this->db->idate($now - 24 * 3600).
"')";
339 $sql .= $this->db->order(
'p.rowid',
'ASC');
342 $numofexpiredmembers = 0;
343 $resql = $this->db->query($sql);
345 $numofexpiredmembers = $this->db->num_rows($resql);
346 $somethingdoneonpartnership = 0;
348 while ($ifetchpartner < $numofexpiredmembers) {
351 $obj = $this->db->fetch_object($resql);
353 if (!empty($partnershipsprocessed[$obj->rowid])) {
357 if ($somethingdoneonpartnership >= $maxpercall) {
358 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);
367 if ($managedfor ==
'member') {
368 $fk_partner =
$object->fk_member;
373 $website = (empty($obj->url_to_check) ? $obj->url : $obj->url_to_check);
375 if (empty($website)) {
376 $websitenotfound .= ($websitenotfound ?
', ' :
'').
'Website not found for id="'.$fk_partner.
'"'.
"\n";
381 if (!$backlinkfound) {
382 $tmpcount =
$object->count_last_url_check_error + 1;
384 $nbminbacklinkerrorforcancel = (int)
getDolGlobalString(
'PARTNERSHIP_MIN_BACKLINK_ERROR_FOR_CANCEL', 3);
385 $nbmaxbacklinkerrorforcancel = (int)
getDolGlobalString(
'PARTNERSHIP_MAX_BACKLINK_ERROR_FOR_CANCEL', (
int) $nbminbacklinkerrorforcancel + 2);
388 if ($nbminbacklinkerrorforcancel > 0) {
389 if ($tmpcount > $nbminbacklinkerrorforcancel && $tmpcount <= $nbmaxbacklinkerrorforcancel) {
390 if (!empty($obj->email)) {
391 $emailnotfound .= ($emailnotfound ?
', ' :
'').
'Email not found for id="'.$fk_partner.
'"'.
"\n";
394 $labeltemplate =
'('.getDolGlobalString(
'PARTNERSHIP_SENDMAIL_IF_NO_LINK',
'SendingEmailOnPartnershipWillSoonBeCanceled').
')';
396 dol_syslog(
"Now we will send an email to partner id=".$fk_partner.
" with label ".$labeltemplate);
399 include_once DOL_DOCUMENT_ROOT.
'/core/class/html.formmail.class.php';
400 include_once DOL_DOCUMENT_ROOT.
'/core/class/CMailFile.class.php';
401 $formmail =
new FormMail($this->db);
404 $outputlangs = $langs;
407 $newlang =
GETPOST(
'lang_id',
'aZ09');
409 if (!empty($newlang)) {
411 $outputlangs->setDefaultLang($newlang);
412 $outputlangs->loadLangs(array(
'main',
'member',
'partnership'));
415 $arraydefaultmessage = $formmail->getEMailTemplate($this->db,
'partnership_send', $user, $outputlangs, 0, 1, $labeltemplate);
420 $subject =
make_substitutions($arraydefaultmessage->topic, $substitutionarray, $outputlangs);
421 $msg =
make_substitutions($arraydefaultmessage->content, $substitutionarray, $outputlangs);
424 $sendto = $obj->email;
426 $trackid =
'par'.$object->id;
427 $sendcontext =
'standard';
429 $cmail =
new CMailFile($subject, $sendto, $from, $msg, array(), array(), array(),
'',
'', 0, 1,
'',
'', $trackid,
'', $sendcontext);
431 $result = $cmail->sendfile();
433 if (!$result || !empty($cmail->error) || !empty($cmail->errors)) {
434 $erroremail .= ($erroremail ?
', ' :
'').$cmail->error;
435 $this->errors[] = $cmail->error;
436 if (is_array($cmail->errors) && count($cmail->errors) > 0) {
437 $this->errors += $cmail->errors;
442 $actiontypecode =
'AC_OTH_AUTO';
443 $attachedfiles = array();
445 if ($managedfor !=
'member') {
448 $object->actiontypecode = $actiontypecode;
449 $object->actionmsg = $arraydefaultmessage->topic.
"\n".$arraydefaultmessage->content;
450 $object->actionmsg2 = $langs->transnoentities(
"PartnershipSentByEMail",
$object->ref);
453 $object->actionmsg2 = $subject;
459 if (is_array($attachedfiles) && count($attachedfiles) > 0) {
460 $object->attachedfiles = $attachedfiles;
464 $object->email_subject = $subject;
466 $object->email_subject = $subject;
468 $triggersendname =
'PARTNERSHIP_SENTBYMAIL';
470 if (!empty($triggersendname)) {
471 $result =
$object->call_trigger($triggersendname, $user);
480 } elseif ($tmpcount > $nbmaxbacklinkerrorforcancel) {
481 $object->status = $object::STATUS_CANCELED;
482 $object->reason_decline_or_cancel = $langs->trans(
'BacklinkNotFoundOnPartnerWebsite');
486 $object->count_last_url_check_error = $tmpcount;
488 $object->count_last_url_check_error = 0;
489 $object->reason_decline_or_cancel =
'';
494 $object->last_check_backlink = $now;
501 $this->error = $this->db->lasterror();
508 $this->db->rollback();
509 $this->output =
"Rollback after error\n";
511 $this->output .= $numofexpiredmembers.
' partnership checked'.
"\n";
513 $this->output .=
'. Got errors when sending some email : '.$erroremail.
"\n";
515 if ($emailnotfound) {
516 $this->output .=
'. Email not found for some partner : '.$emailnotfound.
"\n";
518 if ($websitenotfound) {
519 $this->output .=
'. Website not found for some partner : '.$websitenotfound.
"\n";
521 $this->output .=
"\nSQL used to find partnerships to scan: ".$sql;