| Conditions | 35 |
| Paths | 67 |
| Total Lines | 200 |
| Code Lines | 127 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 70 | public function doCancelStatusOfMemberPartnership() |
||
| 71 | { |
||
| 72 | global $conf, $langs, $user; |
||
| 73 | |||
| 74 | $managedfor = getDolGlobalString('PARTNERSHIP_IS_MANAGED_FOR', 'thirdparty'); |
||
| 75 | |||
| 76 | if ($managedfor != 'member') { |
||
| 77 | return 0; // If option 'PARTNERSHIP_IS_MANAGED_FOR' = 'thirdparty', this cron job does nothing. |
||
| 78 | } |
||
| 79 | |||
| 80 | $partnership = new Partnership($this->db); |
||
| 81 | $MAXPERCALL = (!getDolGlobalString('PARTNERSHIP_MAX_EXPIRATION_CANCEL_PER_CALL') ? 25 : $conf->global->PARTNERSHIP_MAX_EXPIRATION_CANCEL_PER_CALL); // Limit to 25 per call |
||
| 82 | |||
| 83 | $langs->loadLangs(array("partnership", "member")); |
||
| 84 | |||
| 85 | $error = 0; |
||
| 86 | $erroremail = ''; |
||
| 87 | $this->output = ''; |
||
| 88 | $this->error = ''; |
||
| 89 | $partnershipsprocessed = array(); |
||
| 90 | |||
| 91 | $gracedelay = getDolGlobalString('PARTNERSHIP_NBDAYS_AFTER_MEMBER_EXPIRATION_BEFORE_CANCEL'); |
||
| 92 | if ($gracedelay < 1) { |
||
| 93 | $this->error = 'BadValueForDelayBeforeCancelCheckSetup'; |
||
| 94 | return -1; |
||
| 95 | } |
||
| 96 | |||
| 97 | dol_syslog(get_class($this) . "::doCancelStatusOfMemberPartnership cancel expired partnerships with grace delay of " . $gracedelay); |
||
| 98 | |||
| 99 | $now = dol_now(); |
||
| 100 | $datetotest = dol_time_plus_duree($now, -1 * abs((float) $gracedelay), 'd'); |
||
| 101 | |||
| 102 | $this->db->begin(); |
||
| 103 | |||
| 104 | $sql = "SELECT p.rowid, p.fk_member, p.status"; |
||
| 105 | $sql .= ", d.datefin, d.fk_adherent_type, dty.subscription"; |
||
| 106 | $sql .= " FROM " . MAIN_DB_PREFIX . "partnership as p"; |
||
| 107 | $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "adherent as d on (d.rowid = p.fk_member)"; |
||
| 108 | $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "adherent_type as dty on (dty.rowid = d.fk_adherent_type)"; |
||
| 109 | $sql .= " WHERE fk_member > 0"; |
||
| 110 | $sql .= " AND (d.datefin < '" . $this->db->idate($datetotest) . "' AND dty.subscription = 1)"; |
||
| 111 | $sql .= " AND p.status = " . ((int) $partnership::STATUS_APPROVED); // Only accepted not yet canceled |
||
| 112 | $sql .= $this->db->order('d.rowid', 'ASC'); |
||
| 113 | // Limit is managed into loop later |
||
| 114 | |||
| 115 | $resql = $this->db->query($sql); |
||
| 116 | if ($resql) { |
||
| 117 | $numofexpiredmembers = $this->db->num_rows($resql); |
||
| 118 | |||
| 119 | $somethingdoneonpartnership = 0; |
||
| 120 | $ifetchpartner = 0; |
||
| 121 | while ($ifetchpartner < $numofexpiredmembers) { |
||
| 122 | $ifetchpartner++; |
||
| 123 | |||
| 124 | $obj = $this->db->fetch_object($resql); |
||
| 125 | if ($obj) { |
||
| 126 | if (!empty($partnershipsprocessed[$obj->rowid])) { |
||
| 127 | continue; |
||
| 128 | } |
||
| 129 | |||
| 130 | if ($somethingdoneonpartnership >= $MAXPERCALL) { |
||
| 131 | 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); |
||
| 132 | break; |
||
| 133 | } |
||
| 134 | |||
| 135 | $object = new Partnership($this->db); |
||
| 136 | $object->fetch($obj->rowid); |
||
| 137 | |||
| 138 | // Get expiration date |
||
| 139 | $expirationdate = $obj->datefin; |
||
| 140 | |||
| 141 | if ($expirationdate && $expirationdate < $now) { // If contract expired (we already had a test into main select, this is a security) |
||
| 142 | $somethingdoneonpartnership++; |
||
| 143 | |||
| 144 | $result = $object->cancel($user, 0); |
||
| 145 | // $conf->global->noapachereload = null; |
||
| 146 | if ($result < 0) { |
||
| 147 | $error++; |
||
| 148 | $this->error = $object->error; |
||
| 149 | if (is_array($object->errors) && count($object->errors)) { |
||
| 150 | if (is_array($this->errors)) { |
||
| 151 | $this->errors = array_merge($this->errors, $object->errors); |
||
| 152 | } else { |
||
| 153 | $this->errors = $object->errors; |
||
| 154 | } |
||
| 155 | } |
||
| 156 | } else { |
||
| 157 | $partnershipsprocessed[$object->id] = $object->ref; |
||
| 158 | |||
| 159 | // Send an email to inform member |
||
| 160 | $labeltemplate = '(' . getDolGlobalString('PARTNERSHIP_SENDMAIL_IF_AUTO_CANCEL', 'SendingEmailOnPartnershipCanceled') . ')'; |
||
| 161 | |||
| 162 | dol_syslog("Now we will send an email to member id=" . $object->fk_member . " with label " . $labeltemplate); |
||
| 163 | |||
| 164 | // Send deployment email |
||
| 165 | $formmail = new FormMail($this->db); |
||
| 166 | |||
| 167 | // Define output language |
||
| 168 | $outputlangs = $langs; |
||
| 169 | $newlang = ''; |
||
| 170 | if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang) && GETPOST('lang_id', 'aZ09')) { |
||
| 171 | $newlang = GETPOST('lang_id', 'aZ09'); |
||
| 172 | } |
||
| 173 | if (!empty($newlang)) { |
||
| 174 | $outputlangs = new Translate("", $conf); |
||
| 175 | $outputlangs->setDefaultLang($newlang); |
||
| 176 | $outputlangs->loadLangs(array('main', 'member', 'partnership')); |
||
| 177 | } |
||
| 178 | |||
| 179 | $arraydefaultmessage = $formmail->getEMailTemplate($this->db, 'partnership_send', $user, $outputlangs, 0, 1, $labeltemplate); |
||
| 180 | |||
| 181 | $substitutionarray = getCommonSubstitutionArray($outputlangs, 0, null, $object); |
||
| 182 | complete_substitutions_array($substitutionarray, $outputlangs, $object); |
||
| 183 | |||
| 184 | $subject = make_substitutions($arraydefaultmessage->topic, $substitutionarray, $outputlangs); |
||
| 185 | $msg = make_substitutions($arraydefaultmessage->content, $substitutionarray, $outputlangs); |
||
| 186 | $from = dol_string_nospecial($conf->global->MAIN_INFO_SOCIETE_NOM, ' ', array(",")) . ' <' . getDolGlobalString('MAIN_INFO_SOCIETE_MAIL') . '>'; |
||
| 187 | |||
| 188 | // We are in the case of autocancellation subscription because of missing backlink |
||
| 189 | $fk_partner = $object->fk_member; |
||
| 190 | |||
| 191 | $adherent = new Adherent($this->db); |
||
| 192 | $adherent->fetch($object->fk_member); |
||
| 193 | $sendto = $adherent->email; |
||
| 194 | |||
| 195 | $trackid = 'par' . $object->id; |
||
| 196 | $sendcontext = 'standard'; |
||
| 197 | |||
| 198 | $cmail = new CMailFile($subject, $sendto, $from, $msg, array(), array(), array(), '', '', 0, 1, '', '', $trackid, '', $sendcontext); |
||
| 199 | |||
| 200 | $result = $cmail->sendfile(); |
||
| 201 | |||
| 202 | if (!$result || !empty($cmail->error) || !empty($cmail->errors)) { |
||
| 203 | $erroremail .= ($erroremail ? ', ' : '') . $cmail->error; |
||
| 204 | $this->errors[] = $cmail->error; |
||
| 205 | if (is_array($cmail->errors) && count($cmail->errors) > 0) { |
||
| 206 | $this->errors += $cmail->errors; |
||
| 207 | } |
||
| 208 | } else { |
||
| 209 | // Initialisation of datas of object to call trigger |
||
| 210 | if (is_object($object)) { |
||
| 211 | $actiontypecode = 'AC_OTH_AUTO'; // Event insert into agenda automatically |
||
| 212 | $attachedfiles = array(); |
||
| 213 | |||
| 214 | $object->actiontypecode = $actiontypecode; // Type of event ('AC_OTH', 'AC_OTH_AUTO', 'AC_XXX'...) |
||
| 215 | $object->actionmsg = $arraydefaultmessage->topic . "\n" . $arraydefaultmessage->content; // Long text |
||
| 216 | $object->actionmsg2 = $langs->transnoentities("PartnershipSentByEMail", $object->ref); |
||
| 217 | ; // Short text ($langs->transnoentities('MailSentByTo')...); |
||
| 218 | if (getDolGlobalString('MAIN_MAIL_REPLACE_EVENT_TITLE_BY_EMAIL_SUBJECT')) { |
||
| 219 | $object->actionmsg2 = $subject; // Short text |
||
| 220 | } |
||
| 221 | |||
| 222 | $object->trackid = $trackid; |
||
|
|
|||
| 223 | $object->fk_element = $object->id; |
||
| 224 | $object->elementtype = $object->element; |
||
| 225 | if (is_array($attachedfiles) && count($attachedfiles) > 0) { |
||
| 226 | $object->attachedfiles = $attachedfiles; |
||
| 227 | } |
||
| 228 | |||
| 229 | $object->email_from = $from; |
||
| 230 | $object->email_subject = $subject; |
||
| 231 | $object->email_to = $sendto; |
||
| 232 | $object->email_subject = $subject; |
||
| 233 | |||
| 234 | $triggersendname = 'PARTNERSHIP_SENTBYMAIL'; |
||
| 235 | // Call of triggers (you should have set $triggersendname to execute trigger) |
||
| 236 | if (!empty($triggersendname)) { |
||
| 237 | $result = $object->call_trigger($triggersendname, $user); |
||
| 238 | if ($result < 0) { |
||
| 239 | $error++; |
||
| 240 | } |
||
| 241 | } |
||
| 242 | // End call of triggers |
||
| 243 | } |
||
| 244 | } |
||
| 245 | } |
||
| 246 | } |
||
| 247 | } |
||
| 248 | } |
||
| 249 | } else { |
||
| 250 | $error++; |
||
| 251 | $this->error = $this->db->lasterror(); |
||
| 252 | } |
||
| 253 | |||
| 254 | if (!$error) { |
||
| 255 | $this->db->commit(); |
||
| 256 | $this->output = $numofexpiredmembers . ' expired partnership members found' . "\n"; |
||
| 257 | if ($erroremail) { |
||
| 258 | $this->output .= '. Got errors when sending some email : ' . $erroremail; |
||
| 259 | } |
||
| 260 | } else { |
||
| 261 | $this->db->rollback(); |
||
| 262 | $this->output = "Rollback after error\n"; |
||
| 263 | $this->output .= $numofexpiredmembers . ' expired partnership members found' . "\n"; |
||
| 264 | if ($erroremail) { |
||
| 265 | $this->output .= '. Got errors when sending some email : ' . $erroremail; |
||
| 266 | } |
||
| 267 | } |
||
| 268 | |||
| 269 | return ($error ? 1 : 0); |
||
| 270 | } |
||
| 560 |