PageRenderTime 191ms CodeModel.GetById 53ms app.highlight 59ms RepoModel.GetById 58ms app.codeStats 1ms

/htdocs/facture/list.old.php

https://bitbucket.org/speedealing/speedealing
PHP | 422 lines | 322 code | 51 blank | 49 comment | 64 complexity | 3e03a1a6f7467ed2b1307436ecafff29 MD5 | raw file
  1<?php
  2/* Copyright (C) 2002-2006 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) 2005      Marc Barilley / Ocebo <marc@ocebo.com>
  6 * Copyright (C) 2005-2012 Regis Houssin         <regis.houssin@capnetworks.com>
  7 * Copyright (C) 2006      Andre Cianfarani      <acianfa@free.fr>
  8 * Copyright (C) 2010-2012 Juanjo Menent         <jmenent@2byte.es>
  9 * Copyright (C) 2012      Christophe Battarel   <christophe.battarel@altairis.fr>
 10 *
 11 * This program is free software; you can redistribute it and/or modify
 12 * it under the terms of the GNU General Public License as published by
 13 * the Free Software Foundation; either version 3 of the License, or
 14 * (at your option) any later version.
 15 *
 16 * This program is distributed in the hope that it will be useful,
 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 19 * GNU General Public License for more details.
 20 *
 21 * You should have received a copy of the GNU General Public License
 22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 23 */
 24
 25/**
 26 *	\file       htdocs/compta/facture/list.php
 27 *	\ingroup    facture
 28 *	\brief      Page to create/see an invoice
 29 */
 30
 31require '../../main.inc.php';
 32require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
 33require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
 34require_once DOL_DOCUMENT_ROOT.'/core/modules/facture/modules_facture.php';
 35require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
 36require_once DOL_DOCUMENT_ROOT.'/core/class/discount.class.php';
 37require_once DOL_DOCUMENT_ROOT.'/compta/paiement/class/paiement.class.php';
 38require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
 39require_once DOL_DOCUMENT_ROOT.'/core/lib/invoice.lib.php';
 40require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
 41if (! empty($conf->commande->enabled)) require_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php';
 42if (! empty($conf->projet->enabled))
 43{
 44	require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
 45	require_once DOL_DOCUMENT_ROOT.'/core/lib/project.lib.php';
 46}
 47
 48$langs->load('bills');
 49$langs->load('companies');
 50$langs->load('products');
 51$langs->load('main');
 52
 53$sall=trim(GETPOST('sall'));
 54$projectid=(GETPOST('projectid')?GETPOST('projectid','int'):0);
 55
 56$id=(GETPOST('id','int')?GETPOST('id','int'):GETPOST('facid','int'));  // For backward compatibility
 57$ref=GETPOST('ref','alpha');
 58$socid=GETPOST('socid','int');
 59$action=GETPOST('action','alpha');
 60$confirm=GETPOST('confirm','alpha');
 61$lineid=GETPOST('lineid','int');
 62$userid=GETPOST('userid','int');
 63$search_ref=GETPOST('sf_ref')?GETPOST('sf_ref','alpha'):GETPOST('search_ref','alpha');
 64$search_societe=GETPOST('search_societe','alpha');
 65$search_montant_ht=GETPOST('search_montant_ht','alpha');
 66$search_montant_ttc=GETPOST('search_montant_ttc','alpha');
 67
 68$sortfield = GETPOST("sortfield",'alpha');
 69$sortorder = GETPOST("sortorder",'alpha');
 70$page = GETPOST("page",'int');
 71if ($page == -1) {
 72    $page = 0;
 73}
 74$offset = $conf->liste_limit * $page;
 75if (! $sortorder) $sortorder='DESC';
 76if (! $sortfield) $sortfield='f.datef';
 77$limit = $conf->liste_limit;
 78
 79$pageprev = $page - 1;
 80$pagenext = $page + 1;
 81
 82$search_user = GETPOST('search_user','int');
 83$search_sale = GETPOST('search_sale','int');
 84$day	= GETPOST('day','int');
 85$month	= GETPOST('month','int');
 86$year	= GETPOST('year','int');
 87$filtre	= GETPOST('filtre');
 88
 89// Security check
 90$fieldid = (! empty($ref)?'facnumber':'rowid');
 91if (! empty($user->societe_id)) $socid=$user->societe_id;
 92$result = restrictedArea($user, 'facture', $id,'','','fk_soc',$fieldid);
 93
 94// FIXME $usehm not used ?
 95$usehm=(! empty($conf->global->MAIN_USE_HOURMIN_IN_DATE_RANGE)?$conf->global->MAIN_USE_HOURMIN_IN_DATE_RANGE:false);
 96
 97$object=new Facture($db);
 98
 99// Initialize technical object to manage hooks of thirdparties. Note that conf->hooks_modules contains array array
