index.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. <?php
  2. /* Copyright (C) 2001-2006 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2015 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005-2014 Regis Houssin <regis.houssin@capnetworks.com>
  5. * Copyright (C) 2014-2016 Charlie BENKE <charlie@patas-monkey.com>
  6. * Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
  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 <http://www.gnu.org/licenses/>.
  20. */
  21. /**
  22. * \file htdocs/product/index.php
  23. * \ingroup product
  24. * \brief Homepage products and services
  25. */
  26. require '../main.inc.php';
  27. require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
  28. require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  29. require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_parser.class.php';
  30. $type=GETPOST("type",'int');
  31. if ($type =='' && !$user->rights->produit->lire) $type='1'; // Force global page on service page only
  32. if ($type =='' && !$user->rights->service->lire) $type='0'; // Force global page on product page only
  33. // Security check
  34. if ($type=='0') $result=restrictedArea($user,'produit');
  35. else if ($type=='1') $result=restrictedArea($user,'service');
  36. else $result=restrictedArea($user,'produit|service');
  37. // Load translation files required by the page
  38. $langs->loadLangs(array('products', 'stocks'));
  39. // Initialize technical object to manage hooks. Note that conf->hooks_modules contains array of hooks
  40. $hookmanager->initHooks(array('productindex'));
  41. $product_static = new Product($db);
  42. /*
  43. * View
  44. */
  45. $transAreaType = $langs->trans("ProductsAndServicesArea");
  46. $helpurl='';
  47. if (! isset($_GET["type"]))
  48. {
  49. $transAreaType = $langs->trans("ProductsAndServicesArea");
  50. $helpurl='EN:Module_Products|FR:Module_Produits|ES:M&oacute;dulo_Productos';
  51. }
  52. if ((isset($_GET["type"]) && $_GET["type"] == 0) || empty($conf->service->enabled))
  53. {
  54. $transAreaType = $langs->trans("ProductsArea");
  55. $helpurl='EN:Module_Products|FR:Module_Produits|ES:M&oacute;dulo_Productos';
  56. }
  57. if ((isset($_GET["type"]) && $_GET["type"] == 1) || empty($conf->product->enabled))
  58. {
  59. $transAreaType = $langs->trans("ServicesArea");
  60. $helpurl='EN:Module_Services_En|FR:Module_Services|ES:M&oacute;dulo_Servicios';
  61. }
  62. llxHeader("", $langs->trans("ProductsAndServices"), $helpurl);
  63. $linkback="";
  64. print load_fiche_titre($transAreaType,$linkback,'title_products.png');
  65. print '<div class="fichecenter"><div class="fichethirdleft">';
  66. if (! empty($conf->global->MAIN_SEARCH_FORM_ON_HOME_AREAS)) // This is useless due to the global search combo
  67. {
  68. // Search contract
  69. if ((! empty($conf->product->enabled) || ! empty($conf->service->enabled)) && ($user->rights->produit->lire || $user->rights->service->lire))
  70. {
  71. $listofsearchfields['search_product']=array('text'=>'ProductOrService');
  72. }
  73. if (count($listofsearchfields))
  74. {
  75. print '<form method="post" action="'.DOL_URL_ROOT.'/core/search.php">';
  76. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  77. print '<table class="noborder nohover centpercent">';
  78. $i=0;
  79. foreach($listofsearchfields as $key => $value)
  80. {
  81. if ($i == 0) print '<tr class="liste_titre"><td colspan="3">'.$langs->trans("Search").'</td></tr>';
  82. print '<tr '.$bc[false].'>';
  83. print '<td class="nowrap"><label for="'.$key.'">'.$langs->trans($value["text"]).'</label></td><td><input type="text" class="flat inputsearch" name="'.$key.'" id="'.$key.'" size="18"></td>';
  84. if ($i == 0) print '<td rowspan="'.count($listofsearchfields).'"><input type="submit" value="'.$langs->trans("Search").'" class="button"></td>';
  85. print '</tr>';
  86. $i++;
  87. }
  88. print '</table>';
  89. print '</form>';
  90. print '<br>';
  91. }
  92. }
  93. /*
  94. * Number of products and/or services
  95. */
  96. $prodser = array();
  97. $prodser[0][0]=$prodser[0][1]=$prodser[0][2]=$prodser[0][3]=0;
  98. $prodser[1][0]=$prodser[1][1]=$prodser[1][2]=$prodser[1][3]=0;
  99. $sql = "SELECT COUNT(p.rowid) as total, p.fk_product_type, p.tosell, p.tobuy";
  100. $sql.= " FROM ".MAIN_DB_PREFIX."product as p";
  101. $sql.= ' WHERE p.entity IN ('.getEntity($product_static->element, 1).')';
  102. // Add where from hooks
  103. $parameters=array();
  104. $reshook=$hookmanager->executeHooks('printFieldListWhere',$parameters); // Note that $action and $object may have been modified by hook
  105. $sql.=$hookmanager->resPrint;
  106. $sql.= " GROUP BY p.fk_product_type, p.tosell, p.tobuy";
  107. $result = $db->query($sql);
  108. while ($objp = $db->fetch_object($result))
  109. {
  110. $status=3;
  111. if (! $objp->tosell && ! $objp->tobuy) $status=0; // Not on sale, not on purchase
  112. if ($objp->tosell && ! $objp->tobuy) $status=1; // On sale only
  113. if (! $objp->tosell && $objp->tobuy) $status=2; // On purchase only
  114. $prodser[$objp->fk_product_type][$status]=$objp->total;
  115. }
  116. print '<div class="div-table-responsive-no-min">';
  117. print '<table class="noborder" width="100%">';
  118. print '<tr class="liste_titre"><th colspan="2">'.$langs->trans("Statistics").'</th></tr>';
  119. if (! empty($conf->product->enabled))
  120. {
  121. $statProducts = '<tr class="oddeven">';
  122. $statProducts.= '<td><a href="list.php?type=0&amp;tosell=0&amp;tobuy=0">'.$langs->trans("ProductsNotOnSell").'</a></td><td align="right">'.round($prodser[0][0]).'</td>';
  123. $statProducts.= "</tr>";
  124. $statProducts.= '<tr class="oddeven">';
  125. $statProducts.= '<td><a href="list.php?type=0">'.$langs->trans("ProductsOnSaleOnly").'</a></td><td align="right">'.round($prodser[0][1]).'</td>';
  126. $statProducts.= "</tr>";
  127. $statProducts.= '<tr class="oddeven">';
  128. $statProducts.= '<td><a href="list.php?type=0">'.$langs->trans("ProductsOnPurchaseOnly").'</a></td><td align="right">'.round($prodser[0][2]).'</td>';
  129. $statProducts.= "</tr>";
  130. $statProducts.= '<tr class="oddeven">';
  131. $statProducts.= '<td><a href="list.php?type=0&amp;tosell=1&amp;tobuy=1">'.$langs->trans("ProductsOnSellAndOnBuy").'</a></td><td align="right">'.round($prodser[0][3]).'</td>';
  132. $statProducts.= "</tr>";
  133. }
  134. if (! empty($conf->service->enabled))
  135. {
  136. $statServices = '<tr class="oddeven">';
  137. $statServices.= '<td><a href="list.php?type=1&amp;tosell=0&amp;tobuy=0">'.$langs->trans("ServicesNotOnSell").'</a></td><td align="right">'.round($prodser[1][0]).'</td>';
  138. $statServices.= "</tr>";
  139. $statServices.= '<tr class="oddeven">';
  140. $statServices.= '<td><a href="list.php?type=1">'.$langs->trans("ServicesOnSaleOnly").'</a></td><td align="right">'.round($prodser[1][1]).'</td>';
  141. $statServices.= "</tr>";
  142. $statServices.= '<tr class="oddeven">';
  143. $statServices.= '<td><a href="list.php?type=1">'.$langs->trans("ServicesOnPurchaseOnly").'</a></td><td align="right">'.round($prodser[1][2]).'</td>';
  144. $statServices.= "</tr>";
  145. $statServices.= '<tr class="oddeven">';
  146. $statServices.= '<td><a href="list.php?type=1&amp;tosell=1&amp;tobuy=1">'.$langs->trans("ServicesOnSellAndOnBuy").'</a></td><td align="right">'.round($prodser[1][3]).'</td>';
  147. $statServices.= "</tr>";
  148. }
  149. $total=0;
  150. if ($type == '0')
  151. {
  152. print $statProducts;
  153. $total=round($prodser[0][0])+round($prodser[0][1])+round($prodser[0][2]);
  154. }
  155. else if ($type == '1')
  156. {
  157. print $statServices;
  158. $total=round($prodser[1][0])+round($prodser[1][1])+round($prodser[1][2]);
  159. }
  160. else
  161. {
  162. print $statProducts.$statServices;
  163. $total=round($prodser[0][0])+round($prodser[0][1])+round($prodser[0][2])+round($prodser[0][3])+round($prodser[1][0])+round($prodser[1][1])+round($prodser[1][2])+round($prodser[1][3]); //Calcul du Total des Produits et Services
  164. }
  165. print '<tr class="liste_total"><td>'.$langs->trans("Total").'</td><td align="right">';
  166. print $total;
  167. print '</td></tr>';
  168. print '</table>';
  169. print '</div>';
  170. if (! empty($conf->categorie->enabled) && ! empty($conf->global->CATEGORY_GRAPHSTATS_ON_PRODUCTS))
  171. {
  172. require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
  173. print '<br>';
  174. print '<div class="div-table-responsive-no-min">';
  175. print '<table class="noborder" width="100%">';
  176. print '<tr class="liste_titre"><th colspan="2">'.$langs->trans("Categories").'</th></tr>';
  177. print '<tr '.$bc[0].'><td align="center" colspan="2">';
  178. $sql = "SELECT c.label, count(*) as nb";
  179. $sql.= " FROM ".MAIN_DB_PREFIX."categorie_product as cs";
  180. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."categorie as c ON cs.fk_categorie = c.rowid";
  181. $sql.= " WHERE c.type = 0";
  182. $sql.= " AND c.entity IN (".getEntity('category').")";
  183. $sql.= " GROUP BY c.label";
  184. $total=0;
  185. $result = $db->query($sql);
  186. if ($result)
  187. {
  188. $num = $db->num_rows($result);
  189. $i=0;
  190. if (! empty($conf->use_javascript_ajax))
  191. {
  192. $dataseries=array();
  193. $rest=0;
  194. $nbmax=10;
  195. while ($i < $num)
  196. {
  197. $obj = $db->fetch_object($result);
  198. if ($i < $nbmax)
  199. {
  200. $dataseries[]=array($obj->label, round($obj->nb));
  201. }
  202. else
  203. {
  204. $rest+=$obj->nb;
  205. }
  206. $total+=$obj->nb;
  207. $i++;
  208. }
  209. if ($i > $nbmax)
  210. {
  211. $dataseries[]=array($langs->trans("Other"), round($rest));
  212. }
  213. include_once DOL_DOCUMENT_ROOT.'/core/class/dolgraph.class.php';
  214. $dolgraph = new DolGraph();
  215. $dolgraph->SetData($dataseries);
  216. $dolgraph->setShowLegend(1);
  217. $dolgraph->setShowPercent(1);
  218. $dolgraph->SetType(array('pie'));
  219. $dolgraph->setWidth('100%');
  220. $dolgraph->draw('idstatscategproduct');
  221. print $dolgraph->show($total?0:1);
  222. }
  223. else
  224. {
  225. while ($i < $num)
  226. {
  227. $obj = $db->fetch_object($result);
  228. print '<tr><td>'.$obj->label.'</td><td>'.$obj->nb.'</td></tr>';
  229. $total+=$obj->nb;
  230. $i++;
  231. }
  232. }
  233. }
  234. print '</td></tr>';
  235. print '<tr class="liste_total"><td>'.$langs->trans("Total").'</td><td align="right">';
  236. print $total;
  237. print '</td></tr>';
  238. print '</table>';
  239. print '</div>';
  240. }
  241. print '</div><div class="fichetwothirdright"><div class="ficheaddleft">';
  242. /*
  243. * Last modified products
  244. */
  245. $max=15;
  246. $sql = "SELECT p.rowid, p.label, p.price, p.ref, p.fk_product_type, p.tosell, p.tobuy, p.tobatch, p.fk_price_expression,";
  247. $sql.= " p.entity,";
  248. $sql.= " p.tms as datem";
  249. $sql.= " FROM ".MAIN_DB_PREFIX."product as p";
  250. $sql.= " WHERE p.entity IN (".getEntity($product_static->element, 1).")";
  251. if ($type != '') $sql.= " AND p.fk_product_type = ".$type;
  252. // Add where from hooks
  253. $parameters=array();
  254. $reshook=$hookmanager->executeHooks('printFieldListWhere',$parameters); // Note that $action and $object may have been modified by hook
  255. $sql.=$hookmanager->resPrint;
  256. $sql.= $db->order("p.tms","DESC");
  257. $sql.= $db->plimit($max,0);
  258. //print $sql;
  259. $result = $db->query($sql);
  260. if ($result)
  261. {
  262. $num = $db->num_rows($result);
  263. $i = 0;
  264. if ($num > 0)
  265. {
  266. $transRecordedType = $langs->trans("LastModifiedProductsAndServices",$max);
  267. if (isset($_GET["type"]) && $_GET["type"] == 0) $transRecordedType = $langs->trans("LastRecordedProducts",$max);
  268. if (isset($_GET["type"]) && $_GET["type"] == 1) $transRecordedType = $langs->trans("LastRecordedServices",$max);
  269. print '<div class="div-table-responsive-no-min">';
  270. print '<table class="noborder" width="100%">';
  271. $colnb=4;
  272. if (empty($conf->global->PRODUIT_MULTIPRICES)) $colnb++;
  273. print '<tr class="liste_titre"><th colspan="'.$colnb.'">'.$transRecordedType.'</th>';
  274. print '<th class="right"><a href="'.DOL_URL_ROOT.'/product/list.php?sortfield=p.tms&sortorder=DESC">'.$langs->trans("FullList").'</td>';
  275. print '</tr>';
  276. while ($i < $num)
  277. {
  278. $objp = $db->fetch_object($result);
  279. //Multilangs
  280. if (! empty($conf->global->MAIN_MULTILANGS))
  281. {
  282. $sql = "SELECT label";
  283. $sql.= " FROM ".MAIN_DB_PREFIX."product_lang";
  284. $sql.= " WHERE fk_product=".$objp->rowid;
  285. $sql.= " AND lang='". $langs->getDefaultLang() ."'";
  286. $resultd = $db->query($sql);
  287. if ($resultd)
  288. {
  289. $objtp = $db->fetch_object($resultd);
  290. if ($objtp && $objtp->label != '') $objp->label = $objtp->label;
  291. }
  292. }
  293. print '<tr class="oddeven">';
  294. print '<td class="nowrap">';
  295. $product_static->id=$objp->rowid;
  296. $product_static->ref=$objp->ref;
  297. $product_static->label = $objp->label;
  298. $product_static->type=$objp->fk_product_type;
  299. $product_static->entity = $objp->entity;
  300. $product_static->status_batch = $objp->tobatch;
  301. print $product_static->getNomUrl(1,'',16);
  302. print "</td>\n";
  303. print '<td>'.dol_trunc($objp->label,32).'</td>';
  304. print "<td>";
  305. print dol_print_date($db->jdate($objp->datem),'day');
  306. print "</td>";
  307. // Sell price
  308. if (empty($conf->global->PRODUIT_MULTIPRICES))
  309. {
  310. if (!empty($conf->dynamicprices->enabled) && !empty($objp->fk_price_expression))
  311. {
  312. $product = new Product($db);
  313. $product->fetch($objp->rowid);
  314. $priceparser = new PriceParser($db);
  315. $price_result = $priceparser->parseProduct($product);
  316. if ($price_result >= 0) {
  317. $objp->price = $price_result;
  318. }
  319. }
  320. print '<td align="right">';
  321. if (isset($objp->price_base_type) && $objp->price_base_type == 'TTC') print price($objp->price_ttc).' '.$langs->trans("TTC");
  322. else print price($objp->price).' '.$langs->trans("HT");
  323. print '</td>';
  324. }
  325. print '<td align="right" class="nowrap"><span class="statusrefsell">';
  326. print $product_static->LibStatut($objp->tosell,3,0);
  327. print "</span></td>";
  328. print '<td align="right" class="nowrap"><span class="statusrefbuy">';
  329. print $product_static->LibStatut($objp->tobuy,3,1);
  330. print "</span></td>";
  331. print "</tr>\n";
  332. $i++;
  333. }
  334. $db->free($result);
  335. print "</table>";
  336. print '</div>';
  337. print '<br>';
  338. }
  339. }
  340. else
  341. {
  342. dol_print_error($db);
  343. }
  344. // TODO Move this into a page that should be available into menu "accountancy - report - turnover - per quarter"
  345. // Also method used for counting must provide the 2 possible methods like done by all other reports into menu "accountancy - report - turnover":
  346. // "commitment engagment" method and "cash accounting" method
  347. if (! empty($conf->global->MAIN_SHOW_PRODUCT_ACTIVITY_TRIM))
  348. {
  349. if (! empty($conf->product->enabled)) activitytrim(0);
  350. if (! empty($conf->service->enabled)) activitytrim(1);
  351. }
  352. print '</div></div></div>';
  353. llxFooter();
  354. $db->close();
  355. /*
  356. * Print html activity for product type
  357. *
  358. * @param int $product_type Type of product
  359. * @return void
  360. */
  361. function activitytrim($product_type)
  362. {
  363. global $conf,$langs,$db;
  364. global $bc;
  365. // We display the last 3 years
  366. $yearofbegindate=date('Y',dol_time_plus_duree(time(), -3, "y"));
  367. // breakdown by quarter
  368. $sql = "SELECT DATE_FORMAT(p.datep,'%Y') as annee, DATE_FORMAT(p.datep,'%m') as mois, SUM(fd.total_ht) as Mnttot";
  369. $sql.= " FROM ".MAIN_DB_PREFIX."facture as f, ".MAIN_DB_PREFIX."facturedet as fd";
  370. $sql.= " , ".MAIN_DB_PREFIX."paiement as p,".MAIN_DB_PREFIX."paiement_facture as pf";
  371. $sql.= " WHERE f.entity = " . $conf->entity;
  372. $sql.= " AND f.rowid = fd.fk_facture";
  373. $sql.= " AND pf.fk_facture = f.rowid";
  374. $sql.= " AND pf.fk_paiement= p.rowid";
  375. $sql.= " AND fd.product_type=".$product_type;
  376. $sql.= " AND p.datep >= '".$db->idate(dol_get_first_day($yearofbegindate),1)."'";
  377. $sql.= " GROUP BY annee, mois ";
  378. $sql.= " ORDER BY annee, mois ";
  379. $result = $db->query($sql);
  380. if ($result)
  381. {
  382. $tmpyear=0;
  383. $trim1=0;
  384. $trim2=0;
  385. $trim3=0;
  386. $trim4=0;
  387. $lgn = 0;
  388. $num = $db->num_rows($result);
  389. if ($num > 0 )
  390. {
  391. print '<table class="noborder" width="75%">';
  392. if ($product_type==0)
  393. print '<tr class="liste_titre"><td align=left>'.$langs->trans("ProductSellByQuarterHT").'</td>';
  394. else
  395. print '<tr class="liste_titre"><td align=left>'.$langs->trans("ServiceSellByQuarterHT").'</td>';
  396. print '<td align=right>'.$langs->trans("Quarter1").'</td>';
  397. print '<td align=right>'.$langs->trans("Quarter2").'</td>';
  398. print '<td align=right>'.$langs->trans("Quarter3").'</td>';
  399. print '<td align=right>'.$langs->trans("Quarter4").'</td>';
  400. print '<td align=right>'.$langs->trans("Total").'</td>';
  401. print '</tr>';
  402. }
  403. $i = 0;
  404. $var=true;
  405. while ($i < $num)
  406. {
  407. $objp = $db->fetch_object($result);
  408. if ($tmpyear != $objp->annee)
  409. {
  410. if ($trim1+$trim2+$trim3+$trim4 > 0)
  411. {
  412. print '<tr class="oddeven"><td align=left>'.$tmpyear.'</td>';
  413. print '<td align=right>'.price($trim1).'</td>';
  414. print '<td align=right>'.price($trim2).'</td>';
  415. print '<td align=right>'.price($trim3).'</td>';
  416. print '<td align=right>'.price($trim4).'</td>';
  417. print '<td align=right>'.price($trim1+$trim2+$trim3+$trim4).'</td>';
  418. print '</tr>';
  419. $lgn++;
  420. }
  421. // We go to the following year
  422. $tmpyear = $objp->annee;
  423. $trim1=0;
  424. $trim2=0;
  425. $trim3=0;
  426. $trim4=0;
  427. }
  428. if ($objp->mois == "01" || $objp->mois == "02" || $objp->mois == "03")
  429. $trim1 += $objp->Mnttot;
  430. if ($objp->mois == "04" || $objp->mois == "05" || $objp->mois == "06")
  431. $trim2 += $objp->Mnttot;
  432. if ($objp->mois == "07" || $objp->mois == "08" || $objp->mois == "09")
  433. $trim3 += $objp->Mnttot;
  434. if ($objp->mois == "10" || $objp->mois == "11" || $objp->mois == "12")
  435. $trim4 += $objp->Mnttot;
  436. $i++;
  437. }
  438. if ($trim1+$trim2+$trim3+$trim4 > 0)
  439. {
  440. print '<tr class="oddeven"><td align=left>'.$tmpyear.'</td>';
  441. print '<td align=right>'.price($trim1).'</td>';
  442. print '<td align=right>'.price($trim2).'</td>';
  443. print '<td align=right>'.price($trim3).'</td>';
  444. print '<td align=right>'.price($trim4).'</td>';
  445. print '<td align=right>'.price($trim1+$trim2+$trim3+$trim4).'</td>';
  446. print '</tr>';
  447. }
  448. if ($num > 0 )
  449. print '</table>';
  450. }
  451. }