﻿SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL';


ALTER TABLE `bddevfac`.`articles` CHANGE COLUMN `societe` `societe` CHAR(2) NOT NULL DEFAULT 'PE'  
, DROP PRIMARY KEY 
, ADD PRIMARY KEY (`REF`, `societe`) 
, DROP INDEX `fam` 
, ADD INDEX `fam` (`societe` ASC, `FAM` ASC, `SFAM` ASC, `REF` ASC) ;

ALTER TABLE `bddevfac`.`clients` ADD COLUMN `website` VARCHAR(50) NULL DEFAULT NULL  AFTER `EMAIL` , CHANGE COLUMN `societe` `societe` CHAR(2) NOT NULL DEFAULT 'PE'  , CHANGE COLUMN `TYPE` `TYPE` VARCHAR(1) NULL DEFAULT 'P'  
, DROP PRIMARY KEY 
, ADD PRIMARY KEY (`societe`, `CD_CLI`) 
, DROP INDEX `NOCOMPTE` 
, ADD INDEX `NOCOMPTE` (`societe` ASC, `NOCOMPTE` ASC, `CD_CLI` ASC) ;

ALTER TABLE `bddevfac`.`dem_devis` DROP COLUMN `Asupprimer` , CHANGE COLUMN `societe` `societe` CHAR(2) NOT NULL DEFAULT 'PE'  
, DROP PRIMARY KEY 
, ADD PRIMARY KEY (`societe`, `NO_ID`) 
, DROP INDEX `chantier` 
, ADD INDEX `chantier` (`societe` ASC, `C_NOM` ASC, `C_ADR` ASC, `DATE` ASC) 
, DROP INDEX `date` 
, ADD INDEX `date` (`societe` ASC, `DATE` ASC, `NO_ID` ASC) 
, DROP INDEX `sinistre` 
, ADD INDEX `sinistre` (`societe` ASC, `NOSIN` ASC, `DATE` ASC) 
, DROP INDEX `police` 
, ADD INDEX `police` (`societe` ASC, `NOPOL` ASC, `DATE` ASC) 
, DROP INDEX `nom_client` 
, ADD INDEX `nom_client` (`societe` ASC, `NOMCLI` ASC, `DATE` ASC) ;

ALTER TABLE `bddevfac`.`dem_lig` CHANGE COLUMN `societe` `societe` CHAR(2) NOT NULL DEFAULT 'PE'  , 
  ADD CONSTRAINT `dem_lig_fk`
  FOREIGN KEY (`societe` , `NO_ID` )
  REFERENCES `bddevfac`.`dem_devis` (`societe` , `NO_ID` )
  ON DELETE CASCADE
, DROP INDEX `VISITE` 
, ADD INDEX `VISITE` (`societe` ASC, `DATEVI` ASC, `HEUREVI` ASC) 
, DROP INDEX `NO_ID` 
, ADD INDEX `NO_ID` (`societe` ASC, `NO_ID` ASC, `NOLIG` ASC) ;

ALTER TABLE `bddevfac`.`devis` CHANGE COLUMN `societe` `societe` CHAR(2) NOT NULL DEFAULT 'PE'  
, DROP PRIMARY KEY 
, ADD PRIMARY KEY (`societe`, `NO_ID`) 
, DROP INDEX `police` 
, ADD INDEX `police` (`societe` ASC, `NOPOL` ASC, `DATE` ASC) 
, DROP INDEX `sinistre` 
, ADD INDEX `sinistre` (`societe` ASC, `NOSIN` ASC, `DATE` ASC) 
, DROP INDEX `nom_client` 
, ADD INDEX `nom_client` (`societe` ASC, `NOMCLI` ASC, `DATE` ASC) 
, DROP INDEX `date` 
, ADD INDEX `date` (`societe` ASC, `DATE` ASC, `NO_ID` ASC) 
, DROP INDEX `chantier` 
, ADD INDEX `chantier` (`societe` ASC, `C_NOM` ASC, `C_ADR` ASC, `DATE` ASC) ;

