Skip to content

Commit 0249486

Browse files
committed
FIX code for email template to use to send email for credit transfer
payment.
1 parent ac91765 commit 0249486

File tree

4 files changed

+83
-76
lines changed

4 files changed

+83
-76
lines changed

ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
* FIX Redirection instances must not appear in the count of backuped instances.
4747
* FIX letsencrypt for wildcard are now using 2 passes and needs 2 TXT entry in DNS.
4848
* FIX Debug master_move_instance.php - Price of old instance is kept/reused.
49+
* FIX code for email template to use to send email for credit transfer payment.
4950
* SEC When using ssh, a user can't see the OS and package information.
5051

5152

class/sellyoursaasutils.class.php

Lines changed: 78 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function __construct($db)
6161

6262
/**
6363
* Action executed by scheduler for job SellYourSaasValidateDraftInvoices
64-
* Search draft invoices on sellyoursaas customers and check they are linked to a not closed contract. Validate it if not, do nothing if closed.
64+
* Search draft invoices on sellyoursaas customers and check they are linked to a not closed contract. Validate it if not closed, do nothing if closed.
6565
* CAN BE A CRON TASK
6666
*
6767
* @param int $restrictonthirdpartyid 0=All qualified draft invoices, >0 = Restrict on qualified draft invoice of thirdparty.
@@ -352,7 +352,7 @@ public function doValidateDraftInvoices($restrictonthirdpartyid = 0, $maxtoproce
352352
$msg = '';
353353
if ($invoice->mode_reglement_code == $codepaiementtransfer) {
354354
$sendemailtocustomer = 1;
355-
$labeltouse = 'InvoicePaymentWaitingCreditTransfer';
355+
$labeltouse = 'YourLinkForYourPayment';
356356
}
357357

358358
$object = $invoice;
@@ -380,88 +380,93 @@ public function doValidateDraftInvoices($restrictonthirdpartyid = 0, $maxtoproce
380380
$msg = $arraydefaultmessage->content;
381381
}
382382

383-
$substitutionarray = getCommonSubstitutionArray($outputlangs, 0, null, $object);
384-
385-
complete_substitutions_array($substitutionarray, $outputlangs, $object);
386-
387-
// Set the property ->ref_customer with ref_customer of contract so __REF_CLIENT__ will be replaced in email content
388-
// Search contract linked to invoice
389-
$foundcontract = null;
390-
$invoice->fetchObjectLinked(null, '', null, '', 'OR', 1, 'sourcetype', 1);
391-
392-
if (is_array($invoice->linkedObjects['contrat']) && count($invoice->linkedObjects['contrat']) > 0) {
393-
//dol_sort_array($object->linkedObjects['facture'], 'date');
394-
foreach ($invoice->linkedObjects['contrat'] as $idcontract => $contract) {
395-
$substitutionarray['__CONTRACT_REF__']=$contract->ref_customer;
396-
$substitutionarray['__REFCLIENT__']=$contract->ref_customer; // For backward compatibility
397-
$substitutionarray['__REF_CLIENT__']=$contract->ref_customer;
398-
$substitutionarray['__REF_CUSTOMER__']=$contract->ref_customer;
399-
$foundcontract = $contract;
400-
break;
383+
if (empty($subject)) {
384+
dol_syslog("Can't send email to inform about invoice availability because template ".$labeltouse." not found", LOG_WARNING);
385+
} else {
386+
$substitutionarray = getCommonSubstitutionArray($outputlangs, 0, null, $object);
387+
388+
complete_substitutions_array($substitutionarray, $outputlangs, $object);
389+
390+
// Set the property ->ref_customer with ref_customer of contract so __REF_CLIENT__ will be replaced in email content
391+
// Search contract linked to invoice
392+
$foundcontract = null;
393+
$invoice->fetchObjectLinked(null, '', null, '', 'OR', 1, 'sourcetype', 1);
394+
395+
if (is_array($invoice->linkedObjects['contrat']) && count($invoice->linkedObjects['contrat']) > 0) {
396+
//dol_sort_array($object->linkedObjects['facture'], 'date');
397+
foreach ($invoice->linkedObjects['contrat'] as $idcontract => $contract) {
398+
$substitutionarray['__CONTRACT_REF__'] = $contract->ref_customer;
399+
$substitutionarray['__REFCLIENT__'] = $contract->ref_customer; // For backward compatibility
400+
$substitutionarray['__REF_CLIENT__'] = $contract->ref_customer;
401+
$substitutionarray['__REF_CUSTOMER__'] = $contract->ref_customer;
402+
$foundcontract = $contract;
403+
break;
404+
}
401405
}
402-
}
403406

404-
dol_syslog('__DIRECTDOWNLOAD_URL_INVOICE__='.$substitutionarray['__DIRECTDOWNLOAD_URL_INVOICE__']);
407+
dol_syslog('__DIRECTDOWNLOAD_URL_INVOICE__='.$substitutionarray['__DIRECTDOWNLOAD_URL_INVOICE__']);
405408

406-
$urlforsellyoursaasaccount = getRootUrlForAccount($foundcontract);
407-
if ($urlforsellyoursaasaccount) {
408-
$tmpforurl=preg_replace('/.*document.php/', '', $substitutionarray['__DIRECTDOWNLOAD_URL_INVOICE__']);
409-
if ($tmpforurl) {
410-
$substitutionarray['__DIRECTDOWNLOAD_URL_INVOICE__']=$urlforsellyoursaasaccount.'/source/document.php'.$tmpforurl;
411-
} else {
412-
$substitutionarray['__DIRECTDOWNLOAD_URL_INVOICE__']=$urlforsellyoursaasaccount;
409+
$urlforsellyoursaasaccount = getRootUrlForAccount($foundcontract);
410+
if ($urlforsellyoursaasaccount) {
411+
$tmpforurl=preg_replace('/.*document.php/', '', $substitutionarray['__DIRECTDOWNLOAD_URL_INVOICE__']);
412+
if ($tmpforurl) {
413+
$substitutionarray['__DIRECTDOWNLOAD_URL_INVOICE__']=$urlforsellyoursaasaccount.'/source/document.php'.$tmpforurl;
414+
} else {
415+
$substitutionarray['__DIRECTDOWNLOAD_URL_INVOICE__']=$urlforsellyoursaasaccount;
416+
}
413417
}
414-
}
415418

416-
$subjecttosend = make_substitutions($subject, $substitutionarray, $outputlangs);
417-
$texttosend = make_substitutions($msg, $substitutionarray, $outputlangs);
418-
419-
// Attach a file ?
420-
$file='';
421-
$listofpaths=array();
422-
$listofnames=array();
423-
$listofmimes=array();
424-
if (is_object($invoice)) {
425-
$invoicediroutput = $conf->facture->dir_output;
426-
$fileparams = dol_most_recent_file($invoicediroutput . '/' . $invoice->ref, preg_quote($invoice->ref, '/').'[^\-]+');
427-
$file = $fileparams['fullname'];
428-
$file = ''; // Disable attachment of invoice in emails
429-
430-
if ($file) {
431-
$listofpaths=array($file);
432-
$listofnames=array(basename($file));
433-
$listofmimes=array(dol_mimetype($file));
419+
$subjecttosend = make_substitutions($subject, $substitutionarray, $outputlangs);
420+
$texttosend = make_substitutions($msg, $substitutionarray, $outputlangs);
421+
422+
// Attach a file ?
423+
$file='';
424+
$listofpaths=array();
425+
$listofnames=array();
426+
$listofmimes=array();
427+
if (is_object($invoice)) {
428+
$invoicediroutput = $conf->facture->dir_output;
429+
$fileparams = dol_most_recent_file($invoicediroutput . '/' . $invoice->ref, preg_quote($invoice->ref, '/').'[^\-]+');
430+
$file = $fileparams['fullname'];
431+
$file = ''; // Disable attachment of invoice in emails
432+
433+
if ($file) {
434+
$listofpaths=array($file);
435+
$listofnames=array(basename($file));
436+
$listofmimes=array(dol_mimetype($file));
437+
}
438+
}
439+
$from = getDolGlobalString('SELLYOURSAAS_NOREPLY_EMAIL');
440+
441+
$trackid = 'inv'.$invoice->id;
442+
$moreinheader = 'X-Dolibarr-Info: doValidateInvoice'."\r\n";
443+
$addr_cc = '';
444+
if (!empty($invoice->thirdparty->array_options['options_emailccinvoice'])) {
445+
dol_syslog("We add the recipient ".$invoice->thirdparty->array_options['options_emailccinvoice']." as CC to warn about invoice availability (direct debit or credit transfer case)", LOG_DEBUG);
446+
$addr_cc = $invoice->thirdparty->array_options['options_emailccinvoice'];
447+
} else {
448+
dol_syslog("We do NOT found recipient to add as CC to warn about invoice availability (direct debit or credit transfer case)", LOG_DEBUG);
434449
}
435-
}
436-
$from = getDolGlobalString('SELLYOURSAAS_NOREPLY_EMAIL');
437-
438-
$trackid = 'inv'.$invoice->id;
439-
$moreinheader = 'X-Dolibarr-Info: doValidateInvoice'."\r\n";
440-
$addr_cc = '';
441-
if (!empty($invoice->thirdparty->array_options['options_emailccinvoice'])) {
442-
dol_syslog("We add the recipient ".$invoice->thirdparty->array_options['options_emailccinvoice']." as CC to warn about invoice availability (direct debit or credit transfer case)", LOG_DEBUG);
443-
$addr_cc = $invoice->thirdparty->array_options['options_emailccinvoice'];
444-
}
445450

446-
// Send email (substitutionarray must be done just before this)
447-
include_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
448-
$mailfile = new CMailFile($subjecttosend, $invoice->thirdparty->email, $from, $texttosend, $listofpaths, $listofmimes, $listofnames, $addr_cc, '', 0, -1, '', '', $trackid, $moreinheader);
449-
if (empty($mailfile->error) && $mailfile->sendfile()) {
450-
$result = 1;
451-
} else {
452-
$errmsg = $langs->trans("ErrorFailedToSendMail", $from, $invoice->thirdparty->email).'. '.$mailfile->error;
451+
// Send email (substitutionarray must be done just before this)
452+
include_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
453+
$mailfile = new CMailFile($subjecttosend, $invoice->thirdparty->email, $from, $texttosend, $listofpaths, $listofmimes, $listofnames, $addr_cc, '', 0, -1, '', '', $trackid, $moreinheader);
454+
if (empty($mailfile->error) && $mailfile->sendfile()) {
455+
$result = 1;
456+
} else {
457+
$errmsg = $langs->trans("ErrorFailedToSendMail", $from, $invoice->thirdparty->email).($addr_cc ? '. CC='.$addr_cc : '').'. '.$mailfile->error;
453458

454-
dol_syslog($errmsg, LOG_WARNING);
459+
dol_syslog($errmsg, LOG_WARNING);
455460

456-
$this->errors[] = $errmsg;
457-
$result = -1;
458-
}
461+
$this->errors[] = $errmsg;
462+
$result = -1;
463+
}
459464

460-
if ($result > 0) {
461-
// TODO Add an event to say email was sent or the event invoice validate is enough ?
465+
if ($result > 0) {
466+
// TODO Add an event to say email was sent or the event invoice validate is enough ?
467+
}
462468
}
463469
}
464-
465470
}
466471
}
467472
}
@@ -490,6 +495,7 @@ public function doValidateDraftInvoices($restrictonthirdpartyid = 0, $maxtoproce
490495
$this->errors[] = $this->error;
491496
}
492497

498+
// Set output of cron job
493499
$this->output = count($draftinvoiceprocessed).' invoice(s) validated on '.$num_rows.' draft invoice(s) found'.(count($draftinvoiceprocessed)>0 ? ' : '.join(',', $draftinvoiceprocessed) : '').' (search done on invoices of SellYourSaas customers only)';
494500

495501
if (!empty($this->errors)) {

core/modules/modSellYourSaas.class.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ public function __construct($db)
170170

171171
$this->cronjobs = array(
172172
// Generation of draft invoices is done with priority 50
173-
0 =>array('priority'=>61, 'label'=>'SellYourSaasValidateDraftInvoices', 'jobtype'=>'method', 'class'=>'/sellyoursaas/class/sellyoursaasutils.class.php', 'objectname'=>'SellYourSaasUtils', 'method'=>'doValidateDraftInvoices', 'parameters'=>'', 'comment'=>'Search draft invoices on sellyoursaas customers and check they are linked to a not closed contract. Validate it if not and if there is not another validated invoice, do nothing if closed. You can set the id of a thirdparty as parameter to restrict the batch to a given thirdparty.', 'frequency'=>1, 'unitfrequency'=>86400, 'status'=>$statusatinstall, 'test'=>'isModEnabled("sellyoursaas")', 'datestart'=>$datestart),
173+
0 =>array('priority'=>61, 'label'=>'SellYourSaasValidateDraftInvoices', 'jobtype'=>'method', 'class'=>'/sellyoursaas/class/sellyoursaasutils.class.php', 'objectname'=>'SellYourSaasUtils', 'method'=>'doValidateDraftInvoices', 'parameters'=>'', 'comment'=>'Search draft invoices on sellyoursaas customers and check they are linked to a not closed contract. Validate it if linked to a not closed contract and if there is not another validated invoice, do nothing if linked to a closed contract. You can set the id of a thirdparty as parameter to restrict the batch to a given thirdparty.', 'frequency'=>1, 'unitfrequency'=>86400, 'status'=>$statusatinstall, 'test'=>'isModEnabled("sellyoursaas")', 'datestart'=>$datestart),
174174

175175
1 =>array('priority'=>62, 'label'=>'SellYourSaasAlertSoftEndTrial', 'jobtype'=>'method', 'class'=>'/sellyoursaas/class/sellyoursaasutils.class.php', 'objectname'=>'SellYourSaasUtils', 'method'=>'doAlertSoftEndTrial', 'parameters'=>'', 'comment'=>'Search contracts of sellyoursaas customers that are deployed + with open lines + about to expired (= date between (end date - SELLYOURSAAS_NBDAYS_BEFORE_TRIAL_END_FOR_SOFT_ALERT) and (end date - SELLYOURSAAS_NBDAYS_BEFORE_TRIAL_END_FOR_SOFT_ALERT + 7)) + not yet already warned (date_softalert_endfreeperiod is null), then send email remind (using email template called GentleTrialExpiringReminder)', 'frequency'=>30, 'unitfrequency'=>60, 'status'=>$statusatinstall, 'test'=>'isModEnabled("sellyoursaas")', 'datestart'=>$datestart),
176176

0 commit comments

Comments
 (0)