* Copyright (C) 2004 Eric Seigne * Copyright (C) 2004-2012 Laurent Destailleur * Copyright (C) 2004 Christophe Combelles * Copyright (C) 2005 Marc Barilley / Ocebo * Copyright (C) 2005-2012 Regis Houssin * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 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, see . */ /** * \file htdocs/fourn/facture/paiement.php * \ingroup fournisseur,facture * \brief Payment page for suppliers invoices */ require '../../main.inc.php'; require DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.class.php'; require DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php'; require DOL_DOCUMENT_ROOT.'/fourn/class/paiementfourn.class.php'; $langs->load('companies'); $langs->load('bills'); $langs->load('banks'); $facid=GETPOST('facid','int'); $action=GETPOST('action','alpha'); $socid=GETPOST('socid','int'); $sortfield = GETPOST("sortfield",'alpha'); $sortorder = GETPOST("sortorder",'alpha'); $page = GETPOST("page",'int'); if ($page == -1) { $page = 0; } $offset = $conf->liste_limit * $page; $pageprev = $page - 1; $pagenext = $page + 1; $limit = $conf->liste_limit; if (! $sortorder) $sortorder="DESC"; if (! $sortfield) $sortfield="p.rowid"; $amounts = array(); // Security check if ($user->societe_id > 0) { $socid = $user->societe_id; } // Initialize technical object to manage hooks of thirdparties. Note that conf->hooks_modules contains array array $hookmanager->initHooks(array('paymentsupplier')); /* * Actions */ if ($action == 'add_paiement') { $error = 0; $datepaye = dol_mktime(12, 0, 0, $_POST['remonth'], $_POST['reday'], $_POST['reyear']); $paiement_id = 0; $total = 0; // Genere tableau des montants amounts foreach ($_POST as $key => $value) { if (substr($key,0,7) == 'amount_') { $other_facid = substr($key,7); $amounts[$other_facid] = price2num(GETPOST($key)); $total = $total + $amounts[$other_facid]; } } // Effectue les verifications des parametres if ($_POST['paiementid'] <= 0) { setEventMessage($langs->trans('ErrorFieldRequired',$langs->transnoentities('PaymentMode')), 'errors'); $error++; } if (! empty($conf->banque->enabled)) { // Si module bank actif, un compte est obligatoire lors de la saisie // d'un paiement if ($_POST['accountid'] <= 0) { setEventMessage($langs->trans('ErrorFieldRequired',$langs->transnoentities('AccountToCredit')), 'errors'); $error++; } } if ($total == 0) { setEventMessage($langs->trans('ErrorFieldRequired',$langs->trans('PaymentAmount')), 'errors'); $error++; } if (empty($datepaye)) { setEventMessage($langs->trans('ErrorFieldRequired',$langs->transnoentities('Date')), 'errors'); $error++; } if (! $error) { $db->begin(); // Creation de la ligne paiement $paiement = new PaiementFourn($db); $paiement->datepaye = $datepaye; $paiement->amounts = $amounts; // Array of amounts $paiement->paiementid = $_POST['paiementid']; $paiement->num_paiement = $_POST['num_paiement']; $paiement->note = $_POST['comment']; if (! $error) { $paiement_id = $paiement->create($user,(GETPOST('closepaidinvoices')=='on'?1:0)); if ($paiement_id < 0) { setEventMessage($paiement->error, 'errors'); $error++; } } if (! $error) { $result=$paiement->addPaymentToBank($user,'payment_supplier','(SupplierInvoicePayment)',$_POST['accountid'],'',''); if ($result < 0) { setEventMessage($paiement->error, 'errors'); $error++; } } if (! $error) { $db->commit(); // If payment dispatching on more than one invoice, we keep on summary page, otherwise go on invoice card $invoiceid=0; foreach ($paiement->amounts as $key => $amount) { $facid = $key; if (is_numeric($amount) && $amount <> 0) { if ($invoiceid != 0) $invoiceid=-1; // There is more than one invoice payed by this payment else $invoiceid=$facid; } } if ($invoiceid > 0) $loc = DOL_URL_ROOT.'/fourn/facture/fiche.php?facid='.$invoiceid; else $loc = DOL_URL_ROOT.'/fourn/paiement/fiche.php?id='.$paiement_id; header('Location: '.$loc); exit; } else { $db->rollback(); } } } /* * View */ $supplierstatic=new Societe($db); $invoicesupplierstatic = new FactureFournisseur($db); llxHeader(); $form=new Form($db); if ($action == 'create' || $action == 'add_paiement') { $object = new FactureFournisseur($db); $object->fetch($facid); $datefacture=dol_mktime(12, 0, 0, GETPOST('remonth'), GETPOST('reday'), GETPOST('reyear')); $dateinvoice=($datefacture==''?(empty($conf->global->MAIN_AUTOFILL_DATE)?-1:0):$datefacture); $sql = 'SELECT s.nom, s.rowid as socid,'; $sql.= ' f.rowid, f.ref, f.ref_supplier, f.amount, f.total_ttc as total'; if (!$user->rights->societe->client->voir && !$socid) $sql .= ", sc.fk_soc, sc.fk_user "; $sql.= ' FROM '.MAIN_DB_PREFIX.'societe as s, '.MAIN_DB_PREFIX.'facture_fourn as f'; if (!$user->rights->societe->client->voir && !$socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; $sql.= ' WHERE f.fk_soc = s.rowid'; $sql.= ' AND f.rowid = '.$facid; if (!$user->rights->societe->client->voir && !$socid) $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = " .$user->id; $resql = $db->query($sql); if ($resql) { $num = $db->num_rows($resql); if ($num) { $obj = $db->fetch_object($resql); $total = $obj->total; print_fiche_titre($langs->trans('DoPayment')); print '
'; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; if (! empty($conf->banque->enabled)) { print ''; } else { print ''; } print '
'.$langs->trans('Payment').'
'.$langs->trans('Company').''; $supplierstatic->id=$obj->socid; $supplierstatic->name=$obj->nom; print $supplierstatic->getNomUrl(1,'supplier'); print '
'.$langs->trans('Date').''; $form->select_date($dateinvoice,'','','','',"addpaiement",1,1); print ''.$langs->trans('Comments').'
'.$langs->trans('PaymentMode').''; $form->select_types_paiements(empty($_POST['paiementid'])?'':$_POST['paiementid'],'paiementid'); print ''; print '
'.$langs->trans('Numero').'
'.$langs->trans('Account').''; $form->select_comptes(empty($_POST['accountid'])?'':$_POST['accountid'],'accountid',0,'',2); print '
 
