project.class.php 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927
  1. <?php
  2. /* Copyright (C) 2002-2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2005-2016 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005-2010 Regis Houssin <regis.houssin@capnetworks.com>
  5. * Copyright (C) 2013 Florian Henry <florian.henry@open-concept.pro>
  6. * Copyright (C) 2014-2017 Marcos García <marcosgdf@gmail.com>
  7. * Copyright (C) 2017 Ferran Marcet <fmarcet@2byte.es>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. /**
  23. * \file htdocs/projet/class/project.class.php
  24. * \ingroup projet
  25. * \brief File of class to manage projects
  26. */
  27. require_once DOL_DOCUMENT_ROOT . '/core/class/commonobject.class.php';
  28. /**
  29. * Class to manage projects
  30. */
  31. class Project extends CommonObject
  32. {
  33. public $element = 'project'; //!< Id that identify managed objects
  34. public $table_element = 'projet'; //!< Name of table without prefix where object is stored
  35. public $table_element_line = 'projet_task';
  36. public $fk_element = 'fk_projet';
  37. protected $ismultientitymanaged = 1; // 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
  38. public $picto = 'projectpub';
  39. /**
  40. * {@inheritdoc}
  41. */
  42. protected $table_ref_field = 'ref';
  43. var $description;
  44. /**
  45. * @var string
  46. * @deprecated
  47. * @see title
  48. */
  49. public $titre;
  50. var $title;
  51. var $date_start;
  52. var $date_end;
  53. var $date_close;
  54. var $socid; // To store id of thirdparty
  55. var $thirdparty_name; // To store name of thirdparty (defined only in some cases)
  56. var $user_author_id; //!< Id of project creator. Not defined if shared project.
  57. var $user_close_id;
  58. var $public; //!< Tell if this is a public or private project
  59. var $budget_amount;
  60. var $statuts_short;
  61. var $statuts_long;
  62. var $statut; // 0=draft, 1=opened, 2=closed
  63. var $opp_status; // opportunity status, into table llx_c_lead_status
  64. var $opp_percent; // opportunity probability
  65. var $oldcopy;
  66. var $weekWorkLoad; // Used to store workload details of a projet
  67. var $weekWorkLoadPerTask; // Used to store workload details of tasks of a projet
  68. /**
  69. * @var int Creation date
  70. * @deprecated
  71. * @see date_c
  72. */
  73. public $datec;
  74. /**
  75. * @var int Creation date
  76. */
  77. public $date_c;
  78. /**
  79. * @var int Modification date
  80. * @deprecated
  81. * @see date_m
  82. */
  83. public $datem;
  84. /**
  85. * @var int Modification date
  86. */
  87. public $date_m;
  88. /**
  89. * @var Task[]
  90. */
  91. public $lines;
  92. /**
  93. * Constructor
  94. *
  95. * @param DoliDB $db Database handler
  96. */
  97. function __construct($db)
  98. {
  99. $this->db = $db;
  100. $this->statuts_short = array(0 => 'Draft', 1 => 'Opened', 2 => 'Closed');
  101. $this->statuts_long = array(0 => 'Draft', 1 => 'Opened', 2 => 'Closed');
  102. }
  103. /**
  104. * Create a project into database
  105. *
  106. * @param User $user User making creation
  107. * @param int $notrigger Disable triggers
  108. * @return int <0 if KO, id of created project if OK
  109. */
  110. function create($user, $notrigger=0)
  111. {
  112. global $conf, $langs;
  113. $error = 0;
  114. $ret = 0;
  115. $now=dol_now();
  116. // Check parameters
  117. if (!trim($this->ref))
  118. {
  119. $this->error = 'ErrorFieldsRequired';
  120. dol_syslog(get_class($this)."::create error -1 ref null", LOG_ERR);
  121. return -1;
  122. }
  123. if (! empty($conf->global->PROJECT_THIRDPARTY_REQUIRED) && ! $this->socid > 0)
  124. {
  125. $this->error = 'ErrorFieldsRequired';
  126. dol_syslog(get_class($this)."::create error -1 ref null", LOG_ERR);
  127. return -1;
  128. }
  129. $this->db->begin();
  130. $sql = "INSERT INTO " . MAIN_DB_PREFIX . "projet (";
  131. $sql.= "ref";
  132. $sql.= ", title";
  133. $sql.= ", description";
  134. $sql.= ", fk_soc";
  135. $sql.= ", fk_user_creat";
  136. $sql.= ", fk_statut";
  137. $sql.= ", fk_opp_status";
  138. $sql.= ", opp_percent";
  139. $sql.= ", public";
  140. $sql.= ", datec";
  141. $sql.= ", dateo";
  142. $sql.= ", datee";
  143. $sql.= ", opp_amount";
  144. $sql.= ", budget_amount";
  145. $sql.= ", entity";
  146. $sql.= ") VALUES (";
  147. $sql.= "'" . $this->db->escape($this->ref) . "'";
  148. $sql.= ", '" . $this->db->escape($this->title) . "'";
  149. $sql.= ", '" . $this->db->escape($this->description) . "'";
  150. $sql.= ", " . ($this->socid > 0 ? $this->socid : "null");
  151. $sql.= ", " . $user->id;
  152. $sql.= ", ".(is_numeric($this->statut) ? $this->statut : '0');
  153. $sql.= ", ".(is_numeric($this->opp_status) ? $this->opp_status : 'NULL');
  154. $sql.= ", ".(is_numeric($this->opp_percent) ? $this->opp_percent : 'NULL');
  155. $sql.= ", " . ($this->public ? 1 : 0);
  156. $sql.= ", '".$this->db->idate($now)."'";
  157. $sql.= ", " . ($this->date_start != '' ? "'".$this->db->idate($this->date_start)."'" : 'null');
  158. $sql.= ", " . ($this->date_end != '' ? "'".$this->db->idate($this->date_end)."'" : 'null');
  159. $sql.= ", " . (strcmp($this->opp_amount,'') ? price2num($this->opp_amount) : 'null');
  160. $sql.= ", " . (strcmp($this->budget_amount,'') ? price2num($this->budget_amount) : 'null');
  161. $sql.= ", ".$conf->entity;
  162. $sql.= ")";
  163. dol_syslog(get_class($this)."::create", LOG_DEBUG);
  164. $resql = $this->db->query($sql);
  165. if ($resql)
  166. {
  167. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . "projet");
  168. $ret = $this->id;
  169. if (!$notrigger)
  170. {
  171. // Call trigger
  172. $result=$this->call_trigger('PROJECT_CREATE',$user);
  173. if ($result < 0) { $error++; }
  174. // End call triggers
  175. }
  176. }
  177. else
  178. {
  179. $this->error = $this->db->lasterror();
  180. $this->errno = $this->db->lasterrno();
  181. $error++;
  182. }
  183. // Update extrafield
  184. if (!$error) {
  185. if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) // For avoid conflicts if trigger used
  186. {
  187. $result=$this->insertExtraFields();
  188. if ($result < 0)
  189. {
  190. $error++;
  191. }
  192. }
  193. }
  194. if (!$error && !empty($conf->global->MAIN_DISABLEDRAFTSTATUS))
  195. {
  196. $res = $this->setValid($user);
  197. if ($res < 0) $error++;
  198. }
  199. if (!$error)
  200. {
  201. $this->db->commit();
  202. return $ret;
  203. }
  204. else
  205. {
  206. $this->db->rollback();
  207. return -1;
  208. }
  209. }
  210. /**
  211. * Update a project
  212. *
  213. * @param User $user User object of making update
  214. * @param int $notrigger 1=Disable all triggers
  215. * @return int <=0 if KO, >0 if OK
  216. */
  217. function update($user, $notrigger=0)
  218. {
  219. global $langs, $conf;
  220. $error=0;
  221. // Clean parameters
  222. $this->title = trim($this->title);
  223. $this->description = trim($this->description);
  224. if ($this->opp_amount < 0) $this->opp_amount='';
  225. if ($this->opp_percent < 0) $this->opp_percent='';
  226. if ($this->date_end && $this->date_end < $this->date_start)
  227. {
  228. $this->error = $langs->trans("ErrorDateEndLowerThanDateStart");
  229. $this->errors[] = $this->error;
  230. $this->db->rollback();
  231. dol_syslog(get_class($this)."::update error -3 " . $this->error, LOG_ERR);
  232. return -3;
  233. }
  234. if (dol_strlen(trim($this->ref)) > 0)
  235. {
  236. $this->db->begin();
  237. $sql = "UPDATE " . MAIN_DB_PREFIX . "projet SET";
  238. $sql.= " ref='" . $this->db->escape($this->ref) . "'";
  239. $sql.= ", title = '" . $this->db->escape($this->title) . "'";
  240. $sql.= ", description = '" . $this->db->escape($this->description) . "'";
  241. $sql.= ", fk_soc = " . ($this->socid > 0 ? $this->socid : "null");
  242. $sql.= ", fk_statut = " . $this->statut;
  243. $sql.= ", fk_opp_status = " . ((is_numeric($this->opp_status) && $this->opp_status > 0) ? $this->opp_status : 'null');
  244. $sql.= ", opp_percent = " . ((is_numeric($this->opp_percent) && $this->opp_percent != '') ? $this->opp_percent : 'null');
  245. $sql.= ", public = " . ($this->public ? 1 : 0);
  246. $sql.= ", datec=" . ($this->date_c != '' ? "'".$this->db->idate($this->date_c)."'" : 'null');
  247. $sql.= ", dateo=" . ($this->date_start != '' ? "'".$this->db->idate($this->date_start)."'" : 'null');
  248. $sql.= ", datee=" . ($this->date_end != '' ? "'".$this->db->idate($this->date_end)."'" : 'null');
  249. $sql.= ", date_close=" . ($this->date_close != '' ? "'".$this->db->idate($this->date_close)."'" : 'null');
  250. $sql.= ", fk_user_close=" . ($this->fk_user_close > 0 ? $this->fk_user_close : "null");
  251. $sql.= ", opp_amount = " . (strcmp($this->opp_amount, '') ? price2num($this->opp_amount) : "null");
  252. $sql.= ", budget_amount = " . (strcmp($this->budget_amount, '') ? price2num($this->budget_amount) : "null");
  253. $sql.= " WHERE rowid = " . $this->id;
  254. dol_syslog(get_class($this)."::update", LOG_DEBUG);
  255. $resql=$this->db->query($sql);
  256. if ($resql)
  257. {
  258. if (!$notrigger)
  259. {
  260. // Call trigger
  261. $result=$this->call_trigger('PROJECT_MODIFY',$user);
  262. if ($result < 0) { $error++; }
  263. // End call triggers
  264. }
  265. //Update extrafield
  266. if (!$error) {
  267. if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) // For avoid conflicts if trigger used
  268. {
  269. $result=$this->insertExtraFields();
  270. if ($result < 0)
  271. {
  272. $error++;
  273. }
  274. }
  275. }
  276. if (! $error && (is_object($this->oldcopy) && $this->oldcopy->ref !== $this->ref))
  277. {
  278. // We remove directory
  279. if ($conf->projet->dir_output)
  280. {
  281. $olddir = $conf->projet->dir_output . "/" . dol_sanitizeFileName($this->oldcopy->ref);
  282. $newdir = $conf->projet->dir_output . "/" . dol_sanitizeFileName($this->ref);
  283. if (file_exists($olddir))
  284. {
  285. include_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php';
  286. $res=dol_move($olddir, $newdir);
  287. if (! $res)
  288. {
  289. $langs->load("errors");
  290. $this->error=$langs->trans('ErrorFailToRenameDir',$olddir,$newdir);
  291. $error++;
  292. }
  293. }
  294. }
  295. }
  296. if (! $error )
  297. {
  298. $this->db->commit();
  299. $result = 1;
  300. }
  301. else
  302. {
  303. $this->db->rollback();
  304. $result = -1;
  305. }
  306. }
  307. else
  308. {
  309. $this->error = $this->db->lasterror();
  310. $this->errors[] = $this->error;
  311. $this->db->rollback();
  312. if ($this->db->lasterrno() == 'DB_ERROR_RECORD_ALREADY_EXISTS')
  313. {
  314. $result = -4;
  315. }
  316. else
  317. {
  318. $result = -2;
  319. }
  320. dol_syslog(get_class($this)."::update error " . $result . " " . $this->error, LOG_ERR);
  321. }
  322. }
  323. else
  324. {
  325. dol_syslog(get_class($this)."::update ref null");
  326. $result = -1;
  327. }
  328. return $result;
  329. }
  330. /**
  331. * Get object from database
  332. *
  333. * @param int $id Id of object to load
  334. * @param string $ref Ref of project
  335. * @return int >0 if OK, 0 if not found, <0 if KO
  336. */
  337. function fetch($id, $ref='')
  338. {
  339. if (empty($id) && empty($ref)) return -1;
  340. $sql = "SELECT rowid, ref, title, description, public, datec, opp_amount, budget_amount,";
  341. $sql.= " tms, dateo, datee, date_close, fk_soc, fk_user_creat, fk_user_close, fk_statut, fk_opp_status, opp_percent, note_private, note_public, model_pdf";
  342. $sql.= " FROM " . MAIN_DB_PREFIX . "projet";
  343. if (! empty($id))
  344. {
  345. $sql.= " WHERE rowid=".$id;
  346. }
  347. else if (! empty($ref))
  348. {
  349. $sql.= " WHERE ref='".$this->db->escape($ref)."'";
  350. $sql.= " AND entity IN (".getEntity('project').")";
  351. }
  352. dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
  353. $resql = $this->db->query($sql);
  354. if ($resql)
  355. {
  356. if ($this->db->num_rows($resql))
  357. {
  358. $obj = $this->db->fetch_object($resql);
  359. $this->id = $obj->rowid;
  360. $this->ref = $obj->ref;
  361. $this->title = $obj->title;
  362. $this->titre = $obj->title; // TODO deprecated
  363. $this->description = $obj->description;
  364. $this->date_c = $this->db->jdate($obj->datec);
  365. $this->datec = $this->db->jdate($obj->datec); // TODO deprecated
  366. $this->date_m = $this->db->jdate($obj->tms);
  367. $this->datem = $this->db->jdate($obj->tms); // TODO deprecated
  368. $this->date_start = $this->db->jdate($obj->dateo);
  369. $this->date_end = $this->db->jdate($obj->datee);
  370. $this->date_close = $this->db->jdate($obj->date_close);
  371. $this->note_private = $obj->note_private;
  372. $this->note_public = $obj->note_public;
  373. $this->socid = $obj->fk_soc;
  374. $this->user_author_id = $obj->fk_user_creat;
  375. $this->user_close_id = $obj->fk_user_close;
  376. $this->public = $obj->public;
  377. $this->statut = $obj->fk_statut;
  378. $this->opp_status = $obj->fk_opp_status;
  379. $this->opp_amount = $obj->opp_amount;
  380. $this->opp_percent = $obj->opp_percent;
  381. $this->budget_amount = $obj->budget_amount;
  382. $this->modelpdf = $obj->model_pdf;
  383. $this->db->free($resql);
  384. return 1;
  385. }
  386. else
  387. {
  388. return 0;
  389. }
  390. }
  391. else
  392. {
  393. $this->error = $this->db->lasterror();
  394. return -1;
  395. }
  396. }
  397. /**
  398. * Return list of projects
  399. *
  400. * @param int $socid To filter on a particular third party
  401. * @return array List of projects
  402. */
  403. function liste_array($socid='')
  404. {
  405. global $conf;
  406. $projects = array();
  407. $sql = "SELECT rowid, title";
  408. $sql.= " FROM " . MAIN_DB_PREFIX . "projet";
  409. $sql.= " WHERE entity = " . $conf->entity;
  410. if (! empty($socid)) $sql.= " AND fk_soc = " . $socid;
  411. $resql = $this->db->query($sql);
  412. if ($resql)
  413. {
  414. $nump = $this->db->num_rows($resql);
  415. if ($nump)
  416. {
  417. $i = 0;
  418. while ($i < $nump)
  419. {
  420. $obj = $this->db->fetch_object($resql);
  421. $projects[$obj->rowid] = $obj->title;
  422. $i++;
  423. }
  424. }
  425. return $projects;
  426. }
  427. else
  428. {
  429. print $this->db->lasterror();
  430. }
  431. }
  432. /**
  433. * Return list of elements for type, linked to project
  434. *
  435. * @param string $type 'propal','order','invoice','order_supplier','invoice_supplier',...
  436. * @param string $tablename name of table associated of the type
  437. * @param string $datefieldname name of date field for filter
  438. * @param string $dates Start date (ex 00:00:00)
  439. * @param string $datee End date (ex 23:59:59)
  440. * @return mixed Array list of object ids linked to project, < 0 or string if error
  441. */
  442. function get_element_list($type, $tablename, $datefieldname='', $dates='', $datee='')
  443. {
  444. $elements = array();
  445. if ($this->id <= 0) return $elements;
  446. if ($type == 'agenda')
  447. {
  448. $sql = "SELECT id as rowid FROM " . MAIN_DB_PREFIX . "actioncomm WHERE fk_project=" . $this->id;
  449. }
  450. elseif ($type == 'expensereport')
  451. {
  452. $sql = "SELECT ed.rowid FROM " . MAIN_DB_PREFIX . "expensereport as e, " . MAIN_DB_PREFIX . "expensereport_det as ed WHERE e.rowid = ed.fk_expensereport AND ed.fk_projet=" . $this->id;
  453. }
  454. elseif ($type == 'project_task')
  455. {
  456. $sql = "SELECT DISTINCT pt.rowid FROM " . MAIN_DB_PREFIX . "projet_task as pt, " . MAIN_DB_PREFIX . "projet_task_time as ptt WHERE pt.rowid = ptt.fk_task AND pt.fk_projet=" . $this->id;
  457. }
  458. elseif ($type == 'project_task_time') // Case we want to duplicate line foreach user
  459. {
  460. $sql = "SELECT DISTINCT pt.rowid, ptt.fk_user FROM " . MAIN_DB_PREFIX . "projet_task as pt, " . MAIN_DB_PREFIX . "projet_task_time as ptt WHERE pt.rowid = ptt.fk_task AND pt.fk_projet=" . $this->id;
  461. }
  462. elseif ($type == 'stock_mouvement')
  463. {
  464. $sql = 'SELECT ms.rowid, ms.fk_user_author as fk_user FROM ' . MAIN_DB_PREFIX . 'stock_mouvement as ms WHERE ms.origintype = "project" AND ms.fk_origin = ' . $this->id . ' AND ms.type_mouvement = 1';
  465. }
  466. else
  467. {
  468. $sql = "SELECT rowid FROM " . MAIN_DB_PREFIX . $tablename." WHERE fk_projet=" . $this->id;
  469. }
  470. if ($dates > 0)
  471. {
  472. if (empty($datefieldname) && ! empty($this->table_element_date)) $datefieldname=$this->table_element_date;
  473. if (empty($datefieldname)) return 'Error this object has no date field defined';
  474. $sql.=" AND (".$datefieldname." >= '".$this->db->idate($dates)."' OR ".$datefieldname." IS NULL)";
  475. }
  476. if ($datee > 0)
  477. {
  478. if (empty($datefieldname) && ! empty($this->table_element_date)) $datefieldname=$this->table_element_date;
  479. if (empty($datefieldname)) return 'Error this object has no date field defined';
  480. $sql.=" AND (".$datefieldname." <= '".$this->db->idate($datee)."' OR ".$datefieldname." IS NULL)";
  481. }
  482. if (! $sql) return -1;
  483. //print $sql;
  484. dol_syslog(get_class($this)."::get_element_list", LOG_DEBUG);
  485. $result = $this->db->query($sql);
  486. if ($result)
  487. {
  488. $nump = $this->db->num_rows($result);
  489. if ($nump)
  490. {
  491. $i = 0;
  492. while ($i < $nump)
  493. {
  494. $obj = $this->db->fetch_object($result);
  495. $elements[$i] = $obj->rowid.(empty($obj->fk_user)?'':'_'.$obj->fk_user);
  496. $i++;
  497. }
  498. $this->db->free($result);
  499. /* Return array */
  500. return $elements;
  501. }
  502. }
  503. else
  504. {
  505. dol_print_error($this->db);
  506. }
  507. }
  508. /**
  509. * Delete a project from database
  510. *
  511. * @param User $user User
  512. * @param int $notrigger Disable triggers
  513. * @return int <0 if KO, 0 if not possible, >0 if OK
  514. */
  515. function delete($user, $notrigger=0)
  516. {
  517. global $langs, $conf;
  518. require_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php';
  519. $error = 0;
  520. $this->db->begin();
  521. if (!$error)
  522. {
  523. // Delete linked contacts
  524. $res = $this->delete_linked_contact();
  525. if ($res < 0)
  526. {
  527. $this->error = 'ErrorFailToDeleteLinkedContact';
  528. //$error++;
  529. $this->db->rollback();
  530. return 0;
  531. }
  532. }
  533. // Set fk_projet into elements to null
  534. $listoftables=array(
  535. 'facture'=>'fk_projet','propal'=>'fk_projet','commande'=>'fk_projet',
  536. 'facture_fourn'=>'fk_projet','commande_fournisseur'=>'fk_projet','supplier_proposal'=>'fk_projet',
  537. 'expensereport_det'=>'fk_projet','contrat'=>'fk_projet','fichinter'=>'fk_projet','don'=>'fk_projet'
  538. );
  539. foreach($listoftables as $key => $value)
  540. {
  541. $sql = "UPDATE " . MAIN_DB_PREFIX . $key . " SET ".$value." = NULL where ".$value." = ". $this->id;
  542. $resql = $this->db->query($sql);
  543. if (!$resql)
  544. {
  545. $this->errors[] = $this->db->lasterror();
  546. $error++;
  547. break;
  548. }
  549. }
  550. // Delete tasks
  551. if (! $error)
  552. {
  553. $sql = "DELETE FROM " . MAIN_DB_PREFIX . "projet_task_time";
  554. $sql.= " WHERE fk_task IN (SELECT rowid FROM " . MAIN_DB_PREFIX . "projet_task WHERE fk_projet=" . $this->id . ")";
  555. $resql = $this->db->query($sql);
  556. if (!$resql)
  557. {
  558. $this->errors[] = $this->db->lasterror();
  559. $error++;
  560. }
  561. }
  562. if (! $error)
  563. {
  564. $sql = "DELETE FROM " . MAIN_DB_PREFIX . "projet_task_extrafields";
  565. $sql.= " WHERE fk_object IN (SELECT rowid FROM " . MAIN_DB_PREFIX . "projet_task WHERE fk_projet=" . $this->id . ")";
  566. $resql = $this->db->query($sql);
  567. if (!$resql)
  568. {
  569. $this->errors[] = $this->db->lasterror();
  570. $error++;
  571. }
  572. }
  573. if (! $error)
  574. {
  575. $sql = "DELETE FROM " . MAIN_DB_PREFIX . "projet_task";
  576. $sql.= " WHERE fk_projet=" . $this->id;
  577. $resql = $this->db->query($sql);
  578. if (!$resql)
  579. {
  580. $this->errors[] = $this->db->lasterror();
  581. $error++;
  582. }
  583. }
  584. // Delete project
  585. if (! $error)
  586. {
  587. $sql = "DELETE FROM " . MAIN_DB_PREFIX . "projet";
  588. $sql.= " WHERE rowid=" . $this->id;
  589. $resql = $this->db->query($sql);
  590. if (!$resql)
  591. {
  592. $this->errors[] = $langs->trans("CantRemoveProject");
  593. $error++;
  594. }
  595. }
  596. if (! $error)
  597. {
  598. $sql = "DELETE FROM " . MAIN_DB_PREFIX . "projet_extrafields";
  599. $sql.= " WHERE fk_object=" . $this->id;
  600. $resql = $this->db->query($sql);
  601. if (! $resql)
  602. {
  603. $this->errors[] = $this->db->lasterror();
  604. $error++;
  605. }
  606. }
  607. if (empty($error))
  608. {
  609. // We remove directory
  610. $projectref = dol_sanitizeFileName($this->ref);
  611. if ($conf->projet->dir_output)
  612. {
  613. $dir = $conf->projet->dir_output . "/" . $projectref;
  614. if (file_exists($dir))
  615. {
  616. $res = @dol_delete_dir_recursive($dir);
  617. if (!$res)
  618. {
  619. $this->errors[] = 'ErrorFailToDeleteDir';
  620. $error++;
  621. }
  622. }
  623. }
  624. if (!$notrigger)
  625. {
  626. // Call trigger
  627. $result=$this->call_trigger('PROJECT_DELETE',$user);
  628. if ($result < 0) {
  629. $error++;
  630. }
  631. // End call triggers
  632. }
  633. }
  634. if (empty($error))
  635. {
  636. $this->db->commit();
  637. return 1;
  638. }
  639. else
  640. {
  641. foreach ( $this->errors as $errmsg )
  642. {
  643. dol_syslog(get_class($this) . "::delete " . $errmsg, LOG_ERR);
  644. $this->error .= ($this->error ? ', ' . $errmsg : $errmsg);
  645. }
  646. dol_syslog(get_class($this) . "::delete " . $this->error, LOG_ERR);
  647. $this->db->rollback();
  648. return -1;
  649. }
  650. }
  651. /**
  652. * Validate a project
  653. *
  654. * @param User $user User that validate
  655. * @param int $notrigger 1=Disable triggers
  656. * @return int <0 if KO, >0 if OK
  657. */
  658. function setValid($user, $notrigger=0)
  659. {
  660. global $langs, $conf;
  661. $error=0;
  662. if ($this->statut != 1)
  663. {
  664. // Check parameters
  665. if (preg_match('/^'.preg_quote($langs->trans("CopyOf").' ').'/', $this->title))
  666. {
  667. $this->error=$langs->trans("ErrorFieldFormat",$langs->transnoentities("Label")).'. '.$langs->trans('RemoveString',$langs->transnoentitiesnoconv("CopyOf"));
  668. return -1;
  669. }
  670. $this->db->begin();
  671. $sql = "UPDATE " . MAIN_DB_PREFIX . "projet";
  672. $sql.= " SET fk_statut = 1";
  673. $sql.= " WHERE rowid = " . $this->id;
  674. $sql.= " AND entity = " . $conf->entity;
  675. dol_syslog(get_class($this)."::setValid", LOG_DEBUG);
  676. $resql = $this->db->query($sql);
  677. if ($resql)
  678. {
  679. // Call trigger
  680. if (empty($notrigger))
  681. {
  682. $result=$this->call_trigger('PROJECT_VALIDATE',$user);
  683. if ($result < 0) { $error++; }
  684. // End call triggers
  685. }
  686. if (!$error)
  687. {
  688. $this->statut=1;
  689. $this->db->commit();
  690. return 1;
  691. }
  692. else
  693. {
  694. $this->db->rollback();
  695. $this->error = join(',', $this->errors);
  696. dol_syslog(get_class($this)."::setValid " . $this->error, LOG_ERR);
  697. return -1;
  698. }
  699. }
  700. else
  701. {
  702. $this->db->rollback();
  703. $this->error = $this->db->lasterror();
  704. return -1;
  705. }
  706. }
  707. }
  708. /**
  709. * Close a project
  710. *
  711. * @param User $user User that close project
  712. * @return int <0 if KO, 0 if already closed, >0 if OK
  713. */
  714. function setClose($user)
  715. {
  716. global $langs, $conf;
  717. $now = dol_now();
  718. $error=0;
  719. if ($this->statut != 2)
  720. {
  721. $this->db->begin();
  722. $sql = "UPDATE " . MAIN_DB_PREFIX . "projet";
  723. $sql.= " SET fk_statut = 2, fk_user_close = ".$user->id.", date_close = '".$this->db->idate($now)."'";
  724. $sql.= " WHERE rowid = " . $this->id;
  725. $sql.= " AND entity = " . $conf->entity;
  726. $sql.= " AND fk_statut = 1";
  727. if (! empty($conf->global->PROJECT_USE_OPPORTUNITIES))
  728. {
  729. // TODO What to do if fk_opp_status is not code 'WON' or 'LOST'
  730. }
  731. dol_syslog(get_class($this)."::setClose", LOG_DEBUG);
  732. $resql = $this->db->query($sql);
  733. if ($resql)
  734. {
  735. // Call trigger
  736. $result=$this->call_trigger('PROJECT_CLOSE',$user);
  737. if ($result < 0) { $error++; }
  738. // End call triggers
  739. if (!$error)
  740. {
  741. $this->statut = 2;
  742. $this->db->commit();
  743. return 1;
  744. }
  745. else
  746. {
  747. $this->db->rollback();
  748. $this->error = join(',', $this->errors);
  749. dol_syslog(get_class($this)."::setClose " . $this->error, LOG_ERR);
  750. return -1;
  751. }
  752. }
  753. else
  754. {
  755. $this->db->rollback();
  756. $this->error = $this->db->lasterror();
  757. return -1;
  758. }
  759. }
  760. return 0;
  761. }
  762. /**
  763. * Return status label of object
  764. *
  765. * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto
  766. * @return string Label
  767. */
  768. function getLibStatut($mode=0)
  769. {
  770. return $this->LibStatut($this->statut, $mode);
  771. }
  772. /**
  773. * Renvoi status label for a status
  774. *
  775. * @param int $statut id statut
  776. * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto
  777. * @return string Label
  778. */
  779. function LibStatut($statut, $mode=0)
  780. {
  781. global $langs;
  782. if ($mode == 0)
  783. {
  784. return $langs->trans($this->statuts_long[$statut]);
  785. }
  786. if ($mode == 1)
  787. {
  788. return $langs->trans($this->statuts_short[$statut]);
  789. }
  790. if ($mode == 2)
  791. {
  792. if ($statut == 0)
  793. return img_picto($langs->trans($this->statuts_long[$statut]), 'statut0') . ' ' . $langs->trans($this->statuts_short[$statut]);
  794. if ($statut == 1)
  795. return img_picto($langs->trans($this->statuts_long[$statut]), 'statut4') . ' ' . $langs->trans($this->statuts_short[$statut]);
  796. if ($statut == 2)
  797. return img_picto($langs->trans($this->statuts_long[$statut]), 'statut6') . ' ' . $langs->trans($this->statuts_short[$statut]);
  798. }
  799. if ($mode == 3)
  800. {
  801. if ($statut == 0)
  802. return img_picto($langs->trans($this->statuts_long[$statut]), 'statut0');
  803. if ($statut == 1)
  804. return img_picto($langs->trans($this->statuts_long[$statut]), 'statut4');
  805. if ($statut == 2)
  806. return img_picto($langs->trans($this->statuts_long[$statut]), 'statut6');
  807. }
  808. if ($mode == 4)
  809. {
  810. if ($statut == 0)
  811. return img_picto($langs->trans($this->statuts_long[$statut]), 'statut0') . ' ' . $langs->trans($this->statuts_long[$statut]);
  812. if ($statut == 1)
  813. return img_picto($langs->trans($this->statuts_long[$statut]), 'statut4') . ' ' . $langs->trans($this->statuts_long[$statut]);
  814. if ($statut == 2)
  815. return img_picto($langs->trans($this->statuts_long[$statut]), 'statut6') . ' ' . $langs->trans($this->statuts_long[$statut]);
  816. }
  817. if ($mode == 5)
  818. {
  819. if ($statut == 0)
  820. return $langs->trans($this->statuts_short[$statut]) . ' ' . img_picto($langs->trans($this->statuts_long[$statut]), 'statut0');
  821. if ($statut == 1)
  822. return $langs->trans($this->statuts_short[$statut]) . ' ' . img_picto($langs->trans($this->statuts_long[$statut]), 'statut4');
  823. if ($statut == 2)
  824. return $langs->trans($this->statuts_short[$statut]) . ' ' . img_picto($langs->trans($this->statuts_long[$statut]), 'statut6');
  825. }
  826. }
  827. /**
  828. * Return clicable name (with picto eventually)
  829. *
  830. * @param int $withpicto 0=No picto, 1=Include picto into link, 2=Only picto
  831. * @param string $option Variant ('', 'nolink')
  832. * @param int $addlabel 0=Default, 1=Add label into string, >1=Add first chars into string
  833. * @param string $moreinpopup Text to add into popup
  834. * @param string $sep Separator between ref and label if option addlabel is set
  835. * @param int $notooltip 1=Disable tooltip
  836. * @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking
  837. * @return string String with URL
  838. */
  839. function getNomUrl($withpicto=0, $option='', $addlabel=0, $moreinpopup='', $sep=' - ', $notooltip=0, $save_lastsearch_value=-1)
  840. {
  841. global $conf, $langs, $user;
  842. if (! empty($conf->dol_no_mouse_hover)) $notooltip=1; // Force disable tooltips
  843. $result = '';
  844. $label='';
  845. if ($option != 'nolink') $label = '<u>' . $langs->trans("ShowProject") . '</u>';
  846. $label .= ($label?'<br>':'').'<b>' . $langs->trans('Ref') . ': </b>' . $this->ref; // The space must be after the : to not being explode when showing the title in img_picto
  847. $label .= ($label?'<br>':'').'<b>' . $langs->trans('Label') . ': </b>' . $this->title; // The space must be after the : to not being explode when showing the title in img_picto
  848. if (! empty($this->thirdparty_name))
  849. $label .= ($label?'<br>':'').'<b>' . $langs->trans('ThirdParty') . ': </b>' . $this->thirdparty_name; // The space must be after the : to not being explode when showing the title in img_picto
  850. if (! empty($this->dateo))
  851. $label .= ($label?'<br>':'').'<b>' . $langs->trans('DateStart') . ': </b>' . dol_print_date($this->dateo, 'day'); // The space must be after the : to not being explode when showing the title in img_picto
  852. if (! empty($this->datee))
  853. $label .= ($label?'<br>':'').'<b>' . $langs->trans('DateEnd') . ': </b>' . dol_print_date($this->datee, 'day'); // The space must be after the : to not being explode when showing the title in img_picto
  854. if ($moreinpopup) $label.='<br>'.$moreinpopup;
  855. if ($option != 'nolink')
  856. {
  857. if (preg_match('/\.php$/',$option)) {
  858. $url = dol_buildpath($option,1) . '?id=' . $this->id;
  859. }
  860. else if ($option == 'task')
  861. {
  862. $url = DOL_URL_ROOT . '/projet/tasks.php?id=' . $this->id;
  863. }
  864. else
  865. {
  866. $url = DOL_URL_ROOT . '/projet/card.php?id=' . $this->id;
  867. }
  868. // Add param to save lastsearch_values or not
  869. $add_save_lastsearch_values=($save_lastsearch_value == 1 ? 1 : 0);
  870. if ($save_lastsearch_value == -1 && preg_match('/list\.php/',$_SERVER["PHP_SELF"])) $add_save_lastsearch_values=1;
  871. if ($add_save_lastsearch_values) $url.='&save_lastsearch_values=1';
  872. }
  873. $linkclose='';
  874. if (empty($notooltip) && $user->rights->propal->lire)
  875. {
  876. if (! empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER))
  877. {
  878. $label=$langs->trans("ShowProject");
  879. $linkclose.=' alt="'.dol_escape_htmltag($label, 1).'"';
  880. }
  881. $linkclose.=' title="'.dol_escape_htmltag($label, 1).'"';
  882. $linkclose.=' class="classfortooltip"';
  883. }
  884. $picto = 'projectpub';
  885. if (!$this->public) $picto = 'project';
  886. $linkstart = '<a href="'.$url.'"';
  887. $linkstart.=$linkclose.'>';
  888. $linkend='</a>';
  889. if ($withpicto) $result.=($linkstart . img_object(($notooltip?'':$label), $picto, ($notooltip?'':'class="classfortooltip"'), 0, 0, $notooltip?0:1) . $linkend);
  890. if ($withpicto && $withpicto != 2) $result.=' ';
  891. if ($withpicto != 2) $result.=$linkstart . $this->ref . $linkend . (($addlabel && $this->title) ? $sep . dol_trunc($this->title, ($addlabel > 1 ? $addlabel : 0)) : '');
  892. return $result;
  893. }
  894. /**
  895. * Initialise an instance with random values.
  896. * Used to build previews or test instances.
  897. * id must be 0 if object instance is a specimen.
  898. *
  899. * @return void
  900. */
  901. function initAsSpecimen()
  902. {
  903. global $user, $langs, $conf;
  904. $now=dol_now();
  905. // Initialise parameters
  906. $this->id = 0;
  907. $this->ref = 'SPECIMEN';
  908. $this->specimen = 1;
  909. $this->socid = 1;
  910. $this->date_c = $now;
  911. $this->date_m = $now;
  912. $this->date_start = $now;
  913. $this->date_end = $now + (3600 * 24 * 365);
  914. $this->note_public = 'SPECIMEN';
  915. $this->fk_ele = 20000;
  916. $this->opp_amount = 20000;
  917. $this->budget_amount = 10000;
  918. /*
  919. $nbp = mt_rand(1, 9);
  920. $xnbp = 0;
  921. while ($xnbp < $nbp)
  922. {
  923. $line = new Task($this->db);
  924. $line->fk_project = 0;
  925. $line->label = $langs->trans("Label") . " " . $xnbp;
  926. $line->description = $langs->trans("Description") . " " . $xnbp;
  927. $this->lines[]=$line;
  928. $xnbp++;
  929. }
  930. */
  931. }
  932. /**
  933. * Check if user has permission on current project
  934. *
  935. * @param User $user Object user to evaluate
  936. * @param string $mode Type of permission we want to know: 'read', 'write'
  937. * @return int >0 if user has permission, <0 if user has no permission
  938. */
  939. function restrictedProjectArea($user, $mode='read')
  940. {
  941. // To verify role of users
  942. $userAccess = 0;
  943. if (($mode == 'read' && ! empty($user->rights->projet->all->lire)) || ($mode == 'write' && ! empty($user->rights->projet->all->creer)) || ($mode == 'delete' && ! empty($user->rights->projet->all->supprimer)))
  944. {
  945. $userAccess = 1;
  946. }
  947. else if ($this->public && (($mode == 'read' && ! empty($user->rights->projet->lire)) || ($mode == 'write' && ! empty($user->rights->projet->creer)) || ($mode == 'delete' && ! empty($user->rights->projet->supprimer))))
  948. {
  949. $userAccess = 1;
  950. }
  951. else
  952. {
  953. foreach (array('internal', 'external') as $source)
  954. {
  955. $userRole = $this->liste_contact(4, $source);
  956. $num = count($userRole);
  957. $nblinks = 0;
  958. while ($nblinks < $num)
  959. {
  960. if ($source == 'internal' && preg_match('/^PROJECT/', $userRole[$nblinks]['code']) && $user->id == $userRole[$nblinks]['id'])
  961. {
  962. if ($mode == 'read' && $user->rights->projet->lire) $userAccess++;
  963. if ($mode == 'write' && $user->rights->projet->creer) $userAccess++;
  964. if ($mode == 'delete' && $user->rights->projet->supprimer) $userAccess++;
  965. }
  966. $nblinks++;
  967. }
  968. }
  969. //if (empty($nblinks)) // If nobody has permission, we grant creator
  970. //{
  971. // if ((!empty($this->user_author_id) && $this->user_author_id == $user->id))
  972. // {
  973. // $userAccess = 1;
  974. // }
  975. //}
  976. }
  977. return ($userAccess?$userAccess:-1);
  978. }
  979. /**
  980. * Return array of projects a user has permission on, is affected to, or all projects
  981. *
  982. * @param User $user User object
  983. * @param int $mode 0=All project I have permission on (assigned to me and public), 1=Projects assigned to me only, 2=Will return list of all projects with no test on contacts
  984. * @param int $list 0=Return array,1=Return string list
  985. * @param int $socid 0=No filter on third party, id of third party
  986. * @return array or string Array of projects id, or string with projects id separated with ","
  987. */
  988. function getProjectsAuthorizedForUser($user, $mode=0, $list=0, $socid=0)
  989. {
  990. $projects = array();
  991. $temp = array();
  992. $sql = "SELECT ".(($mode == 0 || $mode == 1) ? "DISTINCT " : "")."p.rowid, p.ref";
  993. $sql.= " FROM " . MAIN_DB_PREFIX . "projet as p";
  994. if ($mode == 0 || $mode == 1)
  995. {
  996. $sql.= ", " . MAIN_DB_PREFIX . "element_contact as ec";
  997. }
  998. $sql.= " WHERE p.entity IN (".getEntity('project').")";
  999. // Internal users must see project he is contact to even if project linked to a third party he can't see.
  1000. //if ($socid || ! $user->rights->societe->client->voir) $sql.= " AND (p.fk_soc IS NULL OR p.fk_soc = 0 OR p.fk_soc = ".$socid.")";
  1001. if ($socid > 0) $sql.= " AND (p.fk_soc IS NULL OR p.fk_soc = 0 OR p.fk_soc = " . $socid . ")";
  1002. // Get id of types of contacts for projects (This list never contains a lot of elements)
  1003. $listofprojectcontacttype=array();
  1004. $sql2 = "SELECT ctc.rowid, ctc.code FROM ".MAIN_DB_PREFIX."c_type_contact as ctc";
  1005. $sql2.= " WHERE ctc.element = '" . $this->db->escape($this->element) . "'";
  1006. $sql2.= " AND ctc.source = 'internal'";
  1007. $resql = $this->db->query($sql2);
  1008. if ($resql)
  1009. {
  1010. while($obj = $this->db->fetch_object($resql))
  1011. {
  1012. $listofprojectcontacttype[$obj->rowid]=$obj->code;
  1013. }
  1014. }
  1015. else dol_print_error($this->db);
  1016. if (count($listofprojectcontacttype) == 0) $listofprojectcontacttype[0]='0'; // To avoid syntax error if not found
  1017. if ($mode == 0)
  1018. {
  1019. $sql.= " AND ec.element_id = p.rowid";
  1020. $sql.= " AND ( p.public = 1";
  1021. $sql.= " OR ( ec.fk_c_type_contact IN (".join(',', array_keys($listofprojectcontacttype)).")";
  1022. $sql.= " AND ec.fk_socpeople = ".$user->id.")";
  1023. $sql.= " )";
  1024. }
  1025. if ($mode == 1)
  1026. {
  1027. $sql.= " AND ec.element_id = p.rowid";
  1028. $sql.= " AND (";
  1029. $sql.= " ( ec.fk_c_type_contact IN (".join(',', array_keys($listofprojectcontacttype)).")";
  1030. $sql.= " AND ec.fk_socpeople = ".$user->id.")";
  1031. $sql.= " )";
  1032. }
  1033. if ($mode == 2)
  1034. {
  1035. // No filter. Use this if user has permission to see all project
  1036. }
  1037. //print $sql;
  1038. $resql = $this->db->query($sql);
  1039. if ($resql)
  1040. {
  1041. $num = $this->db->num_rows($resql);
  1042. $i = 0;
  1043. while ($i < $num)
  1044. {
  1045. $row = $this->db->fetch_row($resql);
  1046. $projects[$row[0]] = $row[1];
  1047. $temp[] = $row[0];
  1048. $i++;
  1049. }
  1050. $this->db->free($resql);
  1051. if ($list)
  1052. {
  1053. if (empty($temp)) return '0';
  1054. $result = implode(',', $temp);
  1055. return $result;
  1056. }
  1057. }
  1058. else
  1059. {
  1060. dol_print_error($this->db);
  1061. }
  1062. return $projects;
  1063. }
  1064. /**
  1065. * Load an object from its id and create a new one in database
  1066. *
  1067. * @param int $fromid Id of object to clone
  1068. * @param bool $clone_contact Clone contact of project
  1069. * @param bool $clone_task Clone task of project
  1070. * @param bool $clone_project_file Clone file of project
  1071. * @param bool $clone_task_file Clone file of task (if task are copied)
  1072. * @param bool $clone_note Clone note of project
  1073. * @param bool $move_date Move task date on clone
  1074. * @param integer $notrigger No trigger flag
  1075. * @param int $newthirdpartyid New thirdparty id
  1076. * @return int New id of clone
  1077. */
  1078. function createFromClone($fromid,$clone_contact=false,$clone_task=true,$clone_project_file=false,$clone_task_file=false,$clone_note=true,$move_date=true,$notrigger=0,$newthirdpartyid=0)
  1079. {
  1080. global $user,$langs,$conf;
  1081. $error=0;
  1082. dol_syslog("createFromClone clone_contact=".$clone_contact." clone_task=".$clone_task." clone_project_file=".$clone_project_file." clone_note=".$clone_note." move_date=".$move_date,LOG_DEBUG);
  1083. $now = dol_mktime(0,0,0,idate('m',dol_now()),idate('d',dol_now()),idate('Y',dol_now()));
  1084. $clone_project=new Project($this->db);
  1085. $clone_project->context['createfromclone']='createfromclone';
  1086. $this->db->begin();
  1087. // Load source object
  1088. $clone_project->fetch($fromid);
  1089. $clone_project->fetch_optionals();
  1090. if ($newthirdpartyid > 0) $clone_project->socid = $newthirdpartyid;
  1091. $clone_project->fetch_thirdparty();
  1092. $orign_dt_start=$clone_project->date_start;
  1093. $orign_project_ref=$clone_project->ref;
  1094. $clone_project->id=0;
  1095. if ($move_date) {
  1096. $clone_project->date_start = $now;
  1097. if (!(empty($clone_project->date_end)))
  1098. {
  1099. $clone_project->date_end = $clone_project->date_end + ($now - $orign_dt_start);
  1100. }
  1101. }
  1102. $clone_project->datec = $now;
  1103. if (! $clone_note)
  1104. {
  1105. $clone_project->note_private='';
  1106. $clone_project->note_public='';
  1107. }
  1108. //Generate next ref
  1109. $defaultref='';
  1110. $obj = empty($conf->global->PROJECT_ADDON)?'mod_project_simple':$conf->global->PROJECT_ADDON;
  1111. // Search template files
  1112. $file=''; $classname=''; $filefound=0;
  1113. $dirmodels=array_merge(array('/'),(array) $conf->modules_parts['models']);
  1114. foreach($dirmodels as $reldir)
  1115. {
  1116. $file=dol_buildpath($reldir."core/modules/project/".$obj.'.php',0);
  1117. if (file_exists($file))
  1118. {
  1119. $filefound=1;
  1120. dol_include_once($reldir."core/modules/project/".$obj.'.php');
  1121. $modProject = new $obj;
  1122. $defaultref = $modProject->getNextValue(is_object($clone_project->thirdparty)?$clone_project->thirdparty:null, $clone_project);
  1123. break;
  1124. }
  1125. }
  1126. if (is_numeric($defaultref) && $defaultref <= 0) $defaultref='';
  1127. $clone_project->ref=$defaultref;
  1128. $clone_project->title=$langs->trans("CopyOf").' '.$clone_project->title;
  1129. // Create clone
  1130. $result=$clone_project->create($user,$notrigger);
  1131. // Other options
  1132. if ($result < 0)
  1133. {
  1134. $this->error.=$clone_project->error;
  1135. $error++;
  1136. }
  1137. if (! $error)
  1138. {
  1139. //Get the new project id
  1140. $clone_project_id=$clone_project->id;
  1141. //Note Update
  1142. if (!$clone_note)
  1143. {
  1144. $clone_project->note_private='';
  1145. $clone_project->note_public='';
  1146. }
  1147. else
  1148. {
  1149. $this->db->begin();
  1150. $res=$clone_project->update_note(dol_html_entity_decode($clone_project->note_public, ENT_QUOTES),'_public');
  1151. if ($res < 0)
  1152. {
  1153. $this->error.=$clone_project->error;
  1154. $error++;
  1155. $this->db->rollback();
  1156. }
  1157. else
  1158. {
  1159. $this->db->commit();
  1160. }
  1161. $this->db->begin();
  1162. $res=$clone_project->update_note(dol_html_entity_decode($clone_project->note_private, ENT_QUOTES), '_private');
  1163. if ($res < 0)
  1164. {
  1165. $this->error.=$clone_project->error;
  1166. $error++;
  1167. $this->db->rollback();
  1168. }
  1169. else
  1170. {
  1171. $this->db->commit();
  1172. }
  1173. }
  1174. //Duplicate contact
  1175. if ($clone_contact)
  1176. {
  1177. $origin_project = new Project($this->db);
  1178. $origin_project->fetch($fromid);
  1179. foreach(array('internal','external') as $source)
  1180. {
  1181. $tab = $origin_project->liste_contact(-1,$source);
  1182. foreach ($tab as $contacttoadd)
  1183. {
  1184. $clone_project->add_contact($contacttoadd['id'], $contacttoadd['code'], $contacttoadd['source'],$notrigger);
  1185. if ($clone_project->error == 'DB_ERROR_RECORD_ALREADY_EXISTS')
  1186. {
  1187. $langs->load("errors");
  1188. $this->error.=$langs->trans("ErrorThisContactIsAlreadyDefinedAsThisType");
  1189. $error++;
  1190. }
  1191. else
  1192. {
  1193. if ($clone_project->error!='')
  1194. {
  1195. $this->error.=$clone_project->error;
  1196. $error++;
  1197. }
  1198. }
  1199. }
  1200. }
  1201. }
  1202. //Duplicate file
  1203. if ($clone_project_file)
  1204. {
  1205. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  1206. $clone_project_dir = $conf->projet->dir_output . "/" . dol_sanitizeFileName($defaultref);
  1207. $ori_project_dir = $conf->projet->dir_output . "/" . dol_sanitizeFileName($orign_project_ref);
  1208. if (dol_mkdir($clone_project_dir) >= 0)
  1209. {
  1210. $filearray=dol_dir_list($ori_project_dir,"files",0,'','(\.meta|_preview.*\.png)$','',SORT_ASC,1);
  1211. foreach($filearray as $key => $file)
  1212. {
  1213. $rescopy = dol_copy($ori_project_dir . '/' . $file['name'], $clone_project_dir . '/' . $file['name'],0,1);
  1214. if (is_numeric($rescopy) && $rescopy < 0)
  1215. {
  1216. $this->error.=$langs->trans("ErrorFailToCopyFile",$ori_project_dir . '/' . $file['name'],$clone_project_dir . '/' . $file['name']);
  1217. $error++;
  1218. }
  1219. }
  1220. }
  1221. else
  1222. {
  1223. $this->error.=$langs->trans('ErrorInternalErrorDetected').':dol_mkdir';
  1224. $error++;
  1225. }
  1226. }
  1227. //Duplicate task
  1228. if ($clone_task)
  1229. {
  1230. require_once DOL_DOCUMENT_ROOT . '/projet/class/task.class.php';
  1231. $taskstatic = new Task($this->db);
  1232. // Security check
  1233. $socid=0;
  1234. if ($user->societe_id > 0) $socid = $user->societe_id;
  1235. $tasksarray=$taskstatic->getTasksArray(0, 0, $fromid, $socid, 0);
  1236. $tab_conv_child_parent=array();
  1237. // Loop on each task, to clone it
  1238. foreach ($tasksarray as $tasktoclone)
  1239. {
  1240. $result_clone = $taskstatic->createFromClone($tasktoclone->id,$clone_project_id,$tasktoclone->fk_parent,$move_date,true,false,$clone_task_file,true,false);
  1241. if ($result_clone <= 0)
  1242. {
  1243. $this->error.=$result_clone->error;
  1244. $error++;
  1245. }
  1246. else
  1247. {
  1248. $new_task_id=$result_clone;
  1249. $taskstatic->fetch($tasktoclone->id);
  1250. //manage new parent clone task id
  1251. // if the current task has child we store the original task id and the equivalent clone task id
  1252. if (($taskstatic->hasChildren()) && !array_key_exists($tasktoclone->id,$tab_conv_child_parent))
  1253. {
  1254. $tab_conv_child_parent[$tasktoclone->id] = $new_task_id;
  1255. }
  1256. }
  1257. }
  1258. //Parse all clone node to be sure to update new parent
  1259. $tasksarray=$taskstatic->getTasksArray(0, 0, $clone_project_id, $socid, 0);
  1260. foreach ($tasksarray as $task_cloned)
  1261. {
  1262. $taskstatic->fetch($task_cloned->id);
  1263. if ($taskstatic->fk_task_parent!=0)
  1264. {
  1265. $taskstatic->fk_task_parent=$tab_conv_child_parent[$taskstatic->fk_task_parent];
  1266. }
  1267. $res=$taskstatic->update($user,$notrigger);
  1268. if ($result_clone <= 0)
  1269. {
  1270. $this->error.=$taskstatic->error;
  1271. $error++;
  1272. }
  1273. }
  1274. }
  1275. }
  1276. unset($clone_project->context['createfromclone']);
  1277. if (! $error)
  1278. {
  1279. $this->db->commit();
  1280. return $clone_project_id;
  1281. }
  1282. else
  1283. {
  1284. $this->db->rollback();
  1285. dol_syslog(get_class($this)."::createFromClone nbError: ".$error." error : " . $this->error, LOG_ERR);
  1286. return -1;
  1287. }
  1288. }
  1289. /**
  1290. * Shift project task date from current date to delta
  1291. *
  1292. * @param timestamp $old_project_dt_start old project start date
  1293. * @return int 1 if OK or < 0 if KO
  1294. */
  1295. function shiftTaskDate($old_project_dt_start)
  1296. {
  1297. global $user,$langs,$conf;
  1298. $error=0;
  1299. $taskstatic = new Task($this->db);
  1300. // Security check
  1301. $socid=0;
  1302. if ($user->societe_id > 0) $socid = $user->societe_id;
  1303. $tasksarray=$taskstatic->getTasksArray(0, 0, $this->id, $socid, 0);
  1304. foreach ($tasksarray as $tasktoshiftdate)
  1305. {
  1306. $to_update=false;
  1307. // Fetch only if update of date will be made
  1308. if ((!empty($tasktoshiftdate->date_start)) || (!empty($tasktoshiftdate->date_end)))
  1309. {
  1310. //dol_syslog(get_class($this)."::shiftTaskDate to_update", LOG_DEBUG);
  1311. $to_update=true;
  1312. $task = new Task($this->db);
  1313. $result = $task->fetch($tasktoshiftdate->id);
  1314. if (!$result)
  1315. {
  1316. $error++;
  1317. $this->error.=$task->error;
  1318. }
  1319. }
  1320. //print "$this->date_start + $tasktoshiftdate->date_start - $old_project_dt_start";exit;
  1321. //Calcultate new task start date with difference between old proj start date and origin task start date
  1322. if (!empty($tasktoshiftdate->date_start))
  1323. {
  1324. $task->date_start = $this->date_start + ($tasktoshiftdate->date_start - $old_project_dt_start);
  1325. }
  1326. //Calcultate new task end date with difference between origin proj end date and origin task end date
  1327. if (!empty($tasktoshiftdate->date_end))
  1328. {
  1329. $task->date_end = $this->date_start + ($tasktoshiftdate->date_end - $old_project_dt_start);
  1330. }
  1331. if ($to_update)
  1332. {
  1333. $result = $task->update($user);
  1334. if (!$result)
  1335. {
  1336. $error++;
  1337. $this->error.=$task->error;
  1338. }
  1339. }
  1340. }
  1341. if ($error!=0)
  1342. {
  1343. return -1;
  1344. }
  1345. return $result;
  1346. }
  1347. /**
  1348. * Associate element to a project
  1349. *
  1350. * @param string $tableName Table of the element to update
  1351. * @param int $elementSelectId Key-rowid of the line of the element to update
  1352. * @return int 1 if OK or < 0 if KO
  1353. */
  1354. function update_element($tableName, $elementSelectId)
  1355. {
  1356. $sql="UPDATE ".MAIN_DB_PREFIX.$tableName;
  1357. if ($tableName == "actioncomm")
  1358. {
  1359. $sql.= " SET fk_project=".$this->id;
  1360. $sql.= " WHERE id=".$elementSelectId;
  1361. }
  1362. else
  1363. {
  1364. $sql.= " SET fk_projet=".$this->id;
  1365. $sql.= " WHERE rowid=".$elementSelectId;
  1366. }
  1367. dol_syslog(get_class($this)."::update_element", LOG_DEBUG);
  1368. $resql=$this->db->query($sql);
  1369. if (!$resql) {
  1370. $this->error=$this->db->lasterror();
  1371. return -1;
  1372. }else {
  1373. return 1;
  1374. }
  1375. }
  1376. /**
  1377. * Associate element to a project
  1378. *
  1379. * @param string $tableName Table of the element to update
  1380. * @param int $elementSelectId Key-rowid of the line of the element to update
  1381. * @return int 1 if OK or < 0 if KO
  1382. */
  1383. function remove_element($tableName, $elementSelectId)
  1384. {
  1385. $sql="UPDATE ".MAIN_DB_PREFIX.$tableName;
  1386. if ($TableName=="actioncomm")
  1387. {
  1388. $sql.= " SET fk_project=NULL";
  1389. $sql.= " WHERE id=".$elementSelectId;
  1390. }
  1391. else
  1392. {
  1393. $sql.= " SET fk_projet=NULL";
  1394. $sql.= " WHERE rowid=".$elementSelectId;
  1395. }
  1396. dol_syslog(get_class($this)."::remove_element", LOG_DEBUG);
  1397. $resql=$this->db->query($sql);
  1398. if (!$resql) {
  1399. $this->error=$this->db->lasterror();
  1400. return -1;
  1401. }else {
  1402. return 1;
  1403. }
  1404. }
  1405. /**
  1406. * Create an intervention document on disk using template defined into PROJECT_ADDON_PDF
  1407. *
  1408. * @param string $modele Force template to use ('' by default)
  1409. * @param Translate $outputlangs Objet lang to use for translation
  1410. * @param int $hidedetails Hide details of lines
  1411. * @param int $hidedesc Hide description
  1412. * @param int $hideref Hide ref
  1413. * @return int 0 if KO, 1 if OK
  1414. */
  1415. public function generateDocument($modele, $outputlangs, $hidedetails=0, $hidedesc=0, $hideref=0)
  1416. {
  1417. global $conf,$langs;
  1418. $langs->load("projects");
  1419. if (! dol_strlen($modele)) {
  1420. $modele = 'baleine';
  1421. if ($this->modelpdf) {
  1422. $modele = $this->modelpdf;
  1423. } elseif (! empty($conf->global->PROJECT_ADDON_PDF)) {
  1424. $modele = $conf->global->PROJECT_ADDON_PDF;
  1425. }
  1426. }
  1427. $modelpath = "core/modules/project/doc/";
  1428. return $this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref);
  1429. }
  1430. /**
  1431. * Load time spent into this->weekWorkLoad and this->weekWorkLoadPerTask for all day of a week of project
  1432. *
  1433. * @param int $datestart First day of week (use dol_get_first_day to find this date)
  1434. * @param int $taskid Filter on a task id
  1435. * @param int $userid Time spent by a particular user
  1436. * @return int <0 if OK, >0 if KO
  1437. */
  1438. public function loadTimeSpent($datestart,$taskid=0,$userid=0)
  1439. {
  1440. $error=0;
  1441. if (empty($datestart)) dol_print_error('','Error datestart parameter is empty');
  1442. $sql = "SELECT ptt.rowid as taskid, ptt.task_duration, ptt.task_date, ptt.fk_task";
  1443. $sql.= " FROM ".MAIN_DB_PREFIX."projet_task_time AS ptt, ".MAIN_DB_PREFIX."projet_task as pt";
  1444. $sql.= " WHERE ptt.fk_task = pt.rowid";
  1445. $sql.= " AND pt.fk_projet = ".$this->id;
  1446. $sql.= " AND (ptt.task_date >= '".$this->db->idate($datestart)."' ";
  1447. $sql.= " AND ptt.task_date <= '".$this->db->idate($datestart + (7 * 24 * 3600) - 1)."')";
  1448. if ($task_id) $sql.= " AND ptt.fk_task=".$taskid;
  1449. if (is_numeric($userid)) $sql.= " AND ptt.fk_user=".$userid;
  1450. //print $sql;
  1451. $resql=$this->db->query($sql);
  1452. if ($resql)
  1453. {
  1454. $num = $this->db->num_rows($resql);
  1455. $i = 0;
  1456. // Loop on each record found, so each couple (project id, task id)
  1457. while ($i < $num)
  1458. {
  1459. $obj=$this->db->fetch_object($resql);
  1460. $day=$this->db->jdate($obj->task_date);
  1461. $this->weekWorkLoad[$day] += $obj->task_duration;
  1462. $this->weekWorkLoadPerTask[$day][$obj->fk_task] += $obj->task_duration;
  1463. $i++;
  1464. }
  1465. $this->db->free($resql);
  1466. return 1;
  1467. }
  1468. else
  1469. {
  1470. $this->error="Error ".$this->db->lasterror();
  1471. dol_syslog(get_class($this)."::fetch ".$this->error, LOG_ERR);
  1472. return -1;
  1473. }
  1474. }
  1475. /**
  1476. * Load indicators for dashboard (this->nbtodo and this->nbtodolate)
  1477. *
  1478. * @param User $user Objet user
  1479. * @return WorkboardResponse|int <0 if KO, WorkboardResponse if OK
  1480. */
  1481. function load_board($user)
  1482. {
  1483. global $conf, $langs;
  1484. $mine=0; $socid=$user->societe_id;
  1485. $projectsListId = $this->getProjectsAuthorizedForUser($user,$mine?$mine:($user->rights->projet->all->lire?2:0),1,$socid);
  1486. $sql = "SELECT p.rowid, p.fk_statut as status, p.fk_opp_status, p.datee as datee";
  1487. $sql.= " FROM (".MAIN_DB_PREFIX."projet as p";
  1488. $sql.= ")";
  1489. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s on p.fk_soc = s.rowid";
  1490. if (! $user->rights->societe->client->voir && ! $socid) $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON sc.fk_soc = s.rowid";
  1491. $sql.= " WHERE p.fk_statut = 1";
  1492. $sql.= " AND p.entity IN (".getEntity('project', 0).')';
  1493. if ($mine || ! $user->rights->projet->all->lire) $sql.= " AND p.rowid IN (".$projectsListId.")";
  1494. // No need to check company, as filtering of projects must be done by getProjectsAuthorizedForUser
  1495. //if ($socid || ! $user->rights->societe->client->voir) $sql.= " AND (p.fk_soc IS NULL OR p.fk_soc = 0 OR p.fk_soc = ".$socid.")";
  1496. if ($socid) $sql.= " AND (p.fk_soc IS NULL OR p.fk_soc = 0 OR p.fk_soc = ".$socid.")";
  1497. if (! $user->rights->societe->client->voir && ! $socid) $sql.= " AND ((s.rowid = sc.fk_soc AND sc.fk_user = " .$user->id.") OR (s.rowid IS NULL))";
  1498. $resql=$this->db->query($sql);
  1499. if ($resql)
  1500. {
  1501. $project_static = new Project($this->db);
  1502. $response = new WorkboardResponse();
  1503. $response->warning_delay = $conf->projet->warning_delay/60/60/24;
  1504. $response->label = $langs->trans("OpenedProjects");
  1505. if ($user->rights->projet->all->lire) $response->url = DOL_URL_ROOT.'/projet/list.php?search_status=1&mainmenu=project';
  1506. else $response->url = DOL_URL_ROOT.'/projet/list.php?search_project_user=-1&search_status=1&mainmenu=project';
  1507. $response->img = img_object('',"projectpub");
  1508. // This assignment in condition is not a bug. It allows walking the results.
  1509. while ($obj=$this->db->fetch_object($resql))
  1510. {
  1511. $response->nbtodo++;
  1512. $project_static->statut = $obj->status;
  1513. $project_static->opp_status = $obj->opp_status;
  1514. $project_static->datee = $this->db->jdate($obj->datee);
  1515. if ($project_static->hasDelay()) {
  1516. $response->nbtodolate++;
  1517. }
  1518. }
  1519. return $response;
  1520. }
  1521. else
  1522. {
  1523. $this->error=$this->db->error();
  1524. return -1;
  1525. }
  1526. }
  1527. /**
  1528. * Function used to replace a thirdparty id with another one.
  1529. *
  1530. * @param DoliDB $db Database handler
  1531. * @param int $origin_id Old thirdparty id
  1532. * @param int $dest_id New thirdparty id
  1533. * @return bool
  1534. */
  1535. public static function replaceThirdparty(DoliDB $db, $origin_id, $dest_id)
  1536. {
  1537. $tables = array(
  1538. 'projet'
  1539. );
  1540. return CommonObject::commonReplaceThirdparty($db, $origin_id, $dest_id, $tables);
  1541. }
  1542. /**
  1543. * Charge indicateurs this->nb pour le tableau de bord
  1544. *
  1545. * @return int <0 if KO, >0 if OK
  1546. */
  1547. function load_state_board()
  1548. {
  1549. global $user;
  1550. $this->nb=array();
  1551. $sql = "SELECT count(p.rowid) as nb";
  1552. $sql.= " FROM ".MAIN_DB_PREFIX."projet as p";
  1553. $sql.= " WHERE";
  1554. $sql.= " p.entity IN (".getEntity('projet').")";
  1555. if (! $user->rights->projet->all->lire)
  1556. {
  1557. $projectsListId = $this->getProjectsAuthorizedForUser($user,0,1);
  1558. $sql .= "AND p.rowid IN (".$projectsListId.")";
  1559. }
  1560. $resql=$this->db->query($sql);
  1561. if ($resql)
  1562. {
  1563. while ($obj=$this->db->fetch_object($resql))
  1564. {
  1565. $this->nb["projects"]=$obj->nb;
  1566. }
  1567. $this->db->free($resql);
  1568. return 1;
  1569. }
  1570. else
  1571. {
  1572. dol_print_error($this->db);
  1573. $this->error=$this->db->error();
  1574. return -1;
  1575. }
  1576. }
  1577. /**
  1578. * Is the project delayed?
  1579. *
  1580. * @return bool
  1581. */
  1582. public function hasDelay()
  1583. {
  1584. global $conf;
  1585. if (! ($this->statut == 1)) return false;
  1586. if (! $this->datee && ! $this->date_end) return false;
  1587. $now = dol_now();
  1588. return ($this->datee ? $this->datee : $this->date_end) < ($now - $conf->projet->warning_delay);
  1589. }
  1590. /**
  1591. * Charge les informations d'ordre info dans l'objet commande
  1592. *
  1593. * @param int $id Id of order
  1594. * @return void
  1595. */
  1596. function info($id)
  1597. {
  1598. $sql = 'SELECT c.rowid, datec as datec, tms as datem,';
  1599. $sql.= ' date_close as datecloture,';
  1600. $sql.= ' fk_user_creat as fk_user_author, fk_user_close as fk_use_cloture';
  1601. $sql.= ' FROM '.MAIN_DB_PREFIX.'projet as c';
  1602. $sql.= ' WHERE c.rowid = '.$id;
  1603. $result=$this->db->query($sql);
  1604. if ($result)
  1605. {
  1606. if ($this->db->num_rows($result))
  1607. {
  1608. $obj = $this->db->fetch_object($result);
  1609. $this->id = $obj->rowid;
  1610. if ($obj->fk_user_author)
  1611. {
  1612. $cuser = new User($this->db);
  1613. $cuser->fetch($obj->fk_user_author);
  1614. $this->user_creation = $cuser;
  1615. }
  1616. if ($obj->fk_user_cloture)
  1617. {
  1618. $cluser = new User($this->db);
  1619. $cluser->fetch($obj->fk_user_cloture);
  1620. $this->user_cloture = $cluser;
  1621. }
  1622. $this->date_creation = $this->db->jdate($obj->datec);
  1623. $this->date_modification = $this->db->jdate($obj->datem);
  1624. $this->date_cloture = $this->db->jdate($obj->datecloture);
  1625. }
  1626. $this->db->free($result);
  1627. }
  1628. else
  1629. {
  1630. dol_print_error($this->db);
  1631. }
  1632. }
  1633. /**
  1634. * Sets object to supplied categories.
  1635. *
  1636. * Deletes object from existing categories not supplied.
  1637. * Adds it to non existing supplied categories.
  1638. * Existing categories are left untouch.
  1639. *
  1640. * @param int[]|int $categories Category or categories IDs
  1641. */
  1642. public function setCategories($categories)
  1643. {
  1644. // Decode type
  1645. $type_id = Categorie::TYPE_PROJECT;
  1646. $type_text = 'project';
  1647. // Handle single category
  1648. if (!is_array($categories)) {
  1649. $categories = array($categories);
  1650. }
  1651. // Get current categories
  1652. require_once DOL_DOCUMENT_ROOT . '/categories/class/categorie.class.php';
  1653. $c = new Categorie($this->db);
  1654. $existing = $c->containing($this->id, $type_id, 'id');
  1655. // Diff
  1656. if (is_array($existing)) {
  1657. $to_del = array_diff($existing, $categories);
  1658. $to_add = array_diff($categories, $existing);
  1659. } else {
  1660. $to_del = array(); // Nothing to delete
  1661. $to_add = $categories;
  1662. }
  1663. // Process
  1664. foreach ($to_del as $del) {
  1665. if ($c->fetch($del) > 0) {
  1666. $result=$c->del_type($this, $type_text);
  1667. if ($result<0) {
  1668. $this->errors=$c->errors;
  1669. $this->error=$c->error;
  1670. return -1;
  1671. }
  1672. }
  1673. }
  1674. foreach ($to_add as $add) {
  1675. if ($c->fetch($add) > 0) {
  1676. $result=$c->add_type($this, $type_text);
  1677. if ($result<0) {
  1678. $this->errors=$c->errors;
  1679. $this->error=$c->error;
  1680. return -1;
  1681. }
  1682. }
  1683. }
  1684. return 1;
  1685. }
  1686. /**
  1687. * Create an array of tasks of current project
  1688. *
  1689. * @param User $user Object user we want project allowed to
  1690. * @return int >0 if OK, <0 if KO
  1691. */
  1692. function getLinesArray($user)
  1693. {
  1694. require_once DOL_DOCUMENT_ROOT.'/projet/class/task.class.php';
  1695. $taskstatic = new Task($this->db);
  1696. $this->lines = $taskstatic->getTasksArray(0, $user, $this->id, 0, 0);
  1697. }
  1698. }