1<?php
2/* Copyright (C) 2001-2005	Rodolphe Quiedeville	<rodolphe@quiedeville.org>
3 * Copyright (C) 2004-2015	Laurent Destailleur		<eldy@users.sourceforge.net>
4 * Copyright (C) 2005-2012	Regis Houssin			<regis.houssin@inodbox.com>
5 * Copyright (C) 2015		Jean-François Ferry		<jfefe@aternatik.fr>
6 * Copyright (C) 2019		Nicolas ZABOURI			<info@inovea-conseil.com>
7 * Copyright (C) 2020		Pierre Ardoin			<mapiolca@me.com>
8 * Copyright (C) 2020		Tobias Sekan			<tobias.sekan@startmail.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 */
23
24/**
25 *	\file		htdocs/comm/index.php
26 *	\ingroup	commercial
27 *	\brief		Home page of commercial area
28 */
29
30require '../main.inc.php';
31require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
32require_once DOL_DOCUMENT_ROOT.'/core/lib/agenda.lib.php';
33require_once DOL_DOCUMENT_ROOT.'/comm/action/class/actioncomm.class.php';
34require_once DOL_DOCUMENT_ROOT.'/comm/propal/class/propal.class.php';
35require_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php';
36require_once DOL_DOCUMENT_ROOT.'/contrat/class/contrat.class.php';
37require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.commande.class.php';
38require_once DOL_DOCUMENT_ROOT.'/societe/class/client.class.php';
39require_once DOL_DOCUMENT_ROOT.'/supplier_proposal/class/supplier_proposal.class.php';
40require_once DOL_DOCUMENT_ROOT.'/core/lib/propal.lib.php';
41require_once DOL_DOCUMENT_ROOT.'/core/lib/order.lib.php';
42
43// Initialize technical object to manage hooks. Note that conf->hooks_modules contains array
44$hookmanager = new HookManager($db);
45$hookmanager->initHooks(array('commercialindex'));
46
47// Load translation files required by the page
48$langs->loadLangs(array("boxes", "commercial", "contracts", "orders", "propal", "supplier_proposal"));
49
50$action = GETPOST('action', 'aZ09');
51$bid = GETPOST('bid', 'int');
52
53// Securite acces client
54$socid = GETPOST('socid', 'int');
55if (isset($user->socid) && $user->socid > 0) {
56	$action = '';
57	$socid = $user->socid;
58}
59
60$max = $conf->global->MAIN_SIZE_SHORTLIST_LIMIT;
61$now = dol_now();
62
63// Security check
64$socid = GETPOST("socid", 'int');
65if ($user->socid > 0) {
66	$action = '';
67	$id = $user->socid;
68} else {
69	$id = 0;
70}
71restrictedArea($user, 'societe', $id, '&societe', '', 'fk_soc', 'rowid', 0);
72
73$maxofloop = (empty($conf->global->MAIN_MAXLIST_OVERLOAD) ? 500 : $conf->global->MAIN_MAXLIST_OVERLOAD);
74
75
76/*
77 * Actions
78 */
79
80// None
81
82
83/*
84 * View
85 */
86
87$form = new Form($db);
88$formfile = new FormFile($db);
89$companystatic = new Societe($db);
90if (!empty($conf->propal->enabled)) {
91	$propalstatic = new Propal($db);
92}
93if (!empty($conf->supplier_proposal->enabled)) {
94	$supplierproposalstatic = new SupplierProposal($db);
95}
96if (!empty($conf->commande->enabled)) {
97	$orderstatic = new Commande($db);
98}
99if ((!empty($conf->fournisseur->enabled) && empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)) || !empty($conf->supplier_order->enabled)) {
100	$supplierorderstatic = new CommandeFournisseur($db);
101}
102
103llxHeader("", $langs->trans("CommercialArea"));
104
105print load_fiche_titre($langs->trans("CommercialArea"), '', 'commercial');
106
107print '<div class="fichecenter"><div class="fichethirdleft">';
108
109$tmp = getCustomerProposalPieChart($socid);
110if ($tmp) {
111	print $tmp;
112	print '<br>';
113}
114$tmp = getCustomerOrderPieChart($socid);
115if ($tmp) {
116	print $tmp;
117	print '<br>';
118}
119
120/*
121 * Draft customer proposals
122 */
123if (!empty($conf->propal->enabled) && $user->rights->propal->lire) {
124	$sql = "SELECT p.rowid, p.ref, p.ref_client, p.total_ht, p.total_tva, p.total_ttc, p.fk_statut as status";
125	$sql .= ", s.rowid as socid, s.nom as name, s.name_alias";
126	$sql .= ", s.code_client, s.code_compta, s.client";
127	$sql .= ", s.code_fournisseur, s.code_compta_fournisseur, s.fournisseur";
128	$sql .= ", s.logo, s.email, s.entity";
129	$sql .= ", s.canvas";
130	$sql .= " FROM ".MAIN_DB_PREFIX."propal as p,";
131	$sql .= " ".MAIN_DB_PREFIX."societe as s";
132	if (!$user->rights->societe->client->voir && !$socid) {
133		$sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
134	}
135	$sql .= " WHERE p.entity IN (".getEntity($propalstatic->element).")";
136	$sql .= " AND p.fk_soc = s.rowid";
137	$sql .= " AND p.fk_statut = ".Propal::STATUS_DRAFT;
138	if (!$user->rights->societe->client->voir && !$socid) {
139		$sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
140	}
141	if ($socid) {
142		$sql .= " AND s.rowid = ".((int) $socid);
143	}
144
145	$resql = $db->query($sql);
146	if ($resql) {
147		$total = 0;
148		$num = $db->num_rows($resql);
149		$nbofloop = min($num, $maxofloop);
150		startSimpleTable("ProposalsDraft", "comm/propal/list.php", "search_status=".Propal::STATUS_DRAFT, 2, $num);
151
152		if ($num > 0) {
153			$i = 0;
154			$othernb = 0;
155
156			while ($i < $nbofloop) {
157				$obj = $db->fetch_object($resql);
158
159				if ($i >= $max) {
160					$othernb += 1;
161					$i++;
162					$total += (!empty($conf->global->MAIN_DASHBOARD_USE_TOTAL_HT) ? $obj->total_ht : $obj->total_ttc);
163					continue;
164				}
165
166				$propalstatic->id = $obj->rowid;
167				$propalstatic->ref = $obj->ref;
168				$propalstatic->ref_client = $obj->ref_client;
169				$propalstatic->total_ht = $obj->total_ht;
170				$propalstatic->total_tva = $obj->total_tva;
171				$propalstatic->total_ttc = $obj->total_ttc;
172				$propalstatic->statut = $obj->status;
173
174				$companystatic->id = $obj->socid;
175				$companystatic->name = $obj->name;
176				$companystatic->name_alias = $obj->name_alias;
177				$companystatic->code_client = $obj->code_client;
178				$companystatic->code_compta = $obj->code_compta;
179				$companystatic->client = $obj->client;
180				$companystatic->code_fournisseur = $obj->code_fournisseur;
181				$companystatic->code_compta_fournisseur = $obj->code_compta_fournisseur;
182				$companystatic->fournisseur = $obj->fournisseur;
183				$companystatic->logo = $obj->logo;
184				$companystatic->email = $obj->email;
185				$companystatic->entity = $obj->entity;
186				$companystatic->canvas = $obj->canvas;
187
188				print '<tr class="oddeven">';
189				print '<td class="nowrap tdoverflowmax100">'.$propalstatic->getNomUrl(1).'</td>';
190				print '<td class="nowrap tdoverflowmax100">'.$companystatic->getNomUrl(1, 'customer').'</td>';
191				print '<td class="nowrap right tdamount amount">'.price((!empty($conf->global->MAIN_DASHBOARD_USE_TOTAL_HT) ? $obj->total_ht : $obj->total_ttc)).'</td>';
192				print '</tr>';
193
194				$i++;
195				$total += (!empty($conf->global->MAIN_DASHBOARD_USE_TOTAL_HT) ? $obj->total_ht : $obj->total_ttc);
196			}
197
198			if ($othernb) {
199				print '<tr class="oddeven">';
200				print '<td class="nowrap" colspan="5">';
201				print '<span class="opacitymedium">'.$langs->trans("More").'...'.($othernb < $maxofloop ? ' ('.$othernb.')' : '').'</span>';
202				print '</td>';
203				print "</tr>\n";
204			}
205		}
206
207		addSummaryTableLine(3, $num, $nbofloop, $total, "NoProposal");
208		finishSimpleTable(true);
209
210		$db->free($resql);
211	} else {
212		dol_print_error($db);
213	}
214}
215
216
217/*
218 * Draft supplier proposals
219 */
220if (!empty($conf->supplier_proposal->enabled) && $user->rights->supplier_proposal->lire) {
221	$sql = "SELECT p.rowid, p.ref, p.total_ht, p.total_tva, p.total_ttc, p.fk_statut as status";
222	$sql .= ", s.rowid as socid, s.nom as name, s.name_alias";
223	$sql .= ", s.code_client, s.code_compta, s.client";
224	$sql .= ", s.code_fournisseur, s.code_compta_fournisseur, s.fournisseur";
225	$sql .= ", s.logo, s.email, s.entity";
226	$sql .= ", s.canvas";
227	$sql .= " FROM ".MAIN_DB_PREFIX."supplier_proposal as p,";
228	$sql .= " ".MAIN_DB_PREFIX."societe as s";
229	if (!$user->rights->societe->client->voir && !$socid) {
230		$sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
231	}
232	$sql .= " WHERE p.entity IN (".getEntity($supplierproposalstatic->element).")";
233	$sql .= " AND p.fk_statut = ".SupplierProposal::STATUS_DRAFT;
234	$sql .= " AND p.fk_soc = s.rowid";
235	if (!$user->rights->societe->client->voir && !$socid) {
236		$sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
237	}
238	if ($socid) {
239		$sql .= " AND s.rowid = ".((int) $socid);
240	}
241
242	$resql = $db->query($sql);
243	if ($resql) {
244		$total = 0;
245		$num = $db->num_rows($resql);
246		$nbofloop = min($num, $maxofloop);
247		startSimpleTable("SupplierProposalsDraft", "supplier_proposal/list.php", "search_status=".SupplierProposal::STATUS_DRAFT, 2, $num);
248
249		if ($num > 0) {
250			$i = 0;
251			$othernb = 0;
252
253			while ($i < $nbofloop) {
254				$obj = $db->fetch_object($resql);
255
256				if ($i >= $max) {
257					$othernb += 1;
258					$i++;
259					$total += (!empty($conf->global->MAIN_DASHBOARD_USE_TOTAL_HT) ? $obj->total_ht : $obj->total_ttc);
260					continue;
261				}
262
263				$supplierproposalstatic->id = $obj->rowid;
264				$supplierproposalstatic->ref = $obj->ref;
265				$supplierproposalstatic->total_ht = $obj->total_ht;
266				$supplierproposalstatic->total_tva = $obj->total_tva;
267				$supplierproposalstatic->total_ttc = $obj->total_ttc;
268				$supplierproposalstatic->statut = $obj->status;
269
270				$companystatic->id = $obj->socid;
271				$companystatic->name = $obj->name;
272				$companystatic->name_alias = $obj->name_alias;
273				$companystatic->code_client = $obj->code_client;
274				$companystatic->code_compta = $obj->code_compta;
275				$companystatic->client = $obj->client;
276				$companystatic->code_fournisseur = $obj->code_fournisseur;
277				$companystatic->code_compta_fournisseur = $obj->code_compta_fournisseur;
278				$companystatic->fournisseur = $obj->fournisseur;
279				$companystatic->logo = $obj->logo;
280				$companystatic->email = $obj->email;
281				$companystatic->entity = $obj->entity;
282				$companystatic->canvas = $obj->canvas;
283
284				print '<tr class="oddeven">';
285				print '<td class="nowrap tdoverflowmax100">'.$supplierproposalstatic->getNomUrl(1).'</td>';
286				print '<td class="nowrap tdoverflowmax100">'.$companystatic->getNomUrl(1, 'supplier').'</td>';
287				print '<td class="nowrap right tdamount amount">'.price(!empty($conf->global->MAIN_DASHBOARD_USE_TOTAL_HT) ? $obj->total_ht : $obj->total_ttc).'</td>';
288				print '</tr>';
289
290				$i++;
291				$total += (!empty($conf->global->MAIN_DASHBOARD_USE_TOTAL_HT) ? $obj->total_ht : $obj->total_ttc);
292			}
293
294			if ($othernb) {
295				print '<tr class="oddeven">';
296				print '<td class="nowrap" colspan="5">';
297				print '<span class="opacitymedium">'.$langs->trans("More").'...'.($othernb < $maxofloop ? ' ('.$othernb.')' : '').'</span>';
298				print '</td>';
299				print "</tr>\n";
300			}
301		}
302
303		addSummaryTableLine(3, $num, $nbofloop, $total, "NoProposal");
304		finishSimpleTable(true);
305
306		$db->free($resql);
307	} else {
308		dol_print_error($db);
309	}
310}
311
312
313/*
314 * Draft customer orders
315 */
316if (!empty($conf->commande->enabled) && $user->rights->commande->lire) {
317	$sql = "SELECT c.rowid, c.ref, c.ref_client, c.total_ht, c.total_tva, c.total_ttc, c.fk_statut as status";
318	$sql .= ", s.rowid as socid, s.nom as name, s.name_alias";
319	$sql .= ", s.code_client, s.code_compta, s.client";
320	$sql .= ", s.code_fournisseur, s.code_compta_fournisseur, s.fournisseur";
321	$sql .= ", s.logo, s.email, s.entity";
322	$sql .= ", s.canvas";
323	$sql .= " FROM ".MAIN_DB_PREFIX."commande as c,";
324	$sql .= " ".MAIN_DB_PREFIX."societe as s";
325	if (!$user->rights->societe->client->voir && !$socid) {
326		$sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
327	}
328	$sql .= " WHERE c.entity IN (".getEntity($orderstatic->element).")";
329	$sql .= " AND c.fk_statut = ".Commande::STATUS_DRAFT;
330	$sql .= " AND c.fk_soc = s.rowid";
331	if (!$user->rights->societe->client->voir && !$socid) {
332		$sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
333	}
334	if ($socid) {
335		$sql .= " AND c.fk_soc = ".((int) $socid);
336	}
337
338	$resql = $db->query($sql);
339	if ($resql) {
340		$total = 0;
341		$num = $db->num_rows($resql);
342		$nbofloop = min($num, $maxofloop);
343		startSimpleTable("DraftOrders", "commande/list.php", "search_status=".Commande::STATUS_DRAFT, 2, $num);
344
345		if ($num > 0) {
346			$i = 0;
347			$othernb = 0;
348
349			while ($i < $nbofloop) {
350				$obj = $db->fetch_object($resql);
351
352				if ($i >= $max) {
353					$othernb += 1;
354					$i++;
355					$total += (!empty($conf->global->MAIN_DASHBOARD_USE_TOTAL_HT) ? $obj->total_ht : $obj->total_ttc);
356					continue;
357				}
358
359				$orderstatic->id = $obj->rowid;
360				$orderstatic->ref = $obj->ref;
361				$orderstatic->ref_client = $obj->ref_client;
362				$orderstatic->total_ht = $obj->total_ht;
363				$orderstatic->total_tva = $obj->total_tva;
364				$orderstatic->total_ttc = $obj->total_ttc;
365				$orderstatic->statut = $obj->status;
366
367				$companystatic->id = $obj->socid;
368				$companystatic->name = $obj->name;
369				$companystatic->name_alias = $obj->name_alias;
370				$companystatic->code_client = $obj->code_client;
371				$companystatic->code_compta = $obj->code_compta;
372				$companystatic->client = $obj->client;
373				$companystatic->code_fournisseur = $obj->code_fournisseur;
374				$companystatic->code_compta_fournisseur = $obj->code_compta_fournisseur;
375				$companystatic->fournisseur = $obj->fournisseur;
376				$companystatic->logo = $obj->logo;
377				$companystatic->email = $obj->email;
378				$companystatic->entity = $obj->entity;
379				$companystatic->canvas = $obj->canvas;
380
381				print '<tr class="oddeven">';
382				print '<td class="nowrap tdoverflowmax100">'.$orderstatic->getNomUrl(1).'</td>';
383				print '<td class="nowrap tdoverflowmax100">'.$companystatic->getNomUrl(1, 'customer').'</td>';
384				print '<td class="nowrap right tdamount amount">'.price(!empty($conf->global->MAIN_DASHBOARD_USE_TOTAL_HT) ? $obj->total_ht : $obj->total_ttc).'</td>';
385				print '</tr>';
386
387				$i++;
388				$total += (!empty($conf->global->MAIN_DASHBOARD_USE_TOTAL_HT) ? $obj->total_ht : $obj->total_ttc);
389			}
390
391			if ($othernb) {
392				print '<tr class="oddeven">';
393				print '<td class="nowrap" colspan="5">';
394				print '<span class="opacitymedium">'.$langs->trans("More").'...'.($othernb < $maxofloop ? ' ('.$othernb.')' : '').'</span>';
395				print '</td>';
396				print "</tr>\n";
397			}
398		}
399
400		addSummaryTableLine(3, $num, $nbofloop, $total, "NoProposal");
401		finishSimpleTable(true);
402
403		$db->free($resql);
404	} else {
405		dol_print_error($db);
406	}
407}
408
409
410/*
411 * Draft purchase orders
412 */
413if ((!empty($conf->fournisseur->enabled) && empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->rights->fournisseur->commande->lire) || (!empty($conf->supplier_order->enabled) && $user->rights->supplier_order->lire)) {
414	$sql = "SELECT cf.rowid, cf.ref, cf.ref_supplier, cf.total_ht, cf.total_tva, cf.total_ttc, cf.fk_statut as status";
415	$sql .= ", s.rowid as socid, s.nom as name, s.name_alias";
416	$sql .= ", s.code_client, s.code_compta, s.client";
417	$sql .= ", s.code_fournisseur, s.code_compta_fournisseur, s.fournisseur";
418	$sql .= ", s.logo, s.email, s.entity";
419	$sql .= ", s.canvas";
420	$sql .= " FROM ".MAIN_DB_PREFIX."commande_fournisseur as cf,";
421	$sql .= " ".MAIN_DB_PREFIX."societe as s";
422	if (!$user->rights->societe->client->voir && !$socid) {
423		$sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
424	}
425	$sql .= " WHERE cf.entity IN (".getEntity($supplierorderstatic->element).")";
426	$sql .= " AND cf.fk_statut = ".CommandeFournisseur::STATUS_DRAFT;
427	$sql .= " AND cf.fk_soc = s.rowid";
428	if (!$user->rights->societe->client->voir && !$socid) {
429		$sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
430	}
431	if ($socid) {
432		$sql .= " AND cf.fk_soc = ".((int) $socid);
433	}
434
435	$resql = $db->query($sql);
436	if ($resql) {
437		$total = 0;
438		$num = $db->num_rows($resql);
439		$nbofloop = min($num, $maxofloop);
440		startSimpleTable("DraftSuppliersOrders", "fourn/commande/list.php", "search_status=".CommandeFournisseur::STATUS_DRAFT, 2, $num);
441
442		if ($num > 0) {
443			$i = 0;
444			$othernb = 0;
445
446			while ($i < $nbofloop) {
447				$obj = $db->fetch_object($resql);
448
449				if ($i >= $max) {
450					$othernb += 1;
451					$i++;
452					$total += (!empty($conf->global->MAIN_DASHBOARD_USE_TOTAL_HT) ? $obj->total_ht : $obj->total_ttc);
453					continue;
454				}
455
456				$supplierorderstatic->id = $obj->rowid;
457				$supplierorderstatic->ref = $obj->ref;
458				$supplierorderstatic->ref_supplier = $obj->ref_supplier;
459				$supplierorderstatic->total_ht = $obj->total_ht;
460				$supplierorderstatic->total_tva = $obj->total_tva;
461				$supplierorderstatic->total_ttc = $obj->total_ttc;
462				$supplierorderstatic->statut = $obj->status;
463
464				$companystatic->id = $obj->socid;
465				$companystatic->name = $obj->name;
466				$companystatic->name_alias = $obj->name_alias;
467				$companystatic->code_client = $obj->code_client;
468				$companystatic->code_compta = $obj->code_compta;
469				$companystatic->client = $obj->client;
470				$companystatic->code_fournisseur = $obj->code_fournisseur;
471				$companystatic->code_compta_fournisseur = $obj->code_compta_fournisseur;
472				$companystatic->fournisseur = $obj->fournisseur;
473				$companystatic->logo = $obj->logo;
474				$companystatic->email = $obj->email;
475				$companystatic->entity = $obj->entity;
476				$companystatic->canvas = $obj->canvas;
477
478				print '<tr class="oddeven">';
479				print '<td class="nowrap tdoverflowmax100">'.$supplierorderstatic->getNomUrl(1).'</td>';
480				print '<td class="nowrap tdoverflowmax100">'.$companystatic->getNomUrl(1, 'supplier').'</td>';
481				print '<td class="nowrap right tdamount amount">'.price(!empty($conf->global->MAIN_DASHBOARD_USE_TOTAL_HT) ? $obj->total_ht : $obj->total_ttc).'</td>';
482				print '</tr>';
483
484				$i++;
485				$total += (!empty($conf->global->MAIN_DASHBOARD_USE_TOTAL_HT) ? $obj->total_ht : $obj->total_ttc);
486			}
487
488			if ($othernb) {
489				print '<tr class="oddeven">';
490				print '<td class="nowrap" colspan="5">';
491				print '<span class="opacitymedium">'.$langs->trans("More").'...'.($othernb < $maxofloop ? ' ('.$othernb.')' : '').'</span>';
492				print '</td>';
493				print "</tr>\n";
494			}
495		}
496
497		addSummaryTableLine(3, $num, $nbofloop, $total, "NoProposal");
498		finishSimpleTable(true);
499
500		$db->free($resql);
501	} else {
502		dol_print_error($db);
503	}
504}
505
506print '</div><div class="fichetwothirdright">';
507print '<div class="ficheaddleft">';
508
509/*
510 * Last modified customers or prospects
511 */
512if (!empty($conf->societe->enabled) && $user->rights->societe->lire) {
513	$sql = "SELECT s.rowid as socid, s.nom as name, s.name_alias";
514	$sql .= ", s.code_client, s.code_compta, s.client";
515	$sql .= ", s.code_fournisseur, s.code_compta_fournisseur, s.fournisseur";
516	$sql .= ", s.logo, s.email, s.entity";
517	$sql .= ", s.canvas";
518	$sql .= ", s.datec, s.tms";
519	$sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
520	if (!$user->rights->societe->client->voir && !$socid) {
521		$sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
522	}
523	$sql .= " WHERE s.entity IN (".getEntity($companystatic->element).")";
524	$sql .= " AND s.client IN (".Societe::CUSTOMER.", ".Societe::PROSPECT.", ".Societe::CUSTOMER_AND_PROSPECT.")";
525	if (!$user->rights->societe->client->voir && !$socid) {
526		$sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
527	}
528	if ($socid) {
529		$sql .= " AND s.rowid = $socid";
530	}
531	$sql .= " ORDER BY s.tms DESC";
532	$sql .= $db->plimit($max, 0);
533
534	$resql = $db->query($sql);
535	if ($resql) {
536		if (empty($conf->global->SOCIETE_DISABLE_PROSPECTS) && empty($conf->global->SOCIETE_DISABLE_CUSTOMERS)) {
537			$header = "BoxTitleLastCustomersOrProspects";
538		} elseif (!empty($conf->global->SOCIETE_DISABLE_CUSTOMERS)) {
539			$header = "BoxTitleLastModifiedProspects";
540		} else {
541			$header = "BoxTitleLastModifiedCustomers";
542		}
543
544		$num = $db->num_rows($resql);
545		startSimpleTable($langs->trans($header, min($max, $num)), "societe/list.php", "type=p,c", 1);
546
547		if ($num) {
548			$i = 0;
549
550			while ($i < $num && $i < $max) {
551				$objp = $db->fetch_object($resql);
552
553				$companystatic->id = $objp->socid;
554				$companystatic->name = $objp->name;
555				$companystatic->name_alias = $objp->name_alias;
556				$companystatic->code_client = $objp->code_client;
557				$companystatic->code_compta = $objp->code_compta;
558				$companystatic->client = $objp->client;
559				$companystatic->code_fournisseur = $objp->code_fournisseur;
560				$companystatic->code_compta_fournisseur = $objp->code_compta_fournisseur;
561				$companystatic->fournisseur = $objp->fournisseur;
562				$companystatic->logo = $objp->logo;
563				$companystatic->email = $objp->email;
564				$companystatic->entity = $objp->entity;
565				$companystatic->canvas = $objp->canvas;
566
567				print '<tr class="oddeven">';
568				print '<td class="nowrap tdoverflowmax100">'.$companystatic->getNomUrl(1, 'customer').'</td>';
569				print '<td class="nowrap">';
570				//print $companystatic->getLibCustProspStatut();
571
572				$obj = $companystatic;
573				$s = '';
574				if (($obj->client == 2 || $obj->client == 3) && empty($conf->global->SOCIETE_DISABLE_PROSPECTS)) {
575					$s .= '<a class="customer-back opacitymedium" title="'.$langs->trans("Prospect").'" href="'.DOL_URL_ROOT.'/comm/card.php?socid='.$companystatic->id.'">'.dol_substr($langs->trans("Prospect"), 0, 1).'</a>';
576				}
577				if (($obj->client == 1 || $obj->client == 3) && empty($conf->global->SOCIETE_DISABLE_CUSTOMERS)) {
578					$s .= '<a class="customer-back" title="'.$langs->trans("Customer").'" href="'.DOL_URL_ROOT.'/comm/card.php?socid='.$companystatic->id.'">'.dol_substr($langs->trans("Customer"), 0, 1).'</a>';
579				}
580				/*
581				if ((!empty($conf->fournisseur->enabled) && empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) || !empty($conf->supplier_order->enabled) || !empty($conf->supplier_invoice->enabled)) && $obj->fournisseur)
582				{
583					$s .= '<a class="vendor-back" title="'.$langs->trans("Supplier").'" href="'.DOL_URL_ROOT.'/fourn/card.php?socid='.$companystatic->id.'">'.dol_substr($langs->trans("Supplier"), 0, 1).'</a>';
584				}*/
585				print $s;
586
587				print '</td>';
588				print '<td class="right nowrap tddate">'.dol_print_date($db->jdate($objp->tms), 'day').'</td>';
589				print '</tr>';
590
591				$i++;
592			}
593		}
594
595		addSummaryTableLine(3, $num);
596		finishSimpleTable(true);
597
598		$db->free($resql);
599	} else {
600		dol_print_error($db);
601	}
602}
603
604
605/*
606 * Last suppliers
607 */
608if (((!empty($conf->fournisseur->enabled) && empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)) || !empty($conf->supplier_order->enabled) || !empty($conf->supplier_invoice->enabled)) && $user->rights->societe->lire) {
609	$sql = "SELECT s.rowid as socid, s.nom as name, s.name_alias";
610	$sql .= ", s.code_client, s.code_compta, s.client";
611	$sql .= ", s.code_fournisseur, s.code_compta_fournisseur, s.fournisseur";
612	$sql .= ", s.logo, s.email, s.entity";
613	$sql .= ", s.canvas";
614	$sql .= ", s.datec as dc, s.tms as dm";
615	$sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
616	if (!$user->rights->societe->client->voir && !$user->socid) {
617		$sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
618	}
619	$sql .= " WHERE s.entity IN (".getEntity($companystatic->element).")";
620	$sql .= " AND s.fournisseur = ".Societe::SUPPLIER;
621	if (!$user->rights->societe->client->voir && !$user->socid) {
622		$sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
623	}
624	if ($socid) {
625		$sql .= " AND s.rowid = ".((int) $socid);
626	}
627	$sql .= " ORDER BY s.datec DESC";
628	$sql .= $db->plimit($max, 0);
629
630	$resql = $db->query($sql);
631	if ($resql) {
632		$num = $db->num_rows($resql);
633		startSimpleTable($langs->trans("BoxTitleLastModifiedSuppliers", min($max, $num)), "societe/list.php", "type=f", 1);
634
635		if ($num) {
636			$i = 0;
637			while ($i < $num && $i < $max) {
638				$objp = $db->fetch_object($resql);
639
640				$companystatic->id = $objp->socid;
641				$companystatic->name = $objp->name;
642				$companystatic->name_alias = $objp->name_alias;
643				$companystatic->code_client = $objp->code_client;
644				$companystatic->code_compta = $objp->code_compta;
645				$companystatic->client = $objp->client;
646				$companystatic->code_fournisseur = $objp->code_fournisseur;
647				$companystatic->code_compta_fournisseur = $objp->code_compta_fournisseur;
648				$companystatic->fournisseur = $objp->fournisseur;
649				$companystatic->logo = $objp->logo;
650				$companystatic->email = $objp->email;
651				$companystatic->entity = $objp->entity;
652				$companystatic->canvas = $objp->canvas;
653
654				print '<tr class="oddeven">';
655				print '<td class="nowrap tdoverflowmax100">'.$companystatic->getNomUrl(1, 'supplier').'</td>';
656				print '<td>';
657
658				$obj = $companystatic;
659				$s = '';
660				/*if (($obj->client == 2 || $obj->client == 3) && empty($conf->global->SOCIETE_DISABLE_PROSPECTS)) {
661					$s .= '<a class="customer-back opacitymedium" title="'.$langs->trans("Prospect").'" href="'.DOL_URL_ROOT.'/comm/card.php?socid='.$companystatic->id.'">'.dol_substr($langs->trans("Prospect"), 0, 1).'</a>';
662				}
663				if (($obj->client == 1 || $obj->client == 3) && empty($conf->global->SOCIETE_DISABLE_CUSTOMERS))
664				{
665					$s .= '<a class="customer-back" title="'.$langs->trans("Customer").'" href="'.DOL_URL_ROOT.'/comm/card.php?socid='.$companystatic->id.'">'.dol_substr($langs->trans("Customer"), 0, 1).'</a>';
666				}*/
667				if (((!empty($conf->fournisseur->enabled) && empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)) || !empty($conf->supplier_order->enabled) || !empty($conf->supplier_invoice->enabled)) && $obj->fournisseur) {
668					$s .= '<a class="vendor-back" title="'.$langs->trans("Supplier").'" href="'.DOL_URL_ROOT.'/fourn/card.php?socid='.$companystatic->id.'">'.dol_substr($langs->trans("Supplier"), 0, 1).'</a>';
669				}
670				print $s;
671
672				print '</td>';
673				print '<td class="right tddate">'.dol_print_date($db->jdate($objp->dm), 'day').'</td>';
674				print '</tr>';
675
676				$i++;
677			}
678		}
679
680		addSummaryTableLine(3, $num);
681		finishSimpleTable(true);
682
683		$db->free($resql);
684	} else {
685		dol_print_error($db);
686	}
687}
688
689
690/*
691 * Last actions
692 */
693/*if ($user->rights->agenda->myactions->read) {
694	show_array_last_actions_done($max);
695}*/
696
697
698/*
699 * Actions to do
700 */
701/*if ($user->rights->agenda->myactions->read) {
702	show_array_actions_to_do($max);
703}*/
704
705
706/*
707 * Latest contracts
708 */
709if (!empty($conf->contrat->enabled) && $user->rights->contrat->lire && 0) { // TODO A REFAIRE DEPUIS NOUVEAU CONTRAT
710	$staticcontrat = new Contrat($db);
711
712	$sql = "SELECT s.rowid as socid, s.nom as name, s.name_alias";
713	$sql .= ", s.code_client, s.code_compta, s.client";
714	$sql .= ", s.code_fournisseur, s.code_compta_fournisseur, s.fournisseur";
715	$sql .= ", s.logo, s.email, s.entity";
716	$sql .= ", s.canvas";
717	$sql .= ", c.statut, c.rowid as contratid, p.ref, c.fin_validite as datefin, c.date_cloture as dateclo";
718	$sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
719	$sql .= ", ".MAIN_DB_PREFIX."contrat as c";
720	$sql .= ", ".MAIN_DB_PREFIX."product as p";
721	if (!$user->rights->societe->client->voir && !$socid) {
722		$sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
723	}
724	$sql .= " WHERE c.entity IN (".getEntity($staticcontrat->element).")";
725	$sql .= " AND c.fk_soc = s.rowid";
726	$sql .= " AND c.fk_product = p.rowid";
727	if (!$user->rights->societe->client->voir && !$socid) {
728		$sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
729	}
730	if ($socid) {
731		$sql .= " AND s.rowid = ".((int) $socid);
732	}
733	$sql .= " ORDER BY c.tms DESC";
734	$sql .= $db->plimit($max + 1, 0);
735
736	$resql = $db->query($sql);
737	if ($resql) {
738		$num = $db->num_rows($resql);
739		startSimpleTable($langs->trans("LastContracts", $max), "", "", 2);
740
741		if ($num > 0) {
742			$i = 0;
743
744			while ($i < $num) {
745				$obj = $db->fetch_object($resql);
746
747				$companystatic->id = $obj->socid;
748				$companystatic->name = $obj->name;
749				$companystatic->name_alias = $obj->name_alias;
750				$companystatic->code_client = $obj->code_client;
751				$companystatic->code_compta = $obj->code_compta;
752				$companystatic->client = $obj->client;
753				$companystatic->code_fournisseur = $obj->code_fournisseur;
754				$companystatic->code_compta_fournisseur = $obj->code_compta_fournisseur;
755				$companystatic->fournisseur = $obj->fournisseur;
756				$companystatic->logo = $obj->logo;
757				$companystatic->email = $obj->email;
758				$companystatic->entity = $obj->entity;
759				$companystatic->canvas = $obj->canvas;
760
761				$staticcontrat->id = $obj->contratid;
762				$staticcontrat->ref = $obj->ref;
763
764				print '<tr class="oddeven">';
765				print '<td>'.$staticcontrat->getNomUrl(1).'</td>';
766				print '<td>'.$companystatic->getNomUrl(1, 'customer', 44).'</td>';
767				print '<td class="right">'.$staticcontrat->LibStatut($obj->statut, 3).'</td>';
768				print '</tr>';
769
770				$i++;
771			}
772		}
773
774		addSummaryTableLine(2, $num);
775		finishSimpleTable(true);
776
777		$db->free($resql);
778	} else {
779		dol_print_error($db);
780	}
781}
782
783
784/*
785 * Opened (validated) proposals
786 */
787if (!empty($conf->propal->enabled) && $user->rights->propal->lire) {
788	$sql = "SELECT p.rowid as propalid, p.entity, p.total_ttc, p.total_ht, p.total_tva, p.ref, p.ref_client, p.fk_statut, p.datep as dp, p.fin_validite as dfv";
789	$sql .= ", s.rowid as socid, s.nom as name, s.name_alias";
790	$sql .= ", s.code_client, s.code_compta, s.client";
791	$sql .= ", s.code_fournisseur, s.code_compta_fournisseur, s.fournisseur";
792	$sql .= ", s.logo, s.email, s.entity";
793	$sql .= ", s.canvas";
794	$sql .= " FROM ".MAIN_DB_PREFIX."propal as p";
795	$sql .= ", ".MAIN_DB_PREFIX."societe as s";
796	if (!$user->rights->societe->client->voir && !$socid) {
797		$sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
798	}
799	$sql .= " WHERE p.entity IN (".getEntity($propalstatic->element).")";
800	$sql .= " AND p.fk_soc = s.rowid";
801	$sql .= " AND p.fk_statut = ".Propal::STATUS_VALIDATED;
802	if (!$user->rights->societe->client->voir && !$socid) {
803		$sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
804	}
805	if ($socid) {
806		$sql .= " AND s.rowid = ".((int) $socid);
807	}
808	$sql .= " ORDER BY p.rowid DESC";
809
810	$resql = $db->query($sql);
811	if ($resql) {
812		$total = $total_ttc = 0;
813		$num = $db->num_rows($resql);
814		$nbofloop = min($num, (empty($conf->global->MAIN_MAXLIST_OVERLOAD) ? 500 : $conf->global->MAIN_MAXLIST_OVERLOAD));
815		startSimpleTable("ProposalsOpened", "comm/propal/list.php", "search_status=1", 4, $num);
816
817		if ($num > 0) {
818			$i = 0;
819			$othernb = 0;
820
821			while ($i < $nbofloop) {
822				$obj = $db->fetch_object($resql);
823
824				if ($i >= $max) {
825					$othernb += 1;
826					$i++;
827					$total += $obj->total_ht;
828					$total_ttc += $obj->total_ttc;
829					continue;
830				}
831
832				$propalstatic->id = $obj->propalid;
833				$propalstatic->ref = $obj->ref;
834				$propalstatic->ref_client = $obj->ref_client;
835				$propalstatic->total_ht = $obj->total_ht;
836				$propalstatic->total_tva = $obj->total_tva;
837				$propalstatic->total_ttc = $obj->total_ttc;
838
839				$companystatic->id = $obj->socid;
840				$companystatic->name = $obj->name;
841				$companystatic->name_alias = $obj->name_alias;
842				$companystatic->code_client = $obj->code_client;
843				$companystatic->code_compta = $obj->code_compta;
844				$companystatic->client = $obj->client;
845				$companystatic->code_fournisseur = $obj->code_fournisseur;
846				$companystatic->code_compta_fournisseur = $obj->code_compta_fournisseur;
847				$companystatic->fournisseur = $obj->fournisseur;
848				$companystatic->logo = $obj->logo;
849				$companystatic->email = $obj->email;
850				$companystatic->entity = $obj->entity;
851				$companystatic->canvas = $obj->canvas;
852
853				$filename = dol_sanitizeFileName($obj->ref);
854				$filedir = $conf->propal->multidir_output[$obj->entity].'/'.dol_sanitizeFileName($obj->ref);
855				//$urlsource = $_SERVER['PHP_SELF'].'?id='.$obj->propalid;
856				$warning = ($db->jdate($obj->dfv) < ($now - $conf->propal->cloture->warning_delay)) ? img_warning($langs->trans("Late")) : '';
857
858				print '<tr class="oddeven">';
859
860				print '<td class="nowrap" width="140">';
861				print '<table class="nobordernopadding"><tr class="nocellnopadd">';
862				print '<td class="nobordernopadding nowrap">'.$propalstatic->getNomUrl(1).'</td>';
863				print '<td width="18" class="nobordernopadding nowrap">'.$warning.'</td>';
864				print '<td width="16" align="center" class="nobordernopadding">'.$formfile->getDocumentsLink($propalstatic->element, $filename, $filedir).'</td>';
865				print '</tr>';
866				print '</table>';
867				print '</td>';
868
869				print '<td class="nowrap">'.$companystatic->getNomUrl(1, 'customer', 44).'</td>';
870				print '<td class="right tddate">'.dol_print_date($db->jdate($obj->dp), 'day').'</td>';
871				print '<td class="right tdamount amount">'.price(!empty($conf->global->MAIN_DASHBOARD_USE_TOTAL_HT) ? $obj->total_ht : $obj->total_ttc).'</td>';
872				print '<td align="center" width="14">'.$propalstatic->LibStatut($obj->fk_statut, 3).'</td>';
873
874				print '</tr>';
875
876				$i++;
877				$total += $obj->total_ht;
878				$total_ttc += $obj->total_ttc;
879			}
880
881			if ($othernb) {
882				print '<tr class="oddeven">';
883				print '<td class="nowrap" colspan="5">';
884				print '<span class="opacitymedium">'.$langs->trans("More").'... ('.$othernb.')</span>';
885				print '</td>';
886				print "</tr>\n";
887			}
888		}
889
890		addSummaryTableLine(5, $num, $nbofloop, empty($conf->global->MAIN_DASHBOARD_USE_TOTAL_HT) ? $total_ttc : $total, "NoProposal", true);
891		finishSimpleTable(true);
892
893		$db->free($resql);
894	} else {
895		dol_print_error($db);
896	}
897}
898
899
900/*
901 * Opened (validated) order
902 */
903if (!empty($conf->commande->enabled) && $user->rights->commande->lire) {
904	$sql = "SELECT c.rowid as commandeid, c.total_ttc, c.total_ht, c.total_tva, c.ref, c.ref_client, c.fk_statut, c.date_valid as dv, c.facture as billed";
905	$sql .= ", s.rowid as socid, s.nom as name, s.name_alias";
906	$sql .= ", s.code_client, s.code_compta, s.client";
907	$sql .= ", s.code_fournisseur, s.code_compta_fournisseur, s.fournisseur";
908	$sql .= ", s.logo, s.email, s.entity";
909	$sql .= ", s.canvas";
910	$sql .= " FROM ".MAIN_DB_PREFIX."commande as c";
911	$sql .= ", ".MAIN_DB_PREFIX."societe as s";
912	if (!$user->rights->societe->client->voir && !$socid) {
913		$sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
914	}
915	$sql .= " WHERE c.entity IN (".getEntity($orderstatic->element).")";
916	$sql .= " AND c.fk_soc = s.rowid";
917	$sql .= " AND c.fk_statut IN (".Commande::STATUS_VALIDATED.", ".Commande::STATUS_SHIPMENTONPROCESS.")";
918	if (!$user->rights->societe->client->voir && !$socid) {
919		$sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
920	}
921	if ($socid) {
922		$sql .= " AND s.rowid = ".((int) $socid);
923	}
924	$sql .= " ORDER BY c.rowid DESC";
925
926	$resql = $db->query($sql);
927	if ($resql) {
928		$total = $total_ttc = 0;
929		$num = $db->num_rows($resql);
930		$nbofloop = min($num, (empty($conf->global->MAIN_MAXLIST_OVERLOAD) ? 500 : $conf->global->MAIN_MAXLIST_OVERLOAD));
931		startSimpleTable("OrdersOpened", "commande/list.php", "search_status=".Commande::STATUS_VALIDATED, 4, $num);
932
933		if ($num > 0) {
934			$i = 0;
935			$othernb = 0;
936
937			while ($i < $nbofloop) {
938				$obj = $db->fetch_object($resql);
939
940				if ($i >= $max) {
941					$othernb += 1;
942					$i++;
943					$total += $obj->total_ht;
944					$total_ttc += $obj->total_ttc;
945					continue;
946				}
947
948				$orderstatic->id = $obj->commandeid;
949				$orderstatic->ref = $obj->ref;
950				$orderstatic->ref_client = $obj->ref_client;
951				$orderstatic->statut = $obj->fk_statut;
952				$orderstatic->total_ht = $obj->total_ht;
953				$orderstatic->total_tva = $obj->total_tva;
954				$orderstatic->total_ttc = $obj->total_ttc;
955
956				$companystatic->id = $obj->socid;
957				$companystatic->name = $obj->name;
958				$companystatic->name_alias = $obj->name_alias;
959				$companystatic->code_client = $obj->code_client;
960				$companystatic->code_compta = $obj->code_compta;
961				$companystatic->client = $obj->client;
962				$companystatic->code_fournisseur = $obj->code_fournisseur;
963				$companystatic->code_compta_fournisseur = $obj->code_compta_fournisseur;
964				$companystatic->fournisseur = $obj->fournisseur;
965				$companystatic->logo = $obj->logo;
966				$companystatic->email = $obj->email;
967				$companystatic->entity = $obj->entity;
968				$companystatic->canvas = $obj->canvas;
969
970				$filename = dol_sanitizeFileName($obj->ref);
971				$filedir = $conf->commande->dir_output.'/'.dol_sanitizeFileName($obj->ref);
972				//$urlsource = $_SERVER['PHP_SELF'].'?id='.$obj->propalid;
973				//$warning = ($db->jdate($obj->dfv) < ($now - $conf->propal->cloture->warning_delay)) ? img_warning($langs->trans("Late")) : '';
974
975				print '<tr class="oddeven">';
976
977				print '<td class="nowrap" width="140">';
978				print '<table class="nobordernopadding"><tr class="nocellnopadd">';
979				print '<td class="nobordernopadding nowrap">'.$orderstatic->getNomUrl(1).'</td>';
980				print '<td width="18" class="nobordernopadding nowrap"></td>';
981				print '<td width="16" align="center" class="nobordernopadding">'.$formfile->getDocumentsLink($orderstatic->element, $filename, $filedir).'</td>';
982				print '</tr>';
983				print '</table>';
984				print '</td>';
985
986				print '<td class="nowrap">'.$companystatic->getNomUrl(1, 'customer', 44).'</td>';
987				print '<td class="right tddate">'.dol_print_date($db->jdate($obj->dv), 'day').'</td>';
988				print '<td class="right tdamount amount">'.price(!empty($conf->global->MAIN_DASHBOARD_USE_TOTAL_HT) ? $obj->total_ht : $obj->total_ttc).'</td>';
989				print '<td align="center" width="14">'.$orderstatic->LibStatut($obj->fk_statut, $obj->billed, 3).'</td>';
990
991				print '</tr>';
992
993				$i++;
994				$total += $obj->total_ht;
995				$total_ttc += $obj->total_ttc;
996			}
997
998			if ($othernb) {
999				print '<tr class="oddeven">';
1000				print '<td class="nowrap" colspan="5">';
1001				print '<span class="opacitymedium">'.$langs->trans("More").'... ('.$othernb.')</span>';
1002				print '</td>';
1003				print "</tr>\n";
1004			}
1005		}
1006
1007		addSummaryTableLine(5, $num, $nbofloop, empty($conf->global->MAIN_DASHBOARD_USE_TOTAL_HT) ? $total_ttc : $total, "None", true);
1008		finishSimpleTable(true);
1009
1010		$db->free($resql);
1011	} else {
1012		dol_print_error($db);
1013	}
1014}
1015
1016print '</div>';
1017print '</div>';
1018print '</div>';
1019
1020$parameters = array('user' => $user);
1021$reshook = $hookmanager->executeHooks('dashboardCommercials', $parameters, $object); // Note that $action and $object may have been modified by hook
1022
1023// End of page
1024llxFooter();
1025$db->close();
1026