VERS='5.4.65'

echo "Mise a jour de la base de donnees pour Version $VERS"

isql - - <<EOT

database gc;

UPDATE 
  gc_parsys 
SET 
  version='$VERS';

CREATE TABLE
  gc_bap (
    nodos   integer,
    desig   char(39),
    annee   smallint,
    dteinit date    ,
    txpr    float   ,
    dltstk  smallint
  );

CREATE UNIQUE INDEX
  ux_bap 
ON
  gc_bap (nodos);

CREATE TABLE
  gc_lbap (
    nodos   integer,
    numlig  smallint,
    fam     char(4) ,
    caant   float,
    txpr    float,
    caprev  float,
    txmrg   float,
    cfrev   float,
    pdm1    float,
    pdm2    float,
    pdm3    float,
    pdm4    float,
    pdm5    float,
    pdm6    float,
    pdm7    float,
    pdm8    float,
    pdm9    float,
    pdm10   float,
    pdm11   float,
    pdm12   float
  );

CREATE UNIQUE INDEX
  ux_lbap 
ON 
  gc_lbap (nodos, numlig);


CREATE TABLE
  gc_cafam (
    code char(4),
    mois smallint,
    annee smallint,
    qtt float,
    pxach float,
    rev float,
    pvt float,
    numdep smallint,
    datcum date
  );

CREATE UNIQUE INDEX
  ux_cafam01 
ON
  gc_cafam (code,mois,annee,numdep);

CREATE UNIQUE INDEX
  ux_cafam02 
ON
  gc_cafam (code,datcum,numdep);

INSERT INTO
  gc_cafam (
    code, 
    mois, 
    annee, 
    numdep, 
    qtt, 
    pxach, 
    rev, 
    pvt, 
    datcum
  )
SELECT 
  fam, mois, annee, numdep, 
  sum(qtt), sum(qtt * pxach), sum(qtt * rev), sum(qtt * pvt), mdy(mois, 1, annee) 
FROM
  gc_mvt 
WHERE
  codmvt = 1
GROUP BY
  fam, mois, annee, numdep;

CREATE TABLE
  gc_douane (
    code    char(15) ,
    tarif   float    ,
    libelle char(20) ,
    taom    float
  );

CREATE UNIQUE INDEX
  ix1_douane
ON 
  gc_douane (code);


CREATE TABLE
  gc_ecf
  (
    dtech   DATE    ,
    codef   CHAR(6) ,
    numpiec CHAR(10),
    numdos  CHAR(7) ,  
    numlig  SMALLINT,
    mont    FLOAT   ,
    statut  SMALLINT
  );

CREATE INDEX
  ux1_ecf
ON 
  gc_ecf (numpiec);

CREATE TABLE
  gc_emp
  (
    dtemp   DATE    ,
    noemp   CHAR(6) ,
    nopiece CHAR(10),
    type    SMALLINT
  );

CREATE INDEX
  ux1_emp
ON 
  gc_emp (noemp);

ALTER TABLE
  gc_parsys
ADD (
    clicess00 char(6),
    clicess01 char(6),
    clicess02 char(6),
    clicess03 char(6),
    clicess04 char(6),
    clicess05 char(6),
    clicess06 char(6),
    clicess07 char(6),
    clicess08 char(6),
    clicess09 char(6),
    clicess10 char(6),
    clicess11 char(6),
    clicess12 char(6),
    clicess13 char(6),
    clicess14 char(6),
    clicess15 char(6),
    clicess16 char(6),
    clicess17 char(6),
    clicess18 char(6),
    clicess19 char(6)
);

ALTER TABLE
  gc_soc
ADD
  (coeftrsp FLOAT);

UPDATE gc_soc SET coeftrsp=1.0;

create table gc_fcc 
  (
    nofac char(10),
    cli char(6),
    ind smallint,
    edite smallint,
    traite smallint,
    ouverte smallint,
    encompte smallint,
    export smallint,
    devise smallint,
    vnd char(2),
    numbl char(10),
    rsoc char(30),
    ad1 char(25),
    ad2 char(25),
    cpv char(25),
    numdep smallint,
    remise float,
    dteinit date,
    dteche date,
    total float,
    totht float,
    totva float,
    margbr float,
    comment1 char(40),
    refcom char(7),
    mdreg smallint,
    delreg smallint,
    jreg smallint,
    typreg smallint,
    dtcom date,
    numop char(10),
    comment2 char(40),
    comment3 char(40),
    hrinit integer,
    totom float,
    totda float,
    numcais smallint,
    codpost char(5),
    numchant smallint,
    solde float
  );

create unique index ix_fcc01 on gc_fcc (nofac);
create index ix_fcc02 on gc_fcc (dteinit,nofac);

create table gc_lfc 
  (
    numfac char(10),
    numlig smallint,
    code char(12),
    desig char(60),
    tva smallint,
    qt float,
    uv float,
    px float,
    rev float,
    remis float,
    promo smallint,
    codtar smallint,
    numdep smallint,
    pxbase float,
    grat float,
    rempromo float,
    om smallint,
    da smallint,
    tl char(2),
    unite char(7),
    compo smallint,
    nodos char(6),
    qtav float,
    qtliv float,
    rvlu float,
    typtail char(3),
    nlt smallint,
    codecoul char(2),
    vnd char(2)
  );

create unique index ux_lfc01 on gc_lfc (numfac,numlig);

alter table gc_cli add (vnda char(2));

CLOSE DATABASE;

EOT

chmod 666 $DBPATH/gc.dbs/*.idx
chmod 666 $DBPATH/gc.dbs/*.dat

echo "Mise a jour terminee"
