67 global $conf, $langs, $user;
71 if ($managedfor !=
'member') {
76 $MAXPERCALL = (!
getDolGlobalString(
'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 =
getDolGlobalString(
'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])) {
125 if ($somethingdoneonpartnership >= $MAXPERCALL) {
126 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);
134 $expirationdate = $obj->datefin;
136 if ($expirationdate && $expirationdate < $now) {
137 $somethingdoneonpartnership++;
139 $result =
$object->cancel($user, 0);
145 if (is_array($this->errors)) {
146 $this->errors = array_merge($this->errors,
$object->errors);
148 $this->errors =
$object->errors;
155 $labeltemplate =
'('.getDolGlobalString(
'PARTNERSHIP_SENDMAIL_IF_AUTO_CANCEL',
'SendingEmailOnPartnershipCanceled').
')';
157 dol_syslog(
"Now we will send an email to member id=".
$object->fk_member.
" with label ".$labeltemplate);
160 include_once DOL_DOCUMENT_ROOT.
'/core/class/html.formmail.class.php';
161 include_once DOL_DOCUMENT_ROOT.
'/core/class/CMailFile.class.php';
162 $formmail =
new FormMail($this->db);
165 $outputlangs = $langs;
168 $newlang =
GETPOST(
'lang_id',
'aZ09');
170 if (!empty($newlang)) {
172 $outputlangs->setDefaultLang($newlang);
173 $outputlangs->loadLangs(array(
'main',
'member',
'partnership'));
176 $arraydefaultmessage = $formmail->getEMailTemplate($this->db,
'partnership_send', $user, $outputlangs, 0, 1, $labeltemplate);
181 $subject =
make_substitutions($arraydefaultmessage->topic, $substitutionarray, $outputlangs);
182 $msg =
make_substitutions($arraydefaultmessage->content, $substitutionarray, $outputlangs);
186 $fk_partner =
$object->fk_member;
188 $adherent =
new Adherent($this->db);
189 $adherent->fetch(
$object->fk_member);
190 $sendto = $adherent->email;
192 $trackid =
'par'.$object->id;
193 $sendcontext =
'standard';
195 $cmail =
new CMailFile($subject, $sendto, $from, $msg, array(), array(), array(),
'',
'', 0, 1,
'',
'', $trackid,
'', $sendcontext);
197 $result = $cmail->sendfile();
199 if (!$result || !empty($cmail->error) || !empty($cmail->errors)) {
200 $erroremail .= ($erroremail ?
', ' :
'').$cmail->error;
201 $this->errors[] = $cmail->error;
202 if (is_array($cmail->errors) && count($cmail->errors) > 0) {
203 $this->errors += $cmail->errors;
208 $actiontypecode =
'AC_OTH_AUTO';
209 $attachedfiles = array();
211 $object->actiontypecode = $actiontypecode;
212 $object->actionmsg = $arraydefaultmessage->topic.
"\n".$arraydefaultmessage->content;
213 $object->actionmsg2 = $langs->transnoentities(
"PartnershipSentByEMail",
$object->ref);
216 $object->actionmsg2 = $subject;
222 if (is_array($attachedfiles) && count($attachedfiles) > 0) {
223 $object->attachedfiles = $attachedfiles;
227 $object->email_subject = $subject;
229 $object->email_subject = $subject;
231 $triggersendname =
'PARTNERSHIP_SENTBYMAIL';
233 if (!empty($triggersendname)) {
234 $result =
$object->call_trigger($triggersendname, $user);
248 $this->error = $this->db->lasterror();
253 $this->output = $numofexpiredmembers.
' expired partnership members found'.
"\n";
255 $this->output .=
'. Got errors when sending some email : '.$erroremail;
258 $this->db->rollback();
259 $this->output =
"Rollback after error\n";
260 $this->output .= $numofexpiredmembers.
' expired partnership members found'.
"\n";
262 $this->output .=
'. Got errors when sending some email : '.$erroremail;
280 global $conf, $langs, $user;
285 if (empty($maxpercall)) {
286 $maxpercall =
getDolGlobalInt(
'PARTNERSHIP_MAX_WARNING_BACKLINK_PER_CALL', 10);
289 $langs->loadLangs(array(
"partnership",
"member"));
295 $partnershipsprocessed = array();
297 $websitenotfound =
'';
305 $fk_partner = ($managedfor ==
'member') ?
'fk_member' :
'fk_soc';
307 dol_syslog(get_class($this).
"::doWarningOfPartnershipIfDolibarrBacklinkNotfound Warning of partnership");
314 $sql =
"SELECT p.rowid, p.status, p.".$fk_partner;
315 $sql .=
", p.url_to_check, p.last_check_backlink";
316 $sql .=
', partner.url, partner.email';
317 $sql .=
" FROM ".MAIN_DB_PREFIX.
"partnership as p";
318 if ($managedfor ==
'member') {
319 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"adherent as partner on (partner.rowid = p.fk_member)";
321 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe as partner on (partner.rowid = p.fk_soc)";
323 $sql .=
" WHERE p.".$fk_partner.
" > 0";
324 $sql .=
" AND p.status = ".((int) $partnership::STATUS_APPROVED);
325 $sql .=
" AND (p.last_check_backlink IS NULL OR p.last_check_backlink <= '".$this->db->idate($now - 24 * 3600).
"')";
326 $sql .= $this->db->order(
'p.rowid',
'ASC');
329 $resql = $this->db->query($sql);
331 $numofexpiredmembers = $this->db->num_rows($resql);
332 $somethingdoneonpartnership = 0;
334 while ($ifetchpartner < $numofexpiredmembers) {
337 $obj = $this->db->fetch_object($resql);
339 if (!empty($partnershipsprocessed[$obj->rowid])) {
343 if ($somethingdoneonpartnership >= $maxpercall) {
344 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);
353 if ($managedfor ==
'member') {
354 $fk_partner =
$object->fk_member;
359 $website = (empty($obj->url_to_check) ? $obj->url : $obj->url_to_check);
361 if (empty($website)) {
362 $websitenotfound .= ($websitenotfound ?
', ' :
'').
'Website not found for id="'.$fk_partner.
'"'.
"\n";
367 if (!$backlinkfound) {
368 $tmpcount =
$object->count_last_url_check_error + 1;
370 $nbminbacklinkerrorforcancel = (int)
getDolGlobalString(
'PARTNERSHIP_MIN_BACKLINK_ERROR_FOR_CANCEL', 3);
371 $nbmaxbacklinkerrorforcancel = (int)
getDolGlobalString(
'PARTNERSHIP_MAX_BACKLINK_ERROR_FOR_CANCEL', (
int) $nbminbacklinkerrorforcancel + 2);
374 if ($nbminbacklinkerrorforcancel > 0) {
375 if ($tmpcount > $nbminbacklinkerrorforcancel && $tmpcount <= $nbmaxbacklinkerrorforcancel) {
376 if (!empty($obj->email)) {
377 $emailnotfound .= ($emailnotfound ?
', ' :
'').
'Email not found for id="'.$fk_partner.
'"'.
"\n";
380 $labeltemplate =
'('.getDolGlobalString(
'PARTNERSHIP_SENDMAIL_IF_NO_LINK',
'SendingEmailOnPartnershipWillSoonBeCanceled').
')';
382 dol_syslog(
"Now we will send an email to partner id=".$fk_partner.
" with label ".$labeltemplate);
385 include_once DOL_DOCUMENT_ROOT.
'/core/class/html.formmail.class.php';
386 include_once DOL_DOCUMENT_ROOT.
'/core/class/CMailFile.class.php';
387 $formmail =
new FormMail($this->db);
390 $outputlangs = $langs;
393 $newlang =
GETPOST(
'lang_id',
'aZ09');
395 if (!empty($newlang)) {
397 $outputlangs->setDefaultLang($newlang);
398 $outputlangs->loadLangs(array(
'main',
'member',
'partnership'));
401 $arraydefaultmessage = $formmail->getEMailTemplate($this->db,
'partnership_send', $user, $outputlangs, 0, 1, $labeltemplate);
406 $subject =
make_substitutions($arraydefaultmessage->topic, $substitutionarray, $outputlangs);
407 $msg =
make_substitutions($arraydefaultmessage->content, $substitutionarray, $outputlangs);
410 $sendto = $obj->email;
412 $trackid =
'par'.$object->id;
413 $sendcontext =
'standard';
415 $cmail =
new CMailFile($subject, $sendto, $from, $msg, array(), array(), array(),
'',
'', 0, 1,
'',
'', $trackid,
'', $sendcontext);
417 $result = $cmail->sendfile();
419 if (!$result || !empty($cmail->error) || !empty($cmail->errors)) {
420 $erroremail .= ($erroremail ?
', ' :
'').$cmail->error;
421 $this->errors[] = $cmail->error;
422 if (is_array($cmail->errors) && count($cmail->errors) > 0) {
423 $this->errors += $cmail->errors;
428 $actiontypecode =
'AC_OTH_AUTO';
429 $attachedfiles = array();
431 if ($managedfor !=
'member') {
434 $object->actiontypecode = $actiontypecode;
435 $object->actionmsg = $arraydefaultmessage->topic.
"\n".$arraydefaultmessage->content;
436 $object->actionmsg2 = $langs->transnoentities(
"PartnershipSentByEMail",
$object->ref);
439 $object->actionmsg2 = $subject;
445 if (is_array($attachedfiles) && count($attachedfiles) > 0) {
446 $object->attachedfiles = $attachedfiles;
450 $object->email_subject = $subject;
452 $object->email_subject = $subject;
454 $triggersendname =
'PARTNERSHIP_SENTBYMAIL';
456 if (!empty($triggersendname)) {
457 $result =
$object->call_trigger($triggersendname, $user);
466 } elseif ($tmpcount > $nbmaxbacklinkerrorforcancel) {
467 $object->status = $object::STATUS_CANCELED;
468 $object->reason_decline_or_cancel = $langs->trans(
'BacklinkNotFoundOnPartnerWebsite');
472 $object->count_last_url_check_error = $tmpcount;
474 $object->count_last_url_check_error = 0;
475 $object->reason_decline_or_cancel =
'';
480 $object->last_check_backlink = $now;
487 $this->error = $this->db->lasterror();
494 $this->db->rollback();
495 $this->output =
"Rollback after error\n";
497 $this->output .= $numofexpiredmembers.
' partnership checked'.
"\n";
499 $this->output .=
'. Got errors when sending some email : '.$erroremail.
"\n";
501 if ($emailnotfound) {
502 $this->output .=
'. Email not found for some partner : '.$emailnotfound.
"\n";
504 if ($websitenotfound) {
505 $this->output .=
'. Website not found for some partner : '.$websitenotfound.
"\n";
507 $this->output .=
"\nSQL used to find partnerships to scan: ".$sql;