'; $parameters=array('facid'=>$facid, 'ref'=>$ref, 'objcanvas'=>$objcanvas); $reshook=$hookmanager->executeHooks('paymentsupplierinvoices',$parameters,$object,$action); // Note that $action and $object may have been modified by some hooks $error=$hookmanager->error; $errors=$hookmanager->errors; if (empty($reshook)) { /* * Autres factures impayees */ $sql = 'SELECT f.rowid as facid, f.ref, f.ref_supplier, f.total_ht, f.total_ttc, 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.entity = ".$conf->entity; $sql.= ' AND f.fk_soc = '.$object->socid; $sql.= ' AND f.paye = 0'; $sql.= ' AND f.fk_statut = 1'; // Statut=0 => non validee, Statut=2 => annulee $sql.= ' GROUP BY f.rowid, f.ref, f.ref_supplier, f.total_ht, f.total_ttc, f.datef'; $resql = $db->query($sql); if ($resql) { $num = $db->num_rows($resql); if ($num > 0) { $i = 0; print '
'; print $langs->trans('Invoices').'
'; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; $var=True; $total=0; $total_ttc=0; $totalrecu=0; while ($i < $num) { $objp = $db->fetch_object($resql); $var=!$var; print ''; print ''; print ''; if ($objp->df > 0 ) { print ''; } else { print ''; } print ''; print ''; print ''; print '\n"; $total+=$objp->total_ht; $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('Ref').''.$langs->trans('RefSupplier').''.$langs->trans('Date').''.$langs->trans('AmountTTC').''.$langs->trans('AlreadyPaid').''.$langs->trans('RemainderToPay').''.$langs->trans('Amount').'
'; $invoicesupplierstatic->ref=$objp->ref; $invoicesupplierstatic->id=$objp->facid; print $invoicesupplierstatic->getNomUrl(1); print ''.$objp->ref_supplier.''; print dol_print_date($db->jdate($objp->df), 'day').'!!!'.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).' 
\n"; } $db->free($resql); } else { dol_print_error($db); } } // print ''; print '

