diff --git a/htdocs/fourn/facture/fiche.php b/htdocs/fourn/facture/fiche.php index 10a7b142827..d7b2a85748a 100644 --- a/htdocs/fourn/facture/fiche.php +++ b/htdocs/fourn/facture/fiche.php @@ -23,27 +23,27 @@ */ /** - \file htdocs/fourn/facture/fiche.php - \ingroup facture - \brief Page des la fiche facture fournisseur - \version $Revision$ + \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(); + accessforbidden(); $langs->load('bills'); $langs->load('suppliers'); $langs->load('companies'); // Sécurité accés client -if ($user->societe_id > 0) +if ($user->societe_id > 0) { - $action = ''; - $socidp = $user->societe_id; + $action = ''; + $socidp = $user->societe_id; } @@ -53,183 +53,170 @@ $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=new FactureFournisseur($db); + $facturefourn->fetch($_GET['facid']); $facturefourn->set_valid($user); - - Header('Location: fiche.php?facid='.$_GET['facid']); - exit; + Header('Location: fiche.php?facid='.$_GET['facid']); + exit; } if ($_GET['action'] == 'payed') { - $facturefourn=new FactureFournisseur($db); - $facturefourn->fetch($_GET['facid']); - + $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->delete($_GET['paiement_id']); - } + $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); + $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"])); + $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); + $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"]); + if ($_POST['facnumber']) + { + $datefacture = mktime(12,0,0, + $_POST['remonth'], + $_POST['reday'], + $_POST['reyear']); - $tva = 0; - $tva = ($_POST['tva_taux'] * $_POST['amount']) / 100 ; - $remise = 0; - $total = $tva + $_POST['amount']; + $tva = 0; + $amo = price2num($_POST['amount']); + $tva = (price2num($_POST['tva_taux']) * $amo) / 100 ; + $remise = 0; + $total = $tva + $amo; - $db->begin(); + $db->begin(); - // Creation facture - $facfou = new FactureFournisseur($db); + // Creation facture + $facfou = new FactureFournisseur($db); - $facfou->number = $_POST["facnumber"]; - $facfou->socid = $_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"]; + $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); + $facid = $facfou->create($user); - // Ajout des lignes de factures - if ($facid > 0) - { - for ($i = 1 ; $i < 9 ; $i++) - { - $label = 'label'.$i; - $amount = 'amount'.$i; - $amountttc = 'amountttc'.$i; - $tauxtva = 'tauxtva'.$i; - $qty = 'qty'.$i; + // 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($_POST[$label]) > 0 && !empty($_POST[$amount])) - { - $ht = $_POST[$amount]; - $ht = str_replace(' ', '', $ht); - $ht = str_replace(',', '.', $ht); - $atleastoneline=1; - $ret=$facfou->addline($_POST["$label"], $_POST["$amount"], $_POST["$tauxtva"], $_POST["$qty"], 1); - if ($ret < 0) $nberror++; - } - else if (strlen($_POST[$label]) > 0 && empty($_POST[$amount])) - { - $ttc = $_POST[$amountttc]; - $ttc = str_replace(' ', '', $ttc); - $ttc = str_replace(',', '.', $ttc); - $ht = $ttc / (1 + ($_POST[$tauxtva] / 100)); - $atleastoneline=1; - $ret=$facfou->addline($_POST[$label], $ht, $_POST[$tauxtva], $_POST[$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 (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') +if ($_GET['action'] == 'del_ligne') { - $facfou = new FactureFournisseur($db,"",$_GET["facid"]); - - $facfou->deleteline($_GET["ligne_id"]); - - $_GET["action"] = "edit"; + $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 = $_POST['amount']; - $ht = str_replace(' ', '', $ht); - $ht = str_replace(',', '.', $ht); - $facfou->addline($_POST['label'], $ht, $_POST['tauxtva'], $_POST['qty']); + $ht = price2num($_POST['amount']); + $facfou->addline($_POST['label'], $ht, $tauxtva, $_POST['qty']); } else { - $ttc = $_POST['amountttc']; - $ttc = str_replace(' ', '', $ttc); - $ttc = str_replace(',', '.', $ttc); - $ht = $ttc / (1 + ($_POST['tauxtva'] / 100)); - $facfou->addline($_POST['label'], $ht, $_POST['tauxtva'], $_POST['qty']); + $ttc = price2num($_POST['amountttc']); + $ht = $ttc / (1 + ($tauxtva / 100)); + $facfou->addline($_POST['label'], $ht, $tauxtva, $_POST['qty']); } $_GET['action'] = 'edit'; } -/* - * - * Fiche facture en mode creation - * - */ +/********************************************************************* +* +* Mode creation +* +**********************************************************************/ if ($_GET['action'] == 'create' or $_GET['action'] == 'copy') { @@ -247,46 +234,11 @@ if ($_GET['action'] == 'create' or $_GET['action'] == 'copy') print '
'; print ''; - print ''; print ''; print ''; -*/ print ''; print ''; @@ -348,374 +300,372 @@ if ($_GET['action'] == 'create' or $_GET['action'] == 'copy') } else { - /* - * Fiche facture en mode visu ou edition - * - */ - if ($_GET["facid"] > 0) - { + if ($_GET['facid'] > 0) + { + /* *************************************************************************** */ + /* */ + /* Fiche en mode visu ou edition */ + /* */ + /* *************************************************************************** */ - $fac = new FactureFournisseur($db); - $fac->fetch($_GET["facid"]); + $fac = new FactureFournisseur($db); + $fac->fetch($_GET['facid']); - $societe = new Fournisseur($db); + $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 ( $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 ($mesg) { print '
'.$mesg.'
'; } - if ($_GET["action"] == "edit") - { + if ($_GET['action'] == 'edit') + { - print_titre($langs->trans("Bill").': '.$fac->ref); + print_titre($langs->trans('Bill').': '.$fac->ref); - print ''; - print ''; + print ''; + print ''; - print '
'.$langs->trans('Company').''; $html->select_societes(empty($_GET['socid'])?'':$_GET['socid'],'socidp','s.fournisseur = 1'); -/* - print ''.$langs->trans('Comments').'
'; - print ''; + print '
'.$langs->trans("Company").'
'; + print ''; - print ''; - print ''; + print ''; + print ''; - print ''; + 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").'
'.stripslashes($fac->socnom).''.$langs->trans('Comments').'
'.$langs->trans("Ref").''; - print '
'.$langs->trans('Ref').''; + print ''; - print '
'; + print '
'.$langs->trans("Label").''; - print '
'.$langs->trans('Label').''; + print '
'.$langs->trans("AmountHT").' / '.$langs->trans("AmountTTC").''.price($fac->total_ht).' / '.price($fac->total_ttc).'
'.$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('DateBill').''; + $html->select_date($fac->datep); + print '
'.$langs->trans("DateEcheance").''; - $html->select_date($fac->date_echeance,'ech'); - 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)."
trans("Save")."\">
"; - print "
"; + $authorfullname=' '; + if ($fac->author) + { + $author = new User($db, $fac->author); + $author->fetch(''); + $authorfullname=$author->fullname; + } + print ''.$langs->trans('Author').''.$authorfullname.''; + print ''.$langs->trans('Status').''.$fac->LibStatut($fac->paye,$fac->statut).''; + print ''; + print ''; + print ''; - /* - * Lignes - * - */ - print '
'; - $var=true; + /* + * Lignes + * + */ + print '
'; + $var=true; - print "
id&action=add_ligne\" method=\"post\">"; - 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 ''; - } + 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 ''; + 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"); - print ''; - print ' '; - print '
"; - print "
"; - } - else - { + /* Nouvelle ligne */ + $var=!$var; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print '-'; + print ''; + $html->select_tva('tauxtva'); + print ''; + print ' '; + print ''; + print ''; + print ''; + } + else + { + /* + * + */ + $h=0; - /* - * - */ - $h=0; + $head[$h][0] = 'fiche.php?facid='.$fac->id; + $head[$h][1] = $langs->trans('Card'); + $hselected = $h; + $h++; - $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); + $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 '
'; - } + /* + * 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 '
'; + print ''; + print ''; + print ''; - print "'; + print ''; + print ''; + if (strlen($fac->note)) + { + print ''; + } + print '
'; - /* - * Facture - */ - print ''; + /* + * Facture + */ + print '
'; // Ref print ""; print "\n"; // Societe - print ""; - print ""; - print "\n"; - - print '\n"; - 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 ''; - print ''; - print ''; - print ''; - if (strlen($fac->note)) - { - print ''; - } - print "
".$langs->trans("Ref")."".$fac->ref."
".$langs->trans("Company")."socidp\">".dolibarr_trunc($fac->socnom,24)."socidp\">".$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('Company').''.dolibarr_trunc($fac->socnom,24).''.$langs->trans('OtherBills').'
".$langs->trans("Author")."$authorfullname
'.$langs->trans("Status").''.$fac->LibStatut($fac->paye,$fac->statut)."
'.$langs->trans('Date').''; + print dolibarr_print_date($fac->datep,'%A %d %B %Y').'
'.$langs->trans('Label').''; + print $fac->libelle; + print '
'.$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 '
"; + $authorfullname=' '; + if ($fac->author) + { + $author = new User($db, $fac->author); + $author->fetch(''); + $authorfullname=$author->fullname; + } + print '
'.$langs->trans('Author').''.$authorfullname.'
'.$langs->trans('Status').''.$fac->LibStatut($fac->paye,$fac->statut).'
"; + print '
'.$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 '
'; - /* - * Liste des paiements - */ + /* + * Liste des paiements + */ - print ''; + print '
'; - print ''; - print '\n"; + print ''; + print ''; - print ''; - - $sql = "SELECT ".$db->pdate("datep")." as dp, p.amount, c.libelle as paiement_type, p.num_paiement, p.rowid"; - $sql .= " FROM ".MAIN_DB_PREFIX."paiementfourn as p, ".MAIN_DB_PREFIX."c_paiement as c "; - $sql .= " WHERE p.fk_facture_fourn = ".$fac->id." AND p.fk_paiement = c.id"; + print ''; - $result = $db->query($sql); - if ($result) - { - $num = $db->num_rows($result); - $i = 0; $total = 0; + $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'; - print '
'.$langs->trans("DateEcheance").''; - print dolibarr_print_date($fac->date_echeance,"%A %d %B %Y")."
'.$langs->trans('DateEcheance').''; + print dolibarr_print_date($fac->date_echeance,'%A %d %B %Y').'
'.$langs->trans("Payments").'
'.$langs->trans('Payments').'
'; - print ''; + $result = $db->query($sql); + if ($result) + { + $num = $db->num_rows($result); + $i = 0; $total = 0; - print ""; - print ''; - print ''; + print '"; - print "
'.$langs->trans("Date").''.$langs->trans("Type").'
'; + print ''; - if ($fac->statut == 1 && $fac->paye == 0 && $user->societe_id == 0) - { - $tdsup=' colspan="2"'; - } - print " "; - print "\n"; + print ''; + print ''; + print ''; - $var=True; - while ($i < $num) - { - $objp = $db->fetch_object($result); - $var=!$var; - print ""; - print '\n"; - print "\n"; - print "\n"; + if ($fac->statut == 1 && $fac->paye == 0 && $user->societe_id == 0) + { + $tdsup=' colspan="2"'; + } + print ' '; - if ($fac->statut == 1 && $fac->paye == 0 && $user->societe_id == 0) - { - print ''; - } + $var=True; + while ($i < $num) + { + $objp = $db->fetch_object($result); + $var=!$var; + print ''; + print '\n"; + print ''; + print ''; - print ""; - $total = $total + $objp->amount; - $i++; - } - print "".$langs->trans("Currency".$conf->monnaie)."\n"; + if ($fac->statut == 1 && $fac->paye == 0 && $user->societe_id == 0) + { + print ''; + } + + print ''; + $total = $total + $objp->amount; + $i++; + } + print ''.$langs->trans('Currency'.$conf->monnaie)."\n"; - if ($fac->statut > 0) - { - $resteapayer = abs($fac->total_ttc - $total); - print ""; - print '".$langs->trans("Currency".$conf->monnaie).""; - print "\n"; - } + if ($fac->statut > 0) + { + $resteapayer = abs($fac->total_ttc - $total); + print ''; + print ''.$langs->trans('Currency'.$conf->monnaie).''; + print ''; + } - print "
".$langs->trans("AmountTTC")."
'.$langs->trans('Date').''.$langs->trans('Type').'
'.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)."'.$langs->trans('AmountTTC').'
'; - print ''; - print img_delete(); - print '
'.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).'
".$langs->trans("Total")." :".price($total)."
'; + print ''; + print img_delete(); + print '
'.$langs->trans('Total').' :'.price($total).'
".$langs->trans("RemainderToPay")." :'.price($resteapayer)."
'.$langs->trans('RemainderToPay').' :'.price($resteapayer).'
"; - $db->free(); - } - else - { - dolibarr_print_error($db); - } - print "
"; + print '
'; + $db->free(); + } + else + { + dolibarr_print_error($db); + } + print '
'; - print ""; - print ""; + print ''; + print ''; - /* - * Lignes - * - */ + /* + * 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 "\n"; - } + 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 - */ + /* + * Boutons actions + */ - print "
\n"; + 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 == 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 && $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 "id&action=payed\">".$langs->trans('ClassifyPayed').""; - } + 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 "id&action=valid\">".$langs->trans('Valid').""; - } - else + 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 ($user->rights->fournisseur->facture->creer) - { - print "id&action=copy&socid=$fac->socidp\">".$langs->trans('Copy').""; - } - - if ($_GET["action"] != "edit" && $fac->statut == 0 && $user->rights->fournisseur->facture->creer) - { - print ''.$langs->trans("Delete").''; - } - - print "
"; - - } + if ($_GET['action'] != 'edit' && $fac->statut == 0 && $user->rights->fournisseur->facture->creer) + { + print ''.$langs->trans('Delete').''; + } + print '
'; + } } $db->close(); diff --git a/htdocs/fourn/facture/paiement.php b/htdocs/fourn/facture/paiement.php index 63c6364808d..10a274560ae 100644 --- a/htdocs/fourn/facture/paiement.php +++ b/htdocs/fourn/facture/paiement.php @@ -1,8 +1,9 @@ - * Copyright (C) 2004 Éric Seigne - * Copyright (C) 2004-2005 Laurent Destailleur - * Copyright (C) 2004 Christophe Combelles +/* Copyright (C) 2003-2005 Rodolphe Quiedeville + * Copyright (C) 2004 Éric Seigne + * 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 @@ -31,242 +32,409 @@ */ -require("./pre.inc.php"); -require("./paiementfourn.class.php"); +require('./pre.inc.php'); +require('./paiementfourn.class.php'); -$langs->load("bills"); -$langs->load("banks"); +$langs->load('bills'); +$langs->load('banks'); +$facid=isset($_GET['facid'])?$_GET['facid']:$_POST['facid']; +$action=isset($_GET['action'])?$_GET['action']:$_POST['action']; -$facid=isset($_GET["facid"])?$_GET["facid"]:$_POST["facid"]; -$action=isset($_GET["action"])?$_GET["action"]:$_POST["action"]; - -$page=$_GET["page"]; -$sortorder=$_GET["sortorder"]; -$sortfield=$_GET["sortfield"]; +$sortfield = isset($_GET['sortfield'])?$_GET['sortfield']:$_POST['sortfield']; +$sortorder = isset($_GET['sortorder'])?$_GET['sortorder']:$_POST['sortorder']; +$page=isset($_GET['page'])?$_GET['page']:$_POST['page']; /* * Sécurité accés client */ -if ($user->societe_id > 0) +if ($user->societe_id > 0) { - $action = ''; - $socidp = $user->societe_id; + $action = ''; + $socidp = $user->societe_id; } /* - * + * Actions */ -if ($action == 'add') { - $paiementfourn = new PaiementFourn($db); +if ($action == 'add_paiement') +{ + $error = 0; - $paiementfourn->facid = $facid; - $paiementfourn->facnumber = $_POST['facnumber']; - $paiementfourn->datepaye = $db->idate(mktime(12, 0 , 0, - $_POST["remonth"], - $_POST["reday"], - $_POST["reyear"])); - $paiementfourn->amount = $_POST['amount']; - $paiementfourn->accountid = $_POST['accountid']; - $paiementfourn->societe = $_POST['societe']; - $paiementfourn->paiementid = $_POST['paiementid']; - $paiementfourn->num_paiement = $_POST['num_paiement']; - $paiementfourn->note = $_POST['note']; + $datepaye = $db->idate(mktime(12, 0 , 0, + $_POST['remonth'], + $_POST['reday'], + $_POST['reyear'])); + $paiement_id = 0; + $total = 0; + // Génère tableau des montants amounts + $amounts = array(); + foreach ($_POST as $key => $value) + { + if (substr($key,0,7) == 'amount_') + { + $other_facid = substr($key,7); + $amounts[$other_facid] = $_POST[$key]; + $total = $total + $amounts[$other_facid]; + } + } - if ( $paiementfourn->create($user) ) - { - Header("Location: fiche.php?facid=$facid"); - } + // Effectue les vérifications des parametres + if ($_POST['paiementid'] <= 0) + { + $fiche_erreur_message = '
'.$langs->trans('ErrorFieldRequired',$langs->trans('PaymentMode')).'
'; + $error++; + } - $action = ''; + if ($conf->banque->enabled) + { + // Si module bank actif, un compte est obligatoire lors de la saisie + // d'un paiement + if (! $_POST['accountid']) + { + $fiche_erreur_message = '
'.$langs->trans('ErrorFieldRequired',$langs->trans('AccountToCredit')).'
'; + $error++; + } + } + + if ($total <= 0) + { + $fiche_erreur_message = '
'.$langs->trans('ErrorFieldRequired',$langs->trans('Amount')).'
'; + $error++; + } + + if (! $error) + { + $db->begin(); + + // Creation de la ligne paiement + $paiement = new PaiementFourn($db); + $paiement->datepaye = $datepaye; + $paiement->amounts = $amounts; // Tableau de montant + $paiement->paiementid = $_POST['paiementid']; + $paiement->num_paiement = $_POST['num_paiement']; + $paiement->note = $_POST['comment']; + + $paiement_id = $paiement->create($user); + if ($paiement_id > 0) + { + if ($conf->banque->enabled) + { + // Insertion dans llx_bank + $label = 'Règlement fournisseur'; + $acc = new Account($db, $_POST['accountid']); + //paiementid contient "CHQ ou VIR par exemple" + $bank_line_id = $acc->addline($paiement->datepaye, + $paiement->paiementid, + $label, + 0.0 - $paiement->total, + $paiement->num_paiement, + '', + $user); + + // Mise a jour fk_bank dans llx_paiement. + // On connait ainsi le paiement qui a généré l'écriture bancaire + if ($bank_line_id > 0) + { + $paiement->update_fk_bank($bank_line_id); + // Mise a jour liens (pour chaque facture concernées par le paiement) + foreach ($paiement->amounts as $key => $value) + { + $facid = $key; + $fac = new FactureFournisseur($db); + $fac->fetch($facid); + $fac->fetch_fournisseur(); + $acc->add_url_line($bank_line_id, + $paiement_id, + DOL_URL_ROOT.'/fourn/paiement/fiche.php?id=', + '(paiement)', + 'payment'); + $acc->add_url_line($bank_line_id, + $fac->fournisseur->id, + DOL_URL_ROOT.'/fourn/fiche.php?socid=', + $fac->fournisseur->nom, + 'company'); + } + } + else + { + $error++; + } + } + } + else + { + $this->error=$paiement->error; + $error++; + } + + if ($error == 0) + { + $loc = DOL_URL_ROOT.'/fourn/paiement/fiche.php?id='.$paiement_id; + $db->commit(); + Header('Location: '.$loc); + exit; + } + else + { + $db->rollback(); + } + } } /* - * - * + * Affichage */ llxHeader(); -if ($action == 'create') +$html=new Form($db); + +if ($fiche_erreur_message) { + print ''.$fiche_erreur_message.''; +} - $sql = "SELECT s.nom,s.idp, f.amount, f.total_ttc, f.facnumber"; - $sql .= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."facture_fourn as f WHERE f.fk_soc = s.idp"; - $sql .= " AND f.rowid = $facid"; +if ($action == 'create' || $action == 'add_paiement') +{ + $facture = new FactureFournisseur($db); + $facture->fetch($facid); - $result = $db->query($sql); - if ($result) - { - $num = $db->num_rows(); - if ($num) + $sql = 'SELECT s.nom,s.idp, f.amount, f.total_ttc as total, f.facnumber'; + $sql .= ' FROM '.MAIN_DB_PREFIX.'societe as s, '.MAIN_DB_PREFIX.'facture_fourn as f WHERE f.fk_soc = s.idp'; + $sql .= ' AND f.rowid = '.$facid; + $resql = $db->query($sql); + if ($resql) { - $obj = $db->fetch_object($result); - - $total = $obj->total_ttc; - - print_titre($langs->trans("DoPayment")); - print '
'; - print ''; - print ''; - - print ""; - - print ''; - print ""; - - print "'; - - $sql = "SELECT sum(p.amount) FROM ".MAIN_DB_PREFIX."paiementfourn as p WHERE p.fk_facture_fourn = $facid;"; - $result = $db->query($sql); - if ($result) { - $sumpayed = $db->result(0,0); - $db->free(); - } - print ''; - - print ""; - - print ""; - print "facnumber\">"; - print "idp\">"; - print "nom\">"; - - $html = new Form($db); - - print ""; - - print ''; - - print ''; - print '\n"; - - print "'; - - print "\n"; - - print "\n"; - - print "'; - print ''; - print ''; - print ''; - print "\n"; - print "
".$langs->trans("Bill")."
'.$langs->trans("Ref").' :'; - print ''.$obj->facnumber.'
".$langs->trans("Company")." :$obj->nom
".$langs->trans("AmountTTC")." :".price($obj->total_ttc).' '.$langs->trans("Currency".$conf->monnaie).'
'.$langs->trans("AlreadyPayed").' :'.price($sumpayed).' '.$langs->trans("Currency".$conf->monnaie).'
".$langs->trans("Payment")."
".$langs->trans("Date")." :"; - $html->select_date(); - print "'.$langs->trans("Comments").' :
'.$langs->trans("Type").' :
"; - print "
"; - print '
".$langs->trans("Number").":
N° du chèque ou du virement
".$langs->trans("Account").":'; + print ''; + print ''; + print ''; + print ''; + + print ''; + + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + if ($conf->banque->enabled) + { + print ''; + } + else + { + print ''; + } + /* + * Autres factures impayées + */ + $sql = 'SELECT f.rowid as facid,f.facnumber,f.total_ttc,'.$db->pdate('f.datef').' as df'; + $sql .= ', sum(pf.amount) as am'; + $sql .= ' FROM '.MAIN_DB_PREFIX.'facture_fourn as f'; + $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'paiementfourn_facturefourn as pf ON pf.fk_facturefourn = f.rowid'; + $sql .= ' WHERE f.fk_soc = '.$facture->socidp; + $sql .= ' AND f.paye = 0'; + $sql .= ' AND f.fk_statut = 1'; // Statut=0 => non validée, Statut=2 => annulée + $sql .= ' GROUP BY f.facnumber'; + $resql = $db->query($sql); + if ($resql) + { + $num = $db->num_rows($resql); + if ($num > 0) + { + $i = 0; + print '\n"; + } + $db->free($resql); + } + else + { + dolibarr_print_error($db); + } + + /* + * + */ + print ''; + print '
'.$langs->trans('Payment').'
'.$langs->trans('Company').''.$obj->nom.'
'.$langs->trans('Date').''; + if (!empty($_POST['remonth']) && !empty($_POST['reday']) && !empty($_POST['reyear'])) + $sel_date=mktime(12, 0 , 0, $_POST['remonth'], $_POST['reday'], $_POST['reyear']); + else + $sel_date=''; + $html->select_date($sel_date); + print ''.$langs->trans('Comments').'
'.$langs->trans('PaymentMode').''; + $html->select_types_paiements(empty($_POST['paiementid'])?'':$_POST['paiementid'],'paiementid'); + print ''; + print '
'.$langs->trans('Numero').'
Numéro du chèque / virement
'.$langs->trans('Account').''; + $html->select_comptes(empty($_POST['accountid'])?(defined('FACTURE_RIB_NUMBER')?FACTURE_RIB_NUMBER:''):$_POST['accountid'],'accountid',0,'',1); + print '
 
'; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + + $var=True; + $total=0; + $totalrecu=0; + while ($i < $num) + { + $objp = $db->fetch_object($resql); + $var=!$var; + print ''; + print ''; + if ($objp->df > 0 ) + { + print ''; + } + else + { + print ''; + } + print ''; + print ''; + print ''; + print '\n"; + $total+=$objp->total; + $total_ttc+=$objp->total_ttc; + $totalrecu+=$objp->am; + $i++; + } + if ($i > 1) + { + // Print total + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print "\n"; + } + print "
'.$langs->trans('Bill').''.$langs->trans('Date').''.$langs->trans('AmountTTC').''.$langs->trans('Paied').''.$langs->trans('RemainderToPay').''.$langs->trans('Amount').'
'.img_object($langs->trans('ShowBill'),'bill').' '.$objp->facnumber; + print ''; + print dolibarr_print_date($objp->df).'!!!'.price($objp->total_ttc).''.price($objp->am).''.price($objp->total_ttc - $objp->am).''; + $namef = 'amount_'.$objp->facid; + print ''; + print "
'.$langs->trans('TotalTTC').':'.price($total_ttc).''.price($totalrecu).''.price($total_ttc - $totalrecu).' 
'; + print ''; } - print '>'.$objopt->label.''; - $i++; - } } - print ""; - print "
".$langs->trans("RemainderToPay")." :".price($total - $sumpayed).' '.$langs->trans("Currency".$conf->monnaie).'
'.$langs->trans("AmountTTC").' :
\n"; - - } - } -} +} /* * Affichage liste */ -if ($action == '') { +if (! $_GET['action'] && ! $_POST['action']) +{ + if ($page == -1) $page = 0 ; + $limit = $conf->liste_limit; + $offset = $limit * $page ; - if ($page == -1) - { - $page = 0 ; - } - $limit = $conf->liste_limit; - $offset = $limit * $page ; + if (! $sortorder) $sortorder='DESC'; + if (! $sortfield) $sortfield='p.datep'; - $sql = "SELECT p.rowid, ".$db->pdate("p.datep")." as dp, p.amount, f.amount as fa_amount, f.facnumber, s.nom"; - $sql .=", f.rowid as facid, c.libelle as paiement_type, p.num_paiement"; - $sql .= " FROM ".MAIN_DB_PREFIX."paiementfourn as p, ".MAIN_DB_PREFIX."facture_fourn as f, ".MAIN_DB_PREFIX."c_paiement as c, ".MAIN_DB_PREFIX."societe as s"; - $sql .= " WHERE p.fk_facture_fourn = f.rowid AND p.fk_paiement = c.id AND s.idp = f.fk_soc"; - - if ($socidp) - { - $sql .= " AND f.fk_soc = $socidp"; - } - - $sql .= " ORDER BY datep DESC"; - $sql .= $db->plimit($limit + 1 ,$offset); - $result = $db->query($sql); - - if ($result) - { - $num = $db->num_rows(); - $i = 0; - $var=True; - - print_barre_liste($langs->trans("Payments"), $page, "paiement.php","&socidp=$socidp",$sortfield,$sortorder,'',$num); - - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ""; - - while ($i < min($num,$limit)) + $sql = 'SELECT p.rowid, '.$db->pdate('p.datep').' as dp, p.amount'; + $sql .= ', f.amount as fa_amount, f.facnumber, s.nom'; + $sql .= ', f.rowid as facid, c.libelle as paiement_type, p.num_paiement'; + $sql .= ', ba.rowid as bid, ba.label'; + $sql .= ' FROM '.MAIN_DB_PREFIX.'paiementfourn AS p'; + $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'paiementfourn_facturefourn AS pf ON p.rowid=pf.fk_paiementfourn'; + $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'facture_fourn AS f ON f.rowid=pf.fk_facturefourn '; + $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_paiement AS c ON p.fk_paiement = c.id'; + $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'societe AS s ON s.idp = f.fk_soc'; + $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'bank as b ON p.fk_bank = b.rowid'; + $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'bank_account as ba ON b.fk_account = ba.rowid'; + if ($socidp) { - $objp = $db->fetch_object($result); - $var=!$var; - print ""; - print "'; - - - print "'; - print ''; - print "\n"; - print "\n"; - print ''; - print ""; - $i++; + $sql .= ' WHERE f.fk_soc = '.$socidp; } - print "
'.$langs->trans("Ref").''.$langs->trans("Bill").''.$langs->trans("Company").''.$langs->trans("Date").''.$langs->trans("Type").''.$langs->trans("AmountTTC").' 
rowid\">".img_object($langs->trans("ShowPayment"),"payment").''; - - print " rowid\">".$objp->rowid.'facid\">".img_object($langs->trans("ShowBill"),"bill").' '.$objp->facnumber.''.$objp->nom.'".dolibarr_print_date($objp->dp)."$objp->paiement_type $objp->num_paiement'.price($objp->amount).' 
"; - } - else - { - dolibarr_print_error($db); - } + $sql .= ' ORDER BY '.$sortfield.' '.$sortorder; + $sql .= $db->plimit($limit + 1 ,$offset); +/* + $sql = "SELECT p.rowid,".$db->pdate("p.datep")." as dp, p.amount,"; + $sql.= " p.statut, p.num_paiement,"; + $sql.= " c.libelle as paiement_type,"; + $sql.= " ba.rowid as bid, ba.label"; + $sql.= " FROM ".MAIN_DB_PREFIX."paiement as p,"; + $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."bank as b ON p.fk_bank = b.rowid"; + $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."bank_account as ba ON b.fk_account = ba.rowid"; + if ($socidp) + { + $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."paiement_facture as pf ON p.rowid = pf.fk_paiement"; + $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."facture as f ON pf.fk_facture = f.rowid"; + } + $sql.= " WHERE p.fk_paiement = c.id"; + if ($socidp) + { + $sql.= " AND f.fk_soc = ".$socidp; + } + if ($_GET["search_montant"]) + { + $sql .=" AND p.amount=".ereg_replace(",",".",$_GET["search_montant"]); + } +*/ + $resql = $db->query($sql); + if ($resql) + { + $num = $db->num_rows($resql); + $i = 0; + $var=True; + + print_barre_liste($langs->trans('Payments'), $page, 'paiement.php','',$sortfield,$sortorder,'',$num); + print ''; + print ''; + print ''; + print_liste_field_titre($langs->trans('Bill'),'paiement.php','facnumber','','','',$sortfield); + print_liste_field_titre($langs->trans('Company'),'paiement.php','s.nom','','','',$sortfield); + print_liste_field_titre($langs->trans('Date'),'paiement.php','dp','','','',$sortfield); + print_liste_field_titre($langs->trans('Type'),'paiement.php','libelle','','','',$sortfield); + print_liste_field_titre($langs->trans('Account'),'paiement.php','ba.label','','','',$sortfield); + print_liste_field_titre($langs->trans('AmountTTC'),'paiement.php','fa_amount','','','align="right"',$sortfield); + print ''; + print "\n"; + + while ($i < min($num,$limit)) + { + $objp = $db->fetch_object($resql); + $var=!$var; + print ''; + print ''; + print ''; + print ''; + print '\n"; + print '\n"; + print ''; + print ''; + print ''; + $i++; + } + print "
'.$langs->trans('Ref').' 
'.img_object($langs->trans('ShowPayment'),'payment').''; + print ' '.$objp->rowid.''.img_object($langs->trans('ShowBill'),'bill').' '.$objp->facnumber.''.$objp->nom.''.dolibarr_print_date($objp->dp)."'.$objp->paiement_type.' '.$objp->num_paiement."'; + if ($objp->bid) print ''.img_object($langs->trans("ShowAccount"),'account').' '.$objp->label.''; + else print ' '; + print ''.price($objp->amount).' 
"; + } + else + { + dolibarr_print_error($db); + } } $db->close(); diff --git a/htdocs/fourn/facture/paiementfourn.class.php b/htdocs/fourn/facture/paiementfourn.class.php index 4eb27ca31da..aec71903ae9 100644 --- a/htdocs/fourn/facture/paiementfourn.class.php +++ b/htdocs/fourn/facture/paiementfourn.class.php @@ -1,6 +1,7 @@ - * Copyright (C) 2004 Laurent Destailleur +/* Copyright (C) 2002-2004 Rodolphe Quiedeville + * Copyright (C) 2004 Laurent Destailleur + * 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 @@ -18,215 +19,393 @@ * * $Id$ * $Source$ - * */ -/*! - \file htdocs/fourn/facture/paiementfourn.class.php +/*! + \file htdocs/fourn/facture/paiementfourn.class.php \ingroup fournisseur, facture \brief Page de création de paiement factures fournisseurs \version $Revision$ */ -require_once(DOL_DOCUMENT_ROOT."/compta/bank/account.class.php"); +require_once(DOL_DOCUMENT_ROOT.'/compta/bank/account.class.php'); + +/** + \class PaiementFourn + \brief Classe permettant la gestion des paiements des factures fournisseurs +*/ class PaiementFourn { - var $id; - var $db; - var $facid; - var $facnumber; - var $datepaye; - var $amount; - var $accountid; - var $paiementid; // Cette variable contient le type de paiement, 7 pour CHQ, etc... (nom pas tres bien choisi) - var $num_paiement; - var $note; - var $societe; - /* - * - * - * - */ - function PaiementFourn($DB) - { - $this->db = $DB ; - } - /* - * - * - */ - function fetch($id,$user) - { - /* - */ - $error = 0; + var $id; + var $ref; + var $facid; + var $datepaye; + var $amount; + var $total; + var $author; + var $paiementid; // Type de paiement. Stocké dans fk_paiement + // de llx_paiement qui est lié aux types de + //paiement de llx_c_paiement + var $num_paiement; // Numéro du CHQ, VIR, etc... + var $bank_account; // Id compte bancaire du paiement + var $bank_line; // Id de la ligne d'écriture bancaire + var $note; + // fk_paiement dans llx_paiement est l'id du type de paiement (7 pour CHQ, ...) + // fk_paiement dans llx_paiement_facture est le rowid du paiement - - $sql = "SELECT fk_facture_fourn, datec, datep, amount, fk_user_author, fk_paiement, num_paiement, note"; - $sql .= " FROM ".MAIN_DB_PREFIX."paiementfourn"; - $sql .= " WHERE rowid = ".$id.";"; - - $resql = $this->db->query($sql); + var $db; - if ($resql) - { - $num = $this->db->num_rows($resl); - if ($num > 0) - { - $obj = $this->db->fetch_object($resql); + /** + * \brief Constructeur de la classe + * \param DB handler accès base de données + */ - $this->facture_id = $obj->fk_facture_fourn; - $this->date = $obj->datep; - $this->montant = $obj->amount; - - - } - else - { - $error = 2; - } - } - else - { - print "$sql"; - $error = 1; - } - - return $error; - - } - /* - * - */ - /* - * \brief Information sur l'objet - * \param id id du paiement dont il faut afficher les infos - */ - - function info($id) - { - $sql = "SELECT c.rowid, ".$this->db->pdate("datec")." as datec, fk_user_author"; - $sql .= " FROM ".MAIN_DB_PREFIX."paiementfourn as c"; - $sql .= " WHERE c.rowid = $id"; - - if ($this->db->query($sql)) + function PaiementFourn($DB) { - if ($this->db->num_rows()) - { - $obj = $this->db->fetch_object(); - - $this->id = $obj->idp; - - if ($obj->fk_user_creat) { - $cuser = new User($this->db, $obj->fk_user_creat); - $cuser->fetch(); - $this->user_creation = $cuser; - } - - if ($obj->fk_user_modif) { - $muser = new User($this->db, $obj->fk_user_modif); - $muser->fetch(); - $this->user_modification = $muser; - } - - $this->date_creation = $obj->datec; - $this->date_modification = $obj->tms; - - } - $this->db->free(); - + $this->db = $DB ; } - else + + /** + * \brief Récupère l'objet paiement + * \param id id du paiement a récupérer + * \return int <0 si ko, >0 si ok + */ + function fetch($id) { - dolibarr_print_error($this->db); + $sql = 'SELECT p.rowid,'.$this->db->pdate('p.datep').' as dp, p.amount, p.statut, p.fk_bank'; + $sql .=', c.libelle as paiement_type'; + $sql .= ', p.num_paiement, p.note, b.fk_account'; + $sql .= ' FROM '.MAIN_DB_PREFIX.'paiementfourn as p, '.MAIN_DB_PREFIX.'c_paiement as c '; + $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'bank as b ON p.fk_bank = b.rowid '; + $sql .= ' WHERE p.fk_paiement = c.id'; + $sql .= ' AND p.rowid = '.$id; + $resql = $this->db->query($sql); + if ($resql) + { + $num = $this->db->num_rows($resql); + if ($num > 0) + { + $obj = $this->db->fetch_object($resql); + $this->id = $obj->rowid; + $this->ref = $obj->rowid; + $this->date = $obj->dp; + $this->numero = $obj->num_paiement; + $this->bank_account = $obj->fk_account; + $this->bank_line = $obj->fk_bank; + $this->montant = $obj->amount; + $this->note = $obj->note; + $this->type_libelle = $obj->paiement_type; + $this->statut = $obj->statut; + $error = 1; + } + else + { + $error = -2; + } + $this->db->free($resql); + } + else + { + dolibarr_print_error($this->db); + $error = -1; + } + return $error; } - } - /* - * - */ - function create($user) - { - /* - * Insertion dans la base - */ - $this->amount = ereg_replace(",",".",$this->amount); - $this->amount = ereg_replace(" ","",$this->amount); - - $sql = "INSERT INTO ".MAIN_DB_PREFIX."paiementfourn (fk_facture_fourn, datec, datep, amount, fk_user_author, fk_paiement, num_paiement, note)"; - $sql .= " VALUES ('$this->facid', now(), '$this->datepaye', '$this->amount', '$user->id', '$this->paiementid', '$this->num_paiement', '$this->note')"; - - $result = $this->db->query($sql); + /** + * \brief Création du paiement en base + * \param user object utilisateur qui crée + * \return int id du paiement crée, < 0 si erreur + */ + function create($user) + { + $sql_err = 0; - if ($result) - { - $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."paiementfourn"); - - $label = "Règlement facture $this->facnumber - $this->societe"; - - $account = new Account($this->db, $this->accountid); + $this->db->begin(); - $result = $account->addline($this->datepaye, - $this->paiementid, - $label, - -$this->amount, - $this->num_paiement); + $this->total = 0.0; + foreach ($this->amounts as $key => $value) + { + $val = price2num($value); + if (is_numeric($val)) + { + $val = price2num(round($val, 2)); + $this->total += $val; + } + $this->amounts[$key] = $val; + } + $this->total = price2num($this->total); + if ($this->total <> 0) /* On accepte les montants négatifs pour les avoirs ??? */ + { + $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'paiementfourn (datec, datep, amount, fk_paiement, num_paiement, note, fk_user_author)'; + $sql .= ' VALUES (now(), '.$this->datepaye.', \''.$this->total.'\', '.$this->paiementid.', \''.$this->num_paiement.'\', \''.$this->note.'\', '.$user->id.')'; + $resql = $this->db->query($sql); + if ($resql) + { + $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.'paiementfourn'); + foreach ($this->amounts as $key => $amount) + { + $facid = $key; + if (is_numeric($amount) && $amount <> 0) + { + $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'paiementfourn_facturefourn (fk_facturefourn, fk_paiementfourn, amount)'; + $sql .= ' VALUES ('.$facid.','. $this->id.',\''.$amount.'\')'; + if (! $this->db->query($sql) ) + { + dolibarr_syslog('Paiement::Create Erreur INSERT dans paiement_facture '.$facid); + $sql_err++; + } + } + else + { + dolibarr_syslog('PaiementFourn::Create Montant non numérique'); + } + } + } + else + { + dolibarr_syslog('PaiementFourn::Create Erreur INSERT dans paiementfourn'); + $sql_err++; + } + } - - // Mise a jour fk_bank dans llx_paiement_fourn - if ($result) - { - $this->bankid = $this->db->last_insert_id(MAIN_DB_PREFIX."bank"); - - $sql = "UPDATE ".MAIN_DB_PREFIX."paiementfourn SET fk_bank=$this->bankid WHERE rowid=$this->id"; - $result = $this->db->query($sql); - } - } - else - { - print "$sql"; - } - - return 1; - } + if ( $this->total <> 0 && $sql_err == 0 ) // On accepte les montants négatifs + { + $this->db->commit(); + dolibarr_syslog('PaiementFourn::Create Ok Total = '.$this->total); + return $this->id; + } + else + { + $this->db->rollback(); + dolibarr_syslog('PaiementFourn::Create Erreur'); + return -1; + } + } - /** - * \brief Affiche la liste des modes de paiement possible - * \param name nom du champ select - * \param filtre filtre sur un sens de paiement particulier, norme ISO (CRDT=Mode propre à un crédit, DBIT=mode propre à un débit) - * \param id ??? - */ - function select($name, $filtre='', $id='') - { - $form = new Form($this->db); + /** + * \brief Affiche la liste des modes de paiement possible + * \param name nom du champ select + * \param filtre filtre sur un sens de paiement particulier, norme ISO (CRDT=Mode propre à un crédit, DBIT=mode propre à un débit) + * \param id ??? + */ + function select($name, $filtre='', $id='') + { + $form = new Form($this->db); - if ($filtre == 'CRDT' || $filtre == 'crédit') - { - $sql = "SELECT id, libelle FROM ".MAIN_DB_PREFIX."c_paiement WHERE active=1 AND type IN (0,2) ORDER BY libelle"; - } - elseif ($filtre == 'DBIT' || $filtre == 'débit') - { - $sql = "SELECT id, libelle FROM ".MAIN_DB_PREFIX."c_paiement WHERE active=1 AND type IN (1,2) ORDER BY libelle"; - } - else - { - $sql = "SELECT id, libelle FROM ".MAIN_DB_PREFIX."c_paiement WHERE active=1 ORDER BY libelle"; - } - $form->select($name, $sql, $id); - } + if ($filtre == 'CRDT' || $filtre == 'crédit') + { + $sql = 'SELECT id, libelle FROM '.MAIN_DB_PREFIX.'c_paiement WHERE active=1 AND type IN (0,2) ORDER BY libelle'; + } + elseif ($filtre == 'DBIT' || $filtre == 'débit') + { + $sql = 'SELECT id, libelle FROM '.MAIN_DB_PREFIX.'c_paiement WHERE active=1 AND type IN (1,2) ORDER BY libelle'; + } + else + { + $sql = 'SELECT id, libelle FROM '.MAIN_DB_PREFIX.'c_paiement WHERE active=1 ORDER BY libelle'; + } + $form->select($name, $sql, $id); + } - /* - * - * - * - */ - function delete($id) - { - $sql = "DELETE FROM ".MAIN_DB_PREFIX."paiementfourn WHERE rowid = $id"; - - return $this->db->query($sql); - } + /** + * \brief Supprime un paiement ainsi que les lignes qu'il a généré dans comptes + * Si le paiement porte sur un écriture compte qui est rapprochée, on refuse + * Si le paiement porte sur au moins une facture à "payée", on refuse + * \return int <0 si ko, >0 si ok + */ + function delete() + { + $bank_line_id = $this->bank_line; + + $this->db->begin(); + + // Vérifier si paiement porte pas sur une facture à l'état payée + // Si c'est le cas, on refuse la suppression + $billsarray=$this->getBillsArray('paye=1'); + if (is_array($billsarray)) + { + if (sizeof($billsarray)) + { + $this->error='Impossible de supprimer un paiement portant sur au moins une facture à l\'état payé'; + $this->db->rollback(); + return -1; + } + } + else + { + $this->db->rollback(); + return -2; + } + + // Vérifier si paiement ne porte pas sur ecriture bancaire rapprochée + // Si c'est le cas, on refuse le paiement + if ($bank_line_id) + { + $accline = new AccountLine($this->db,$bank_line_id); + $accline->fetch($bank_line_id); + if ($accline->rappro) + { + $this->error='Impossible de supprimer un paiement qui a généré une écriture qui a été rapprochée'; + $this->db->rollback(); + return -3; + } + } + + // Efface la ligne de paiement (dans paiement_facture et paiement) + $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'paiementfourn_facturefourn'; + $sql.= ' WHERE fk_paiementfourn = '.$this->id; + $resql = $this->db->query($sql); + if ($resql) + { + $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'paiementfourn'; + $sql.= ' WHERE rowid = '.$this->id; + $result = $this->db->query($sql); + if (! $result) + { + $this->error=$this->db->error(); + $this->db->rollback(); + return -3; + } + + // Supprimer l'écriture bancaire si paiement lié à écriture + if ($bank_line_id) + { + $acc = new Account($this->db); + $result=$acc->deleteline($bank_line_id); + if ($result < 0) + { + $this->error=$acc->error; + $this->db->rollback(); + return -4; + } + } + $this->db->commit(); + return 1; + } + else + { + $this->error=$this->db->error; + $this->db->rollback(); + return -5; + } + } + + /** + * \brief Mise a jour du lien entre le paiement et la ligne générée dans llx_bank + * \param id_bank Id compte bancaire + */ + function update_fk_bank($id_bank) + { + $sql = 'UPDATE '.MAIN_DB_PREFIX.'paiementfourn set fk_bank = '.$id_bank; + $sql.= ' WHERE rowid = '.$this->id; + $result = $this->db->query($sql); + if ($result) + { + return 1; + } + else + { + dolibarr_print_error($this->db); + return 0; + } + } + + /** + * \brief Valide le paiement + * \return int <0 si ko, >0 si ok + */ + function valide() + { + $sql = 'UPDATE '.MAIN_DB_PREFIX.'paiementfourn SET statut = 1 WHERE rowid = '.$this->id; + $result = $this->db->query($sql); + if ($result) + { + return 0; + } + else + { + dolibarr_syslog('Paiement::Valide Error -1'); + return -1; + } + } + + /* + * \brief Information sur l'objet + * \param id id du paiement dont il faut afficher les infos + */ + function info($id) + { + $sql = 'SELECT c.rowid, '.$this->db->pdate('datec').' as datec, fk_user_author'; + $sql .= ', '.$this->db->pdate('tms').' as tms'; + $sql .= ' FROM '.MAIN_DB_PREFIX.'paiementfourn as c'; + $sql .= ' WHERE c.rowid = '.$id; + $resql = $this->db->query($sql); + if ($resql) + { + $num = $this->db->num_rows($resql); + if ($num) + { + $obj = $this->db->fetch_object($resql); + $this->id = $obj->idp; + if ($obj->fk_user_creat) + { + $cuser = new User($this->db, $obj->fk_user_creat); + $cuser->fetch(); + $this->user_creation = $cuser; + } + if ($obj->fk_user_modif) + { + $muser = new User($this->db, $obj->fk_user_modif); + $muser->fetch(); + $this->user_modification = $muser; + } + $this->date_creation = $obj->datec; + $this->date_modification = $obj->tms; + } + $this->db->free($resql); + } + else + { + dolibarr_print_error($this->db); + } + } + + /** + * \brief Retourne la liste des factures sur lesquels porte le paiement + * \param filter Critere de filtre + * \return array Tableau des id de factures + */ + function getBillsArray($filter='') + { + $sql = 'SELECT fk_facturefourn'; + $sql.= ' FROM '.MAIN_DB_PREFIX.'paiementfourn_facturefourn as pf, '.MAIN_DB_PREFIX.'facture_fourn as f'; + $sql.= ' WHERE pf.fk_facturefourn = f.rowid AND fk_paiementfourn = '.$this->id; + if ($filter) $sql.= ' AND '.$filter; + $resql = $this->db->query($sql); + if ($resql) + { + $i=0; + $num=$this->db->num_rows($resql); + $billsarray=array(); + + while ($i < $num) + { + $obj = $this->db->fetch_object($resql); + $billsarray[$i]=$obj->fk_facture; + $i++; + } + + return $billsarray; + } + else + { + $this->error=$this->db->error(); + dolibarr_syslog('PaiementFourn::getBillsArray Error '.$this->error.' - sql='.$sql); + return -1; + } + } } ?> diff --git a/htdocs/fourn/fiche.php b/htdocs/fourn/fiche.php index 62c7876a358..58b115b6d66 100644 --- a/htdocs/fourn/fiche.php +++ b/htdocs/fourn/fiche.php @@ -28,15 +28,15 @@ \version $Revision$ */ -require("./pre.inc.php"); +require('./pre.inc.php'); -$langs->load("suppliers"); -$langs->load("products"); -$langs->load("bills"); -$langs->load("orders"); -$langs->load("companies"); +$langs->load('suppliers'); +$langs->load('products'); +$langs->load('bills'); +$langs->load('orders'); +$langs->load('companies'); -$socid = $_GET["socid"]; +$socid = $_GET['socid']; /* * Sécurité accés client */ @@ -57,7 +57,7 @@ if ( $societe->fetch($socid) ) $addons[0][0] = DOL_URL_ROOT.'/fourn/fiche.php?socid='.$socid; $addons[0][1] = $societe->nom; - llxHeader('',$langs->trans("SupplierCard").' : '.$societe->nom, $addons); + llxHeader('',$langs->trans('SupplierCard').' : '.$societe->nom, $addons); /* * Affichage onglets @@ -65,49 +65,49 @@ if ( $societe->fetch($socid) ) $h = 0; $head[$h][0] = DOL_URL_ROOT.'/soc.php?socid='.$socid; - $head[$h][1] = $langs->trans("Company"); + $head[$h][1] = $langs->trans('Company'); $h++; if ($societe->client==1) { $head[$h][0] = DOL_URL_ROOT.'/comm/fiche.php?socid='.$socid; - $head[$h][1] = $langs->trans("Customer"); + $head[$h][1] = $langs->trans('Customer'); $h++; } if ($societe->client==2) { $head[$h][0] = DOL_URL_ROOT.'/comm/prospect/fiche.php?id='.$socid; - $head[$h][1] = $langs->trans("Prospect"); + $head[$h][1] = $langs->trans('Prospect'); $h++; } if ($societe->fournisseur) { $hselected=$h; $head[$h][0] = DOL_URL_ROOT.'/fourn/fiche.php?socid='.$socid; - $head[$h][1] = $langs->trans("Supplier"); + $head[$h][1] = $langs->trans('Supplier'); $h++; } if ($conf->compta->enabled) { - $langs->load("compta"); + $langs->load('compta'); $head[$h][0] = DOL_URL_ROOT.'/compta/fiche.php?socid='.$socid; - $head[$h][1] = $langs->trans("Accountancy"); + $head[$h][1] = $langs->trans('Accountancy'); $h++; } $head[$h][0] = DOL_URL_ROOT.'/socnote.php?socid='.$societe->id; - $head[$h][1] = $langs->trans("Note"); + $head[$h][1] = $langs->trans('Note'); $h++; if ($user->societe_id == 0) { $head[$h][0] = DOL_URL_ROOT.'/docsoc.php?socid='.$societe->id; - $head[$h][1] = $langs->trans("Documents"); + $head[$h][1] = $langs->trans('Documents'); $h++; } $head[$h][0] = DOL_URL_ROOT.'/societe/notify/fiche.php?socid='.$societe->id; - $head[$h][1] = $langs->trans("Notifications"); + $head[$h][1] = $langs->trans('Notifications'); $h++; dolibarr_fiche_head($head, $hselected, $societe->nom); diff --git a/htdocs/fourn/fournisseur.class.php b/htdocs/fourn/fournisseur.class.php index b2d83f546b1..c6ed1c392a6 100644 --- a/htdocs/fourn/fournisseur.class.php +++ b/htdocs/fourn/fournisseur.class.php @@ -22,7 +22,7 @@ /** \file htdocs/fourn/fournisseur.class.php - \ingroup societe + \ingroup fournisseur,societe \brief Fichier de la classe des fournisseurs \version $Revision$ */ diff --git a/htdocs/fourn/fournisseur.facture.class.php b/htdocs/fourn/fournisseur.facture.class.php index e89357bc46a..ecc52c02438 100644 --- a/htdocs/fourn/fournisseur.facture.class.php +++ b/htdocs/fourn/fournisseur.facture.class.php @@ -1,7 +1,8 @@ - * Copyright (C) 2004-2005 Laurent Destailleur - * Copyright (C) 2004 Christophe Combelles +/* Copyright (C) 2002-2004 Rodolphe Quiedeville + * 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 @@ -21,546 +22,551 @@ * $Source$ */ -/** - \file htdocs/fourn/fournisseur.facture.class.php - \ingroup fournisseur - \brief Fichier de la classe des factures fournisseurs - \version $Revision$ +/** + \file htdocs/fourn/fournisseur.facture.class.php + \ingroup fournisseur,facture + \brief Fichier de la classe des factures fournisseurs + \version $Revision$ */ /** - \class FactureFournisseur - \brief Classe permettant la gestion des factures fournisseurs + \class FactureFournisseur + \brief Classe permettant la gestion des factures fournisseurs */ class FactureFournisseur { - var $id; - var $db; - var $socid; - var $number; - var $statut; - var $paye; - var $author; - var $libelle; - var $date; - var $date_echeance; - var $ref; - var $amount; - var $remise; - var $tva; - var $total_ht; - var $total_tva; - var $total_ttc; - var $note; - var $db_table; - var $propalid; - var $lignes; - - /** - * \brief Constructeur de la classe - * \param DB handler accès base de données - * \param soc_idp id societe ("" par defaut) - * \param facid id facture ("" par defaut) - */ - function FactureFournisseur($DB, $soc_idp="", $facid="") - { - $this->db = $DB ; - $this->socidp = $soc_idp; - $this->products = array(); - $this->db_table = MAIN_DB_PREFIX."facture"; - $this->amount = 0; - $this->remise = 0; - $this->tva = 0; - $this->total = 0; - $this->propalid = 0; - $this->id = $facid; - - $this->lignes = array(); - } + var $id; + var $db; + var $socidp; + var $statut; + var $paye; + var $author; + var $libelle; + var $date; + var $date_echeance; + var $ref; + var $amount; + var $remise; + var $tva; + var $total_ht; + var $total_tva; + var $total_ttc; + var $note; + var $propalid; + var $lignes; + var $fournisseur; - /** - * \brief Création de la facture en base - * \param user object utilisateur qui crée - * \return int id facture si ok, < 0 si erreur - */ - function create($user) - { - global $langs; - - // Nettoyage parametres - $socid = $this->socidp; - $number = strtoupper($this->number); - $amount = $this->amount; - $remise = $this->remise; - - $this->db->begin(); - - if (! $remise) $remise = 0 ; - $totalht = ($amount - $remise); - - $sql = "INSERT INTO ".MAIN_DB_PREFIX."facture_fourn (facnumber, libelle, fk_soc, datec, datef, note, fk_user_author, date_lim_reglement) "; - $sql .= " VALUES ('".addslashes($number)."','".addslashes($this->libelle)."',"; - $sql .= $this->socid.", now(),'".$this->db->idate($this->date)."','".addslashes($this->note)."', ".$user->id.",'".$this->db->idate($this->date_echeance)."');"; - - $resql=$this->db->query($sql); - - if ($resql) - { - $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."facture_fourn"); - - for ($i = 0 ; $i < sizeof($this->lignes) ; $i++) - { - - $sql = "INSERT INTO ".MAIN_DB_PREFIX."facture_fourn_det (fk_facture_fourn)"; - $sql .= " VALUES ($this->id);"; - if ($this->db->query($sql)) - { - $idligne = $this->db->last_insert_id(MAIN_DB_PREFIX."facture_fourn_det"); - - $this->updateline($idligne, - $this->lignes[$i][0], - $this->lignes[$i][1], - $this->lignes[$i][2], - $this->lignes[$i][3]); - } - } - - // Mise à jour prix - if ($this->updateprice($this->id) > 0) - { - $this->db->commit(); - return $this->id; - } - else - { - $this->error=$langs->trans("FailedToUpdatePrice"); - $this->db->rollback(); - return -3; - } - } - else - { - if ($this->db->errno() == 'DB_ERROR_RECORD_ALREADY_EXISTS') - { - $this->error=$langs->trans("ErrorBillRefAlreadyExists"); - $this->db->rollback(); - return -1; - } - else - { - $this->error=$this->db->error(); - $this->db->rollback(); - return -2; - } - } - } - - /** - * \brief Recupére l'objet facture et ses lignes de factures - * \param rowid id de la facture a récupérer - */ - function fetch($rowid) - { - $sql = "SELECT fk_soc,libelle,facnumber,amount,remise,".$this->db->pdate(datef)."as df"; - $sql .= ", total_ht, total_tva, total_ttc, fk_user_author"; - $sql .= ", fk_statut, paye, f.note,".$this->db->pdate(date_lim_reglement)."as de"; - $sql .= ", s.nom as socnom, s.idp as socidp"; - $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn as f,".MAIN_DB_PREFIX."societe as s"; - $sql .= " WHERE f.rowid=$rowid AND f.fk_soc = s.idp ;"; - - if ($this->db->query($sql) ) + /** + * \brief Constructeur de la classe + * \param DB handler accès base de données + * \param soc_idp id societe ('' par defaut) + * \param facid id facture ('' par defaut) + */ + function FactureFournisseur($DB, $soc_idp='', $facid='') { - if ($this->db->num_rows()) - { - $obj = $this->db->fetch_object(); - - $this->id = $rowid; - $this->datep = $obj->df; - $this->date_echeance = $obj->de; - $this->ref = $obj->facnumber; - $this->libelle = $obj->libelle; + $this->db = $DB ; + $this->socidp = $soc_idp; + $this->products = array(); + $this->amount = 0; + $this->remise = 0; + $this->tva = 0; + $this->total_ht = 0; + $this->total_tva = 0; + $this->total_ttc = 0; + $this->propalid = 0; + $this->id = $facid; - $this->remise = $obj->remise; - $this->socidp = $obj->fk_soc; + $this->lignes = array(); + } - $this->total_ht = $obj->total_ht; - $this->total_tva = $obj->total_tva; - $this->total_ttc = $obj->total_ttc; + /** + * \brief Création de la facture en base + * \param user object utilisateur qui crée + * \return int id facture si ok, < 0 si erreur + */ + function create($user) + { + global $langs; - $this->author = $obj->fk_user_author; + $socidp = $this->socidp; + $number = $this->ref; + $amount = $this->amount; + $remise = $this->remise; - $this->statut = $obj->fk_statut; - $this->paye = $obj->paye; + $this->db->begin(); - $this->socidp = $obj->socidp; - $this->socnom = $obj->socnom; - $this->note = $obj->note; - $this->db->free(); + if (! $remise) $remise = 0 ; + $totalht = ($amount - $remise); +// NE ME SEMBLE PLUS JUSTIFIE ICI +// $tva = tva($totalht); +// $total = $totalht + $tva; - /* - * Lignes - */ - $sql = "SELECT rowid,description, pu_ht, qty, tva_taux, tva, total_ht, total_ttc FROM ".MAIN_DB_PREFIX."facture_fourn_det WHERE fk_facture_fourn=".$this->id; - - if ($this->db->query($sql) ) + $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'facture_fourn (facnumber, libelle, fk_soc, datec, datef, note, fk_user_author, date_lim_reglement) '; + $sql .= " VALUES ('".addslashes($number)."','".addslashes($this->libelle)."',"; + $sql .= $this->socidp.", now(),'".$this->db->idate($this->date)."','".addslashes($this->note)."', ".$user->id.",'".$this->db->idate($this->date_echeance)."');"; + $resql=$this->db->query($sql); + if ($resql) { - $num = $this->db->num_rows(); - $i = 0; - if ($num) - { - while ($i < $num) + $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.'facture_fourn'); + for ($i = 0 ; $i < sizeof($this->lignes) ; $i++) { - $obj = $this->db->fetch_object(); - $this->lignes[$i][0] = stripslashes($obj->description); - $this->lignes[$i][1] = $obj->pu_ht; - $this->lignes[$i][2] = $obj->tva_taux; - $this->lignes[$i][3] = $obj->qty; - $this->lignes[$i][4] = $obj->total_ht; - $this->lignes[$i][5] = $obj->tva; - $this->lignes[$i][6] = $obj->total_ttc; - $this->lignes[$i][7] = $obj->rowid; - $i++; + $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'facture_fourn_det (fk_facture_fourn)'; + $sql .= ' VALUES ('.$this->id.');'; + $resql_insert=$this->db->query($sql); + if ($resql_insert) + { + $idligne = $this->db->last_insert_id(MAIN_DB_PREFIX.'facture_fourn_det'); + $this->updateline($idligne, + $this->lignes[$i][0], + $this->lignes[$i][1], + $this->lignes[$i][2], + $this->lignes[$i][3]); + } + } + // Mise à jour prix + if ($this->updateprice($this->id) > 0) + { + $this->db->commit(); + return $this->id; + } + else + { + $this->error=$langs->trans('FailedToUpdatePrice'); + $this->db->rollback(); + return -3; } - } } - else + else { - dolibarr_print_error($this->db); + if ($this->db->errno() == 'DB_ERROR_RECORD_ALREADY_EXISTS') + { + $this->error=$langs->trans('ErrorBillRefAlreadyExists'); + $this->db->rollback(); + return -1; + } + else + { + $this->error=$this->db->error(); + $this->db->rollback(); + return -2; + } } - } } - else + + /** + * \brief Recupére l'objet facture et ses lignes de factures + * \param rowid id de la facture a récupérer + */ + function fetch($rowid) { - dolibarr_print_error($this->db); - } - } - - /** - * \brief Supprime la facture - * \param rowid id de la facture à supprimer - */ - function delete($rowid) - { - - $sql = "DELETE FROM ".MAIN_DB_PREFIX."facture_fourn WHERE rowid = $rowid AND fk_statut = 0"; - - if ( $this->db->query( $sql) ) - { - if ( $this->db->affected_rows() ) - { - $sql = "DELETE FROM ".MAIN_DB_PREFIX."facture_fourn_det WHERE fk_facture_fourn = $rowid;"; - - if ($this->db->query( $sql) ) + $sql = 'SELECT libelle, facnumber, amount, remise, '.$this->db->pdate(datef).'as df'; + $sql .= ', total_ht, total_tva, total_ttc, fk_user_author'; + $sql .= ', fk_statut, paye, f.note,'.$this->db->pdate('date_lim_reglement').'as de'; + $sql .= ', s.nom as socnom, s.idp as socidp'; + $sql .= ' FROM '.MAIN_DB_PREFIX.'facture_fourn as f,'.MAIN_DB_PREFIX.'societe as s'; + $sql .= ' WHERE f.rowid='.$rowid.' AND f.fk_soc = s.idp ;'; + $resql = $this->db->query($sql); + if ($resql) { - return 1; - } - else - { - dolibarr_print_error($this->db); - } - } - } - else - { - dolibarr_print_error($this->db); - } - } + $num=$this->db->num_rows($resql); + if ($num) + { + $obj = $this->db->fetch_object(); - /** - * \brief Tag la facture comme payée complètement - * \param user Objet utilisateur qui modifie + $this->id = $rowid; + $this->datep = $obj->df; + $this->date_echeance = $obj->de; + $this->ref = $obj->facnumber; + $this->libelle = $obj->libelle; + + $this->remise = $obj->remise; + $this->socidp = $obj->socidp; + + $this->total_ht = $obj->total_ht; + $this->total_tva = $obj->total_tva; + $this->total_ttc = $obj->total_ttc; + + $this->author = $obj->fk_user_author; + + $this->statut = $obj->fk_statut; + $this->paye = $obj->paye; + + $this->socnom = $obj->socnom; + $this->note = $obj->note; + $this->db->free($resql); + + /* + * Lignes + */ + $sql = 'SELECT rowid,description, pu_ht, qty, tva_taux, tva, total_ht, total_ttc'; + $sql .= ' FROM '.MAIN_DB_PREFIX.'facture_fourn_det'; + $sql .= ' WHERE fk_facture_fourn='.$this->id; + $resql_rows = $this->db->query($sql); + if ($resql_rows) + { + $num_rows = $this->db->num_rows($resql_rows); + $i = 0; + if ($num_rows) + { + while ($i < $num_rows) + { + $obj = $this->db->fetch_object(); + $this->lignes[$i][0] = stripslashes($obj->description); + $this->lignes[$i][1] = $obj->pu_ht; + $this->lignes[$i][2] = $obj->tva_taux; + $this->lignes[$i][3] = $obj->qty; + $this->lignes[$i][4] = $obj->total_ht; + $this->lignes[$i][5] = $obj->tva; + $this->lignes[$i][6] = $obj->total_ttc; + $this->lignes[$i][7] = $obj->rowid; + $i++; + } + } + $this->db->free($resql_rows); + } + else + { + dolibarr_print_error($this->db); + } + } + } + else + { + dolibarr_print_error($this->db); + } + } + + /** + * \brief Recupére l'objet fournisseur lié à la facture + * + */ + function fetch_fournisseur() + { + $fournisseur = new Fournisseur($this->db); + $fournisseur->fetch($this->socidp); + $this->fournisseur = $fournisseur; + } + + /** + * \brief Supprime la facture + * \param rowid id de la facture à supprimer + */ + function delete($rowid) + { + $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'facture_fourn WHERE rowid = '.$rowid.' AND fk_statut = 0;'; + $resql = $this->db->query($sql); + if ($resql) + { + $num = $this->db->affected_rows($resql); + if ($num) + { + $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'facture_fourn_det WHERE fk_facture_fourn = '.$rowid.';'; + $resql2 = $this->db->query($sql); + if ($resql2) + { + return 1; + } + else + { + dolibarr_print_error($this->db); + } + } + } + else + { + dolibarr_print_error($this->db); + } + } + + /** + * \brief Tag la facture comme payée complètement + * \param userid utilisateur qui modifie l'état * \return int <0 si ko, >0 si ok - */ + */ function set_payed($user) - { - $sql = "UPDATE ".MAIN_DB_PREFIX."facture_fourn"; - $sql.= " SET paye = 1"; - $sql.= " WHERE rowid = ".$this->id; - $result = $this->db->query($sql); - if (! $result) - { - dolibarr_print_error($this->db); + { + $sql = 'UPDATE '.MAIN_DB_PREFIX.'facture_fourn set paye = 1 WHERE rowid = '.$this->id.';'; + $resql = $this->db->query($sql); + if (! $resql) + { + dolibarr_print_error($this->db); return -1; - } + } return 1; - } - - /** - * \brief Tag la facture comme validée et valide la facture - * \param user Objet utilisateur qui modifie - * \return int <0 si ko, >0 si ok - */ - function set_valid($user) - { + } + + /** + * \brief Tag la facture comme validée et valide la facture + * \param userid utilisateur qui valide la facture + */ + function set_valid($userid) + { $sql = "UPDATE ".MAIN_DB_PREFIX."facture_fourn"; $sql.= " SET fk_statut = 1, fk_user_valid = ".$user->id; $sql.= " WHERE rowid = ".$this->id; - $result = $this->db->query( $sql); - if (! $result) { - dolibarr_print_error($this->db); - } - } + $resql = $this->db->query($sql); + if (! $resql) + { + dolibarr_print_error($this->db); + } + } - /** - * \brief Ajoute une ligne de facture (associé à aucun produit/service prédéfini) - * \param desc description de la ligne - * \param pu prix unitaire - * \param tauxtva taux de tva - * \param qty quantité - */ - function addline($desc, $pu, $tauxtva, $qty) - { - - $sql = "INSERT INTO ".MAIN_DB_PREFIX."facture_fourn_det (fk_facture_fourn)"; - $sql .= " VALUES ($this->id);"; - if ($this->db->query($sql) ) - { - $idligne = $this->db->last_insert_id(MAIN_DB_PREFIX."facture_fourn_det"); - - $this->updateline($idligne, $desc, $pu, $tauxtva, $qty); - } - else - { - dolibarr_print_error($this->db); - } - - // Mise a jour prix facture - $this->updateprice($this->id); - - } + /** + * \brief Ajoute une ligne de facture (associé à aucun produit/service prédéfini) + * \param desc description de la ligne + * \param pu prix unitaire + * \param tauxtva taux de tva + * \param qty quantité + */ + function addline($desc, $pu, $tauxtva, $qty) + { + $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'facture_fourn_det (fk_facture_fourn)'; + $sql .= ' VALUES ('.$this->id.');'; + $resql = $this->db->query($sql); + if ($resql) + { + $idligne = $this->db->last_insert_id(MAIN_DB_PREFIX.'facture_fourn_det'); + $this->updateline($idligne, $desc, $pu, $tauxtva, $qty); + } + else + { + dolibarr_print_error($this->db); + } + // Mise a jour prix facture + $this->updateprice($this->id); + } - /** - * \brief Mets à jour une ligne de facture - * \param id id de la ligne de facture - * \param label description de la ligne - * \param puht prix unitaire - * \param tauxtva taux tva - * \param qty quantité - * \return int <0 si ko, >0 si ok - */ - function updateline($id, $label, $puht, $tauxtva, $qty=1) - { - $puht = ereg_replace(",",".",$puht); - $qty = ereg_replace(",",".",$qty); - - if (is_numeric($puht) && is_numeric($qty)) - { - $totalht = ($puht * $qty); - $tva = ($totalht * $tauxtva / 100); - $totalttc = $totalht + $tva; - - $sql = "UPDATE ".MAIN_DB_PREFIX."facture_fourn_det "; - $sql .= "SET description ='".addslashes($label)."'"; - $sql .= ", pu_ht = " .ereg_replace(",",".",$puht); - $sql .= ", qty =" .ereg_replace(",",".",$qty); - $sql .= ", total_ht=" .ereg_replace(",",".",$totalht); - $sql .= ", tva=" .ereg_replace(",",".",$tva); - $sql .= ", tva_taux=" .ereg_replace(",",".",$tauxtva); - $sql .= ", total_ttc=".ereg_replace(",",".",$totalttc); - $sql .= " WHERE rowid = ".$id; - - $resql=$this->db->query($sql); - if ($resql) - { - // Mise a jour prix facture - return $this->updateprice($this->id); - } - else - { - $this->error=$this->db->error(); - return -1; - } - } - } + /** + * \brief Mets à jour une ligne de facture + * \param id id de la ligne de facture + * \param label description de la ligne + * \param puht prix unitaire + * \param tauxtva taux tva + * \param qty quantité + * \return int <0 si ko, >0 si ok + */ + function updateline($id, $label, $puht, $tauxtva, $qty=1) + { + $puht = price2num($puht); + $qty = price2num($qty); - /** - * \brief Supprime une ligne facture de la base - * \param rowid id de la ligne de facture a supprimer - */ - function deleteline($rowid) - { - // Supprime ligne - $sql = "DELETE FROM ".MAIN_DB_PREFIX."facture_fourn_det "; - $sql .= " WHERE rowid = $rowid"; - - if (! $this->db->query($sql) ) - { - dolibarr_print_error($this->db); - } - - // Mise a jour prix facture - $this->updateprice($this->id); - - return 1; - } + if (is_numeric($puht) && is_numeric($qty)) + { + $totalht = ($puht * $qty); + $tva = ($totalht * $tauxtva / 100); + $totalttc = $totalht + $tva; - /** - * \brief Mise à jour des sommes de la facture - * \param facid id de la facture a modifier - * \return int <0 si ko, >0 si ok - */ - function updateprice($facid) - { - $total_ht = 0; - $total_tva = 0; - $total_ttc = 0; - - $sql = "SELECT sum(total_ht), sum(tva), sum(total_ttc) FROM ".MAIN_DB_PREFIX."facture_fourn_det"; - $sql .= " WHERE fk_facture_fourn = $facid;"; - - $result = $this->db->query($sql); - - if ($result) - { - if ($this->db->num_rows() ) - { - $row = $this->db->fetch_row(); - $total_ht = $row[0]; - $total_tva = $row[1]; - $total_ttc = $row[2]; - - } - $total_ht = $total_ht != '' ? $total_ht : 0; - $total_tva = $total_tva != '' ? $total_tva : 0; - $total_ttc = $total_ttc != '' ? $total_ttc : 0; - - $sql = "UPDATE ".MAIN_DB_PREFIX."facture_fourn SET"; - $sql .= " total_ht = ". ereg_replace(",",".",$total_ht); - $sql .= ",total_tva = ".ereg_replace(",",".",$total_tva); - $sql .= ",total_ttc = ".ereg_replace(",",".",$total_ttc); - $sql .= " WHERE rowid = $facid ;"; - - $result = $this->db->query($sql); - if ($result) - { - return 1; - } - else - { - $this->error=$this->db->error(); - return -2; - } - } - else - { - dolibarr_print_error($this->db); - return -1; - } - } - - - /** - * \brief Retourne le libellé du statut d'une facture (brouillon, validée, abandonnée, payée) - * \return string Libellé - */ - function getLibStatut() - { + $sql = 'UPDATE '.MAIN_DB_PREFIX.'facture_fourn_det '; + $sql .= 'SET '; + $sql .= 'description =\''.addslashes($label).'\''; + $sql .= ', pu_ht = ' .$puht; + $sql .= ', qty =' .$qty; + $sql .= ', total_ht=' .price2num($totalht); + $sql .= ', tva=' .price2num($tva); + $sql .= ', tva_taux=' .price2num($tauxtva); + $sql .= ', total_ttc='.price2num($totalttc); + $sql .= ' WHERE rowid = '.$id.';'; + + $resql=$this->db->query($sql); + if ($resql) + { + // Mise a jour prix facture + return $this->updateprice($this->id); + } + else + { + $this->error=$this->db->error(); + return -1; + } + } + } + + /** + * \brief Supprime une ligne facture de la base + * \param rowid id de la ligne de facture a supprimer + */ + function deleteline($rowid) + { + // Supprime ligne + $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'facture_fourn_det '; + $sql .= ' WHERE rowid = '.$rowid.';'; + $resql = $this->db->query($sql); + if (! $resql) + { + dolibarr_print_error($this->db); + } + // Mise a jour prix facture + $this->updateprice($this->id); + return 1; + } + + /** + * \brief Mise à jour des sommes de la facture + * \param facid id de la facture a modifier + * \return int <0 si ko, >0 si ok + */ + function updateprice($facid) + { + $total_ht = 0; + $total_tva = 0; + $total_ttc = 0; + + $sql = 'SELECT sum(total_ht), sum(tva), sum(total_ttc) FROM '.MAIN_DB_PREFIX.'facture_fourn_det'; + $sql .= ' WHERE fk_facture_fourn = '.$facid.';'; + $resql = $this->db->query($sql); + if ($resql) + { + $num = $this->db->num_rows($resql); + if ($num) + { + $row = $this->db->fetch_row(); + $total_ht = $row[0]; + $total_tva = $row[1]; + $total_ttc = $row[2]; + } + $this->db->free($resql); + + $total_ht = $total_ht != '' ? $total_ht : 0; + $total_tva = $total_tva != '' ? $total_tva : 0; + $total_ttc = $total_ttc != '' ? $total_ttc : 0; + + $sql = 'UPDATE '.MAIN_DB_PREFIX.'facture_fourn SET'; + $sql .= ' total_ht = '. price2num($total_ht); + $sql .= ',total_tva = '.price2num($total_tva); + $sql .= ',total_ttc = '.price2num($total_ttc); + $sql .= ' WHERE rowid = '.$facid.';'; + $resql2 = $this->db->query($sql); + if ($resql2) + { + return 1; + } + else + { + $this->error=$this->db->error(); + return -2; + } + } + else + { + dolibarr_print_error($this->db); + return -1; + } + } + + + /** + * \brief Retourne le libellé du statut d'une facture (brouillon, validée, abandonnée, payée) + * \return string Libellé + */ + function getLibStatut() + { return $this->LibStatut($this->paye,$this->statut); - } + } - /** - * \brief Renvoi le libellé court d'un statut donné - * \param paye etat paye - * \param statut id statut - * \return string Libellé long du statut - */ - function LibStatutShort($paye,$statut) - { - global $langs; - $langs->load("bills"); - if (! $paye) - { - if ($statut == 0) return $langs->trans("BillShortStatusDraft"); - if ($statut == 3) return $langs->trans("BillShortStatusCanceled"); - return $langs->trans("BillShortValidated"); - } - else - { - return $langs->trans("BillShortStatusPayed"); - } - } + /** + * \brief Renvoi le libellé court d'un statut donné + * \param paye etat paye + * \param statut id statut + * \return string Libellé long du statut + */ + function LibStatutShort($paye,$statut) + { + global $langs; + $langs->load('bills'); + if (! $paye) + { + if ($statut == 0) return $langs->trans('BillShortStatusDraft'); + if ($statut == 3) return $langs->trans('BillShortStatusCanceled'); + return $langs->trans('BillShortValidated'); + } + else + { + return $langs->trans('BillShortStatusPayed'); + } + } - /** - * \brief Renvoi le libellé long d'un statut donné - * \param paye etat paye - * \param statut id statut - * \return string Libellé long du statut - */ - function LibStatut($paye,$statut) - { - global $langs; - $langs->load("bills"); - if (! $paye) - { - if ($statut == 0) return $langs->trans("BillStatusDraft"); - if ($statut == 3) return $langs->trans("BillStatusCanceled"); - return $langs->trans("BillStatusValidated"); - } - else - { - return $langs->trans("BillStatusPayed"); - } - } - - /** - * \brief Renvoi le libellé court d'un statut donné - * \param paye etat paye - * \param statut id statut - * \param amount amount already payed - * \return string Libellé court du statut - */ - function PayedLibStatut($paye,$statut,$amount=0) - { - global $langs; - $langs->load("bills"); - if (! $paye) - { - if ($statut == 0) return $langs->trans("BillShortStatusDraft"); - if ($statut == 3) return $langs->trans("BillStatusCanceled"); - if ($amount) return $langs->trans("BillStatusStarted"); - return $langs->trans("BillStatusNotPayed"); - } - else - { - return $langs->trans("BillStatusPayed"); - } - } + /** + * \brief Renvoi le libellé long d'un statut donné + * \param paye etat paye + * \param statut id statut + * \return string Libellé long du statut + */ + function LibStatut($paye,$statut) + { + global $langs; + $langs->load('bills'); + if (! $paye) + { + if ($statut == 0) return $langs->trans('BillStatusDraft'); + if ($statut == 3) return $langs->trans('BillStatusCanceled'); + return $langs->trans('BillStatusValidated'); + } + else + { + return $langs->trans('BillStatusPayed'); + } + } + + /** + * \brief Renvoi le libellé court d'un statut donné + * \param paye etat paye + * \param statut id statut + * \param amount amount already payed + * \return string Libellé court du statut + */ + function PayedLibStatut($paye,$statut,$amount=0) + { + global $langs; + $langs->load('bills'); + if (! $paye) + { + if ($statut == 0) return $langs->trans('BillShortStatusDraft'); + if ($statut == 3) return $langs->trans('BillStatusCanceled'); + if ($amount) return $langs->trans('BillStatusStarted'); + return $langs->trans('BillStatusNotPayed'); + } + else + { + return $langs->trans('BillStatusPayed'); + } + } - /** - * \brief Charge indicateurs this->nbtodo et this->nbtodolate de tableau de bord - * \param user Objet user - * \return int <0 si ko, >0 si ok - */ - function load_board($user) - { - global $conf; - - $this->nbtodo=$this->nbtodolate=0; - $sql = "SELECT ff.rowid,".$this->db->pdate("ff.date_lim_reglement")." as datefin"; - $sql.= " FROM ".MAIN_DB_PREFIX."facture_fourn as ff"; - $sql.= " WHERE ff.paye=0"; - if ($user->societe_id) $sql.=" AND fk_soc = ".$user->societe_id; - $resql=$this->db->query($sql); - if ($resql) - { - while ($obj=$this->db->fetch_object($resql)) - { - $this->nbtodo++; - if ($obj->datefin < (time() - $conf->facture->fournisseur->warning_delay)) $this->nbtodolate++; - } - return 1; - } - else - { - dolibarr_print_error($this->db); - $this->error=$this->db->error(); - return -1; - } - } + /** + * \brief Charge indicateurs this->nbtodo et this->nbtodolate de tableau de bord + * \param user Objet user + * \return int <0 si ko, >0 si ok + */ + function load_board($user) + { + global $conf; + + $this->nbtodo=$this->nbtodolate=0; + $sql = 'SELECT ff.rowid,'.$this->db->pdate('ff.date_lim_reglement').' as datefin'; + $sql.= ' FROM '.MAIN_DB_PREFIX.'facture_fourn as ff'; + $sql.= ' WHERE ff.paye=0;'; + if ($user->societe_id) $sql.=' AND fk_soc = '.$user->societe_id; + $resql=$this->db->query($sql); + if ($resql) + { + while ($obj=$this->db->fetch_object($resql)) + { + $this->nbtodo++; + if ($obj->datefin < (time() - $conf->facture->fournisseur->warning_delay)) $this->nbtodolate++; + } + $this->db->free($resql); + return 1; + } + else + { + dolibarr_print_error($this->db); + $this->error=$this->db->error(); + return -1; + } + } } ?> diff --git a/htdocs/fourn/paiement/fiche.php b/htdocs/fourn/paiement/fiche.php index bf5f975c11c..200006c45bb 100644 --- a/htdocs/fourn/paiement/fiche.php +++ b/htdocs/fourn/paiement/fiche.php @@ -1,5 +1,6 @@ +/* Copyright (C) 2005 Rodolphe Quiedeville + * 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 @@ -20,121 +21,226 @@ * */ -/** \file htdocs/compta/paiement/fiche.php +/** + \file htdocs/fourn/paiement/fiche.php \ingroup facture \brief Onglet paiement d'un paiement \version $Revision$ */ -require("./pre.inc.php"); +require('./pre.inc.php'); -require(DOL_DOCUMENT_ROOT."/fourn/facture/paiementfourn.class.php"); +require(DOL_DOCUMENT_ROOT.'/fourn/facture/paiementfourn.class.php'); $user->getrights('facture'); -$langs->load("bills"); -$langs->load("banks"); -$langs->load("companies"); +$langs->load('bills'); +$langs->load('banks'); +$langs->load('companies'); + +$mesg=''; + + +/* + * Actions + */ +if ($_POST['action'] == 'confirm_delete' && $_POST['confirm'] == 'yes' && $user->rights->facture->creer) +{ + $db->begin(); + + $paiement = new PaiementFourn($db); + $paiement->fetch($_GET['id']); + $result = $paiement->delete(); + if ($result > 0) + { + $db->commit(); + Header('Location: /fourn/facture/paiement.php'); + exit; + } + else + { + $mesg='
'.$paiement->error.'
'; + $db->rollback(); + } +} + +if ($_POST['action'] == 'confirm_valide' && $_POST['confirm'] == 'yes' && $user->rights->facture->creer) +{ + $db->begin(); + + $paiement = new PaiementFourn($db); + $paiement->id = $_GET['id']; + if ( $paiement->valide() == 0 ) + { + $db->commit(); + Header('Location: fiche.php?id='.$paiement->id); + exit; + } + else + { + $mesg='
'.$paiement->error.'
'; + $db->rollback(); + } +} /* * Visualisation de la fiche - * */ llxHeader(); + +$paiement = new PaiementFourn($db); +$paiement->fetch($_GET['id']); + +$html = new Form($db); + $h=0; -$head[$h][0] = DOL_URL_ROOT.'/fourn/paiement/fiche.php?id='.$_GET["id"]; -$head[$h][1] = $langs->trans("Card"); +$head[$h][0] = DOL_URL_ROOT.'/fourn/paiement/fiche.php?id='.$_GET['id']; +$head[$h][1] = $langs->trans('Card'); $hselected = $h; -$h++; +$h++; -$head[$h][0] = DOL_URL_ROOT.'/fourn/paiement/info.php?id='.$_GET["id"]; -$head[$h][1] = $langs->trans("Info"); -$h++; - -dolibarr_fiche_head($head, $hselected, $langs->trans("Payment").": ".$_GET["id"]); +$head[$h][0] = DOL_URL_ROOT.'/fourn/paiement/info.php?id='.$_GET['id']; +$head[$h][1] = $langs->trans('Info'); +$h++; -$facture = new FactureFournisseur($db); -$paiement = new PaiementFourn($db); +dolibarr_fiche_head($head, $hselected, $langs->trans('Payment').': '.$paiement->ref); -if ($paiement->fetch($_GET["id"], $user) == 0) +/* + * Confirmation de la suppression du paiement + */ +if ($_GET['action'] == 'delete') { - $html = new Form($db); + $html->form_confirm('fiche.php?id='.$paiement->id, 'Supprimer le paiement', 'Etes-vous sûr de vouloir supprimer ce paiement ?', 'confirm_delete'); + print '
'; +} - $facture->fetch($paiement->facture_id); - - /* - * Confirmation de la suppression du paiement - * - */ - if ($_GET["action"] == 'delete') - { - $html->form_confirm("fiche.php?id=$paiement->id","Supprimer le paiement","Etes-vous sûr de vouloir supprimer ce paiement ?","confirm_delete"); - print '
'; - - } - - if ($_GET["action"] == 'valide') - { - $html->form_confirm("fiche.php?id=$paiement->id","Valider le paiement","Etes-vous sûr de vouloir valider ce paiment, auncune modification n'est possible une fois le paiement validé ?","confirm_valide"); - print '
'; - - } - - print ''; - - print ''; - print "
'; - - if ($paiement->bank_account) { - // Si compte renseigné, on affiche libelle - $bank=new Account($db); - $bank->fetch($paiement->bank_account); - print $langs->trans("BankAccount").' : '.$bank->label.'
'; - } - print $langs->trans("Date").' : '.dolibarr_print_date($paiement->date)."
"; - print $langs->trans("Bill").' : '.$facture->ref."
"; - print $langs->trans("Type").' : '.$paiement->type_libelle."
"; - if ($paiement->numero) { print $langs->trans("Numero").' : '.$paiement->numero."
"; } - print $langs->trans("Amount").' : '.$paiement->montant." ".$conf->monnaie."
"; - print '
"; - - print nl2br($paiement->note); - - - /* - * - * - -print "
"; - -print '
'; - -if ($user->societe_id == 0 && $paiement->statut == 0 && $_GET["action"] == '') +/* + * Confirmation de la validation du paiement + */ +if ($_GET['action'] == 'valide') { - print ''.$langs->trans("Valid").''; + $html->form_confirm('fiche.php?id='.$paiement->id, 'Valider le paiement', 'Etes-vous sûr de vouloir valider ce paiment, auncune modification n\'est possible une fois le paiement validé ?', 'confirm_valide'); + print '
'; } -if ($user->societe_id == 0 && $allow_delete && $paiement->statut == 0 && $_GET["action"] == '') +if ($mesg) print $mesg.'
'; + + +print ''; + +print ''; +if ($conf->banque->enabled) { - print ''.$langs->trans("Delete").''; - + if ($paiement->bank_account) + { + // Si compte renseigné, on affiche libelle + $bank=new Account($db); + $bank->fetch($paiement->bank_account); + + $bankline=new AccountLine($db); + $bankline->fetch($paiement->bank_line); + + print ''; + print ''; + print ''; + print ''; + print ''; + } } -print ""; +print ''; +print ''; +if ($paiement->numero) +{ + print ''; +} +print ''; +print ''; +print '
'.$langs->trans('Ref').''.$paiement->id.'
'.$langs->trans('BankAccount').''.img_object($langs->trans('ShowAccount'),'account').' '.$bank->label.''.$langs->trans('BankLineConciliated').''.yn($bankline->rappro).'
'.$langs->trans('Date').''.dolibarr_print_date($paiement->date).'
'.$langs->trans('Type').''.$paiement->type_libelle.'
'.$langs->trans('Numero').''.$paiement->numero.'
'.$langs->trans('Amount').''.price($paiement->montant).' '.$langs->trans('Currency'.$conf->monnaie).'
'.$langs->trans('Note').''.nl2br($paiement->note).'
'; -*/ +/* + * + * + */ +$allow_delete = 1 ; +$sql = 'SELECT f.facnumber, f.total_ttc, pf.amount, f.rowid as facid, f.paye, f.fk_statut, s.nom, s.idp'; +$sql .= ' FROM '.MAIN_DB_PREFIX.'paiementfourn_facturefourn as pf,'.MAIN_DB_PREFIX.'facture_fourn as f,'.MAIN_DB_PREFIX.'societe as s'; +$sql .= ' WHERE pf.fk_facturefourn = f.rowid AND f.fk_soc = s.idp'; +$sql .= ' AND pf.fk_paiementfourn = '.$paiement->id; +$resql=$db->query($sql); +if ($resql) +{ + $num = $db->num_rows($resql); + + $i = 0; + $total = 0; + print '
'; + print ''; + print ''; + print ''; + print "\n"; + + if ($num > 0) + { + $var=True; + + while ($i < $num) + { + $objp = $db->fetch_object($resql); + $var=!$var; + print ''; + print '\n"; + print ''; + print ''; + $fac=new FactureFournisseur($db); + print ''; + print "\n"; + if ($objp->paye == 1) + { + $allow_delete = 0; + } + $total = $total + $objp->amount; + $i++; + } + } + $var=!$var; + + print "
'.$langs->trans('Bill').''.$langs->trans('Company').''.$langs->trans('AmountTTC').''.$langs->trans('Status').'
'.img_object($langs->trans('ShowBill'),'bill').' '; + print $objp->facnumber; + print "'.img_object($langs->trans('ShowCompany'),'company').' '.$objp->nom.''.price($objp->amount).''.$fac->LibStatut($objp->paye,$objp->fk_statut).'
\n"; + $db->free($resql); } else { - print "Erreur de lecture"; + dolibarr_print_error($db); } +print '
'; + + +/* + * Boutons Actions + */ + +print '
'; + +if ($user->societe_id == 0 && $paiement->statut == 0 && $_GET['action'] == '') +{ + print ''.$langs->trans('Valid').''; +} + +if ($user->societe_id == 0 && $allow_delete && $paiement->statut == 0 && $_GET['action'] == '') +{ + print ''.$langs->trans('Delete').''; +} +print '
'; + $db->close(); llxFooter("Dernière modification $Date$ révision $Revision$"); diff --git a/mysql/migration/1.1.0-2.0.0.sql b/mysql/migration/1.1.0-2.0.0.sql index a30e17c3c8a..b2628d2dc81 100644 --- a/mysql/migration/1.1.0-2.0.0.sql +++ b/mysql/migration/1.1.0-2.0.0.sql @@ -1948,3 +1948,16 @@ alter table llx_facture add model varchar(50) after note; alter table llx_facturedet modify fk_product integer NULL; alter table llx_contratdet modify fk_product integer NULL; update llx_facturedet set fk_product = null where fk_product=0; + +CREATE TABLE IF NOT EXISTS `llx_paiementfourn_facturefourn` +( + `rowid` int(11) NOT NULL auto_increment, + `fk_paiementfourn` int(11) default NULL, + `fk_facturefourn` int(11) default NULL, + `amount` double default '0', + PRIMARY KEY (`rowid`), + KEY `idx_fk_facture` (`fk_facturefourn`), + KEY `idx_fk_paiement` (`fk_paiementfourn`) +) TYPE=innodb; + +ALTER TABLE `llx_paiementfourn` ADD `statut` SMALLINT( 6 ) NOT NULL ; diff --git a/mysql/tables/llx_paiementfourn_facturefourn.sql b/mysql/tables/llx_paiementfourn_facturefourn.sql new file mode 100644 index 00000000000..e03f313fa0d --- /dev/null +++ b/mysql/tables/llx_paiementfourn_facturefourn.sql @@ -0,0 +1,32 @@ +-- ============================================================================ +-- Copyright (C) 2003 Rodolphe Quiedeville +-- Copyright (C) 2005 Marc Barilley / Océbo +-- +-- 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$ +-- +-- =========================================================================== + +CREATE TABLE IF NOT EXISTS `llx_paiementfourn_facturefourn` ( + `rowid` int(11) NOT NULL auto_increment, + `fk_paiementfourn` int(11) default NULL, + `fk_facturefourn` int(11) default NULL, + `amount` double default '0', + PRIMARY KEY (`rowid`), + KEY `idx_fk_facture` (`fk_facturefourn`), + KEY `idx_fk_paiement` (`fk_paiementfourn`) +) TYPE=innodb;