NEW Can filter on a custom group of accounts. Perf or ledger list.

This commit is contained in:
Laurent Destailleur 2022-12-29 13:28:36 +01:00
parent aea48e87de
commit a7c2d72315
8 changed files with 277 additions and 179 deletions

View File

@ -282,7 +282,7 @@ if ($action == 'create') {
print $form->textwithpicto($langs->trans("AccountingCategory"), $langs->transnoentitiesnoconv("AccountingAccountGroupsDesc")); print $form->textwithpicto($langs->trans("AccountingCategory"), $langs->transnoentitiesnoconv("AccountingAccountGroupsDesc"));
print '</td>'; print '</td>';
print '<td>'; print '<td>';
$formaccounting->select_accounting_category($object->account_category, 'account_category', 1, 0, 1); print $formaccounting->select_accounting_category($object->account_category, 'account_category', 1, 0, 1);
print '</td></tr>'; print '</td></tr>';
print '</table>'; print '</table>';
@ -359,7 +359,7 @@ if ($action == 'create') {
print $form->textwithpicto($langs->trans("AccountingCategory"), $langs->transnoentitiesnoconv("AccountingAccountGroupsDesc")); print $form->textwithpicto($langs->trans("AccountingCategory"), $langs->transnoentitiesnoconv("AccountingAccountGroupsDesc"));
print '</td>'; print '</td>';
print '<td>'; print '<td>';
$formaccounting->select_accounting_category($object->account_category, 'account_category', 1); print $formaccounting->select_accounting_category($object->account_category, 'account_category', 1);
print '</td></tr>'; print '</td></tr>';
print '</table>'; print '</table>';

View File

@ -111,7 +111,7 @@ print '<table class="border centpercent">';
// Select the category // Select the category
print '<tr><td class="titlefield">'.$langs->trans("AccountingCategory").'</td>'; print '<tr><td class="titlefield">'.$langs->trans("AccountingCategory").'</td>';
print '<td>'; print '<td>';
$formaccounting->select_accounting_category($cat_id, 'account_category', 1, 0, 0, 0); print $formaccounting->select_accounting_category($cat_id, 'account_category', 1, 0, 0, 0);
print '<input type="submit" class="button small" value="'.$langs->trans("Select").'">'; print '<input type="submit" class="button small" value="'.$langs->trans("Select").'">';
print '</td></tr>'; print '</td></tr>';

View File

@ -102,7 +102,9 @@ if (GETPOST("button_export_file_x") || GETPOST("button_export_file.x") || GETPOS
$action = 'export_file'; $action = 'export_file';
} }
$search_accountancy_code = GETPOST("search_accountancy_code"); $search_account_category = GETPOST('search_account_category', 'int');
$search_accountancy_code = GETPOST("search_accountancy_code", 'alpha');
$search_accountancy_code_start = GETPOST('search_accountancy_code_start', 'alpha'); $search_accountancy_code_start = GETPOST('search_accountancy_code_start', 'alpha');
if ($search_accountancy_code_start == - 1) { if ($search_accountancy_code_start == - 1) {
$search_accountancy_code_start = ''; $search_accountancy_code_start = '';
@ -253,6 +255,7 @@ if (empty($reshook)) {
$search_doc_type = ''; $search_doc_type = '';
$search_doc_ref = ''; $search_doc_ref = '';
$search_doc_date = ''; $search_doc_date = '';
$search_account_category = '';
$search_accountancy_code = ''; $search_accountancy_code = '';
$search_accountancy_code_start = ''; $search_accountancy_code_start = '';
$search_accountancy_code_end = ''; $search_accountancy_code_end = '';
@ -335,6 +338,20 @@ if (empty($reshook)) {
$filter['t.doc_ref'] = $search_doc_ref; $filter['t.doc_ref'] = $search_doc_ref;
$param .= '&search_doc_ref='.urlencode($search_doc_ref); $param .= '&search_doc_ref='.urlencode($search_doc_ref);
} }
if ($search_account_category != '-1' && !empty($search_account_category)) {
require_once DOL_DOCUMENT_ROOT.'/accountancy/class/accountancycategory.class.php';
$accountingcategory = new AccountancyCategory($db);
$listofaccountsforgroup = $accountingcategory->getCptsCat(0, 'fk_accounting_category = '.((int) $search_account_category));
$listofaccountsforgroup2 = array();
if (is_array($listofaccountsforgroup)) {
foreach ($listofaccountsforgroup as $tmpval) {
$listofaccountsforgroup2[] = $tmpval['id'];
}
}
$filter['t.search_accounting_code_in'] = join(',', $listofaccountsforgroup2);
$param .= '&search_account_category='.urlencode($search_account_category);
}
if (!empty($search_accountancy_code)) { if (!empty($search_accountancy_code)) {
$filter['t.numero_compte'] = $search_accountancy_code; $filter['t.numero_compte'] = $search_accountancy_code;
$param .= '&search_accountancy_code='.urlencode($search_accountancy_code); $param .= '&search_accountancy_code='.urlencode($search_accountancy_code);
@ -642,6 +659,9 @@ $sql .= " t.tms as date_modification,";
$sql .= " t.date_export,"; $sql .= " t.date_export,";
$sql .= " t.date_validated as date_validation,"; $sql .= " t.date_validated as date_validation,";
$sql .= " t.import_key"; $sql .= " t.import_key";
$sqlfields = $sql; // $sql fields to remove for count total
$sql .= ' FROM '.MAIN_DB_PREFIX.$object->table_element.' as t'; $sql .= ' FROM '.MAIN_DB_PREFIX.$object->table_element.' as t';
// Manage filter // Manage filter
$sqlwhere = array(); $sqlwhere = array();
@ -672,7 +692,13 @@ if (count($filter) > 0) {
} elseif ($key == 't.reconciled_option') { } elseif ($key == 't.reconciled_option') {
$sqlwhere[] = 't.lettering_code IS NULL'; $sqlwhere[] = 't.lettering_code IS NULL';
} elseif ($key == 't.code_journal' && !empty($value)) { } elseif ($key == 't.code_journal' && !empty($value)) {
if (is_array($value)) {
$sqlwhere[] = natural_search("t.code_journal", join(',', $value), 3, 1); $sqlwhere[] = natural_search("t.code_journal", join(',', $value), 3, 1);
} else {
$sqlwhere[] = natural_search("t.code_journal", $value, 3, 1);
}
} elseif ($key == 't.search_accounting_code_in' && !empty($value)) {
$sqlwhere[] = 't.numero_compte IN ('.$value.')';
} else { } else {
$sqlwhere[] = natural_search($key, $value, 0, 1); $sqlwhere[] = natural_search($key, $value, 0, 1);
} }
@ -685,9 +711,6 @@ if (empty($conf->global->ACCOUNTING_REEXPORT)) {
if (count($sqlwhere) > 0) { if (count($sqlwhere) > 0) {
$sql .= ' AND '.implode(' AND ', $sqlwhere); $sql .= ' AND '.implode(' AND ', $sqlwhere);
} }
if (!empty($sortfield)) {
$sql .= $db->order($sortfield, $sortorder);
}
//print $sql; //print $sql;
@ -783,28 +806,38 @@ $title_page = $langs->trans("Operations").' - '.$langs->trans("Journals");
// Count total nb of records // Count total nb of records
$nbtotalofrecords = ''; $nbtotalofrecords = '';
if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST)) { if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST)) {
$resql = $db->query($sql); /* The fast and low memory method to get and count full list converts the sql into a sql count */
$nbtotalofrecords = $db->num_rows($resql); $sqlforcount = preg_replace('/^'.preg_quote($sqlfields, '/').'/', 'SELECT COUNT(*) as nbtotalofrecords', $sql);
if (($page * $limit) > $nbtotalofrecords) { // if total of record found is smaller than page * limit, goto and load page 0 $sqlforcount = preg_replace('/GROUP BY .*$/', '', $sqlforcount);
$resql = $db->query($sqlforcount);
if ($resql) {
$objforcount = $db->fetch_object($resql);
$nbtotalofrecords = $objforcount->nbtotalofrecords;
} else {
dol_print_error($db);
}
if (($page * $limit) > $nbtotalofrecords) { // if total resultset is smaller then paging size (filtering), goto and load page 0
$page = 0; $page = 0;
$offset = 0; $offset = 0;
} }
$db->free($resql);
} }
// if total of record found is smaller than limit, no need to do paging and to restart another select with limits set.
if (is_numeric($nbtotalofrecords) && $limit > $nbtotalofrecords) {
$num = $nbtotalofrecords;
} else {
$sql .= $db->plimit($limit + 1, $offset);
$resql = $db->query($sql); // Complete request and execute it with limit
if (!$resql) { $sql .= $db->order($sortfield, $sortorder);
if ($limit) {
$sql .= $db->plimit($limit + 1, $offset);
}
$resql = $db->query($sql);
if (!$resql) {
dol_print_error($db); dol_print_error($db);
exit; exit;
}
$num = $db->num_rows($resql);
} }
$num = $db->num_rows($resql);
$arrayofselected = is_array($toselect) ? $toselect : array(); $arrayofselected = is_array($toselect) ? $toselect : array();
// Output page // Output page
@ -997,6 +1030,12 @@ if ($massactionbutton && $contextpage != 'poslist') {
} }
$moreforfilter = ''; $moreforfilter = '';
$moreforfilter .= '<div class="divsearchfield">';
$moreforfilter .= $langs->trans('AccountingCategory').': ';
$moreforfilter .= '<div class="nowrap inline-block">';
$moreforfilter .= $formaccounting->select_accounting_category($search_account_category, 'search_account_category', 1, 0, 0, 0);
$moreforfilter .= '</div>';
$moreforfilter .= '</div>';
$parameters = array(); $parameters = array();
$reshook = $hookmanager->executeHooks('printFieldPreListTitle', $parameters, $object, $action); // Note that $action and $object may have been modified by hook $reshook = $hookmanager->executeHooks('printFieldPreListTitle', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
@ -1006,6 +1045,10 @@ if (empty($reshook)) {
$moreforfilter = $hookmanager->resPrint; $moreforfilter = $hookmanager->resPrint;
} }
print '<div class="liste_titre liste_titre_bydiv centpercent">';
print $moreforfilter;
print '</div>';
print '<div class="div-table-responsive">'; print '<div class="div-table-responsive">';
print '<table class="tagtable liste centpercent">'; print '<table class="tagtable liste centpercent">';

