| Total Complexity | 124 |
| Total Lines | 867 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like AccountancyCategory 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 AccountancyCategory, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 36 | class AccountancyCategory // extends CommonObject |
||
| 37 | { |
||
| 38 | /** |
||
| 39 | * @var DoliDB Database handler. |
||
|
|
|||
| 40 | */ |
||
| 41 | public $db; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var string Error string |
||
| 45 | */ |
||
| 46 | public $error; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var string[] Error codes (or messages) |
||
| 50 | */ |
||
| 51 | public $errors = array(); |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var string ID to identify managed object |
||
| 55 | */ |
||
| 56 | public $element = 'c_accounting_category'; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var string Name of table without prefix where object is stored |
||
| 60 | */ |
||
| 61 | public $table_element = 'c_accounting_category'; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var int ID |
||
| 65 | * @deprecated |
||
| 66 | */ |
||
| 67 | public $rowid; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var int ID |
||
| 71 | */ |
||
| 72 | public $id; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var string Accountancy code |
||
| 76 | */ |
||
| 77 | public $code; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var string Accountancy Category label |
||
| 81 | */ |
||
| 82 | public $label; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var string Accountancy range account |
||
| 86 | */ |
||
| 87 | public $range_account; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var int Sens of the account: 0: credit - debit, 1: debit - credit |
||
| 91 | */ |
||
| 92 | public $sens; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @var int Category type of accountancy |
||
| 96 | */ |
||
| 97 | public $category_type; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @var string Formula |
||
| 101 | */ |
||
| 102 | public $formula; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @var int Position |
||
| 106 | */ |
||
| 107 | public $position; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @var int country id |
||
| 111 | */ |
||
| 112 | public $fk_country; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @var int Is active |
||
| 116 | */ |
||
| 117 | public $active; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @var array Lines cptbk |
||
| 121 | */ |
||
| 122 | public $lines_cptbk; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @var array Lines display |
||
| 126 | */ |
||
| 127 | public $lines_display; |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @var mixed Sum debit credit |
||
| 131 | */ |
||
| 132 | public $sdc; |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @var array Sum debit credit per month |
||
| 136 | */ |
||
| 137 | public $sdcpermonth; |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @var array Sum debit credit per account |
||
| 141 | */ |
||
| 142 | public $sdcperaccount; |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Constructor |
||
| 146 | * |
||
| 147 | * @param DoliDB $db Database handler |
||
| 148 | */ |
||
| 149 | public function __construct($db) |
||
| 152 | } |
||
| 153 | |||
| 154 | |||
| 155 | /** |
||
| 156 | * Create object into database |
||
| 157 | * |
||
| 158 | * @param User $user User that create |
||
| 159 | * @param int $notrigger 0=launch triggers after, 1=disable triggers |
||
| 160 | * @return int Return integer <0 if KO, Id of created object if OK |
||
| 161 | */ |
||
| 162 | public function create($user, $notrigger = 0) |
||
| 163 | { |
||
| 164 | global $conf, $langs; |
||
| 165 | $error = 0; |
||
| 166 | |||
| 167 | // Clean parameters |
||
| 168 | if (isset($this->code)) { |
||
| 169 | $this->code = trim($this->code); |
||
| 170 | } |
||
| 171 | if (isset($this->label)) { |
||
| 172 | $this->label = trim($this->label); |
||
| 173 | } |
||
| 174 | if (isset($this->range_account)) { |
||
| 175 | $this->range_account = trim($this->range_account); |
||
| 176 | } |
||
| 177 | if (isset($this->sens)) { |
||
| 178 | $this->sens = (int) $this->sens; |
||
| 179 | } |
||
| 180 | if (isset($this->category_type)) { |
||
| 181 | $this->category_type = (int) $this->category_type; |
||
| 182 | } |
||
| 183 | if (isset($this->formula)) { |
||
| 184 | $this->formula = trim($this->formula); |
||
| 185 | } |
||
| 186 | if (isset($this->position)) { |
||
| 187 | $this->position = (int) $this->position; |
||
| 188 | } |
||
| 189 | if (isset($this->fk_country)) { |
||
| 190 | $this->fk_country = (int) $this->fk_country; |
||
| 191 | } |
||
| 192 | if (isset($this->active)) { |
||
| 193 | $this->active = (int) $this->active; |
||
| 194 | } |
||
| 195 | |||
| 196 | // Check parameters |
||
| 197 | // Put here code to add control on parameters values |
||
| 198 | |||
| 199 | // Insert request |
||
| 200 | $sql = "INSERT INTO " . MAIN_DB_PREFIX . "c_accounting_category("; |
||
| 201 | if ($this->rowid > 0) { |
||
| 202 | $sql .= "rowid, "; |
||
| 203 | } |
||
| 204 | $sql .= "code, "; |
||
| 205 | $sql .= "label, "; |
||
| 206 | $sql .= "range_account, "; |
||
| 207 | $sql .= "sens, "; |
||
| 208 | $sql .= "category_type, "; |
||
| 209 | $sql .= "formula, "; |
||
| 210 | $sql .= "position, "; |
||
| 211 | $sql .= "fk_country, "; |
||
| 212 | $sql .= "active, "; |
||
| 213 | $sql .= "entity"; |
||
| 214 | $sql .= ") VALUES ("; |
||
| 215 | if ($this->rowid > 0) { |
||
| 216 | $sql .= " " . ((int) $this->rowid) . ","; |
||
| 217 | } |
||
| 218 | $sql .= " " . (!isset($this->code) ? "NULL" : "'" . $this->db->escape($this->code) . "'") . ","; |
||
| 219 | $sql .= " " . (!isset($this->label) ? 'NULL' : "'" . $this->db->escape($this->label) . "'") . ","; |
||
| 220 | $sql .= " " . (!isset($this->range_account) ? 'NULL' : "'" . $this->db->escape($this->range_account) . "'") . ","; |
||
| 221 | $sql .= " " . (!isset($this->sens) ? 'NULL' : "'" . $this->db->escape($this->sens) . "'") . ","; |
||
| 222 | $sql .= " " . (!isset($this->category_type) ? 'NULL' : "'" . $this->db->escape($this->category_type) . "'") . ","; |
||
| 223 | $sql .= " " . (!isset($this->formula) ? 'NULL' : "'" . $this->db->escape($this->formula) . "'") . ","; |
||
| 224 | $sql .= " " . (!isset($this->position) ? 'NULL' : ((int) $this->position)) . ","; |
||
| 225 | $sql .= " " . (!isset($this->fk_country) ? 'NULL' : ((int) $this->fk_country)) . ","; |
||
| 226 | $sql .= " " . (!isset($this->active) ? 'NULL' : ((int) $this->active)); |
||
| 227 | $sql .= ", " . ((int) $conf->entity); |
||
| 228 | $sql .= ")"; |
||
| 229 | |||
| 230 | $this->db->begin(); |
||
| 231 | |||
| 232 | dol_syslog(get_class($this) . "::create", LOG_DEBUG); |
||
| 233 | $resql = $this->db->query($sql); |
||
| 234 | if (!$resql) { |
||
| 235 | $error++; |
||
| 236 | $this->errors[] = "Error " . $this->db->lasterror(); |
||
| 237 | } |
||
| 238 | |||
| 239 | // Commit or rollback |
||
| 240 | if ($error) { |
||
| 241 | foreach ($this->errors as $errmsg) { |
||
| 242 | dol_syslog(get_class($this) . "::create " . $errmsg, LOG_ERR); |
||
| 243 | $this->error .= ($this->error ? ', ' . $errmsg : $errmsg); |
||
| 244 | } |
||
| 245 | $this->db->rollback(); |
||
| 246 | return -1 * $error; |
||
| 247 | } else { |
||
| 248 | $this->db->commit(); |
||
| 249 | return $this->id; |
||
| 250 | } |
||
| 251 | } |
||
| 252 | |||
| 253 | |||
| 254 | /** |
||
| 255 | * Load object in memory from database |
||
| 256 | * |
||
| 257 | * @param int $id Id object |
||
| 258 | * @param string $code Code |
||
| 259 | * @param string $label Label |
||
| 260 | * @return int Return integer <0 if KO, >0 if OK |
||
| 261 | */ |
||
| 262 | public function fetch($id, $code = '', $label = '') |
||
| 263 | { |
||
| 264 | $sql = "SELECT"; |
||
| 265 | $sql .= " t.rowid,"; |
||
| 266 | $sql .= " t.code,"; |
||
| 267 | $sql .= " t.label,"; |
||
| 268 | $sql .= " t.range_account,"; |
||
| 269 | $sql .= " t.sens,"; |
||
| 270 | $sql .= " t.category_type,"; |
||
| 271 | $sql .= " t.formula,"; |
||
| 272 | $sql .= " t.position,"; |
||
| 273 | $sql .= " t.fk_country,"; |
||
| 274 | $sql .= " t.active"; |
||
| 275 | $sql .= " FROM " . MAIN_DB_PREFIX . "c_accounting_category as t"; |
||
| 276 | if ($id) { |
||
| 277 | $sql .= " WHERE t.rowid = " . ((int) $id); |
||
| 278 | } else { |
||
| 279 | $sql .= " WHERE t.entity IN (" . getEntity('c_accounting_category') . ")"; // Don't use entity if you use rowid |
||
| 280 | if ($code) { |
||
| 281 | $sql .= " AND t.code = '" . $this->db->escape($code) . "'"; |
||
| 282 | } elseif ($label) { |
||
| 283 | $sql .= " AND t.label = '" . $this->db->escape($label) . "'"; |
||
| 284 | } |
||
| 285 | } |
||
| 286 | |||
| 287 | dol_syslog(get_class($this) . "::fetch", LOG_DEBUG); |
||
| 288 | $resql = $this->db->query($sql); |
||
| 289 | if ($resql) { |
||
| 290 | if ($this->db->num_rows($resql)) { |
||
| 291 | $obj = $this->db->fetch_object($resql); |
||
| 292 | |||
| 293 | $this->id = $obj->rowid; |
||
| 294 | $this->code = $obj->code; |
||
| 295 | $this->label = $obj->label; |
||
| 296 | $this->range_account = $obj->range_account; |
||
| 297 | $this->sens = $obj->sens; |
||
| 298 | $this->category_type = $obj->category_type; |
||
| 299 | $this->formula = $obj->formula; |
||
| 300 | $this->position = $obj->position; |
||
| 301 | $this->fk_country = $obj->fk_country; |
||
| 302 | $this->active = $obj->active; |
||
| 303 | } |
||
| 304 | $this->db->free($resql); |
||
| 305 | |||
| 306 | return 1; |
||
| 307 | } else { |
||
| 308 | $this->error = "Error " . $this->db->lasterror(); |
||
| 309 | return -1; |
||
| 310 | } |
||
| 311 | } |
||
| 312 | |||
| 313 | |||
| 314 | /** |
||
| 315 | * Update object into database |
||
| 316 | * |
||
| 317 | * @param User $user User that modify |
||
| 318 | * @param int $notrigger 0=launch triggers after, 1=disable triggers |
||
| 319 | * @return int Return integer <0 if KO, >0 if OK |
||
| 320 | */ |
||
| 321 | public function update($user = null, $notrigger = 0) |
||
| 322 | { |
||
| 323 | global $conf, $langs; |
||
| 324 | $error = 0; |
||
| 325 | |||
| 326 | // Clean parameters |
||
| 327 | if (isset($this->code)) { |
||
| 328 | $this->code = trim($this->code); |
||
| 329 | } |
||
| 330 | if (isset($this->label)) { |
||
| 331 | $this->label = trim($this->label); |
||
| 332 | } |
||
| 333 | if (isset($this->range_account)) { |
||
| 334 | $this->range_account = trim($this->range_account); |
||
| 335 | } |
||
| 336 | if (isset($this->sens)) { |
||
| 337 | $this->sens = (int) $this->sens; |
||
| 338 | } |
||
| 339 | if (isset($this->category_type)) { |
||
| 340 | $this->category_type = (int) $this->category_type; |
||
| 341 | } |
||
| 342 | if (isset($this->formula)) { |
||
| 343 | $this->formula = trim($this->formula); |
||
| 344 | } |
||
| 345 | if (isset($this->position)) { |
||
| 346 | $this->position = (int) $this->position; |
||
| 347 | } |
||
| 348 | if (isset($this->fk_country)) { |
||
| 349 | $this->fk_country = (int) $this->fk_country; |
||
| 350 | } |
||
| 351 | if (isset($this->active)) { |
||
| 352 | $this->active = (int) $this->active; |
||
| 353 | } |
||
| 354 | |||
| 355 | |||
| 356 | // Check parameters |
||
| 357 | // Put here code to add control on parameters values |
||
| 358 | |||
| 359 | // Update request |
||
| 360 | $sql = "UPDATE " . MAIN_DB_PREFIX . "c_accounting_category SET"; |
||
| 361 | $sql .= " code=" . (isset($this->code) ? "'" . $this->db->escape($this->code) . "'" : "null") . ","; |
||
| 362 | $sql .= " label=" . (isset($this->label) ? "'" . $this->db->escape($this->label) . "'" : "null") . ","; |
||
| 363 | $sql .= " range_account=" . (isset($this->range_account) ? "'" . $this->db->escape($this->range_account) . "'" : "null") . ","; |
||
| 364 | $sql .= " sens=" . (isset($this->sens) ? ((int) $this->sens) : "null") . ","; |
||
| 365 | $sql .= " category_type=" . (isset($this->category_type) ? ((int) $this->category_type) : "null") . ","; |
||
| 366 | $sql .= " formula=" . (isset($this->formula) ? "'" . $this->db->escape($this->formula) . "'" : "null") . ","; |
||
| 367 | $sql .= " position=" . (isset($this->position) ? ((int) $this->position) : "null") . ","; |
||
| 368 | $sql .= " fk_country=" . (isset($this->fk_country) ? ((int) $this->fk_country) : "null") . ","; |
||
| 369 | $sql .= " active=" . (isset($this->active) ? ((int) $this->active) : "null"); |
||
| 370 | $sql .= " WHERE rowid=" . ((int) $this->id); |
||
| 371 | |||
| 372 | $this->db->begin(); |
||
| 373 | |||
| 374 | dol_syslog(get_class($this) . "::update", LOG_DEBUG); |
||
| 375 | $resql = $this->db->query($sql); |
||
| 376 | if (!$resql) { |
||
| 377 | $error++; |
||
| 378 | $this->errors[] = "Error " . $this->db->lasterror(); |
||
| 379 | } |
||
| 380 | |||
| 381 | // Commit or rollback |
||
| 382 | if ($error) { |
||
| 383 | foreach ($this->errors as $errmsg) { |
||
| 384 | dol_syslog(get_class($this) . "::update " . $errmsg, LOG_ERR); |
||
| 385 | $this->error .= ($this->error ? ', ' . $errmsg : $errmsg); |
||
| 386 | } |
||
| 387 | $this->db->rollback(); |
||
| 388 | return -1 * $error; |
||
| 389 | } else { |
||
| 390 | $this->db->commit(); |
||
| 391 | return 1; |
||
| 392 | } |
||
| 393 | } |
||
| 394 | |||
| 395 | |||
| 396 | /** |
||
| 397 | * Delete object in database |
||
| 398 | * |
||
| 399 | * @param User $user User that delete |
||
| 400 | * @param int $notrigger 0=launch triggers after, 1=disable triggers |
||
| 401 | * @return int Return integer <0 if KO, >0 if OK |
||
| 402 | */ |
||
| 403 | public function delete($user, $notrigger = 0) |
||
| 404 | { |
||
| 405 | global $conf, $langs; |
||
| 406 | $error = 0; |
||
| 407 | |||
| 408 | $sql = "DELETE FROM " . MAIN_DB_PREFIX . "c_accounting_category"; |
||
| 409 | $sql .= " WHERE rowid=" . ((int) $this->id); |
||
| 410 | |||
| 411 | $this->db->begin(); |
||
| 412 | |||
| 413 | dol_syslog(get_class($this) . "::delete", LOG_DEBUG); |
||
| 414 | $resql = $this->db->query($sql); |
||
| 415 | if (!$resql) { |
||
| 416 | $error++; |
||
| 417 | $this->errors[] = "Error " . $this->db->lasterror(); |
||
| 418 | } |
||
| 419 | |||
| 420 | // Commit or rollback |
||
| 421 | if ($error) { |
||
| 422 | foreach ($this->errors as $errmsg) { |
||
| 423 | dol_syslog(get_class($this) . "::delete " . $errmsg, LOG_ERR); |
||
| 424 | $this->error .= ($this->error ? ', ' . $errmsg : $errmsg); |
||
| 425 | } |
||
| 426 | $this->db->rollback(); |
||
| 427 | return -1 * $error; |
||
| 428 | } else { |
||
| 429 | $this->db->commit(); |
||
| 430 | return 1; |
||
| 431 | } |
||
| 432 | } |
||
| 433 | |||
| 434 | |||
| 435 | /** |
||
| 436 | * Function to select into ->lines_display all accounting accounts for a given custom accounting group |
||
| 437 | * |
||
| 438 | * @param int $id Id |
||
| 439 | * @return int Return integer <0 if KO, 0 if not found, >0 if OK |
||
| 440 | */ |
||
| 441 | public function display($id) |
||
| 442 | { |
||
| 443 | global $conf; |
||
| 444 | $sql = "SELECT t.rowid, t.account_number, t.label"; |
||
| 445 | $sql .= " FROM " . MAIN_DB_PREFIX . "accounting_account as t"; |
||
| 446 | $sql .= " WHERE t.fk_accounting_category = " . ((int) $id); |
||
| 447 | $sql .= " AND t.entity = " . $conf->entity; |
||
| 448 | |||
| 449 | $this->lines_display = array(); |
||
| 450 | |||
| 451 | dol_syslog(__METHOD__, LOG_DEBUG); |
||
| 452 | $resql = $this->db->query($sql); |
||
| 453 | if ($resql) { |
||
| 454 | $num = $this->db->num_rows($resql); |
||
| 455 | if ($num) { |
||
| 456 | while ($obj = $this->db->fetch_object($resql)) { |
||
| 457 | $this->lines_display[] = $obj; |
||
| 458 | } |
||
| 459 | } |
||
| 460 | return $num; |
||
| 461 | } else { |
||
| 462 | $this->error = "Error " . $this->db->lasterror(); |
||
| 463 | $this->errors[] = $this->error; |
||
| 464 | dol_syslog(__METHOD__ . " " . implode(',', $this->errors), LOG_ERR); |
||
| 465 | |||
| 466 | return -1; |
||
| 467 | } |
||
| 468 | } |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Function to fill ->lines_cptbk with accounting account (defined in chart of account) and not yet into a custom group |
||
| 472 | * |
||
| 473 | * @param int $id Id of category to know which account to exclude |
||
| 474 | * @return int Return integer <0 if KO, 0 if not found, >0 if OK |
||
| 475 | */ |
||
| 476 | public function getAccountsWithNoCategory($id) |
||
| 477 | { |
||
| 478 | global $conf; |
||
| 479 | |||
| 480 | $sql = "SELECT aa.account_number as numero_compte, aa.label as label_compte"; |
||
| 481 | $sql .= " FROM " . MAIN_DB_PREFIX . "accounting_account as aa"; |
||
| 482 | $sql .= " INNER JOIN " . MAIN_DB_PREFIX . "accounting_system as asy ON aa.fk_pcg_version = asy.pcg_version"; |
||
| 483 | $sql .= " WHERE (aa.fk_accounting_category <> " . ((int) $id) . " OR aa.fk_accounting_category IS NULL)"; |
||
| 484 | $sql .= " AND asy.rowid = " . ((int) getDolGlobalInt('CHARTOFACCOUNTS')); |
||
| 485 | $sql .= " AND aa.active = 1"; |
||
| 486 | $sql .= " AND aa.entity = " . $conf->entity; |
||
| 487 | $sql .= " GROUP BY aa.account_number, aa.label"; |
||
| 488 | $sql .= " ORDER BY aa.account_number, aa.label"; |
||
| 489 | |||
| 490 | $this->lines_cptbk = array(); |
||
| 491 | |||
| 492 | dol_syslog(__METHOD__, LOG_DEBUG); |
||
| 493 | $resql = $this->db->query($sql); |
||
| 494 | if ($resql) { |
||
| 495 | $num = $this->db->num_rows($resql); |
||
| 496 | if ($num) { |
||
| 497 | while ($obj = $this->db->fetch_object($resql)) { |
||
| 498 | $this->lines_cptbk[] = $obj; |
||
| 499 | } |
||
| 500 | } |
||
| 501 | |||
| 502 | return $num; |
||
| 503 | } else { |
||
| 504 | $this->error = "Error " . $this->db->lasterror(); |
||
| 505 | $this->errors[] = $this->error; |
||
| 506 | dol_syslog(__METHOD__ . " " . implode(',', $this->errors), LOG_ERR); |
||
| 507 | |||
| 508 | return -1; |
||
| 509 | } |
||
| 510 | } |
||
| 511 | |||
| 512 | /** |
||
| 513 | * Function to add an accounting account in an accounting category |
||
| 514 | * |
||
| 515 | * @param int $id_cat Id category |
||
| 516 | * @param array $cpts list of accounts array |
||
| 517 | * |
||
| 518 | * @return int Return integer <0 if KO, >0 if OK |
||
| 519 | */ |
||
| 520 | public function updateAccAcc($id_cat, $cpts = array()) |
||
| 521 | { |
||
| 522 | global $conf; |
||
| 523 | $error = 0; |
||
| 524 | |||
| 525 | require_once constant('DOL_DOCUMENT_ROOT') . '/core/lib/accounting.lib.php'; |
||
| 526 | |||
| 527 | $sql = "SELECT aa.rowid, aa.account_number"; |
||
| 528 | $sql .= " FROM " . MAIN_DB_PREFIX . "accounting_account as aa"; |
||
| 529 | $sql .= " INNER JOIN " . MAIN_DB_PREFIX . "accounting_system as asy ON aa.fk_pcg_version = asy.pcg_version"; |
||
| 530 | $sql .= " AND asy.rowid = " . ((int) getDolGlobalInt('CHARTOFACCOUNTS')); |
||
| 531 | $sql .= " AND aa.active = 1"; |
||
| 532 | $sql .= " AND aa.entity = " . $conf->entity; |
||
| 533 | $sql .= " ORDER BY LENGTH(aa.account_number) DESC;"; // LENGTH is ok with mysql and postgresql |
||
| 534 | |||
| 535 | $this->db->begin(); |
||
| 536 | |||
| 537 | dol_syslog(__METHOD__, LOG_DEBUG); |
||
| 538 | $resql = $this->db->query($sql); |
||
| 539 | if (!$resql) { |
||
| 540 | $error++; |
||
| 541 | $this->errors[] = "Error " . $this->db->lasterror(); |
||
| 542 | $this->db->rollback(); |
||
| 543 | return -1; |
||
| 544 | } |
||
| 545 | |||
| 546 | $accountincptsadded = array(); |
||
| 547 | while ($obj = $this->db->fetch_object($resql)) { |
||
| 548 | $account_number_formated = length_accountg($obj->account_number); |
||
| 549 | if (!empty($accountincptsadded[$account_number_formated])) { |
||
| 550 | continue; |
||
| 551 | } |
||
| 552 | |||
| 553 | if (array_key_exists($account_number_formated, $cpts)) { |
||
| 554 | $accountincptsadded[$account_number_formated] = 1; |
||
| 555 | // We found an account number that is in list $cpts of account to add |
||
| 556 | $sql = "UPDATE " . MAIN_DB_PREFIX . "accounting_account"; |
||
| 557 | $sql .= " SET fk_accounting_category=" . ((int) $id_cat); |
||
| 558 | $sql .= " WHERE rowid=" . ((int) $obj->rowid); |
||
| 559 | dol_syslog(__METHOD__, LOG_DEBUG); |
||
| 560 | $resqlupdate = $this->db->query($sql); |
||
| 561 | if (!$resqlupdate) { |
||
| 562 | $error++; |
||
| 563 | $this->errors[] = "Error " . $this->db->lasterror(); |
||
| 564 | } |
||
| 565 | } |
||
| 566 | } |
||
| 567 | |||
| 568 | // Commit or rollback |
||
| 569 | if ($error) { |
||
| 570 | foreach ($this->errors as $errmsg) { |
||
| 571 | dol_syslog(__METHOD__ . " " . $errmsg, LOG_ERR); |
||
| 572 | $this->error .= ($this->error ? ', ' . $errmsg : $errmsg); |
||
| 573 | } |
||
| 574 | $this->db->rollback(); |
||
| 575 | |||
| 576 | return -1 * $error; |
||
| 577 | } else { |
||
| 578 | $this->db->commit(); |
||
| 579 | |||
| 580 | return 1; |
||
| 581 | } |
||
| 582 | } |
||
| 583 | |||
| 584 | /** |
||
| 585 | * Function to delete an accounting account from an accounting category |
||
| 586 | * |
||
| 587 | * @param int $cpt_id Id of accounting account |
||
| 588 | * |
||
| 589 | * @return int Return integer <0 if KO, >0 if OK |
||
| 590 | */ |
||
| 591 | public function deleteCptCat($cpt_id) |
||
| 592 | { |
||
| 593 | $error = 0; |
||
| 594 | |||
| 595 | $sql = "UPDATE " . MAIN_DB_PREFIX . "accounting_account as aa"; |
||
| 596 | $sql .= " SET fk_accounting_category= 0"; |
||
| 597 | $sql .= " WHERE aa.rowid = " . ((int) $cpt_id); |
||
| 598 | $this->db->begin(); |
||
| 599 | |||
| 600 | dol_syslog(__METHOD__, LOG_DEBUG); |
||
| 601 | $resql = $this->db->query($sql); |
||
| 602 | if (!$resql) { |
||
| 603 | $error++; |
||
| 604 | $this->errors[] = "Error " . $this->db->lasterror(); |
||
| 605 | } |
||
| 606 | |||
| 607 | // Commit or rollback |
||
| 608 | if ($error) { |
||
| 609 | foreach ($this->errors as $errmsg) { |
||
| 610 | dol_syslog(__METHOD__ . " " . $errmsg, LOG_ERR); |
||
| 611 | $this->error .= ($this->error ? ', ' . $errmsg : $errmsg); |
||
| 612 | } |
||
| 613 | $this->db->rollback(); |
||
| 614 | |||
| 615 | return -1 * $error; |
||
| 616 | } else { |
||
| 617 | $this->db->commit(); |
||
| 618 | |||
| 619 | return 1; |
||
| 620 | } |
||
| 621 | } |
||
| 622 | |||
| 623 | /** |
||
| 624 | * Function to show result of an accounting account from the ledger with a direction and a period |
||
| 625 | * |
||
| 626 | * @param int|array $cpt Accounting account or array of accounting account |
||
| 627 | * @param string $date_start Date start |
||
| 628 | * @param string $date_end Date end |
||
| 629 | * @param int $sens Sens of the account: 0: credit - debit (use this by default), 1: debit - credit |
||
| 630 | * @param string $thirdparty_code Thirdparty code |
||
| 631 | * @param int $month Specific month - Can be empty |
||
| 632 | * @param int $year Specific year - Can be empty |
||
| 633 | * @return integer Return integer <0 if KO, >= 0 if OK |
||
| 634 | */ |
||
| 635 | public function getSumDebitCredit($cpt, $date_start, $date_end, $sens, $thirdparty_code = 'nofilter', $month = 0, $year = 0) |
||
| 710 | } |
||
| 711 | } |
||
| 712 | |||
| 713 | /** |
||
| 714 | * Function to get an array of all active custom groups (llx_c_accunting_categories) with their accounts from the chart of account (ll_accounting_acount) |
||
| 715 | * |
||
| 716 | * @param int $catid Custom group ID |
||
| 717 | * @return array|integer Result in table (array), -1 if KO |
||
| 718 | * @see getCats(), getCptsCat() |
||
| 719 | */ |
||
| 720 | public function getCatsCpts($catid = 0) |
||
| 771 | } |
||
| 772 | } |
||
| 773 | |||
| 774 | /** |
||
| 775 | * Return list of custom groups. |
||
| 776 | * For list + detail of accounting account, see getCatsCpt() |
||
| 777 | * |
||
| 778 | * @param int $categorytype -1=All, 0=Only non computed groups, 1=Only computed groups |
||
| 779 | * @param int $active 1= active, 0=not active |
||
| 780 | * @return array<array{code:string,label:string,formula:string,position:string,category_type:string,sens:string,bc:string}>|int Array of groups or -1 if error |
||
| 781 | * @see getCatsCpts(), getCptsCat() |
||
| 782 | */ |
||
| 783 | public function getCats($categorytype = -1, $active = 1) |
||
| 784 | { |
||
| 785 | global $conf, $mysoc; |
||
| 786 | |||
| 787 | if (empty($mysoc->country_id)) { |
||
| 788 | dol_print_error(null, 'Call to select_accounting_account with mysoc country not yet defined'); |
||
| 789 | exit(); |
||
| 790 | } |
||
| 791 | |||
| 792 | $sql = "SELECT c.rowid, c.code, c.label, c.formula, c.position, c.category_type, c.sens"; |
||
| 793 | $sql .= " FROM " . MAIN_DB_PREFIX . "c_accounting_category as c"; |
||
| 794 | $sql .= " WHERE c.active = " . (int) $active; |
||
| 795 | $sql .= " AND c.entity = " . $conf->entity; |
||
| 796 | if ($categorytype >= 0) { |
||
| 797 | $sql .= " AND c.category_type = 1"; |
||
| 798 | } |
||
| 799 | $sql .= " AND (c.fk_country = " . ((int) $mysoc->country_id) . " OR c.fk_country = 0)"; |
||
| 800 | $sql .= " ORDER BY c.position ASC"; |
||
| 801 | |||
| 802 | $resql = $this->db->query($sql); |
||
| 803 | if ($resql) { |
||
| 804 | $i = 0; |
||
| 805 | $obj = ''; |
||
| 806 | $num = $this->db->num_rows($resql); |
||
| 807 | $data = array(); |
||
| 808 | if ($num) { |
||
| 809 | while ($i < $num) { |
||
| 810 | $obj = $this->db->fetch_object($resql); |
||
| 811 | |||
| 812 | $data[] = array( |
||
| 813 | 'rowid' => $obj->rowid, |
||
| 814 | 'code' => $obj->code, |
||
| 815 | 'label' => $obj->label, |
||
| 816 | 'position' => $obj->position, |
||
| 817 | 'category_type' => $obj->category_type, |
||
| 818 | 'formula' => $obj->formula, |
||
| 819 | 'sens' => $obj->sens, |
||
| 820 | 'bc' => $obj->sens |
||
| 821 | ); |
||
| 822 | $i++; |
||
| 823 | } |
||
| 824 | } |
||
| 825 | return $data; |
||
| 826 | } else { |
||
| 827 | $this->error = "Error " . $this->db->lasterror(); |
||
| 828 | $this->errors[] = $this->error; |
||
| 829 | dol_syslog(__METHOD__ . " " . implode(',', $this->errors), LOG_ERR); |
||
| 830 | |||
| 831 | return -1; |
||
| 832 | } |
||
| 833 | } |
||
| 834 | |||
| 835 | |||
| 836 | /** |
||
| 837 | * Get all accounting account of a given custom group (or a list of custom groups). |
||
| 838 | * You must choose between first parameter (personalized group) or the second (free criteria filter) |
||
| 839 | * |
||
| 840 | * @param int $cat_id Id if personalized accounting group/category |
||
| 841 | * @param string $predefinedgroupwhere Sql criteria filter to select accounting accounts. This value must be sanitized and not come from an input of a user. |
||
| 842 | * Example: "pcg_type = 'EXPENSE' AND fk_pcg_version = 'xx'" |
||
| 843 | * Example: "fk_accounting_category = 99" |
||
| 844 | * @return array|int Array of accounting accounts or -1 if error |
||
| 845 | * @see getCats(), getCatsCpts() |
||
| 846 | */ |
||
| 847 | public function getCptsCat($cat_id, $predefinedgroupwhere = '') |
||
| 903 | } |
||
| 904 | } |
||
| 905 | } |
||
| 906 |