1<?php
2/* Copyright (C) 2003-2004	Rodolphe Quiedeville	<rodolphe@quiedeville.org>
3 * Copyright (C) 2004-2017	Laurent Destailleur		<eldy@users.sourceforge.net>
4 * Copyright (C) 2005-2012	Regis Houssin			<regis.houssin@inodbox.com>
5 * Copyright (C) 2019		Nicolas ZABOURI			<info@inovea-conseil.com>
6 * Copyright (C) 2020		Tobias Sekan			<tobias.sekan@startmail.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/comm/propal/index.php
24 *	\ingroup	propal
25 *	\brief	Home page of proposal area
26 */
27
28require '../../main.inc.php';
29require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
30require_once DOL_DOCUMENT_ROOT.'/comm/propal/class/propal.class.php';
31require_once DOL_DOCUMENT_ROOT.'/core/lib/propal.lib.php';
32
33// Initialize technical object to manage hooks. Note that conf->hooks_modules contains array
34$hookmanager = new HookManager($db);
35$hookmanager->initHooks(array('proposalindex'));
36
37// Load translation files required by the page
38$langs->loadLangs(array('propal', 'companies'));
39
40$now = dol_now();
41$max = 5;
42
43// Security check
44$socid = GETPOST('socid', 'int');
45if (isset($user->socid) && $user->socid > 0) {
46	$action = '';
47	$socid = $user->socid;
48}
49
50restrictedArea($user, 'propal');
51
52
53/*
54 * View
55 */
56
57$propalstatic = new Propal($db);
58$companystatic = new Societe($db);
59$form = new Form($db);
60$formfile = new FormFile($db);
61$help_url = "EN:Module_Commercial_Proposals|FR:Module_Propositions_commerciales|ES:Módulo_Presupuestos";
62
63llxHeader("", $langs->trans("ProspectionArea"), $help_url);
64
65print load_fiche_titre($langs->trans("ProspectionArea"), '', 'propal');
66
67print '<div class="fichecenter">';
68print '<div class="fichethirdleft">';
69
70$tmp = getCustomerProposalPieChart($socid);
71if ($tmp) {
72	print $tmp;
73	print '<br>';
74}
75
76/*
77 * Draft proposals
78 */
79if (!empty($conf->propal->enabled)) {
80	$sql = "SELECT p.rowid, p.ref, p.ref_client, p.total_ht, p.total_tva, p.total_ttc";
81	$sql .= ", s.rowid as socid, s.nom as name, s.client, s.canvas, s.code_client, s.email, s.entity, s.code_compta";
82	$sql .= " FROM ".MAIN_DB_PREFIX."propal as p";
83	$sql .= ", ".MAIN_DB_PREFIX."societe as s";
84	if (!$user->rights->societe->client->voir && !$socid) {
85		$sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
86	}
87	$sql .= " WHERE p.entity IN (".getEntity($propalstatic->element).")";
88	$sql .= " AND p.fk_soc = s.rowid";
89	$sql .= " AND p.fk_statut =".Propal::STATUS_DRAFT;
90	if (!$user->rights->societe->client->voir && !$socid) {
91		$sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
92	}
93	if ($socid) {
94		$sql .= " AND p.fk_soc = ".((int) $socid);
95	}
96
97	$resql = $db->query($sql);
98	if ($resql) {
99		$num = $db->num_rows($resql);
100		$nbofloop = min($num, (empty($conf->global->MAIN_MAXLIST_OVERLOAD) ? 500 : $conf->global->MAIN_MAXLIST_OVERLOAD));
101		startSimpleTable("DraftPropals", "comm/propal/list.php", "search_status=".Propal::STATUS_DRAFT, 2, $num);
102
103		if ($num) {
104			$total = 0;
105			$i = 0;
106
107			while ($i < $nbofloop) {
108				$obj = $db->fetch_object($resql);
109
110				$propalstatic->id = $obj->rowid;
111				$propalstatic->ref = $obj->ref;
112				$propalstatic->ref_client = $obj->ref_client;
113				$propalstatic->total_ht = $obj->total_ht;
114				$propalstatic->total_tva = $obj->total_tva;
115				$propalstatic->total_ttc = $obj->total_ttc;
116
117				$companystatic->id = $obj->socid;
118				$companystatic->name = $obj->name;
119				$companystatic->client = $obj->client;
120				$companystatic->code_client = $obj->code_client;
121				$companystatic->code_fournisseur = $obj->code_fournisseur;
122				$companystatic->canvas = $obj->canvas;
123				$companystatic->entity = $obj->entity;
124				$companystatic->email = $obj->email;
125				$companystatic->code_compta = $obj->code_compta;
126
127				print '<tr class="oddeven">';
128				print '<td class="nowrap">'.$propalstatic->getNomUrl(1).'</td>';
129				print '<td class="nowrap">'.$companystatic->getNomUrl(1, 'customer', 16).'</td>';
130				print '<td class="nowrap right">'.price(!empty($conf->global->MAIN_DASHBOARD_USE_TOTAL_HT) ? $obj->total_ht : $obj->total_ttc).'</td>';
131				print '</tr>';
132
133				$i++;
134				$total += (!empty($conf->global->MAIN_DASHBOARD_USE_TOTAL_HT) ? $obj->total_ht : $obj->total_ttc);
135			}
136		}
137
138		addSummaryTableLine(3, $num, $nbofloop, $total, "NoProposal");
139		finishSimpleTable(true);
140		$db->free($resql);
141	} else {
142		dol_print_error($db);
143	}
144}
145
146print '</div>';
147
148print '<div class="fichetwothirdright">';
149print '<div class="ficheaddleft">';
150
151/*
152 * Last modified proposals
153 */
154
155$sql = "SELECT c.rowid, c.entity, c.ref, c.fk_statut, date_cloture as datec";
156$sql .= ", s.nom as socname, s.rowid as socid, s.canvas, s.client, s.email, s.code_compta";
157$sql .= " FROM ".MAIN_DB_PREFIX."propal as c";
158$sql .= ", ".MAIN_DB_PREFIX."societe as s";
159if (!$user->rights->societe->client->voir && !$socid) {
160	$sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
161}
162$sql .= " WHERE c.entity IN (".getEntity($propalstatic->element).")";
163$sql .= " AND c.fk_soc = s.rowid";
164//$sql.= " AND c.fk_statut > 2";
165if ($socid) {
166	$sql .= " AND c.fk_soc = ".((int) $socid);
167}
168if (!$user->rights->societe->client->voir && !$socid) {
169	$sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
170}
171$sql .= " ORDER BY c.tms DESC";
172$sql .= $db->plimit($max, 0);
173
174$resql = $db->query($sql);
175if ($resql) {
176	$num = $db->num_rows($resql);
177	startSimpleTable($langs->trans("LastModifiedProposals", $max), "", "", 3);
178
179	if ($num) {
180		$i = 0;
181		while ($i < $num) {
182			$obj = $db->fetch_object($resql);
183
184			$propalstatic->id = $obj->rowid;
185			$propalstatic->ref = $obj->ref;
186
187			$companystatic->id = $obj->socid;
188			$companystatic->name = $obj->socname;
189			$companystatic->client = $obj->client;
190			$companystatic->canvas = $obj->canvas;
191			$companystatic->email = $obj->email;
192			$companystatic->code_compta = $obj->code_compta;
193
194			$filename = dol_sanitizeFileName($obj->ref);
195			$filedir = $conf->propal->multidir_output[$obj->entity].'/'.dol_sanitizeFileName($obj->ref);
196			$urlsource = $_SERVER['PHP_SELF'].'?id='.$obj->rowid;
197
198			print '<tr class="oddeven">';
199
200			print '<td width="20%" class="nowrap">';
201			print '<table class="nobordernopadding">';
202			print '<tr class="nocellnopadd">';
203			print '<td width="96" class="nobordernopadding nowrap">'.$propalstatic->getNomUrl(1).'</td>';
204			print '<td width="16" class="nobordernopadding nowrap"></td>';
205			print '<td width="16" class="nobordernopadding right">'.$formfile->getDocumentsLink($propalstatic->element, $filename, $filedir).'</td>';
206			print '</tr>';
207			print '</table>';
208			print '</td>';
209
210			print '<td>'.$companystatic->getNomUrl(1, 'customer').'</td>';
211			print '<td>'.dol_print_date($db->jdate($obj->datec), 'day').'</td>';
212			print '<td class="right">'.$propalstatic->LibStatut($obj->fk_statut, 3).'</td>';
213
214			print '</tr>';
215
216			$i++;
217		}
218	}
219
220	finishSimpleTable(true);
221	$db->free($resql);
222} else {
223	dol_print_error($db);
224}
225
226
227/*
228 * Open (validated) proposals
229 */
230if (!empty($conf->propal->enabled) && $user->rights->propale->lire) {
231	$sql = "SELECT s.nom as socname, s.rowid as socid, s.canvas, s.client, s.email, s.code_compta";
232	$sql .= ", p.rowid as propalid, p.entity, p.total_ttc, p.total_ht, p.ref, p.fk_statut, p.datep as dp, p.fin_validite as dfv";
233	$sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
234	$sql .= ", ".MAIN_DB_PREFIX."propal as p";
235	if (!$user->rights->societe->client->voir && !$socid) {
236		$sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
237	}
238	$sql .= " WHERE p.fk_soc = s.rowid";
239	$sql .= " AND p.entity IN (".getEntity($propalstatic->element).")";
240	$sql .= " AND p.fk_statut = ".Propal::STATUS_VALIDATED;
241	if (!$user->rights->societe->client->voir && !$socid) {
242		$sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
243	}
244	if ($socid) {
245		$sql .= " AND s.rowid = ".((int) $socid);
246	}
247	$sql .= " ORDER BY p.rowid DESC";
248
249	$resql = $db->query($sql);
250	if ($resql) {
251		$total = 0;
252		$num = $db->num_rows($resql);
253		$nbofloop = min($num, (empty($conf->global->MAIN_MAXLIST_OVERLOAD) ? 500 : $conf->global->MAIN_MAXLIST_OVERLOAD));
254		startSimpleTable("ProposalsOpened", "comm/propal/list.php", "search_status=".Propal::STATUS_VALIDATED, 4, $num);
255
256		if ($num > 0) {
257			$i = 0;
258			while ($i < $nbofloop) {
259				$obj = $db->fetch_object($resql);
260
261				$propalstatic->id = $obj->propalid;
262				$propalstatic->ref = $obj->ref;
263
264				$companystatic->id = $obj->socid;
265				$companystatic->name = $obj->socname;
266				$companystatic->client = $obj->client;
267				$companystatic->canvas = $obj->canvas;
268				$companystatic->email = $obj->email;
269				$companystatic->code_compta = $obj->code_compta;
270
271				$filename = dol_sanitizeFileName($obj->ref);
272				$filedir = $conf->propal->multidir_output[$obj->entity].'/'.dol_sanitizeFileName($obj->ref);
273				$urlsource = $_SERVER['PHP_SELF'].'?id='.$obj->propalid;
274
275				$warning = ($db->jdate($obj->dfv) < ($now - $conf->propal->cloture->warning_delay)) ? img_warning($langs->trans("Late")) : '';
276
277				print '<tr class="oddeven">';
278
279				// Ref
280				print '<td class="nowrap" width="140">';
281				print '<table class="nobordernopadding">';
282				print '<tr class="nocellnopadd">';
283				print '<td class="nobordernopadding nowrap">'.$propalstatic->getNomUrl(1).'</td>';
284				print '<td width="18" class="nobordernopadding nowrap">'.$warning.'</td>';
285				print '<td width="16" align="center" class="nobordernopadding">'.$formfile->getDocumentsLink($propalstatic->element, $filename, $filedir).'</td>';
286				print '</tr>';
287				print '</table>';
288				print '</td>';
289
290				print '<td class="left">'.$companystatic->getNomUrl(1, 'customer', 44).'</td>';
291				print '<td class="right">'.dol_print_date($db->jdate($obj->dp), 'day').'</td>';
292				print '<td class="right">'.price(!empty($conf->global->MAIN_DASHBOARD_USE_TOTAL_HT) ? $obj->total_ht : $obj->total_ttc).'</td>';
293				print '<td align="center" width="14">'.$propalstatic->LibStatut($obj->fk_statut, 3).'</td>';
294
295				print '</tr>';
296
297				$i++;
298				$total += (!empty($conf->global->MAIN_DASHBOARD_USE_TOTAL_HT) ? $obj->total_ht : $obj->total_ttc);
299			}
300		}
301
302		addSummaryTableLine(5, $num, $nbofloop, $total, "None", true);
303		finishSimpleTable(true);
304		$db->free($resql);
305	} else {
306		dol_print_error($db);
307	}
308}
309
310/*
311 * Proposals to process
312 */
313
314/*
315if (! empty($conf->propal->enabled))
316{
317	$sql = "SELECT c.rowid, c.ref, c.fk_statut, s.nom as name, s.rowid as socid";
318	$sql.=" FROM ".MAIN_DB_PREFIX."propal as c";
319	$sql.= ", ".MAIN_DB_PREFIX."societe as s";
320	if (!$user->rights->societe->client->voir && !$socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
321	$sql.= " WHERE c.fk_soc = s.rowid";
322	$sql.= " AND c.entity = ".$conf->entity;
323	$sql.= " AND c.fk_statut = 1";
324	if ($socid) $sql.= " AND c.fk_soc = ".((int) $socid);
325	if (!$user->rights->societe->client->voir && !$socid) $sql.= " AND s.rowid = sc.fk_soc AND sc.fk_user = " .((int) $user->id);
326	$sql.= " ORDER BY c.rowid DESC";
327
328	$resql=$db->query($sql);
329	if ($resql)
330	{
331		$num = $db->num_rows($resql);
332
333		print '<div class="div-table-responsive-no-min">';
334		print '<table class="noborder centpercent">';
335		print '<tr class="liste_titre">';
336		print '<td colspan="3">'.$langs->trans("ProposalsToProcess").' <a href="'.DOL_URL_ROOT.'/commande/list.php?search_status=1"><span class="badge">'.$num.'</span></a></td></tr>';
337
338		if ($num)
339		{
340			$i = 0;
341			while ($i < $num)
342			{
343
344				$obj = $db->fetch_object($resql);
345				print '<tr class="oddeven">';
346				print '<td class="nowrap">';
347
348				$propalstatic->id=$obj->rowid;
349				$propalstatic->ref=$obj->ref;
350
351				print '<table class="nobordernopadding"><tr class="nocellnopadd">';
352				print '<td width="96" class="nobordernopadding nowrap">';
353				print $propalstatic->getNomUrl(1);
354				print '</td>';
355
356				print '<td width="16" class="nobordernopadding nowrap">';
357				print '&nbsp;';
358				print '</td>';
359
360				print '<td width="16" class="nobordernopadding right">';
361				$filename=dol_sanitizeFileName($obj->ref);
362				$filedir=$conf->commande->dir_output . '/' . dol_sanitizeFileName($obj->ref);
363				$urlsource=$_SERVER['PHP_SELF'].'?id='.$obj->rowid;
364				print $formfile->getDocumentsLink($propalstatic->element, $filename, $filedir);
365				print '</td></tr></table>';
366
367				print '</td>';
368
369				print '<td><a href="'.DOL_URL_ROOT.'/comm/card.php?socid='.$obj->socid.'">'.img_object($langs->trans("ShowCompany"),"company").' '.dol_trunc($obj->name,24).'</a></td>';
370
371				print '<td class="right">'.$propalstatic->LibStatut($obj->fk_statut,$obj->facture,5).'</td>';
372
373				print '</tr>';
374				$i++;
375			}
376		}
377
378		print "</table>";
379		print "</div><br>";
380	}
381	else dol_print_error($db);
382}
383*/
384
385/*
386 * Proposal that are in a shipping process
387 */
388
389/*
390if (! empty($conf->propal->enabled))
391{
392	$sql = "SELECT c.rowid, c.ref, c.fk_statut, c.facture, s.nom as name, s.rowid as socid";
393	$sql.= " FROM ".MAIN_DB_PREFIX."commande as c";
394	$sql.= ", ".MAIN_DB_PREFIX."societe as s";
395	if (!$user->rights->societe->client->voir && !$socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
396	$sql.= " WHERE c.fk_soc = s.rowid";
397	$sql.= " AND c.entity = ".$conf->entity;
398	$sql.= " AND c.fk_statut = 2 ";
399	if ($socid) $sql.= " AND c.fk_soc = ".((int) $socid);
400	if (!$user->rights->societe->client->voir && !$socid) $sql.= " AND s.rowid = sc.fk_soc AND sc.fk_user = " .((int) $user->id);
401	$sql.= " ORDER BY c.rowid DESC";
402
403	$resql=$db->query($sql);
404	if ($resql)
405	{
406		$num = $db->num_rows($resql);
407
408		print '<div class="div-table-responsive-no-min">';
409		print '<table class="noborder centpercent">';
410		print '<tr class="liste_titre">';
411		print '<td colspan="3">'.$langs->trans("OnProcessOrders").' <a href="'.DOL_URL_ROOT.'/commande/list.php?search_status=2"><span class="badge">'.$num.'</span></a></td></tr>';
412
413		if ($num)
414		{
415			$i = 0;
416			while ($i < $num)
417			{
418
419				$obj = $db->fetch_object($resql);
420				print '<tr class="oddeven">';
421				print '<td width="20%" class="nowrap">';
422
423				$propalstatic->id=$obj->rowid;
424				$propalstatic->ref=$obj->ref;
425
426				print '<table class="nobordernopadding"><tr class="nocellnopadd">';
427				print '<td width="96" class="nobordernopadding nowrap">';
428				print $propalstatic->getNomUrl(1);
429				print '</td>';
430
431				print '<td width="16" class="nobordernopadding nowrap">';
432				print '&nbsp;';
433				print '</td>';
434
435				print '<td width="16" class="nobordernopadding right">';
436				$filename=dol_sanitizeFileName($obj->ref);
437				$filedir=$conf->commande->dir_output . '/' . dol_sanitizeFileName($obj->ref);
438				$urlsource=$_SERVER['PHP_SELF'].'?id='.$obj->rowid;
439				print $formfile->getDocumentsLink($propalstatic->element, $filename, $filedir);
440				print '</td></tr></table>';
441
442				print '</td>';
443
444				print '<td><a href="'.DOL_URL_ROOT.'/comm/card.php?socid='.$obj->socid.'">'.img_object($langs->trans("ShowCompany"),"company").' '.$obj->name.'</a></td>';
445
446				print '<td class="right">'.$propalstatic->LibStatut($obj->fk_statut,$obj->facture,5).'</td>';
447
448				print '</tr>';
449				$i++;
450			}
451		}
452		print "</table>";
453		print "</div><br>";
454	}
455	else dol_print_error($db);
456}
457*/
458
459print '</div>';
460print '</div>';
461print '</div>';
462
463$parameters = array('user' => $user);
464$reshook = $hookmanager->executeHooks('dashboardPropals', $parameters, $object); // Note that $action and $object may have been modified by hook
465
466// End of page
467llxFooter();
468$db->close();
469