View File

@ -79,7 +79,8 @@ $search_date_validation_start = dol_mktime(0, 0, 0, $search_date_validation_star
$search_date_validation_end = dol_mktime(23, 59, 59, $search_date_validation_endmonth, $search_date_validation_endday, $search_date_validation_endyear); $search_date_validation_end = dol_mktime(23, 59, 59, $search_date_validation_endmonth, $search_date_validation_endday, $search_date_validation_endyear);
$search_import_key = GETPOST("search_import_key", 'alpha'); $search_import_key = GETPOST("search_import_key", 'alpha');
$search_accountancy_code = GETPOST("search_accountancy_code"); $search_account_category = GETPOST('search_account_category', 'int');
$search_accountancy_code_start = GETPOST('search_accountancy_code_start', 'alpha'); $search_accountancy_code_start = GETPOST('search_accountancy_code_start', 'alpha');
if ($search_accountancy_code_start == - 1) { if ($search_accountancy_code_start == - 1) {
$search_accountancy_code_start = ''; $search_accountancy_code_start = '';
@ -198,6 +199,8 @@ if (!$user->hasRight('accounting', 'mouvements', 'lire')) {
accessforbidden(); accessforbidden();
} }
$error = 0;
/* /*
* Action * Action
@ -224,7 +227,7 @@ if (empty($reshook)) {
if (GETPOST('button_removefilter_x', 'alpha') || GETPOST('button_removefilter.x', 'alpha') || GETPOST('button_removefilter', 'alpha')) { // All tests are required to be compatible with all browsers if (GETPOST('button_removefilter_x', 'alpha') || GETPOST('button_removefilter.x', 'alpha') || GETPOST('button_removefilter', 'alpha')) { // All tests are required to be compatible with all browsers
$search_doc_date = ''; $search_doc_date = '';
$search_accountancy_code = ''; $search_account_category = '';
$search_accountancy_code_start = ''; $search_accountancy_code_start = '';
$search_accountancy_code_end = ''; $search_accountancy_code_end = '';
$search_label_account = ''; $search_label_account = '';
@ -280,6 +283,20 @@ if (empty($reshook)) {
$filter['t.doc_date'] = $search_doc_date; $filter['t.doc_date'] = $search_doc_date;
$param .= '&doc_datemonth='.GETPOST('doc_datemonth', 'int').'&doc_dateday='.GETPOST('doc_dateday', 'int').'&doc_dateyear='.GETPOST('doc_dateyear', 'int'); $param .= '&doc_datemonth='.GETPOST('doc_datemonth', 'int').'&doc_dateday='.GETPOST('doc_dateday', 'int').'&doc_dateyear='.GETPOST('doc_dateyear', 'int');
} }
if ($search_account_category != '-1' && !empty($search_account_category)) {
require_once DOL_DOCUMENT_ROOT.'/accountancy/class/accountancycategory.class.php';
$accountingcategory = new AccountancyCategory($db);
$listofaccountsforgroup = $accountingcategory->getCptsCat(0, 'fk_accounting_category = '.((int) $search_account_category));
$listofaccountsforgroup2 = array();
if (is_array($listofaccountsforgroup)) {
foreach ($listofaccountsforgroup as $tmpval) {
$listofaccountsforgroup2[] = $tmpval['id'];
}
}
$filter['t.search_accounting_code_in'] = join(',', $listofaccountsforgroup2);
$param .= '&search_account_category='.urlencode($search_account_category);
}
if (!empty($search_accountancy_code_start)) { if (!empty($search_accountancy_code_start)) {
if ($type == 'sub') { if ($type == 'sub') {
$filter['t.subledger_account>='] = $search_accountancy_code_start; $filter['t.subledger_account>='] = $search_accountancy_code_start;
@ -358,7 +375,6 @@ if (empty($reshook)) {
$filter['t.import_key'] = $search_import_key; $filter['t.import_key'] = $search_import_key;
$param .= '&search_import_key='.urlencode($search_import_key); $param .= '&search_import_key='.urlencode($search_import_key);
} }
// param with type of list // param with type of list
$url_param = substr($param, 1); // remove first "&" $url_param = substr($param, 1); // remove first "&"
if (!empty($type)) { if (!empty($type)) {
@ -544,25 +560,29 @@ llxHeader('', $title_page);
// List // List
$nbtotalofrecords = ''; $nbtotalofrecords = '';
if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST)) { if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST)) {
// TODO Perf Replace this by a count
if ($type == 'sub') { if ($type == 'sub') {
$nbtotalofrecords = $object->fetchAllByAccount($sortorder, $sortfield, 0, 0, $filter, 'AND', 1); $nbtotalofrecords = $object->fetchAllByAccount($sortorder, $sortfield, 0, 0, $filter, 'AND', 1, 1);
} else { } else {
$nbtotalofrecords = $object->fetchAllByAccount($sortorder, $sortfield, 0, 0, $filter); $nbtotalofrecords = $object->fetchAllByAccount($sortorder, $sortfield, 0, 0, $filter, 'AND', 0, 1);
} }
if ($nbtotalofrecords < 0) { if ($nbtotalofrecords < 0) {
setEventMessages($object->error, $object->errors, 'errors'); setEventMessages($object->error, $object->errors, 'errors');
$error++;
} }
} }
if ($type == 'sub') { if (!$error) {
if ($type == 'sub') {
$result = $object->fetchAllByAccount($sortorder, $sortfield, $limit, $offset, $filter, 'AND', 1); $result = $object->fetchAllByAccount($sortorder, $sortfield, $limit, $offset, $filter, 'AND', 1);
} else { } else {
$result = $object->fetchAllByAccount($sortorder, $sortfield, $limit, $offset, $filter); $result = $object->fetchAllByAccount($sortorder, $sortfield, $limit, $offset, $filter, 'AND', 0);
} }
if ($result < 0) { if ($result < 0) {
setEventMessages($object->error, $object->errors, 'errors'); setEventMessages($object->error, $object->errors, 'errors');
}
} }
$arrayofselected = is_array($toselect) ? $toselect : array(); $arrayofselected = is_array($toselect) ? $toselect : array();
@ -711,7 +731,7 @@ if ($type == 'sub') {
$moreforfilter = ''; $moreforfilter = '';
// Accountancy account // Search on accountancy custom groups or account
$moreforfilter .= '<div class="divsearchfield">'; $moreforfilter .= '<div class="divsearchfield">';
$moreforfilter .= $langs->trans('AccountAccounting').': '; $moreforfilter .= $langs->trans('AccountAccounting').': ';
$moreforfilter .= '<div class="nowrap inline-block">'; $moreforfilter .= '<div class="nowrap inline-block">';
@ -729,6 +749,13 @@ if ($type == 'sub') {
$moreforfilter .= '</div>'; $moreforfilter .= '</div>';
$moreforfilter .= '</div>'; $moreforfilter .= '</div>';
$moreforfilter .= '<div class="divsearchfield">';
$moreforfilter .= $langs->trans('AccountingCategory').': ';
$moreforfilter .= '<div class="nowrap inline-block">';
$moreforfilter .= $formaccounting->select_accounting_category($search_account_category, 'search_account_category', 1, 0, 0, 0);
$moreforfilter .= '</div>';
$moreforfilter .= '</div>';
$parameters = array(); $parameters = array();
$reshook = $hookmanager->executeHooks('printFieldPreListTitle', $parameters); // Note that $action and $object may have been modified by hook $reshook = $hookmanager->executeHooks('printFieldPreListTitle', $parameters); // Note that $action and $object may have been modified by hook
if (empty($reshook)) { if (empty($reshook)) {

View File

@ -605,60 +605,6 @@ class AccountancyCategory // extends CommonObject
} }
} }
/**
* Function to know all custom groupd from an accounting account
*
* @return array|integer Result in table (array), -1 if KO
*/
public function getCatsCpts()
{
global $mysoc, $conf;
if (empty($mysoc->country_id)) {
dol_print_error('', 'Call to select_accounting_account with mysoc country not yet defined');
exit();
}
$sql = "SELECT t.rowid, t.account_number, t.label as account_label, cat.code, cat.position, cat.label as name_cat, cat.sens ";
$sql .= " FROM ".MAIN_DB_PREFIX."accounting_account as t, ".MAIN_DB_PREFIX."c_accounting_category as cat";
$sql .= " WHERE t.fk_accounting_category IN ( SELECT c.rowid ";
$sql .= " FROM ".MAIN_DB_PREFIX."c_accounting_category as c";
$sql .= " WHERE c.active = 1";
$sql .= " AND c.entity = ".$conf->entity;
$sql .= " AND (c.fk_country = ".((int) $mysoc->country_id)." OR c.fk_country = 0)";
$sql .= " AND cat.rowid = t.fk_accounting_category";
$sql .= " AND t.entity = ".$conf->entity;
$sql .= " ORDER BY cat.position ASC";
$resql = $this->db->query($sql);
if ($resql) {
$i = 0;
$obj = '';
$num = $this->db->num_rows($resql);
$data = array();
if ($num) {
while ($obj = $this->db->fetch_object($resql)) {
$name_cat = $obj->name_cat;
$data[$name_cat][$i] = array(
'id' => $obj->rowid,
'code' => $obj->code,
'position' => $obj->position,
'account_number' => $obj->account_number,
'account_label' => $obj->account_label,
'sens' => $obj->sens
);
$i++;
}
}
return $data;
} else {
$this->error = "Error ".$this->db->lasterror();
dol_syslog(__METHOD__." ".$this->error, LOG_ERR);
return -1;
}
}
/** /**
* Function to show result of an accounting account from the ledger with a direction and a period * Function to show result of an accounting account from the ledger with a direction and a period
* *
@ -734,12 +680,75 @@ class AccountancyCategory // extends CommonObject
} }
} }
/**
* 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)
*
* @param int $catid Custom group ID
* @return array|integer Result in table (array), -1 if KO
* @see getCats(), getCptsCat()
*/
public function getCatsCpts($catid = 0)
{
global $mysoc, $conf;
if (empty($mysoc->country_id)) {
$this->error = "Error ".$this->db->lasterror();
dol_syslog(__METHOD__." ".$this->error, LOG_ERR);
return -1;
}
$sql = "SELECT t.rowid, t.account_number, t.label as account_label,";
$sql .= " cat.code, cat.position, cat.label as name_cat, cat.sens, cat.category_type, cat.formula";
$sql .= " FROM ".MAIN_DB_PREFIX."accounting_account as t, ".MAIN_DB_PREFIX."c_accounting_category as cat";
$sql .= " WHERE t.fk_accounting_category IN (SELECT c.rowid";
$sql .= " FROM ".MAIN_DB_PREFIX."c_accounting_category as c";
$sql .= " WHERE c.active = 1";
$sql .= " AND c.entity = ".$conf->entity;
$sql .= " AND (c.fk_country = ".((int) $mysoc->country_id)." OR c.fk_country = 0)";
$sql .= " AND cat.rowid = t.fk_accounting_category";
$sql .= " AND t.entity = ".$conf->entity;
if ($catid > 0) {
$sql .= " AND cat.rowid = ".((int) $catid);
}
$sql .= " ORDER BY cat.position ASC";
$resql = $this->db->query($sql);
if ($resql) {
$obj = '';
$num = $this->db->num_rows($resql);
$data = array();
if ($num) {
while ($obj = $this->db->fetch_object($resql)) {
$name_cat = $obj->name_cat;
$data[$name_cat][$obj->rowid] = array(
'id' => $obj->rowid,
'code' => $obj->code,
'label' => $obj->label,
'position' => $obj->position,
'category_type' => $obj->category_type,
'formula' => $obj->formula,
'sens' => $obj->sens,
'account_number' => $obj->account_number,
'account_label' => $obj->account_label
);
}
}
return $data;
} else {
$this->error = "Error ".$this->db->lasterror();
dol_syslog(__METHOD__." ".$this->error, LOG_ERR);
return -1;
}
}
/** /**
* Return list of custom groups. * Return list of custom groups.
* For list + detail of accounting account, see getCatsCpt()
* *
* @param int $categorytype -1=All, 0=Only non computed groups, 1=Only computed groups * @param int $categorytype -1=All, 0=Only non computed groups, 1=Only computed groups
* @param int $active 1= active, 0=not active * @param int $active 1= active, 0=not active
* @return array|int Array of groups or -1 if error * @return array|int Array of groups or -1 if error
* @see getCatsCpts(), getCptsCat()
*/ */
public function getCats($categorytype = -1, $active = 1) public function getCats($categorytype = -1, $active = 1)
{ {
@ -774,9 +783,10 @@ class AccountancyCategory // extends CommonObject
'rowid' => $obj->rowid, 'rowid' => $obj->rowid,
'code' => $obj->code, 'code' => $obj->code,
'label' => $obj->label, 'label' => $obj->label,
'formula' => $obj->formula,
'position' => $obj->position, 'position' => $obj->position,
'category_type' => $obj->category_type, 'category_type' => $obj->category_type,
'formula' => $obj->formula,
'sens' => $obj->sens,
'bc' => $obj->sens 'bc' => $obj->sens
); );
$i++; $i++;
@ -794,12 +804,15 @@ class AccountancyCategory // extends CommonObject
/** /**
* Get all accounting account of a custom group (or a list of custom groups). * Get all accounting account of a given custom group (or a list of custom groups).
* You must choose between first parameter (personalized group) or the second (free criteria filter) * You must choose between first parameter (personalized group) or the second (free criteria filter)
* *
* @param int $cat_id Id if personalized accounting group/category * @param int $cat_id Id if personalized accounting group/category
* @param string $predefinedgroupwhere Sql criteria filter to select accounting accounts. This value must not come from an input of a user. * @param string $predefinedgroupwhere Sql criteria filter to select accounting accounts. This value must be sanitized and not come from an input of a user.
* Example: "pcg_type = 'EXPENSE' AND fk_pcg_version = 'xx'"
* Example: "fk_accounting_category = 99"
* @return array|int Array of accounting accounts or -1 if error * @return array|int Array of accounting accounts or -1 if error
* @see getCats(), getCatsCpts()
*/ */
public function getCptsCat($cat_id, $predefinedgroupwhere = '') public function getCptsCat($cat_id, $predefinedgroupwhere = '')
{ {

View File

@ -817,7 +817,7 @@ class BookKeeping extends CommonObject
/** /**
* Load object in memory from the database * Load object in memory from the database in ->lines. Or just make a simple count if $countonly=1.
* *
* @param string $sortorder Sort Order * @param string $sortorder Sort Order
* @param string $sortfield Sort field * @param string $sortfield Sort field
@ -826,18 +826,22 @@ class BookKeeping extends CommonObject
* @param array $filter filter array * @param array $filter filter array
* @param string $filtermode filter mode (AND or OR) * @param string $filtermode filter mode (AND or OR)
* @param int $option option (0: general account or 1: subaccount) * @param int $option option (0: general account or 1: subaccount)
* * @param int $countonly Do not fill the $object->lines, return only the count.
* @return int <0 if KO, >=0 if OK * @return int <0 if KO, Number of lines if OK
*/ */
public function fetchAllByAccount($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, array $filter = array(), $filtermode = 'AND', $option = 0) public function fetchAllByAccount($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, array $filter = array(), $filtermode = 'AND', $option = 0, $countonly = 0)
{ {
global $conf; global $conf;
dol_syslog(__METHOD__, LOG_DEBUG); dol_syslog(__METHOD__, LOG_DEBUG);
$this->lines = array(); $this->lines = array();
$num = 0;
$sql = 'SELECT'; $sql = 'SELECT';
if ($countonly) {
$sql .= ' COUNT(t.rowid) as nb';
} else {
$sql .= ' t.rowid,'; $sql .= ' t.rowid,';
$sql .= " t.doc_date,"; $sql .= " t.doc_date,";
$sql .= " t.doc_type,"; $sql .= " t.doc_type,";
@ -867,6 +871,7 @@ class BookKeeping extends CommonObject
$sql .= " t.date_export,"; $sql .= " t.date_export,";
$sql .= " t.date_validated as date_validation,"; $sql .= " t.date_validated as date_validation,";
$sql .= " t.import_key"; $sql .= " t.import_key";
}
// Manage filter // Manage filter
$sqlwhere = array(); $sqlwhere = array();
if (count($filter) > 0) { if (count($filter) > 0) {
@ -880,7 +885,7 @@ class BookKeeping extends CommonObject
} elseif ($key == 't.fk_doc' || $key == 't.fk_docdet' || $key == 't.piece_num') { } elseif ($key == 't.fk_doc' || $key == 't.fk_docdet' || $key == 't.piece_num') {
$sqlwhere[] = $key.'='.$value; $sqlwhere[] = $key.'='.$value;
} elseif ($key == 't.subledger_account' || $key == 't.numero_compte') { } elseif ($key == 't.subledger_account' || $key == 't.numero_compte') {
$sqlwhere[] = $key.' LIKE \''.$this->db->escape($value).'%\''; $sqlwhere[] = $key.' LIKE \''.$this->db->escapeforlike($this->db->escape($value)).'%\'';
} elseif ($key == 't.date_creation>=' || $key == 't.date_creation<=') { } elseif ($key == 't.date_creation>=' || $key == 't.date_creation<=') {
$sqlwhere[] = $key.'\''.$this->db->idate($value).'\''; $sqlwhere[] = $key.'\''.$this->db->idate($value).'\'';
} elseif ($key == 't.date_export>=' || $key == 't.date_export<=') { } elseif ($key == 't.date_export>=' || $key == 't.date_export<=') {
@ -897,18 +902,19 @@ class BookKeeping extends CommonObject
} else { } else {
$sqlwhere[] = natural_search("t.code_journal", $value, 3, 1); $sqlwhere[] = natural_search("t.code_journal", $value, 3, 1);
} }
} elseif ($key == 't.search_accounting_code_in' && !empty($value)) {
$sqlwhere[] = 't.numero_compte IN ('.$value.')';
} else { } else {
$sqlwhere[] = natural_search($key, $value, 0, 1); $sqlwhere[] = natural_search($key, $value, 0, 1);
} }
} }
} }
$sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t'; $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
$sql .= ' WHERE 1 = 1'; $sql .= ' WHERE entity = ' . ((int) $conf->entity); // Do not use getEntity for accounting features
$sql .= " AND entity = " . ((int) $conf->entity); // Do not use getEntity for accounting features
if (count($sqlwhere) > 0) { if (count($sqlwhere) > 0) {
$sql .= " AND ".implode(" ".$filtermode." ", $sqlwhere); $sql .= " AND ".implode(" ".$filtermode." ", $sqlwhere);
} }
// Affichage par compte comptable // Filter by ledger account or subledger account
if (!empty($option)) { if (!empty($option)) {
$sql .= " AND t.subledger_account IS NOT NULL"; $sql .= " AND t.subledger_account IS NOT NULL";
$sql .= " AND t.subledger_account <> ''"; $sql .= " AND t.subledger_account <> ''";
@ -919,13 +925,21 @@ class BookKeeping extends CommonObject
$sortorder = 'ASC'.($sortorder ? ','.$sortorder : ''); $sortorder = 'ASC'.($sortorder ? ','.$sortorder : '');
} }
if (!$countonly) {
$sql .= $this->db->order($sortfield, $sortorder); $sql .= $this->db->order($sortfield, $sortorder);
if (!empty($limit)) { if (!empty($limit)) {
$sql .= $this->db->plimit($limit + 1, $offset); $sql .= $this->db->plimit($limit + 1, $offset);
} }
}
$resql = $this->db->query($sql); $resql = $this->db->query($sql);
if ($resql) { if ($resql) {
if ($countonly) {
$obj = $this->db->fetch_object($resql);
if ($obj) {
$num = $obj->nb;
}
} else {
$num = $this->db->num_rows($resql); $num = $this->db->num_rows($resql);
$i = 0; $i = 0;
@ -968,6 +982,7 @@ class BookKeeping extends CommonObject
$i++; $i++;
} }
}
$this->db->free($resql); $this->db->free($resql);
return $num; return $num;

