diff --git a/htdocs/compta/facture.php b/htdocs/compta/facture.php index 86a49f9f232..d5e99af0df6 100644 --- a/htdocs/compta/facture.php +++ b/htdocs/compta/facture.php @@ -3886,9 +3886,9 @@ else if ($id > 0 || ! empty($ref)) $formmail->withtocc = $liste; // List suggested for CC $formmail->withtoccc = $conf->global->MAIN_EMAIL_USECCC; if (empty($object->ref_client)) { - $formmail->withtopic = $outputlangs->transnoentities($topicmail, '__FACREF__'); + $formmail->withtopic = $outputlangs->transnoentities($topicmail, '__REF__'); } else if (! empty($object->ref_client)) { - $formmail->withtopic = $outputlangs->transnoentities($topicmail, '__FACREF__ (__REFCLIENT__)'); + $formmail->withtopic = $outputlangs->transnoentities($topicmail, '__REF__ (__REFCLIENT__)'); } $formmail->withfile = 2; @@ -3896,7 +3896,7 @@ else if ($id > 0 || ! empty($ref)) $formmail->withdeliveryreceipt = 1; $formmail->withcancel = 1; // Tableau des substitutions - $formmail->substit['__FACREF__'] = $object->ref; + $formmail->substit['__REF__'] = $object->ref; $formmail->substit['__SIGNATURE__'] = $user->signature; $formmail->substit['__REFCLIENT__'] = $object->ref_client; $formmail->substit['__THIRDPARTY_NAME__'] = $object->thirdparty->name; diff --git a/htdocs/compta/facture/class/facture.class.php b/htdocs/compta/facture/class/facture.class.php index b27bc9d2294..b58d2052b99 100644 --- a/htdocs/compta/facture/class/facture.class.php +++ b/htdocs/compta/facture/class/facture.class.php @@ -3687,11 +3687,9 @@ class Facture extends CommonInvoice $now = dol_now(); - //Paid invoices have status STATUS_CLOSED - if (!$this->statut != Facture::STATUS_VALIDATED) { - return false; - } - + // Paid invoices have status STATUS_CLOSED + if ($this->statut != Facture::STATUS_VALIDATED) return false; + return $this->date_lim_reglement < ($now - $conf->facture->client->warning_delay); } } diff --git a/htdocs/compta/facture/list.php b/htdocs/compta/facture/list.php index 9372f50f4aa..cce908dc368 100644 --- a/htdocs/compta/facture/list.php +++ b/htdocs/compta/facture/list.php @@ -60,6 +60,7 @@ $id=(GETPOST('id','int')?GETPOST('id','int'):GETPOST('facid','int')); // For ba $ref=GETPOST('ref','alpha'); $socid=GETPOST('socid','int'); $action=GETPOST('action','alpha'); +$massaction=GETPOST('massaction','alpha'); $confirm=GETPOST('confirm','alpha'); $lineid=GETPOST('lineid','int'); $userid=GETPOST('userid','int'); @@ -70,6 +71,9 @@ $search_societe=GETPOST('search_societe','alpha'); $search_montant_ht=GETPOST('search_montant_ht','alpha'); $search_montant_ttc=GETPOST('search_montant_ttc','alpha'); $search_status=GETPOST('search_status','int'); +$search_paymentmode=GETPOST('search_paymentmode','int'); +$option = GETPOST('option'); +if ($option == 'late') $filter = 'paye:0'; $sortfield = GETPOST("sortfield",'alpha'); $sortorder = GETPOST("sortorder",'alpha'); @@ -94,6 +98,7 @@ $day_lim = GETPOST('day_lim','int'); $month_lim = GETPOST('month_lim','int'); $year_lim = GETPOST('year_lim','int'); $filtre = GETPOST('filtre'); +$toselect = GETPOST('toselect', 'array'); // Security check $fieldid = (! empty($ref)?'facnumber':'rowid'); @@ -122,9 +127,290 @@ if (empty($user->socid)) $fieldstosearchall["f.note_private"]="NotePrivate"; * Actions */ +if (GETPOST('cancel')) { $action='list'; $massaction=''; } + $parameters=array('socid'=>$socid); $reshook=$hookmanager->executeHooks('doActions',$parameters,$object,$action); // Note that $action and $object may have been modified by some hooks if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors'); +if (empty($reshook)) +{ + // Mass actions + if (! empty($massaction) && count($toselect) < 1) + { + $error++; + setEventMessage("NoLineChecked","warnings"); + } + + if (! $error && $massaction == 'confirm_presend') + { + $resaction = ''; + $nbsent = 0; + $nbignored = 0; + $langs->load("mails"); + include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php'; + + if (!isset($user->email)) + { + $error++; + setEventMessages($langs->trans("NoSenderEmailDefined"), null, 'warnings'); + } + + if (! $error) + { + $thirdparty=new Societe($db); + $objecttmp=new Facture($db); + $listofobjectid=array(); + $listofobjectthirdparties=array(); + $listofobjectref=array(); + foreach($toselect as $toselectid) + { + $objecttmp=new Facture($db); // must create new instance because instance is saved into $listofobjectref array for future use + $result=$objecttmp->fetch($toselectid); + if ($result > 0) + { + $listoinvoicesid[$toselectid]=$toselectid; + $thirdpartyid=$objecttmp->fk_soc?$objecttmp->fk_soc:$objecttmp->socid; + $listofobjectthirdparties[$thirdpartyid]=$thirdpartyid; + $listofobjectref[$thirdpartyid][$toselectid]=$objecttmp; + } + } + //var_dump($listofobjectthirdparties);exit; + + foreach ($listofobjectthirdparties 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($listofobjectref[$thirdpartyid]); // Array of invoice for this thirdparty + + $attachedfiles=array('paths'=>array(), 'names'=>array(), 'mimes'=>array()); + $listofqualifiedinvoice=array(); + $listofqualifiedref=array(); + foreach($listofobjectref[$thirdpartyid] as $objectid => $object) + { + //var_dump($object); + //var_dump($thirdpartyid.' - '.$objectid.' - '.$object->statut); + + if ($object->statut != Facture::STATUS_VALIDATED) + { + $nbignored++; + continue; // Payment done or started or canceled + } + + // Read document + // TODO Use future field $object->fullpathdoc to know where is stored default file + // TODO If not defined, use $object->modelpdf (or defaut invoice config) to know what is template to use to regenerate doc. + $filename=dol_sanitizeFileName($object->ref).'.pdf'; + $filedir=$conf->facture->dir_output . '/' . dol_sanitizeFileName($object->ref); + $file = $filedir . '/' . $filename; + $mime = dol_mimetype($file); + + if (dol_is_file($file)) + { + if (empty($sendto)) // For the case, no recipient were set (multi thirdparties send) + { + $object->fetch_thirdparty(); + $sendto = $object->thirdparty->email; + } + + if (empty($sendto)) + { + //print "No recipient for thirdparty ".$object->thirdparty->name; + $nbignored++; + continue; + } + + if (dol_strlen($sendto)) + { + // Create form object + $attachedfiles=array( + 'paths'=>array_merge($attachedfiles['paths'],array($file)), + 'names'=>array_merge($attachedfiles['names'],array($filename)), + 'mimes'=>array_merge($attachedfiles['mimes'],array($mime)) + ); + } + + $listofqualifiedinvoice[$objectid]=$object; + $listofqualifiedref[$objectid]=$object->ref; + } + else + { + $nbignored++; + $langs->load("other"); + $resaction.='