-- MySQL dump 10.13  Distrib 5.7.24, for Win64 (x86_64)
--
-- Host: localhost    Database: bddevfac
-- ------------------------------------------------------
-- Server version	5.7.24-log

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`localhost`*/ /*!50003 TRIGGER `bddevfac`.`articles_BINS`
BEFORE INSERT ON `bddevfac`.`articles`
FOR EACH ROW
BEGIN
   set NEW.cree_le = NOW();
   set NEW.modif_le = NOW();
END */;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`localhost`*/ /*!50003 TRIGGER `bddevfac`.`clients_BINS`
BEFORE INSERT ON `bddevfac`.`clients`
FOR EACH ROW
BEGIN
   set NEW.cree_le = NOW();
   set NEW.modif_le = NOW();
END */;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`localhost`*/ /*!50003 TRIGGER `bddevfac`.`clients_BUPD`
BEFORE UPDATE ON `bddevfac`.`clients`
FOR EACH ROW
BEGIN
  SET NEW.adresse=REPLACE(NEW.adresse,",","");
END */;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`localhost`*/ /*!50003 TRIGGER `bddevfac`.`dem_devis_before_ins_tr`
BEFORE INSERT ON `bddevfac`.`dem_devis`
FOR EACH ROW
BEGIN
	SET NEW.datemaj = NOW();
	-- SET NEW.date = NOW();
	SET NEW.tauxtva = (SELECT taux FROM p_tva WHERE code=NEW.codetva);
	SET NEW.cabinet = (SELECT cabinet FROM clients WHERE societe=NEW.societe AND cd_cli=NEW.cd_cli);
	SET NEW.no_agrement = (SELECT agrement FROM clients WHERE societe=NEW.societe AND cd_cli=NEW.cd_cli);
END */;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`localhost`*/ /*!50003 TRIGGER `bddevfac`.`dem_devis_after_ins_tr`
AFTER INSERT ON `bddevfac`.`dem_devis`
FOR EACH ROW
BEGIN

  INSERT INTO dem_lig (societe, NO_ID, DATE, COMMENT, USERMAJ) VALUES
        (new.societe,new.no_id, CURRENT_DATE(), 'RECEPTION DEMANDE DE CHANTIER',new.usermaj);

END */;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`localhost`*/ /*!50003 TRIGGER `bddevfac`.`dem_devis_BUPD`
BEFORE UPDATE ON `bddevfac`.`dem_devis`
FOR EACH ROW
BEGIN

	SET NEW.tauxtva = (SELECT taux FROM p_tva WHERE code=NEW.codetva);
	SET NEW.c_adr=REPLACE(NEW.c_adr,",","");
	SET NEW.c_adr2=REPLACE(NEW.c_adr2,",","");
    -- recupere le no_agrement du client
	SET NEW.no_agrement = (select agrement from clients where societe=new.societe and cd_cli=new.cd_cli);

END */;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`localhost`*/ /*!50003 TRIGGER `bddevfac`.`dem_lig_before_ins_tr`
BEFORE INSERT ON `bddevfac`.`dem_lig`
FOR EACH ROW
BEGIN

   set NEW.datemaj = NOW();
   if NEW.datevi is not null and NEW.heurevi is not null then
		set NEW.rdv_debut = ADDTIME(NEW.datevi,NEW.heurevi);
        set NEW.rdv_fin = ADDTIME(NEW.rdv_debut,'01:00:00');
	else
		set NEW.rdv_debut = NULL;
        set NEW.rdv_fin = NULL;
   end if;
   
END */;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`127.0.0.1`*/ /*!50003 TRIGGER `bddevfac`.`dem_lig_BEFORE_UPDATE`
BEFORE UPDATE ON `bddevfac`.`dem_lig`
FOR EACH ROW
BEGIN

   if NEW.datevi is not null and NEW.heurevi is not null then
		set NEW.rdv_debut = ADDTIME(NEW.datevi,NEW.heurevi);
        set NEW.rdv_fin = ADDTIME(NEW.rdv_debut,NEW.duree);
	else
		set NEW.rdv_debut = NULL;
        set NEW.rdv_fin = NULL;
   end if;

END */;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION' */ ;
DELIMITER ;;
/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`%`*/ /*!50003 TRIGGER `bddevfac`.`dem_lig_AFTER_UPDATE` AFTER UPDATE ON `dem_lig` FOR EACH ROW
BEGIN

        	IF NEW.datevi IS NOT NULL AND NEW.liste <> 'X' THEN
        		-- memoriser la date de visite
        		UPDATE dem_devis 
        			SET datevi = NEW.datevi, HEUREVI = NEW.heurevi
        		WHERE SOCIETE=NEW.societe AND NO_ID = NEW.no_id;
                -- memoriser la première personne au RDV
        		UPDATE dem_devis 
        			SET suivi_par = NEW.liste
        		WHERE SOCIETE=NEW.societe AND NO_ID = NEW.no_id AND suivi_par IS NULL;
        	END IF;

END */;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`localhost`*/ /*!50003 TRIGGER `bddevfac`.`devis_before_ins_tr`
BEFORE INSERT ON `bddevfac`.`devis`
FOR EACH ROW
BEGIN
	set NEW.datemaj = NOW();
	set NEW.date = NOW();

	-- changer les anciens taux de TVA en nouveaux taux
	IF NEW.codetva = 4 THEN
		SET NEW.codetva = 6;
	ELSEIF NEW.codetva = 0 THEN
		SET NEW.codetva = 7;
	END IF;

	SET NEW.tauxtva = (SELECT taux FROM p_tva WHERE code=NEW.codetva);

END */;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`localhost`*/ /*!50003 TRIGGER `bddevfac`.`devis_BEFORE_UPDATE`
BEFORE UPDATE ON `bddevfac`.`devis`
FOR EACH ROW
BEGIN

	SET NEW.tauxtva = (SELECT taux FROM p_tva WHERE code=NEW.codetva);
	SET NEW.totaltva = NEW.totalht * NEW.tauxtva / 100;
	SET NEW.totalttc = NEW.totalht + NEW.totaltva;

END */;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`localhost`*/ /*!50003 TRIGGER `bddevfac`.`devis_lig_BEFORE_INSERT`
BEFORE INSERT ON `bddevfac`.`devis_lig`
FOR EACH ROW
BEGIN
   -- calculer le mtht
   SET NEW.mtht = ROUND(NEW.prixht * NEW.qte,2);
   SET NEW.datemaj = CURRENT_DATE();

END */;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`localhost`*/ /*!50003 TRIGGER `bddevfac`.`devis_lig_BUPD`
BEFORE UPDATE ON `bddevfac`.`devis_lig`
FOR EACH ROW
BEGIN
   -- calculer le mtht
   SET NEW.mtht = ROUND(NEW.prixht * NEW.qte,2);
   SET NEW.datemaj = CURRENT_DATE();

END */;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`localhost`*/ /*!50003 TRIGGER `bddevfac`.`dossier_attaches_BEFORE_INSERT`
BEFORE INSERT ON `bddevfac`.`dossier_attaches`
FOR EACH ROW
BEGIN
   -- set NEW.cree_le = NOW();
END */;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`::1`*/ /*!50003 TRIGGER `bddevfac`.`email_rappels_BEFORE_INSERT`
BEFORE INSERT ON `bddevfac`.`email_rappels`
FOR EACH ROW
BEGIN
	SET NEW.cree_le = NOW();
END */;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`localhost`*/ /*!50003 TRIGGER `bddevfac`.`facture_before_ins_tr`
BEFORE INSERT ON `bddevfac`.`facture`
FOR EACH ROW
BEGIN

	SET NEW.datemaj = NOW();
	SET NEW.date = NOW();
	-- changer les anciens taux de TVA en nouveaux taux
	IF NEW.codetva = 4 THEN
		SET NEW.codetva = 6;
	ELSEIF NEW.codetva = 0 THEN
		SET NEW.codetva = 7;
	END IF;

	SET NEW.tauxtva = (SELECT taux FROM p_tva WHERE code=NEW.codetva);
	-- calculer la date d'echeance
	IF (SELECT type FROM clients WHERE societe=NEW.societe AND cd_cli = NEW.cd_cli) = "P" THEN
		SET NEW.ECHEANCE = DATE_ADD(CURRENT_DATE, INTERVAL 8 DAY);
	ELSE
		SET NEW.ECHEANCE = DATE_ADD(CURRENT_DATE, INTERVAL 1 MONTH);
	END IF;

END */;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`localhost`*/ /*!50003 TRIGGER `bddevfac`.`facture_before_upd_tr`
BEFORE UPDATE ON `bddevfac`.`facture`
FOR EACH ROW
BEGIN

	SET NEW.tauxtva = (SELECT taux FROM p_tva WHERE code=NEW.codetva);
	SET NEW.totaltva = NEW.totalht * NEW.tauxtva / 100;
	SET NEW.totalttc = NEW.totalht + NEW.totaltva;

	-- determiner le status de la facture
    IF NEW.mtregl <> 0 THEN
	   IF NEW.totalttc - NEW.mtregl = 0 THEN
		  SET NEW.status = 'Réglée';
	   ELSE
		  SET NEW.status = 'Régl part.';
	   END IF;
	ELSE
	  SET NEW.status = '';
	END IF;

   IF ISNULL(NEW.ECHEANCE) THEN
      SET NEW.ECHEANCE = IF(NEW.typecli="P",DATE_ADD(NEW.date,INTERVAL 8 DAY),DATE_ADD(NEW.date,INTERVAL 1 MONTH));
   END IF;

  SET NEW.tauxtva = (SELECT taux FROM p_tva WHERE code=NEW.codetva);
  SET NEW.c_adr=REPLACE(NEW.c_adr,",","");
  SET NEW.c_adr2=REPLACE(NEW.c_adr2,",","");

END */;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`localhost`*/ /*!50003 TRIGGER `bddevfac`.`facture_AUPD`
AFTER UPDATE ON `bddevfac`.`facture`
FOR EACH ROW
BEGIN

	-- affecter eventuellement un nouveau numero de compte comptable au client
	IF NEW.cd_cli > 0 THEN
		CALL spUPD_CLIENTS_COMPTE(NEW.societe,NEW.cd_cli);
	END IF;
  
	IF NEW.status <> '' THEN
		-- marquer la demande de devis comme FACTUREE
		UPDATE DEM_DEVIS SET status = NEW.status WHERE societe = NEW.societe AND NO_ID = NEW.nochantier;
	END IF;

END */;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`localhost`*/ /*!50003 TRIGGER `bddevfac`.`facture_lig_BEFORE_INSERT`
BEFORE INSERT ON `bddevfac`.`facture_lig`
FOR EACH ROW
BEGIN

   -- calculer le mtht
   SET NEW.mtht = NEW.prixht * NEW.qte;
   SET NEW.datemaj = NOW();

END */;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`localhost`*/ /*!50003 TRIGGER `bddevfac`.`facture_lig_BUPD`
BEFORE UPDATE ON `bddevfac`.`facture_lig`
FOR EACH ROW
BEGIN
   -- calculer le mtht
   SET NEW.mtht = NEW.prixht * NEW.qte;
   SET NEW.datemaj = CURRENT_DATE();

END */;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`localhost`*/ /*!50003 TRIGGER `bddevfac`.`facture_reg_AINS`
AFTER INSERT ON `bddevfac`.`facture_reg`
FOR EACH ROW
-- Edit trigger body code below this line. Do not edit lines above this one
BEGIN

	UPDATE `liv_bnq` 
		SET montantdebit = (SELECT COALESCE(SUM(montant),0) FROM `facture_reg` 
						   WHERE societe=NEW.societe AND cod_bnq=NEW.cod_bnq)
		WHERE societe=NEW.societe AND cod_bnq=NEW.cod_bnq; 
	IF NEW.modereg < 7 OR NEW.modereg > 8 THEN
		UPDATE `liv_bnq` 
			SET codetva = (SELECT codetva FROM `facture` 
							   WHERE societe=NEW.societe AND no_id=NEW.nofact)
			WHERE societe=NEW.societe AND cod_bnq=NEW.cod_bnq; 
    END IF;
	CALL spUPD_FACTURE_STATUT(NEW.societe, NEW.nofact);

END */;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`localhost`*/ /*!50003 TRIGGER `bddevfac`.`facture_reg_AUPD`
AFTER UPDATE ON `bddevfac`.`facture_reg`
FOR EACH ROW
-- Edit trigger body code below this line. Do not edit lines above this one

BEGIN

	UPDATE `liv_bnq` 
		SET montantdebit = (SELECT COALESCE(SUM(montant),0) FROM `facture_reg` 
						   WHERE societe=NEW.societe AND cod_bnq=NEW.cod_bnq)
		WHERE societe=NEW.societe AND cod_bnq=NEW.cod_bnq; 
	-- mode de reglement <> facture ou avoir
	IF NEW.modereg < 7 OR NEW.modereg > 8 THEN
		UPDATE `liv_bnq` 
			SET codetva = (SELECT codetva FROM `facture` 
							   WHERE societe=NEW.societe AND no_id=NEW.nofact)
			WHERE societe=NEW.societe AND cod_bnq=NEW.cod_bnq; 
	END IF;

	CALL spUPD_FACTURE_STATUT(OLD.societe,OLD.nofact);
  
END */;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`localhost`*/ /*!50003 TRIGGER `bddevfac`.`facture_reg_ADEL`
AFTER DELETE ON `bddevfac`.`facture_reg`
FOR EACH ROW
-- Edit trigger body code below this line. Do not edit lines above this one

BEGIN

  IF OLD.modereg < 7 THEN
     UPDATE `liv_bnq` 
       SET montantdebit = (SELECT COALESCE(SUM(montant),0) FROM `facture_reg` 
                           WHERE societe=OLD.societe AND cod_bnq=OLD.cod_bnq),
		   codetva = 5
       WHERE societe=OLD.societe AND cod_bnq=OLD.cod_bnq;
  END IF;

  CALL spUPD_FACTURE_STATUT(OLD.societe,OLD.nofact);

END */;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`localhost`*/ /*!50003 TRIGGER `bddevfac`.`liv_bnq_BINS`
BEFORE INSERT ON `bddevfac`.`liv_bnq`
FOR EACH ROW
BEGIN
   set NEW.cree_le = NOW();
   set NEW.modif_le = NOW();
	-- si REMBOURSEMENT, solde = 0
	IF NEW.remboursement <> 0 THEN
		SET NEW.montantdebit = NEW.montantregl;
	END IF;
	SET NEW.tauxtva = (SELECT taux FROM p_tva WHERE code=NEW.codetva);
END */;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`localhost`*/ /*!50003 TRIGGER `bddevfac`.`liv_bnq_BUPD`
BEFORE UPDATE ON `bddevfac`.`liv_bnq`
FOR EACH ROW
BEGIN

	-- si REMBOURSEMENT, solde = 0
	IF NEW.remboursement <> 0 THEN
		SET NEW.montantdebit = NEW.montantregl;
	END IF;
	SET NEW.tauxtva = (SELECT taux FROM p_tva WHERE code=NEW.codetva);

END */;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`localhost`*/ /*!50003 TRIGGER `bddevfac`.`liv_bnq_AUPD`
AFTER UPDATE ON `bddevfac`.`liv_bnq`
FOR EACH ROW
BEGIN

  -- affecter eventuellement un nouveau numero de compte comptable au client
  IF NEW.cd_cli > 0 THEN
     CALL spUPD_CLIENTS_COMPTE(NEW.societe,NEW.cd_cli);
  END IF;

END */;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`localhost`*/ /*!50003 TRIGGER `bddevfac`.`p_cabinet_BINS`
BEFORE INSERT ON `bddevfac`.`p_cabinet`
FOR EACH ROW
BEGIN
   set NEW.cree_le = NOW();
   set NEW.modif_le = NOW();
END */;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`127.0.0.1`*/ /*!50003 TRIGGER `bddevfac`.`p_experts_BEFORE_INSERT`
BEFORE INSERT ON `bddevfac`.`p_experts`
FOR EACH ROW
BEGIN

	SET NEW.cree_le = NOW();
	SET NEW.modif_le = NOW();

END */;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`localhost`*/ /*!50003 TRIGGER `bddevfac`.`p_printers_BINS`
BEFORE INSERT ON `bddevfac`.`p_printers`
FOR EACH ROW
BEGIN
   set NEW.cree_le = NOW();
   set NEW.modif_le = NOW();
END */;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`localhost`*/ /*!50003 TRIGGER `bddevfac`.`p_societe_BINS`
BEFORE INSERT ON `bddevfac`.`p_societe`
FOR EACH ROW
BEGIN
   set NEW.cree_le = NOW();
   set NEW.modif_le = NOW();
END */;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`localhost`*/ /*!50003 TRIGGER `bddevfac`.`p_users_BINS`
BEFORE INSERT ON `bddevfac`.`p_users`
FOR EACH ROW
BEGIN
  set NEW.cree_le = NOW();
  set NEW.modif_le = NOW();
END */;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`localhost`*/ /*!50003 TRIGGER `bddevfac`.`proforma_before_ins_tr`
BEFORE INSERT ON `bddevfac`.`proforma`
FOR EACH ROW
BEGIN
	set NEW.datemaj = NOW();
	set NEW.date = NOW();
    
	-- changer les anciens taux de TVA en nouveaux taux
	IF NEW.codetva = 4 THEN
		SET NEW.codetva = 6;
	ELSEIF NEW.codetva = 0 THEN
		SET NEW.codetva = 7;
	END IF;

	SET NEW.tauxtva = (SELECT taux FROM p_tva WHERE code=NEW.codetva);

END */;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`localhost`*/ /*!50003 TRIGGER `bddevfac`.`proforma_BUPD`
BEFORE UPDATE ON `bddevfac`.`proforma`
FOR EACH ROW
BEGIN

	SET NEW.tauxtva = (SELECT taux FROM p_tva WHERE code=NEW.codetva);
	SET NEW.totaltva = NEW.totalht * NEW.tauxtva / 100;
	SET NEW.totalttc = NEW.totalht + NEW.totaltva;

	SET NEW.status = '';
        
END */;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`127.0.0.1`*/ /*!50003 TRIGGER `bddevfac`.`proforma_lig_BEFORE_INSERT`
BEFORE INSERT ON `bddevfac`.`proforma_lig`
FOR EACH ROW
BEGIN

   -- calculer le mtht
   SET NEW.mtht = ROUND(NEW.prixht * NEW.qte,2);
   SET NEW.datemaj = CURRENT_DATE();

END */;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`localhost`*/ /*!50003 TRIGGER `bddevfac`.`proforma_lig_BUPD`
BEFORE UPDATE ON `bddevfac`.`proforma_lig`
FOR EACH ROW
BEGIN
   -- calculer le mtht
   SET NEW.mtht = NEW.prixht * NEW.qte;
   SET NEW.datemaj = CURRENT_DATE();

END */;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`localhost`*/ /*!50003 TRIGGER `bddevfac`.`t_imp_rdv_jour_before_upd_tr`
BEFORE INSERT ON `bddevfac`.`t_imp_rdv_jour`
FOR EACH ROW
BEGIN
  IF NEW.HEUREVI >= '12:00' THEN
     SET NEW.Code = 'AM';
  END IF;
END */;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;

