Skip to content

Commit

Permalink
Merge pull request #11 from starkbank/refactor/credited-duplicates
Browse files Browse the repository at this point in the history
Fix boleto log query for duplicates
  • Loading branch information
matheuscferraz authored Aug 5, 2020
2 parents 8fbcf82 + 76e4646 commit 49b54cb
Show file tree
Hide file tree
Showing 11 changed files with 210 additions and 34 deletions.
105 changes: 78 additions & 27 deletions src.vba/ChargeGateway.bas
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,10 @@ Public Function getCustomers(cursor As String, optionalParam As Dictionary)

End Function

Public Function getOrders() As Collection
Public Function getOrders(initRow As Long, midRow As Long) As Collection
Dim orders As New Collection

For Each obj In SheetParser.dict
For Each obj In SheetParser.longDict(initRow, midRow)
Dim amount As Long
Dim customerId As String
Dim dueDate As String
Expand Down Expand Up @@ -207,11 +207,51 @@ Public Function getOrders() As Collection
Set order = ChargeGateway.order(amount, customerId, dueDate, fine, interest, overdueLimit, description1, description2, description3)

orders.Add order

Next
Set getOrders = orders
End Function

Public Function getCustomerOrders(initRow As Long, midRow As Long) As Collection
Dim orders As New Collection

For Each obj In SheetParser.longDict(initRow, midRow)
Dim name As String
Dim taxId As String
Dim email As String
Dim phone As String
Dim streetLine1 As String
Dim streetLine2 As String
Dim district As String
Dim city As String
Dim stateCode As String
Dim zipCode As String
Dim tags As String
Dim order As Dictionary

If obj("Nome") = "" Then
MsgBox "Por favor, não deixe linhas em branco entre os clientes para cadastro", vbExclamation, "Erro"
End
End If
name = obj("Nome")
taxId = obj("CPF/CNPJ")
email = obj("E-mail")
phone = obj("Telefone")
streetLine1 = obj("Logradouro")
streetLine2 = obj("Complemento")
district = obj("Bairro")
city = obj("Cidade")
stateCode = obj("Estado")
zipCode = obj("CEP")
tags = obj("Tags")

Set order = ChargeGateway.customerOrder(name, taxId, email, phone, streetLine1, streetLine2, district, city, stateCode, zipCode, tags)

orders.Add order

Next
Set getCustomerOrders = orders
End Function