ALTER TABLE `bddevfac`.`devis_lig` CHANGE COLUMN `societe` `societe` CHAR(2) NOT NULL DEFAULT 'PE'  , 
  ADD CONSTRAINT `devis_lig_fk`
  FOREIGN KEY (`societe` , `NO_ID` )
  REFERENCES `bddevfac`.`devis` (`societe` , `NO_ID` )
  ON DELETE CASCADE
, DROP PRIMARY KEY 
, ADD PRIMARY KEY (`societe`, `NO_ID`, `NOLIG`) ;

ALTER TABLE `bddevfac`.`facture` CHANGE COLUMN `societe` `societe` CHAR(2) NOT NULL DEFAULT 'PE'  
, DROP PRIMARY KEY 
, ADD PRIMARY KEY (`societe`, `NO_ID`) 
, DROP INDEX `reglement` 
, ADD INDEX `reglement` (`societe` ASC, `STATUS` ASC, `NOMCLI` ASC, `DATE` ASC) 
, DROP INDEX `sinistre` 
, ADD INDEX `sinistre` (`societe` ASC, `NOSIN` ASC, `DATE` ASC) 
, DROP INDEX `police` 
, ADD INDEX `police` (`societe` ASC, `NOPOL` ASC, `DATE` ASC) 
, DROP INDEX `nom_client` 
, ADD INDEX `nom_client` (`societe` ASC, `NOMCLI` ASC, `DATE` ASC) 
, DROP INDEX `date` 
, ADD INDEX `date` (`societe` ASC, `DATE` ASC, `NO_ID` ASC) 
, DROP INDEX `code_client` 
, ADD INDEX `code_client` (`societe` ASC, `CD_CLI` ASC, `TOTALTTC` ASC) 
, DROP INDEX `chantier` 
, ADD INDEX `chantier` (`societe` ASC, `C_NOM` ASC, `C_ADR` ASC, `DATE` ASC) ;

ALTER TABLE `bddevfac`.`facture_lig` CHANGE COLUMN `societe` `societe` CHAR(2) NOT NULL DEFAULT 'PE'  , 
  ADD CONSTRAINT `facture_lig_fk`
  FOREIGN KEY (`societe` , `NO_ID` )
  REFERENCES `bddevfac`.`facture` (`societe` , `NO_ID` )
  ON DELETE CASCADE
, DROP PRIMARY KEY 
, ADD PRIMARY KEY (`societe`, `NO_ID`, `NOLIG`) ;

ALTER TABLE `bddevfac`.`facture_reg` 
  ADD CONSTRAINT `facture_reg_fk`
  FOREIGN KEY (`societe` , `NOFACT` )
  REFERENCES `bddevfac`.`facture` (`societe` , `NO_ID` )
  ON DELETE CASCADE
  ON UPDATE NO ACTION
, DROP INDEX `client` 
, ADD INDEX `client` (`societe` ASC, `CD_CLI` ASC, `COD_BNQ` ASC) 
, DROP INDEX `date` 
, ADD INDEX `date` (`societe` ASC, `DATE` ASC, `NOFACT` ASC, `COD_BNQ` ASC) 
, DROP INDEX `nofact` 
, ADD INDEX `nofact` (`societe` ASC, `NOFACT` ASC, `MODEREG` ASC, `COD_BNQ` ASC) ;

ALTER TABLE `bddevfac`.`facture_sui` 
DROP INDEX `TYPE` 
, ADD INDEX `TYPE` (`societe` ASC, `TYPE` ASC, `NOFACT` ASC, `Code` ASC) ;

ALTER TABLE `bddevfac`.`lettre` CHANGE COLUMN `societe` `societe` CHAR(2) NOT NULL DEFAULT 'PE'  , CHANGE COLUMN `REF` `REF` VARCHAR(10) NOT NULL  
, ADD PRIMARY KEY (`societe`, `REF`) ;

