bookkeeping.class.php 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545
  1. <?php
  2. /* Copyright (C) 2014-2016 Olivier Geffroy <jeff@jeffinfo.com>
  3. * Copyright (C) 2015-2016 Alexandre Spangaro <aspangaro.dolibarr@gmail.com>
  4. * Copyright (C) 2015-2016 Florian Henry <florian.henry@open-concept.pro>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \file htdocs/accountancy/class/bookkeeping.class.php
  21. * \ingroup Advanced accountancy
  22. * \brief File of class to manage general ledger
  23. */
  24. // Class
  25. require_once DOL_DOCUMENT_ROOT . '/core/class/commonobject.class.php';
  26. /**
  27. * Class to manage general ledger
  28. */
  29. class BookKeeping extends CommonObject
  30. {
  31. /**
  32. *
  33. * @var string Error code (or message)
  34. * @deprecated
  35. *
  36. * @see Accountingbookkeeping::errors
  37. */
  38. public $error;
  39. /**
  40. *
  41. * @var string[] Error codes (or messages)
  42. */
  43. public $errors = array ();
  44. /**
  45. *
  46. * @var string Id to identify managed objects
  47. */
  48. public $element = 'accountingbookkeeping';
  49. /**
  50. *
  51. * @var string Name of table without prefix where object is stored
  52. */
  53. public $table_element = 'accounting_bookkeeping';
  54. public $entity = 1;
  55. /**
  56. *
  57. * @var BookKeepingLine[] Lines
  58. */
  59. public $lines = array ();
  60. /**
  61. *
  62. * @var int ID
  63. */
  64. public $id;
  65. /**
  66. */
  67. public $doc_date;
  68. public $doc_type;
  69. public $doc_ref;
  70. public $fk_doc;
  71. public $fk_docdet;
  72. public $code_tiers;
  73. public $numero_compte;
  74. public $label_compte;
  75. public $debit;
  76. public $credit;
  77. public $montant;
  78. public $sens;
  79. public $fk_user_author;
  80. public $import_key;
  81. public $code_journal;
  82. public $piece_num;
  83. /**
  84. */
  85. /**
  86. * Constructor
  87. *
  88. * @param DoliDb $db Database handler
  89. */
  90. public function __construct(DoliDB $db) {
  91. $this->db = $db;
  92. }
  93. /**
  94. * Create object into database
  95. *
  96. * @param User $user User that creates
  97. * @param bool $notrigger false=launch triggers after, true=disable triggers
  98. * @return int <0 if KO, Id of created object if OK
  99. */
  100. public function create(User $user, $notrigger = false) {
  101. global $conf, $langs;
  102. dol_syslog(__METHOD__, LOG_DEBUG);
  103. $error = 0;
  104. // Clean parameters
  105. if (isset($this->doc_type)) {
  106. $this->doc_type = trim($this->doc_type);
  107. }
  108. if (isset($this->doc_ref)) {
  109. $this->doc_ref = trim($this->doc_ref);
  110. }
  111. if (isset($this->fk_doc)) {
  112. $this->fk_doc = trim($this->fk_doc);
  113. }
  114. if (isset($this->fk_docdet)) {
  115. $this->fk_docdet = trim($this->fk_docdet);
  116. }
  117. if (isset($this->code_tiers)) {
  118. $this->code_tiers = trim($this->code_tiers);
  119. }
  120. if (isset($this->numero_compte)) {
  121. $this->numero_compte = trim($this->numero_compte);
  122. }
  123. if (isset($this->label_compte)) {
  124. $this->label_compte = trim($this->label_compte);
  125. }
  126. if (isset($this->debit)) {
  127. $this->debit = trim($this->debit);
  128. }
  129. if (isset($this->credit)) {
  130. $this->credit = trim($this->credit);
  131. }
  132. if (isset($this->montant)) {
  133. $this->montant = trim($this->montant);
  134. }
  135. if (isset($this->sens)) {
  136. $this->sens = trim($this->sens);
  137. }
  138. if (isset($this->fk_user_author)) {
  139. $this->fk_user_author = trim($this->fk_user_author);
  140. }
  141. if (isset($this->import_key)) {
  142. $this->import_key = trim($this->import_key);
  143. }
  144. if (isset($this->code_journal)) {
  145. $this->code_journal = trim($this->code_journal);
  146. }
  147. if (isset($this->piece_num)) {
  148. $this->piece_num = trim($this->piece_num);
  149. }
  150. if (empty($this->debit)) $this->debit = 0;
  151. if (empty($this->credit)) $this->credit = 0;
  152. // Check parameters
  153. if (empty($this->numero_compte) || $this->numero_compte == '-1')
  154. {
  155. $langs->load("errors");
  156. if (in_array($this->doc_type, array('bank', 'expense_report')))
  157. {
  158. $this->errors[]=$langs->trans('ErrorFieldAccountNotDefinedForBankLine', $this->fk_docdet, $this->doc_type);
  159. }
  160. else
  161. {
  162. $this->errors[]=$langs->trans('ErrorFieldAccountNotDefinedForInvoiceLine', $this->fk_doc, $this->doc_type);
  163. }
  164. return -1;
  165. }
  166. $this->db->begin();
  167. $this->piece_num = 0;
  168. // First check if line not yet already in bookkeeping
  169. $sql = "SELECT count(*) as nb";
  170. $sql .= " FROM " . MAIN_DB_PREFIX . $this->table_element;
  171. $sql .= " WHERE doc_type = '" . $this->db->escape($this->doc_type) . "'";
  172. $sql .= " AND fk_doc = " . $this->fk_doc;
  173. $sql .= " AND fk_docdet = " . $this->fk_docdet; // This field can be 0 is record is for several lines
  174. $sql .= " AND numero_compte = '" . $this->db->escape($this->numero_compte) . "'";
  175. $sql .= " AND entity IN (" . getEntity("accountancy", 1) . ")";
  176. $resql = $this->db->query($sql);
  177. if ($resql) {
  178. $row = $this->db->fetch_object($resql);
  179. if ($row->nb == 0)
  180. {
  181. // Determine piece_num
  182. $sqlnum = "SELECT piece_num";
  183. $sqlnum .= " FROM " . MAIN_DB_PREFIX . $this->table_element;
  184. $sqlnum .= " WHERE doc_type = '" . $this->db->escape($this->doc_type) . "'"; // For example doc_type = 'bank'
  185. $sqlnum .= " AND fk_docdet = " . $this->db->escape($this->fk_docdet); // fk_docdet is rowid into llx_bank or llx_facturedet or llx_facturefourndet, or ...
  186. $sqlnum .= " AND doc_ref = '" . $this->db->escape($this->doc_ref) . "'"; // ref of source object
  187. $sqlnum .= " AND entity IN (" . getEntity("accountancy", 1) . ")";
  188. dol_syslog(get_class($this) . ":: create sqlnum=" . $sqlnum, LOG_DEBUG);
  189. $resqlnum = $this->db->query($sqlnum);
  190. if ($resqlnum) {
  191. $objnum = $this->db->fetch_object($resqlnum);
  192. $this->piece_num = $objnum->piece_num;
  193. }
  194. dol_syslog(get_class($this) . ":: create this->piece_num=" . $this->piece_num, LOG_DEBUG);
  195. if (empty($this->piece_num)) {
  196. $sqlnum = "SELECT MAX(piece_num)+1 as maxpiecenum";
  197. $sqlnum .= " FROM " . MAIN_DB_PREFIX . $this->table_element;
  198. $sqlnum .= " WHERE entity IN (" . getEntity("accountancy", 1) . ")";
  199. dol_syslog(get_class($this) . ":: create sqlnum=" . $sqlnum, LOG_DEBUG);
  200. $resqlnum = $this->db->query($sqlnum);
  201. if ($resqlnum) {
  202. $objnum = $this->db->fetch_object($resqlnum);
  203. $this->piece_num = $objnum->maxpiecenum;
  204. }
  205. }
  206. dol_syslog(get_class($this) . ":: create this->piece_num=" . $this->piece_num, LOG_DEBUG);
  207. if (empty($this->piece_num)) {
  208. $this->piece_num = 1;
  209. }
  210. $now = dol_now();
  211. if (empty($this->date_create)) {
  212. $this->date_create = $now;
  213. }
  214. $sql = "INSERT INTO " . MAIN_DB_PREFIX . $this->table_element . " (";
  215. $sql .= "doc_date";
  216. $sql .= ", doc_type";
  217. $sql .= ", doc_ref";
  218. $sql .= ", fk_doc";
  219. $sql .= ", fk_docdet";
  220. $sql .= ", code_tiers";
  221. $sql .= ", numero_compte";
  222. $sql .= ", label_compte";
  223. $sql .= ", debit";
  224. $sql .= ", credit";
  225. $sql .= ", montant";
  226. $sql .= ", sens";
  227. $sql .= ", fk_user_author";
  228. $sql .= ", import_key";
  229. $sql .= ", code_journal";
  230. $sql .= ", piece_num";
  231. $sql .= ', entity';
  232. $sql .= ") VALUES (";
  233. $sql .= "'" . $this->db->idate($this->doc_date) . "'";
  234. $sql .= ",'" . $this->doc_type . "'";
  235. $sql .= ",'" . $this->doc_ref . "'";
  236. $sql .= "," . $this->fk_doc;
  237. $sql .= "," . $this->fk_docdet;
  238. $sql .= ",'" . $this->code_tiers . "'";
  239. $sql .= ",'" . $this->numero_compte . "'";
  240. $sql .= ",'" . $this->db->escape($this->label_compte) . "'";
  241. $sql .= "," . $this->debit;
  242. $sql .= "," . $this->credit;
  243. $sql .= "," . $this->montant;
  244. $sql .= ",'" . $this->sens . "'";
  245. $sql .= ",'" . $this->fk_user_author . "'";
  246. $sql .= ",'" . $this->db->idate($this->date_create). "'";
  247. $sql .= ",'" . $this->code_journal . "'";
  248. $sql .= "," . $this->piece_num;
  249. $sql .= ", " . (! isset($this->entity) ? '1' : $this->entity);
  250. $sql .= ")";
  251. dol_syslog(get_class($this) . ":: create sql=" . $sql, LOG_DEBUG);
  252. $resql = $this->db->query($sql);
  253. if ($resql) {
  254. $id = $this->db->last_insert_id(MAIN_DB_PREFIX . $this->table_element);
  255. if ($id > 0) {
  256. $this->id = $id;
  257. $result = 0;
  258. } else {
  259. $result = -2;
  260. $error ++;
  261. $this->errors[] = 'Error Create Error ' . $result . ' lecture ID';
  262. dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
  263. }
  264. } else {
  265. $result = -1;
  266. $error ++;
  267. $this->errors[] = 'Error ' . $this->db->lasterror();
  268. dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
  269. }
  270. } else { // Already exists
  271. $result = -3;
  272. $error++;
  273. $this->error='BookkeepingRecordAlreadyExists';
  274. dol_syslog(__METHOD__ . ' ' . $this->error, LOG_WARNING);
  275. }
  276. } else {
  277. $result = -5;
  278. $error ++;
  279. $this->errors[] = 'Error ' . $this->db->lasterror();
  280. dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
  281. }
  282. if (! $error) {
  283. if (! $notrigger) {
  284. // Uncomment this and change MYOBJECT to your own tag if you
  285. // want this action to call a trigger.
  286. // // Call triggers
  287. // $result=$this->call_trigger('MYOBJECT_CREATE',$user);
  288. // if ($result < 0) $error++;
  289. // // End call triggers
  290. }
  291. }
  292. // Commit or rollback
  293. if ($error) {
  294. $this->db->rollback();
  295. return -1 * $error;
  296. } else {
  297. $this->db->commit();
  298. return $result;
  299. }
  300. }
  301. /**
  302. * Create object into database
  303. *
  304. * @param User $user User that creates
  305. * @param bool $notrigger false=launch triggers after, true=disable triggers
  306. * @return int <0 if KO, Id of created object if OK
  307. */
  308. public function createStd(User $user, $notrigger = false) {
  309. dol_syslog(__METHOD__, LOG_DEBUG);
  310. $error = 0;
  311. // Clean parameters
  312. if (isset($this->doc_type)) {
  313. $this->doc_type = trim($this->doc_type);
  314. }
  315. if (isset($this->doc_ref)) {
  316. $this->doc_ref = trim($this->doc_ref);
  317. }
  318. if (isset($this->fk_doc)) {
  319. $this->fk_doc = trim($this->fk_doc);
  320. }
  321. if (isset($this->fk_docdet)) {
  322. $this->fk_docdet = trim($this->fk_docdet);
  323. }
  324. if (isset($this->code_tiers)) {
  325. $this->code_tiers = trim($this->code_tiers);
  326. }
  327. if (isset($this->numero_compte)) {
  328. $this->numero_compte = trim($this->numero_compte);
  329. }
  330. if (isset($this->label_compte)) {
  331. $this->label_compte = trim($this->label_compte);
  332. }
  333. if (isset($this->debit)) {
  334. $this->debit = trim($this->debit);
  335. }
  336. if (isset($this->credit)) {
  337. $this->credit = trim($this->credit);
  338. }
  339. if (isset($this->montant)) {
  340. $this->montant = trim($this->montant);
  341. }
  342. if (isset($this->sens)) {
  343. $this->sens = trim($this->sens);
  344. }
  345. if (isset($this->fk_user_author)) {
  346. $this->fk_user_author = trim($this->fk_user_author);
  347. }
  348. if (isset($this->import_key)) {
  349. $this->import_key = trim($this->import_key);
  350. }
  351. if (isset($this->code_journal)) {
  352. $this->code_journal = trim($this->code_journal);
  353. }
  354. if (isset($this->piece_num)) {
  355. $this->piece_num = trim($this->piece_num);
  356. }
  357. if (empty($this->debit)) $this->debit = 0;
  358. if (empty($this->credit)) $this->credit = 0;
  359. // Check parameters
  360. // Put here code to add control on parameters values
  361. // Insert request
  362. $sql = 'INSERT INTO ' . MAIN_DB_PREFIX . $this->table_element . '(';
  363. $sql .= 'doc_date,';
  364. $sql .= 'doc_type,';
  365. $sql .= 'doc_ref,';
  366. $sql .= 'fk_doc,';
  367. $sql .= 'fk_docdet,';
  368. $sql .= 'code_tiers,';
  369. $sql .= 'numero_compte,';
  370. $sql .= 'label_compte,';
  371. $sql .= 'debit,';
  372. $sql .= 'credit,';
  373. $sql .= 'montant,';
  374. $sql .= 'sens,';
  375. $sql .= 'fk_user_author,';
  376. $sql .= 'import_key,';
  377. $sql .= 'code_journal,';
  378. $sql .= 'piece_num,';
  379. $sql .= 'entity';
  380. $sql .= ') VALUES (';
  381. $sql .= ' ' . (! isset($this->doc_date) || dol_strlen($this->doc_date) == 0 ? 'NULL' : "'" . $this->db->idate($this->doc_date) . "'") . ',';
  382. $sql .= ' ' . (! isset($this->doc_type) ? 'NULL' : "'" . $this->db->escape($this->doc_type) . "'") . ',';
  383. $sql .= ' ' . (! isset($this->doc_ref) ? 'NULL' : "'" . $this->db->escape($this->doc_ref) . "'") . ',';
  384. $sql .= ' ' . (empty($this->fk_doc) ? '0' : $this->fk_doc) . ',';
  385. $sql .= ' ' . (empty($this->fk_docdet) ? '0' : $this->fk_docdet) . ',';
  386. $sql .= ' ' . (! isset($this->code_tiers) ? 'NULL' : "'" . $this->db->escape($this->code_tiers) . "'") . ',';
  387. $sql .= ' ' . (! isset($this->numero_compte) ? "'NotDefined'" : "'" . $this->db->escape($this->numero_compte) . "'") . ',';
  388. $sql .= ' ' . (! isset($this->label_compte) ? 'NULL' : "'" . $this->db->escape($this->label_compte) . "'") . ',';
  389. $sql .= ' ' . (! isset($this->debit) ? 'NULL' : $this->debit ). ',';
  390. $sql .= ' ' . (! isset($this->credit) ? 'NULL' : $this->credit ). ',';
  391. $sql .= ' ' . (! isset($this->montant) ? 'NULL' : $this->montant ). ',';
  392. $sql .= ' ' . (! isset($this->sens) ? 'NULL' : "'" . $this->db->escape($this->sens) . "'") . ',';
  393. $sql .= ' ' . $user->id . ',';
  394. $sql .= ' ' . (! isset($this->import_key) ? 'NULL' : "'" . $this->db->escape($this->import_key) . "'") . ',';
  395. $sql .= ' ' . (empty($this->code_journal) ? 'NULL' : "'" . $this->db->escape($this->code_journal) . "'") . ',';
  396. $sql .= ' ' . (empty($this->piece_num) ? 'NULL' : $this->piece_num).',';
  397. $sql .= ' ' . (! isset($this->entity) ? '1' : $this->entity);
  398. $sql .= ')';
  399. $this->db->begin();
  400. $resql = $this->db->query($sql);
  401. if (! $resql) {
  402. $error ++;
  403. $this->errors[] = 'Error ' . $this->db->lasterror();
  404. dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
  405. }
  406. if (! $error) {
  407. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . $this->table_element);
  408. if (! $notrigger) {
  409. // Uncomment this and change MYOBJECT to your own tag if you
  410. // want this action to call a trigger.
  411. // // Call triggers
  412. // $result=$this->call_trigger('MYOBJECT_CREATE',$user);
  413. // if ($result < 0) $error++;
  414. // // End call triggers
  415. }
  416. }
  417. // Commit or rollback
  418. if ($error) {
  419. $this->db->rollback();
  420. return - 1 * $error;
  421. } else {
  422. $this->db->commit();
  423. return $this->id;
  424. }
  425. }
  426. /**
  427. * Load object in memory from the database
  428. *
  429. * @param int $id Id object
  430. * @param string $ref Ref
  431. *
  432. * @return int <0 if KO, 0 if not found, >0 if OK
  433. */
  434. public function fetch($id, $ref = null) {
  435. global $conf;
  436. dol_syslog(__METHOD__, LOG_DEBUG);
  437. $sql = 'SELECT';
  438. $sql .= ' t.rowid,';
  439. $sql .= " t.doc_date,";
  440. $sql .= " t.doc_type,";
  441. $sql .= " t.doc_ref,";
  442. $sql .= " t.fk_doc,";
  443. $sql .= " t.fk_docdet,";
  444. $sql .= " t.code_tiers,";
  445. $sql .= " t.numero_compte,";
  446. $sql .= " t.label_compte,";
  447. $sql .= " t.debit,";
  448. $sql .= " t.credit,";
  449. $sql .= " t.montant,";
  450. $sql .= " t.sens,";
  451. $sql .= " t.fk_user_author,";
  452. $sql .= " t.import_key,";
  453. $sql .= " t.code_journal,";
  454. $sql .= " t.piece_num";
  455. $sql .= ' FROM ' . MAIN_DB_PREFIX . $this->table_element . ' as t';
  456. $sql .= ' WHERE 1 = 1';
  457. $sql .= " AND entity IN (" . getEntity("accountancy", 1) . ")";
  458. if (null !== $ref) {
  459. $sql .= ' AND t.ref = ' . '\'' . $ref . '\'';
  460. } else {
  461. $sql .= ' AND t.rowid = ' . $id;
  462. }
  463. $resql = $this->db->query($sql);
  464. if ($resql) {
  465. $numrows = $this->db->num_rows($resql);
  466. if ($numrows) {
  467. $obj = $this->db->fetch_object($resql);
  468. $this->id = $obj->rowid;
  469. $this->doc_date = $this->db->jdate($obj->doc_date);
  470. $this->doc_type = $obj->doc_type;
  471. $this->doc_ref = $obj->doc_ref;
  472. $this->fk_doc = $obj->fk_doc;
  473. $this->fk_docdet = $obj->fk_docdet;
  474. $this->code_tiers = $obj->code_tiers;
  475. $this->numero_compte = $obj->numero_compte;
  476. $this->label_compte = $obj->label_compte;
  477. $this->debit = $obj->debit;
  478. $this->credit = $obj->credit;
  479. $this->montant = $obj->montant;
  480. $this->sens = $obj->sens;
  481. $this->fk_user_author = $obj->fk_user_author;
  482. $this->import_key = $obj->import_key;
  483. $this->code_journal = $obj->code_journal;
  484. $this->piece_num = $obj->piece_num;
  485. }
  486. $this->db->free($resql);
  487. if ($numrows) {
  488. return 1;
  489. } else {
  490. return 0;
  491. }
  492. } else {
  493. $this->errors[] = 'Error ' . $this->db->lasterror();
  494. dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
  495. return - 1;
  496. }
  497. }
  498. /**
  499. * Load object in memory from the database
  500. *
  501. * @param string $sortorder Sort Order
  502. * @param string $sortfield Sort field
  503. * @param int $limit offset limit
  504. * @param int $offset offset limit
  505. * @param array $filter filter array
  506. * @param string $filtermode filter mode (AND or OR)
  507. *
  508. * @return int <0 if KO, >0 if OK
  509. */
  510. public function fetchAllByAccount($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, array $filter = array(), $filtermode = 'AND') {
  511. global $conf;
  512. dol_syslog(__METHOD__, LOG_DEBUG);
  513. $sql = 'SELECT';
  514. $sql .= ' t.rowid,';
  515. $sql .= " t.doc_date,";
  516. $sql .= " t.doc_type,";
  517. $sql .= " t.doc_ref,";
  518. $sql .= " t.fk_doc,";
  519. $sql .= " t.fk_docdet,";
  520. $sql .= " t.code_tiers,";
  521. $sql .= " t.numero_compte,";
  522. $sql .= " t.label_compte,";
  523. $sql .= " t.debit,";
  524. $sql .= " t.credit,";
  525. $sql .= " t.montant,";
  526. $sql .= " t.sens,";
  527. $sql .= " t.fk_user_author,";
  528. $sql .= " t.import_key,";
  529. $sql .= " t.code_journal,";
  530. $sql .= " t.piece_num";
  531. // Manage filter
  532. $sqlwhere = array ();
  533. if (count($filter) > 0) {
  534. foreach ( $filter as $key => $value ) {
  535. if ($key == 't.doc_date') {
  536. $sqlwhere[] = $key . '=\'' . $this->db->idate($value) . '\'';
  537. } elseif ($key == 't.doc_date>=' || $key == 't.doc_date<=') {
  538. $sqlwhere[] = $key . '\'' . $this->db->idate($value) . '\'';
  539. } elseif ($key == 't.numero_compte>=' || $key == 't.numero_compte<=' || $key == 't.code_tiers>=' || $key == 't.code_tiers<=') {
  540. $sqlwhere[] = $key . '\'' . $this->db->escape($value) . '\'';
  541. } elseif ($key == 't.fk_doc' || $key == 't.fk_docdet' || $key == 't.piece_num') {
  542. $sqlwhere[] = $key . '=' . $value;
  543. } elseif ($key == 't.code_tiers' || $key == 't.numero_compte') {
  544. $sqlwhere[] = $key . ' LIKE \'' . $this->db->escape($value) . '%\'';
  545. } elseif ($key == 't.label_compte') {
  546. $sqlwhere[] = $key . ' LIKE \'' . $this->db->escape($value) . '%\'';
  547. }else {
  548. $sqlwhere[] = $key . ' LIKE \'%' . $this->db->escape($value) . '%\'';
  549. }
  550. }
  551. }
  552. $sql .= ' FROM ' . MAIN_DB_PREFIX . $this->table_element . ' as t';
  553. $sql .= ' WHERE 1 = 1';
  554. $sql .= " AND entity IN (" . getEntity("accountancy", 1) . ")";
  555. if (count($sqlwhere) > 0) {
  556. $sql .= ' AND ' . implode(' ' . $filtermode . ' ', $sqlwhere);
  557. }
  558. // Affichage par compte comptable
  559. $sql .= ' ORDER BY t.numero_compte ASC';
  560. if (! empty($sortfield)) {
  561. $sql .= ', ' . $sortfield . ' ' .$sortorder;
  562. }
  563. if (! empty($limit)) {
  564. $sql .= ' ' . $this->db->plimit($limit + 1, $offset);
  565. }
  566. $this->lines = array ();
  567. $resql = $this->db->query($sql);
  568. if ($resql) {
  569. $num = $this->db->num_rows($resql);
  570. while ( $obj = $this->db->fetch_object($resql) ) {
  571. $line = new BookKeepingLine();
  572. $line->id = $obj->rowid;
  573. $line->doc_date = $this->db->jdate($obj->doc_date);
  574. $line->doc_type = $obj->doc_type;
  575. $line->doc_ref = $obj->doc_ref;
  576. $line->fk_doc = $obj->fk_doc;
  577. $line->fk_docdet = $obj->fk_docdet;
  578. $line->code_tiers = $obj->code_tiers;
  579. $line->numero_compte = $obj->numero_compte;
  580. $line->label_compte = $obj->label_compte;
  581. $line->debit = $obj->debit;
  582. $line->credit = $obj->credit;
  583. $line->montant = $obj->montant;
  584. $line->sens = $obj->sens;
  585. $line->fk_user_author = $obj->fk_user_author;
  586. $line->import_key = $obj->import_key;
  587. $line->code_journal = $obj->code_journal;
  588. $line->piece_num = $obj->piece_num;
  589. $this->lines[] = $line;
  590. }
  591. $this->db->free($resql);
  592. return $num;
  593. } else {
  594. $this->errors[] = 'Error ' . $this->db->lasterror();
  595. dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
  596. return - 1;
  597. }
  598. }
  599. /**
  600. * Load object in memory from the database
  601. *
  602. * @param string $sortorder Sort Order
  603. * @param string $sortfield Sort field
  604. * @param int $limit offset limit
  605. * @param int $offset offset limit
  606. * @param array $filter filter array
  607. * @param string $filtermode filter mode (AND or OR)
  608. *
  609. * @return int <0 if KO, >0 if OK
  610. */
  611. public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, array $filter = array(), $filtermode = 'AND') {
  612. global $conf;
  613. dol_syslog(__METHOD__, LOG_DEBUG);
  614. $sql = 'SELECT';
  615. $sql .= ' t.rowid,';
  616. $sql .= " t.doc_date,";
  617. $sql .= " t.doc_type,";
  618. $sql .= " t.doc_ref,";
  619. $sql .= " t.fk_doc,";
  620. $sql .= " t.fk_docdet,";
  621. $sql .= " t.code_tiers,";
  622. $sql .= " t.numero_compte,";
  623. $sql .= " t.label_compte,";
  624. $sql .= " t.debit,";
  625. $sql .= " t.credit,";
  626. $sql .= " t.montant,";
  627. $sql .= " t.sens,";
  628. $sql .= " t.fk_user_author,";
  629. $sql .= " t.import_key,";
  630. $sql .= " t.code_journal,";
  631. $sql .= " t.piece_num";
  632. $sql .= ' FROM ' . MAIN_DB_PREFIX . $this->table_element . ' as t';
  633. // Manage filter
  634. $sqlwhere = array ();
  635. if (count($filter) > 0) {
  636. foreach ( $filter as $key => $value ) {
  637. if ($key == 't.doc_date') {
  638. $sqlwhere[] = $key . '=\'' . $this->db->idate($value) . '\'';
  639. } elseif ($key == 't.doc_date>=' || $key == 't.doc_date<=') {
  640. $sqlwhere[] = $key . '\'' . $this->db->idate($value) . '\'';
  641. } elseif ($key == 't.numero_compte>=' || $key == 't.numero_compte<=' || $key == 't.code_tiers>=' || $key == 't.code_tiers<=') {
  642. $sqlwhere[] = $key . '\'' . $this->db->escape($value) . '\'';
  643. } elseif ($key == 't.fk_doc' || $key == 't.fk_docdet' || $key == 't.piece_num') {
  644. $sqlwhere[] = $key . '=' . $value;
  645. } elseif ($key == 't.code_tiers' || $key == 't.numero_compte') {
  646. $sqlwhere[] = $key . ' LIKE \'' . $this->db->escape($value) . '%\'';
  647. } else {
  648. $sqlwhere[] = $key . ' LIKE \'%' . $this->db->escape($value) . '%\'';
  649. }
  650. }
  651. }
  652. $sql.= ' WHERE 1 = 1';
  653. $sql .= " AND entity IN (" . getEntity("accountancy", 1) . ")";
  654. if (count($sqlwhere) > 0) {
  655. $sql .= ' AND ' . implode(' ' . $filtermode . ' ', $sqlwhere);
  656. }
  657. if (! empty($sortfield)) {
  658. $sql .= $this->db->order($sortfield, $sortorder);
  659. }
  660. if (! empty($limit)) {
  661. $sql .= ' ' . $this->db->plimit($limit + 1, $offset);
  662. }
  663. $this->lines = array ();
  664. $resql = $this->db->query($sql);
  665. if ($resql) {
  666. $num = $this->db->num_rows($resql);
  667. while ( $obj = $this->db->fetch_object($resql) ) {
  668. $line = new BookKeepingLine();
  669. $line->id = $obj->rowid;
  670. $line->doc_date = $this->db->jdate($obj->doc_date);
  671. $line->doc_type = $obj->doc_type;
  672. $line->doc_ref = $obj->doc_ref;
  673. $line->fk_doc = $obj->fk_doc;
  674. $line->fk_docdet = $obj->fk_docdet;
  675. $line->code_tiers = $obj->code_tiers;
  676. $line->numero_compte = $obj->numero_compte;
  677. $line->label_compte = $obj->label_compte;
  678. $line->debit = $obj->debit;
  679. $line->credit = $obj->credit;
  680. $line->montant = $obj->montant;
  681. $line->sens = $obj->sens;
  682. $line->fk_user_author = $obj->fk_user_author;
  683. $line->import_key = $obj->import_key;
  684. $line->code_journal = $obj->code_journal;
  685. $line->piece_num = $obj->piece_num;
  686. $this->lines[] = $line;
  687. }
  688. $this->db->free($resql);
  689. return $num;
  690. } else {
  691. $this->errors[] = 'Error ' . $this->db->lasterror();
  692. dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
  693. return - 1;
  694. }
  695. }
  696. /**
  697. * Load object in memory from the database
  698. *
  699. * @param string $sortorder Sort Order
  700. * @param string $sortfield Sort field
  701. * @param int $limit offset limit
  702. * @param int $offset offset limit
  703. * @param array $filter filter array
  704. * @param string $filtermode filter mode (AND or OR)
  705. *
  706. * @return int <0 if KO, >0 if OK
  707. */
  708. public function fetchAllBalance($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, array $filter = array(), $filtermode = 'AND') {
  709. global $conf;
  710. dol_syslog(__METHOD__, LOG_DEBUG);
  711. $sql = 'SELECT';
  712. $sql .= " t.numero_compte,";
  713. $sql .= " SUM(t.debit) as debit,";
  714. $sql .= " SUM(t.credit) as credit";
  715. $sql .= ' FROM ' . MAIN_DB_PREFIX . $this->table_element . ' as t';
  716. // Manage filter
  717. $sqlwhere = array ();
  718. if (count($filter) > 0) {
  719. foreach ( $filter as $key => $value ) {
  720. if ($key == 't.doc_date') {
  721. $sqlwhere[] = $key . '=\'' . $this->db->idate($value) . '\'';
  722. } elseif ($key == 't.doc_date>=' || $key == 't.doc_date<=') {
  723. $sqlwhere[] = $key . '\'' . $this->db->idate($value) . '\'';
  724. } elseif ($key == 't.numero_compte>=' || $key == 't.numero_compte<=' || $key == 't.code_tiers>=' || $key == 't.code_tiers<=') {
  725. $sqlwhere[] = $key . '\'' . $this->db->escape($value) . '\'';
  726. } elseif ($key == 't.fk_doc' || $key == 't.fk_docdet' || $key == 't.piece_num') {
  727. $sqlwhere[] = $key . '=' . $value;
  728. } elseif ($key == 't.code_tiers' || $key == 't.numero_compte') {
  729. $sqlwhere[] = $key . ' LIKE \'' . $this->db->escape($value) . '%\'';
  730. } else {
  731. $sqlwhere[] = $key . ' LIKE \'%' . $this->db->escape($value) . '%\'';
  732. }
  733. }
  734. }
  735. $sql.= ' WHERE 1 = 1';
  736. $sql .= " AND entity IN (" . getEntity("accountancy", 1) . ")";
  737. if (count($sqlwhere) > 0) {
  738. $sql .= ' AND ' . implode(' ' . $filtermode . ' ', $sqlwhere);
  739. }
  740. $sql .= ' GROUP BY t.numero_compte';
  741. if (! empty($sortfield)) {
  742. $sql .= $this->db->order($sortfield, $sortorder);
  743. }
  744. if (! empty($limit)) {
  745. $sql .= ' ' . $this->db->plimit($limit + 1, $offset);
  746. }
  747. $this->lines = array ();
  748. $resql = $this->db->query($sql);
  749. if ($resql) {
  750. $num = $this->db->num_rows($resql);
  751. while ( $obj = $this->db->fetch_object($resql) ) {
  752. $line = new BookKeepingLine();
  753. $line->numero_compte = $obj->numero_compte;
  754. $line->debit = $obj->debit;
  755. $line->credit = $obj->credit;
  756. $this->lines[] = $line;
  757. }
  758. $this->db->free($resql);
  759. return $num;
  760. } else {
  761. $this->errors[] = 'Error ' . $this->db->lasterror();
  762. dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
  763. return - 1;
  764. }
  765. }
  766. /**
  767. * Update object into database
  768. *
  769. * @param User $user User that modifies
  770. * @param bool $notrigger false=launch triggers after, true=disable triggers
  771. *
  772. * @return int <0 if KO, >0 if OK
  773. */
  774. public function update(User $user, $notrigger = false) {
  775. $error = 0;
  776. dol_syslog(__METHOD__, LOG_DEBUG);
  777. // Clean parameters
  778. if (isset($this->doc_type)) {
  779. $this->doc_type = trim($this->doc_type);
  780. }
  781. if (isset($this->doc_ref)) {
  782. $this->doc_ref = trim($this->doc_ref);
  783. }
  784. if (isset($this->fk_doc)) {
  785. $this->fk_doc = trim($this->fk_doc);
  786. }
  787. if (isset($this->fk_docdet)) {
  788. $this->fk_docdet = trim($this->fk_docdet);
  789. }
  790. if (isset($this->code_tiers)) {
  791. $this->code_tiers = trim($this->code_tiers);
  792. }
  793. if (isset($this->numero_compte)) {
  794. $this->numero_compte = trim($this->numero_compte);
  795. }
  796. if (isset($this->label_compte)) {
  797. $this->label_compte = trim($this->label_compte);
  798. }
  799. if (isset($this->debit)) {
  800. $this->debit = trim($this->debit);
  801. }
  802. if (isset($this->credit)) {
  803. $this->credit = trim($this->credit);
  804. }
  805. if (isset($this->montant)) {
  806. $this->montant = trim($this->montant);
  807. }
  808. if (isset($this->sens)) {
  809. $this->sens = trim($this->sens);
  810. }
  811. if (isset($this->fk_user_author)) {
  812. $this->fk_user_author = trim($this->fk_user_author);
  813. }
  814. if (isset($this->import_key)) {
  815. $this->import_key = trim($this->import_key);
  816. }
  817. if (isset($this->code_journal)) {
  818. $this->code_journal = trim($this->code_journal);
  819. }
  820. if (isset($this->piece_num)) {
  821. $this->piece_num = trim($this->piece_num);
  822. }
  823. // Check parameters
  824. // Put here code to add a control on parameters values
  825. // Update request
  826. $sql = 'UPDATE ' . MAIN_DB_PREFIX . $this->table_element . ' SET';
  827. $sql .= ' doc_date = ' . (! isset($this->doc_date) || dol_strlen($this->doc_date) != 0 ? "'" . $this->db->idate($this->doc_date) . "'" : 'null') . ',';
  828. $sql .= ' doc_type = ' . (isset($this->doc_type) ? "'" . $this->db->escape($this->doc_type) . "'" : "null") . ',';
  829. $sql .= ' doc_ref = ' . (isset($this->doc_ref) ? "'" . $this->db->escape($this->doc_ref) . "'" : "null") . ',';
  830. $sql .= ' fk_doc = ' . (isset($this->fk_doc) ? $this->fk_doc : "null") . ',';
  831. $sql .= ' fk_docdet = ' . (isset($this->fk_docdet) ? $this->fk_docdet : "null") . ',';
  832. $sql .= ' code_tiers = ' . (isset($this->code_tiers) ? "'" . $this->db->escape($this->code_tiers) . "'" : "null") . ',';
  833. $sql .= ' numero_compte = ' . (isset($this->numero_compte) ? "'" . $this->db->escape($this->numero_compte) . "'" : "null") . ',';
  834. $sql .= ' label_compte = ' . (isset($this->label_compte) ? "'" . $this->db->escape($this->label_compte) . "'" : "null") . ',';
  835. $sql .= ' debit = ' . (isset($this->debit) ? $this->debit : "null") . ',';
  836. $sql .= ' credit = ' . (isset($this->credit) ? $this->credit : "null") . ',';
  837. $sql .= ' montant = ' . (isset($this->montant) ? $this->montant : "null") . ',';
  838. $sql .= ' sens = ' . (isset($this->sens) ? "'" . $this->db->escape($this->sens) . "'" : "null") . ',';
  839. $sql .= ' fk_user_author = ' . (isset($this->fk_user_author) ? $this->fk_user_author : "null") . ',';
  840. $sql .= ' import_key = ' . (isset($this->import_key) ? "'" . $this->db->escape($this->import_key) . "'" : "null") . ',';
  841. $sql .= ' code_journal = ' . (isset($this->code_journal) ? "'" . $this->db->escape($this->code_journal) . "'" : "null") . ',';
  842. $sql .= ' piece_num = ' . (isset($this->piece_num) ? $this->piece_num : "null");
  843. $sql .= ' WHERE rowid=' . $this->id;
  844. $this->db->begin();
  845. $resql = $this->db->query($sql);
  846. if (! $resql) {
  847. $error ++;
  848. $this->errors[] = 'Error ' . $this->db->lasterror();
  849. dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
  850. }
  851. if (! $error && ! $notrigger) {
  852. // Uncomment this and change MYOBJECT to your own tag if you
  853. // want this action calls a trigger.
  854. // // Call triggers
  855. // $result=$this->call_trigger('MYOBJECT_MODIFY',$user);
  856. // if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
  857. // // End call triggers
  858. }
  859. // Commit or rollback
  860. if ($error) {
  861. $this->db->rollback();
  862. return - 1 * $error;
  863. } else {
  864. $this->db->commit();
  865. return 1;
  866. }
  867. }
  868. /**
  869. * Delete object in database
  870. *
  871. * @param User $user User that deletes
  872. * @param bool $notrigger false=launch triggers after, true=disable triggers
  873. *
  874. * @return int <0 if KO, >0 if OK
  875. */
  876. public function delete(User $user, $notrigger = false) {
  877. dol_syslog(__METHOD__, LOG_DEBUG);
  878. $error = 0;
  879. $this->db->begin();
  880. if (! $error) {
  881. if (! $notrigger) {
  882. // Uncomment this and change MYOBJECT to your own tag if you
  883. // want this action calls a trigger.
  884. // // Call triggers
  885. // $result=$this->call_trigger('MYOBJECT_DELETE',$user);
  886. // if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
  887. // // End call triggers
  888. }
  889. }
  890. if (! $error) {
  891. $sql = 'DELETE FROM ' . MAIN_DB_PREFIX . $this->table_element;
  892. $sql .= ' WHERE rowid=' . $this->id;
  893. $resql = $this->db->query($sql);
  894. if (! $resql) {
  895. $error ++;
  896. $this->errors[] = 'Error ' . $this->db->lasterror();
  897. dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
  898. }
  899. }
  900. // Commit or rollback
  901. if ($error) {
  902. $this->db->rollback();
  903. return - 1 * $error;
  904. } else {
  905. $this->db->commit();
  906. return 1;
  907. }
  908. }
  909. /**
  910. * Delete bookkepping by importkey
  911. *
  912. * @param string $importkey Import key
  913. * @return int Result
  914. */
  915. function deleteByImportkey($importkey) {
  916. $this->db->begin();
  917. // first check if line not yet in bookkeeping
  918. $sql = "DELETE";
  919. $sql .= " FROM " . MAIN_DB_PREFIX . $this->table_element;
  920. $sql .= " WHERE import_key = '" . $importkey . "'";
  921. $resql = $this->db->query($sql);
  922. if (! $resql) {
  923. $this->errors[] = "Error " . $this->db->lasterror();
  924. dol_syslog(get_class($this)."::delete Error " . $this->db->lasterror(), LOG_ERR);
  925. $this->db->rollback();
  926. return - 1;
  927. }
  928. $this->db->commit();
  929. return 1;
  930. }
  931. /**
  932. * Delete bookkepping by year
  933. *
  934. * @param string $delyear Year to delete
  935. * @param string $journal Journal to delete
  936. * @return int <0 if KO, >0 if OK
  937. */
  938. function deleteByYearAndJournal($delyear='', $journal='') {
  939. global $conf;
  940. if (empty($delyear) && empty($journal))
  941. {
  942. return -1;
  943. }
  944. $this->db->begin();
  945. // first check if line not yet in bookkeeping
  946. $sql = "DELETE";
  947. $sql.= " FROM " . MAIN_DB_PREFIX . $this->table_element;
  948. $sql.= " WHERE 1 = 1";
  949. if (! empty($delyear)) $sql.= " AND YEAR(doc_date) = " . $delyear; // FIXME Must use between
  950. if (! empty($journal)) $sql.= " AND code_journal = '".$journal."'";
  951. $sql .= " AND entity IN (" . getEntity("accountancy", 1) . ")";
  952. $resql = $this->db->query($sql);
  953. if (! $resql) {
  954. $this->errors[] = "Error " . $this->db->lasterror();
  955. foreach ( $this->errors as $errmsg ) {
  956. dol_syslog(get_class($this) . "::delete " . $errmsg, LOG_ERR);
  957. $this->error .= ($this->error ? ', ' . $errmsg : $errmsg);
  958. }
  959. $this->db->rollback();
  960. return -1;
  961. }
  962. $this->db->commit();
  963. return 1;
  964. }
  965. /**
  966. * Delete bookkepping by piece number
  967. *
  968. * @param int $piecenum peicenum to delete
  969. * @return int Result
  970. */
  971. function deleteMvtNum($piecenum) {
  972. global $conf;
  973. $this->db->begin();
  974. // first check if line not yet in bookkeeping
  975. $sql = "DELETE";
  976. $sql .= " FROM " . MAIN_DB_PREFIX . $this->table_element;
  977. $sql .= " WHERE piece_num = " . $piecenum;
  978. $sql .= " AND entity IN (" . getEntity("accountancy", 1) . ")";
  979. $resql = $this->db->query($sql);
  980. if (! $resql) {
  981. $this->errors[] = "Error " . $this->db->lasterror();
  982. foreach ( $this->errors as $errmsg ) {
  983. dol_syslog(get_class($this) . "::delete " . $errmsg, LOG_ERR);
  984. $this->error .= ($this->error ? ', ' . $errmsg : $errmsg);
  985. }
  986. $this->db->rollback();
  987. return - 1;
  988. }
  989. $this->db->commit();
  990. return 1;
  991. }
  992. /**
  993. * Load an object from its id and create a new one in database
  994. *
  995. * @param int $fromid Id of object to clone
  996. *
  997. * @return int New id of clone
  998. */
  999. public function createFromClone($fromid) {
  1000. dol_syslog(__METHOD__, LOG_DEBUG);
  1001. global $user;
  1002. $error = 0;
  1003. $object = new Accountingbookkeeping($this->db);
  1004. $this->db->begin();
  1005. // Load source object
  1006. $object->fetch($fromid);
  1007. // Reset object
  1008. $object->id = 0;
  1009. // Clear fields
  1010. // ...
  1011. // Create clone
  1012. $result = $object->create($user);
  1013. // Other options
  1014. if ($result < 0) {
  1015. $error ++;
  1016. $this->errors = $object->errors;
  1017. dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
  1018. }
  1019. // End
  1020. if (! $error) {
  1021. $this->db->commit();
  1022. return $object->id;
  1023. } else {
  1024. $this->db->rollback();
  1025. return - 1;
  1026. }
  1027. }
  1028. /**
  1029. * Initialise object with example values
  1030. * Id must be 0 if object instance is a specimen
  1031. *
  1032. * @return void
  1033. */
  1034. public function initAsSpecimen() {
  1035. global $user;
  1036. $now=dol_now();
  1037. $this->id = 0;
  1038. $this->doc_date = $now;
  1039. $this->doc_type = '';
  1040. $this->doc_ref = '';
  1041. $this->fk_doc = '';
  1042. $this->fk_docdet = '';
  1043. $this->code_tiers = '';
  1044. $this->numero_compte = '';
  1045. $this->label_compte = '';
  1046. $this->debit = 99.9;
  1047. $this->credit = '';
  1048. $this->montant = '';
  1049. $this->sens = '';
  1050. $this->fk_user_author = $user->id;
  1051. $this->import_key = '';
  1052. $this->code_journal = '';
  1053. $this->piece_num = '';
  1054. }
  1055. /**
  1056. * Load an accounting document into memory from database
  1057. *
  1058. * @param int $piecenum Accounting document to get
  1059. * @return int <0 if KO, >0 if OK
  1060. */
  1061. public function fetchPerMvt($piecenum) {
  1062. global $conf;
  1063. $sql = "SELECT piece_num,doc_date,code_journal,doc_ref,doc_type";
  1064. $sql .= " FROM " . MAIN_DB_PREFIX . $this->table_element;
  1065. $sql .= " WHERE piece_num = " . $piecenum;
  1066. $sql .= " AND entity IN (" . getEntity("accountancy", 1) . ")";
  1067. dol_syslog(get_class($this) . "::" . __METHOD__, LOG_DEBUG);
  1068. $result = $this->db->query($sql);
  1069. if ($result) {
  1070. $obj = $this->db->fetch_object($result);
  1071. $this->piece_num = $obj->piece_num;
  1072. $this->code_journal = $obj->code_journal;
  1073. $this->doc_date = $this->db->jdate($obj->doc_date);
  1074. $this->doc_ref = $obj->doc_ref;
  1075. $this->doc_type = $obj->doc_type;
  1076. } else {
  1077. $this->error = "Error " . $this->db->lasterror();
  1078. dol_syslog(get_class($this) . "::" . __METHOD__ . $this->error, LOG_ERR);
  1079. return - 1;
  1080. }
  1081. return 1;
  1082. }
  1083. /**
  1084. * Return next number movement
  1085. *
  1086. * @return string Next numero to use
  1087. */
  1088. public function getNextNumMvt()
  1089. {
  1090. global $conf;
  1091. $sql = "SELECT MAX(piece_num)+1 as max FROM " . MAIN_DB_PREFIX . $this->table_element;
  1092. $sql .= " WHERE entity IN (" . getEntity("accountancy", 1) . ")";
  1093. dol_syslog(get_class($this) . "getNextNumMvt sql=" . $sql, LOG_DEBUG);
  1094. $result = $this->db->query($sql);
  1095. if ($result) {
  1096. $obj = $this->db->fetch_object($result);
  1097. if ($obj) $result = $obj->max;
  1098. if (empty($result)) $result = 1;
  1099. return $result;
  1100. } else {
  1101. $this->error = "Error " . $this->db->lasterror();
  1102. dol_syslog(get_class($this) . "::getNextNumMvt " . $this->error, LOG_ERR);
  1103. return - 1;
  1104. }
  1105. }
  1106. /**
  1107. * Load all informations of accountancy document
  1108. *
  1109. * @param int $piecenum id of line to get
  1110. * @return int <0 if KO, >0 if OK
  1111. */
  1112. function fetch_all_per_mvt($piecenum) {
  1113. global $conf;
  1114. $sql = "SELECT rowid, doc_date, doc_type,";
  1115. $sql .= " doc_ref, fk_doc, fk_docdet, code_tiers,";
  1116. $sql .= " numero_compte, label_compte, debit, credit,";
  1117. $sql .= " montant, sens, fk_user_author, import_key, code_journal, piece_num";
  1118. $sql .= " FROM " . MAIN_DB_PREFIX . $this->table_element;
  1119. $sql .= " WHERE piece_num = " . $piecenum;
  1120. $sql .= " AND entity IN (" . getEntity("accountancy", 1) . ")";
  1121. dol_syslog(get_class($this) . "::" . __METHOD__, LOG_DEBUG);
  1122. $result = $this->db->query($sql);
  1123. if ($result) {
  1124. while ( $obj = $this->db->fetch_object($result) ) {
  1125. $line = new BookKeepingLine();
  1126. $line->id = $obj->rowid;
  1127. $line->doc_date = $this->db->jdate($obj->doc_date);
  1128. $line->doc_type = $obj->doc_type;
  1129. $line->doc_ref = $obj->doc_ref;
  1130. $line->fk_doc = $obj->fk_doc;
  1131. $line->fk_docdet = $obj->fk_docdet;
  1132. $line->code_tiers = $obj->code_tiers;
  1133. $line->numero_compte = $obj->numero_compte;
  1134. $line->label_compte = $obj->label_compte;
  1135. $line->debit = $obj->debit;
  1136. $line->credit = $obj->credit;
  1137. $line->montant = $obj->montant;
  1138. $line->sens = $obj->sens;
  1139. $line->code_journal = $obj->code_journal;
  1140. $line->piece_num = $obj->piece_num;
  1141. $this->linesmvt[] = $line;
  1142. }
  1143. } else {
  1144. $this->error = "Error " . $this->db->lasterror();
  1145. dol_syslog(get_class($this) . "::" . __METHOD__ . $this->error, LOG_ERR);
  1146. return - 1;
  1147. }
  1148. return 1;
  1149. }
  1150. /**
  1151. * Export bookkeping
  1152. *
  1153. * @param string $model Model
  1154. * @return int Result
  1155. */
  1156. function export_bookkeping($model = 'ebp') {
  1157. global $conf;
  1158. $sql = "SELECT rowid, doc_date, doc_type,";
  1159. $sql .= " doc_ref, fk_doc, fk_docdet, code_tiers,";
  1160. $sql .= " numero_compte, label_compte, debit, credit,";
  1161. $sql .= " montant, sens, fk_user_author, import_key, code_journal, piece_num";
  1162. $sql .= " FROM " . MAIN_DB_PREFIX . $this->table_element;
  1163. $sql .= " WHERE entity IN (" . getEntity("accountancy", 1) . ")";
  1164. dol_syslog(get_class($this) . "::export_bookkeping", LOG_DEBUG);
  1165. $resql = $this->db->query($sql);
  1166. if ($resql) {
  1167. $this->linesexport = array ();
  1168. $num = $this->db->num_rows($resql);
  1169. while ( $obj = $this->db->fetch_object($resql) ) {
  1170. $line = new BookKeepingLine();
  1171. $line->id = $obj->rowid;
  1172. $line->doc_date = $this->db->jdate($obj->doc_date);
  1173. $line->doc_type = $obj->doc_type;
  1174. $line->doc_ref = $obj->doc_ref;
  1175. $line->fk_doc = $obj->fk_doc;
  1176. $line->fk_docdet = $obj->fk_docdet;
  1177. $line->code_tiers = $obj->code_tiers;
  1178. $line->numero_compte = $obj->numero_compte;
  1179. $line->label_compte = $obj->label_compte;
  1180. $line->debit = $obj->debit;
  1181. $line->credit = $obj->credit;
  1182. $line->montant = $obj->montant;
  1183. $line->sens = $obj->sens;
  1184. $line->code_journal = $obj->code_journal;
  1185. $line->piece_num = $obj->piece_num;
  1186. $this->linesexport[] = $line;
  1187. }
  1188. $this->db->free($resql);
  1189. return $num;
  1190. } else {
  1191. $this->error = "Error " . $this->db->lasterror();
  1192. dol_syslog(get_class($this) . "::export_bookkeping " . $this->error, LOG_ERR);
  1193. return - 1;
  1194. }
  1195. }
  1196. /**
  1197. * Return list of accounts with label by chart of accounts
  1198. *
  1199. * @param string $selectid Preselected chart of accounts
  1200. * @param string $htmlname Name of field in html form
  1201. * @param int $showempty Add an empty field
  1202. * @param array $event Event options
  1203. * @param int $select_in Value is a aa.rowid (0 default) or aa.account_number (1)
  1204. * @param int $select_out Set value returned by select 0=rowid (default), 1=account_number
  1205. * @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
  1206. * @return string String with HTML select
  1207. */
  1208. function select_account($selectid, $htmlname = 'account', $showempty = 0, $event = array(), $select_in = 0, $select_out = 0, $aabase = '') {
  1209. global $conf;
  1210. require_once DOL_DOCUMENT_ROOT . '/core/lib/accounting.lib.php';
  1211. $pcgver = $conf->global->CHARTOFACCOUNTS;
  1212. $sql = "SELECT DISTINCT ab.numero_compte as account_number, aa.label as label, aa.rowid as rowid, aa.fk_pcg_version";
  1213. $sql .= " FROM " . MAIN_DB_PREFIX . "accounting_bookkeeping as ab";
  1214. $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "accounting_account as aa ON aa.account_number = ab.numero_compte";
  1215. $sql .= " AND aa.active = 1";
  1216. $sql .= " INNER JOIN " . MAIN_DB_PREFIX . "accounting_system as asy ON aa.fk_pcg_version = asy.pcg_version";
  1217. $sql .= " AND asy.rowid = " . $pcgver;
  1218. $sql .= " AND ab.entity IN (" . getEntity("accountancy", 1) . ")";
  1219. $sql .= " ORDER BY account_number ASC";
  1220. dol_syslog(get_class($this) . "::select_account", LOG_DEBUG);
  1221. $resql = $this->db->query($sql);
  1222. if (! $resql) {
  1223. $this->error = "Error " . $this->db->lasterror();
  1224. dol_syslog(get_class($this) . "::select_account " . $this->error, LOG_ERR);
  1225. return - 1;
  1226. }
  1227. $out = ajax_combobox($htmlname, $event);
  1228. $options = array();
  1229. $selected = null;
  1230. while ($obj = $this->db->fetch_object($resql)) {
  1231. $label = length_accountg($obj->account_number) . ' - ' . $obj->label;
  1232. $select_value_in = $obj->rowid;
  1233. $select_value_out = $obj->rowid;
  1234. if ($select_in == 1) {
  1235. $select_value_in = $obj->account_number;
  1236. }
  1237. if ($select_out == 1) {
  1238. $select_value_out = $obj->account_number;
  1239. }
  1240. // Remember guy's we store in database llx_facturedet the rowid of accounting_account and not the account_number
  1241. // Because same account_number can be share between different accounting_system and do have the same meaning
  1242. if (($selectid != '') && $selectid == $select_value_in) {
  1243. $selected = $select_value_out;
  1244. }
  1245. $options[$select_value_out] = $label;
  1246. }
  1247. $out .= Form::selectarray($htmlname, $options, $selected, $showempty, 0, 0, '', 0, 0, 0, '', 'maxwidth300');
  1248. $this->db->free($resql);
  1249. return $out;
  1250. }
  1251. /**
  1252. * Description of a root accounting account
  1253. *
  1254. * @param string $account Accounting account
  1255. * @return string Root account
  1256. */
  1257. function get_compte_racine($account = null)
  1258. {
  1259. global $conf;
  1260. $pcgver = $conf->global->CHARTOFACCOUNTS;
  1261. $sql = "SELECT root.account_number, root.label as label";
  1262. $sql .= " FROM " . MAIN_DB_PREFIX . "accounting_account as aa";
  1263. $sql .= " INNER JOIN " . MAIN_DB_PREFIX . "accounting_system as asy ON aa.fk_pcg_version = asy.pcg_version";
  1264. $sql .= " AND asy.rowid = " . $pcgver;
  1265. $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "accounting_account as parent ON aa.account_parent = parent.rowid";
  1266. $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "accounting_account as root ON parent.account_parent = root.rowid";
  1267. $sql .= " WHERE aa.account_number = '" . $account . "'";
  1268. $sql .= " AND parent.active = 1";
  1269. $sql .= " AND root.active = 1";
  1270. $sql .= " AND aa.entity IN (" . getEntity("accountancy", 1) . ")";
  1271. dol_syslog(get_class($this) . "::select_account sql=" . $sql, LOG_DEBUG);
  1272. $resql = $this->db->query($sql);
  1273. if ($resql) {
  1274. $obj = '';
  1275. if ($this->db->num_rows($resql)) {
  1276. $obj = $this->db->fetch_object($resql);
  1277. }
  1278. return $obj->label;
  1279. } else {
  1280. $this->error = "Error " . $this->db->lasterror();
  1281. dol_syslog(__METHOD__ . " " . $this->error, LOG_ERR);
  1282. return -1;
  1283. }
  1284. }
  1285. /**
  1286. * Description of accounting account
  1287. *
  1288. * @param string $account Accounting account
  1289. * @return string Account desc
  1290. */
  1291. function get_compte_desc($account = null)
  1292. {
  1293. global $conf;
  1294. $pcgver = $conf->global->CHARTOFACCOUNTS;
  1295. $sql = "SELECT aa.account_number, aa.label, aa.rowid, aa.fk_pcg_version, cat.label as category";
  1296. $sql .= " FROM " . MAIN_DB_PREFIX . "accounting_account as aa ";
  1297. $sql .= " INNER JOIN " . MAIN_DB_PREFIX . "accounting_system as asy ON aa.fk_pcg_version = asy.pcg_version";
  1298. $sql .= " AND aa.account_number = '" . $account . "'";
  1299. $sql .= " AND asy.rowid = " . $pcgver;
  1300. $sql .= " AND aa.active = 1";
  1301. $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "c_accounting_category as cat ON aa.fk_accounting_category = cat.rowid";
  1302. $sql .= " WHERE aa.entity IN (" . getEntity("accountancy", 1) . ")";
  1303. dol_syslog(get_class($this) . "::select_account sql=" . $sql, LOG_DEBUG);
  1304. $resql = $this->db->query($sql);
  1305. if ($resql) {
  1306. $obj = '';
  1307. if ($this->db->num_rows($resql)) {
  1308. $obj = $this->db->fetch_object($resql);
  1309. }
  1310. if(empty($obj->category)){
  1311. return $obj->label;
  1312. }else{
  1313. return $obj->label.' ('.$obj->category.')';
  1314. }
  1315. } else {
  1316. $this->error = "Error " . $this->db->lasterror();
  1317. dol_syslog(__METHOD__ . " " . $this->error, LOG_ERR);
  1318. return -1;
  1319. }
  1320. }
  1321. }
  1322. /**
  1323. * Class BookKeepingLine
  1324. */
  1325. class BookKeepingLine
  1326. {
  1327. public $id;
  1328. public $doc_date = '';
  1329. public $doc_type;
  1330. public $doc_ref;
  1331. public $fk_doc;
  1332. public $fk_docdet;
  1333. public $code_tiers;
  1334. public $numero_compte;
  1335. public $label_compte;
  1336. public $debit;
  1337. public $credit;
  1338. public $montant;
  1339. public $sens;
  1340. public $fk_user_author;
  1341. public $import_key;
  1342. public $code_journal;
  1343. public $piece_num;
  1344. }