Public Function order(amount As Long, customerId As String, dueDate As String, fine As Single, interest As Single, overdueLimit As Long, description1 As Dictionary, description2 As Dictionary, description3 As Dictionary) As Dictionary
Dim dict As New Dictionary
Dim descriptions As New Collection
Expand All @@ -238,6 +278,28 @@ Public Function order(amount As Long, customerId As String, dueDate As String, f

End Function

Public Function customerOrder(name As String, taxId As String, email As String, phone As String, streetLine1 As String, streetLine2 As String, district As String, city As String, stateCode As String, zipCode As String, tags As String) As Dictionary
Dim dict As New Dictionary
Dim address As New Dictionary

address.Add "streetLine1", streetLine1
address.Add "streetLine2", streetLine2
address.Add "district", district
address.Add "city", city
address.Add "stateCode", stateCode
address.Add "zipCode", zipCode

dict.Add "name", name
dict.Add "taxId", taxId
dict.Add "email", email
dict.Add "phone", phone
dict.Add "address", address
dict.Add "tags", Split(tags, ",")

Set customerOrder = dict

End Function

Public Function createCharges(charges As Collection)
Dim resp As response
Dim payload As String
Expand All @@ -247,31 +309,20 @@ Public Function createCharges(charges As Collection)

payload = JsonConverter.ConvertToJson(dict)

Set resp = StarkBankApi.postRequest("/v1/charge", payload, New Dictionary)
Set createCharges = StarkBankApi.postRequest("/v1/charge", payload, New Dictionary)

If resp.Status = 200 Then
createCharges = resp.json()("message")
MsgBox resp.json()("message"), , "Sucesso"

ElseIf resp.error().Exists("errors") Then
Dim errors As Collection: Set errors = resp.error()("errors")
Dim error As Dictionary
Dim errorList As String
Dim errorDescription As String

For Each error In errors
errorDescription = Utils.correctErrorLine(error("message"), TableFormat.HeaderRow())
errorList = errorList & errorDescription & Chr(10)
Next

Dim messageBox As String
messageBox = resp.error()("message") & Chr(10) & Chr(10) & errorList
MsgBox messageBox, , "Erro"

Else
MsgBox resp.error()("message"), , "Erro"

End If
End Function

Public Function createCustomers(customers As Collection)
Dim resp As response
Dim payload As String
Dim dict As New Dictionary

dict.Add "customers", customers

payload = JsonConverter.ConvertToJson(dict)

Set createCustomers = StarkBankApi.postRequest("/v1/charge/customer", payload, New Dictionary)

End Function

Expand Down
5 changes: 5 additions & 0 deletions src.vba/Cover.bas
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ Public Sub BotaoConsultaClientes()
ThisWorkbook.Sheets(name).Activate
End Sub

Public Sub BotaoCadastroClientes()
name = "Cadastro de Clientes"
ThisWorkbook.Sheets(name).Activate
End Sub

Public Sub BotaoConsultaEmitidos()
name = "Consulta de Boletos Emitidos"
ThisWorkbook.Sheets(name).Activate
Expand Down
111 changes: 106 additions & 5 deletions src.vba/Main.bas
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,11 @@ Public Sub searchCustomers()
End Sub

Public Sub createCharges()
On Error Resume Next

Dim charges As Collection
Dim resp As response
Dim initRow As Long
Dim midRow As Long
Dim lastRow As Long
Dim respMessage As String

Call Utils.applyStandardLayout("L")
Expand All @@ -194,13 +196,112 @@ Public Sub createCharges()
.SplitRow = 9
End With
ActiveWindow.FreezePanes = True

Set charges = ChargeGateway.getOrders()

respMessage = ChargeGateway.createCharges(charges)
initRow = 10
lastRow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).row
midRow = initRow - 1
Do
midRow = IIf(midRow + 100 >= lastRow, lastRow, midRow + 100)
Set charges = ChargeGateway.getOrders(initRow, midRow)
Set resp = ChargeGateway.createCharges(charges)

If resp.Status = 200 Then
respMessage = resp.json()("message")
MsgBox "Linhas " + CStr(initRow) + " a " + CStr(midRow) + ": " + resp.json()("message"), , "Sucesso"

ElseIf resp.error().Exists("errors") Then
Dim errors As Collection: Set errors = resp.error()("errors")
Dim error As Dictionary
Dim errorList As String
Dim errorDescription As String

For Each error In errors
errorDescription = Utils.correctErrorLine(error("message"), initRow - 1)
errorList = errorList & errorDescription & Chr(10)
Next

Dim messageBox As String
messageBox = resp.error()("message") & Chr(10) & Chr(10) & errorList
MsgBox messageBox, , "Erro"
End
Else
MsgBox resp.error()("message"), , "Erro"

End If

initRow = initRow + 100
Loop Until (midRow >= lastRow)

End Sub

Public Sub sendCustomers()
Dim customers As Collection
Dim resp As response
Dim initRow As Long
Dim midRow As Long
Dim lastRow As Long
Dim respMessage As String
Dim errorDescription As String

Call Utils.applyStandardLayout("K")