View File

@ -224,7 +224,7 @@ class FormAccounting extends Form
* @param int $maxlen Max length of text in combo box * @param int $maxlen Max length of text in combo box
* @param int $help Add or not the admin help picto * @param int $help Add or not the admin help picto
* @param int $allcountries All countries * @param int $allcountries All countries
* @return void * @return string HTML component with the select
*/ */
public function select_accounting_category($selected = '', $htmlname = 'account_category', $useempty = 0, $maxlen = 0, $help = 1, $allcountries = 0) public function select_accounting_category($selected = '', $htmlname = 'account_category', $useempty = 0, $maxlen = 0, $help = 1, $allcountries = 0)
{ {
@ -295,7 +295,7 @@ class FormAccounting extends Form
$out .= ajax_combobox($htmlname, array()); $out .= ajax_combobox($htmlname, array());
print $out; return $out;
} }
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps

View File

@ -217,7 +217,7 @@ Codejournal=Journal
JournalLabel=Journal label JournalLabel=Journal label
NumPiece=Piece number NumPiece=Piece number
TransactionNumShort=Num. transaction TransactionNumShort=Num. transaction
AccountingCategory=Custom group AccountingCategory=Custom group of accounts
GroupByAccountAccounting=Group by general ledger account GroupByAccountAccounting=Group by general ledger account
GroupBySubAccountAccounting=Group by subledger account GroupBySubAccountAccounting=Group by subledger account
AccountingAccountGroupsDesc=You can define here some groups of accounting account. They will be used for personalized accounting reports. AccountingAccountGroupsDesc=You can define here some groups of accounting account. They will be used for personalized accounting reports.