DELIMITER $$;
CREATE TRIGGER on_delete_visites
AFTER DELETE ON visites
    FOR EACH ROW 
        BEGIN
            -- when delete add visite line to deleted_visites_table
            INSERT INTO `deleted_visites`
            (
                `id`, `visite_code`, `visite`, `commande`, `bl`, `devis`, `type`, `soussociete_code`, `client_code`, 
                `user_code`, `depot_code`, `date_visite`, `commentaire`, `cause`, `cloture`, `longitude`, `latitude`, 
                `distance`, `deleted_at`, `created_at`, `updated_at`, `routing_id`, `heure_debut`, `heure_fin`, `date_prochaineVisite`, 
                `wavesoft`, `tournee_code`, `inTournee`, `ascensseur_code`, `machine_code`
            ) VALUES 
            (
                OLD.id, OLD.visite_code, OLD.visite, OLD.commande, OLD.bl, OLD.devis, OLD.type, OLD.soussociete_code, OLD.client_code, 
                OLD.user_code, OLD.depot_code, OLD.date_visite, OLD.commentaire, OLD.cause, OLD.cloture, OLD.longitude, OLD.latitude, 
                OLD.distance, OLD.deleted_at, OLD.created_at, NOW(), OLD.routing_id, OLD.heure_debut, OLD.heure_fin, OLD.date_prochaineVisite, 
                OLD.wavesoft, OLD.tournee_code, OLD.inTournee, OLD.ascensseur_code, OLD.machine_code
            );                 
        END;

DELIMITER $$;
CREATE TRIGGER on_delete_mouvement_clients
AFTER DELETE ON mouvement_clients
    FOR EACH ROW 
        BEGIN
            -- when delete add visite line to deleted_mouvement_clients
            INSERT INTO `deleted_mouvement_clients`
            (
                `id`, `code_ligne`, `societe_code`, `soussociete_code`, `depot_code`, `client_code`, `user_code`, `commercial_code`, 
                `date_mouvemenet`, `entetcommercial_code`, `caisse_code`, `cloture`, `entetcommercial_numero`, `mouvement_libelle`, 
                `montant`, `sens_ecriture`, `date_echeance`, `payement_code`, `marque_code`, `isactif`, `commentaire`, `wavesoft`,
                 `cree_par`, `deleted_at`, `created_at`, `updated_at`
            ) VALUES 
            (
                OLD.id, OLD.code_ligne, OLD.societe_code, OLD.soussociete_code, OLD.depot_code, OLD.client_code, OLD.user_code, OLD.commercial_code, 
                OLD.date_mouvemenet, OLD.entetcommercial_code, OLD.caisse_code, OLD.cloture, OLD.entetcommercial_numero, OLD.mouvement_libelle, 
                OLD.montant, OLD.sens_ecriture, OLD.date_echeance, OLD.payement_code, OLD.marque_code, OLD.isactif, OLD.commentaire, OLD.wavesoft,
                OLD.cree_par, OLD.deleted_at, OLD.created_at, NOW()
            );             
        END;

DELIMITER $$;
CREATE TRIGGER on_delete_paiements
AFTER DELETE ON paiements
    FOR EACH ROW 
        BEGIN
            -- when delete add visite line to deleted_paiements
            INSERT INTO `deleted_paiements`
            (
                `id`, `code_ligne`, `commercial_code`, `client_code`, `depot_code`, `cloture`, `societe_code`, `soussociete_code`, `visite_code`, 
                `bl_code`, `code_bl`, `caisse_code`, `recouvrement_code`, `blRecouvreCode`, `montant`, `espece`, `cheque`, `num_cheque`, `banque_cheque`, 
                `traite`, `num_traite`, `banque_traite`, `porteur_cheque`, `credit`, `fidelite`, `rs`, `paiement_instance`, `montant_avoir`, `num_avoir`, 
                `montantTicket`, `nombreTicket`, `idTicket`, `virement`, `date_echeance_cheque`, `garantie`, `date_echeance_traite`, `date_echeance_credit`, 
                `recouvrement`, `commentaire`, `date`, `wavesoft`, `tolerance`, `cree_par`, `deleted_at`, `created_at`, `updated_at`, `device_code`, `impaye`
            ) VALUES 
            (
                OLD.id, OLD.code_ligne, OLD.commercial_code, OLD.client_code, OLD.depot_code, OLD.cloture, OLD.societe_code, OLD.soussociete_code, OLD.visite_code, 
                OLD.bl_code, OLD.code_bl, OLD.caisse_code, OLD.recouvrement_code, OLD.blRecouvreCode, OLD.montant, OLD.espece, OLD.cheque, OLD.num_cheque, OLD.banque_cheque, 
                OLD.traite, OLD.num_traite, OLD.banque_traite, OLD.porteur_cheque, OLD.credit, OLD.fidelite, OLD.rs, OLD.paiement_instance, OLD.montant_avoir, OLD.num_avoir, 
                OLD.montantTicket, OLD.nombreTicket, OLD.idTicket, OLD.virement, OLD.date_echeance_cheque, OLD.garantie, OLD.date_echeance_traite, OLD.date_echeance_credit, 
                OLD.recouvrement, OLD.commentaire, OLD.date, OLD.wavesoft, OLD.tolerance, OLD.cree_par, OLD.deleted_at, OLD.created_at, NOW(), OLD.device_code, OLD.impaye
            );             
        END;



