* Copyright (C) 2004-2008 Laurent Destailleur * Copyright (C) 2004 Christophe Combelles * Copyright (C) 2005 Marc Barilley / Ocebo * Copyright (C) 2005-2007 Regis Houssin * * 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. */ /** \file htdocs/fourn/facture/fiche.php \ingroup facture, fournisseur \brief Page for supplier invoice card \version $Id$ */ require_once('./pre.inc.php'); require_once(DOL_DOCUMENT_ROOT.'/fourn/facture/paiementfourn.class.php'); require_once(DOL_DOCUMENT_ROOT.'/lib/fourn.lib.php'); require_once(DOL_DOCUMENT_ROOT.'/product.class.php'); if ($conf->projet->enabled) require_once(DOL_DOCUMENT_ROOT.'/project.class.php'); if (!$user->rights->fournisseur->facture->lire) accessforbidden(); $langs->load('bills'); $langs->load('suppliers'); $langs->load('companies'); // Security check if ($user->societe_id > 0) { $action = ''; $socid = $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']); $result = $facturefourn->set_valid($user); } if ($_POST['action'] == 'confirm_delete' && $_POST['confirm'] == 'yes') { if ($user->rights->fournisseur->facture->supprimer ) { $facturefourn = new FactureFournisseur($db); $factfournid = $_GET['facid']; $facturefourn->delete($factfournid); Header('Location: index.php'); exit; } } if ($_POST['action'] == 'confirm_deleteproductline' && $_POST['confirm'] == 'yes') { if ($user->rights->fournisseur->facture->creer) { $facturefourn = new FactureFournisseur($db); $facturefourn->fetch($_GET['facid']); $facturefourn->deleteline($_GET['ligne_id']); $_GET['action'] = ''; } } 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'] == 'update' && ! $_POST['cancel']) { $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='".addslashes(trim($_POST['facnumber']))."'"; $sql .= ", libelle='".addslashes(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 = dolibarr_mktime(12,0,0, $_POST['remonth'], $_POST['reday'], $_POST['reyear']); $db->begin(); // Creation facture $facfou = new FactureFournisseur($db); $facfou->ref = $_POST['facnumber']; $facfou->socid = $_POST['socid']; $facfou->libelle = $_POST['libelle']; $facfou->date = $datefacture; $facfou->date_echeance = dolibarr_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]; $amountht = price2num($_POST['amount'.$i]); $amountttc = price2num($_POST['amountttc'.$i]); $tauxtva = price2num($_POST['tauxtva'.$i]); $qty = $_POST['qty'.$i]; $fk_product = $_POST['fk_product'.$i]; if ($label) { if ($amountht) { $price_base='HT'; $amount=$amountht; } else { $price_base='TTC'; $amount=$amountttc; } $atleastoneline=1; $ret=$facfou->addline($label, $amount, $tauxtva, $qty, $fk_product, $remise_percent, '', '', '', 0, $price_base); if ($ret < 0) $nberror++; } } if ($nberror) { $db->rollback(); $mesg='
'.$facfou->error.'
'; $_GET['action']='create'; $_GET['socid']=$_POST['socid']; } else { $db->commit(); header('Location: fiche.php?facid='.$facid); exit; } } else { $db->rollback(); $mesg='
'.$facfou->error.'
'; $_GET['action']='create'; $_GET['socid']=$_POST['socid']; } } else { $mesg='
'.$langs->trans('ErrorFieldRequired',$langs->transnoentities('RefSupplier')).'
'; $_GET['action']='create'; $_GET['socid']=$_POST['socid']; } } if ($_GET['action'] == 'del_ligne') { $facfou = new FactureFournisseur($db,'',$_GET['facid']); $facfou->deleteline($_GET['ligne_id']); $_GET['action'] = 'edit'; } // Modification d'une ligne if ($_REQUEST['action'] == 'update_line') { if ($_REQUEST['etat'] == '1' && ! $_REQUEST['cancel']) // si on valide la modification { $facfou = new FactureFournisseur($db); $facfou->fetch($_GET['facid']); if ($_POST['puht']) { $pu=$_POST['puht']; $price_base_type='HT'; } if ($_POST['puttc']) { $pu=$_POST['puttc']; $price_base_type='TTC'; } if ($_POST['idprod']) { $prod = new Product($db); $prod->fetch($_POST['idprod']); $label = $prod->libelle; } else { $label = $_POST['label']; } $facfou->updateline($_GET['ligne_id'], $label, $pu, $_POST['tauxtva'], $_POST['qty'], $_POST['idprod'], $price_base_type); } } if ($_GET['action'] == 'add_ligne') { $facfou = new FactureFournisseur($db, '', $_GET['facid']); if ($_POST['prodfournpriceid']) { $nv_prod = new Product($db); $idprod=$nv_prod->get_buyprice($_POST['prodfournpriceid'], $_POST['qty']); if ($idprod > 0) { $result=$nv_prod->fetch($idprod); // cas sp�cial pour lequel on a les meme r�f�rence que le fournisseur // $label = '['.$nv_prod->ref.'] - '. $nv_prod->libelle; $label = $nv_prod->libelle; $societe=''; if ($_POST['socid']) { $societe=new Societe($db); $societe->fetch($_POST['socid']); } $tvatx=get_default_tva($societe,$mysoc,$nv_prod->tva_tx); $result=$facfou->addline($label, $nv_prod->fourn_pu, $tvatx, $_POST['qty'], $idprod); } if ($idprod == -1) { // Quantit� insuffisante $mesg='
'.$langs->trans("ErrorQtyTooLowForThisSupplier").'
'; } } else { $tauxtva = price2num($_POST['tauxtva']); if (! $_POST['label']) { $mesg='
'.$langs->trans("ErrorFieldRequired",$langs->transnoentities("Label")).'
'; } else { if (!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'] = ''; } if ($_POST['action'] == 'classin') { $facture = new FactureFournisseur($db,'',$_GET['facid']); $facture->fetch($_GET['facid']); $facture->setProject($_POST['projetid']); } /* * Affichage page */ $addons=''; llxHeader('','', $addons); // Mode creation if ($_GET['action'] == 'create' or $_GET['action'] == 'copy') { print_titre($langs->trans('NewBill')); if ($mesg) { print $mesg.'
'; } if ($_GET['action'] == 'copy') { $fac_ori = new FactureFournisseur($db); $fac_ori->fetch($_GET['facid']); } $societe=''; if ($_GET['socid']) { $societe=new Societe($db); $societe->fetch($_GET['socid']); } print '
'; print ''; print ''; print ''; print ''; print ''; if($_GET['action'] == 'copy'){ print ''; }else{ print ''; } print ''; if ($_GET['action'] == 'copy') { print ''; } else { print ''; } print ''; print ''; print '
'.$langs->trans('Company').''; if ($_GET['socid']) { print $societe->getNomUrl(1); print ''; } else { $html->select_societes((empty($_GET['socid'])?'':$_GET['socid']),'socid','s.fournisseur = 1'); } print ''.$langs->trans('NotePublic').'
'.$langs->trans('RefSupplier').'
'.$langs->trans('RefSupplier').'
'.$langs->trans('Label').'
'.$langs->trans('Label').'
'.$langs->trans('DateInvoice').''; $html->select_date('','','','','',"add"); print '
'.$langs->trans('DateEcheance').''; $html->select_date('','ech','','','',"add"); print '

