1<?php
2/* Copyright (C) 2004-2007 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3 * Copyright (C) 2004-2009 Laurent Destailleur  <eldy@users.sourceforge.net>
4 * Copyright (C) 2005-2012 Regis Houssin        <regis.houssin@inodbox.com>
5 * Copyright (C) 2014	   Florian Henry		<florian.henry@open-concept.pro>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21/**
22 * \file 	htdocs/product/stats/supplier_proposal.php
23 * \ingroup product service propal
24 * \brief 	Page des stats des propals pour un produit
25 */
26
27require '../../main.inc.php';
28require_once DOL_DOCUMENT_ROOT.'/core/lib/product.lib.php';
29require_once DOL_DOCUMENT_ROOT.'/supplier_proposal/class/supplier_proposal.class.php';
30require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
31require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
32
33// Load translation files required by the page
34$langs->loadLangs(array('products', 'companies', 'supplier_proposal'));
35
36$id = GETPOST('id', 'int');
37$ref = GETPOST('ref', 'alpha');
38
39// Security check
40$fieldvalue = (!empty($id) ? $id : (!empty($ref) ? $ref : ''));
41$fieldtype = (!empty($ref) ? 'ref' : 'rowid');
42$socid = '';
43if (!empty($user->socid)) {
44	$socid = $user->socid;
45}
46
47// Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
48$hookmanager->initHooks(array('productstatssupplierpropal'));
49
50// Load variable for pagination
51$limit = GETPOST('limit', 'int') ?GETPOST('limit', 'int') : $conf->liste_limit;
52$sortfield = GETPOST('sortfield', 'aZ09comma');
53$sortorder = GETPOST('sortorder', 'aZ09comma');
54$page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
55if (empty($page) || $page == -1) {
56	$page = 0;
57}     // If $page is not defined, or '' or -1
58$offset = $limit * $page;
59$pageprev = $page - 1;
60$pagenext = $page + 1;
61if (!$sortorder) {
62	$sortorder = "DESC";
63}
64if (!$sortfield) {
65	$sortfield = "p.date_valid";
66}
67
68$search_month = GETPOST('search_month', 'int');
69$search_year = GETPOST('search_year', 'int');
70
71if (GETPOST('button_removefilter_x', 'alpha') || GETPOST('button_removefilter', 'alpha')) {
72	$search_month = '';
73	$search_year = '';
74}
75
76$result = restrictedArea($user, 'produit|service', $fieldvalue, 'product&product', '', '', $fieldtype);
77
78
79/*
80 * View
81 */
82
83$propalstatic = new SupplierProposal($db);
84$societestatic = new Societe($db);
85
86$form = new Form($db);
87$formother = new FormOther($db);
88
89if ($id > 0 || !empty($ref)) {
90	$product = new Product($db);
91	$result = $product->fetch($id, $ref);
92
93	$object = $product;
94
95	$parameters = array('id' => $id);
96	$reshook = $hookmanager->executeHooks('doActions', $parameters, $product, $action); // Note that $action and $object may have been modified by some hooks
97	if ($reshook < 0) {
98		setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
99	}
100
101	llxHeader("", "", $langs->trans("CardProduct".$product->type));
102
103	if ($result > 0) {
104		$head = product_prepare_head($product);
105		$titre = $langs->trans("CardProduct".$product->type);
106		$picto = ($product->type == Product::TYPE_SERVICE ? 'service' : 'product');
107		print dol_get_fiche_head($head, 'referers', $titre, -1, $picto);
108
109		$reshook = $hookmanager->executeHooks('formObjectOptions', $parameters, $product, $action); // Note that $action and $object may have been modified by hook
110		print $hookmanager->resPrint;
111		if ($reshook < 0) {
112			setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
113		}
114
115		$linkback = '<a href="'.DOL_URL_ROOT.'/product/list.php?restore_lastsearch_values=1">'.$langs->trans("BackToList").'</a>';
116
117		$shownav = 1;
118		if ($user->socid && !in_array('product', explode(',', $conf->global->MAIN_MODULES_FOR_EXTERNAL))) {
119			$shownav = 0;
120		}
121
122		dol_banner_tab($object, 'ref', $linkback, $shownav, 'ref');
123
124		print '<div class="fichecenter">';
125
126		print '<div class="underbanner clearboth"></div>';
127		print '<table class="border tableforfield" width="100%">';
128
129		$nboflines = show_stats_for_company($product, $socid);
130
131		print "</table>";
132
133		print '</div>';
134		print '<div style="clear:both"></div>';
135
136		print dol_get_fiche_end();
137
138
139		if ($user->rights->propale->lire) {
140			$sql = "SELECT DISTINCT s.nom as name, s.rowid as socid, p.rowid as propalid, p.ref, d.total_ht as amount,";
141			//$sql .= " p.ref_supplier,";
142			$sql .= "p.date_valid, p.fk_statut as statut, d.rowid, d.qty";
143			if (!$user->rights->societe->client->voir && !$socid) {
144				$sql .= ", sc.fk_soc, sc.fk_user ";
145			}
146			$sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
147			$sql .= ",".MAIN_DB_PREFIX."supplier_proposal as p";
148			$sql .= ", ".MAIN_DB_PREFIX."supplier_proposaldet as d";
149			if (!$user->rights->societe->client->voir && !$socid) {
150				$sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
151			}
152			$sql .= " WHERE p.fk_soc = s.rowid";
153			$sql .= " AND p.entity IN (".getEntity('supplier_proposal').")";
154			$sql .= " AND d.fk_supplier_proposal = p.rowid";
155			$sql .= " AND d.fk_product = ".((int) $product->id);
156			if (!empty($search_month)) {
157				$sql .= ' AND MONTH(p.datep) IN ('.$db->sanitize($search_month).')';
158			}
159			if (!empty($search_year)) {
160				$sql .= ' AND YEAR(p.datep) IN ('.$db->sanitize($search_year).')';
161			}
162			if (!$user->rights->societe->client->voir && !$socid) {
163				$sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
164			}
165			if ($socid) {
166				$sql .= " AND p.fk_soc = ".((int) $socid);
167			}
168			$sql .= $db->order($sortfield, $sortorder);
169
170			// Calcul total qty and amount for global if full scan list
171			$total_ht = 0;
172			$total_qty = 0;
173
174			// Count total nb of records
175			$totalofrecords = '';
176			if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST)) {
177				$result = $db->query($sql);
178				$totalofrecords = $db->num_rows($result);
179			}
180
181			$sql .= $db->plimit($limit + 1, $offset);
182
183			$result = $db->query($sql);
184			if ($result) {
185				$num = $db->num_rows($result);
186
187				if ($limit > 0 && $limit != $conf->liste_limit) {
188					$option .= '&limit='.urlencode($limit);
189				}
190				if (!empty($id)) {
191					$option .= '&id='.$product->id;
192				}
193				if (!empty($search_month)) {
194					$option .= '&search_month='.urlencode($search_month);
195				}
196				if (!empty($search_year)) {
197					$option .= '&search_year='.urlencode($search_year);
198				}
199
200				print '<form method="post" action="'.$_SERVER['PHP_SELF'].'?id='.$product->id.'" name="search_form">'."\n";
201				if (!empty($sortfield)) {
202					print '<input type="hidden" name="sortfield" value="'.$sortfield.'"/>';
203				}
204				if (!empty($sortorder)) {
205					print '<input type="hidden" name="sortorder" value="'.$sortorder.'"/>';
206				}
207
208				print_barre_liste($langs->trans("Proposals"), $page, $_SERVER["PHP_SELF"], $option, $sortfield, $sortorder, '', $num, $totalofrecords, '', 0, '', '', $limit, 0, 0, 1);
209
210				if (!empty($page)) {
211					$option .= '&page='.urlencode($page);
212				}
213
214				print '<div class="liste_titre liste_titre_bydiv centpercent">';
215				print '<div class="divsearchfield">';
216				print $langs->trans('Period').' ('.$langs->trans("DatePropal").') - ';
217				print $langs->trans('Month').':<input class="flat" type="text" size="4" name="search_month" value="'.$search_month.'"> ';
218				print $langs->trans('Year').':'.$formother->selectyear($search_year ? $search_year : - 1, 'search_year', 1, 20, 5);
219				print '<div style="vertical-align: middle; display: inline-block">';
220				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")).'">';
221				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")).'">';
222				print '</div>';
223				print '</div>';
224				print '</div>';
225
226				$i = 0;
227				print '<div class="div-table-responsive">';
228				print '<table class="tagtable liste listwithfilterbefore" width="100%">';
229				print '<tr class="liste_titre">';
230				print_liste_field_titre("Ref", $_SERVER["PHP_SELF"], "p.rowid", "", $option, '', $sortfield, $sortorder);
231				print_liste_field_titre("Company", $_SERVER["PHP_SELF"], "s.nom", "", $option, '', $sortfield, $sortorder);
232				print_liste_field_titre("DatePropal", $_SERVER["PHP_SELF"], "p.date_valid", "", $option, 'align="center"', $sortfield, $sortorder);
233				print_liste_field_titre("Qty", $_SERVER["PHP_SELF"], "d.qty", "", $option, 'align="center"', $sortfield, $sortorder);
234				print_liste_field_titre("AmountHT", $_SERVER["PHP_SELF"], "p.total_ht", "", $option, 'align="right"', $sortfield, $sortorder);
235				print_liste_field_titre("Status", $_SERVER["PHP_SELF"], "p.fk_statut", "", $option, 'align="right"', $sortfield, $sortorder);
236				print "</tr>\n";
237
238				if ($num > 0) {
239					while ($i < min($num, $limit)) {
240						$objp = $db->fetch_object($result);
241
242						$total_ht += $objp->amount;
243						$total_qty += $objp->qty;
244
245						$propalstatic->id = $objp->propalid;
246						$propalstatic->ref = $objp->ref;
247						$societestatic->fetch($objp->socid);
248
249						print '<tr class="oddeven">';
250						print '<td>';
251						print $propalstatic->getNomUrl(1);
252						print "</td>\n";
253						print '<td>'.$societestatic->getNomUrl(1).'</td>';
254						print '<td class="center">';
255						print dol_print_date($db->jdate($objp->date_valid), 'dayhour')."</td>";
256						print "<td align=\"center\">".$objp->qty."</td>\n";
257						print '<td align="right">'.price($objp->amount).'</td>'."\n";
258						print '<td align="right">'.$propalstatic->LibStatut($objp->statut, 5).'</td>';
259						print "</tr>\n";
260						$i++;
261					}
262				}
263
264				print '<tr class="liste_total">';
265				if ($num < $limit) {
266					print '<td class="left">'.$langs->trans("Total").'</td>';
267				} else {
268					print '<td class="left">'.$langs->trans("Totalforthispage").'</td>';
269				}
270				print '<td colspan="2"></td>';
271				print '<td class="center">'.$total_qty.'</td>';
272				print '<td align="right">'.price($total_ht).'</td>';
273				print '<td></td>';
274				print "</table>";
275				print '</div>';
276				print '</form>';
277			} else {
278				dol_print_error($db);
279			}
280			$db->free($result);
281		}
282	}
283} else {
284	dol_print_error();
285}
286
287// End of page
288llxFooter();
289$db->close();
290