-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclsDatenbank.vb
1417 lines (1111 loc) · 62.8 KB
/
clsDatenbank.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Imports System.Data
Imports System.Data.SqlClient
Imports System.Net
Imports System.IO
Public Class clsDatenbank
Public strConnectionString As String
Public strConnectionString_eazybusiness As String = ""
Public iKID As Integer = -1
'##########################################################
'# >> Verbindung aufbauen + Connection String
'##########################################################
Public Async Function getDBConnect(ByVal strConnection As String, Optional bEazybusiness As Boolean = False) As Threading.Tasks.Task(Of Boolean)
Dim sqlConn As New SqlConnection(strConnection)
Try
If strConnection.IndexOf("eazybusiness") > 0 Then
bEazybusiness = True
strConnectionString = strConnection
End If
Await sqlConn.OpenAsync()
'If bEazybusiness = False And strConnection.IndexOf("eazybusiness") < 0 Then
If bEazybusiness = False Then
strConnectionString = strConnection
Else
strConnectionString_eazybusiness = strConnection
'strConnectionString = strConnection
End If
sqlConn.Close()
Return True
Catch ex As Exception
MessageBox.Show(ex.Message & vbCrLf & ex.StackTrace, "Fehler bei Verbindung getDBConnect()", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return False
End Try
End Function
'##########################################################
'# >> setConnectionString
'# Datenbankverbindungsstring setzen
'##########################################################
Public Function setConnectionString(strConnection As String, bMainDB As Boolean) As String
Try
If bMainDB = True Then
strConnectionString_eazybusiness = strConnection
Else
strConnectionString = strConnection
End If
Return "1"
Catch ex As Exception
MessageBox.Show("Fehler bei setConnectionString: " & ex.Message & vbCrLf & ex.StackTrace, "setConnectionString()", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Return "0"
End Try
End Function
'##########################################################
'# >> Existiert der Mandant welcher eingelesen wird
'##########################################################
Public Async Function chkMandantExists(strMandant_db As String) As Threading.Tasks.Task(Of Boolean)
Dim sqlConn As New SqlConnection(strConnectionString_eazybusiness)
Try
Await sqlConn.OpenAsync()
Dim sqlComm As New SqlCommand("USE " & strMandant_db, sqlConn)
Await sqlComm.ExecuteNonQueryAsync()
sqlConn.Close()
Return True
Catch ex As Exception
Return False
End Try
End Function
'##########################################################
'# >> chkMandantPosition
'# Mandanten Position innerhalb der tMandant bestimmen
'##########################################################
Public Async Function chkMandantPosition(strMandantDBName As String) As Threading.Tasks.Task(Of Integer)
Try
Dim sqlConn As New SqlConnection(strConnectionString_eazybusiness)
Dim iCount As Byte = 0
Await sqlConn.OpenAsync()
Dim sqlComm As New SqlCommand("SELECT * FROM tMandant WHERE cDB='" & strMandantDBName & "' ORDER BY cNAME ASC", sqlConn)
Dim r As SqlDataReader = Await sqlComm.ExecuteReaderAsync()
While Await r.ReadAsync()
If Await chkMandantExists(r("cdb").ToString) = True Then
If strMandantDBName = r("cDB").ToString Then
My.Settings.mandant_position = r("kMandant").ToString
My.Settings.Save()
Exit While
End If
End If
iCount += 1
End While
Return My.Settings.mandant_position
Catch ex As Exception
MessageBox.Show(ex.Message & vbCrLf & ex.StackTrace, "Fehler bei chkMandantPosition abrufen", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return -1
End Try
End Function
'##########################################################
'# >> setMandantbyCombobox
'# Mandanten in Combobox einlesen
'##########################################################
Public Async Function setMandantbyCombobox(ByVal cmbMandant As ComboBox, ByVal bAllDBs As Boolean) As Threading.Tasks.Task(Of Boolean)
Try
Dim sqlConn As New SqlConnection(strConnectionString_eazybusiness)
Await sqlConn.OpenAsync()
'# Combobox löschen
cmbMandant.Items.Clear()
Dim sqlComm As New SqlCommand("SELECT * FROM tMandant ORDER BY cNAME ASC", sqlConn)
Dim r As SqlDataReader = Await sqlComm.ExecuteReaderAsync()
While r.Read()
'# Prüfen ob Datenbank überhaupt angelegt ist
If Await chkMandantExists(r("cDB").ToString) = True Then
Dim lvwItem As New ListViewItem
'# Nur Verfügbare Datenbanken eintragen, wenn Setting gefunden wurde und bAllDbs nicht True ist
If bAllDBs = True Then
cmbMandant.Items.Add(r("cName").ToString)
Else
If frmMain.getMySettingsPositionByDatabase(r("cDB").ToString) >= 0 Then
cmbMandant.Items.Add(r("cName").ToString)
End If
End If
End If
End While
If cmbMandant.Items.Count > 0 Then
If cmbMandant.Items.Count - 1 > 0 Then
For i As Byte = 0 To cmbMandant.Items.Count - 1
If My.Settings.mandant_letzter_name = cmbMandant.Items(i) Then
cmbMandant.SelectedIndex = i
My.Settings.mandant_letzter_name = cmbMandant.Text
Exit For
End If
Next
Else
cmbMandant.SelectedIndex = 0
My.Settings.mandant_letzter_name = cmbMandant.Text
End If
End If
My.Settings.Save()
sqlConn.Close()
Return True
Catch ex As Exception
MessageBox.Show(ex.Message & vbCrLf & ex.StackTrace, "Fehler beim JTL Mandant abrufen getMandant()", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return False
End Try
End Function
Public Async Function getKundenKategorie(cmb As ComboBox) As Threading.Tasks.Task(Of Boolean)
Try
Dim sqlConn As New SqlConnection(strConnectionString_eazybusiness)
Await sqlConn.OpenAsync()
'# Combobox löschen
cmb.Items.Clear()
Dim sqlComm As New SqlCommand("SELECT * FROM tKundenKategorie ORDER BY cName ASC", sqlConn)
Dim r As SqlDataReader = Await sqlComm.ExecuteReaderAsync()
cmb.Items.Add("Alle Kundenkategorien")
While Await r.ReadAsync()
cmb.Items.Add(r("cName").ToString & " | " & Await getKundenkategorie_count(r("kKundenKategorie").ToString))
End While
sqlConn.Close()
Return True
Catch ex As Exception
MessageBox.Show(ex.Message & vbCrLf & ex.StackTrace, "Fehler beim JTL Mandant abrufen getMandant()", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return False
End Try
End Function
Public Async Function getKundenSprache_count(strSprache_id As String) As Threading.Tasks.Task(Of Integer)
Try
Dim iAnzahl As Integer = 0
Dim sqlConn As New SqlConnection(strConnectionString_eazybusiness)
Await sqlConn.OpenAsync()
Dim sqlComm As New SqlCommand("SELECT count(kKunde) as anzahl FROM tkunde WHERE kSprache='" & strSprache_id & "'", sqlConn)
Dim r As SqlDataReader = Await sqlComm.ExecuteReaderAsync()
While Await r.ReadAsync()
iAnzahl = CInt(r("anzahl").ToString)
End While
sqlConn.Close()
Return iAnzahl
Catch ex As Exception
MessageBox.Show(ex.Message & vbCrLf & ex.StackTrace, "Fehler bei getKundenSprache_count()", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return 0
End Try
End Function
Public Async Function getKundenkategorie_count(strKundenkategorie_id As String) As Threading.Tasks.Task(Of Integer)
Try
Dim iAnzahl As Integer = 0
Dim sqlConn As New SqlConnection(strConnectionString_eazybusiness)
Await sqlConn.OpenAsync()
Dim sqlComm As New SqlCommand("SELECT count(kKundenKategorie) as anzahl FROM tkunde WHERE kKundenKategorie='" & strKundenkategorie_id & "'", sqlConn)
Dim r As SqlDataReader = Await sqlComm.ExecuteReaderAsync()
While Await r.ReadAsync()
iAnzahl = CInt(r("anzahl").ToString)
End While
sqlConn.Close()
Return iAnzahl
Catch ex As Exception
MessageBox.Show(ex.Message & vbCrLf & ex.StackTrace, "Fehler bei getKundenSprache_count()", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return 0
End Try
End Function
Public Async Function getKundenGruppe_count(strKundengruppe_id As String) As Threading.Tasks.Task(Of Integer)
Try
Dim iAnzahl As Integer = 0
Dim sqlConn As New SqlConnection(strConnectionString_eazybusiness)
Await sqlConn.OpenAsync()
Dim sqlComm As New SqlCommand("SELECT count(kKunde) as anzahl FROM tkunde WHERE kKundenGruppe='" & strKundengruppe_id & "'", sqlConn)
Dim r As SqlDataReader = Await sqlComm.ExecuteReaderAsync()
While Await r.ReadAsync()
iAnzahl = CInt(r("anzahl").ToString)
End While
sqlConn.Close()
Return iAnzahl
Catch ex As Exception
MessageBox.Show(ex.Message & vbCrLf & ex.StackTrace, "Fehler bei getKundenSprache_count()", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return 0
End Try
End Function
Public Async Function getKundenOptionCount() As Threading.Tasks.Task(Of String)
Try
Dim strWhere As String = ""
If frmMain.chkNichtAmazon.Checked = True Then
strWhere = strWhere & " AND tAdresse.cMail NOT LIKE '%marketplace.amazon.%' AND tAdresse.cMail != ''"
Else
strWhere = strWhere & " AND tAdresse.cMail LIKE '%marketplace.amazon.%' AND tAdresse.cMail != ''"
End If
If Not My.Settings.jtlshop_kunde_kategorie = "" Then
strWhere = strWhere & " AND tkunde.kKundenKategorie = '" & My.Settings.jtlshop_kunde_kategorie & "'"
End If
Dim sqlConn As New SqlConnection(strConnectionString_eazybusiness)
Await sqlConn.OpenAsync()
Dim sqlComm As New SqlCommand("SELECT count(tkunde.kKunde) as anzahl FROM tkunde LEFT JOIN cubss_newsletter ON tkunde.kKunde = cubss_newsletter.kunde_id LEFT JOIN tAdresse ON tAdresse.kKunde = tkunde.kKunde WHERE cubss_newsletter.sended = 'Y'" & strWhere, sqlConn)
Dim r As SqlDataReader = Await sqlComm.ExecuteReaderAsync()
While Await r.ReadAsync()
frmMain.optNewsletterVerschickt.Text = "Verschickt (" & r("anzahl").ToString & ")"
End While
sqlConn.Close()
Dim sqlConn2 As New SqlConnection(strConnectionString_eazybusiness)
Await sqlConn2.OpenAsync()
Dim sqlComm2 As New SqlCommand("SELECT count(tkunde.kKunde) as anzahl FROM tkunde LEFT JOIN cubss_newsletter ON tkunde.kKunde = cubss_newsletter.kunde_id LEFT JOIN tAdresse ON tAdresse.kKunde = tkunde.kKunde WHERE cubss_newsletter.getnewsletter = 'Y'" & strWhere, sqlConn2)
Dim r2 As SqlDataReader = Await sqlComm2.ExecuteReaderAsync()
While Await r2.ReadAsync()
frmMain.optNewsletterAbschicken.Text = "&Abschick bereit (" & r2("anzahl").ToString & ")"
End While
sqlConn2.Close()
Dim sqlConn3 As New SqlConnection(strConnectionString_eazybusiness)
Await sqlConn3.OpenAsync()
Dim sqlComm3 As New SqlCommand("SELECT count(tkunde.kKunde) as anzahl FROM tkunde LEFT JOIN cubss_newsletter ON tkunde.kKunde = cubss_newsletter.kunde_id LEFT JOIN tAdresse ON tAdresse.kKunde = tkunde.kKunde WHERE cubss_newsletter.getnewsletter = 'N'" & strWhere, sqlConn3)
Dim r3 As SqlDataReader = Await sqlComm3.ExecuteReaderAsync()
While Await r3.ReadAsync()
frmMain.optAbgemeldet.Text = "&Abgemeldet (" & r3("anzahl").ToString & ")"
End While
sqlConn3.Close()
Dim sqlConn4 As New SqlConnection(strConnectionString_eazybusiness)
Await sqlConn4.OpenAsync()
Dim sqlComm4 As New SqlCommand("SELECT count(tkunde.kKunde) as anzahl FROM tkunde LEFT JOIN cubss_newsletter ON tkunde.kKunde = cubss_newsletter.kunde_id LEFT JOIN tAdresse ON tAdresse.kKunde = tkunde.kKunde WHERE cubss_newsletter.sended = 'N'" & strWhere, sqlConn4)
Dim r4 As SqlDataReader = Await sqlComm4.ExecuteReaderAsync()
While Await r4.ReadAsync()
frmMain.optAngemeldet.Text = "&Angemeldet (" & r4("anzahl").ToString & ")"
End While
sqlConn4.Close()
Dim sqlConn5 As New SqlConnection(strConnectionString_eazybusiness)
Await sqlConn5.OpenAsync()
Dim sqlComm5 As New SqlCommand("SELECT count(tkunde.kKunde) as anzahl FROM tkunde LEFT JOIN cubss_newsletter ON tkunde.kKunde = cubss_newsletter.kunde_id LEFT JOIN tAdresse ON tAdresse.kKunde = tkunde.kKunde WHERE tkunde.cNewsletter = 'Y'" & strWhere, sqlConn5)
Dim r5 As SqlDataReader = Await sqlComm5.ExecuteReaderAsync()
While Await r5.ReadAsync()
frmMain.optNewsletterYES.Text = "&Newsletter Wawi (Ja) (" & r5("anzahl").ToString & ")"
End While
sqlConn5.Close()
Return strWhere
Catch ex As Exception
MessageBox.Show("Es gab einen Fehler: " & ex.Message & vbCrLf & ex.StackTrace, "Fehler in getJTLKundenData_Where", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return "-1"
End Try
End Function
Public Async Function getKundenGruppe(cmb As ComboBox) As Threading.Tasks.Task(Of Boolean)
Try
Dim sqlConn As New SqlConnection(strConnectionString_eazybusiness)
Await sqlConn.OpenAsync()
'# Combobox löschen
cmb.Items.Clear()
Dim sqlComm As New SqlCommand("SELECT * FROM tKundenGruppe ORDER BY kKundenGruppe ASC", sqlConn)
Dim r As SqlDataReader = Await sqlComm.ExecuteReaderAsync()
cmb.Items.Add("Alle Kundengruppen")
While Await r.ReadAsync()
cmb.Items.Add(r("cName").ToString & " | " & Await getKundenGruppe_count(r("kKundenGruppe").ToString) & " | " & Math.Round(CDbl(r("fRabatt")), 2) & "%")
End While
sqlConn.Close()
Return True
Catch ex As Exception
MessageBox.Show(ex.Message & vbCrLf & ex.StackTrace, "Fehler bei getKundenGruppe()", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return False
End Try
End Function
Public Async Function getKundenSprache(cmb As ComboBox) As Threading.Tasks.Task(Of Boolean)
Try
Dim sqlConn As New SqlConnection(strConnectionString_eazybusiness)
Await sqlConn.OpenAsync()
'# Combobox löschen
cmb.Items.Clear()
Dim sqlComm As New SqlCommand("SELECT * FROM tSpracheUsed ORDER BY kSprache ASC", sqlConn)
Dim r As SqlDataReader = Await sqlComm.ExecuteReaderAsync()
cmb.Items.Add("Alle Sprachen")
While Await r.ReadAsync()
cmb.Items.Add(r("cNameDeu").ToString & " | " & Await getKundenSprache_count(r("kSprache").ToString))
End While
sqlConn.Close()
Return True
Catch ex As Exception
MessageBox.Show(ex.Message & vbCrLf & ex.StackTrace, "Fehler bei getKundenSprache()", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return False
End Try
End Function
'##########################################################
'# >> getMandantbyName
'# Mandant DB Name einlesen
'# Returns DB-Name
'##########################################################
Public Async Function getMandantDatabaseByMandantName(strMandantName As String) As Threading.Tasks.Task(Of String)
Dim sqlConn As New SqlConnection(strConnectionString_eazybusiness)
Try
Dim strDBName As String = ""
Await sqlConn.OpenAsync()
'# Datenbank einlesen
Dim sqlComm As New SqlCommand("SELECT * FROM tMandant WHERE cName='" & strMandantName & "'", sqlConn)
Dim r As SqlDataReader = Await sqlComm.ExecuteReaderAsync()
While Await r.ReadAsync()
Dim lvwItem As New ListViewItem
strDBName = r("cDB").ToString
End While
sqlConn.Close()
Return strDBName
Catch ex As Exception
MessageBox.Show(ex.Message & vbCrLf & ex.StackTrace, "getMandantbyName()", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return False
End Try
End Function
'##########################################################
'# >> getMandantbyDBName
'# Mandant DB Name einlesen
'# Returns Profil-Name
'##########################################################
Public Async Function getMandantbyDBName(strMandantDB As String) As Threading.Tasks.Task(Of String)
Dim sqlConn As New SqlConnection(strConnectionString_eazybusiness)
Try
Dim strMandantName As String = ""
Await sqlConn.OpenAsync()
'# Datenbank einlesen
Dim sqlComm As New SqlCommand("SELECT * FROM tMandant WHERE cDB='" & strMandantDB & "'", sqlConn)
Dim r As SqlDataReader = Await sqlComm.ExecuteReaderAsync()
While Await r.ReadAsync()
Dim lvwItem As New ListViewItem
strMandantName = r("cName").ToString
End While
sqlConn.Close()
Return strMandantName
Catch ex As Exception
MessageBox.Show(ex.Message & vbCrLf & ex.StackTrace, "getMandantbyDBName()", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return False
End Try
End Function
'###########################################################
'# >> getJTLKundenData_Where
'###########################################################
Public Async Function getJTLKundenData_Where(optNewsletterAbschicken As RadioButton, optNewsletterVerschickt As RadioButton, optAngemeldet As RadioButton, optAbgemeldet As RadioButton, chkNichtAmazon As CheckBox) As Threading.Tasks.Task(Of String)
Dim strWhere As String = ""
Try
'# Nur JTL Wawi Newsleter = Y
'If frmMain.optNewsletterYES.Checked = True Then
' strWhere = " AND tkunde.cNewsletter = 'Y'"
'Else
' If optAngemeldet.Checked = False And optAbgemeldet.Checked = False Then
' If optNewsletterAbschicken.Checked = False Then
' strWhere = " AND cubss_newsletter.sended = 'Y'"
' Else
' strWhere = " AND cubss_newsletter.sended = 'N' AND cubss_newsletter.getnewsletter = 'Y'"
' End If
' Else
' If optAngemeldet.Checked = True Then
' strWhere = " AND cubss_newsletter.getnewsletter = 'Y'"
' Else
' strWhere = " AND cubss_newsletter.getnewsletter = 'N'"
' End If
' End If
'End If
If frmMain.optNewsletterYES.Checked = True Then
strWhere = " tkunde.cNewsletter = 'Y'"
Else
If optAngemeldet.Checked = False And optAbgemeldet.Checked = False Then
If optNewsletterAbschicken.Checked = False Then
strWhere = " cubss_newsletter.sended = 'Y'"
Else
strWhere = " cubss_newsletter.sended = 'N' AND cubss_newsletter.getnewsletter = 'Y'"
End If
Else
If optAngemeldet.Checked = True Then
strWhere = " cubss_newsletter.getnewsletter = 'Y'"
Else
strWhere = " cubss_newsletter.getnewsletter = 'N'"
End If
End If
End If
If chkNichtAmazon.Checked = True Then
strWhere = strWhere & " AND tAdresse.cMail NOT LIKE '%marketplace.amazon.%'"
Else
strWhere = strWhere & " AND tAdresse.cMail LIKE '%marketplace.amazon.%'"
End If
If Not My.Settings.jtlshop_kunde_kategorie = "" Then
strWhere = strWhere & " AND kKundenKategorie = '" & My.Settings.jtlshop_kunde_kategorie & "'"
End If
Return strWhere
Catch ex As Exception
MessageBox.Show("Es gab einen Fehler: " & ex.Message & vbCrLf & ex.StackTrace, "Fehler in getJTLKundenData_Where", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return "-1"
End Try
End Function
Public Async Function getJTLKundenData_bestellsumme(strkKunde As String) As Task(Of Double)
Dim sqlConn2 As New SqlConnection(strConnectionString)
Await sqlConn2.OpenAsync()
Dim sqlComm2 As New SqlCommand("SELECT sum(fVKPreis) as fVKPreis_agg FROM tBestellung JOIN tbestellpos ON tBestellung.kBestellung = tbestellpos.tBestellung_kBestellung WHERE tBestellung.tKunde_kKunde='" & strkKunde & "'", sqlConn2)
Dim r2 As SqlDataReader = Await sqlComm2.ExecuteReaderAsync()
Dim dblSum As Double = 0
While Await r2.ReadAsync()
Try
dblSum = CDbl(r2("fVKPreis_agg"))
Catch ex As Exception
End Try
End While
sqlConn2.Close()
Return dblSum
End Function
'##########################################################
'# >> Kundendaten einlesen
'##########################################################
Public Async Function getJTLKundenData(ByVal lvwJTLKunden As ListView, ByVal strWhere As String, ByVal frmMain As frmMain) As Threading.Tasks.Task(Of Boolean)
Try
'#############################################
'# >> Kundensprache
'#############################################
Dim strSprachen_id As String = ""
Dim sqlSprachen_alle As String = ""
Dim strSprachen_input() As String = frmMain.cmbSprache.Text.Split("|")
If Not frmMain.cmbSprache.Text = "Alle Sprachen" Then
Dim con_kunden_sprachen As New SqlConnection(strConnectionString)
Await con_kunden_sprachen.OpenAsync()
Dim cmd_kunden_sprachen As New SqlCommand("SELECT kSprache FROM tSpracheUsed WHERE cNameDeu='" & strSprachen_input(0).Trim & "'", con_kunden_sprachen)
Dim read_kunden_sprache As SqlDataReader = Await cmd_kunden_sprachen.ExecuteReaderAsync()
While Await read_kunden_sprache.ReadAsync()
strSprachen_id = read_kunden_sprache("kSprache").ToString
End While
sqlSprachen_alle = " AND tKunde.kSprache='" & strSprachen_id & "' "
con_kunden_sprachen.Close()
End If
'#############################################
'# >> Kundengruppe
'#############################################
Dim strKundengruppe_id As String = ""
Dim sqlKundengruppe_alle As String = ""
Dim strKundengruppe_input() As String = frmMain.cmbKundengruppe.Text.Split("|")
If Not frmMain.cmbKundengruppe.Text = "Alle Kundengruppen" Then
Dim con_kunden_gruppen As New SqlConnection(strConnectionString)
Await con_kunden_gruppen.OpenAsync()
Dim cmd_kunden_gruppen As New SqlCommand("SELECT kKundenGruppe FROM tKundenGruppe WHERE cName='" & strKundengruppe_input(0).Trim & "'", con_kunden_gruppen)
Dim read_kunden_gruppen As SqlDataReader = Await cmd_kunden_gruppen.ExecuteReaderAsync()
While Await read_kunden_gruppen.ReadAsync()
strKundengruppe_id = read_kunden_gruppen("kKundenGruppe").ToString
End While
sqlKundengruppe_alle = " AND tKunde.kKundenGruppe='" & strKundengruppe_id & "' "
con_kunden_gruppen.Close()
End If
'#############################################
'# >> Kundenkategorie
'#############################################
Dim strKundenkategorie_id As String = ""
Dim sqlKundenkategorie_alle As String = ""
Dim strKundenkategorie_input() As String = frmMain.cmbKundenKategorie.Text.Split("|")
If Not frmMain.cmbKundenKategorie.Text = "Alle Kundenkategorien" Then
Dim con_kunden_kategorie As New SqlConnection(strConnectionString)
Await con_kunden_kategorie.OpenAsync()
Dim cmd_kunden_kategorie As New SqlCommand("SELECT kKundenKategorie FROM tKundenKategorie WHERE cName='" & strKundenkategorie_input(0).Trim & "'", con_kunden_kategorie)
Dim read_kunden_gruppen As SqlDataReader = Await cmd_kunden_kategorie.ExecuteReaderAsync()
While Await read_kunden_gruppen.ReadAsync()
strKundenkategorie_id = read_kunden_gruppen("kKundenKategorie").ToString
End While
sqlKundengruppe_alle = " AND tKunde.kKundenKategorie='" & strKundenkategorie_id & "' "
con_kunden_kategorie.Close()
End If
'#############################################
'# >> Kunden zählen
'#############################################
Dim con_count_kunden As New SqlConnection(strConnectionString)
Await con_count_kunden.OpenAsync()
Dim sqlComm_count_kunden As New SqlCommand("SELECT count(*) as anzahl FROM cubss_newsletter LEFT JOIN tKunde ON tKunde.kKunde = cubss_newsletter.kunde_id LEFT JOIN tAdresse ON tKunde.kKunde = tAdresse.kKunde WHERE " & strWhere & sqlSprachen_alle & sqlKundengruppe_alle & sqlKundengruppe_alle, con_count_kunden)
Dim rKunden_count As SqlDataReader = Await sqlComm_count_kunden.ExecuteReaderAsync()
Dim iMax As Integer = 0
While Await rKunden_count.ReadAsync()
iMax = Convert.ToInt32(rKunden_count("anzahl").ToString)
End While
con_count_kunden.Close()
Dim iCount As Integer = 0
frmMain.tss_main_text.Text = iCount & " / " & iMax
frmMain.masterpgr.Value = iCount
frmMain.masterpgr.Maximum = iMax
frmMain.masterpgr.Visible = True
Dim sqlConn As New SqlConnection(strConnectionString)
Await sqlConn.OpenAsync()
lvwJTLKunden.Items.Clear()
'SELECT * FROM tAdresse LEFT JOIN tKunde ON tKunde.kKunde = tAdresse.kKunde LEFT JOIN cubss_newsletter ON tKunde.kKunde = cubss_newsletter.kunde_id WHERE tAdresse.cmail !='' AND tAdresse.cMail NOT LIKE '%@marketplace.amazon.%'
Dim strQuery As String = "SELECT *,tAdresse.cMail as Email FROM cubss_newsletter LEFT JOIN tKunde ON tKunde.kKunde = cubss_newsletter.kunde_id LEFT JOIN tAdresse ON tKunde.kKunde = tAdresse.kKunde WHERE " & strWhere & " AND (tAdresse.cmail !='' AND tAdresse.cMail NOT LIKE '%@marketplace.amazon.%') " & sqlSprachen_alle & sqlKundengruppe_alle & " ORDER BY tAdresse.cMail ASC"
Dim sqlComm As New SqlCommand(strQuery, sqlConn)
Dim r As SqlDataReader = Await sqlComm.ExecuteReaderAsync()
Dim tmpKunde_id As String = ""
While Await r.ReadAsync()
If r("Email").ToString = tmpKunde_id Then
Else
Dim lvwItem As New ListViewItem
lvwItem.Text = r("kKunde").ToString
lvwItem.SubItems.Add(r("cVorname").ToString)
lvwItem.SubItems.Add(r("cName").ToString)
lvwItem.SubItems.Add(r("cOrt").ToString)
lvwItem.SubItems.Add(r("Email").ToString)
lvwItem.SubItems.Add(r("cFirma").ToString)
lvwItem.SubItems.Add(r("cKundenNr").ToString)
lvwItem.SubItems.Add(r("cStrasse").ToString)
lvwItem.SubItems.Add(r("cLand").ToString)
lvwItem.SubItems.Add(r("cAnrede").ToString)
lvwItem.SubItems.Add(r("cPLZ").ToString)
If r("getNewsletter").ToString = "Y" Then
lvwItem.SubItems.Add("Ja")
Else
lvwItem.SubItems.Add("Nein")
End If
lvwItem.SubItems.Add(r("dErstellt").ToString)
Dim dblUmsatz = Math.Round(Await getJTLKundenData_bestellsumme(r("kKunde").ToString), 2)
lvwItem.SubItems.Add(CStr(dblUmsatz))
Try
Dim datum1 As Date = Date.Parse(CType(r("dErstellt"), String))
Dim datum2 As Date = Now
' Anzahl der Tage berechnen
Dim days As Long = DateDiff(DateInterval.Day, datum1, datum2)
lvwItem.SubItems.Add(CStr(days))
lvwItem.SubItems.Add(CStr(Math.Round(dblUmsatz / days, 2)))
Catch ex As Exception
lvwItem.SubItems.Add("NaN")
lvwItem.SubItems.Add("NaN")
End Try
lvwJTLKunden.Items.Add(lvwItem)
If bAbbruch = True Then
bAbbruch = False
Exit Function
End If
iCount = iCount + 1
End If
frmMain.masterpgr.Value = iCount
frmMain.tss_main_text.Text = iCount & " / " & iMax
Application.DoEvents()
tmpKunde_id = r("Email").ToString
End While
' lvwJTLKunden.EndUpdate()
frmMain.tss_main_text.Text = CType(lvwJTLKunden.Items.Count - 1, String)
sqlConn.Close()
Return True
Catch ex As Exception
lvwJTLKunden.EndUpdate()
If ex.Message.IndexOf("cubss_newsletter") > 0 Then
If MessageBox.Show("Datenbanktabelle cubss_newsletter fehlt, möchten Sie diese jetzt installlieren?" & vbCrLf & ex.Message, "getJTLKundenData_send", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
Dim frmDBSetting As New frmDatenbankEinstellungen
frmDBSetting.ShowDialog()
End If
Else
MessageBox.Show(ex.Message & vbCrLf & ex.StackTrace, "Fehler bei getJTLKundenData", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
Return False
End Try
End Function
'##########################################################
'# >> Kundendaten einlesen
'##########################################################
Public Async Function getSYNC_JTL_WAWI_KUNDEN(ByVal lvwJTLKunden As ListView, ByVal frmStatus As frmJTLSync) As Threading.Tasks.Task(Of Boolean)
Try
'Dim sqlConn As New SqlConnection(strConnectionString)
Dim strWhere As String = ""
'Await sqlConn.OpenAsync()
lvwJTLKunden.Items.Clear()
'SELECT
'count(tKunde.kKunde) As anzahl,
'tAdresse.cName,
'tAdresse.cOrt
'From tAdresse Left outer Join tKunde On tKunde.kKunde = tAdresse.kKunde Group By tAdresse.cName, tAdresse.cOrt Order By cName DESC '# Einträge zählen
frmStatus.pgrStatus.Value = 0
frmStatus.pgrStatus.Visible = True
Dim strQuery_count As String = "SELECT
count(tAdresse.kKunde) as anzahl
FROM tAdresse right Join tKunde ON tKunde.kKunde = tAdresse.kKunde WHERE (tAdresse.cMail IS NOT NULL AND tAdresse.cMail !='' AND tAdresse.cMail NOT LIKE '%@marketplace.amazon.%')"
Dim sqlConn_group_by_count As New SqlConnection(strConnectionString)
Await sqlConn_group_by_count.OpenAsync()
Dim sqlComm_group_by_count As New SqlCommand(strQuery_count, sqlConn_group_by_count)
Dim rGroup_by_count As SqlDataReader = Await sqlComm_group_by_count.ExecuteReaderAsync()
Dim iMax As Int64
While Await rGroup_by_count.ReadAsync()
iMax += CLng(rGroup_by_count("anzahl").ToString)
End While
sqlConn_group_by_count.Close()
' MsgBox(iMax)
Dim strQuery As String = "SELECT
tKunde.kKunde,*
FROM tAdresse right Join tKunde ON tKunde.kKunde = tAdresse.kKunde WHERE (tAdresse.cMail IS NOT NULL AND tAdresse.cMail !='' AND tAdresse.cMail NOT LIKE '%@marketplace.amazon.%') ORDER BY tKunde.kKunde ASC"
Dim sqlConn_group_by As New SqlConnection(strConnectionString)
Await sqlConn_group_by.OpenAsync()
Dim sqlComm_group_by As New SqlCommand(strQuery, sqlConn_group_by)
Dim rGroup_by As SqlDataReader = Await sqlComm_group_by.ExecuteReaderAsync()
Dim iCount As Integer = 0
Dim iCount_neu As Integer = 0
Dim iCount_exist As Integer = 0
frmStatus.lblJTLNewsletterKundenExist.Text = iCount_exist & ": Kunde existiert"
frmStatus.lblNeueJTLNewsletterKunden.Text = iCount_exist & ": neuer Newsletter Kunde"
Dim strKunde_id As String = ""
frmStatus.pgrStatus.Maximum = CInt(iMax)
While Await rGroup_by.ReadAsync()
If frmStatus.bStopSync = True Then
Exit While
End If
If strKunde_id = rGroup_by("kKunde").ToString Then
Else
strKunde_id = rGroup_by("kKunde").ToString
If frmStatus.bStopSync = True Then
Exit While
End If
'# Prüfung ob Kunde in TMP Existiert
If CBool(My.Settings.bKomplettSync(My.Settings.mandant_position)) = False Then
If Await frmMain.clsDB.ChkJTLKundeExistByKunden_id(rGroup_by("kKunde").ToString) = False Then
frmStatus.lblStatus.Text = iCount + 1 & " / " & iMax & " | Füge hinzu: " & rGroup_by("cKundenNr").ToString & " / " & rGroup_by("cVorname").ToString & " " & rGroup_by("cName").ToString & " | " & rGroup_by("cOrt").ToString & " / " & rGroup_by("cFirma").ToString
' Datensatz in Listview anlegen
Dim lvwItem As New ListViewItem
lvwItem.Text = rGroup_by("kKunde").ToString
lvwItem.SubItems.Add(rGroup_by("cVorname").ToString)
lvwItem.SubItems.Add(rGroup_by("cName").ToString)
lvwItem.SubItems.Add(rGroup_by("cOrt").ToString)
lvwItem.SubItems.Add(rGroup_by("cMail").ToString)
lvwItem.SubItems.Add(rGroup_by("cFirma").ToString)
lvwItem.SubItems.Add(rGroup_by("cKundenNr").ToString)
lvwItem.SubItems.Add(rGroup_by("cStrasse").ToString)
lvwItem.SubItems.Add(rGroup_by("cLand").ToString)
lvwJTLKunden.Items.Add(lvwItem)
' JTL Newsletter Kunden anzulegen
Await frmMain.clsDB.setJTLKundeTMP_new(lvwJTLKunden.Items(lvwJTLKunden.Items.Count - 1))
iCount_neu += 1
frmStatus.lblNeueJTLNewsletterKunden.Text = iCount_neu & ": neuer Newsletter Kunde"
Else
frmStatus.lblStatus.Text = iCount + 1 & " / " & iMax & " | Existiert bereits: " & rGroup_by("cKundenNr").ToString & " / " & rGroup_by("cVorname").ToString & " " & rGroup_by("cName").ToString & " | " & rGroup_by("cOrt").ToString & " / " & rGroup_by("cFirma").ToString
Exit While
End If
Else
frmStatus.lblStatus.Text = iCount + 1 & " / " & iMax & " | Existiert bereits: " & rGroup_by("cKundenNr").ToString & " / " & rGroup_by("cVorname").ToString & " " & rGroup_by("cName").ToString & " | " & rGroup_by("cOrt").ToString & " / " & rGroup_by("cFirma").ToString
' Existiert der JTL Wawi Kunde schon?
If Await frmMain.clsDB.ChkJTLKundeExistByKunden_id(rGroup_by("kKunde").ToString) = False Then
frmStatus.lblStatus.Text = iCount + 1 & " / " & iMax & " | Füge hinzu: " & rGroup_by("cKundenNr").ToString & " / " & rGroup_by("cVorname").ToString & " " & rGroup_by("cName").ToString & " | " & rGroup_by("cOrt").ToString & " / " & rGroup_by("cFirma").ToString
' Datensatz in Listview anlegen
Dim lvwItem As New ListViewItem
lvwItem.Text = rGroup_by("kKunde").ToString
lvwItem.SubItems.Add(rGroup_by("cVorname").ToString)
lvwItem.SubItems.Add(rGroup_by("cName").ToString)
lvwItem.SubItems.Add(rGroup_by("cOrt").ToString)
lvwItem.SubItems.Add(rGroup_by("cMail").ToString)
lvwItem.SubItems.Add(rGroup_by("cFirma").ToString)
lvwItem.SubItems.Add(rGroup_by("cKundenNr").ToString)
lvwItem.SubItems.Add(rGroup_by("cStrasse").ToString)
lvwItem.SubItems.Add(rGroup_by("cLand").ToString)
lvwJTLKunden.Items.Add(lvwItem)
'Neuen JTL Wawi Kunden hinzufügen
Await frmMain.clsDB.setJTLKundeTMP_new(lvwJTLKunden.Items(lvwJTLKunden.Items.Count - 1))
iCount_neu += 1
frmStatus.lblNeueJTLNewsletterKunden.Text = iCount_neu & ": neuer Newsletter Kunde"
Else
iCount_exist += 1
frmStatus.lblJTLNewsletterKundenExist.Text = iCount_exist & ": Kunde existiert"
End If
End If
End If
Try
frmStatus.pgrStatus.Value = iCount
Catch ex As Exception
End Try
' Await sqlConn.OpenAsync()
'# 1.3.x tKunde zu 1.4.x tinetkunde
'Dim sqlComm As New SqlCommand("SELECT tAdresse.cName,tAdresse.cOrt,tAdresse.kAdresse as kAdresse_adresse, tAdresse.*,tKunde.* FROM tAdresse LEFT JOIN tKunde ON tKunde.kKunde = tAdresse.kKunde WHERE tAdresse.cName ='" & rGroup_by("cName").ToString.Replace("'", "''") & "' AND tAdresse.cOrt='" & rGroup_by("cOrt").ToString.Replace("'", "''") & "' AND tAdresse.cStrasse='" & rGroup_by("cStrasse").ToString.Replace("'", "''") & "'", sqlConn)
' MsgBox("SELECT tAdresse.cName,tAdresse.cOrt,tAdresse.kAdresse as kAdresse_adresse, tAdresse.*,tKunde.* FROM tAdresse LEFT JOIN tKunde ON tKunde.kKunde = tAdresse.kKunde WHERE tAdresse.cName ='" & rGroup_by("cName").ToString & "' AND tAdresse.cOrt='" & rGroup_by("cOrt").ToString & "' AND tAdresse.cStrasse='" & rGroup_by("cStrasse").ToString & "'")
' Clipboard.SetText("SELECT tAdresse.cName,tAdresse.cOrt,tAdresse.kAdresse as kAdresse_adresse, tAdresse.*,tKunde.* FROM tAdresse LEFT JOIN tKunde ON tKunde.kKunde = tAdresse.kKunde WHERE tAdresse.cName ='" & rGroup_by("cName").ToString & "' AND tAdresse.cOrt='" & rGroup_by("cOrt").ToString & "' AND tAdresse.cStrasse='" & rGroup_by("cStrasse").ToString & "'")
' Dim r As SqlDataReader = Await sqlComm.ExecuteReaderAsync()
' While Await r.ReadAsync()
' End While
' sqlConn.Close()
iCount = iCount + 1
End While
sqlConn_group_by.Close()
'sqlConn.Close()
Return True
Catch ex As Exception
MessageBox.Show(ex.Message & vbCrLf & ex.StackTrace, "Fehler beim JTL Kunden abrufen", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return False
End Try
End Function
'##########################################################
'# >> chkJTLKundeExistByKunden_id
'##########################################################
Public Async Function ChkJTLKundeExistByKunden_id(ByVal strKID As String) As Threading.Tasks.Task(Of Boolean)
Try
Dim sqlConn As New SqlConnection(strConnectionString)
Await sqlConn.OpenAsync()
If strKID.Length > 0 Then
Dim sqlComm As New SqlCommand("SELECT * FROM cubss_newsletter WHERE kunde_id =" & strKID, sqlConn)
Dim r As SqlDataReader = Await sqlComm.ExecuteReaderAsync()
While Await r.ReadAsync()
sqlConn.Close()
Return True
End While
sqlConn.Close()
End If
Return False
Catch ex As Exception
MessageBox.Show(ex.Message & vbCrLf & ex.StackTrace, "Fehler beim JTL Kunden prüfen", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return False
End Try
End Function
'##########################################################
'# >> JTLKundeExistByEmail
'##########################################################
Public Async Function JTLKundeExistByEmail(ByVal strKID As String) As Threading.Tasks.Task(Of Boolean)
Try
Dim sqlConn As New SqlConnection(strConnectionString)
Await sqlConn.OpenAsync()
If strKID.Length > 0 Then
Dim sqlComm As New SqlCommand("SELECT * FROM cubss_newsletter WHERE kunde_id=" & strKID, sqlConn)
Dim r As SqlDataReader = Await sqlComm.ExecuteReaderAsync()
While Await r.ReadAsync()
sqlConn.Close()
Return True
End While
sqlConn.Close()
Return False
End If
Return False
Catch ex As Exception
MessageBox.Show(ex.Message & vbCrLf & ex.StackTrace, "Fehler beim JTL Kunden prüfen", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return False
End Try
End Function
'##########################################################
'# >> Kunde Existiert in Temptabelle
'##########################################################
Public Async Function chkJTLKundeExistByEmail(ByVal strEmail As String) As Threading.Tasks.Task(Of Boolean)
Try
Dim sqlConn As New SqlConnection(strConnectionString)
Await sqlConn.OpenAsync()
If strEmail.Length > 0 Then
Dim sqlComm As New SqlCommand("SELECT * FROM tAdresse WHERE cMail='" & strEmail & "'", sqlConn)
Dim r As SqlDataReader = Await sqlComm.ExecuteReaderAsync()
While Await r.ReadAsync()
Dim bOK As Boolean = Await JTLKundeExistByEmail(r("kKunde").ToString)
sqlConn.Close()
Return bOK
End While
sqlConn.Close()
End If
Return False
Catch ex As Exception
MessageBox.Show(ex.Message & vbCrLf & ex.StackTrace, "Fehler beim JTL Kunden prüfen", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return False
End Try
End Function
'####################################################################
'# >> Maximale ID ermitteln
'####################################################################
Public Async Function getJTLID_MAX(ByVal strTabelle As String, ByVal strSpalte As String) As Threading.Tasks.Task(Of Integer)
Try
Dim sqlConn As New SqlConnection(strConnectionString)