bookkeeping.class.php 63 KB

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