Skip to content

Commit

Permalink
FIX Accounting personalized report when using multicompany
Browse files Browse the repository at this point in the history
  • Loading branch information
eldy committed Nov 22, 2018
1 parent 21e9c02 commit 89138bc
Showing 1 changed file with 30 additions and 27 deletions.
57 changes: 30 additions & 27 deletions htdocs/accountancy/class/accountancycategory.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,16 +163,17 @@ function create($user, $notrigger=0)

// Insert request
$sql = "INSERT INTO ".MAIN_DB_PREFIX."c_accounting_category(";
if ($this->rowid > 0) $sql.= "rowid,";
$sql.= "code,";
$sql.= "label,";
$sql.= "range_account,";
$sql.= "sens,";
$sql.= "category_type,";
$sql.= "formula,";
$sql.= "position,";
$sql.= "fk_country,";
$sql.= "active";
if ($this->rowid > 0) $sql.= "rowid, ";
$sql.= "code, ";
$sql.= "label, ";
$sql.= "range_account, ";
$sql.= "sens, ";
$sql.= "category_type, ";
$sql.= "formula, ";
$sql.= "position, ";
$sql.= "fk_country, ";
$sql.= "active, ";
$sql.= "entity";
$sql.= ") VALUES (";
if ($this->rowid > 0) $sql.= " ".$this->rowid.",";
$sql.= " ".(! isset($this->code)?'NULL':"'".$this->db->escape($this->code)."'").",";
Expand All @@ -184,6 +185,7 @@ function create($user, $notrigger=0)
$sql.= " ".(! isset($this->position)?'NULL':$this->db->escape($this->position)).",";
$sql.= " ".(! isset($this->fk_country)?'NULL':$this->db->escape($this->fk_country)).",";
$sql.= " ".(! isset($this->active)?'NULL':$this->db->escape($this->active));
$sql.= ", ".$conf->entity;
$sql.= ")";

$this->db->begin();
Expand Down Expand Up @@ -237,9 +239,8 @@ function create($user, $notrigger=0)
* @param string $label Label
* @return int <0 if KO, >0 if OK
*/
function fetch($id,$code='',$label='')
function fetch($id, $code='', $label='')
{
global $langs;
$sql = "SELECT";
$sql.= " t.rowid,";
$sql.= " t.code,";
Expand All @@ -253,8 +254,12 @@ function fetch($id,$code='',$label='')
$sql.= " t.active";
$sql.= " FROM ".MAIN_DB_PREFIX."c_accounting_category as t";
if ($id) $sql.= " WHERE t.rowid = ".$id;
elseif ($code) $sql.= " WHERE t.code = '".$this->db->escape($code)."'";
elseif ($label) $sql.= " WHERE t.label = '".$this->db->escape($label)."'";
else
{
$sql.= " WHERE t.entity IN (".getEntity('c_accounting_category').")"; // Dont't use entity if you use rowid
if ($code) $sql.= " AND t.code = '".$this->db->escape($code)."'";
elseif ($label) $sql.= " AND t.label = '".$this->db->escape($label)."'";
}

dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
$resql=$this->db->query($sql);
Expand Down Expand Up @@ -480,7 +485,7 @@ public function getCptBK($id)
$sql .= " INNER JOIN " . MAIN_DB_PREFIX . "accounting_system as asy ON aa.fk_pcg_version = asy.pcg_version";
$sql .= " AND asy.rowid = " . $conf->global->CHARTOFACCOUNTS;
$sql .= " AND aa.active = 1";
$sql .= " AND aa.entity = = " . $conf->entity . ")";
$sql .= " AND aa.entity = " . $conf->entity . ")";
$sql .= " GROUP BY t.numero_compte, t.label_operation, t.doc_ref";
$sql .= " ORDER BY t.numero_compte";

Expand Down Expand Up @@ -723,25 +728,21 @@ public function getCatsCpts()
*/
public function getSumDebitCredit($cpt, $date_start, $date_end, $sens, $thirdparty_code='nofilter', $month=0, $year=0)
{
global $conf;

$sql = "SELECT SUM(t.debit) as debit, SUM(t.credit) as credit";
$sql .= " FROM " . MAIN_DB_PREFIX . "accounting_bookkeeping as t";
$sql .= " WHERE t.numero_compte = '" . $this->db->escape($cpt) . "'";
/*
if (! empty($date_start) && ! empty($date_end))
$sql.= " AND t.doc_date >= '".$this->db->idate($date_start)."' AND t.doc_date <= '".$this->db->idate($date_end)."'";
if (! empty($month)) {
$sql .= " AND MONTH(t.doc_date) = " . $month;
}
*/
if (! empty($date_start) && ! empty($date_end))
if (! empty($date_start) && ! empty($date_end) && (empty($month) || empty($year))) // If month/year provided, it is stronger than filter date_start/date_end
$sql .= " AND t.doc_date BETWEEN '".$this->db->idate($date_start)."' AND '".$this->db->idate($date_end)."'";
if (! empty($month) && ! empty($year)) {
$sql .= " AND t.doc_date BETWEEN '".$this->db->idate(dol_get_first_day($year, $month))."' AND '".$this->db->idate(dol_get_last_day($year, $month))."'";
}
if ($thirdparty_code != 'nofilter')
{
$sql .= " AND thirdparty_code = '".$this->db->escape($thirdparty_code)."'";
$sql .= " AND t.thirdparty_code = '".$this->db->escape($thirdparty_code)."'";
}
$sql .= " AND t.entity = ".$conf->entity;
//print $sql;

dol_syslog(__METHOD__ . " sql=" . $sql, LOG_DEBUG);
Expand Down Expand Up @@ -775,7 +776,7 @@ public function getSumDebitCredit($cpt, $date_start, $date_end, $sens, $thirdpar
*/
public function getCats($categorytype=-1)
{
global $db, $langs, $user, $mysoc, $conf;
global $conf, $mysoc;

if (empty($mysoc->country_id)) {
dol_print_error('', 'Call to select_accounting_account with mysoc country not yet defined');
Expand All @@ -784,7 +785,7 @@ public function getCats($categorytype=-1)

$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 ";
$sql .= " WHERE c.active = 1";
$sql .= " AND c.entity = " . $conf->entity;
if ($categorytype >= 0) $sql.=" AND c.category_type = 1";
$sql .= " AND (c.fk_country = ".$mysoc->country_id." OR c.fk_country = 0)";
Expand Down Expand Up @@ -832,7 +833,7 @@ public function getCats($categorytype=-1)
*/
public function getCptsCat($cat_id, $predefinedgroupwhere='')
{
global $mysoc;
global $conf, $mysoc;
$sql = '';

if (empty($mysoc->country_id) && empty($mysoc->country_code)) {
Expand All @@ -845,13 +846,15 @@ public function getCptsCat($cat_id, $predefinedgroupwhere='')
$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 .= " AND t.entity = " . $conf->entity;
$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 .= " AND t.entity = " . $conf->entity;
$sql .= " ORDER BY t.account_number";
}
//echo $sql;
Expand Down

0 comments on commit 89138bc

Please sign in to comment.