expensereport.class.php 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297
  1. <?php
  2. /* Copyright (C) 2011 Dimitri Mouillard <dmouillard@teclib.com>
  3. * Copyright (C) 2015 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2015 Alexandre Spangaro <aspangaro@zendsi.com>
  5. * Copyright (C) 2016 Ferran Marcet <fmarcet@2byte.es>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * \file htdocs/expensereport/class/expensereport.class.php
  22. * \ingroup expensereport
  23. * \brief File to manage Expense Reports
  24. */
  25. require_once DOL_DOCUMENT_ROOT .'/core/class/commonobject.class.php';
  26. /**
  27. * Class to manage Trips and Expenses
  28. */
  29. class ExpenseReport extends CommonObject
  30. {
  31. var $element='expensereport';
  32. var $table_element='expensereport';
  33. var $table_element_line = 'expensereport_det';
  34. var $fk_element = 'fk_expensereport';
  35. var $picto = 'trip';
  36. var $lignes=array();
  37. public $date_debut;
  38. public $date_fin;
  39. var $fk_user_validator;
  40. var $status;
  41. var $fk_statut; // -- 0=draft, 2=validated (attente approb), 4=canceled, 5=approved, 6=payed, 99=denied
  42. var $fk_c_paiement;
  43. var $paid;
  44. var $user_author_infos;
  45. var $user_validator_infos;
  46. var $fk_typepayment;
  47. var $num_payment;
  48. var $code_paiement;
  49. var $code_statut;
  50. // ACTIONS
  51. // Create
  52. var $date_create;
  53. var $fk_user_author; // Note fk_user_author is not the 'author' but the guy the expense report is for.
  54. // Update
  55. var $date_modif;
  56. var $fk_user_modif;
  57. // Refus
  58. var $date_refuse;
  59. var $detail_refuse;
  60. var $fk_user_refuse;
  61. // Annulation
  62. var $date_cancel;
  63. var $detail_cancel;
  64. var $fk_user_cancel;
  65. // Validation
  66. var $date_valid;
  67. var $fk_user_valid;
  68. var $user_valid_infos;
  69. // Approve
  70. var $date_approve;
  71. var $fk_user_approve;
  72. // Paiement
  73. var $user_paid_infos;
  74. /*
  75. END ACTIONS
  76. */
  77. /**
  78. * Constructor
  79. *
  80. * @param DoliDB $db Handler acces base de donnees
  81. */
  82. function __construct($db)
  83. {
  84. $this->db = $db;
  85. $this->total_ht = 0;
  86. $this->total_ttc = 0;
  87. $this->total_tva = 0;
  88. $this->modepaymentid = 0;
  89. // List of language codes for status
  90. $this->statuts_short = array(0 => 'Draft', 2 => 'Validated', 4 => 'Canceled', 5 => 'Approved', 6 => 'Paid', 99 => 'Refused');
  91. $this->statuts = array(0 => 'Draft', 2 => 'ValidatedWaitingApproval', 4 => 'Canceled', 5 => 'Approved', 6 => 'Paid', 99 => 'Refused');
  92. $this->statuts_logo = array(0 => 'statut0', 2 => 'statut1', 4 => 'statut5', 5 => 'statut3', 6 => 'statut6', 99 => 'statut8');
  93. return 1;
  94. }
  95. /**
  96. * Create object in database
  97. *
  98. * @param User $user User that create
  99. * @param int $notrigger Disable triggers
  100. * @return int <0 if KO, >0 if OK
  101. */
  102. function create($user, $notrigger=0)
  103. {
  104. global $conf;
  105. $now = dol_now();
  106. $error = 0;
  107. // Check parameters
  108. if (empty($this->date_debut) || empty($this->date_fin))
  109. {
  110. $this->error='ErrorFieldRequired';
  111. return -1;
  112. }
  113. $fuserid = $this->fk_user_author; // Note fk_user_author is not the 'author' but the guy the expense report is for.
  114. if (empty($fuserid)) $fuserid = $user->id;
  115. $this->db->begin();
  116. $sql = "INSERT INTO ".MAIN_DB_PREFIX.$this->table_element." (";
  117. $sql.= "ref";
  118. $sql.= ",total_ht";
  119. $sql.= ",total_ttc";
  120. $sql.= ",total_tva";
  121. $sql.= ",date_debut";
  122. $sql.= ",date_fin";
  123. $sql.= ",date_create";
  124. $sql.= ",fk_user_author";
  125. $sql.= ",fk_user_validator";
  126. $sql.= ",fk_user_modif";
  127. $sql.= ",fk_statut";
  128. $sql.= ",fk_c_paiement";
  129. $sql.= ",paid";
  130. $sql.= ",note_public";
  131. $sql.= ",note_private";
  132. $sql.= ",entity";
  133. $sql.= ") VALUES(";
  134. $sql.= "'(PROV)'";
  135. $sql.= ", ".$this->total_ht;
  136. $sql.= ", ".$this->total_ttc;
  137. $sql.= ", ".$this->total_tva;
  138. $sql.= ", '".$this->db->idate($this->date_debut)."'";
  139. $sql.= ", '".$this->db->idate($this->date_fin)."'";
  140. $sql.= ", '".$this->db->idate($now)."'";
  141. $sql.= ", ".$fuserid;
  142. $sql.= ", ".($this->fk_user_validator > 0 ? $this->fk_user_validator:"null");
  143. $sql.= ", ".($this->fk_user_modif > 0 ? $this->fk_user_modif:"null");
  144. $sql.= ", ".($this->fk_statut > 1 ? $this->fk_statut:0);
  145. $sql.= ", ".($this->modepaymentid?$this->modepaymentid:"null");
  146. $sql.= ", 0";
  147. $sql.= ", ".($this->note_public?"'".$this->db->escape($this->note_public)."'":"null");
  148. $sql.= ", ".($this->note_private?"'".$this->db->escape($this->note_private)."'":"null");
  149. $sql.= ", ".$conf->entity;
  150. $sql.= ")";
  151. $result = $this->db->query($sql);
  152. if ($result)
  153. {
  154. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
  155. $this->ref='(PROV'.$this->id.')';
  156. $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element." SET ref='".$this->db->escape($this->ref)."' WHERE rowid=".$this->id;
  157. $resql=$this->db->query($sql);
  158. if (!$resql) $error++;
  159. foreach ($this->lines as $i => $val)
  160. {
  161. $newndfline=new ExpenseReportLine($this->db);
  162. $newndfline=$this->lines[$i];
  163. $newndfline->fk_expensereport=$this->id;
  164. if ($result >= 0)
  165. {
  166. $result=$newndfline->insert();
  167. }
  168. if ($result < 0)
  169. {
  170. $error++;
  171. break;
  172. }
  173. }
  174. if (! $error)
  175. {
  176. $result=$this->update_price();
  177. if ($result > 0)
  178. {
  179. if (!$notrigger)
  180. {
  181. // Call trigger
  182. $result=$this->call_trigger('EXPENSE_REPORT_CREATE',$user);
  183. if ($result < 0) {
  184. $error++;
  185. }
  186. // End call triggers
  187. }
  188. if (empty($error))
  189. {
  190. $this->db->commit();
  191. return $this->id;
  192. }
  193. else
  194. {
  195. $this->db->rollback();
  196. return -4;
  197. }
  198. }
  199. else
  200. {
  201. $this->db->rollback();
  202. return -3;
  203. }
  204. }
  205. else
  206. {
  207. dol_syslog(get_class($this)."::create error ".$this->error, LOG_ERR);
  208. $this->db->rollback();
  209. return -2;
  210. }
  211. }
  212. else
  213. {
  214. $this->error=$this->db->lasterror()." sql=".$sql;
  215. $this->db->rollback();
  216. return -1;
  217. }
  218. }
  219. /**
  220. * Load an object from its id and create a new one in database
  221. *
  222. * @param int $fk_user_author Id of new user
  223. * @return int New id of clone
  224. */
  225. function createFromClone($fk_user_author)
  226. {
  227. global $user,$hookmanager;
  228. $error=0;
  229. if (empty($fk_user_author)) $fk_user_author = $user->id;
  230. $this->context['createfromclone'] = 'createfromclone';
  231. $this->db->begin();
  232. // get extrafields so they will be clone
  233. //foreach($this->lines as $line)
  234. //$line->fetch_optionals($line->rowid);
  235. // Load source object
  236. $objFrom = clone $this;
  237. $this->id=0;
  238. $this->ref = '';
  239. $this->status=0;
  240. $this->fk_statut=0;
  241. // Clear fields
  242. $this->fk_user_author = $fk_user_author; // Note fk_user_author is not the 'author' but the guy the expense report is for.
  243. $this->fk_user_valid = '';
  244. $this->date_create = '';
  245. $this->date_creation = '';
  246. $this->date_validation = '';
  247. // Create clone
  248. $result=$this->create($user);
  249. if ($result < 0) $error++;
  250. if (! $error)
  251. {
  252. // Hook of thirdparty module
  253. if (is_object($hookmanager))
  254. {
  255. $parameters=array('objFrom'=>$objFrom);
  256. $action='';
  257. $reshook=$hookmanager->executeHooks('createFrom',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
  258. if ($reshook < 0) $error++;
  259. }
  260. // Call trigger
  261. $result=$this->call_trigger('EXPENSEREPORT_CLONE',$user);
  262. if ($result < 0) $error++;
  263. // End call triggers
  264. }
  265. unset($this->context['createfromclone']);
  266. // End
  267. if (! $error)
  268. {
  269. $this->db->commit();
  270. return $this->id;
  271. }
  272. else
  273. {
  274. $this->db->rollback();
  275. return -1;
  276. }
  277. }
  278. /**
  279. * update
  280. *
  281. * @param User $user User making change
  282. * @param int $notrigger Disable triggers
  283. * @param User $userofexpensereport New user we want to have the expense report on.
  284. * @return int <0 if KO, >0 if OK
  285. */
  286. function update($user, $notrigger = 0, $userofexpensereport=null)
  287. {
  288. global $langs;
  289. $error = 0;
  290. $this->db->begin();
  291. $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET";
  292. $sql.= " total_ht = ".$this->total_ht;
  293. $sql.= " , total_ttc = ".$this->total_ttc;
  294. $sql.= " , total_tva = ".$this->total_tva;
  295. $sql.= " , date_debut = '".$this->db->idate($this->date_debut)."'";
  296. $sql.= " , date_fin = '".$this->db->idate($this->date_fin)."'";
  297. if ($userofexpensereport && is_object($userofexpensereport))
  298. {
  299. $sql.= " , fk_user_author = ".($userofexpensereport->id > 0 ? "'".$userofexpensereport->id."'":"null"); // Note fk_user_author is not the 'author' but the guy the expense report is for.
  300. }
  301. $sql.= " , fk_user_validator = ".($this->fk_user_validator > 0 ? $this->fk_user_validator:"null");
  302. $sql.= " , fk_user_valid = ".($this->fk_user_valid > 0 ? $this->fk_user_valid:"null");
  303. $sql.= " , fk_user_modif = ".$user->id;
  304. $sql.= " , fk_statut = ".($this->fk_statut >= 0 ? $this->fk_statut:'0');
  305. $sql.= " , fk_c_paiement = ".($this->fk_c_paiement > 0 ? $this->fk_c_paiement:"null");
  306. $sql.= " , note_public = ".(!empty($this->note_public)?"'".$this->db->escape($this->note_public)."'":"''");
  307. $sql.= " , note_private = ".(!empty($this->note_private)?"'".$this->db->escape($this->note_private)."'":"''");
  308. $sql.= " , detail_refuse = ".(!empty($this->detail_refuse)?"'".$this->db->escape($this->detail_refuse)."'":"''");
  309. $sql.= " WHERE rowid = ".$this->id;
  310. dol_syslog(get_class($this)."::update sql=".$sql, LOG_DEBUG);
  311. $result = $this->db->query($sql);
  312. if ($result)
  313. {
  314. if (!$notrigger)
  315. {
  316. // Call trigger
  317. $result=$this->call_trigger('EXPENSE_REPORT_UPDATE',$user);
  318. if ($result < 0) {
  319. $error++;
  320. }
  321. // End call triggers
  322. }
  323. if (empty($error))
  324. {
  325. $this->db->commit();
  326. return 1;
  327. }
  328. else
  329. {
  330. $this->db->rollback();
  331. $this->error=$this->db->error();
  332. return -2;
  333. }
  334. }
  335. else
  336. {
  337. $this->db->rollback();
  338. $this->error=$this->db->error();
  339. return -1;
  340. }
  341. }
  342. /**
  343. * Load an object from database
  344. *
  345. * @param int $id Id {@min 1}
  346. * @param string $ref Ref {@name ref}
  347. * @return int <0 if KO, >0 if OK
  348. */
  349. function fetch($id, $ref='')
  350. {
  351. global $conf;
  352. $sql = "SELECT d.rowid, d.ref, d.note_public, d.note_private,"; // DEFAULT
  353. $sql.= " d.detail_refuse, d.detail_cancel, d.fk_user_refuse, d.fk_user_cancel,"; // ACTIONS
  354. $sql.= " d.date_refuse, d.date_cancel,"; // ACTIONS
  355. $sql.= " d.total_ht, d.total_ttc, d.total_tva,"; // TOTAUX (int)
  356. $sql.= " d.date_debut, d.date_fin, d.date_create, d.tms as date_modif, d.date_valid, d.date_approve,"; // DATES (datetime)
  357. $sql.= " d.fk_user_author, d.fk_user_modif, d.fk_user_validator,";
  358. $sql.= " d.fk_user_valid, d.fk_user_approve,";
  359. $sql.= " d.fk_statut as status, d.fk_c_paiement,";
  360. $sql.= " dp.libelle as libelle_paiement, dp.code as code_paiement"; // INNER JOIN paiement
  361. $sql.= " FROM ".MAIN_DB_PREFIX.$this->table_element." as d LEFT JOIN ".MAIN_DB_PREFIX."c_paiement as dp ON d.fk_c_paiement = dp.id";
  362. if ($ref) $sql.= " WHERE d.ref = '".$this->db->escape($ref)."'";
  363. else $sql.= " WHERE d.rowid = ".$id;
  364. $sql.= $restrict;
  365. dol_syslog(get_class($this)."::fetch sql=".$sql, LOG_DEBUG);
  366. $resql = $this->db->query($sql) ;
  367. if ($resql)
  368. {
  369. $obj = $this->db->fetch_object($resql);
  370. if ($obj)
  371. {
  372. $this->id = $obj->rowid;
  373. $this->ref = $obj->ref;
  374. $this->total_ht = $obj->total_ht;
  375. $this->total_tva = $obj->total_tva;
  376. $this->total_ttc = $obj->total_ttc;
  377. $this->note_public = $obj->note_public;
  378. $this->note_private = $obj->note_private;
  379. $this->detail_refuse = $obj->detail_refuse;
  380. $this->detail_cancel = $obj->detail_cancel;
  381. $this->date_debut = $this->db->jdate($obj->date_debut);
  382. $this->date_fin = $this->db->jdate($obj->date_fin);
  383. $this->date_valid = $this->db->jdate($obj->date_valid);
  384. $this->date_approve = $this->db->jdate($obj->date_approve);
  385. $this->date_create = $this->db->jdate($obj->date_create);
  386. $this->date_modif = $this->db->jdate($obj->date_modif);
  387. $this->date_refuse = $this->db->jdate($obj->date_refuse);
  388. $this->date_cancel = $this->db->jdate($obj->date_cancel);
  389. $this->fk_user_author = $obj->fk_user_author; // Note fk_user_author is not the 'author' but the guy the expense report is for.
  390. $this->fk_user_modif = $obj->fk_user_modif;
  391. $this->fk_user_validator = $obj->fk_user_validator;
  392. $this->fk_user_valid = $obj->fk_user_valid;
  393. $this->fk_user_refuse = $obj->fk_user_refuse;
  394. $this->fk_user_cancel = $obj->fk_user_cancel;
  395. $this->fk_user_approve = $obj->fk_user_approve;
  396. $user_author = new User($this->db);
  397. if ($this->fk_user_author > 0) $user_author->fetch($this->fk_user_author);
  398. $this->user_author_infos = dolGetFirstLastname($user_author->firstname, $user_author->lastname);
  399. $user_approver = new User($this->db);
  400. if ($this->fk_user_validator > 0) $user_approver->fetch($this->fk_user_validator);
  401. $this->user_validator_infos = dolGetFirstLastname($user_approver->firstname, $user_approver->lastname);
  402. $this->fk_statut = $obj->status;
  403. $this->status = $obj->status;
  404. $this->fk_c_paiement = $obj->fk_c_paiement;
  405. $this->paid = $obj->paid;
  406. if ($this->fk_statut==5 || $this->fk_statut==6)
  407. {
  408. $user_valid = new User($this->db);
  409. if ($this->fk_user_valid > 0) $user_valid->fetch($this->fk_user_valid);
  410. $this->user_valid_infos = dolGetFirstLastname($user_valid->firstname, $user_valid->lastname);
  411. }
  412. $this->libelle_statut = $obj->libelle_statut;
  413. $this->libelle_paiement = $obj->libelle_paiement;
  414. $this->code_statut = $obj->code_statut;
  415. $this->code_paiement = $obj->code_paiement;
  416. $this->lines = array();
  417. $result=$this->fetch_lines();
  418. return $result;
  419. }
  420. else
  421. {
  422. return 0;
  423. }
  424. }
  425. else
  426. {
  427. $this->error=$this->db->lasterror();
  428. return -1;
  429. }
  430. }
  431. /**
  432. * Classify the expense report as paid
  433. *
  434. * @param int $id Id of expense report
  435. * @param user $fuser User making change
  436. * @param int $notrigger Disable triggers
  437. * @return int <0 if KO, >0 if OK
  438. */
  439. function set_paid($id, $fuser, $notrigger = 0)
  440. {
  441. $error = 0;
  442. $this->db->begin();
  443. $sql = "UPDATE ".MAIN_DB_PREFIX."expensereport";
  444. $sql.= " SET fk_statut = 6, paid=1";
  445. $sql.= " WHERE rowid = ".$id." AND fk_statut = 5";
  446. dol_syslog(get_class($this)."::set_paid sql=".$sql, LOG_DEBUG);
  447. $resql=$this->db->query($sql);
  448. if ($resql)
  449. {
  450. if ($this->db->affected_rows($resql))
  451. {
  452. if (!$notrigger)
  453. {
  454. // Call trigger
  455. $result=$this->call_trigger('EXPENSE_REPORT_PAID',$fuser);
  456. if ($result < 0) {
  457. $error++;
  458. }
  459. // End call triggers
  460. }
  461. if (empty($error))
  462. {
  463. $this->db->commit();
  464. return 1;
  465. }
  466. else
  467. {
  468. $this->db->rollback();
  469. $this->error=$this->db->error();
  470. return -2;
  471. }
  472. }
  473. else
  474. {
  475. $this->db->commit();
  476. return 0;
  477. }
  478. }
  479. else
  480. {
  481. $this->db->rollback();
  482. dol_print_error($this->db);
  483. return -1;
  484. }
  485. }
  486. /**
  487. * Returns the label status
  488. *
  489. * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto
  490. * @return string Label
  491. */
  492. function getLibStatut($mode=0)
  493. {
  494. return $this->LibStatut($this->status,$mode);
  495. }
  496. /**
  497. * Returns the label of a statut
  498. *
  499. * @param int $status id statut
  500. * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto, 6=Long label + Picto
  501. * @return string Label
  502. */
  503. function LibStatut($status,$mode=0)
  504. {
  505. global $langs;
  506. if ($mode == 0)
  507. return $langs->transnoentities($this->statuts[$status]);
  508. if ($mode == 1)
  509. return $langs->transnoentities($this->statuts_short[$status]);
  510. if ($mode == 2)
  511. return img_picto($langs->transnoentities($this->statuts_short[$status]), $this->statuts_logo[$status]).' '.$langs->transnoentities($this->statuts_short[$status]);
  512. if ($mode == 3)
  513. return img_picto($langs->transnoentities($this->statuts_short[$status]), $this->statuts_logo[$status]);
  514. if ($mode == 4)
  515. return img_picto($langs->transnoentities($this->statuts_short[$status]),$this->statuts_logo[$status]).' '.$langs->transnoentities($this->statuts[$status]);
  516. if ($mode == 5)
  517. return '<span class="hideonsmartphone">'.$langs->transnoentities($this->statuts_short[$status]).' </span>'.img_picto($langs->transnoentities($this->statuts_short[$status]),$this->statuts_logo[$status]);
  518. if ($mode == 6)
  519. return $langs->transnoentities($this->statuts[$status]).' '.img_picto($langs->transnoentities($this->statuts_short[$status]),$this->statuts_logo[$status]);
  520. }
  521. /**
  522. * Load information on object
  523. *
  524. * @param int $id Id of object
  525. * @return void
  526. */
  527. function info($id)
  528. {
  529. global $conf;
  530. $sql = "SELECT f.rowid,";
  531. $sql.= " f.date_create as datec,";
  532. $sql.= " f.tms as date_modification,";
  533. $sql.= " f.date_valid as datev,";
  534. $sql.= " f.date_approve as datea,";
  535. $sql.= " f.fk_user_author as fk_user_creation,";
  536. $sql.= " f.fk_user_modif as fk_user_modification,";
  537. $sql.= " f.fk_user_valid,";
  538. $sql.= " f.fk_user_approve";
  539. $sql.= " FROM ".MAIN_DB_PREFIX."expensereport as f";
  540. $sql.= " WHERE f.rowid = ".$id;
  541. $sql.= " AND f.entity = ".$conf->entity;
  542. $resql = $this->db->query($sql);
  543. if ($resql)
  544. {
  545. if ($this->db->num_rows($resql))
  546. {
  547. $obj = $this->db->fetch_object($resql);
  548. $this->id = $obj->rowid;
  549. $this->date_creation = $this->db->jdate($obj->datec);
  550. $this->date_modification = $this->db->jdate($obj->date_modification);
  551. $this->date_validation = $this->db->jdate($obj->datev);
  552. $this->date_approbation = $this->db->jdate($obj->datea);
  553. $cuser = new User($this->db);
  554. $cuser->fetch($obj->fk_user_author);
  555. $this->user_creation = $cuser;
  556. if ($obj->fk_user_creation)
  557. {
  558. $cuser = new User($this->db);
  559. $cuser->fetch($obj->fk_user_creation);
  560. $this->user_creation = $cuser;
  561. }
  562. if ($obj->fk_user_valid)
  563. {
  564. $vuser = new User($this->db);
  565. $vuser->fetch($obj->fk_user_valid);
  566. $this->user_validation = $vuser;
  567. }
  568. if ($obj->fk_user_modification)
  569. {
  570. $muser = new User($this->db);
  571. $muser->fetch($obj->fk_user_modification);
  572. $this->user_modification = $muser;
  573. }
  574. if ($obj->fk_user_approve)
  575. {
  576. $auser = new User($this->db);
  577. $auser->fetch($obj->fk_user_approve);
  578. $this->user_approve = $auser;
  579. }
  580. }
  581. $this->db->free($resql);
  582. }
  583. else
  584. {
  585. dol_print_error($this->db);
  586. }
  587. }
  588. /**
  589. * Initialise an instance with random values.
  590. * Used to build previews or test instances.
  591. * id must be 0 if object instance is a specimen.
  592. *
  593. * @return void
  594. */
  595. function initAsSpecimen()
  596. {
  597. global $user,$langs,$conf;
  598. $now=dol_now();
  599. // Initialise parametres
  600. $this->id=0;
  601. $this->ref = 'SPECIMEN';
  602. $this->specimen=1;
  603. $this->date_create = $now;
  604. $this->date_debut = $now;
  605. $this->date_fin = $now;
  606. $this->date_approve = $now;
  607. $type_fees_id = 2; // TF_TRIP
  608. $this->status = 5;
  609. $this->fk_statut = 5;
  610. $this->fk_user_author = $user->id;
  611. $this->fk_user_valid = $user->id;
  612. $this->fk_user_approve = $user->id;
  613. $this->fk_user_validator = $user->id;
  614. $this->note_private='Private note';
  615. $this->note_public='SPECIMEN';
  616. $nbp = 5;
  617. $xnbp = 0;
  618. while ($xnbp < $nbp)
  619. {
  620. $line=new ExpenseReportLine($this->db);
  621. $line->comments=$langs->trans("Comment")." ".$xnbp;
  622. $line->date=($now-3600*(1+$xnbp));
  623. $line->total_ht=100;
  624. $line->total_tva=20;
  625. $line->total_ttc=120;
  626. $line->qty=1;
  627. $line->vatrate=20;
  628. $line->value_unit=120;
  629. $line->fk_expensereport=0;
  630. $line->type_fees_code='TRA';
  631. $line->fk_c_type_fees=$type_fees_id;
  632. $line->projet_ref = 'ABC';
  633. $this->lines[$xnbp]=$line;
  634. $xnbp++;
  635. $this->total_ht+=$line->total_ht;
  636. $this->total_tva+=$line->total_tva;
  637. $this->total_ttc+=$line->total_ttc;
  638. }
  639. }
  640. /**
  641. * fetch_line_by_project
  642. *
  643. * @param int $projectid Project id
  644. * @param User $user User
  645. * @return int <0 if KO, >0 if OK
  646. */
  647. function fetch_line_by_project($projectid,$user='')
  648. {
  649. global $conf,$db,$langs;
  650. $langs->load('trips');
  651. if ($user->rights->expensereport->lire) {
  652. $sql = "SELECT de.fk_expensereport, de.date, de.comments, de.total_ht, de.total_ttc";
  653. $sql.= " FROM ".MAIN_DB_PREFIX."expensereport_det as de";
  654. $sql.= " WHERE de.fk_projet = ".$projectid;
  655. dol_syslog(get_class($this)."::fetch sql=".$sql, LOG_DEBUG);
  656. $result = $db->query($sql) ;
  657. if ($result)
  658. {
  659. $num = $db->num_rows($result);
  660. $i = 0;
  661. $total_HT = 0;
  662. $total_TTC = 0;
  663. while ($i < $num)
  664. {
  665. $objp = $db->fetch_object($result);
  666. $sql2 = "SELECT d.rowid, d.fk_user_author, d.ref, d.fk_statut";
  667. $sql2.= " FROM ".MAIN_DB_PREFIX."expensereport as d";
  668. $sql2.= " WHERE d.rowid = '".$objp->fk_expensereport."'";
  669. $result2 = $db->query($sql2);
  670. $obj = $db->fetch_object($result2);
  671. $objp->fk_user_author = $obj->fk_user_author;
  672. $objp->ref = $obj->ref;
  673. $objp->fk_c_expensereport_status = $obj->fk_statut;
  674. $objp->rowid = $obj->rowid;
  675. $total_HT = $total_HT + $objp->total_ht;
  676. $total_TTC = $total_TTC + $objp->total_ttc;
  677. $author = new User($db);
  678. $author->fetch($objp->fk_user_author);
  679. print '<tr>';
  680. print '<td><a href="'.DOL_URL_ROOT.'/expensereport/card.php?id='.$objp->rowid.'">'.$objp->ref_num.'</a></td>';
  681. print '<td align="center">'.dol_print_date($objp->date,'day').'</td>';
  682. print '<td>'.$author->getNomUrl().'</td>';
  683. print '<td>'.$objp->comments.'</td>';
  684. print '<td align="right">'.price($objp->total_ht).'</td>';
  685. print '<td align="right">'.price($objp->total_ttc).'</td>';
  686. print '<td align="right">';
  687. switch($objp->fk_c_expensereport_status) {
  688. case 4:
  689. print img_picto($langs->trans('StatusOrderCanceled'),'statut5');
  690. break;
  691. case 1:
  692. print $langs->trans('Draft').' '.img_picto($langs->trans('Draft'),'statut0');
  693. break;
  694. case 2:
  695. print $langs->trans('TripForValid').' '.img_picto($langs->trans('TripForValid'),'statut3');
  696. break;
  697. case 5:
  698. print $langs->trans('TripForPaid').' '.img_picto($langs->trans('TripForPaid'),'statut3');
  699. break;
  700. case 6:
  701. print $langs->trans('TripPaid').' '.img_picto($langs->trans('TripPaid'),'statut4');
  702. break;
  703. }
  704. /*
  705. if ($status==4) return img_picto($langs->trans('StatusOrderCanceled'),'statut5');
  706. if ($status==1) return img_picto($langs->trans('StatusOrderDraft'),'statut0');
  707. if ($status==2) return img_picto($langs->trans('StatusOrderValidated'),'statut1');
  708. if ($status==2) return img_picto($langs->trans('StatusOrderOnProcess'),'statut3');
  709. if ($status==5) return img_picto($langs->trans('StatusOrderToBill'),'statut4');
  710. if ($status==6) return img_picto($langs->trans('StatusOrderOnProcess'),'statut6');
  711. */
  712. print '</td>';
  713. print '</tr>';
  714. $i++;
  715. }
  716. print '<tr class="liste_total"><td colspan="4">'.$langs->trans("Number").': '.$i.'</td>';
  717. print '<td align="right" width="100">'.$langs->trans("TotalHT").' : '.price($total_HT).'</td>';
  718. print '<td align="right" width="100">'.$langs->trans("TotalTTC").' : '.price($total_TTC).'</td>';
  719. print '<td>&nbsp;</td>';
  720. print '</tr>';
  721. }
  722. else
  723. {
  724. $this->error=$db->lasterror();
  725. return -1;
  726. }
  727. }
  728. }
  729. /**
  730. * recalculer
  731. * TODO Replace this with call to update_price if not already done
  732. *
  733. * @param int $id Id of expense report
  734. * @return int <0 if KO, >0 if OK
  735. */
  736. function recalculer($id)
  737. {
  738. $sql = 'SELECT tt.total_ht, tt.total_ttc, tt.total_tva';
  739. $sql.= ' FROM '.MAIN_DB_PREFIX.$this->table_element_line.' as tt';
  740. $sql.= ' WHERE tt.'.$this->fk_element.' = '.$id;
  741. $total_ht = 0; $total_tva = 0; $total_ttc = 0;
  742. $result = $this->db->query($sql);
  743. if($result)
  744. {
  745. $num = $this->db->num_rows($result);
  746. $i = 0;
  747. while ($i < $num):
  748. $objp = $this->db->fetch_object($result);
  749. $total_ht+=$objp->total_ht;
  750. $total_tva+=$objp->total_tva;
  751. $i++;
  752. endwhile;
  753. $total_ttc = $total_ht + $total_tva;
  754. $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET";
  755. $sql.= " total_ht = ".$total_ht;
  756. $sql.= " , total_ttc = ".$total_ttc;
  757. $sql.= " , total_tva = ".$total_tva;
  758. $sql.= " WHERE rowid = ".$id;
  759. $result = $this->db->query($sql);
  760. if($result):
  761. $this->db->free($result);
  762. return 1;
  763. else:
  764. $this->error=$this->db->lasterror();
  765. dol_syslog(get_class($this)."::recalculer: Error ".$this->error,LOG_ERR);
  766. return -3;
  767. endif;
  768. }
  769. else
  770. {
  771. $this->error=$this->db->lasterror();
  772. dol_syslog(get_class($this)."::recalculer: Error ".$this->error,LOG_ERR);
  773. return -3;
  774. }
  775. }
  776. /**
  777. * fetch_lines
  778. *
  779. * @return int <0 if OK, >0 if KO
  780. */
  781. function fetch_lines()
  782. {
  783. $this->lines=array();
  784. $sql = ' SELECT de.rowid, de.comments, de.qty, de.value_unit, de.date,';
  785. $sql.= ' de.'.$this->fk_element.', de.fk_c_type_fees, de.fk_projet, de.tva_tx,';
  786. $sql.= ' de.total_ht, de.total_tva, de.total_ttc,';
  787. $sql.= ' ctf.code as code_type_fees, ctf.label as libelle_type_fees,';
  788. $sql.= ' p.ref as ref_projet, p.title as title_projet';
  789. $sql.= ' FROM '.MAIN_DB_PREFIX.$this->table_element_line.' as de';
  790. $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_type_fees as ctf ON de.fk_c_type_fees = ctf.id';
  791. $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'projet as p ON de.fk_projet = p.rowid';
  792. $sql.= ' WHERE de.'.$this->fk_element.' = '.$this->id;
  793. $sql.= ' ORDER BY de.date ASC';
  794. $resql = $this->db->query($sql);
  795. if ($resql)
  796. {
  797. $num = $this->db->num_rows($resql);
  798. $i = 0;
  799. while ($i < $num)
  800. {
  801. $objp = $this->db->fetch_object($resql);
  802. $deplig = new ExpenseReportLine($this->db);
  803. $deplig->rowid = $objp->rowid;
  804. $deplig->comments = $objp->comments;
  805. $deplig->qty = $objp->qty;
  806. $deplig->value_unit = $objp->value_unit;
  807. $deplig->date = $objp->date;
  808. $deplig->fk_expensereport = $objp->fk_expensereport;
  809. $deplig->fk_c_type_fees = $objp->fk_c_type_fees;
  810. $deplig->fk_projet = $objp->fk_projet;
  811. $deplig->total_ht = $objp->total_ht;
  812. $deplig->total_tva = $objp->total_tva;
  813. $deplig->total_ttc = $objp->total_ttc;
  814. $deplig->type_fees_code = empty($objp->code_type_fees)?'TF_OTHER':$objp->code_type_fees;
  815. $deplig->type_fees_libelle = $objp->libelle_type_fees;
  816. $deplig->tva_tx = $objp->tva_tx;
  817. $deplig->vatrate = $objp->tva_tx;
  818. $deplig->projet_ref = $objp->ref_projet;
  819. $deplig->projet_title = $objp->title_projet;
  820. $this->lines[$i] = $deplig;
  821. $i++;
  822. }
  823. $this->db->free($resql);
  824. return 1;
  825. }
  826. else
  827. {
  828. $this->error=$this->db->lasterror();
  829. dol_syslog(get_class($this)."::fetch_lines: Error ".$this->error, LOG_ERR);
  830. return -3;
  831. }
  832. }
  833. /**
  834. * delete
  835. *
  836. * @param User $fuser User that delete
  837. * @return int <0 if KO, >0 if OK
  838. */
  839. function delete(User $fuser=null)
  840. {
  841. global $user,$langs,$conf;
  842. if (! $rowid) $rowid=$this->id;
  843. $sql = 'DELETE FROM '.MAIN_DB_PREFIX.$this->table_element_line.' WHERE '.$this->fk_element.' = '.$rowid;
  844. if ($this->db->query($sql))
  845. {
  846. $sql = 'DELETE FROM '.MAIN_DB_PREFIX.$this->table_element.' WHERE rowid = '.$rowid;
  847. $resql=$this->db->query($sql);
  848. if ($resql)
  849. {
  850. $this->db->commit();
  851. return 1;
  852. }
  853. else
  854. {
  855. $this->error=$this->db->error()." sql=".$sql;
  856. dol_syslog(get_class($this)."::delete ".$this->error, LOG_ERR);
  857. $this->db->rollback();
  858. return -6;
  859. }
  860. }
  861. else
  862. {
  863. $this->error=$this->db->error()." sql=".$sql;
  864. dol_syslog(get_class($this)."::delete ".$this->error, LOG_ERR);
  865. $this->db->rollback();
  866. return -4;
  867. }
  868. }
  869. /**
  870. * Set to status validate
  871. *
  872. * @param User $fuser User
  873. * @param int $notrigger Disable triggers
  874. * @return int <0 if KO, 0 if nothing done, >0 if OK
  875. */
  876. function setValidate($fuser, $notrigger=0)
  877. {
  878. global $conf,$langs;
  879. $error = 0;
  880. $this->oldref = $this->ref;
  881. $expld_car = (empty($conf->global->NDF_EXPLODE_CHAR))?"-":$conf->global->NDF_EXPLODE_CHAR;
  882. // Sélection de la date de début de la NDF
  883. $sql = 'SELECT date_debut';
  884. $sql.= ' FROM '.MAIN_DB_PREFIX.$this->table_element;
  885. $sql.= ' WHERE rowid = '.$this->id;
  886. $result = $this->db->query($sql);
  887. $objp = $this->db->fetch_object($result);
  888. $this->date_debut = $this->db->jdate($objp->date_debut);
  889. $update_number_int = false;
  890. // Create next ref if ref is PROVxx
  891. // Rename directory if dir was a temporary ref
  892. if (preg_match('/^[\(]?PROV/i', $this->ref))
  893. {
  894. // Sélection du numéro de ref suivant
  895. $ref_next = $this->getNextNumRef();
  896. $ref_number_int = ($this->ref+1)-1;
  897. $update_number_int = true;
  898. // Création du ref_number suivant
  899. if($ref_next)
  900. {
  901. $prefix="ER";
  902. if (! empty($conf->global->EXPENSE_REPORT_PREFIX)) $prefix=$conf->global->EXPENSE_REPORT_PREFIX;
  903. $this->ref = str_replace(' ','_', $this->user_author_infos).$expld_car.$prefix.$this->ref.$expld_car.dol_print_date($this->date_debut,'%y%m%d');
  904. }
  905. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  906. // We rename directory in order to avoid losing the attachments
  907. $oldref = dol_sanitizeFileName($this->oldref);
  908. $newref = dol_sanitizeFileName($this->ref);
  909. $dirsource = $conf->expensereport->dir_output.'/'.$oldref;
  910. $dirdest = $conf->expensereport->dir_output.'/'.$newref;
  911. if (file_exists($dirsource))
  912. {
  913. dol_syslog(get_class($this)."::setValidate() rename dir ".$dirsource." into ".$dirdest);
  914. if (@rename($dirsource, $dirdest))
  915. {
  916. dol_syslog("Rename ok");
  917. // Rename docs starting with $oldref with $newref
  918. $listoffiles=dol_dir_list($conf->expensereport->dir_output.'/'.$newref, 'files', 1, '^'.preg_quote($oldref,'/'));
  919. foreach($listoffiles as $fileentry)
  920. {
  921. $dirsource=$fileentry['name'];
  922. $dirdest=preg_replace('/^'.preg_quote($oldref,'/').'/',$newref, $dirsource);
  923. $dirsource=$fileentry['path'].'/'.$dirsource;
  924. $dirdest=$fileentry['path'].'/'.$dirdest;
  925. @rename($dirsource, $dirdest);
  926. }
  927. }
  928. }
  929. }
  930. if ($this->fk_statut != 2)
  931. {
  932. $now = dol_now();
  933. $this->db->begin();
  934. $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
  935. $sql.= " SET ref = '".$this->db->escape($this->ref)."', fk_statut = 2, fk_user_valid = ".$fuser->id.", date_valid='".$this->db->idate($now)."'";
  936. if ($update_number_int) {
  937. $sql.= ", ref_number_int = ".$ref_number_int;
  938. }
  939. $sql.= ' WHERE rowid = '.$this->id;
  940. $resql=$this->db->query($sql);
  941. if ($resql)
  942. {
  943. if (!$notrigger)
  944. {
  945. // Call trigger
  946. $result=$this->call_trigger('EXPENSE_REPORT_VALIDATE',$fuser);
  947. if ($result < 0) {
  948. $error++;
  949. }
  950. // End call triggers
  951. }
  952. if (empty($error))
  953. {
  954. $this->db->commit();
  955. return 1;
  956. }
  957. else
  958. {
  959. $this->db->rollback();
  960. $this->error=$this->db->error();
  961. return -2;
  962. }
  963. }
  964. else
  965. {
  966. $this->db->rollback();
  967. $this->error=$this->db->lasterror();
  968. return -1;
  969. }
  970. }
  971. else
  972. {
  973. dol_syslog(get_class($this)."::setValidate expensereport already with validated status", LOG_WARNING);
  974. }
  975. return 0;
  976. }
  977. /**
  978. * set_save_from_refuse
  979. *
  980. * @param User $fuser User
  981. * @return int <0 if KO, >0 if OK
  982. */
  983. function set_save_from_refuse($fuser)
  984. {
  985. global $conf,$langs;
  986. // Sélection de la date de début de la NDF
  987. $sql = 'SELECT date_debut';
  988. $sql.= ' FROM '.MAIN_DB_PREFIX.$this->table_element;
  989. $sql.= ' WHERE rowid = '.$this->id;
  990. $result = $this->db->query($sql);
  991. $objp = $this->db->fetch_object($result);
  992. $this->date_debut = $this->db->jdate($objp->date_debut);
  993. if ($this->fk_statut != 2)
  994. {
  995. $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
  996. $sql.= " SET fk_statut = 2";
  997. $sql.= ' WHERE rowid = '.$this->id;
  998. dol_syslog(get_class($this)."::set_save_from_refuse sql=".$sql, LOG_DEBUG);
  999. if ($this->db->query($sql))
  1000. {
  1001. return 1;
  1002. }
  1003. else
  1004. {
  1005. $this->error=$this->db->lasterror();
  1006. return -1;
  1007. }
  1008. }
  1009. else
  1010. {
  1011. dol_syslog(get_class($this)."::set_save_from_refuse expensereport already with save status", LOG_WARNING);
  1012. }
  1013. }
  1014. /**
  1015. * Set status to approved
  1016. *
  1017. * @param User $fuser User
  1018. * @param int $notrigger Disable triggers
  1019. * @return int <0 if KO, 0 if nothing done, >0 if OK
  1020. */
  1021. function setApproved($fuser, $notrigger=0)
  1022. {
  1023. $now=dol_now();
  1024. $error = 0;
  1025. // date approval
  1026. $this->date_approve = $this->db->idate($now);
  1027. if ($this->fk_statut != 5)
  1028. {
  1029. $this->db->begin();
  1030. $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
  1031. $sql.= " SET ref = '".$this->db->escape($this->ref)."', fk_statut = 5, fk_user_approve = ".$fuser->id.",";
  1032. $sql.= " date_approve='".$this->db->idate($this->date_approve)."'";
  1033. $sql.= ' WHERE rowid = '.$this->id;
  1034. if ($this->db->query($sql))
  1035. {
  1036. if (!$notrigger)
  1037. {
  1038. // Call trigger
  1039. $result=$this->call_trigger('EXPENSE_REPORT_APPROVE',$fuser);
  1040. if ($result < 0) {
  1041. $error++;
  1042. }
  1043. // End call triggers
  1044. }
  1045. if (empty($error))
  1046. {
  1047. $this->db->commit();
  1048. return 1;
  1049. }
  1050. else
  1051. {
  1052. $this->db->rollback();
  1053. $this->error=$this->db->error();
  1054. return -2;
  1055. }
  1056. }
  1057. else
  1058. {
  1059. $this->db->rollback();
  1060. $this->error=$this->db->lasterror();
  1061. return -1;
  1062. }
  1063. }
  1064. else
  1065. {
  1066. dol_syslog(get_class($this)."::setApproved expensereport already with approve status", LOG_WARNING);
  1067. }
  1068. return 0;
  1069. }
  1070. /**
  1071. * setDeny
  1072. *
  1073. * @param User $fuser User
  1074. * @param Details $details Details
  1075. * @param int $notrigger Disable triggers
  1076. */
  1077. function setDeny($fuser,$details,$notrigger=0)
  1078. {
  1079. $now = dol_now();
  1080. $error = 0;
  1081. // date de refus
  1082. if ($this->fk_statut != 99)
  1083. {
  1084. $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
  1085. $sql.= " SET ref = '".$this->db->escape($this->ref)."', fk_statut = 99, fk_user_refuse = ".$fuser->id.",";
  1086. $sql.= " date_refuse='".$this->db->idate($now)."',";
  1087. $sql.= " detail_refuse='".$this->db->escape($details)."',";
  1088. $sql.= " fk_user_approve = NULL";
  1089. $sql.= ' WHERE rowid = '.$this->id;
  1090. if ($this->db->query($sql))
  1091. {
  1092. $this->fk_statut = 99;
  1093. $this->fk_user_refuse = $fuser->id;
  1094. $this->detail_refuse = $details;
  1095. $this->date_refuse = $now;
  1096. if (!$notrigger)
  1097. {
  1098. // Call trigger
  1099. $result=$this->call_trigger('EXPENSE_REPORT_DENY',$fuser);
  1100. if ($result < 0) {
  1101. $error++;
  1102. }
  1103. // End call triggers
  1104. }
  1105. if (empty($error))
  1106. {
  1107. $this->db->commit();
  1108. return 1;
  1109. }
  1110. else
  1111. {
  1112. $this->db->rollback();
  1113. $this->error=$this->db->error();
  1114. return -2;
  1115. }
  1116. }
  1117. else
  1118. {
  1119. $this->db->rollback();
  1120. $this->error=$this->db->lasterror();
  1121. return -1;
  1122. }
  1123. }
  1124. else
  1125. {
  1126. dol_syslog(get_class($this)."::setDeny expensereport already with refuse status", LOG_WARNING);
  1127. }
  1128. }
  1129. /**
  1130. * set_unpaid
  1131. *
  1132. * @param User $fuser User
  1133. * @param int $notrigger Disable triggers
  1134. * @return int <0 if KO, >0 if OK
  1135. */
  1136. function set_unpaid($fuser, $notrigger = 0)
  1137. {
  1138. $error = 0;
  1139. if ($this->fk_c_deplacement_statuts != 5)
  1140. {
  1141. $this->db->begin();
  1142. $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
  1143. $sql.= " SET fk_statut = 5";
  1144. $sql.= ' WHERE rowid = '.$this->id;
  1145. dol_syslog(get_class($this)."::set_unpaid sql=".$sql, LOG_DEBUG);
  1146. if ($this->db->query($sql))
  1147. {
  1148. if (!$notrigger)
  1149. {
  1150. // Call trigger
  1151. $result=$this->call_trigger('EXPENSE_REPORT_UNPAID',$fuser);
  1152. if ($result < 0) {
  1153. $error++;
  1154. }
  1155. // End call triggers
  1156. }
  1157. if (empty($error))
  1158. {
  1159. $this->db->commit();
  1160. return 1;
  1161. }
  1162. else
  1163. {
  1164. $this->db->rollback();
  1165. $this->error=$this->db->error();
  1166. return -2;
  1167. }
  1168. }
  1169. else
  1170. {
  1171. $this->db->rollback();
  1172. $this->error=$this->db->error();
  1173. return -1;
  1174. }
  1175. }
  1176. else
  1177. {
  1178. dol_syslog(get_class($this)."::set_unpaid expensereport already with unpaid status", LOG_WARNING);
  1179. }
  1180. }
  1181. /**
  1182. * set_cancel
  1183. *
  1184. * @param User $fuser User
  1185. * @param string $detail Detail
  1186. * @param int $notrigger Disable triggers
  1187. * @return int <0 if KO, >0 if OK
  1188. */
  1189. function set_cancel($fuser,$detail, $notrigger=0)
  1190. {
  1191. $error = 0;
  1192. $this->date_cancel = $this->db->idate(gmmktime());
  1193. if ($this->fk_statut != 4)
  1194. {
  1195. $this->db->begin();
  1196. $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
  1197. $sql.= " SET fk_statut = 4, fk_user_cancel = ".$fuser->id;
  1198. $sql.= ", date_cancel='".$this->db->idate($this->date_cancel)."'";
  1199. $sql.= " ,detail_cancel='".$this->db->escape($detail)."'";
  1200. $sql.= ' WHERE rowid = '.$this->id;
  1201. dol_syslog(get_class($this)."::set_cancel sql=".$sql, LOG_DEBUG);
  1202. if ($this->db->query($sql))
  1203. {
  1204. if (!$notrigger)
  1205. {
  1206. // Call trigger
  1207. $result=$this->call_trigger('EXPENSE_REPORT_CANCEL',$fuser);
  1208. if ($result < 0) {
  1209. $error++;
  1210. }
  1211. // End call triggers
  1212. }
  1213. if (empty($error))
  1214. {
  1215. $this->db->commit();
  1216. return 1;
  1217. }
  1218. else
  1219. {
  1220. $this->db->rollback();
  1221. $this->error=$this->db->error();
  1222. return -2;
  1223. }
  1224. }
  1225. else
  1226. {
  1227. $this->db->rollback();
  1228. $this->error=$this->db->error();
  1229. return -1;
  1230. }
  1231. }
  1232. else
  1233. {
  1234. dol_syslog(get_class($this)."::set_cancel expensereport already with cancel status", LOG_WARNING);
  1235. }
  1236. }
  1237. /**
  1238. * Return next reference of expense report not already used
  1239. *
  1240. * @return string free ref
  1241. */
  1242. function getNextNumRef()
  1243. {
  1244. global $conf;
  1245. $expld_car = (empty($conf->global->NDF_EXPLODE_CHAR))?"-":$conf->global->NDF_EXPLODE_CHAR;
  1246. $num_car = (empty($conf->global->NDF_NUM_CAR_REF))?"5":$conf->global->NDF_NUM_CAR_REF;
  1247. $sql = 'SELECT MAX(de.ref_number_int) as max';
  1248. $sql.= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' de';
  1249. $result = $this->db->query($sql);
  1250. if($this->db->num_rows($result) > 0):
  1251. $objp = $this->db->fetch_object($result);
  1252. $this->ref = $objp->max;
  1253. $this->ref++;
  1254. while(strlen($this->ref) < $num_car):
  1255. $this->ref = "0".$this->ref;
  1256. endwhile;
  1257. else:
  1258. $this->ref = 1;
  1259. while(strlen($this->ref) < $num_car):
  1260. $this->ref = "0".$this->ref;
  1261. endwhile;
  1262. endif;
  1263. if ($result):
  1264. return 1;
  1265. else:
  1266. $this->error=$this->db->error();
  1267. return -1;
  1268. endif;
  1269. }
  1270. /**
  1271. * Return clicable name (with picto eventually)
  1272. *
  1273. * @param int $withpicto 0=No picto, 1=Include picto into link, 2=Only picto
  1274. * @param int $max Max length of shown ref
  1275. * @param int $short 1=Return just URL
  1276. * @param string $moretitle Add more text to title tooltip
  1277. * @param int $notooltip 1=Disable tooltip
  1278. * @return string String with URL
  1279. */
  1280. function getNomUrl($withpicto=0,$max=0,$short=0,$moretitle='',$notooltip=0)
  1281. {
  1282. global $langs, $conf;
  1283. $result='';
  1284. $url = DOL_URL_ROOT.'/expensereport/card.php?id='.$this->id;
  1285. if ($short) return $url;
  1286. $picto='trip';
  1287. $label = '<u>' . $langs->trans("ShowExpenseReport") . '</u>';
  1288. if (! empty($this->ref))
  1289. $label .= '<br><b>' . $langs->trans('Ref') . ':</b> ' . $this->ref;
  1290. if (! empty($this->total_ht))
  1291. $label.= '<br><b>' . $langs->trans('AmountHT') . ':</b> ' . price($this->total_ht, 0, $langs, 0, -1, -1, $conf->currency);
  1292. if (! empty($this->total_tva))
  1293. $label.= '<br><b>' . $langs->trans('VAT') . ':</b> ' . price($this->total_tva, 0, $langs, 0, -1, -1, $conf->currency);
  1294. if (! empty($this->total_ttc))
  1295. $label.= '<br><b>' . $langs->trans('AmountTTC') . ':</b> ' . price($this->total_ttc, 0, $langs, 0, -1, -1, $conf->currency);
  1296. if ($moretitle) $label.=' - '.$moretitle;
  1297. $ref=$this->ref;
  1298. if (empty($ref)) $ref=$this->id;
  1299. $linkclose='';
  1300. if (empty($notooltip))
  1301. {
  1302. if (! empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER))
  1303. {
  1304. $label=$langs->trans("ShowExpenseReport");
  1305. $linkclose.=' alt="'.dol_escape_htmltag($label, 1).'"';
  1306. }
  1307. $linkclose.= ' title="'.dol_escape_htmltag($label, 1).'"';
  1308. $linkclose.=' class="classfortooltip"';
  1309. }
  1310. $linkstart = '<a href="'.$url.'"';
  1311. $linkstart.=$linkclose.'>';
  1312. $linkend='</a>';
  1313. if ($withpicto) $result.=($linkstart.img_object(($notooltip?'':$label), $picto, ($notooltip?'':'class="classfortooltip"'), 0, 0, $notooltip?0:1).$linkend.' ');
  1314. $result.=$linkstart.($max?dol_trunc($ref,$max):$ref).$linkend;
  1315. return $result;
  1316. }
  1317. /**
  1318. * Update total of an expense report when you add a line.
  1319. *
  1320. * @param string $ligne_total_ht Amount without taxes
  1321. * @param string $ligne_total_tva Amount of all taxes
  1322. * @return void
  1323. */
  1324. function update_totaux_add($ligne_total_ht,$ligne_total_tva)
  1325. {
  1326. $this->total_ht = $this->total_ht + $ligne_total_ht;
  1327. $this->total_tva = $this->total_tva + $ligne_total_tva;
  1328. $this->total_ttc = $this->total_ht + $this->total_tva;
  1329. $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET";
  1330. $sql.= " total_ht = ".$this->total_ht;
  1331. $sql.= " , total_ttc = ".$this->total_ttc;
  1332. $sql.= " , total_tva = ".$this->total_tva;
  1333. $sql.= " WHERE rowid = ".$this->id;
  1334. $result = $this->db->query($sql);
  1335. if ($result):
  1336. return 1;
  1337. else:
  1338. $this->error=$this->db->error();
  1339. return -1;
  1340. endif;
  1341. }
  1342. /**
  1343. * Update total of an expense report when you delete a line.
  1344. *
  1345. * @param string $ligne_total_ht Amount without taxes
  1346. * @param string $ligne_total_tva Amount of all taxes
  1347. * @return void
  1348. */
  1349. function update_totaux_del($ligne_total_ht,$ligne_total_tva)
  1350. {
  1351. $this->total_ht = $this->total_ht - $ligne_total_ht;
  1352. $this->total_tva = $this->total_tva - $ligne_total_tva;
  1353. $this->total_ttc = $this->total_ht + $this->total_tva;
  1354. $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET";
  1355. $sql.= " total_ht = ".$this->total_ht;
  1356. $sql.= " , total_ttc = ".$this->total_ttc;
  1357. $sql.= " , total_tva = ".$this->total_tva;
  1358. $sql.= " WHERE rowid = ".$this->id;
  1359. $result = $this->db->query($sql);
  1360. if ($result):
  1361. return 1;
  1362. else:
  1363. $this->error=$this->db->error();
  1364. return -1;
  1365. endif;
  1366. }
  1367. /**
  1368. * updateline
  1369. *
  1370. * @param int $rowid Line to edit
  1371. * @param int $type_fees_id Type payment
  1372. * @param int $projet_id Project id
  1373. * @param double $vatrate Vat rate
  1374. * @param string $comments Description
  1375. * @param real $qty Qty
  1376. * @param double $value_unit Value init
  1377. * @param int $date Date
  1378. * @param int $expensereport_id Expense report id
  1379. * @return int <0 if KO, >0 if OK
  1380. */
  1381. function updateline($rowid, $type_fees_id, $projet_id, $vatrate, $comments, $qty, $value_unit, $date, $expensereport_id)
  1382. {
  1383. global $user;
  1384. if ($this->fk_statut==0 || $this->fk_statut==99)
  1385. {
  1386. $this->db->begin();
  1387. // calcul de tous les totaux de la ligne
  1388. $total_ttc = price2num($qty*$value_unit, 'MT');
  1389. $tx_tva = $vatrate / 100;
  1390. $tx_tva = $tx_tva + 1;
  1391. $total_ht = price2num($total_ttc/$tx_tva, 'MT');
  1392. $total_tva = price2num($total_ttc - $total_ht, 'MT');
  1393. // fin calculs
  1394. $ligne = new ExpenseReportLine($this->db);
  1395. $ligne->comments = $comments;
  1396. $ligne->qty = $qty;
  1397. $ligne->value_unit = $value_unit;
  1398. $ligne->date = $date;
  1399. $ligne->fk_expensereport= $expensereport_id;
  1400. $ligne->fk_c_type_fees = $type_fees_id;
  1401. $ligne->fk_projet = $projet_id;
  1402. $ligne->total_ht = $total_ht;
  1403. $ligne->total_tva = $total_tva;
  1404. $ligne->total_ttc = $total_ttc;
  1405. $ligne->vatrate = price2num($vatrate);
  1406. $ligne->rowid = $rowid;
  1407. // Select des infos sur le type fees
  1408. $sql = "SELECT c.code as code_type_fees, c.label as libelle_type_fees";
  1409. $sql.= " FROM ".MAIN_DB_PREFIX."c_type_fees as c";
  1410. $sql.= " WHERE c.id = ".$type_fees_id;
  1411. $result = $this->db->query($sql);
  1412. $objp_fees = $this->db->fetch_object($result);
  1413. $ligne->type_fees_code = $objp_fees->code_type_fees;
  1414. $ligne->type_fees_libelle = $objp_fees->libelle_type_fees;
  1415. // Select des informations du projet
  1416. $sql = "SELECT p.ref as ref_projet, p.title as title_projet";
  1417. $sql.= " FROM ".MAIN_DB_PREFIX."projet as p";
  1418. $sql.= " WHERE p.rowid = ".$projet_id;
  1419. $result = $this->db->query($sql);
  1420. if ($result) {
  1421. $objp_projet = $this->db->fetch_object($result);
  1422. }
  1423. $ligne->projet_ref = $objp_projet->ref_projet;
  1424. $ligne->projet_title = $objp_projet->title_projet;
  1425. $result = $ligne->update($user);
  1426. if ($result > 0)
  1427. {
  1428. $this->db->commit();
  1429. return 1;
  1430. }
  1431. else
  1432. {
  1433. $this->error=$ligne->error;
  1434. $this->errors=$ligne->errors;
  1435. $this->db->rollback();
  1436. return -2;
  1437. }
  1438. }
  1439. }
  1440. /**
  1441. * deleteline
  1442. *
  1443. * @param int $rowid Row id
  1444. * @param User $fuser User
  1445. * @return int <0 if KO, >0 if OK
  1446. */
  1447. function deleteline($rowid, $fuser='')
  1448. {
  1449. $this->db->begin();
  1450. $sql = 'DELETE FROM '.MAIN_DB_PREFIX.$this->table_element_line;
  1451. $sql.= ' WHERE rowid = '.$rowid;
  1452. dol_syslog(get_class($this)."::deleteline sql=".$sql);
  1453. $result = $this->db->query($sql);
  1454. if (!$result)
  1455. {
  1456. $this->error=$this->db->error();
  1457. dol_syslog(get_class($this)."::deleteline Error ".$this->error, LOG_ERR);
  1458. $this->db->rollback();
  1459. return -1;
  1460. }
  1461. $this->db->commit();
  1462. return 1;
  1463. }
  1464. /**
  1465. * periode_existe
  1466. *
  1467. * @param User $fuser User
  1468. * @param Date $date_debut Start date
  1469. * @param Date $date_fin End date
  1470. * @return int <0 if KO, >0 if OK
  1471. */
  1472. function periode_existe($fuser, $date_debut, $date_fin)
  1473. {
  1474. $sql = "SELECT rowid, date_debut, date_fin";
  1475. $sql.= " FROM ".MAIN_DB_PREFIX.$this->table_element;
  1476. $sql.= " WHERE fk_user_author = '{$fuser->id}'";
  1477. dol_syslog(get_class($this)."::periode_existe sql=".$sql);
  1478. $result = $this->db->query($sql);
  1479. if($result)
  1480. {
  1481. $num_lignes = $this->db->num_rows($result); $i = 0;
  1482. if ($num_lignes>0)
  1483. {
  1484. $date_d_form = $date_debut;
  1485. $date_f_form = $date_fin;
  1486. $existe = false;
  1487. while ($i < $num_lignes)
  1488. {
  1489. $objp = $this->db->fetch_object($result);
  1490. $date_d_req = $this->db->jdate($objp->date_debut); // 3
  1491. $date_f_req = $this->db->jdate($objp->date_fin); // 4
  1492. if (!($date_f_form < $date_d_req || $date_d_form > $date_f_req)) $existe = true;
  1493. $i++;
  1494. }
  1495. if($existe) return 1;
  1496. else return 0;
  1497. }
  1498. else
  1499. {
  1500. return 0;
  1501. }
  1502. }
  1503. else
  1504. {
  1505. $this->error=$this->db->lasterror();
  1506. dol_syslog(get_class($this)."::periode_existe Error ".$this->error, LOG_ERR);
  1507. return -1;
  1508. }
  1509. }
  1510. /**
  1511. * Return list of people with permission to validate expense reports.
  1512. * Search for permission "approve expense report"
  1513. *
  1514. * @return array Array of user ids
  1515. */
  1516. function fetch_users_approver_expensereport()
  1517. {
  1518. $users_validator=array();
  1519. $sql = "SELECT DISTINCT ur.fk_user";
  1520. $sql.= " FROM ".MAIN_DB_PREFIX."user_rights as ur, ".MAIN_DB_PREFIX."rights_def as rd";
  1521. $sql.= " WHERE ur.fk_id = rd.id and rd.module = 'expensereport' AND rd.perms = 'approve'"; // Permission 'Approve';
  1522. $sql.= "UNION";
  1523. $sql.= " SELECT DISTINCT ugu.fk_user";
  1524. $sql.= " FROM ".MAIN_DB_PREFIX."usergroup_user as ugu, ".MAIN_DB_PREFIX."usergroup_rights as ur, ".MAIN_DB_PREFIX."rights_def as rd";
  1525. $sql.= " WHERE ugu.fk_usergroup = ur.fk_usergroup AND ur.fk_id = rd.id and rd.module = 'expensereport' AND rd.perms = 'approve'"; // Permission 'Approve';
  1526. //print $sql;
  1527. dol_syslog(get_class($this)."::fetch_users_approver_expensereport sql=".$sql);
  1528. $result = $this->db->query($sql);
  1529. if($result)
  1530. {
  1531. $num_lignes = $this->db->num_rows($result); $i = 0;
  1532. while ($i < $num_lignes)
  1533. {
  1534. $objp = $this->db->fetch_object($result);
  1535. array_push($users_validator,$objp->fk_user);
  1536. $i++;
  1537. }
  1538. return $users_validator;
  1539. }
  1540. else
  1541. {
  1542. $this->error=$this->db->lasterror();
  1543. dol_syslog(get_class($this)."::fetch_users_approver_expensereport Error ".$this->error, LOG_ERR);
  1544. return -1;
  1545. }
  1546. }
  1547. /**
  1548. * Create a document onto disk accordign to template module.
  1549. *
  1550. * @param string $modele Force le mnodele a utiliser ('' to not force)
  1551. * @param Translate $outputlangs objet lang a utiliser pour traduction
  1552. * @param int $hidedetails Hide details of lines
  1553. * @param int $hidedesc Hide description
  1554. * @param int $hideref Hide ref
  1555. * @return int 0 if KO, 1 if OK
  1556. */
  1557. public function generateDocument($modele, $outputlangs, $hidedetails=0, $hidedesc=0, $hideref=0)
  1558. {
  1559. global $conf,$langs;
  1560. $langs->load("trips");
  1561. if (! dol_strlen($modele)) {
  1562. $modele = 'standard';
  1563. if ($this->modelpdf) {
  1564. $modele = $this->modelpdf;
  1565. } elseif (! empty($conf->global->EXPENSEREPORT_ADDON_PDF)) {
  1566. $modele = $conf->global->EXPENSEREPORT_ADDON_PDF;
  1567. }
  1568. }
  1569. $modelpath = "core/modules/expensereport/doc/";
  1570. return $this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref);
  1571. }
  1572. /**
  1573. * List of types
  1574. *
  1575. * @param int $active Active or not
  1576. * @return array
  1577. */
  1578. function listOfTypes($active=1)
  1579. {
  1580. global $langs;
  1581. $ret=array();
  1582. $sql = "SELECT id, code, label";
  1583. $sql.= " FROM ".MAIN_DB_PREFIX."c_type_fees";
  1584. $sql.= " WHERE active = ".$active;
  1585. dol_syslog(get_class($this)."::listOfTypes", LOG_DEBUG);
  1586. $result = $this->db->query($sql);
  1587. if ( $result )
  1588. {
  1589. $num = $this->db->num_rows($result);
  1590. $i=0;
  1591. while ($i < $num)
  1592. {
  1593. $obj = $this->db->fetch_object($result);
  1594. $ret[$obj->code]=(($langs->trans($obj->code)!=$obj->code)?$langs->trans($obj->code):$obj->label);
  1595. $i++;
  1596. }
  1597. }
  1598. else
  1599. {
  1600. dol_print_error($this->db);
  1601. }
  1602. return $ret;
  1603. }
  1604. /**
  1605. * Charge indicateurs this->nb pour le tableau de bord
  1606. *
  1607. * @return int <0 if KO, >0 if OK
  1608. */
  1609. function load_state_board()
  1610. {
  1611. global $conf;
  1612. $this->nb=array();
  1613. $sql = "SELECT count(ex.rowid) as nb";
  1614. $sql.= " FROM ".MAIN_DB_PREFIX."expensereport as ex";
  1615. $sql.= " WHERE ex.fk_statut > 0";
  1616. $sql.= " AND ex.entity IN (".getEntity('expensereport', 1).")";
  1617. $resql=$this->db->query($sql);
  1618. if ($resql)
  1619. {
  1620. while ($obj=$this->db->fetch_object($resql))
  1621. {
  1622. $this->nb["expensereports"]=$obj->nb;
  1623. }
  1624. $this->db->free($resql);
  1625. return 1;
  1626. }
  1627. else
  1628. {
  1629. dol_print_error($this->db);
  1630. $this->error=$this->db->error();
  1631. return -1;
  1632. }
  1633. }
  1634. /**
  1635. * Load indicators for dashboard (this->nbtodo and this->nbtodolate)
  1636. *
  1637. * @param User $user Objet user
  1638. * @param string $option 'topay' or 'toapprove'
  1639. * @return WorkboardResponse|int <0 if KO, WorkboardResponse if OK
  1640. */
  1641. function load_board($user, $option='topay')
  1642. {
  1643. global $conf, $langs;
  1644. if ($user->societe_id) return -1; // protection pour eviter appel par utilisateur externe
  1645. $now=dol_now();
  1646. $userchildids = $user->getAllChildIds(1);
  1647. $sql = "SELECT ex.rowid, ex.date_valid";
  1648. $sql.= " FROM ".MAIN_DB_PREFIX."expensereport as ex";
  1649. if ($option == 'toapprove') $sql.= " WHERE ex.fk_statut = 2";
  1650. else $sql.= " WHERE ex.fk_statut = 5";
  1651. $sql.= " AND ex.entity IN (".getEntity('expensereport', 1).")";
  1652. $sql.= " AND (ex.fk_user_author IN (".join(',',$userchildids).")";
  1653. $sql.= " OR ex.fk_user_validator IN (".join(',',$userchildids)."))";
  1654. $resql=$this->db->query($sql);
  1655. if ($resql)
  1656. {
  1657. $langs->load("members");
  1658. $response = new WorkboardResponse();
  1659. if ($option == 'toapprove')
  1660. {
  1661. $response->warning_delay=$conf->expensereport->approve->warning_delay/60/60/24;
  1662. $response->label=$langs->trans("ExpenseReportsToApprove");
  1663. $response->url=DOL_URL_ROOT.'/expensereport/list.php?mainmenu=hrm&amp;statut=2';
  1664. }
  1665. else
  1666. {
  1667. $response->warning_delay=$conf->expensereport->payment->warning_delay/60/60/24;
  1668. $response->label=$langs->trans("ExpenseReportsToPay");
  1669. $response->url=DOL_URL_ROOT.'/expensereport/list.php?mainmenu=hrm&amp;statut=5';
  1670. }
  1671. $response->img=img_object('',"trip");
  1672. while ($obj=$this->db->fetch_object($resql))
  1673. {
  1674. $response->nbtodo++;
  1675. if ($option == 'toapprove')
  1676. {
  1677. if ($this->db->jdate($obj->date_valid) < ($now - $conf->expensereport->approve->warning_delay)) {
  1678. $response->nbtodolate++;
  1679. }
  1680. }
  1681. else
  1682. {
  1683. if ($this->db->jdate($obj->date_valid) < ($now - $conf->expensereport->payment->warning_delay)) {
  1684. $response->nbtodolate++;
  1685. }
  1686. }
  1687. }
  1688. return $response;
  1689. }
  1690. else
  1691. {
  1692. dol_print_error($this->db);
  1693. $this->error=$this->db->error();
  1694. return -1;
  1695. }
  1696. }
  1697. /**
  1698. * Return if an expense report is late or not
  1699. *
  1700. * @param string $option 'topay' or 'toapprove'
  1701. * @return boolean True if late, False if not late
  1702. */
  1703. public function hasDelay($option)
  1704. {
  1705. global $conf;
  1706. //Only valid members
  1707. if ($option == 'toapprove' && $this->status != 2) return false;
  1708. if ($option == 'topay' && $this->status != 5) return false;
  1709. $now = dol_now();
  1710. if ($option == 'toapprove')
  1711. {
  1712. return ($this->datevalid?$this->datevalid:$this->date_valid) < ($now - $conf->expensereport->approve->warning_delay);
  1713. }
  1714. else
  1715. return ($this->datevalid?$this->datevalid:$this->date_valid) < ($now - $conf->expensereport->payment->warning_delay);
  1716. }
  1717. }
  1718. /**
  1719. * Class of expense report details lines
  1720. */
  1721. class ExpenseReportLine
  1722. {
  1723. var $db;
  1724. var $error;
  1725. var $rowid;
  1726. var $comments;
  1727. var $qty;
  1728. var $value_unit;
  1729. var $date;
  1730. var $fk_c_type_fees;
  1731. var $fk_projet;
  1732. var $fk_expensereport;
  1733. var $type_fees_code;
  1734. var $type_fees_libelle;
  1735. var $projet_ref;
  1736. var $projet_title;
  1737. var $vatrate;
  1738. var $total_ht;
  1739. var $total_tva;
  1740. var $total_ttc;
  1741. /**
  1742. * Constructor
  1743. *
  1744. * @param DoliDB $db Handlet database
  1745. */
  1746. function __construct($db)
  1747. {
  1748. $this->db= $db;
  1749. }
  1750. /**
  1751. * Fetch record for expense report detailed line
  1752. *
  1753. * @param int $rowid Id of object to load
  1754. * @return int <0 if KO, >0 if OK
  1755. */
  1756. function fetch($rowid)
  1757. {
  1758. $sql = 'SELECT fde.rowid, fde.fk_expensereport, fde.fk_c_type_fees, fde.fk_projet, fde.date,';
  1759. $sql.= ' fde.tva_tx as vatrate, fde.comments, fde.qty, fde.value_unit, fde.total_ht, fde.total_tva, fde.total_ttc,';
  1760. $sql.= ' ctf.code as type_fees_code, ctf.label as type_fees_libelle,';
  1761. $sql.= ' pjt.rowid as projet_id, pjt.title as projet_title, pjt.ref as projet_ref';
  1762. $sql.= ' FROM '.MAIN_DB_PREFIX.'expensereport_det as fde';
  1763. $sql.= ' INNER JOIN '.MAIN_DB_PREFIX.'c_type_fees as ctf ON fde.fk_c_type_fees=ctf.id';
  1764. $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'projet as pjt ON fde.fk_projet=pjt.rowid';
  1765. $sql.= ' WHERE fde.rowid = '.$rowid;
  1766. $result = $this->db->query($sql);
  1767. if($result)
  1768. {
  1769. $objp = $this->db->fetch_object($result);
  1770. $this->rowid = $objp->rowid;
  1771. $this->id = $obj->rowid;
  1772. $this->ref = $obj->ref;
  1773. $this->fk_expensereport = $objp->fk_expensereport;
  1774. $this->comments = $objp->comments;
  1775. $this->qty = $objp->qty;
  1776. $this->date = $objp->date;
  1777. $this->value_unit = $objp->value_unit;
  1778. $this->fk_c_type_fees = $objp->fk_c_type_fees;
  1779. $this->fk_projet = $objp->fk_projet;
  1780. $this->type_fees_code = $objp->type_fees_code;
  1781. $this->type_fees_libelle = $objp->type_fees_libelle;
  1782. $this->projet_ref = $objp->projet_ref;
  1783. $this->projet_title = $objp->projet_title;
  1784. $this->vatrate = $objp->vatrate;
  1785. $this->total_ht = $objp->total_ht;
  1786. $this->total_tva = $objp->total_tva;
  1787. $this->total_ttc = $objp->total_ttc;
  1788. $this->db->free($result);
  1789. } else {
  1790. dol_print_error($this->db);
  1791. }
  1792. }
  1793. /**
  1794. * insert
  1795. *
  1796. * @param int $notrigger 1=No trigger
  1797. * @return int <0 if KO, >0 if OK
  1798. */
  1799. function insert($notrigger=0)
  1800. {
  1801. global $langs,$user,$conf;
  1802. $error=0;
  1803. dol_syslog("ExpenseReportLine::Insert rang=".$this->rang, LOG_DEBUG);
  1804. // Clean parameters
  1805. $this->comments=trim($this->comments);
  1806. if (!$this->value_unit_HT) $this->value_unit_HT=0;
  1807. $this->qty = price2num($this->qty);
  1808. $this->vatrate = price2num($this->vatrate);
  1809. $this->db->begin();
  1810. $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'expensereport_det';
  1811. $sql.= ' (fk_expensereport, fk_c_type_fees, fk_projet,';
  1812. $sql.= ' tva_tx, comments, qty, value_unit, total_ht, total_tva, total_ttc, date)';
  1813. $sql.= " VALUES (".$this->fk_expensereport.",";
  1814. $sql.= " ".$this->fk_c_type_fees.",";
  1815. $sql.= " ".($this->fk_projet>0?$this->fk_projet:'null').",";
  1816. $sql.= " ".$this->vatrate.",";
  1817. $sql.= " '".$this->db->escape($this->comments)."',";
  1818. $sql.= " ".$this->qty.",";
  1819. $sql.= " ".$this->value_unit.",";
  1820. $sql.= " ".$this->total_ht.",";
  1821. $sql.= " ".$this->total_tva.",";
  1822. $sql.= " ".$this->total_ttc.",";
  1823. $sql.= "'".$this->db->idate($this->date)."'";
  1824. $sql.= ")";
  1825. dol_syslog("ExpenseReportLine::insert sql=".$sql);
  1826. $resql=$this->db->query($sql);
  1827. if ($resql)
  1828. {
  1829. $this->rowid=$this->db->last_insert_id(MAIN_DB_PREFIX.'expensereport_det');
  1830. $tmpparent=new ExpenseReport($this->db);
  1831. $tmpparent->fetch($this->fk_expensereport);
  1832. $result = $tmpparent->update_price();
  1833. if ($result < 0)
  1834. {
  1835. $error++;
  1836. $this->error = $tmpparent->error;
  1837. $this->errors = $tmpparent->errors;
  1838. }
  1839. }
  1840. if (! $error)
  1841. {
  1842. $this->db->commit();
  1843. return $this->rowid;
  1844. }
  1845. else
  1846. {
  1847. $this->error=$this->db->lasterror();
  1848. dol_syslog("ExpenseReportLine::insert Error ".$this->error, LOG_ERR);
  1849. $this->db->rollback();
  1850. return -2;
  1851. }
  1852. }
  1853. /**
  1854. * update
  1855. *
  1856. * @param User $fuser User
  1857. * @return int <0 if KO, >0 if OK
  1858. */
  1859. function update($fuser)
  1860. {
  1861. global $fuser,$langs,$conf;
  1862. $error=0;
  1863. // Clean parameters
  1864. $this->comments=trim($this->comments);
  1865. $this->vatrate = price2num($this->vatrate);
  1866. $this->value_unit = price2num($this->value_unit);
  1867. $this->db->begin();
  1868. // Mise a jour ligne en base
  1869. $sql = "UPDATE ".MAIN_DB_PREFIX."expensereport_det SET";
  1870. $sql.= " comments='".$this->db->escape($this->comments)."'";
  1871. $sql.= ",value_unit=".$this->value_unit;
  1872. $sql.= ",qty=".$this->qty;
  1873. $sql.= ",date='".$this->db->idate($this->date)."'";
  1874. $sql.= ",total_ht=".$this->total_ht."";
  1875. $sql.= ",total_tva=".$this->total_tva."";
  1876. $sql.= ",total_ttc=".$this->total_ttc."";
  1877. $sql.= ",tva_tx=".$this->vatrate;
  1878. if ($this->fk_c_type_fees) $sql.= ",fk_c_type_fees=".$this->fk_c_type_fees;
  1879. else $sql.= ",fk_c_type_fees=null";
  1880. if ($this->fk_projet) $sql.= ",fk_projet=".$this->fk_projet;
  1881. else $sql.= ",fk_projet=null";
  1882. $sql.= " WHERE rowid = ".$this->rowid;
  1883. dol_syslog("ExpenseReportLine::update sql=".$sql);
  1884. $resql=$this->db->query($sql);
  1885. if ($resql)
  1886. {
  1887. $tmpparent=new ExpenseReport($this->db);
  1888. $result = $tmpparent->fetch($this->fk_expensereport);
  1889. if ($result > 0)
  1890. {
  1891. $result = $tmpparent->update_price();
  1892. if ($result < 0)
  1893. {
  1894. $error++;
  1895. $this->error = $tmpparent->error;
  1896. $this->errors = $tmpparent->errors;
  1897. }
  1898. }
  1899. else
  1900. {
  1901. $error++;
  1902. $this->error = $tmpparent->error;
  1903. $this->errors = $tmpparent->errors;
  1904. }
  1905. }
  1906. else
  1907. {
  1908. $error++;
  1909. dol_print_error($this->db);
  1910. }
  1911. if (! $error)
  1912. {
  1913. $this->db->commit();
  1914. return 1;
  1915. }
  1916. else
  1917. {
  1918. $this->error=$this->db->lasterror();
  1919. dol_syslog("ExpenseReportLine::update Error ".$this->error, LOG_ERR);
  1920. $this->db->rollback();
  1921. return -2;
  1922. }
  1923. }
  1924. }
  1925. /**
  1926. * Retourne la liste deroulante des differents etats d'une note de frais.
  1927. * Les valeurs de la liste sont les id de la table c_expensereport_statuts
  1928. *
  1929. * @param int $selected preselect status
  1930. * @param string $htmlname Name of HTML select
  1931. * @param int $useempty 1=Add empty line
  1932. * @param int $useshortlabel Use short labels
  1933. * @return string HTML select with status
  1934. */
  1935. function select_expensereport_statut($selected='',$htmlname='fk_statut',$useempty=1, $useshortlabel=0)
  1936. {
  1937. global $db, $langs;
  1938. $tmpep=new ExpenseReport($db);
  1939. print '<select class="flat" name="'.$htmlname.'">';
  1940. if ($useempty) print '<option value="-1">&nbsp;</option>';
  1941. $arrayoflabels=$tmpep->statuts;
  1942. if ($useshortlabel) $arrayoflabels=$tmpep->statuts_short;
  1943. foreach ($arrayoflabels as $key => $val)
  1944. {
  1945. if ($selected != '' && $selected == $key)
  1946. {
  1947. print '<option value="'.$key.'" selected>';
  1948. }
  1949. else
  1950. {
  1951. print '<option value="'.$key.'">';
  1952. }
  1953. print $langs->trans($val);
  1954. print '</option>';
  1955. }
  1956. print '</select>';
  1957. }
  1958. /**
  1959. * Return list of types of notes with select value = id
  1960. *
  1961. * @param int $selected Preselected type
  1962. * @param string $htmlname Name of field in form
  1963. * @param int $showempty Add an empty field
  1964. * @param int $active 1=Active only, 0=Unactive only, -1=All
  1965. * @return string Select html
  1966. */
  1967. function select_type_fees_id($selected='',$htmlname='type',$showempty=0, $active=1)
  1968. {
  1969. global $db,$langs,$user;
  1970. $langs->load("trips");
  1971. print '<select class="flat" name="'.$htmlname.'">';
  1972. if ($showempty)
  1973. {
  1974. print '<option value="-1"';
  1975. if ($selected == -1) print ' selected';
  1976. print '>&nbsp;</option>';
  1977. }
  1978. $sql = "SELECT c.id, c.code, c.label as type FROM ".MAIN_DB_PREFIX."c_type_fees as c";
  1979. if ($active >= 0) $sql.= " WHERE c.active = ".$active;
  1980. $sql.= " ORDER BY c.label ASC";
  1981. $resql=$db->query($sql);
  1982. if ($resql)
  1983. {
  1984. $num = $db->num_rows($resql);
  1985. $i = 0;
  1986. while ($i < $num)
  1987. {
  1988. $obj = $db->fetch_object($resql);
  1989. print '<option value="'.$obj->id.'"';
  1990. if ($obj->code == $selected || $obj->id == $selected) print ' selected';
  1991. print '>';
  1992. if ($obj->code != $langs->trans($obj->code)) print $langs->trans($obj->code);
  1993. else print $langs->trans($obj->type);
  1994. $i++;
  1995. }
  1996. }
  1997. print '</select>';
  1998. }