diff --git a/htdocs/compta/facture/mergepdftool.php b/htdocs/compta/facture/mergepdftool.php
index ea0eedfa36a..b84dd1a62c6 100644
--- a/htdocs/compta/facture/mergepdftool.php
+++ b/htdocs/compta/facture/mergepdftool.php
@@ -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__' => '
',
- //'__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.='
'.$mailfile->error.'
';
- }
- 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."
\n";
- }
- else
- {
- dol_print_error($db);
- }
- $nbsent++;
-
- }
- else
- {
- $langs->load("other");
- if ($mailfile->error)
- {
- $resultmasssend.=$langs->trans('ErrorFailedToSendMail',$from,$sendto);
- $resultmasssend.='
'.$mailfile->error.'
';
- }
- else
- {
- $resultmasssend.='No mail sent. Feature is disabled by option MAIN_DISABLE_ALL_MAILS
';
- }
- }
- }
+ $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.=''.$langs->trans('ErrorCantReadFile',$file).'
';
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__' => '
',
+ //'__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.=''.$mailfile->error.'
';
+ }
+ 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."
\n";
+ }
+ else
+ {
+ dol_print_error($db);
+ }
+ $nbsent++;
+ }
+ }
+ else
+ {
+ $langs->load("other");
+ if ($mailfile->error)
+ {
+ $resultmasssend.=$langs->trans('ErrorFailedToSendMail',$from,$sendto);
+ $resultmasssend.='
'.$mailfile->error.'
';
+ }
+ else
+ {
+ $resultmasssend.='No mail sent. Feature is disabled by option MAIN_DISABLE_ALL_MAILS
';
+ }
+ }
}
}
}
@@ -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.='&search_paymentmode='.urlencode($search_paymentmode);
if ($search_montant_ht) $param.='&search_montant_ht='.urlencode($search_montant_ht);
if ($search_montant_ttc) $param.='&search_montant_ttc='.urlencode($search_montant_ttc);
- if ($search_status) $param.='&search_status='.urlencode($search_status);
if ($late) $param.='&late='.urlencode($late);
if ($mode) $param.='&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 '