--
-- Dumping routines for database 'bddevfac'
--
/*!50003 DROP FUNCTION IF EXISTS `fDBL_QUOTE` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` FUNCTION `fDBL_QUOTE`(chaine VARCHAR(50)) RETURNS varchar(50) CHARSET utf8
BEGIN
   
   SET chaine = Replace(chaine, "'", "''");

RETURN chaine;
END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP FUNCTION IF EXISTS `GetNewNoBanque` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` FUNCTION `GetNewNoBanque`(pSTE CHAR(2)) RETURNS int(11)
BEGIN

	declare nAYDeb, nAYFin int;
	declare nNumero int default 0;

	
	set nAYDeb = right(cast(year(current_date) AS CHAR),2) * 10000;
	set nAYFin = nAYDeb + 9999;

	
	set nNumero = (SELECT cod_bnq FROM liv_bnq 
                   WHERE societe=pSTE AND cod_bnq BETWEEN nAYDeb AND nAYFin
                   ORDER BY cod_bnq DESC LIMIT 1);
    if nNUMERO IS NULL then
       SET nNUMERO = nAYDeb;
    end if;

	RETURN nNumero + 1;

END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP FUNCTION IF EXISTS `GetNewNoCabinet` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` FUNCTION `GetNewNoCabinet`() RETURNS int(11)
BEGIN

	SET @Numero = (SELECT code FROM p_cabinet ORDER BY code DESC LIMIT 1);
    IF @NUMERO IS NULL then
       SET @NUMERO = 0;
    END IF;

	RETURN @Numero + 1;

END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP FUNCTION IF EXISTS `GetNewNochantier` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` FUNCTION `GetNewNochantier`(
	pSTE CHAR(2),
    pRubrique VARCHAR(20),
    pAnnee SMALLINT
    ) RETURNS int(11)
BEGIN

	declare nAYDeb, nAYFin int;
	declare nNumero int default 0;

	
	set nAYDeb = right(cast(pAnnee AS CHAR),2) * 10000;
	set nAYFin = nAYDeb + 9999;

	SET nNumero = (SELECT no_id FROM dem_devis 
			   WHERE societe=pSTE AND no_id BETWEEN nAYDeb AND nAYFin
			   ORDER BY no_id DESC LIMIT 1);
    if nNUMERO IS NULL then
       SET nNUMERO = nAYDeb;
    end if;

	RETURN nNumero + 1;

END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP FUNCTION IF EXISTS `GetNewNoClient` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` FUNCTION `GetNewNoClient`(pSTE CHAR(2)) RETURNS int(11)
BEGIN

	SET @Numero = (SELECT COALESCE(cd_cli,0) FROM clients
                   WHERE societe=pSTE ORDER BY cd_cli DESC LIMIT 1);

	RETURN @Numero + 1;

END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP FUNCTION IF EXISTS `GetNewNoCompte` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` FUNCTION `GetNewNoCompte`(pSTE CHAR(2)) RETURNS int(11)
BEGIN

	declare nAYDeb, nAYFin int;
	declare nNumero int default 0;

	set nAYDeb = right(cast(year(current_date) AS CHAR),2) * 10000;
	set nAYFin = nAYDeb + 9999;
	
	set nNumero = (SELECT nocompte FROM clients
                   WHERE societe=pSTE AND nocompte BETWEEN nAYDeb AND nAYFin
				   ORDER BY nocompte DESC LIMIT 1);

    if nNUMERO IS NULL then
       SET nNUMERO = nAYDeb;
    end if;

	RETURN nNumero + 1;

END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP FUNCTION IF EXISTS `GetNewNoEntete` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` FUNCTION `GetNewNoEntete`(pSTE CHAR(2), pRubrique VARCHAR(20)) RETURNS int(11)
BEGIN

	declare nAYDeb, nAYFin int;
	declare nNumero int default 0;

	
	set nAYDeb = right(cast(year(current_date) AS CHAR),2) * 10000;
	set nAYFin = nAYDeb + 9999;

	IF pRubrique = 'DEVIS' THEN
		SET nNumero = (SELECT no_id FROM devis 
                   WHERE societe=pSTE AND no_id BETWEEN nAYDeb AND nAYFin
                   ORDER BY no_id DESC LIMIT 1);
	ELSEIF pRubrique = 'FACTURE' THEN
		SET nNumero = (SELECT no_id FROM facture 
                   WHERE societe=pSTE AND no_id BETWEEN nAYDeb AND nAYFin
                   ORDER BY no_id DESC LIMIT 1);
	ELSEIF pRubrique = 'PROFORMA' THEN
		SET nNumero = (SELECT no_id FROM proforma 
                   WHERE societe=pSTE AND no_id BETWEEN nAYDeb AND nAYFin
                   ORDER BY no_id DESC LIMIT 1);
	ELSEIF pRubrique = 'DEM_DEVIS' THEN
		SET nNumero = (SELECT no_id FROM dem_devis 
                   WHERE societe=pSTE AND no_id BETWEEN nAYDeb AND nAYFin
                   ORDER BY no_id DESC LIMIT 1);
	ELSEIF pRubrique = 'ORDRES' THEN
		SET nNumero = (SELECT no_id FROM ordres 
                   WHERE societe=pSTE AND no_id BETWEEN nAYDeb AND nAYFin
                   ORDER BY no_id DESC LIMIT 1);
	ELSEIF pRubrique = 'RDVOUS' THEN
		SET nNumero = (SELECT no_id FROM rdvous 
                   WHERE societe=pSTE AND no_id BETWEEN nAYDeb AND nAYFin
                   ORDER BY no_id DESC LIMIT 1);
    END IF;
    if nNUMERO IS NULL then
       SET nNUMERO = nAYDeb;
    end if;

	RETURN nNumero + 1;

END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP FUNCTION IF EXISTS `GetNewNoExpert` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` FUNCTION `GetNewNoExpert`(pCode_cab INT) RETURNS int(11)
BEGIN

	SET @Numero = (SELECT code_exp FROM p_experts WHERE code_cab = pCode_cab
                   ORDER BY code_cab, code_exp DESC LIMIT 1);
    IF @NUMERO IS NULL then
       SET @NUMERO = 0;
    END IF;

	RETURN @Numero + 1;

END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spDEL_ARTICLES` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spDEL_ARTICLES`(
        IN pSOCIETE CHAR(2),
        IN pREF VARCHAR(20)
    )
BEGIN

  DELETE FROM ARTICLES WHERE ref=pREF;

END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spDEL_CLIENTS` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spDEL_CLIENTS`(
        IN pSTE CHAR(2),
        IN pCode INT
    )
BEGIN

  DELETE FROM CLIENTS 
         WHERE societe=pSTE AND cd_cli=pCode;

END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spDEL_CLIENTS_PURGE` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spDEL_CLIENTS_PURGE`()
BEGIN
	DECLARE sEOF INT;
	DECLARE d_dern_fact DATE;
	DECLARE d_dern_ope DATE;
	DECLARE l_dern_ope DATE;
	DECLARE l_societe CHAR(2);
	DECLARE l_cd_cli INT;
  
	DECLARE cclients CURSOR FOR SELECT societe, cd_cli FROM clients;
	DECLARE CONTINUE HANDLER FOR NOT FOUND SET sEOF = 1;

	SET sEOF = 0;
	OPEN cclients;

lignes_loop : 
  WHILE sEOF = 0 DO
    
    FETCH cclients INTO l_societe, l_cd_cli;
    IF sEOF = 1 THEN
       LEAVE lignes_loop;
    END IF;
    
       SET d_dern_ope = '1900/01/01';
       -- demande de devis
       SET l_dern_ope = (SELECT COALESCE(max(date),'1900/01/01') FROM dem_devis WHERE societe=l_societe AND cd_cli=l_cd_cli);
       IF d_dern_ope < l_dern_ope THEN
          SET d_dern_ope = l_dern_ope;
       END IF;
       -- devis
       SET l_dern_ope = (SELECT COALESCE(max(date),'1900/01/01') FROM devis WHERE societe=l_societe AND cd_cli=l_cd_cli);
       IF d_dern_ope < l_dern_ope THEN
          SET d_dern_ope = l_dern_ope;
       END IF;
       -- factures
       SET l_dern_ope = (SELECT COALESCE(max(date),'1900/01/01') FROM facture WHERE societe=l_societe AND cd_cli=l_cd_cli);
       SET d_dern_fact = l_dern_ope;
       IF d_dern_ope < l_dern_ope THEN
          SET d_dern_ope = l_dern_ope;
       END IF;
       -- proforma
       SET l_dern_ope = (SELECT COALESCE(max(date),'1900/01/01') FROM proforma WHERE societe=l_societe AND cd_cli=l_cd_cli);
       IF d_dern_ope < l_dern_ope THEN
          SET d_dern_ope = l_dern_ope;
       END IF;

       UPDATE clients SET dern_operation = d_dern_ope, dern_facture = d_dern_fact
         WHERE societe=l_societe AND cd_cli=l_cd_cli;

       -- ce client a-t-il des factures
       IF d_dern_fact = '1900/01/01' THEN
          -- oui, supprimer tous les versements si pas de facture
          DELETE liv_bnq FROM liv_bnq 
		  WHERE societe=l_societe AND cd_cli=l_cd_cli;
	   END IF;

  END WHILE lignes_loop;
  CLOSE cclients;

  -- supprimer les clients sans aucun dossier 
  DELETE FROM clients WHERE dern_operation = '1900/01/01';
    
END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spDEL_DEM_DEVIS` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spDEL_DEM_DEVIS`(
        IN pSTE CHAR(2),
        IN pNO_ID INT
    )
BEGIN

  DELETE FROM dem_devis WHERE societe=pSTE AND no_id=pNO_ID;
 
END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spDEL_DEM_LIG` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spDEL_DEM_LIG`(
        IN pSTE CHAR(2),
        IN pNO_ID INT,
        IN pNOLIG INT
    )
BEGIN

  DELETE FROM dem_lig WHERE societe=pSTE AND no_id=pNO_ID AND nolig=pNOLIG;
 
END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spDEL_DEVIS` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spDEL_DEVIS`(
        IN pSTE CHAR(2),
        IN pNO_ID INT
    )
BEGIN

  DELETE FROM devis WHERE societe=pSTE AND no_id=pNO_ID;
 
END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spDEL_DEVIS_LIG` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spDEL_DEVIS_LIG`(
        IN pSTE CHAR(2),
        IN pNO_ID INT,
        IN pNOLIG INT
    )
BEGIN

  DELETE FROM devis_lig WHERE societe=pSTE AND no_id=pNO_ID AND nolig=pNOLIG;
 
END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spDEL_ENTETE` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spDEL_ENTETE`(
        IN pSTE CHAR(2),
        IN pTABLE VARCHAR(20),
        IN pID INTEGER(11)
    )
BEGIN

  SET @sql_text = CONCAT("DELETE FROM ",pTABLE," WHERE societe='",pSTE,"' AND no_id=",pID);

  PREPARE sRequete FROM @sql_text ;
  EXECUTE sRequete ;
  DEALLOCATE PREPARE sRequete ;
  
END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spDEL_FACTURE` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spDEL_FACTURE`(
        IN pSTE CHAR(2),
        IN pNO_ID INT
    )
BEGIN

  DELETE FROM facture WHERE societe=pSTE AND no_id=pNO_ID;
 
END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spDEL_FACTURES_PURGE` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spDEL_FACTURES_PURGE`(
    )
BEGIN
	DECLARE sEOF INT;
	DECLARE l_ste CHAR(2);
	DECLARE l_no_id INT;
	DECLARE l_no_dd INT;
	DECLARE l_noordre INT;
	DECLARE l_nodevis INT;
  
	DECLARE cFactures CURSOR FOR SELECT societe, no_id, no_dd, noordre, nodevis FROM facture 
		WHERE YEAR(date) < YEAR(CURRENT_DATE()) - 10 AND ABS(totalttc - mtregl) < 1;
               
	DECLARE CONTINUE HANDLER FOR NOT FOUND SET sEOF = 1;

	SET sEOF = 0;
	OPEN cFactures;

lignes_loop : 
	WHILE sEOF = 0 DO

		FETCH cFactures INTO l_ste, l_no_id, l_no_dd, l_noordre, l_nodevis;
		IF sEOF = 1 THEN
		   LEAVE lignes_loop;
		END IF;
			   
		IF l_nodevis > 0 THEN
			-- supprimer le devis attaché à une facture REGLEE
			DELETE FROM DEVIS WHERE societe=l_ste AND no_id=l_nodevis;
		END IF;
				   
	END WHILE lignes_loop;
	CLOSE cFactures;

	-- supprimer les factures REGLEES de + de 10 ans
	DELETE FROM facture 
		WHERE YEAR(date) < YEAR(CURRENT_DATE()) - 10 AND ABS(totalttc - mtregl) < 1;
	-- supprimer les devis de + de 10 ans n'ayant pas de facture		 
	DELETE FROM devis WHERE YEAR(date) < YEAR(CURRENT_DATE()) - 10 AND nofact=0;

	-- REZ les liens FACTURE et DEVIS
	UPDATE dem_devis SET nodevis = 0, nofact = 0 WHERE YEAR(date) < YEAR(CURRENT_DATE()) - 10;
    -- recreer les lien DEVIS
	UPDATE dem_devis INNER JOIN facture ON dem_devis.societe = facture.societe and dem_devis.no_id = facture.nochantier
	SET dem_devis.nofact = facture.no_id WHERE YEAR(dem_devis.date) < YEAR(CURRENT_DATE()) - 10;
	UPDATE dem_devis INNER JOIN devis ON dem_devis.societe = devis.societe and dem_devis.no_id = devis.nochantier
	SET dem_devis.nodevis = devis.no_id WHERE YEAR(dem_devis.date) < YEAR(CURRENT_DATE()) - 10;
	-- supprimer les DEM_DEVIS n'ayant aucun DEVIS ni FACRUE
    DELETE FROM bddevfac.dem_devis WHERE YEAR(dem_devis.date) < YEAR(CURRENT_DATE()) - 10 AND nodevis = 0 AND nofact=0;

END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spDEL_FACTURE_LIG` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spDEL_FACTURE_LIG`(
        IN pSTE CHAR(2),
        IN pNO_ID INT,
        IN pNOLIG INT
    )
BEGIN

  DELETE FROM facture_lig WHERE societe=pSTE AND no_id=pNO_ID AND nolig=pNOLIG;
 
END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spDEL_FACTURE_REG` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spDEL_FACTURE_REG`(
        IN pSTE CHAR(2),
        IN pNofact INT,
        IN pCod_bnq INT
    )
BEGIN

  DELETE FROM FACTURE_REG 
         WHERE societe=pSTE AND nofact=pNofact AND cod_bnq=pCod_bnq;

END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spDEL_LETTRE` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spDEL_LETTRE`(
        IN pLETTRE VARCHAR(20)
    )
BEGIN

  DELETE FROM LETTRE WHERE ref=pLETTRE;

END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spDEL_LIGNE` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spDEL_LIGNE`(
        IN pSTE CHAR(2),
        IN pTABLE VARCHAR(20),
        IN pNO_ID INT,
        IN pNOLIG INT
    )
BEGIN

  SET @sql_text = CONCAT("DELETE FROM ",pTABLE," WHERE societe='",pSTE,"' AND no_id=",pNO_ID," AND nolig=",pNOLIG);

  PREPARE sRequete FROM @sql_text ;
  EXECUTE sRequete ;
  DEALLOCATE PREPARE sRequete ;
  
END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spDEL_LIGNES` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spDEL_LIGNES`(
        IN pRubrique VARCHAR(10),
        IN pSTE CHAR(2),
        IN noDossier INTEGER(11),
        IN noDepart INTEGER(11),
        IN noFin INTEGER(11)
    )
BEGIN

  IF pRubrique = "DEVIS" THEN  
     DELETE FROM devis_lig 
       WHERE societe=pSTE AND no_id=noDossier AND nolig BETWEEN noDepart AND noFin;
  ELSEIF pRubrique = "FACTURE" THEN
     DELETE FROM facture_lig 
       WHERE societe=pSTE AND no_id=noDossier AND nolig BETWEEN noDepart AND noFin;
  ELSE
     DELETE FROM proforma_lig 
       WHERE societe=pSTE AND no_id=noDossier AND nolig BETWEEN noDepart AND noFin;
  END IF;
END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spDEL_LIV_BNQ` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spDEL_LIV_BNQ`(
        IN pSTE CHAR(2),
        IN pCod_bnq INT
    )
BEGIN

  DELETE FROM LIV_BNQ 
         WHERE societe=pSTE AND cod_bnq=pCod_bnq;

END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spDEL_PROFORMA` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spDEL_PROFORMA`(
        IN pSTE CHAR(2),
        IN pNO_ID INT
    )
BEGIN

  DELETE FROM proforma WHERE societe=pSTE AND no_id=pNO_ID;
 
END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spDEL_PROFORMA_LIG` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spDEL_PROFORMA_LIG`(
        IN pSTE CHAR(2),
        IN pNO_ID INT,
        IN pNOLIG INT
    )
BEGIN

  DELETE FROM proforma_lig WHERE societe=pSTE AND no_id=pNO_ID AND nolig=pNOLIG;
 
END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spDEL_PURGE` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spDEL_PURGE`()
BEGIN

	CALL spDEL_FACTURES_PURGE();

	-- CALL spDEL_CLIENTS_PURGE();


END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spDEL_P_CABINET` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spDEL_P_CABINET`(
        IN pCode INT
    )
BEGIN

  DELETE FROM P_CABINET WHERE code=pCode;

END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spDEL_P_EXPERTS` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spDEL_P_EXPERTS`(
        IN pSTE CHAR(2),
        IN pCode_cab INT,
        IN pCode_exp INT
    )
BEGIN

  SET @sql_text = CONCAT("DELETE FROM P_EXPERTS WHERE code_cab=",pCode_cab);
  IF pCode_exp > 0 THEN
     SET @sql_text = CONCAT(@sql_text," AND code_exp=",pCode_exp);
  END IF;
  
  PREPARE sRequete FROM @sql_text ;
  EXECUTE sRequete ;
  DEALLOCATE PREPARE sRequete ;

END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spDEL_P_USERS` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spDEL_P_USERS`(
        IN pCODE CHAR(5)
    )
BEGIN
   delete from `p_users` where cd_uti = pCODE;
END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spDEL_T_IMP_RDV_JOUR` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spDEL_T_IMP_RDV_JOUR`(
        IN pCode VARCHAR(10)
    )
BEGIN

  DELETE FROM T_IMP_RDV_JOUR WHERE codeuser = pCode;
  
END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spDEL_T_RELANCE` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spDEL_T_RELANCE`(
        IN pCode VARCHAR(10)
    )
BEGIN

  DELETE FROM T_RELANCE WHERE codeuser = pCode;
  
END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spDEL_T_TABLE` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spDEL_T_TABLE`(
        IN pTable VARCHAR(25),
        IN pCode VARCHAR(10)
    )
BEGIN

  SET @sql_text = CONCAT("DELETE FROM ",pTable," WHERE codeuser = '",pCode,"'");
  
  PREPARE sRequete FROM @sql_text ;
  EXECUTE sRequete ;
  DEALLOCATE PREPARE sRequete ;

END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spDUP_CLIENTS` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spDUP_CLIENTS`(
        IN pSTE CHAR(2),
        IN pCode INT,
        IN pDESTINATION CHAR(2)
        )
BEGIN


SET @newcode = GetNewNoClient(pDESTINATION);

INSERT INTO CLIENTS (societe,CD_CLI,qualite,nom,ADRESSE,ADRESSE2,CP,VILLE,tel1,NOMRESP,tel2,TELP,FAX,Type,AGREMENT, website,notarif,MODEREG,DelaiReg,Relance,CABINET,EXPERT,groupe,comment,etage,code,EMAIL) 
  SELECT pDESTINATION,@newcode,qualite,nom,ADRESSE,ADRESSE2,CP,VILLE,tel1,NOMRESP,tel2,TELP,FAX,Type,AGREMENT, website,notarif,MODEREG,DelaiReg,Relance,CABINET,EXPERT,groupe,comment,etage,code,EMAIL
  FROM clients
  WHERE societe=pSTE AND cd_cli=pCode;

END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spDUP_DEM_DEVIS` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spDUP_DEM_DEVIS`(
        IN pSTE CHAR(2),
        IN pCode INT,
        IN pCD_CLI INT,
		IN pNOMCLI VARCHAR(40),
        IN pDESTINATION CHAR(2),
		IN pUSER VARCHAR(10)
        )
BEGIN

SET @newcode = GetNewNoEntete(pDESTINATION,"DEM_DEVIS");
-- recherche du client dans la nouvelle societe
SET @cd_cli = (SELECT COALESCE(cd_cli,0) FROM clients 
               WHERE societe=pDESTINATION 
               AND nom=pNOMCLI 
               AND adresse=(SELECT adresse FROM clients WHERE societe=pSTE AND cd_cli=pCD_CLI)
               AND cp=(SELECT cp FROM clients WHERE societe=pSTE AND cd_cli=pCD_CLI) LIMIT 1);

IF @cd_cli > 0 THEN
INSERT INTO DEM_DEVIS (societe,no_id,cd_cli,nomcli,date,c_qualite,c_nom,c_adr,c_adr2,c_cp,c_ville,c_tel1,c_tel2,
  c_obs,nopol,nosin,vref,libcompl,typecli,mttrav,tx_trav,c_etage,c_code,c_fax,notarif,
  codetva,tauxtva,c_telp,typetrav,groupe,ref_expert,c_email,usermaj) 
  SELECT pDESTINATION,@newcode,@cd_cli,nomcli,CURRENT_DATE(),c_qualite,c_nom,c_adr,c_adr2,c_cp,c_ville,c_tel1,c_tel2,
  c_obs,nopol,nosin,vref,libcompl,typecli,mttrav,tx_trav,c_etage,c_code,c_fax,notarif,
  codetva,tauxtva,c_telp,typetrav,groupe,ref_expert,c_email,pUSER 
  FROM dem_devis
  WHERE societe=pSTE AND no_id=pCode;
END IF;

END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spDUP_DEVIS` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spDUP_DEVIS`(
        IN pSTE CHAR(2),
        IN pCode INT,
        IN pCD_CLI INT,
		IN pNOMCLI VARCHAR(40),
        IN pDESTINATION CHAR(2),
		IN pUSER VARCHAR(10)
        )
BEGIN

SET @newcode = GetNewNoEntete(pDESTINATION,"DEVIS");
-- recherche du client dans la nouvelle societe
SET @cd_cli = (SELECT COALESCE(cd_cli,0) FROM clients 
               WHERE societe=pDESTINATION 
               AND nom=pNOMCLI 
               AND adresse=(SELECT adresse FROM clients WHERE societe=pSTE AND cd_cli=pCD_CLI)
               AND cp=(SELECT cp FROM clients WHERE societe=pSTE AND cd_cli=pCD_CLI) LIMIT 1);

IF @cd_cli > 0 THEN
	INSERT INTO DEVIS (societe,no_id,cd_cli,nomcli,date,c_qualite,c_nom,c_adr,c_adr2,c_cp,c_ville,c_tel1,c_tel2,
	  c_obs,nopol,nosin,vref,libcompl,typecli,c_etage,c_code,c_fax,notarif,
	  codetva,tauxtva,c_telp,typetrav,groupe,ref_expert,c_email,usermaj) 
	  SELECT pDESTINATION,@newcode,@cd_cli,nomcli,CURRENT_DATE(),c_qualite,c_nom,c_adr,c_adr2,c_cp,c_ville,c_tel1,c_tel2,
	  c_obs,nopol,nosin,vref,libcompl,typecli,c_etage,c_code,c_fax,notarif,
	  codetva,tauxtva,c_telp,typetrav,groupe,ref_expert,c_email,pUSER 
	  FROM devis
	  WHERE societe=pSTE AND no_id=pCode;
      
	INSERT INTO devis_lig (societe,NO_ID,NOLIG,REF,LIB,QTE,PRIXHT,MTHT,GROUPE,DATEMAJ,USERMAJ) 
      SELECT pDESTINATION,@newcode,nolig,ref,lib,qte,prixht,mtht,groupe,now(), pUSER 
          FROM devis_lig WHERE societe=pSTE AND no_id=pCode;

END IF;

END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spDUP_FACTURE` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spDUP_FACTURE`(
        IN pSTE CHAR(2),
        IN pCode INT,
        IN pCD_CLI INT,
		IN pNOMCLI VARCHAR(40),
        IN pDESTINATION CHAR(2),
		IN pUSER VARCHAR(10)
        )
BEGIN

SET @newcode = GetNewNoEntete(pDESTINATION,"FACTURE");
-- recherche du client dans la nouvelle societe
SET @cd_cli = (SELECT COALESCE(cd_cli,0) FROM clients 
               WHERE societe=pDESTINATION 
               AND nom=pNOMCLI 
               AND adresse=(SELECT adresse FROM clients WHERE societe=pSTE AND cd_cli=pCD_CLI)
               AND cp=(SELECT cp FROM clients WHERE societe=pSTE AND cd_cli=pCD_CLI) LIMIT 1);

IF @cd_cli > 0 THEN
	INSERT INTO FACTURE (societe,no_id,cd_cli,nomcli,date,c_qualite,c_nom,c_adr,c_adr2,c_cp,c_ville,c_tel1,c_tel2,
	  c_obs,nopol,nosin,vref,libcompl,typecli,c_etage,c_code,c_fax,notarif,
	  codetva,tauxtva,c_telp,typetrav,groupe,ref_expert,c_email,usermaj) 
	  SELECT pDESTINATION,@newcode,@cd_cli,nomcli,CURRENT_DATE(),c_qualite,c_nom,c_adr,c_adr2,c_cp,c_ville,c_tel1,c_tel2,
	  c_obs,nopol,nosin,vref,libcompl,typecli,c_etage,c_code,c_fax,notarif,
	  codetva,tauxtva,c_telp,typetrav,groupe,ref_expert,c_email,pUSER 
	  FROM FACTURE
	  WHERE societe=pSTE AND no_id=pCode;
      
	INSERT INTO FACTURE_LIG (societe,NO_ID,NOLIG,REF,LIB,QTE,PRIXHT,MTHT,GROUPE,DATEMAJ,USERMAJ) 
      SELECT pDESTINATION,@newcode,nolig,ref,lib,qte,prixht,mtht,groupe,now(), pUSER 
          FROM FACTURE_LIG WHERE societe=pSTE AND no_id=pCode;

END IF;

END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spDUP_LIGNES` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spDUP_LIGNES`(
        IN pRubrique VARCHAR(10),
        IN pSTE CHAR(2),
        IN noDossier INTEGER(11),
        IN noDepart INTEGER(11),
        IN noFin INTEGER(11),
        IN noDestin INTEGER(11)
    )
BEGIN

  IF pRubrique = "DEVIS" THEN  
     UPDATE devis_lig SET nolig = nolig + noFin - noDepart + 1
         WHERE societe=pSTE AND no_id=noDossier AND nolig >= noDestin ORDER BY nolig DESC;
  
     INSERT INTO devis_lig (societe,no_id,nolig,ref,lib,qte,prixht,mtht,datemaj, usermaj) 
         SELECT societe,no_id, noDestin + nolig - noDepart,ref,lib,qte,prixht,mtht,datemaj, usermaj 
         FROM devis_lig WHERE societe=pSTE AND no_id = noDossier AND nolig BETWEEN noDepart AND noFin;
  ELSEIF pRubrique = "FACTURE" THEN
     UPDATE facture_lig SET nolig = nolig + noFin - noDepart + 1
         WHERE societe=pSTE AND no_id=noDossier AND nolig >= noDestin ORDER BY nolig DESC;
  
     INSERT INTO facture_lig (societe,no_id,nolig,ref,lib,qte,prixht,mtht,datemaj, usermaj) 
         SELECT societe,no_id, noDestin + nolig - noDepart,ref,lib,qte,prixht,mtht,datemaj, usermaj 
         FROM facture_lig WHERE societe=pSTE AND no_id = noDossier AND nolig BETWEEN noDepart AND noFin;
  ELSE
     UPDATE proforma_lig SET nolig = nolig + noFin - noDepart + 1
         WHERE societe=pSTE AND no_id=noDossier AND nolig >= noDestin ORDER BY nolig DESC;
  
     INSERT INTO proforma_lig (societe,no_id,nolig,ref,lib,qte,prixht,mtht,datemaj, usermaj) 
         SELECT societe,no_id, noDestin + nolig - noDepart,ref,lib,qte,prixht,mtht,datemaj, usermaj 
         FROM devis_lig WHERE societe=pSTE AND no_id = noDossier AND nolig BETWEEN noDepart AND noFin;
  END IF;
     
END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spGET_AGENDAS` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`127.0.0.1` PROCEDURE `spGET_AGENDAS`(
	IN pCode CHAR
    )
BEGIN

	IF pCode = "T" THEN
		-- Lire tous les agendas actifs ?
		SELECT * FROM p_users WHERE agenda IS NOT NULL AND actif <> 0 ORDER BY nom;
	ELSEIF pCode = "C" THEN
		-- Lire agendas de chantier ?
		SELECT * FROM p_users WHERE agenda = 'N' ORDER BY nom;
	ELSE
		-- Lire agendas de rendez-vous ?
		SELECT * FROM p_users WHERE agenda <> 'N' AND agenda IS NOT NULL AND actif <> 0 ORDER BY nom;
	END IF;  

END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spGET_ARTICLES` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spGET_ARTICLES`(
        IN pCode VARCHAR(10)
    )
BEGIN
  IF INSTR(pCODE,'%') > 0 then
    
    SELECT * FROM articles WHERE ref LIKE pCode;
  ELSE
    
    SELECT * FROM articles WHERE ref = pCode;
  END IF;

END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spGET_ARTICLES_byFAM` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spGET_ARTICLES_byFAM`(
        IN pFam VARCHAR(10)
    )
BEGIN

    SELECT * FROM articles WHERE fam = pFam order by ref;

END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spGET_CABINET` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spGET_CABINET`(
        IN pCode INT
    )
BEGIN

    SELECT * FROM P_CABINET WHERE code = pCode;
  
END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spGET_CABINETbyNom` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spGET_CABINETbyNom`(
        IN pNom VARCHAR(30)
    )
BEGIN

    SELECT * FROM p_cabinet WHERE nom like pNom ORDER BY nom;
  
END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spGET_CHANTIERS_byName` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spGET_CHANTIERS_byName`(
        IN pSTE CHAR(2),
        IN pChantier VARCHAR(100)
    )
BEGIN

SET @numero = COALESCE(CAST(pChantier AS UNSIGNED),0);
IF @numero > 0 THEN
	-- recherche par numero 
	SELECT date, LPAD(no_id,6,'0') AS numero, nomcli, CONCAT(c_nom,'; ',c_adr,'; ',c_ville) AS chantier, mttrav AS montant, status, nosin, nopol, datecl
	FROM dem_devis
	WHERE societe=pSTE AND no_id >= @numero ORDER BY numero LIMIT 300;

ELSEIF length(pChantier) = 0 THEN
	-- recherche les derniers dossiers crees 
	SELECT date, LPAD(no_id,6,'0') AS numero, nomcli, CONCAT(c_nom,'; ',c_adr,'; ',c_ville) AS chantier, mttrav AS montant, status, nosin, nopol, datecl FROM dem_devis
	WHERE societe=pSTE ORDER BY no_id DESC LIMIT 300;
ELSE
	-- recherche par nom chantier
	SET @I = LOCATE(";",pChantier);
	SET @J = 0;
	IF @I = 0 THEN
		SET @nom = CONCAT(TRIM(pChantier),"%");
		SET @adresse= "%";
		SET @ville = "%";
	ELSE
		SET @J = LOCATE(";",pChantier,@I+1);
		IF @J = 0 THEN
			SET @nom = CONCAT(TRIM(SUBSTRING(pChantier, 1, @I - 1)),"%");
			SET @adresse = CONCAT("%",TRIM(SUBSTRING(pChantier, @I + 1, 50)),"%");
			SET @ville = "%";
		ELSE
			SET @nom = CONCAT(TRIM(SUBSTRING(pChantier, 1, @I - 1)),"%");
			SET @adresse = CONCAT("%",TRIM(SUBSTRING(pChantier, @I + 1, @J - @I - 1)),"%");
			SET @ville = CONCAT("%",TRIM(SUBSTRING(pChantier, @J + 1, 50)),"%");
		END IF;
	END IF;

	IF @nom = "NOPOL%" THEN
		-- recherche par no police
		SELECT date,'DD' AS TYPE, LPAD(no_id,6,'0') AS numero, nomcli, CONCAT(c_nom,'; ',c_adr,'; ',c_ville) AS chantier, mttrav AS montant, status, nosin, nopol, datecl 
		FROM dem_devis
		WHERE societe=pSTE AND nopol LIKE @adresse
		ORDER BY date DESC;
	ELSEIF @nom = "NOSIN%" THEN
		-- recherche par no sinistre
		SELECT date,'DD' AS TYPE, LPAD(no_id,6,'0') AS numero, nomcli, CONCAT(c_nom,'; ',c_adr,'; ',c_ville) AS chantier, mttrav AS montant, status, nosin, nopol, datecl 
		   FROM dem_devis
		   WHERE societe=pSTE AND nosin LIKE @adresse 
		ORDER BY date DESC;
	ELSEIF @nom = "REFCLI%" THEN
		-- recherche par no sinistre
		SELECT date,'DD' AS TYPE, LPAD(no_id,6,'0') AS numero, nomcli, CONCAT(c_nom,'; ',c_adr,'; ',c_ville) AS chantier, mttrav AS montant, status, nosin, nopol, datecl 
		FROM dem_devis
		WHERE societe=pSTE AND vref LIKE @adresse 
		ORDER BY chantier, nomcli, date DESC;
	ELSE
		-- dem_devis du client
		SELECT date,'DD' AS TYPE, LPAD(no_id,6,'0') AS numero, nomcli, CONCAT(c_nom,'; ',c_adr,'; ',c_ville) AS chantier, mttrav AS montant, status, nosin, nopol, datecl 
		FROM dem_devis
		WHERE societe=pSTE AND c_nom LIKE @nom AND c_adr LIKE @adresse   AND c_ville LIKE @ville 
	  ORDER BY chantier, nomcli, date DESC;
  END IF;

END IF;

END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spGET_CLIENTS` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spGET_CLIENTS`(
        IN pSte CHAR(2),
        IN pCode INT
    )
BEGIN

    SELECT * FROM clients WHERE societe=pSte AND cd_cli = pCode;
  
END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spGET_CLIENTSbyADRESSE` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spGET_CLIENTSbyADRESSE`(
        IN pSte CHAR(2),
        IN pAdresse VARCHAR(40)
    )
BEGIN

  SELECT * FROM clients 
    WHERE societe=pSte AND adresse LIKE pAdresse ORDER BY adresse;
  
END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spGET_CLIENTSbyAGREMENT` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spGET_CLIENTSbyAGREMENT`(
        IN pSte CHAR(2)
    )
BEGIN

  SELECT agrement, nom, notarif FROM clients 
    WHERE societe=pSte AND length(agrement) > 0 GROUP BY agrement ORDER BY nom;
  
END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spGET_CLIENTSbyDOUBLON` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spGET_CLIENTSbyDOUBLON`(
        IN pSte CHAR(2),
        IN pNom VARCHAR(40),
        IN pAdresse VARCHAR(40),
        IN pCP VARCHAR(40)
    )
BEGIN

  SELECT * FROM clients 
    WHERE societe=pSte AND nom=pNom AND adresse=pAdresse AND cp=pCP;
  
END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spGET_CLIENTSbyNom` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spGET_CLIENTSbyNom`(
        IN pSte CHAR(2),
        IN pNom VARCHAR(30)
    )
BEGIN

	SET @numero = COALESCE(CAST(pNom AS UNSIGNED),0);
    if pNom <> CAST(@numero AS CHAR) then
		SET @numero = 0;
    end if;
	IF @numero > 0 THEN
		-- recherche par numero client
		SELECT * FROM clients WHERE societe=pSte AND cd_cli >= @numero LIMIT 100;
	ELSE
		-- recherche par nom client
		SET @I = LOCATE(";",pNom);
		SET @J = 0;
		IF @I = 0 THEN
			SET @nom = CONCAT(TRIM(pNom),"%");
			SET @adresse= "%";
			SET @ville = "%";
		ELSE
			SET @J = LOCATE(";",pNom,@I+1);
			IF @J = 0 THEN
				SET @nom = CONCAT(TRIM(SUBSTRING(pNom, 1, @I - 1)),"%");
				SET @adresse = CONCAT("%",TRIM(SUBSTRING(pNom, @I + 1, 50)),"%");
				SET @ville = "%";
			ELSE
				SET @nom = CONCAT(TRIM(SUBSTRING(pNom, 1, @I - 1)),"%");
				SET @adresse = CONCAT("%",TRIM(SUBSTRING(pNom, @I + 1, @J - @I - 1)),"%");
				SET @ville = CONCAT("%",TRIM(SUBSTRING(pNom, @J + 1, 50)),"%");
			END IF;
		END IF;

		SELECT * FROM clients 
			WHERE societe=pSTE AND nom LIKE @nom AND adresse LIKE @adresse AND ville LIKE @ville  LIMIT 100;
	END IF;


END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spGET_CLIENT_COMPTE` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spGET_CLIENT_COMPTE`(
        IN pSTE CHAR(2),
        IN pCd_cli INTEGER,
        IN pToutes SMALLINT
    )
BEGIN

IF pToutes = 0 THEN
  -- lire les lignes non lettrees
  SELECT * FROM (
     -- factures du client
	 SELECT date, CONCAT(c_qualite,' ',c_nom) AS libelle, totalttc AS debit, 0 AS credit, totalttc-mtregl AS SOLDE, CONCAT('FA-',LPAD(no_id,6,'0')) AS noligne, lettre, 0 AS remboursement FROM FACTURE 
       WHERE societe=pSTE AND cd_cli=pCd_cli AND lettre = 0 
  UNION
     -- versement du client
	 SELECT date, CONCAT(lib,': ', ets,' ',jst) AS libelle, 0 AS debit, montantregl AS credit, montantregl-montantdebit AS SOLDE, LPAD(cod_bnq,6,'0') AS noligne, lettre, remboursement FROM LIV_BNQ 
       INNER JOIN p_modereg ON liv_bnq.modereg = p_modereg.code
       WHERE societe=pSTE AND cd_cli=pCd_cli AND lettre = 0 
  ) a
  ORDER BY date DESC;
ELSE
  SELECT * FROM (
     -- factures du client
	 SELECT date, CONCAT(c_qualite,' ',c_nom) AS libelle, totalttc AS debit, 0 AS credit, totalttc-mtregl AS SOLDE, CONCAT('FA-',LPAD(no_id,6,'0')) AS noligne, lettre, 0 AS remboursement FROM FACTURE 
       WHERE societe=pSTE AND cd_cli=pCd_cli
  UNION
     -- versement du client
	 SELECT date, CONCAT(lib,': ', ets,' ',jst) AS libelle, 0 AS debit, montantregl AS credit, montantregl-montantdebit AS SOLDE, LPAD(cod_bnq,6,'0') AS noligne, lettre, remboursement FROM LIV_BNQ 
       INNER JOIN p_modereg ON liv_bnq.modereg = p_modereg.code
       WHERE societe=pSTE AND cd_cli=pCd_cli
  ) a
  ORDER BY date DESC;
END IF;
 
END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spGET_DOSSIERS_byChantier` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spGET_DOSSIERS_byChantier`(
        IN pSTE CHAR(2),
        IN pTYPE CHAR(2),
        IN pChantier VARCHAR(100)
    )
BEGIN

SET @numero = COALESCE(CAST(pChantier AS UNSIGNED),0);

IF @numero > 0 THEN
	-- recherche par numero 
	CASE pTYPE
	WHEN 'DE' THEN
		-- devis du client
		SELECT date,'DE' AS TYPE, LPAD(no_id,6,'0') AS numero, nomcli, CONCAT(c_nom,'; ',c_adr,'; ',c_ville) AS chantier, COALESCE(totalht,0) AS montant, status, nosin, nopol, nochantier
		FROM devis WHERE societe=pSTE AND no_id >= @numero LIMIT 300;
	WHEN 'FA' THEN
		-- factures du client
		SELECT date,'FA' AS TYPE, LPAD(no_id,6,'0') AS numero, nomcli, CONCAT(c_nom,'; ',c_adr,'; ',c_ville) AS chantier, COALESCE(totalht,0) AS montant, status, nosin, nopol, nochantier
		FROM facture
		WHERE societe=pSTE AND no_id >= @numero LIMIT 300;
	WHEN 'PF' THEN
		-- proforma du client
		SELECT date,'PF' AS TYPE, LPAD(no_id,6,'0') AS numero, nomcli, CONCAT(c_nom,'; ',c_adr,'; ',c_ville) AS chantier, COALESCE(totalht,0) AS montant, status, nosin, nopol , nochantier
		FROM proforma
		WHERE societe=pSTE AND no_id >= @numero LIMIT 300;
	END CASE;

ELSEIF length(pChantier) = 0 THEN
	-- recherche les derniers dossiers crees 
	CASE pTYPE
	WHEN 'DE' THEN
		-- devis du client
		SELECT date,'DE' AS TYPE, LPAD(no_id,6,'0') AS numero, nomcli, CONCAT(c_nom,'; ',c_adr,'; ',c_ville) AS chantier, COALESCE(totalht,0) AS montant, status, nosin, nopol, nochantier FROM devis
		WHERE societe=pSTE ORDER BY no_id DESC LIMIT 300;
	WHEN 'FA' THEN
		-- factures du client
		SELECT date,'FA' AS TYPE, LPAD(no_id,6,'0') AS numero, nomcli, CONCAT(c_nom,'; ',c_adr,'; ',c_ville) AS chantier, COALESCE(totalht,0) AS montant, status, nosin, nopol , nochantier FROM facture
		WHERE societe=pSTE ORDER BY no_id DESC LIMIT 300;
	WHEN 'PF' THEN
		-- proforma du client
		SELECT date,'PF' AS TYPE, LPAD(no_id,6,'0') AS numero, nomcli, CONCAT(c_nom,'; ',c_adr,'; ',c_ville) AS chantier, COALESCE(totalht,0) AS montant, status, nosin, nopol , nochantier FROM proforma
		WHERE societe=pSTE ORDER BY no_id DESC LIMIT 300;
	END CASE;
ELSE
	-- recherche par nom chantier
	SET @I = LOCATE(";",pChantier);
	SET @J = 0;
	IF @I = 0 THEN
		SET @nom = CONCAT(TRIM(pChantier),"%");
		SET @adresse= "%";
		SET @ville = "%";
	ELSE
		SET @J = LOCATE(";",pChantier,@I+1);
		IF @J = 0 THEN
			SET @nom = CONCAT(TRIM(SUBSTRING(pChantier, 1, @I - 1)),"%");
			SET @adresse = CONCAT("%",TRIM(SUBSTRING(pChantier, @I + 1, 50)),"%");
			SET @ville = "%";
		ELSE
			SET @nom = CONCAT(TRIM(SUBSTRING(pChantier, 1, @I - 1)),"%");
			SET @adresse = CONCAT("%",TRIM(SUBSTRING(pChantier, @I + 1, @J - @I - 1)),"%");
			SET @ville = CONCAT("%",TRIM(SUBSTRING(pChantier, @J + 1, 50)),"%");
		END IF;
	END IF;

	IF @nom = "NOPOL%" THEN
	  -- recherche par no police
	  SELECT * FROM (
		 -- dem_devis du client
		 SELECT date,'DD' AS TYPE, LPAD(no_id,6,'0') AS numero, nomcli, CONCAT(c_nom,'; ',c_adr,'; ',c_ville) AS chantier, mttrav AS montant, status, nosin, nopol , no_id as nochantier
		   FROM dem_devis
		   WHERE societe=pSTE AND nopol LIKE @adresse
	  UNION
		 -- devis du client
		 SELECT date,'DE', LPAD(no_id,6,'0') AS numero, nomcli, CONCAT(c_nom,'; ',c_adr,'; ',c_ville) AS chantier, COALESCE(totalht,0) AS montant, status, nosin, nopol , nochantier
		   FROM devis
		   WHERE societe=pSTE AND nopol LIKE @adresse 
	  UNION
		 -- factures du client
		 SELECT date,'FA', LPAD(no_id,6,'0') AS numero, nomcli, CONCAT(c_nom,'; ',c_adr,'; ',c_ville) AS chantier, COALESCE(totalht,0) AS montant, status, nosin, nopol , nochantier
		   FROM facture
		   WHERE societe=pSTE AND nopol LIKE @adresse 
	  UNION
		 -- proforma du client
		 SELECT date,'PF', LPAD(no_id,6,'0') AS numero, nomcli, CONCAT(c_nom,'; ',c_adr,'; ',c_ville) AS chantier, COALESCE(totalht,0) AS montant, status, nosin, nopol , nochantier
		   FROM proforma
		   WHERE societe=pSTE AND nopol LIKE @adresse 
	  ) a
	  ORDER BY chantier, nomcli, date DESC;
  ELSEIF @nom = "NOSIN%" THEN
	  -- recherche par no sinistre
	  SELECT * FROM (
		 -- dem_devis du client
		 SELECT date,'DD' AS TYPE, LPAD(no_id,6,'0') AS numero, nomcli, CONCAT(c_nom,'; ',c_adr,'; ',c_ville) AS chantier, mttrav AS montant, status, nosin, nopol , no_id as nochantier
		   FROM dem_devis
		   WHERE societe=pSTE AND nosin LIKE @adresse 
	  UNION
		 -- devis du client
		 SELECT date,'DE', LPAD(no_id,6,'0') AS numero, nomcli, CONCAT(c_nom,'; ',c_adr,'; ',c_ville) AS chantier, COALESCE(totalht,0) AS montant, status, nosin, nopol , nochantier
		   FROM devis
		   WHERE societe=pSTE AND nosin LIKE @adresse 
	  UNION
		 -- factures du client
		 SELECT date,'FA', LPAD(no_id,6,'0') AS numero, nomcli, CONCAT(c_nom,'; ',c_adr,'; ',c_ville) AS chantier, COALESCE(totalht,0) AS montant, status, nosin, nopol , nochantier
		   FROM facture
		   WHERE societe=pSTE AND nosin LIKE @adresse 
	  UNION
		 -- proforma du client
		 SELECT date,'PF', LPAD(no_id,6,'0') AS numero, nomcli, CONCAT(c_nom,'; ',c_adr,'; ',c_ville) AS chantier, COALESCE(totalht,0) AS montant, status, nosin, nopol, nochantier
		   FROM proforma
		   WHERE societe=pSTE AND nosin LIKE @adresse 
	  ) a
	  ORDER BY chantier, nomcli, date DESC;
  ELSEIF @nom = "REFCLI%" THEN
	  -- recherche par no sinistre
	  SELECT * FROM (
		 -- dem_devis du client
		 SELECT date,'DD' AS TYPE, LPAD(no_id,6,'0') AS numero, nomcli, CONCAT(c_nom,'; ',c_adr,'; ',c_ville) AS chantier, mttrav AS montant, status, nosin, nopol , no_id as nochantier
		   FROM dem_devis
		   WHERE societe=pSTE AND vref LIKE @adresse 
	  UNION
		 -- devis du client
		 SELECT date,'DE', LPAD(no_id,6,'0') AS numero, nomcli, CONCAT(c_nom,'; ',c_adr,'; ',c_ville) AS chantier, COALESCE(totalht,0) AS montant, status, nosin, nopol , nochantier
		   FROM devis
		   WHERE societe=pSTE AND vref LIKE @adresse 
	  UNION
		 -- factures du client
		 SELECT date,'FA', LPAD(no_id,6,'0') AS numero, nomcli, CONCAT(c_nom,'; ',c_adr,'; ',c_ville) AS chantier, COALESCE(totalht,0) AS montant, status, nosin, nopol , nochantier
		   FROM facture
		   WHERE societe=pSTE AND vref LIKE @adresse 
	  UNION
		 -- proforma du client
		 SELECT date,'PF', LPAD(no_id,6,'0') AS numero, nomcli, CONCAT(c_nom,'; ',c_adr,'; ',c_ville) AS chantier, COALESCE(totalht,0) AS montant, status, nosin, nopol, nochantier
		   FROM proforma
		   WHERE societe=pSTE AND vref LIKE @adresse 
	  ) a
	  ORDER BY chantier, nomcli, date DESC;
	ELSE
		SELECT * FROM (
			-- dem_devis du client
			(SELECT date,'DD' AS TYPE, LPAD(no_id,6,'0') AS numero, nomcli, CONCAT(c_nom,'; ',c_adr,'; ',c_ville) AS chantier, mttrav AS montant, status, nosin, nopol , no_id as nochantier
			FROM dem_devis
			WHERE societe=pSTE AND c_nom LIKE @nom AND c_adr LIKE @adresse   AND c_ville LIKE @ville  LIMIT 500)
		UNION
			-- devis du client
			(SELECT date,'DE' AS TYPE, LPAD(no_id,6,'0') AS numero, nomcli, CONCAT(c_nom,'; ',c_adr,'; ',c_ville) AS chantier, COALESCE(totalht,0) AS montant, status, nosin, nopol , nochantier
			FROM devis
			WHERE societe=pSTE AND c_nom LIKE @nom AND c_adr LIKE @adresse   AND c_ville LIKE @ville LIMIT 500)
		UNION
			-- factures du client
			(SELECT date,'FA' AS TYPE, LPAD(no_id,6,'0') AS numero, nomcli, CONCAT(c_nom,'; ',c_adr,'; ',c_ville) AS chantier, COALESCE(totalht,0) AS montant, status, nosin, nopol , nochantier
			FROM facture
			WHERE societe=pSTE AND c_nom LIKE @nom AND c_adr LIKE @adresse   AND c_ville LIKE @ville LIMIT 500)
		UNION
			-- proforma du client
			(SELECT date,'PF' AS TYPE, LPAD(no_id,6,'0') AS numero, nomcli, CONCAT(c_nom,'; ',c_adr,'; ',c_ville) AS chantier, COALESCE(totalht,0) AS montant, status, nosin, nopol , nochantier
			FROM proforma
			WHERE societe=pSTE AND c_nom LIKE @nom AND c_adr LIKE @adresse   AND c_ville LIKE @ville LIMIT 100)
	  ) a
	  ORDER BY date DESC;
  END IF;

END IF;

END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spGET_DOSSIERS_byClient` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spGET_DOSSIERS_byClient`(
        IN pSTE CHAR(2),
        IN pCd_cli INTEGER
    )
BEGIN

  SELECT * FROM (
     -- dem_devis du client
	 SELECT date,'DD' AS TYPE, LPAD(no_id,6,'0') AS numero, CONCAT(c_qualite,' ',c_nom) AS libelle, c_adr, 0 AS montant, status 
       FROM dem_devis
       WHERE societe=pSTE AND cd_cli=pCd_cli
  UNION
     -- devis du client
	 SELECT date,'DE', LPAD(no_id,6,'0'), CONCAT(c_qualite,' ',c_nom) AS libelle, c_adr, totalttc AS montant, status 
       FROM devis
       WHERE societe=pSTE AND cd_cli=pCd_cli
  UNION
     -- factures du client
	 SELECT date,'FA', LPAD(no_id,6,'0'), CONCAT(c_qualite,' ',c_nom) AS libelle, c_adr, totalttc AS montant, status 
       FROM facture
       WHERE societe=pSTE AND cd_cli=pCd_cli
  ) a
ORDER BY date DESC;
 
END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spGET_DOSSIERS_byNumeo` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spGET_DOSSIERS_byNumeo`(
        IN pSTE CHAR(2),
        IN pChantier INT(11)
    )
BEGIN

	-- recherche par numero de chantier
	SELECT * FROM (
		-- devis du client
		SELECT date,'DE' AS TYPE, LPAD(no_id,6,'0') AS numero, nomcli, CONCAT(c_nom,'; ',c_adr,'; ',c_ville) AS chantier, COALESCE(totalht,0) AS montant, 
        status, nosin, nopol, nochantier, concat(societe,'-DE',no_id) AS nodossier
		FROM devis
		WHERE societe=pSTE AND nochantier = pChantier
	UNION
		-- factures du client
		SELECT date,'FA' AS TYPE, LPAD(no_id,6,'0') AS numero, nomcli, CONCAT(c_nom,'; ',c_adr,'; ',c_ville) AS chantier, COALESCE(totalht,0) AS montant, 
        status, nosin, nopol, nochantier, concat(societe,'-FA',no_id) AS nodossier
		FROM facture
		WHERE societe=pSTE AND nochantier = pChantier
	UNION
		-- proforma du client
		SELECT date,'PF' AS TYPE, LPAD(no_id,6,'0') AS numero, nomcli, CONCAT(c_nom,'; ',c_adr,'; ',c_ville) AS chantier, COALESCE(totalht,0) AS montant, 
        status, nosin, nopol , nochantier, concat(societe,'-PF',no_id) AS nodossier
		FROM proforma
		WHERE societe=pSTE AND nochantier = pChantier
	  ) a
	ORDER BY date;

END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spGET_DOSSIER_EMAIL` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spGET_DOSSIER_EMAIL`(
        IN pSTE CHAR(2),
        IN pNoDossier varchar(10)
    )
BEGIN

	SET @NO_ID = SUBSTR(pNoDossier, 3, 6);
    
	IF SUBSTR(pNoDossier, 1, 2) = "DD" THEN
		SELECT d.*, c.email, k.nom AS nom_cab, e.nom AS nom_exp FROM dem_devis d 
		INNER JOIN clients c ON d.societe=c.societe AND d.cd_cli=c.cd_cli 
		INNER JOIN p_cabinet k ON d.cabinet = k.code
		INNER JOIN p_experts e ON d.cabinet = e.code_cab AND d.expert = e.code_exp
		WHERE d.societe = pSTE AND d.no_id = @NO_ID;
    ELSEIF SUBSTR(pNoDossier, 1, 2) = "FA" THEN
		SELECT d.*, c.email, k.nom AS nom_cab, e.nom AS nom_exp FROM facture d 
		INNER JOIN clients c ON d.societe=c.societe AND d.cd_cli=c.cd_cli 
		INNER JOIN p_cabinet k ON d.cabinet = k.code
		INNER JOIN p_experts e ON d.cabinet = e.code_cab AND d.expert = e.code_exp
		WHERE d.societe = pSTE AND d.no_id = @NO_ID;
    ELSEIF SUBSTR(pNoDossier, 1, 2) = "DE" THEN
		SELECT d.*, c.email, k.nom AS nom_cab, e.nom AS nom_exp FROM devis d 
		INNER JOIN clients c ON d.societe=c.societe AND d.cd_cli=c.cd_cli 
		INNER JOIN p_cabinet k ON d.cabinet = k.code
		INNER JOIN p_experts e ON d.cabinet = e.code_cab AND d.expert = e.code_exp
		WHERE d.societe = pSTE AND d.no_id = @NO_ID;
	END IF;
END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spGET_ENTETE` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spGET_ENTETE`(
        IN pSTE CHAR(2),
        IN pTABLE VARCHAR(20),
        IN pID INTEGER(11)
    )
BEGIN

  SET @sql_text = CONCAT("SELECT * FROM ",pTABLE," WHERE societe='",pSTE,"' AND no_id=",pID);

  PREPARE sRequete FROM @sql_text ;
  EXECUTE sRequete ;
  DEALLOCATE PREPARE sRequete ;
  
END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spGET_ENTETE_byCriteria` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spGET_ENTETE_byCriteria`(
        IN pSTE CHAR(2),
        IN pTABLE VARCHAR(20),
        IN pCRITERE VARCHAR(15),
        IN pVALEUR1 VARCHAR(50),
        IN pVALEUR2 VARCHAR(50),
        IN pLIMITE tinyint
    )
BEGIN


SET @sql_text = CONCAT("SELECT * FROM ",pTABLE," WHERE societe='",pSTE,"' ");
  SET @sql_text = CONCAT(@sql_text,
      (SELECT CASE pCRITERE
        WHEN "PARCD_CLI" THEN CONCAT('AND cd_cli = ',pVALEUR1)
        WHEN "PARNOM" THEN CONCAT('AND nomcli LIKE "',pVALEUR1,'%" ORDER BY nomcli, date DESC')
        WHEN "PARCHANTIER" THEN CONCAT('AND c_nom LIKE "',pVALEUR1,'%" AND c_adr LIKE "',pVALEUR2,
                                            '" ORDER BY c_nom, c_adr, date DESC')
        WHEN "PARSINISTRE" THEN CONCAT('AND nosin LIKE "',pVALEUR1,'%" ORDER BY nosin')
        WHEN "PARPOLICE" THEN CONCAT('AND nopol LIKE "',pVALEUR1,'%" ORDER BY nopol')
        WHEN "PARDATE" THEN CONCAT('AND date >= "',pVALEUR1,'" ORDER BY date DESC, no_id DESC')
        WHEN "DERNIER" THEN CONCAT(' ORDER BY date DESC, no_id DESC')
        WHEN "PARNO" THEN CONCAT('AND no_id >= "',pVALEUR1,'" ORDER BY no_id')
        WHEN "PARDATEREL" THEN CONCAT('AND length(daterel) > 0 ORDER BY daterel, nomcli')
      END)
      );
  
  IF pLIMITE <> 0 THEN
     SET @sql_text = CONCAT(@sql_text," LIMIT 100");
  END IF;
  
--  insert into t_debug (proc,msg)
--  values ('get by cd_cli',CONCAT('sql_text=',@sql_text));

  PREPARE sRequete FROM @sql_text ;
  EXECUTE sRequete ;
  DEALLOCATE PREPARE sRequete ;

END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spGET_EXPERTS` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spGET_EXPERTS`(
        IN pCode_cab INT,
        IN pCode_exp INT
    )
BEGIN

	IF pCode_exp = 0 THEN
        -- lire tous les experts 
		SELECT * FROM P_EXPERTS WHERE code_cab = pCode_cab ORDER BY NOM;
	ELSEIF pCode_exp = -1 THEN
        -- lire tous les experts en activié 
		SELECT * FROM P_EXPERTS WHERE code_cab = pCode_cab AND EN_ACTIVITE <> 0 ORDER BY NOM;
	ELSE
		SELECT * FROM P_EXPERTS 
        WHERE code_cab = pCode_cab AND code_exp = pCode_exp;
	END IF;
  
END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spGET_FACTURES_byClient` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spGET_FACTURES_byClient`(
        IN pSTE CHAR(2),
        IN pCd_cli INTEGER
    )
BEGIN

      -- factures d'un client
	  SELECT * FROM FACTURE 
      WHERE societe=pSTE AND cd_cli=pCd_cli ORDER BY date;

END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spGET_FACTURE_ARegler` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spGET_FACTURE_ARegler`(
        IN pSTE CHAR(2),
        IN pClient VARCHAR(100),
        IN pAvoir smallint
    )
BEGIN

SET @numero = COALESCE(CAST(pClient AS UNSIGNED),0);
-- est-ce un numeric ?
IF @numero > 0 THEN
	-- oui, contient un point
	if INSTR(pClient, '.') > 0 then
		-- recherche par montant
		SET @montant = COALESCE(CAST(pClient AS decimal(11,2)),0);
		SELECT date,LPAD(no_id,6,'0') AS numero, nomcli, CONCAT(c_nom,'; ',c_adr,'; ',c_ville) AS chantier, COALESCE(totalttc,0) AS montant, TotalTTC - MtRegl as solde, cd_cli
		FROM facture WHERE societe=pSTE AND TotalTTC - MtRegl = @montant and cd_cli > 1
		ORDER BY no_id LIMIT 100;
	else
		-- recherche par numero de facture
		SELECT date,LPAD(no_id,6,'0') AS numero, nomcli, CONCAT(c_nom,'; ',c_adr,'; ',c_ville) AS chantier, COALESCE(totalttc,0) AS montant, TotalTTC - MtRegl as solde, cd_cli
		FROM facture WHERE societe=pSTE AND no_id >= @numero AND ABS(TotalTTC - MtRegl) > 0.05 and cd_cli > 1
		ORDER BY no_id LIMIT 100;
	end if;
ELSE
	-- recherche par nom client
	if pAvoir <> 0 then
		-- factures et avoirs
		SELECT date, LPAD(no_id,6,'0') AS numero, nomcli, CONCAT(c_nom,'; ',c_adr,'; ',c_ville) AS chantier, COALESCE(totalttc,0) AS montant, TotalTTC - MtRegl as solde, cd_cli
		FROM facture
		WHERE societe=pSTE AND c_nom LIKE CONCAT(pClient,"%") AND ABS(TotalTTC - MtRegl) > 0.05 and cd_cli > 1
		ORDER BY nomcli, date;
    else
		-- que les factures
		SELECT date, LPAD(no_id,6,'0') AS numero, nomcli, CONCAT(c_nom,'; ',c_adr,'; ',c_ville) AS chantier, COALESCE(totalttc,0) AS montant, TotalTTC - MtRegl as solde, cd_cli
		FROM facture
		WHERE societe=pSTE AND c_nom LIKE CONCAT(pClient,"%") AND TotalTTC > 0 AND ABS(TotalTTC - MtRegl) > 0.05 and cd_cli > 1
		ORDER BY nomcli, date;
    end if;
    
END IF;

END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spGET_FACTURE_byDates` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spGET_FACTURE_byDates`(
        IN pSTE CHAR(2),
        IN pDATE1 VARCHAR(10),
        IN pDATE2 VARCHAR(10)
    )
BEGIN

  SELECT * FROM facture INNER JOIN clients ON facture.societe=clients.societe AND facture.cd_cli = clients.cd_cli
	 WHERE facture.societe=pSTE AND facture.date>=pDATE1 AND facture.date<=pDATE2 and facture.cd_cli > 1
     ORDER BY no_id;
      
END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spGET_FACTURE_NonReglees` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spGET_FACTURE_NonReglees`(
        IN pSTE CHAR(2),
        IN pMode SMALLINT,
        IN pCd_cli INTEGER
    )
BEGIN

	IF pMode = 0 THEN
      -- toutes factures non soldees
	  SELECT * FROM FACTURE WHERE societe=pSTE AND TotalTTC - MtRegl > 0.05
		  ORDER BY nomcli, date;
	ELSEIF pMode = 7 THEN
      -- factures non soldees d'un client
	  SELECT * FROM FACTURE 
      WHERE societe=pSTE AND cd_cli=pCd_cli AND TotalTTC >= 0 AND TotalTTC - MtRegl > 0.05
		  ORDER BY nomcli, date;
	ELSE
      -- avoirs non soldes d'un client
	  SELECT * FROM FACTURE 
      WHERE societe=pSTE AND cd_cli=pCd_cli AND TotalTTC < 0 AND TotalTTC < MtRegl
		  ORDER BY nomcli, date;
	END IF;

END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spGET_FACTURE_REG` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spGET_FACTURE_REG`(
        IN pSTE CHAR(2),
        IN pNOFACT INTEGER(11),
        IN pMODEREG SMALLINT,
        IN pCOD_BNQ INTEGER(11)
    )
BEGIN

 SET @sql_text = CONCAT("SELECT * FROM FACTURE_REG WHERE societe='",pSTE,"' AND nofact=", pNOFACT);
  IF pMODEREG <> 0 THEN
      SET @sql_text = CONCAT(@sql_text," AND MODEREG=", pMODEREG);
  END IF;    
  IF pCOD_BNQ <> 0 THEN
      SET @sql_text = CONCAT(@sql_text," AND COD_BNQ=",pCOD_BNQ);
  END IF;    
    
  SET @sql_text = CONCAT(@sql_text," ORDER BY date");
  
  PREPARE sRequete FROM @sql_text ;
  EXECUTE sRequete ;
  DEALLOCATE PREPARE sRequete ;
  
END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spGET_FACTURE_REG_byBanque` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spGET_FACTURE_REG_byBanque`(
        IN pSTE CHAR(2),
        IN pCOD_BNQ INTEGER(11)
    )
BEGIN

  SELECT r.nofact, r.date, COALESCE(f.c_nom,"???") AS c_nom, r.montant, COALESCE(f.totalttc,0.0) AS totalttc
     FROM facture_reg r LEFT JOIN facture f ON r.societe=f.societe AND r.nofact=f.no_id
     WHERE r.societe=pSTE AND r.cod_bnq = pCOD_BNQ
     ORDER BY r.date DESC;
    
END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spGET_LETTRE` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spGET_LETTRE`(
        IN pREF VARCHAR(10)
    )
BEGIN

  IF pREF = "" THEN
		SELECT * FROM lettre ORDER BY ref;
  ELSE
		SELECT * FROM lettre WHERE ref=pREF;
  END IF;

END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spGET_LIGNES` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spGET_LIGNES`(
        IN pSTE CHAR(2),
        IN pTABLE VARCHAR(20),
        IN noID INTEGER(11),
        IN noLIG INTEGER(11)
    )
BEGIN

  SET @sql_text = CONCAT("SELECT * FROM ",pTABLE," WHERE societe='",pSTE,"' AND no_id=",noID);
  IF noLIG > 0 THEN
      SET @sql_text = CONCAT(@sql_text," AND nolig=",noLIG);
  END IF;
  
  SET @sql_text = CONCAT(@sql_text," ORDER BY nolig") ;

  PREPARE sRequete FROM @sql_text ;
  EXECUTE sRequete ;
  DEALLOCATE PREPARE sRequete ;
  
END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spGET_LIGNES_MULTIPLES` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spGET_LIGNES_MULTIPLES`(
        IN pDOSSIER int, 
        IN pDATEVI date,
        IN pHEUREVI time,
        IN pLISTE VARCHAR(20)
        )
BEGIN

	SELECT 'DD' as origine, dem_devis.nomcli,dem_lig.no_id,dem_lig.datevi,dem_lig.heurevi,dem_lig.liste from dem_lig
      LEFT JOIN dem_devis ON dem_lig.societe = dem_devis.societe AND dem_lig.no_id = dem_devis.no_id 
	  WHERE dem_lig.no_id<>pDOSSIER AND dem_lig.datevi=pDATEVI AND dem_lig.heurevi=pHEUREVI AND dem_lig.liste=pLISTE; 

END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spGET_LIV_BNQ` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spGET_LIV_BNQ`(
        IN pTYPE VARCHAR(10),
        IN pSTE CHAR(2),
        IN pCD_CLI INTEGER(11),
        IN pNumero INTEGER(11),
        IN pDATE1 VARCHAR(10),
        IN pDATE2 VARCHAR(10)
    )
BEGIN

	IF pTYPE = 'byMode' THEN
	  SELECT * FROM liv_bnq 
		 WHERE societe=pSTE AND cd_cli=pCD_CLI AND modereg=pNumero ORDER BY cod_bnq DESC;

	ELSEIF pTYPE = 'byDate' THEN
	  SELECT * FROM liv_bnq 
		 WHERE societe=pSTE AND date=pDATE1 AND modereg=1 ORDER BY jnl, cd_cli;

	ELSEIF pTYPE = 'byDates' THEN
	  SELECT liv_bnq.*, m.lib as lib_regl, j.compte, c.nocompte, c.nom FROM liv_bnq 
		 INNER JOIN p_modereg m ON liv_bnq.modereg = m.code
		 INNER JOIN clients c ON c.societe=pSTE AND c.cd_cli = liv_bnq.cd_cli
		 INNER JOIN p_journaux j ON liv_bnq.jnl = j.code
		 WHERE liv_bnq.societe=pSTE AND liv_bnq.date>=pDATE1 AND liv_bnq.date<=pDATE2 
		 ORDER BY liv_bnq.JNL, liv_bnq.MODEREG, liv_bnq.Date, c.nocompte;

	ELSEIF pTYPE = 'byCode' THEN
	  SELECT * FROM liv_bnq INNER JOIN p_modereg ON liv_bnq.modereg = p_modereg.code
		 WHERE societe=pSTE AND cod_bnq=pCD_CLI;

	ELSEIF pTYPE = 'byCD_CLI' THEN
	  SELECT * FROM liv_bnq INNER JOIN p_modereg ON liv_bnq.modereg = p_modereg.code
		 WHERE societe=pSTE AND cd_cli=pCD_CLI ORDER BY date DESC;

	ELSEIF pTYPE = 'byDISPO' THEN
		-- lire tous les versements dispos 
		SELECT * FROM liv_bnq INNER JOIN p_modereg ON liv_bnq.modereg = p_modereg.code
		WHERE societe=pSTE AND cd_cli=pCD_CLI AND montantregl > montantdebit ORDER BY date DESC;
	ELSE
		SELECT -1;
	END IF;    
	  
END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spGET_NEW_CLIENT` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spGET_NEW_CLIENT`(
        IN pSte CHAR(2)
    )
BEGIN

  SELECT cd_cli FROM clients
         WHERE societe=pSTE ORDER BY cd_cli DESC LIMIT 1;

  
END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spGET_NEW_NO_ID` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spGET_NEW_NO_ID`(
        IN pSte CHAR(2),
        IN pTable VARCHAR(20),
        IN pDebut INT,
        IN pFin INT
    )
BEGIN

  SET @sql_text = CONCAT("SELECT no_id FROM ",pTable,
           " WHERE societe='",pSTE,"' AND no_id BETWEEN ",pDebut," AND ", pFin,
           " ORDER BY no_id DESC LIMIT 1");


  
  

  PREPARE sRequete FROM @sql_text ;
  EXECUTE sRequete ;
  DEALLOCATE PREPARE sRequete ;
  
END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spGET_P_EDITCRYS` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spGET_P_EDITCRYS`(
		IN pType VARCHAR(10),
        IN pNom VARCHAR(30)
    )
BEGIN

  IF pType = "REPORT" THEN  
     SELECT * FROM p_editcrys WHERE crystal_rpt = pNom;
  ELSE
     SELECT * FROM p_editcrys WHERE rubrique = pNom order by libelle;
  END IF;

END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spGET_P_JOURNAUX` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spGET_P_JOURNAUX`(
        IN pCode CHAR(5)
    )
BEGIN

	IF pCode = "" THEN
		SELECT * FROM p_journaux ORDER BY Code;
	ELSE
		SELECT * FROM p_journaux WHERE code=pCode;
	END IF;
  
END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spGET_P_MODEREG` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spGET_P_MODEREG`(
        IN pCode SMALLINT
    )
BEGIN

  -- Lire qu'un mode REGUL
  IF pCode = 9 THEN
    SELECT * FROM p_modereg WHERE code = pCode;
  ELSEIF pCode = -1 THEN
    -- lire que les vrai mode de réglements
    SELECT * FROM p_modereg WHERE nocompte > 0 ORDER BY code;
  ELSE
    SELECT * FROM p_modereg ORDER BY code;
  END IF;
  
END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spGET_P_SOCIETE` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spGET_P_SOCIETE`(
        IN pSte CHAR(2)
    )
BEGIN

    SELECT * FROM p_societe WHERE societe=pSte;
  
END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spGET_P_TABLES` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spGET_P_TABLES`(
        IN pTable varchar(20)
    )
BEGIN

	IF pTable = "P_QUALITE" THEN
		SELECT * FROM p_qualite ORDER BY Code;
	ELSEIF pTable = "P_FAMILLE" THEN
		SELECT * FROM p_famille ORDER BY Code;
	ELSEIF pTable = "P_GROUPE" THEN
		SELECT * FROM p_groupe ORDER BY Code;
	ELSEIF pTable = "P_MODEREG" THEN
		SELECT * FROM p_modereg ORDER BY Code;
	ELSEIF pTable = "P_SOCIETE" THEN
		SELECT * FROM p_societe ORDER BY societe;
	ELSEIF pTable = "P_TARIFS" THEN
		SELECT * FROM p_tarifs ORDER BY code;
	ELSEIF pTable = "P_TYPE" THEN
		SELECT * FROM p_type ORDER BY Code;
	END IF;
  
END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spGET_P_TARIFS` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spGET_P_TARIFS`(
        IN pSociete CHAR(2),
        IN pCode INTEGER
    )
BEGIN

  IF pCode <> 0 THEN
     SELECT * FROM P_TARIFS WHERE societe = pSociete AND code = pCode;
  ELSE
     SELECT * FROM P_TARIFS ORDER BY code;
  END IF;
  
END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spGET_P_TVA_byCODE` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spGET_P_TVA_byCODE`(
        IN pCode INTEGER
    )
BEGIN

  IF pCode <> 0 THEN
     SELECT * FROM P_TVA WHERE code = pCode;
  ELSE
     SELECT * FROM P_TVA ORDER BY code;
  END IF;
  
END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spGET_T_IMP_RDV_JOUR` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spGET_T_IMP_RDV_JOUR`(
        IN pCode VARCHAR(10)
    )
BEGIN

  SELECT * FROM T_IMP_RDV_JOUR WHERE codeuser = pCode ORDER BY datevi, heurevi;
  
END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spGET_T_RELANCE` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spGET_T_RELANCE`(
        IN pCode VARCHAR(10)
        )
BEGIN

  SELECT * FROM T_RELANCE WHERE codeuser = pCode ORDER BY no_fact;

END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spGET_USERS` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spGET_USERS`(
        IN pCode CHAR
)
BEGIN

  -- Lire tout ?
  IF pCode = "ALL" THEN
     SELECT * FROM P_USERS ORDER BY cd_uti;
  -- que les personnels actifs
  ELSEIF pCode = "ACT" THEN
     SELECT * FROM P_USERS WHERE actif <> 0 ORDER BY cd_uti;
  ELSE
     SELECT * FROM P_USERS WHERE cd_uti = pCODE AND actif <> 0;
  END IF;  
END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spINS_DEVIS_FROM_DOSSIER` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spINS_DEVIS_FROM_DOSSIER`(
        IN pSTE CHAR(2),
        IN pFROM CHAR(2),
        IN pID INTEGER(11),
        IN pUSER VARCHAR(10)
    )
BEGIN

IF pFROM = "DD" THEN
  -- creer un devis a partir d'une DD
  SET @new_id = GetNewNoEntete(pSTE,"DEVIS");

  INSERT INTO devis 
	(societe,no_id,nofact,nochantier,no_dd,nocompte,cd_cli,nomcli,date,
     C_QUALITE,C_NOM,C_ADR,C_ADR2,C_CP,C_VILLE,C_TEL1,C_TEL2,C_TELP,C_FAX,C_OBS,C_ETAGE,C_CODE,C_EMAIL,
     CABINET,EXPERT,NOPOL,NOSIN,VREF,LIBCOMPL,TYPECLI,
     NOTARIF,CODETVA,TYPETRAV,GROUPE,REF_EXPERT,A_REVOIR,no_agrement,usermaj) 
  SELECT 
     societe,@new_id,0,pID,no_id,nocompte,cd_cli,nomcli,CURRENT_DATE,
     C_QUALITE,C_NOM,C_ADR,C_ADR2,C_CP,C_VILLE,C_TEL1,C_TEL2,C_TELP,C_FAX,C_OBS,C_ETAGE,C_CODE,C_EMAIL,
     CABINET,EXPERT,NOPOL,NOSIN,VREF,LIBCOMPL,TYPECLI,
     NOTARIF,CODETVA,TYPETRAV,GROUPE,REF_EXPERT,A_REVOIR,no_agrement,pUSER
     FROM dem_devis
     WHERE societe = pSTE AND no_id = pID;

	-- marquer la demande de devis comme AYANT UN DEVIS
    UPDATE DEM_DEVIS SET 
      nodevis = @new_id, 
      status = CONCAT('DE-',@new_id)
	WHERE societe = pSTE AND NO_ID = pID;
      
ELSEIF pFROM = "DE" THEN
  -- creer un devis a partir d'un autre devis
  SET @new_id = GetNewNoEntete(pSTE,"DEVIS");

  INSERT INTO devis 
	(societe,no_id,nofact,nochantier,no_dd,nocompte,cd_cli,nomcli,date,
     C_QUALITE,C_NOM,C_ADR,C_ADR2,C_CP,C_VILLE,C_TEL1,C_TEL2,C_TELP,C_FAX,C_OBS,C_ETAGE,C_CODE,C_EMAIL,
     CABINET,EXPERT,NOPOL,NOSIN,VREF,LIBCOMPL,TYPECLI,
     NOTARIF,CODETVA,TYPETRAV,GROUPE,REF_EXPERT,A_REVOIR,no_agrement,usermaj) 
  SELECT 
     societe,@new_id,0,nochantier,0,nocompte,cd_cli,nomcli,CURRENT_DATE,
     C_QUALITE,C_NOM,C_ADR,C_ADR2,C_CP,C_VILLE,C_TEL1,C_TEL2,C_TELP,C_FAX,C_OBS,C_ETAGE,C_CODE,C_EMAIL,
     CABINET,EXPERT,NOPOL,NOSIN,VREF,LIBCOMPL,TYPECLI,
     NOTARIF,CODETVA,TYPETRAV,GROUPE,REF_EXPERT,A_REVOIR,no_agrement,pUSER
     FROM devis
     WHERE societe = pSTE AND no_id = pID;

     CALL spINS_DEVIS_LIG_2DEVIS(pSTE,pID,@new_id,pUSER);
else
	SET @new_id = 0;

END IF;

  -- retourne l'id du dossier cree
  SELECT @new_id AS last_insert_id;

END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spINS_DEVIS_LIG_2DEVIS` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spINS_DEVIS_LIG_2DEVIS`(
        IN pSTE CHAR(2),
        IN noDepart INT,
        IN noDestin INT,
        IN codeUser varchar(20)
    )
BEGIN

  INSERT INTO devis_lig (societe,NO_ID,NOLIG,REF,LIB,QTE,PRIXHT,MTHT,GROUPE,DATEMAJ,USERMAJ) 
      SELECT societe,noDestin,nolig,ref,lib,qte,prixht,mtht,groupe,now(), codeUser 
          FROM devis_lig WHERE societe=pSTE AND no_id = noDepart ;
     
END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spINS_DEVIS_LIG_2FACTURE` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spINS_DEVIS_LIG_2FACTURE`(
        IN pSTE CHAR(2),
        IN noDepart INT,
        IN noDestin INT,
        IN codeUser varchar(20)
    )
BEGIN

  INSERT INTO facture_lig (societe,NO_ID,NOLIG,REF,LIB,QTE,PRIXHT,MTHT,GROUPE,DATEMAJ,USERMAJ) 
      SELECT societe,noDestin,nolig,ref,lib,qte,prixht,mtht,groupe,CURRENT_DATE, codeUser 
          FROM devis_lig WHERE societe=pSTE AND no_id = noDepart ;
     
END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spINS_DEVIS_LIG_2PROFORMA` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spINS_DEVIS_LIG_2PROFORMA`(
        IN pSTE CHAR(2),
        IN noDepart INT,
        IN noDestin INT,
        IN codeUser varchar(20)
    )
BEGIN

  INSERT INTO proforma_lig (societe,NO_ID,NOLIG,REF,LIB,QTE,PRIXHT,MTHT,GROUPE,DATEMAJ,USERMAJ) 
      SELECT societe,noDestin,nolig,ref,lib,qte,prixht,mtht,groupe,now(), codeUser 
          FROM devis_lig WHERE societe=pSTE AND no_id = noDepart ;
     
END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spINS_ENTETE` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spINS_ENTETE`(
        IN pSTE CHAR(2),
        IN pTABLE VARCHAR(20),
        IN pID INTEGER(11),
        IN pCD_CLI INTEGER(11),
        IN pUSER VARCHAR(10)
    )
BEGIN

  SET @sql_text = CONCAT("INSERT INTO ",pTABLE," (societe,NO_ID,CD_CLI,usermaj) 
      VALUES ('",pSTE,"',",pID,",",pCD_CLI,",'",pUSER,"')");

PREPARE sRequete FROM @sql_text ;
  EXECUTE sRequete ;
  DEALLOCATE PREPARE sRequete ;
  
END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spINS_FACTURE_FROM_DOSSIER` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spINS_FACTURE_FROM_DOSSIER`(
        IN pSTE CHAR(2),
        IN pFROM CHAR(2),
        IN pID INTEGER(11),
        IN pUSER VARCHAR(10)
    )
BEGIN

-- obtenir un nouveau numero de facture
SET @new_id = GetNewNoEntete(pSTE,"FACTURE");
  
IF pFROM = "DD" THEN
  -- creer une facture a partir d'un DD
  INSERT INTO facture 
	(societe,no_id,nodevis,nochantier,no_dd,cd_cli,nomcli,date,
     C_QUALITE,C_NOM,C_ADR,C_ADR2,C_CP,C_VILLE,C_TEL1,C_TEL2,C_TELP,C_FAX,C_OBS,C_ETAGE,C_CODE,C_EMAIL,
     CABINET,EXPERT,NOPOL,NOSIN,VREF,LIBCOMPL,TYPECLI,
     NOTARIF,CODETVA,TYPETRAV,GROUPE,REF_EXPERT,A_REVOIR,usermaj) 
  SELECT 
     societe,@new_id,nodevis,pID,no_dd,cd_cli,nomcli,CURRENT_DATE,
     C_QUALITE,C_NOM,C_ADR,C_ADR2,C_CP,C_VILLE,C_TEL1,C_TEL2,C_TELP,C_FAX,C_OBS,C_ETAGE,C_CODE,C_EMAIL,
     CABINET,EXPERT,NOPOL,NOSIN,VREF,LIBCOMPL,TYPECLI,
     NOTARIF,CODETVA,TYPETRAV,GROUPE,REF_EXPERT,A_REVOIR,pUSER
     FROM dem_devis
     WHERE societe = pSTE AND no_id = pID;

ELSE
  -- creer une facture a partir d'un DE
  INSERT INTO facture 
	(societe,no_id,nodevis,nochantier,no_dd,cd_cli,nomcli,date,
     C_QUALITE,C_NOM,C_ADR,C_ADR2,C_CP,C_VILLE,C_TEL1,C_TEL2,C_TELP,C_FAX,C_OBS,C_ETAGE,C_CODE,C_EMAIL,
     CABINET,EXPERT,NOPOL,NOSIN,VREF,LIBCOMPL,TYPECLI,
     NOTARIF,CODETVA,TYPETRAV,GROUPE,REF_EXPERT,A_REVOIR,usermaj) 
  SELECT 
     societe,@new_id,pID,nochantier,no_dd,cd_cli,nomcli,CURRENT_DATE,
     C_QUALITE,C_NOM,C_ADR,C_ADR2,C_CP,C_VILLE,C_TEL1,C_TEL2,C_TELP,C_FAX,C_OBS,C_ETAGE,C_CODE,C_EMAIL,
     CABINET,EXPERT,NOPOL,NOSIN,VREF,LIBCOMPL,TYPECLI,
     NOTARIF,CODETVA,TYPETRAV,GROUPE,REF_EXPERT,A_REVOIR,pUSER
     FROM devis
     WHERE societe = pSTE AND no_id = pID;

	  -- copie des lignes du devis correspondant
	  CALL spINS_DEVIS_LIG_2FACTURE(pSTE, pID, @new_id,pUSER);

	-- marquer le devis comme AYANT UNE FACTURE
    UPDATE DEVIS SET 
      nofact = @new_id, 
      status = CONCAT('FA-',@new_id)
	WHERE societe = pSTE AND NO_ID = pID;

END IF;
	-- marquer la demande de devis comme AYANT UNE FACTURE
    UPDATE DEM_DEVIS SET 
      nofact = @new_id, 
      status = CONCAT('FA-',@new_id)
	WHERE societe = pSTE AND NO_ID = pID;

  -- retourne l'id du dossier cree
  SELECT @new_id AS last_insert_id;

END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spINS_FACTURE_REG` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spINS_FACTURE_REG`(
        IN pSTE CHAR(2),
        IN pNOFACT VARCHAR(20),
        IN pMODEREG SMALLINT,
        IN pCOD_BNQ INTEGER(11),
        IN pCD_CLI INTEGER(11),
        IN pETS VARCHAR(5),
        IN pJST VARCHAR(20),
        IN pDATE DATE,
        IN pMONTANT DECIMAL(11,2)
    )
BEGIN

    INSERT INTO FACTURE_REG (SOCIETE,NOFACT,MODEREG,COD_BNQ,CD_CLI,ETS,JST,Date,MONTANT) 
    VALUES (pSTE,pNOFACT,pMODEREG,pCOD_BNQ,pCD_CLI,pETS,pJST,pDATE,pMONTANT);

END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spINS_LIGNE` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spINS_LIGNE`(
        IN pSTE CHAR(2),
        IN pTABLE VARCHAR(20),
        IN noID INTEGER(11),
        IN noLIG INTEGER(11)
    )
BEGIN

  SET @sql_text = CONCAT("INSERT INTO ",pTABLE," (societe,no_id,nolig) VALUES ('",pSTE,"',",noID,",",noLIG,")");

  PREPARE sRequete FROM @sql_text ;
  EXECUTE sRequete ;
  DEALLOCATE PREPARE sRequete ;
  
END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spINS_LIGNES_DEVIS2DEVIS` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spINS_LIGNES_DEVIS2DEVIS`(
        IN pSTE CHAR(2),
        IN noDepart INT,
        IN noDestin INT,
        IN codeUser varchar(20)
    )
BEGIN

  INSERT INTO devis_lig (societe,NO_ID,NOLIG,REF,LIB,QTE,PRIXHT,MTHT,GROUPE,DATEMAJ,USERMAJ) 
      SELECT societe,noDestin,nolig,ref,lib,qte,prixht,mtht,groupe,now(), codeUser 
          FROM devis_lig WHERE societe=pSTE AND no_id = noDepart ;
     
END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spINS_LIGNES_TX` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spINS_LIGNES_TX`(
        IN pRubrique VARCHAR(10),
        IN pSTE CHAR(2),
        IN noDossier INTEGER(11),
        IN noDepart INTEGER(11),
        IN nbLignes INTEGER(11),
        IN pUSER VARCHAR(10)
    )
BEGIN

  IF pRubrique = "DEVIS" THEN  
		UPDATE devis_lig SET nolig = nolig + nbLignes
		WHERE societe=pSTE AND no_id=noDossier AND nolig >= noDepart ORDER BY nolig DESC;

		SET @x = 0;
		REPEAT 
			SET @l = noDepart + @x;
			INSERT INTO devis_lig (societe,no_id, nolig, ref, datemaj, usermaj) 
			 VALUES(pSTE,noDossier, @l, 'TX', NOW(), pUSER);
			SET @x = @x + 1;
			UNTIL @x >= nbLignes 
		END REPEAT;

  ELSEIF pRubrique = "FACTURE" THEN
		UPDATE facture_lig SET nolig = nolig + nbLignes
		WHERE societe=pSTE AND no_id=noDossier AND nolig >= noDepart ORDER BY nolig DESC;

		SET @x = 0;
		REPEAT 
			SET @l = noDepart + @x;
			INSERT INTO facture_lig (societe,no_id, nolig, ref, datemaj, usermaj) 
			VALUES(pSTE,noDossier, @l, 'TX', NOW(), pUSER);
			SET @x = @x + 1;
		UNTIL @x >= nbLignes 
		END REPEAT;

  ELSE
     UPDATE proforma_lig SET nolig = nolig + nbLignes
         WHERE societe=pSTE AND no_id=noDossier AND nolig >= noDepart ORDER BY nolig DESC;

     SET @x = 0;
     REPEAT 
       SET @l = noDepart + @x;
       INSERT INTO proforma_lig (societe,no_id, nolig, ref, datemaj, usermaj) 
         VALUES(pSTE,noDossier, @l, 'TX', NOW(), pUSER);
       SET @x = @x + 1;
     UNTIL @x >= nbLignes 
     END REPEAT;
  END IF;
     
END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spINS_PROFORMA` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spINS_PROFORMA`(
        IN pSTE CHAR(2),
        IN pNO_ID INTEGER(11),
        IN pNOORDRE INTEGER(11),
        IN pNO_DD INTEGER(11)
        )
BEGIN

  INSERT INTO proforma (societe,NO_ID,NOORDRE,NO_DD) VALUES 
     (pSTE,pNO_ID,pNOORDRE,NO_DD);
  
END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spINS_PROFORMA_FROM_DOSSIER` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spINS_PROFORMA_FROM_DOSSIER`(
        IN pSTE CHAR(2),
        IN pFROM CHAR(2),
        IN pID INTEGER(11),
        IN pUSER VARCHAR(10)
    )
BEGIN

IF pFROM = "DD" THEN
  -- creer un PF a partir d'un DD
  SET @new_id = GetNewNoEntete(pSTE,"PROFORMA");

  INSERT INTO proforma 
	(societe,no_id,nofact,nochantier,no_dd,nocompte,cd_cli,nomcli,date,
     C_QUALITE,C_NOM,C_ADR,C_ADR2,C_CP,C_VILLE,C_TEL1,C_TEL2,C_TELP,C_FAX,C_OBS,C_ETAGE,C_CODE,C_EMAIL,
     CABINET,EXPERT,NOPOL,NOSIN,VREF,LIBCOMPL,TYPECLI,
     NOTARIF,CODETVA,TYPETRAV,GROUPE,REF_EXPERT,A_REVOIR,no_agrement,usermaj) 
  SELECT 
     societe,@new_id,0,pID,no_dd,nocompte,cd_cli,nomcli,CURRENT_DATE,
     C_QUALITE,C_NOM,C_ADR,C_ADR2,C_CP,C_VILLE,C_TEL1,C_TEL2,C_TELP,C_FAX,C_OBS,C_ETAGE,C_CODE,C_EMAIL,
     CABINET,EXPERT,NOPOL,NOSIN,VREF,LIBCOMPL,TYPECLI,
     NOTARIF,CODETVA,TYPETRAV,GROUPE,REF_EXPERT,A_REVOIR,no_agrement,pUSER
     FROM dem_devis WHERE societe = pSTE AND no_id = pID;
    
ELSEIF pFROM = "DE" THEN
	-- creer un PF a partir d'un DEVIS
	SET @new_id = GetNewNoEntete(pSTE,"PROFORMA");

	INSERT INTO proforma 
		(societe,no_id,nofact,nochantier,no_dd,nocompte,cd_cli,nomcli,date,
		 C_QUALITE,C_NOM,C_ADR,C_ADR2,C_CP,C_VILLE,C_TEL1,C_TEL2,C_TELP,C_FAX,C_OBS,C_ETAGE,C_CODE,C_EMAIL,
		 CABINET,EXPERT,NOPOL,NOSIN,VREF,LIBCOMPL,TYPECLI,
		 NOTARIF,CODETVA,TYPETRAV,GROUPE,REF_EXPERT,A_REVOIR,no_agrement,usermaj) 
	SELECT 
		 societe,@new_id,0,nochantier,no_dd,nocompte,cd_cli,nomcli,CURRENT_DATE,
		 C_QUALITE,C_NOM,C_ADR,C_ADR2,C_CP,C_VILLE,C_TEL1,C_TEL2,C_TELP,C_FAX,C_OBS,C_ETAGE,C_CODE,C_EMAIL,
		 CABINET,EXPERT,NOPOL,NOSIN,VREF,LIBCOMPL,TYPECLI,
		 NOTARIF,CODETVA,TYPETRAV,GROUPE,REF_EXPERT,A_REVOIR,no_agrement,pUSER
		 FROM devis
		 WHERE societe = pSTE AND no_id = pID;  
	CALL spINS_DEVIS_LIG_2PROFORMA(pSTE,pID,@new_id,pUSER);
  
ELSE
	SET @new_id = 0;
    
END IF;

  -- retourne l'id du dossier cree
  SELECT @new_id AS last_insert_id;

END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spINS_T_ANALYSE_DEVIS` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spINS_T_ANALYSE_DEVIS`(
        IN pSTE VARCHAR(10),
        IN pDATEDEB DATE,
        IN pDATEFIN DATE,
        IN pUSER VARCHAR(10)
    )
BEGIN
  DECLARE sEOF INT;
  DECLARE nTotalDossiers INT DEFAULT 0;
  DECLARE nDossiersFactures INT DEFAULT 0;
  DECLARE nTotalMontant DECIMAL(11,2);
  DECLARE nMontantFactures DECIMAL(11,2);
  DECLARE nMontantDevis DECIMAL(11,2);
  DECLARE nDelai INT DEFAULT 0;
  DECLARE nDelaimax INT DEFAULT 0;
  DECLARE nDelaimin INT DEFAULT 3650;
  DECLARE nDelaimoy DECIMAL(11,2) DEFAULT 0;

  DECLARE lno_id INT DEFAULT 0;
  DECLARE lnofact INT DEFAULT 0;
  DECLARE lnodevis INT DEFAULT 0;
  DECLARE ldate DATE;
  DECLARE ltotalht DECIMAL(11,2);
  
  DECLARE cDevis CURSOR FOR SELECT no_id,nofact,date,totalht FROM devis
     WHERE societe = pSTE AND date >= pDATEDEB AND date <= pDATEFIN;
               
  DECLARE cFacture CURSOR FOR SELECT no_id,nodevis,date,totalht FROM facture
     WHERE societe = pSTE AND date >= pDATEDEB AND date <= pDATEFIN;
               
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET sEOF = 1;
  
  SET sEOF = 0;
    
  OPEN cDevis;

-- Balayge des DEVIS
lignes_loop : 
  WHILE sEOF = 0 DO
    
    FETCH cDevis INTO lno_id, lnofact, ldate, ltotalht;
    IF sEOF = 1 THEN
       LEAVE lignes_loop;
    END IF;
    
    SET nTotalDossiers = nTotalDossiers + 1;
    SET nTotalMontant = nTotalMontant + ltotalht;
    IF lnofact > 0 THEN
       -- cumuler la facture correspondante
       SET nDossiersFactures = nDossiersFactures + 1;
       SET nMontantDevis = nMontantFactures + ltotalht;
       SET nMontantFactures = nMontantFactures + (select COALESCE(totalht,0) FROM facture WHERE societe = pSTE AND no_id = lnofact);
       SET nDelai = DATEDIFF(ldate, (select date FROM facture WHERE societe = pSTE AND no_id = lnofact));
       IF nDelai < 1 THEN
          SET nDelai = 1;
	   END IF;
       IF nDelai > nDelaimax Then
          SET nDelaimax = nDelai;
	   END IF;
       IF nDelai < nDelaimin Then
          SET nDelaimin = nDelai;
	   END IF;
       SET nDelaimoy = (nDelaimoy + nDelai) / 2;
    END IF;
               
  END WHILE lignes_loop;
  CLOSE cDevis;
  
  INSERT INTO T_ANALYSE_DEVIS (comment, nombre, pourcent, montant, codeUser) VALUES(
         'Total Devis :', nTotalDossiers, 100, nTotalMontant, pUSER);
   
  INSERT INTO T_ANALYSE_DEVIS (comment, nombre, pourcent, montant, montantFacture, codeUser) VALUES (
         'Total Devis facturés :', nDossiersFactures, ROUND(nDossiersFactures * 100 / nTotalDossiers),
		 nMontantDevis, nMontantFactures, pUSER);
   
  INSERT INTO T_ANALYSE_DEVIS (comment, nombre, pourcent, montant, montantFacture, codeUser) VALUES (
         'Total Devis non facturés :', nTotalDossiers - nDossiersFactures, 
         ROUND((nTotalDossiers-nDossiersFactures)*100 / nTotalDossiers), nTotalMontant-nMontantDevis, 0, pUSER);
   
  INSERT INTO T_ANALYSE_DEVIS (comment, nombre, pourcent, montant, montantFacture, codeUser) VALUES (
         '----------------------', 0, 0, 0, 0, pUSER);
   
  INSERT INTO T_ANALYSE_DEVIS (comment, nombre, pourcent, montant, montantFacture, codeUser) VALUES (
         'Délai facture maximum :', nDelaimax, 0, 0, 0, pUSER);
   
  INSERT INTO T_ANALYSE_DEVIS (comment, nombre, pourcent, montant, montantFacture, codeUser) VALUES (
         'Délai facture minimum :', nDelaimin, 0, 0, 0, pUSER);
   
  INSERT INTO T_ANALYSE_DEVIS (comment, nombre, pourcent, montant, montantFacture, codeUser) VALUES (
         'Délai facture moyen :', ROUND(nDelaimoy), 0, 0, 0, pUSER);
   
  INSERT INTO T_ANALYSE_DEVIS (comment, nombre, pourcent, montant, montantFacture, codeUser) VALUES (
         '----------------------', 0, 0, 0, 0, pUSER);

  SET sEOF = 0;
    
  OPEN cFacture;

SET nTotalDossiers = 0;
SET nDossiersFactures = 0;
SET nTotalMontant = 0;
SET nMontantFactures = 0;

-- Balayge des FACTURES
lignes_loop : 
  WHILE sEOF = 0 DO
    
    FETCH cFacture INTO lno_id, lnodevis, ldate, ltotalht;
    IF sEOF = 1 THEN
       LEAVE lignes_loop;
    END IF;
    
    SET nTotalDossiers = nTotalDossiers + 1;
    SET nTotalMontant = nTotalMontant + ltotalht;
    IF lnodevis > 0 THEN
       -- cumuler la facture correspondante
       SET nDossiersFactures = nDossiersFactures + 1;
       SET nMontantFactures = nMontantFactures + ltotalht;
    END IF;


  END WHILE lignes_loop;
  CLOSE cFacture;
 
 INSERT INTO T_ANALYSE_DEVIS (comment, nombre, pourcent, montant, codeUser) VALUES(
         'Total Factures :', nTotalDossiers, 100, nTotalMontant, pUSER);
  INSERT INTO T_ANALYSE_DEVIS (comment, nombre, pourcent, montant, montantFacture, codeUser) VALUES (
         'Total Factures avec Devis :', nDossiersFactures, ROUND(nDossiersFactures * 100 / nTotalDossiers),
		 nMontantFactures, 0, pUSER);
  INSERT INTO T_ANALYSE_DEVIS (comment, nombre, pourcent, montant, montantFacture, codeUser) VALUES (
         'Total Factures sans Devis :', nTotalDossiers - nDossiersFactures, 
         ROUND((nTotalDossiers-nDossiersFactures)*100 / nTotalDossiers), nTotalMontant-nMontantDevis, 0, pUSER);
  INSERT INTO T_ANALYSE_DEVIS (comment, nombre, pourcent, montant, montantFacture, codeUser) VALUES (
         '----------------------', nTotalDossiers - nDossiersFactures, Round((nTotalDossiers - nDossiersFactures) * 100 / nTotalDossiers),
         nTotalMontant - nMontantFactures, 0, pUSER);
  INSERT INTO T_ANALYSE_DEVIS (comment, nombre, pourcent, montant, montantFacture, codeUser) VALUES (
         '----------------------', 0, 0, 0, 0, pUSER);
     
END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spINS_T_COMPTE_CLIENTS` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spINS_T_COMPTE_CLIENTS`(
        IN pSTE VARCHAR(10),
        IN pDATEDEB DATE,
        IN pDATEFIN DATE,
        IN pUSER VARCHAR(10)
    )
BEGIN
  
  -- Effacer les anciennes ecritures 
  DELETE FROM t_ecritures WHERE user = pUSER;
  
  -- Insert les factures
  INSERT INTO t_ecritures (user, societe, compte, cd_cli, journal, date, libelle, codetva, montantTTC, montantHT, montantTVA) 
	 SELECT pUSER, pSTE, clients.nocompte, facture.cd_cli, 'VE', date, 
		CONCAT(LEFT(c_nom, 13), " ", LPAD(no_id,6,'0')), CONCAT("T", codetva),
        totalttc, totalht, totaltva 
     FROM facture INNER JOIN clients ON facture.societe = clients.societe AND facture.cd_cli = clients.cd_cli
     WHERE facture.societe=pSTE AND date >= pDATEDEB AND date <= pDATEFIN AND totalttc <> 0;
       
      
  -- Insert les versements
  INSERT INTO t_ecritures (user, societe, compte, cd_cli, journal, date, libelle, codetva, montantTTC) 
     -- versement du client
	SELECT pUSER, pSTE, clients.nocompte, liv_bnq.cd_cli, jnl, date, 
	CONCAT(lib,' ', ets,' ',jst), CONCAT("T", codetva), montantregl FROM liv_bnq 
    INNER JOIN p_modereg ON liv_bnq.modereg = p_modereg.code
	INNER JOIN clients ON liv_bnq.societe = clients.societe AND liv_bnq.cd_cli = clients.cd_cli
	WHERE liv_bnq.societe=pSTE AND date >= pDATEDEB AND date <= pDATEFIN AND montantregl <> 0;


     
END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spINS_T_IMP_RDV_JOUR` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spINS_T_IMP_RDV_JOUR`(
        IN pSTE VARCHAR(10),
        IN pDATEDEB DATE,
        IN pDATEFIN DATE,
        IN pLISTE VARCHAR(10),
        IN USER VARCHAR(10)
    )
BEGIN

  -- générer les RDV des DEMANDES de DEVIS 
  INSERT INTO T_IMP_RDV_JOUR (codeuser,DATEVI,HEUREVI,societe,no_id,comment,liste,rubrique,nom,adr,tel,tel_bur,tel_gsm) 
    SELECT USER,l.DATEVI,l.HEUREVI,l.societe,l.no_id,l.comment,l.liste,"DEM_DEVIS",
           LEFT(CONCAT(e.c_qualite,' ',e.c_nom,' (',e.nomcli,')'), 40),
		   LEFT(CONCAT(e.c_adr,' ',e.C_ADR2,' ',e.C_VILLE,' ',e.C_CP), 100),
           e.c_tel1, e.c_tel2, e.c_telp
    FROM dem_lig l INNER JOIN dem_devis e ON l.societe=e.societe AND l.no_id=e.no_id 
    WHERE l.societe=pSTE AND l.datevi >= pDATEDEB AND l.datevi <= pDATEFIN AND l.liste=pLISTE
    ORDER BY l.DATEVI, l.heurevi;


END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spINS_T_IMP_RDV_JOUR_AXA` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spINS_T_IMP_RDV_JOUR_AXA`(
        IN pSTE VARCHAR(10),
        IN pDATEDEB DATE,
        IN pDATEFIN DATE,
        IN pLISTE VARCHAR(10),
        IN USER VARCHAR(10)
    )
BEGIN

IF pLISTE = "N" THEN
  -- générer les RDV des DEMANDES de DEVIS 
  INSERT INTO T_IMP_RDV_JOUR (codeuser,DATEVI,HEUREVI,societe,no_id,comment,liste,rubrique,nom,adr,tel,tel_bur,tel_gsm,client,no_pol,no_sin) 
    SELECT USER,l.DATEVI,l.HEUREVI,l.societe,l.no_id,l.comment,l.liste,"DEM_DEVIS",
           LEFT(CONCAT(e.c_qualite,' ',e.c_nom), 40),
		   LEFT(CONCAT(e.c_adr,' ',e.C_ADR2,' ',e.C_VILLE,' ',e.C_CP), 100),
           e.c_tel1, e.c_tel2, e.c_telp, e.nomcli, e.nopol, e.nosin
    FROM ordres_lig l INNER JOIN ordres e ON l.societe=e.societe AND l.no_id=e.no_id 
    WHERE l.societe=pSTE AND l.datevi >= pDATEDEB AND l.datevi <= pDATEFIN AND l.liste=pLISTE
    ORDER BY l.DATEVI, l.heurevi;
ELSE
  -- générer les RDV des DEMANDES de DEVIS 
  INSERT INTO T_IMP_RDV_JOUR (codeuser,DATEVI,HEUREVI,societe,no_id,comment,liste,rubrique,nom,adr,tel,tel_bur,tel_gsm,client,no_pol,no_sin) 
    SELECT USER,l.DATEVI,l.HEUREVI,l.societe,l.no_id,l.comment,l.liste,"DEM_DEVIS",
           LEFT(CONCAT(e.c_qualite,' ',e.c_nom), 40),
		   LEFT(CONCAT(e.c_adr,' ',e.C_ADR2,' ',e.C_VILLE,' ',e.C_CP), 100),
           e.c_tel1, e.c_tel2, e.c_telp, e.nomcli, e.nopol, e.nosin
    FROM dem_lig l INNER JOIN dem_devis e ON l.societe=e.societe AND l.no_id=e.no_id 
    WHERE l.societe=pSTE AND l.datevi >= pDATEDEB AND l.datevi <= pDATEFIN AND l.liste <> 'N'
    ORDER BY l.DATEVI, l.heurevi;
END IF;

END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spINS_T_LETTRE` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spINS_T_LETTRE`(
        IN pUSER VARCHAR(10),
        IN pSTE CHAR(2),
        IN pREF VARCHAR(10),
        IN pDATE VARCHAR(10),
        IN pDOSSIER INT
    )
BEGIN

  INSERT INTO t_lettre (codeuser,ref,libelle,corps,type,no_id,typecli,corresp1,corresp2,corresp3,corresp4) 
     SELECT pUSER, ref, pDATE, corps, pSTE, pDOSSIER,"S",
            (SELECT corresp1 FROM p_societe where societe = pSTE),
            (SELECT corresp2 FROM p_societe where societe = pSTE),
            (SELECT corresp3 FROM p_societe where societe = pSTE),
            (SELECT corresp4 FROM p_societe where societe = pSTE)
     FROM LETTRE WHERE ref=pREF;

     -- Chantier ?
     SET @chantier = (SELECT COALESCE(c_adr,'') FROM dem_devis WHERE societe = pSTE AND no_id = pDOSSIER);
     SET @cd_cli = (SELECT cd_cli FROM dem_devis WHERE societe = pSTE AND no_id = pDOSSIER);
     -- destinataire = chantier
	SET @qualite = (SELECT c_qualite FROM dem_devis WHERE societe = pSTE AND no_id = pDOSSIER);
	SET @nom = (SELECT c_nom FROM dem_devis WHERE societe = pSTE AND no_id = pDOSSIER);
	SET @adr = (SELECT c_adr FROM dem_devis WHERE societe = pSTE AND no_id = pDOSSIER);
	SET @adr2 = (SELECT c_adr2 FROM dem_devis WHERE societe = pSTE AND no_id = pDOSSIER);
	SET @cp = (SELECT c_cp FROM dem_devis WHERE societe = pSTE AND no_id = pDOSSIER);
	SET @ville = (SELECT c_ville FROM dem_devis WHERE societe = pSTE AND no_id = pDOSSIER);
	SET @c_nom = CONCAT((SELECT qualite FROM clients where societe = pSTE AND cd_cli = @cd_cli), " ",
					(SELECT nom FROM clients where societe = pSTE AND cd_cli = @cd_cli));
	SET @c_adr = CONCAT((SELECT adresse FROM clients where societe = pSTE AND cd_cli = @cd_cli), " ",
					(SELECT adresse2 FROM clients where societe = pSTE AND cd_cli = @cd_cli));
	SET @c_ville = CONCAT((SELECT cp FROM clients where societe = pSTE AND cd_cli = @cd_cli), " ",
					(SELECT ville FROM clients where societe = pSTE AND cd_cli = @cd_cli));

     UPDATE t_lettre SET
       qualite = @qualite, nom = @nom, adr = @adr, adr2 = @adr2, cp = @cp, ville = @ville,
       c_nom = @c_nom, c_adr = @c_adr, c_ville = @c_ville, 
       vref = (SELECT vref FROM dem_devis where societe = pSTE AND no_id = pDOSSIER),
       nosin = (SELECT nosin FROM dem_devis where societe = pSTE AND no_id = pDOSSIER),
       nopol = (SELECT nopol FROM dem_devis where societe = pSTE AND no_id = pDOSSIER)
     WHERE codeuser = pUSER;
     SET @dateheure = CONCAT((SELECT DATE_FORMAT(datevi,"%d/%m/%Y") FROM dem_devis where societe = pSTE AND no_id = pDOSSIER),
                              " à " ,
                             (SELECT  LEFT(heurevi,5) FROM dem_devis where societe = pSTE AND no_id = pDOSSIER));
     SET @comment = (SELECT com_vi FROM dem_devis where societe = pSTE AND no_id = pDOSSIER);

-- si lettre de confirmation, inclure Date de RDV
  IF pRef = "CONFIRM" THEN
     UPDATE t_lettre SET
       corps = REPLACE(corps, "JJMMAA", COALESCE(@dateheure,'<date vide>'))
     WHERE codeuser = pUSER;
     UPDATE t_lettre SET
       corps = REPLACE(corps, "__COMMENTAIRE__", COALESCE(@comment,'<commentaire vide>'))
     WHERE codeuser = pUSER;
  END IF;

END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spINS_T_RELANCE` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spINS_T_RELANCE`(
        IN pCode VARCHAR(10),
        IN pSte VARCHAR(10),
        IN pTypeClient CHAR(2),
        IN pDateEcheance DATE
        )
BEGIN

  -- vider la table temporaire T_RELANCE
  DELETE FROM T_RELANCE WHERE codeuser = pCode;

  -- remplir T_RELANCE a prtir de FACTURES
  IF pTypeClient = "P" THEN
     INSERT INTO T_RELANCE (CODEUSER,NOM,NO_FACT,date,cd_cli,adr1,adr2,cp,ville,nopol,nosin,
                        chantier,mt_aregler,mt_facture,mt_regle,daterel,daterel2,type,niveau) 
       SELECT pCode,CONCAT(c.qualite," ",c.nom),f.no_id,f.date,f.cd_cli,c.adresse,c.adresse2,c.cp,c.ville,f.nopol,f.nosin,
            LEFT(CONCAT(f.c_nom, " ",f.c_adr," ",f.c_adr2," ",f.c_cp," ",f.c_ville),100),
			f.totalttc-f.mtregl,f.totalttc,f.mtregl,f.daterel,f.daterel2,pTypeClient,
            IF(pTypeClient<>"P",'1',if(ISNULL(f.daterel),'1','2'))
       FROM facture f INNER JOIN clients c ON f.societe = c.societe AND f.cd_cli = c.cd_cli
       WHERE f.societe=pSte AND f.totalttc > mtregl + 0.05 AND echeance <= pDateEcheance AND f.typecli = pTypeClient;
  ELSE
     INSERT INTO T_RELANCE (CODEUSER,NOM,NO_FACT,date,cd_cli,adr1,adr2,cp,ville,nopol,nosin,
                        chantier,mt_aregler,mt_facture,mt_regle,daterel,daterel2,type,niveau) 
       SELECT pCode,CONCAT(c.qualite," ",c.nom),f.no_id,f.date,f.cd_cli,c.adresse,c.adresse2,c.cp,c.ville,f.nopol,f.nosin,
            LEFT(CONCAT(f.c_nom, " ",f.c_adr," ",f.c_adr2," ",f.c_cp," ",f.c_ville),100),
			f.totalttc-f.mtregl,f.totalttc,f.mtregl,f.daterel,f.daterel2,pTypeClient,
            IF(pTypeClient<>"P",'1',if(ISNULL(f.daterel),'1','2'))
       FROM facture f INNER JOIN clients c ON f.societe = c.societe AND f.cd_cli = c.cd_cli
       WHERE f.societe=pSte AND f.totalttc > mtregl + 0.05 AND echeance <= pDateEcheance AND f.typecli <> "P";
  END IF;

END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spINS_USERS` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spINS_USERS`(
        IN pcd_uti CHAR(3)
    )
BEGIN
  
  INSERT p_users (cd_uti) VALUE (pcd_uti);
  
END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spUpd_ARTICLE` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spUpd_ARTICLE`(
        IN pREF VARCHAR(6),
        IN pOldREF VARCHAR(6),
        IN pFAM CHAR(3),
        IN pREF_CLI VARCHAR(45),
        IN pLIBCOMPL1 VARCHAR(45),
        IN pLIBCOMPL2 VARCHAR(45),
        IN pLIBCOMPL3 VARCHAR(45),
        IN pLIBCOMPL4 VARCHAR(45),
        IN pUNITE CHAR(5),
        IN pPRIXHT1 DECIMAL(9,2),
        IN pPRIXHT2 DECIMAL(9,2),
        IN pPRIXHT3 DECIMAL(9,2),
        IN pPRIXHT4 DECIMAL(9,2),
        IN pPRIXHT5 DECIMAL(9,2),
        IN pPRIXHT6 DECIMAL(9,2),
        IN pPRIXHT7 DECIMAL(9,2),
        IN pPRIXHT8 DECIMAL(9,2),
        IN pPRIXHT9 DECIMAL(9,2),
        IN pPRIXHT10 DECIMAL(9,2),
        IN pREF_CLI1 VARCHAR(45),
        IN pREF_CLI2 VARCHAR(45),
        IN pREF_CLI3 VARCHAR(45),
        IN pREF_CLI4 VARCHAR(45),
        IN pREF_CLI5 VARCHAR(45),
        IN pREF_CLI6 VARCHAR(45),
        IN pREF_CLI7 VARCHAR(45),
        IN pREF_CLI8 VARCHAR(45),
        IN pREF_CLI9 VARCHAR(45),
        IN pREF_CLI10 VARCHAR(45)
   )
BEGIN

	if length(pOldREF) = 0 then
		INSERT INTO articles (ref) VALUES (pREF);
		SET @newref = pREF;
	else
		set @newref = pOldREF;
	end if;

	UPDATE `articles` SET
      ref = pREF,
      fam = pFAM,
      libart = pREF_CLI,
      libcompl1 = pLIBCOMPL1,
      libcompl2 = pLIBCOMPL2,
      libcompl3 = pLIBCOMPL3,
      libcompl4 = pLIBCOMPL4,
      UNITE = pUNITE,
      prixht1 = pPRIXHT1,
      prixht2 = pPRIXHT2,
      prixht3 = pPRIXHT3,
      prixht4 = pPRIXHT4,
      prixht5 = pPRIXHT5,
      prixht6 = pPRIXHT6,
      prixht7 = pPRIXHT7,
      prixht8 = pPRIXHT8,
      prixht9 = pPRIXHT9,
      prixht10 = pPRIXHT10,
      ref_cli1 = pREF_CLI1,
      ref_cli2 = pREF_CLI2,
      ref_cli3 = pREF_CLI3,
      ref_cli4 = pREF_CLI4,
      ref_cli5 = pREF_CLI5,
      ref_cli6 = pREF_CLI6,
      ref_cli7 = pREF_CLI7,
      ref_cli8 = pREF_CLI8,
      ref_cli9 = pREF_CLI9,
      ref_cli10 = pREF_CLI10
	WHERE ref = @newref;

END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spUPD_CLIENTS` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spUPD_CLIENTS`(
        IN pSte CHAR(2),
        IN pCd_cli INT,
        IN pQualite VARCHAR(5),
        IN pnom VARCHAR(40),
        IN pADRESSE VARCHAR(40),
        IN pADRESSE2 VARCHAR(40),
        IN pCP VARCHAR(5),
        IN pVILLE VARCHAR(40),
        IN pTel1 VARCHAR(25),
        IN pNOMRESP VARCHAR(25),
        IN pTel2 VARCHAR(25),
        IN pTELP VARCHAR(25),
        IN pFAX VARCHAR(25),
        IN pType VARCHAR(1),
        IN pAGREMENT VARCHAR(30),
        IN pWebsite VARCHAR(50),
        IN pNotarif SMALLINT,
        IN pMODEREG SMALLINT,
        IN pDelaiReg SMALLINT,
        IN pRelance CHAR(1),
        IN pCABINET SMALLINT,
        IN pEXPERT SMALLINT,
        IN pGroupe VARCHAR(10),
        IN pComment VARCHAR(140),
        IN pEtage VARCHAR(10),
        IN pCode VARCHAR(10),
        IN pEMAIL VARCHAR(50)
    )
BEGIN

  declare newligne int;

  IF pCD_CLI = 0 THEN
        SET @new_no = GetNewNoClient(pSTE);
		INSERT INTO clients (societe,cd_cli) VALUES(pSTE,@new_no);
		SET newligne = @new_no;
  ELSE
		SET newligne = pCD_CLI;
  END IF;

    UPDATE clients SET
       qualite=pQualite,
       nom=pNom,
       adresse=pADRESSE,
       adresse2=pADRESSE2,
       cp=pCP,
       ville=pVILLE,
       tel1=pTel1,
       nomresp=pNOMRESP,
       tel2=pTel2,
       telp=pTELP,
       fax=pFAX,
       type=pType,
       agrement=pAGREMENT,
       website=pWebsite,
       notarif=pNotarif,
       modereg=pMODEREG,
       delaireg=pDelaiReg,
       relance=pRelance,
       cabinet=pCABINET,
       expert=pEXPERT,
       groupe=pGroupe,
       comment=pComment,
       etage=pEtage,
       code=pCode,
       email=pEMAIL
     WHERE societe=pSte AND cd_cli = newligne;
  
END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spUPD_CLIENTS_COMPTE` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spUPD_CLIENTS_COMPTE`(
        IN pSte CHAR(2),
        IN pCodeClient INT
    )
BEGIN

    UPDATE clients SET nocompte = GetNewNoCompte(pSte)
       WHERE societe=pSte AND cd_cli = pCodeClient AND nocompte = 0;
  
END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spUPD_CLIENTS_LETTRER` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spUPD_CLIENTS_LETTRER`(
		IN pSOCIETE CHAR(2),
        IN pCD_CLI INTEGER(11)
    )
BEGIN

  DECLARE no_more_rows BOOLEAN DEFAULT FALSE;
  DECLARE cCredit DECIMAL(11,2);
  DECLARE cDebit DECIMAL(11,2);
  DECLARE cNo_ligne INTEGER(11);

  DECLARE cLignes CURSOR FOR SELECT Credit, Debit, No_ligne FROM `eleves_cpt` 
       WHERE cd_cli = pCD_CLI AND date < DATE_ADD(CURDATE(), INTERVAL -3 MONTH) AND 
             credit+debit <> 0 AND date_valeur IS NOT NULL AND lettre=0 
       ORDER BY no_ligne;

  DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET no_more_rows = TRUE;  
  
  
  SET @lettre = (SELECT COALESCE(MAX(lettre),0) FROM eleves_cpt WHERE cd_cli=pCD_CLI) + 1;
  SET @NB = 0;
  SET @mtSolde = 0;

  OPEN cLignes;
  boucle1 : LOOP
    FETCH cLignes INTO cCredit, cDebit, cNo_ligne;
    
    IF no_more_rows THEN
       CLOSE cLignes;
       LEAVE boucle1;
    END IF;

    SET @mtSolde = @mtSolde + cCredit - cDebit;
    IF @mtSolde = 0 Then
       
       UPDATE `eleves_cpt` 
         SET lettre = @lettre
         WHERE cd_cli = pCD_CLI AND date < DATE_ADD(CURDATE(), INTERVAL -3 MONTH)  AND credit+debit <> 0 
			   AND no_ligne <= cNo_ligne AND date_valeur IS NOT NULL AND lettre=0;

       SET @mtSolde = 0;
       SET @lettre = @lettre + 1;
       SET @NB = @NB + 1;
	END IF;
    
  END LOOP boucle1;
  
  
  SELECT @NB AS NB;

END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spUPD_DEMANDES` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spUPD_DEMANDES`(
        IN pSTE CHAR(2),
        IN pNO_ID INTEGER(11),
        IN pDATE DATE,
        IN pCD_CLI VARCHAR(40),
        IN pNOM VARCHAR(40),
        IN pTYPECLI CHAR(1),
        IN pC_NOM VARCHAR(40),
        IN pC_ADR VARCHAR(40),
        IN pC_ADR2 VARCHAR(40),
        IN pC_CP VARCHAR(40),
        IN pC_VILLE VARCHAR(40),
        IN pC_TEL1 VARCHAR(40),
        IN pC_TEL2 VARCHAR(40),
        IN pC_TELP VARCHAR(40),
        IN pC_FAX VARCHAR(40),
        IN pC_OBS VARCHAR(40),
        IN pC_ETAGE VARCHAR(10),
        IN pC_CODE VARCHAR(20),
        IN pC_EMAIL VARCHAR(50),
        IN pC_Qualite VARCHAR(40),
        IN pCABINET smallint,
        IN pEXPERT smallint,
        IN pCODETVA SMALLINT,
        IN pTAUXTVA decimal(11,2),
        IN pNOSIN VARCHAR(40),
        IN pNOPOL VARCHAR(40),
        IN pVREF VARCHAR(40),
        IN pREF_EXPERT VARCHAR(40),
        IN pLIBCOMPL VARCHAR(40),
        IN pTypeTrav CHAR(1),
        IN pGROUPE VARCHAR(10),
        IN pA_REVOIR SMALLINT,
		IN pHUMIDITE SMALLINT,
        IN pNOTARIF SMALLINT,
        IN pTX_TRAV VARCHAR(100),
        IN pMTTRAV DECIMAL(11,2),
        IN pSUIVI_PAR VARCHAR(10),
        IN pUser VARCHAR(10)
    )
BEGIN

	declare newligne int;
	
	if pNO_ID = 0 then
        SET @new_no = GetNewNoEntete(pSTE,'DEM_DEVIS');
		INSERT INTO dem_devis (societe,NO_ID,usermaj) VALUES 
		       (pSTE,@new_no,pUSER);
		SET newligne = @new_no;
	else
		set newligne = pNO_ID;
	end if;

--  insert into t_debug (proc,msg)
--  values ('spUPD_LIV_BNQ',CONCAT('newligne=',newligne));

	UPDATE dem_devis SET
	   Date = pDATE,
	   cd_cli = pCD_CLI,
	   nomcli = pNOM,
	   typecli = pTYPECLI,
	   c_nom = pC_NOM,
	   c_adr = pC_ADR,
	   C_ADR2 = pC_ADR2,
	   C_CP = pC_CP,
	   C_VILLE = pC_VILLE,
	   c_tel1 = pC_TEL1,
	   c_tel2 = pC_TEL2,
	   c_telp = pC_TELP,
	   C_FAX = pC_FAX,
	   C_OBS = pC_OBS,
	   C_ETAGE = pC_ETAGE,
	   c_code = pC_CODE,
	   c_EMAIL = pC_EMAIL,
	   C_QUALITE = pC_Qualite,
	   CABINET = pCABINET,
	   EXPERT = pEXPERT,
       codetva = pCODETVA,
       tauxtva = pTAUXTVA,
	   noSin = pNOSIN,
	   noPol = pNOPOL,
	   VREF = pVREF,
	   REF_EXPERT = pREF_EXPERT,
	   LIBCOMPL = pLIBCOMPL,
	   typeTrav = pTypeTrav,
	   groupe = pGROUPE,
	   A_REVOIR = pA_REVOIR,
       HUMIDITE = pHUMIDITE,
	   notarif = pNOTARIF,
       tx_trav = pTX_TRAV,
       mttrav = pMTTRAV,
       suivi_par = pSUIVI_PAR,
 	   USERMAJ = pUser
    WHERE societe = pSTE AND no_id = newligne;
 
    -- retourne le nouveau numero de dossier
    SELECT newligne as NewNumero;  

END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spUPD_DEM_DEVIS_LIGNE` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spUPD_DEM_DEVIS_LIGNE`(
        IN pNolig INT,
        IN pSTE CHAR(2),
        IN pNOID INT,
        IN pDATE DATE, 
        IN pDATEVI VARCHAR(10),
        IN pHEUREVI VARCHAR(10),
        IN pcomment VARCHAR(100),
        IN pMontant decimal(11,2),
        IN pLISTE VARCHAR(10),
        IN pUSERMAJ VARCHAR(10)

    )
BEGIN

	declare newligne int;
	
	if pNolig = 0 then
       
	   INSERT INTO DEM_LIG (SOCIETE,NO_ID,DATE) VALUES (pSTE,pNOID,NOW());
       SET newligne = (select LAST_INSERT_ID());
	else
	   SET newligne = pNolig;
	end if;

	UPDATE DEM_LIG SET comment = pcomment, montant = pMontant, USERMAJ = pUSERMAJ
	WHERE NO_ID = pNOID AND NOLIG = newligne;

    IF length(pDATEVI) > 0 AND length(pHEUREVI) > 0 THEN
		UPDATE DEM_LIG SET DATEVI = pDATEVI, HEUREVI = pHEUREVI, LISTE = pLISTE
		WHERE NO_ID = pNOID AND NOLIG = newligne;
    ELSE
		UPDATE DEM_LIG SET DATEVI = NULL, HEUREVI = NULL, LISTE = 'X'
		WHERE NO_ID = pNOID AND NOLIG = newligne;
    end if;

END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spUPD_DEVIS` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spUPD_DEVIS`(
        IN pSTE CHAR(2),
        IN pNO_ID INTEGER(11),
        IN pNOORDRE INTEGER(11),
        IN pNO_DD INTEGER(11),
        IN pDATE DATE,
        IN pCD_CLI VARCHAR(40),
        IN pNOM VARCHAR(40),
        IN pTYPECLI CHAR(1),
        IN pC_NOM VARCHAR(40),
        IN pC_ADR VARCHAR(40),
        IN pC_ADR2 VARCHAR(40),
        IN pC_CP VARCHAR(40),
        IN pC_VILLE VARCHAR(40),
        IN pC_TEL1 VARCHAR(40),
        IN pC_TEL2 VARCHAR(40),
        IN pC_TELP VARCHAR(40),
        IN pC_FAX VARCHAR(40),
        IN pC_OBS VARCHAR(40),
        IN pC_ETAGE VARCHAR(10),
        IN pC_CODE VARCHAR(20),
        IN pC_EMAIL VARCHAR(50),
        IN pC_Qualite VARCHAR(40),
        IN pCABINET smallint,
        IN pEXPERT smallint,
        IN pCODETVA SMALLINT,
        IN pTAUXTVA decimal(11,2),
        IN pNOSIN VARCHAR(40),
        IN pNOPOL VARCHAR(40),
        IN pVREF VARCHAR(40),
        IN pREF_EXPERT VARCHAR(40),
        IN pLIBCOMPL VARCHAR(40),
        IN pTypeTrav CHAR(1),
        IN pGROUPE VARCHAR(10),
        IN pA_REVOIR SMALLINT,
        IN pNOTARIF SMALLINT,
        IN pNO_AGREMENT VARCHAR(40),
        IN pUser VARCHAR(10),
        IN pStatus VARCHAR(10)
    )
BEGIN

	declare newligne int;
	
	if pNO_ID = 0 then
        SET @new_no = GetNewNoEntete(pSTE,'DEVIS');
		INSERT INTO devis (societe,NO_ID,NOORDRE,NO_DD,nochantier,usermaj) VALUES 
		       (pSTE,@new_no,pNOORDRE,pNO_DD,pNO_DD,pUser);
		SET newligne = @new_no;
	else
		set newligne = pNO_ID;
	end if;

--  insert into t_debug (proc,msg)
--  values ('spUPD_LIV_BNQ',CONCAT('newligne=',newligne));

	UPDATE devis SET
		Date = pDATE,
		nochantier = pNO_DD,
		noordre = pNOORDRE,
		cd_cli = pCD_CLI,
		nomcli = pNOM,
		typecli = pTYPECLI,
		c_nom = pC_NOM,
		c_adr = pC_ADR,
		C_ADR2 = pC_ADR2,
		C_CP = pC_CP,
		C_VILLE = pC_VILLE,
		c_tel1 = pC_TEL1,
		c_tel2 = pC_TEL2,
		c_telp = pC_TELP,
		C_FAX = pC_FAX,
		C_OBS = pC_OBS,
		C_ETAGE = pC_ETAGE,
		c_code = pC_CODE,
		c_EMAIL = pC_EMAIL,
		C_QUALITE = pC_Qualite,
		CABINET = pCABINET,
		EXPERT = pEXPERT,
		codetva = pCODETVA,
		tauxtva = pTAUXTVA,
		noSin = pNOSIN,
		noPol = pNOPOL,
		VREF = pVREF,
		REF_EXPERT = pREF_EXPERT,
		LIBCOMPL = pLIBCOMPL,
		typeTrav = pTypeTrav,
		groupe = pGROUPE,
		A_REVOIR = pA_REVOIR,
		notarif = pNOTARIF,
		no_agrement = pNO_AGREMENT,
		USERMAJ = pUser,
		totalht = COALESCE((SELECT SUM(mtht) FROM devis_lig WHERE societe=pSTE AND no_id=newligne),0),
		totaltva = ROUND(totalht * tauxtva / 100,2),
		totalttc = totalht + totaltva,
        status = pStatus
    WHERE societe = pSTE AND no_id = newligne;
 
    -- 26/10/2015 : menoriser le lien sur le DD
    if pNO_ID = 0 AND pNO_DD > 0 then
       UPDATE dem_devis SET nodevis = newligne
	   WHERE societe = pSTE AND NO_ID = pNO_DD AND nodevis=0;
	end if;

    -- retourne le nouveau numero de dossier
    SELECT newligne as newnumero;

END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spUPD_DEVIS_LIGNE` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spUPD_DEVIS_LIGNE`(
        IN pSTE CHAR(2),
        IN pTABLE VARCHAR(20),
        IN pDossier INT,
        IN pNolig INT,
        IN pREF CHAR(2),
        IN pLIB VARCHAR(60),
        IN pQTE FLOAT,
        IN pPrixht DECIMAL(11,2),
        IN pUsermaj VARCHAR(10)
    )
BEGIN

	IF pTABLE = "DEVIS_LIG" THEN
		REPLACE devis_lig (societe, no_id, nolig, ref, LIB, QTE, prixht, USERMAJ)
			VALUES (pSTE, pDossier, pNolig, pREF, pLIB, pQTE, pPrixht, pUsermaj);
	ELSE
		REPLACE proforma_lig (societe, no_id, nolig, ref, LIB, QTE, prixht, USERMAJ)
			VALUES (pSTE, pDossier, pNolig, pREF, pLIB, pQTE, pPrixht, pUsermaj);
	END IF;

END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spUPD_DEVIS_NOID` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spUPD_DEVIS_NOID`(
        IN pSTE CHAR(2),
        IN pNodevis INT,
        IN pNewid INT
    )
BEGIN

     UPDATE `devis` SET no_id = pNewid
        WHERE societe=pSTE AND no_id = pNodevis;

END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spUPD_DEVIS_TOTAUX` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spUPD_DEVIS_TOTAUX`(
        IN pSTE CHAR(2),
		IN pNO_ID INT
        )
BEGIN
  DECLARE sEOF INT;
  DECLARE l_groupe INT DEFAULT 0;
  DECLARE l_nolig INT;
  
  
  DECLARE cDevis_lig CURSOR FOR SELECT nolig FROM devis_lig 
     WHERE societe = pSTE AND no_id = pNO_ID AND ref='ST' ORDER BY nolig;
               
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET sEOF = 1;
  
	-- RAZ groupe
	UPDATE devis_lig SET groupe=0 WHERE societe=pSTE AND no_id=pNO_ID;

  SET sEOF = 0;
    
  OPEN cDevis_lig;

lignes_loop : 
  WHILE sEOF = 0 DO
    
    FETCH cDevis_lig INTO l_nolig;
    IF sEOF = 1 THEN
       LEAVE lignes_loop;
    END IF;
    
    
       SET l_groupe = l_groupe + 1;
       UPDATE devis_lig 
         SET groupe = l_groupe
         WHERE societe=pSTE AND no_id=pNO_ID AND nolig >= l_nolig;
               
  END WHILE lignes_loop;
  CLOSE cDevis_lig;
  
  SELECT l_groupe;
  
END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spUPD_ENTETE_TOTAUX` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spUPD_ENTETE_TOTAUX`(
        IN pSTE CHAR(2),
        IN pTABLE VARCHAR(10),
        IN pNO_ID INT
        )
BEGIN

  IF pTABLE = "DEVIS" THEN
     UPDATE devis 
        SET totalht = (SELECT SUM(mtht) FROM devis_lig WHERE societe=pSTE AND no_id=pNO_ID),
            totaltva = ROUND(totalht * tauxtva / 100,2),
            totalttc = totalht + totaltva
        WHERE societe=pSTE AND no_id=pNO_ID;
  ELSEIF pTABLE = "FACTURE" THEN
     UPDATE facture 
        SET totalht = (SELECT SUM(mtht) FROM facture_lig WHERE societe=pSTE AND no_id=pNO_ID),
            totaltva = ROUND(totalht * tauxtva / 100,2),
            totalttc = totalht + totaltva
        WHERE societe=pSTE AND no_id=pNO_ID;
  ELSE
     UPDATE proforma 
        SET totalht = (SELECT SUM(mtht) FROM proforma_lig WHERE societe=pSTE AND no_id=pNO_ID),
            totaltva = ROUND(totalht * tauxtva / 100,2),
            totalttc = totalht + totaltva
        WHERE societe=pSTE AND no_id=pNO_ID;
  END IF;

END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spUPD_FACTURE` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spUPD_FACTURE`(
        IN pSTE CHAR(2),
        IN pNO_ID INTEGER(11),
        IN pNODEVIS INTEGER(11),
        IN pNOORDRE INTEGER(11),
        IN pNO_DD INTEGER(11),
        IN pDATE DATE,
        IN pCD_CLI VARCHAR(40),
        IN pNOM VARCHAR(40),
        IN pTYPECLI CHAR(1),
        IN pC_NOM VARCHAR(40),
        IN pC_ADR VARCHAR(40),
        IN pC_ADR2 VARCHAR(40),
        IN pC_CP VARCHAR(40),
        IN pC_VILLE VARCHAR(40),
        IN pC_TEL1 VARCHAR(40),
        IN pC_TEL2 VARCHAR(40),
        IN pC_TELP VARCHAR(40),
        IN pC_FAX VARCHAR(40),
        IN pC_OBS VARCHAR(40),
        IN pC_ETAGE VARCHAR(10),
        IN pC_CODE VARCHAR(20),
        IN pC_EMAIL VARCHAR(50),
        IN pC_Qualite VARCHAR(40),
        IN pCABINET smallint,
        IN pEXPERT smallint,
        IN pCODETVA SMALLINT,
        IN pTAUXTVA decimal(11,2),
        IN pNOSIN VARCHAR(40),
        IN pNOPOL VARCHAR(40),
        IN pVREF VARCHAR(40),
        IN pREF_EXPERT VARCHAR(40),
        IN pLIBCOMPL VARCHAR(40),
        IN pTypeTrav CHAR(1),
        IN pGROUPE VARCHAR(10),
        IN pA_REVOIR SMALLINT,
        IN pNOTARIF SMALLINT,
        IN pATTENTION VARCHAR(50),
        IN pUser VARCHAR(10)
    )
BEGIN

	declare newligne int;
	
	if pNO_ID = 0 then
        SET @new_no = GetNewNoEntete(pSTE,'FACTURE');
		INSERT INTO facture (societe,NO_ID,NOORDRE,NO_DD,chantierusermaj) VALUES 
		       (pSTE,@new_no,pNOORDRE,pNO_DD,pNO_DD,pUser);
		SET newligne = @new_no;
	else
		set newligne = pNO_ID;
	end if;

--  insert into t_debug (proc,msg)
--  values ('spUPD_LIV_BNQ',CONCAT('newligne=',newligne));

	UPDATE facture SET
	   date = pDATE,
       nochantier = pNO_DD,
       nodevis = pNODEVIS,
       noordre = pNOORDRE,
	   cd_cli = pCD_CLI,
	   nomcli = pNOM,
	   typecli = pTYPECLI,
	   c_nom = pC_NOM,
	   c_adr = pC_ADR,
	   C_ADR2 = pC_ADR2,
	   C_CP = pC_CP,
	   C_VILLE = pC_VILLE,
	   c_tel1 = pC_TEL1,
	   c_tel2 = pC_TEL2,
	   c_telp = pC_TELP,
	   C_FAX = pC_FAX,
	   C_OBS = pC_OBS,
	   C_ETAGE = pC_ETAGE,
	   c_code = pC_CODE,
	   c_EMAIL = pC_EMAIL,
	   C_QUALITE = pC_Qualite,
	   CABINET = pCABINET,
	   EXPERT = pEXPERT,
       codetva = pCODETVA,
       tauxtva = pTAUXTVA,
	   noSin = pNOSIN,
	   noPol = pNOPOL,
	   VREF = pVREF,
	   REF_EXPERT = pREF_EXPERT,
	   LIBCOMPL = pLIBCOMPL,
	   typeTrav = pTypeTrav,
	   groupe = pGROUPE,
	   A_REVOIR = pA_REVOIR,
	   notarif = pNOTARIF,
	   ATTENTION = pATTENTION,
	   USERMAJ = pUser,
		totalht = COALESCE((SELECT SUM(mtht) FROM facture_lig WHERE societe=pSTE AND no_id=newligne),0),
        totaltva = ROUND(totalht * tauxtva / 100,2),
        totalttc = totalht + totaltva
    WHERE societe = pSTE AND no_id = newligne;
 
    -- retourne le nouveau numero de dossier
    SELECT newligne as NewNumero;  

END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spUPD_FACTURES_SOLDE` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spUPD_FACTURES_SOLDE`(
        IN pSTE CHAR(2),
        IN pDATE DATE
    )
BEGIN
  DECLARE sEOF INT;
  DECLARE l_no_id INT;
  
  DECLARE cFactures CURSOR FOR SELECT no_id FROM facture WHERE societe = pSTE;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET sEOF = 1;

  SET sEOF = 0;
    
    
    UPDATE facture SET
      mt_solde = 0,
      date_solde = pDate
    WHERE societe=pSTE;

  OPEN cFactures;

lignes_loop : 
  WHILE sEOF = 0 DO
    
    FETCH cFactures INTO l_no_id;
    IF sEOF = 1 THEN
       LEAVE lignes_loop;
    END IF;
    
    
    UPDATE facture SET
      mt_solde = totalttc - (SELECT COALESCE(sum(montant),0) FROM facture_reg WHERE societe=pSTE AND nofact=l_no_id AND date <= pDate),
      date_solde = pDate
    WHERE societe=pSTE AND no_id=l_no_id;
               
  END WHILE lignes_loop;
  CLOSE cFactures;
  
END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spUPD_FACTURE_ALL_TOTAUX` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spUPD_FACTURE_ALL_TOTAUX`(
        IN pSTE CHAR(2)
    )
BEGIN
  DECLARE sEOF INT;
  DECLARE l_groupe INT DEFAULT 0;
  DECLARE l_no_id INT;
  
  
  DECLARE cFactures CURSOR FOR SELECT no_id FROM facture WHERE societe = pSTE;
               
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET sEOF = 1;

  SET sEOF = 0;
    
  OPEN cFactures;

lignes_loop : 
  WHILE sEOF = 0 DO
    
    FETCH cFactures INTO l_no_id;
    IF sEOF = 1 THEN
       LEAVE lignes_loop;
    END IF;
    
    
    
    UPDATE facture 
     SET totalht = (SELECT SUM(mtht) FROM facture_lig WHERE societe=pSTE AND no_id=l_no_id),
         totaltva = ROUND(totalht * tauxtva / 100,2),
         totalttc = totalht + totaltva
     WHERE societe=pSTE AND no_id=l_no_id;
    SET l_groupe = l_groupe + 1;
               
  END WHILE lignes_loop;
  CLOSE cFactures;
  
  
  SELECT l_groupe;
  
END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spUPD_FACTURE_ECHEANCE` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spUPD_FACTURE_ECHEANCE`(
        IN pSTE CHAR(2),
        IN pNOFACT INTEGER(11),
        IN pModereg SMALLINT,
        IN pEcheance DATE
    )
BEGIN

UPDATE `facture` 
    SET modereg = pModereg,
        echeance = pEcheance
    WHERE societe=pSTE AND no_id = pNOFACT;

END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spUPD_FACTURE_LIGNE` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spUPD_FACTURE_LIGNE`(
        IN pSTE CHAR(2),
        IN pDossier INT,
        IN pNolig INT,
        IN pREF CHAR(2),
        IN pLIB VARCHAR(60),
        IN pQTE FLOAT,
        IN pPrixht DECIMAL(11,2),
        IN pUsermaj VARCHAR(10)
    )
BEGIN

	REPLACE facture_lig (societe, no_id, nolig, ref, LIB, QTE, prixht, USERMAJ)
		VALUES (pSTE, pDossier, pNolig, pREF, pLIB, pQTE, pPrixht, pUsermaj);

END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spUPD_FACTURE_NOID` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spUPD_FACTURE_NOID`(
        IN pSTE CHAR(2),
        IN pNofact INT,
        IN pNewid INT
    )
BEGIN

     UPDATE `facture` SET no_id = pNewid
        WHERE societe=pSTE AND no_id = pNofact;

END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spUPD_FACTURE_REG` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spUPD_FACTURE_REG`(
        IN pSTE CHAR(2),
        IN pNOFACT VARCHAR(20),
        IN pMODEREG SMALLINT,
        IN pCOD_BNQ INTEGER(11),
        IN pDATE DATE,
        IN pMONTANT DECIMAL(11,2)
    )
BEGIN

    UPDATE FACTURE_REG SET
       date = pDATE, MONTANT = pMONTANT
       WHERE SOCIETE=pSTE AND NOFACT= pNOFACT AND MODEREG = pMODEREG AND COD_BNQ = pCOD_BNQ;

END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spUPD_FACTURE_RELANCE` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spUPD_FACTURE_RELANCE`(
        IN pSTE CHAR(2),
        IN pNOFACT INTEGER(11),
        IN pRELANCE SMALLINT
    )
BEGIN

  -- relance 1 ?
  IF pRELANCE = 1 THEN
	  UPDATE `facture` SET daterel = CURRENT_DATE()
		WHERE societe=pSTE AND no_id = pNOFACT;
  ELSE
	  UPDATE `facture` SET daterel2 = CURRENT_DATE()
		WHERE societe=pSTE AND no_id = pNOFACT;
  END IF;

END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spUPD_FACTURE_STATUT` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spUPD_FACTURE_STATUT`(
        IN pSTE CHAR(2),
        IN pNOFACT INTEGER(11)
    )
BEGIN

UPDATE `facture` 
    SET mtregl = (SELECT COALESCE(SUM(montant),0) FROM `facture_reg` WHERE societe=pSTE AND nofact=pNOFACT),
        datereg = (SELECT MAX(DATE) FROM `facture_reg` WHERE societe=pSTE AND nofact=pNOFACT)
    WHERE societe=pSTE AND no_id = pNOFACT;

END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spUPD_FACTURE_TOTAUX` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spUPD_FACTURE_TOTAUX`(
        IN pSTE CHAR(2),
        IN pNO_ID INT
    )
BEGIN
  DECLARE sEOF INT;
  DECLARE l_groupe INT DEFAULT 0;
  DECLARE l_nolig INT;
  
  DECLARE cFacture_lig CURSOR FOR SELECT nolig FROM facture_lig 
     WHERE societe = pSTE AND no_id = pNO_ID AND ref='ST' ORDER BY nolig;
               
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET sEOF = 1;

	-- RAZ no de groupe
	UPDATE facture_lig SET groupe=0 WHERE societe=pSTE AND no_id=pNO_ID;

  SET sEOF = 0;
    
  OPEN cFacture_lig;

lignes_loop : 
  WHILE sEOF = 0 DO
    
    FETCH cFacture_lig INTO l_nolig;
    IF sEOF = 1 THEN
       LEAVE lignes_loop;
    END IF;
    
    
       SET l_groupe = l_groupe + 1;
       UPDATE facture_lig 
         SET groupe = l_groupe
         WHERE societe=pSTE AND no_id=pNO_ID AND nolig >= l_nolig;
               
  END WHILE lignes_loop;
  CLOSE cFacture_lig;
  
  
  SELECT l_groupe;
  
END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spUPD_LETTRE` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spUPD_LETTRE`(
        IN pREF VARCHAR(10),
        IN pLIBELLE VARCHAR(30),
        IN pCORPS mediumtext
    )
BEGIN

  REPLACE INTO lettre (ref,libelle,corps) VALUES
     (pREF,pLIBELLE,pCORPS);
  
END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spUPD_LIGNES_MAJO` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spUPD_LIGNES_MAJO`(
        IN pSTE CHAR(2),
        IN pRubrique VARCHAR(10),
        IN pNO_ID INT,
        IN pPOUR100 DECIMAL(6,2)
    )
BEGIN

IF pRubrique = 'DEVIS' THEN
  UPDATE devis_lig SET  
    prixht = ROUND(prixht * (100+pPOUR100) / 100,2)
    WHERE societe=pSTE AND no_id=pNO_ID AND prixht <> 0;
ELSEIF pRubrique = 'FACTURE' THEN
  UPDATE facture_lig SET  
    prixht = ROUND(prixht * (100+pPOUR100) / 100,2)
    WHERE societe=pSTE AND no_id=pNO_ID AND prixht <> 0;
ELSE
  UPDATE proforma_lig SET  
    prixht = ROUND(prixht * (100+pPOUR100) / 100,2)
    WHERE societe=pSTE AND no_id=pNO_ID AND prixht <> 0;
END IF;

END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spUPD_LIV_BNQ` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spUPD_LIV_BNQ`(
        IN pCOD_BNQ INTEGER(11),
        IN pSTE CHAR(2),
        IN pCD_CLI INTEGER(11),
        IN pMODEREG INTEGER(11),
        IN pDATE VARCHAR(10),
        IN PMONTANT DECIMAL(11,2),
        IN pETS VARCHAR(20),
        IN pJST VARCHAR(20),
        IN pJNL VARCHAR(20),
		IN pREMB TINYINT,
        IN pCODETVA TINYINT
    )
BEGIN

	declare newligne int;
	
	if pCOD_BNQ = 0 then
        SET @new_no = GetNewNoBanque(pSTE);
		INSERT INTO liv_bnq (societe,cd_cli,modereg,cod_bnq) 
		   VALUES(pSTE,pCD_CLI,pMODEREG,@new_no);
		SET newligne = @new_no;
	else
		set newligne = pCOD_BNQ;
	end if;

--  insert into t_debug (proc,msg)
--  values ('spUPD_LIV_BNQ',CONCAT('newligne=',newligne));

	UPDATE liv_bnq SET
        cd_cli = pCD_CLI,
		modereg = pMODEREG,
        Date = pDATE,
        MontantRegl = pMONTANT,
        ets = pETS,
        jst = pJST,
        jnl = pJNL,
        compte = (SELECT COALESCE(compte,671800) FROM p_journaux WHERE code = pJNL),
		remboursement = pREMB,
        codetva = pCODETVA
    WHERE societe=pSTE AND COD_BNQ=newligne;

    SELECT newligne as NewCodeBanque;  
    
END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spUPD_PROFORMA` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spUPD_PROFORMA`(
        IN pSTE CHAR(2),
        IN pNO_ID INTEGER(11),
        IN pNOORDRE INTEGER(11),
        IN pNO_DD INTEGER(11),
        IN pDATE DATE,
        IN pCD_CLI VARCHAR(40),
        IN pNOM VARCHAR(40),
        IN pTYPECLI CHAR(1),
        IN pC_NOM VARCHAR(40),
        IN pC_ADR VARCHAR(40),
        IN pC_ADR2 VARCHAR(40),
        IN pC_CP VARCHAR(40),
        IN pC_VILLE VARCHAR(40),
        IN pC_TEL1 VARCHAR(40),
        IN pC_TEL2 VARCHAR(40),
        IN pC_TELP VARCHAR(40),
        IN pC_FAX VARCHAR(40),
        IN pC_OBS VARCHAR(40),
        IN pC_ETAGE VARCHAR(10),
        IN pC_CODE VARCHAR(20),
        IN pC_EMAIL VARCHAR(50),
        IN pC_Qualite VARCHAR(40),
        IN pCABINET smallint,
        IN pEXPERT smallint,
        IN pCODETVA SMALLINT,
        IN pTAUXTVA decimal(11,2),
        IN pNOSIN VARCHAR(40),
        IN pNOPOL VARCHAR(40),
        IN pVREF VARCHAR(40),
        IN pREF_EXPERT VARCHAR(40),
        IN pLIBCOMPL VARCHAR(40),
        IN pTypeTrav CHAR(1),
        IN pGROUPE VARCHAR(10),
        IN pA_REVOIR SMALLINT,
        IN pNOTARIF SMALLINT,
        IN pUser VARCHAR(10)
    )
BEGIN

	declare newligne int;
	
	if pNO_ID = 0 then
        SET @new_no = GetNewNoEntete(pSTE,'PROFORMA');
		INSERT INTO proforma (societe,NO_ID,NOORDRE,NO_DD,nochantier,usermaj) VALUES 
		       (pSTE,@new_no,pNOORDRE,pNO_DD,pNO_DD,pUser);
		SET newligne = @new_no;
	else
		set newligne = pNO_ID;
	end if;

--  insert into t_debug (proc,msg)
--  values ('spUPD_LIV_BNQ',CONCAT('newligne=',newligne));

	UPDATE proforma SET
	   Date = pDATE,
	   cd_cli = pCD_CLI,
	   nomcli = pNOM,
	   typecli = pTYPECLI,
	   c_nom = pC_NOM,
	   c_adr = pC_ADR,
	   C_ADR2 = pC_ADR2,
	   C_CP = pC_CP,
	   C_VILLE = pC_VILLE,
	   c_tel1 = pC_TEL1,
	   c_tel2 = pC_TEL2,
	   c_telp = pC_TELP,
	   C_FAX = pC_FAX,
	   C_OBS = pC_OBS,
	   C_ETAGE = pC_ETAGE,
	   c_code = pC_CODE,
	   c_EMAIL = pC_EMAIL,
	   C_QUALITE = pC_Qualite,
	   CABINET = pCABINET,
	   EXPERT = pEXPERT,
       codetva = pCODETVA,
       tauxtva = pTAUXTVA,
	   noSin = pNOSIN,
	   noPol = pNOPOL,
	   VREF = pVREF,
	   REF_EXPERT = pREF_EXPERT,
	   LIBCOMPL = pLIBCOMPL,
	   typeTrav = pTypeTrav,
	   groupe = pGROUPE,
	   A_REVOIR = pA_REVOIR,
	   notarif = pNOTARIF,
       nochantier = pNO_DD,
	   USERMAJ = pUser,
		totalht = COALESCE((SELECT SUM(mtht) FROM proforma_lig WHERE societe=pSTE AND no_id=newligne),0),
        totaltva = ROUND(totalht * tauxtva / 100,2),
        totalttc = totalht + totaltva
    WHERE societe = pSTE AND no_id = newligne;
 
    -- retourne le nouveau numero de dossier
    SELECT newligne as NewNumero;  

END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spUPD_PROFORMA_TOTAUX` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spUPD_PROFORMA_TOTAUX`(
        IN pSTE CHAR(2),
        IN pNO_ID INT
    )
BEGIN
  DECLARE sEOF INT;
  DECLARE l_groupe INT DEFAULT 0;
  DECLARE l_nolig INT;
  
  
  DECLARE cproforma_lig CURSOR FOR SELECT nolig FROM proforma_lig 
     WHERE societe = pSTE AND no_id = pNO_ID AND ref='ST' ORDER BY nolig;
               
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET sEOF = 1;
  
  UPDATE proforma_lig SET groupe=0 WHERE societe=pSTE AND no_id=pNO_ID;

  SET sEOF = 0;
    
  OPEN cproforma_lig;

lignes_loop : 
  WHILE sEOF = 0 DO
    
    FETCH cproforma_lig INTO l_nolig;
    IF sEOF = 1 THEN
       LEAVE lignes_loop;
    END IF;
    
    
       SET l_groupe = l_groupe + 1;
       UPDATE proforma_lig 
         SET groupe = l_groupe
         WHERE societe=pSTE AND no_id=pNO_ID AND nolig >= l_nolig;
               
  END WHILE lignes_loop;
  CLOSE cproforma_lig;
  
  
  SELECT l_groupe;
  
END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spUPD_P_CABINET` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spUPD_P_CABINET`(
        IN pCode INTEGER(11),
        IN pNom VARCHAR(30),
        IN pAdresse VARCHAR(30),
        IN pAdresse2 VARCHAR(30),
        IN pCP CHAR(5),
        IN pVILLE VARCHAR(25),
        IN PTel1 VARCHAR(14),
        IN PTel2 VARCHAR(14),
        IN PTelP VARCHAR(14),
        IN PFax VARCHAR(14),
        IN pAfficher SMALLINT
    )
BEGIN

	declare newligne int;
	
	if pCode = 0 then
        SET newligne = GetNewNoCabinet();
		INSERT INTO p_cabinet (code) VALUES(newligne);

        -- créer une ligne expert INDETERMINE pour ce nouveau CABINET
        INSERT INTO P_EXPERTS (code_cab, code_exp, nom) VALUES(newligne,1,'Indéterminé');
	else
		set newligne = pCode;
	end if;

	UPDATE p_cabinet SET
        nom = pNom,
		adresse = pAdresse,
        adresse2 = pAdresse2,
        cp = pCP,
        ville = pVille,
        tel1 = pTel1,
        tel2 = pTel2,
        telP = pTelP,
        fax = pFax,
        afficher = pAfficher

    -- retourne le nouveau numero de cabinet
    WHERE code=newligne;
    SELECT newligne AS NewNumero;
 
END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spUPD_P_EXPERTS` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spUPD_P_EXPERTS`(
        IN pCode_cab INTEGER(11),
        IN pCode_exp INTEGER(11),
        IN pNom VARCHAR(30),
        IN PTel1 VARCHAR(14),
        IN PTel2 VARCHAR(14),
        IN pen_activite SMALLINT,
        IN pEmail VARCHAR(50)
    )
BEGIN

	DECLARE newligne INT;
	
	IF pCode_exp = 0 then
        SET @new_no = GetNewNoExpert(pCode_cab);
		INSERT INTO p_experts (code_cab,code_exp) 
		   VALUES(pCode_cab,@new_no);
		SET newligne = @new_no;
	ELSE
		SET newligne = pCode_exp;
	end if;

--  insert into t_debug (proc,msg)
--  values ('spUPD_LIV_BNQ',CONCAT('newligne=',newligne));

	UPDATE p_experts SET
        nom = pNom,
        tel1 = pTel1,
        tel2 = pTel2,
        en_activite = pEn_activite,
        email = pEmail
    WHERE code_cab = pCode_cab AND code_exp = newligne;
    
END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spUPD_P_SOCIETE` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spUPD_P_SOCIETE`(
        IN pSte CHAR(2),
        IN pNOM VARCHAR(50),
        IN pADRESSE VARCHAR(50),
        IN pCP VARCHAR(50),
        IN pVILLE VARCHAR(50),
        IN pTEL VARCHAR(15),
        IN pC_NODOS INT,
        IN pC_NomFic VARCHAR(13),
        IN pv_code VARCHAR(50),
        IN pV_Lib VARCHAR(20),
        IN pV_Compte VARCHAR(50),
        IN pB_Code CHAR(2),
        IN pB_Lib VARCHAR(20),
        IN pB_Compte VARCHAR(50),
        IN pCorresp1 VARCHAR(65),
        IN pCorresp2 VARCHAR(65),
        IN pCorresp3 VARCHAR(65),
        IN pCorresp4 VARCHAR(65)
    )
BEGIN

    UPDATE p_societe SET
      nom = pNOM,
      ADRESSE = pADRESSE,
      CP = pCP,
      VILLE = pVILLE,
      TEL = pTEL,
      C_NODOS = pC_NODOS,
      C_NomFic = pC_NomFic,
      v_code = pV_Code,
      V_Lib = pV_Lib,
      V_Compte = pV_Compte,
      B_Code = pB_Code,
      B_Lib = pB_Lib,
      B_Compte = pB_Compte,
      Corresp1 = pCorresp1,
      Corresp2 = pCorresp2,
      Corresp3 = pCorresp3,
      Corresp4 = pCorresp4
    WHERE societe=pSte;
  
END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spUPD_P_SOCIETE_DATEEXPORT` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spUPD_P_SOCIETE_DATEEXPORT`(
        IN pSte CHAR(2),
        IN pc_dern DATE
    )
BEGIN

    UPDATE p_societe SET c_dern = pc_dern
       WHERE societe=pSte;
  
END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spUPD_RAPPELS` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`127.0.0.1` PROCEDURE `spUPD_RAPPELS`()
BEGIN

    -- supprimer les anciens rappels
    DELETE FROM email_rappels WHERE date(envoye_le) <= DATE_ADD(current_date,INTERVAL -1 YEAR);

	-- RAPPEL RENDEZ-VOUS dans J+2
	SET @date_rappel = DATE_ADD(current_date(),INTERVAL 2 DAY);

	INSERT INTO email_rappels (societe, nochantier, nompren, rdv_date, email, rdv_statut) 
		SELECT DISTINCT l.societe, l.no_id, s.c_nom, l.rdv_debut, s.c_email, l.liste
		FROM dem_lig l
		INNER JOIN dem_devis s on s.societe = l.societe AND s.no_id = l.no_id  
		WHERE date(l.rdv_debut) = @date_rappel AND l.liste <> "X";

END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spUPD_RDVOUS` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spUPD_RDVOUS`(
        IN pSTE CHAR(2),
        IN pNO_ID INTEGER(11),
        IN pNOFACT INTEGER(11),
        IN pNODEVIS INTEGER(11),
        IN pDATE DATE,
        IN pCD_CLI VARCHAR(40),
        IN pNOM VARCHAR(40),
        IN pTYPECLI CHAR(1),
        IN pC_NOM VARCHAR(40),
        IN pC_ADR VARCHAR(40),
        IN pC_ADR2 VARCHAR(40),
        IN pC_CP VARCHAR(40),
        IN pC_VILLE VARCHAR(40),
        IN pC_TEL1 VARCHAR(40),
        IN pC_TEL2 VARCHAR(40),
        IN pC_TELP VARCHAR(40),
        IN pC_FAX VARCHAR(40),
        IN pC_OBS VARCHAR(40),
        IN pC_ETAGE VARCHAR(10),
        IN pC_CODE VARCHAR(20),
        IN pC_EMAIL VARCHAR(50),
        IN pC_Qualite VARCHAR(40),
        IN pCABINET smallint,
        IN pEXPERT smallint,
        IN pNOSIN VARCHAR(40),
        IN pNOPOL VARCHAR(40),
        IN pVREF VARCHAR(40),
        IN pREF_EXPERT VARCHAR(40),
        IN pLIBCOMPL VARCHAR(40),
        IN pTypeTrav CHAR(1),
        IN pGROUPE VARCHAR(10),
        IN pA_REVOIR SMALLINT,
        IN pTX_TRAV VARCHAR(100),
        IN pMTTRAV DECIMAL(11,2),
        IN pUser VARCHAR(10)
    )
BEGIN

	declare newligne int;
	
	if pNO_ID = 0 then
        SET @new_no = GetNewNoEntete(pSTE,'RDVOUS');
		INSERT INTO rdvous (societe,NO_ID,NOFACT,NODEVIS,usermaj) VALUES 
		       (pSTE,@new_no,pNOFACT,pNODEVIS,pUser);
		SET newligne = @new_no;
	else
		set newligne = pNO_ID;
	end if;

--  insert into t_debug (proc,msg)
--  values ('spUPD_LIV_BNQ',CONCAT('newligne=',newligne));

	UPDATE rdvous SET
	   Date = pDATE,
       nodevis = pNODEVIS,
       nofact = pNOFACT,
	   cd_cli = pCD_CLI,
	   nomcli = pNOM,
	   typecli = pTYPECLI,
	   c_nom = pC_NOM,
	   c_adr = pC_ADR,
	   C_ADR2 = pC_ADR2,
	   C_CP = pC_CP,
	   C_VILLE = pC_VILLE,
	   c_tel1 = pC_TEL1,
	   c_tel2 = pC_TEL2,
	   c_telp = pC_TELP,
	   C_FAX = pC_FAX,
	   C_OBS = pC_OBS,
	   C_ETAGE = pC_ETAGE,
	   c_code = pC_CODE,
	   c_EMAIL = pC_EMAIL,
	   C_QUALITE = pC_Qualite,
	   CABINET = pCABINET,
	   EXPERT = pEXPERT,
	   noSin = pNOSIN,
	   noPol = pNOPOL,
	   VREF = pVREF,
	   REF_EXPERT = pREF_EXPERT,
	   LIBCOMPL = pLIBCOMPL,
	   typeTrav = pTypeTrav,
	   groupe = pGROUPE,
	   A_REVOIR = pA_REVOIR,
       tx_trav = pTX_TRAV,
       mttrav = pMTTRAV,
	   USERMAJ = pUser
    WHERE societe = pSTE AND no_id = newligne;
 
    -- retourne le nouveau numero de dossier
    SELECT newligne as NewNumero;  

END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spUPD_USERS` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spUPD_USERS`(
        IN pcd_uti CHAR(3),
        IN pnom VARCHAR(25),
        IN ppsw VARCHAR(45),
        IN paccess SMALLINT,
		IN pemail VARCHAR(55),
        IN pcode CHAR(3)
    )
BEGIN

	UPDATE p_users SET cd_uti = pcd_uti, nom = pnom, access = paccess, email = pemail
	WHERE cd_uti = pcode;

	IF ppsw <> '' THEN
		UPDATE p_users SET mdp = SHA1(ppsw)
		WHERE cd_uti = pcode;
    END IF;
END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `spUPD_USERS_PREFS` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `spUPD_USERS_PREFS`(
        IN pcd_uti CHAR(3),
        IN psociete CHAR(2),
        IN pimp_blanc VARCHAR(100),
        IN pimp_entete VARCHAR(100)
    )
BEGIN

  REPLACE INTO p_printers (cd_uti, societe, imp_blanc, imp_entete) VALUES
    (pcd_uti, psociete, pimp_blanc, pimp_entete);

END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `utGet_LastAlteredRoutines` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'ALLOW_INVALID_DATES' */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `utGet_LastAlteredRoutines`()
BEGIN
  SELECT routine_name, last_altered, created FROM information_schema.routines
    WHERE DATEDIFF(CURDATE(),last_altered) < 14 AND ROUTINE_SCHEMA='bddevfac'
    ORDER BY last_altered DESC;

END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

-- Dump completed on 2019-01-31 10:03:10
