dolibarr  16.0.5
facture.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2003-2007 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3  * Copyright (C) 2004-2016 Laurent Destailleur <eldy@users.sourceforge.net>
4  * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
5  * Copyright (C) 2014 Juanjo Menent <jmenent@2byte.es>
6  * Copyright (C) 2014 Florian Henry <florian.henry@open-concept.pro>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <https://www.gnu.org/licenses/>.
20  */
21 
28 require '../../main.inc.php';
29 require_once DOL_DOCUMENT_ROOT.'/core/lib/product.lib.php';
30 require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
31 require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
32 require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
33 
34 // Load translation files required by the page
35 $langs->loadLangs(array('companies', 'bills', 'products', 'supplier_proposal'));
36 
37 $id = GETPOST('id', 'int');
38 $ref = GETPOST('ref', 'alpha');
39 
40 // Security check
41 $fieldvalue = (!empty($id) ? $id : (!empty($ref) ? $ref : ''));
42 $fieldtype = (!empty($ref) ? 'ref' : 'rowid');
43 $socid = '';
44 if (!empty($user->socid)) {
45  $socid = $user->socid;
46 }
47 
48 // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
49 $hookmanager->initHooks(array('productstatsinvoice'));
50 
51 $showmessage = GETPOST('showmessage');
52 
53 // Load variable for pagination
54 $limit = GETPOST('limit', 'int') ?GETPOST('limit', 'int') : $conf->liste_limit;
55 $sortfield = GETPOST('sortfield', 'aZ09comma');
56 $sortorder = GETPOST('sortorder', 'aZ09comma');
57 $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
58 if (empty($page) || $page == -1) {
59  $page = 0;
60 } // If $page is not defined, or '' or -1
61 $offset = $limit * $page;
62 $pageprev = $page - 1;
63 $pagenext = $page + 1;
64 if (!$sortorder) {
65  $sortorder = "DESC";
66 }
67 if (!$sortfield) {
68  $sortfield = "f.datef";
69 }
70 
71 $search_month = GETPOST('search_month', 'int');
72 $search_year = GETPOST('search_year', 'int');
73 
74 if (GETPOST('button_removefilter_x', 'alpha') || GETPOST('button_removefilter', 'alpha')) {
75  $search_month = '';
76  $search_year = '';
77 }
78 
79 $result = restrictedArea($user, 'produit|service', $fieldvalue, 'product&product', '', '', $fieldtype);
80 
81 
82 /*
83  * View
84  */
85 
86 $invoicestatic = new Facture($db);
87 $societestatic = new Societe($db);
88 
89 $form = new Form($db);
90 $formother = new FormOther($db);
91 
92 if ($id > 0 || !empty($ref)) {
93  $product = new Product($db);
94  $result = $product->fetch($id, $ref);
95 
96  $object = $product;
97 
98  $parameters = array('id'=>$id);
99  $reshook = $hookmanager->executeHooks('doActions', $parameters, $product, $action); // Note that $action and $object may have been modified by some hooks
100  if ($reshook < 0) {
101  setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
102  }
103 
104  $title = $langs->trans('ProductServiceCard');
105  $helpurl = '';
106  $shortlabel = dol_trunc($object->label, 16);
107  if (GETPOST("type") == '0' || ($object->type == Product::TYPE_PRODUCT)) {
108  $title = $langs->trans('Product')." ".$shortlabel." - ".$langs->trans('Referers');
109  $helpurl = 'EN:Module_Products|FR:Module_Produits|ES:M&oacute;dulo_Productos';
110  }
111  if (GETPOST("type") == '1' || ($object->type == Product::TYPE_SERVICE)) {
112  $title = $langs->trans('Service')." ".$shortlabel." - ".$langs->trans('Referers');
113  $helpurl = 'EN:Module_Services_En|FR:Module_Services|ES:M&oacute;dulo_Servicios';
114  }
115 
116  llxHeader('', $title, $helpurl);
117 
118  if ($result > 0) {
119  $head = product_prepare_head($product);
120  $titre = $langs->trans("CardProduct".$product->type);
121  $picto = ($product->type == Product::TYPE_SERVICE ? 'service' : 'product');
122  print dol_get_fiche_head($head, 'referers', $titre, -1, $picto);
123 
124  $reshook = $hookmanager->executeHooks('formObjectOptions', $parameters, $product, $action); // Note that $action and $object may have been modified by hook
125  print $hookmanager->resPrint;
126  if ($reshook < 0) {
127  setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
128  }
129 
130  $linkback = '<a href="'.DOL_URL_ROOT.'/product/list.php?restore_lastsearch_values=1">'.$langs->trans("BackToList").'</a>';
131 
132  $shownav = 1;
133  if ($user->socid && !in_array('product', explode(',', $conf->global->MAIN_MODULES_FOR_EXTERNAL))) {
134  $shownav = 0;
135  }
136 
137  dol_banner_tab($object, 'ref', $linkback, $shownav, 'ref');
138 
139  print '<div class="fichecenter">';
140 
141  print '<div class="underbanner clearboth"></div>';
142  print '<table class="border tableforfield" width="100%">';
143 
144  $nboflines = show_stats_for_company($product, $socid);
145 
146  print "</table>";
147 
148  print '</div>';
149  print '<div style="clear:both"></div>';
150 
151  print dol_get_fiche_end();
152 
153  if ($showmessage && $nboflines > 1) {
154  print '<span class="opacitymedium">'.$langs->trans("ClinkOnALinkOfColumn", $langs->transnoentitiesnoconv("Referers")).'</span>';
155  } elseif ($user->rights->facture->lire) {
156  $sql = "SELECT DISTINCT s.nom as name, s.rowid as socid, s.code_client,";
157  $sql .= " f.ref, f.datef, f.paye, f.type, f.fk_statut as statut, f.rowid as facid,";
158  $sql .= " d.rowid, d.total_ht as total_ht, d.qty"; // We must keep the d.rowid here to not loose record because of the distinct used to ignore duplicate line when link on societe_commerciaux is used
159  if (empty($user->rights->societe->client->voir) && !$socid) {
160  $sql .= ", sc.fk_soc, sc.fk_user ";
161  }
162  $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
163  $sql .= ", ".MAIN_DB_PREFIX."facture as f";
164  $sql .= ", ".MAIN_DB_PREFIX."facturedet as d";
165  if (empty($user->rights->societe->client->voir) && !$socid) {
166  $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
167  }
168  $sql .= " WHERE f.fk_soc = s.rowid";
169  $sql .= " AND f.entity IN (".getEntity('invoice').")";
170  $sql .= " AND d.fk_facture = f.rowid";
171  $sql .= " AND d.fk_product = ".((int) $product->id);
172  if (!empty($search_month)) {
173  $sql .= ' AND MONTH(f.datef) IN ('.$db->sanitize($search_month).')';
174  }
175  if (!empty($search_year)) {
176  $sql .= ' AND YEAR(f.datef) IN ('.$db->sanitize($search_year).')';
177  }
178  if (empty($user->rights->societe->client->voir) && !$socid) {
179  $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
180  }
181  if ($socid) {
182  $sql .= " AND f.fk_soc = ".((int) $socid);
183  }
184  $sql .= $db->order($sortfield, $sortorder);
185 
186  // Calcul total qty and amount for global if full scan list
187  $total_ht = 0;
188  $total_qty = 0;
189 
190  // Count total nb of records
191  $totalofrecords = '';
192  if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST)) {
193  $result = $db->query($sql);
194  $totalofrecords = $db->num_rows($result);
195  }
196 
197  $sql .= $db->plimit($limit + 1, $offset);
198 
199  $result = $db->query($sql);
200  if ($result) {
201  $num = $db->num_rows($result);
202 
203  $option .= '&id='.$product->id;
204 
205  if ($limit > 0 && $limit != $conf->liste_limit) {
206  $option .= '&limit='.urlencode($limit);
207  }
208  if (!empty($search_month)) {
209  $option .= '&search_month='.urlencode($search_month);
210  }
211  if (!empty($search_year)) {
212  $option .= '&search_year='.urlencode($search_year);
213  }
214 
215  print '<form method="post" action="'.$_SERVER ['PHP_SELF'].'?id='.$product->id.'" name="search_form">'."\n";
216  print '<input type="hidden" name="token" value="'.newToken().'">';
217  if (!empty($sortfield)) {
218  print '<input type="hidden" name="sortfield" value="'.$sortfield.'"/>';
219  }
220  if (!empty($sortorder)) {
221  print '<input type="hidden" name="sortorder" value="'.$sortorder.'"/>';
222  }
223 
224  print_barre_liste($langs->trans("CustomersInvoices"), $page, $_SERVER["PHP_SELF"], $option, $sortfield, $sortorder, '', $num, $totalofrecords, '', 0, '', '', $limit, 0, 0, 1);
225 
226  if (!empty($page)) {
227  $option .= '&page='.urlencode($page);
228  }
229 
230  print '<div class="liste_titre liste_titre_bydiv centpercent">';
231  print '<div class="divsearchfield">';
232  print $langs->trans('Period').' ('.$langs->trans("DateInvoice").') - ';
233  print $langs->trans('Month').':<input class="flat" type="text" size="4" name="search_month" value="'.$search_month.'"> ';
234  print $langs->trans('Year').':'.$formother->selectyear($search_year ? $search_year : - 1, 'search_year', 1, 20, 5);
235  print '<div style="vertical-align: middle; display: inline-block">';
236  print '<input type="image" class="liste_titre" name="button_search" src="'.img_picto($langs->trans("Search"), 'search.png', '', '', 1).'" value="'.dol_escape_htmltag($langs->trans("Search")).'" title="'.dol_escape_htmltag($langs->trans("Search")).'">';
237  print '<input type="image" class="liste_titre" name="button_removefilter" src="'.img_picto($langs->trans("Search"), 'searchclear.png', '', '', 1).'" value="'.dol_escape_htmltag($langs->trans("RemoveFilter")).'" title="'.dol_escape_htmltag($langs->trans("RemoveFilter")).'">';
238  print '</div>';
239  print '</div>';
240  print '</div>';
241 
242  $i = 0;
243  print '<div class="div-table-responsive">';
244  print '<table class="tagtable liste listwithfilterbefore" width="100%">';
245  print '<tr class="liste_titre">';
246  print_liste_field_titre("Ref", $_SERVER["PHP_SELF"], "s.rowid", "", $option, '', $sortfield, $sortorder);
247  print_liste_field_titre("Company", $_SERVER["PHP_SELF"], "s.nom", "", $option, '', $sortfield, $sortorder);
248  print_liste_field_titre("CustomerCode", $_SERVER["PHP_SELF"], "s.code_client", "", $option, '', $sortfield, $sortorder);
249  print_liste_field_titre("DateInvoice", $_SERVER["PHP_SELF"], "f.datef", "", $option, 'align="center"', $sortfield, $sortorder);
250  print_liste_field_titre("Qty", $_SERVER["PHP_SELF"], "d.qty", "", $option, 'align="center"', $sortfield, $sortorder);
251  print_liste_field_titre("AmountHT", $_SERVER["PHP_SELF"], "d.total_ht", "", $option, 'align="right"', $sortfield, $sortorder);
252  print_liste_field_titre("Status", $_SERVER["PHP_SELF"], "f.paye,f.fk_statut", "", $option, 'align="right"', $sortfield, $sortorder);
253  print "</tr>\n";
254 
255  if ($num > 0) {
256  while ($i < min($num, $limit)) {
257  $objp = $db->fetch_object($result);
258 
259  if ($objp->type == Facture::TYPE_CREDIT_NOTE) {
260  $objp->qty = -($objp->qty);
261  }
262 
263  $total_ht += $objp->total_ht;
264  $total_qty += $objp->qty;
265 
266  $invoicestatic->id = $objp->facid;
267  $invoicestatic->ref = $objp->ref;
268  $societestatic->fetch($objp->socid);
269  $paiement = $invoicestatic->getSommePaiement();
270 
271  print '<tr class="oddeven">';
272  print '<td>';
273  print $invoicestatic->getNomUrl(1);
274  print "</td>\n";
275  print '<td>'.$societestatic->getNomUrl(1).'</td>';
276  print "<td>".$objp->code_client."</td>\n";
277  print '<td class="center">';
278  print dol_print_date($db->jdate($objp->datef), 'dayhour')."</td>";
279  print '<td class="center">'.$objp->qty."</td>\n";
280  print '<td align="right">'.price($objp->total_ht)."</td>\n";
281  print '<td align="right">'.$invoicestatic->LibStatut($objp->paye, $objp->statut, 5, $paiement, $objp->type).'</td>';
282  print "</tr>\n";
283  $i++;
284  }
285  }
286  print '<tr class="liste_total">';
287  if ($num < $limit) {
288  print '<td class="left">'.$langs->trans("Total").'</td>';
289  } else {
290  print '<td class="left">'.$langs->trans("Totalforthispage").'</td>';
291  }
292  print '<td colspan="3"></td>';
293  print '<td class="center">'.$total_qty.'</td>';
294  print '<td align="right">'.price($total_ht).'</td>';
295  print '<td></td>';
296  print "</table>";
297  print '</div>';
298  print '</form>';
299  } else {
300  dol_print_error($db);
301  }
302  $db->free($result);
303  }
304  }
305 } else {
306  dol_print_error();
307 }
308 
309 // End of page
310 llxFooter();
311 $db->close();
Societe
Class to manage third parties objects (customers, suppliers, prospects...)
Definition: societe.class.php:48
dol_escape_htmltag
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0)
Returns text escaped for inclusion in HTML alt or title tags, or into values of HTML input fields.
Definition: functions.lib.php:1468
dol_trunc
dol_trunc($string, $size=40, $trunc='right', $stringencoding='UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding '…' if string larger than length.
Definition: functions.lib.php:3805
restrictedArea
restrictedArea($user, $features, $objectid=0, $tableandshare='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $isdraft=0, $mode=0)
Check permissions of a user to show a page and an object.
Definition: security.lib.php:234
llxFooter
llxFooter()
Empty footer.
Definition: wrapper.php:73
GETPOST
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Definition: functions.lib.php:484
dol_print_error
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
Definition: functions.lib.php:4844
product_prepare_head
product_prepare_head($object)
Prepare array with list of tabs.
Definition: product.lib.php:35
$form
if($cancel &&! $id) if($action=='add' &&! $cancel) if($action=='delete') if($id) $form
Actions.
Definition: card.php:142
FormOther
Classe permettant la generation de composants html autre Only common components are here.
Definition: html.formother.class.php:39
Facture
Class to manage invoices.
Definition: facture.class.php:60
dol_banner_tab
dol_banner_tab($object, $paramid, $morehtml='', $shownav=1, $fieldid='rowid', $fieldref='ref', $morehtmlref='', $moreparam='', $nodbprefix=0, $morehtmlleft='', $morehtmlstatus='', $onlybanner=0, $morehtmlright='')
Show tab footer of a card.
Definition: functions.lib.php:2046
dol_print_date
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
Definition: functions.lib.php:2514
print_barre_liste
print_barre_liste($titre, $page, $file, $options='', $sortfield='', $sortorder='', $morehtmlcenter='', $num=-1, $totalnboflines='', $picto='generic', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limit=-1, $hideselectlimit=0, $hidenavigation=0, $pagenavastextinput=0, $morehtmlrightbeforearrow='')
Print a title with navigation controls for pagination.
Definition: functions.lib.php:5257
dol_get_fiche_head
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='')
Show tabs of a record.
Definition: functions.lib.php:1822
dol_get_fiche_end
dol_get_fiche_end($notab=0)
Return tab footer of a card.
Definition: functions.lib.php:2018
Facture\TYPE_CREDIT_NOTE
const TYPE_CREDIT_NOTE
Credit note invoice.
Definition: facture.class.php:392
GETPOSTISSET
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname from a POST of a form.
Definition: functions.lib.php:386
print_liste_field_titre
print_liste_field_titre($name, $file="", $field="", $begin="", $moreparam="", $moreattrib="", $sortfield="", $sortorder="", $prefix="", $tooltip="", $forcenowrapcolumntitle=0)
Show title line of an array.
Definition: functions.lib.php:5026
Product
Class to manage products or services.
Definition: product.class.php:46
Form
Class to manage generation of HTML components Only common components must be here.
Definition: html.form.class.php:52
show_stats_for_company
show_stats_for_company($product, $socid)
Show stats for company.
Definition: product.lib.php:361
Product\TYPE_SERVICE
const TYPE_SERVICE
Service.
Definition: product.class.php:504
setEventMessages
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='')
Set event messages in dol_events session object.
Definition: functions.lib.php:8137
Product\TYPE_PRODUCT
const TYPE_PRODUCT
Regular product.
Definition: product.class.php:500
llxHeader
if(!defined('NOREQUIRESOC')) if(!defined('NOREQUIRETRAN')) if(!defined('NOCSRFCHECK')) if(!defined('NOTOKENRENEWAL')) if(!defined('NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined('NOREQUIREAJAX')) llxHeader()
Empty header.
Definition: wrapper.php:59