'.$langs->trans("ClosePaidInvoicesAutomatically"); print '
'; // print ''; print '
'; } } } /* * Show list */ if (empty($action)) { if ($page == -1) $page = 0 ; $limit = $conf->liste_limit; $offset = $limit * $page ; if (! $sortorder) $sortorder='DESC'; if (! $sortfield) $sortfield='p.datep'; $search_ref=GETPOST('search_ref'); $search_account=GETPOST('search_account'); $search_paymenttype=GETPOST('search_paymenttype'); $search_amount=GETPOST('search_amount'); $search_company=GETPOST('search_company'); $sql = 'SELECT p.rowid as pid, p.datep as dp, p.amount as pamount, p.num_paiement,'; $sql.= ' s.rowid as socid, s.nom,'; $sql.= ' c.libelle as paiement_type,'; $sql.= ' ba.rowid as bid, ba.label,'; if (!$user->rights->societe->client->voir) $sql .= ' sc.fk_soc, sc.fk_user,'; $sql.= ' SUM(f.amount)'; $sql.= ' FROM '.MAIN_DB_PREFIX.'paiementfourn AS p'; if (!$user->rights->societe->client->voir) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; $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.rowid = 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'; $sql.= " WHERE f.entity = ".$conf->entity; if (!$user->rights->societe->client->voir) $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = " .$user->id; if ($socid) { $sql .= ' AND f.fk_soc = '.$socid; } // Search criteria if (! empty($search_ref)) { $sql .= ' AND p.rowid='.$db->escape($search_ref); } if (! empty($search_account)) { $sql .= ' AND b.fk_account='.$db->escape($search_account); } if (! empty($search_paymenttype)) { $sql .= " AND c.code='".$db->escape($search_paymenttype)."'"; } if (! empty($search_amount)) { $sql .= " AND p.amount=".price2num($search_amount); } if (! empty($search_company)) { $sql .= " AND s.nom LIKE '%".$db->escape($search_company)."%'"; } $sql.= " GROUP BY p.rowid, p.datep, p.amount, p.num_paiement, s.rowid, s.nom, c.libelle, ba.rowid, ba.label"; if (!$user->rights->societe->client->voir) $sql .= ", sc.fk_soc, sc.fk_user"; $sql.= $db->order($sortfield,$sortorder); $sql.= $db->plimit($limit+1, $offset); $resql = $db->query($sql); if ($resql) { $num = $db->num_rows($resql); $i = 0; $var=True; $paramlist=''; $paramlist.=(! empty($search_ref)?"&search_ref=".$search_ref:""); $paramlist.=(! empty($search_company)?"&search_company=".$search_company:""); $paramlist.=(! empty($search_amount)?"&search_amount=".$search_amount:""); print_barre_liste($langs->trans('SupplierPayments'), $page, 'paiement.php',$paramlist,$sortfield,$sortorder,'',$num); if ($mesg) dol_htmloutput_mesg($mesg); if ($errmsg) dol_htmloutput_errors($errmsg); print '
'; print ''; print ''; print_liste_field_titre($langs->trans('RefPayment'),'paiement.php','p.rowid','',$paramlist,'',$sortfield,$sortorder); print_liste_field_titre($langs->trans('Date'),'paiement.php','dp','',$paramlist,'align="center"',$sortfield,$sortorder); print_liste_field_titre($langs->trans('ThirdParty'),'paiement.php','s.nom','',$paramlist,'',$sortfield,$sortorder); print_liste_field_titre($langs->trans('Type'),'paiement.php','c.libelle','',$paramlist,'',$sortfield,$sortorder); print_liste_field_titre($langs->trans('Account'),'paiement.php','ba.label','',$paramlist,'',$sortfield,$sortorder); print_liste_field_titre($langs->trans('Amount'),'paiement.php','f.amount','',$paramlist,'align="right"',$sortfield,$sortorder); //print_liste_field_titre($langs->trans('Invoice'),'paiement.php','ref_supplier','',$paramlist,'',$sortfield,$sortorder); print "\n"; // Lines for filters fields print ''; print ''; print ''; print ''; print ''; print ''; print ''; print "\n"; while ($i < min($num,$limit)) { $objp = $db->fetch_object($resql); $var=!$var; print ''; // Ref payment print ''; // Date print '\n"; print ''; print '\n"; print ''; print ''; // Ref invoice /*$invoicesupplierstatic->ref=$objp->ref_supplier; $invoicesupplierstatic->id=$objp->facid; print '';*/ print ''; $i++; } print "
'; print ''; print ' '; print ''; print ''; $form->select_types_paiements($search_paymenttype,'search_paymenttype','',2,1,1); print ''; $form->select_comptes($search_account,'search_account',0,'',1); print ''; print ''; print ''; print '
'.img_object($langs->trans('ShowPayment'),'payment').' '.$objp->pid.''.dol_print_date($db->jdate($objp->dp),'day')."'; if ($objp->socid) print ''.img_object($langs->trans('ShowCompany'),'company').' '.dol_trunc($objp->nom,32).''; else print ' '; print ''.dol_trunc($objp->paiement_type.' '.$objp->num_paiement,32)."'; if ($objp->bid) print ''.img_object($langs->trans("ShowAccount"),'account').' '.dol_trunc($objp->label,24).''; else print ' '; print ''.price($objp->pamount).''; print $invoicesupplierstatic->getNomUrl(1); print '
"; print "
\n"; } else { dol_print_error($db); } } $db->close(); llxFooter(); ?>