66 global $conf, $langs, $user;
70 if ($managedfor !=
'member') {
75 $MAXPERCALL = (!
getDolGlobalString(
'PARTNERSHIP_MAX_EXPIRATION_CANCEL_PER_CALL') ? 25 : $conf->global->PARTNERSHIP_MAX_EXPIRATION_CANCEL_PER_CALL);
77 $langs->loadLangs(array(
"partnership",
"member"));
83 $partnershipsprocessed = array();
85 $gracedelay = $conf->global->PARTNERSHIP_NBDAYS_AFTER_MEMBER_EXPIRATION_BEFORE_CANCEL;
86 if ($gracedelay < 1) {
87 $this->error =
'BadValueForDelayBeforeCancelCheckSetup';
91 dol_syslog(get_class($this).
"::doCancelStatusOfMemberPartnership cancel expired partnerships with grace delay of ".$gracedelay);
98 $sql =
"SELECT p.rowid, p.fk_member, p.status";
99 $sql .=
", d.datefin, d.fk_adherent_type, dty.subscription";
100 $sql .=
" FROM ".MAIN_DB_PREFIX.
"partnership as p";
101 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"adherent as d on (d.rowid = p.fk_member)";
102 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"adherent_type as dty on (dty.rowid = d.fk_adherent_type)";
103 $sql .=
" WHERE fk_member > 0";
104 $sql .=
" AND (d.datefin < '".$this->db->idate($datetotest).
"' AND dty.subscription = 1)";
105 $sql .=
" AND p.status = ".((int) $partnership::STATUS_APPROVED);
106 $sql .= $this->db->order(
'd.rowid',
'ASC');
109 $resql = $this->db->query($sql);
111 $numofexpiredmembers = $this->db->num_rows($resql);
113 $somethingdoneonpartnership = 0;
115 while ($ifetchpartner < $numofexpiredmembers) {
118 $obj = $this->db->fetch_object($resql);
120 if (!empty($partnershipsprocessed[$obj->rowid])) {
124 if ($somethingdoneonpartnership >= $MAXPERCALL) {
125 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);
130 $object->fetch($obj->rowid);
133 $expirationdate = $obj->datefin;
135 if ($expirationdate && $expirationdate < $now) {
136 $somethingdoneonpartnership++;
138 $result = $object->cancel($user, 0);
142 $this->error = $object->error;
143 if (is_array($object->errors) && count($object->errors)) {
144 if (is_array($this->errors)) {
145 $this->errors = array_merge($this->errors, $object->errors);
147 $this->errors = $object->errors;
151 $partnershipsprocessed[$object->id] = $object->ref;
154 $labeltemplate =
'('.getDolGlobalString(
'PARTNERSHIP_SENDMAIL_IF_AUTO_CANCEL',
'SendingEmailOnPartnershipCanceled').
')';
156 dol_syslog(
"Now we will send an email to member id=".$object->fk_member.
" with label ".$labeltemplate);
159 include_once DOL_DOCUMENT_ROOT.
'/core/class/html.formmail.class.php';
160 include_once DOL_DOCUMENT_ROOT.
'/core/class/CMailFile.class.php';
161 $formmail =
new FormMail($this->db);
164 $outputlangs = $langs;
167 $newlang =
GETPOST(
'lang_id',
'aZ09');
169 if (!empty($newlang)) {
171 $outputlangs->setDefaultLang($newlang);
172 $outputlangs->loadLangs(array(
'main',
'member',
'partnership'));
175 $arraydefaultmessage = $formmail->getEMailTemplate($this->db,
'partnership_send', $user, $outputlangs, 0, 1, $labeltemplate);
180 $subject =
make_substitutions($arraydefaultmessage->topic, $substitutionarray, $outputlangs);
181 $msg =
make_substitutions($arraydefaultmessage->content, $substitutionarray, $outputlangs);
185 $fk_partner = $object->fk_member;
187 $adherent =
new Adherent($this->db);
188 $adherent->fetch($object->fk_member);
189 $sendto = $adherent->email;
191 $trackid =
'par'.$object->id;
192 $sendcontext =
'standard';
194 $cmail =
new CMailFile($subject, $sendto, $from, $msg, array(), array(), array(),
'',
'', 0, 1,
'',
'', $trackid,
'', $sendcontext);
196 $result = $cmail->sendfile();
198 if (!$result || !empty($cmail->error) || !empty($cmail->errors)) {
199 $erroremail .= ($erroremail ?
', ' :
'').$cmail->error;
200 $this->errors[] = $cmail->error;
201 if (is_array($cmail->errors) && count($cmail->errors) > 0) {
202 $this->errors += $cmail->errors;
206 if (is_object($object)) {
207 $actiontypecode =
'AC_OTH_AUTO';
208 $attachedfiles = array();
210 $object->actiontypecode = $actiontypecode;
211 $object->actionmsg = $arraydefaultmessage->topic.
"\n".$arraydefaultmessage->content;
212 $object->actionmsg2 = $langs->transnoentities(
"PartnershipSentByEMail", $object->ref);
215 $object->actionmsg2 = $subject;
218 $object->trackid = $trackid;
219 $object->fk_element = $object->id;
220 $object->elementtype = $object->element;
221 if (is_array($attachedfiles) && count($attachedfiles) > 0) {
222 $object->attachedfiles = $attachedfiles;
225 $object->email_from = $from;
226 $object->email_subject = $subject;
227 $object->email_to = $sendto;
228 $object->email_subject = $subject;
230 $triggersendname =
'PARTNERSHIP_SENTBYMAIL';
232 if (!empty($triggersendname)) {
233 $result = $object->call_trigger($triggersendname, $user);
247 $this->error = $this->db->lasterror();
252 $this->output = $numofexpiredmembers.
' expired partnership members found'.
"\n";
254 $this->output .=
'. Got errors when sending some email : '.$erroremail;
257 $this->db->rollback();
258 $this->output =
"Rollback after error\n";
259 $this->output .= $numofexpiredmembers.
' expired partnership members found'.
"\n";
261 $this->output .=
'. Got errors when sending some email : '.$erroremail;
279 global $conf, $langs, $user;
284 if (empty($maxpercall)) {
285 $maxpercall =
getDolGlobalInt(
'PARTNERSHIP_MAX_WARNING_BACKLINK_PER_CALL', 10);
288 $langs->loadLangs(array(
"partnership",
"member"));
294 $partnershipsprocessed = array();
296 $websitenotfound =
'';
304 $fk_partner = ($managedfor ==
'member') ?
'fk_member' :
'fk_soc';
306 dol_syslog(get_class($this).
"::doWarningOfPartnershipIfDolibarrBacklinkNotfound Warning of partnership");
313 $sql =
"SELECT p.rowid, p.status, p.".$fk_partner;
314 $sql .=
", p.url_to_check, p.last_check_backlink";
315 $sql .=
', partner.url, partner.email';
316 $sql .=
" FROM ".MAIN_DB_PREFIX.
"partnership as p";
317 if ($managedfor ==
'member') {
318 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"adherent as partner on (partner.rowid = p.fk_member)";
320 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe as partner on (partner.rowid = p.fk_soc)";
322 $sql .=
" WHERE p.".$fk_partner.
" > 0";
323 $sql .=
" AND p.status = ".((int) $partnership::STATUS_APPROVED);
324 $sql .=
" AND (p.last_check_backlink IS NULL OR p.last_check_backlink <= '".$this->db->idate($now - 24 * 3600).
"')";
325 $sql .= $this->db->order(
'p.rowid',
'ASC');
328 $resql = $this->db->query($sql);
330 $numofexpiredmembers = $this->db->num_rows($resql);
331 $somethingdoneonpartnership = 0;
333 while ($ifetchpartner < $numofexpiredmembers) {
336 $obj = $this->db->fetch_object($resql);
338 if (!empty($partnershipsprocessed[$obj->rowid])) {
342 if ($somethingdoneonpartnership >= $maxpercall) {
343 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);
350 $object->fetch($obj->rowid);
352 if ($managedfor ==
'member') {
353 $fk_partner = $object->fk_member;
355 $fk_partner = $object->fk_soc;
358 $website = (empty($obj->url_to_check) ? $obj->url : $obj->url_to_check);
360 if (empty($website)) {
361 $websitenotfound .= ($websitenotfound ?
', ' :
'').
'Website not found for id="'.$fk_partner.
'"'.
"\n";
366 if (!$backlinkfound) {
367 $tmpcount = $object->count_last_url_check_error + 1;
369 $nbminbacklinkerrorforcancel =
getDolGlobalString(
'PARTNERSHIP_MIN_BACKLINK_ERROR_FOR_CANCEL', 3);
370 $nbmaxbacklinkerrorforcancel =
getDolGlobalString(
'PARTNERSHIP_MAX_BACKLINK_ERROR_FOR_CANCEL', $nbminbacklinkerrorforcancel + 2);
373 if ($nbminbacklinkerrorforcancel > 0) {
374 if ($tmpcount > $nbminbacklinkerrorforcancel && $tmpcount <= $nbmaxbacklinkerrorforcancel) {
375 if (!empty($obj->email)) {
376 $emailnotfound .= ($emailnotfound ?
', ' :
'').
'Email not found for id="'.$fk_partner.
'"'.
"\n";
379 $labeltemplate =
'('.getDolGlobalString(
'PARTNERSHIP_SENDMAIL_IF_NO_LINK',
'SendingEmailOnPartnershipWillSoonBeCanceled').
')';
381 dol_syslog(
"Now we will send an email to partner id=".$fk_partner.
" with label ".$labeltemplate);
384 include_once DOL_DOCUMENT_ROOT.
'/core/class/html.formmail.class.php';
385 include_once DOL_DOCUMENT_ROOT.
'/core/class/CMailFile.class.php';
386 $formmail =
new FormMail($this->db);
389 $outputlangs = $langs;
392 $newlang =
GETPOST(
'lang_id',
'aZ09');
394 if (!empty($newlang)) {
396 $outputlangs->setDefaultLang($newlang);
397 $outputlangs->loadLangs(array(
'main',
'member',
'partnership'));
400 $arraydefaultmessage = $formmail->getEMailTemplate($this->db,
'partnership_send', $user, $outputlangs, 0, 1, $labeltemplate);
405 $subject =
make_substitutions($arraydefaultmessage->topic, $substitutionarray, $outputlangs);
406 $msg =
make_substitutions($arraydefaultmessage->content, $substitutionarray, $outputlangs);
409 $sendto = $obj->email;
411 $trackid =
'par'.$object->id;
412 $sendcontext =
'standard';
414 $cmail =
new CMailFile($subject, $sendto, $from, $msg, array(), array(), array(),
'',
'', 0, 1,
'',
'', $trackid,
'', $sendcontext);
416 $result = $cmail->sendfile();
418 if (!$result || !empty($cmail->error) || !empty($cmail->errors)) {
419 $erroremail .= ($erroremail ?
', ' :
'').$cmail->error;
420 $this->errors[] = $cmail->error;
421 if (is_array($cmail->errors) && count($cmail->errors) > 0) {
422 $this->errors += $cmail->errors;
426 if (is_object($object)) {
427 $actiontypecode =
'AC_OTH_AUTO';
428 $attachedfiles = array();
430 if ($managedfor !=
'member') {
431 $object->socid = $fk_partner;
433 $object->actiontypecode = $actiontypecode;
434 $object->actionmsg = $arraydefaultmessage->topic.
"\n".$arraydefaultmessage->content;
435 $object->actionmsg2 = $langs->transnoentities(
"PartnershipSentByEMail", $object->ref);
438 $object->actionmsg2 = $subject;
441 $object->trackid = $trackid;
442 $object->fk_element = $object->id;
443 $object->elementtype = $object->element;
444 if (is_array($attachedfiles) && count($attachedfiles) > 0) {
445 $object->attachedfiles = $attachedfiles;
448 $object->email_from = $from;
449 $object->email_subject = $subject;
450 $object->email_to = $sendto;
451 $object->email_subject = $subject;
453 $triggersendname =
'PARTNERSHIP_SENTBYMAIL';
455 if (!empty($triggersendname)) {
456 $result = $object->call_trigger($triggersendname, $user);
465 } elseif ($tmpcount > $nbmaxbacklinkerrorforcancel) {
466 $object->status = $object::STATUS_CANCELED;
467 $object->reason_decline_or_cancel = $langs->trans(
'BacklinkNotFoundOnPartnerWebsite');
471 $object->count_last_url_check_error = $tmpcount;
473 $object->count_last_url_check_error = 0;
474 $object->reason_decline_or_cancel =
'';
477 $partnershipsprocessed[$object->id] = $object->ref;
479 $object->last_check_backlink = $now;
481 $object->update($user);
486 $this->error = $this->db->lasterror();
493 $this->db->rollback();
494 $this->output =
"Rollback after error\n";
496 $this->output .= $numofexpiredmembers.
' partnership checked'.
"\n";
498 $this->output .=
'. Got errors when sending some email : '.$erroremail.
"\n";
500 if ($emailnotfound) {
501 $this->output .=
'. Email not found for some partner : '.$emailnotfound.
"\n";
503 if ($websitenotfound) {
504 $this->output .=
'. Website not found for some partner : '.$websitenotfound.
"\n";
506 $this->output .=
"\nSQL used to find partnerships to scan: ".$sql;