bookkeeping.class.php 61 KB

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