-
Notifications
You must be signed in to change notification settings - Fork 0
/
APProcessSteelReturns.vb
2410 lines (2051 loc) · 130 KB
/
APProcessSteelReturns.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 APProcessSteelReturns
Inherits System.Windows.Forms.Form
Dim LineReturnNumber, LineReturnLine, LineQuantity, LastInventoryTransactionNumber, NextInventoryTransactionNumber, PONumber, LastVoucherNumber, NextVoucherNumber As Integer
Dim LongDescription, LinePartDescription, VendorName, InvoiceNumber, InvoiceDate, VendorID, Comment As String
Dim LineExtendedAmount, LineCost, LineExtendedCost, SUMInvoiceTotal, ProductTotal, InvoiceFreight, InvoiceSalesTax, InvoiceTotal As Double
Dim VendorClass, CheckStatus, VerifyVendor, UniqueInvoice, VoucherDate, VoucherStatus, PartNumber, PartDescription, LineComment, DebitGLAccount, CreditGLAccount As String
Dim DeleteReferenceNumber, GetReturnNumber, ReturnLineNumber As Integer
Dim DiscountAmount, SUMExtendedAmount, Cost, ExtendedAmount, InvoiceAmount, Quantity As Double
Dim ReturnDate As String
Dim DeleteDescription, DeletePartNumber, CheckVoucherStatus As String
Dim CheckType As String = ""
Dim VendorCheckType As String = ""
Dim ValidateCheckType As String = ""
'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;Connect Timeout=30")
Dim cmd, cmd1, cmd2, cmd3, cmd4, cmd5 As SqlCommand
Dim myAdapter, myAdapter1, myAdapter2, myAdapter3, myAdapter4, myAdapter5, myAdapter6 As New SqlDataAdapter
Dim comBuilder As SqlCommandBuilder
Dim ds, ds1, ds2, ds3, ds4, ds5, ds6 As DataSet
Dim dt As DataTable
'Form operations
Private Sub APProcessSteelReturns_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.CenterToScreen()
Call Disable(Me)
'Load default from Batch Form
txtDivisionID.Text = GlobalAPDivisionID
txtBatchNumber.Text = GlobalAPBatchNumber
LoadVendor()
LoadVoucher()
LoadPartNumber()
LoadGLAccount()
ClearData()
If GlobalVoucherNumber > 0 Then
cboVoucherNumber.Text = GlobalVoucherNumber
Else
cboVoucherNumber.SelectedIndex = -1
cboReturnNumber.SelectedIndex = -1
cboPONumber.SelectedIndex = -1
End If
End Sub
Private Sub APProcessReturns_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
Call Disable(Me)
End Sub
'Load Datasets into controls
Public Sub ShowVoucherLines()
'Load Batch Number table for specific division
cmd = New SqlCommand("SELECT * FROM ReceiptOfInvoiceVoucherLines WHERE VoucherNumber = @VoucherNumber", con)
cmd.Parameters.Add("@VoucherNumber", SqlDbType.VarChar).Value = Val(cboVoucherNumber.Text)
If con.State = ConnectionState.Closed Then con.Open()
ds = New DataSet()
myAdapter.SelectCommand = cmd
myAdapter.Fill(ds, "ReceiptOfInvoiceVoucherLines")
dgvVoucherLines.DataSource = ds.Tables("ReceiptOfInvoiceVoucherLines")
cboDeleteLine.DataSource = ds.Tables("ReceiptOfInvoiceVoucherLines")
con.Close()
End Sub
Public Sub ClearDatagrid()
dgvVoucherLines.DataSource = Nothing
End Sub
Public Sub LoadVendor()
'Load Vendor table for specific division
cmd = New SqlCommand("SELECT VendorCode FROM Vendor WHERE DivisionID = @DivisionID AND VendorClass = @VendorClass", con)
cmd.Parameters.Add("@DivisionID", SqlDbType.VarChar).Value = txtDivisionID.Text
cmd.Parameters.Add("@VendorClass", SqlDbType.VarChar).Value = "SteelVendor"
If con.State = ConnectionState.Closed Then con.Open()
ds1 = New DataSet()
myAdapter1.SelectCommand = cmd
myAdapter1.Fill(ds1, "Vendor")
cboVendorID.DataSource = ds1.Tables("Vendor")
con.Close()
cboVendorID.SelectedIndex = -1
End Sub
Public Sub LoadVoucher()
'Load Voucher Number for specific division
cmd = New SqlCommand("SELECT VoucherNumber FROM ReceiptOfInvoiceBatchLine WHERE BatchNumber = @BatchNumber AND VoucherSource = @VoucherSource AND DivisionID = @DivisionID", con)
cmd.Parameters.Add("@BatchNumber", SqlDbType.VarChar).Value = txtBatchNumber.Text
cmd.Parameters.Add("@VoucherSource", SqlDbType.VarChar).Value = "STEEL VENDOR RETURN"
cmd.Parameters.Add("@DivisionID", SqlDbType.VarChar).Value = txtDivisionID.Text
If con.State = ConnectionState.Closed Then con.Open()
ds2 = New DataSet()
myAdapter2.SelectCommand = cmd
myAdapter2.Fill(ds2, "ReceiptOfInvoiceBatchLine")
cboVoucherNumber.DataSource = ds2.Tables("ReceiptOfInvoiceBatchLine")
con.Close()
cboVoucherNumber.SelectedIndex = -1
End Sub
Public Sub LoadPONumber()
'Load Voucher Number for specific division
cmd = New SqlCommand("SELECT SteelPurchaseOrderKey FROM SteelPurchaseOrderHeader WHERE DivisionID = @DivisionID AND SteelVendorID = @SteelVendorID ORDER BY SteelPurchaseOrderKey DESC", con)
cmd.Parameters.Add("@DivisionID", SqlDbType.VarChar).Value = txtDivisionID.Text
cmd.Parameters.Add("@SteelVendorID", SqlDbType.VarChar).Value = cboVendorID.Text
If con.State = ConnectionState.Closed Then con.Open()
ds3 = New DataSet()
myAdapter3.SelectCommand = cmd
myAdapter3.Fill(ds3, "SteelPurchaseOrderHeader")
cboPONumber.DataSource = ds3.Tables("SteelPurchaseOrderHeader")
con.Close()
cboPONumber.SelectedIndex = -1
End Sub
Public Sub LoadReturnNumber()
'Load Voucher Number for specific division
cmd = New SqlCommand("SELECT SteelReturnNumber FROM SteelReturnHeaderTable WHERE SteelVendor = @SteelVendor AND DivisionID = @DivisionID AND ReturnStatus = @ReturnStatus", con)
cmd.Parameters.Add("@SteelVendor", SqlDbType.VarChar).Value = cboVendorID.Text
cmd.Parameters.Add("@DivisionID", SqlDbType.VarChar).Value = txtDivisionID.Text
cmd.Parameters.Add("@ReturnStatus", SqlDbType.VarChar).Value = "POSTED"
If con.State = ConnectionState.Closed Then con.Open()
ds4 = New DataSet()
myAdapter4.SelectCommand = cmd
myAdapter4.Fill(ds4, "SteelReturnHeaderTable")
cboReturnNumber.DataSource = ds4.Tables("SteelReturnHeaderTable")
con.Close()
cboReturnNumber.SelectedIndex = -1
End Sub
Public Sub LoadPartNumber()
'Load Vendor table for specific division
cmd = New SqlCommand("SELECT ItemID FROM NonInventoryItemList WHERE DivisionID = @DivisionID", con)
cmd.Parameters.Add("@DivisionID", SqlDbType.VarChar).Value = txtDivisionID.Text
If con.State = ConnectionState.Closed Then con.Open()
ds5 = New DataSet()
myAdapter5.SelectCommand = cmd
myAdapter5.Fill(ds5, "NonInventoryItemList")
cboLinePartNumber.DataSource = ds5.Tables("NonInventoryItemList")
con.Close()
cboLinePartNumber.SelectedIndex = -1
End Sub
Public Sub LoadGLAccount()
'Load Vendor table for specific division
cmd = New SqlCommand("SELECT GLAccountNumber, GLAccountShortDescription FROM GLAccounts", con)
If con.State = ConnectionState.Closed Then con.Open()
ds6 = New DataSet()
myAdapter6.SelectCommand = cmd
myAdapter6.Fill(ds6, "GLAccounts")
cboDebitAccount.DataSource = ds6.Tables("GLAccounts")
cboDebitDescription.DataSource = ds6.Tables("GLAccounts")
con.Close()
cboDebitAccount.SelectedIndex = -1
cboDebitDescription.SelectedIndex = -1
End Sub
'Load Data Commands
Public Sub LoadVendorData()
Dim VendorNameStatement As String = "SELECT VendorName FROM Vendor WHERE VendorCode = @VendorCode AND DivisionID = @DivisionID"
Dim VendorNameCommand As New SqlCommand(VendorNameStatement, con)
VendorNameCommand.Parameters.Add("@VendorCode", SqlDbType.VarChar).Value = cboVendorID.Text
VendorNameCommand.Parameters.Add("@DivisionID", SqlDbType.VarChar).Value = txtDivisionID.Text
Dim CheckTypeStatement As String = "SELECT CheckCode FROM Vendor WHERE VendorCode = @VendorCode AND DivisionID = @DivisionID"
Dim CheckTypeCommand As New SqlCommand(CheckTypeStatement, con)
CheckTypeCommand.Parameters.Add("@VendorCode", SqlDbType.VarChar).Value = cboVendorID.Text
CheckTypeCommand.Parameters.Add("@DivisionID", SqlDbType.VarChar).Value = txtDivisionID.Text
If con.State = ConnectionState.Closed Then con.Open()
Try
VendorName = CStr(VendorNameCommand.ExecuteScalar)
Catch ex As Exception
VendorName = ""
End Try
Try
VendorCheckType = CStr(CheckTypeCommand.ExecuteScalar)
Catch ex As Exception
VendorCheckType = ""
End Try
con.Close()
txtVendorName.Text = VendorName
cboCheckType.Text = VendorCheckType
End Sub
Private Sub CheckReturnAmount()
If Val(txtReturnAmount.Text) = 0 And InvoiceTotal <> 0 Then
MsgBox("You cannot round to zero.", MsgBoxStyle.OkOnly)
GlobalSteelReturnRounding = "EXIT SUB"
Exit Sub
Else
GlobalSteelReturnRounding = "CONTINUE"
End If
If Val(txtReturnAmount.Text) <> InvoiceTotal Then
Dim button As DialogResult = MessageBox.Show("The Calculated Total does not match the Invoice Amount. Do you wish to round the difference?", "ROUND DIFFERENCE", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)
If button = DialogResult.Yes Then
Dim SUMNewExtendedAmount, NewExtendedAmount, GetExtendedAmount As Double
Dim RoundingAmount As Double = 0
RoundingAmount = Val(txtReturnAmount.Text) - InvoiceTotal
Dim GetExtendedAmountStatement As String = "SELECT ExtendedAmount FROM ReceiptOfInvoiceVoucherLines WHERE VoucherNumber = @VoucherNumber AND VoucherLine = @VoucherLine"
Dim GetExtendedAmountCommand As New SqlCommand(GetExtendedAmountStatement, con)
GetExtendedAmountCommand.Parameters.Add("@VoucherNumber", SqlDbType.VarChar).Value = Val(cboVoucherNumber.Text)
GetExtendedAmountCommand.Parameters.Add("@VoucherLine", SqlDbType.VarChar).Value = 1
If con.State = ConnectionState.Closed Then con.Open()
Try
GetExtendedAmount = CDbl(GetExtendedAmountCommand.ExecuteScalar)
Catch ex1 As Exception
GetExtendedAmount = 0
End Try
con.Close()
NewExtendedAmount = GetExtendedAmount + RoundingAmount
NewExtendedAmount = Math.Round(NewExtendedAmount, 2)
'Update Line Database Table
cmd = New SqlCommand("UPDATE ReceiptOfInvoiceVoucherLines SET ExtendedAmount = @ExtendedAmount WHERE VoucherNumber = @VoucherNumber AND VoucherLine = @VoucherLine", con)
cmd.Parameters.Add("@VoucherNumber", SqlDbType.VarChar).Value = Val(cboVoucherNumber.Text)
cmd.Parameters.Add("@VoucherLine", SqlDbType.VarChar).Value = 1
cmd.Parameters.Add("@ExtendedAmount", SqlDbType.VarChar).Value = NewExtendedAmount
If con.State = ConnectionState.Closed Then con.Open()
cmd.ExecuteNonQuery()
con.Close()
Dim SUMNewExtendedAmountStatement As String = "SELECT SUM(ExtendedAmount) FROM ReceiptOfInvoiceVoucherLines WHERE VoucherNumber = @VoucherNumber"
Dim SUMNewExtendedAmountCommand As New SqlCommand(SUMNewExtendedAmountStatement, con)
SUMNewExtendedAmountCommand.Parameters.Add("@VoucherNumber", SqlDbType.VarChar).Value = Val(cboVoucherNumber.Text)
If con.State = ConnectionState.Closed Then con.Open()
Try
SUMNewExtendedAmount = CDbl(SUMNewExtendedAmountCommand.ExecuteScalar)
Catch ex1 As Exception
SUMNewExtendedAmount = 0
End Try
con.Close()
SUMNewExtendedAmount = Math.Round(SUMNewExtendedAmount, 2)
'Update Header Database Table
cmd = New SqlCommand("UPDATE ReceiptOfInvoiceBatchLine SET ProductTotal = @ProductTotal, InvoiceTotal = @InvoiceTotal WHERE VoucherNumber = @VoucherNumber AND DivisionID = @DivisionID", con)
cmd.Parameters.Add("@DivisionID", SqlDbType.VarChar).Value = txtDivisionID.Text
cmd.Parameters.Add("@VoucherNumber", SqlDbType.VarChar).Value = Val(cboVoucherNumber.Text)
cmd.Parameters.Add("@ProductTotal", SqlDbType.VarChar).Value = SUMNewExtendedAmount
cmd.Parameters.Add("@InvoiceTotal", SqlDbType.VarChar).Value = Val(txtReturnAmount.Text)
If con.State = ConnectionState.Closed Then con.Open()
cmd.ExecuteNonQuery()
con.Close()
ShowVoucherLines()
LoadVoucherTotals()
GlobalSteelReturnRounding = "CONTINUE"
ElseIf button = DialogResult.No Then
GlobalSteelReturnRounding = "EXIT SUB"
End If
End If
End Sub
Public Sub LoadPartDescription()
Dim PartDescriptionStatement As String = "SELECT ShortDescription FROM NonInventoryItemList WHERE ItemID = @ItemID AND DivisionID = @DivisionID"
Dim PartDescriptionCommand As New SqlCommand(PartDescriptionStatement, con)
PartDescriptionCommand.Parameters.Add("@ItemID", SqlDbType.VarChar).Value = cboLinePartNumber.Text
PartDescriptionCommand.Parameters.Add("@DivisionID", SqlDbType.VarChar).Value = txtDivisionID.Text
Dim DebitGLAccountStatement As String = "SELECT GLDebitAccount FROM NonInventoryItemList WHERE ItemID = @ItemID AND DivisionID = @DivisionID"
Dim DebitGLAccountCommand As New SqlCommand(DebitGLAccountStatement, con)
DebitGLAccountCommand.Parameters.Add("@ItemID", SqlDbType.VarChar).Value = cboLinePartNumber.Text
DebitGLAccountCommand.Parameters.Add("@DivisionID", SqlDbType.VarChar).Value = txtDivisionID.Text
If con.State = ConnectionState.Closed Then con.Open()
Try
PartDescription = CStr(PartDescriptionCommand.ExecuteScalar)
Catch ex As Exception
PartDescription = ""
End Try
Try
DebitGLAccount = CStr(DebitGLAccountCommand.ExecuteScalar)
Catch ex As Exception
DebitGLAccount = ""
End Try
con.Close()
txtLineDescription.Text = PartDescription
cboDebitAccount.Text = DebitGLAccount
End Sub
Public Sub LoadVoucherTotals()
Dim ProductTotalStatement As String = "SELECT SUM(ExtendedAmount) FROM ReceiptOfInvoiceVoucherLines WHERE VoucherNumber = @VoucherNumber"
Dim ProductTotalCommand As New SqlCommand(ProductTotalStatement, con)
ProductTotalCommand.Parameters.Add("@VoucherNumber", SqlDbType.VarChar).Value = Val(cboVoucherNumber.Text)
Dim FreightTotalStatement As String = "SELECT InvoiceFreight FROM ReceiptOfInvoiceBatchLine WHERE VoucherNumber = @VoucherNumber AND DivisionID = @DivisionID"
Dim FreightTotalCommand As New SqlCommand(FreightTotalStatement, con)
FreightTotalCommand.Parameters.Add("@VoucherNumber", SqlDbType.VarChar).Value = Val(cboVoucherNumber.Text)
FreightTotalCommand.Parameters.Add("@DivisionID", SqlDbType.VarChar).Value = txtDivisionID.Text
Dim ReturnTotalStatement As String = "SELECT InvoiceTotal FROM ReceiptOfInvoiceBatchLine WHERE VoucherNumber = @VoucherNumber AND DivisionID = @DivisionID"
Dim ReturnTotalCommand As New SqlCommand(ReturnTotalStatement, con)
ReturnTotalCommand.Parameters.Add("@VoucherNumber", SqlDbType.VarChar).Value = Val(cboVoucherNumber.Text)
ReturnTotalCommand.Parameters.Add("@DivisionID", SqlDbType.VarChar).Value = txtDivisionID.Text
If con.State = ConnectionState.Closed Then con.Open()
Try
ProductTotal = CDbl(ProductTotalCommand.ExecuteScalar)
Catch ex As Exception
ProductTotal = 0
End Try
Try
InvoiceFreight = CDbl(FreightTotalCommand.ExecuteScalar)
Catch ex As Exception
InvoiceFreight = 0
End Try
Try
InvoiceTotal = CDbl(ReturnTotalCommand.ExecuteScalar)
Catch ex As Exception
InvoiceTotal = 0
End Try
con.Close()
'Round Variables
InvoiceFreight = Math.Round(InvoiceFreight, 2)
ProductTotal = Math.Round(ProductTotal, 2)
InvoiceTotal = Math.Round(InvoiceTotal, 2)
InvoiceSalesTax = Math.Round(InvoiceSalesTax, 2)
txtFreightTotal.Text = InvoiceFreight
txtProductTotal.Text = FormatCurrency(ProductTotal, 2)
txtReturnTotal.Text = FormatCurrency(InvoiceTotal, 2)
End Sub
Public Sub ReloadVoucherTotals()
Dim ProductTotalStatement As String = "SELECT SUM(ExtendedAmount) FROM ReceiptOfInvoiceVoucherLines WHERE VoucherNumber = @VoucherNumber"
Dim ProductTotalCommand As New SqlCommand(ProductTotalStatement, con)
ProductTotalCommand.Parameters.Add("@VoucherNumber", SqlDbType.VarChar).Value = Val(cboVoucherNumber.Text)
If con.State = ConnectionState.Closed Then con.Open()
Try
ProductTotal = CDbl(ProductTotalCommand.ExecuteScalar)
Catch ex As Exception
ProductTotal = 0
End Try
con.Close()
InvoiceFreight = Val(txtFreightTotal.Text)
InvoiceTotal = Math.Round(ProductTotal + InvoiceFreight + InvoiceSalesTax, 2)
txtProductTotal.Text = FormatCurrency(ProductTotal, 2)
txtReturnTotal.Text = FormatCurrency(InvoiceTotal, 2)
End Sub
Public Sub UpdateBatchTotal()
Dim SUMInvoiceTotalStatement As String = "SELECT SUM(InvoiceTotal) FROM ReceiptOfInvoiceBatchLine WHERE BatchNumber = @BatchNumber AND DivisionID = @DivisionID"
Dim SUMInvoiceTotalCommand As New SqlCommand(SUMInvoiceTotalStatement, con)
SUMInvoiceTotalCommand.Parameters.Add("@BatchNumber", SqlDbType.VarChar).Value = Val(txtBatchNumber.Text)
SUMInvoiceTotalCommand.Parameters.Add("@DivisionID", SqlDbType.VarChar).Value = txtDivisionID.Text
If con.State = ConnectionState.Closed Then con.Open()
Try
SUMInvoiceTotal = CDbl(SUMInvoiceTotalCommand.ExecuteScalar)
Catch ex As Exception
SUMInvoiceTotal = 0
End Try
con.Close()
SUMInvoiceTotal = Math.Round(SUMInvoiceTotal, 2)
cmd = New SqlCommand("UPDATE ReceiptOfInvoiceBatchHeader SET BatchAmount = @BatchAmount WHERE BatchNumber = @BatchNumber AND DivisionID = @DivisionID", con)
cmd.Parameters.Add("@BatchNumber", SqlDbType.VarChar).Value = Val(txtBatchNumber.Text)
cmd.Parameters.Add("@BatchAmount", SqlDbType.VarChar).Value = SUMInvoiceTotal
cmd.Parameters.Add("@DivisionID", SqlDbType.VarChar).Value = txtDivisionID.Text
If con.State = ConnectionState.Closed Then con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Sub
Public Sub LoadReturnVoucherData()
Dim PONumberStatement As String = "SELECT PONumber FROM ReceiptOfInvoiceBatchLine WHERE VoucherNumber = @VoucherNumber AND BatchNumber = @BatchNumber AND DivisionID = @DivisionID"
Dim PONumberCommand As New SqlCommand(PONumberStatement, con)
PONumberCommand.Parameters.Add("@VoucherNumber", SqlDbType.VarChar).Value = Val(cboVoucherNumber.Text)
PONumberCommand.Parameters.Add("@BatchNumber", SqlDbType.VarChar).Value = Val(txtBatchNumber.Text)
PONumberCommand.Parameters.Add("@DivisionID", SqlDbType.VarChar).Value = txtDivisionID.Text
Dim InvoiceNumberStatement As String = "SELECT InvoiceNumber FROM ReceiptOfInvoiceBatchLine WHERE VoucherNumber = @VoucherNumber AND BatchNumber = @BatchNumber AND DivisionID = @DivisionID"
Dim InvoiceNumberCommand As New SqlCommand(InvoiceNumberStatement, con)
InvoiceNumberCommand.Parameters.Add("@VoucherNumber", SqlDbType.VarChar).Value = Val(cboVoucherNumber.Text)
InvoiceNumberCommand.Parameters.Add("@BatchNumber", SqlDbType.VarChar).Value = Val(txtBatchNumber.Text)
InvoiceNumberCommand.Parameters.Add("@DivisionID", SqlDbType.VarChar).Value = txtDivisionID.Text
Dim InvoiceDateStatement As String = "SELECT InvoiceDate FROM ReceiptOfInvoiceBatchLine WHERE VoucherNumber = @VoucherNumber AND BatchNumber = @BatchNumber AND DivisionID = @DivisionID"
Dim InvoiceDateCommand As New SqlCommand(InvoiceDateStatement, con)
InvoiceDateCommand.Parameters.Add("@VoucherNumber", SqlDbType.VarChar).Value = Val(cboVoucherNumber.Text)
InvoiceDateCommand.Parameters.Add("@BatchNumber", SqlDbType.VarChar).Value = Val(txtBatchNumber.Text)
InvoiceDateCommand.Parameters.Add("@DivisionID", SqlDbType.VarChar).Value = txtDivisionID.Text
Dim VendorIDStatement As String = "SELECT VendorID FROM ReceiptOfInvoiceBatchLine WHERE VoucherNumber = @VoucherNumber AND BatchNumber = @BatchNumber AND DivisionID = @DivisionID"
Dim VendorIDCommand As New SqlCommand(VendorIDStatement, con)
VendorIDCommand.Parameters.Add("@VoucherNumber", SqlDbType.VarChar).Value = Val(cboVoucherNumber.Text)
VendorIDCommand.Parameters.Add("@BatchNumber", SqlDbType.VarChar).Value = Val(txtBatchNumber.Text)
VendorIDCommand.Parameters.Add("@DivisionID", SqlDbType.VarChar).Value = txtDivisionID.Text
Dim ProductTotalStatement As String = "SELECT ProductTotal FROM ReceiptOfInvoiceBatchLine WHERE VoucherNumber = @VoucherNumber AND BatchNumber = @BatchNumber AND DivisionID = @DivisionID"
Dim ProductTotalCommand As New SqlCommand(ProductTotalStatement, con)
ProductTotalCommand.Parameters.Add("@VoucherNumber", SqlDbType.VarChar).Value = Val(cboVoucherNumber.Text)
ProductTotalCommand.Parameters.Add("@BatchNumber", SqlDbType.VarChar).Value = Val(txtBatchNumber.Text)
ProductTotalCommand.Parameters.Add("@DivisionID", SqlDbType.VarChar).Value = txtDivisionID.Text
Dim InvoiceFreightStatement As String = "SELECT InvoiceFreight FROM ReceiptOfInvoiceBatchLine WHERE VoucherNumber = @VoucherNumber AND BatchNumber = @BatchNumber AND DivisionID = @DivisionID"
Dim InvoiceFreightCommand As New SqlCommand(InvoiceFreightStatement, con)
InvoiceFreightCommand.Parameters.Add("@VoucherNumber", SqlDbType.VarChar).Value = Val(cboVoucherNumber.Text)
InvoiceFreightCommand.Parameters.Add("@BatchNumber", SqlDbType.VarChar).Value = Val(txtBatchNumber.Text)
InvoiceFreightCommand.Parameters.Add("@DivisionID", SqlDbType.VarChar).Value = txtDivisionID.Text
Dim InvoiceSalesTaxStatement As String = "SELECT InvoiceSalesTax FROM ReceiptOfInvoiceBatchLine WHERE VoucherNumber = @VoucherNumber AND BatchNumber = @BatchNumber AND DivisionID = @DivisionID"
Dim InvoiceSalesTaxCommand As New SqlCommand(InvoiceSalesTaxStatement, con)
InvoiceSalesTaxCommand.Parameters.Add("@VoucherNumber", SqlDbType.VarChar).Value = Val(cboVoucherNumber.Text)
InvoiceSalesTaxCommand.Parameters.Add("@BatchNumber", SqlDbType.VarChar).Value = Val(txtBatchNumber.Text)
InvoiceSalesTaxCommand.Parameters.Add("@DivisionID", SqlDbType.VarChar).Value = txtDivisionID.Text
Dim InvoiceTotalStatement As String = "SELECT InvoiceTotal FROM ReceiptOfInvoiceBatchLine WHERE VoucherNumber = @VoucherNumber AND BatchNumber = @BatchNumber AND DivisionID = @DivisionID"
Dim InvoiceTotalCommand As New SqlCommand(InvoiceTotalStatement, con)
InvoiceTotalCommand.Parameters.Add("@VoucherNumber", SqlDbType.VarChar).Value = Val(cboVoucherNumber.Text)
InvoiceTotalCommand.Parameters.Add("@BatchNumber", SqlDbType.VarChar).Value = Val(txtBatchNumber.Text)
InvoiceTotalCommand.Parameters.Add("@DivisionID", SqlDbType.VarChar).Value = txtDivisionID.Text
Dim CommentStatement As String = "SELECT Comment FROM ReceiptOfInvoiceBatchLine WHERE VoucherNumber = @VoucherNumber AND BatchNumber = @BatchNumber AND DivisionID = @DivisionID"
Dim CommentCommand As New SqlCommand(CommentStatement, con)
CommentCommand.Parameters.Add("@VoucherNumber", SqlDbType.VarChar).Value = Val(cboVoucherNumber.Text)
CommentCommand.Parameters.Add("@BatchNumber", SqlDbType.VarChar).Value = Val(txtBatchNumber.Text)
CommentCommand.Parameters.Add("@DivisionID", SqlDbType.VarChar).Value = txtDivisionID.Text
Dim VoucherStatusStatement As String = "SELECT VoucherStatus FROM ReceiptOfInvoiceBatchLine WHERE VoucherNumber = @VoucherNumber AND BatchNumber = @BatchNumber AND DivisionID = @DivisionID"
Dim VoucherStatusCommand As New SqlCommand(VoucherStatusStatement, con)
VoucherStatusCommand.Parameters.Add("@VoucherNumber", SqlDbType.VarChar).Value = Val(cboVoucherNumber.Text)
VoucherStatusCommand.Parameters.Add("@BatchNumber", SqlDbType.VarChar).Value = Val(txtBatchNumber.Text)
VoucherStatusCommand.Parameters.Add("@DivisionID", SqlDbType.VarChar).Value = txtDivisionID.Text
Dim InvoiceAmountStatement As String = "SELECT InvoiceAmount FROM ReceiptOfInvoiceBatchLine WHERE VoucherNumber = @VoucherNumber AND BatchNumber = @BatchNumber AND DivisionID = @DivisionID"
Dim InvoiceAmountCommand As New SqlCommand(InvoiceAmountStatement, con)
InvoiceAmountCommand.Parameters.Add("@VoucherNumber", SqlDbType.VarChar).Value = Val(cboVoucherNumber.Text)
InvoiceAmountCommand.Parameters.Add("@BatchNumber", SqlDbType.VarChar).Value = Val(txtBatchNumber.Text)
InvoiceAmountCommand.Parameters.Add("@DivisionID", SqlDbType.VarChar).Value = txtDivisionID.Text
Dim VoucherDateStatement As String = "SELECT InvoiceDate FROM ReceiptOfInvoiceBatchLine WHERE VoucherNumber = @VoucherNumber AND BatchNumber = @BatchNumber AND DivisionID = @DivisionID"
Dim VoucherDateCommand As New SqlCommand(VoucherDateStatement, con)
VoucherDateCommand.Parameters.Add("@VoucherNumber", SqlDbType.VarChar).Value = Val(cboVoucherNumber.Text)
VoucherDateCommand.Parameters.Add("@BatchNumber", SqlDbType.VarChar).Value = Val(txtBatchNumber.Text)
VoucherDateCommand.Parameters.Add("@DivisionID", SqlDbType.VarChar).Value = txtDivisionID.Text
Dim DeleteReferenceNumberStatement As String = "SELECT DeleteReferenceNumber FROM ReceiptOfInvoiceBatchLine WHERE VoucherNumber = @VoucherNumber AND BatchNumber = @BatchNumber AND DivisionID = @DivisionID"
Dim DeleteReferenceNumberCommand As New SqlCommand(DeleteReferenceNumberStatement, con)
DeleteReferenceNumberCommand.Parameters.Add("@VoucherNumber", SqlDbType.VarChar).Value = Val(cboVoucherNumber.Text)
DeleteReferenceNumberCommand.Parameters.Add("@BatchNumber", SqlDbType.VarChar).Value = Val(txtBatchNumber.Text)
DeleteReferenceNumberCommand.Parameters.Add("@DivisionID", SqlDbType.VarChar).Value = txtDivisionID.Text
Dim CheckTypeStatement As String = "SELECT CheckType FROM ReceiptOfInvoiceBatchLine WHERE VoucherNumber = @VoucherNumber AND BatchNumber = @BatchNumber AND DivisionID = @DivisionID"
Dim CheckTypeCommand As New SqlCommand(CheckTypeStatement, con)
CheckTypeCommand.Parameters.Add("@VoucherNumber", SqlDbType.VarChar).Value = Val(cboVoucherNumber.Text)
CheckTypeCommand.Parameters.Add("@BatchNumber", SqlDbType.VarChar).Value = Val(txtBatchNumber.Text)
CheckTypeCommand.Parameters.Add("@DivisionID", SqlDbType.VarChar).Value = txtDivisionID.Text
If con.State = ConnectionState.Closed Then con.Open()
Try
PONumber = CInt(PONumberCommand.ExecuteScalar)
Catch ex As Exception
PONumber = 0
End Try
Try
InvoiceNumber = CStr(InvoiceNumberCommand.ExecuteScalar)
Catch ex As Exception
InvoiceNumber = ""
End Try
Try
InvoiceDate = CStr(InvoiceDateCommand.ExecuteScalar)
Catch ex As Exception
InvoiceDate = ""
End Try
Try
VendorID = CStr(VendorIDCommand.ExecuteScalar)
Catch ex As Exception
VendorID = ""
End Try
Try
ProductTotal = CDbl(ProductTotalCommand.ExecuteScalar)
Catch ex As Exception
ProductTotal = 0
End Try
Try
InvoiceFreight = CDbl(InvoiceFreightCommand.ExecuteScalar)
Catch ex As Exception
InvoiceFreight = 0
End Try
Try
InvoiceTotal = CDbl(InvoiceTotalCommand.ExecuteScalar)
Catch ex As Exception
InvoiceTotal = 0
End Try
Try
Comment = CStr(CommentCommand.ExecuteScalar)
Catch ex As Exception
Comment = ""
End Try
Try
VoucherStatus = CStr(VoucherStatusCommand.ExecuteScalar)
Catch ex As Exception
VoucherStatus = ""
End Try
Try
InvoiceAmount = CDbl(InvoiceAmountCommand.ExecuteScalar)
Catch ex As Exception
InvoiceAmount = 0
End Try
Try
VoucherDate = CStr(VoucherDateCommand.ExecuteScalar)
Catch ex As Exception
VoucherDate = ""
End Try
Try
DeleteReferenceNumber = CInt(DeleteReferenceNumberCommand.ExecuteScalar)
Catch ex As Exception
DeleteReferenceNumber = 0
End Try
Try
CheckType = CStr(CheckTypeCommand.ExecuteScalar)
Catch ex As Exception
CheckType = "STANDARD"
End Try
con.Close()
cboVendorID.Text = VendorID
If PONumber = 0 Then
cboPONumber.SelectedIndex = -1
Else
cboPONumber.Text = PONumber
End If
txtInvoiceNumber.Text = InvoiceNumber
dtpReturnDate.Text = InvoiceDate
txtProductTotal.Text = FormatCurrency(ProductTotal, 2)
txtFreightTotal.Text = InvoiceFreight
txtReturnTotal.Text = FormatCurrency(InvoiceTotal, 2)
txtComment.Text = Comment
dtpReturnDate.Text = VoucherDate
cboReturnNumber.Text = DeleteReferenceNumber
txtReturnAmount.Text = InvoiceAmount
cboCheckType.Text = CheckType
If VoucherStatus = "POSTED" Or VoucherStatus = "CLOSED" Then
cmdDelete.Enabled = False
cmdSave.Enabled = False
SaveReturnVoucherToolStripMenuItem.Enabled = False
DeleteReturnVoucherToolStripMenuItem.Enabled = False
dgvVoucherLines.Enabled = False
Else
cmdDelete.Enabled = True
cmdSave.Enabled = True
SaveReturnVoucherToolStripMenuItem.Enabled = True
DeleteReturnVoucherToolStripMenuItem.Enabled = True
dgvVoucherLines.Enabled = True
End If
End Sub
Public Sub CheckAndUpdateSteelVendorLines()
Dim CheckLineReturnNumber As Integer = 0
Dim CheckLineReturnLineNumber As Integer = 0
Dim CheckQuantityVouched As Double = 0
Dim CheckQuantityReturned As Double = 0
If Me.dgvVoucherLines.RowCount > 0 Then
For Each row As DataGridViewRow In dgvVoucherLines.Rows
Try
CheckLineReturnNumber = row.Cells("ReceiverNumberColumn").Value
Catch ex As Exception
CheckLineReturnNumber = 0
End Try
Try
CheckLineReturnLineNumber = row.Cells("ReceiverLineColumn").Value
Catch ex As Exception
CheckLineReturnLineNumber = 0
End Try
'Get Total Quantity Vouched
Dim CheckQuantityVouchedStatement As String = "SELECT SUM(Quantity) FROM ReceiptOfInvoiceVoucherLines WHERE ReceiverNumber = @ReceiverNumber AND ReceiverLine = @ReceiverLine"
Dim CheckQuantityVouchedCommand As New SqlCommand(CheckQuantityVouchedStatement, con)
CheckQuantityVouchedCommand.Parameters.Add("@ReceiverNumber", SqlDbType.VarChar).Value = CheckLineReturnNumber
CheckQuantityVouchedCommand.Parameters.Add("@ReceiverLine", SqlDbType.VarChar).Value = CheckLineReturnLineNumber
If con.State = ConnectionState.Closed Then con.Open()
Try
CheckQuantityVouched = CDbl(CheckQuantityVouchedCommand.ExecuteScalar)
Catch ex As Exception
CheckQuantityVouched = 0
End Try
con.Close()
'Get Total Quantity Returned
Dim CheckQuantityReturnedStatement As String = "SELECT SUM(ReturnQuantity) FROM SteelReturnLineTable WHERE SteelReturnNumber = @SteelReturnNumber AND SteelReturnLine = @SteelReturnLine"
Dim CheckQuantityReturnedCommand As New SqlCommand(CheckQuantityReturnedStatement, con)
CheckQuantityReturnedCommand.Parameters.Add("@SteelReturnNumber", SqlDbType.VarChar).Value = CheckLineReturnNumber
CheckQuantityReturnedCommand.Parameters.Add("@SteelReturnLine", SqlDbType.VarChar).Value = CheckLineReturnLineNumber
If con.State = ConnectionState.Closed Then con.Open()
Try
CheckQuantityReturned = CInt(CheckQuantityReturnedCommand.ExecuteScalar)
Catch ex As Exception
CheckQuantityReturned = 0
End Try
con.Close()
If CheckQuantityVouched >= CheckQuantityReturned Then
'Close Vendor Return Lines
cmd = New SqlCommand("UPDATE SteelReturnLineTable SET LineStatus = @LineStatus WHERE SteelReturnNumber = @SteelReturnNumber AND SteelReturnLine = @SteelReturnLine", con)
With cmd.Parameters
.Add("@SteelReturnNumber", SqlDbType.VarChar).Value = CheckLineReturnNumber
.Add("@SteelReturnLine", SqlDbType.VarChar).Value = CheckLineReturnLineNumber
.Add("@LineStatus", SqlDbType.VarChar).Value = "CLOSED"
End With
If con.State = ConnectionState.Closed Then con.Open()
cmd.ExecuteNonQuery()
con.Close()
Else
'Re-open Vendor Return Lines
cmd = New SqlCommand("UPDATE SteelReturnLineTable SET LineStatus = @LineStatus WHERE SteelReturnNumber = @SteelReturnNumber AND SteelReturnLine = @SteelReturnLine", con)
With cmd.Parameters
.Add("@SteelReturnNumber", SqlDbType.VarChar).Value = CheckLineReturnNumber
.Add("@SteelReturnLine", SqlDbType.VarChar).Value = CheckLineReturnLineNumber
.Add("@LineStatus", SqlDbType.VarChar).Value = "POSTED"
End With
If con.State = ConnectionState.Closed Then con.Open()
cmd.ExecuteNonQuery()
con.Close()
End If
'Clear Variables
CheckLineReturnNumber = 0
CheckLineReturnLineNumber = 0
CheckQuantityVouched = 0
CheckQuantityReturned = 0
Next
'Close Return Header if necessary
Dim CountOpenLines As Integer = 0
Dim CountOpenLinesStatement As String = "SELECT COUNT(SteelReturnNumber) FROM SteelReturnLineTable WHERE SteelReturnNumber = @SteelReturnNumber AND LineStatus = @LineStatus"
Dim CountOpenLinesCommand As New SqlCommand(CountOpenLinesStatement, con)
CountOpenLinesCommand.Parameters.Add("@SteelReturnNumber", SqlDbType.VarChar).Value = CheckLineReturnNumber
CountOpenLinesCommand.Parameters.Add("@LineStatus", SqlDbType.VarChar).Value = CheckLineReturnLineNumber
If con.State = ConnectionState.Closed Then con.Open()
Try
CountOpenLines = CInt(CountOpenLinesCommand.ExecuteScalar)
Catch ex As Exception
CountOpenLines = 0
End Try
con.Close()
If CountOpenLines > 0 Then
'Re-open Return to be vouched again
cmd = New SqlCommand("UPDATE SteelReturnHeaderTable SET ReturnStatus = @ReturnStatus WHERE SteelReturnNumber = @SteelReturnNumber", con)
With cmd.Parameters
.Add("@SteelReturnNumber", SqlDbType.VarChar).Value = Val(cboReturnNumber.Text)
.Add("@ReturnStatus", SqlDbType.VarChar).Value = "POSTED"
End With
If con.State = ConnectionState.Closed Then con.Open()
cmd.ExecuteNonQuery()
con.Close()
Else
'Re-open Return to be vouched again
cmd = New SqlCommand("UPDATE SteelReturnHeaderTable SET ReturnStatus = @ReturnStatus WHERE SteelReturnNumber = @SteelReturnNumber", con)
With cmd.Parameters
.Add("@SteelReturnNumber", SqlDbType.VarChar).Value = Val(cboReturnNumber.Text)
.Add("@ReturnStatus", SqlDbType.VarChar).Value = "CLOSED"
End With
If con.State = ConnectionState.Closed Then con.Open()
cmd.ExecuteNonQuery()
con.Close()
End If
End If
End Sub
'Datagrid Operations
Private Sub dgvVoucherLines_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvVoucherLines.CellValueChanged
Dim LineReturnQuantity, LineExtendedAmount, LineUnitCost As Double
Dim LineNumber As Integer = 0
Dim LinePartDescription As String = ""
Dim LineReturnNumber, LineReturnLine As Integer
If Me.dgvVoucherLines.RowCount <> 0 Then
Dim RowIndex As Integer = Me.dgvVoucherLines.CurrentCell.RowIndex
Try
LineNumber = Me.dgvVoucherLines.Rows(RowIndex).Cells("VoucherLineColumn").Value
Catch ex As Exception
LineNumber = 0
End Try
Try
LineUnitCost = Me.dgvVoucherLines.Rows(RowIndex).Cells("UnitCostColumn").Value
Catch ex As Exception
LineUnitCost = 0
End Try
Try
LineReturnQuantity = Me.dgvVoucherLines.Rows(RowIndex).Cells("QuantityColumn").Value
Catch ex As Exception
LineReturnQuantity = 0
End Try
Try
LinePartDescription = Me.dgvVoucherLines.Rows(RowIndex).Cells("PartDescriptionColumn").Value
Catch ex As Exception
LinePartDescription = ""
End Try
Try
LineReturnNumber = Me.dgvVoucherLines.Rows(RowIndex).Cells("ReceiverNumberColumn").Value
Catch ex As Exception
LineReturnNumber = 0
End Try
Try
LineReturnLine = Me.dgvVoucherLines.Rows(RowIndex).Cells("ReceiverLineColumn").Value
Catch ex As Exception
LineReturnLine = 0
End Try
If LineUnitCost < 0 Then
MsgBox("Unit Cost must be a positive number. For a negative value, set quantity to a negative.", MsgBoxStyle.OkOnly)
Exit Sub
End If
LineExtendedAmount = LineUnitCost * LineReturnQuantity
'Round Variables
LineExtendedAmount = Math.Round(LineExtendedAmount, 2)
LineUnitCost = Math.Round(LineUnitCost, 4)
'UPDATE Vendor Return Lines on cell value changes
cmd = New SqlCommand("UPDATE ReceiptOfInvoiceVoucherLines SET PartDescription = @PartDescription, UnitCost = @UnitCost, Quantity = @Quantity, ExtendedAmount = @ExtendedAmount WHERE VoucherNumber = @VoucherNumber AND VoucherLine = @VoucherLine", con)
With cmd.Parameters
.Add("@VoucherNumber", SqlDbType.VarChar).Value = Val(cboVoucherNumber.Text)
.Add("@VoucherLine", SqlDbType.VarChar).Value = LineNumber
.Add("@PartDescription", SqlDbType.VarChar).Value = LinePartDescription
.Add("@UnitCost", SqlDbType.VarChar).Value = LineUnitCost
.Add("@Quantity", SqlDbType.VarChar).Value = LineReturnQuantity
.Add("@ExtendedAmount", SqlDbType.VarChar).Value = LineExtendedAmount
End With
If con.State = ConnectionState.Closed Then con.Open()
cmd.ExecuteNonQuery()
con.Close()
End If
'Load Totals and Update Header Table
LoadVoucherTotals()
'UPDATE Purchase Order Extended Amount based on line changes
cmd = New SqlCommand("UPDATE ReceiptOfInvoiceBatchLine SET ProductTotal = @ProductTotal, InvoiceFreight = @InvoiceFreight, InvoiceSalesTax = @InvoiceSalesTax, InvoiceTotal = @InvoiceTotal WHERE VoucherNumber = @VoucherNumber AND DivisionID = @DivisionID", con)
With cmd.Parameters
.Add("@VoucherNumber", SqlDbType.VarChar).Value = Val(cboVoucherNumber.Text)
.Add("@DivisionID", SqlDbType.VarChar).Value = txtDivisionID.Text
.Add("@ProductTotal", SqlDbType.VarChar).Value = ProductTotal
.Add("@InvoiceFreight", SqlDbType.VarChar).Value = InvoiceFreight
.Add("@InvoiceSalesTax", SqlDbType.VarChar).Value = InvoiceSalesTax
.Add("@InvoiceTotal", SqlDbType.VarChar).Value = InvoiceTotal
End With
If con.State = ConnectionState.Closed Then con.Open()
cmd.ExecuteNonQuery()
con.Close()
'Re-calculate lines
cmd = New SqlCommand("UPDATE ReceiptOfInvoiceVoucherLines SET ExtendedAmount = Quantity * UnitCost WHERE VoucherNumber = @VoucherNumber AND DivisionID = @DivisionID", con)
With cmd.Parameters
.Add("@VoucherNumber", SqlDbType.VarChar).Value = Val(cboVoucherNumber.Text)
.Add("@DivisionID", SqlDbType.VarChar).Value = txtDivisionID.Text
End With
If con.State = ConnectionState.Closed Then con.Open()
cmd.ExecuteNonQuery()
con.Close()
ShowVoucherLines()
''reloads totals for the lines after a change was made
ReloadVoucherTotals()
End Sub
'Validate, clear, and error checking
Public Sub ClearData()
cboVoucherNumber.Text = ""
cboPONumber.Refresh()
cboReturnNumber.Refresh()
cboVendorID.Refresh()
cboVoucherNumber.Refresh()
cboCheckType.Refresh()
txtComment.Refresh()
txtFreightTotal.Refresh()
txtProductTotal.Refresh()
txtReturnTotal.Refresh()
txtVendorName.Refresh()
txtInvoiceNumber.Refresh()
cboPONumber.SelectedIndex = -1
cboReturnNumber.SelectedIndex = -1
cboVendorID.SelectedIndex = -1
cboVoucherNumber.SelectedIndex = -1
cboLinePartNumber.SelectedIndex = -1
cboCheckType.SelectedIndex = -1
txtComment.Clear()
txtFreightTotal.Clear()
txtProductTotal.Clear()
txtReturnTotal.Clear()
txtVendorName.Clear()
txtInvoiceNumber.Clear()
txtLineUnitCost.Clear()
txtLineQuantity.Clear()
txtLineDescription.Clear()
txtLineExtendedAmount.Clear()
dtpReturnDate.Text = ""
dtpVoucherDate.Text = ""
cmdGenerateVoucher.Focus()
cmdDelete.Enabled = True
cmdSave.Enabled = True
SaveReturnVoucherToolStripMenuItem.Enabled = True
DeleteReturnVoucherToolStripMenuItem.Enabled = True
dgvVoucherLines.Enabled = True
End Sub
Public Sub ClearVariables()
LastInventoryTransactionNumber = 0
NextInventoryTransactionNumber = 0
PONumber = 0
LastVoucherNumber = 0
NextVoucherNumber = 0
VendorName = ""
InvoiceNumber = ""
InvoiceDate = ""
VendorID = ""
Comment = ""
VoucherStatus = ""
SUMInvoiceTotal = 0
ProductTotal = 0
InvoiceFreight = 0
InvoiceSalesTax = 0
InvoiceTotal = 0
PartNumber = ""
PartDescription = ""
LineComment = ""
DebitGLAccount = ""
CreditGLAccount = ""
ReturnLineNumber = 0
Quantity = 0
Cost = 0
ExtendedAmount = 0
LineCost = 0
LineExtendedAmount = 0
LineQuantity = 0
LinePartDescription = ""
LineExtendedCost = 0
InvoiceAmount = 0
GlobalVoucherNumber = 0
VerifyVendor = ""
UniqueInvoice = ""
CheckStatus = ""
DeleteReferenceNumber = 0
LineReturnLine = 0
LineReturnNumber = 0
SUMExtendedAmount = 0
DiscountAmount = 0
VendorClass = ""
CheckType = ""
VendorCheckType = ""
ValidateCheckType = ""
cmdDelete.Enabled = True
cmdSave.Enabled = True
SaveReturnVoucherToolStripMenuItem.Enabled = True
DeleteReturnVoucherToolStripMenuItem.Enabled = True
dgvVoucherLines.Enabled = True
End Sub
Public Sub VerifyUniqueInvoiceNumber()
Dim UniqueInvoiceStatement As String = "SELECT InvoiceNumber FROM ReceiptOfInvoiceBatchLine WHERE DivisionID = @DivisionID AND VoucherNumber <> @VoucherNumber AND InvoiceNumber = @InvoiceNumber AND VendorID = @VendorID"
Dim UniqueInvoiceCommand As New SqlCommand(UniqueInvoiceStatement, con)
UniqueInvoiceCommand.Parameters.Add("@DivisionID", SqlDbType.VarChar).Value = txtDivisionID.Text
UniqueInvoiceCommand.Parameters.Add("@VoucherNumber", SqlDbType.VarChar).Value = Val(cboVoucherNumber.Text)
UniqueInvoiceCommand.Parameters.Add("@InvoiceNumber", SqlDbType.VarChar).Value = txtInvoiceNumber.Text
UniqueInvoiceCommand.Parameters.Add("@VendorID", SqlDbType.VarChar).Value = cboVendorID.Text
If con.State = ConnectionState.Closed Then con.Open()
Try
UniqueInvoice = CStr(UniqueInvoiceCommand.ExecuteScalar)
Catch ex As Exception
UniqueInvoice = "YES"
End Try
con.Close()
If UniqueInvoice = "" Then
UniqueInvoice = "YES"
End If
End Sub
Public Sub TFPErrorLogUpdate()
'Insert Data into error log
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
'Command Buttons
Private Sub cmdExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExit.Click
'Validation
If cboVoucherNumber.Text <> "" And Val(cboVoucherNumber.Text) <> 0 Then
'Prompt before Saving
Dim button As DialogResult = MessageBox.Show("Do you wish to save this Vendor Return before exiting? (If NO, Voucher will be deleted)", "SAVE VENDOR RETURN", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)
If button = DialogResult.Yes Then
VerifyUniqueInvoiceNumber()
If UniqueInvoice = "YES" And txtInvoiceNumber.Text <> "" Then
'Validate Vendor
Dim VerifyVendorStatement As String = "SELECT VendorCode FROM Vendor WHERE VendorCode = @VendorCode AND DivisionID = @DivisionID"
Dim VerifyVendorCommand As New SqlCommand(VerifyVendorStatement, con)
VerifyVendorCommand.Parameters.Add("@VendorCode", SqlDbType.VarChar).Value = cboVendorID.Text
VerifyVendorCommand.Parameters.Add("@DivisionID", SqlDbType.VarChar).Value = txtDivisionID.Text
If con.State = ConnectionState.Closed Then con.Open()
Try
VerifyVendor = CStr(VerifyVendorCommand.ExecuteScalar)
Catch ex As Exception
VerifyVendor = ""
End Try
con.Close()
If VerifyVendor = "" Then
MsgBox("You must have a valid Vendor to process the return.", MsgBoxStyle.OkOnly)
Exit Sub
End If
'***************************************************************************************************
'Check to see if Voucher is already posted
Dim CheckVoucherStatusStatement As String = "SELECT VoucherStatus FROM ReceiptOfInvoiceBatchLine WHERE VoucherNumber = @VoucherNumber AND DivisionID = @DivisionID"
Dim CheckVoucherStatusCommand As New SqlCommand(CheckVoucherStatusStatement, con)
CheckVoucherStatusCommand.Parameters.Add("@VoucherNumber", SqlDbType.VarChar).Value = Val(cboVoucherNumber.Text)
CheckVoucherStatusCommand.Parameters.Add("@DivisionID", SqlDbType.VarChar).Value = txtDivisionID.Text
If con.State = ConnectionState.Closed Then con.Open()
Try
CheckVoucherStatus = CStr(CheckVoucherStatusCommand.ExecuteScalar)
Catch ex As Exception