diff --git a/htdocs/accountancy/class/accountancycategory.class.php b/htdocs/accountancy/class/accountancycategory.class.php
index 8edcd4bca04..0652f8580aa 100644
--- a/htdocs/accountancy/class/accountancycategory.class.php
+++ b/htdocs/accountancy/class/accountancycategory.class.php
@@ -286,7 +286,7 @@ class AccountancyCategory
}
if (! empty($mysoc->country_id)) {
- $sql = "SELECT t.rowid, t.account_number, t.label as name_cpt, cat.code, cat.position, cat.label as name_cat, cat.sens ";
+ $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";
@@ -316,7 +316,7 @@ class AccountancyCategory
'code' => $obj->code,
'position' => $obj->position,
'account_number' => $obj->account_number,
- 'name_cpt' => $obj->name_cpt,
+ 'account_label' => $obj->account_label,
'sens' => $obj->sens
);
$i ++;
@@ -334,14 +334,15 @@ class AccountancyCategory
/**
* Function to show result of an accounting account from the ledger with a direction and a period
*
- * @param int $cpt Id accounting account
- * @param string $month Specifig month - Can be empty
- * @param string $date_start Date start
- * @param string $date_end Date end
- * @param int $sens Sens of the account: 0: credit - debit, 1: debit - credit
- * @return integer Result in table
+ * @param int $cpt Id accounting account
+ * @param string $month Specifig month - Can be empty
+ * @param string $date_start Date start
+ * @param string $date_end Date end
+ * @param int $sens Sens of the account: 0: credit - debit, 1: debit - credit
+ * @param string $thirdparty_code Thirdparty code
+ * @return integer Result in table
*/
- public function getResult($cpt, $month, $date_start, $date_end, $sens)
+ public function getResult($cpt, $month, $date_start, $date_end, $sens, $thirdparty_code='nofilter')
{
$sql = "SELECT SUM(t.debit) as debit, SUM(t.credit) as credit";
$sql .= " FROM " . MAIN_DB_PREFIX . "accounting_bookkeeping as t";
@@ -351,6 +352,10 @@ class AccountancyCategory
if (! empty($month)) {
$sql .= " AND MONTH(t.doc_date) = " . $month;
}
+ if ($thirdparty_code != 'nofilter')
+ {
+ $sql .= " AND thirdparty_code = '".$ths->db->escape($thirdparty_code)."'";
+ }
dol_syslog(__METHOD__ . " sql=" . $sql, LOG_DEBUG);
$resql = $this->db->query($sql);
@@ -376,62 +381,13 @@ class AccountancyCategory
}
/**
- * Function to call category from a specific country
+ * Return list of personalized groups
*
- * @return array Result in table
+ * @return int $categorytype -1=All, 0=Only non computed groups, 1=Only computed groups
+ * @return array|int Array of personalized groups or < 0 if KO
*/
- public function getCatsCal() {
- global $db, $langs, $user, $mysoc;
-
- if (empty($mysoc->country_id) && empty($mysoc->country_code)) {
- dol_print_error('', 'Call to select_accounting_account with mysoc country not yet defined');
- exit();
- }
-
- if (! empty($mysoc->country_id)) {
- $sql = "SELECT c.rowid, c.code, c.label, c.formula, c.position";
- $sql .= " FROM " . MAIN_DB_PREFIX . "c_accounting_category as c";
- $sql .= " WHERE c.active = 1 AND c.category_type = 1 ";
- $sql .= " AND c.fk_country = " . $mysoc->country_id;
- $sql .= " ORDER BY c.position ASC";
- } else {
- $sql = "SELECT c.rowid, c.code, c.label, c.formula, c.position";
- $sql .= " FROM " . MAIN_DB_PREFIX . "c_accounting_category as c, " . MAIN_DB_PREFIX . "c_country as co";
- $sql .= " WHERE c.active = 1 AND c.category_type = 1 AND c.fk_country = co.rowid";
- $sql .= " AND co.code = '" . $mysoc->country_code . "'";
- $sql .= " ORDER BY c.position ASC";
- }
-
- dol_syslog(__METHOD__ . " sql=" . $sql, LOG_DEBUG);
- $resql = $this->db->query($sql);
- if ($resql) {
- $i = 0;
- $obj = '';
- $num = $this->db->num_rows($resql);
- $data = array ();
- if ($num) {
- while ( $i < $num ) {
- $obj = $this->db->fetch_object($resql);
- $position = $obj->position;
- $data[$position] = array (
- 'code' => $obj->code,
- 'label' => $obj->label,
- 'formula' => $obj->formula
- );
- $i ++;
- }
- }
- return $data;
- } else {
- $this->error = "Error " . $this->db->lasterror();
- $this->errors[] = $this->error;
- dol_syslog(__METHOD__ . " " . implode(',', $this->errors), LOG_ERR);
-
- return - 1;
- }
- }
-
- public function getCats() {
+ public function getCats($categorytype=-1)
+ {
global $db, $langs, $user, $mysoc;
if (empty($mysoc->country_id) && empty($mysoc->country_code)) {
@@ -443,17 +399,18 @@ class AccountancyCategory
$sql = "SELECT c.rowid, c.code, c.label, c.formula, c.position, c.category_type";
$sql .= " FROM " . MAIN_DB_PREFIX . "c_accounting_category as c";
$sql .= " WHERE c.active = 1 ";
+ if ($categorytype >= 0) $sql.=" AND c.category_type = 1";
$sql .= " AND c.fk_country = " . $mysoc->country_id;
$sql .= " ORDER BY c.position ASC";
- } else {
+ } else { // Note: this should not happen
$sql = "SELECT c.rowid, c.code, c.label, c.formula, c.position, c.category_type";
$sql .= " FROM " . MAIN_DB_PREFIX . "c_accounting_category as c, " . MAIN_DB_PREFIX . "c_country as co";
$sql .= " WHERE c.active = 1 AND c.fk_country = co.rowid";
+ if ($categorytype >= 0) $sql.=" AND c.category_type = 1";
$sql .= " AND co.code = '" . $mysoc->country_code . "'";
$sql .= " ORDER BY c.position ASC";
}
- dol_syslog(__METHOD__ . " sql=" . $sql, LOG_DEBUG);
$resql = $this->db->query($sql);
if ($resql) {
$i = 0;
@@ -467,9 +424,9 @@ class AccountancyCategory
$data[] = array (
'rowid' => $obj->rowid,
'code' => $obj->code,
- 'position' => $obj->position,
'label' => $obj->label,
'formula' => $obj->formula,
+ 'position' => $obj->position,
'category_type' => $obj->category_type
);
$i ++;
@@ -546,26 +503,37 @@ class AccountancyCategory
/**
- * get cpts of category
+ * Get all accounting account of a group.
+ * You must choose between first parameter (personalized group) or the second (free criteria filter)
*
- * @param int $cat_id Id accounting account category
- *
- * @return array Result in table
+ * @param int $cat_id Id if personalized accounting group/category
+ * @param string $predefinedgroup Sql criteria filter to select accounting accounts
+ * @return array Array of accounting accounts
*/
- public function getCptsCat($cat_id) {
+ public function getCptsCat($cat_id, $predefinedgroupwhere='')
+ {
global $mysoc;
- $sql = "";
+ $sql = '';
if (empty($mysoc->country_id) && empty($mysoc->country_code)) {
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 name_cpt";
- $sql .= " FROM " . MAIN_DB_PREFIX . "accounting_account as t";
- $sql .= " WHERE t.fk_accounting_category = ".$cat_id;
- $sql .= " ORDER BY t.account_number ";
-
+ if (! empty($cat_id))
+ {
+ $sql = "SELECT t.rowid, t.account_number, t.label as account_label";
+ $sql .= " FROM " . MAIN_DB_PREFIX . "accounting_account as t";
+ $sql .= " WHERE t.fk_accounting_category = ".$cat_id;
+ $sql .= " ORDER BY t.account_number ";
+ }
+ else
+ {
+ $sql = "SELECT t.rowid, t.account_number, t.label as account_label";
+ $sql .= " FROM " . MAIN_DB_PREFIX . "accounting_account as t";
+ $sql .= " WHERE ".$predefinedgroupwhere;
+ $sql .= " ORDER BY t.account_number ";
+ }
//echo $sql;
$resql = $this->db->query($sql);
@@ -573,14 +541,15 @@ class AccountancyCategory
$i = 0;
$obj = '';
$num = $this->db->num_rows($resql);
- $data = array ();
+ $data = array();
if ($num) {
- while ( $obj = $this->db->fetch_object($resql) ) {
+ while ($obj = $this->db->fetch_object($resql))
+ {
$name_cat = $obj->name_cat;
$data[] = array (
'id' => $obj->rowid,
'account_number' => $obj->account_number,
- 'name_cpt' => $obj->name_cpt,
+ 'account_label' => $obj->account_label,
);
$i ++;
}
diff --git a/htdocs/compta/resultat/clientfourn.php b/htdocs/compta/resultat/clientfourn.php
index 4c5be71ff88..959779e05f0 100644
--- a/htdocs/compta/resultat/clientfourn.php
+++ b/htdocs/compta/resultat/clientfourn.php
@@ -36,7 +36,7 @@ require_once DOL_DOCUMENT_ROOT.'/core/lib/tax.lib.php';
require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
require_once DOL_DOCUMENT_ROOT.'/accountancy/class/accountancycategory.class.php';
-$langs->loadLangs(array('compta','bills','donation','salaries'));
+$langs->loadLangs(array('compta','bills','donation','salaries','accountancy'));
$date_startmonth=GETPOST('date_startmonth','int');
$date_startday=GETPOST('date_startday','int');
@@ -158,10 +158,10 @@ if ($modecompta=="CREANCES-DETTES")
{
$name=$langs->trans("AnnualByCompanies");
$calcmode=$langs->trans("CalcModeDebt");
- $calcmode.='
('.$langs->trans("SeeReportInInputOutputMode",'0?'&month='.GETPOST("month"):'').'&modecompta=RECETTES-DEPENSES">','').')';
- $calcmode.='
('.$langs->trans("SeeReportInBookkeepingMode",'','').')';
+ $calcmode.='
('.$langs->trans("SeeReportInInputOutputMode",'','').')';
+ $calcmode.='
('.$langs->trans("SeeReportInBookkeepingMode",'','').')';
$period=$form->select_date($date_start,'date_start',0,0,0,'',1,0,1).' - '.$form->select_date($date_end,'date_end',0,0,0,'',1,0,1);
- $periodlink=($year_start?"".img_previous()." ".img_next()."":"");
+ $periodlink=($year_start?"".img_previous()." ".img_next()."":"");
$description=$langs->trans("RulesResultDue");
if (! empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) $description.= $langs->trans("DepositsAreNotIncluded");
else $description.= $langs->trans("DepositsAreIncluded");
@@ -172,11 +172,11 @@ elseif ($modecompta=="RECETTES-DEPENSES")
{
$name=$langs->trans("AnnualByCompanies");
$calcmode=$langs->trans("CalcModeEngagement");
- $calcmode.='
('.$langs->trans("SeeReportInDueDebtMode",'0?'&month='.GETPOST("month"):'').'&modecompta=CREANCES-DETTES">','').')';
- $calcmode.='
('.$langs->trans("SeeReportInBookkeepingMode",'','').')';
+ $calcmode.='
('.$langs->trans("SeeReportInDueDebtMode",'','').')';
+ $calcmode.='
('.$langs->trans("SeeReportInBookkeepingMode",'','').')';
//$period=$form->select_date($date_start,'date_start',0,0,0,'',1,0,1).' - '.$form->select_date($date_end,'date_end',1,1,0,'',1,0,1);
$period=$form->select_date($date_start,'date_start',0,0,0,'',1,0,1).' - '.$form->select_date($date_end,'date_end',0,0,0,'',1,0,1);
- $periodlink=($year_start?"".img_previous()." ".img_next()."":"");
+ $periodlink=($year_start?"".img_previous()." ".img_next()."":"");
$description=$langs->trans("RulesResultInOut");
$builddate=dol_now();
//$exportlink=$langs->trans("NotYetAvailable");
@@ -185,12 +185,12 @@ elseif ($modecompta=="BOOKKEEPING")
{
$name=$langs->trans("AnnualByCompanies");
$calcmode=$langs->trans("CalcModeBookkeeping");
- $calcmode.='
('.$langs->trans("SeeReportInInputOutputMode",'0?'&month='.GETPOST("month"):'').'&modecompta=RECETTES-DEPENSES">','').')';
- $calcmode.='
('.$langs->trans("SeeReportInDueDebtMode",'0?'&month='.GETPOST("month"):'').'&modecompta=CREANCES-DETTES">','').')';
+ $calcmode.='
('.$langs->trans("SeeReportInInputOutputMode",'','').')';
+ $calcmode.='
('.$langs->trans("SeeReportInDueDebtMode",'','').')';
//$period=$form->select_date($date_start,'date_start',0,0,0,'',1,0,1).' - '.$form->select_date($date_end,'date_end',1,1,0,'',1,0,1);
$period=$form->select_date($date_start,'date_start',0,0,0,'',1,0,1).' - '.$form->select_date($date_end,'date_end',0,0,0,'',1,0,1);
$period.=' '.$langs->trans("DetailByAccount").' '. $form->selectyesno('showaccountdetail',$showaccountdetail,0);
- $periodlink=($year_start?"".img_previous()." ".img_next()."":"");
+ $periodlink=($year_start?"".img_previous()." ".img_next()."":"");
$description=$langs->trans("RulesResultBookkeepingPredefined");
$description.=' ('.$langs->trans("SeePageForSetup", DOL_URL_ROOT.'/accountancy/admin/account.php?mainmenu=accountancy&leftmenu=accountancy_admin', $langs->transnoentitiesnoconv("Accountancy").' / '.$langs->transnoentitiesnoconv("Setup").' / '.$langs->trans("Chartofaccounts")).')';
$builddate=dol_now();
@@ -237,22 +237,29 @@ print "\n";
if ($modecompta == 'BOOKKEEPING')
{
+ $predefinedgroupwhere = "(";
+ $predefinedgroupwhere.= " (pcg_type = 'EXPENSE' and pcg_subtype in ('PRODUCT','SERVICE'))";
+ $predefinedgroupwhere.= " OR ";
+ $predefinedgroupwhere.= " (pcg_type = 'INCOME' and pcg_subtype in ('PRODUCT','SERVICE'))";
+ $predefinedgroupwhere.= ")";
+
+ $charofaccountstring = $conf->global->CHARTOFACCOUNTS;
+ $charofaccountstring=dol_getIdFromCode($db, $conf->global->CHARTOFACCOUNTS, 'accounting_system', 'rowid', 'pcg_version');
+
$sql = "SELECT f.thirdparty_code as name, -1 as socid, aa.pcg_type, aa.pcg_subtype, sum(f.credit - f.debit) as amount";
$sql.= " FROM ".MAIN_DB_PREFIX."accounting_bookkeeping as f";
$sql.= ", ".MAIN_DB_PREFIX."accounting_account as aa";
$sql.= " WHERE f.numero_compte = aa.account_number";
//$sql.= " AND fk_statut in (1,2)";
- $sql.= " AND (";
- $sql.= " (pcg_type = 'EXPENSE' and pcg_subtype in ('PRODUCT','SERVICE'))";
- $sql.= " OR ";
- $sql.= " (pcg_type = 'INCOME' and pcg_subtype in ('PRODUCT','SERVICE'))";
- $sql.= ")";
+ $sql.= " AND ".$predefinedgroupwhere;
+ $sql.= " AND aa.fk_pcg_version = '".$charofaccountstring."'";
if (! empty($date_start) && ! empty($date_end))
$sql.= " AND f.doc_date >= '".$db->idate($date_start)."' AND f.doc_date <= '".$db->idate($date_end)."'";
- $sql.= " GROUP BY name, socid, pcg_type, pcg_subtype";
+ $sql.= " GROUP BY pcg_type, pcg_subtype, name, socid";
$sql.= $db->order($sortfield, $sortorder);
$oldpcgtype = '';
+ $oldpcgsubtype = '';
dol_syslog("get bookkeeping entries", LOG_DEBUG);
$result = $db->query($sql);
@@ -284,32 +291,31 @@ if ($modecompta == 'BOOKKEEPING')
// This make 14 calls for each detail of account (NP, N and month m)
if ($showaccountdetail == 'yes')
{
- // TODO Get list of account for this group/subgroup
- $cpts = array();
+ $tmppredefinedgroupwhere="pcg_type = '".$db->escape($objp->pcg_type)."' AND pcg_subtype = '".$db->escape($objp->pcg_subtype)."'";
+ $tmppredefinedgroupwhere.= " AND fk_pcg_version = '".$charofaccountstring."'";
+ //$tmppredefinedgroupwhere.= " AND thirdparty_code = '".$db->escape($objp->name)."'";
+
+ // Get cpts of category/group
+ $cpts = $AccCat->getCptsCat(0, $tmppredefinedgroupwhere);
foreach($cpts as $i => $cpt)
{
- print '