Advertisement
Will94566

PMTPOST01 Import AP Payment Batch

Jul 10th, 2018
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 11.81 KB | None | 0 0
  1. Public strFileName, strPostingDate, parsed_line As String
  2. Public booNewHeader As Boolean
  3. Public intPostYear, intPostMonth, intPostDay As Integer
  4. Public intRecordCount, intRowsProcessed As Integer
  5. Public PaymentArray() As String
  6.  
  7. Sub MainSub()
  8. '
  9. ' Sage 300 Macro file: \\SAPP002\Sage\Sage 300 ERP\Macros\PMTPOST01.AVB
  10. ' Recorded at: Mon Jul 02 15:56:48 2018
  11. '
  12.  
  13.  
  14. On Error GoTo ACCPACErrorHandler
  15. frmInvoiceOpen.Show
  16. If VBA.Len(strFileName) = 0 Then
  17.    MsgBox "No file specified. Program exiting without import"
  18.    End
  19. End If
  20.    
  21. '    MsgBox "File Name = " + strFileName + VBA.Chr(13) + "Posting Date = " + strPostingDate
  22. intPostYear = VBA.Val(VBA.Left(strPostingDate, 4))
  23. intPostMonth = VBA.Val(VBA.Mid(strPostingDate, 5, 2))
  24. intPostDay = VBA.Val(VBA.Right(strPostingDate, 2))
  25.  
  26.  
  27. TransformInputFile
  28. CreateBatch
  29. MsgBox VBA.Format(intRowsProcessed, "#,###") + " Records were processed.", vbOKOnly
  30.  
  31. Exit Sub
  32.  
  33. ACCPACErrorHandler:
  34.   Dim lCount As Long
  35.   Dim lIndex As Long
  36.  
  37.   If Errors Is Nothing Then
  38.        MsgBox Err.Description
  39.   Else
  40.       lCount = Errors.Count
  41.  
  42.       If lCount = 0 Then
  43.           MsgBox Err.Description
  44.       Else
  45.           For lIndex = 0 To lCount - 1
  46.               MsgBox Errors.Item(lIndex)
  47.           Next
  48.           Errors.Clear
  49.      
  50.       End If
  51.       Resume Next
  52.  
  53.   End If
  54. Close All
  55.  
  56. End Sub
  57.  
  58. Sub TransformInputFile()
  59.  
  60. Dim file_name As String
  61. Dim fnum As Integer
  62. Dim whole_file As String
  63. Dim lines As Variant
  64. Dim first_line() As Variant
  65. Dim raw_line As String
  66. Dim one_line() As String
  67. Dim num_rows As Long
  68. Dim num_cols As Long
  69. Dim r As Long
  70. Dim C As Long
  71.  
  72.     file_name = strFileName
  73.  
  74.     ' Load the file.
  75.     fnum = FreeFile()
  76.     Open file_name For Input As fnum
  77.     whole_file = Input$(LOF(fnum), #fnum)
  78.     Close fnum
  79.  
  80.     ' Break the file into lines.
  81.     lines = Split(whole_file, vbCrLf)
  82.  
  83.     ' Dimension the array.
  84.     num_rows = UBound(lines) - 1
  85.     num_cols = 24
  86.     ReDim PaymentArray(num_rows, num_cols)
  87.     intRecordCount = UBound(lines) - 1
  88.  
  89.     ' Copy the data into the array.
  90.     For r = 1 To num_rows
  91.         If Len(lines(r)) > 0 Then
  92.             raw_line = lines(r)
  93.             ParseLine (raw_line)
  94.             one_line = Split(parsed_line, "|")
  95.             For C = 0 To num_cols
  96.                 If IsNull(one_line(C)) Then
  97.                     PaymentArray(r, C) = ""
  98.                 Else
  99.                     PaymentArray(r, C) = one_line(C)
  100.                 End If
  101.             Next C
  102.         End If
  103.     Next r
  104.  
  105.     ' Prove we have the data loaded.
  106.     For r = 0 To num_rows
  107.         For C = 0 To num_cols
  108.             Debug.Print PaymentArray(r, C) & "|";
  109.         Next C
  110.         Debug.Print
  111.     Next r
  112.     Debug.Print "======="
  113.  
  114. End Sub
  115.  
  116. Sub CreateBatch()
  117.  
  118. ' Declare custom objects and variables
  119. Dim intDocYear, intDocMonth, intDocDay As Integer
  120. Dim i, k, r, C, intPaymentCount As Integer
  121. Dim strGLACCT, strDescription, strDocumentNumber As String
  122. Dim curPaymentTotal As Currency
  123.  
  124.  
  125. ' Sage-generated DB connections, views and view composition
  126. Dim mDBLinkCmpRW As AccpacCOMAPI.AccpacDBLink
  127. Set mDBLinkCmpRW = OpenDBLink(DBLINK_COMPANY, DBLINK_FLG_READWRITE)
  128.  
  129. Dim mDBLinkSysRW As AccpacCOMAPI.AccpacDBLink
  130. Set mDBLinkSysRW = OpenDBLink(DBLINK_SYSTEM, DBLINK_FLG_READWRITE)
  131.  
  132. Dim temp As Boolean
  133. Dim APPAYMENT1batch As AccpacCOMAPI.AccpacView
  134. Dim APPAYMENT1batchFields As AccpacCOMAPI.AccpacViewFields
  135. mDBLinkCmpRW.OpenView "AP0030", APPAYMENT1batch
  136. Set APPAYMENT1batchFields = APPAYMENT1batch.Fields
  137.  
  138. Dim APPAYMENT1header As AccpacCOMAPI.AccpacView
  139. Dim APPAYMENT1headerFields As AccpacCOMAPI.AccpacViewFields
  140. mDBLinkCmpRW.OpenView "AP0031", APPAYMENT1header
  141. Set APPAYMENT1headerFields = APPAYMENT1header.Fields
  142.  
  143. Dim APPAYMENT1detail1 As AccpacCOMAPI.AccpacView
  144. Dim APPAYMENT1detail1Fields As AccpacCOMAPI.AccpacViewFields
  145. mDBLinkCmpRW.OpenView "AP0033", APPAYMENT1detail1
  146. Set APPAYMENT1detail1Fields = APPAYMENT1detail1.Fields
  147.  
  148. Dim APPAYMENT1detail2 As AccpacCOMAPI.AccpacView
  149. Dim APPAYMENT1detail2Fields As AccpacCOMAPI.AccpacViewFields
  150. mDBLinkCmpRW.OpenView "AP0034", APPAYMENT1detail2
  151. Set APPAYMENT1detail2Fields = APPAYMENT1detail2.Fields
  152.  
  153. Dim APPAYMENT1detail3 As AccpacCOMAPI.AccpacView
  154. Dim APPAYMENT1detail3Fields As AccpacCOMAPI.AccpacViewFields
  155. mDBLinkCmpRW.OpenView "AP0032", APPAYMENT1detail3
  156. Set APPAYMENT1detail3Fields = APPAYMENT1detail3.Fields
  157.  
  158. Dim APPAYMENT1detail4 As AccpacCOMAPI.AccpacView
  159. Dim APPAYMENT1detail4Fields As AccpacCOMAPI.AccpacViewFields
  160. mDBLinkCmpRW.OpenView "AP0048", APPAYMENT1detail4
  161. Set APPAYMENT1detail4Fields = APPAYMENT1detail4.Fields
  162.  
  163. Dim APPAYMENT1detail5 As AccpacCOMAPI.AccpacView
  164. Dim APPAYMENT1detail5Fields As AccpacCOMAPI.AccpacViewFields
  165. mDBLinkCmpRW.OpenView "AP0170", APPAYMENT1detail5
  166. Set APPAYMENT1detail5Fields = APPAYMENT1detail5.Fields
  167.  
  168. Dim APPAYMENT1detail6 As AccpacCOMAPI.AccpacView
  169. Dim APPAYMENT1detail6Fields As AccpacCOMAPI.AccpacViewFields
  170. mDBLinkCmpRW.OpenView "AP0406", APPAYMENT1detail6
  171. Set APPAYMENT1detail6Fields = APPAYMENT1detail6.Fields
  172.  
  173. APPAYMENT1batch.Compose Array(APPAYMENT1header)
  174. APPAYMENT1header.Compose Array(APPAYMENT1batch, APPAYMENT1detail3, APPAYMENT1detail1, APPAYMENT1detail6, APPAYMENT1detail5)
  175. APPAYMENT1detail1.Compose Array(APPAYMENT1header, APPAYMENT1detail2, APPAYMENT1detail4)
  176. APPAYMENT1detail2.Compose Array(APPAYMENT1detail1)
  177. APPAYMENT1detail3.Compose Array(APPAYMENT1header)
  178. APPAYMENT1detail4.Compose Array(APPAYMENT1batch, APPAYMENT1header, APPAYMENT1detail3, APPAYMENT1detail1, APPAYMENT1detail2)
  179. APPAYMENT1detail5.Compose Array(APPAYMENT1header)
  180. APPAYMENT1detail6.Compose Array(APPAYMENT1header)
  181.  
  182. Dim APPAYMPOST2 As AccpacCOMAPI.AccpacView
  183. Dim APPAYMPOST2Fields As AccpacCOMAPI.AccpacViewFields
  184. mDBLinkCmpRW.OpenView "AP0040", APPAYMPOST2
  185. Set APPAYMPOST2Fields = APPAYMPOST2.Fields
  186.  
  187. ' Initialize batch (One time only)
  188. APPAYMENT1batch.RecordClear
  189. APPAYMENT1batchFields("PAYMTYPE").PutWithoutVerification ("PY")       ' Batch Selector
  190. APPAYMENT1headerFields("BTCHTYPE").PutWithoutVerification ("PY")      ' Batch Type
  191. APPAYMENT1detail3Fields("BATCHTYPE").PutWithoutVerification ("PY")    ' Batch Type
  192. APPAYMENT1detail1Fields("BATCHTYPE").PutWithoutVerification ("PY")    ' Batch Type
  193. APPAYMENT1detail2Fields("BATCHTYPE").PutWithoutVerification ("PY")    ' Batch Type
  194. APPAYMENT1batchFields("PAYMTYPE").PutWithoutVerification ("PY")       ' Batch Selector
  195. APPAYMENT1batchFields("CNTBTCH").PutWithoutVerification ("0")         ' Batch Number
  196. APPAYMENT1batch.RecordCreate 1
  197. APPAYMENT1batchFields("PROCESSCMD").PutWithoutVerification ("1")      ' Process Command Code
  198. APPAYMENT1batch.Process
  199. APPAYMENT1batch.Browse "((PAYMTYPE = ""PY"") AND ((BATCHSTAT = 1) OR (BATCHSTAT = 7) OR (BATCHSTAT = 8)))", 1
  200. APPAYMENT1batchFields("BATCHDESC").PutWithoutVerification ("BDC Payments Import " & PaymentArray(1, 0) & " (Process Date)") ' Description
  201. APPAYMENT1batch.Update
  202. intDocYear = VBA.DateTime.Year(PaymentArray(1, 0))                     ' Set DocDate to ProcessDate
  203. intDocMonth = VBA.DateTime.Month(PaymentArray(1, 0))
  204. intDocDay = VBA.DateTime.Day(PaymentArray(1, 0))
  205. APPAYMENT1batchFields("DATEBTCH").Value = DateSerial(intDocYear, intDocMonth, intDocDay)
  206. APPAYMENT1batchFields("IDBANK").Value = "CBTCHK"                       ' Bank Code
  207. APPAYMENT1batch.Update
  208. APPAYMENT1header.RecordCreate 2 ' Create first header record
  209.  
  210. 'Record Detail Processing Loop
  211. intRowsProcessed = 0
  212. intPaymentCount = 1
  213. booNewHeader = True
  214. For i = 1 To (intRecordCount) ' Process the next invoice record
  215.     'Header Test to see if this Payment Number is the same as the previous line
  216.     If booNewHeader Then
  217.         ' Begin Header Loop: assign values to payment header (vendor and payment data)
  218.         booNewHeader = False
  219.         k = 0
  220.         strDocumentNumber = PaymentArray(i, 4)
  221.         APPAYMENT1batch.Read
  222.         APPAYMENT1headerFields("TEXTRMIT").Value = PaymentArray(i, 5) & " BDC#" & PaymentArray(i, 4)    ' Create new payment header after last detail line (even if no more header records are needed)
  223.         APPAYMENT1headerFields("RMITTYPE").Value = "4"                        ' Payment Trans. Type
  224.         APPAYMENT1headerFields("PROCESSCMD").PutWithoutVerification ("0")     ' Process Command Code
  225.         APPAYMENT1header.Process
  226.         APPAYMENT1headerFields("IDVEND").Value = PaymentArray(i, 6)           ' Vendor Number
  227.         APPAYMENT1headerFields("CNTENTR").PutWithoutVerification ("0")        ' Entry Number
  228.         intDocYear = VBA.DateTime.Year(PaymentArray(i, 21))
  229.         intDocMonth = VBA.DateTime.Month(PaymentArray(i, 21))
  230.         intDocDay = VBA.DateTime.Day(PaymentArray(i, 21))
  231.         APPAYMENT1headerFields("DATERMIT").Value = DateSerial(intDocYear, intDocMonth, intDocDay)        ' Payment Date/Adjustment Date
  232.         APPAYMENT1headerFields("PROCESSCMD").PutWithoutVerification ("0")      ' Process Command Code
  233.         APPAYMENT1header.Process
  234.         APPAYMENT1headerFields("IDACCTSET").Value = "STD"                      ' Account Set
  235.         APPAYMENT1headerFields("PAYMCODE").Value = "BDC"                       ' Payment Code
  236.         APPAYMENT1headerFields("SWPRNTRMIT").Value = "0"                       ' Check Print Required
  237.         APPAYMENT1headerFields("IDRMIT").Value = Right(PaymentArray(i, 4), 7)  ' Check Number
  238.         APPAYMENT1headerFields("TXTRMITREF").Value = "BDC " & PaymentArray(i, 22)         ' Entry Reference
  239.         APPAYMENT1detail1.RecordClear
  240.      End If
  241.     'Begin Detail Loop: Initialize and assign values to payment detail lines (specific invoices being paid)
  242.     k = k - 1
  243.     APPAYMENT1detail1.RecordCreate 0
  244.     APPAYMENT1detail1Fields("IDINVC").Value = PaymentArray(i, 17)          ' Document Number
  245.     APPAYMENT1detail1Fields("PROCESSCMD").Value = "12"                     ' Process Command
  246.     APPAYMENT1detail1.Process
  247.     APPAYMENT1detail1Fields("AMTPAYM").Value = PaymentArray(i, 18)         ' Payment Amount
  248.     APPAYMENT1detail1Fields("CNTLINE").PutWithoutVerification (k)          ' Line Number (k)
  249.     APPAYMENT1detail1.Insert
  250.     'Test for end of batch before looking ahead in array to see if we need a new payment record
  251.     If i < (intRecordCount) Then
  252.         'See if the next record is a line on the same remittance
  253.         If Trim(strDocumentNumber) <> Trim(PaymentArray(i + 1, 4)) Then
  254.             ' Create new payment header after last detail line (even if no more header records are needed)
  255.             APPAYMENT1header.Insert
  256.             APPAYMENT1batch.Read
  257.             APPAYMENT1headerFields("CNTENTR").PutWithoutVerification ("0")  ' Entry Number
  258.             APPAYMENT1header.RecordCreate 2
  259.             booNewHeader = True
  260.         Else
  261.             APPAYMENT1detail1.Read
  262.             APPAYMENT1detail1.Update
  263.         End If
  264.     Else
  265.         ' Create new payment header after last detail line (even if no more header records are needed)
  266.         APPAYMENT1header.Insert  'THIS IS WHERE IT RETURNS THE ERROR: INVOICE NUMBER CANNOT BE BLANK
  267.         APPAYMENT1batch.Read
  268.         APPAYMENT1headerFields("CNTENTR").PutWithoutVerification ("0")       ' Entry Number
  269.         APPAYMENT1header.RecordCreate 2
  270.         booNewHeader = True
  271.     End If
  272.     intRowsProcessed = intRowsProcessed + 1
  273. Next
  274.  
  275. End Sub
  276.  
  277. Function ParseLine(ByVal oneLine As String) As String()
  278.         ' Returns an array containing the values of the comma-separated fields.
  279.         ' This pattern actually recognizes the correct commas.
  280.         ' The Regex.Split() command later gets text between the commas.
  281.         Dim strPattern As String: strPattern = ",(?=(?:[^""]*""[^""]*"")*(?![^""]*""))"
  282.         Dim strReplace As String: strReplace = "|"
  283.         Dim regEx As New RegExp
  284.         Dim ColCount() As Variant
  285.         With regEx
  286.             .Global = True
  287.             .MultiLine = False
  288.             .IgnoreCase = False
  289.             .pattern = strPattern
  290.         End With
  291.         parsed_line = regEx.Replace(oneLine, strReplace)
  292.     End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement