* * 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$ * */ require("./pre.inc.php3"); require("../facture.class.php3"); require("../lib/CMailFile.class.php3"); require("../paiement.class.php"); llxHeader(); $db = new Db(); $yn[1] = "oui"; $yn[0] = "non"; /* * Sécurité accés client */ if ($user->societe_id > 0) { $action = ''; $socidp = $user->societe_id; } /* * */ if ($action == 'add_paiement') { $datepaye = $db->idate(mktime(12, 0 , 0, $pmonth, $pday, $pyear)); $paiement = new Paiement($db); $paiement->facid = $facid; $paiement->datepaye = $datepaye; $paiement->amount = $amount; $paiement->author = $author; $paiement->paiementid = $paiementid; $paiement->num_paiement = $num_paiement; $paiement->note = $note; $paiement->create(); $action = ''; } /* * */ if ($action == 'valid') { $fac = new Facture($db); $result = $fac->set_valid($facid, $user->id); if ($result) { facture_pdf_create($db, $facid); } } if ($action == 'payed') { $fac = new Facture($db); $result = $fac->set_payed($facid); } if ($action == 'addligne') { $fac = new Facture($db); $result = $fac->addline($facid,$HTTP_POST_VARS["desc"],$HTTP_POST_VARS["pu"],$HTTP_POST_VARS["qty"]); } if ($action == 'updateligne') { $fac = new Facture($db,"",$facid); $result = $fac->updateline($rowid,$HTTP_POST_VARS["desc"],$HTTP_POST_VARS["price"],$HTTP_POST_VARS["qty"]); } if ($action == 'deleteline') { $fac = new Facture($db,"",$facid); $fac->id = $facid; $result = $fac->deleteline($rowid); } if ($action == 'delete') { $fac = new Facture($db); $fac->delete($facid); $facid = 0 ; } if ($action == 'add') { $datefacture = $db->idate(mktime(12, 0 , 0, $remonth, $reday, $reyear)); if (! $propalid) { $facture = new Facture($db, $socid); $facture->number = $facnumber; $facture->date = $datefacture; $facture->note = $note; $facture->amount = $amount; $facture->remise = $remise; $facid = $facture->create($user->id, $statut, $note); } else { $facture = new Facture($db, $socid); $facture->number = $facnumber; $facture->date = $datefacture; $facture->note = $note; $facture->amount = $amount; $facture->remise = $remise; $facture->propalid = $propalid; $facid = $facture->create($user->id); if (! $facid ) { print "

Erreur : la facture n'a pas été créée, vérifier le numéro !"; print "

Retour à la propal"; print $db->error(); } } $action = ''; } /* * */ if ($action == 'send') { $fac = new Facture($db,"",$facid); $fac->fetch($facid); $soc = new Societe($db, $fac->socidp); $file = $conf->facture->outputdir . "/" . $fac->ref . "/" . $fac->ref . ".pdf"; if (file_exists($file)) { $sendto = $soc->contact_get_email($HTTP_POST_VARS["destinataire"]); $sendtoid = $HTTP_POST_VARS["destinataire"]; if (strlen($sendto)) { $subject = "Facture $fac->ref"; $message = "Veuillez trouver ci-joint la facture $fac->ref\n\nCordialement\n\n"; $filename = "$fac->ref.pdf"; $replyto = $HTTP_POST_VARS["replytoname"] . " <".$HTTP_POST_VARS["replytomail"] .">"; $mailfile = new CMailFile($subject,$sendto,$replyto,$message,$file, "application/pdf", $filename); if ( $mailfile->sendfile() ) { $sendto = htmlentities($sendto); $sql = "INSERT INTO actioncomm (datea,fk_action,fk_soc,note,fk_facture, fk_contact,fk_user_author, label) VALUES (now(), 9 ,$fac->socidp ,'Envoyée à $sendto',$fac->id, $sendtoid, $user->id, 'Envoi Facture par mail');"; if (! $db->query($sql) ) { print $db->error(); print "

$sql

"; } } else { print "!! erreur d'envoi
$sendto
$replyto
$filename"; } } else { print "Can't get email $sendto"; } } } if ($action == 'pdf') { /* * Generation de la facture * */ print facture_pdf_create($db, $facid); // définit dans /includes/modules/facture/modules_facture.php } /* * * Mode creation * * * */ if ($action == 'create') { print_titre("Emettre une facture"); if ($propalid) { $sql = "SELECT s.nom, s.prefix_comm, s.idp, p.price, p.remise, p.tva, p.total, p.ref, ".$db->pdate("p.datep")." as dp, c.id as statut, c.label as lst"; $sql .= " FROM societe as s, llx_propal as p, c_propalst as c WHERE p.fk_soc = s.idp AND p.fk_statut = c.id"; $sql .= " AND p.rowid = $propalid"; } else { $sql = "SELECT s.nom, s.prefix_comm, s.idp "; $sql .= "FROM societe as s "; $sql .= "WHERE s.idp = $socidp"; } if ( $db->query($sql) ) { $num = $db->num_rows(); if ($num) { $obj = $db->fetch_object(0); $numfa = facture_get_num(); // définit dans includes/modules/facture print "
"; print ''; print "idp\">"; print ''; print ""; print ''; if ($propalid) { $amount = ($obj->price - $obj->remise); print ''; print ''; print ''; print ''; print ''; print ""; print ''; print ""; print ""; } else { } print ""; print ""; print ""; print ""; print ''; print "\n"; print "
Client :$obj->nom'; print '
Propal :$obj->ref
Montant HT :'.price($amount).'
TVA 19.6% :".price($obj->tva)."
Total TTC :".price($obj->total)."
Auteur :".$user->fullname."
Date :"; print_date_select(time()); print "
Numéro :
\n"; } } else { print $db->error(); } } else /* *************************************************************************** */ /* */ /* */ /* */ /* *************************************************************************** */ { if ($facid > 0) { $sql = "SELECT s.nom as socnom, s.idp as socidp, f.facnumber, f.amount, f.total, ".$db->pdate("f.datef")." as df, f.paye, f.fk_statut as statut, f.fk_user_author, f.note"; $sql .= " FROM societe as s,llx_facture as f WHERE f.fk_soc = s.idp AND f.rowid = $facid"; if ($user->societe_id > 0) { $sql .= " AND s.idp = ".$user->societe_id; } $result = $db->query( $sql); if ($result) { $num = $db->num_rows(); if ($num) { $obj = $db->fetch_object( $i); } $db->free(); } else { print $db->error(); } if ($num > 0) { $soc = new Societe($db, $obj->socidp); $author = new User($db); $author->id = $obj->fk_user_author; $author->fetch(); print_titre("Facture : ".$obj->facnumber); print ""; print ""; print ""; print "
"; /* * Facture */ print ""; print ""; print ""; print ""; print "\n"; print ""; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ""; print ""; print "
Client"; print "socidp\">$obj->socnom
Date".strftime("%A %d %B %Y",$obj->df)."
Auteur$author->fullname
Montant'.price($obj->amount).'euros HT
TVA'.tva($obj->amount).'euros
Total'.price($obj->total).'euros TTC
Statut'.$obj->statut.'Payé".$yn[$obj->paye]."
"; print "
"; $_MONNAIE="euros"; /* * Paiements */ $sql = "SELECT ".$db->pdate("datep")." as dp, p.amount, c.libelle as paiement_type, p.num_paiement, p.rowid"; $sql .= " FROM llx_paiement as p, c_paiement as c WHERE p.fk_facture = $facid AND p.fk_paiement = c.id"; $result = $db->query($sql); if ($result) { $num = $db->num_rows(); $i = 0; $total = 0; print "

Paiements"; echo ''; print ""; print ""; print ""; print ""; print "\n"; $var=True; while ($i < $num) { $objp = $db->fetch_object( $i); $var=!$var; print ""; print "\n"; print "\n"; print "\n"; print ""; $total = $total + $objp->amount; $i++; } if ($obj->paye == 0) { print "\n"; print "\n"; $resteapayer = $obj->total - $total; print ""; print "\n"; } print "
DateTypeMontant 
".strftime("%d %B %Y",$objp->dp)."$objp->paiement_type $objp->num_paiement".price($objp->amount)."$_MONNAIE
Total :".price($total)."$_MONNAIE
Facturé :".price($obj->total)."$_MONNAIE
Reste a payer :".price($resteapayer)."$_MONNAIE
"; $db->free(); } else { print $db->error(); } print "

Note : ".nl2br($obj->note)."
"; /* * Lignes de factures * */ $sql = "SELECT l.description, l.price, l.qty, l.rowid, l.tva_taux"; $sql .= " FROM llx_facturedet as l WHERE l.fk_facture = $facid"; $result = $db->query($sql); if ($result) { $num = $db->num_rows(); $i = 0; $total = 0; echo ''; print ""; print ''; print ''; print ''; print ''; print "\n"; $var=True; while ($i < $num) { $objp = $db->fetch_object( $i); $var=!$var; print ""; print "\n"; print ''; print ''; print '\n"; if ($obj->statut == 0) { print ''; print ''; } else { print ''; } print ""; if ($action == 'editline' && $rowid == $objp->rowid) { print ""; print ''; print ''; print ""; print ''; print ''; print ''; print ''; print '' . "\n"; print "\n"; } $total = $total + ($objp->qty * $objp->price); $i++; } $db->free(); // print "
DescriptionTva TxQuantitéMontant  
".stripslashes(nl2br($objp->description))."'.$objp->tva_taux.' %'.$objp->qty.''.price($objp->price)."deledit  
"; } else { print $db->error(); } /* * Ajouter une ligne * */ if ($obj->statut == 0) { print "
"; // echo ''; print ""; print ''; print ''; print ''; print ''; print ''; print ''; print "\n"; print ''; print ''; print ''; print ''; print ''; print ''; print ''; // print "
DescriptionTva TxQuantitéMontant  
"; print "
"; } print ""; /* * Fin Ajout ligne * */ if ($user->societe_id == 0) { print "

"; if ($obj->statut == 0) { print ""; } elseif ($obj->statut == 1 && $resteapayer > 0) { print ""; } else { print ""; } if ($obj->statut == 1 && $resteapayer > 0) { print ""; } else { print ""; } if ($obj->statut == 1 && abs($resteapayer == 0) && $obj->paye == 0) { print ""; } else { print ""; } if ($obj->statut == 0) { print ""; } elseif ($obj->statut == 1 && $obj->paye == 0) { print ""; } else { print ""; } print "
[Supprimer][Envoyer]-[Emettre un paiement]-[Classer 'Payée']-[Valider]Générer la facture-
"; } print "

\n"; /* * Documents générés * */ print "


"; print ""; print '"; print "
"; print_titre("Documents générés"); print ''; $file = $conf->facture->outputdir . "/" . $obj->facnumber . "/" . $obj->facnumber . ".pdf"; if (file_exists($file)) { print ""; print ''; print ''; print ''; print ''; } $file = $conf->facture->outputdir . "/" . $obj->facnumber . "/" . $obj->facnumber . ".ps"; if (file_exists($file)) { print ""; print ''; print ''; print ''; print ''; } print ''; print "
Facture PDFfacnumber."/".$obj->facnumber.'.pdf">'.$obj->facnumber.'.pdf'.filesize($file). ' bytes'.strftime("%d %b %Y %H:%M:%S",filemtime($file)).'
Facture Postscriptfacnumber."/".$obj->facnumber.'.ps">'.$obj->facnumber.'.ps'.filesize($file). ' bytes'.strftime("%d %b %Y %H:%M:%S",filemtime($file)).'
(liste...)
\n"; print "
'; print_titre("Actions"); /* * Liste des actions * */ $sql = "SELECT ".$db->pdate("a.datea")." as da, a.note"; $sql .= " FROM actioncomm as a WHERE a.fk_soc = $obj->socidp AND a.fk_action = 9 AND a.fk_facture = $facid"; $result = $db->query($sql); if ($result) { $num = $db->num_rows(); if ($num) { $i = 0; $total = 0; print ''; print ""; print ""; print ""; print "\n"; $var=True; while ($i < $num) { $objp = $db->fetch_object( $i); $var=!$var; print ""; print "\n"; print ''; print ""; $i++; } print "
DateAction
".strftime("%d %B %Y",$objp->da)."'.stripslashes($objp->note).'
"; } } else { print $db->error(); } /* * * */ print "
"; /* * * */ if ($action == 'presend') { $replytoname = $user->fullname; $from_name = $replytoname; $replytomail = $user->email; $from_mail = $replytomail; print "
\n"; print "\n"; print "\n"; print "

Envoyer la facture par mail"; print ""; print ''; print ""; print ""; print ""; print ""; print "
Destinataire'; $form = new Form($db); $form->select_array("destinataire",$soc->contact_email_array()); print 'email\">
Expéditeur$from_name$from_mail
Reply-to$replytoname$replytomail
"; print ""; print "

"; } /* * Propales */ $sql = "SELECT ".$db->pdate("p.datep")." as dp, p.price, p.ref, p.rowid as propalid"; $sql .= " FROM llx_propal as p, llx_fa_pr as fp WHERE fp.fk_propal = p.rowid AND fp.fk_facture = $facid"; $result = $db->query($sql); if ($result) { $num = $db->num_rows(); if ($num) { $i = 0; $total = 0; print "

Proposition(s) commerciale(s) associée(s)"; print ''; print ""; print ""; print ""; print ""; print "\n"; $var=True; while ($i < $num) { $objp = $db->fetch_object( $i); $var=!$var; print ""; print "\n"; print "\n"; print ''; print ""; $total = $total + $objp->price; $i++; } print "\n"; print "
NumDatePrix
propalid\">$objp->ref".strftime("%d %B %Y",$objp->dp)."'.price($objp->price).'
Total : ".price($total)." $_MONNAIE HT
"; } } else { print $db->error(); } } else { /* Facture non trouvée */ print "Facture inexistante ou accés refusé"; } } else { /* * * Mode Liste * * */ print_barre_liste("Factures",$page,$PHP_SELF); $sql = "SELECT s.nom,s.idp,f.facnumber,f.amount,".$db->pdate("f.datef")." as df,f.paye,f.rowid as facid"; $sql .= " FROM societe as s,llx_facture as f WHERE f.fk_soc = s.idp"; if ($socidp) { $sql .= " AND s.idp = $socidp"; } if ($month > 0) { $sql .= " AND date_format(f.datef, '%m') = $month"; } if ($year > 0) { $sql .= " AND date_format(f.datef, '%Y') = $year"; } $sql .= " ORDER BY f.datef DESC, f.facnumber DESC "; $result = $db->query($sql); if ($result) { $num = $db->num_rows(); $i = 0; print ""; print ''; print ""; print ''; print "\n"; if ($num > 0) { $var=True; while ($i < $num) { $objp = $db->fetch_object( $i); $var=!$var; print ""; print "\n"; print "\n"; if ($objp->df > 0 ) { print "\n"; } else { print "\n"; } print "\n"; if (! $objp->paye) { print ''; } else { print ''; } $total = $total + $objp->amount; $subtotal = $subtotal + $objp->amount; print "\n"; $i++; $j++; } } if ($i == 0) { $i=1; } if ($j == 0) { $j=1; } print ""; print ""; print ''; print ""; print ""; print ''; print "
Numéro"; print_liste_field_titre("Société",$PHP_SELF,"s.nom"); print "DateMontant 
facid\">"; if ($objp->paye) { print $objp->facnumber; } else { print ''.$objp->facnumber.''; } print "idp\">$objp->nom"; $y = strftime("%Y",$objp->df); $m = strftime("%m",$objp->df); print strftime("%d",$objp->df)."\n"; print " "; print strftime("%B",$objp->df)."\n"; print " "; print strftime("%Y",$objp->df)."!!!".price($objp->amount)."impayée 
$j factures Sous Total : ".price($subtotal)." 
$i factures Total (euros HT): ".price($total)." 
"; $db->free(); } else { print $db->error(); } } } $db->close(); llxFooter("Dernière modification $Date$ révision $Revision$"); ?>