lettering.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 Alexandre Spangaro <alexandre.spangaro@gmail.com>
  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 2 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 accountancy/class/bookkeeping.class.php
  21. * \ingroup Advanced accountancy
  22. * \brief File of class for lettering
  23. */
  24. include_once DOL_DOCUMENT_ROOT . "/accountancy/class/bookkeeping.class.php";
  25. include_once DOL_DOCUMENT_ROOT . "/societe/class/societe.class.php";
  26. include_once DOL_DOCUMENT_ROOT . "/core/lib/date.lib.php";
  27. /**
  28. * Class lettering
  29. */
  30. class lettering extends BookKeeping
  31. {
  32. /**
  33. * lettrageTiers
  34. *
  35. * @param int $socid Thirdparty id
  36. * @return void
  37. */
  38. public function lettrageTiers($socid)
  39. {
  40. $error = 0;
  41. $object = new Societe($this->db);
  42. $object->id = $socid;
  43. $object->fetch($socid);
  44. if ($object->code_compta == '411CUSTCODE') {
  45. $object->code_compta = '';
  46. }
  47. if ($object->code_compta_fournisseur == '401SUPPCODE') {
  48. $object->code_compta_fournisseur = '';
  49. }
  50. /**
  51. * Prise en charge des lettering complexe avec prelevment , virement
  52. */
  53. $sql = "SELECT DISTINCT bk.rowid, bk.doc_date, bk.doc_type, bk.doc_ref, bk.thirdparty_code, ";
  54. $sql .= " bk.numero_compte , bk.label_compte, bk.debit , bk.credit, bk.montant ";
  55. $sql .= " , bk.sens , bk.code_journal , bk.piece_num, bk.date_lettering, bu.url_id , bu.type ";
  56. $sql .= " FROM " . MAIN_DB_PREFIX . "accounting_bookkeeping as bk";
  57. $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "bank_url as bu ON(bk.fk_doc = bu.fk_bank AND bu.type IN ('payment', 'payment_supplier') ) ";
  58. $sql .= " WHERE ( ";
  59. if (! empty($object->code_compta))
  60. $sql .= " bk.thirdparty_code = '" . $object->code_compta . "' ";
  61. if (! empty($object->code_compta) && ! empty($object->code_compta_fournisseur))
  62. $sql .= " OR ";
  63. if (! empty($object->code_compta_fournisseur))
  64. $sql .= " bk.thirdparty_code = '" . $object->code_compta_fournisseur . "' ";
  65. $sql .= " ) AND (bk.date_lettering ='' OR bk.date_lettering IS NULL) ";
  66. $sql .= " AND (bk.lettering_code != '' OR bk.lettering_code IS NULL) ";
  67. $sql .= $this->db->order('bk.doc_date', 'DESC');
  68. // echo $sql;
  69. //
  70. $resql = $this->db->query($sql);
  71. if ($resql) {
  72. $num = $this->db->num_rows($resql);
  73. while ($obj = $this->db->fetch_object($resql) ) {
  74. $ids = array();
  75. $ids_fact = array();
  76. if ($obj->type == 'payment_supplier')
  77. {
  78. $sql = 'SELECT DISTINCT bk.rowid, facf.ref, facf.ref_supplier, payf.fk_bank, facf.rowid as fact_id';
  79. $sql .= " FROM " . MAIN_DB_PREFIX . "facture_fourn facf ";
  80. $sql .= " INNER JOIN " . MAIN_DB_PREFIX . "paiementfourn_facturefourn as payfacf ON payfacf.fk_facturefourn=facf.rowid";
  81. $sql .= " INNER JOIN " . MAIN_DB_PREFIX . "paiementfourn as payf ON payfacf.fk_paiementfourn=payf.rowid";
  82. $sql .= " INNER JOIN " . MAIN_DB_PREFIX . "accounting_bookkeeping as bk ON (bk.fk_doc = payf.fk_bank AND bk.code_journal='" . $obj->code_journal . "')";
  83. $sql .= " WHERE payfacf.fk_paiementfourn = '" . $obj->url_id . "' ";
  84. $sql .= " AND code_journal IN (SELECT code FROM " . MAIN_DB_PREFIX . "accounting_journal WHERE nature=4) ";
  85. $sql .= " AND ( ";
  86. if (! empty($object->code_compta)) {
  87. $sql .= " bk.thirdparty_code = '" . $object->code_compta . "' ";
  88. }
  89. if (! empty($object->code_compta) && ! empty($object->code_compta_fournisseur)) {
  90. $sql .= " OR ";
  91. }
  92. if (! empty($object->code_compta_fournisseur)) {
  93. $sql .= " bk.thirdparty_code = '" . $object->code_compta_fournisseur . "' ";
  94. }
  95. $sql .= " ) ";
  96. $resql2 = $this->db->query($sql);
  97. if ($resql2) {
  98. while ( $obj2 = $this->db->fetch_object($resql2) ) {
  99. $ids[$obj2->rowid] = $obj2->rowid;
  100. $ids_fact[] = $obj2->fact_id;
  101. }
  102. } else {
  103. $this->errors[] = $this->db->lasterror;
  104. return - 1;
  105. }
  106. if (count($ids_fact)) {
  107. $sql = 'SELECT bk.rowid, facf.ref, facf.ref_supplier ';
  108. $sql .= " FROM " . MAIN_DB_PREFIX . "facture_fourn facf ";
  109. $sql .= " INNER JOIN " . MAIN_DB_PREFIX . "accounting_bookkeeping as bk ON( bk.fk_doc = facf.rowid AND facf.rowid IN (" . implode(',', $ids_fact) . "))";
  110. $sql .= " WHERE bk.code_journal IN (SELECT code FROM " . MAIN_DB_PREFIX . "accounting_journal WHERE nature=3) ";
  111. $sql .= " AND ( ";
  112. if (! empty($object->code_compta)) {
  113. $sql .= " bk.thirdparty_code = '" . $object->code_compta . "' ";
  114. }
  115. if (! empty($object->code_compta) && ! empty($object->code_compta_fournisseur)) {
  116. $sql .= " OR ";
  117. }
  118. if (! empty($object->code_compta_fournisseur)) {
  119. $sql .= " bk.thirdparty_code = '" . $object->code_compta_fournisseur . "' ";
  120. }
  121. $sql .= " ) ";
  122. $resql2 = $this->db->query($sql);
  123. if ($resql2) {
  124. while ( $obj2 = $this->db->fetch_object($resql2) ) {
  125. $ids[$obj2->rowid] = $obj2->rowid;
  126. }
  127. } else {
  128. $this->errors[] = $this->db->lasterror;
  129. return - 1;
  130. }
  131. }
  132. } elseif ($obj->type == 'payment') {
  133. $sql = 'SELECT DISTINCT bk.rowid, fac.ref, fac.ref, pay.fk_bank, fac.rowid as fact_id';
  134. $sql .= " FROM " . MAIN_DB_PREFIX . "facture fac ";
  135. $sql .= " INNER JOIN " . MAIN_DB_PREFIX . "paiement_facture as payfac ON payfac.fk_facture=fac.rowid";
  136. $sql .= " INNER JOIN " . MAIN_DB_PREFIX . "paiement as pay ON payfac.fk_paiement=pay.rowid";
  137. $sql .= " INNER JOIN " . MAIN_DB_PREFIX . "accounting_bookkeeping as bk ON (bk.fk_doc = pay.fk_bank AND bk.code_journal='" . $obj->code_journal . "')";
  138. $sql .= " WHERE payfac.fk_paiement = '" . $obj->url_id . "' ";
  139. $sql .= " AND bk.code_journal IN (SELECT code FROM " . MAIN_DB_PREFIX . "accounting_journal WHERE nature=4) ";
  140. $sql .= " AND ( ";
  141. if (! empty($object->code_compta)) {
  142. $sql .= " bk.thirdparty_code = '" . $object->code_compta . "' ";
  143. }
  144. if (! empty($object->code_compta) && ! empty($object->code_compta_fournisseur)) {
  145. $sql .= " OR ";
  146. }
  147. if (! empty($object->code_compta_fournisseur)) {
  148. $sql .= " bk.thirdparty_code = '" . $object->code_compta_fournisseur . "' ";
  149. }
  150. $sql .= " ) ";
  151. $resql2 = $this->db->query($sql);
  152. if ($resql2) {
  153. while ( $obj2 = $this->db->fetch_object($resql2) ) {
  154. $ids[$obj2->rowid] = $obj2->rowid;
  155. $ids_fact[] = $obj2->fact_id;
  156. }
  157. } else {
  158. $this->errors[] = $this->db->lasterror;
  159. return - 1;
  160. }
  161. if (count($ids_fact)) {
  162. $sql = 'SELECT bk.rowid, fac.ref, fac.ref_supplier ';
  163. $sql .= " FROM " . MAIN_DB_PREFIX . "facture fac ";
  164. $sql .= " INNER JOIN " . MAIN_DB_PREFIX . "accounting_bookkeeping as bk ON( bk.fk_doc = fac.rowid AND fac.rowid IN (" . implode(',', $ids_fact) . "))";
  165. $sql .= " WHERE code_journal IN (SELECT code FROM " . MAIN_DB_PREFIX . "accounting_journal WHERE nature=2) ";
  166. $sql .= " AND ( ";
  167. if (! empty($object->code_compta)) {
  168. $sql .= " bk.thirdparty_code = '" . $object->code_compta . "' ";
  169. }
  170. if (! empty($object->code_compta) && ! empty($object->code_compta_fournisseur)) {
  171. $sql .= " OR ";
  172. }
  173. if (! empty($object->code_compta_fournisseur)) {
  174. $sql .= " bk.thirdparty_code = '" . $object->code_compta_fournisseur . "' ";
  175. }
  176. $sql .= " ) ";
  177. $resql2 = $this->db->query($sql);
  178. if ($resql2) {
  179. while ( $obj2 = $this->db->fetch_object($resql2) ) {
  180. $ids[$obj2->rowid] = $obj2->rowid;
  181. }
  182. } else {
  183. $this->errors[] = $this->db->lasterror;
  184. return - 1;
  185. }
  186. }
  187. }
  188. if (count($ids) > 1) {
  189. $result = $this->updatelettrage($ids);
  190. }
  191. }
  192. }
  193. if ($error) {
  194. foreach ( $this->errors as $errmsg ) {
  195. dol_syslog(get_class($this) . "::" . __METHOD__ . $errmsg, LOG_ERR);
  196. $this->error .= ($this->error ? ', ' . $errmsg : $errmsg);
  197. }
  198. return - 1 * $error;
  199. } else {
  200. return 1;
  201. }
  202. }
  203. /**
  204. *
  205. * @param array $ids ids array
  206. * @param boolean $notrigger no trigger
  207. * @return number
  208. */
  209. public function updateLettrage($ids = array(), $notrigger = false) {
  210. $error = 0;
  211. dol_syslog(get_class($this) . "::" . __METHOD__, LOG_DEBUG);
  212. $sql = "SELECT DISTINCT lettering_code FROM " . MAIN_DB_PREFIX . "accounting_bookkeeping WHERE ";
  213. $sql .= " lettering_code != '' ORDER BY lettering_code DESC limit 1; ";
  214. $result = $this->db->query($sql);
  215. if ($result) {
  216. $obj = $this->db->fetch_object($result);
  217. $lettre = (empty($obj->lettering_code) ? 'AAA' : $obj->lettering_code);
  218. if (! empty($obj->lettering_code))
  219. $lettre ++;
  220. } else {
  221. $this->errors[] = 'Error' . $this->db->lasterror();
  222. ;
  223. $error ++;
  224. }
  225. $sql = "SELECT SUM(ABS(debit)) as deb, SUM(ABS(credit)) as cred FROM " . MAIN_DB_PREFIX . "accounting_bookkeeping WHERE ";
  226. $sql .= " rowid IN (" . implode(',', $ids) . ") ";
  227. $result = $this->db->query($sql);
  228. if ($result) {
  229. $obj = $this->db->fetch_object($result);
  230. if (! (round(abs($obj->deb), 2) === round(abs($obj->cred), 2))) {
  231. $this->errors[] = 'Total not exacts ' . round(abs($obj->deb), 2) . ' vs ' . round(abs($obj->cred), 2);
  232. $error ++;
  233. }
  234. } else {
  235. $this->errors[] = 'Erreur sql' . $this->db->lasterror();
  236. ;
  237. $error ++;
  238. }
  239. // Update request
  240. $now = dol_now();
  241. $sql = "UPDATE " . MAIN_DB_PREFIX . "accounting_bookkeeping SET";
  242. $sql .= " lettering_code='" . $lettre . "'";
  243. $sql .= " , date_lettering = '" . $this->db->idate($now) . "'"; // todo correct date it's false
  244. $sql .= " WHERE rowid IN (" . implode(',', $ids) . ") ";
  245. $this->db->begin();
  246. dol_syslog(get_class($this) . "::update sql=" . $sql, LOG_DEBUG);
  247. $resql = $this->db->query($sql);
  248. if (! $resql) {
  249. $error ++;
  250. $this->errors[] = "Error " . $this->db->lasterror();
  251. }
  252. if (! $error) {
  253. if (! $notrigger) {
  254. // Uncomment this and change MYOBJECT to your own tag if you
  255. // want this action calls a trigger.
  256. // // Call triggers
  257. // include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  258. // $interface=new Interfaces($this->db);
  259. // $result=$interface->run_triggers('MYOBJECT_MODIFY',$this,$user,$langs,$conf);
  260. // if ($result < 0) { $error++; $this->errors=$interface->errors; }
  261. // // End call triggers
  262. }
  263. }
  264. // Commit or rollback
  265. if ($error) {
  266. foreach ( $this->errors as $errmsg ) {
  267. dol_syslog(get_class($this) . "::update " . $errmsg, LOG_ERR);
  268. $this->error .= ($this->error ? ', ' . $errmsg : $errmsg);
  269. }
  270. $this->db->rollback();
  271. return - 1 * $error;
  272. } else {
  273. $this->db->commit();
  274. return 1;
  275. }
  276. }
  277. }