From f0a15ff97523f832553acd2b3a39db46e9117785 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Wed, 9 Mar 2011 10:47:17 +0000 Subject: [PATCH] Fix: pgsql compatibility --- .../comm/propal/class/propalestats.class.php | 19 ++++++++++------- htdocs/commande/class/commandestats.class.php | 21 ++++++++++++------- .../class/deplacementstats.class.php | 12 +++++++---- .../facture/class/facturestats.class.php | 10 ++++----- htdocs/install/pgsql/functions/functions.sql | 7 +++++++ 5 files changed, 45 insertions(+), 24 deletions(-) diff --git a/htdocs/comm/propal/class/propalestats.class.php b/htdocs/comm/propal/class/propalestats.class.php index da71b12b1f0..d960cedc1c0 100644 --- a/htdocs/comm/propal/class/propalestats.class.php +++ b/htdocs/comm/propal/class/propalestats.class.php @@ -88,13 +88,14 @@ class PropaleStats extends Stats if (!$user->rights->societe->client->voir && !$user->societe_id) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; $sql.= " WHERE date_format(p.datep,'%Y') = ".$year; $sql.= " AND ".$this->where; - $sql.= " GROUP BY dm DESC"; + $sql.= " GROUP BY dm"; + $sql.= $this->db->order('dm','DESC'); return $this->_getNbByMonth($year, $sql); } /** - * Renvoie le nombre de propale par ann�e + * Renvoie le nombre de propale par annee * */ function getNbByYear() @@ -105,12 +106,13 @@ class PropaleStats extends Stats $sql.= " FROM ".$this->from; if (!$user->rights->societe->client->voir && !$user->societe_id) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; $sql.= " WHERE ".$this->where; - $sql.= " GROUP BY dm DESC"; + $sql.= " GROUP BY dm"; + $sql.= $this->db->order('dm','DESC'); return $this->_getNbByYear($sql); } /** - * Renvoie le nombre de propale par mois pour une ann�e donn�e + * Renvoie le nombre de propale par mois pour une annee donnee * */ function getAmountByMonth($year) @@ -122,7 +124,8 @@ class PropaleStats extends Stats if (!$user->rights->societe->client->voir && !$user->societe_id) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; $sql.= " WHERE date_format(p.datep,'%Y') = ".$year; $sql.= " AND ".$this->where; - $sql.= " GROUP BY dm DESC"; + $sql.= " GROUP BY dm"; + $sql.= $this->db->order('dm','DESC'); return $this->_getAmountByMonth($year, $sql); } @@ -139,7 +142,8 @@ class PropaleStats extends Stats if (!$user->rights->societe->client->voir && !$this->socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; $sql.= " WHERE date_format(p.datep,'%Y') = ".$year; $sql.= " AND ".$this->where; - $sql.= " GROUP BY dm DESC"; + $sql.= " GROUP BY dm"; + $sql.= $this->db->order('dm','DESC'); return $this->_getAverageByMonth($year, $sql); } @@ -157,7 +161,8 @@ class PropaleStats extends Stats $sql.= " FROM ".$this->from; if (!$user->rights->societe->client->voir && !$this->socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; $sql.= " WHERE ".$this->where; - $sql.= " GROUP BY year DESC"; + $sql.= " GROUP BY year"; + $sql.= $this->db->order('year','DESC'); return $this->_getAllByYear($sql); } diff --git a/htdocs/commande/class/commandestats.class.php b/htdocs/commande/class/commandestats.class.php index 8e517b76004..f6917f6d872 100644 --- a/htdocs/commande/class/commandestats.class.php +++ b/htdocs/commande/class/commandestats.class.php @@ -100,13 +100,14 @@ class CommandeStats extends Stats if (!$user->rights->societe->client->voir && !$this->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; $sql.= " WHERE date_format(c.date_commande,'%Y') = ".$year; $sql.= " AND ".$this->where; - $sql.= " GROUP BY dm DESC"; + $sql.= " GROUP BY dm"; + $sql.= $this->db->order('dm','DESC'); return $this->_getNbByMonth($year, $sql); } /** - * Renvoie le nombre de commande par ann�e + * Renvoie le nombre de commande par annee * */ function getNbByYear() @@ -118,13 +119,14 @@ class CommandeStats extends Stats $sql.= " FROM ".$this->from; if (!$user->rights->societe->client->voir && !$this->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; $sql.= " WHERE ".$this->where; - $sql.= " GROUP BY dm DESC"; + $sql.= " GROUP BY dm"; + $sql.= $this->db->order('dm','DESC'); return $this->_getNbByYear($sql); } /** - * Renvoie le nombre de commande par mois pour une ann�e donn�e + * Renvoie le nombre de commande par mois pour une annee donnee * */ function getAmountByMonth($year) @@ -137,13 +139,14 @@ class CommandeStats extends Stats if (!$user->rights->societe->client->voir && !$this->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; $sql.= " WHERE date_format(c.date_commande,'%Y') = ".$year; $sql.= " AND ".$this->where; - $sql.= " GROUP BY dm DESC"; + $sql.= " GROUP BY dm"; + $sql.= $this->db->order('dm','DESC'); return $this->_getAmountByMonth($year, $sql); } /** - * Renvoie le nombre de commande par mois pour une ann�e donn�e + * Renvoie le nombre de commande par mois pour une annee donnee * */ function getAverageByMonth($year) @@ -156,7 +159,8 @@ class CommandeStats extends Stats if (!$user->rights->societe->client->voir && !$this->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; $sql.= " WHERE date_format(c.date_commande,'%Y') = ".$year; $sql.= " AND ".$this->where; - $sql.= " GROUP BY dm DESC"; + $sql.= " GROUP BY dm"; + $sql.= $this->db->order('dm','DESC'); return $this->_getAverageByMonth($year, $sql); } @@ -174,7 +178,8 @@ class CommandeStats extends Stats $sql.= " FROM ".$this->from; if (!$user->rights->societe->client->voir && !$this->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; $sql.= " WHERE ".$this->where; - $sql.= " GROUP BY year DESC"; + $sql.= " GROUP BY year"; + $sql.= $this->db->order('year','DESC'); return $this->_getAllByYear($sql); } diff --git a/htdocs/compta/deplacement/class/deplacementstats.class.php b/htdocs/compta/deplacement/class/deplacementstats.class.php index b893725c70d..77b4805bcd5 100644 --- a/htdocs/compta/deplacement/class/deplacementstats.class.php +++ b/htdocs/compta/deplacement/class/deplacementstats.class.php @@ -94,7 +94,8 @@ class DeplacementStats extends Stats $sql.= " FROM ".$this->from; $sql.= " WHERE YEAR(dated) = ".$year; $sql.= " AND ".$this->where; - $sql.= " GROUP BY dm DESC"; + $sql.= " GROUP BY dm"; + $sql.= $this->db->order('dm','DESC'); $res=$this->_getNbByMonth($year, $sql); //var_dump($res);print '
'; @@ -113,7 +114,8 @@ class DeplacementStats extends Stats $sql.= " FROM ".$this->from; $sql.= " WHERE date_format(dated,'%Y') = ".$year; $sql.= " AND ".$this->where; - $sql.= " GROUP BY dm DESC"; + $sql.= " GROUP BY dm"; + $sql.= $this->db->order('dm','DESC'); $res=$this->_getAmountByMonth($year, $sql); //var_dump($res);print '
'; @@ -131,7 +133,8 @@ class DeplacementStats extends Stats $sql.= " FROM ".$this->from; $sql.= " WHERE date_format(dated,'%Y') = ".$year; $sql.= " AND ".$this->where; - $sql.= " GROUP BY dm DESC"; + $sql.= " GROUP BY dm"; + $sql.= $this->db->order('dm','DESC'); return $this->_getAverageByMonth($year, $sql); } @@ -145,7 +148,8 @@ class DeplacementStats extends Stats $sql = "SELECT date_format(dated,'%Y') as year, count(*) as nb, sum(".$this->field.") as total, avg(".$this->field.") as avg"; $sql.= " FROM ".$this->from; $sql.= " WHERE ".$this->where; - $sql.= " GROUP BY year DESC"; + $sql.= " GROUP BY year"; + $sql.= $this->db->order('year','DESC'); return $this->_getAllByYear($sql); } diff --git a/htdocs/compta/facture/class/facturestats.class.php b/htdocs/compta/facture/class/facturestats.class.php index c9d13816452..7cf5984b1b1 100644 --- a/htdocs/compta/facture/class/facturestats.class.php +++ b/htdocs/compta/facture/class/facturestats.class.php @@ -91,7 +91,7 @@ class FactureStats extends Stats $sql.= " FROM ".$this->from; $sql.= " WHERE ".$this->where; $sql.= " GROUP BY dm"; - $sql.= " ORDER BY dm DESC"; + $sql.= $this->db->order('dm','DESC'); return $this->_getNbByYear($sql); } @@ -109,7 +109,7 @@ class FactureStats extends Stats $sql.= " WHERE datef BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'"; $sql.= " AND ".$this->where; $sql.= " GROUP BY dm"; - $sql.= " ORDER BY dm DESC"; + $sql.= $this->db->order('dm','DESC'); $res=$this->_getNbByMonth($year, $sql); //var_dump($res);print '
'; @@ -129,7 +129,7 @@ class FactureStats extends Stats $sql.= " WHERE date_format(datef,'%Y') = ".$year; $sql.= " AND ".$this->where; $sql.= " GROUP BY dm"; - $sql.= " ORDER BY dm DESC"; + $sql.= $this->db->order('dm','DESC'); $res=$this->_getAmountByMonth($year, $sql); //var_dump($res);print '
'; @@ -148,7 +148,7 @@ class FactureStats extends Stats $sql.= " WHERE datef BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'"; $sql.= " AND ".$this->where; $sql.= " GROUP BY dm"; - $sql.= " ORDER BY dm DESC"; + $sql.= $this->db->order('dm','DESC'); return $this->_getAverageByMonth($year, $sql); } @@ -163,7 +163,7 @@ class FactureStats extends Stats $sql.= " FROM ".$this->from; $sql.= " WHERE ".$this->where; $sql.= " GROUP BY year"; - $sql.= " ORDER BY year DESC"; + $sql.= $this->db->order('year','DESC'); return $this->_getAllByYear($sql); } diff --git a/htdocs/install/pgsql/functions/functions.sql b/htdocs/install/pgsql/functions/functions.sql index a5cd735053f..100d9fe82b0 100644 --- a/htdocs/install/pgsql/functions/functions.sql +++ b/htdocs/install/pgsql/functions/functions.sql @@ -29,6 +29,13 @@ CREATE OR REPLACE FUNCTION UNIX_TIMESTAMP(TIMESTAMP WITH TIME ZONE) RETURNS BIGI CREATE OR REPLACE FUNCTION date_format(timestamp without time zone, text) RETURNS text AS $$ DECLARE i int := 1; temp text := ''; c text; n text; res text; BEGIN WHILE i <= pg_catalog.length($2) LOOP c := SUBSTRING ($2 FROM i FOR 1); IF c = '%' AND i != pg_catalog.length($2) THEN n := SUBSTRING ($2 FROM (i + 1) FOR 1); SELECT INTO res CASE WHEN n = 'a' THEN pg_catalog.to_char($1, 'Dy') WHEN n = 'b' THEN pg_catalog.to_char($1, 'Mon') WHEN n = 'c' THEN pg_catalog.to_char($1, 'FMMM') WHEN n = 'D' THEN pg_catalog.to_char($1, 'FMDDth') WHEN n = 'd' THEN pg_catalog.to_char($1, 'DD') WHEN n = 'e' THEN pg_catalog.to_char($1, 'FMDD') WHEN n = 'f' THEN pg_catalog.to_char($1, 'US') WHEN n = 'H' THEN pg_catalog.to_char($1, 'HH24') WHEN n = 'h' THEN pg_catalog.to_char($1, 'HH12') WHEN n = 'I' THEN pg_catalog.to_char($1, 'HH12') WHEN n = 'i' THEN pg_catalog.to_char($1, 'MI') WHEN n = 'j' THEN pg_catalog.to_char($1, 'DDD') WHEN n = 'k' THEN pg_catalog.to_char($1, 'FMHH24') WHEN n = 'l' THEN pg_catalog.to_char($1, 'FMHH12') WHEN n = 'M' THEN pg_catalog.to_char($1, 'FMMonth') WHEN n = 'm' THEN pg_catalog.to_char($1, 'MM') WHEN n = 'p' THEN pg_catalog.to_char($1, 'AM') WHEN n = 'r' THEN pg_catalog.to_char($1, 'HH12:MI:SS AM') WHEN n = 'S' THEN pg_catalog.to_char($1, 'SS') WHEN n = 's' THEN pg_catalog.to_char($1, 'SS') WHEN n = 'T' THEN pg_catalog.to_char($1, 'HH24:MI:SS') WHEN n = 'U' THEN pg_catalog.to_char($1, '?') WHEN n = 'u' THEN pg_catalog.to_char($1, '?') WHEN n = 'V' THEN pg_catalog.to_char($1, '?') WHEN n = 'v' THEN pg_catalog.to_char($1, '?') WHEN n = 'W' THEN pg_catalog.to_char($1, 'FMDay') WHEN n = 'w' THEN EXTRACT(DOW FROM $1)::text WHEN n = 'X' THEN pg_catalog.to_char($1, '?') WHEN n = 'x' THEN pg_catalog.to_char($1, '?') WHEN n = 'Y' THEN pg_catalog.to_char($1, 'YYYY') WHEN n = 'y' THEN pg_catalog.to_char($1, 'YY') WHEN n = '%' THEN pg_catalog.to_char($1, '%') ELSE NULL END; temp := temp operator(pg_catalog.||) res; i := i + 2; ELSE temp = temp operator(pg_catalog.||) c; i := i + 1; END IF; END LOOP; RETURN temp; END $$ IMMUTABLE STRICT LANGUAGE PLPGSQL; +CREATE OR REPLACE FUNCTION YEAR(TIMESTAMP without TIME ZONE) RETURNS INTEGER AS $$ SELECT EXTRACT(YEAR FROM $1)::INTEGER; $$ LANGUAGE SQL IMMUTABLE; + +CREATE OR REPLACE FUNCTION YEAR(TIMESTAMP WITH TIME ZONE) RETURNS INTEGER AS $$ SELECT EXTRACT(YEAR FROM $1)::INTEGER; $$ LANGUAGE SQL STABLE; + +CREATE OR REPLACE FUNCTION YEAR(DATE) RETURNS INTEGER AS $$ SELECT EXTRACT(YEAR FROM $1)::INTEGER; $$ LANGUAGE SQL IMMUTABLE; + + CREATE OR REPLACE FUNCTION MONTH(TIMESTAMP without TIME ZONE) RETURNS INTEGER AS $$ SELECT EXTRACT(MONTH FROM $1)::INTEGER; $$ LANGUAGE SQL IMMUTABLE; CREATE OR REPLACE FUNCTION MONTH(TIMESTAMP WITH TIME ZONE) RETURNS INTEGER AS $$ SELECT EXTRACT(MONTH FROM $1)::INTEGER; $$ LANGUAGE SQL STABLE;