From ae0b1bd43c93194e34c6db505efcca6b2a3b5808 Mon Sep 17 00:00:00 2001 From: Rodolphe Quiedeville Date: Tue, 6 Sep 2005 10:19:29 +0000 Subject: [PATCH] Nouveau fichier --- htdocs/telephonie/client/facture.php | 749 +++++++++++++++++++++++++++ 1 file changed, 749 insertions(+) create mode 100644 htdocs/telephonie/client/facture.php diff --git a/htdocs/telephonie/client/facture.php b/htdocs/telephonie/client/facture.php new file mode 100644 index 00000000000..bdb710249fd --- /dev/null +++ b/htdocs/telephonie/client/facture.php @@ -0,0 +1,749 @@ + + * Copyright (C) 2004 Éric Seigne + * Copyright (C) 2004-2005 Laurent Destailleur + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * $Id$ + * $Source$ + */ + +/** + \file htdocs/compta/facture.php + \ingroup facture + \brief Page de création d'une facture + \version $Revision$ +*/ + +require("./pre.inc.php"); + +$user->getrights('facture'); +$user->getrights('banque'); + +if (!$user->rights->facture->lire) +accessforbidden(); + +$langs->load("bills"); + + +$warning_delay=31*24*60*60; // Delai affichage warning retard (si retard paiement facture > delai) + + +require_once DOL_DOCUMENT_ROOT."/facture.class.php"; +require_once DOL_DOCUMENT_ROOT."/paiement.class.php"; +require_once DOL_DOCUMENT_ROOT."/lib/CMailFile.class.php"; + + +if ($_GET["socidp"]) { $socidp=$_GET["socidp"]; } +if (isset($_GET["msg"])) { $msg=urldecode($_GET["msg"]); } + +// Sécurité accés client +if ($user->societe_id > 0) +{ + $action = ''; + $socidp = $user->societe_id; +} + +// Nombre de ligne pour choix de produit/service prédéfinis +$NBLINES=4; + + +/* + * Action envoi de mail + */ +if ($_POST["action"] == 'send' || $_POST["action"] == 'relance') +{ + $langs->load("mails"); + + $fac = new Facture($db,"",$_POST["facid"]); + if ( $fac->fetch($_POST["facid"]) ) + { + $facref = sanitize_string($fac->ref); + $file = $conf->facture->dir_output . "/" . $facref . "/" . $facref . ".pdf"; + + if (is_readable($file)) + { + $soc = new Societe($db, $fac->socidp); + + if ($_POST["sendto"]) { + // Le destinataire a été fourni via le champ libre + $sendto = $_POST["sendto"]; + $sendtoid = 0; + } + elseif ($_POST["receiver"]) { + // Le destinataire a été fourni via la liste déroulante + $sendto = $soc->contact_get_email($_POST["receiver"]); + $sendtoid = $_POST["receiver"]; + } + + if (strlen($sendto)) + { + $from = $_POST["fromname"] . " <" . $_POST["frommail"] .">"; + $replyto = $_POST["replytoname"]. " <" . $_POST["replytomail"].">"; + $message = $_POST["message"]; + if ($_POST["action"] == 'send') { + $subject = $langs->trans("Bill")." $fac->ref"; + $actiontypeid=9; + $actionmsg ="Mail envoyé par $from à $sendto.
"; + if ($message) { + $actionmsg.="Texte utilisé dans le corps du message:
"; + $actionmsg.="$message"; + } + $actionmsg2="Envoi facture par mail"; + } + if ($_POST["action"] == 'relance') { + $subject = "Relance facture $fac->ref"; + $actiontypeid=10; + $actionmsg="Mail envoyé par $from à $sendto.
"; + if ($message) { + $actionmsg.="Texte utilisé dans le corps du message:
"; + $actionmsg.="$message"; + } + $actionmsg2="Relance facture par mail"; + } + + $filepath[0] = $file; + $filename[0] = $fac->ref.".pdf"; + $mimetype[0] = "application/pdf"; + $filepath[1] = $_FILES['addedfile']['tmp_name']; + $filename[1] = $_FILES['addedfile']['name']; + $mimetype[1] = $_FILES['addedfile']['type']; + + // Envoi de la facture + $mailfile = new CMailFile($subject,$sendto,$from,$message,$filepath,$mimetype,$filename,$sendtocc); + + if ($mailfile->sendfile()) + { + $msg='
'.$langs->trans("MailSuccessfulySent",$from,$sendto).'.
'; + + // Insertion action + include_once("../contact.class.php"); + include_once("../actioncomm.class.php"); + $actioncomm = new ActionComm($db); + $actioncomm->type_id = $actiontypeid; + $actioncomm->label = $actionmsg2; + $actioncomm->note = $actionmsg; + $actioncomm->date = time(); + $actioncomm->percent = 100; + $actioncomm->contact = new Contact($db,$sendtoid); + $actioncomm->societe = new Societe($db,$fac->socidp); + $actioncomm->user = $user; // User qui a fait l'action + $actioncomm->facid = $fac->id; + + $ret=$actioncomm->add($user); // User qui saisi l'action + + if ($ret < 0) + { + dolibarr_print_error($db); + } + else + { + // Renvoie sur la fiche + Header("Location: facture.php?facid=".$fac->id."&msg=".urlencode($msg)); + exit; + } + } + else + { + $msg='
'.$langs->trans("ErrorFailedToSendMail",$from,$sendto).' !
'; + } + } + else + { + $msg='
'.$langs->trans("ErrorMailRecipientIsEmpty").'
'; + dolibarr_syslog("Le mail du destinataire est vide"); + } + + } + else + { + dolibarr_syslog("Impossible de lire :".$file); + } + } + else + { + dolibarr_syslog("Impossible de lire les données de la facture. Le fichier facture n'a peut-être pas été généré."); + } +} + + + + +llxHeader('',$langs->trans("Bill"),'Facture'); + +$html = new Form($db); + + + + +if ($_GET["facid"] > 0) +{ + if ($msg) print "$msg
"; + + $fac = New Facture($db); + if ( $fac->fetch($_GET["facid"], $user->societe_id) > 0) + { + + $soc = new Societe($db, $fac->socidp); + $soc->fetch($fac->socidp); + + $author = new User($db); + $author->id = $fac->user_author; + $author->fetch(); + + $h = 0; + + $head[$h][0] = DOL_URL_ROOT.'/telephonie/client/fiche.php?id='.$soc->id; + $head[$h][1] = $langs->trans("Fiche client"); + $h++; + + $head[$h][0] = DOL_URL_ROOT.'/telephonie/client/factures.php?id='.$soc->id; + $head[$h][1] = $langs->trans("Factures"); + $h++; + + $head[$h][0] = DOL_URL_ROOT.'/telephonie/client/facture.php?facid='.$fac->id; + $head[$h][1] = $langs->trans("CardBill"); + $hselected = $h; + $h++; + + dolibarr_fiche_head($head, $hselected, $langs->trans("Bill")." : $fac->ref"); + + /* + * Facture + */ + print ''; + print ''; + print ''; + + print ""; + + print ''; + print "\n"; + print '"; + + print ''; + + // Projet + if ($conf->projet->enabled) + { + $langs->load("projects"); + print '"; + } else { + print ''; + } + + print '"; + + print ""; + + print ''; + if ($fac->brouillon == 1 && $user->rights->facture->creer) + { + print ''; + print ''; + print ''; + print ''; + } + else + { + print ''; + } + print ''; + + print ''; + print ''; + print ''; + + print ''; + print ''; + print ''; + print ''; + + print ''; + + if ($fac->note) + { + print '"; + } + else { + print ''; + } + + print "
'.$langs->trans("Company").''; + print ''.$soc->nom.'Conditions de réglement" . $fac->cond_reglement ."
'.$langs->trans("Date").'".dolibarr_print_date($fac->date,"%A %d %B %Y")."'.$langs->trans("DateClosing").'' . dolibarr_print_date($fac->date_lim_reglement,"%A %d %B %Y"); + if ($fac->date_lim_reglement < (time() - $warning_delay) && ! $fac->paye && $fac->statut == 1 && ! $fac->am) print img_warning($langs->trans("Late")); + print "
'; + print ''; + if ($_GET["action"] != "classer") print ''; + print '
'; + print $langs->trans("Project"); + print '
'; + print '
'; + if ($_GET["action"] == "classer") + { + $html->form_project("facture.php?facid=$fac->id",$fac->fk_soc,$fac->projetid,"projetid"); + } + else + { + $html->form_project("facture.php?facid=$fac->id",$fac->fk_soc,$fac->projetid,"none"); + } + print "  '; + + /* + * Paiements + */ + print $langs->trans("Payments").' :
'; + $sql = "SELECT ".$db->pdate("datep")." as dp, pf.amount,"; + $sql.= " c.libelle as paiement_type, p.num_paiement, p.rowid"; + $sql.= " FROM ".MAIN_DB_PREFIX."paiement as p, ".MAIN_DB_PREFIX."c_paiement as c, ".MAIN_DB_PREFIX."paiement_facture as pf"; + $sql.= " WHERE pf.fk_facture = ".$fac->id." AND p.fk_paiement = c.id AND pf.fk_paiement = p.rowid"; + $sql.= " ORDER BY dp DESC"; + + $result = $db->query($sql); + + if ($result) + { + $num = $db->num_rows($result); + $i = 0; $total = 0; + print ''; + print ''; + print ''; + + $var=True; + while ($i < $num) + { + $objp = $db->fetch_object($result); + $var=!$var; + print "\n"; + print "\n"; + print '\n"; + print ""; + $totalpaye += $objp->amount; + $i++; + } + + if ($fac->paye == 0) + { + print "\n"; + print "\n"; + + $resteapayer = $fac->total_ttc - $totalpaye; + + print ""; + print "\n"; + } + print "
'.$langs->trans("Date").''.$langs->trans("Type").''.$langs->trans("Amount").' 
"; + print " ".strftime("%d %B %Y",$objp->dp)."$objp->paiement_type $objp->num_paiement'.price($objp->amount)."".$langs->trans("Currency".$conf->monnaie)."
".$langs->trans("AlreadyPayed")." :".price($totalpaye)."".$langs->trans("Currency".$conf->monnaie)."
Facturé :".price($fac->total_ttc)."".$langs->trans("Currency".$conf->monnaie)."
".$langs->trans("RemainderToPay")." :".price($resteapayer)."".$langs->trans("Currency".$conf->monnaie)."
"; + $db->free($result); + } else { + dolibarr_print_error($db); + } + + print "
".$langs->trans("Author")."$author->fullname
'.$langs->trans("GlobalDiscount").'
% '; + print ''.$fac->remise_percent.'%
'.$langs->trans("AmountHT").''.price($fac->total_ht).''.$langs->trans("Currency".$conf->monnaie).'
'.$langs->trans("VAT").''.price($fac->total_tva).''.$langs->trans("Currency".$conf->monnaie).'
'.$langs->trans("AmountTTC").''.price($fac->total_ttc).''.$langs->trans("Currency".$conf->monnaie).'
'.$langs->trans("Status").''.($fac->getLibStatut()).'
'.$langs->trans("Note").' : '.nl2br($fac->note)."
 

"; + + /* + * Lignes de factures + * + */ + $sql = "SELECT l.fk_product, l.description, l.price, l.qty, l.rowid, l.tva_taux"; + $sql .= " , l.remise_percent, l.subprice,"; + $sql .= $db->pdate("l.date_start")." as date_start"; + $sql .= " , ".$db->pdate("l.date_end")." as date_end, "; + $sql .= " p.fk_product_type"; + $sql .= " FROM ".MAIN_DB_PREFIX."facturedet as l "; + $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product p ON l.fk_product=p.rowid"; + $sql .= " WHERE l.fk_facture = ".$fac->id; + $sql .= " ORDER BY l.rang ASC, l.rowid"; + + $resql = $db->query($sql); + if ($resql) + { + $num_lignes = $db->num_rows($resql); + $i = 0; $total = 0; + + print ''; + if ($num_lignes) + { + print ""; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print "\n"; + } + $var=True; + while ($i < $num_lignes) + { + $objp = $db->fetch_object($resql); + $var=!$var; + + // Update ligne de facture + if ($_GET["action"] != 'editline' || $_GET["rowid"] != $objp->rowid) + { + + print ""; + if ($objp->fk_product > 0) + { + print ''; + } + else + { + print "\n"; + } + + print ''; + print '\n"; + print ''; + if ($objp->remise_percent > 0) + { + print '\n"; + } + else + { + print ''; + } + print '\n"; + + // Icone d'edition et suppression + if ($fac->statut == 0 && $user->rights->facture->creer) + { + print ''; + print ''; + + print ''; + } + } + else + { + print ''; + } + print ""; + + } + + + + $total = $total + ($objp->qty * $objp->price); + $i++; + } + + $db->free($resql); + } + else + { + dolibarr_print_error($db); + } + + /* + * Ajouter une ligne + */ + + print "
'.$langs->trans("Description").''.$langs->trans("VAT").''.$langs->trans("PriceUHT").''.$langs->trans("Qty").''.$langs->trans("Discount").''.$langs->trans("AmountHT").' 
'; + if ($objp->fk_product_type) print img_object($langs->trans("ShowService"),"service"); + else print img_object($langs->trans("ShowProduct"),"product"); + print ' '.stripslashes(nl2br($objp->description)).''; + if ($objp->date_start && $objp->date_end) { print " (Du ".dolibarr_print_date($objp->date_start)." au ".dolibarr_print_date($objp->date_end).")"; } + if ($objp->date_start && ! $objp->date_end) { print " (A partir du ".dolibarr_print_date($objp->date_start).")"; } + if (! $objp->date_start && $objp->date_end) { print " (Jusqu'au ".dolibarr_print_date($objp->date_end).")"; } + print '".stripslashes(nl2br($objp->description)); + if ($objp->date_start && $objp->date_end) { print " (Du ".dolibarr_print_date($objp->date_start)." au ".dolibarr_print_date($objp->date_end).")"; } + if ($objp->date_start && ! $objp->date_end) { print " (A partir du ".dolibarr_print_date($objp->date_start).")"; } + if (! $objp->date_start && $objp->date_end) { print " (Jusqu'au ".dolibarr_print_date($objp->date_end).")"; } + print "'.$objp->tva_taux.'%'.price($objp->subprice)."'.$objp->qty.''.$objp->remise_percent."% '.price($objp->subprice*$objp->qty*(100-$objp->remise_percent)/100)."'; + print img_edit(); + print ''; + print img_delete(); + print ''; + if ($i > 0) + { + print ''; + print img_up(); + print ''; + } + if ($i < $num_lignes-1) + { + print ''; + print img_down(); + print ' 
\n"; + + + print "\n"; + + + /* + * Boutons actions + */ + + if ($user->societe_id == 0 && $_GET["action"] <> 'valid' && $_GET["action"] <> 'editline') + { + print "
\n"; + + + + + + // Envoyer + if ($fac->statut == 1 && $user->rights->facture->envoyer) + { + print " id&action=presend\">".$langs->trans("Send")."\n"; + } + + // Envoyer une relance + if ($fac->statut == 1 && price($resteapayer) > 0 && $user->rights->facture->envoyer) + { + print " id&action=prerelance\">".$langs->trans("SendRemind")."\n"; + } + + + + + + print "
\n"; + + } + + print '
'; + + /* + * Documents générés + * Le fichier de facture détaillée est de la forme + * REFFACTURE-XXXXXX-detail.pdf ou XXXXX est une forme diverse + */ + + $facref = sanitize_string($fac->ref); + $file = $conf->facture->dir_output . "/" . $facref . "/" . $facref . ".pdf"; + $relativepath = "${facref}/${facref}.pdf"; + + $var=true; + + if (file_exists($file)) + { + print "
\n"; + print_titre("Documents"); + print ''; + + print ""; + + print ''; + print ''; + print ''; + print ''; + + + $dir = $conf->facture->dir_output . "/" . $facref . "/"; + $handle=opendir($dir); + + while (($file = readdir($handle))!==false) + { + if (is_readable($dir.$file) && substr($file, -10) == 'detail.pdf') + { + print ""; + $relativepathdetail = "${facref}/$file"; + + print ''; + print ''; + print ''; + print ''; + } + } + print "
".$langs->trans("Bill")." PDF'.$fac->ref.'.pdf'.filesize($file). ' bytes'.strftime("%d %b %Y %H:%M:%S",filemtime($file)).'
Facture détaillée'.$file.''.filesize($dir.$file). ' bytes'.strftime("%d %b %Y %H:%M:%S",filemtime($dir.$file)).'
\n"; + } + + + /* + * Propales + */ + $sql = "SELECT ".$db->pdate("p.datep")." as dp, p.price, p.ref, p.rowid as propalid"; + $sql .= " FROM ".MAIN_DB_PREFIX."propal as p, ".MAIN_DB_PREFIX."fa_pr as fp WHERE fp.fk_propal = p.rowid AND fp.fk_facture = $fac->id"; + + $resql = $db->query($sql); + if ($resql) + { + $num = $db->num_rows($resql); + if ($num) + { + $i = 0; $total = 0; + print "
"; + print_titre($langs->trans("RelatedCommercialProposals")); + + print ''; + print ''; + print ''; + print ''; + print ''; + print "\n"; + + $var=True; + while ($i < $num) + { + $objp = $db->fetch_object($resql); + $var=!$var; + print ""; + print ''; + print "\n"; + print ''; + print ""; + $total = $total + $objp->price; + $i++; + } + print "\n"; + print "
'.$langs->trans("Ref").''.$langs->trans("Date").''.$langs->trans("Price").'
'.img_object($langs->trans("ShowPropal"),"propal").' '.$objp->ref.'".dolibarr_print_date($objp->dp)."'.price($objp->price).'
 ".$langs->trans("TotalHT")."".price($total)."
