PageRenderTime 98ms CodeModel.GetById 30ms app.highlight 41ms RepoModel.GetById 19ms app.codeStats 1ms

/htdocs/fourn/facture/paiement.php

https://bitbucket.org/speedealing/speedealing
PHP | 505 lines | 387 code | 60 blank | 58 comment | 72 complexity | d7109b03a455329009800fc49bc44647 MD5 | raw file
  1<?php
  2/* Copyright (C) 2003-2005	Rodolphe Quiedeville	<rodolphe@quiedeville.org>
  3 * Copyright (C) 2004		Eric Seigne				<eric.seigne@ryxeo.com>
  4 * Copyright (C) 2004-2012	Laurent Destailleur		<eldy@users.sourceforge.net>
  5 * Copyright (C) 2004		Christophe Combelles	<ccomb@free.fr>
  6 * Copyright (C) 2005		Marc Barilley / Ocebo	<marc@ocebo.com>
  7 * Copyright (C) 2005-2012	Regis Houssin			<regis.houssin@capnetworks.com>
  8 *
  9 * This program is free software; you can redistribute it and/or modify
 10 * it under the terms of the GNU General Public License as published by
 11 * the Free Software Foundation; either version 3 of the License, or
 12 * (at your option) any later version.
 13 *
 14 * This program is distributed in the hope that it will be useful,
 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 17 * GNU General Public License for more details.
 18 *
 19 * You should have received a copy of the GNU General Public License
 20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 21 */
 22
 23/**
 24 *	\file       htdocs/fourn/facture/paiement.php
 25 *	\ingroup    fournisseur,facture
 26 *	\brief      Payment page for suppliers invoices
 27 */
 28
 29require '../../main.inc.php';
 30require DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.class.php';
 31require DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
 32require DOL_DOCUMENT_ROOT.'/fourn/class/paiementfourn.class.php';
 33
 34$langs->load('companies');
 35$langs->load('bills');
 36$langs->load('banks');
 37
 38$facid=GETPOST('facid','int');
 39$action=GETPOST('action');
 40$socid=GETPOST('socid','int');
 41
 42$sortfield = GETPOST("sortfield",'alpha');
 43$sortorder = GETPOST("sortorder",'alpha');
 44$page = GETPOST("page",'int');
 45if ($page == -1) { $page = 0; }
 46$offset = $conf->liste_limit * $page;
 47$pageprev = $page - 1;
 48$pagenext = $page + 1;
 49$limit = $conf->liste_limit;
 50if (! $sortorder) $sortorder="DESC";
 51if (! $sortfield) $sortfield="p.rowid";
 52
 53$amounts = array();
 54
 55// Security check
 56if ($user->societe_id > 0)
 57{
 58    $socid = $user->societe_id;
 59}
 60
 61
 62
 63
 64/*
 65 * Actions
 66 */
 67if ($action == 'add_paiement')
 68{
 69    $error = 0;
 70
 71    $datepaye = dol_mktime(12, 0, 0, $_POST['remonth'], $_POST['reday'], $_POST['reyear']);
 72    $paiement_id = 0;
 73    $total = 0;
 74
 75    // Genere tableau des montants amounts
 76    foreach ($_POST as $key => $value)
 77    {
 78        if (substr($key,0,7) == 'amount_')
 79        {
 80            $other_facid = substr($key,7);
 81            $amounts[$other_facid] = price2num(GETPOST($key));
 82            $total = $total + $amounts[$other_facid];
 83        }
 84    }
 85
 86    // Effectue les verifications des parametres
 87    if ($_POST['paiementid'] <= 0)
 88    {
 89        $mesg = '<div class="error">'.$langs->trans('ErrorFieldRequired',$langs->transnoentities('PaymentMode')).'</div>';
 90        $error++;
 91    }
 92
 93    if (! empty($conf->banque->enabled))
 94    {
 95        // Si module bank actif, un compte est obligatoire lors de la saisie
 96        // d'un paiement
 97        if (! $_POST['accountid'])
 98        {
 99            $mesg = '<div class="error">'.$langs->trans('ErrorFieldRequired',$langs->transnoentities('AccountToCredit')).'</div>';
100            $error++;
101        }
102    }
103
104    if ($total == 0)
105    {
106        $mesg = '<div class="error">'.$langs->transnoentities('ErrorFieldRequired',$langs->trans('PaymentAmount')).'</div>';
107        $error++;
108    }
109
110    if (empty($datepaye))
111    {
112        $mesg = '<div class="error">'.$langs->trans('ErrorFieldRequired',$langs->transnoentities('Date')).'</div>';
113        $error++;
114    }
115
116    if (! $error)
117    {
118        $db->begin();
119
120        // Creation de la ligne paiement
121        $paiement = new PaiementFourn($db);
122        $paiement->datepaye     = $datepaye;
123        $paiement->amounts      = $amounts;   // Array of amounts
124        $paiement->paiementid   = $_POST['paiementid'];
125        $paiement->num_paiement = $_POST['num_paiement'];
126        $paiement->note         = $_POST['comment'];
127        if (! $error)
128        {
129            $paiement_id = $paiement->create($user,(GETPOST('closepaidinvoices')=='on'?1:0));
130            if ($paiement_id < 0)
131            {
132                $errmsg='<div class="error">'.$paiement->error.'</div>';
133                $error++;
134            }
135        }
136
137        if (! $error)
138        {
139            $result=$paiement->addPaymentToBank($user,'payment_supplier','(SupplierInvoicePayment)',$_POST['accountid'],'','');
140            if ($result < 0)
141            {
142                $errmsg='<div class="error">'.$paiement->error.'</div>';
143                $error++;
144            }
145        }
146
147        if (! $error)
148        {
149            $db->commit();
150
151            // If payment dispatching on more than one invoice, we keep on summary page, otherwise go on invoice card
152            $invoiceid=0;
153            foreach ($paiement->amounts as $key => $amount)
154            {
155                $facid = $key;
156                if (is_numeric($amount) && $amount <> 0)
157                {
158                    if ($invoiceid != 0) $invoiceid=-1; // There is more than one invoice payed by this payment
159                    else $invoiceid=$facid;
160                }
161            }
162            if ($invoiceid > 0) $loc = DOL_URL_ROOT.'/fourn/facture/fiche.php?facid='.$invoiceid;
163            else $loc = DOL_URL_ROOT.'/fourn/paiement/fiche.php?id='.$paiement_id;
164            header('Location: '.$loc);
165            exit;
166        }
167        else
168        {
169            $db->rollback();
170        }
171    }
172}
173
174
175/*
176 * View
177 */
178
179$supplierstatic=new Societe($db);
180$invoicesupplierstatic = new FactureFournisseur($db);
181
182llxHeader();
183
184$form=new Form($db);
185
186if ($action == 'create' || $action == 'add_paiement')
187{
188    $facture = new FactureFournisseur($db);
189    $facture->fetch($facid);
190
191    $datefacture=dol_mktime(12, 0, 0, $_POST['remonth'], $_POST['reday'], $_POST['reyear']);
192    $dateinvoice=($datefacture==''?(empty($conf->global->MAIN_AUTOFILL_DATE)?-1:0):$datefacture);
193
194    $sql = 'SELECT s.nom, s.rowid as socid,';
195    $sql.= ' f.rowid as ref, f.facnumber, f.amount, f.total_ttc as total';
196    if (!$user->rights->societe->client->voir && !$socid) $sql .= ", sc.fk_soc, sc.fk_user ";
197    $sql.= ' FROM '.MAIN_DB_PREFIX.'societe as s, '.MAIN_DB_PREFIX.'facture_fourn as f';
198    if (!$user->rights->societe->client->voir && !$socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
199    $sql.= ' WHERE f.fk_soc = s.rowid';
200    $sql.= ' AND f.rowid = '.$facid;
201    if (!$user->rights->societe->client->voir && !$socid) $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = " .$user->id;
202    $resql = $db->query($sql);
203    if ($resql)
204    {
205        $num = $db->num_rows($resql);
206        if ($num)
207        {
208            $obj = $db->fetch_object($resql);
209            $total = $obj->total;
210
211            print_fiche_titre($langs->trans('DoPayment'));
212
213            if ($mesg)   dol_htmloutput_mesg($mesg);
214            if ($errmsg) dol_htmloutput_errors($errmsg);
215
216            print '<form name="addpaiement" action="paiement.php" method="post">';
217            print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
218            print '<input type="hidden" name="action" value="add_paiement">';
219            print '<input type="hidden" name="facid" value="'.$facid.'">';
220            print '<input type="hidden" name="facnumber" value="'.$obj->facnumber.'">';
221            print '<input type="hidden" name="socid" value="'.$obj->socid.'">';
222            print '<input type="hidden" name="societe" value="'.$obj->nom.'">';
223
224            print '<table class="border" width="100%">';
225
226            print '<tr class="liste_titre"><td colspan="3">'.$langs->trans('Payment').'</td>';
227            print '<tr><td>'.$langs->trans('Company').'</td><td colspan="2">';
228            $supplierstatic->id=$obj->socid;
229            $supplierstatic->name=$obj->nom;
230            print $supplierstatic->getNomUrl(1,'supplier');
231            print '</td></tr>';
232            print '<tr><td class="fieldrequired">'.$langs->trans('Date').'</td><td>';
233            $form->select_date($dateinvoice,'','','','',"addpaiement",1,1);
234            print '</td>';
235            print '<td>'.$langs->trans('Comments').'</td></tr>';
236            print '<tr><td class="fieldrequired">'.$langs->trans('PaymentMode').'</td><td>';
237            $form->select_types_paiements(empty($_POST['paiementid'])?'':$_POST['paiementid'],'paiementid');
238            print '</td>';
239            print '<td rowspan="3" valign="top">';
240            print '<textarea name="comment" wrap="soft" cols="60" rows="'._ROWS_3.'">'.(empty($_POST['comment'])?'':$_POST['comment']).'</textarea></td></tr>';
241            print '<tr><td>'.$langs->trans('Numero').'</td><td><input name="num_paiement" type="text" value="'.(empty($_POST['num_paiement'])?'':$_POST['num_paiement']).'"></td></tr>';
242            if (! empty($conf->banque->enabled))
243            {
244                print '<tr><td class="fieldrequired">'.$langs->trans('Account').'</td><td>';
245                $form->select_comptes(empty($_POST['accountid'])?'':$_POST['accountid'],'accountid',0,'',2);
246                print '</td></tr>';
247            }
248            else
249            {
250                print '<tr><td colspan="2">&nbsp;</td></tr>';
251            }
252            print '</table>';
253
254            /*
255             * Autres factures impayees
256             */
257            $sql = 'SELECT f.rowid as facid,f.rowid as ref,f.facnumber,f.total_ttc, f.datef as df';
258            $sql.= ', sum(pf.amount) as am';
259            $sql.= ' FROM '.MAIN_DB_PREFIX.'facture_fourn as f';
260            $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'paiementfourn_facturefourn as pf ON pf.fk_facturefourn = f.rowid';
261            $sql.= " WHERE f.entity = ".$conf->entity;
262            $sql.= ' AND f.fk_soc = '.$facture->socid;
263            $sql.= ' AND f.paye = 0';
264            $sql.= ' AND f.fk_statut = 1';  // Statut=0 => non validee, Statut=2 => annulee
265            $sql.= ' GROUP BY f.rowid,f.facnumber,f.total_ttc,f.datef';
266            $resql = $db->query($sql);
267            if ($resql)
268            {
269                $num = $db->num_rows($resql);
270                if ($num > 0)
271                {
272                    $i = 0;
273                    print '<br>';
274
275                    print $langs->trans('Invoices').'<br>';
276                    print '<table class="noborder" width="100%">';
277                    print '<tr class="liste_titre">';
278                    print '<td>'.$langs->trans('Ref').'</td>';
279                    print '<td>'.$langs->trans('RefSupplier').'</td>';
280                    print '<td align="center">'.$langs->trans('Date').'</td>';
281                    print '<td align="right">'.$langs->trans('AmountTTC').'</td>';
282                    print '<td align="right">'.$langs->trans('AlreadyPaid').'</td>';
283                    print '<td align="right">'.$langs->trans('RemainderToPay').'</td>';
284                    print '<td align="center">'.$langs->trans('Amount').'</td>';
285                    print '</tr>';
286
287                    $var=True;
288                    $total=0;
289                    $totalrecu=0;
290                    while ($i < $num)
291                    {
292                        $objp = $db->fetch_object($resql);
293                        $var=!$var;
294                        print '<tr '.$bc[$var].'>';
295                        print '<td><a href="fiche.php?facid='.$objp->facid.'">'.img_object($langs->trans('ShowBill'),'bill').' '.$objp->ref;
296                        print '</a></td>';
297                        print '<td>'.$objp->facnumber.'</td>';
298                        if ($objp->df > 0 )
299                        {
300                            print '<td align="center">';
301                            print dol_print_date($db->jdate($objp->df)).'</td>';
302                        }
303                        else
304                        {
305                            print '<td align="center"><b>!!!</b></td>';
306                        }
307                        print '<td align="right">'.price($objp->total_ttc).'</td>';
308                        print '<td align="right">'.price($objp->am).'</td>';
309                        print '<td align="right">'.price($objp->total_ttc - $objp->am).'</td>';
310                        print '<td align="center">';
311                        $namef = 'amount_'.$objp->facid;
312                        print '<input type="text" size="8" name="'.$namef.'" value="'.GETPOST($namef).'">';
313                        print "</td></tr>\n";
314                        $total+=$objp->total;
315                        $total_ttc+=$objp->total_ttc;
316                        $totalrecu+=$objp->am;
317                        $i++;
318                    }
319                    if ($i > 1)
320                    {
321                        // Print total
322                        print '<tr class="liste_total">';
323                        print '<td colspan="3" align="left">'.$langs->trans('TotalTTC').':</td>';
324                        print '<td align="right"><b>'.price($total_ttc).'</b></td>';
325                        print '<td align="right"><b>'.price($totalrecu).'</b></td>';
326                        print '<td align="right"><b>'.price($total_ttc - $totalrecu).'</b></td>';
327                        print '<td align="center">&nbsp;</td>';
328                        print "</tr>\n";
329                    }
330                    print "</table>\n";
331                }
332                $db->free($resql);
333            }
334            else
335            {
336                dol_print_error($db);
337            }
338
339			//			print '<tr><td colspan="3" align="center">';
340			print '<center><br><input type="checkbox" checked="checked" name="closepaidinvoices"> '.$langs->trans("ClosePaidInvoicesAutomatically");
341			print '<br><input type="submit" class="button" value="'.$langs->trans('Save').'"></center>';
342			//			print '</td></tr>';
343
344            print '</form>';
345        }
346    }
347}
348
349/*
350 * Show list
351 */
352if (! $_GET['action'] && ! $_POST['action'])
353{
354    if ($page == -1) $page = 0 ;
355    $limit = $conf->liste_limit;
356    $offset = $limit * $page ;
357
358    if (! $sortorder) $sortorder='DESC';
359    if (! $sortfield) $sortfield='p.datep';
360
361    $sql = 'SELECT p.rowid as pid, p.datep as dp, p.amount as pamount, p.num_paiement,';
362    $sql.= ' s.rowid as socid, s.nom,';
363    $sql.= ' c.libelle as paiement_type,';
364    $sql.= ' ba.rowid as bid, ba.label,';
365    if (!$user->rights->societe->client->voir) $sql .= ' sc.fk_soc, sc.fk_user,';
366    $sql.= ' SUM(f.amount)';
367    $sql.= ' FROM '.MAIN_DB_PREFIX.'paiementfourn AS p';
368    if (!$user->rights->societe->client->voir) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
369    $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'paiementfourn_facturefourn AS pf ON p.rowid=pf.fk_paiementfourn';
370    $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'facture_fourn AS f ON f.rowid=pf.fk_facturefourn';
371    $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_paiement AS c ON p.fk_paiement = c.id';
372    $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'societe AS s ON s.rowid = f.fk_soc';
373    $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'bank as b ON p.fk_bank = b.rowid';
374    $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'bank_account as ba ON b.fk_account = ba.rowid';
375    $sql.= " WHERE f.entity = ".$conf->entity;
376    if (!$user->rights->societe->client->voir) $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = " .$user->id;
377    if ($socid)
378    {
379        $sql .= ' AND f.fk_soc = '.$socid;
380    }
381    // Search criteria
382    if ($_REQUEST["search_ref"])
383    {
384        $sql .= ' AND p.rowid='.$db->escape($_REQUEST["search_ref"]);
385    }
386    if ($_REQUEST["search_account"])
387    {
388        $sql .= ' AND b.fk_account='.$db->escape($_REQUEST["search_account"]);
389    }
390    if ($_REQUEST["search_paymenttype"])
391    {
392        $sql .= " AND c.code='".$db->escape($_REQUEST["search_paymenttype"])."'";
393    }
394    if ($_REQUEST["search_amount"])
395    {
396        $sql .= " AND p.amount=".price2num($_REQUEST["search_amount"]);
397    }
398    if ($_REQUEST["search_company"])
399    {
400        $sql .= " AND s.nom LIKE '%".$db->escape($_REQUEST["search_company"])."%'";
401    }
402    $sql.= " GROUP BY p.rowid, p.datep, p.amount, p.num_paiement, s.rowid, s.nom, c.libelle, ba.rowid, ba.label";
403    if (!$user->rights->societe->client->voir) $sql .= ", sc.fk_soc, sc.fk_user";
404    $sql.= $db->order($sortfield,$sortorder);
405    $sql.= $db->plimit($limit+1, $offset);
406
407    $resql = $db->query($sql);
408    if ($resql)
409    {
410        $num = $db->num_rows($resql);
411        $i = 0;
412        $var=True;
413
414        $paramlist='';
415        $paramlist.=($_REQUEST["search_ref"]?"&search_ref=".$_REQUEST["search_ref"]:"");
416        $paramlist.=($_REQUEST["search_company"]?"&search_company=".$_REQUEST["search_company"]:"");
417        $paramlist.=($_REQUEST["search_amount"]?"&search_amount=".$_REQUEST["search_amount"]:"");
418
419        print_barre_liste($langs->trans('SupplierPayments'), $page, 'paiement.php',$paramlist,$sortfield,$sortorder,'',$num);
420
421        if ($mesg) dol_htmloutput_mesg($mesg);
422        if ($errmsg) dol_htmloutput_errors($errmsg);
423
424        print '<form method="GET" action="'.$_SERVER["PHP_SELF"].'">';
425        print '<table class="noborder" width="100%">';
426        print '<tr class="liste_titre">';
427        print_liste_field_titre($langs->trans('RefPayment'),'paiement.php','p.rowid','',$paramlist,'',$sortfield,$sortorder);
428        print_liste_field_titre($langs->trans('Date'),'paiement.php','dp','',$paramlist,'align="center"',$sortfield,$sortorder);
429        print_liste_field_titre($langs->trans('ThirdParty'),'paiement.php','s.nom','',$paramlist,'',$sortfield,$sortorder);
430        print_liste_field_titre($langs->trans('Type'),'paiement.php','c.libelle','',$paramlist,'',$sortfield,$sortorder);
431        print_liste_field_titre($langs->trans('Account'),'paiement.php','ba.label','',$paramlist,'',$sortfield,$sortorder);
432        print_liste_field_titre($langs->trans('Amount'),'paiement.php','f.amount','',$paramlist,'align="right"',$sortfield,$sortorder);
433        //print_liste_field_titre($langs->trans('Invoice'),'paiement.php','facnumber','',$paramlist,'',$sortfield,$sortorder);
434        print "</tr>\n";
435
436        // Lines for filters fields
437        print '<tr class="liste_titre">';
438        print '<td align="left">';
439        print '<input class="fat" type="text" size="4" name="search_ref" value="'.$_REQUEST["search_ref"].'">';
440        print '</td>';
441        print '<td>&nbsp;</td>';
442        print '<td align="left">';
443        print '<input class="fat" type="text" size="6" name="search_company" value="'.$_REQUEST["search_company"].'">';
444        print '</td>';
445        print '<td>';
446        $form->select_types_paiements($_REQUEST["search_paymenttype"],'search_paymenttype','',2,1,1);
447        print '</td>';
448        print '<td>';
449        $form->select_comptes($_REQUEST["search_account"],'search_account',0,'',1);
450        print '</td>';
451        print '<td align="right">';
452        print '<input class="fat" type="text" size="4" name="search_amount" value="'.$_REQUEST["search_amount"].'">';
453        print '<input type="image" class="liste_titre" name="button_search" src="'.DOL_URL_ROOT.'/theme/'.$conf->theme.'/img/search.png" alt="'.$langs->trans("Search").'">';
454        print '</td>';
455        print "</tr>\n";
456
457        while ($i < min($num,$limit))
458        {
459            $objp = $db->fetch_object($resql);
460            $var=!$var;
461            print '<tr '.$bc[$var].'>';
462
463            // Ref payment
464            print '<td nowrap="nowrap"><a href="'.DOL_URL_ROOT.'/fourn/paiement/fiche.php?id='.$objp->pid.'">'.img_object($langs->trans('ShowPayment'),'payment').' '.$objp->pid.'</a></td>';
465
466            // Date
467            print '<td nowrap="nowrap" align="center">'.dol_print_date($db->jdate($objp->dp),'day')."</td>\n";
468
469            print '<td>';
470            if ($objp->socid) print '<a href="'.DOL_URL_ROOT.'/societe/soc.php?socid='.$objp->socid.'">'.img_object($langs->trans('ShowCompany'),'company').' '.dol_trunc($objp->nom,32).'</a>';
471            else print '&nbsp;';
472            print '</td>';
473
474            print '<td>'.dol_trunc($objp->paiement_type.' '.$objp->num_paiement,32)."</td>\n";
475
476            print '<td>';
477            if ($objp->bid) print '<a href="'.DOL_URL_ROOT.'/compta/bank/account.php?account='.$objp->bid.'">'.img_object($langs->trans("ShowAccount"),'account').' '.dol_trunc($objp->label,24).'</a>';
478            else print '&nbsp;';
479            print '</td>';
480
481            print '<td align="right">'.price($objp->pamount).'</td>';
482
483            // Ref invoice
484            /*$invoicesupplierstatic->ref=$objp->facnumber;
485            $invoicesupplierstatic->id=$objp->facid;
486            print '<td nowrap="nowrap">';
487            print $invoicesupplierstatic->getNomUrl(1);
488            print '</td>';*/
489
490            print '</tr>';
491            $i++;
492        }
493        print "</table>";
494        print "</form>\n";
495    }
496    else
497    {
498        dol_print_error($db);
499    }
500}
501
502$db->close();
503
504llxFooter();
505?>