from AppliDef import *


def GrabLogo(CanvasLogo, PathLogo):
    x = CanvasLogo.winfo_rootx()
    y = CanvasLogo.winfo_rooty()
    w = CanvasLogo.winfo_width()
    h = CanvasLogo.winfo_height()
    image = ImageGrab.grab((x, y, x + w, y + h))
    image.save(PathLogo)


class FrmAjoutFour(tk.Toplevel):
    def __init__(self, container, RatioX, RatioY, Appli):
        tk.Toplevel.__init__(self, container)
        self.MainAppli = Appli
        self.parent = container
        self.RatioX = RatioX
        self.RatioY = RatioY
        self.PathFilejpgToOpen = ''
        self.CanvasLogo = None
        self.ImgLogo = None
        self.Logo = ''
        self.Web = ''
        self.Capture = ''
        self.ImgCaptWeb = None
        self.window = None
        self.t = None
        self.MenuClicDroitImgWeb = None
        self.screen = None
        self.NContact = 0
        self.ActionModifNom = False

        screen = ctypes.windll.user32
        FrmFond = Image.open('Media/FrmAjout/Fond/Fond.jpg')
        self.SX = int((float(FrmFond.size[0]) * float(self.RatioX)))
        self.SY = int((float(FrmFond.size[1]) * float(self.RatioY)))

        self.ImgFrmFond = []
        self.ImgFrmFond = RedimImg(self.SX, self.SY, 'Media/FrmAjout/Fond/')
        self.CanvasFrmFond = CanvasApp(self, width=self.SX, height=self.SY)
        self.ImgCanvasFrmFond = self.CanvasFrmFond.create_image(0, 0, anchor=tk.NW, image=self.ImgFrmFond[0])

        self.geometry(str(self.SX) + "x" + str(self.SY) + "+" + str(int(screen.GetSystemMetrics(0) * 0.002)) + "+" +
                      str(int(screen.GetSystemMetrics(1) * 0.031)))
        self.resizable(width=False, height=False)
        self.overrideredirect(True)
        self.lift()

        self.ImgBtnClose, wsize, hsize, self.CanvasBtnClose, self.ImgCanvasBtnClose = CanInWindow(
            'Media/FrmAjout/BtnClose/', self.RatioX, self.RatioY, self.CanvasFrmFond, tk.CENTER,
            int(self.SX * 0.988), int(self.SY * 0.014), 0)
        ChangeCursor(self.CanvasBtnClose)
        self.CanvasBtnClose.bind('<Motion>',
                                 lambda action: self.CanvasBtnClose.itemconfig(self.ImgCanvasBtnClose,
                                                                               image=self.ImgBtnClose[1]))
        self.CanvasBtnClose.bind('<ButtonRelease-1>', self.AnnulFrmAjout)

        if self.MainAppli.BaseEnCours == "CHIMIE":
            Type = 'chimie'
        else:
            Type = 'physique'

        if self.MainAppli.ModifEnCours:
            textlabelTag = 'Modifier un fournisseur de ' + Type
        else:
            textlabelTag = 'Ajouter un fournisseur de ' + Type

        coefY = float(ConfigResolution(self.MainAppli.Nconfig, 'YTitreFrm', self.MainAppli.curSave))
        self.LabelTag = LabelInCanvas(self.CanvasFrmFond, textlabelTag, '#E0E0E0',
                                      AppliFont('Helvetica', self.MainAppli.SizeFont, 'normal', 0),
                                      int(self.SX * 0.005), int(self.SY * coefY), tk.NW)

        self.CanvasMenu1 = CanvasMenu(self.CanvasFrmFond, 'Media/FrmAjout/Menu/', int(self.SX * 0.665),
                                      int(self.SY * 0.967), int(self.SX * 0.003), int(self.SY * 0.03), tk.NW)
        LabelInCanvas(self.CanvasMenu1.CanvasMenu, 'Site Web du fournisseur', '#E0E0E0',
                      AppliFont('Helvetica', self.MainAppli.SizeFont, 'normal', 0),
                      int(int(self.CanvasMenu1.CanvasMenu['width']) * 0.01),
                      int(int(self.CanvasMenu1.CanvasMenu['height']) * 0.005), tk.NW)

        self.CanvasWeb = CanvasInWindow('Media/FrmFour/Web/', self.RatioX * 0.88, self.RatioY * 0.865,
                                        self.CanvasMenu1.CanvasMenu, tk.CENTER,
                                        int(int(self.CanvasMenu1.CanvasMenu['width']) * 0.5),
                                        int(int(self.CanvasMenu1.CanvasMenu['height']) * 0.512), 0)

        self.ImgWeb = ImgInCanvas(self.CanvasWeb.CanvasListIma, 'Datas/Fournisseurs/Captures/NoWeb', 0.4, tk.CENTER,
                                  int(int(self.CanvasWeb.CanvasListIma['width']) * 0.5),
                                  int(int(self.CanvasWeb.CanvasListIma['height']) * 0.5))
        self.ActivImgWeb()

        self.EntryWeb = EntryData(self.CanvasWeb.CanvasListIma, int(self.CanvasWeb.wsize * 0.121),
                                  int(self.CanvasWeb.wsize * 0.017),
                                  int(self.CanvasWeb.hsize * 0.018), tk.W,
                                  AppliFont('Helvetica', self.MainAppli.SizeFont + 1, 'normal', 0))
        self.EntryWeb.EntryData.config(bd=0, bg='#F1F3F4')
        self.EntryWeb.EntryData.focus_set()
        self.EntryWeb.EntryData.bind('<Return>', self.ChargeWeb)

        self.CanvasMenu2 = CanvasMenu(self.CanvasFrmFond, 'Media/FrmAjout/Menu/', int(self.SX * 0.33),
                                      int(self.SY * 0.967), int(self.SX * 0.998), int(self.SY * 0.03), tk.NE)

        LabelInCanvas(self.CanvasMenu2.CanvasMenu, 'Informations sur le fournisseur', '#E0E0E0',
                      AppliFont('Helvetica', self.MainAppli.SizeFont, 'normal', 0),
                      int(int(self.CanvasMenu2.CanvasMenu['width']) * 0.01),
                      int(int(self.CanvasMenu2.CanvasMenu['height']) * 0.005), tk.NW)

        self.CanvasDescript = CanvasInWindow('Media/FrmInfo/FondDescriptMat/', self.RatioX * 1.65, self.RatioY * 1.65,
                                             self.CanvasMenu2.CanvasMenu, tk.CENTER,
                                             int(int(self.CanvasMenu2.CanvasMenu['width']) * 0.5),
                                             int(int(self.CanvasMenu2.CanvasMenu['height']) * 0.5), 0)
        self.ImgFournisseur = ImgInCanvas(self.CanvasDescript.CanvasListIma, 'Datas/Fournisseurs/Logos/VIDE', 0.5,
                                          tk.CENTER, int(int(self.CanvasDescript.CanvasListIma['width']) * 0.5),
                                          int(int(self.CanvasDescript.CanvasListIma['height']) * 0.18))

        self.NomLogo = self.ImgFournisseur.CanvasImg.create_text(
            int(int(self.ImgFournisseur.CanvasImg['width']) * 0.5),
            int(int(self.ImgFournisseur.CanvasImg['height']) * 0.5), text='Logo \n Nouveau fournisseur',
            font=AppliFont('Helvetica', self.MainAppli.SizeFont + 4, 'bold', 0), anchor=tk.CENTER, justify=tk.CENTER)
        self.ImgFournisseur.CanvasImg.addtag_withtag('NomLogo', self.NomLogo)
        ChangeCursor(self.ImgFournisseur.CanvasImg)
        self.ImgFournisseur.CanvasImg.bind('<Button-1>', self.ChargeLogo)

        self.MenuImgFour = tk.Menu(self.ImgFournisseur.CanvasImg, tearoff=0)
        self.MenuImgFour.add_command(label="Recherche internet", command=self.LogoChercheWeb)
        self.MenuImgFour.add_separator()
        self.MenuImgFour.add_command(label="Coller une image", command=self.ColleLogoFour)
        self.ImgFournisseur.CanvasImg.bind('<Button-3>', self.AffichMenuClicDroitLogo)

        self.CanvasDescript.CanvasListIma.create_text(
            int(self.CanvasDescript.wsize * 0.08), int(self.CanvasDescript.hsize * 0.35), text='Contact:',
            font=AppliFont('Helvetica', self.MainAppli.SizeFont + 2, 'bold', 1), anchor=tk.W)

        self.ListEntryContact = []
        self.ListEntryData = ['Nom du fournisseur', 'Nom du commercial', 'Adresse (complément)', 'Adresse',
                              'Code postal', 'Tel', 'Fax', 'Email']
        self.ListMenuClicDroitContact = []
        for i in range(8):
            self.ListEntryContact.append(
                EntryData(self.CanvasDescript.CanvasListIma, int(self.CanvasDescript.wsize * 0.105),
                          int(self.CanvasDescript.wsize * 0.1),
                          int(self.CanvasDescript.hsize * 0.4 + i * self.CanvasDescript.hsize * 0.035), tk.W,
                          AppliFont('Helvetica', self.MainAppli.SizeFont + 1, 'normal', 0)))
            self.ListEntryContact[i].EntryData.config(justify='left')
            self.ListEntryContact[i].Data.set(self.ListEntryData[i])
            self.ListMenuClicDroitContact.append(tk.Menu(self.ListEntryContact[i].EntryData, tearoff=0))
            self.ListMenuClicDroitContact[i].add_command(label="Recherche internet", command=self.ChercheWebFour)

        self.ListEntryContact[0].EntryData.bind('<KeyRelease>', self.ModifNomFour)

        self.CanvasDescript.CanvasListIma.create_text(
            int(self.CanvasDescript.wsize * 0.08), int(self.CanvasDescript.hsize * 0.73), text='Commandes:',
            font=AppliFont('Helvetica', self.MainAppli.SizeFont + 2, 'bold', 1), anchor=tk.W)

        self.CanvasDescript.CanvasListIma.create_text(
            int(self.CanvasDescript.wsize * 0.1), int(self.CanvasDescript.hsize * 0.78), text='Frais de port (€ HT):',
            font=AppliFont('Helvetica', self.MainAppli.SizeFont + 2, 'normal', 0), anchor=tk.W)

        self.EntryFP = EntryData(self.CanvasDescript.CanvasListIma, int(self.CanvasDescript.wsize * 0.05),
                                 int(self.CanvasDescript.wsize * 0.55),
                                 int(self.CanvasDescript.hsize * 0.78), tk.W,
                                 AppliFont('Helvetica', self.MainAppli.SizeFont + 1, 'normal', 0))
        self.ListEntryContact.append(self.EntryFP)
        self.ListMenuClicDroitContact.append(tk.Menu(self.EntryFP.EntryData, tearoff=0))
        self.ListMenuClicDroitContact[8].add_command(label="Recherche internet", command=self.ChercheWebFour)

        self.CanvasDescript.CanvasListIma.create_text(
            int(self.CanvasDescript.wsize * 0.1), int(self.CanvasDescript.hsize * 0.82),
            text='Limite Franco de port (€ HT):',
            font=AppliFont('Helvetica', self.MainAppli.SizeFont + 2, 'normal', 0), anchor=tk.W)

        self.EntryFranco = EntryData(self.CanvasDescript.CanvasListIma, int(self.CanvasDescript.wsize * 0.05),
                                     int(self.CanvasDescript.wsize * 0.55),
                                     int(self.CanvasDescript.hsize * 0.82), tk.W,
                                     AppliFont('Helvetica', self.MainAppli.SizeFont + 1, 'normal', 0))
        self.ListEntryContact.append(self.EntryFranco)
        self.ListMenuClicDroitContact.append(tk.Menu(self.EntryFranco.EntryData, tearoff=0))
        self.ListMenuClicDroitContact[9].add_command(label="Recherche internet", command=self.ChercheWebFour)

        self.ListEntryContact.append(self.CanvasDescript.CanvasListIma)
        self.ListMenuClicDroitContact.append(tk.Menu(self.CanvasDescript.CanvasListIma, tearoff=0))
        self.ListMenuClicDroitContact[10].add_command(label="Recherche internet", command=self.ChercheWebFour)
        for i in range(11):
            def GestEvt(evt, j=i):
                return self.AffichMenuClicDroitContact(evt, j)

            if i != 10:
                self.ListEntryContact[i].EntryData.bind("<Button-3>", GestEvt)
            else:
                self.ListEntryContact[i].bind("<Button-3>", GestEvt)

        self.CanvasDescript.CanvasListIma.create_text(
            int(self.CanvasDescript.wsize * 0.08), int(self.CanvasDescript.hsize * 0.9), text='Catégorie:',
            font=AppliFont('Helvetica', self.MainAppli.SizeFont + 2, 'bold', 1), anchor=tk.W)

        self.EntryCat = EntryData(self.CanvasDescript.CanvasListIma, int(self.CanvasDescript.wsize * 0.105),
                                  int(self.CanvasDescript.wsize * 0.1),
                                  int(self.CanvasDescript.hsize * 0.95), tk.W,
                                  AppliFont('Helvetica', self.MainAppli.SizeFont + 1, 'normal', 0))
        self.EntryCat.EntryData.config(justify='left')

        self.BtnOK = BtnAction(self.CanvasFrmFond, int(self.SX * 0.962), int(self.SY * 0.972), self.RatioX * 0.15,
                               self.RatioX * 0.15, 'Media/FrmAjout/BtnOK/')
        self.BtnAnnul = BtnAction(self.CanvasFrmFond, int(self.SX * 0.985), int(self.SY * 0.972), self.RatioX * 0.15,
                                  self.RatioX * 0.15, 'Media/FrmAjout/BtnAnnul/')
        ChangeCursor(self.BtnAnnul.CanvasBtn)
        ChangeCursor(self.BtnOK.CanvasBtn)
        self.BtnAnnul.CanvasBtn.bind('<ButtonRelease-1>', self.AnnulFrmAjout)
        self.BtnOK.CanvasBtn.bind('<ButtonRelease-1>', self.SaveNewFour)

        if self.MainAppli.NAction != 603:
            self.AffichInfoFour()
            self.TextAction = 'Mise à jour - fournisseur - '
        else:
            self.TextAction = 'Ajout - fournisseur - '

        self.CanvasFrmFond.focus_set()
        self.CanvasFrmFond.bind('<Motion>', self.MoveOnCanvas)

        self.CanvasFrmFond.pack()

    def AffichInfoFour(self):
        ListIndex = [0, 12, 1, 2, 3, 4, 10, 11]
        Four = []
        if self.MainAppli.NAction == 6:
            Four = self.MainAppli.ListFours[self.MainAppli.IndexElt]
        elif self.MainAppli.NAction == 6011:
            Four = self.MainAppli.ListRecherch[self.MainAppli.IndexElt]
        elif self.MainAppli.NAction == 6022:
            Four = self.MainAppli.ListFourCat[self.MainAppli.IndexElt]
        for i in range(8):
            if Four[ListIndex[i]] is not None:
                self.ListEntryContact[i].Data.set(Four[ListIndex[i]])
            else:
                self.ListEntryContact[i].Data.set('')
        self.EntryFP.Data.set(Four[5])
        self.EntryFranco.Data.set(Four[6])
        if Four[7] != '' and Four[7] is not None and os.path.exists('Datas/Fournisseurs/Logos/' + Four[7]):
            self.Logo = Four[7]
            self.ImgFournisseur.CanvasImg.destroy()
            self.ImgLogo = ImgInCanvas(self.CanvasDescript.CanvasListIma, 'Datas/Fournisseurs/Logos/' + self.Logo,
                                       0.5, tk.CENTER, int(int(self.CanvasDescript.CanvasListIma['width']) * 0.5),
                                       int(int(self.CanvasDescript.CanvasListIma['height']) * 0.18))
            ChangeCursor(self.ImgLogo.CanvasImg)
            self.ImgLogo.CanvasImg.bind('<Button-1>', self.ChargeLogo)
            self.MenuImgFour = tk.Menu(self.ImgLogo.CanvasImg, tearoff=0)
            self.MenuImgFour.add_command(label="Recherche internet", command=self.LogoChercheWeb)
            self.MenuImgFour.add_separator()
            self.MenuImgFour.add_command(label="Coller une image", command=self.ColleLogoFour)
            self.MenuImgFour.add_command(label="Supprimer l'image", command=self.SupLogoFour)
            self.ImgLogo.CanvasImg.bind('<Button-3>', self.AffichMenuClicDroitLogo)
        else:
            if len(Four[0]) < 15:
                TextFournisseur = Four[0]
            else:
                TextFournisseur = Four[0].split(' ')[0]
                for i in range(1, len(Four[0].split(' '))):
                    TextFournisseur = TextFournisseur + '\n' + Four[0].split(' ')[i]
            self.ImgFournisseur.CanvasImg.itemconfigure('NomLogo', text=TextFournisseur)
        if Four[8] != '' and Four[8] is not None:
            self.Web = Four[8]
            self.EntryWeb.Data.set(Four[8])
        if Four[9] != '' and Four[9] is not None and \
                os.path.exists('Datas/Fournisseurs/Captures/' + Four[9]):
            self.Capture = Four[9]
            self.ImgWeb.CanvasImg.destroy()
            self.ImgWeb = ImgInCanvas(self.CanvasWeb.CanvasListIma, 'Datas/Fournisseurs/Captures/' +
                                      self.Capture, 0.98, tk.CENTER,
                                      int(int(self.CanvasWeb.CanvasListIma['width']) * 0.5),
                                      int(int(self.CanvasWeb.CanvasListIma['height']) * 0.5))
            self.ActivImgWeb()
        if Four[13] is not None:
            self.EntryCat.Data.set(Four[13])

    def AffichMenuClicDroitImgWeb(self, event):
        self.MenuClicDroitImgWeb.post(event.x_root, event.y_root)

    def ActivImgWeb(self):
        ChangeCursor(self.ImgWeb.CanvasImg)
        self.ImgWeb.CanvasImg.bind('<Button-1>', self.ChargeWeb)
        self.MenuClicDroitImgWeb = tk.Menu(self.ImgWeb.CanvasImg, tearoff=0)
        self.MenuClicDroitImgWeb.add_command(label="Capture page accueil site Web", command=self.ChargeCaptWeb)
        if self.Capture !='':
            self.MenuClicDroitImgWeb.add_separator()
            self.MenuClicDroitImgWeb.add_command(label="Supprimer capture page accueil site Web",
                                                 command=self.SupCaptWeb)
        self.ImgWeb.CanvasImg.bind("<Button-3>", self.AffichMenuClicDroitImgWeb)

    def ChargeWeb(self, _):
        if self.EntryWeb.Data.get() != '':
            Adresse = self.EntryWeb.Data.get()
            self.Web = self.EntryWeb.Data.get()
        else:
            Adresse = 'https://www.google.fr/search?q=' + self.ListEntryContact[0].Data.get()
        if Adresse != '':
            webbrowser.open(Adresse, new=1)

    def ChargeCaptWeb(self):
        if self.EntryWeb.Data.get() != '':
            Adresse = self.EntryWeb.Data.get()
        else:
            Adresse = 'https://www.google.fr/search?q=' + self.ListEntryContact[0].Data.get()
        if Adresse != '':
            self.window = webview.create_window(Adresse, Adresse)
            webview.start(self.resize, self.window)
            self.window.events.closed += self.on_closed()
            self.window.events.closing += self.on_closing()

    def SupCaptWeb(self):
        self.ImgWeb.CanvasImg.destroy()
        self.ImgWeb = ImgInCanvas(self.CanvasWeb.CanvasListIma, 'Datas/Fournisseurs/Captures/NoWeb', 0.4, tk.CENTER,
                                  int(int(self.CanvasWeb.CanvasListIma['width']) * 0.5),
                                  int(int(self.CanvasWeb.CanvasListIma['height']) * 0.5))
        self.Capture = ''
        self.ActivImgWeb()

    def resize(self, window):
        self.screen = ctypes.windll.user32
        window.move(-10, -5)
        window.resize(int(self.screen.GetSystemMetrics(0) * 1.05), int(self.screen.GetSystemMetrics(1) * 1.05))

    def CloseWeb(self):
        self.MainAppli.parent.focus_force()
        self.MainAppli.FrmLift()
        self.focus_force()

    def on_closing(self):
        self.t = threading.Thread(target=self.CloseWeb)
        self.t.start()

    def on_closed(self):
        self.GrabWeb()
        self.CloseWeb()

    def GrabWeb(self):
        screen = ctypes.windll.user32
        img = ImageGrab.grabclipboard()
        if img is not None:
            self.ImgCaptWeb = img.crop((0, 25, int(screen.GetSystemMetrics(0)), int(screen.GetSystemMetrics(1)) - 39))

            self.ImgCaptWeb.save('Datas/Fournisseurs/Captures/' + self.ListEntryContact[0].Data.get() + '.bmp')
            if os.path.exists('Datas/Fournisseurs/Captures/' + self.ListEntryContact[0].Data.get()):
                os.remove('Datas/Fournisseurs/Captures/' + self.ListEntryContact[0].Data.get())
            os.rename('Datas/Fournisseurs/Captures/' + self.ListEntryContact[0].Data.get() + '.bmp',
                      'Datas/Fournisseurs/Captures/' + self.ListEntryContact[0].Data.get())

            self.ImgWeb.CanvasImg.destroy()
            self.ImgWeb = ImgInCanvas(self.CanvasWeb.CanvasListIma, 'Datas/Fournisseurs/Captures/' +
                                      self.ListEntryContact[0].Data.get(), 0.98, tk.CENTER,
                                      int(int(self.CanvasWeb.CanvasListIma['width']) * 0.5),
                                      int(int(self.CanvasWeb.CanvasListIma['height']) * 0.5))
            self.Capture = self.ListEntryContact[0].Data.get()
            self.ActivImgWeb()

    def ModifNomFour(self, _):
        self.ActionModifNom = True
        if self.PathFilejpgToOpen == '' and self.Logo == '':
            if len(self.ListEntryContact[0].Data.get()) < 15:
                TextFournisseur = self.ListEntryContact[0].Data.get()
            else:
                TextFournisseur = self.ListEntryContact[0].Data.get().split(' ')[0]
                for i in range(1, len(self.ListEntryContact[0].Data.get().split(' '))):
                    TextFournisseur = TextFournisseur + '\n' + self.ListEntryContact[0].Data.get().split(' ')[i]
            self.ImgFournisseur.CanvasImg.itemconfigure('NomLogo', text=TextFournisseur)

    def ChargeLogo(self, _):
        self.PathFilejpgToOpen = ''
        self.PathFilejpgToOpen = tkfiledialog.askopenfilename(title="Ouvrir un fichier",
                                                              filetypes=[('JPG files', '*.*')], parent=self)
        if self.PathFilejpgToOpen != '':
            self.AffichLogo(self.PathFilejpgToOpen)

        self.lift()

    def AffichLogo(self, File):
        if self.ImgLogo is None:
            self.ImgFournisseur.CanvasImg.destroy()
        else:
            self.ImgLogo.CanvasImg.destroy()
            if self.MainAppli.ModifEnCours and self.CanvasLogo is not None:
                self.CanvasLogo.CanvasListIma.destroy()

        self.CanvasLogo = CanvasInWindow('Media/FrmFour/Logo/', self.RatioX, self.RatioX,
                                         self.CanvasMenu2.CanvasMenu, tk.N,
                                         int(int(self.CanvasMenu2.CanvasMenu['width']) * 0.5),
                                         int(int(self.CanvasMenu2.CanvasMenu['height']) * 0.16), 0)
        self.ImgLogo = ImgInCanvas(self.CanvasLogo.CanvasListIma, File, 0.93, tk.CENTER,
                                   int(int(self.CanvasLogo.CanvasListIma['width']) * 0.5),
                                   int(int(self.CanvasLogo.CanvasListIma['height']) * 0.5))
        ChangeCursor(self.ImgLogo.CanvasImg)
        self.ImgLogo.CanvasImg.bind('<Button-1>', self.ChargeLogo)
        self.MenuImgFour = tk.Menu(self.ImgLogo.CanvasImg, tearoff=0)
        self.MenuImgFour.add_command(label="Recherche internet", command=self.LogoChercheWeb)
        self.MenuImgFour.add_separator()
        self.MenuImgFour.add_command(label="Coller une image", command=self.ColleLogoFour)
        self.MenuImgFour.add_command(label="Supprimer l'image", command=self.SupLogoFour)
        self.ImgLogo.CanvasImg.bind('<Button-3>', self.AffichMenuClicDroitLogo)

    def SaveNewFour(self, _):
        if self.ListEntryContact[0].Data.get() != '':
            NOcur = 0
            Rep = True
            if self.MainAppli.BaseEnCours == "CHIMIE":
                self.MainAppli.cur.execute("Select * from Fournisseurs where NOM = '" +
                                           self.ListEntryContact[0].Data.get() + "'")
                NOcur = len(self.MainAppli.cur.fetchall())
            elif self.MainAppli.BaseEnCours == "PHYS":
                self.MainAppli.curphys.execute("Select * from Fournisseurs where NOM = '" +
                                               self.ListEntryContact[0].Data.get() + "'")
                NOcur = len(self.MainAppli.curphys.fetchall())

            if NOcur > 0:
                Rep = tkMB.askyesno('Attention', 'Un fournisseur nommé "' + self.ListEntryContact[0].Data.get()
                                    + '" est déja présent dans la base de données, souhaitez-vous mettre à jour '
                                      'les données?', parent=self)
            if Rep:
                if NOcur > 0:
                    SQLRequet = "DELETE FROM Fournisseurs WHERE NOM='" + self.ListEntryContact[0].Data.get() + "'"
                    if self.MainAppli.BaseEnCours == "CHIMIE":
                        self.MainAppli.cur.execute(SQLRequet)
                        self.MainAppli.DataBaseChimie.commit()
                    elif self.MainAppli.BaseEnCours == "PHYS":
                        self.MainAppli.curphys.execute(SQLRequet)
                        self.MainAppli.DataBasePhys.commit()

                NomFour = self.ListEntryContact[0].Data.get()

                if self.PathFilejpgToOpen != '':
                    self.Logo = NomFour
                    GrabLogo(self.CanvasLogo.CanvasListIma, 'Datas/Fournisseurs/Logos/' + NomFour + '.bmp')
                    if os.path.exists('Datas/Fournisseurs/Logos/' + NomFour):
                        os.remove('Datas/Fournisseurs/Logos/' + NomFour)
                    os.rename('Datas/Fournisseurs/Logos/' + NomFour + '.bmp', 'Datas/Fournisseurs/Logos/' + NomFour)

                if self.ActionModifNom:
                    if self.Logo != '':
                        if not os.path.exists('Datas/Fournisseurs/Logos/' + NomFour):
                            shutil.copy('Datas/Fournisseurs/Logos/' + self.Logo, 'Datas/Fournisseurs/Logos/' + NomFour)
                        self.Logo = NomFour
                    if self.Capture != '':
                        if not os.path.exists('Datas/Fournisseurs/Captures/' + NomFour):
                            shutil.copy('Datas/Fournisseurs/Captures/' + self.Capture,
                                        'Datas/Fournisseurs/Captures/' + NomFour)
                        self.Capture = NomFour

                if self.EntryWeb.Data.get() != '':
                    self.Web = self.EntryWeb.Data.get()

                data = [(NomFour, self.ListEntryContact[2].Data.get(), self.ListEntryContact[3].Data.get(),
                         self.ListEntryContact[4].Data.get(), self.ListEntryContact[5].Data.get(),
                         self.EntryFP.Data.get(), self.EntryFranco.Data.get(), self.Logo, self.Web, self.Capture,
                         self.ListEntryContact[6].Data.get(), self.ListEntryContact[7].Data.get(),
                         self.ListEntryContact[1].Data.get(), self.EntryCat.Data.get())]
                if self.MainAppli.BaseEnCours == "CHIMIE":
                    self.MainAppli.cur.execute(
                        "INSERT INTO Fournisseurs(" + self.MainAppli.ListChampsTabFournisseurs +
                        ") VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?)", data[0])

                    self.MainAppli.DataBaseChimie.commit()
                    self.MainAppli.SaveListModifDTB('Chimie', self.TextAction + NomFour)
                    self.MainAppli.cur.execute("select " + self.MainAppli.ListChampsTabFournisseurs +
                                               " from Fournisseurs order by NOM")
                    self.MainAppli.ListFours = self.MainAppli.cur.fetchall()
                elif self.MainAppli.BaseEnCours == "PHYS":
                    self.MainAppli.curphys.execute(
                        "INSERT INTO Fournisseurs(" + self.MainAppli.ListChampsTabFournisseurs +
                        ") VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?)", data[0])
                    self.MainAppli.DataBasePhys.commit()
                    self.MainAppli.SaveListModifDTB('Physique', self.TextAction + NomFour)
                    self.MainAppli.curphys.execute("select " + self.MainAppli.ListChampsTabFournisseurs +
                                                   " from Fournisseurs order by NOM")
                    self.MainAppli.ListFours = self.MainAppli.curphys.fetchall()

                if self.Logo == '':
                    if os.path.exists('Datas/Fournisseurs/Logos/' + NomFour):
                        os.remove('Datas/Fournisseurs/Logos/' + NomFour)

                if self.Capture == '':
                    if os.path.exists('Datas/Fournisseurs/Captures/' + NomFour):
                        os.remove('Datas/Fournisseurs/Captures/' + NomFour)

                if self.MainAppli.NAction == 604:
                    if self.MainAppli.OldNAction == 6:
                        self.MainAppli.ChargeListProduits(self.MainAppli.ListFours)
                        self.MainAppli.AffichEtagProduits(self.MainAppli.ListFours)
                    elif self.MainAppli.OldNAction == 6011:
                        self.MainAppli.NAction = 601
                        self.MainAppli.ActionRecherche()
                    elif self.MainAppli.OldNAction == 6022:
                        self.MainAppli.AffichListFourCat(self.MainAppli.NSCatselected)
                else:
                    self.MainAppli.ChargeListProduits(self.MainAppli.ListFours)
                    self.MainAppli.AffichEtagProduits(self.MainAppli.ListFours)

                self.MainAppli.IndexElt = self.MainAppli.ListNomProd.index(NomFour)
                self.MainAppli.FrmFour.SelectFour(self.MainAppli.IndexElt)

                self.CacheFrmAjout()
            else:
                self.lift()
        else:
            tkMB.showwarning('Attention', "Veuillez indiquer le nom du fournisseur!", parent=self)
            self.lift()

    def LogoChercheWeb(self):
        webbrowser.open('https://www.google.fr/search?q= Logo - ' + self.ListEntryContact[0].EntryData.get(), new=1)

    def AffichMenuClicDroitLogo(self, event):
        self.MenuImgFour.post(event.x_root, event.y_root)

    def AffichMenuClicDroitContact(self, event, j):
        self.ListMenuClicDroitContact[j].post(event.x_root, event.y_root)
        self.NContact = j

    def ColleLogoFour(self):
        img = ImageGrab.grabclipboard()
        if img is not None:
            self.PathFilejpgToOpen = 'Datas/Temp/Logo.png'
            img.save(self.PathFilejpgToOpen)
            self.AffichLogo(self.PathFilejpgToOpen)

    def SupLogoFour(self):
        self.ImgLogo.CanvasImg.destroy()
        self.ImgLogo = None
        if self.CanvasLogo is not None:
            self.CanvasLogo.CanvasListIma.destroy()
        self.ImgFournisseur = ImgInCanvas(self.CanvasDescript.CanvasListIma, 'Datas/Fournisseurs/Logos/VIDE', 0.5,
                                          tk.CENTER, int(int(self.CanvasDescript.CanvasListIma['width']) * 0.5),
                                          int(int(self.CanvasDescript.CanvasListIma['height']) * 0.18))

        self.NomLogo = self.ImgFournisseur.CanvasImg.create_text(
            int(int(self.ImgFournisseur.CanvasImg['width']) * 0.5),
            int(int(self.ImgFournisseur.CanvasImg['height']) * 0.5), text=self.ListEntryContact[0].EntryData.get(),
            font=AppliFont('Helvetica', self.MainAppli.SizeFont + 4, 'bold', 0), anchor=tk.CENTER, justify=tk.CENTER)
        self.ImgFournisseur.CanvasImg.addtag_withtag('NomLogo', self.NomLogo)
        ChangeCursor(self.ImgFournisseur.CanvasImg)
        self.ImgFournisseur.CanvasImg.bind('<Button-1>', self.ChargeLogo)

        self.MenuImgFour = tk.Menu(self.ImgFournisseur.CanvasImg, tearoff=0)
        self.MenuImgFour.add_command(label="Recherche internet", command=self.LogoChercheWeb)
        self.MenuImgFour.add_separator()
        self.MenuImgFour.add_command(label="Coller une image", command=self.ColleLogoFour)
        self.ImgFournisseur.CanvasImg.bind('<Button-3>', self.AffichMenuClicDroitLogo)
        self.PathFilejpgToOpen = ''
        self.Logo = ''

    def ChercheWebFour(self):
        InfoFourList = ['Contact', 'Contact', 'Adresse', 'Adresse', 'Adresse', 'Téléphone', 'Fax', 'Email',
                        'Frais de port', 'Franco de port', 'Informations']
        webbrowser.open('https://www.google.fr/search?q=' + self.ListEntryContact[0].EntryData.get() + ' - ' +
                        InfoFourList[self.NContact], new=1)

    def MoveOnCanvas(self, _):
        self.CanvasBtnClose.itemconfig(self.ImgCanvasBtnClose, image=self.ImgBtnClose[0])
        self.MainAppli.CanvasBtnQuit.itemconfig(self.MainAppli.ImgCanvasBtnQuit, image=self.MainAppli.ImgBtnQuit[0])

    def AnnulFrmAjout(self, _):
        if self.ImgCaptWeb is not None and not self.MainAppli.ModifEnCours and \
                os.path.exists('Datas/Fournisseurs/Captures/' + self.Capture):
            os.remove('Datas/Fournisseurs/Captures/' + self.Capture)
        self.CacheFrmAjout()

    def CacheFrmAjout(self):
        self.CanvasBtnClose.itemconfig(self.ImgCanvasBtnClose, image=self.ImgBtnClose[0])
        self.MainAppli.ListBtnActionV[2].CanvasBtn.itemconfig(self.MainAppli.ListBtnActionV[2].ImgCanvasBtn,
                                                              image=self.MainAppli.ListBtnActionV[2].ImgBtn[0])
        self.destroy()
        if self.MainAppli.OldNAction != 0:
            self.MainAppli.NAction = self.MainAppli.OldNAction
        else:
            self.MainAppli.NAction = 6
        self.MainAppli.MAJInfo()
        self.MainAppli.parent.lift()
        self.MainAppli.FrmLift()
        self.MainAppli.ModifEnCours = False
