| Total Complexity | 156 | 
| Total Lines | 932 | 
| Duplicated Lines | 0 % | 
| Changes | 0 | ||
Complex classes like Lettering often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Lettering, and based on these observations, apply Extract Interface, too.
| 1 | <?php | ||
| 38 | class Lettering extends BookKeeping | ||
| 39 | { | ||
| 40 | public static $doc_type_infos = array( | ||
| 41 | 'customer_invoice' => array( | ||
| 42 | 'payment_table' => 'paiement', | ||
| 43 | 'payment_table_fk_bank' => 'fk_bank', | ||
| 44 | 'doc_payment_table' => 'paiement_facture', | ||
| 45 | 'doc_payment_table_fk_payment' => 'fk_paiement', | ||
| 46 | 'doc_payment_table_fk_doc' => 'fk_facture', | ||
| 47 | 'linked_info' => array( | ||
| 48 | array( | ||
| 49 | 'table' => 'paiement_facture', | ||
| 50 | 'fk_doc' => 'fk_facture', | ||
| 51 | 'fk_link' => 'fk_paiement', | ||
| 52 | 'prefix' => 'p', | ||
| 53 | ), | ||
| 54 | array( | ||
| 55 | 'table' => 'societe_remise_except', | ||
| 56 | 'fk_doc' => 'fk_facture_source', | ||
| 57 | 'fk_link' => 'fk_facture', | ||
| 58 | 'fk_line_link' => 'fk_facture_line', | ||
| 59 | 'table_link_line' => 'facturedet', | ||
| 60 | 'fk_table_link_line' => 'rowid', | ||
| 61 | 'fk_table_link_line_parent' => 'fk_facture', | ||
| 62 | 'prefix' => 'a', | ||
| 63 | 'is_fk_link_is_also_fk_doc' => true, | ||
| 64 | ), | ||
| 65 | ), | ||
| 66 | ), | ||
| 67 | 'supplier_invoice' => array( | ||
| 68 | 'payment_table' => 'paiementfourn', | ||
| 69 | 'payment_table_fk_bank' => 'fk_bank', | ||
| 70 | 'doc_payment_table' => 'paiementfourn_facturefourn', | ||
| 71 | 'doc_payment_table_fk_payment' => 'fk_paiementfourn', | ||
| 72 | 'doc_payment_table_fk_doc' => 'fk_facturefourn', | ||
| 73 | 'linked_info' => array( | ||
| 74 | array( | ||
| 75 | 'table' => 'paiementfourn_facturefourn', | ||
| 76 | 'fk_doc' => 'fk_facturefourn', | ||
| 77 | 'fk_link' => 'fk_paiementfourn', | ||
| 78 | 'prefix' => 'p', | ||
| 79 | ), | ||
| 80 | array( | ||
| 81 | 'table' => 'societe_remise_except', | ||
| 82 | 'fk_doc' => 'fk_invoice_supplier_source', | ||
| 83 | 'fk_link' => 'fk_invoice_supplier', | ||
| 84 | 'fk_line_link' => 'fk_invoice_supplier_line', | ||
| 85 | 'table_link_line' => 'facture_fourn_det', | ||
| 86 | 'fk_table_link_line' => 'rowid', | ||
| 87 | 'fk_table_link_line_parent' => 'fk_facture_fourn', | ||
| 88 | 'prefix' => 'a', | ||
| 89 | 'is_fk_link_is_also_fk_doc' => true, | ||
| 90 | ), | ||
| 91 | ), | ||
| 92 | ), | ||
| 93 | ); | ||
| 94 | |||
| 95 | /** | ||
| 96 | * letteringThirdparty | ||
| 97 | * | ||
| 98 | * @param int $socid Thirdparty id | ||
| 99 | * @return int 1 OK, <0 error | ||
| 100 | */ | ||
| 101 | public function letteringThirdparty($socid) | ||
| 102 |     { | ||
| 103 | global $conf; | ||
| 104 | |||
| 105 | $error = 0; | ||
| 106 | |||
| 107 | $object = new Societe($this->db); | ||
| 108 | $object->id = $socid; | ||
| 109 | $object->fetch($socid); | ||
| 110 | |||
| 111 | |||
| 112 |         if ($object->code_compta_client == '411CUSTCODE') { | ||
| 113 | $object->code_compta_client = ''; | ||
| 114 | } | ||
| 115 | |||
| 116 |         if ($object->code_compta_fournisseur == '401SUPPCODE') { | ||
| 117 | $object->code_compta_fournisseur = ''; | ||
| 118 | } | ||
| 119 | |||
| 120 | /** | ||
| 121 | * Support for complex lettering with debit, credit transfer | ||
| 122 | */ | ||
| 123 | $sql = "SELECT DISTINCT bk.rowid, bk.doc_date, bk.doc_type, bk.doc_ref, bk.subledger_account, "; | ||
| 124 | $sql .= " bk.numero_compte , bk.label_compte, bk.debit , bk.credit, bk.montant "; | ||
| 125 | $sql .= " , bk.sens , bk.code_journal , bk.piece_num, bk.date_lettering, bu.url_id , bu.type "; | ||
| 126 | $sql .= " FROM " . MAIN_DB_PREFIX . "accounting_bookkeeping as bk"; | ||
| 127 |         $sql .= " LEFT JOIN  " . MAIN_DB_PREFIX . "bank_url as bu ON(bk.fk_doc = bu.fk_bank AND bu.type IN ('payment', 'payment_supplier') ) "; | ||
| 128 | $sql .= " WHERE ( "; | ||
| 129 |         if ($object->code_compta_client != "") { | ||
| 130 | $sql .= " bk.subledger_account = '" . $this->db->escape($object->code_compta_client) . "' "; | ||
| 131 | } | ||
| 132 |         if ($object->code_compta_client != "" && $object->code_compta_fournisseur != "") { | ||
| 133 | $sql .= " OR "; | ||
| 134 | } | ||
| 135 |         if ($object->code_compta_fournisseur != "") { | ||
| 136 | $sql .= " bk.subledger_account = '" . $this->db->escape($object->code_compta_fournisseur) . "' "; | ||
| 137 | } | ||
| 138 | |||
| 139 | $sql .= " ) AND (bk.date_lettering ='' OR bk.date_lettering IS NULL) "; | ||
| 140 | $sql .= " AND (bk.lettering_code != '' OR bk.lettering_code IS NULL) "; | ||
| 141 | $sql .= ' AND bk.date_validated IS NULL '; | ||
| 142 |         $sql .= $this->db->order('bk.doc_date', 'DESC'); | ||
| 143 | |||
| 144 | // echo $sql; | ||
| 145 | // | ||
| 146 | $resql = $this->db->query($sql); | ||
| 147 |         if ($resql) { | ||
| 148 | $num = $this->db->num_rows($resql); | ||
| 149 | |||
| 150 |             while ($obj = $this->db->fetch_object($resql)) { | ||
| 151 | $ids = array(); | ||
| 152 | $ids_fact = array(); | ||
| 153 | |||
| 154 |                 if ($obj->type == 'payment_supplier') { | ||
| 155 | $sql = 'SELECT DISTINCT bk.rowid, facf.ref, facf.ref_supplier, payf.fk_bank, facf.rowid as fact_id'; | ||
| 156 | $sql .= " FROM " . MAIN_DB_PREFIX . "facture_fourn facf "; | ||
| 157 | $sql .= " INNER JOIN " . MAIN_DB_PREFIX . "paiementfourn_facturefourn as payfacf ON payfacf.fk_facturefourn=facf.rowid"; | ||
| 158 | $sql .= " INNER JOIN " . MAIN_DB_PREFIX . "paiementfourn as payf ON payfacf.fk_paiementfourn=payf.rowid"; | ||
| 159 | $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) . "')"; | ||
| 160 | $sql .= " WHERE payfacf.fk_paiementfourn = '" . $this->db->escape($obj->url_id) . "' "; | ||
| 161 | $sql .= " AND facf.entity = " . $conf->entity; | ||
| 162 | $sql .= " AND code_journal IN (SELECT code FROM " . MAIN_DB_PREFIX . "accounting_journal WHERE nature=4 AND entity=" . $conf->entity . ") "; | ||
| 163 | $sql .= " AND ( "; | ||
| 164 |                     if ($object->code_compta_client != "") { | ||
| 165 | $sql .= " bk.subledger_account = '" . $this->db->escape($object->code_compta_client) . "' "; | ||
| 166 | } | ||
| 167 |                     if ($object->code_compta_client != "" && $object->code_compta_fournisseur != "") { | ||
| 168 | $sql .= " OR "; | ||
| 169 | } | ||
| 170 |                     if ($object->code_compta_fournisseur != "") { | ||
| 171 | $sql .= " bk.subledger_account = '" . $this->db->escape($object->code_compta_fournisseur) . "' "; | ||
| 172 | } | ||
| 173 | $sql .= " ) "; | ||
| 174 | |||
| 175 | $resql2 = $this->db->query($sql); | ||
| 176 |                     if ($resql2) { | ||
| 177 |                         while ($obj2 = $this->db->fetch_object($resql2)) { | ||
| 178 | $ids[$obj2->rowid] = $obj2->rowid; | ||
| 179 | $ids_fact[] = $obj2->fact_id; | ||
| 180 | } | ||
| 181 | $this->db->free($resql2); | ||
| 182 |                     } else { | ||
| 183 | $this->errors[] = $this->db->lasterror; | ||
| 184 | return -1; | ||
| 185 | } | ||
| 186 |                     if (count($ids_fact)) { | ||
| 187 | $sql = 'SELECT bk.rowid, facf.ref, facf.ref_supplier '; | ||
| 188 | $sql .= " FROM " . MAIN_DB_PREFIX . "facture_fourn facf "; | ||
| 189 |                         $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)) . "))"; | ||
| 190 | $sql .= " WHERE bk.code_journal IN (SELECT code FROM " . MAIN_DB_PREFIX . "accounting_journal WHERE nature=3 AND entity=" . $conf->entity . ") "; | ||
| 191 | $sql .= " AND facf.entity = " . $conf->entity; | ||
| 192 | $sql .= " AND ( "; | ||
| 193 |                         if ($object->code_compta_client != "") { | ||
| 194 | $sql .= " bk.subledger_account = '" . $this->db->escape($object->code_compta_client) . "' "; | ||
| 195 | } | ||
| 196 |                         if ($object->code_compta_client != "" && $object->code_compta_fournisseur != "") { | ||
| 197 | $sql .= " OR "; | ||
| 198 | } | ||
| 199 |                         if ($object->code_compta_fournisseur != "") { | ||
| 200 | $sql .= " bk.subledger_account = '" . $this->db->escape($object->code_compta_fournisseur) . "' "; | ||
| 201 | } | ||
| 202 | $sql .= ") "; | ||
| 203 | |||
| 204 | $resql2 = $this->db->query($sql); | ||
| 205 |                         if ($resql2) { | ||
| 206 |                             while ($obj2 = $this->db->fetch_object($resql2)) { | ||
| 207 | $ids[$obj2->rowid] = $obj2->rowid; | ||
| 208 | } | ||
| 209 | $this->db->free($resql2); | ||
| 210 |                         } else { | ||
| 211 | $this->errors[] = $this->db->lasterror; | ||
| 212 | return -1; | ||
| 213 | } | ||
| 214 | } | ||
| 215 |                 } elseif ($obj->type == 'payment') { | ||
| 216 | $sql = 'SELECT DISTINCT bk.rowid, fac.ref, fac.ref, pay.fk_bank, fac.rowid as fact_id'; | ||
| 217 | $sql .= " FROM " . MAIN_DB_PREFIX . "facture fac "; | ||
| 218 | $sql .= " INNER JOIN " . MAIN_DB_PREFIX . "paiement_facture as payfac ON payfac.fk_facture=fac.rowid"; | ||
| 219 | $sql .= " INNER JOIN " . MAIN_DB_PREFIX . "paiement as pay ON payfac.fk_paiement=pay.rowid"; | ||
| 220 | $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) . "')"; | ||
| 221 | $sql .= " WHERE payfac.fk_paiement = '" . $this->db->escape($obj->url_id) . "' "; | ||
| 222 | $sql .= " AND bk.code_journal IN (SELECT code FROM " . MAIN_DB_PREFIX . "accounting_journal WHERE nature=4 AND entity=" . $conf->entity . ") "; | ||
| 223 |                     $sql .= " AND fac.entity IN (" . getEntity('invoice', 0) . ")"; // We don't share object for accountancy | ||
| 224 | $sql .= " AND ( "; | ||
| 225 |                     if ($object->code_compta_client != "") { | ||
| 226 | $sql .= " bk.subledger_account = '" . $this->db->escape($object->code_compta_client) . "' "; | ||
| 227 | } | ||
| 228 |                     if ($object->code_compta_client != "" && $object->code_compta_fournisseur != "") { | ||
| 229 | $sql .= " OR "; | ||
| 230 | } | ||
| 231 |                     if ($object->code_compta_fournisseur != "") { | ||
| 232 | $sql .= " bk.subledger_account = '" . $this->db->escape($object->code_compta_fournisseur) . "' "; | ||
| 233 | } | ||
| 234 | $sql .= " )"; | ||
| 235 | |||
| 236 | $resql2 = $this->db->query($sql); | ||
| 237 |                     if ($resql2) { | ||
| 238 |                         while ($obj2 = $this->db->fetch_object($resql2)) { | ||
| 239 | $ids[$obj2->rowid] = $obj2->rowid; | ||
| 240 | $ids_fact[] = $obj2->fact_id; | ||
| 241 | } | ||
| 242 |                     } else { | ||
| 243 | $this->errors[] = $this->db->lasterror; | ||
| 244 | return -1; | ||
| 245 | } | ||
| 246 |                     if (count($ids_fact)) { | ||
| 247 | $sql = 'SELECT bk.rowid, fac.ref, fac.ref_supplier '; | ||
| 248 | $sql .= " FROM " . MAIN_DB_PREFIX . "facture fac "; | ||
| 249 |                         $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)) . "))"; | ||
| 250 | $sql .= " WHERE code_journal IN (SELECT code FROM " . MAIN_DB_PREFIX . "accounting_journal WHERE nature=2 AND entity=" . $conf->entity . ") "; | ||
| 251 |                         $sql .= " AND fac.entity IN (" . getEntity('invoice', 0) . ")"; // We don't share object for accountancy | ||
| 252 | $sql .= " AND ( "; | ||
| 253 |                         if ($object->code_compta_client != "") { | ||
| 254 | $sql .= " bk.subledger_account = '" . $this->db->escape($object->code_compta_client) . "' "; | ||
| 255 | } | ||
| 256 |                         if ($object->code_compta_client != "" && $object->code_compta_fournisseur != "") { | ||
| 257 | $sql .= " OR "; | ||
| 258 | } | ||
| 259 |                         if ($object->code_compta_fournisseur != "") { | ||
| 260 | $sql .= " bk.subledger_account = '" . $this->db->escape($object->code_compta_fournisseur) . "' "; | ||
| 261 | } | ||
| 262 | $sql .= " ) "; | ||
| 263 | |||
| 264 | $resql2 = $this->db->query($sql); | ||
| 265 |                         if ($resql2) { | ||
| 266 |                             while ($obj2 = $this->db->fetch_object($resql2)) { | ||
| 267 | $ids[$obj2->rowid] = $obj2->rowid; | ||
| 268 | } | ||
| 269 | $this->db->free($resql2); | ||
| 270 |                         } else { | ||
| 271 | $this->errors[] = $this->db->lasterror; | ||
| 272 | return -1; | ||
| 273 | } | ||
| 274 | } | ||
| 275 | } | ||
| 276 | |||
| 277 |                 if (count($ids) > 1) { | ||
| 278 | $result = $this->updateLettering($ids); | ||
| 279 | } | ||
| 280 | } | ||
| 281 | $this->db->free($resql); | ||
| 282 | } | ||
| 283 |         if ($error) { | ||
| 284 |             foreach ($this->errors as $errmsg) { | ||
| 285 | dol_syslog(__METHOD__ . ' ' . $errmsg, LOG_ERR); | ||
| 286 | $this->error .= ($this->error ? ', ' . $errmsg : $errmsg); | ||
| 287 | } | ||
| 288 | return -1 * $error; | ||
| 289 |         } else { | ||
| 290 | return 1; | ||
| 291 | } | ||
| 292 | } | ||
| 293 | |||
| 294 | /** | ||
| 295 | * | ||
| 296 | * @param array $ids ids array | ||
| 297 | * @param int $notrigger no trigger | ||
| 298 | * @param bool $partial Partial lettering | ||
| 299 | * @return int | ||
| 300 | */ | ||
| 301 | public function updateLettering($ids = array(), $notrigger = 0, $partial = false) | ||
| 302 |     { | ||
| 303 | $now = dol_now(); | ||
| 304 | $error = 0; | ||
| 305 | $affected_rows = 0; | ||
| 306 | |||
| 307 | // Generate a string with n char 'A' (for manual/auto reconcile) or 'a' (for partial reconcile) where n is ACCOUNTING_LETTERING_NBLETTERS (So 'AA'/'aa', 'AAA'/'aaa', ...) @phan-suppress-next-line PhanParamSuspiciousOrder | ||
| 308 |         $letter = str_pad("", getDolGlobalInt('ACCOUNTING_LETTERING_NBLETTERS', 3), $partial ? 'a' : 'A'); | ||
| 309 | |||
| 310 | $this->db->begin(); | ||
| 311 | |||
| 312 | // Check partial / normal lettering case | ||
| 313 | $sql = "SELECT ab.lettering_code, GROUP_CONCAT(DISTINCT ab.rowid SEPARATOR ',') AS bookkeeping_ids"; | ||
| 314 | $sql .= " FROM " . MAIN_DB_PREFIX . "accounting_bookkeeping AS ab"; | ||
| 315 |         $sql .= " WHERE ab.rowid IN (" . $this->db->sanitize(implode(',', $ids)) . ")"; | ||
| 316 | $sql .= " GROUP BY ab.lettering_code"; | ||
| 317 | $sql .= " ORDER BY ab.lettering_code DESC"; | ||
| 318 | |||
| 319 | dol_syslog(__METHOD__ . " - Check partial / normal lettering case", LOG_DEBUG); | ||
| 320 | $resql = $this->db->query($sql); | ||
| 321 |         if ($resql) { | ||
| 322 |             while ($obj = $this->db->fetch_object($resql)) { | ||
| 323 | if (empty($obj->lettering_code)) continue; | ||
| 324 | |||
| 325 | // Remove normal lettering code if set partial lettering | ||
| 326 |                 if ($partial && preg_match('/^[A-Z]+$/', $obj->lettering_code)) { | ||
| 327 |                     if (!empty($obj->bookkeeping_ids)) $ids = array_diff($ids, explode(',', $obj->bookkeeping_ids)); | ||
| 328 |                 } elseif (!$partial && preg_match('/^[a-z]+$/', $obj->lettering_code)) { | ||
| 329 | // Delete partial lettering code if set normal lettering | ||
| 330 | $sql2 = "UPDATE " . MAIN_DB_PREFIX . "accounting_bookkeeping SET"; | ||
| 331 | $sql2 .= " lettering_code = NULL"; | ||
| 332 | $sql2 .= ", date_lettering = NULL"; | ||
| 333 |                     $sql2 .= " WHERE entity IN (" . getEntity('accountancy') . ")"; | ||
| 334 | $sql2 .= " AND lettering_code = '" . $this->db->escape($obj->lettering_code) . "'"; | ||
| 335 | |||
| 336 | dol_syslog(__METHOD__ . " - Remove partial lettering", LOG_DEBUG); | ||
| 337 | $resql2 = $this->db->query($sql2); | ||
| 338 |                     if (!$resql2) { | ||
| 339 | $this->errors[] = 'Error' . $this->db->lasterror(); | ||
| 340 | $error++; | ||
| 341 | break; | ||
| 342 | } | ||
| 343 | } | ||
| 344 | } | ||
| 345 | $this->db->free($resql); | ||
| 346 |         } else { | ||
| 347 | $this->errors[] = 'Error' . $this->db->lasterror(); | ||
| 348 | $error++; | ||
| 349 | } | ||
| 350 | |||
| 351 |         if (!$error && !empty($ids)) { | ||
| 352 | // Get next code | ||
| 353 | $sql = "SELECT DISTINCT ab2.lettering_code"; | ||
| 354 | $sql .= " FROM " . MAIN_DB_PREFIX . "accounting_bookkeeping AS ab"; | ||
| 355 | $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "accounting_bookkeeping AS ab2 ON ab2.subledger_account = ab.subledger_account"; | ||
| 356 |             $sql .= " WHERE ab.rowid IN (" . $this->db->sanitize(implode(',', $ids)) . ")"; | ||
| 357 | $sql .= " AND ab2.lettering_code != ''"; | ||
| 358 | $sql .= " ORDER BY ab2.lettering_code DESC"; | ||
| 359 | |||
| 360 | dol_syslog(__METHOD__ . " - Get next code", LOG_DEBUG); | ||
| 361 | $resql = $this->db->query($sql); | ||
| 362 |             if ($resql) { | ||
| 363 |                 while ($obj = $this->db->fetch_object($resql)) { | ||
| 364 | if ( | ||
| 365 | !empty($obj->lettering_code) && | ||
| 366 |                         (($partial && preg_match('/^[a-z]+$/', $obj->lettering_code)) || | ||
| 367 |                             (!$partial && preg_match('/^[A-Z]+$/', $obj->lettering_code))) | ||
| 368 |                     ) { | ||
| 369 | $letter = $obj->lettering_code; | ||
| 370 | $letter++; | ||
| 371 | break; | ||
| 372 | } | ||
| 373 | } | ||
| 374 | $this->db->free($resql); | ||
| 375 |             } else { | ||
| 376 | $this->errors[] = 'Error' . $this->db->lasterror(); | ||
| 377 | $error++; | ||
| 378 | } | ||
| 379 | |||
| 380 | // Test amount integrity | ||
| 381 |             if (!$error && !$partial) { | ||
| 382 | $sql = "SELECT SUM(ABS(debit)) as deb, SUM(ABS(credit)) as cred FROM " . MAIN_DB_PREFIX . "accounting_bookkeeping WHERE "; | ||
| 383 |                 $sql .= " rowid IN (" . $this->db->sanitize(implode(',', $ids)) . ") AND lettering_code IS NULL AND subledger_account != ''"; | ||
| 384 | |||
| 385 | dol_syslog(__METHOD__ . " - Test amount integrity", LOG_DEBUG); | ||
| 386 | $resql = $this->db->query($sql); | ||
| 387 |                 if ($resql) { | ||
| 388 |                     if ($obj = $this->db->fetch_object($resql)) { | ||
| 389 |                         if (!(round(abs($obj->deb), 2) === round(abs($obj->cred), 2))) { | ||
| 390 | $this->errors[] = 'Total not exacts ' . round(abs($obj->deb), 2) . ' vs ' . round(abs($obj->cred), 2); | ||
| 391 | $error++; | ||
| 392 | } | ||
| 393 | } | ||
| 394 | $this->db->free($resql); | ||
| 395 |                 } else { | ||
| 396 | $this->errors[] = 'Erreur sql' . $this->db->lasterror(); | ||
| 397 | $error++; | ||
| 398 | } | ||
| 399 | } | ||
| 400 | |||
| 401 | // Update lettering code | ||
| 402 |             if (!$error) { | ||
| 403 | $sql = "UPDATE " . MAIN_DB_PREFIX . "accounting_bookkeeping SET"; | ||
| 404 | $sql .= " lettering_code='" . $this->db->escape($letter) . "'"; | ||
| 405 | $sql .= ", date_lettering = '" . $this->db->idate($now) . "'"; // todo correct date it's false | ||
| 406 |                 $sql .= "  WHERE rowid IN (" . $this->db->sanitize(implode(',', $ids)) . ") AND lettering_code IS NULL AND subledger_account != ''"; | ||
| 407 | |||
| 408 | dol_syslog(__METHOD__ . " - Update lettering code", LOG_DEBUG); | ||
| 409 | $resql = $this->db->query($sql); | ||
| 410 |                 if (!$resql) { | ||
| 411 | $error++; | ||
| 412 | $this->errors[] = "Error " . $this->db->lasterror(); | ||
| 413 |                 } else { | ||
| 414 | $affected_rows = $this->db->affected_rows($resql); | ||
| 415 | } | ||
| 416 | } | ||
| 417 | } | ||
| 418 | |||
| 419 | // Commit or rollback | ||
| 420 |         if ($error) { | ||
| 421 | $this->db->rollback(); | ||
| 422 |             foreach ($this->errors as $errmsg) { | ||
| 423 | dol_syslog(get_only_class($this) . "::update " . $errmsg, LOG_ERR); | ||
| 424 | $this->error .= ($this->error ? ', ' . $errmsg : $errmsg); | ||
| 425 | } | ||
| 426 | return -1 * $error; | ||
| 427 |         } else { | ||
| 428 | $this->db->commit(); | ||
| 429 | return $affected_rows; | ||
| 430 | } | ||
| 431 | } | ||
| 432 | |||
| 433 | /** | ||
| 434 | * | ||
| 435 | * @param array $ids ids array | ||
| 436 | * @param int $notrigger no trigger | ||
| 437 | * @return int | ||
| 438 | */ | ||
| 439 | public function deleteLettering($ids, $notrigger = 0) | ||
| 440 |     { | ||
| 441 | $error = 0; | ||
| 442 | |||
| 443 | $sql = "UPDATE " . MAIN_DB_PREFIX . "accounting_bookkeeping SET"; | ||
| 444 | $sql .= " lettering_code = NULL"; | ||
| 445 | $sql .= ", date_lettering = NULL"; | ||
| 446 |         $sql .= " WHERE rowid IN (" . $this->db->sanitize(implode(',', $ids)) . ")"; | ||
| 447 | $sql .= " AND subledger_account != ''"; | ||
| 448 | |||
| 449 | dol_syslog(get_only_class($this) . "::update", LOG_DEBUG); | ||
| 450 | $resql = $this->db->query($sql); | ||
| 451 |         if (!$resql) { | ||
| 452 | $error++; | ||
| 453 | $this->errors[] = "Error " . $this->db->lasterror(); | ||
| 454 | } | ||
| 455 | |||
| 456 | // Commit or rollback | ||
| 457 |         if ($error) { | ||
| 458 |             foreach ($this->errors as $errmsg) { | ||
| 459 | dol_syslog(get_only_class($this) . "::update " . $errmsg, LOG_ERR); | ||
| 460 | $this->error .= ($this->error ? ', ' . $errmsg : $errmsg); | ||
| 461 | } | ||
| 462 | return -1 * $error; | ||
| 463 |         } else { | ||
| 464 | return $this->db->affected_rows($resql); | ||
| 465 | } | ||
| 466 | } | ||
| 467 | |||
| 468 | /** | ||
| 469 | * Lettering bookkeeping lines all types | ||
| 470 | * | ||
| 471 | * @param array $bookkeeping_ids Lettering specific list of bookkeeping id | ||
| 472 | * @param bool $unlettering Do unlettering | ||
| 473 | * @return int Return integer <0 if error (nb lettered = result -1), 0 if noting to lettering, >0 if OK (nb lettered) | ||
| 474 | */ | ||
| 475 | public function bookkeepingLetteringAll($bookkeeping_ids, $unlettering = false) | ||
| 497 | } | ||
| 498 | } | ||
| 499 | |||
| 500 | /** | ||
| 501 | * Lettering bookkeeping lines | ||
| 502 | * | ||
| 503 | * @param array $bookkeeping_ids Lettering specific list of bookkeeping id | ||
| 504 | * @param bool $unlettering Do unlettering | ||
| 505 | * @return int Return integer <0 if error (nb lettered = result -1), 0 if noting to lettering, >0 if OK (nb lettered) | ||
| 506 | */ | ||
| 507 | public function bookkeepingLettering($bookkeeping_ids, $unlettering = false) | ||
| 508 |     { | ||
| 509 | global $langs; | ||
| 510 | |||
| 511 | $this->errors = array(); | ||
| 512 | |||
| 513 | // Clean parameters | ||
| 514 | $bookkeeping_ids = is_array($bookkeeping_ids) ? $bookkeeping_ids : array(); | ||
| 515 | |||
| 516 | $error = 0; | ||
| 517 | $nb_lettering = 0; | ||
| 518 | $grouped_lines = $this->getLinkedLines($bookkeeping_ids); | ||
| 519 |         if (!is_array($grouped_lines)) { | ||
| 520 | return -2; | ||
| 521 | } | ||
| 522 | |||
| 523 |         foreach ($grouped_lines as $lines) { | ||
| 524 | $group_error = 0; | ||
| 525 | $total = 0; | ||
| 526 | $do_it = !$unlettering; | ||
| 527 | $lettering_code = null; | ||
| 528 | $piece_num_lines = array(); | ||
| 529 | $bookkeeping_lines = array(); | ||
| 530 |             foreach ($lines as $line_infos) { | ||
| 531 | $bookkeeping_lines[$line_infos['id']] = $line_infos['id']; | ||
| 532 | $piece_num_lines[$line_infos['piece_num']] = $line_infos['piece_num']; | ||
| 533 | $total += ($line_infos['credit'] > 0 ? $line_infos['credit'] : -$line_infos['debit']); | ||
| 534 | |||
| 535 | // Check lettering code | ||
| 536 |                 if ($unlettering) { | ||
| 537 |                     if (isset($lettering_code) && $lettering_code != $line_infos['lettering_code']) { | ||
| 538 |                         $this->errors[] = $langs->trans('AccountancyErrorMismatchLetteringCode'); | ||
| 539 | $group_error++; | ||
| 540 | break; | ||
| 541 | } | ||
| 542 |                     if (!isset($lettering_code)) { | ||
| 543 | $lettering_code = (string) $line_infos['lettering_code']; | ||
| 544 | } | ||
| 545 |                     if (!empty($line_infos['lettering_code'])) { | ||
| 546 | $do_it = true; | ||
| 547 | } | ||
| 548 |                 } elseif (!empty($line_infos['lettering_code'])) { | ||
| 549 | $do_it = false; | ||
| 550 | } | ||
| 551 | } | ||
| 552 | |||
| 553 | // Check balance amount | ||
| 554 |             if (!$group_error && !$unlettering && price2num($total) != 0) { | ||
| 555 |                 $this->errors[] = $langs->trans('AccountancyErrorMismatchBalanceAmount', $total); | ||
| 556 | $group_error++; | ||
| 557 | } | ||
| 558 | |||
| 559 | // Lettering/Unlettering the group of bookkeeping lines | ||
| 560 |             if (!$group_error && $do_it) { | ||
| 561 |                 if ($unlettering) { | ||
| 562 | $result = $this->deleteLettering($bookkeeping_lines); | ||
| 563 |                 } else { | ||
| 564 | $result = $this->updateLettering($bookkeeping_lines); | ||
| 565 | } | ||
| 566 |                 if ($result < 0) { | ||
| 567 | $group_error++; | ||
| 568 |                 } elseif ($result > 0) { | ||
| 569 | $nb_lettering++; | ||
| 570 | } | ||
| 571 | } | ||
| 572 | |||
| 573 |             if ($group_error) { | ||
| 574 |                 $this->errors[] = $langs->trans('AccountancyErrorLetteringBookkeeping', implode(', ', $piece_num_lines)); | ||
| 575 | $error++; | ||
| 576 | } | ||
| 577 | } | ||
| 578 | |||
| 579 |         if ($error) { | ||
| 580 | return -2 - $nb_lettering; | ||
| 581 |         } else { | ||
| 582 | return $nb_lettering; | ||
| 583 | } | ||
| 584 | } | ||
| 585 | |||
| 586 | /** | ||
| 587 | * Lettering bookkeeping lines | ||
| 588 | * | ||
| 589 | * @param array $bookkeeping_ids Lettering specific list of bookkeeping id | ||
| 590 | * @param bool $only_has_subledger_account Get only lines who have subledger account | ||
| 591 | * @return array|int Return integer <0 if error otherwise all linked lines by block | ||
| 592 | */ | ||
| 593 | public function getLinkedLines($bookkeeping_ids, $only_has_subledger_account = true) | ||
| 594 |     { | ||
| 595 | global $conf, $langs; | ||
| 596 | $this->errors = array(); | ||
| 597 | |||
| 598 | // Clean parameters | ||
| 599 | $bookkeeping_ids = is_array($bookkeeping_ids) ? $bookkeeping_ids : array(); | ||
| 600 | |||
| 601 | // Get all bookkeeping lines | ||
| 602 | $sql = "SELECT DISTINCT ab.doc_type, ab.fk_doc"; | ||
| 603 | $sql .= " FROM " . MAIN_DB_PREFIX . "accounting_bookkeeping AS ab"; | ||
| 604 |         $sql .= " WHERE ab.entity IN (" . getEntity('accountancy') . ")"; | ||
| 605 | $sql .= " AND ab.fk_doc > 0"; | ||
| 606 |         if (!empty($bookkeeping_ids)) { | ||
| 607 | // Get all bookkeeping lines of piece number | ||
| 608 |             $sql .= " AND EXISTS ("; | ||
| 609 | $sql .= " SELECT rowid"; | ||
| 610 | $sql .= " FROM " . MAIN_DB_PREFIX . "accounting_bookkeeping AS pn"; | ||
| 611 |             $sql .= "  WHERE pn.entity IN (" . getEntity('accountancy') . ")"; | ||
| 612 |             $sql .= "  AND pn.rowid IN (" . $this->db->sanitize(implode(',', $bookkeeping_ids)) . ")"; | ||
| 613 | $sql .= " AND pn.piece_num = ab.piece_num"; | ||
| 614 | $sql .= " )"; | ||
| 615 | } | ||
| 616 |         if ($only_has_subledger_account) { | ||
| 617 | $sql .= " AND ab.subledger_account != ''"; | ||
| 618 | } | ||
| 619 | |||
| 620 | dol_syslog(__METHOD__ . " - Get all bookkeeping lines", LOG_DEBUG); | ||
| 621 | $resql = $this->db->query($sql); | ||
| 622 |         if (!$resql) { | ||
| 623 | $this->errors[] = "Error " . $this->db->lasterror(); | ||
| 624 | return -1; | ||
| 625 | } | ||
| 626 | |||
| 627 | $bookkeeping_lines_by_type = array(); | ||
| 628 |         while ($obj = $this->db->fetch_object($resql)) { | ||
| 629 | $bookkeeping_lines_by_type[$obj->doc_type][$obj->fk_doc] = $obj->fk_doc; | ||
| 630 | } | ||
| 631 | $this->db->free($resql); | ||
| 632 | |||
| 633 |         if (empty($bookkeeping_lines_by_type)) { | ||
| 634 | return array(); | ||
| 635 | } | ||
| 636 | |||
| 637 |         if (!empty($bookkeeping_lines_by_type['bank'])) { | ||
| 638 | $new_bookkeeping_lines_by_type = $this->getDocTypeAndFkDocFromBankLines($bookkeeping_lines_by_type['bank']); | ||
| 639 |             if (!is_array($new_bookkeeping_lines_by_type)) { | ||
| 640 | return -1; | ||
| 641 | } | ||
| 642 |             foreach ($new_bookkeeping_lines_by_type as $doc_type => $fk_docs) { | ||
| 643 |                 foreach ($fk_docs as $fk_doc) { | ||
| 644 | $bookkeeping_lines_by_type[$doc_type][$fk_doc] = $fk_doc; | ||
| 645 | } | ||
| 646 | } | ||
| 647 | } | ||
| 648 | |||
| 649 | $grouped_lines = array(); | ||
| 650 |         foreach (self::$doc_type_infos as $doc_type => $doc_type_info) { | ||
| 651 |             if (!is_array($bookkeeping_lines_by_type[$doc_type])) { | ||
| 652 | continue; | ||
| 653 | } | ||
| 654 | |||
| 655 | // Get all document ids grouped | ||
| 656 | $doc_grouped = $this->getLinkedDocumentByGroup($bookkeeping_lines_by_type[$doc_type], $doc_type); | ||
| 657 |             if (!is_array($doc_grouped)) { | ||
| 658 | return -1; | ||
| 659 | } | ||
| 660 | |||
| 661 | // Group all lines by document/piece number | ||
| 662 |             foreach ($doc_grouped as $doc_ids) { | ||
| 663 | $bank_ids = $this->getBankLinesFromFkDocAndDocType($doc_ids, $doc_type); | ||
| 664 |                 if (!is_array($bank_ids)) { | ||
| 665 | return -1; | ||
| 666 | } | ||
| 667 | |||
| 668 | // Get all bookkeeping lines linked | ||
| 669 | $sql = "SELECT DISTINCT ab.rowid, ab.piece_num, ab.debit, ab.credit, ab.lettering_code"; | ||
| 670 | $sql .= " FROM " . MAIN_DB_PREFIX . "accounting_bookkeeping AS ab"; | ||
| 671 |                 $sql .= " WHERE ab.entity IN (" . getEntity('accountancy') . ")"; | ||
| 672 |                 $sql .= " AND ("; | ||
| 673 |                 if (!empty($bank_ids)) { | ||
| 674 |                     $sql .= " EXISTS ("; | ||
| 675 | $sql .= " SELECT bpn.rowid"; | ||
| 676 | $sql .= " FROM " . MAIN_DB_PREFIX . "accounting_bookkeeping AS bpn"; | ||
| 677 |                     $sql .= "  WHERE bpn.entity IN (" . getEntity('accountancy') . ")"; | ||
| 678 | $sql .= " AND bpn.doc_type = 'bank'"; | ||
| 679 |                     $sql .= "  AND bpn.fk_doc IN (" . $this->db->sanitize(implode(',', $bank_ids)) . ")"; | ||
| 680 | $sql .= " AND bpn.piece_num = ab.piece_num"; | ||
| 681 | $sql .= " ) OR "; | ||
| 682 | } | ||
| 683 |                 $sql .= " EXISTS ("; | ||
| 684 | $sql .= " SELECT dpn.rowid"; | ||
| 685 | $sql .= " FROM " . MAIN_DB_PREFIX . "accounting_bookkeeping AS dpn"; | ||
| 686 |                 $sql .= "  WHERE dpn.entity IN (" . getEntity('accountancy') . ")"; | ||
| 687 | $sql .= " AND dpn.doc_type = '" . $this->db->escape($doc_type) . "'"; | ||
| 688 |                 $sql .= "  AND dpn.fk_doc IN (" . $this->db->sanitize(implode(',', $doc_ids)) . ")"; | ||
| 689 | $sql .= " AND dpn.piece_num = ab.piece_num"; | ||
| 690 | $sql .= " )"; | ||
| 691 | $sql .= ")"; | ||
| 692 |                 if ($only_has_subledger_account) { | ||
| 693 | $sql .= " AND ab.subledger_account != ''"; | ||
| 694 | } | ||
| 695 | |||
| 696 | dol_syslog(__METHOD__ . " - Get all bookkeeping lines linked", LOG_DEBUG); | ||
| 697 | $resql = $this->db->query($sql); | ||
| 698 |                 if (!$resql) { | ||
| 699 | $this->errors[] = "Error " . $this->db->lasterror(); | ||
| 700 | return -1; | ||
| 701 | } | ||
| 702 | |||
| 703 | $group = array(); | ||
| 704 |                 while ($obj = $this->db->fetch_object($resql)) { | ||
| 705 | $group[$obj->rowid] = array( | ||
| 706 | 'id' => $obj->rowid, | ||
| 707 | 'piece_num' => $obj->piece_num, | ||
| 708 | 'debit' => $obj->debit, | ||
| 709 | 'credit' => $obj->credit, | ||
| 710 | 'lettering_code' => $obj->lettering_code, | ||
| 711 | ); | ||
| 712 | } | ||
| 713 | $this->db->free($resql); | ||
| 714 | |||
| 715 |                 if (!empty($group)) { | ||
| 716 | $grouped_lines[] = $group; | ||
| 717 | } | ||
| 718 | } | ||
| 719 | } | ||
| 720 | |||
| 721 | return $grouped_lines; | ||
| 722 | } | ||
| 723 | |||
| 724 | /** | ||
| 725 | * Get all fk_doc by doc_type from list of bank ids | ||
| 726 | * | ||
| 727 | * @param array $bank_ids List of bank ids | ||
| 728 | * @return array|int Return integer <0 if error otherwise all fk_doc by doc_type | ||
| 729 | */ | ||
| 730 | public function getDocTypeAndFkDocFromBankLines($bank_ids) | ||
| 731 |     { | ||
| 732 | dol_syslog(__METHOD__ . " - bank_ids=" . json_encode($bank_ids), LOG_DEBUG); | ||
| 733 | |||
| 734 | // Clean parameters | ||
| 735 | $bank_ids = is_array($bank_ids) ? $bank_ids : array(); | ||
| 736 | |||
| 737 |         if (empty($bank_ids)) { | ||
| 738 | return array(); | ||
| 739 | } | ||
| 740 | |||
| 741 | $bookkeeping_lines_by_type = array(); | ||
| 742 |         foreach (self::$doc_type_infos as $doc_type => $doc_type_info) { | ||
| 743 | // Get all fk_doc by doc_type from bank ids | ||
| 744 | $sql = "SELECT DISTINCT dp." . $this->db->sanitize($doc_type_info['doc_payment_table_fk_doc']) . " AS fk_doc"; | ||
| 745 | $sql .= " FROM " . MAIN_DB_PREFIX . $this->db->sanitize($doc_type_info['payment_table']) . " AS p"; | ||
| 746 | $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . $this->db->sanitize($doc_type_info['doc_payment_table']) . " AS dp ON dp." . $this->db->sanitize($doc_type_info['doc_payment_table_fk_payment']) . " = p.rowid"; | ||
| 747 |             $sql .= " WHERE p." . $this->db->sanitize($doc_type_info['payment_table_fk_bank']) . " IN (" . $this->db->sanitize(implode(',', $bank_ids)) . ")"; | ||
| 748 | $sql .= " AND dp." . $this->db->sanitize($doc_type_info['doc_payment_table_fk_doc']) . " > 0"; | ||
| 749 | |||
| 750 | dol_syslog(__METHOD__ . " - Get all fk_doc by doc_type from list of bank ids for '" . $doc_type . "'", LOG_DEBUG); | ||
| 751 | $resql = $this->db->query($sql); | ||
| 752 |             if (!$resql) { | ||
| 753 | $this->errors[] = "Error " . $this->db->lasterror(); | ||
| 754 | return -1; | ||
| 755 | } | ||
| 756 | |||
| 757 |             while ($obj = $this->db->fetch_object($resql)) { | ||
| 758 | $bookkeeping_lines_by_type[$doc_type][$obj->fk_doc] = $obj->fk_doc; | ||
| 759 | } | ||
| 760 | $this->db->free($resql); | ||
| 761 | } | ||
| 762 | |||
| 763 | return $bookkeeping_lines_by_type; | ||
| 764 | } | ||
| 765 | |||
| 766 | /** | ||
| 767 | * Get all bank ids from list of document ids of a type | ||
| 768 | * | ||
| 769 | * @param array $document_ids List of document id | ||
| 770 |      * @param   string          $doc_type       Type of document ('customer_invoice' or 'supplier_invoice', ...) | ||
| 771 | * @return array|int Return integer <0 if error otherwise all all bank ids from list of document ids of a type | ||
| 772 | */ | ||
| 773 | public function getBankLinesFromFkDocAndDocType($document_ids, $doc_type) | ||
| 774 |     { | ||
| 775 | global $langs; | ||
| 776 | |||
| 777 | dol_syslog(__METHOD__ . " - bank_ids=" . json_encode($document_ids) . ", doc_type=$doc_type", LOG_DEBUG); | ||
| 778 | |||
| 779 | // Clean parameters | ||
| 780 | $document_ids = is_array($document_ids) ? $document_ids : array(); | ||
| 781 | //remove empty entries | ||
| 782 | $document_ids = array_filter($document_ids); | ||
| 783 | |||
| 784 | $doc_type = trim($doc_type); | ||
| 785 | |||
| 786 |         if (empty($document_ids)) { | ||
| 787 | return array(); | ||
| 788 | } | ||
| 789 |         if (!is_array(self::$doc_type_infos[$doc_type])) { | ||
| 790 |             $langs->load('errors'); | ||
| 791 |             $this->errors[] = $langs->trans('ErrorBadParameters'); | ||
| 792 | return -1; | ||
| 793 | } | ||
| 794 | |||
| 795 | $doc_type_info = self::$doc_type_infos[$doc_type]; | ||
| 796 | $bank_ids = array(); | ||
| 797 | |||
| 798 | // Get all fk_doc by doc_type from bank ids | ||
| 799 | $sql = "SELECT DISTINCT p." . $this->db->sanitize($doc_type_info['payment_table_fk_bank']) . " AS fk_doc"; | ||
| 800 | $sql .= " FROM " . MAIN_DB_PREFIX . $this->db->sanitize($doc_type_info['payment_table']) . " AS p"; | ||
| 801 | $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . $this->db->sanitize($doc_type_info['doc_payment_table']) . " AS dp ON dp." . $this->db->sanitize($doc_type_info['doc_payment_table_fk_payment']) . " = p.rowid"; | ||
| 802 |         $sql .= " WHERE dp." . $this->db->sanitize($doc_type_info['doc_payment_table_fk_doc']) . " IN (" . $this->db->sanitize(implode(',', $document_ids)) . ")"; | ||
| 803 | $sql .= " AND p." . $this->db->sanitize($doc_type_info['payment_table_fk_bank']) . " > 0"; | ||
| 804 | |||
| 805 | dol_syslog(__METHOD__ . " - Get all bank ids from list of document ids of a type '" . $doc_type . "'", LOG_DEBUG); | ||
| 806 | $resql = $this->db->query($sql); | ||
| 807 |         if (!$resql) { | ||
| 808 | $this->errors[] = "Error " . $this->db->lasterror(); | ||
| 809 | return -1; | ||
| 810 | } | ||
| 811 | |||
| 812 |         while ($obj = $this->db->fetch_object($resql)) { | ||
| 813 | $bank_ids[$obj->fk_doc] = $obj->fk_doc; | ||
| 814 | } | ||
| 815 | $this->db->free($resql); | ||
| 816 | |||
| 817 | return $bank_ids; | ||
| 818 | } | ||
| 819 | |||
| 820 | /** | ||
| 821 | * Get all linked document ids by group and type | ||
| 822 | * | ||
| 823 | * @param array $document_ids List of document id | ||
| 824 |      * @param   string          $doc_type       Type of document ('customer_invoice' or 'supplier_invoice', ...) | ||
| 825 | * @return array|int Return integer <0 if error otherwise all linked document ids by group and type [ [ 'doc_type' => [ doc_id, ... ], ... ], ... ] | ||
| 826 | */ | ||
| 827 | public function getLinkedDocumentByGroup($document_ids, $doc_type) | ||
| 906 | } | ||
| 907 | |||
| 908 | /** | ||
| 909 | * Get element ids grouped by link or element in common | ||
| 910 | * | ||
| 911 | * @param array $link_by_element List of payment ids by link key | ||
| 912 | * @param array $element_by_link List of element ids by link key | ||
| 913 | * @param string $link_key Link key (used for recursive function) | ||
| 914 | * @param array $current_group Current group (used for recursive function) | ||
| 915 | * @return array List of element ids grouped by link or element in common | ||
| 916 | */ | ||
| 917 | public function getGroupElements(&$link_by_element, &$element_by_link, $link_key = '', &$current_group = array()) | ||
| 970 | } | ||
| 971 | } | ||
| 972 |