index.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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. $langs->load("products");
  38. $langs->load("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 '<table class="noborder" width="100%">';
  117. print '<tr class="liste_titre"><th colspan="2">'.$langs->trans("Statistics").'</th></tr>';
  118. if (! empty($conf->product->enabled))
  119. {
  120. $statProducts = '<tr class="oddeven">';
  121. $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>';
  122. $statProducts.= "</tr>";
  123. $statProducts.= '<tr class="oddeven">';
  124. $statProducts.= '<td><a href="list.php?type=0">'.$langs->trans("ProductsOnSaleOnly").'</a></td><td align="right">'.round($prodser[0][1]).'</td>';
  125. $statProducts.= "</tr>";
  126. $statProducts.= '<tr class="oddeven">';
  127. $statProducts.= '<td><a href="list.php?type=0">'.$langs->trans("ProductsOnPurchaseOnly").'</a></td><td align="right">'.round($prodser[0][2]).'</td>';
  128. $statProducts.= "</tr>";
  129. $statProducts.= '<tr class="oddeven">';
  130. $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>';
  131. $statProducts.= "</tr>";
  132. }
  133. if (! empty($conf->service->enabled))
  134. {
  135. $statServices = '<tr class="oddeven">';
  136. $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>';
  137. $statServices.= "</tr>";
  138. $statServices.= '<tr class="oddeven">';
  139. $statServices.= '<td><a href="list.php?type=1">'.$langs->trans("ServicesOnSaleOnly").'</a></td><td align="right">'.round($prodser[1][1]).'</td>';
  140. $statServices.= "</tr>";
  141. $statServices.= '<tr class="oddeven">';
  142. $statServices.= '<td><a href="list.php?type=1">'.$langs->trans("ServicesOnPurchaseOnly").'</a></td><td align="right">'.round($prodser[1][2]).'</td>';
  143. $statServices.= "</tr>";
  144. $statServices.= '<tr class="oddeven">';
  145. $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>';
  146. $statServices.= "</tr>";
  147. }
  148. $total=0;
  149. if ($type == '0')
  150. {
  151. print $statProducts;
  152. $total=round($prodser[0][0])+round($prodser[0][1])+round($prodser[0][2]);
  153. }
  154. else if ($type == '1')
  155. {
  156. print $statServices;
  157. $total=round($prodser[1][0])+round($prodser[1][1])+round($prodser[1][2]);
  158. }
  159. else
  160. {
  161. print $statProducts.$statServices;
  162. $total=round($prodser[1][0])+round($prodser[1][1])+round($prodser[1][2])+round($prodser[0][0])+round($prodser[0][1])+round($prodser[0][2]);
  163. }
  164. print '<tr class="liste_total"><td>'.$langs->trans("Total").'</td><td align="right">';
  165. print $total;
  166. print '</td></tr>';
  167. print '</table>';
  168. if (! empty($conf->categorie->enabled) && ! empty($conf->global->CATEGORY_GRAPHSTATS_ON_PRODUCTS))
  169. {
  170. require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
  171. print '<br>';
  172. print '<table class="noborder" width="100%">';
  173. print '<tr class="liste_titre"><th colspan="2">'.$langs->trans("Categories").'</th></tr>';
  174. print '<tr '.$bc[0].'><td align="center" colspan="2">';
  175. $sql = "SELECT c.label, count(*) as nb";
  176. $sql.= " FROM ".MAIN_DB_PREFIX."categorie_product as cs";
  177. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."categorie as c ON cs.fk_categorie = c.rowid";
  178. $sql.= " WHERE c.type = 0";
  179. $sql.= " AND c.entity IN (".getEntity('category').")";
  180. $sql.= " GROUP BY c.label";
  181. $total=0;
  182. $result = $db->query($sql);
  183. if ($result)
  184. {
  185. $num = $db->num_rows($result);
  186. $i=0;
  187. if (! empty($conf->use_javascript_ajax))
  188. {
  189. $dataseries=array();
  190. $rest=0;
  191. $nbmax=10;
  192. while ($i < $num)
  193. {
  194. $obj = $db->fetch_object($result);
  195. if ($i < $nbmax)
  196. $dataseries[]=array('label'=>$obj->label,'data'=>round($obj->nb));
  197. else
  198. $rest+=$obj->nb;
  199. $total+=$obj->nb;
  200. $i++;
  201. }
  202. if ($i > $nbmax)
  203. $dataseries[]=array('label'=>$langs->trans("Other"),'data'=>round($rest));
  204. $data=array('series'=>$dataseries);
  205. dol_print_graph('statscategproduct',300,180,$data,1,'pie',0);
  206. }
  207. else
  208. {
  209. $var=true;
  210. while ($i < $num)
  211. {
  212. $obj = $db->fetch_object($result);
  213. print '<tr $bc[$var]><td>'.$obj->label.'</td><td>'.$obj->nb.'</td></tr>';
  214. $total+=$obj->nb;
  215. $i++;
  216. }
  217. }
  218. }
  219. print '</td></tr>';
  220. print '<tr class="liste_total"><td>'.$langs->trans("Total").'</td><td align="right">';
  221. print $total;
  222. print '</td></tr>';
  223. print '</table>';
  224. }
  225. print '</div><div class="fichetwothirdright"><div class="ficheaddleft">';
  226. /*
  227. * Last modified products
  228. */
  229. $max=15;
  230. $sql = "SELECT p.rowid, p.label, p.price, p.ref, p.fk_product_type, p.tosell, p.tobuy, p.fk_price_expression,";
  231. $sql.= " p.entity,";
  232. $sql.= " p.tms as datem";
  233. $sql.= " FROM ".MAIN_DB_PREFIX."product as p";
  234. $sql.= " WHERE p.entity IN (".getEntity($product_static->element, 1).")";
  235. if ($type != '') $sql.= " AND p.fk_product_type = ".$type;
  236. // Add where from hooks
  237. $parameters=array();
  238. $reshook=$hookmanager->executeHooks('printFieldListWhere',$parameters); // Note that $action and $object may have been modified by hook
  239. $sql.=$hookmanager->resPrint;
  240. $sql.= $db->order("p.tms","DESC");
  241. $sql.= $db->plimit($max,0);
  242. //print $sql;
  243. $result = $db->query($sql);
  244. if ($result)
  245. {
  246. $num = $db->num_rows($result);
  247. $i = 0;
  248. if ($num > 0)
  249. {
  250. $transRecordedType = $langs->trans("LastModifiedProductsAndServices",$max);
  251. if (isset($_GET["type"]) && $_GET["type"] == 0) $transRecordedType = $langs->trans("LastRecordedProducts",$max);
  252. if (isset($_GET["type"]) && $_GET["type"] == 1) $transRecordedType = $langs->trans("LastRecordedServices",$max);
  253. print '<table class="noborder" width="100%">';
  254. $colnb=5;
  255. if (empty($conf->global->PRODUIT_MULTIPRICES)) $colnb++;
  256. print '<tr class="liste_titre"><th colspan="'.$colnb.'">'.$transRecordedType.'</th></tr>';
  257. $var=True;
  258. while ($i < $num)
  259. {
  260. $objp = $db->fetch_object($result);
  261. //Multilangs
  262. if (! empty($conf->global->MAIN_MULTILANGS))
  263. {
  264. $sql = "SELECT label";
  265. $sql.= " FROM ".MAIN_DB_PREFIX."product_lang";
  266. $sql.= " WHERE fk_product=".$objp->rowid;
  267. $sql.= " AND lang='". $langs->getDefaultLang() ."'";
  268. $resultd = $db->query($sql);
  269. if ($resultd)
  270. {
  271. $objtp = $db->fetch_object($resultd);
  272. if ($objtp && $objtp->label != '') $objp->label = $objtp->label;
  273. }
  274. }
  275. print '<tr class="oddeven">';
  276. print '<td class="nowrap">';
  277. $product_static->id=$objp->rowid;
  278. $product_static->ref=$objp->ref;
  279. $product_static->label = $objp->label;
  280. $product_static->type=$objp->fk_product_type;
  281. $product_static->entity = $objp->entity;
  282. print $product_static->getNomUrl(1,'',16);
  283. print "</td>\n";
  284. print '<td>'.dol_trunc($objp->label,32).'</td>';
  285. print "<td>";
  286. print dol_print_date($db->jdate($objp->datem),'day');
  287. print "</td>";
  288. // Sell price
  289. if (empty($conf->global->PRODUIT_MULTIPRICES))
  290. {
  291. if (!empty($conf->dynamicprices->enabled) && !empty($objp->fk_price_expression))
  292. {
  293. $product = new Product($db);
  294. $product->fetch($objp->rowid);
  295. $priceparser = new PriceParser($db);
  296. $price_result = $priceparser->parseProduct($product);
  297. if ($price_result >= 0) {
  298. $objp->price = $price_result;
  299. }
  300. }
  301. print '<td align="right">';
  302. if (isset($objp->price_base_type) && $objp->price_base_type == 'TTC') print price($objp->price_ttc).' '.$langs->trans("TTC");
  303. else print price($objp->price).' '.$langs->trans("HT");
  304. print '</td>';
  305. }
  306. print '<td align="right" class="nowrap">';
  307. print $product_static->LibStatut($objp->tosell,3,0);
  308. print "</td>";
  309. print '<td align="right" class="nowrap">';
  310. print $product_static->LibStatut($objp->tobuy,3,1);
  311. print "</td>";
  312. print "</tr>\n";
  313. $i++;
  314. }
  315. $db->free($result);
  316. print "</table>";
  317. print '<br>';
  318. }
  319. }
  320. else
  321. {
  322. dol_print_error($db);
  323. }
  324. // TODO Move this into a page that should be available into menu "accountancy - report - turnover - per quarter"
  325. // Also method used for counting must provide the 2 possible methods like done by all other reports into menu "accountancy - report - turnover":
  326. // "commitment engagment" method and "cash accounting" method
  327. if (! empty($conf->global->MAIN_SHOW_PRODUCT_ACTIVITY_TRIM))
  328. {
  329. if (! empty($conf->product->enabled)) activitytrim(0);
  330. if (! empty($conf->service->enabled)) activitytrim(1);
  331. }
  332. print '</div></div></div>';
  333. llxFooter();
  334. $db->close();
  335. /*
  336. * Print html activity for product type
  337. *
  338. * @param int $product_type Type of product
  339. * @return void
  340. */
  341. function activitytrim($product_type)
  342. {
  343. global $conf,$langs,$db;
  344. global $bc;
  345. // We display the last 3 years
  346. $yearofbegindate=date('Y',dol_time_plus_duree(time(), -3, "y"));
  347. // breakdown by quarter
  348. $sql = "SELECT DATE_FORMAT(p.datep,'%Y') as annee, DATE_FORMAT(p.datep,'%m') as mois, SUM(fd.total_ht) as Mnttot";
  349. $sql.= " FROM ".MAIN_DB_PREFIX."facture as f, ".MAIN_DB_PREFIX."facturedet as fd";
  350. $sql.= " , ".MAIN_DB_PREFIX."paiement as p,".MAIN_DB_PREFIX."paiement_facture as pf";
  351. $sql.= " WHERE f.entity = " . $conf->entity;
  352. $sql.= " AND f.rowid = fd.fk_facture";
  353. $sql.= " AND pf.fk_facture = f.rowid";
  354. $sql.= " AND pf.fk_paiement= p.rowid";
  355. $sql.= " AND fd.product_type=".$product_type;
  356. $sql.= " AND p.datep >= '".$db->idate(dol_get_first_day($yearofbegindate),1)."'";
  357. $sql.= " GROUP BY annee, mois ";
  358. $sql.= " ORDER BY annee, mois ";
  359. $result = $db->query($sql);
  360. if ($result)
  361. {
  362. $tmpyear=0;
  363. $trim1=0;
  364. $trim2=0;
  365. $trim3=0;
  366. $trim4=0;
  367. $lgn = 0;
  368. $num = $db->num_rows($result);
  369. if ($num > 0 )
  370. {
  371. print '<table class="noborder" width="75%">';
  372. if ($product_type==0)
  373. print '<tr class="liste_titre"><td align=left>'.$langs->trans("ProductSellByQuarterHT").'</td>';
  374. else
  375. print '<tr class="liste_titre"><td align=left>'.$langs->trans("ServiceSellByQuarterHT").'</td>';
  376. print '<td align=right>'.$langs->trans("Quarter1").'</td>';
  377. print '<td align=right>'.$langs->trans("Quarter2").'</td>';
  378. print '<td align=right>'.$langs->trans("Quarter3").'</td>';
  379. print '<td align=right>'.$langs->trans("Quarter4").'</td>';
  380. print '<td align=right>'.$langs->trans("Total").'</td>';
  381. print '</tr>';
  382. }
  383. $i = 0;
  384. $var=true;
  385. while ($i < $num)
  386. {
  387. $objp = $db->fetch_object($result);
  388. if ($tmpyear != $objp->annee)
  389. {
  390. if ($trim1+$trim2+$trim3+$trim4 > 0)
  391. {
  392. print '<tr class="oddeven"><td align=left>'.$tmpyear.'</td>';
  393. print '<td align=right>'.price($trim1).'</td>';
  394. print '<td align=right>'.price($trim2).'</td>';
  395. print '<td align=right>'.price($trim3).'</td>';
  396. print '<td align=right>'.price($trim4).'</td>';
  397. print '<td align=right>'.price($trim1+$trim2+$trim3+$trim4).'</td>';
  398. print '</tr>';
  399. $lgn++;
  400. }
  401. // We go to the following year
  402. $tmpyear = $objp->annee;
  403. $trim1=0;
  404. $trim2=0;
  405. $trim3=0;
  406. $trim4=0;
  407. }
  408. if ($objp->mois == "01" || $objp->mois == "02" || $objp->mois == "03")
  409. $trim1 += $objp->Mnttot;
  410. if ($objp->mois == "04" || $objp->mois == "05" || $objp->mois == "06")
  411. $trim2 += $objp->Mnttot;
  412. if ($objp->mois == "07" || $objp->mois == "08" || $objp->mois == "09")
  413. $trim3 += $objp->Mnttot;
  414. if ($objp->mois == "10" || $objp->mois == "11" || $objp->mois == "12")
  415. $trim4 += $objp->Mnttot;
  416. $i++;
  417. }
  418. if ($trim1+$trim2+$trim3+$trim4 > 0)
  419. {
  420. print '<tr class="oddeven"><td align=left>'.$tmpyear.'</td>';
  421. print '<td align=right>'.price($trim1).'</td>';
  422. print '<td align=right>'.price($trim2).'</td>';
  423. print '<td align=right>'.price($trim3).'</td>';
  424. print '<td align=right>'.price($trim4).'</td>';
  425. print '<td align=right>'.price($trim1+$trim2+$trim3+$trim4).'</td>';
  426. print '</tr>';
  427. }
  428. if ($num > 0 )
  429. print '</table>';
  430. }
  431. }