1<?php
2/* Copyright (C) 2003-2005 Rodolphe Quiedeville     <rodolphe@quiedeville.org>
3 * Copyright (C) 2004-2011 Laurent Destailleur      <eldy@users.sourceforge.net>
4 * Copyright (C) 2005-2012 Regis Houssin            <regis.houssin@capnetworks.com>
5 * Copyright (C) 2018	   Quentin Vial-Gouteyron   <quentin.vial-gouteyron@atm-consulting.fr>
6 * Copyright (C) 2019      Nicolas ZABOURI          <info@inovea-conseil.com>
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
22/**
23 *       \file       htdocs/reception/index.php
24 *       \ingroup    reception
25 *       \brief      Home page of reception area.
26 */
27
28require '../main.inc.php';
29require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.commande.class.php';
30require_once DOL_DOCUMENT_ROOT.'/reception/class/reception.class.php';
31
32$hookmanager = new HookManager($db);
33
34// Initialize technical object to manage hooks. Note that conf->hooks_modules contains array
35$hookmanager->initHooks(array('receptionindex'));
36
37$langs->loadLangs(array("orders", "receptions"));
38
39$reception = new Reception($db);
40
41// Security check
42if ($user->socid) {
43	$socid = $user->socid;
44}
45$result = restrictedArea($user, 'reception', 0, '');
46
47
48/*
49 *	View
50 */
51
52$orderstatic = new CommandeFournisseur($db);
53$companystatic = new Societe($db);
54
55$helpurl = 'EN:Module_Receptions|FR:Module_Receptions|ES:M&oacute;dulo_Receptiones';
56llxHeader('', $langs->trans("Reception"), $helpurl);
57
58print load_fiche_titre($langs->trans("ReceptionsArea"), '', 'dollyrevert');
59
60
61print '<div class="fichecenter"><div class="fichethirdleft">';
62
63
64if (!empty($conf->global->MAIN_SEARCH_FORM_ON_HOME_AREAS)) {     // This may be useless due to the global search combo
65	print '<form method="post" action="list.php">';
66	print '<input type="hidden" name="token" value="'.newToken().'">';
67	print '<div class="div-table-responsive-no-min">';
68	print '<table class="noborder nohover centpercent">';
69	print '<tr class="liste_titre"><td colspan="3">'.$langs->trans("Search").'</td></tr>';
70	print '<tr class="oddeven"><td>';
71	print $langs->trans("Reception").':</td><td><input type="text" class="flat" name="sall" size="18"></td><td><input type="submit" value="'.$langs->trans("Search").'" class="button"></td></tr>';
72	print "</table></div></form><br>\n";
73}
74
75
76/*
77 * Draft receptions
78 */
79
80$clause = " WHERE ";
81
82$sql = "SELECT e.rowid, e.ref, e.ref_supplier,";
83$sql .= " s.nom as name, s.rowid as socid,";
84$sql .= " c.ref as commande_fournisseur_ref, c.rowid as commande_fournisseur_id";
85$sql .= " FROM ".MAIN_DB_PREFIX."reception as e";
86$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."element_element as el ON e.rowid = el.fk_target AND el.targettype = 'reception'";
87$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."commande_fournisseur as c ON el.fk_source = c.rowid";
88$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = e.fk_soc";
89if (!$user->rights->societe->client->voir && !$socid) {
90	$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON e.fk_soc = sc.fk_soc";
91	$sql .= $clause." sc.fk_user = ".((int) $user->id);
92	$clause = " AND ";
93}
94$sql .= $clause." e.fk_statut = 0";
95$sql .= " AND e.entity IN (".getEntity('reception').")";
96if ($socid) {
97	$sql .= " AND c.fk_soc = ".((int) $socid);
98}
99
100$resql = $db->query($sql);
101if ($resql) {
102	print '<div class="div-table-responsive-no-min">';
103	print '<table class="noborder centpercent">';
104	print '<tr class="liste_titre">';
105	print '<th colspan="3">'.$langs->trans("ReceptionsToValidate").'</th></tr>';
106
107	$num = $db->num_rows($resql);
108	if ($num) {
109		$i = 0;
110		while ($i < $num) {
111			$obj = $db->fetch_object($resql);
112
113			$reception->id = $obj->rowid;
114			$reception->ref = $obj->ref;
115			$reception->ref_supplier = $obj->ref_supplier;
116
117			print '<tr class="oddeven"><td class="nowrap">';
118			print $reception->getNomUrl(1);
119			print "</td>";
120			print '<td>';
121			print '<a href="'.DOL_URL_ROOT.'/comm/card.php?socid='.$obj->socid.'">'.$obj->name.'</a>';
122			print '</td>';
123			print '<td>';
124			if ($obj->commande_fournisseur_id) {
125				print '<a href="'.DOL_URL_ROOT.'/commande_fournisseur/card.php?id='.$obj->commande_fournisseur_id.'">'.$obj->commande_fournisseur_ref.'</a>';
126			}
127			print '</td></tr>';
128			$i++;
129		}
130	} else {
131		print '<tr><td><span class="opacitymedium">'.$langs->trans("None").'</span></td><td></td><td></td></tr>';
132	}
133
134	print "</table></div><br>";
135}
136
137
138//print '</td><td valign="top" width="70%">';
139print '</div><div class="fichetwothirdright"><div class="ficheaddleft">';
140
141$max = 5;
142
143/*
144 * Latest receptions
145 */
146
147$sql = "SELECT e.rowid, e.ref, e.ref_supplier,";
148$sql .= " s.nom as name, s.rowid as socid,";
149$sql .= " c.ref as commande_fournisseur_ref, c.rowid as commande_fournisseur_id";
150$sql .= " FROM ".MAIN_DB_PREFIX."reception as e";
151$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."element_element as el ON e.rowid = el.fk_target AND el.targettype = 'reception' AND el.sourcetype IN ('order_supplier')";
152$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."commande_fournisseur as c ON el.fk_source = c.rowid AND el.sourcetype IN ('order_supplier') AND el.targettype = 'reception'";
153$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = e.fk_soc";
154if (!$user->rights->societe->client->voir && !$socid) {
155	$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON e.fk_soc = sc.fk_soc";
156}
157$sql .= " WHERE e.entity IN (".getEntity('reception').")";
158if (!$user->rights->societe->client->voir && !$socid) {
159	$sql .= " AND sc.fk_user = ".((int) $user->id);
160}
161$sql .= " AND e.fk_statut = 1";
162if ($socid) {
163	$sql .= " AND c.fk_soc = ".((int) $socid);
164}
165$sql .= " ORDER BY e.date_delivery DESC";
166$sql .= $db->plimit($max, 0);
167
168$resql = $db->query($sql);
169if ($resql) {
170	$num = $db->num_rows($resql);
171	if ($num) {
172		$i = 0;
173		print '<div class="div-table-responsive-no-min">';
174		print '<table class="noborder centpercent">';
175		print '<tr class="liste_titre">';
176		print '<th colspan="3">'.$langs->trans("LastReceptions", $num).'</th></tr>';
177		while ($i < $num) {
178			$obj = $db->fetch_object($resql);
179
180			$reception->id = $obj->rowid;
181			$reception->ref = $obj->ref;
182			$reception->ref_supplier = $obj->ref_supplier;
183
184			print '<tr class="oddeven"><td>';
185			print $reception->getNomUrl(1);
186			print '</td>';
187			print '<td><a href="'.DOL_URL_ROOT.'/comm/card.php?socid='.$obj->socid.'">'.img_object($langs->trans("ShowCompany"), "company").' '.$obj->name.'</a></td>';
188			print '<td>';
189			if ($obj->commande_fournisseur_id > 0) {
190				$orderstatic->id = $obj->commande_fournisseur_id;
191				$orderstatic->ref = $obj->commande_fournisseur_ref;
192				print $orderstatic->getNomUrl(1);
193			} else {
194				print '&nbsp;';
195			}
196			print '</td></tr>';
197			$i++;
198		}
199		print "</table></div><br>";
200	}
201	$db->free($resql);
202} else {
203	dol_print_error($db);
204}
205
206
207
208/*
209 * Open pruchase orders to process
210 */
211
212$sql = "SELECT c.rowid, c.ref, c.ref_supplier as ref_supplier, c.fk_statut as status, c.billed as billed, s.nom as name, s.rowid as socid";
213$sql .= " FROM ".MAIN_DB_PREFIX."commande_fournisseur as c,";
214$sql .= " ".MAIN_DB_PREFIX."societe as s";
215if (!$user->rights->societe->client->voir && !$socid) {
216	$sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
217}
218$sql .= " WHERE c.fk_soc = s.rowid";
219$sql .= " AND c.entity IN (".getEntity('supplier_order').")";
220$sql .= " AND c.fk_statut IN (".CommandeFournisseur::STATUS_ORDERSENT.", ".CommandeFournisseur::STATUS_RECEIVED_PARTIALLY.")";
221if ($socid > 0) {
222	$sql .= " AND c.fk_soc = ".((int) $socid);
223}
224if (!$user->rights->societe->client->voir && !$socid) {
225	$sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
226}
227$sql .= " ORDER BY c.rowid ASC";
228$resql = $db->query($sql);
229if ($resql) {
230	$num = $db->num_rows($resql);
231	if ($num) {
232		$langs->load("orders");
233
234		$i = 0;
235		print '<div class="div-table-responsive-no-min">';
236		print '<table class="noborder centpercent">';
237		print '<tr class="liste_titre">';
238		print '<th colspan="3">'.$langs->trans("SuppliersOrdersToProcess").' <span class="badge">'.$num.'</span></th></tr>';
239		while ($i < $num) {
240			$obj = $db->fetch_object($resql);
241
242			$orderstatic->id = $obj->rowid;
243			$orderstatic->ref = $obj->ref;
244			$orderstatic->ref_supplier = $obj->ref_supplier;
245			$orderstatic->statut = $obj->status;
246			$orderstatic->facturee = $obj->billed;
247
248			$companystatic->name = $obj->name;
249			$companystatic->id = $obj->socid;
250
251			print '<tr class="oddeven">';
252			print '<td class="nowrap">';
253			print $orderstatic->getNomUrl(1);
254			print '</td>';
255			print '<td>';
256			print $companystatic->getNomUrl(1, 'customer', 32);
257			print '</td>';
258			print '<td class="right">';
259			print $orderstatic->getLibStatut(3);
260			print '</td>';
261			print '</tr>';
262			$i++;
263		}
264		print "</table></div><br>";
265	}
266}
267
268print '</div></div></div>';
269
270$parameters = array('user' => $user);
271$reshook = $hookmanager->executeHooks('dashboardWarehouseReceptions', $parameters, $object); // Note that $action and $object may have been modified by hook
272
273llxFooter();
274$db->close();
275