Fix sending emails from mergepedtool was just doing wrong things.

This commit is contained in:
Laurent Destailleur 2015-12-03 12:47:21 +01:00
parent a6d8f943df
commit a7d03af4af

View File

@ -70,6 +70,11 @@ $diroutputpdf=$conf->facture->dir_output . '/unpaid/temp';
if (! $user->rights->societe->client->voir || $socid) $diroutputpdf.='/private/'.$user->id; // If user has no permission to see all, output dir is specific to user
$resultmasssend='';
if (GETPOST('buttonsendremind'))
{
$action='presend';
$mode='sendmassremind';
}
if (GETPOST("button_removefilter_x") || GETPOST("button_removefilter")) // Both test must be present to be compatible with all browsers
{
@ -122,15 +127,90 @@ if ($action == 'presend' && GETPOST('sendmail'))
$nbsent = 0;
$nbignored = 0;
for ($i = 0; $i < $countToSend; $i++)
$arrayofinvoices=GETPOST('toSend','array');
$thirdparty=new Societe($db);
$invoicetmp=new Facture($db);
$listofinvoicesid=array();
$listofinvoicesthirdparties=array();
$listofinvoicesref=array();
foreach($arrayofinvoices as $invoiceid)
{
$object = new Facture($db);
$result = $object->fetch($_POST['toSend'][$i]);
if ($result > 0) // Invoice was found
$invoicetmp=new Facture($db); // must create new instance because instance is saved into $listofinvoicesref array for future use
$result=$invoicetmp->fetch($invoiceid);
if ($result > 0)
{
$listoinvoicesid[$invoiceid]=$invoiceid;
$thirdpartyid=$invoicetmp->fk_soc?$invoicetmp->fk_soc:$invoicetmp->socid;
$listofinvoicesthirdparties[$thirdpartyid]=$thirdpartyid;
$listofinvoicesref[$thirdpartyid][$invoiceid]=$invoicetmp;
}
}
//var_dump($listofinvoicesref);exit;
foreach ($listofinvoicesthirdparties as $thirdpartyid)
{
$result = $thirdparty->fetch($thirdpartyid);
if ($result < 0)
{
dol_print_error($db);
exit;
}
// Define recipient $sendto and $sendtocc
if (trim($_POST['sendto']))
{
// Recipient is provided into free text
$sendto = trim($_POST['sendto']);
$sendtoid = 0;
}
elseif ($_POST['receiver'] != '-1')
{
// Recipient was provided from combo list
if ($_POST['receiver'] == 'thirdparty') // Id of third party
{
$sendto = $thirdparty->email;
$sendtoid = 0;
}
else // Id du contact
{
$sendto = $thirdparty->contact_get_property((int) $_POST['receiver'],'email');
$sendtoid = $_POST['receiver'];
}
}
if (trim($_POST['sendtocc']))
{
$sendtocc = trim($_POST['sendtocc']);
}
elseif ($_POST['receivercc'] != '-1')
{
// Recipient was provided from combo list
if ($_POST['receivercc'] == 'thirdparty') // Id of third party
{
$sendtocc = $thirdparty->email;
}
else // Id du contact
{
$sendtocc = $thirdparty->contact_get_property((int) $_POST['receivercc'],'email');
}
}
//var_dump($listofinvoicesref[$thirdpartyid]);
$attachedfiles=array('paths'=>array(), 'names'=>array(), 'mimes'=>array());
$listofqualifiedinvoice=array();
$listofqualifiedref=array();
foreach($listofinvoicesref[$thirdpartyid] as $invoiceid => $invoice)
{
//var_dump($invoice);
$object = $invoice;
//$object = new Facture($db);
//$result = $object->fetch();
//var_dump($thirdpartyid.' - '.$invoiceid.' - '.$object->statut);
if ($object->statut != Facture::STATUS_VALIDATED)
{
$nbignored++;
continue; // Payment done or started or canceled
}
@ -144,108 +224,30 @@ if ($action == 'presend' && GETPOST('sendmail'))
if (dol_is_file($file))
{
$object->fetch_thirdparty();
$sendto = $object->thirdparty->email;
if (empty($sendto)) // For the case, no recipient were set (multi thirdparties send)
{
$object->fetch_thirdparty();
$sendto = $object->thirdparty->email;
}
if (empty($sendto)) $nbignored++;
if (empty($sendto))
{
$nbignored++;
continue;
}
if (dol_strlen($sendto))
{
$langs->load("commercial");
$from = $user->getFullName($langs) . ' <' . $user->email .'>';
$replyto = $from;
$subject = GETPOST('subject');
$message = GETPOST('message');
$sendtocc = GETPOST('sentocc');
$sendtobcc = (empty($conf->global->MAIN_MAIL_AUTOCOPY_INVOICE_TO)?'':$conf->global->MAIN_MAIL_AUTOCOPY_INVOICE_TO);
$substitutionarray=array(
'__ID__' => $object->id,
'__EMAIL__' => $object->thirdparty->email,
'__CHECK_READ__' => '<img src="'.DOL_MAIN_URL_ROOT.'/public/emailing/mailing-read.php?tag='.$obj2->tag.'&securitykey='.urlencode($conf->global->MAILING_EMAIL_UNSUBSCRIBE_KEY).'" width="1" height="1" style="width:1px;height:1px" border="0"/>',
//'__LASTNAME__' => $obj2->lastname,
//'__FIRSTNAME__' => $obj2->firstname,
'__FACREF__' => $object->ref, // For backward compatibility
'__REF__' => $object->ref,
'__REFCLIENT__' => $object->thirdparty->name
);
$subject=make_substitutions($subject, $substitutionarray);
$message=make_substitutions($message, $substitutionarray);
$actiontypecode='AC_FAC';
$actionmsg=$langs->transnoentities('MailSentBy').' '.$from.' '.$langs->transnoentities('To').' '.$sendto;
if ($message)
{
if ($sendtocc) $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('Bcc') . ": " . $sendtocc);
$actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('MailTopic') . ": " . $subject);
$actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('TextUsedInTheMessageBody') . ":");
$actionmsg = dol_concatdesc($actionmsg, $message);
}
// Create form object
$attachedfiles=array('paths'=>array($file), 'names'=>array($filename), 'mimes'=>array($mime));
$filepath = $attachedfiles['paths'];
$filename = $attachedfiles['names'];
$mimetype = $attachedfiles['mimes'];
// Send mail
require_once(DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php');
$mailfile = new CMailFile($subject,$sendto,$from,$message,$filepath,$mimetype,$filename,$sendtocc,$sendtobcc,$deliveryreceipt,-1);
if ($mailfile->error)
{
$resultmasssend.='<div class="error">'.$mailfile->error.'</div>';
}
else
{
$result=$mailfile->sendfile();
if ($result)
{
$resultmasssend.=$langs->trans('MailSuccessfulySent',$mailfile->getValidAddress($from,2),$mailfile->getValidAddress($sendto,2)); // Must not contain "
$error=0;
// Initialisation donnees
$object->sendtoid = 0;
$object->actiontypecode = $actiontypecode;
$object->actionmsg = $actionmsg; // Long text
$object->actionmsg2 = $actionmsg2; // Short text
$object->fk_element = $object->id;
$object->elementtype = $object->element;
// Appel des triggers
include_once(DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php");
$interface=new Interfaces($db);
$result=$interface->run_triggers('BILL_SENTBYMAIL',$object,$user,$langs,$conf);
if ($result < 0) { $error++; $this->errors=$interface->errors; }
// Fin appel triggers
if (! $error)
{
$resultmasssend.=$langs->trans("MailSent").': '.$sendto."<br>\n";
}
else
{
dol_print_error($db);
}
$nbsent++;
}
else
{
$langs->load("other");
if ($mailfile->error)
{
$resultmasssend.=$langs->trans('ErrorFailedToSendMail',$from,$sendto);
$resultmasssend.='<br><div class="error">'.$mailfile->error.'</div>';
}
else
{
$resultmasssend.='<div class="warning">No mail sent. Feature is disabled by option MAIN_DISABLE_ALL_MAILS</div>';
}
}
}
$attachedfiles=array(
'paths'=>array_merge($attachedfiles['paths'],array($file)),
'names'=>array_merge($attachedfiles['names'],array($filename)),
'mimes'=>array_merge($attachedfiles['mimes'],array($mime))
);
}
$listofqualifiedinvoice[$invoiceid]=$invoice;
$listofqualifiedref[$invoiceid]=$invoice->ref;
}
else
{
@ -253,6 +255,109 @@ if ($action == 'presend' && GETPOST('sendmail'))
$langs->load("other");
$resultmasssend.='<div class="error">'.$langs->trans('ErrorCantReadFile',$file).'</div>';
dol_syslog('Failed to read file: '.$file, LOG_WARNING);
continue;
}
//var_dump($listofqualifiedref);
}
if (count($listofqualifiedinvoice) > 0)
{
$langs->load("commercial");
$from = $user->getFullName($langs) . ' <' . $user->email .'>';
$replyto = $from;
$subject = GETPOST('subject');
$message = GETPOST('message');
$sendtocc = GETPOST('sentocc');
$sendtobcc = (empty($conf->global->MAIN_MAIL_AUTOCOPY_INVOICE_TO)?'':$conf->global->MAIN_MAIL_AUTOCOPY_INVOICE_TO);
$substitutionarray=array(
'__ID__' => join(', ',array_keys($listofqualifiedinvoice)),
'__EMAIL__' => $thirdparty->email,
'__CHECK_READ__' => '<img src="'.DOL_MAIN_URL_ROOT.'/public/emailing/mailing-read.php?tag='.$thirdparty->tag.'&securitykey='.urlencode($conf->global->MAILING_EMAIL_UNSUBSCRIBE_KEY).'" width="1" height="1" style="width:1px;height:1px" border="0"/>',
//'__LASTNAME__' => $obj2->lastname,
//'__FIRSTNAME__' => $obj2->firstname,
'__FACREF__' => join(', ',$listofqualifiedref), // For backward compatibility
'__REF__' => join(', ',$listofqualifiedref),
'__REFCLIENT__' => $thirdparty->name
);
$subject=make_substitutions($subject, $substitutionarray);
$message=make_substitutions($message, $substitutionarray);
$filepath = $attachedfiles['paths'];
$filename = $attachedfiles['names'];
$mimetype = $attachedfiles['mimes'];
//var_dump($filepath);
// Send mail
require_once(DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php');
$mailfile = new CMailFile($subject,$sendto,$from,$message,$filepath,$mimetype,$filename,$sendtocc,$sendtobcc,$deliveryreceipt,-1);
if ($mailfile->error)
{
$resultmasssend.='<div class="error">'.$mailfile->error.'</div>';
}
else
{
$result=$mailfile->sendfile();
if ($result)
{
$resultmasssend.=$langs->trans('MailSuccessfulySent',$mailfile->getValidAddress($from,2),$mailfile->getValidAddress($sendto,2)); // Must not contain "
$error=0;
foreach($listofqualifiedinvoice as $invid => $object)
{
$actiontypecode='AC_FAC';
$actionmsg=$langs->transnoentities('MailSentBy').' '.$from.' '.$langs->transnoentities('To').' '.$sendto;
if ($message)
{
if ($sendtocc) $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('Bcc') . ": " . $sendtocc);
$actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('MailTopic') . ": " . $subject);
$actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('TextUsedInTheMessageBody') . ":");
$actionmsg = dol_concatdesc($actionmsg, $message);
}
// Initialisation donnees
$object->sendtoid = 0;
$object->actiontypecode = $actiontypecode;
$object->actionmsg = $actionmsg; // Long text
$object->actionmsg2 = $actionmsg2; // Short text
$object->fk_element = $invid;
$object->elementtype = $invoice->element;
// Appel des triggers
include_once(DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php");
$interface=new Interfaces($db);
$result=$interface->run_triggers('BILL_SENTBYMAIL',$object,$user,$langs,$conf);
if ($result < 0) { $error++; $this->errors=$interface->errors; }
// Fin appel triggers
if (! $error)
{
$resultmasssend.=$langs->trans("MailSent").': '.$sendto."<br>\n";
}
else
{
dol_print_error($db);
}
$nbsent++;
}
}
else
{
$langs->load("other");
if ($mailfile->error)
{
$resultmasssend.=$langs->trans('ErrorFailedToSendMail',$from,$sendto);
$resultmasssend.='<br><div class="error">'.$mailfile->error.'</div>';
}
else
{
$resultmasssend.='<div class="warning">No mail sent. Feature is disabled by option MAIN_DISABLE_ALL_MAILS</div>';
}
}
}
}
}
@ -417,7 +522,6 @@ $search_societe = GETPOST("search_societe");
$search_paymentmode = GETPOST("search_paymentmode");
$search_montant_ht = GETPOST("search_montant_ht");
$search_montant_ttc = GETPOST("search_montant_ttc");
$search_status = GETPOST("search_status");
$late = GETPOST("late");
// Do we click on purge search criteria ?
@ -429,7 +533,6 @@ if (GETPOST("button_removefilter_x") || GETPOST("button_removefilter")) // Both
$search_paymentmode='';
$search_montant_ht='';
$search_montant_ttc='';
$search_status='';
}
$sortfield = GETPOST("sortfield",'alpha');
@ -485,7 +588,6 @@ if ($search_paymentmode) $sql .= " AND f.fk_mode_reglement = ".$search_paymentmo
if ($search_montant_ht) $sql .= " AND f.total = '".$db->escape($search_montant_ht)."'";
if ($search_montant_ttc) $sql .= " AND f.total_ttc = '".$db->escape($search_montant_ttc)."'";
if (GETPOST('sf_ref')) $sql .= " AND f.facnumber LIKE '%".$db->escape(GETPOST('sf_ref'))."%'";
if ($search_status) $sql .= " AND f.fk_statut = ".$search_status;
if ($month > 0)
{
if ($year > 0)
@ -531,7 +633,6 @@ if ($resql)
if ($search_societe) $param.='&amp;search_paymentmode='.urlencode($search_paymentmode);
if ($search_montant_ht) $param.='&amp;search_montant_ht='.urlencode($search_montant_ht);
if ($search_montant_ttc) $param.='&amp;search_montant_ttc='.urlencode($search_montant_ttc);
if ($search_status) $param.='&amp;search_status='.urlencode($search_status);
if ($late) $param.='&amp;late='.urlencode($late);
if ($mode) $param.='&amp;mode='.urlencode($mode);
$urlsource=$_SERVER['PHP_SELF'].'?sortfield='.$sortfield.'&sortorder='.$sortorder;
@ -551,6 +652,31 @@ if ($resql)
print_fiche_titre($titre,$link);
//print_barre_liste($titre,$page,$_SERVER["PHP_SELF"],$param,$sortfield,$sortorder,'',0); // We don't want pagination on this page
$arrayofinvoices=GETPOST('toSend','array');
if ($action == 'presend' && count($arrayofinvoices) == 0 && ! GETPOST('cancel'))
{
setEventMessages($langs->trans("InvoiceNotChecked"), null, 'errors');
$action='list';
$mode='sendmassremind';
}
else
{
$invoicetmp=new Facture($db);
$listofinvoicesid=array();
$listofinvoicesthirdparties=array();
$listofinvoicesref=array();
foreach($arrayofinvoices as $invoiceid)
{
$result=$invoicetmp->fetch($invoiceid);
if ($result > 0)
{
$listofinvoicesid[$invoiceid]=$invoiceid;
$thirdpartyid=$invoicetmp->fk_soc?$invoicetmp->fk_soc:$invoicetmp->socid;
$listofinvoicesthirdparties[$thirdpartyid]=$thirdpartyid;
$listofinvoicesref[$thirdpartyid][$invoiceid]=$invoicetmp->ref;
}
}
}
print '<form id="form_unpaid" method="POST" action="'.$_SERVER["PHP_SELF"].'?sortfield='. $sortfield .'&sortorder='. $sortorder .'">';
if (GETPOST('modelselected')) {
@ -575,10 +701,25 @@ if ($resql)
$formmail->fromname = $user->getFullName($langs);
$formmail->frommail = $user->email;
$formmail->withfrom=1;
$liste=array();
$formmail->withto=$langs->trans("AllRecipientSelectedForRemind");
$liste=$langs->trans("AllRecipientSelectedForRemind");
if (count($listofinvoicesthirdparties) == 1)
{
$liste=array();
$thirdpartyid=array_shift($listofinvoicesthirdparties);
$soc=new Societe($db);
$soc->fetch($thirdpartyid);
foreach ($soc->thirdparty_and_contact_email_array(1) as $key=>$value)
{
$liste[$key]=$value;
}
$formmail->withtoreadonly=0;
}
else
{
$formmail->withtoreadonly=1;
}
$formmail->withto=$liste;
$formmail->withtofree=0;
$formmail->withtoreadonly=1;
$formmail->withtocc=1;
$formmail->withtoccc=$conf->global->MAIN_EMAIL_USECCC;
$formmail->withtopic=$langs->transnoentities($topicmail, '__FACREF__', '__REFCLIENT__');
@ -597,7 +738,7 @@ if ($resql)
$formmail->param['action']=$action;
$formmail->param['models']=$modelmail;
$formmail->param['models_id']=GETPOST('modelmailselected','int');
$formmail->param['facid']=$object->id;
$formmail->param['facid']=join(',',$arrayofinvoices);
$formmail->param['returnurl']=$_SERVER["PHP_SELF"].'?id='.$object->id;
print $formmail->get_form();
@ -822,7 +963,9 @@ if ($resql)
{
// Checkbox to send remind
print '<td class="nowrap" align="center">';
if ($objp->email) print '<input class="flat checkforsend" type="checkbox" name="toSend[]" value="'.$objp->facid.'">';
$selected=0;
if (in_array($objp->facid, $arrayofinvoices)) $selected=1;
if ($objp->email) print '<input class="flat checkforsend" type="checkbox" name="toSend[]" value="'.$objp->facid.'"'.($selected?' checked="checked"':'').'>';
else print img_picto($langs->trans("NoEMail"), 'warning.png');
print '</td>' ;
}
@ -869,7 +1012,7 @@ if ($resql)
if ($action != 'presend')
{
print '<div class="tabsAction">';
print '<a href="'.$_SERVER["PHP_SELF"].'?mode=sendremind&action=presend" class="butAction" name="buttonsendremind" value="'.dol_escape_htmltag($langs->trans("SendRemind")).'">'.$langs->trans("SendRemind").'</a>';
print '<input type="submit" class="butAction" name="buttonsendremind" value="'.dol_escape_htmltag($langs->trans("SendRemind")).'">';
print '</div>';
print '<br>';
}