| Total Complexity | 118 |
| Total Lines | 711 |
| Duplicated Lines | 0 % |
| Changes | 5 | ||
| Bugs | 0 | Features | 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 |
||
| 34 | class Lettering extends BookKeeping |
||
| 35 | { |
||
| 36 | /** |
||
| 37 | * @var BookKeeping[] Bookkeeping cached |
||
| 38 | */ |
||
| 39 | public static $bookkeeping_cached = array(); |
||
| 40 | |||
| 41 | |||
| 42 | /** |
||
| 43 | * letteringThirdparty |
||
| 44 | * |
||
| 45 | * @param int $socid Thirdparty id |
||
| 46 | * @return int 1 OK, <0 error |
||
| 47 | */ |
||
| 48 | public function letteringThirdparty($socid) |
||
| 49 | { |
||
| 50 | global $conf; |
||
| 51 | |||
| 52 | $error = 0; |
||
| 53 | |||
| 54 | $object = new Societe($this->db); |
||
| 55 | $object->id = $socid; |
||
| 56 | $object->fetch($socid); |
||
| 57 | |||
| 58 | |||
| 59 | if ($object->code_compta == '411CUSTCODE') { |
||
| 60 | $object->code_compta = ''; |
||
| 61 | } |
||
| 62 | |||
| 63 | if ($object->code_compta_fournisseur == '401SUPPCODE') { |
||
| 64 | $object->code_compta_fournisseur = ''; |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Prise en charge des lettering complexe avec prelevment , virement |
||
| 69 | */ |
||
| 70 | $sql = "SELECT DISTINCT bk.rowid, bk.doc_date, bk.doc_type, bk.doc_ref, bk.subledger_account, "; |
||
| 71 | $sql .= " bk.numero_compte , bk.label_compte, bk.debit , bk.credit, bk.montant "; |
||
| 72 | $sql .= " , bk.sens , bk.code_journal , bk.piece_num, bk.date_lettering, bu.url_id , bu.type "; |
||
| 73 | $sql .= " FROM ".MAIN_DB_PREFIX."accounting_bookkeeping as bk"; |
||
| 74 | $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."bank_url as bu ON(bk.fk_doc = bu.fk_bank AND bu.type IN ('payment', 'payment_supplier') ) "; |
||
| 75 | $sql .= " WHERE ( "; |
||
| 76 | if ($object->code_compta != "") { |
||
| 77 | $sql .= " bk.subledger_account = '".$this->db->escape($object->code_compta)."' "; |
||
| 78 | } |
||
| 79 | if ($object->code_compta != "" && $object->code_compta_fournisseur != "") { |
||
| 80 | $sql .= " OR "; |
||
| 81 | } |
||
| 82 | if ($object->code_compta_fournisseur != "") { |
||
| 83 | $sql .= " bk.subledger_account = '".$this->db->escape($object->code_compta_fournisseur)."' "; |
||
| 84 | } |
||
| 85 | |||
| 86 | $sql .= " ) AND (bk.date_lettering ='' OR bk.date_lettering IS NULL) "; |
||
| 87 | $sql .= " AND (bk.lettering_code != '' OR bk.lettering_code IS NULL) "; |
||
| 88 | $sql .= ' AND bk.date_validated IS NULL '; |
||
| 89 | $sql .= $this->db->order('bk.doc_date', 'DESC'); |
||
| 90 | |||
| 91 | // echo $sql; |
||
| 92 | // |
||
| 93 | $resql = $this->db->query($sql); |
||
| 94 | if ($resql) { |
||
| 95 | $num = $this->db->num_rows($resql); |
||
| 96 | |||
| 97 | while ($obj = $this->db->fetch_object($resql)) { |
||
| 98 | $ids = array(); |
||
| 99 | $ids_fact = array(); |
||
| 100 | |||
| 101 | if ($obj->type == 'payment_supplier') { |
||
| 102 | $sql = 'SELECT DISTINCT bk.rowid, facf.ref, facf.ref_supplier, payf.fk_bank, facf.rowid as fact_id'; |
||
| 103 | $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn facf "; |
||
| 104 | $sql .= " INNER JOIN ".MAIN_DB_PREFIX."paiementfourn_facturefourn as payfacf ON payfacf.fk_facturefourn=facf.rowid"; |
||
| 105 | $sql .= " INNER JOIN ".MAIN_DB_PREFIX."paiementfourn as payf ON payfacf.fk_paiementfourn=payf.rowid"; |
||
| 106 | $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)."')"; |
||
| 107 | $sql .= " WHERE payfacf.fk_paiementfourn = '".$this->db->escape($obj->url_id)."' "; |
||
| 108 | $sql .= " AND facf.entity = ".$conf->entity; |
||
| 109 | $sql .= " AND code_journal IN (SELECT code FROM ".MAIN_DB_PREFIX."accounting_journal WHERE nature=4 AND entity=".$conf->entity.") "; |
||
| 110 | $sql .= " AND ( "; |
||
| 111 | if ($object->code_compta != "") { |
||
| 112 | $sql .= " bk.subledger_account = '".$this->db->escape($object->code_compta)."' "; |
||
| 113 | } |
||
| 114 | if ($object->code_compta != "" && $object->code_compta_fournisseur != "") { |
||
| 115 | $sql .= " OR "; |
||
| 116 | } |
||
| 117 | if ($object->code_compta_fournisseur != "") { |
||
| 118 | $sql .= " bk.subledger_account = '".$this->db->escape($object->code_compta_fournisseur)."' "; |
||
| 119 | } |
||
| 120 | $sql .= " ) "; |
||
| 121 | |||
| 122 | $resql2 = $this->db->query($sql); |
||
| 123 | if ($resql2) { |
||
| 124 | while ($obj2 = $this->db->fetch_object($resql2)) { |
||
| 125 | $ids[$obj2->rowid] = $obj2->rowid; |
||
| 126 | $ids_fact[] = $obj2->fact_id; |
||
| 127 | } |
||
| 128 | $this->db->free($resql2); |
||
| 129 | } else { |
||
| 130 | $this->errors[] = $this->db->lasterror; |
||
| 131 | return -1; |
||
| 132 | } |
||
| 133 | if (count($ids_fact)) { |
||
| 134 | $sql = 'SELECT bk.rowid, facf.ref, facf.ref_supplier '; |
||
| 135 | $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn facf "; |
||
| 136 | $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))."))"; |
||
| 137 | $sql .= " WHERE bk.code_journal IN (SELECT code FROM ".MAIN_DB_PREFIX."accounting_journal WHERE nature=3 AND entity=".$conf->entity.") "; |
||
| 138 | $sql .= " AND facf.entity = ".$conf->entity; |
||
| 139 | $sql .= " AND ( "; |
||
| 140 | if ($object->code_compta != "") { |
||
| 141 | $sql .= " bk.subledger_account = '".$this->db->escape($object->code_compta)."' "; |
||
| 142 | } |
||
| 143 | if ($object->code_compta != "" && $object->code_compta_fournisseur != "") { |
||
| 144 | $sql .= " OR "; |
||
| 145 | } |
||
| 146 | if ($object->code_compta_fournisseur != "") { |
||
| 147 | $sql .= " bk.subledger_account = '".$this->db->escape($object->code_compta_fournisseur)."' "; |
||
| 148 | } |
||
| 149 | $sql .= ") "; |
||
| 150 | |||
| 151 | $resql2 = $this->db->query($sql); |
||
| 152 | if ($resql2) { |
||
| 153 | while ($obj2 = $this->db->fetch_object($resql2)) { |
||
| 154 | $ids[$obj2->rowid] = $obj2->rowid; |
||
| 155 | } |
||
| 156 | $this->db->free($resql2); |
||
| 157 | } else { |
||
| 158 | $this->errors[] = $this->db->lasterror; |
||
| 159 | return -1; |
||
| 160 | } |
||
| 161 | } |
||
| 162 | } elseif ($obj->type == 'payment') { |
||
| 163 | $sql = 'SELECT DISTINCT bk.rowid, fac.ref, fac.ref, pay.fk_bank, fac.rowid as fact_id'; |
||
| 164 | $sql .= " FROM ".MAIN_DB_PREFIX."facture fac "; |
||
| 165 | $sql .= " INNER JOIN ".MAIN_DB_PREFIX."paiement_facture as payfac ON payfac.fk_facture=fac.rowid"; |
||
| 166 | $sql .= " INNER JOIN ".MAIN_DB_PREFIX."paiement as pay ON payfac.fk_paiement=pay.rowid"; |
||
| 167 | $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)."')"; |
||
| 168 | $sql .= " WHERE payfac.fk_paiement = '".$this->db->escape($obj->url_id)."' "; |
||
| 169 | $sql .= " AND bk.code_journal IN (SELECT code FROM ".MAIN_DB_PREFIX."accounting_journal WHERE nature=4 AND entity=".$conf->entity.") "; |
||
| 170 | $sql .= " AND fac.entity IN (".getEntity('invoice', 0).")"; // We don't share object for accountancy |
||
| 171 | $sql .= " AND ( "; |
||
| 172 | if ($object->code_compta != "") { |
||
| 173 | $sql .= " bk.subledger_account = '".$this->db->escape($object->code_compta)."' "; |
||
| 174 | } |
||
| 175 | if ($object->code_compta != "" && $object->code_compta_fournisseur != "") { |
||
| 176 | $sql .= " OR "; |
||
| 177 | } |
||
| 178 | if ($object->code_compta_fournisseur != "") { |
||
| 179 | $sql .= " bk.subledger_account = '".$this->db->escape($object->code_compta_fournisseur)."' "; |
||
| 180 | } |
||
| 181 | $sql .= " )"; |
||
| 182 | |||
| 183 | $resql2 = $this->db->query($sql); |
||
| 184 | if ($resql2) { |
||
| 185 | while ($obj2 = $this->db->fetch_object($resql2)) { |
||
| 186 | $ids[$obj2->rowid] = $obj2->rowid; |
||
| 187 | $ids_fact[] = $obj2->fact_id; |
||
| 188 | } |
||
| 189 | } else { |
||
| 190 | $this->errors[] = $this->db->lasterror; |
||
| 191 | return -1; |
||
| 192 | } |
||
| 193 | if (count($ids_fact)) { |
||
| 194 | $sql = 'SELECT bk.rowid, fac.ref, fac.ref_supplier '; |
||
| 195 | $sql .= " FROM ".MAIN_DB_PREFIX."facture fac "; |
||
| 196 | $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))."))"; |
||
| 197 | $sql .= " WHERE code_journal IN (SELECT code FROM ".MAIN_DB_PREFIX."accounting_journal WHERE nature=2 AND entity=".$conf->entity.") "; |
||
| 198 | $sql .= " AND fac.entity IN (".getEntity('invoice', 0).")"; // We don't share object for accountancy |
||
| 199 | $sql .= " AND ( "; |
||
| 200 | if ($object->code_compta != "") { |
||
| 201 | $sql .= " bk.subledger_account = '".$this->db->escape($object->code_compta)."' "; |
||
| 202 | } |
||
| 203 | if ($object->code_compta != "" && $object->code_compta_fournisseur != "") { |
||
| 204 | $sql .= " OR "; |
||
| 205 | } |
||
| 206 | if ($object->code_compta_fournisseur != "") { |
||
| 207 | $sql .= " bk.subledger_account = '".$this->db->escape($object->code_compta_fournisseur)."' "; |
||
| 208 | } |
||
| 209 | $sql .= " ) "; |
||
| 210 | |||
| 211 | $resql2 = $this->db->query($sql); |
||
| 212 | if ($resql2) { |
||
| 213 | while ($obj2 = $this->db->fetch_object($resql2)) { |
||
| 214 | $ids[$obj2->rowid] = $obj2->rowid; |
||
| 215 | } |
||
| 216 | $this->db->free($resql2); |
||
| 217 | } else { |
||
| 218 | $this->errors[] = $this->db->lasterror; |
||
| 219 | return -1; |
||
| 220 | } |
||
| 221 | } |
||
| 222 | } |
||
| 223 | |||
| 224 | if (count($ids) > 1) { |
||
| 225 | $result = $this->updateLettering($ids); |
||
| 226 | } |
||
| 227 | } |
||
| 228 | $this->db->free($resql); |
||
| 229 | } |
||
| 230 | if ($error) { |
||
| 231 | foreach ($this->errors as $errmsg) { |
||
| 232 | dol_syslog(__METHOD__.' '.$errmsg, LOG_ERR); |
||
| 233 | $this->error .= ($this->error ? ', '.$errmsg : $errmsg); |
||
| 234 | } |
||
| 235 | return -1 * $error; |
||
| 236 | } else { |
||
| 237 | return 1; |
||
| 238 | } |
||
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * |
||
| 243 | * @param array $ids ids array |
||
| 244 | * @param boolean $notrigger no trigger |
||
| 245 | * @return int |
||
| 246 | */ |
||
| 247 | public function updateLettering($ids = array(), $notrigger = false) |
||
| 248 | { |
||
| 249 | $error = 0; |
||
| 250 | $lettre = 'AAA'; |
||
| 251 | |||
| 252 | $sql = "SELECT DISTINCT ab2.lettering_code"; |
||
| 253 | $sql .= " FROM " . MAIN_DB_PREFIX . "accounting_bookkeeping As ab"; |
||
| 254 | $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "bank_url AS bu ON bu.fk_bank = ab.fk_doc"; |
||
| 255 | $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "bank_url AS bu2 ON bu2.url_id = bu.url_id"; |
||
| 256 | $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "accounting_bookkeeping AS ab2 ON ab2.fk_doc = bu2.fk_bank"; |
||
| 257 | $sql .= " WHERE ab.rowid IN (" . $this->db->sanitize(implode(',', $ids)) . ")"; |
||
| 258 | $sql .= " AND ab.doc_type = 'bank'"; |
||
| 259 | $sql .= " AND ab2.doc_type = 'bank'"; |
||
| 260 | $sql .= " AND bu.type = 'company'"; |
||
| 261 | $sql .= " AND bu2.type = 'company'"; |
||
| 262 | $sql .= " AND ab.subledger_account != ''"; |
||
| 263 | $sql .= " AND ab2.subledger_account != ''"; |
||
| 264 | $sql .= " AND ab.lettering_code IS NULL"; |
||
| 265 | $sql .= " AND ab2.lettering_code != ''"; |
||
| 266 | $sql .= " ORDER BY ab2.lettering_code DESC"; |
||
| 267 | $sql .= " LIMIT 1 "; |
||
| 268 | |||
| 269 | $result = $this->db->query($sql); |
||
| 270 | if ($result) { |
||
| 271 | $obj = $this->db->fetch_object($result); |
||
| 272 | $lettre = (empty($obj->lettering_code) ? 'AAA' : $obj->lettering_code); |
||
| 273 | if (!empty($obj->lettering_code)) { |
||
| 274 | $lettre++; |
||
| 275 | } |
||
| 276 | $this->db->free($result); |
||
| 277 | } else { |
||
| 278 | $this->errors[] = 'Error'.$this->db->lasterror(); |
||
| 279 | $error++; |
||
| 280 | } |
||
| 281 | |||
| 282 | $sql = "SELECT SUM(ABS(debit)) as deb, SUM(ABS(credit)) as cred FROM ".MAIN_DB_PREFIX."accounting_bookkeeping WHERE "; |
||
| 283 | $sql .= " rowid IN (".$this->db->sanitize(implode(',', $ids)).") AND lettering_code IS NULL AND subledger_account != ''"; |
||
| 284 | $result = $this->db->query($sql); |
||
| 285 | if ($result) { |
||
| 286 | $obj = $this->db->fetch_object($result); |
||
| 287 | if (!(round(abs($obj->deb), 2) === round(abs($obj->cred), 2))) { |
||
| 288 | $this->errors[] = 'Total not exacts '.round(abs($obj->deb), 2).' vs '.round(abs($obj->cred), 2); |
||
| 289 | $error++; |
||
| 290 | } |
||
| 291 | $this->db->free($result); |
||
| 292 | } else { |
||
| 293 | $this->errors[] = 'Erreur sql'.$this->db->lasterror(); |
||
| 294 | $error++; |
||
| 295 | } |
||
| 296 | |||
| 297 | // Update request |
||
| 298 | |||
| 299 | $now = dol_now(); |
||
| 300 | |||
| 301 | if (!$error) { |
||
| 302 | $sql = "UPDATE ".MAIN_DB_PREFIX."accounting_bookkeeping SET"; |
||
| 303 | $sql .= " lettering_code='".$this->db->escape($lettre)."'"; |
||
| 304 | $sql .= " , date_lettering = '".$this->db->idate($now)."'"; // todo correct date it's false |
||
| 305 | $sql .= " WHERE rowid IN (".$this->db->sanitize(implode(',', $ids)).") AND lettering_code IS NULL AND subledger_account != ''"; |
||
| 306 | |||
| 307 | dol_syslog(get_class($this)."::update", LOG_DEBUG); |
||
| 308 | $resql = $this->db->query($sql); |
||
| 309 | if (!$resql) { |
||
| 310 | $error++; |
||
| 311 | $this->errors[] = "Error ".$this->db->lasterror(); |
||
| 312 | } |
||
| 313 | } |
||
| 314 | |||
| 315 | // Commit or rollback |
||
| 316 | if ($error) { |
||
| 317 | foreach ($this->errors as $errmsg) { |
||
| 318 | dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR); |
||
| 319 | $this->error .= ($this->error ? ', '.$errmsg : $errmsg); |
||
| 320 | } |
||
| 321 | return -1 * $error; |
||
| 322 | } else { |
||
| 323 | return 1; |
||
| 324 | } |
||
| 325 | } |
||
| 326 | |||
| 327 | /** |
||
| 328 | * |
||
| 329 | * @param array $ids ids array |
||
| 330 | * @param boolean $notrigger no trigger |
||
| 331 | * @return int |
||
| 332 | */ |
||
| 333 | public function deleteLettering($ids, $notrigger = false) |
||
| 334 | { |
||
| 335 | $error = 0; |
||
| 336 | |||
| 337 | $sql = "UPDATE ".MAIN_DB_PREFIX."accounting_bookkeeping SET"; |
||
| 338 | $sql .= " lettering_code = NULL"; |
||
| 339 | $sql .= " , date_lettering = NULL"; |
||
| 340 | $sql .= " WHERE rowid IN (".$this->db->sanitize(implode(',', $ids)).")"; |
||
| 341 | $sql .= " AND subledger_account != ''"; |
||
| 342 | |||
| 343 | dol_syslog(get_class($this)."::update", LOG_DEBUG); |
||
| 344 | $resql = $this->db->query($sql); |
||
| 345 | if (!$resql) { |
||
| 346 | $error++; |
||
| 347 | $this->errors[] = "Error ".$this->db->lasterror(); |
||
| 348 | } |
||
| 349 | |||
| 350 | // Commit or rollback |
||
| 351 | if ($error) { |
||
| 352 | foreach ($this->errors as $errmsg) { |
||
| 353 | dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR); |
||
| 354 | $this->error .= ($this->error ? ', '.$errmsg : $errmsg); |
||
| 355 | } |
||
| 356 | return -1 * $error; |
||
| 357 | } else { |
||
| 358 | return 1; |
||
| 359 | } |
||
| 360 | } |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Lettering bookkeeping lines all types |
||
| 364 | * |
||
| 365 | * @param array $bookkeeping_ids Lettering specific list of bookkeeping id |
||
| 366 | * @param bool $unlettering Do unlettering |
||
| 367 | * @return int <0 if error (nb lettered = result -1), 0 if noting to lettering, >0 if OK (nb lettered) |
||
| 368 | */ |
||
| 369 | public function bookkeepingLetteringAll($bookkeeping_ids, $unlettering = false) |
||
| 400 | } |
||
| 401 | } |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Lettering bookkeeping lines |
||
| 405 | * |
||
| 406 | * @param array $bookkeeping_ids Lettering specific list of bookkeeping id |
||
| 407 | * @param string $type Type of bookkeeping type to lettering ('customer_invoice' or 'supplier_invoice') |
||
| 408 | * @param bool $unlettering Do unlettering |
||
| 409 | * @return int <0 if error (nb lettered = result -1), 0 if noting to lettering, >0 if OK (nb lettered) |
||
| 410 | */ |
||
| 411 | public function bookkeepingLettering($bookkeeping_ids, $type = 'customer_invoice', $unlettering = false) |
||
| 475 | } |
||
| 476 | } |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Lettering bookkeeping lines |
||
| 480 | * |
||
| 481 | * @param array $bookkeeping_ids Lettering specific list of bookkeeping id |
||
| 482 | * @param string $type Type of bookkeeping type to lettering ('customer_invoice' or 'supplier_invoice') |
||
| 483 | * @return array|int <0 if error otherwise all linked lines by block |
||
| 484 | */ |
||
| 485 | public function getLinkedLines($bookkeeping_ids, $type = 'customer_invoice') |
||
| 620 | } |
||
| 621 | |||
| 622 | /** |
||
| 623 | * Linked payment by group |
||
| 624 | * |
||
| 625 | * @param array $payment_ids list of payment id |
||
| 626 | * @param string $type Type of bookkeeping type to lettering ('customer_invoice' or 'supplier_invoice') |
||
| 627 | * @return array|int <0 if error otherwise all linked lines by block |
||
| 628 | */ |
||
| 629 | public function getLinkedPaymentByGroup($payment_ids, $type) |
||
| 630 | { |
||
| 631 | global $langs; |
||
| 632 | |||
| 633 | // Clean parameters |
||
| 634 | $payment_ids = is_array($payment_ids) ? $payment_ids : array(); |
||
| 635 | $type = trim($type); |
||
| 636 | |||
| 637 | if (empty($payment_ids)) { |
||
| 638 | return array(); |
||
| 639 | } |
||
| 640 | |||
| 641 | if ($type == 'customer_invoice') { |
||
| 642 | $payment_element = 'paiement_facture'; |
||
| 643 | $fk_payment_element = 'fk_paiement'; |
||
| 644 | $fk_element = 'fk_facture'; |
||
| 645 | } elseif ($type == 'supplier_invoice') { |
||
| 646 | $payment_element = 'paiementfourn_facturefourn'; |
||
| 647 | $fk_payment_element = 'fk_paiementfourn'; |
||
| 648 | $fk_element = 'fk_facturefourn'; |
||
| 649 | } else { |
||
| 650 | $langs->load('errors'); |
||
| 651 | $this->errors[] = $langs->trans('ErrorBadParameters'); |
||
| 652 | return -1; |
||
| 653 | } |
||
| 654 | |||
| 655 | // Get payment lines |
||
| 656 | $sql = "SELECT DISTINCT pe2.$fk_payment_element, pe2.$fk_element"; |
||
| 657 | $sql .= " FROM " . MAIN_DB_PREFIX . "$payment_element AS pe"; |
||
| 658 | $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "$payment_element AS pe2 ON pe2.$fk_element = pe.$fk_element"; |
||
| 659 | $sql .= " WHERE pe.$fk_payment_element IN (" . $this->db->sanitize(implode(',', $payment_ids)) . ")"; |
||
| 660 | |||
| 661 | dol_syslog(__METHOD__ . " - Get payment lines", LOG_DEBUG); |
||
| 662 | $resql = $this->db->query($sql); |
||
| 663 | if (!$resql) { |
||
| 664 | $this->errors[] = "Error " . $this->db->lasterror(); |
||
| 665 | return -1; |
||
| 666 | } |
||
| 667 | |||
| 668 | $current_payment_ids = array(); |
||
| 669 | $payment_by_element = array(); |
||
| 670 | $element_by_payment = array(); |
||
| 671 | while ($obj = $this->db->fetch_object($resql)) { |
||
| 672 | $current_payment_ids[$obj->$fk_payment_element] = $obj->$fk_payment_element; |
||
| 673 | $element_by_payment[$obj->$fk_payment_element][$obj->$fk_element] = $obj->$fk_element; |
||
| 674 | $payment_by_element[$obj->$fk_element][$obj->$fk_payment_element] = $obj->$fk_payment_element; |
||
| 675 | } |
||
| 676 | $this->db->free($resql); |
||
| 677 | |||
| 678 | if (count(array_diff($payment_ids, $current_payment_ids))) { |
||
| 679 | return $this->getLinkedPaymentByGroup($current_payment_ids, $type); |
||
| 680 | } |
||
| 681 | |||
| 682 | return $this->getGroupElements($payment_by_element, $element_by_payment); |
||
| 683 | } |
||
| 684 | |||
| 685 | /** |
||
| 686 | * Get payment ids grouped by payment id and element id in common |
||
| 687 | * |
||
| 688 | * @param array $payment_by_element List of payment ids by element id |
||
| 689 | * @param array $element_by_payment List of element ids by payment id |
||
| 690 | * @param int $element_id Element Id (used for recursive function) |
||
| 691 | * @param array $current_group Current group (used for recursive function) |
||
| 692 | * @return array List of payment ids grouped by payment id and element id in common |
||
| 693 | */ |
||
| 694 | public function getGroupElements(&$payment_by_element, &$element_by_payment, $element_id = 0, &$current_group = array()) |
||
| 745 | } |
||
| 746 | } |
||
| 747 |