-
Notifications
You must be signed in to change notification settings - Fork 0
/
APDesignatePayables.vb
4617 lines (3973 loc) · 255 KB
/
APDesignatePayables.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
Imports System.Windows.Forms
Imports System.Math
Imports System.IO
Imports System.Data
Imports System.Data.OleDb
Imports CrystalDecisions.Windows.Forms
Imports CrystalDecisions.ReportSource
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Imports System.Data.SqlClient
Public Class APDesignatePayables
Inherits System.Windows.Forms.Form
Dim CheckVoucherExists, SelectedVoucher, DiscountDays As Integer
Dim LastBatchNumber, NextBatchNumber As Long
Dim BatchTotal, DiscountPercentage As Double
Dim PaymentTerms, DuplicateVoucherExists As String
Dim SumVendorTotalPlus, SumVendorTotalMinus, SumVendorTotalNet As Double
Dim TodaysDate As Date = Today.ToShortDateString()
Dim DeleteSingleBatch As Long = 0
'Declare variables for ACH Batches
Dim ATLACHBatchNumber, CBSACHBatchNumber, CGOACHBatchNumber, CHTACHBatchNumber, DENACHBatchNumber, HOUACHBatchNumber, LLHACHBatchNumber, SLCACHBatchNumber, TFFACHBatchNumberAmerican, TFJACHBatchNumber, TFFACHBatchNumberCanadian, ALBACHBatchNumberAmerican, ALBACHBatchNumberCanadian, TFTACHBatchNumber, TORACHBatchNumberAmerican, TORACHBatchNumberCanadian, TWDACHBatchNumber, TWEACHBatchNumber As Long
Dim NewBatchNumber, OldBatchNumber As Long
Dim ACHBatchCount As Integer = 0
Dim DivisionACHBatchNumber As Long = 0
Dim GlobalBatchNumber As Long = 0
'Initialize error variables
Dim ErrorDate As String = ""
Dim ErrorDescription As String = ""
Dim ErrorUser As String = ""
Dim ErrorComment As String = ""
Dim ErrorDivision As String = ""
Dim ErrorReferenceNumber As String = ""
'Setup data connection and variables
Dim con As SqlConnection = New SqlConnection("Data Source=TFP-SQL;Initial Catalog=TFPOperationsDatabase;Integrated Security=True;Connect Timeout=30")
Dim cmd As SqlCommand
Dim myAdapter, myAdapter1, myAdapter2, myAdapter3, myAdapter4 As New SqlDataAdapter
Dim ds, ds1, ds2, ds3, ds4 As DataSet
Private Sub APDesignatePayables_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
ClearVariables()
ClearData()
If EmployeeCompanyCode = "ADM" Then
Try
'Update to unselected
cmd = New SqlCommand("UPDATE ReceiptOfInvoiceBatchLine SET SelectedInDatagrid = @SelectedInDatagrid", con)
With cmd.Parameters
.Add("@SelectedInDatagrid", SqlDbType.VarChar).Value = "NO"
End With
If con.State = ConnectionState.Closed Then con.Open()
cmd.ExecuteNonQuery()
con.Close()
Catch ex As Exception
End Try
Else
Try
'Update to unselected
cmd = New SqlCommand("UPDATE ReceiptOfInvoiceBatchLine SET SelectedInDatagrid = @SelectedInDatagrid WHERE DivisionID = @DivisionID", con)
With cmd.Parameters
.Add("@DivisionID", SqlDbType.VarChar).Value = EmployeeCompanyCode
.Add("@SelectedInDatagrid", SqlDbType.VarChar).Value = "NO"
End With
If con.State = ConnectionState.Closed Then con.Open()
cmd.ExecuteNonQuery()
con.Close()
Catch ex As Exception
End Try
End If
End Sub
Private Sub APDesignatePayables_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'SQLTFPOperationsDatabaseDataSet.DivisionTable' table. You can move, or remove it, as needed.
Me.DivisionTableTableAdapter.Fill(Me.SQLTFPOperationsDatabaseDataSet.DivisionTable)
'Set Company Default
If EmployeeCompanyCode = "ADM" Then
cboDivisionID.Enabled = True
cboDivisionID.Text = EmployeeCompanyCode
Me.dgvPayables.Columns("DivisionIDColumn").Visible = True
Else
cboDivisionID.Enabled = False
cboDivisionID.Text = EmployeeCompanyCode
Me.dgvPayables.Columns("DivisionIDColumn").Visible = False
End If
'For Each row As DataGridViewRow In dgvPayables.Rows
' Dim cell As DataGridViewCheckBoxCell = row.Cells("SelectForPayment")
' cell.Value = "UNSELECTED"
'Next
ClearVariables()
ClearData()
If cboDivisionID.Text = "TFF" Or cboDivisionID.Text = "TOR" Or cboDivisionID.Text = "ALB" Then
cboVendorClass.Enabled = True
cboVendorClass.Text = "Canadian"
ShowPostedVouchersCanadian()
ElseIf cboDivisionID.Text = "ADM" Then
cboVendorClass.SelectedIndex = -1
cboVendorClass.Enabled = False
ShowPostedVouchersADM()
Else
cboVendorClass.SelectedIndex = -1
cboVendorClass.Enabled = False
ShowPostedVouchers()
End If
If EmployeeLoginName = "HSRYAN" Or EmployeeSecurityCode = "1001" Then
gpxPostOff.Enabled = True
Else
gpxPostOff.Enabled = False
End If
'Loads totals for the whole company
LoadDivisionTotals()
LoadCompanyTotals()
End Sub
Public Sub ShowPostedVouchers()
cmd = New SqlCommand("SELECT * FROM ReceiptOfInvoiceBatchLine WHERE VoucherStatus = @VoucherStatus AND DivisionID = @DivisionID ORDER BY VendorID", con)
cmd.Parameters.Add("@VoucherStatus", SqlDbType.VarChar).Value = "POSTED"
cmd.Parameters.Add("@DivisionID", SqlDbType.VarChar).Value = cboDivisionID.Text
ds = New DataSet()
myAdapter.SelectCommand = cmd
If con.State = ConnectionState.Closed Then con.Open()
myAdapter.Fill(ds, "ReceiptOfInvoiceBatchLine")
con.Close()
dgvPayables.DataSource = ds.Tables("ReceiptOfInvoiceBatchLine")
FormatWhitePaperChecks()
End Sub
Public Sub FormatWhitePaperChecks()
Dim CheckType As String = ""
Dim IsWhitePaper As String = ""
Dim RowIndex As Integer = 0
For Each row As DataGridViewRow In dgvPayables.Rows
Try
CheckType = row.Cells("CheckTypeColumn").Value
Catch ex As System.Exception
CheckType = ""
End Try
Try
IsWhitePaper = row.Cells("WhitePaperColumn").Value
Catch ex As System.Exception
IsWhitePaper = "NO"
End Try
If CheckType = "STANDARD" And IsWhitePaper = "YES" Then
Me.dgvPayables.Rows(RowIndex).DefaultCellStyle.BackColor = Color.White
Me.dgvPayables.Rows(RowIndex).DefaultCellStyle.ForeColor = Color.Blue
ElseIf CheckType = "STANDARD" And IsWhitePaper <> "YES" Then
Me.dgvPayables.Rows(RowIndex).DefaultCellStyle.BackColor = Color.White
Me.dgvPayables.Rows(RowIndex).DefaultCellStyle.ForeColor = Color.Black
ElseIf CheckType = "FEDEX" Then
Me.dgvPayables.Rows(RowIndex).DefaultCellStyle.BackColor = Color.LightGray
Me.dgvPayables.Rows(RowIndex).DefaultCellStyle.ForeColor = Color.Blue
ElseIf CheckType = "INTERCOMPANY" Then
Me.dgvPayables.Rows(RowIndex).DefaultCellStyle.BackColor = Color.LightGray
Me.dgvPayables.Rows(RowIndex).DefaultCellStyle.ForeColor = Color.Red
ElseIf CheckType = "ACH" Then
Me.dgvPayables.Rows(RowIndex).DefaultCellStyle.BackColor = Color.LightGray
Me.dgvPayables.Rows(RowIndex).DefaultCellStyle.ForeColor = Color.Purple
Else
Me.dgvPayables.Rows(RowIndex).DefaultCellStyle.BackColor = Color.White
Me.dgvPayables.Rows(RowIndex).DefaultCellStyle.ForeColor = Color.Black
End If
RowIndex = RowIndex + 1
Next
End Sub
Public Sub FormatADM()
'*********************************************************************************************************************
Dim CountIndex As Integer = 0
Dim LineDivision As String = ""
'Format
For Each row As DataGridViewRow In dgvPayables.Rows
Try
LineDivision = dgvPayables.Rows(CountIndex).Cells("DivisionIDColumn").Value
Catch ex As Exception
LineDivision = ""
End Try
Select Case LineDivision
Case "ATL"
Try
dgvPayables.Rows(CountIndex).Cells("DivisionIDColumn").Style.BackColor = Color.LightBlue
Catch ex As Exception
'skip
End Try
Case "CBS"
Try
dgvPayables.Rows(CountIndex).Cells("DivisionIDColumn").Style.BackColor = Color.LightCoral
Catch ex As Exception
'skip
End Try
Case "CGO"
Try
dgvPayables.Rows(CountIndex).Cells("DivisionIDColumn").Style.BackColor = Color.LightSeaGreen
Catch ex As Exception
'skip
End Try
Case "CHT"
Try
dgvPayables.Rows(CountIndex).Cells("DivisionIDColumn").Style.BackColor = Color.LightYellow
Catch ex As Exception
'skip
End Try
Case "DEN"
Try
dgvPayables.Rows(CountIndex).Cells("DivisionIDColumn").Style.BackColor = Color.LightSalmon
Catch ex As Exception
'skip
End Try
Case "HOU"
Try
dgvPayables.Rows(CountIndex).Cells("DivisionIDColumn").Style.BackColor = Color.LightSlateGray
Catch ex As Exception
'skip
End Try
Case "LLH"
Try
dgvPayables.Rows(CountIndex).Cells("DivisionIDColumn").Style.BackColor = Color.LightCyan
Catch ex As Exception
'skip
End Try
Case "SLC"
Try
dgvPayables.Rows(CountIndex).Cells("DivisionIDColumn").Style.BackColor = Color.LightPink
Catch ex As Exception
'skip
End Try
Case "TFF"
Try
dgvPayables.Rows(CountIndex).Cells("DivisionIDColumn").Style.BackColor = Color.LightCoral
Catch ex As Exception
'skip
End Try
Case "TFJ"
Try
dgvPayables.Rows(CountIndex).Cells("DivisionIDColumn").Style.BackColor = Color.LemonChiffon
Catch ex As Exception
'skip
End Try
Case "TFT"
Try
dgvPayables.Rows(CountIndex).Cells("DivisionIDColumn").Style.BackColor = Color.LightGoldenrodYellow
Catch ex As Exception
'skip
End Try
Case "TWD"
Try
dgvPayables.Rows(CountIndex).Cells("DivisionIDColumn").Style.BackColor = Color.LightSteelBlue
Catch ex As Exception
'skip
End Try
Case "TFP"
Try
dgvPayables.Rows(CountIndex).Cells("DivisionIDColumn").Style.BackColor = Color.LightGray
Catch ex As Exception
'skip
End Try
Case "TOR"
Try
dgvPayables.Rows(CountIndex).Cells("DivisionIDColumn").Style.BackColor = Color.LightCyan
Catch ex As Exception
'skip
End Try
Case "TWE"
Try
dgvPayables.Rows(CountIndex).Cells("DivisionIDColumn").Style.BackColor = Color.LightGreen
Catch ex As Exception
'skip
End Try
Case "ALB"
Try
dgvPayables.Rows(CountIndex).Cells("DivisionIDColumn").Style.BackColor = Color.Wheat
Catch ex As Exception
'skip
End Try
Case Else
Try
dgvPayables.Rows(CountIndex).Cells("DivisionIDColumn").Style.BackColor = Color.White
Catch ex As Exception
'skip
End Try
End Select
CountIndex = CountIndex + 1
Next
End Sub
Public Sub ShowPostedVouchersADM()
cmd = New SqlCommand("SELECT * FROM ReceiptOfInvoiceBatchLine WHERE VoucherStatus = @VoucherStatus AND DivisionID <> @DivisionID AND DivisionID <> @DivisionID2 ORDER BY DivisionID, VendorID", con)
cmd.Parameters.Add("@VoucherStatus", SqlDbType.VarChar).Value = "POSTED"
cmd.Parameters.Add("@DivisionID", SqlDbType.VarChar).Value = "ADM"
cmd.Parameters.Add("@DivisionID2", SqlDbType.VarChar).Value = "TST"
ds = New DataSet()
myAdapter.SelectCommand = cmd
If con.State = ConnectionState.Closed Then con.Open()
myAdapter.Fill(ds, "ReceiptOfInvoiceBatchLine")
con.Close()
dgvPayables.DataSource = ds.Tables("ReceiptOfInvoiceBatchLine")
FormatWhitePaperChecks()
FormatADM()
End Sub
Public Sub ShowPostedVouchersAmerican()
cmd = New SqlCommand("SELECT * FROM ReceiptOfInvoiceBatchLine WHERE VoucherStatus = @VoucherStatus AND DivisionID = @DivisionID AND VendorClass = @VendorClass ORDER BY VendorID", con)
cmd.Parameters.Add("@VoucherStatus", SqlDbType.VarChar).Value = "POSTED"
cmd.Parameters.Add("@DivisionID", SqlDbType.VarChar).Value = cboDivisionID.Text
cmd.Parameters.Add("@VendorClass", SqlDbType.VarChar).Value = cboVendorClass.Text
ds = New DataSet()
myAdapter.SelectCommand = cmd
If con.State = ConnectionState.Closed Then con.Open()
myAdapter.Fill(ds, "ReceiptOfInvoiceBatchLine")
con.Close()
dgvPayables.DataSource = ds.Tables("ReceiptOfInvoiceBatchLine")
FormatWhitePaperChecks()
End Sub
Public Sub ShowPostedVouchersCanadian()
cmd = New SqlCommand("SELECT * FROM ReceiptOfInvoiceBatchLine WHERE VoucherStatus = @VoucherStatus AND DivisionID = @DivisionID AND VendorClass = @VendorClass ORDER BY VendorID", con)
cmd.Parameters.Add("@VoucherStatus", SqlDbType.VarChar).Value = "POSTED"
cmd.Parameters.Add("@DivisionID", SqlDbType.VarChar).Value = cboDivisionID.Text
cmd.Parameters.Add("@VendorClass", SqlDbType.VarChar).Value = cboVendorClass.Text
ds = New DataSet()
myAdapter.SelectCommand = cmd
If con.State = ConnectionState.Closed Then con.Open()
myAdapter.Fill(ds, "ReceiptOfInvoiceBatchLine")
con.Close()
dgvPayables.DataSource = ds.Tables("ReceiptOfInvoiceBatchLine")
FormatWhitePaperChecks()
End Sub
Public Sub TFPErrorLogUpdate()
'Insert Data into error log
If ErrorComment.Length > 399 Then
ErrorComment = ErrorComment.Substring(0, 399)
End If
cmd = New SqlCommand("INSERT INTO TFPErrorLog (ErrorDate, ErrorDescription, ErrorReferenceNumber, ErrorUserID, ErrorComment, ErrorDivision) values (@ErrorDate, @ErrorDescription, @ErrorReferenceNumber, @ErrorUserID, @ErrorComment, @ErrorDivision)", con)
With cmd.Parameters
.Add("@ErrorDate", SqlDbType.VarChar).Value = ErrorDate
.Add("@ErrorDescription", SqlDbType.VarChar).Value = ErrorDescription
.Add("@ErrorReferenceNumber", SqlDbType.VarChar).Value = ErrorReferenceNumber
.Add("@ErrorUserID", SqlDbType.VarChar).Value = ErrorUser
.Add("@ErrorComment", SqlDbType.VarChar).Value = ErrorComment
.Add("@ErrorDivision", SqlDbType.VarChar).Value = ErrorDivision
End With
If con.State = ConnectionState.Closed Then con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Sub
Public Sub ClearData()
If cboDivisionID.Text = "TFF" Or cboDivisionID.Text = "TOR" Or cboDivisionID.Text = "ALB" Then
cboVendorClass.Enabled = True
cboVendorClass.Text = "Canadian"
nbrDaysOutACH.Value = 1
Else
cboVendorClass.SelectedIndex = -1
cboVendorClass.Enabled = False
nbrDaysOutACH.Value = 1
End If
End Sub
Public Sub ClearVariables()
LastBatchNumber = 0
NextBatchNumber = 0
SelectedVoucher = 0
DiscountDays = 0
BatchTotal = 0
DiscountPercentage = 0
PaymentTerms = ""
CheckVoucherExists = 0
DuplicateVoucherExists = "NO"
SumVendorTotalPlus = 0
SumVendorTotalMinus = 0
SumVendorTotalNet = 0
End Sub
Private Sub LoadCompanyTotals()
Dim TotalOpenPayables, TotalPendingPayables, TotalPayablesRemaining As Double
'Check APVoucherTable
Dim TotalPendingPayablesStatement As String = "SELECT SUM(InvoiceTotal) FROM ReceiptOfInvoiceBatchLine WHERE DivisionID <> 'TST' AND VoucherStatus = @VoucherStatus"
Dim TotalPendingPayablesCommand As New SqlCommand(TotalPendingPayablesStatement, con)
TotalPendingPayablesCommand.Parameters.Add("@VoucherStatus", SqlDbType.VarChar).Value = "PENDING"
Dim TotalPayablesRemainingStatement As String = "SELECT SUM(InvoiceTotal) FROM ReceiptOfInvoiceBatchLine WHERE DivisionID <> 'TST' AND VoucherStatus = @VoucherStatus"
Dim TotalPayablesRemainingCommand As New SqlCommand(TotalPayablesRemainingStatement, con)
TotalPayablesRemainingCommand.Parameters.Add("@VoucherStatus", SqlDbType.VarChar).Value = "POSTED"
If con.State = ConnectionState.Closed Then con.Open()
Try
TotalPendingPayables = CDbl(TotalPendingPayablesCommand.ExecuteScalar)
Catch ex As Exception
TotalPendingPayables = 0
End Try
Try
TotalPayablesRemaining = CDbl(TotalPayablesRemainingCommand.ExecuteScalar)
Catch ex As Exception
TotalPendingPayables = 0
End Try
con.Close()
TotalOpenPayables = TotalPendingPayables + TotalPayablesRemaining
lblCompanyOpenPayables.Text = FormatCurrency(TotalOpenPayables, 2)
lblCompanyTotalRemaining.Text = FormatCurrency(TotalPayablesRemaining, 2)
lblCompanyTotalPending.Text = FormatCurrency(TotalPendingPayables, 2)
End Sub
Public Sub LoadDivisionTotals()
Dim DivisionOpenPayables, DivisionPendingPayables, DivisionRemainingPayables As Double
'Retrieve Discount Days for selected Payment Term
Dim SumOpenPayablesStatement As String = "SELECT SUM(InvoiceTotal) FROM ReceiptOfInvoiceBatchLine WHERE VoucherStatus = @VoucherStatus AND DivisionID = @DivisionID"
Dim SumOpenPayablesCommand As New SqlCommand(SumOpenPayablesStatement, con)
SumOpenPayablesCommand.Parameters.Add("@VoucherStatus", SqlDbType.VarChar).Value = "POSTED"
SumOpenPayablesCommand.Parameters.Add("@DivisionID", SqlDbType.VarChar).Value = cboDivisionID.Text
Dim SumPendingPayablesStatement As String = "SELECT SUM(InvoiceTotal) FROM ReceiptOfInvoiceBatchLine WHERE VoucherStatus = @VoucherStatus AND DivisionID = @DivisionID"
Dim SumPendingPayablesCommand As New SqlCommand(SumPendingPayablesStatement, con)
SumPendingPayablesCommand.Parameters.Add("@VoucherStatus", SqlDbType.VarChar).Value = "PENDING"
SumPendingPayablesCommand.Parameters.Add("@DivisionID", SqlDbType.VarChar).Value = cboDivisionID.Text
If con.State = ConnectionState.Closed Then con.Open()
Try
DivisionRemainingPayables = CDbl(SumOpenPayablesCommand.ExecuteScalar)
Catch ex As Exception
DivisionRemainingPayables = 0
End Try
Try
DivisionPendingPayables = CDbl(SumPendingPayablesCommand.ExecuteScalar)
Catch ex As Exception
DivisionPendingPayables = 0
End Try
con.Close()
DivisionOpenPayables = DivisionRemainingPayables + DivisionPendingPayables
lblTotalOpenPayables.Text = FormatCurrency(DivisionRemainingPayables, 2)
lblTotalPending.Text = FormatCurrency(DivisionPendingPayables, 2)
lblGrandTotal.Text = FormatCurrency(DivisionOpenPayables, 2)
End Sub
Private Sub cboVendorClass_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboVendorClass.SelectedIndexChanged
If cboVendorClass.Text = "Canadian" And (cboDivisionID.Text = "TFF" Or cboDivisionID.Text = "TOR" Or cboDivisionID.Text = "ALB") Then
ShowPostedVouchersCanadian()
ElseIf cboVendorClass.Text = "American" And (cboDivisionID.Text = "TFF" Or cboDivisionID.Text = "TOR" Or cboDivisionID.Text = "ALB") Then
ShowPostedVouchersAmerican()
ElseIf cboDivisionID.Text <> "TFF" And cboDivisionID.Text <> "TOR" And cboDivisionID.Text <> "ALB" Then
ShowPostedVouchers()
End If
End Sub
Private Sub cmdSelectLines_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSelectLines.Click
Dim InvoiceTotal, VendorTotal As Double
Dim VendorID, RowDivision As String
Dim DueDate, DiscountDate As Date
'********************************************************
'Update to get rid of Nulls
cmd = New SqlCommand("UPDATE ReceiptOfInvoiceBatchLine SET OnHold = @OnHold WHERE OnHold IS NULL", con)
With cmd.Parameters
.Add("@OnHold", SqlDbType.VarChar).Value = "NO"
End With
If con.State = ConnectionState.Closed Then con.Open()
cmd.ExecuteNonQuery()
con.Close()
'-------------------------------------------------------------------------------------
'Check #1 --- Check for due date
For Each row As DataGridViewRow In dgvPayables.Rows
Dim cell As DataGridViewCheckBoxCell = row.Cells("SelectForPayment")
Try
DueDate = row.Cells("DueDateColumn").Value
Catch ex As Exception
DueDate = dtpDueDate.Value
End Try
Try
InvoiceTotal = row.Cells("InvoiceTotalColumn").Value
Catch ex As Exception
InvoiceTotal = 0
End Try
Try
VendorID = row.Cells("VendorIDColumn").Value
Catch ex As Exception
VendorID = ""
End Try
Try
DiscountDate = row.Cells("DiscountDateColumn").Value
Catch ex As Exception
DiscountDate = dtpDueDate.Value
End Try
Try
RowDivision = row.Cells("DivisionIDColumn").Value
Catch ex As Exception
RowDivision = ""
End Try
If DueDate <= dtpDueDate.Value And InvoiceTotal >= 0 Then
cell.Value = "SELECTED"
Else
'Do nothing
End If
'Check to see if any credit can be taken
Dim SumVendorStatement As String = "SELECT SUM(InvoiceTotal) FROM ReceiptOfInvoiceBatchLine WHERE VendorID = @VendorID AND DivisionID = @DivisionID AND InvoiceTotal >= @InvoiceTotal AND VoucherStatus = @VoucherStatus AND (DueDate <= @DueDate OR DiscountDate <= @DiscountDate)"
Dim SumVendorCommand As New SqlCommand(SumVendorStatement, con)
SumVendorCommand.Parameters.Add("@VendorID", SqlDbType.VarChar).Value = VendorID
SumVendorCommand.Parameters.Add("@InvoiceTotal", SqlDbType.VarChar).Value = 0
SumVendorCommand.Parameters.Add("@DivisionID", SqlDbType.VarChar).Value = RowDivision
SumVendorCommand.Parameters.Add("@DueDate", SqlDbType.VarChar).Value = dtpDueDate.Text
SumVendorCommand.Parameters.Add("@VoucherStatus", SqlDbType.VarChar).Value = "POSTED"
SumVendorCommand.Parameters.Add("@DiscountDate", SqlDbType.VarChar).Value = dtpDueDate.Text
If con.State = ConnectionState.Closed Then con.Open()
Try
VendorTotal = CDbl(SumVendorCommand.ExecuteScalar)
Catch ex As Exception
VendorTotal = 0
End Try
con.Close()
If InvoiceTotal < 0 And (VendorTotal - (InvoiceTotal * -1) > 0) Then
cell.Value = "SELECTED"
Else
'Do nothing
End If
Next
'-------------------------------------------------------------------------------------
'Check #2 --- Check for discount date
For Each row As DataGridViewRow In dgvPayables.Rows
Dim cell1 As DataGridViewCheckBoxCell = row.Cells("SelectForPayment")
Try
DiscountDate = row.Cells("DiscountDateColumn").Value
Catch ex As Exception
DiscountDate = dtpDueDate.Value
End Try
Try
PaymentTerms = row.Cells("PaymentTermsColumn").Value
Catch ex As Exception
PaymentTerms = "N30"
End Try
Try
InvoiceTotal = row.Cells("InvoiceTotalColumn").Value
Catch ex As Exception
InvoiceTotal = 0
End Try
Try
VendorID = row.Cells("VendorIDColumn").Value
Catch ex As Exception
VendorID = ""
End Try
'Verifies that the line selected offers a discount
SelectDiscountDays()
If DiscountDays = 0 Then
'Do nothing - no discount
Else
If DiscountDate <= dtpDueDate.Value And InvoiceTotal >= 0 Then
cell1.Value = "SELECTED"
Else
'Do nothing
End If
End If
Next
'-------------------------------------------------------------------------------------
'Check #3 --- Unselect if credit exceeds the sum of the invoices for that vendor
For Each row As DataGridViewRow In dgvPayables.Rows
Dim cell2 As DataGridViewCheckBoxCell = row.Cells("SelectForPayment")
If cell2.Value = "SELECTED" Then
Try
DueDate = row.Cells("DueDateColumn").Value
Catch ex As Exception
DueDate = dtpDueDate.Value
End Try
Try
InvoiceTotal = row.Cells("InvoiceTotalColumn").Value
Catch ex As Exception
InvoiceTotal = 0
End Try
Try
VendorID = row.Cells("VendorIDColumn").Value
Catch ex As Exception
VendorID = ""
End Try
Try
RowDivision = row.Cells("DivisionIDColumn").Value
Catch ex As Exception
RowDivision = ""
End Try
Try
DiscountDate = row.Cells("DiscountDateColumn").Value
Catch ex As Exception
DiscountDate = dtpDueDate.Value
End Try
If DueDate <= dtpDueDate.Value And InvoiceTotal >= 0 Then
cell2.Value = "SELECTED"
Else
'Do nothing
End If
'Check to see of there is a credit that exceeds the sum of the invoices and un-select the invoices
Dim SumVendorPlusStatement As String = "SELECT SUM(InvoiceTotal) FROM ReceiptOfInvoiceBatchLine WHERE VendorID = @VendorID AND DivisionID = @DivisionID AND InvoiceTotal >= @InvoiceTotal AND VoucherStatus = @VoucherStatus AND (DueDate <= @DueDate OR DiscountDate <= @DiscountDate)"
Dim SumVendorPlusCommand As New SqlCommand(SumVendorPlusStatement, con)
SumVendorPlusCommand.Parameters.Add("@VendorID", SqlDbType.VarChar).Value = VendorID
SumVendorPlusCommand.Parameters.Add("@InvoiceTotal", SqlDbType.VarChar).Value = 0
SumVendorPlusCommand.Parameters.Add("@DivisionID", SqlDbType.VarChar).Value = RowDivision
SumVendorPlusCommand.Parameters.Add("@DueDate", SqlDbType.VarChar).Value = dtpDueDate.Text
SumVendorPlusCommand.Parameters.Add("@VoucherStatus", SqlDbType.VarChar).Value = "POSTED"
SumVendorPlusCommand.Parameters.Add("@DiscountDate", SqlDbType.VarChar).Value = dtpDueDate.Text
If con.State = ConnectionState.Closed Then con.Open()
Try
SumVendorTotalPlus = CDbl(SumVendorPlusCommand.ExecuteScalar)
Catch ex As Exception
SumVendorTotalPlus = 0
End Try
con.Close()
Dim SumVendorMinusStatement As String = "SELECT SUM(InvoiceTotal) FROM ReceiptOfInvoiceBatchLine WHERE VendorID = @VendorID AND DivisionID = @DivisionID AND InvoiceTotal < @InvoiceTotal AND VoucherStatus = @VoucherStatus"
Dim SumVendorMinusCommand As New SqlCommand(SumVendorMinusStatement, con)
SumVendorMinusCommand.Parameters.Add("@VendorID", SqlDbType.VarChar).Value = VendorID
SumVendorMinusCommand.Parameters.Add("@InvoiceTotal", SqlDbType.VarChar).Value = 0
SumVendorMinusCommand.Parameters.Add("@DivisionID", SqlDbType.VarChar).Value = RowDivision
'SumVendorMinusCommand.Parameters.Add("@DueDate", SqlDbType.VarChar).Value = dtpDueDate.Text
SumVendorMinusCommand.Parameters.Add("@VoucherStatus", SqlDbType.VarChar).Value = "POSTED"
'SumVendorMinusCommand.Parameters.Add("@DiscountDate", SqlDbType.VarChar).Value = dtpDueDate.Text
If con.State = ConnectionState.Closed Then con.Open()
Try
SumVendorTotalMinus = CDbl(SumVendorMinusCommand.ExecuteScalar)
Catch ex As Exception
SumVendorTotalMinus = 0
End Try
con.Close()
SumVendorTotalNet = SumVendorTotalPlus + SumVendorTotalMinus
If SumVendorTotalNet <= 0 Then
cell2.Value = "UNSELECTED"
Else
'Do nothing
End If
End If
Next
'-------------------------------------------------------------------------------------
'Check #4 --- Unselect if voucher is on hold
For Each row As DataGridViewRow In dgvPayables.Rows
Dim cell3 As DataGridViewCheckBoxCell = row.Cells("SelectForPayment")
Dim OnholdStatus As String = ""
If cell3.Value = "SELECTED" Then
Try
OnholdStatus = row.Cells("OnHoldColumn").Value
Catch ex As Exception
OnholdStatus = "NO"
End Try
If OnholdStatus = "YES" Then
cell3.Value = "UNSELECTED"
Else
'Skip
End If
End If
Next
'-------------------------------------------------------------------------------------
'Check #5 --- Unselect if voucher is ACH and more than one day out
For Each row As DataGridViewRow In dgvPayables.Rows
Dim cell4 As DataGridViewCheckBoxCell = row.Cells("SelectForPayment")
If cell4.Value = "SELECTED" Then
Dim CheckType As String = ""
Dim ACHDueDate As Date = Today()
Dim CheckDayOfWeek As Integer = 0
Dim NewACHDueDate As Date
Dim NumberOfDaysToAdd As Integer = 1
NumberOfDaysToAdd = nbrDaysOutACH.Value
CheckDayOfWeek = ACHDueDate.DayOfWeek
If CheckDayOfWeek = 5 And nbrDaysOutACH.Value = 1 Then
NewACHDueDate = ACHDueDate.AddDays(3)
Else
NewACHDueDate = ACHDueDate.AddDays(NumberOfDaysToAdd)
End If
Try
CheckType = row.Cells("CheckTypeColumn").Value
Catch ex As Exception
CheckType = "STANDARD"
End Try
Try
DiscountDate = row.Cells("DiscountDateColumn").Value
Catch ex As Exception
DiscountDate = dtpDueDate.Value
End Try
Try
DueDate = row.Cells("DueDateColumn").Value
Catch ex As Exception
DueDate = dtpDueDate.Value
End Try
If CheckType <> "STANDARD" And (DueDate > NewACHDueDate And DiscountDate > NewACHDueDate) Then
cell4.Value = "UNSELECTED"
Else
'Skip
End If
End If
Next
'-------------------------------------------------------------------------------------
'Update Database to show selected invoices in datagrid
For Each row As DataGridViewRow In dgvPayables.Rows
Dim cell6 As DataGridViewCheckBoxCell = row.Cells("SelectForPayment")
Dim RowVoucherNumber As Integer = 0
Dim RowDivisionID As String = ""
If cell6.Value = "SELECTED" Then
Try
RowVoucherNumber = row.Cells("VoucherNumberColumn").Value
Catch ex As Exception
RowVoucherNumber = 0
End Try
Try
RowDivisionID = row.Cells("DivisionIDColumn").Value
Catch ex As Exception
RowDivisionID = ""
End Try
'Update to selected
cmd = New SqlCommand("UPDATE ReceiptOfInvoiceBatchLine SET SelectedInDatagrid = @SelectedInDatagrid WHERE VoucherNumber = @VoucherNumber AND DivisionID = @DivisionID", con)
With cmd.Parameters
.Add("@VoucherNumber", SqlDbType.VarChar).Value = RowVoucherNumber
.Add("@DivisionID", SqlDbType.VarChar).Value = RowDivisionID
.Add("@SelectedInDatagrid", SqlDbType.VarChar).Value = "YES"
End With
If con.State = ConnectionState.Closed Then con.Open()
cmd.ExecuteNonQuery()
con.Close()
Else
Try
RowVoucherNumber = row.Cells("VoucherNumberColumn").Value
Catch ex As Exception
RowVoucherNumber = 0
End Try
Try
RowDivisionID = row.Cells("DivisionIDColumn").Value
Catch ex As Exception
RowDivisionID = ""
End Try
'Update to unselected
cmd = New SqlCommand("UPDATE ReceiptOfInvoiceBatchLine SET SelectedInDatagrid = @SelectedInDatagrid WHERE VoucherNumber = @VoucherNumber AND DivisionID = @DivisionID", con)
With cmd.Parameters
.Add("@VoucherNumber", SqlDbType.VarChar).Value = RowVoucherNumber
.Add("@DivisionID", SqlDbType.VarChar).Value = RowDivisionID
.Add("@SelectedInDatagrid", SqlDbType.VarChar).Value = "NO"
End With
If con.State = ConnectionState.Closed Then con.Open()
cmd.ExecuteNonQuery()
con.Close()
End If
Next
'-------------------------------------------------------------------------------------
'Check #6 --- Unselect if voucher is ACH and would produce a negative check for items selected
For Each row As DataGridViewRow In dgvPayables.Rows
Dim cell5 As DataGridViewCheckBoxCell = row.Cells("SelectForPayment")
If cell5.Value = "SELECTED" Then
Dim RowVendor As String = ""
Dim RowDivisionID As String = ""
Dim SumVendorTotalPlus As Double = 0
Dim SumVendorTotalMinus As Double = 0
Try
RowVendor = row.Cells("VendorIDColumn").Value
Catch ex As Exception
RowVendor = ""
End Try
Try
RowDivisionID = row.Cells("DivisionIDColumn").Value
Catch ex As Exception
RowDivisionID = ""
End Try
'Check to see of there is a credit that exceeds the sum of the invoices and un-select the invoices
Dim SumVendorPlusStatement As String = "SELECT SUM(InvoiceTotal) FROM ReceiptOfInvoiceBatchLine WHERE VendorID = @VendorID AND DivisionID = @DivisionID AND InvoiceTotal >= @InvoiceTotal AND SelectedInDatagrid = @SelectedInDatagrid AND VoucherStatus = @VoucherStatus"
Dim SumVendorPlusCommand As New SqlCommand(SumVendorPlusStatement, con)
SumVendorPlusCommand.Parameters.Add("@VendorID", SqlDbType.VarChar).Value = RowVendor
SumVendorPlusCommand.Parameters.Add("@InvoiceTotal", SqlDbType.VarChar).Value = 0
SumVendorPlusCommand.Parameters.Add("@DivisionID", SqlDbType.VarChar).Value = RowDivisionID
SumVendorPlusCommand.Parameters.Add("@SelectedInDatagrid", SqlDbType.VarChar).Value = "YES"
SumVendorPlusCommand.Parameters.Add("@VoucherStatus", SqlDbType.VarChar).Value = "POSTED"
If con.State = ConnectionState.Closed Then con.Open()
Try
SumVendorTotalPlus = CDbl(SumVendorPlusCommand.ExecuteScalar)
Catch ex As Exception
SumVendorTotalPlus = 0
End Try
con.Close()
Dim SumVendorMinusStatement As String = "SELECT SUM(InvoiceTotal) FROM ReceiptOfInvoiceBatchLine WHERE VendorID = @VendorID AND DivisionID = @DivisionID AND InvoiceTotal < @InvoiceTotal AND SelectedInDatagrid = @SelectedInDatagrid AND VoucherStatus = @VoucherStatus"
Dim SumVendorMinusCommand As New SqlCommand(SumVendorMinusStatement, con)
SumVendorMinusCommand.Parameters.Add("@VendorID", SqlDbType.VarChar).Value = RowVendor
SumVendorMinusCommand.Parameters.Add("@InvoiceTotal", SqlDbType.VarChar).Value = 0
SumVendorMinusCommand.Parameters.Add("@DivisionID", SqlDbType.VarChar).Value = RowDivisionID
SumVendorMinusCommand.Parameters.Add("@SelectedInDatagrid", SqlDbType.VarChar).Value = "YES"
SumVendorMinusCommand.Parameters.Add("@VoucherStatus", SqlDbType.VarChar).Value = "POSTED"
If con.State = ConnectionState.Closed Then con.Open()
Try
SumVendorTotalMinus = CDbl(SumVendorMinusCommand.ExecuteScalar)
Catch ex As Exception
SumVendorTotalMinus = 0
End Try
con.Close()
If SumVendorTotalPlus <= (SumVendorTotalMinus * -1) Then
cell5.Value = "UNSELECTED"
Else
'Skip
End If
End If
Next
'-------------------------------------------------------------------------------------
'Update Database to show selected invoices in datagrid
For Each row As DataGridViewRow In dgvPayables.Rows
Dim cell7 As DataGridViewCheckBoxCell = row.Cells("SelectForPayment")
Dim RowVoucherNumber As Integer = 0
Dim RowDivisionID As String = ""
If cell7.Value = "SELECTED" Then
Try
RowVoucherNumber = row.Cells("VoucherNumberColumn").Value
Catch ex As Exception
RowVoucherNumber = 0
End Try
Try
RowDivisionID = row.Cells("DivisionIDColumn").Value
Catch ex As Exception
RowDivisionID = ""
End Try
'Update to selected
cmd = New SqlCommand("UPDATE ReceiptOfInvoiceBatchLine SET SelectedInDatagrid = @SelectedInDatagrid WHERE VoucherNumber = @VoucherNumber AND DivisionID = @DivisionID", con)
With cmd.Parameters
.Add("@VoucherNumber", SqlDbType.VarChar).Value = RowVoucherNumber
.Add("@DivisionID", SqlDbType.VarChar).Value = RowDivisionID
.Add("@SelectedInDatagrid", SqlDbType.VarChar).Value = "YES"
End With
If con.State = ConnectionState.Closed Then con.Open()
cmd.ExecuteNonQuery()
con.Close()
Else
Try
RowVoucherNumber = row.Cells("VoucherNumberColumn").Value
Catch ex As Exception
RowVoucherNumber = 0
End Try
Try
RowDivisionID = row.Cells("DivisionIDColumn").Value
Catch ex As Exception
RowDivisionID = ""
End Try
'Update to unselected
cmd = New SqlCommand("UPDATE ReceiptOfInvoiceBatchLine SET SelectedInDatagrid = @SelectedInDatagrid WHERE VoucherNumber = @VoucherNumber AND DivisionID = @DivisionID", con)
With cmd.Parameters
.Add("@VoucherNumber", SqlDbType.VarChar).Value = RowVoucherNumber
.Add("@DivisionID", SqlDbType.VarChar).Value = RowDivisionID
.Add("@SelectedInDatagrid", SqlDbType.VarChar).Value = "NO"
End With
If con.State = ConnectionState.Closed Then con.Open()
cmd.ExecuteNonQuery()
con.Close()
End If
Next
'-------------------------------------------------------------------------------------
'Check #7 --- Unselect if voucher is ACH/INTERCOMPANY and would be selected over the weekend from a Friday
' Friday - if the 2,3,4 or 17,18,19 fall on a Friday, wait until Monday to process intercompany checks
'-------------------------------------------------------------------------------------
'Check if today is Friday - if not, skip this whole routine
Dim CheckFridayDate As Date = Today()
Dim FridaysDayOfWeek As Integer = 0
FridaysDayOfWeek = CheckFridayDate.Day
If CheckFridayDate.DayOfWeek = DayOfWeek.Friday Then
'If today is Friday - and today is the 2nd, 3rd, 4th, 17th, 18th, 19th - unselect Intercompany Vouchers
If FridaysDayOfWeek = 2 Or FridaysDayOfWeek = 3 Or FridaysDayOfWeek = 4 Or FridaysDayOfWeek = 17 Or FridaysDayOfWeek = 18 Or FridaysDayOfWeek = 19 Then
For Each row As DataGridViewRow In dgvPayables.Rows
Dim cell7 As DataGridViewCheckBoxCell = row.Cells("SelectForPayment")
If cell7.Value = "SELECTED" Then
Dim RowCheckType As String = ""
Try
RowCheckType = row.Cells("CheckTypeColumn").Value
Catch ex As Exception
RowCheckType = ""
End Try
'Uncheck if Intercompany
If RowCheckType = "INTERCOMPANY" Then
cell7.Value = "UNSELECTED"
Else
'Skip
End If
End If
Next
'-------------------------------------------------------------------------------------
'Update Database to show selected invoices in datagrid
For Each row As DataGridViewRow In dgvPayables.Rows
Dim cell8 As DataGridViewCheckBoxCell = row.Cells("SelectForPayment")
Dim RowVoucherNumber As Integer = 0
Dim RowDivisionID As String = ""
If cell8.Value = "SELECTED" Then
Try
RowVoucherNumber = row.Cells("VoucherNumberColumn").Value
Catch ex As Exception
RowVoucherNumber = 0
End Try
Try
RowDivisionID = row.Cells("DivisionIDColumn").Value
Catch ex As Exception
RowDivisionID = ""
End Try
'Update to selected
cmd = New SqlCommand("UPDATE ReceiptOfInvoiceBatchLine SET SelectedInDatagrid = @SelectedInDatagrid WHERE VoucherNumber = @VoucherNumber AND DivisionID = @DivisionID", con)
With cmd.Parameters
.Add("@VoucherNumber", SqlDbType.VarChar).Value = RowVoucherNumber
.Add("@DivisionID", SqlDbType.VarChar).Value = RowDivisionID
.Add("@SelectedInDatagrid", SqlDbType.VarChar).Value = "YES"
End With
If con.State = ConnectionState.Closed Then con.Open()
cmd.ExecuteNonQuery()
con.Close()
Else
Try
RowVoucherNumber = row.Cells("VoucherNumberColumn").Value
Catch ex As Exception
RowVoucherNumber = 0
End Try
Try
RowDivisionID = row.Cells("DivisionIDColumn").Value
Catch ex As Exception