bookkeeping.class.php 68 KB

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