ALTER TABLE `bddevfac`.`liv_bnq` CHANGE COLUMN `societe` `societe` CHAR(2) NOT NULL DEFAULT 'PE'  , CHANGE COLUMN `COD_BNQ` `COD_BNQ` INT(11) NOT NULL  
, DROP PRIMARY KEY 
, ADD PRIMARY KEY (`societe`, `COD_BNQ`) 
, DROP INDEX `client` 
, ADD INDEX `client` (`societe` ASC, `CD_CLI` ASC, `MODEREG` ASC, `COD_BNQ` ASC) 
, DROP INDEX `journal` 
, ADD INDEX `journal` (`societe` ASC, `JNL` ASC, `DATE` ASC, `CD_CLI` ASC) ;

ALTER TABLE `bddevfac`.`ordres` CHANGE COLUMN `societe` `societe` CHAR(2) NOT NULL DEFAULT 'PE'  
, DROP PRIMARY KEY 
, ADD PRIMARY KEY (`societe`, `NO_ID`) 
, DROP INDEX `chantier` 
, ADD INDEX `chantier` (`societe` ASC, `C_NOM` ASC, `C_ADR` ASC, `DATE` ASC) 
, DROP INDEX `date` 
, ADD INDEX `date` (`societe` ASC, `DATE` ASC, `NO_ID` ASC) 
, DROP INDEX `nom_client` 
, ADD INDEX `nom_client` (`societe` ASC, `NOMCLI` ASC, `DATE` ASC) 
, DROP INDEX `police` 
, ADD INDEX `police` (`societe` ASC, `NOPOL` ASC, `DATE` ASC) 
, DROP INDEX `sinistre` 
, ADD INDEX `sinistre` (`societe` ASC, `NOSIN` ASC, `DATE` ASC) ;

ALTER TABLE `bddevfac`.`ordres_lig` 
  ADD CONSTRAINT `ordres_lig_fk`
  FOREIGN KEY (`societe` , `NO_ID` )
  REFERENCES `bddevfac`.`ordres` (`societe` , `NO_ID` )
  ON DELETE CASCADE
, DROP INDEX `LISTE` 
, ADD INDEX `LISTE` (`societe` ASC, `LISTE` ASC, `DATEVI` ASC, `HEUREVI` ASC) 
, DROP INDEX `NO_ID` 
, ADD INDEX `NO_ID` (`societe` ASC, `NO_ID` ASC, `NOLIG` ASC) 
, ADD INDEX `societe` (`societe` ASC, `NO_ID` ASC) 
, DROP INDEX `NO_ID_2` ;

ALTER TABLE `bddevfac`.`p_cabinet` CHANGE COLUMN `societe` `societe` CHAR(2) NOT NULL DEFAULT 'PE'  
, DROP PRIMARY KEY 
, ADD PRIMARY KEY (`societe`, `CODE`) ;

ALTER TABLE `bddevfac`.`p_editcrys` ADD COLUMN `selection` VARCHAR(250) NULL DEFAULT NULL  AFTER `Ecran` ;

ALTER TABLE `bddevfac`.`p_experts` CHANGE COLUMN `societe` `societe` CHAR(2) NOT NULL DEFAULT 'PE'  
, DROP PRIMARY KEY 
, ADD PRIMARY KEY (`societe`, `CODE_CAB`, `CODE_EXP`) ;

ALTER TABLE `bddevfac`.`p_societe` CHANGE COLUMN `societe` `societe` CHAR(2) NOT NULL DEFAULT 'PE'  
, ADD PRIMARY KEY (`societe`) ;

ALTER TABLE `bddevfac`.`p_tva` ADD COLUMN `MT_TVA` DECIMAL(11,2) NULL DEFAULT '0.00'  AFTER `CPT_VTE` , ADD COLUMN `MT_VTE` DECIMAL(11,2) NULL DEFAULT '0.00'  AFTER `MT_TVA` , CHANGE COLUMN `TAUX` `TAUX` FLOAT(11) NULL DEFAULT '19.6'  , CHANGE COLUMN `LIB` `LIB` VARCHAR(20) NULL DEFAULT 'Taux 19.6'  , CHANGE COLUMN `CPT_TVA` `CPT_TVA` INT(11) NULL DEFAULT '44571000'  , CHANGE COLUMN `CPT_VTE` `CPT_VTE` INT(11) NULL DEFAULT '70410000'  ;