'; if ($conf->global->PRODUCT_SHOW_WHEN_CREATE) { 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]->description; $value_pu = $fac_ori->lignes[$i-1]->pu_ht; $value_tauxtva = $fac_ori->lignes[$i-1]->tva_taux; $value_qty = $fac_ori->lignes[$i-1]->qty; } else { $value_qty = '1'; $value_tauxtva = ''; } print ''; print ''; print ''; print ''; print ''; print ''; } print '
 '.$langs->trans('Label').''.$langs->trans('PriceUHT').''.$langs->trans('VAT').''.$langs->trans('Qty').''.$langs->trans('PriceUTTC').'
'.$i.''; $html->select_tva('tauxtva'.$i,$value_tauxtva,$societe,$mysoc); print '
'; } print '
'; print '
'; } else { if ($_GET['facid'] > 0) { /* *************************************************************************** */ /* */ /* Fiche en mode visu ou edition */ /* */ /* *************************************************************************** */ $productstatic = new Product($db); $fac = new FactureFournisseur($db); $fac->fetch($_GET['facid']); $societe = new Fournisseur($db); $societe->fetch($fac->socid); if ($_GET['action'] == 'edit') { print_titre($langs->trans('SupplierInvoice')); print '
'; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; $rownb=9; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print '
'.$langs->trans('Company').''.$societe->getNomUrl(1).''.$langs->trans('NotePublic').'
'.$langs->trans('Ref').''; print $fac->ref.'
'.$langs->trans('RefSupplier').''; print ''; print '
'.$langs->trans('Label').''; print '
'.$langs->trans('DateInvoice').''; $html->select_date($fac->datep,'','','','',"update"); print '
'.$langs->trans('DateEcheance').''; $html->select_date($fac->date_echeance,'ech','','','',"update"); if (($fac->paye == 0) && ($fac->statut > 0) && $fac->date_echeance < (time() - $conf->facture->fournisseur->warning_delay)) print img_picto($langs->trans("Late"),"warning"); print '
'.$langs->trans('AmountHT').''.price($fac->total_ht).'
'.$langs->trans('AmountVAT').''.price($fac->total_tva).'
'.$langs->trans('AmountTTC').''.price($fac->total_ttc).'
'.$langs->trans('Status').''.$fac->getLibStatut(4).'
'; print ''; print '     '; print ''; print '
'; print '
'; /* * Lignes * */ print '
'; $var=true; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; for ($i = 0 ; $i < sizeof($fac->lignes) ; $i++) { $var=!$var; // Affichage simple de la ligne print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; } print '
'.$langs->trans('Label').''.$langs->trans('VAT').''.$langs->trans('PriceUHT').''.$langs->trans('PriceUTTC').''.$langs->trans('Qty').''.$langs->trans('TotalHT').''.$langs->trans('TotalTTC').' 
'.$fac->lignes[$i]->description.''.vatrate($fac->lignes[$i]->tva_taux).'%'.price($fac->lignes[$i]->pu_ht,'MU').''.($fac->lignes[$i]->pu_ttc?price($fac->lignes[$i]->pu_ttc,'MU'):' ').''.$fac->lignes[$i]->qty.''.price($fac->lignes[$i]->total_ht,'MT').''.price($fac->lignes[$i]->total_ttc,'MT').''; print ''.img_edit().''; print ''; print ''.img_delete().''; print '
'; } else { /* * */ $head = facturefourn_prepare_head($fac); $titre=$langs->trans('SupplierInvoice'); dolibarr_fiche_head($head, 'card', $titre); if ($mesg) { print $mesg.'
'; } /* * Confirmation de la suppression d'une ligne produit */ if ($_GET['action'] == 'confirm_delete_line') { $html->form_confirm($_SERVER["PHP_SELF"].'?facid='.$fac->id.'&ligne_id='.$_GET["ligne_id"], $langs->trans('DeleteProductLine'), $langs->trans('ConfirmDeleteProductLine'), 'confirm_deleteproductline'); print '
'; } /* * 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 '
'; } /* * Confirmation de la suppression de la facture fournisseur */ if ($_GET['action'] == 'delete') { $html->form_confirm('fiche.php?facid='.$fac->id, $langs->trans('DeleteBill'), $langs->trans('ConfirmDeleteBill'), 'confirm_delete'); print '
'; } print ''; print ''; print '
'; /* * Facture */ print ''; // Ref print ''; print "\n"; // Ref supplier print ''; print "\n"; // Societe print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; // Status $alreadypayed=$fac->getSommePaiement(); print ''; print ''; print ''; print ''; // Project if ($conf->projet->enabled) { $langs->load('projects'); print ''; print ''; print ''; } print '
'.$langs->trans("Ref").''.$fac->ref.'
'.$langs->trans("RefSupplier").''.$fac->ref_supplier.'
'.$langs->trans('Company').''.$societe->getNomUrl(1).''.$langs->trans('OtherBills').'
'.$langs->trans('Label').''; print $fac->libelle; print '
'.$langs->trans('Date').''; print dolibarr_print_date($fac->datep,'daytext').'
'.$langs->trans('DateEcheance').''; print dolibarr_print_date($fac->date_echeance,'daytext'); if (($fac->paye == 0) && ($fac->statut > 0) && $fac->date_echeance < (time() - $conf->facture->fournisseur->warning_delay)) print img_picto($langs->trans("Late"),"warning"); print '
'.$langs->trans('Status').''.$fac->getLibStatut(4,$alreadypayed).'
'.$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).'
'; print ''; if ($_GET['action'] != 'classer') { print ''; } print '
'; print $langs->trans('Project'); print 'id.'">'; print img_edit($langs->trans('SetProject'),1); print '
'; print '
'; if ($_GET['action'] == 'classer') { $html->form_project($_SERVER['PHP_SELF'].'?facid='.$fac->id,$fac->socid,$fac->fk_project,'projetid'); } else { $html->form_project($_SERVER['PHP_SELF'].'?facid='.$fac->id,$fac->socid,$fac->fk_project,'none'); } print '
'; print '
'; print ''; /* * Liste des paiements */ print ''; print '
'; $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('Payments').''.$langs->trans('Type').''.$langs->trans('AmountTTC').'
'.img_object($langs->trans('Payment'),'payment').' '.dolibarr_print_date($objp->dp,'day')."'.$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 ''; $var=1; for ($i = 0 ; $i < sizeof($fac->lignes) ; $i++) { if ($i == 0) { print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; } $var=!$var; // Ligne en modification if ($fac->statut == 0 && $_GET['action'] == 'mod_ligne' && $_GET['etat'] == '0' && $_GET['ligne_id'] == $fac->lignes[$i]->rowid) { print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; } else // Affichage simple de la ligne { print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; } } if ($fac->statut == 0 && $_GET['action'] != 'mod_ligne') { print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; /* Nouvelle ligne */ $var=!$var; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; // Ajout de produits/services pr�d�finis if ($conf->produit->enabled) { print ''; print ''; print ''; print ''; $var=! $var; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; } } print '
'.$langs->trans('Label').''.$langs->trans('VAT').''.$langs->trans('PriceUHT').''.$langs->trans('PriceUTTC').''.$langs->trans('Qty').''.$langs->trans('TotalHT').''.$langs->trans('TotalTTC').'  
'; if ($conf->produit->enabled && $fac->lignes[$i]->fk_product) { $product=new ProductFournisseur($db); $product->fetch($fac->lignes[$i]->fk_product); $product->ref=$product->libelle; // Car sur facture fourn on met juste le libelle sur produits lies print $product->getNomUrl(1); print ''; } else { print ''; } print ''; $html->select_tva('tauxtva',$fac->lignes[$i]->tva_taux,$societe,$mysoc); print '  '; print '
'; if ($fac->lignes[$i]->fk_product) { $productstatic->id=$fac->lignes[$i]->fk_product; $productstatic->type=1; //$productstatic->ref=$fac->lignes[$i]->ref; //print $productstatic->getNomUrl(1).' ('.$fac->lignes[$i]->ref_fourn.') - '.$fac->lignes[$i]->libelle; $productstatic->ref=$fac->lignes[$i]->libelle; print $productstatic->getNomUrl(1); } else { print nl2br($fac->lignes[$i]->description); } print ''.vatrate($fac->lignes[$i]->tva_taux).'%'.price($fac->lignes[$i]->pu_ht,'MU').''.($fac->lignes[$i]->pu_ttc?price($fac->lignes[$i]->pu_ttc,'MU'):' ').''.$fac->lignes[$i]->qty.''.price($fac->lignes[$i]->total_ht).''.price($fac->lignes[$i]->total_ttc).''; if ($fac->statut == 0) print ''.img_edit().''; else print ' '; print ''; if ($fac->statut == 0) print ''.img_delete().''; else print ' '; print '
'.$langs->trans('Label').''.$langs->trans('VAT').''.$langs->trans('PriceUHT').''.$langs->trans('PriceUTTC').''.$langs->trans('Qty').''.$langs->trans('TotalHT').''.$langs->trans('TotalTTC').'  
'; print ''; print ''; $html->select_tva('tauxtva','',$societe,$mysoc); print ''; print ''; print ''; print ''; print ''; print ''; print '  
'; $html->select_produits_fournisseurs($fac->socid,'','prodfournpriceid',$filtre); print '  
'; print ''; } /* * Boutons actions */ print '
'; if ($fac->statut <= 1 && $fac->getSommePaiement() <= 0 && $user->rights->fournisseur->facture->creer) { if ($_GET['action'] != 'edit') { print ''.$langs->trans('Modify').''; } } if ($fac->statut == 1 && $fac->paye == 0 && $user->societe_id == 0) { print ''.$langs->trans('DoPayment').''; } 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 (sizeof($fac->lignes) && $_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->supprimer) { print ''.$langs->trans('Delete').''; } print '
'; } } $db->close(); llxFooter('$Date$ - $Revision$'); ?>