From 4cde15080488f0bf5e3707dcfe9e3ef3a07d85db Mon Sep 17 00:00:00 2001 From: Alexandre SPANGARO Date: Wed, 19 Jun 2024 20:10:04 +0200 Subject: [PATCH] NEW Accountancy - Add partial reconcile on subledger (#30027) * NEW Accountancy - Add partial lettering on subledger * Fix PHPCS * Fix codespell --- .../accountancy/bookkeeping/listbyaccount.php | 17 +- htdocs/accountancy/class/lettering.class.php | 169 ++++++++++++------ htdocs/langs/en_US/accountancy.lang | 1 + 3 files changed, 129 insertions(+), 58 deletions(-) diff --git a/htdocs/accountancy/bookkeeping/listbyaccount.php b/htdocs/accountancy/bookkeeping/listbyaccount.php index d692349eb84..29123020436 100644 --- a/htdocs/accountancy/bookkeeping/listbyaccount.php +++ b/htdocs/accountancy/bookkeeping/listbyaccount.php @@ -533,7 +533,17 @@ if (empty($reshook)) { if ($result < 0) { setEventMessages('', $lettering->errors, 'errors'); } else { - setEventMessages($langs->trans('AccountancyOneLetteringModifiedSuccessfully'), array(), 'mesgs'); + setEventMessages($langs->trans($result == 0 ? 'AccountancyNoLetteringModified' : 'AccountancyOneLetteringModifiedSuccessfully'), array(), 'mesgs'); + header('Location: ' . $_SERVER['PHP_SELF'] . '?noreset=1' . $param); + exit(); + } + } elseif ($type == 'sub' && $massaction == 'letteringpartial') { + $lettering = new Lettering($db); + $result = $lettering->updateLettering($toselect, false, true); + if ($result < 0) { + setEventMessages('', $lettering->errors, 'errors'); + } else { + setEventMessages($langs->trans($result == 0 ? 'AccountancyNoLetteringModified' : 'AccountancyOneLetteringModifiedSuccessfully'), array(), 'mesgs'); header('Location: ' . $_SERVER['PHP_SELF'] . '?noreset=1' . $param); exit(); } @@ -564,7 +574,7 @@ if (empty($reshook)) { if ($result < 0) { setEventMessages('', $lettering->errors, 'errors'); } else { - setEventMessages($langs->trans('AccountancyOneUnletteringModifiedSuccessfully'), array(), 'mesgs'); + setEventMessages($langs->trans($result == 0 ? 'AccountancyNoUnletteringModified' : 'AccountancyOneUnletteringModifiedSuccessfully'), array(), 'mesgs'); header('Location: ' . $_SERVER['PHP_SELF'] . '?noreset=1' . $param); exit(); } @@ -675,6 +685,9 @@ if (getDolGlobalInt('ACCOUNTING_ENABLE_LETTERING') && $user->hasRight('accountin $arrayofmassactions['letteringauto'] = img_picto('', 'check', 'class="pictofixedwidth"') . $langs->trans('LetteringAuto'); $arrayofmassactions['preunletteringauto'] = img_picto('', 'uncheck', 'class="pictofixedwidth"') . $langs->trans('UnletteringAuto'); $arrayofmassactions['letteringmanual'] = img_picto('', 'check', 'class="pictofixedwidth"') . $langs->trans('LetteringManual'); + if ($type == 'sub') { + $arrayofmassactions['letteringpartial'] = img_picto('', 'check', 'class="pictofixedwidth"') . $langs->trans('LetteringPartial'); + } $arrayofmassactions['preunletteringmanual'] = img_picto('', 'uncheck', 'class="pictofixedwidth"') . $langs->trans('UnletteringManual'); } if ($user->hasRight('accounting', 'mouvements', 'supprimer')) { diff --git a/htdocs/accountancy/class/lettering.class.php b/htdocs/accountancy/class/lettering.class.php index 14a76497c1e..59710b1ab31 100644 --- a/htdocs/accountancy/class/lettering.class.php +++ b/htdocs/accountancy/class/lettering.class.php @@ -1,7 +1,7 @@ * Copyright (C) 2013 Olivier Geffroy - * Copyright (C) 2013-2023 Alexandre Spangaro + * Copyright (C) 2013-2024 Alexandre Spangaro * Copyright (C) 2018 Frédéric France * * This program is free software; you can redistribute it and/or modify @@ -291,79 +291,136 @@ class Lettering extends BookKeeping * * @param array $ids ids array * @param int $notrigger no trigger + * @param bool $partial Partial lettering * @return int */ - public function updateLettering($ids = array(), $notrigger = 0) + public function updateLettering($ids = array(), $notrigger = 0, $partial = false) { - $error = 0; - - // Generate a string with n char A where n is ACCOUNTING_LETTERING_NBLETTERS (So 'AA', 'AAA', ...) @phan-suppress-next-line PhanParamSuspiciousOrder - $lettre = str_pad("", getDolGlobalInt('ACCOUNTING_LETTERING_NBLETTERS', 3), "A"); - - $sql = "SELECT DISTINCT ab2.lettering_code"; - $sql .= " FROM " . MAIN_DB_PREFIX . "accounting_bookkeeping AS ab"; - $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "accounting_bookkeeping AS ab2 ON ab2.subledger_account = ab.subledger_account"; - $sql .= " WHERE ab.rowid IN (" . $this->db->sanitize(implode(',', $ids)) . ")"; - $sql .= " AND ab2.lettering_code != ''"; - $sql .= " ORDER BY ab2.lettering_code DESC"; - $sql .= " LIMIT 1 "; - - $resqla = $this->db->query($sql); - if ($resqla) { - $obj = $this->db->fetch_object($resqla); - $lettre = (empty($obj->lettering_code) ? $lettre : $obj->lettering_code); - if (!empty($obj->lettering_code)) { - $lettre++; - } - $this->db->free($resqla); - } else { - $this->errors[] = 'Error'.$this->db->lasterror(); - $error++; - } - - $sql = "SELECT SUM(ABS(debit)) as deb, SUM(ABS(credit)) as cred FROM ".MAIN_DB_PREFIX."accounting_bookkeeping WHERE "; - $sql .= " rowid IN (".$this->db->sanitize(implode(',', $ids)).") AND lettering_code IS NULL AND subledger_account != ''"; - $resqlb = $this->db->query($sql); - if ($resqlb) { - $obj = $this->db->fetch_object($resqlb); - if (!(round(abs($obj->deb), 2) === round(abs($obj->cred), 2))) { - $this->errors[] = 'Total not exacts '.round(abs($obj->deb), 2).' vs '.round(abs($obj->cred), 2); - $error++; - } - $this->db->free($resqlb); - } else { - $this->errors[] = 'Erreur sql'.$this->db->lasterror(); - $error++; - } - - // Update request $now = dol_now(); + $error = 0; $affected_rows = 0; - if (!$error) { - $sql = "UPDATE ".MAIN_DB_PREFIX."accounting_bookkeeping SET"; - $sql .= " lettering_code='".$this->db->escape($lettre)."'"; - $sql .= ", date_lettering = '".$this->db->idate($now)."'"; // todo correct date it's false - $sql .= " WHERE rowid IN (".$this->db->sanitize(implode(',', $ids)).") AND lettering_code IS NULL AND subledger_account != ''"; + // 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 + $letter = str_pad("", getDolGlobalInt('ACCOUNTING_LETTERING_NBLETTERS', 3), $partial ? 'a' : 'A'); - dol_syslog(get_class($this)."::update", LOG_DEBUG); + $this->db->begin(); + + // Check partial / normal lettering case + $sql = "SELECT ab.lettering_code, GROUP_CONCAT(DISTINCT ab.rowid SEPARATOR ',') AS bookkeeping_ids"; + $sql .= " FROM " . MAIN_DB_PREFIX . "accounting_bookkeeping AS ab"; + $sql .= " WHERE ab.rowid IN (" . $this->db->sanitize(implode(',', $ids)) . ")"; + $sql .= " GROUP BY ab.lettering_code"; + $sql .= " ORDER BY ab.lettering_code DESC"; + + dol_syslog(__METHOD__ . " - Check partial / normal lettering case", LOG_DEBUG); + $resql = $this->db->query($sql); + if ($resql) { + while ($obj = $this->db->fetch_object($resql)) { + if (empty($obj->lettering_code)) continue; + + // Remove normal lettering code if set partial lettering + if ($partial && preg_match('/^[A-Z]+$/', $obj->lettering_code)) { + if (!empty($obj->bookkeeping_ids)) $ids = array_diff($ids, explode(',', $obj->bookkeeping_ids)); + } elseif (!$partial && preg_match('/^[a-z]+$/', $obj->lettering_code)) { + // Delete partial lettering code if set normal lettering + $sql2 = "UPDATE " . MAIN_DB_PREFIX . "accounting_bookkeeping SET"; + $sql2 .= " lettering_code = NULL"; + $sql2 .= ", date_lettering = NULL"; + $sql2 .= " WHERE entity IN (" . getEntity('accountancy') . ")"; + $sql2 .= " AND lettering_code = '" . $this->db->escape($obj->lettering_code) . "'"; + + dol_syslog(__METHOD__ . " - Remove partial lettering", LOG_DEBUG); + $resql2 = $this->db->query($sql2); + if (!$resql2) { + $this->errors[] = 'Error' . $this->db->lasterror(); + $error++; + break; + } + } + } + $this->db->free($resql); + } else { + $this->errors[] = 'Error' . $this->db->lasterror(); + $error++; + } + + if (!$error && !empty($ids)) { + // Get next code + $sql = "SELECT DISTINCT ab2.lettering_code"; + $sql .= " FROM " . MAIN_DB_PREFIX . "accounting_bookkeeping AS ab"; + $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "accounting_bookkeeping AS ab2 ON ab2.subledger_account = ab.subledger_account"; + $sql .= " WHERE ab.rowid IN (" . $this->db->sanitize(implode(',', $ids)) . ")"; + $sql .= " AND ab2.lettering_code != ''"; + $sql .= " ORDER BY ab2.lettering_code DESC"; + + dol_syslog(__METHOD__ . " - Get next code", LOG_DEBUG); $resql = $this->db->query($sql); - if (!$resql) { - $error++; - $this->errors[] = "Error ".$this->db->lasterror(); + if ($resql) { + while ($obj = $this->db->fetch_object($resql)) { + if (!empty($obj->lettering_code) && + (($partial && preg_match('/^[a-z]+$/', $obj->lettering_code)) || + (!$partial && preg_match('/^[A-Z]+$/', $obj->lettering_code))) + ) { + $letter = $obj->lettering_code; + $letter++; + break; + } + } + $this->db->free($resql); } else { - $affected_rows = $this->db->affected_rows($resql); + $this->errors[] = 'Error' . $this->db->lasterror(); + $error++; + } + + // Test amount integrity + if (!$error && !$partial) { + $sql = "SELECT SUM(ABS(debit)) as deb, SUM(ABS(credit)) as cred FROM " . MAIN_DB_PREFIX . "accounting_bookkeeping WHERE "; + $sql .= " rowid IN (" . $this->db->sanitize(implode(',', $ids)) . ") AND lettering_code IS NULL AND subledger_account != ''"; + + dol_syslog(__METHOD__ . " - Test amount integrity", LOG_DEBUG); + $resql = $this->db->query($sql); + if ($resql) { + if ($obj = $this->db->fetch_object($resql)) { + if (!(round(abs($obj->deb), 2) === round(abs($obj->cred), 2))) { + $this->errors[] = 'Total not exacts ' . round(abs($obj->deb), 2) . ' vs ' . round(abs($obj->cred), 2); + $error++; + } + } + $this->db->free($resql); + } else { + $this->errors[] = 'Erreur sql' . $this->db->lasterror(); + $error++; + } + } + + // Update lettering code + if (!$error) { + $sql = "UPDATE " . MAIN_DB_PREFIX . "accounting_bookkeeping SET"; + $sql .= " lettering_code='" . $this->db->escape($letter) . "'"; + $sql .= ", date_lettering = '" . $this->db->idate($now) . "'"; // todo correct date it's false + $sql .= " WHERE rowid IN (" . $this->db->sanitize(implode(',', $ids)) . ") AND lettering_code IS NULL AND subledger_account != ''"; + + dol_syslog(__METHOD__ . " - Update lettering code", LOG_DEBUG); + $resql = $this->db->query($sql); + if (!$resql) { + $error++; + $this->errors[] = "Error " . $this->db->lasterror(); + } else { + $affected_rows = $this->db->affected_rows($resql); + } } } // Commit or rollback if ($error) { + $this->db->rollback(); foreach ($this->errors as $errmsg) { - dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR); - $this->error .= ($this->error ? ', '.$errmsg : $errmsg); + dol_syslog(get_class($this) . "::update " . $errmsg, LOG_ERR); + $this->error .= ($this->error ? ', ' . $errmsg : $errmsg); } return -1 * $error; } else { + $this->db->commit(); return $affected_rows; } } diff --git a/htdocs/langs/en_US/accountancy.lang b/htdocs/langs/en_US/accountancy.lang index 8f4929883e2..de82ca33202 100644 --- a/htdocs/langs/en_US/accountancy.lang +++ b/htdocs/langs/en_US/accountancy.lang @@ -409,6 +409,7 @@ Formula=Formula ## Reconcile LetteringAuto=Reconcile auto LetteringManual=Reconcile manual +LetteringPartial=Reconcile partial Unlettering=Unreconcile UnletteringAuto=Unreconcile auto UnletteringManual=Unreconcile manual