get_value('select f_id from vw_poste_qcode where j_qcode=$1',array($a_detail[$f]['j_qcode'])) ;
- $lib=$cn->get_value('select av_text from jnt_fic_att_value join attr_value using (jft_id) where ad_id=$1 and f_id=$2',
+ $lib=$cn->get_value('select ad_value from fiche_detail where ad_id=$1 and f_id=$2',
array(ATTR_DEF_NAME,$f_id));
}
$pdf->Cell($size[$l],6,$lib,0,0,$align[$l]);
diff --git a/include/class_contact.php b/include/class_contact.php
index 34f527686..2593d5041 100644
--- a/include/class_contact.php
+++ b/include/class_contact.php
@@ -53,8 +53,8 @@ class contact extends Fiche
$extra_sql="";
if ( $this->company != "")
{
- $extra_sql="and f_id in (select f_id from jnt_fic_att_value join
- attr_value using (jft_id) where av_text=upper('".$this->company."') and ad_id=".ATTR_DEF_COMPANY.") ";
+ $extra_sql="and f_id in (select f_id from fiche_detail
+ where ad_value=upper('".$this->company."') and ad_id=".ATTR_DEF_COMPANY.") ";
}
$url=urlencode($_SERVER['REQUEST_URI']);
$script=$_SERVER['PHP_SELF'];
@@ -70,11 +70,9 @@ class contact extends Fiche
if ( trim($p_search) != "" )
{
$search=" and f_id in
- (select f_id from jnt_fic_att_value
- join fiche using (f_id)
- join attr_value using (jft_id)
+ (select f_id from fiche_Detail
where
- ad_id=1 and av_text ~* '$p_search') ";
+ ad_id=1 and ad_value ~* '$p_search') ";
}
// Get The result Array
$step_contact=$this->GetAll($offset,$search.$extra_sql.$p_sql);
diff --git a/include/class_fiche.php b/include/class_fiche.php
index 4085370b9..b2376f7a4 100644
--- a/include/class_fiche.php
+++ b/include/class_fiche.php
@@ -79,8 +79,8 @@ class Fiche
if ( $p_qcode == null )
$p_qcode=$this->quick_code;
$p_qcode=trim($p_qcode);
- $sql="select f_id from jnt_fic_att_value join attr_value
- using (jft_id) where ad_id=23 and av_text=upper($1)";
+ $sql="select f_id from fiche_detail
+ where ad_id=23 and ad_value=upper($1)";
$this->id=$this->cn->get_value($sql,array($p_qcode));
if ( $this->cn->count()==0)
{
@@ -123,11 +123,11 @@ class Fiche
return;
}
$sql="select *
- from jnt_fic_att_value
- natural join fiche
- natural join attr_value
- join jnt_fic_attr on (jnt_fic_attr.fd_id=fiche.fd_id and jnt_fic_attr.ad_id=jnt_fic_att_value.ad_id)
- join attr_def on (attr_def.ad_id=jnt_fic_att_value.ad_id) where f_id=".$this->id.
+ from
+ fiche
+ natural join fiche_detail
+ join jnt_fic_attr on (jnt_fic_attr.fd_id=fiche.fd_id and fiche_detail.ad_id=jnt_fic_attr.ad_id)
+ join attr_def on (attr_def.ad_id=fiche_detail.ad_id) where f_id=".$this->id.
" order by jnt_order";
$Ret=$this->cn->exec_sql($sql);
@@ -140,7 +140,7 @@ class Fiche
$t=new Fiche_Attr ($this->cn);
$t->ad_id=$row['ad_id'];
$t->ad_text=$row['ad_text'];
- $t->av_text=$row['av_text'];
+ $t->av_text=$row['ad_value'];
$t->ad_type=$row['ad_type'];
$t->ad_size=$row['ad_size'];
$t->jnt_order=$row['jnt_order'];
@@ -188,8 +188,8 @@ class Fiche
*/
function seek($p_attribut,$p_value)
{
- $sql="select jft_id,f_id,fd_id,ad_id,av_text from fiche join jnt_fic_att_value using (f_id) join attr_value
- using (jft_id) where ad_id=$1 and upper(av_text)=upper($2)";
+ $sql="select jft_id,f_id,fd_id,ad_id,ad_value from fiche join fiche_detail using (f_id)
+ where ad_id=$1 and upper(ad_value)=upper($2)";
$res=$this->cn->get_array($sql,array($p_attribut,$p_value));
return $res;
}
@@ -317,7 +317,7 @@ class Fiche
if ($this->id==0) return NOTFOUND;
// object is not in memory we need to look into the database
- $sql="select av_text from attr_value join jnt_fic_att_value using(jft_id)
+ $sql="select ad_value from fiche_detail
where f_id=".FormatString($this->id)." and ad_id=".$p_ad_id;
$Res=$this->cn->exec_sql($sql);
$row=Database::fetch_all($Res);
@@ -325,7 +325,7 @@ class Fiche
if ( $row == false )
return NOTFOUND;
- return $row[0]['av_text'];
+ return $row[0]['ad_value'];
}
foreach ($this->attribut as $e)
@@ -712,9 +712,9 @@ class Fiche
if ( $id == ATTR_DEF_COMPANY )
{
$exist=$this->cn->count_sql("select f_id from fiche join fiche_def using (fd_id) ".
- " join jnt_fic_att_value using (f_id) join attr_value using (jft_id) ".
+ " join fiche_detail using(f_id) ".
" where frd_id in (8,9,14) and ad_id=".ATTR_DEF_QUICKCODE.
- " and av_text='".FormatString($value)."'");
+ " and ad_value='".FormatString($value)."'");
if ( $exist == 0 && FormatString($value) != null )
{
$value="";
@@ -757,21 +757,17 @@ class Fiche
if ( $id == null ) continue;
// retrieve jft_id to update table attr_value
- $sql=" select jft_id from jnt_fic_att_value where ad_id=$id and f_id=$this->id";
+ $sql=" select jft_id from fiche_detail where ad_id=$id and f_id=$this->id";
$Ret=$this->cn->exec_sql($sql);
if ( Database::num_row($Ret) != 1 )
{
// we need to insert this new attribut
$jft_id=$this->cn->get_next_seq('s_jnt_fic_att_value');
- $sql2=sprintf("insert into jnt_fic_att_value(jft_id,ad_id,f_id) values (%s,%s,%s)",
- $jft_id,$id,$this->id);
+ $sql2=sprintf("insert into fiche_detail(jft_id,ad_id,f_id,ad_value) values ($1,$2,$3,NULL)");
+
+ $ret2=$this->cn->exec_sql($sql2,array($jft_id,$id,$this->id));
- $ret2=$this->cn->exec_sql($sql2);
- // insert a null value for this attribut
- $sql3=sprintf("insert into attr_value(jft_id,av_text) values (%s,null)",
- $jft_id);
- $ret3=$this->cn->exec_sql($sql3);
}
else
{
@@ -883,9 +879,9 @@ class Fiche
if ( $id == ATTR_DEF_COMPANY )
{
$exist=$this->cn->count_sql("select f_id from fiche join fiche_def using (fd_id) ".
- " join jnt_fic_att_value using (f_id) join attr_value using (jft_id) ".
+ " join fiche_detail using (f_id) ".
" where frd_id in (8,9,14) and ad_id=".ATTR_DEF_QUICKCODE.
- " and av_text='".FormatString($value)."'");
+ " and ad_value='".FormatString($value)."'");
if ( $exist == 0 && FormatString($value) != null )
@@ -896,7 +892,7 @@ class Fiche
// Normal traitement
$value2=FormatString($value);
- $sql=sprintf("update attr_value set av_text='%s' where jft_id=%d",
+ $sql=sprintf("update fiche_detail set ad_value='%s' where jft_id=%d",
trim($value2),$jft_id);
$this->cn->exec_sql($sql);
}
@@ -943,13 +939,13 @@ class Fiche
*/
function getName()
{
- $sql="select av_text from jnt_fic_att_value join attr_value
- using (jft_id) where ad_id=1 and f_id=".$this->id;
+ $sql="select ad_value from fiche_detail
+ where ad_id=1 and f_id=".$this->id;
$Res=$this->cn->exec_sql($sql);
$r=Database::fetch_all($Res);
if ( sizeof($r) == 0 )
return 1;
- return $r[0]['av_text'];
+ return $r[0]['ad_value'];
}
/*!\brief return the quick_code of a card
@@ -957,13 +953,12 @@ class Fiche
*/
function get_quick_code()
{
- $sql="select av_text from jnt_fic_att_value join attr_value
- using (jft_id) where ad_id=23 and f_id=".$this->id;
+ $sql="select ad_value from fiche_detail where ad_id=23 and f_id=".$this->id;
$Res=$this->cn->exec_sql($sql);
$r=Database::fetch_all($Res);
if ( sizeof($r) == 0 )
return null;
- return $r[0]['av_text'];
+ return $r[0]['ad_value'];
}
/*!\brief Synonum of fiche::getAttribut
@@ -1325,10 +1320,9 @@ class Fiche
*/
function empty_attribute($p_attr)
{
- $sql="select av_text
- from jnt_fic_att_value
+ $sql="select ad_value
+ from fiche_detail
natural join fiche
- natural join attr_value
left join attr_def using (ad_id) where f_id=".$this->id.
" and ad_id = ".$p_attr.
" order by ad_id";
@@ -1382,11 +1376,9 @@ class Fiche
if ( trim($p_search) != "" )
{
$search.=" and f_id in
- (select f_id from jnt_fic_att_value
- join fiche using (f_id)
- join attr_value using (jft_id)
+ (select f_id from fiche_detail
where
- ad_id=1 and av_text ~* '$p_search')";
+ ad_id=1 and ad_value ~* '$p_search')";
}
// Get The result Array
$step_tiers=$this->GetAll($offset,$search.$filter_amount,'name');
@@ -1596,12 +1588,9 @@ class Fiche
function delete()
{
// Remove from attr_value
- $Res=$this->cn->exec_sql("delete from attr_value
- where jft_id in (select jft_id
- from jnt_fic_att_value
- natural join fiche where f_id=".$this->id.")");
- // Remove from jnt_fic_att_value
- $Res=$this->cn->exec_sql("delete from jnt_fic_att_value where f_id=".$this->id);
+ $Res=$this->cn->exec_sql("delete from fiche_detail
+ where
+ f_id=".$this->id);
// Remove from fiche
$Res=$this->cn->exec_sql("delete from fiche where f_id=".$this->id);
@@ -1706,7 +1695,7 @@ class Fiche
// add missing
$this->cn->exec_sql('select fiche_attribut_synchro($1)',array($p_fdid));
// add to the destination missing fields
- $this->cn->exec_sql("insert into jnt_fic_attr (fd_id,ad_id,jnt_order) select $1,ad_id,100 from jnt_fic_att_value where f_id=$2 and ad_id not in (select ad_id from jnt_fic_attr where fd_id=$3)",array($p_fdid,$this->id,$p_fdid));
+ $this->cn->exec_sql("insert into jnt_fic_attr (fd_id,ad_id,jnt_order) select $1,ad_id,100 from fiche_detail where f_id=$2 and ad_id not in (select ad_id from jnt_fic_attr where fd_id=$3)",array($p_fdid,$this->id,$p_fdid));
$this->cn->commit();
}
diff --git a/include/class_fiche_attr.php b/include/class_fiche_attr.php
index b0af48415..53f6fd57d 100644
--- a/include/class_fiche_attr.php
+++ b/include/class_fiche_attr.php
@@ -192,10 +192,8 @@ class Fiche_Attr
public function delete()
{
if ($this->ad_id < 9000) return;
- $sql=$this->cn->exec_sql("delete from attr_value where jft_id in (select jft_id from jnt_fic_att_value where ad_id=$1 )",
+ $sql=$this->cn->exec_sql("delete from fiche_detail where ad_id=$1 )",
array($this->ad_id));
- $sql="delete from jnt_fic_att_value where ad_id=$1";
- $res=$this->cn->exec_sql($sql,array($this->ad_id));
$sql="delete from jnt_fic_attr where ad_id=$1";
$res=$this->cn->exec_sql($sql,array($this->ad_id));
diff --git a/include/class_fiche_def.php b/include/class_fiche_def.php
index 7c80bdcc5..c64a47a25 100644
--- a/include/class_fiche_def.php
+++ b/include/class_fiche_def.php
@@ -364,9 +364,8 @@ class Fiche_Def
$page=(isset($_GET['page']))?$_GET['page']:1;
$offset=(isset($_GET['offset']))?$_GET['offset']:0;
- $max_line=$this->cn->count_sql("select f_id,av_text from
- fiche join jnt_fic_att_value using (f_id)
- join attr_value using (jft_id)
+ $max_line=$this->cn->count_sql("select f_id,ad_value from
+ fiche join fiche_detail using (f_id)
where fd_id='".$this->id."' and ad_id=".ATTR_DEF_NAME." order by f_id");
$sql_limit=" limit ".$step;
$sql_offset=" offset ".$offset;
@@ -611,20 +610,14 @@ class Fiche_Def
" and ad_id=".$ch;
$this->cn->exec_sql($sql);
- $sql="delete from attr_value where jft_id in ( select ".
- " jft_id from attr_value join jnt_fic_att_value using (jft_id) ".
+ $sql="delete from fiche_detail where jft_id in ( select ".
+ " jft_id from fiche_Detail ".
" join fiche using(f_id) ".
" where ".
"fd_id = ".$this->id." and ".
"ad_id=".$ch.")";
$this->cn->exec_sql($sql);
- $sql="delete from jnt_fic_att_value where jft_id in (".
- " select jft_id from jnt_fic_att_value join fiche using (f_id) ".
- " where ".
- " fd_id = ".$this->id." and ".
- " ad_id = ".$ch.")";
- $this->cn->exec_sql($sql);
$this->cn->commit();
}
}
diff --git a/include/class_print_ledger_detail.php b/include/class_print_ledger_detail.php
index 6a7f569f5..6bde5c020 100644
--- a/include/class_print_ledger_detail.php
+++ b/include/class_print_ledger_detail.php
@@ -118,7 +118,7 @@ class Print_Ledger_Detail extends PDF
{
$f_id=$this->cn->get_value('select f_id from vw_poste_qcode where j_qcode=$1',array($entry['j_qcode']));
if ($f_id != '')
- $name=$this->cn->get_value('select av_text from attr_value join jnt_fic_att_value using(jft_id) where f_id=$1 and ad_id=1',
+ $name=$this->cn->get_value('select ad_value from fiche_detail where f_id=$1 and ad_id=1',
array($f_id));
else
$name=$entry['pcm_lib'];
diff --git a/include/fiche.inc.php b/include/fiche.inc.php
index 089505422..6aa92bd03 100644
--- a/include/fiche.inc.php
+++ b/include/fiche.inc.php
@@ -345,9 +345,9 @@ if ( isset ( $_GET["action"]) )
{
echo '';
ShowRecherche();
- $sql="select distinct f_id,fd_id from fiche join jnt_fic_att_value using (f_id)
- join attr_value using (jft_id) where
- upper(av_text) like upper('%".FormatString($_GET["search_text"])."%') order by f_id";
+ $sql="select distinct f_id,fd_id from fiche join fiche_detail using (f_id)
+ where
+ upper(ad_value) like upper('%".FormatString($_GET["search_text"])."%') order by f_id";
$all=$cn->get_array($sql);
// test on the size
diff --git a/include/modele.inc.php b/include/modele.inc.php
index 233ee1628..9ad22ab71 100644
--- a/include/modele.inc.php
+++ b/include/modele.inc.php
@@ -183,8 +183,7 @@ if ( isset ($_POST["FMOD_NAME"]) )
}
if ( isset($_POST['CARD']))
{
- $Res=$cn_mod->exec_sql("delete from attr_value");
- $Res=$cn_mod->exec_sql("delete from jnt_fic_att_value");
+ $Res=$cn_mod->exec_sql("delete from fiche_detail");
$Res=$cn_mod->exec_sql("delete from fiche");
$Res=$cn_mod->exec_sql("delete from action_gestion");
$Res=$cn_mod->exec_sql("delete from document");
diff --git a/include/stock_inc.php b/include/stock_inc.php
index 75047d18b..adfe1b963 100644
--- a/include/stock_inc.php
+++ b/include/stock_inc.php
@@ -126,10 +126,9 @@ function ViewStock($p_cn,$p_year)
function getFicheNameCode ($p_cn,$p_sg_code)
{
// Sql stmt
- $sql="select distinct f_id,av_text
+ $sql="select distinct f_id,ad_value
from stock_goods
- join jnt_fic_att_value using (f_id )
- join attr_value using (jft_id)
+ join fiche_detail
where
ad_id=".ATTR_DEF_STOCK."
and sg_code='$p_sg_code'
diff --git a/sql/upgrade.sql b/sql/upgrade.sql
index 8b1378917..dd4abfe09 100644
--- a/sql/upgrade.sql
+++ b/sql/upgrade.sql
@@ -1 +1,498 @@
+alter table jnt_fic_att_value add ad_value text;
+update jnt_fic_att_value set ad_value=av_text from attr_value where jnt_fic_att_value.jft_id=attr_value.jft_id;
+DROP VIEW vw_supplier;
+DROP VIEW vw_poste_qcode;
+DROP VIEW vw_fiche_attr;
+DROP VIEW vw_fiche_name;
+DROP VIEW vw_client;
+alter table jnt_fic_att_value rename to fiche_detail;
+drop table attr_value;
+
+CREATE OR REPLACE VIEW vw_poste_qcode AS
+ SELECT c.f_id, a.ad_value AS j_poste, b.ad_value AS j_qcode
+ FROM
+ fiche c
+ left join
+ ( SELECT f_id,ad_value from fiche_Detail
+ WHERE ad_id = 5) a using(f_id)
+ left JOIN ( select f_id,ad_value from fiche_detail
+ WHERE ad_id = 23) b USING (f_id);
+
+
+CREATE OR REPLACE VIEW vw_client AS
+ SELECT fiche.f_id, a1.ad_value AS name, a.ad_value AS quick_code, b.ad_value AS tva_num, c.ad_value AS poste_comptable, d.ad_value AS rue, e.ad_value AS code_postal, f.ad_value AS pays, g.ad_value AS telephone, h.ad_value AS email
+ FROM fiche
+ join fiche_def using (fd_id)
+ join fiche_def_ref using(frd_id)
+ left JOIN ( SELECT jft_id, F_ID, ad_id, ad_value
+ from fiche_detail
+ WHERE ad_id = 1) a1 USING (f_id)
+ left JOIN ( SELECT jft_id, F_ID, ad_id, ad_value
+ from fiche_detail
+ WHERE ad_id = 13) b USING (f_id)
+ left JOIN ( SELECT jft_id, F_ID, ad_id, ad_value
+ from fiche_detail
+ WHERE ad_id = 23) a USING (f_id)
+ left JOIN ( SELECT jft_id, F_ID, ad_id, ad_value
+ from fiche_detail
+ WHERE ad_id = 5) c USING (f_id)
+ left JOIN ( SELECT jft_id, F_ID, ad_id, ad_value
+ from fiche_detail
+ WHERE ad_id = 14) d USING (f_id)
+ left JOIN ( SELECT jft_id, F_ID, ad_id, ad_value
+ from fiche_detail
+ WHERE ad_id = 15) e USING (f_id)
+ left JOIN ( SELECT jft_id, F_ID, ad_id, ad_value
+ from fiche_detail
+ WHERE ad_id = 16) f USING (f_id)
+ left JOIN ( SELECT jft_id, F_ID, ad_id, ad_value
+ from fiche_detail
+ WHERE ad_id = 17) g USING (f_id)
+ LEFT JOIN ( SELECT jft_id, F_ID, ad_id, ad_value
+ from fiche_detail
+ WHERE ad_id = 18) h USING (f_id)
+ WHERE fiche_def_ref.frd_id = 9;
+
+CREATE OR REPLACE VIEW vw_fiche_name AS
+ SELECT f_id, ad_value AS name
+ FROM fiche_detail
+ WHERE ad_id = 1;
+
+ CREATE OR REPLACE VIEW vw_supplier AS
+ SELECT fiche.f_id, a1.ad_value AS name, a.ad_value AS quick_code, b.ad_value AS tva_num, c.ad_value AS poste_comptable, d.ad_value AS rue, e.ad_value AS code_postal, f.ad_value AS pays, g.ad_value AS telephone, h.ad_value AS email
+ FROM fiche
+ join fiche_def using (fd_id)
+ join fiche_def_ref using(frd_id)
+ left JOIN ( SELECT jft_id, F_ID, ad_id, ad_value
+ from fiche_detail
+ WHERE ad_id = 1) a1 USING (f_id)
+ left JOIN ( SELECT jft_id, F_ID, ad_id, ad_value
+ from fiche_detail
+ WHERE ad_id = 13) b USING (f_id)
+ left JOIN ( SELECT jft_id, F_ID, ad_id, ad_value
+ from fiche_detail
+ WHERE ad_id = 23) a USING (f_id)
+ left JOIN ( SELECT jft_id, F_ID, ad_id, ad_value
+ from fiche_detail
+ WHERE ad_id = 5) c USING (f_id)
+ left JOIN ( SELECT jft_id, F_ID, ad_id, ad_value
+ from fiche_detail
+ WHERE ad_id = 14) d USING (f_id)
+ left JOIN ( SELECT jft_id, F_ID, ad_id, ad_value
+ from fiche_detail
+ WHERE ad_id = 15) e USING (f_id)
+ left JOIN ( SELECT jft_id, F_ID, ad_id, ad_value
+ from fiche_detail
+ WHERE ad_id = 16) f USING (f_id)
+ left JOIN ( SELECT jft_id, F_ID, ad_id, ad_value
+ from fiche_detail
+ WHERE ad_id = 17) g USING (f_id)
+ LEFT JOIN ( SELECT jft_id, F_ID, ad_id, ad_value
+ from fiche_detail
+ WHERE ad_id = 18) h USING (f_id)
+ WHERE fiche_def_ref.frd_id = 8;
+
+
+CREATE OR REPLACE VIEW vw_fiche_attr AS
+ SELECT a.f_id, a.fd_id, a.ad_value AS vw_name, b.ad_value AS vw_sell, c.ad_value AS vw_buy, d.ad_value AS tva_code, tva_rate.tva_id, tva_rate.tva_rate, tva_rate.tva_label, e.ad_value AS vw_addr, f.ad_value AS vw_cp, j.ad_value AS quick_code, h.ad_value AS vw_description, i.ad_value AS tva_num, fiche_def.frd_id
+ FROM ( SELECT fiche.f_id, fiche.fd_id, ad_value
+ FROM fiche
+ left join fiche_detail using (f_id)
+ WHERE ad_id = 1) a
+ LEFT JOIN (
+ select f_id ,ad_value from fiche_detail
+ WHERE ad_id = 6) b ON a.f_id = b.f_id
+ LEFT JOIN ( select f_id,ad_value from fiche_detail
+ WHERE ad_id = 7) c ON a.f_id = c.f_id
+ LEFT JOIN ( select f_id,ad_value from fiche_detail
+ WHERE ad_id = 2) d ON a.f_id = d.f_id
+ LEFT JOIN ( select f_id,ad_value from fiche_detail
+ WHERE ad_id = 14) e ON a.f_id = e.f_id
+ LEFT JOIN ( select f_id,ad_value from fiche_detail
+ WHERE ad_id = 15) f ON a.f_id = f.f_id
+ LEFT JOIN ( select f_id,ad_value from fiche_detail
+ WHERE ad_id = 23) j ON a.f_id = j.f_id
+ LEFT JOIN ( select f_id,ad_value from fiche_detail
+ WHERE ad_id = 9) h ON a.f_id = h.f_id
+ LEFT JOIN ( select f_id,ad_value from fiche_detail
+ WHERE ad_id = 13) i ON a.f_id = i.f_id
+ LEFT JOIN tva_rate ON d.ad_value = tva_rate.tva_id::text
+ JOIN fiche_def USING (fd_id);
+
+
+--
+-- Name: account_insert(integer, text); Type: FUNCTION; Schema: comptaproc; Owner: dany
+--
+
+CREATE FUNCTION account_insert(p_f_id integer, p_account text) RETURNS integer
+ LANGUAGE plpgsql
+ AS $_$
+declare
+ nParent tmp_pcmn.pcm_val_parent%type;
+ sName varchar;
+ nNew tmp_pcmn.pcm_val%type;
+ bAuto bool;
+ nFd_id integer;
+ sClass_Base fiche_def.fd_class_base%TYPE;
+ nCount integer;
+ first text;
+ second text;
+begin
+
+ if length(trim(p_account)) != 0 then
+ -- if there is coma in p_account, treat normally
+ if position (',' in p_account) = 0 then
+ raise info 'p_account is not empty';
+ select count(*) into nCount from tmp_pcmn where pcm_val=p_account::account_type;
+ raise notice 'found in tmp_pcm %',nCount;
+ if nCount !=0 then
+ raise info 'this account exists in tmp_pcmn ';
+ perform attribut_insert(p_f_id,5,p_account);
+ else
+ -- account doesn't exist, create it
+ select ad_value into sName from
+ fiche_detail
+ where
+ ad_id=1 and f_id=p_f_id;
+
+ nParent:=account_parent(p_account::account_type);
+ insert into tmp_pcmn(pcm_val,pcm_lib,pcm_val_parent) values (p_account::account_type,sName,nParent);
+ perform attribut_insert(p_f_id,5,p_account);
+
+ end if;
+ else
+ raise info 'presence of a comma';
+ -- there is 2 accounts separated by a comma
+ first := split_part(p_account,',',1);
+ second := split_part(p_account,',',2);
+ -- check there is no other coma
+ raise info 'first value % second value %', first, second;
+
+ if position (',' in first) != 0 or position (',' in second) != 0 then
+ raise exception 'Too many comas, invalid account';
+ end if;
+ perform attribut_insert(p_f_id,5,p_account);
+ end if;
+ else
+ raise info 'p_account is empty';
+ select fd_id into nFd_id from fiche where f_id=p_f_id;
+ bAuto:= account_auto(nFd_id);
+
+ select fd_class_base into sClass_base from fiche_def where fd_id=nFd_id;
+raise info 'sClass_Base : %',sClass_base;
+ if bAuto = true and sClass_base similar to '^[[:digit:]]*$' then
+ raise info 'account generated automatically';
+ nNew:=account_compute(p_f_id);
+ raise info 'nNew %', nNew;
+ select ad_value into sName from
+ fiche_detail
+ where
+ ad_id=1 and f_id=p_f_id;
+ nParent:=account_parent(nNew);
+ perform account_add (nNew,sName);
+ perform attribut_insert(p_f_id,5,nNew);
+
+ else
+ -- if there is an account_base then it is the default
+ select fd_class_base::account_type into nNew from fiche_def join fiche using (fd_id) where f_id=p_f_id;
+ if nNew is null or length(trim(nNew)) = 0 then
+ raise notice 'count is null';
+ perform attribut_insert(p_f_id,5,null);
+ else
+ perform attribut_insert(p_f_id,5,nNew);
+ end if;
+ end if;
+ end if;
+
+return 0;
+end;
+$_$;
+
+--
+-- Name: account_update(integer, public.account_type); Type: FUNCTION; Schema: comptaproc; Owner: dany
+--
+
+CREATE FUNCTION account_update(p_f_id integer, p_account public.account_type) RETURNS integer
+ LANGUAGE plpgsql
+ AS $$
+declare
+ nMax fiche.f_id%type;
+ nCount integer;
+ nParent tmp_pcmn.pcm_val_parent%type;
+ sName varchar;
+ first text;
+ second text;
+begin
+
+ if length(trim(p_account)) != 0 then
+ if position (',' in p_account) = 0 then
+ select count(*) into nCount from tmp_pcmn where pcm_val=p_account;
+ if nCount = 0 then
+ select ad_value into sName from
+ fiche_detail
+ where
+ ad_id=1 and f_id=p_f_id;
+ nParent:=account_parent(p_account);
+ insert into tmp_pcmn(pcm_val,pcm_lib,pcm_val_parent) values (p_account,sName,nParent);
+ end if;
+ else
+ raise info 'presence of a comma';
+ -- there is 2 accounts separated by a comma
+ first := split_part(p_account,',',1);
+ second := split_part(p_account,',',2);
+ -- check there is no other coma
+ raise info 'first value % second value %', first, second;
+
+ if position (',' in first) != 0 or position (',' in second) != 0 then
+ raise exception 'Too many comas, invalid account';
+ end if;
+ end if;
+ end if;
+
+ update fiche_detail set ad_value=p_account where f_id=p_f_id and ad_id=5 ;
+
+return 0;
+end;
+$$;
+
+--
+-- Name: attribut_insert(integer, integer, character varying); Type: FUNCTION; Schema: comptaproc; Owner: dany
+--
+
+CREATE FUNCTION attribut_insert(p_f_id integer, p_ad_id integer, p_value character varying) RETURNS void
+ LANGUAGE plpgsql
+ AS $$
+begin
+ insert into fiche_detail (f_id,ad_id, ad_value) values (p_f_id,p_ad_id,p_value);
+
+return;
+end;
+$$;
+
+--
+-- Name: fiche_attribut_synchro(integer); Type: FUNCTION; Schema: comptaproc; Owner: dany
+--
+
+CREATE FUNCTION fiche_attribut_synchro(p_fd_id integer) RETURNS void
+ LANGUAGE plpgsql
+ AS $$
+declare
+ -- this sql gives the f_id and the missing attribute (ad_id)
+ list_missing cursor for select f_id,fd_id,ad_id,jnt_order from jnt_fic_attr join fiche as A using (fd_id) where fd_id=p_fd_id and ad_id not in (select ad_id from fiche join fiche_detail using (f_id) where fd_id=jnt_fic_attr.fd_id and A.f_id=f_id);
+ rec record;
+begin
+ open list_missing;
+ loop
+
+ fetch list_missing into rec;
+ IF NOT FOUND then
+ exit;
+ end if;
+
+ -- now we insert into attr_value
+ insert into fiche_detail (f_id,ad_id,ad_value) values (rec.f_id,rec.ad_id,null);
+ end loop;
+ close list_missing;
+end;
+$$;
+
+--
+-- Name: insert_quant_sold(text, numeric, character varying, numeric, numeric, numeric, integer, character varying); Type: FUNCTION; Schema: comptaproc; Owner: dany
+--
+
+CREATE FUNCTION insert_quant_sold(p_internal text, p_jid numeric, p_fiche character varying, p_quant numeric, p_price numeric, p_vat numeric, p_vat_code integer, p_client character varying) RETURNS void
+ LANGUAGE plpgsql
+ AS $$
+declare
+ fid_client integer;
+ fid_good integer;
+begin
+
+ select f_id into fid_client from
+ fiche_detail where ad_id=23 and ad_value=upper(trim(p_client));
+ select f_id into fid_good from
+ fiche_detail where ad_id=23 and ad_value=upper(trim(p_fiche));
+ insert into quant_sold
+ (qs_internal,j_id,qs_fiche,qs_quantite,qs_price,qs_vat,qs_vat_code,qs_client,qs_valid)
+ values
+ (p_internal,p_jid,fid_good,p_quant,p_price,p_vat,p_vat_code,fid_client,'Y');
+ return;
+end;
+ $$;
+
+--
+-- Name: insert_quant_purchase(text, numeric, character varying, numeric, numeric, numeric, integer, numeric, numeric, numeric, numeric, character varying); Type: FUNCTION; Schema: comptaproc; Owner: dany
+--
+
+CREATE FUNCTION insert_quant_purchase(p_internal text, p_j_id numeric, p_fiche character varying, p_quant numeric, p_price numeric, p_vat numeric, p_vat_code integer, p_nd_amount numeric, p_nd_tva numeric, p_nd_tva_recup numeric, p_dep_priv numeric, p_client character varying) RETURNS void
+ LANGUAGE plpgsql
+ AS $$
+declare
+ fid_client integer;
+ fid_good integer;
+begin
+ select f_id into fid_client from
+ fiche_detail where ad_id=23 and ad_value=upper(trim(p_client));
+ select f_id into fid_good from
+ fiche_detail where ad_id=23 and ad_value=upper(trim(p_fiche));
+ insert into quant_purchase
+ (qp_internal,
+ j_id,
+ qp_fiche,
+ qp_quantite,
+ qp_price,
+ qp_vat,
+ qp_vat_code,
+ qp_nd_amount,
+ qp_nd_tva,
+ qp_nd_tva_recup,
+ qp_supplier,
+ qp_dep_priv)
+ values
+ (p_internal,
+ p_j_id,
+ fid_good,
+ p_quant,
+ p_price,
+ p_vat,
+ p_vat_code,
+ p_nd_amount,
+ p_nd_tva,
+ p_nd_tva_recup,
+ fid_client,
+ p_dep_priv);
+ return;
+end;
+ $$;
+
+--
+-- Name: insert_quick_code(integer, text); Type: FUNCTION; Schema: comptaproc; Owner: dany
+--
+
+CREATE FUNCTION insert_quick_code(nf_id integer, tav_text text) RETURNS integer
+ LANGUAGE plpgsql
+ AS $$
+ declare
+ ns integer;
+ nExist integer;
+ tText text;
+ begin
+ tText := upper(trim(tav_text));
+ tText := replace(tText,' ','');
+
+ loop
+ -- take the next sequence
+ select nextval('s_jnt_fic_att_value') into ns;
+ if length (tText) = 0 or tText is null then
+ tText := 'FID'||ns;
+ end if;
+ -- av_text already used ?
+ select count(*) into nExist
+ from fiche_detail
+ where
+ ad_id=23 and ad_value=upper(tText);
+
+ if nExist = 0 then
+ exit;
+ end if;
+ tText:='FID'||ns;
+ end loop;
+
+
+ insert into fiche_detail(jft_id,f_id,ad_id,ad_value) values (ns,nf_id,23,upper(tText));
+ return ns;
+ end;
+$$;
+
+
+--
+-- Name: update_quick_code(integer, text); Type: FUNCTION; Schema: comptaproc; Owner: dany
+--
+
+CREATE FUNCTION update_quick_code(njft_id integer, tav_text text) RETURNS integer
+ LANGUAGE plpgsql
+ AS $$
+ declare
+ ns integer;
+ nExist integer;
+ tText text;
+ old_qcode varchar;
+ begin
+ -- get current value
+ select ad_value into old_qcode from fiche_detail where jft_id=njft_id;
+ -- av_text didn't change so no update
+ if tav_text = upper( trim(old_qcode)) then
+ return 0;
+ end if;
+
+ tText := trim(upper(tav_text));
+ tText := replace(tText,' ','');
+ if length ( tText) = 0 or tText is null then
+ return 0;
+ end if;
+
+ ns := njft_id;
+
+ loop
+ -- av_text already used ?
+ select count(*) into nExist
+ from fiche_detail
+ where
+ ad_id=23 and ad_value=tText;
+
+ if nExist = 0 then
+ exit;
+ end if;
+ if tText = 'FID'||ns then
+ -- take the next sequence
+ select nextval('s_jnt_fic_att_value') into ns;
+ end if;
+ tText :='FID'||ns;
+
+ end loop;
+ update fiche_detail set ad_value = tText where jft_id=njft_id;
+
+ -- update also the contact
+ update fiche_detail set ad_value = tText
+ where jft_id in
+ ( select jft_id
+ from fiche_detail
+ where ad_id=25 and ad_value=old_qcode);
+
+
+ update jrnx set j_qcode=tText where j_qcode = old_qcode;
+ return ns;
+ end;
+$$;
+
+
+--
+-- Name: jrnx_ins(); Type: FUNCTION; Schema: comptaproc; Owner: dany
+--
+
+CREATE FUNCTION jrnx_ins() RETURNS trigger
+ LANGUAGE plpgsql
+ AS $$
+declare
+n_fid bigint;
+begin
+
+if NEW.j_qcode is NULL then
+ return NEW;
+end if;
+
+NEW.j_qcode=trim(upper(NEW.j_qcode));
+
+if length (NEW.j_qcode) = 0 then
+ NEW.j_qcode=NULL;
+ else
+ select f_id into n_fid from fiche_detail where ad_id=23 and ad_value=NEW.j_qcode;
+ if NOT FOUND then
+ raise exception 'La fiche dont le quick code est % n''existe pas',NEW.j_qcode;
+ end if;
+end if;
+NEW.f_id:=n_fid;
+return NEW;
+end;
+$$;