bookkeeping.class.php 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034
  1. <?php
  2. /* Copyright (C) 2014-2017 Olivier Geffroy <jeff@jeffinfo.com>
  3. * Copyright (C) 2015-2017 Alexandre Spangaro <aspangaro@open-dsi.fr>
  4. * Copyright (C) 2015-2017 Florian Henry <florian.henry@open-concept.pro>
  5. * Copyright (C) 2018-2019 Frédéric France <frederic.france@netlogic.fr>
  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 <https://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * \file htdocs/accountancy/class/bookkeeping.class.php
  22. * \ingroup Accountancy (Double entries)
  23. * \brief File of class to manage Ledger (General Ledger and Subledger)
  24. */
  25. // Class
  26. require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
  27. /**
  28. * Class to manage Ledger (General Ledger and Subledger)
  29. */
  30. class BookKeeping extends CommonObject
  31. {
  32. /**
  33. * @var string Id to identify managed objects
  34. */
  35. public $element = 'accountingbookkeeping';
  36. /**
  37. * @var string Name of table without prefix where object is stored
  38. */
  39. public $table_element = 'accounting_bookkeeping';
  40. /**
  41. * @var int Entity
  42. */
  43. public $entity;
  44. /**
  45. * @var BookKeepingLine[] Lines
  46. */
  47. public $lines = array();
  48. /**
  49. * @var int ID
  50. */
  51. public $id;
  52. /**
  53. * @var string Date of source document, in db date NOT NULL
  54. */
  55. public $doc_date;
  56. /**
  57. * @var int Deadline for payment
  58. */
  59. public $date_lim_reglement;
  60. /**
  61. * @var string doc_type
  62. */
  63. public $doc_type;
  64. /**
  65. * @var string doc_ref
  66. */
  67. public $doc_ref;
  68. /**
  69. * @var int ID
  70. */
  71. public $fk_doc;
  72. /**
  73. * @var int ID
  74. */
  75. public $fk_docdet;
  76. /**
  77. * @var string thirdparty code
  78. */
  79. public $thirdparty_code;
  80. /**
  81. * @var string subledger account
  82. */
  83. public $subledger_account;
  84. /**
  85. * @var string subledger label
  86. */
  87. public $subledger_label;
  88. /**
  89. * @var string doc_type
  90. */
  91. public $numero_compte;
  92. /**
  93. * @var string label compte
  94. */
  95. public $label_compte;
  96. /**
  97. * @var string label operation
  98. */
  99. public $label_operation;
  100. /**
  101. * @var float FEC:Debit
  102. */
  103. public $debit;
  104. /**
  105. * @var float FEC:Credit
  106. */
  107. public $credit;
  108. /**
  109. * @var float FEC:Amount (Not necessary)
  110. */
  111. public $montant;
  112. /**
  113. * @var string FEC:Sens (Not necessary)
  114. */
  115. public $sens;
  116. /**
  117. * @var int ID
  118. */
  119. public $fk_user_author;
  120. /**
  121. * @var string key for import
  122. */
  123. public $import_key;
  124. /**
  125. * @var string code journal
  126. */
  127. public $code_journal;
  128. /**
  129. * @var string label journal
  130. */
  131. public $journal_label;
  132. /**
  133. * @var int accounting transaction id
  134. */
  135. public $piece_num;
  136. /**
  137. * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png
  138. */
  139. public $picto = 'generic';
  140. /**
  141. * Constructor
  142. *
  143. * @param DoliDb $db Database handler
  144. */
  145. public function __construct(DoliDB $db)
  146. {
  147. $this->db = $db;
  148. }
  149. /**
  150. * Create object into database
  151. *
  152. * @param User $user User that creates
  153. * @param bool $notrigger false=launch triggers after, true=disable triggers
  154. * @return int <0 if KO, Id of created object if OK
  155. */
  156. public function create(User $user, $notrigger = false)
  157. {
  158. global $conf, $langs;
  159. dol_syslog(__METHOD__, LOG_DEBUG);
  160. $error = 0;
  161. // Clean parameters
  162. if (isset($this->doc_type)) {
  163. $this->doc_type = trim($this->doc_type);
  164. }
  165. if (isset($this->doc_ref)) {
  166. $this->doc_ref = trim($this->doc_ref);
  167. }
  168. if (isset($this->fk_doc)) {
  169. $this->fk_doc = (int) $this->fk_doc;
  170. }
  171. if (isset($this->fk_docdet)) {
  172. $this->fk_docdet = (int) $this->fk_docdet;
  173. }
  174. if (isset($this->thirdparty_code)) {
  175. $this->thirdparty_code = trim($this->thirdparty_code);
  176. }
  177. if (isset($this->subledger_account)) {
  178. $this->subledger_account = trim($this->subledger_account);
  179. }
  180. if (isset($this->subledger_label)) {
  181. $this->subledger_label = trim($this->subledger_label);
  182. }
  183. if (isset($this->numero_compte)) {
  184. $this->numero_compte = trim($this->numero_compte);
  185. }
  186. if (isset($this->label_compte)) {
  187. $this->label_compte = trim($this->label_compte);
  188. }
  189. if (isset($this->label_operation)) {
  190. $this->label_operation = trim($this->label_operation);
  191. }
  192. if (isset($this->debit)) {
  193. $this->debit = (float) $this->debit;
  194. }
  195. if (isset($this->credit)) {
  196. $this->credit = (float) $this->credit;
  197. }
  198. if (isset($this->montant)) {
  199. $this->montant = (float) $this->montant;
  200. }
  201. if (isset($this->sens)) {
  202. $this->sens = trim($this->sens);
  203. }
  204. if (isset($this->import_key)) {
  205. $this->import_key = trim($this->import_key);
  206. }
  207. if (isset($this->code_journal)) {
  208. $this->code_journal = trim($this->code_journal);
  209. }
  210. if (isset($this->journal_label)) {
  211. $this->journal_label = trim($this->journal_label);
  212. }
  213. if (isset($this->piece_num)) {
  214. $this->piece_num = trim($this->piece_num);
  215. }
  216. if (empty($this->debit)) $this->debit = 0.0;
  217. if (empty($this->credit)) $this->credit = 0.0;
  218. // Check parameters
  219. if (($this->numero_compte == "") || $this->numero_compte == '-1' || $this->numero_compte == 'NotDefined')
  220. {
  221. $langs->loadLangs(array("errors"));
  222. if (in_array($this->doc_type, array('bank', 'expense_report')))
  223. {
  224. $this->errors[] = $langs->trans('ErrorFieldAccountNotDefinedForBankLine', $this->fk_docdet, $this->doc_type);
  225. }
  226. else
  227. {
  228. //$this->errors[]=$langs->trans('ErrorFieldAccountNotDefinedForInvoiceLine', $this->doc_ref, $this->label_compte);
  229. $mesg = $this->doc_ref.', '.$langs->trans("AccountAccounting").': '.$this->numero_compte;
  230. if ($this->subledger_account && $this->subledger_account != $this->numero_compte)
  231. {
  232. $mesg .= ', '.$langs->trans("SubledgerAccount").': '.$this->subledger_account;
  233. }
  234. $this->errors[] = $langs->trans('ErrorFieldAccountNotDefinedForLine', $mesg);
  235. }
  236. return -1;
  237. }
  238. $this->db->begin();
  239. $this->piece_num = 0;
  240. // First check if line not yet already in bookkeeping.
  241. // Note that we must include doc_type - fk_doc - numero_compte - label to be sure to have unicity of line (we may have several lines
  242. // with same doc_type, fk_doc, numero_compte for 1 invoice line when using localtaxes with same account)
  243. // WARNING: This is not reliable, label may have been modified. This is just a small protection.
  244. // The page to make journalization make the test on couple doc_type - fk_doc only.
  245. $sql = "SELECT count(*) as nb";
  246. $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element;
  247. $sql .= " WHERE doc_type = '".$this->db->escape($this->doc_type)."'";
  248. $sql .= " AND fk_doc = ".$this->fk_doc;
  249. //$sql .= " AND fk_docdet = " . $this->fk_docdet; // This field can be 0 if record is for several lines
  250. $sql .= " AND numero_compte = '".$this->db->escape($this->numero_compte)."'";
  251. $sql .= " AND label_operation = '".$this->db->escape($this->label_operation)."'";
  252. $sql .= " AND entity IN (".getEntity('accountancy').")";
  253. $resql = $this->db->query($sql);
  254. if ($resql) {
  255. $row = $this->db->fetch_object($resql);
  256. if ($row->nb == 0)
  257. {
  258. // Determine piece_num
  259. $sqlnum = "SELECT piece_num";
  260. $sqlnum .= " FROM ".MAIN_DB_PREFIX.$this->table_element;
  261. $sqlnum .= " WHERE doc_type = '".$this->db->escape($this->doc_type)."'"; // For example doc_type = 'bank'
  262. $sqlnum .= " AND fk_docdet = ".$this->db->escape($this->fk_docdet); // fk_docdet is rowid into llx_bank or llx_facturedet or llx_facturefourndet, or ...
  263. $sqlnum .= " AND doc_ref = '".$this->db->escape($this->doc_ref)."'"; // ref of source object
  264. $sqlnum .= " AND entity IN (".getEntity('accountancy').")";
  265. dol_syslog(get_class($this).":: create sqlnum=".$sqlnum, LOG_DEBUG);
  266. $resqlnum = $this->db->query($sqlnum);
  267. if ($resqlnum) {
  268. $objnum = $this->db->fetch_object($resqlnum);
  269. $this->piece_num = $objnum->piece_num;
  270. }
  271. dol_syslog(get_class($this).":: create this->piece_num=".$this->piece_num, LOG_DEBUG);
  272. if (empty($this->piece_num)) {
  273. $sqlnum = "SELECT MAX(piece_num)+1 as maxpiecenum";
  274. $sqlnum .= " FROM ".MAIN_DB_PREFIX.$this->table_element;
  275. $sqlnum .= " WHERE entity IN (".getEntity('accountancy').")";
  276. dol_syslog(get_class($this).":: create sqlnum=".$sqlnum, LOG_DEBUG);
  277. $resqlnum = $this->db->query($sqlnum);
  278. if ($resqlnum) {
  279. $objnum = $this->db->fetch_object($resqlnum);
  280. $this->piece_num = $objnum->maxpiecenum;
  281. }
  282. }
  283. dol_syslog(get_class($this).":: create this->piece_num=".$this->piece_num, LOG_DEBUG);
  284. if (empty($this->piece_num)) {
  285. $this->piece_num = 1;
  286. }
  287. $now = dol_now();
  288. $sql = "INSERT INTO ".MAIN_DB_PREFIX.$this->table_element." (";
  289. $sql .= "doc_date";
  290. $sql .= ", date_lim_reglement";
  291. $sql .= ", doc_type";
  292. $sql .= ", doc_ref";
  293. $sql .= ", fk_doc";
  294. $sql .= ", fk_docdet";
  295. $sql .= ", thirdparty_code";
  296. $sql .= ", subledger_account";
  297. $sql .= ", subledger_label";
  298. $sql .= ", numero_compte";
  299. $sql .= ", label_compte";
  300. $sql .= ", label_operation";
  301. $sql .= ", debit";
  302. $sql .= ", credit";
  303. $sql .= ", montant";
  304. $sql .= ", sens";
  305. $sql .= ", fk_user_author";
  306. $sql .= ", date_creation";
  307. $sql .= ", code_journal";
  308. $sql .= ", journal_label";
  309. $sql .= ", piece_num";
  310. $sql .= ', entity';
  311. $sql .= ") VALUES (";
  312. $sql .= "'".$this->db->idate($this->doc_date)."'";
  313. $sql .= ", ".(!isset($this->date_lim_reglement) || dol_strlen($this->date_lim_reglement) == 0 ? 'NULL' : "'".$this->db->idate($this->date_lim_reglement)."'");
  314. $sql .= ", '".$this->db->escape($this->doc_type)."'";
  315. $sql .= ", '".$this->db->escape($this->doc_ref)."'";
  316. $sql .= ", ".$this->fk_doc;
  317. $sql .= ", ".$this->fk_docdet;
  318. $sql .= ", ".(!empty($this->thirdparty_code) ? ("'".$this->db->escape($this->thirdparty_code)."'") : "NULL");
  319. $sql .= ", ".(!empty($this->subledger_account) ? ("'".$this->db->escape($this->subledger_account)."'") : "NULL");
  320. $sql .= ", ".(!empty($this->subledger_label) ? ("'".$this->db->escape($this->subledger_label)."'") : "NULL");
  321. $sql .= ", '".$this->db->escape($this->numero_compte)."'";
  322. $sql .= ", ".(!empty($this->label_operation) ? ("'".$this->db->escape($this->label_operation)."'") : "NULL");
  323. $sql .= ", '".$this->db->escape($this->label_operation)."'";
  324. $sql .= ", ".$this->debit;
  325. $sql .= ", ".$this->credit;
  326. $sql .= ", ".$this->montant;
  327. $sql .= ", ".(!empty($this->sens) ? ("'".$this->db->escape($this->sens)."'") : "NULL");
  328. $sql .= ", '".$this->db->escape($this->fk_user_author)."'";
  329. $sql .= ", '".$this->db->idate($now)."'";
  330. $sql .= ", '".$this->db->escape($this->code_journal)."'";
  331. $sql .= ", ".(!empty($this->journal_label) ? ("'".$this->db->escape($this->journal_label)."'") : "NULL");
  332. $sql .= ", ".$this->db->escape($this->piece_num);
  333. $sql .= ", ".(!isset($this->entity) ? $conf->entity : $this->entity);
  334. $sql .= ")";
  335. dol_syslog(get_class($this).":: create sql=".$sql, LOG_DEBUG);
  336. $resql = $this->db->query($sql);
  337. if ($resql) {
  338. $id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
  339. if ($id > 0) {
  340. $this->id = $id;
  341. $result = 0;
  342. } else {
  343. $result = -2;
  344. $error++;
  345. $this->errors[] = 'Error Create Error '.$result.' lecture ID';
  346. dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
  347. }
  348. } else {
  349. $result = -1;
  350. $error++;
  351. $this->errors[] = 'Error '.$this->db->lasterror();
  352. dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
  353. }
  354. } else { // Already exists
  355. $result = -3;
  356. $error++;
  357. $this->error = 'BookkeepingRecordAlreadyExists';
  358. dol_syslog(__METHOD__.' '.$this->error, LOG_WARNING);
  359. }
  360. } else {
  361. $result = -5;
  362. $error++;
  363. $this->errors[] = 'Error '.$this->db->lasterror();
  364. dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
  365. }
  366. // Uncomment this and change MYOBJECT to your own tag if you
  367. // want this action to call a trigger.
  368. //if (! $error && ! $notrigger) {
  369. // // Call triggers
  370. // $result=$this->call_trigger('MYOBJECT_CREATE',$user);
  371. // if ($result < 0) $error++;
  372. // // End call triggers
  373. //}
  374. // Commit or rollback
  375. if ($error) {
  376. $this->db->rollback();
  377. return -1 * $error;
  378. } else {
  379. $this->db->commit();
  380. return $result;
  381. }
  382. }
  383. /**
  384. * Return a link to the object card (with optionally the picto)
  385. *
  386. * @param int $withpicto Include picto in link (0=No picto, 1=Include picto into link, 2=Only picto)
  387. * @param string $option On what the link point to ('nolink', ...)
  388. * @param int $notooltip 1=Disable tooltip
  389. * @param string $morecss Add more css on link
  390. * @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking
  391. * @return string String with URL
  392. */
  393. public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
  394. {
  395. global $db, $conf, $langs;
  396. global $dolibarr_main_authentication, $dolibarr_main_demo;
  397. global $menumanager;
  398. if (!empty($conf->dol_no_mouse_hover)) $notooltip = 1; // Force disable tooltips
  399. $result = '';
  400. $companylink = '';
  401. $label = '<u>'.$langs->trans("Transaction").'</u>';
  402. $label .= '<br>';
  403. $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->piece_num;
  404. $url = DOL_URL_ROOT.'/accountancy/bookkeeping/card.php?piece_num='.$this->piece_num;
  405. if ($option != 'nolink')
  406. {
  407. // Add param to save lastsearch_values or not
  408. $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
  409. if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) $add_save_lastsearch_values = 1;
  410. if ($add_save_lastsearch_values) $url .= '&save_lastsearch_values=1';
  411. }
  412. $linkclose = '';
  413. if (empty($notooltip))
  414. {
  415. if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER))
  416. {
  417. $label = $langs->trans("ShowTransaction");
  418. $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
  419. }
  420. $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
  421. $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
  422. }
  423. else $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
  424. $linkstart = '<a href="'.$url.'"';
  425. $linkstart .= $linkclose.'>';
  426. $linkend = '</a>';
  427. $result .= $linkstart;
  428. if ($withpicto) $result .= img_object(($notooltip ? '' : $label), ($this->picto ? $this->picto : 'generic'), ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
  429. if ($withpicto != 2) $result .= $this->piece_num;
  430. $result .= $linkend;
  431. //if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : '');
  432. return $result;
  433. }
  434. /**
  435. * Create object into database
  436. *
  437. * @param User $user User that creates
  438. * @param bool $notrigger false=launch triggers after, true=disable triggers
  439. * @param string $mode Mode
  440. * @return int <0 if KO, Id of created object if OK
  441. */
  442. public function createStd(User $user, $notrigger = false, $mode = '')
  443. {
  444. global $conf, $langs;
  445. $langs->loadLangs(array("accountancy", "bills", "compta"));
  446. dol_syslog(__METHOD__, LOG_DEBUG);
  447. $error = 0;
  448. // Clean parameters
  449. if (isset($this->doc_type)) {
  450. $this->doc_type = trim($this->doc_type);
  451. }
  452. if (isset($this->doc_ref)) {
  453. $this->doc_ref = trim($this->doc_ref);
  454. }
  455. if (isset($this->fk_doc)) {
  456. $this->fk_doc = (int) $this->fk_doc;
  457. }
  458. if (isset($this->fk_docdet)) {
  459. $this->fk_docdet = (int) $this->fk_docdet;
  460. }
  461. if (isset($this->thirdparty_code)) {
  462. $this->thirdparty_code = trim($this->thirdparty_code);
  463. }
  464. if (isset($this->subledger_account)) {
  465. $this->subledger_account = trim($this->subledger_account);
  466. }
  467. if (isset($this->subledger_label)) {
  468. $this->subledger_label = trim($this->subledger_label);
  469. }
  470. if (isset($this->numero_compte)) {
  471. $this->numero_compte = trim($this->numero_compte);
  472. }
  473. if (isset($this->label_compte)) {
  474. $this->label_compte = trim($this->label_compte);
  475. }
  476. if (isset($this->label_operation)) {
  477. $this->label_operation = trim($this->label_operation);
  478. }
  479. if (isset($this->debit)) {
  480. $this->debit = trim($this->debit);
  481. }
  482. if (isset($this->credit)) {
  483. $this->credit = trim($this->credit);
  484. }
  485. if (isset($this->montant)) {
  486. $this->montant = trim($this->montant);
  487. }
  488. if (isset($this->sens)) {
  489. $this->sens = trim($this->sens);
  490. }
  491. if (isset($this->import_key)) {
  492. $this->import_key = trim($this->import_key);
  493. }
  494. if (isset($this->code_journal)) {
  495. $this->code_journal = trim($this->code_journal);
  496. }
  497. if (isset($this->journal_label)) {
  498. $this->journal_label = trim($this->journal_label);
  499. }
  500. if (isset($this->piece_num)) {
  501. $this->piece_num = trim($this->piece_num);
  502. }
  503. if (empty($this->debit)) $this->debit = 0;
  504. if (empty($this->credit)) $this->credit = 0;
  505. $this->debit = price2num($this->debit, 'MT');
  506. $this->credit = price2num($this->credit, 'MT');
  507. $now = dol_now();
  508. // Check parameters
  509. $this->journal_label = $langs->trans($this->journal_label);
  510. // Insert request
  511. $sql = 'INSERT INTO '.MAIN_DB_PREFIX.$this->table_element.$mode.' (';
  512. $sql .= 'doc_date,';
  513. $sql .= 'date_lim_reglement,';
  514. $sql .= 'doc_type,';
  515. $sql .= 'doc_ref,';
  516. $sql .= 'fk_doc,';
  517. $sql .= 'fk_docdet,';
  518. $sql .= 'thirdparty_code,';
  519. $sql .= 'subledger_account,';
  520. $sql .= 'subledger_label,';
  521. $sql .= 'numero_compte,';
  522. $sql .= 'label_compte,';
  523. $sql .= 'label_operation,';
  524. $sql .= 'debit,';
  525. $sql .= 'credit,';
  526. $sql .= 'montant,';
  527. $sql .= 'sens,';
  528. $sql .= 'fk_user_author,';
  529. $sql .= 'date_creation,';
  530. $sql .= 'code_journal,';
  531. $sql .= 'journal_label,';
  532. $sql .= 'piece_num,';
  533. $sql .= 'entity';
  534. $sql .= ') VALUES (';
  535. $sql .= ' '.(!isset($this->doc_date) || dol_strlen($this->doc_date) == 0 ? 'NULL' : "'".$this->db->idate($this->doc_date)."'").',';
  536. $sql .= ' '.(!isset($this->date_lim_reglement) || dol_strlen($this->date_lim_reglement) == 0 ? 'NULL' : "'".$this->db->idate($this->date_lim_reglement)."'").',';
  537. $sql .= ' '.(!isset($this->doc_type) ? 'NULL' : "'".$this->db->escape($this->doc_type)."'").',';
  538. $sql .= ' '.(!isset($this->doc_ref) ? 'NULL' : "'".$this->db->escape($this->doc_ref)."'").',';
  539. $sql .= ' '.(empty($this->fk_doc) ? '0' : $this->fk_doc).',';
  540. $sql .= ' '.(empty($this->fk_docdet) ? '0' : $this->fk_docdet).',';
  541. $sql .= ' '.(!isset($this->thirdparty_code) ? 'NULL' : "'".$this->db->escape($this->thirdparty_code)."'").',';
  542. $sql .= ' '.(!isset($this->subledger_account) ? 'NULL' : "'".$this->db->escape($this->subledger_account)."'").',';
  543. $sql .= ' '.(!isset($this->subledger_label) ? 'NULL' : "'".$this->db->escape($this->subledger_label)."'").',';
  544. $sql .= ' '.(!isset($this->numero_compte) ? 'NULL' : "'".$this->db->escape($this->numero_compte)."'").',';
  545. $sql .= ' '.(!isset($this->label_compte) ? 'NULL' : "'".$this->db->escape($this->label_compte)."'").',';
  546. $sql .= ' '.(!isset($this->label_operation) ? 'NULL' : "'".$this->db->escape($this->label_operation)."'").',';
  547. $sql .= ' '.(!isset($this->debit) ? 'NULL' : $this->debit).',';
  548. $sql .= ' '.(!isset($this->credit) ? 'NULL' : $this->credit).',';
  549. $sql .= ' '.(!isset($this->montant) ? 'NULL' : $this->montant).',';
  550. $sql .= ' '.(!isset($this->sens) ? 'NULL' : "'".$this->db->escape($this->sens)."'").',';
  551. $sql .= ' '.$user->id.',';
  552. $sql .= ' '."'".$this->db->idate($now)."',";
  553. $sql .= ' '.(empty($this->code_journal) ? 'NULL' : "'".$this->db->escape($this->code_journal)."'").',';
  554. $sql .= ' '.(empty($this->journal_label) ? 'NULL' : "'".$this->db->escape($this->journal_label)."'").',';
  555. $sql .= ' '.(empty($this->piece_num) ? 'NULL' : $this->db->escape($this->piece_num)).',';
  556. $sql .= ' '.(!isset($this->entity) ? $conf->entity : $this->entity);
  557. $sql .= ')';
  558. $this->db->begin();
  559. $resql = $this->db->query($sql);
  560. if (!$resql) {
  561. $error++;
  562. $this->errors[] = 'Error '.$this->db->lasterror();
  563. dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
  564. }
  565. if (!$error) {
  566. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element.$mode);
  567. // Uncomment this and change MYOBJECT to your own tag if you
  568. // want this action to call a trigger.
  569. //if (! $notrigger) {
  570. // // Call triggers
  571. // $result=$this->call_trigger('MYOBJECT_CREATE',$user);
  572. // if ($result < 0) $error++;
  573. // // End call triggers
  574. //}
  575. }
  576. // Commit or rollback
  577. if ($error) {
  578. $this->db->rollback();
  579. return -1 * $error;
  580. } else {
  581. $this->db->commit();
  582. return $this->id;
  583. }
  584. }
  585. /**
  586. * Load object in memory from the database
  587. *
  588. * @param int $id Id object
  589. * @param string $ref Ref
  590. * @param string $mode Mode
  591. *
  592. * @return int <0 if KO, 0 if not found, >0 if OK
  593. */
  594. public function fetch($id, $ref = null, $mode = '')
  595. {
  596. global $conf;
  597. dol_syslog(__METHOD__, LOG_DEBUG);
  598. $sql = 'SELECT';
  599. $sql .= ' t.rowid,';
  600. $sql .= " t.doc_date,";
  601. $sql .= " t.date_lim_reglement,";
  602. $sql .= " t.doc_type,";
  603. $sql .= " t.doc_ref,";
  604. $sql .= " t.fk_doc,";
  605. $sql .= " t.fk_docdet,";
  606. $sql .= " t.thirdparty_code,";
  607. $sql .= " t.subledger_account,";
  608. $sql .= " t.subledger_label,";
  609. $sql .= " t.numero_compte,";
  610. $sql .= " t.label_compte,";
  611. $sql .= " t.label_operation,";
  612. $sql .= " t.debit,";
  613. $sql .= " t.credit,";
  614. $sql .= " t.montant,";
  615. $sql .= " t.sens,";
  616. $sql .= " t.fk_user_author,";
  617. $sql .= " t.import_key,";
  618. $sql .= " t.code_journal,";
  619. $sql .= " t.journal_label,";
  620. $sql .= " t.piece_num,";
  621. $sql .= " t.date_creation";
  622. $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.$mode.' as t';
  623. $sql .= ' WHERE 1 = 1';
  624. $sql .= " AND entity IN (".getEntity('accountancy').")";
  625. if (null !== $ref) {
  626. $sql .= ' AND t.ref = '.'\''.$ref.'\'';
  627. } else {
  628. $sql .= ' AND t.rowid = '.$id;
  629. }
  630. $resql = $this->db->query($sql);
  631. if ($resql) {
  632. $numrows = $this->db->num_rows($resql);
  633. if ($numrows) {
  634. $obj = $this->db->fetch_object($resql);
  635. $this->id = $obj->rowid;
  636. $this->doc_date = $this->db->jdate($obj->doc_date);
  637. $this->date_lim_reglement = $this->db->jdate($obj->date_lim_reglement);
  638. $this->doc_type = $obj->doc_type;
  639. $this->doc_ref = $obj->doc_ref;
  640. $this->fk_doc = $obj->fk_doc;
  641. $this->fk_docdet = $obj->fk_docdet;
  642. $this->thirdparty_code = $obj->thirdparty_code;
  643. $this->subledger_account = $obj->subledger_account;
  644. $this->subledger_label = $obj->subledger_label;
  645. $this->numero_compte = $obj->numero_compte;
  646. $this->label_compte = $obj->label_compte;
  647. $this->label_operation = $obj->label_operation;
  648. $this->debit = $obj->debit;
  649. $this->credit = $obj->credit;
  650. $this->montant = $obj->montant;
  651. $this->sens = $obj->sens;
  652. $this->fk_user_author = $obj->fk_user_author;
  653. $this->import_key = $obj->import_key;
  654. $this->code_journal = $obj->code_journal;
  655. $this->journal_label = $obj->journal_label;
  656. $this->piece_num = $obj->piece_num;
  657. $this->date_creation = $this->db->jdate($obj->date_creation);
  658. }
  659. $this->db->free($resql);
  660. if ($numrows) {
  661. return 1;
  662. } else {
  663. return 0;
  664. }
  665. } else {
  666. $this->errors[] = 'Error '.$this->db->lasterror();
  667. dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
  668. return -1;
  669. }
  670. }
  671. /**
  672. * Load object in memory from the database
  673. *
  674. * @param string $sortorder Sort Order
  675. * @param string $sortfield Sort field
  676. * @param int $limit offset limit
  677. * @param int $offset offset limit
  678. * @param array $filter filter array
  679. * @param string $filtermode filter mode (AND or OR)
  680. *
  681. * @return int <0 if KO, >=0 if OK
  682. */
  683. public function fetchAllByAccount($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, array $filter = array(), $filtermode = 'AND')
  684. {
  685. global $conf;
  686. dol_syslog(__METHOD__, LOG_DEBUG);
  687. $this->lines = array();
  688. $sql = 'SELECT';
  689. $sql .= ' t.rowid,';
  690. $sql .= " t.doc_date,";
  691. $sql .= " t.doc_type,";
  692. $sql .= " t.doc_ref,";
  693. $sql .= " t.fk_doc,";
  694. $sql .= " t.fk_docdet,";
  695. $sql .= " t.thirdparty_code,";
  696. $sql .= " t.subledger_account,";
  697. $sql .= " t.subledger_label,";
  698. $sql .= " t.numero_compte,";
  699. $sql .= " t.label_compte,";
  700. $sql .= " t.label_operation,";
  701. $sql .= " t.debit,";
  702. $sql .= " t.credit,";
  703. $sql .= " t.montant,";
  704. $sql .= " t.sens,";
  705. $sql .= " t.multicurrency_amount,";
  706. $sql .= " t.multicurrency_code,";
  707. $sql .= " t.lettering_code,";
  708. $sql .= " t.date_lettering,";
  709. $sql .= " t.fk_user_author,";
  710. $sql .= " t.import_key,";
  711. $sql .= " t.code_journal,";
  712. $sql .= " t.journal_label,";
  713. $sql .= " t.piece_num,";
  714. $sql .= " t.date_creation";
  715. // Manage filter
  716. $sqlwhere = array();
  717. if (count($filter) > 0) {
  718. foreach ($filter as $key => $value) {
  719. if ($key == 't.doc_date') {
  720. $sqlwhere[] = $key.'=\''.$this->db->idate($value).'\'';
  721. } elseif ($key == 't.doc_date>=' || $key == 't.doc_date<=') {
  722. $sqlwhere[] = $key.'\''.$this->db->idate($value).'\'';
  723. } elseif ($key == 't.numero_compte>=' || $key == 't.numero_compte<=' || $key == 't.subledger_account>=' || $key == 't.subledger_account<=') {
  724. $sqlwhere[] = $key.'\''.$this->db->escape($value).'\'';
  725. } elseif ($key == 't.fk_doc' || $key == 't.fk_docdet' || $key == 't.piece_num') {
  726. $sqlwhere[] = $key.'='.$value;
  727. } elseif ($key == 't.subledger_account' || $key == 't.numero_compte') {
  728. $sqlwhere[] = $key.' LIKE \''.$this->db->escape($value).'%\'';
  729. } elseif ($key == 't.date_creation>=' || $key == 't.date_creation<=') {
  730. $sqlwhere[] = $key.'\''.$this->db->idate($value).'\'';
  731. } elseif ($key == 't.credit' || $key == 't.debit') {
  732. $sqlwhere[] = natural_search($key, $value, 1, 1);
  733. } else {
  734. $sqlwhere[] = natural_search($key, $value, 0, 1);
  735. }
  736. }
  737. }
  738. $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
  739. $sql .= ' WHERE 1 = 1';
  740. $sql .= " AND entity IN (".getEntity('accountancy').")";
  741. if (count($sqlwhere) > 0) {
  742. $sql .= ' AND '.implode(' '.$filtermode.' ', $sqlwhere);
  743. }
  744. // Affichage par compte comptable
  745. $sql .= ' ORDER BY t.numero_compte ASC';
  746. if (!empty($sortfield)) {
  747. $sql .= ', '.$sortfield.' '.$sortorder;
  748. }
  749. if (!empty($limit)) {
  750. $sql .= ' '.$this->db->plimit($limit + 1, $offset);
  751. }
  752. $resql = $this->db->query($sql);
  753. if ($resql) {
  754. $num = $this->db->num_rows($resql);
  755. $i = 0;
  756. while (($obj = $this->db->fetch_object($resql)) && (empty($limit) || $i < min($limit, $num))) {
  757. $line = new BookKeepingLine();
  758. $line->id = $obj->rowid;
  759. $line->doc_date = $this->db->jdate($obj->doc_date);
  760. $line->doc_type = $obj->doc_type;
  761. $line->doc_ref = $obj->doc_ref;
  762. $line->fk_doc = $obj->fk_doc;
  763. $line->fk_docdet = $obj->fk_docdet;
  764. $line->thirdparty_code = $obj->thirdparty_code;
  765. $line->subledger_account = $obj->subledger_account;
  766. $line->subledger_label = $obj->subledger_label;
  767. $line->numero_compte = $obj->numero_compte;
  768. $line->label_compte = $obj->label_compte;
  769. $line->label_operation = $obj->label_operation;
  770. $line->debit = $obj->debit;
  771. $line->credit = $obj->credit;
  772. $line->montant = $obj->montant;
  773. $line->sens = $obj->sens;
  774. $line->multicurrency_amount = $obj->multicurrency_amount;
  775. $line->multicurrency_code = $obj->multicurrency_code;
  776. $line->lettering_code = $obj->lettering_code;
  777. $line->date_lettering = $obj->date_lettering;
  778. $line->fk_user_author = $obj->fk_user_author;
  779. $line->import_key = $obj->import_key;
  780. $line->code_journal = $obj->code_journal;
  781. $line->journal_label = $obj->journal_label;
  782. $line->piece_num = $obj->piece_num;
  783. $line->date_creation = $obj->date_creation;
  784. $this->lines[] = $line;
  785. $i++;
  786. }
  787. $this->db->free($resql);
  788. return $num;
  789. } else {
  790. $this->errors[] = 'Error '.$this->db->lasterror();
  791. dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
  792. return -1;
  793. }
  794. }
  795. /**
  796. * Load object in memory from the database
  797. *
  798. * @param string $sortorder Sort Order
  799. * @param string $sortfield Sort field
  800. * @param int $limit Offset limit
  801. * @param int $offset Offset limit
  802. * @param array $filter Filter array
  803. * @param string $filtermode Filter mode (AND or OR)
  804. * @param int $showAlreadyExportMovements Show movements when field 'date_export' is not empty (0:No / 1:Yes (Default))
  805. * @return int <0 if KO, >0 if OK
  806. */
  807. public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, array $filter = array(), $filtermode = 'AND', $showAlreadyExportMovements = 1)
  808. {
  809. global $conf;
  810. dol_syslog(__METHOD__, LOG_DEBUG);
  811. $sql = 'SELECT';
  812. $sql .= ' t.rowid,';
  813. $sql .= " t.doc_date,";
  814. $sql .= " t.doc_type,";
  815. $sql .= " t.doc_ref,";
  816. $sql .= " t.fk_doc,";
  817. $sql .= " t.fk_docdet,";
  818. $sql .= " t.thirdparty_code,";
  819. $sql .= " t.subledger_account,";
  820. $sql .= " t.subledger_label,";
  821. $sql .= " t.numero_compte,";
  822. $sql .= " t.label_compte,";
  823. $sql .= " t.label_operation,";
  824. $sql .= " t.debit,";
  825. $sql .= " t.credit,";
  826. $sql .= " t.lettering_code,";
  827. $sql .= " t.date_lettering,";
  828. $sql .= " t.montant,";
  829. $sql .= " t.sens,";
  830. $sql .= " t.fk_user_author,";
  831. $sql .= " t.import_key,";
  832. $sql .= " t.code_journal,";
  833. $sql .= " t.journal_label,";
  834. $sql .= " t.piece_num,";
  835. $sql .= " t.date_creation,";
  836. $sql .= " t.date_lim_reglement,";
  837. $sql .= " t.tms as date_modification,";
  838. $sql .= " t.date_export";
  839. $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
  840. // Manage filter
  841. $sqlwhere = array();
  842. if (count($filter) > 0) {
  843. foreach ($filter as $key => $value) {
  844. if ($key == 't.doc_date') {
  845. $sqlwhere[] = $key.'=\''.$this->db->idate($value).'\'';
  846. } elseif ($key == 't.doc_date>=' || $key == 't.doc_date<=') {
  847. $sqlwhere[] = $key.'\''.$this->db->idate($value).'\'';
  848. } elseif ($key == 't.numero_compte>=' || $key == 't.numero_compte<=' || $key == 't.subledger_account>=' || $key == 't.subledger_account<=') {
  849. $sqlwhere[] = $key.'\''.$this->db->escape($value).'\'';
  850. } elseif ($key == 't.fk_doc' || $key == 't.fk_docdet' || $key == 't.piece_num') {
  851. $sqlwhere[] = $key.'='.$value;
  852. } elseif ($key == 't.subledger_account' || $key == 't.numero_compte') {
  853. $sqlwhere[] = $key.' LIKE \''.$this->db->escape($value).'%\'';
  854. } elseif ($key == 't.date_creation>=' || $key == 't.date_creation<=') {
  855. $sqlwhere[] = $key.'\''.$this->db->idate($value).'\'';
  856. } elseif ($key == 't.tms>=' || $key == 't.tms<=') {
  857. $sqlwhere[] = $key.'\''.$this->db->idate($value).'\'';
  858. } elseif ($key == 't.date_export>=' || $key == 't.date_export<=') {
  859. $sqlwhere[] = $key.'\''.$this->db->idate($value).'\'';
  860. } elseif ($key == 't.credit' || $key == 't.debit') {
  861. $sqlwhere[] = natural_search($key, $value, 1, 1);
  862. } else {
  863. $sqlwhere[] = natural_search($key, $value, 0, 1);
  864. }
  865. }
  866. }
  867. $sql .= ' WHERE t.entity IN ('.getEntity('accountancy').')';
  868. if ($showAlreadyExportMovements == 0) {
  869. $sql .= " AND t.date_export IS NULL";
  870. }
  871. if (count($sqlwhere) > 0) {
  872. $sql .= ' AND '.implode(' '.$filtermode.' ', $sqlwhere);
  873. }
  874. if (!empty($sortfield)) {
  875. $sql .= $this->db->order($sortfield, $sortorder);
  876. }
  877. if (!empty($limit)) {
  878. $sql .= ' '.$this->db->plimit($limit + 1, $offset);
  879. }
  880. $this->lines = array();
  881. $resql = $this->db->query($sql);
  882. if ($resql) {
  883. $num = $this->db->num_rows($resql);
  884. $i = 0;
  885. while (($obj = $this->db->fetch_object($resql)) && (empty($limit) || $i < min($limit, $num)))
  886. {
  887. $line = new BookKeepingLine();
  888. $line->id = $obj->rowid;
  889. $line->doc_date = $this->db->jdate($obj->doc_date);
  890. $line->doc_type = $obj->doc_type;
  891. $line->doc_ref = $obj->doc_ref;
  892. $line->fk_doc = $obj->fk_doc;
  893. $line->fk_docdet = $obj->fk_docdet;
  894. $line->thirdparty_code = $obj->thirdparty_code;
  895. $line->subledger_account = $obj->subledger_account;
  896. $line->subledger_label = $obj->subledger_label;
  897. $line->numero_compte = $obj->numero_compte;
  898. $line->label_compte = $obj->label_compte;
  899. $line->label_operation = $obj->label_operation;
  900. $line->debit = $obj->debit;
  901. $line->credit = $obj->credit;
  902. $line->montant = $obj->montant;
  903. $line->sens = $obj->sens;
  904. $line->lettering_code = $obj->lettering_code;
  905. $line->date_lettering = $obj->date_lettering;
  906. $line->fk_user_author = $obj->fk_user_author;
  907. $line->import_key = $obj->import_key;
  908. $line->code_journal = $obj->code_journal;
  909. $line->journal_label = $obj->journal_label;
  910. $line->piece_num = $obj->piece_num;
  911. $line->date_creation = $this->db->jdate($obj->date_creation);
  912. $line->date_lim_reglement = $this->db->jdate($obj->date_lim_reglement);
  913. $line->date_modification = $this->db->jdate($obj->date_modification);
  914. $line->date_export = $this->db->jdate($obj->date_export);
  915. $this->lines[] = $line;
  916. $i++;
  917. }
  918. $this->db->free($resql);
  919. return $num;
  920. } else {
  921. $this->errors[] = 'Error '.$this->db->lasterror();
  922. dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
  923. return -1;
  924. }
  925. }
  926. /**
  927. * Load object in memory from the database
  928. *
  929. * @param string $sortorder Sort Order
  930. * @param string $sortfield Sort field
  931. * @param int $limit offset limit
  932. * @param int $offset offset limit
  933. * @param array $filter filter array
  934. * @param string $filtermode filter mode (AND or OR)
  935. *
  936. * @return int <0 if KO, >0 if OK
  937. */
  938. public function fetchAllBalance($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, array $filter = array(), $filtermode = 'AND')
  939. {
  940. global $conf;
  941. $this->lines = array();
  942. dol_syslog(__METHOD__, LOG_DEBUG);
  943. $sql = 'SELECT';
  944. $sql .= " t.numero_compte,";
  945. $sql .= " SUM(t.debit) as debit,";
  946. $sql .= " SUM(t.credit) as credit";
  947. $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
  948. // Manage filter
  949. $sqlwhere = array();
  950. if (count($filter) > 0) {
  951. foreach ($filter as $key => $value) {
  952. if ($key == 't.doc_date') {
  953. $sqlwhere[] = $key.'=\''.$this->db->idate($value).'\'';
  954. } elseif ($key == 't.doc_date>=' || $key == 't.doc_date<=') {
  955. $sqlwhere[] = $key.'\''.$this->db->idate($value).'\'';
  956. } elseif ($key == 't.numero_compte>=' || $key == 't.numero_compte<=' || $key == 't.subledger_account>=' || $key == 't.subledger_account<=') {
  957. $sqlwhere[] = $key.'\''.$this->db->escape($value).'\'';
  958. } elseif ($key == 't.fk_doc' || $key == 't.fk_docdet' || $key == 't.piece_num') {
  959. $sqlwhere[] = $key.'='.$value;
  960. } elseif ($key == 't.subledger_account' || $key == 't.numero_compte') {
  961. $sqlwhere[] = $key.' LIKE \''.$this->db->escape($value).'%\'';
  962. } elseif ($key == 't.subledger_label') {
  963. $sqlwhere[] = $key.' LIKE \''.$this->db->escape($value).'%\'';
  964. } else {
  965. $sqlwhere[] = $key.' LIKE \'%'.$this->db->escape($value).'%\'';
  966. }
  967. }
  968. }
  969. $sql .= ' WHERE entity IN ('.getEntity('accountancy').')';
  970. if (count($sqlwhere) > 0) {
  971. $sql .= ' AND '.implode(' '.$filtermode.' ', $sqlwhere);
  972. }
  973. $sql .= ' GROUP BY t.numero_compte';
  974. if (!empty($sortfield)) {
  975. $sql .= $this->db->order($sortfield, $sortorder);
  976. }
  977. if (!empty($limit)) {
  978. $sql .= ' '.$this->db->plimit($limit + 1, $offset);
  979. }
  980. $resql = $this->db->query($sql);
  981. if ($resql)
  982. {
  983. $num = $this->db->num_rows($resql);
  984. $i = 0;
  985. while (($obj = $this->db->fetch_object($resql)) && (empty($limit) || $i < min($limit, $num)))
  986. {
  987. $line = new BookKeepingLine();
  988. $line->numero_compte = $obj->numero_compte;
  989. $line->debit = $obj->debit;
  990. $line->credit = $obj->credit;
  991. $this->lines[] = $line;
  992. $i++;
  993. }
  994. $this->db->free($resql);
  995. return $num;
  996. } else {
  997. $this->errors[] = 'Error '.$this->db->lasterror();
  998. dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
  999. return -1;
  1000. }
  1001. }
  1002. /**
  1003. * Update object into database
  1004. *
  1005. * @param User $user User that modifies
  1006. * @param bool $notrigger false=launch triggers after, true=disable triggers
  1007. * @param string $mode Mode
  1008. * @return int <0 if KO, >0 if OK
  1009. */
  1010. public function update(User $user, $notrigger = false, $mode = '')
  1011. {
  1012. $error = 0;
  1013. dol_syslog(__METHOD__, LOG_DEBUG);
  1014. // Clean parameters
  1015. if (isset($this->doc_type)) {
  1016. $this->doc_type = trim($this->doc_type);
  1017. }
  1018. if (isset($this->doc_ref)) {
  1019. $this->doc_ref = trim($this->doc_ref);
  1020. }
  1021. if (isset($this->fk_doc)) {
  1022. $this->fk_doc = (int) $this->fk_doc;
  1023. }
  1024. if (isset($this->fk_docdet)) {
  1025. $this->fk_docdet = (int) $this->fk_docdet;
  1026. }
  1027. if (isset($this->thirdparty_code)) {
  1028. $this->thirdparty_code = trim($this->thirdparty_code);
  1029. }
  1030. if (isset($this->subledger_account)) {
  1031. $this->subledger_account = trim($this->subledger_account);
  1032. }
  1033. if (isset($this->subledger_label)) {
  1034. $this->subledger_label = trim($this->subledger_label);
  1035. }
  1036. if (isset($this->numero_compte)) {
  1037. $this->numero_compte = trim($this->numero_compte);
  1038. }
  1039. if (isset($this->label_compte)) {
  1040. $this->label_compte = trim($this->label_compte);
  1041. }
  1042. if (isset($this->label_operation)) {
  1043. $this->label_operation = trim($this->label_operation);
  1044. }
  1045. if (isset($this->debit)) {
  1046. $this->debit = trim($this->debit);
  1047. }
  1048. if (isset($this->credit)) {
  1049. $this->credit = trim($this->credit);
  1050. }
  1051. if (isset($this->montant)) {
  1052. $this->montant = trim($this->montant);
  1053. }
  1054. if (isset($this->sens)) {
  1055. $this->sens = trim($this->sens);
  1056. }
  1057. if (isset($this->import_key)) {
  1058. $this->import_key = trim($this->import_key);
  1059. }
  1060. if (isset($this->code_journal)) {
  1061. $this->code_journal = trim($this->code_journal);
  1062. }
  1063. if (isset($this->journal_label)) {
  1064. $this->journal_label = trim($this->journal_label);
  1065. }
  1066. if (isset($this->piece_num)) {
  1067. $this->piece_num = trim($this->piece_num);
  1068. }
  1069. $this->debit = price2num($this->debit, 'MT');
  1070. $this->credit = price2num($this->credit, 'MT');
  1071. // Check parameters
  1072. // Put here code to add a control on parameters values
  1073. // Update request
  1074. $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element.$mode.' SET';
  1075. $sql .= ' doc_date = '.(!isset($this->doc_date) || dol_strlen($this->doc_date) != 0 ? "'".$this->db->idate($this->doc_date)."'" : 'null').',';
  1076. $sql .= ' doc_type = '.(isset($this->doc_type) ? "'".$this->db->escape($this->doc_type)."'" : "null").',';
  1077. $sql .= ' doc_ref = '.(isset($this->doc_ref) ? "'".$this->db->escape($this->doc_ref)."'" : "null").',';
  1078. $sql .= ' fk_doc = '.(isset($this->fk_doc) ? $this->fk_doc : "null").',';
  1079. $sql .= ' fk_docdet = '.(isset($this->fk_docdet) ? $this->fk_docdet : "null").',';
  1080. $sql .= ' thirdparty_code = '.(isset($this->thirdparty_code) ? "'".$this->db->escape($this->thirdparty_code)."'" : "null").',';
  1081. $sql .= ' subledger_account = '.(isset($this->subledger_account) ? "'".$this->db->escape($this->subledger_account)."'" : "null").',';
  1082. $sql .= ' subledger_label = '.(isset($this->subledger_label) ? "'".$this->db->escape($this->subledger_label)."'" : "null").',';
  1083. $sql .= ' numero_compte = '.(isset($this->numero_compte) ? "'".$this->db->escape($this->numero_compte)."'" : "null").',';
  1084. $sql .= ' label_compte = '.(isset($this->label_compte) ? "'".$this->db->escape($this->label_compte)."'" : "null").',';
  1085. $sql .= ' label_operation = '.(isset($this->label_operation) ? "'".$this->db->escape($this->label_operation)."'" : "null").',';
  1086. $sql .= ' debit = '.(isset($this->debit) ? $this->debit : "null").',';
  1087. $sql .= ' credit = '.(isset($this->credit) ? $this->credit : "null").',';
  1088. $sql .= ' montant = '.(isset($this->montant) ? $this->montant : "null").',';
  1089. $sql .= ' sens = '.(isset($this->sens) ? "'".$this->db->escape($this->sens)."'" : "null").',';
  1090. $sql .= ' fk_user_author = '.(isset($this->fk_user_author) ? $this->fk_user_author : "null").',';
  1091. $sql .= ' import_key = '.(isset($this->import_key) ? "'".$this->db->escape($this->import_key)."'" : "null").',';
  1092. $sql .= ' code_journal = '.(isset($this->code_journal) ? "'".$this->db->escape($this->code_journal)."'" : "null").',';
  1093. $sql .= ' journal_label = '.(isset($this->journal_label) ? "'".$this->db->escape($this->journal_label)."'" : "null").',';
  1094. $sql .= ' piece_num = '.(isset($this->piece_num) ? $this->piece_num : "null");
  1095. $sql .= ' WHERE rowid='.$this->id;
  1096. $this->db->begin();
  1097. $resql = $this->db->query($sql);
  1098. if (!$resql) {
  1099. $error++;
  1100. $this->errors[] = 'Error '.$this->db->lasterror();
  1101. dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
  1102. }
  1103. // Uncomment this and change MYOBJECT to your own tag if you
  1104. // want this action calls a trigger.
  1105. //if (! $error && ! $notrigger) {
  1106. // // Call triggers
  1107. // $result=$this->call_trigger('MYOBJECT_MODIFY',$user);
  1108. // if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
  1109. // // End call triggers
  1110. //}
  1111. // Commit or rollback
  1112. if ($error) {
  1113. $this->db->rollback();
  1114. return -1 * $error;
  1115. } else {
  1116. $this->db->commit();
  1117. return 1;
  1118. }
  1119. }
  1120. /**
  1121. * Update movement
  1122. *
  1123. * @param string $piece_num Piece num
  1124. * @param string $field Field
  1125. * @param string $value Value
  1126. * @param string $mode Mode
  1127. * @return number <0 if KO, >0 if OK
  1128. */
  1129. public function updateByMvt($piece_num = '', $field = '', $value = '', $mode = '')
  1130. {
  1131. $error = 0;
  1132. $this->db->begin();
  1133. $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element.$mode." as ab";
  1134. $sql .= ' SET ab.'.$field.'='.(is_numeric($value) ? $value : "'".$this->db->escape($value)."'");
  1135. $sql .= ' WHERE ab.piece_num='.$piece_num;
  1136. $resql = $this->db->query($sql);
  1137. if (!$resql) {
  1138. $error++;
  1139. $this->errors[] = 'Error '.$this->db->lasterror();
  1140. dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
  1141. }
  1142. if ($error) {
  1143. $this->db->rollback();
  1144. return -1 * $error;
  1145. } else {
  1146. $this->db->commit();
  1147. return 1;
  1148. }
  1149. }
  1150. /**
  1151. * Delete object in database
  1152. *
  1153. * @param User $user User that deletes
  1154. * @param bool $notrigger false=launch triggers after, true=disable triggers
  1155. * @param string $mode Mode
  1156. * @return int <0 if KO, >0 if OK
  1157. */
  1158. public function delete(User $user, $notrigger = false, $mode = '')
  1159. {
  1160. dol_syslog(__METHOD__, LOG_DEBUG);
  1161. $error = 0;
  1162. $this->db->begin();
  1163. // Uncomment this and change MYOBJECT to your own tag if you
  1164. // want this action calls a trigger.
  1165. //if (! $error && ! $notrigger) {
  1166. // // Call triggers
  1167. // $result=$this->call_trigger('MYOBJECT_DELETE',$user);
  1168. // if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
  1169. // // End call triggers
  1170. //}
  1171. if (!$error) {
  1172. $sql = 'DELETE FROM '.MAIN_DB_PREFIX.$this->table_element.$mode;
  1173. $sql .= ' WHERE rowid='.$this->id;
  1174. $resql = $this->db->query($sql);
  1175. if (!$resql) {
  1176. $error++;
  1177. $this->errors[] = 'Error '.$this->db->lasterror();
  1178. dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
  1179. }
  1180. }
  1181. // Commit or rollback
  1182. if ($error) {
  1183. $this->db->rollback();
  1184. return -1 * $error;
  1185. } else {
  1186. $this->db->commit();
  1187. return 1;
  1188. }
  1189. }
  1190. /**
  1191. * Delete bookkeeping by importkey
  1192. *
  1193. * @param string $importkey Import key
  1194. * @return int Result
  1195. */
  1196. public function deleteByImportkey($importkey)
  1197. {
  1198. $this->db->begin();
  1199. // first check if line not yet in bookkeeping
  1200. $sql = "DELETE";
  1201. $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element;
  1202. $sql .= " WHERE import_key = '".$this->db->escape($importkey)."'";
  1203. $resql = $this->db->query($sql);
  1204. if (!$resql) {
  1205. $this->errors[] = "Error ".$this->db->lasterror();
  1206. dol_syslog(get_class($this)."::delete Error ".$this->db->lasterror(), LOG_ERR);
  1207. $this->db->rollback();
  1208. return -1;
  1209. }
  1210. $this->db->commit();
  1211. return 1;
  1212. }
  1213. /**
  1214. * Delete bookkeeping by year
  1215. *
  1216. * @param int $delyear Year to delete
  1217. * @param string $journal Journal to delete
  1218. * @param string $mode Mode
  1219. * @param int $delmonth Month
  1220. * @return int <0 if KO, >0 if OK
  1221. */
  1222. public function deleteByYearAndJournal($delyear = 0, $journal = '', $mode = '', $delmonth = 0)
  1223. {
  1224. global $langs;
  1225. if (empty($delyear) && empty($journal))
  1226. {
  1227. $this->error = 'ErrorOneFieldRequired';
  1228. return -1;
  1229. }
  1230. if (!empty($delmonth) && empty($delyear))
  1231. {
  1232. $this->error = 'YearRequiredIfMonthDefined';
  1233. return -2;
  1234. }
  1235. $this->db->begin();
  1236. // Delete record in bookkeeping
  1237. $sql = "DELETE";
  1238. $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element.$mode;
  1239. $sql .= " WHERE 1 = 1";
  1240. $sql .= dolSqlDateFilter('doc_date', 0, $delmonth, $delyear);
  1241. if (!empty($journal)) $sql .= " AND code_journal = '".$this->db->escape($journal)."'";
  1242. $sql .= " AND entity IN (".getEntity('accountancy').")";
  1243. // TODO: In a future we must forbid deletion if record is inside a closed fiscal period.
  1244. $resql = $this->db->query($sql);
  1245. if (!$resql) {
  1246. $this->errors[] = "Error ".$this->db->lasterror();
  1247. foreach ($this->errors as $errmsg) {
  1248. dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
  1249. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  1250. }
  1251. $this->db->rollback();
  1252. return -1;
  1253. }
  1254. $this->db->commit();
  1255. return 1;
  1256. }
  1257. /**
  1258. * Delete bookkeeping by piece number
  1259. *
  1260. * @param int $piecenum Piecenum to delete
  1261. * @return int Result
  1262. */
  1263. public function deleteMvtNum($piecenum)
  1264. {
  1265. global $conf;
  1266. $this->db->begin();
  1267. // first check if line not yet in bookkeeping
  1268. $sql = "DELETE";
  1269. $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element;
  1270. $sql .= " WHERE piece_num = ".(int) $piecenum;
  1271. $sql .= " AND entity IN (".getEntity('accountancy').")";
  1272. $resql = $this->db->query($sql);
  1273. if (!$resql) {
  1274. $this->errors[] = "Error ".$this->db->lasterror();
  1275. foreach ($this->errors as $errmsg) {
  1276. dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
  1277. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  1278. }
  1279. $this->db->rollback();
  1280. return -1;
  1281. }
  1282. $this->db->commit();
  1283. return 1;
  1284. }
  1285. /**
  1286. * Load an object from its id and create a new one in database
  1287. *
  1288. * @param User $user User making the clone
  1289. * @param int $fromid Id of object to clone
  1290. * @return int New id of clone
  1291. */
  1292. public function createFromClone(User $user, $fromid)
  1293. {
  1294. dol_syslog(__METHOD__, LOG_DEBUG);
  1295. $error = 0;
  1296. $object = new BookKeeping($this->db);
  1297. $this->db->begin();
  1298. // Load source object
  1299. $object->fetch($fromid);
  1300. // Reset object
  1301. $object->id = 0;
  1302. // Clear fields
  1303. // ...
  1304. // Create clone
  1305. $object->context['createfromclone'] = 'createfromclone';
  1306. $result = $object->create($user);
  1307. // Other options
  1308. if ($result < 0) {
  1309. $error++;
  1310. $this->errors = $object->errors;
  1311. dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
  1312. }
  1313. unset($object->context['createfromclone']);
  1314. // End
  1315. if (!$error) {
  1316. $this->db->commit();
  1317. return $object->id;
  1318. } else {
  1319. $this->db->rollback();
  1320. return -1;
  1321. }
  1322. }
  1323. /**
  1324. * Initialise object with example values
  1325. * Id must be 0 if object instance is a specimen
  1326. *
  1327. * @return void
  1328. */
  1329. public function initAsSpecimen()
  1330. {
  1331. global $user;
  1332. $now = dol_now();
  1333. $this->id = 0;
  1334. $this->doc_date = $now;
  1335. $this->doc_type = '';
  1336. $this->doc_ref = '';
  1337. $this->fk_doc = 0;
  1338. $this->fk_docdet = 0;
  1339. $this->thirdparty_code = 'CU001';
  1340. $this->subledger_account = '41100001';
  1341. $this->subledger_label = 'My customer company';
  1342. $this->numero_compte = '411';
  1343. $this->label_compte = 'Customer';
  1344. $this->label_operation = 'Sales of pea';
  1345. $this->debit = 99.9;
  1346. $this->credit = '';
  1347. $this->montant = '';
  1348. $this->sens = 'D';
  1349. $this->fk_user_author = $user->id;
  1350. $this->import_key = '';
  1351. $this->code_journal = 'VT';
  1352. $this->journal_label = 'Journal de vente';
  1353. $this->piece_num = '';
  1354. $this->date_creation = $now;
  1355. }
  1356. /**
  1357. * Load an accounting document into memory from database
  1358. *
  1359. * @param int $piecenum Accounting document to get
  1360. * @param string $mode Mode
  1361. * @return int <0 if KO, >0 if OK
  1362. */
  1363. public function fetchPerMvt($piecenum, $mode = '')
  1364. {
  1365. global $conf;
  1366. $sql = "SELECT piece_num,doc_date,code_journal,journal_label,doc_ref,doc_type,date_creation";
  1367. $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element.$mode;
  1368. $sql .= " WHERE piece_num = ".$piecenum;
  1369. $sql .= " AND entity IN (".getEntity('accountancy').")";
  1370. dol_syslog(__METHOD__, LOG_DEBUG);
  1371. $result = $this->db->query($sql);
  1372. if ($result) {
  1373. $obj = $this->db->fetch_object($result);
  1374. $this->piece_num = $obj->piece_num;
  1375. $this->code_journal = $obj->code_journal;
  1376. $this->journal_label = $obj->journal_label;
  1377. $this->doc_date = $this->db->jdate($obj->doc_date);
  1378. $this->doc_ref = $obj->doc_ref;
  1379. $this->doc_type = $obj->doc_type;
  1380. $this->date_creation = $obj->date_creation;
  1381. } else {
  1382. $this->error = "Error ".$this->db->lasterror();
  1383. dol_syslog(__METHOD__.$this->error, LOG_ERR);
  1384. return -1;
  1385. }
  1386. return 1;
  1387. }
  1388. /**
  1389. * Return next number movement
  1390. *
  1391. * @param string $mode Mode
  1392. * @return string Next numero to use
  1393. */
  1394. public function getNextNumMvt($mode = '')
  1395. {
  1396. global $conf;
  1397. $sql = "SELECT MAX(piece_num)+1 as max FROM ".MAIN_DB_PREFIX.$this->table_element.$mode;
  1398. $sql .= " WHERE entity IN (".getEntity('accountancy').")";
  1399. dol_syslog(get_class($this)."getNextNumMvt sql=".$sql, LOG_DEBUG);
  1400. $result = $this->db->query($sql);
  1401. if ($result) {
  1402. $obj = $this->db->fetch_object($result);
  1403. if ($obj) $result = $obj->max;
  1404. if (empty($result)) $result = 1;
  1405. return $result;
  1406. } else {
  1407. $this->error = "Error ".$this->db->lasterror();
  1408. dol_syslog(get_class($this)."::getNextNumMvt ".$this->error, LOG_ERR);
  1409. return -1;
  1410. }
  1411. }
  1412. /**
  1413. * Load all informations of accountancy document
  1414. *
  1415. * @param int $piecenum Id of line to get
  1416. * @param string $mode Mode
  1417. * @return int <0 if KO, >0 if OK
  1418. */
  1419. public function fetchAllPerMvt($piecenum, $mode = '')
  1420. {
  1421. global $conf;
  1422. $sql = "SELECT rowid, doc_date, doc_type,";
  1423. $sql .= " doc_ref, fk_doc, fk_docdet, thirdparty_code, subledger_account, subledger_label,";
  1424. $sql .= " numero_compte, label_compte, label_operation, debit, credit,";
  1425. $sql .= " montant, sens, fk_user_author, import_key, code_journal, journal_label, piece_num, date_creation";
  1426. $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element.$mode;
  1427. $sql .= " WHERE piece_num = ".$piecenum;
  1428. $sql .= " AND entity IN (".getEntity('accountancy').")";
  1429. dol_syslog(__METHOD__, LOG_DEBUG);
  1430. $result = $this->db->query($sql);
  1431. if ($result) {
  1432. while ($obj = $this->db->fetch_object($result)) {
  1433. $line = new BookKeepingLine();
  1434. $line->id = $obj->rowid;
  1435. $line->doc_date = $this->db->jdate($obj->doc_date);
  1436. $line->doc_type = $obj->doc_type;
  1437. $line->doc_ref = $obj->doc_ref;
  1438. $line->fk_doc = $obj->fk_doc;
  1439. $line->fk_docdet = $obj->fk_docdet;
  1440. $line->thirdparty_code = $obj->thirdparty_code;
  1441. $line->subledger_account = $obj->subledger_account;
  1442. $line->subledger_label = $obj->subledger_label;
  1443. $line->numero_compte = $obj->numero_compte;
  1444. $line->label_compte = $obj->label_compte;
  1445. $line->label_operation = $obj->label_operation;
  1446. $line->debit = $obj->debit;
  1447. $line->credit = $obj->credit;
  1448. $line->montant = $obj->montant;
  1449. $line->sens = $obj->sens;
  1450. $line->code_journal = $obj->code_journal;
  1451. $line->journal_label = $obj->journal_label;
  1452. $line->piece_num = $obj->piece_num;
  1453. $line->date_creation = $obj->date_creation;
  1454. $this->linesmvt[] = $line;
  1455. }
  1456. } else {
  1457. $this->error = "Error ".$this->db->lasterror();
  1458. dol_syslog(__METHOD__.$this->error, LOG_ERR);
  1459. return -1;
  1460. }
  1461. return 1;
  1462. }
  1463. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1464. /**
  1465. * Export bookkeeping
  1466. *
  1467. * @param string $model Model
  1468. * @return int Result
  1469. */
  1470. public function export_bookkeeping($model = 'ebp')
  1471. {
  1472. // phpcs:enable
  1473. global $conf;
  1474. $sql = "SELECT rowid, doc_date, doc_type,";
  1475. $sql .= " doc_ref, fk_doc, fk_docdet, thirdparty_code, subledger_account, subledger_label,";
  1476. $sql .= " numero_compte, label_compte, label_operation, debit, credit,";
  1477. $sql .= " montant, sens, fk_user_author, import_key, code_journal, piece_num";
  1478. $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element;
  1479. $sql .= " WHERE entity IN (".getEntity('accountancy').")";
  1480. dol_syslog(get_class($this)."::export_bookkeeping", LOG_DEBUG);
  1481. $resql = $this->db->query($sql);
  1482. if ($resql) {
  1483. $this->linesexport = array();
  1484. $num = $this->db->num_rows($resql);
  1485. while ($obj = $this->db->fetch_object($resql)) {
  1486. $line = new BookKeepingLine();
  1487. $line->id = $obj->rowid;
  1488. $line->doc_date = $this->db->jdate($obj->doc_date);
  1489. $line->doc_type = $obj->doc_type;
  1490. $line->doc_ref = $obj->doc_ref;
  1491. $line->fk_doc = $obj->fk_doc;
  1492. $line->fk_docdet = $obj->fk_docdet;
  1493. $line->thirdparty_code = $obj->thirdparty_code;
  1494. $line->subledger_account = $obj->subledger_account;
  1495. $line->subledger_label = $obj->subledger_label;
  1496. $line->numero_compte = $obj->numero_compte;
  1497. $line->label_compte = $obj->label_compte;
  1498. $line->label_operation = $obj->label_operation;
  1499. $line->debit = $obj->debit;
  1500. $line->credit = $obj->credit;
  1501. $line->montant = $obj->montant;
  1502. $line->sens = $obj->sens;
  1503. $line->code_journal = $obj->code_journal;
  1504. $line->piece_num = $obj->piece_num;
  1505. $this->linesexport[] = $line;
  1506. }
  1507. $this->db->free($resql);
  1508. return $num;
  1509. } else {
  1510. $this->error = "Error ".$this->db->lasterror();
  1511. dol_syslog(get_class($this)."::export_bookkeeping ".$this->error, LOG_ERR);
  1512. return -1;
  1513. }
  1514. }
  1515. /**
  1516. * Transform transaction
  1517. *
  1518. * @param number $direction If 0 tmp => real, if 1 real => tmp
  1519. * @param string $piece_num Piece num
  1520. * @return int int <0 if KO, >0 if OK
  1521. */
  1522. public function transformTransaction($direction = 0, $piece_num = '')
  1523. {
  1524. $error = 0;
  1525. $this->db->begin();
  1526. if ($direction == 0)
  1527. {
  1528. $next_piecenum = $this->getNextNumMvt();
  1529. $now = dol_now();
  1530. if ($next_piecenum < 0) {
  1531. $error++;
  1532. }
  1533. $sql = 'INSERT INTO '.MAIN_DB_PREFIX.$this->table_element.' (doc_date, doc_type,';
  1534. $sql .= ' doc_ref, fk_doc, fk_docdet, entity, thirdparty_code, subledger_account, subledger_label,';
  1535. $sql .= ' numero_compte, label_compte, label_operation, debit, credit,';
  1536. $sql .= ' montant, sens, fk_user_author, import_key, code_journal, journal_label, piece_num, date_creation)';
  1537. $sql .= ' SELECT doc_date, doc_type,';
  1538. $sql .= ' doc_ref, fk_doc, fk_docdet, entity, thirdparty_code, subledger_account, subledger_label,';
  1539. $sql .= ' numero_compte, label_compte, label_operation, debit, credit,';
  1540. $sql .= ' montant, sens, fk_user_author, import_key, code_journal, journal_label, '.$next_piecenum.", '".$this->db->idate($now)."'";
  1541. $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.'_tmp WHERE piece_num = '.$this->db->escape($piece_num);
  1542. $resql = $this->db->query($sql);
  1543. if (!$resql) {
  1544. $error++;
  1545. $this->errors[] = 'Error '.$this->db->lasterror();
  1546. dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
  1547. }
  1548. $sql = 'DELETE FROM '.MAIN_DB_PREFIX.$this->table_element.'_tmp WHERE piece_num = '.$this->db->escape($piece_num);
  1549. $resql = $this->db->query($sql);
  1550. if (!$resql) {
  1551. $error++;
  1552. $this->errors[] = 'Error '.$this->db->lasterror();
  1553. dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
  1554. }
  1555. } elseif ($direction == 1) {
  1556. $sql = 'DELETE FROM '.MAIN_DB_PREFIX.$this->table_element.'_tmp WHERE piece_num = '.$piece_num;
  1557. $resql = $this->db->query($sql);
  1558. if (!$resql) {
  1559. $error++;
  1560. $this->errors[] = 'Error '.$this->db->lasterror();
  1561. dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
  1562. }
  1563. $sql = 'INSERT INTO '.MAIN_DB_PREFIX.$this->table_element.'_tmp (doc_date, doc_type,';
  1564. $sql .= ' doc_ref, fk_doc, fk_docdet, thirdparty_code, subledger_account, subledger_label,';
  1565. $sql .= ' numero_compte, label_compte, label_operation, debit, credit,';
  1566. $sql .= ' montant, sens, fk_user_author, import_key, code_journal, journal_label, piece_num)';
  1567. $sql .= ' SELECT doc_date, doc_type,';
  1568. $sql .= ' doc_ref, fk_doc, fk_docdet, thirdparty_code, subledger_account, subledger_label,';
  1569. $sql .= ' numero_compte, label_compte, label_operation, debit, credit,';
  1570. $sql .= ' montant, sens, fk_user_author, import_key, code_journal, journal_label, piece_num';
  1571. $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' WHERE piece_num = '.$piece_num;
  1572. $resql = $this->db->query($sql);
  1573. if (!$resql) {
  1574. $error++;
  1575. $this->errors[] = 'Error '.$this->db->lasterror();
  1576. dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
  1577. }
  1578. $sql = 'DELETE FROM '.MAIN_DB_PREFIX.$this->table_element.'_tmp WHERE piece_num = '.$piece_num;
  1579. $resql = $this->db->query($sql);
  1580. if (!$resql) {
  1581. $error++;
  1582. $this->errors[] = 'Error '.$this->db->lasterror();
  1583. dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
  1584. }
  1585. }
  1586. if (!$error) {
  1587. $this->db->commit();
  1588. return 1;
  1589. } else {
  1590. $this->db->rollback();
  1591. return -1;
  1592. }
  1593. /*
  1594. $sql = "DELETE FROM ";
  1595. $sql .= " FROM " . MAIN_DB_PREFIX . "accounting_bookkeeping as ab";
  1596. $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "accounting_account as aa ON aa.account_number = ab.numero_compte";
  1597. $sql .= " AND aa.active = 1";
  1598. $sql .= " INNER JOIN " . MAIN_DB_PREFIX . "accounting_system as asy ON aa.fk_pcg_version = asy.pcg_version";
  1599. $sql .= " AND asy.rowid = " . $pcgver;
  1600. $sql .= " AND ab.entity IN (" . getEntity('accountancy') . ")";
  1601. $sql .= " ORDER BY account_number ASC";
  1602. */
  1603. }
  1604. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1605. /**
  1606. * Return list of accounts with label by chart of accounts
  1607. *
  1608. * @param string $selectid Preselected chart of accounts
  1609. * @param string $htmlname Name of field in html form
  1610. * @param int $showempty Add an empty field
  1611. * @param array $event Event options
  1612. * @param int $select_in Value is a aa.rowid (0 default) or aa.account_number (1)
  1613. * @param int $select_out Set value returned by select 0=rowid (default), 1=account_number
  1614. * @param int $aabase Set accounting_account base class to display empty=all or from 1 to 8 will display only account beginning by this number
  1615. * @return string String with HTML select
  1616. */
  1617. public function select_account($selectid, $htmlname = 'account', $showempty = 0, $event = array(), $select_in = 0, $select_out = 0, $aabase = '')
  1618. {
  1619. // phpcs:enable
  1620. global $conf;
  1621. require_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
  1622. $pcgver = $conf->global->CHARTOFACCOUNTS;
  1623. $sql = "SELECT DISTINCT ab.numero_compte as account_number, aa.label as label, aa.rowid as rowid, aa.fk_pcg_version";
  1624. $sql .= " FROM ".MAIN_DB_PREFIX."accounting_bookkeeping as ab";
  1625. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."accounting_account as aa ON aa.account_number = ab.numero_compte";
  1626. $sql .= " AND aa.active = 1";
  1627. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."accounting_system as asy ON aa.fk_pcg_version = asy.pcg_version";
  1628. $sql .= " AND asy.rowid = ".$pcgver;
  1629. $sql .= " AND ab.entity IN (".getEntity('accountancy').")";
  1630. $sql .= " ORDER BY account_number ASC";
  1631. dol_syslog(get_class($this)."::select_account", LOG_DEBUG);
  1632. $resql = $this->db->query($sql);
  1633. if (!$resql) {
  1634. $this->error = "Error ".$this->db->lasterror();
  1635. dol_syslog(get_class($this)."::select_account ".$this->error, LOG_ERR);
  1636. return -1;
  1637. }
  1638. $out = ajax_combobox($htmlname, $event);
  1639. $options = array();
  1640. $selected = null;
  1641. while ($obj = $this->db->fetch_object($resql)) {
  1642. $label = length_accountg($obj->account_number).' - '.$obj->label;
  1643. $select_value_in = $obj->rowid;
  1644. $select_value_out = $obj->rowid;
  1645. if ($select_in == 1) {
  1646. $select_value_in = $obj->account_number;
  1647. }
  1648. if ($select_out == 1) {
  1649. $select_value_out = $obj->account_number;
  1650. }
  1651. // Remember guy's we store in database llx_facturedet the rowid of accounting_account and not the account_number
  1652. // Because same account_number can be share between different accounting_system and do have the same meaning
  1653. if (($selectid != '') && $selectid == $select_value_in) {
  1654. $selected = $select_value_out;
  1655. }
  1656. $options[$select_value_out] = $label;
  1657. }
  1658. $out .= Form::selectarray($htmlname, $options, $selected, $showempty, 0, 0, '', 0, 0, 0, '', 'maxwidth300');
  1659. $this->db->free($resql);
  1660. return $out;
  1661. }
  1662. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1663. /**
  1664. * Description of a root accounting account
  1665. *
  1666. * @param string $account Accounting account
  1667. * @return string Root account
  1668. */
  1669. public function get_compte_racine($account = null)
  1670. {
  1671. // phpcs:enable
  1672. global $conf;
  1673. $pcgver = $conf->global->CHARTOFACCOUNTS;
  1674. $sql = "SELECT root.account_number, root.label as label";
  1675. $sql .= " FROM ".MAIN_DB_PREFIX."accounting_account as aa";
  1676. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."accounting_system as asy ON aa.fk_pcg_version = asy.pcg_version";
  1677. $sql .= " AND asy.rowid = ".$pcgver;
  1678. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."accounting_account as parent ON aa.account_parent = parent.rowid";
  1679. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."accounting_account as root ON parent.account_parent = root.rowid";
  1680. $sql .= " WHERE aa.account_number = '".$account."'";
  1681. $sql .= " AND parent.active = 1";
  1682. $sql .= " AND root.active = 1";
  1683. $sql .= " AND aa.entity IN (".getEntity('accountancy').")";
  1684. dol_syslog(get_class($this)."::select_account sql=".$sql, LOG_DEBUG);
  1685. $resql = $this->db->query($sql);
  1686. if ($resql) {
  1687. $obj = '';
  1688. if ($this->db->num_rows($resql)) {
  1689. $obj = $this->db->fetch_object($resql);
  1690. }
  1691. return $obj->label;
  1692. } else {
  1693. $this->error = "Error ".$this->db->lasterror();
  1694. dol_syslog(__METHOD__." ".$this->error, LOG_ERR);
  1695. return -1;
  1696. }
  1697. }
  1698. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1699. /**
  1700. * Description of accounting account
  1701. *
  1702. * @param string $account Accounting account
  1703. * @return string Account desc
  1704. */
  1705. public function get_compte_desc($account = null)
  1706. {
  1707. // phpcs:enable
  1708. global $conf;
  1709. $pcgver = $conf->global->CHARTOFACCOUNTS;
  1710. $sql = "SELECT aa.account_number, aa.label, aa.rowid, aa.fk_pcg_version, cat.label as category";
  1711. $sql .= " FROM ".MAIN_DB_PREFIX."accounting_account as aa ";
  1712. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."accounting_system as asy ON aa.fk_pcg_version = asy.pcg_version";
  1713. $sql .= " AND aa.account_number = '".$account."'";
  1714. $sql .= " AND asy.rowid = ".$pcgver;
  1715. $sql .= " AND aa.active = 1";
  1716. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_accounting_category as cat ON aa.fk_accounting_category = cat.rowid";
  1717. $sql .= " WHERE aa.entity IN (".getEntity('accountancy').")";
  1718. dol_syslog(get_class($this)."::select_account sql=".$sql, LOG_DEBUG);
  1719. $resql = $this->db->query($sql);
  1720. if ($resql) {
  1721. $obj = '';
  1722. if ($this->db->num_rows($resql)) {
  1723. $obj = $this->db->fetch_object($resql);
  1724. }
  1725. if (empty($obj->category)) {
  1726. return $obj->label;
  1727. } else {
  1728. return $obj->label.' ('.$obj->category.')';
  1729. }
  1730. } else {
  1731. $this->error = "Error ".$this->db->lasterror();
  1732. dol_syslog(__METHOD__." ".$this->error, LOG_ERR);
  1733. return -1;
  1734. }
  1735. }
  1736. }
  1737. /**
  1738. * Class BookKeepingLine
  1739. */
  1740. class BookKeepingLine
  1741. {
  1742. /**
  1743. * @var int ID
  1744. */
  1745. public $id;
  1746. public $doc_date = '';
  1747. public $doc_type;
  1748. public $doc_ref;
  1749. /**
  1750. * @var int ID
  1751. */
  1752. public $fk_doc;
  1753. /**
  1754. * @var int ID
  1755. */
  1756. public $fk_docdet;
  1757. public $thirdparty_code;
  1758. public $subledger_account;
  1759. public $subledger_label;
  1760. public $numero_compte;
  1761. public $label_compte;
  1762. public $label_operation;
  1763. public $debit;
  1764. public $credit;
  1765. public $montant;
  1766. public $sens;
  1767. public $lettering_code;
  1768. /**
  1769. * @var int ID
  1770. */
  1771. public $fk_user_author;
  1772. public $import_key;
  1773. public $code_journal;
  1774. public $journal_label;
  1775. public $piece_num;
  1776. /**
  1777. * @var integer|string date_creation
  1778. */
  1779. public $date_creation;
  1780. /**
  1781. * @var integer|string $date_modification;
  1782. */
  1783. public $date_modification;
  1784. /**
  1785. * @var integer|string $date_export;
  1786. */
  1787. public $date_export;
  1788. }