projectstats.class.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. <?php
  2. /* Lead
  3. * Copyright (C) 2014-2015 Florian HENRY <florian.henry@open-concept.pro>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. include_once DOL_DOCUMENT_ROOT.'/core/class/stats.class.php';
  19. include_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  20. /**
  21. * Class to manage statistics on projects
  22. */
  23. class ProjectStats extends Stats
  24. {
  25. private $project;
  26. public $userid;
  27. public $socid;
  28. public $year;
  29. public $yearmonth;
  30. public $status;
  31. public $opp_status;
  32. //SQL stat
  33. public $field;
  34. public $from;
  35. public $where;
  36. /**
  37. * Constructor
  38. *
  39. * @param DoliDB $db Database handler
  40. */
  41. public function __construct($db)
  42. {
  43. global $conf, $user;
  44. $this->db = $db;
  45. require_once 'project.class.php';
  46. $this->project = new Project($this->db);
  47. $this->from = MAIN_DB_PREFIX.$this->project->table_element;
  48. $this->field = 'opp_amount';
  49. $this->where = " entity = ".$conf->entity;
  50. if ($this->socid > 0) {
  51. $this->where .= " AND fk_soc = ".((int) $this->socid);
  52. }
  53. if (is_array($this->userid) && count($this->userid) > 0) {
  54. $this->where .= ' AND fk_user IN ('.$this->db->sanitize(join(',', $this->userid)).')';
  55. } elseif ($this->userid > 0) {
  56. $this->where .= " AND fk_user = ".((int) $this->userid);
  57. }
  58. }
  59. /**
  60. * Return all leads grouped by opportunity status.
  61. * Warning: There is no filter on WON/LOST because we want this for statistics.
  62. *
  63. * @param int $limit Limit results
  64. * @return array|int Array with value or -1 if error
  65. * @throws Exception
  66. */
  67. public function getAllProjectByStatus($limit = 5)
  68. {
  69. global $conf, $user, $langs;
  70. $datay = array();
  71. $sql = "SELECT";
  72. $sql .= " SUM(t.opp_amount), t.fk_opp_status, cls.code, cls.label";
  73. $sql .= " FROM ".MAIN_DB_PREFIX."projet as t";
  74. // No check is done on company permission because readability is managed by public status of project and assignement.
  75. //if (! $user->rights->societe->client->voir && ! $user->socid)
  76. // $sql .= " INNER JOIN " . MAIN_DB_PREFIX . "societe_commerciaux as sc ON sc.fk_soc=t.fk_soc AND sc.fk_user = ".((int) $user->id);
  77. $sql .= ", ".MAIN_DB_PREFIX."c_lead_status as cls";
  78. $sql .= $this->buildWhere();
  79. // For external user, no check is done on company permission because readability is managed by public status of project and assignement.
  80. //if ($socid > 0) $sql.= " AND t.fk_soc = ".((int) $socid);
  81. // No check is done on company permission because readability is managed by public status of project and assignement.
  82. //if (! $user->rights->societe->client->voir && ! $socid) $sql.= " AND ((s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id).") OR (s.rowid IS NULL))";
  83. $sql .= " AND t.fk_opp_status = cls.rowid";
  84. $sql .= " AND t.fk_statut <> 0"; // We want historic also, so all projects not draft
  85. $sql .= " GROUP BY t.fk_opp_status, cls.code, cls.label";
  86. $result = array();
  87. dol_syslog(get_class($this).'::'.__METHOD__."", LOG_DEBUG);
  88. $resql = $this->db->query($sql);
  89. if ($resql) {
  90. $num = $this->db->num_rows($resql);
  91. $i = 0;
  92. $other = 0;
  93. while ($i < $num) {
  94. $row = $this->db->fetch_row($resql);
  95. if ($i < $limit || $num == $limit) {
  96. $label = (($langs->trans("OppStatus".$row[2]) != "OppStatus".$row[2]) ? $langs->trans("OppStatus".$row[2]) : $row[2]);
  97. $result[$i] = array(
  98. $label.' ('.price(price2num($row[0], 'MT'), 1, $langs, 1, -1, -1, $conf->currency).')',
  99. $row[0]
  100. );
  101. } else {
  102. $other += $row[1];
  103. }
  104. $i++;
  105. }
  106. if ($num > $limit) {
  107. $result[$i] = array(
  108. $langs->transnoentitiesnoconv("Other"),
  109. $other
  110. );
  111. }
  112. $this->db->free($resql);
  113. } else {
  114. $this->error = "Error ".$this->db->lasterror();
  115. dol_syslog(get_class($this).'::'.__METHOD__.' '.$this->error, LOG_ERR);
  116. return -1;
  117. }
  118. return $result;
  119. }
  120. /**
  121. * Return count, and sum of products
  122. *
  123. * @return array of values
  124. */
  125. public function getAllByYear()
  126. {
  127. global $conf, $user, $langs;
  128. $datay = array();
  129. $wonlostfilter = 0; // No filter on status WON/LOST
  130. $sql = "SELECT date_format(t.datec,'%Y') as year, COUNT(t.rowid) as nb, SUM(t.opp_amount) as total, AVG(t.opp_amount) as avg,";
  131. $sql .= " SUM(t.opp_amount * ".$this->db->ifsql("t.opp_percent IS NULL".($wonlostfilter ? " OR cls.code IN ('WON','LOST')" : ""), '0', 't.opp_percent')." / 100) as weighted";
  132. $sql .= " FROM ".MAIN_DB_PREFIX."projet as t LEFT JOIN ".MAIN_DB_PREFIX."c_lead_status as cls ON cls.rowid = t.fk_opp_status";
  133. // No check is done on company permission because readability is managed by public status of project and assignement.
  134. //if (! $user->rights->societe->client->voir && ! $user->soc_id)
  135. // $sql .= " INNER JOIN " . MAIN_DB_PREFIX . "societe_commerciaux as sc ON sc.fk_soc=t.fk_soc AND sc.fk_user = ".((int) $user->id);
  136. $sql .= $this->buildWhere();
  137. // For external user, no check is done on company permission because readability is managed by public status of project and assignement.
  138. //if ($socid > 0) $sql.= " AND t.fk_soc = ".((int) $socid);
  139. // No check is done on company permission because readability is managed by public status of project and assignement.
  140. //if (! $user->rights->societe->client->voir && ! $socid) $sql.= " AND ((s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id).") OR (s.rowid IS NULL))";
  141. $sql .= " GROUP BY year";
  142. $sql .= $this->db->order('year', 'DESC');
  143. return $this->_getAllByYear($sql);
  144. }
  145. /**
  146. * Build the where part
  147. *
  148. * @return string
  149. */
  150. public function buildWhere()
  151. {
  152. global $user;
  153. $sqlwhere_str = '';
  154. $sqlwhere = array();
  155. // Get list of project id allowed to user (in a string list separated by coma)
  156. $object = new Project($this->db);
  157. $projectsListId = '';
  158. if (empty($user->rights->projet->all->lire)) {
  159. $projectsListId = $object->getProjectsAuthorizedForUser($user, 0, 1, $user->socid);
  160. }
  161. $sqlwhere[] = ' t.entity IN ('.getEntity('project').')';
  162. if (!empty($this->userid)) {
  163. $sqlwhere[] = ' t.fk_user_resp = '.((int) $this->userid);
  164. }
  165. // Forced filter on socid is similar to forced filter on project. TODO Use project assignement to allow to not use filter on project
  166. if (!empty($this->socid)) {
  167. $sqlwhere[] = ' t.fk_soc = '.((int) $this->socid);
  168. }
  169. if (!empty($this->year) && empty($this->yearmonth)) {
  170. $sqlwhere[] = " date_format(t.datec,'%Y') = '".$this->db->escape($this->year)."'";
  171. }
  172. if (!empty($this->yearmonth)) {
  173. $sqlwhere[] = " t.datec BETWEEN '".$this->db->idate(dol_get_first_day($this->yearmonth))."' AND '".$this->db->idate(dol_get_last_day($this->yearmonth))."'";
  174. }
  175. if (!empty($this->status)) {
  176. $sqlwhere[] = " t.fk_statut IN (".$this->db->sanitize($this->status).")";
  177. }
  178. if (!empty($this->opp_status)) {
  179. if (is_numeric($this->opp_status) && $this->opp_status > 0) {
  180. $sqlwhere[] = " t.fk_opp_status = ".((int) $this->opp_status);
  181. }
  182. if ($this->opp_status == 'all') {
  183. $sqlwhere[] = " (t.fk_opp_status IS NOT NULL AND t.fk_opp_status <> -1)";
  184. }
  185. if ($this->opp_status == 'openedopp') {
  186. $sqlwhere[] = " (t.fk_opp_status IS NOT NULL AND t.fk_opp_status <> -1 AND t.fk_opp_status NOT IN (SELECT rowid FROM ".MAIN_DB_PREFIX."c_lead_status WHERE code IN ('WON','LOST')))";
  187. }
  188. if ($this->opp_status == 'notopenedopp') {
  189. $sqlwhere[] = " (t.fk_opp_status IS NULL OR t.fk_opp_status = -1 OR t.fk_opp_status IN (SELECT rowid FROM ".MAIN_DB_PREFIX."c_lead_status WHERE code = 'WON'))";
  190. }
  191. if ($this->opp_status == 'none') {
  192. $sqlwhere[] = " (t.fk_opp_status IS NULL OR t.fk_opp_status = -1)";
  193. }
  194. }
  195. if (empty($user->rights->projet->all->lire)) {
  196. $sqlwhere[] = " t.rowid IN (".$this->db->sanitize($projectsListId).")"; // public and assigned to, or restricted to company for external users
  197. }
  198. if (count($sqlwhere) > 0) {
  199. $sqlwhere_str = ' WHERE '.implode(' AND ', $sqlwhere);
  200. }
  201. return $sqlwhere_str;
  202. }
  203. /**
  204. * Return Project number by month for a year
  205. *
  206. * @param int $year Year to scan
  207. * @param int $format 0=Label of abscissa is a translated text, 1=Label of abscissa is month number, 2=Label of abscissa is first letter of month
  208. * @return array Array of values
  209. */
  210. public function getNbByMonth($year, $format = 0)
  211. {
  212. global $user;
  213. $this->yearmonth = $year;
  214. $sql = "SELECT date_format(t.datec,'%m') as dm, COUNT(*) as nb";
  215. $sql .= " FROM ".MAIN_DB_PREFIX."projet as t";
  216. // No check is done on company permission because readability is managed by public status of project and assignement.
  217. //if (! $user->rights->societe->client->voir && ! $user->soc_id)
  218. // $sql .= " INNER JOIN " . MAIN_DB_PREFIX . "societe_commerciaux as sc ON sc.fk_soc=t.fk_soc AND sc.fk_user = ".((int) $user->id);
  219. $sql .= $this->buildWhere();
  220. $sql .= " GROUP BY dm";
  221. $sql .= $this->db->order('dm', 'DESC');
  222. $this->yearmonth = 0;
  223. $res = $this->_getNbByMonth($year, $sql, $format);
  224. // var_dump($res);print '<br>';
  225. return $res;
  226. }
  227. /**
  228. * Return the Project amount by month for a year
  229. *
  230. * @param int $year Year to scan
  231. * @param int $format 0=Label of abscissa is a translated text, 1=Label of abscissa is month number, 2=Label of abscissa is first letter of month
  232. * @return array Array with amount by month
  233. */
  234. public function getAmountByMonth($year, $format = 0)
  235. {
  236. global $user;
  237. $this->yearmonth = $year;
  238. $sql = "SELECT date_format(t.datec,'%m') as dm, SUM(t.opp_amount)";
  239. $sql .= " FROM ".MAIN_DB_PREFIX."projet as t";
  240. // No check is done on company permission because readability is managed by public status of project and assignement.
  241. //if (! $user->rights->societe->client->voir && ! $user->soc_id)
  242. // $sql .= " INNER JOIN " . MAIN_DB_PREFIX . "societe_commerciaux as sc ON sc.fk_soc=t.fk_soc AND sc.fk_user = ".((int) $user->id);
  243. $sql .= $this->buildWhere();
  244. $sql .= " GROUP BY dm";
  245. $sql .= $this->db->order('dm', 'DESC');
  246. $this->yearmonth = 0;
  247. $res = $this->_getAmountByMonth($year, $sql, $format);
  248. // var_dump($res);print '<br>';
  249. return $res;
  250. }
  251. /**
  252. * Return amount of elements by month for several years
  253. *
  254. * @param int $endyear Start year
  255. * @param int $startyear End year
  256. * @param int $cachedelay Delay we accept for cache file (0=No read, no save of cache, -1=No read but save)
  257. * @param int $wonlostfilter Add a filter on status won/lost
  258. * @return array Array of values
  259. */
  260. public function getWeightedAmountByMonthWithPrevYear($endyear, $startyear, $cachedelay = 0, $wonlostfilter = 1)
  261. {
  262. global $conf, $user, $langs;
  263. if ($startyear > $endyear) {
  264. return -1;
  265. }
  266. $datay = array();
  267. // Search into cache
  268. if (!empty($cachedelay)) {
  269. include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  270. include_once DOL_DOCUMENT_ROOT.'/core/lib/json.lib.php';
  271. }
  272. $newpathofdestfile = $conf->user->dir_temp.'/'.get_class($this).'_'.__FUNCTION__.'_'.(empty($this->cachefilesuffix) ? '' : $this->cachefilesuffix.'_').$langs->defaultlang.'_user'.$user->id.'.cache';
  273. $newmask = '0644';
  274. $nowgmt = dol_now();
  275. $foundintocache = 0;
  276. if ($cachedelay > 0) {
  277. $filedate = dol_filemtime($newpathofdestfile);
  278. if ($filedate >= ($nowgmt - $cachedelay)) {
  279. $foundintocache = 1;
  280. $this->lastfetchdate[get_class($this).'_'.__FUNCTION__] = $filedate;
  281. } else {
  282. dol_syslog(get_class($this).'::'.__FUNCTION__." cache file ".$newpathofdestfile." is not found or older than now - cachedelay (".$nowgmt." - ".$cachedelay.") so we can't use it.");
  283. }
  284. }
  285. // Load file into $data
  286. if ($foundintocache) { // Cache file found and is not too old
  287. dol_syslog(get_class($this).'::'.__FUNCTION__." read data from cache file ".$newpathofdestfile." ".$filedate.".");
  288. $data = json_decode(file_get_contents($newpathofdestfile), true);
  289. } else {
  290. $year = $startyear;
  291. while ($year <= $endyear) {
  292. $datay[$year] = $this->getWeightedAmountByMonth($year, $wonlostfilter);
  293. $year++;
  294. }
  295. $data = array();
  296. // $data = array('xval'=>array(0=>xlabel,1=>yval1,2=>yval2...),...)
  297. for ($i = 0; $i < 12; $i++) {
  298. $data[$i][] = $datay[$endyear][$i][0]; // set label
  299. $year = $startyear;
  300. while ($year <= $endyear) {
  301. $data[$i][] = $datay[$year][$i][1]; // set yval for x=i
  302. $year++;
  303. }
  304. }
  305. }
  306. // Save cache file
  307. if (empty($foundintocache) && ($cachedelay > 0 || $cachedelay == -1)) {
  308. dol_syslog(get_class($this).'::'.__FUNCTION__." save cache file ".$newpathofdestfile." onto disk.");
  309. if (!dol_is_dir($conf->user->dir_temp)) {
  310. dol_mkdir($conf->user->dir_temp);
  311. }
  312. $fp = fopen($newpathofdestfile, 'w');
  313. if ($fp) {
  314. fwrite($fp, json_encode($data));
  315. fclose($fp);
  316. if (!empty($conf->global->MAIN_UMASK)) {
  317. $newmask = $conf->global->MAIN_UMASK;
  318. }
  319. @chmod($newpathofdestfile, octdec($newmask));
  320. } else {
  321. dol_syslog("Failed to write cache file", LOG_ERR);
  322. }
  323. $this->lastfetchdate[get_class($this).'_'.__FUNCTION__] = $nowgmt;
  324. }
  325. return $data;
  326. }
  327. /**
  328. * Return the Project weighted opp amount by month for a year.
  329. *
  330. * @param int $year Year to scan
  331. * @param int $wonlostfilter Add a filter on status won/lost
  332. * @return array Array with amount by month
  333. */
  334. public function getWeightedAmountByMonth($year, $wonlostfilter = 1)
  335. {
  336. global $user;
  337. $this->yearmonth = $year;
  338. $sql = "SELECT date_format(t.datec,'%m') as dm, SUM(t.opp_amount * ".$this->db->ifsql("t.opp_percent IS NULL".($wonlostfilter ? " OR cls.code IN ('WON','LOST')" : ""), '0', 't.opp_percent')." / 100)";
  339. $sql .= " FROM ".MAIN_DB_PREFIX."projet as t LEFT JOIN ".MAIN_DB_PREFIX.'c_lead_status as cls ON t.fk_opp_status = cls.rowid';
  340. // No check is done on company permission because readability is managed by public status of project and assignement.
  341. //if (! $user->rights->societe->client->voir && ! $user->soc_id)
  342. // $sql .= " INNER JOIN " . MAIN_DB_PREFIX . "societe_commerciaux as sc ON sc.fk_soc=t.fk_soc AND sc.fk_user = ".((int) $user->id);
  343. $sql .= $this->buildWhere();
  344. $sql .= " GROUP BY dm";
  345. $sql .= $this->db->order('dm', 'DESC');
  346. $this->yearmonth = 0;
  347. $res = $this->_getAmountByMonth($year, $sql);
  348. // var_dump($res);print '<br>';
  349. return $res;
  350. }
  351. /**
  352. * Return amount of elements by month for several years
  353. *
  354. * @param int $endyear End year
  355. * @param int $startyear Start year
  356. * @param int $cachedelay accept for cache file (0=No read, no save of cache, -1=No read but save)
  357. * @return array of values
  358. */
  359. public function getTransformRateByMonthWithPrevYear($endyear, $startyear, $cachedelay = 0)
  360. {
  361. global $conf, $user, $langs;
  362. if ($startyear > $endyear) {
  363. return -1;
  364. }
  365. $datay = array();
  366. // Search into cache
  367. if (!empty($cachedelay)) {
  368. include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  369. include_once DOL_DOCUMENT_ROOT.'/core/lib/json.lib.php';
  370. }
  371. $newpathofdestfile = $conf->user->dir_temp.'/'.get_class($this).'_'.__FUNCTION__.'_'.(empty($this->cachefilesuffix) ? '' : $this->cachefilesuffix.'_').$langs->defaultlang.'_user'.$user->id.'.cache';
  372. $newmask = '0644';
  373. $nowgmt = dol_now();
  374. $foundintocache = 0;
  375. if ($cachedelay > 0) {
  376. $filedate = dol_filemtime($newpathofdestfile);
  377. if ($filedate >= ($nowgmt - $cachedelay)) {
  378. $foundintocache = 1;
  379. $this->lastfetchdate[get_class($this).'_'.__FUNCTION__] = $filedate;
  380. } else {
  381. dol_syslog(get_class($this).'::'.__FUNCTION__." cache file ".$newpathofdestfile." is not found or older than now - cachedelay (".$nowgmt." - ".$cachedelay.") so we can't use it.");
  382. }
  383. }
  384. // Load file into $data
  385. if ($foundintocache) { // Cache file found and is not too old
  386. dol_syslog(get_class($this).'::'.__FUNCTION__." read data from cache file ".$newpathofdestfile." ".$filedate.".");
  387. $data = json_decode(file_get_contents($newpathofdestfile), true);
  388. } else {
  389. $year = $startyear;
  390. while ($year <= $endyear) {
  391. $datay[$year] = $this->getTransformRateByMonth($year);
  392. $year++;
  393. }
  394. $data = array();
  395. // $data = array('xval'=>array(0=>xlabel,1=>yval1,2=>yval2...),...)
  396. for ($i = 0; $i < 12; $i++) {
  397. $data[$i][] = $datay[$endyear][$i][0]; // set label
  398. $year = $startyear;
  399. while ($year <= $endyear) {
  400. $data[$i][] = $datay[$year][$i][1]; // set yval for x=i
  401. $year++;
  402. }
  403. }
  404. }
  405. // Save cache file
  406. if (empty($foundintocache) && ($cachedelay > 0 || $cachedelay == - 1)) {
  407. dol_syslog(get_class($this).'::'.__FUNCTION__." save cache file ".$newpathofdestfile." onto disk.");
  408. if (!dol_is_dir($conf->user->dir_temp)) {
  409. dol_mkdir($conf->user->dir_temp);
  410. }
  411. $fp = fopen($newpathofdestfile, 'w');
  412. fwrite($fp, json_encode($data));
  413. fclose($fp);
  414. if (!empty($conf->global->MAIN_UMASK)) {
  415. $newmask = $conf->global->MAIN_UMASK;
  416. }
  417. @chmod($newpathofdestfile, octdec($newmask));
  418. $this->lastfetchdate[get_class($this).'_'.__FUNCTION__] = $nowgmt;
  419. }
  420. return $data;
  421. }
  422. /**
  423. * Return the Project transformation rate by month for a year
  424. *
  425. * @param int $year Year to scan
  426. * @param int $format 0=Label of abscissa is a translated text, 1=Label of abscissa is month number, 2=Label of abscissa is first letter of month
  427. * @return array Array with amount by month
  428. */
  429. public function getTransformRateByMonth($year, $format = 0)
  430. {
  431. global $user;
  432. $this->yearmonth = $year;
  433. $sql = "SELECT date_format(t.datec,'%m') as dm, count(t.opp_amount)";
  434. $sql .= " FROM ".MAIN_DB_PREFIX."projet as t";
  435. // No check is done on company permission because readability is managed by public status of project and assignement.
  436. //if (! $user->rights->societe->client->voir && ! $user->soc_id)
  437. // $sql .= " INNER JOIN " . MAIN_DB_PREFIX . "societe_commerciaux as sc ON sc.fk_soc=t.fk_soc AND sc.fk_user = ".((int) $user->id);
  438. $sql .= $this->buildWhere();
  439. $sql .= " GROUP BY dm";
  440. $sql .= $this->db->order('dm', 'DESC');
  441. $res_total = $this->_getNbByMonth($year, $sql, $format);
  442. $this->status = 6;
  443. $sql = "SELECT date_format(t.datec,'%m') as dm, count(t.opp_amount)";
  444. $sql .= " FROM ".MAIN_DB_PREFIX."projet as t";
  445. // No check is done on company permission because readability is managed by public status of project and assignement.
  446. //if (! $user->rights->societe->client->voir && ! $user->soc_id)
  447. // $sql .= " INNER JOIN " . MAIN_DB_PREFIX . "societe_commerciaux as sc ON sc.fk_soc=t.fk_soc AND sc.fk_user = ".((int) $user->id);
  448. $sql .= $this->buildWhere();
  449. $sql .= " GROUP BY dm";
  450. $sql .= $this->db->order('dm', 'DESC');
  451. $this->status = 0;
  452. $this->yearmonth = 0;
  453. $res_only_wined = $this->_getNbByMonth($year, $sql, $format);
  454. $res = array();
  455. foreach ($res_total as $key => $total_row) {
  456. //var_dump($total_row);
  457. if (!empty($total_row[1])) {
  458. $res[$key] = array($total_row[0], (100 * $res_only_wined[$key][1]) / $total_row[1]);
  459. } else {
  460. $res[$key] = array($total_row[0], 0);
  461. }
  462. }
  463. // var_dump($res);print '<br>';
  464. return $res;
  465. }
  466. /**
  467. * Return average of entity by month
  468. * @param int $year year number
  469. * @return int value
  470. */
  471. protected function getAverageByMonth($year)
  472. {
  473. $sql = "SELECT date_format(datef,'%m') as dm, AVG(f.".$this->field.")";
  474. $sql .= " FROM ".$this->from;
  475. $sql .= " WHERE f.datef BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
  476. $sql .= " AND ".$this->where;
  477. $sql .= " GROUP BY dm";
  478. $sql .= $this->db->order('dm', 'DESC');
  479. return $this->_getAverageByMonth($year, $sql);
  480. }
  481. }