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