lettering.class.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. <?php
  2. /* Copyright (C) 2004-2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2013 Olivier Geffroy <jeff@jeffinfo.com>
  4. * Copyright (C) 2013-2019 Alexandre Spangaro <aspangaro@open-dsi.fr>
  5. * Copyright (C) 2018 Frédéric France <frederic.france@netlogic.fr>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * \file htdocs/accountancy/class/lettering.class.php
  22. * \ingroup Accountancy (Double entries)
  23. * \brief File of class for lettering
  24. */
  25. include_once DOL_DOCUMENT_ROOT . "/accountancy/class/bookkeeping.class.php";
  26. include_once DOL_DOCUMENT_ROOT . "/societe/class/societe.class.php";
  27. include_once DOL_DOCUMENT_ROOT . "/core/lib/date.lib.php";
  28. /**
  29. * Class Lettering
  30. */
  31. class Lettering extends BookKeeping
  32. {
  33. /**
  34. * letteringThirdparty
  35. *
  36. * @param int $socid Thirdparty id
  37. * @return int 1 OK, <0 error
  38. */
  39. public function letteringThirdparty($socid)
  40. {
  41. global $conf;
  42. $error = 0;
  43. $object = new Societe($this->db);
  44. $object->id = $socid;
  45. $object->fetch($socid);
  46. if ($object->code_compta == '411CUSTCODE') {
  47. $object->code_compta = '';
  48. }
  49. if ($object->code_compta_fournisseur == '401SUPPCODE') {
  50. $object->code_compta_fournisseur = '';
  51. }
  52. /**
  53. * Prise en charge des lettering complexe avec prelevment , virement
  54. */
  55. $sql = "SELECT DISTINCT bk.rowid, bk.doc_date, bk.doc_type, bk.doc_ref, bk.subledger_account, ";
  56. $sql .= " bk.numero_compte , bk.label_compte, bk.debit , bk.credit, bk.montant ";
  57. $sql .= " , bk.sens , bk.code_journal , bk.piece_num, bk.date_lettering, bu.url_id , bu.type ";
  58. $sql .= " FROM " . MAIN_DB_PREFIX . "accounting_bookkeeping as bk";
  59. $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "bank_url as bu ON(bk.fk_doc = bu.fk_bank AND bu.type IN ('payment', 'payment_supplier') ) ";
  60. $sql .= " WHERE ( ";
  61. if ($object->code_compta != "")
  62. $sql .= " bk.subledger_account = '" . $object->code_compta . "' ";
  63. if ($object->code_compta != "" && $object->code_compta_fournisseur != "")
  64. $sql .= " OR ";
  65. if ($object->code_compta_fournisseur != "")
  66. $sql .= " bk.subledger_account = '" . $object->code_compta_fournisseur . "' ";
  67. $sql .= " ) AND (bk.date_lettering ='' OR bk.date_lettering IS NULL) ";
  68. $sql .= " AND (bk.lettering_code != '' OR bk.lettering_code IS NULL) ";
  69. $sql .= $this->db->order('bk.doc_date', 'DESC');
  70. // echo $sql;
  71. //
  72. $resql = $this->db->query($sql);
  73. if ($resql) {
  74. $num = $this->db->num_rows($resql);
  75. while ($obj = $this->db->fetch_object($resql) ) {
  76. $ids = array();
  77. $ids_fact = array();
  78. if ($obj->type == 'payment_supplier')
  79. {
  80. $sql = 'SELECT DISTINCT bk.rowid, facf.ref, facf.ref_supplier, payf.fk_bank, facf.rowid as fact_id';
  81. $sql .= " FROM " . MAIN_DB_PREFIX . "facture_fourn facf ";
  82. $sql .= " INNER JOIN " . MAIN_DB_PREFIX . "paiementfourn_facturefourn as payfacf ON payfacf.fk_facturefourn=facf.rowid";
  83. $sql .= " INNER JOIN " . MAIN_DB_PREFIX . "paiementfourn as payf ON payfacf.fk_paiementfourn=payf.rowid";
  84. $sql .= " INNER JOIN " . MAIN_DB_PREFIX . "accounting_bookkeeping as bk ON (bk.fk_doc = payf.fk_bank AND bk.code_journal='" . $obj->code_journal . "')";
  85. $sql .= " WHERE payfacf.fk_paiementfourn = '" . $obj->url_id . "' ";
  86. $sql .= " AND facf.entity = ".$conf->entity;
  87. $sql .= " AND code_journal IN (SELECT code FROM " . MAIN_DB_PREFIX . "accounting_journal WHERE nature=4 AND entity=".$conf->entity.") ";
  88. $sql .= " AND ( ";
  89. if ($object->code_compta != "") {
  90. $sql .= " bk.subledger_account = '" . $object->code_compta . "' ";
  91. }
  92. if ($object->code_compta != "" && $object->code_compta_fournisseur != "") {
  93. $sql .= " OR ";
  94. }
  95. if ($object->code_compta_fournisseur != "") {
  96. $sql .= " bk.subledger_account = '" . $object->code_compta_fournisseur . "' ";
  97. }
  98. $sql .= " ) ";
  99. $resql2 = $this->db->query($sql);
  100. if ($resql2) {
  101. while ( $obj2 = $this->db->fetch_object($resql2) ) {
  102. $ids[$obj2->rowid] = $obj2->rowid;
  103. $ids_fact[] = $obj2->fact_id;
  104. }
  105. } else {
  106. $this->errors[] = $this->db->lasterror;
  107. return - 1;
  108. }
  109. if (count($ids_fact)) {
  110. $sql = 'SELECT bk.rowid, facf.ref, facf.ref_supplier ';
  111. $sql .= " FROM " . MAIN_DB_PREFIX . "facture_fourn facf ";
  112. $sql .= " INNER JOIN " . MAIN_DB_PREFIX . "accounting_bookkeeping as bk ON( bk.fk_doc = facf.rowid AND facf.rowid IN (" . implode(',', $ids_fact) . "))";
  113. $sql .= " WHERE bk.code_journal IN (SELECT code FROM " . MAIN_DB_PREFIX . "accounting_journal WHERE nature=3 AND entity=".$conf->entity.") ";
  114. $sql .= " AND facf.entity = ".$conf->entity;
  115. $sql .= " AND ( ";
  116. if ($object->code_compta != "") {
  117. $sql .= " bk.subledger_account = '" . $object->code_compta . "' ";
  118. }
  119. if ($object->code_compta != "" && $object->code_compta_fournisseur != "") {
  120. $sql .= " OR ";
  121. }
  122. if ($object->code_compta_fournisseur != "") {
  123. $sql .= " bk.subledger_account = '" . $object->code_compta_fournisseur . "' ";
  124. }
  125. $sql .= ") ";
  126. $resql2 = $this->db->query($sql);
  127. if ($resql2) {
  128. while ( $obj2 = $this->db->fetch_object($resql2) ) {
  129. $ids[$obj2->rowid] = $obj2->rowid;
  130. }
  131. } else {
  132. $this->errors[] = $this->db->lasterror;
  133. return - 1;
  134. }
  135. }
  136. } elseif ($obj->type == 'payment') {
  137. $sql = 'SELECT DISTINCT bk.rowid, fac.ref, fac.ref, pay.fk_bank, fac.rowid as fact_id';
  138. $sql .= " FROM " . MAIN_DB_PREFIX . "facture fac ";
  139. $sql .= " INNER JOIN " . MAIN_DB_PREFIX . "paiement_facture as payfac ON payfac.fk_facture=fac.rowid";
  140. $sql .= " INNER JOIN " . MAIN_DB_PREFIX . "paiement as pay ON payfac.fk_paiement=pay.rowid";
  141. $sql .= " INNER JOIN " . MAIN_DB_PREFIX . "accounting_bookkeeping as bk ON (bk.fk_doc = pay.fk_bank AND bk.code_journal='" . $obj->code_journal . "')";
  142. $sql .= " WHERE payfac.fk_paiement = '" . $obj->url_id . "' ";
  143. $sql .= " AND bk.code_journal IN (SELECT code FROM " . MAIN_DB_PREFIX . "accounting_journal WHERE nature=4 AND entity=".$conf->entity.") ";
  144. $sql .= " AND fac.entity IN (".getEntity('invoice', 0).")";// We don't share object for accountancy
  145. $sql .= " AND ( ";
  146. if ($object->code_compta != "") {
  147. $sql .= " bk.subledger_account = '" . $object->code_compta . "' ";
  148. }
  149. if ($object->code_compta != "" && $object->code_compta_fournisseur != "") {
  150. $sql .= " OR ";
  151. }
  152. if ($object->code_compta_fournisseur != "") {
  153. $sql .= " bk.subledger_account = '" . $object->code_compta_fournisseur . "' ";
  154. }
  155. $sql .= " ) ";
  156. $resql2 = $this->db->query($sql);
  157. if ($resql2) {
  158. while ( $obj2 = $this->db->fetch_object($resql2) ) {
  159. $ids[$obj2->rowid] = $obj2->rowid;
  160. $ids_fact[] = $obj2->fact_id;
  161. }
  162. } else {
  163. $this->errors[] = $this->db->lasterror;
  164. return - 1;
  165. }
  166. if (count($ids_fact)) {
  167. $sql = 'SELECT bk.rowid, fac.ref, fac.ref_supplier ';
  168. $sql .= " FROM " . MAIN_DB_PREFIX . "facture fac ";
  169. $sql .= " INNER JOIN " . MAIN_DB_PREFIX . "accounting_bookkeeping as bk ON( bk.fk_doc = fac.rowid AND fac.rowid IN (" . implode(',', $ids_fact) . "))";
  170. $sql .= " WHERE code_journal IN (SELECT code FROM " . MAIN_DB_PREFIX . "accounting_journal WHERE nature=2 AND entity=".$conf->entity.") ";
  171. $sql .= " AND fac.entity IN (".getEntity('invoice', 0).")";// We don't share object for accountancy
  172. $sql .= " AND ( ";
  173. if ($object->code_compta != "") {
  174. $sql .= " bk.subledger_account = '" . $object->code_compta . "' ";
  175. }
  176. if ($object->code_compta != "" && $object->code_compta_fournisseur != "") {
  177. $sql .= " OR ";
  178. }
  179. if ($object->code_compta_fournisseur != "") {
  180. $sql .= " bk.subledger_account = '" . $object->code_compta_fournisseur . "' ";
  181. }
  182. $sql .= " ) ";
  183. $resql2 = $this->db->query($sql);
  184. if ($resql2) {
  185. while ( $obj2 = $this->db->fetch_object($resql2) ) {
  186. $ids[$obj2->rowid] = $obj2->rowid;
  187. }
  188. } else {
  189. $this->errors[] = $this->db->lasterror;
  190. return - 1;
  191. }
  192. }
  193. }
  194. if (count($ids) > 1) {
  195. $result = $this->updateLettering($ids);
  196. }
  197. }
  198. }
  199. if ($error) {
  200. foreach ($this->errors as $errmsg) {
  201. dol_syslog(get_class($this) . "::" . __METHOD__ . $errmsg, LOG_ERR);
  202. $this->error .= ($this->error ? ', ' . $errmsg : $errmsg);
  203. }
  204. return - 1 * $error;
  205. } else {
  206. return 1;
  207. }
  208. }
  209. /**
  210. *
  211. * @param array $ids ids array
  212. * @param boolean $notrigger no trigger
  213. * @return number
  214. */
  215. public function updateLettering($ids = array(), $notrigger = false)
  216. {
  217. $error = 0;
  218. $lettre = 'AAA';
  219. dol_syslog(get_class($this) . "::" . __METHOD__, LOG_DEBUG);
  220. $sql = "SELECT DISTINCT lettering_code FROM " . MAIN_DB_PREFIX . "accounting_bookkeeping WHERE ";
  221. $sql .= " lettering_code != '' ORDER BY lettering_code DESC limit 1; ";
  222. $result = $this->db->query($sql);
  223. if ($result) {
  224. $obj = $this->db->fetch_object($result);
  225. $lettre = (empty($obj->lettering_code) ? 'AAA' : $obj->lettering_code);
  226. if (! empty($obj->lettering_code))
  227. $lettre++;
  228. } else {
  229. $this->errors[] = 'Error' . $this->db->lasterror();
  230. $error++;
  231. }
  232. $sql = "SELECT SUM(ABS(debit)) as deb, SUM(ABS(credit)) as cred FROM " . MAIN_DB_PREFIX . "accounting_bookkeeping WHERE ";
  233. $sql .= " rowid IN (" . implode(',', $ids) . ") ";
  234. $result = $this->db->query($sql);
  235. if ($result) {
  236. $obj = $this->db->fetch_object($result);
  237. if (! (round(abs($obj->deb), 2) === round(abs($obj->cred), 2))) {
  238. $this->errors[] = 'Total not exacts ' . round(abs($obj->deb), 2) . ' vs ' . round(abs($obj->cred), 2);
  239. $error++;
  240. }
  241. } else {
  242. $this->errors[] = 'Erreur sql' . $this->db->lasterror();
  243. $error++;
  244. }
  245. // Update request
  246. $now = dol_now();
  247. if (! $error)
  248. {
  249. $sql = "UPDATE " . MAIN_DB_PREFIX . "accounting_bookkeeping SET";
  250. $sql .= " lettering_code='" . $lettre . "'";
  251. $sql .= " , date_lettering = '" . $this->db->idate($now) . "'"; // todo correct date it's false
  252. $sql .= " WHERE rowid IN (" . implode(',', $ids) . ") ";
  253. $this->db->begin();
  254. dol_syslog(get_class($this) . "::update sql=" . $sql, LOG_DEBUG);
  255. $resql = $this->db->query($sql);
  256. if (! $resql) {
  257. $error++;
  258. $this->errors[] = "Error " . $this->db->lasterror();
  259. }
  260. }
  261. if (! $error) {
  262. if (! $notrigger) {
  263. // Uncomment this and change MYOBJECT to your own tag if you
  264. // want this action calls a trigger.
  265. // // Call triggers
  266. // include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  267. // $interface=new Interfaces($this->db);
  268. // $result=$interface->run_triggers('MYOBJECT_MODIFY',$this,$user,$langs,$conf);
  269. // if ($result < 0) { $error++; $this->errors=$interface->errors; }
  270. // // End call triggers
  271. }
  272. }
  273. // Commit or rollback
  274. if ($error) {
  275. foreach ($this->errors as $errmsg) {
  276. dol_syslog(get_class($this) . "::update " . $errmsg, LOG_ERR);
  277. $this->error .= ($this->error ? ', ' . $errmsg : $errmsg);
  278. }
  279. $this->db->rollback();
  280. return - 1 * $error;
  281. } else {
  282. $this->db->commit();
  283. return 1;
  284. }
  285. }
  286. }