ALTER TABLE `bddevfac`.`proforma` CHANGE COLUMN `societe` `societe` CHAR(2) NOT NULL DEFAULT 'PE'  
, DROP PRIMARY KEY 
, ADD PRIMARY KEY (`societe`, `NO_ID`) 
, DROP INDEX `chantier` 
, ADD INDEX `chantier` (`societe` ASC, `C_NOM` ASC, `C_ADR` ASC, `DATE` ASC) 
, DROP INDEX `date` 
, ADD INDEX `date` (`societe` ASC, `DATE` ASC, `NO_ID` ASC) 
, DROP INDEX `nom_client` 
, ADD INDEX `nom_client` (`societe` ASC, `NOMCLI` ASC, `DATE` ASC) 
, DROP INDEX `police` 
, ADD INDEX `police` (`societe` ASC, `NOPOL` ASC, `DATE` ASC) 
, DROP INDEX `sinistre` 
, ADD INDEX `sinistre` (`societe` ASC, `NOSIN` ASC, `DATE` ASC) ;

ALTER TABLE `bddevfac`.`proforma_lig` CHANGE COLUMN `societe` `societe` CHAR(2) NOT NULL DEFAULT 'PE'  , 
  ADD CONSTRAINT `proforma_lig_fk`
  FOREIGN KEY (`societe` , `NO_ID` )
  REFERENCES `bddevfac`.`proforma` (`societe` , `NO_ID` )
  ON DELETE CASCADE
, DROP PRIMARY KEY 
, ADD PRIMARY KEY (`societe`, `NO_ID`, `NOLIG`) 
, ADD INDEX `societe` (`societe` ASC, `NO_ID` ASC) ;

ALTER TABLE `bddevfac`.`rdvous` CHANGE COLUMN `societe` `societe` CHAR(2) NOT NULL DEFAULT 'PE'  
, DROP PRIMARY KEY 
, ADD PRIMARY KEY (`societe`, `NO_ID`) 
, DROP INDEX `chantier` 
, ADD INDEX `chantier` (`societe` ASC, `C_NOM` ASC, `C_ADR` ASC, `DATE` ASC) 
, DROP INDEX `date` 
, ADD INDEX `date` (`societe` ASC, `DATE` ASC, `NO_ID` ASC) 
, DROP INDEX `nom_client` 
, ADD INDEX `nom_client` (`societe` ASC, `NOMCLI` ASC, `DATE` ASC) 
, DROP INDEX `police` 
, ADD INDEX `police` (`societe` ASC, `NOPOL` ASC, `DATE` ASC) 
, DROP INDEX `sinistre` 
, ADD INDEX `sinistre` (`societe` ASC, `NOSIN` ASC, `DATE` ASC) ;

ALTER TABLE `bddevfac`.`rdvous_lig` 
  ADD CONSTRAINT `rdvous_lig_fk`
  FOREIGN KEY (`societe` , `NO_ID` )
  REFERENCES `bddevfac`.`rdvous` (`societe` , `NO_ID` )
  ON DELETE CASCADE
, DROP INDEX `VISITE` 
, ADD INDEX `VISITE` (`societe` ASC, `DATEVI` ASC, `HEUREVI` ASC) 
, DROP INDEX `NO_ID` 
, ADD INDEX `NO_ID` (`societe` ASC, `NO_ID` ASC, `NOLIG` ASC) 
, ADD INDEX `societe` (`societe` ASC, `NO_ID` ASC) ;

CREATE  TABLE IF NOT EXISTS `bddevfac`.`t_debug` (
  `date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ,
  `proc` VARCHAR(20) NULL DEFAULT NULL ,
  `msg` VARCHAR(512) NULL DEFAULT NULL ,
  `id` INT(11) NOT NULL AUTO_INCREMENT ,
  PRIMARY KEY (`id`) )
ENGINE = MEMORY
DEFAULT CHARACTER SET = latin1
COLLATE = latin1_swedish_ci;

DROP TABLE IF EXISTS `bddevfac`.`t_export` ;

DROP TABLE IF EXISTS `bddevfac`.`p_numero` ;


SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
