lettering.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * \file htdocs/accountancy/class/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 = '".$this->db->escape($object->code_compta)."' ";
  63. }
  64. if ($object->code_compta != "" && $object->code_compta_fournisseur != "") {
  65. $sql .= " OR ";
  66. }
  67. if ($object->code_compta_fournisseur != "") {
  68. $sql .= " bk.subledger_account = '".$this->db->escape($object->code_compta_fournisseur)."' ";
  69. }
  70. $sql .= " ) AND (bk.date_lettering ='' OR bk.date_lettering IS NULL) ";
  71. $sql .= " AND (bk.lettering_code != '' OR bk.lettering_code IS NULL) ";
  72. $sql .= ' AND bk.date_validated IS NULL ';
  73. $sql .= $this->db->order('bk.doc_date', 'DESC');
  74. // echo $sql;
  75. //
  76. $resql = $this->db->query($sql);
  77. if ($resql) {
  78. $num = $this->db->num_rows($resql);
  79. while ($obj = $this->db->fetch_object($resql)) {
  80. $ids = array();
  81. $ids_fact = array();
  82. if ($obj->type == 'payment_supplier') {
  83. $sql = 'SELECT DISTINCT bk.rowid, facf.ref, facf.ref_supplier, payf.fk_bank, facf.rowid as fact_id';
  84. $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn facf ";
  85. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."paiementfourn_facturefourn as payfacf ON payfacf.fk_facturefourn=facf.rowid";
  86. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."paiementfourn as payf ON payfacf.fk_paiementfourn=payf.rowid";
  87. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."accounting_bookkeeping as bk ON (bk.fk_doc = payf.fk_bank AND bk.code_journal='".$this->db->escape($obj->code_journal)."')";
  88. $sql .= " WHERE payfacf.fk_paiementfourn = '".$this->db->escape($obj->url_id)."' ";
  89. $sql .= " AND facf.entity = ".$conf->entity;
  90. $sql .= " AND code_journal IN (SELECT code FROM ".MAIN_DB_PREFIX."accounting_journal WHERE nature=4 AND entity=".$conf->entity.") ";
  91. $sql .= " AND ( ";
  92. if ($object->code_compta != "") {
  93. $sql .= " bk.subledger_account = '".$this->db->escape($object->code_compta)."' ";
  94. }
  95. if ($object->code_compta != "" && $object->code_compta_fournisseur != "") {
  96. $sql .= " OR ";
  97. }
  98. if ($object->code_compta_fournisseur != "") {
  99. $sql .= " bk.subledger_account = '".$this->db->escape($object->code_compta_fournisseur)."' ";
  100. }
  101. $sql .= " ) ";
  102. $resql2 = $this->db->query($sql);
  103. if ($resql2) {
  104. while ($obj2 = $this->db->fetch_object($resql2)) {
  105. $ids[$obj2->rowid] = $obj2->rowid;
  106. $ids_fact[] = $obj2->fact_id;
  107. }
  108. } else {
  109. $this->errors[] = $this->db->lasterror;
  110. return -1;
  111. }
  112. if (count($ids_fact)) {
  113. $sql = 'SELECT bk.rowid, facf.ref, facf.ref_supplier ';
  114. $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn facf ";
  115. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."accounting_bookkeeping as bk ON( bk.fk_doc = facf.rowid AND facf.rowid IN (".$this->db->sanitize(implode(',', $ids_fact))."))";
  116. $sql .= " WHERE bk.code_journal IN (SELECT code FROM ".MAIN_DB_PREFIX."accounting_journal WHERE nature=3 AND entity=".$conf->entity.") ";
  117. $sql .= " AND facf.entity = ".$conf->entity;
  118. $sql .= " AND ( ";
  119. if ($object->code_compta != "") {
  120. $sql .= " bk.subledger_account = '".$this->db->escape($object->code_compta)."' ";
  121. }
  122. if ($object->code_compta != "" && $object->code_compta_fournisseur != "") {
  123. $sql .= " OR ";
  124. }
  125. if ($object->code_compta_fournisseur != "") {
  126. $sql .= " bk.subledger_account = '".$this->db->escape($object->code_compta_fournisseur)."' ";
  127. }
  128. $sql .= ") ";
  129. $resql2 = $this->db->query($sql);
  130. if ($resql2) {
  131. while ($obj2 = $this->db->fetch_object($resql2)) {
  132. $ids[$obj2->rowid] = $obj2->rowid;
  133. }
  134. } else {
  135. $this->errors[] = $this->db->lasterror;
  136. return -1;
  137. }
  138. }
  139. } elseif ($obj->type == 'payment') {
  140. $sql = 'SELECT DISTINCT bk.rowid, fac.ref, fac.ref, pay.fk_bank, fac.rowid as fact_id';
  141. $sql .= " FROM ".MAIN_DB_PREFIX."facture fac ";
  142. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."paiement_facture as payfac ON payfac.fk_facture=fac.rowid";
  143. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."paiement as pay ON payfac.fk_paiement=pay.rowid";
  144. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."accounting_bookkeeping as bk ON (bk.fk_doc = pay.fk_bank AND bk.code_journal='".$this->db->escape($obj->code_journal)."')";
  145. $sql .= " WHERE payfac.fk_paiement = '".$this->db->escape($obj->url_id)."' ";
  146. $sql .= " AND bk.code_journal IN (SELECT code FROM ".MAIN_DB_PREFIX."accounting_journal WHERE nature=4 AND entity=".$conf->entity.") ";
  147. $sql .= " AND fac.entity IN (".getEntity('invoice', 0).")"; // We don't share object for accountancy
  148. $sql .= " AND ( ";
  149. if ($object->code_compta != "") {
  150. $sql .= " bk.subledger_account = '".$this->db->escape($object->code_compta)."' ";
  151. }
  152. if ($object->code_compta != "" && $object->code_compta_fournisseur != "") {
  153. $sql .= " OR ";
  154. }
  155. if ($object->code_compta_fournisseur != "") {
  156. $sql .= " bk.subledger_account = '".$this->db->escape($object->code_compta_fournisseur)."' ";
  157. }
  158. $sql .= " )";
  159. $resql2 = $this->db->query($sql);
  160. if ($resql2) {
  161. while ($obj2 = $this->db->fetch_object($resql2)) {
  162. $ids[$obj2->rowid] = $obj2->rowid;
  163. $ids_fact[] = $obj2->fact_id;
  164. }
  165. } else {
  166. $this->errors[] = $this->db->lasterror;
  167. return -1;
  168. }
  169. if (count($ids_fact)) {
  170. $sql = 'SELECT bk.rowid, fac.ref, fac.ref_supplier ';
  171. $sql .= " FROM ".MAIN_DB_PREFIX."facture fac ";
  172. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."accounting_bookkeeping as bk ON( bk.fk_doc = fac.rowid AND fac.rowid IN (".$this->db->sanitize(implode(',', $ids_fact))."))";
  173. $sql .= " WHERE code_journal IN (SELECT code FROM ".MAIN_DB_PREFIX."accounting_journal WHERE nature=2 AND entity=".$conf->entity.") ";
  174. $sql .= " AND fac.entity IN (".getEntity('invoice', 0).")"; // We don't share object for accountancy
  175. $sql .= " AND ( ";
  176. if ($object->code_compta != "") {
  177. $sql .= " bk.subledger_account = '".$this->db->escape($object->code_compta)."' ";
  178. }
  179. if ($object->code_compta != "" && $object->code_compta_fournisseur != "") {
  180. $sql .= " OR ";
  181. }
  182. if ($object->code_compta_fournisseur != "") {
  183. $sql .= " bk.subledger_account = '".$this->db->escape($object->code_compta_fournisseur)."' ";
  184. }
  185. $sql .= " ) ";
  186. $resql2 = $this->db->query($sql);
  187. if ($resql2) {
  188. while ($obj2 = $this->db->fetch_object($resql2)) {
  189. $ids[$obj2->rowid] = $obj2->rowid;
  190. }
  191. } else {
  192. $this->errors[] = $this->db->lasterror;
  193. return -1;
  194. }
  195. }
  196. }
  197. if (count($ids) > 1) {
  198. $result = $this->updateLettering($ids);
  199. }
  200. }
  201. }
  202. if ($error) {
  203. foreach ($this->errors as $errmsg) {
  204. dol_syslog(__METHOD__.' '.$errmsg, LOG_ERR);
  205. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  206. }
  207. return -1 * $error;
  208. } else {
  209. return 1;
  210. }
  211. }
  212. /**
  213. *
  214. * @param array $ids ids array
  215. * @param boolean $notrigger no trigger
  216. * @return number
  217. */
  218. public function updateLettering($ids = array(), $notrigger = false)
  219. {
  220. $error = 0;
  221. $lettre = 'AAA';
  222. $sql = "SELECT DISTINCT lettering_code FROM ".MAIN_DB_PREFIX."accounting_bookkeeping WHERE ";
  223. $sql .= " lettering_code != '' ORDER BY lettering_code DESC limit 1";
  224. $result = $this->db->query($sql);
  225. if ($result) {
  226. $obj = $this->db->fetch_object($result);
  227. $lettre = (empty($obj->lettering_code) ? 'AAA' : $obj->lettering_code);
  228. if (!empty($obj->lettering_code)) {
  229. $lettre++;
  230. }
  231. } else {
  232. $this->errors[] = 'Error'.$this->db->lasterror();
  233. $error++;
  234. }
  235. $sql = "SELECT SUM(ABS(debit)) as deb, SUM(ABS(credit)) as cred FROM ".MAIN_DB_PREFIX."accounting_bookkeeping WHERE ";
  236. $sql .= " rowid IN (".$this->db->sanitize(implode(',', $ids)).") AND date_validated IS NULL";
  237. $result = $this->db->query($sql);
  238. if ($result) {
  239. $obj = $this->db->fetch_object($result);
  240. if (!(round(abs($obj->deb), 2) === round(abs($obj->cred), 2))) {
  241. $this->errors[] = 'Total not exacts '.round(abs($obj->deb), 2).' vs '.round(abs($obj->cred), 2);
  242. $error++;
  243. }
  244. } else {
  245. $this->errors[] = 'Erreur sql'.$this->db->lasterror();
  246. $error++;
  247. }
  248. // Update request
  249. $now = dol_now();
  250. if (!$error) {
  251. $sql = "UPDATE ".MAIN_DB_PREFIX."accounting_bookkeeping SET";
  252. $sql .= " lettering_code='".$this->db->escape($lettre)."'";
  253. $sql .= " , date_lettering = '".$this->db->idate($now)."'"; // todo correct date it's false
  254. $sql .= " WHERE rowid IN (".$this->db->sanitize(implode(',', $ids)).") AND date_validated IS NULL ";
  255. $this->db->begin();
  256. dol_syslog(get_class($this)."::update", LOG_DEBUG);
  257. $resql = $this->db->query($sql);
  258. if (!$resql) {
  259. $error++;
  260. $this->errors[] = "Error ".$this->db->lasterror();
  261. }
  262. }
  263. // Commit or rollback
  264. if ($error) {
  265. foreach ($this->errors as $errmsg) {
  266. dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
  267. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  268. }
  269. $this->db->rollback();
  270. return -1 * $error;
  271. } else {
  272. $this->db->commit();
  273. return 1;
  274. }
  275. }
  276. }