"; + } + } + else + { + dolibarr_print_error($db); + } + + + print '
'; + + /* + * Liste des actions propres à la facture + */ + $sql = "SELECT id, ".$db->pdate("a.datea")." as da, a.label, a.note, code"; + $sql .= " FROM ".MAIN_DB_PREFIX."actioncomm as a, ".MAIN_DB_PREFIX."user as u "; + $sql .= " WHERE a.fk_user_author = u.rowid "; + $sql .= " AND a.fk_action in (9,10) "; + $sql .= " AND a.fk_soc = ".$fac->socidp ; + $sql .= " AND a.fk_facture = ".$fac->id; + + $resql = $db->query($sql); + if ($resql) + { + $num = $db->num_rows($resql); + if ($num) + { + print "
\n"; + print_titre($langs->trans("ActionsOnBill")); + + $i = 0; $total = 0; + print ''; + print ''; + print "\n"; + + $var=True; + while ($i < $num) + { + $objp = $db->fetch_object($resql); + $var=!$var; + print ""; + print ''; + print '\n"; + print ''; + print ''; + print "\n"; + $i++; + } + print "
'.$langs->trans("Ref").''.$langs->trans("Date").''.$langs->trans("Action").''.$langs->trans("By").'
'.img_object($langs->trans("ShowTask"),"task").' '.$objp->id.''.dolibarr_print_date($objp->da)."'.stripslashes($objp->label).''.$objp->code.'
\n"; + } + } + else + { + dolibarr_print_error($db); + } + + print "
"; + + + /* + * Affiche formulaire mail + */ + if ($_GET["action"] == 'presend') + { + print '
'; + print_titre($langs->trans("SendBillByMail")); + + $liste[0]=" "; + foreach ($soc->contact_email_array() as $key=>$value) { + $liste[$key]=$value; + } + + // Créé l'objet formulaire mail + include_once(DOL_DOCUMENT_ROOT."/html.formmail.class.php"); + $formmail = new FormMail($db); + $formmail->fromname = $user->fullname; + $formmail->frommail = $user->email; + $formmail->withfrom=1; + $formmail->withto=$liste; + $formmail->withcc=1; + $formmail->withtopic=$langs->trans("SendBillRef","__FACREF__"); + $formmail->withfile=1; + $formmail->withbody=1; + // Tableau des substitutions + $formmail->substit["__FACREF__"]=$fac->ref; + // Tableau des paramètres complémentaires du post + $formmail->param["action"]="send"; + $formmail->param["models"]="facture_send"; + $formmail->param["facid"]=$fac->id; + $formmail->param["returnurl"]=DOL_URL_ROOT."/telephonie/client/facture.php?facid=$fac->id"; + + $formmail->show_form(); + + print '
'; + } + + if ($_GET["action"] == 'prerelance') + { + print '
'; + print_titre($langs->trans("SendReminderBillByMail")); + + $liste[0]=" "; + foreach ($soc->contact_email_array() as $key=>$value) + { + $liste[$key]=$value; + } + + // Créé l'objet formulaire mail + include_once("../html.formmail.class.php"); + $formmail = new FormMail($db); + $formmail->fromname = $user->fullname; + $formmail->frommail = $user->email; + $formmail->withfrom=1; + $formmail->withto=$liste; + $formmail->withcc=1; + $formmail->withtopic=$langs->trans("SendReminderBillRef","__FACREF__"); + $formmail->withfile=1; + $formmail->withbody=1; + // Tableau des substitutions + $formmail->substit["__FACREF__"]=$fac->ref; + // Tableau des paramètres complémentaires + $formmail->param["action"]="relance"; + $formmail->param["models"]="facture_relance"; + $formmail->param["facid"]=$fac->id; + $formmail->param["returnurl"]=DOL_URL_ROOT."/compta/facture.php?facid=$fac->id"; + + $formmail->show_form(); + + print '
'; + } + + } + else + { + /* Facture non trouvée */ + print $langs->trans("ErrorBillNotFound",$_GET["facid"]); + } +} + +$db->close(); + +llxFooter('$Date$ - $Revision$'); +?>