'Headers definition
ActiveSheet.Cells(TableFormat.HeaderRow(), 1).Value = "Nome"
ActiveSheet.Cells(TableFormat.HeaderRow(), 2).Value = "CPF/CNPJ"
ActiveSheet.Cells(TableFormat.HeaderRow(), 3).Value = "E-mail"
ActiveSheet.Cells(TableFormat.HeaderRow(), 4).Value = "Telefone"
ActiveSheet.Cells(TableFormat.HeaderRow(), 5).Value = "Logradouro"
ActiveSheet.Cells(TableFormat.HeaderRow(), 6).Value = "Complemento"
ActiveSheet.Cells(TableFormat.HeaderRow(), 7).Value = "Bairro"
ActiveSheet.Cells(TableFormat.HeaderRow(), 8).Value = "Cidade"
ActiveSheet.Cells(TableFormat.HeaderRow(), 9).Value = "Estado"
ActiveSheet.Cells(TableFormat.HeaderRow(), 10).Value = "CEP"
ActiveSheet.Cells(TableFormat.HeaderRow(), 11).Value = "Tags"

With ActiveWindow
.SplitColumn = 11
.SplitRow = 9
End With
ActiveWindow.FreezePanes = True

initRow = 10
lastRow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).row
midRow = initRow - 1
Do
midRow = IIf(midRow + 100 >= lastRow, lastRow, midRow + 100)
Set customers = ChargeGateway.getCustomerOrders(initRow, midRow)
Set resp = ChargeGateway.createCustomers(customers)

If resp.Status = 200 Then
respMessage = resp.json()("message")
MsgBox "Linhas " + CStr(initRow) + " a " + CStr(midRow) + ": " + resp.json()("message"), , "Sucesso"

ElseIf resp.error().Exists("errors") Then
Dim errors As Collection: Set errors = resp.error()("errors")
Dim error As Dictionary
Dim errorList As String

For Each error In errors
errorDescription = Utils.correctErrorLine(error("message"), initRow - 1)
errorList = errorList & errorDescription & Chr(10)
Next

Dim messageBox As String
messageBox = resp.error()("message") & Chr(10) & Chr(10) & errorList
MsgBox messageBox, , "Erro"
End
Else
Dim errorMessage As String: errorMessage = resp.error()("message")
errorDescription = Utils.correctErrorLine(errorMessage, initRow - 1)
MsgBox errorDescription, , "Erro"

End If

initRow = initRow + 100
Loop Until (midRow >= lastRow)

End Sub

Public Sub executeTransfers()
SendTransferForm.Show
End Sub
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 2 additions & 1 deletion src.vba/Request.bas
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ Public Function fetch(url As String, method As String, headers As Dictionary, pa
Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")
objHTTP.Open method, url, False

Debug.Print url
' Debug.Print url
' Debug.Print payload
For Each key In headers.keys()
objHTTP.setRequestHeader key, headers(key)
Next
Expand Down
18 changes: 18 additions & 0 deletions src.vba/SheetParser.bas
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,22 @@ Public Function dict()
Next

Set dict = result
End Function

Public Function longDict(initRow, lastRow)
Dim result As New Collection
Dim keys As Collection

Set keys = headers()

For row = initRow To lastRow
Dim obj As Object
Set obj = JsonConverter.ParseJson("{}")
For Each elem In ActiveSheet.UsedRange.columns
obj(keys(elem.column)) = ActiveSheet.Cells(row, elem.column).text
Next
result.Add obj
Next

Set longDict = result
End Function
2 changes: 1 addition & 1 deletion src.vba/ViewChargeEventsForm.frm
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ Private Sub SearchButton_Click()
ActiveSheet.Cells(row, 21).Value = charge("stateCode")
ActiveSheet.Cells(row, 22).Value = charge("zipCode")

If chargeStatus = "paid" Then
If chargeStatus = "paid" And Not logsPaidByCharge.Exists(id) Then
logsPaidByCharge.Add id, chargeLog
logsRegisteredByCharge.Add id, New Dictionary
End If
Expand Down
Binary file modified starkbank-sdk.xlsm
Binary file not shown.

0 comments on commit 49b54cb

Please sign in to comment.