100$hookmanager->initHooks(array('invoicecard'));
101
102$now=dol_now();
103
104
105/*
106 * Actions
107 */
108
109$parameters=array('socid'=>$socid);
110$reshook=$hookmanager->executeHooks('doActions',$parameters,$object,$action);    // Note that $action and $object may have been modified by some hooks
111
112// Do we click on purge search criteria ?
113if (GETPOST("button_removefilter_x"))
114{
115    $search_categ='';
116    $search_user='';
117    $search_sale='';
118    $search_ref='';
119    $search_refcustomer='';
120    $search_societe='';
121    $search_montant_ht='';
122    $year='';
123    $month='';
124}
125
126
127/*
128 * View
129 */
130
131llxHeader('',$langs->trans('Bill'),'EN:Customers_Invoices|FR:Factures_Clients|ES:Facturas_a_clientes');
132
133$form = new Form($db);
134$formother = new FormOther($db);
135$formfile = new FormFile($db);
136$bankaccountstatic=new Account($db);
137$facturestatic=new Facture($db);
138
139if (! $sall) $sql = 'SELECT';
140else $sql = 'SELECT DISTINCT';
141$sql.= ' f.rowid as facid, f.facnumber, f.type, f.increment, f.total as total_ht, f.tva as total_tva, f.total_ttc,';
142$sql.= ' f.datef as df, f.date_lim_reglement as datelimite,';
143$sql.= ' f.paye as paye, f.fk_statut, f.note,';
144$sql.= ' s.nom, s.rowid as socid';
145if (! $sall) $sql.= ', SUM(pf.amount) as am';   // To be able to sort on status
146$sql.= ' FROM '.MAIN_DB_PREFIX.'societe as s';
147$sql.= ', '.MAIN_DB_PREFIX.'facture as f';
148if (! $sall) $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'paiement_facture as pf ON pf.fk_facture = f.rowid';
149else $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'facturedet as fd ON fd.fk_facture = f.rowid';
150// We'll need this table joined to the select in order to filter by sale
151if ($search_sale > 0 || (! $user->rights->societe->client->voir && ! $socid)) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
152if ($search_user > 0)
153{
154    $sql.=", ".MAIN_DB_PREFIX."element_contact as ec";
155    $sql.=", ".MAIN_DB_PREFIX."c_type_contact as tc";
156}
157$sql.= ' WHERE f.fk_soc = s.rowid';
158$sql.= " AND f.entity = ".$conf->entity;
159if (! $user->rights->societe->client->voir && ! $socid) $sql.= " AND s.rowid = sc.fk_soc AND sc.fk_user = " .$user->id;
160if ($socid) $sql.= ' AND s.rowid = '.$socid;
161if ($userid)
162{
163    if ($userid == -1) $sql.=' AND f.fk_user_author IS NULL';
164    else $sql.=' AND f.fk_user_author = '.$userid;
165}
166if ($filtre)
167{
168    $aFilter = explode(',', $filtre);
169    foreach ($aFilter as $filter)
170    {
171        $filt = explode(':', $filter);
172        $sql .= ' AND ' . trim($filt[0]) . ' = ' . trim($filt[1]);
173    }
174}
175if ($search_ref)
176{
177    $sql.= ' AND f.facnumber LIKE \'%'.$db->escape(trim($search_ref)).'%\'';
178}
179if ($search_societe)
180{
181    $sql.= ' AND s.nom LIKE \'%'.$db->escape(trim($search_societe)).'%\'';
182}
183if ($search_montant_ht)
184{
185    $sql.= ' AND f.total = \''.$db->escape(trim($search_montant_ht)).'\'';
186}
187if ($search_montant_ttc)
188{
189    $sql.= ' AND f.total_ttc = \''.$db->escape(trim($search_montant_ttc)).'\'';
190}
191if ($month > 0)
192{
193    if ($year > 0 && empty($day))
194    $sql.= " AND f.datef BETWEEN '".$db->idate(dol_get_first_day($year,$month,false))."' AND '".$db->idate(dol_get_last_day($year,$month,false))."'";
195    else if ($year > 0 && ! empty($day))
196    $sql.= " AND f.datef BETWEEN '".$db->idate(dol_mktime(0, 0, 0, $month, $day, $year))."' AND '".$db->idate(dol_mktime(23, 59, 59, $month, $day, $year))."'";
197    else
198    $sql.= " AND date_format(f.datef, '%m') = '".$month."'";
199}
200else if ($year > 0)
201{
202    $sql.= " AND f.datef BETWEEN '".$db->idate(dol_get_first_day($year,1,false))."' AND '".$db->idate(dol_get_last_day($year,12,false))."'";
203}
204if ($search_sale > 0) $sql.= " AND s.rowid = sc.fk_soc AND sc.fk_user = " .$search_sale;
205if ($search_user > 0)
206{
207    $sql.= " AND ec.fk_c_type_contact = tc.rowid AND tc.element='facture' AND tc.source='internal' AND ec.element_id = f.rowid AND ec.fk_socpeople = ".$search_user;
208}
209if (! $sall)
210{
211    $sql.= ' GROUP BY f.rowid, f.facnumber, f.type, f.increment, f.total, f.total_ttc,';
212    $sql.= ' f.datef, f.date_lim_reglement,';
213    $sql.= ' f.paye, f.fk_statut, f.note,';
214    $sql.= ' s.nom, s.rowid';
215}
216else
217{
218	$sql.= ' AND (s.nom LIKE \'%'.$db->escape($sall).'%\' OR f.facnumber LIKE \'%'.$db->escape($sall).'%\' OR f.note LIKE \'%'.$db->escape($sall).'%\' OR fd.description LIKE \'%'.$db->escape($sall).'%\')';
219}
220$sql.= ' ORDER BY ';
221$listfield=explode(',',$sortfield);
222foreach ($listfield as $key => $value) $sql.= $listfield[$key].' '.$sortorder.',';
223$sql.= ' f.rowid DESC ';
224$sql.= $db->plimit($limit+1,$offset);
225//print $sql;
226
227print '<div class="row">';
228print start_box($langs->trans('BillsCustomers'),"twelve","16-Money-2.png",false);
229
230$resql = $db->query($sql);
231if ($resql)
232{
233    $num = $db->num_rows($resql);
234
235    if ($socid)
236    {
237        $soc = new Societe($db);
238        $soc->fetch($socid);
239    }
240
241    $param='&socid='.$socid;
242    if ($month)              $param.='&month='.$month;
243    if ($year)               $param.='&year=' .$year;
244    if ($search_ref)         $param.='&search_ref=' .$search_ref;
245    if ($search_societe)     $param.='&search_societe=' .$search_societe;
246    if ($search_sale > 0)    $param.='&search_sale=' .$search_sale;
247    if ($search_user > 0)    $param.='&search_user=' .$search_user;
248    if ($search_montant_ht)  $param.='&search_montant_ht='.$search_montant_ht;
249    if ($search_montant_ttc) $param.='&search_montant_ttc='.$search_montant_ttc;
250    //print_barre_liste($langs->trans('BillsCustomers').' '.($socid?' '.$soc->nom:''),$page,$_SERVER["PHP_SELF"],$param,$sortfield,$sortorder,'',$num);
251
252    $i = 0;
253    print '<form method="GET" action="'.$_SERVER["PHP_SELF"].'">'."\n";
254    print '<table class="liste" width="100%">';
255
256 	// If the user can view prospects other than his'
257    $moreforfilter='';
258 	if ($user->rights->societe->client->voir || $socid)
259 	{
260 		$langs->load("commercial");
261 		$moreforfilter.=$langs->trans('ThirdPartiesOfSaleRepresentative'). ': ';
262		$moreforfilter.=$formother->select_salesrepresentatives($search_sale,'search_sale',$user);
263	 	$moreforfilter.=' &nbsp; &nbsp; &nbsp; ';
264 	}
265    // If the user can view prospects other than his'
266    if ($user->rights->societe->client->voir || $socid)
267    {
268        $moreforfilter.=$langs->trans('LinkedToSpecificUsers'). ': ';
269        $moreforfilter.=$form->select_dolusers($search_user,'search_user',1);
270    }
271    if ($moreforfilter)
272    {
273        print '<tr class="liste_titre">';
274        print '<td class="liste_titre" colspan="9">';
275        print $moreforfilter;
276        print '</td></tr>';
277    }
278
279    print '<tr class="liste_titre">';
280    print_liste_field_titre($langs->trans('Ref'),$_SERVER['PHP_SELF'],'f.facnumber','',$param,'',$sortfield,$sortorder);
281    print_liste_field_titre($langs->trans('Date'),$_SERVER['PHP_SELF'],'f.datef','',$param,'align="center"',$sortfield,$sortorder);
282    print_liste_field_titre($langs->trans("DateDue"),$_SERVER['PHP_SELF'],"f.date_lim_reglement",'',$param,'align="center"',$sortfield,$sortorder);
283    print_liste_field_titre($langs->trans('Company'),$_SERVER['PHP_SELF'],'s.nom','',$param,'',$sortfield,$sortorder);
284    print_liste_field_titre($langs->trans('AmountHT'),$_SERVER['PHP_SELF'],'f.total','',$param,'align="right"',$sortfield,$sortorder);
285    print_liste_field_titre($langs->trans('AmountVAT'),$_SERVER['PHP_SELF'],'f.tva','',$param,'align="right"',$sortfield,$sortorder);
286    print_liste_field_titre($langs->trans('AmountTTC'),$_SERVER['PHP_SELF'],'f.total_ttc','',$param,'align="right"',$sortfield,$sortorder);
287    print_liste_field_titre($langs->trans('Received'),$_SERVER['PHP_SELF'],'am','',$param,'align="right"',$sortfield,$sortorder);
288    print_liste_field_titre($langs->trans('Status'),$_SERVER['PHP_SELF'],'fk_statut,paye,am','',$param,'align="right"',$sortfield,$sortorder);
289    //print '<td class="liste_titre">&nbsp;</td>';
290    print '</tr>';
291
292    // Filters lines
293    print '<tr class="liste_titre">';
294    print '<td class="liste_titre" align="left">';
295    print '<input class="flat" size="10" type="text" name="search_ref" value="'.$search_ref.'">';
296    print '</td>';
297    print '<td class="liste_titre" align="center">';
298    if (! empty($conf->global->MAIN_LIST_FILTER_ON_DAY)) print '<input class="flat" type="text" size="1" maxlength="2" name="day" value="'.$day.'">';
299    print '<input class="flat" type="text" size="1" maxlength="2" name="month" value="'.$month.'">';
300    $formother->select_year($year?$year:-1,'year',1, 20, 5);
301    print '</td>';
302    print '<td class="liste_titre" align="left">&nbsp;</td>';
303    print '<td class="liste_titre" align="left"><input class="flat" type="text" name="search_societe" value="'.$search_societe.'"></td>';
304    print '<td class="liste_titre" align="right"><input class="flat" type="text" size="10" name="search_montant_ht" value="'.$search_montant_ht.'"></td>';
305    print '<td class="liste_titre" align="right">&nbsp;</td>';
306    print '<td class="liste_titre" align="right"><input class="flat" type="text" size="10" name="search_montant_ttc" value="'.$search_montant_ttc.'"></td>';
307    print '<td class="liste_titre" align="right">&nbsp;</td>';
308    print '<td class="liste_titre" align="right"><input type="image" class="liste_titre" name="button_search" src="'.DOL_URL_ROOT.'/theme/'.$conf->theme.'/img/search.png" value="'.dol_escape_htmltag($langs->trans("Search")).'" title="'.dol_escape_htmltag($langs->trans("Search")).'">';
309    print "</td></tr>\n";
310
311    if ($num > 0)
312    {
313        $var=True;
314        $total_ht=0;
315        $total_tva=0;
316        $total_ttc=0;
317        $totalrecu=0;
318
319        while ($i < min($num,$limit))
320        {
321            $objp = $db->fetch_object($resql);
322            $var=!$var;
323
324            $datelimit=$db->jdate($objp->datelimite);
325
326            print '<tr '.$bc[$var].'>';
327            print '<td nowrap="nowrap">';
328
329            $facturestatic->id=$objp->facid;
330            $facturestatic->ref=$objp->facnumber;
331            $facturestatic->type=$objp->type;
332            $notetoshow=dol_string_nohtmltag(($user->societe_id>0?$objp->note_public:$objp->note),1);
333            $paiement = $facturestatic->getSommePaiement();
334
335            print '<table class="nobordernopadding"><tr class="nocellnopadd">';
336
337            print '<td class="nobordernopadding" nowrap="nowrap">';
338            print $facturestatic->getNomUrl(1,'',200,0,$notetoshow);
339            print $objp->increment;
340            print '</td>';
341
342            print '<td width="16" align="right" class="nobordernopadding">';
343            $filename=dol_sanitizeFileName($objp->facnumber);
344            $filedir=$conf->facture->dir_output . '/' . dol_sanitizeFileName($objp->facnumber);
345            $urlsource=$_SERVER['PHP_SELF'].'?id='.$objp->facid;
346            print $formfile->getDocumentsLink($facturestatic->element, $filename, $filedir);
347            print '</td>';
348            print '</tr>';
349            print '</table>';
350
351            print "</td>\n";
352
353            // Date
354            print '<td align="center" nowrap>';
355            print dol_print_date($db->jdate($objp->df),'day');
356            print '</td>';
357
358            // Date limit
359            print '<td align="center" nowrap="1">'.dol_print_date($datelimit,'day');
360            if ($datelimit < ($now - $conf->facture->client->warning_delay) && ! $objp->paye && $objp->fk_statut == 1 && ! $paiement)
361            {
362                print img_warning($langs->trans('Late'));
363            }
364            print '</td>';
365
366            print '<td>';
367            $thirdparty=new Societe($db);
368            $thirdparty->id=$objp->socid;
369            $thirdparty->nom=$objp->nom;
370            print $thirdparty->getNomUrl(1,'customer');
371            print '</td>';
372
373            print '<td align="right">'.price($objp->total_ht).' '.$langs->getCurrencySymbol($conf->currency).'</td>';
374
375            print '<td align="right">'.price($objp->total_tva).' '.$langs->getCurrencySymbol($conf->currency).'</td>';
376
377            print '<td align="right">'.price($objp->total_ttc).' '.$langs->getCurrencySymbol($conf->currency).'</td>';
378
379            print '<td align="right">'.(! empty($paiement)?price($paiement).' '.$langs->getCurrencySymbol($conf->currency):'&nbsp;').'</td>';
380
381            // Affiche statut de la facture
382            print '<td align="right" nowrap="nowrap">';
383            print $facturestatic->LibStatut($objp->paye,$objp->fk_statut,5,$paiement,$objp->type);
384            print "</td>";
385            //print "<td>&nbsp;</td>";
386            print "</tr>\n";
387            $total_ht+=$objp->total_ht;
388            $total_tva+=$objp->total_tva;
389            $total_ttc+=$objp->total_ttc;
390            $totalrecu+=$paiement;
391            $i++;
392        }
393
394        if (($offset + $num) <= $limit)
395        {
396            // Print total
397            print '<tr class="liste_total">';
398            print '<td class="liste_total" colspan="4" align="left">'.$langs->trans('Total').'</td>';
399            print '<td class="liste_total" align="right">'.price($total_ht).' '.$langs->getCurrencySymbol($conf->currency).'</td>';
400            print '<td class="liste_total" align="right">'.price($total_tva).' '.$langs->getCurrencySymbol($conf->currency).'</td>';
401            print '<td class="liste_total" align="right">'.price($total_ttc).' '.$langs->getCurrencySymbol($conf->currency).'</td>';
402            print '<td class="liste_total" align="right">'.price($totalrecu).' '.$langs->getCurrencySymbol($conf->currency).'</td>';
403            print '<td class="liste_total" align="center">&nbsp;</td>';
404            print '</tr>';
405        }
406    }
407
408    print "</table>\n";
409    print "</form>\n";
410    $db->free($resql);
411}
412else
413{
414    dol_print_error($db);
415}
416
417print end_box();
418print '</div>';
419
420llxFooter();
421$db->close();
422?>