bookkeeping.class.php 62 KB

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