* Copyright (C) 2004-2005 Laurent Destailleur * Copyright (C) 2004 Christophe Combelles * Copyright (C) 2005 Marc Barilley / Ocebo * * 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/fourn/facture/fiche.php \ingroup facture \brief Page des la fiche facture fournisseur \version $Revision$ */ require('./pre.inc.php'); require('./paiementfourn.class.php'); if (!$user->rights->fournisseur->facture->lire) accessforbidden(); $langs->load('bills'); $langs->load('suppliers'); $langs->load('companies'); // Sécurité accés client if ($user->societe_id > 0) { $action = ''; $socidp = $user->societe_id; } $html = new Form($db); $mesg=''; $action=isset($_GET['action'])?$_GET['action']:$_POST['action']; if ($_POST['action'] == 'confirm_valid' && $_POST['confirm'] == 'yes' && $user->rights->fournisseur->facture->valider) { $facturefourn=new FactureFournisseur($db); $facturefourn->fetch($_GET['facid']); $facturefourn->set_valid($user); Header('Location: fiche.php?facid='.$_GET['facid']); exit; } if ($_GET['action'] == 'payed') { $facturefourn=new FactureFournisseur($db); $facturefourn->fetch($_GET['facid']); $facturefourn->set_payed($user); } if($_GET['action'] == 'deletepaiement') { $facfou = new FactureFournisseur($db); $facfou->fetch($_GET['facid']); if ($facfou->statut == 1 && $facfou->paye == 0 && $user->societe_id == 0) { $paiementfourn = new PaiementFourn($db); $paiementfourn->fetch($_GET['paiement_id']); $paiementfourn->delete(); } } if ($_POST['action'] == 'modif_libelle') { $sql = 'UPDATE '.MAIN_DB_PREFIX.'facture_fourn set libelle = \''.$form_libelle.'\' WHERE rowid = '.$_GET['facid']; $result = $db->query( $sql); } if ($_POST['action'] == 'update') { $datefacture = $db->idate(mktime(12, 0 , 0, $_POST['remonth'], $_POST['reday'], $_POST['reyear'])); $date_echeance = $db->idate(mktime(12,0,0,$_POST['echmonth'],$_POST['echday'],$_POST['echyear'])); $sql = 'UPDATE '.MAIN_DB_PREFIX.'facture_fourn set '; $sql .= " facnumber='".trim($_POST['facnumber'])."'"; $sql .= ", libelle='".trim($_POST['libelle'])."'"; $sql .= ", note='".$_POST['note']."'"; $sql .= ", datef = '$datefacture'"; $sql .= ", date_lim_reglement = '$date_echeance'"; $sql .= ' WHERE rowid = '.$_GET['facid'].' ;'; $result = $db->query( $sql); } /* * Action création */ if ($_POST['action'] == 'add' && $user->rights->fournisseur->facture->creer) { if ($_POST['facnumber']) { $datefacture = mktime(12,0,0, $_POST['remonth'], $_POST['reday'], $_POST['reyear']); $tva = 0; $amo = price2num($_POST['amount']); $tva = (price2num($_POST['tva_taux']) * $amo) / 100 ; $remise = 0; $total = $tva + $amo; $db->begin(); // Creation facture $facfou = new FactureFournisseur($db); $facfou->ref = $_POST['facnumber']; $facfou->socidp = $_POST['socidp']; $facfou->libelle = $_POST['libelle']; $facfou->date = $datefacture; $facfou->date_echeance = mktime(12,0,0,$_POST['echmonth'],$_POST['echday'],$_POST['echyear']); $facfou->note = $_POST['note']; $facid = $facfou->create($user); // Ajout des lignes de factures if ($facid > 0) { for ($i = 1 ; $i < 9 ; $i++) { $label = $_POST['label'.$i]; $amount = price2num($_POST['amount'.$i]); $amountttc = price2num($_POST['amountttc'.$i]); $tauxtva = price2num($_POST['tauxtva'.$i]); $qty = $_POST['qty'.$i]; if (strlen($label) > 0 && !empty($amount)) { $atleastoneline=1; $ret=$facfou->addline($label, $amount, $tauxtva, $qty, 1); if ($ret < 0) $nberror++; } else if (strlen($label) > 0 && empty($amount)) { $ht = $amountttc / (1 + ($tauxtva / 100)); $atleastoneline=1; $ret=$facfou->addline($label, $ht, $tauxtva, $qty, 1); if ($ret < 0) $nberror++; } } if ($nberror) { $db->rollback(); $mesg='
'.$facfou->error.'
'; $_GET['action']='create'; } else { $db->commit(); header('Location: fiche.php?facid='.$facid); exit; } } else { $db->rollback(); $mesg='
'.$facfou->error.'
'; $_GET['action']='create'; } } else { $mesg='
'.$langs->trans('ErrorFieldRequired',$langs->trans('Ref')).'
'; $_GET['action']='create'; } } if ($_GET['action'] == 'del_ligne') { $facfou = new FactureFournisseur($db,'',$_GET['facid']); $facfou->deleteline($_GET['ligne_id']); $_GET['action'] = 'edit'; } if ($_GET['action'] == 'add_ligne') { $facfou = new FactureFournisseur($db, '', $_GET['facid']); $tauxtva = price2num($_POST['tauxtva']); if (strlen($_POST['label']) > 0 && !empty($_POST['amount'])) { $ht = price2num($_POST['amount']); $facfou->addline($_POST['label'], $ht, $tauxtva, $_POST['qty']); } else { $ttc = price2num($_POST['amountttc']); $ht = $ttc / (1 + ($tauxtva / 100)); $facfou->addline($_POST['label'], $ht, $tauxtva, $_POST['qty']); } $_GET['action'] = 'edit'; } /********************************************************************* * * Mode creation * **********************************************************************/ if ($_GET['action'] == 'create' or $_GET['action'] == 'copy') { llxHeader(); print_titre($langs->trans('NewBill')); if ($mesg) { print $mesg.'
'; } if ($_GET['action'] == 'copy') { $fac_ori = new FactureFournisseur($db); $fac_ori->fetch($_GET['facid']); } print '
'; print ''; print ''; print ''; print ''; print ''; print ''; print ''; if ($_GET['action'] == 'copy') { print ''; } else { print ''; } print ''; print ''; print '
'.$langs->trans('Company').''; $html->select_societes(empty($_GET['socid'])?'':$_GET['socid'],'socidp','s.fournisseur = 1'); print ''.$langs->trans('Comments').'
'.$langs->trans('Ref').'
'.$langs->trans('Label').'
'.$langs->trans('Label').'
'.$langs->trans('Date').''; $html->select_date(); print '
'.$langs->trans('DateEcheance').''; $html->select_date('','ech'); print '

'; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; for ($i = 1 ; $i < 9 ; $i++) { if ($_GET['action'] == 'copy') { $value_label = $fac_ori->lignes[$i-1][0]; $value_pu = $fac_ori->lignes[$i-1][1]; $value_qty = $fac_ori->lignes[$i-1][3]; } else { $value_qty = '1'; } print ''; print ''; print ''; print ''; print ''; print ''; } print '
 '.$langs->trans('Label').''.$langs->trans('PriceUHT').''.$langs->trans('Qty').''.$langs->trans('VATRate').''.$langs->trans('PriceUTTC').'
'.$i.''; $html->select_tva('tauxtva'.$i,'', '',$mysoc); print '
'; print '
'; print '
'; } else { if ($_GET['facid'] > 0) { /* *************************************************************************** */ /* */ /* Fiche en mode visu ou edition */ /* */ /* *************************************************************************** */ $fac = new FactureFournisseur($db); $fac->fetch($_GET['facid']); $societe = new Fournisseur($db); if ( $societe->fetch($fac->socidp) ) { $addons[0][0] = DOL_URL_ROOT.'/fourn/fiche.php?socid='.$fac->socidp; $addons[0][1] = $societe->nom; } llxHeader('','', $addons); if ($mesg) { print '
'.$mesg.'
'; } if ($_GET['action'] == 'edit') { print_titre($langs->trans('Bill').': '.$fac->ref); print '
'; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; $authorfullname=' '; if ($fac->author) { $author = new User($db, $fac->author); $author->fetch(''); $authorfullname=$author->fullname; } print ''; print ''; print ''; print '
'.$langs->trans('Company').''.stripslashes($fac->socnom).''.$langs->trans('Comments').'
'.$langs->trans('Ref').''; print ''; print '
'.$langs->trans('Label').''; print '
'.$langs->trans('AmountHT').' / '.$langs->trans('AmountTTC').''.price($fac->total_ht).' / '.price($fac->total_ttc).'
'.$langs->trans('DateBill').''; $html->select_date($fac->datep); print '
'.$langs->trans('DateEcheance').''; $html->select_date($fac->date_echeance,'ech'); print '
'.$langs->trans('Author').''.$authorfullname.'
'.$langs->trans('Status').''.$fac->LibStatut($fac->paye,$fac->statut).'
'; print '
'; /* * Lignes * */ print '
'; $var=true; print '
'; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; for ($i = 0 ; $i < sizeof($fac->lignes) ; $i++) { $var=!$var; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; } /* Nouvelle ligne */ $var=!$var; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print '
'.$langs->trans('Label').''.$langs->trans('PriceUHT').''.$langs->trans('PriceUTTC').''.$langs->trans('Qty').''.$langs->trans('TotalHT').''.$langs->trans('VATRate').''.$langs->trans('VAT').''.$langs->trans('TotalTTC').' 
'.$fac->lignes[$i][0].''.price($fac->lignes[$i][1]).''.price($fac->lignes[$i][1] * (1+($fac->lignes[$i][2]/100))).''.$fac->lignes[$i][3].''.price($fac->lignes[$i][4]).''.$fac->lignes[$i][2].''.price($fac->lignes[$i][5]).''.price($fac->lignes[$i][6]).''; print ''.img_delete().'
'; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print '-'; $html->select_tva('tauxtva','',$societe,$mysoc); print ''; print ' '; print '
'; print '
'; } else { /* * */ $h=0; $head[$h][0] = 'fiche.php?facid='.$fac->id; $head[$h][1] = $langs->trans('Card'); $hselected = $h; $h++; $titre=$langs->trans('SupplierBill').': '.$fac->ref; dolibarr_fiche_head($head, $hselected, $titre); /* * Confirmation de la validation * */ if ($_GET['action'] == 'valid') { $html->form_confirm('fiche.php?facid='.$fac->id, $langs->trans('ValidateBill'), $langs->trans('ConfirmValidateBill', $fac->ref), 'confirm_valid'); print '
'; } print ''; print ''; print '
'; /* * Facture */ print ''; // Ref print ""; print "\n"; // Societe print ''; print ''; print ''; print ''; print ''; print ''; $authorfullname=' '; if ($fac->author) { $author = new User($db, $fac->author); $author->fetch(''); $authorfullname=$author->fullname; } print ''; print ''; print ''; print ''; print ''; if (strlen($fac->note)) { print ''; } print '
".$langs->trans("Ref")."".$fac->ref."
'.$langs->trans('Company').''.dolibarr_trunc($fac->socnom,24).''.$langs->trans('OtherBills').'
'.$langs->trans('Date').''; print dolibarr_print_date($fac->datep,'%A %d %B %Y').'
'.$langs->trans('Label').''; print $fac->libelle; print '
'.$langs->trans('Author').''.$authorfullname.'
'.$langs->trans('Status').''.$fac->LibStatut($fac->paye,$fac->statut).'
'.$langs->trans('AmountHT').''.price($fac->total_ht).''.$langs->trans('Currency'.$conf->monnaie).'
'.$langs->trans('AmountVAT').''.price($fac->total_tva).''.$langs->trans('Currency'.$conf->monnaie).'
'.$langs->trans('AmountTTC').''.price($fac->total_ttc).''.$langs->trans('Currency'.$conf->monnaie).'
'.$langs->trans('Comments').''; print nl2br(stripslashes($fac->note)); print '
'; print '
'; print ''; print ''; print ''; /* * Liste des paiements */ print ''; print '
'.$langs->trans('DateEcheance').''; print dolibarr_print_date($fac->date_echeance,'%A %d %B %Y').'
'; 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.'paiementfourn as p'; $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_paiement as c ON p.fk_paiement = c.id'; $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'paiementfourn_facturefourn as pf ON pf.fk_paiementfourn = p.rowid'; $sql .= ' WHERE pf.fk_facturefourn = '.$fac->id; $sql .= ' ORDER BY dp DESC'; $result = $db->query($sql); if ($result) { $num = $db->num_rows($result); $i = 0; $totalpaye = 0; print ''; print ''; print ''; print ''; if ($fac->statut == 1 && $fac->paye == 0 && $user->societe_id == 0) { $tdsup=' colspan="2"'; } print ' '; $var=True; while ($i < $num) { $objp = $db->fetch_object($result); $var=!$var; print ''; print '\n"; print ''; print ''; if ($fac->statut == 1 && $fac->paye == 0 && $user->societe_id == 0) { print ''; } print ''; $totalpaye += $objp->amount; $i++; } if ($fac->paye == 0) { print ''; print ''; $resteapayer = $fac->total_ttc - $totalpaye; print ''; print ''; } print '
'.$langs->trans('Date').''.$langs->trans('Type').''.$langs->trans('AmountTTC').'
'.img_object($langs->trans('Payment'),'payment').' '.dolibarr_print_date($objp->dp)."'.$objp->paiement_type.' '.$objp->num_paiement.''.price($objp->amount).''.$langs->trans('Currency'.$conf->monnaie).''; print ''; print img_delete(); print '
'.$langs->trans('AlreadyPayed').' :'.price($totalpaye).''.$langs->trans('Currency'.$conf->monnaie).'
'.$langs->trans("Billed").' :'.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 '
'; print '
'; /* * Lignes * */ print '
'; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; $var=1; for ($i = 0 ; $i < sizeof($fac->lignes) ; $i++) { $var=!$var; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; } print '
'.$langs->trans('Label').''.$langs->trans('PriceUHT').''.$langs->trans('Qty').''.$langs->trans('TotalHT').''.$langs->trans('VATRate').''.$langs->trans('VAT').''.$langs->trans('TotalTTC').'
'.$fac->lignes[$i][0].''.price($fac->lignes[$i][1]).''.$fac->lignes[$i][3].''.price($fac->lignes[$i][4]).''.$fac->lignes[$i][2].' %'.price($fac->lignes[$i][5]).''.price($fac->lignes[$i][6]).'
'; print ''; } /* * Boutons actions */ print '
'; if ($fac->statut == 0 && $user->societe_id == 0) { if ($_GET['action'] == 'edit') { print ''.$langs->trans('Cancel').''; } else { print ''.$langs->trans('Edit').''; } } if ($fac->statut == 1 && $fac->paye == 0 && $user->societe_id == 0) { print ''.$langs->trans('DoPaiement').''; } if ($fac->statut == 1 && price($resteapayer) <= 0 && $fac->paye == 0 && $user->societe_id == 0) { print ''.$langs->trans('ClassifyPayed').''; } if ($fac->statut == 0 && $user->rights->fournisseur->facture->valider) { if ($_GET['action'] <> 'edit') print ''.$langs->trans('Valid').''; } else if ($user->rights->fournisseur->facture->creer) { print ''.$langs->trans('Copy').''; } if ($_GET['action'] != 'edit' && $fac->statut == 0 && $user->rights->fournisseur->facture->creer) { print ''.$langs->trans('Delete').''; } print '
'; } } $db->close(); llxFooter('$Date$ - $Revision$'); ?>