' change log:
' 2023-09-24, update error handling
' 2023-09-23, update for "################" when zoom out

Option Explicit

Dim first_row As Integer
Dim current_row As Integer
Dim last_check_row As Integer

' Dim amount_in_func As String
Dim starting_cell As String

Dim grand_total_curr As Currency
Dim grand_total_str As String

Dim amount_in_func_curr As Currency
Dim amount_in_func_str As String

Dim pcco_str As String

Dim ISIN_CODE_ESTD_COL As String
Dim BOOK_COL As String
Dim BUY_COL As String
Dim SELL_COL As String
Dim GRANDTOTAL_COL As String
Dim PCCO_COL As String
Dim AMOUNT_IN_FUNC_COL As String
Dim BMF_ID_COL As String

Dim ACCOUNT_COL As String
Dim DEBIT_COL As String
Dim CREDIT_COL As String

Dim REMARKS_COL As String

Dim ADJUSTMENT_WORKSHEET As String
Dim ADJUSTMENT_TEST_WORKSHEET As String

Dim AC_1020 As String
Dim AC_8600 As String
Dim AC_8601 As String
Dim AC_8700 As String
Dim AC_8701 As String
Dim AC_1311 As String
Dim AC_1375 As String
Dim AC_2020 As String

Dim current_row_value_valid As Boolean
Dim check_next_row_ready As Boolean

Sub init()
  On Error GoTo eh

  first_row = 7
  current_row = first_row
  last_check_row = getLastCheckRow(current_row)

  ' amount_in_func = "J"
  ISIN_CODE_ESTD_COL = "C"
  BOOK_COL = "D"
  BUY_COL = "E"
  SELL_COL = "F"
  GRANDTOTAL_COL = "G"
  PCCO_COL = "I"
  AMOUNT_IN_FUNC_COL = "J"
  BMF_ID_COL = "K"

  ACCOUNT_COL = "M"
  DEBIT_COL = "N"
  CREDIT_COL = "O"

  REMARKS_COL = "Q"

  starting_cell = GRANDTOTAL_COL & first_row

  AC_1020 = "AC_1020"
  AC_8600 = "AC_8600"
  AC_8601 = "AC_8601"
  AC_8700 = "AC_8700"
  AC_8701 = "AC_8701"
  AC_1311 = "AC_1311"
  AC_1375 = "AC_1375"
  AC_2020 = "AC_2020"

  'ADJUSTMENT_WORKSHEET = "Adjustment_TEST"
  ADJUSTMENT_WORKSHEET = "Adjustment"

Done:
  Exit Sub

eh:
  MsgBox "init:Error: " & Err.Description
  Stop

End Sub

Function writeRemarks(current_row As Integer, remarks As String)
  On Error GoTo eh

  Worksheets(ADJUSTMENT_WORKSHEET).Range(REMARKS_COL & CStr(current_row)).Value = remarks

Done:
  Exit Function

eh:
  MsgBox "writeRemarks:Error: " & Err.Description
  Stop

End Function

Function writeCredit(current_row As Integer, credit As Variant)
  On Error GoTo eh

  Worksheets(ADJUSTMENT_WORKSHEET).Range(CREDIT_COL & CStr(current_row)).Value = Abs(credit)
  Worksheets(ADJUSTMENT_WORKSHEET).Range(CREDIT_COL & CStr(current_row)).NumberFormat = "#,##0.00"

Done:
  Exit Function

eh:
  MsgBox "writeCredit:Error: " & Err.Description
  Stop

End Function

Function writeDebit(current_row As Integer, debit As Variant)
  On Error GoTo eh

  Worksheets(ADJUSTMENT_WORKSHEET).Range(DEBIT_COL & CStr(current_row)).Value = Abs(debit)
  Worksheets(ADJUSTMENT_WORKSHEET).Range(DEBIT_COL & CStr(current_row)).NumberFormat = "#,##0.00"

Done:
  Exit Function

eh:
  MsgBox "writeDebit:Error: " & Err.Description
  Stop

End Function

Function writeAccount(current_row As Integer, account As String)
  On Error GoTo eh

  Worksheets(ADJUSTMENT_WORKSHEET).Range(ACCOUNT_COL & CStr(current_row)).Value = account

Done:
  Exit Function

eh:
  MsgBox "writeAccount:Error: " & Err.Description
  Stop

End Function

Function getPCCO(current_row As Integer)
  On Error GoTo eh

  Dim output As String
  output = Worksheets(ADJUSTMENT_WORKSHEET).Range(PCCO_COL & CStr(current_row)).Value

Done:
  getPCCO = output
  Exit Function

eh:
  MsgBox "getPCCO:Error: " & Err.Description
  Stop

End Function


Function getGrandTotal(current_row As Integer)
  On Error GoTo eh

  Dim output As String

  output = Worksheets(ADJUSTMENT_WORKSHEET).Range(GRANDTOTAL_COL & CStr(current_row)).Text
  If (InStr(output, "###") >= 0) Then
    output = Worksheets(ADJUSTMENT_WORKSHEET).Range(GRANDTOTAL_COL & CStr(current_row)).Value
  End If

Done:
  getGrandTotal = output
  Exit Function

eh:
  MsgBox "getGrandTotal:Error: " & Err.Description & ":" & CStr(current_row)
  Stop

End Function

Function getAmountInFunc(current_row As Integer)
  On Error GoTo eh

  Dim output As String

  output = Worksheets(ADJUSTMENT_WORKSHEET).Range(AMOUNT_IN_FUNC_COL & CStr(current_row)).Text
  If (InStr(output, "###") >= 0) Then
    output = Worksheets(ADJUSTMENT_WORKSHEET).Range(AMOUNT_IN_FUNC_COL & CStr(current_row)).Value
  End If

Done:
  getAmountInFunc = output
  Exit Function

eh:
  MsgBox "getAmountInFunc:Error: " & Err.Description & ":" & CStr(current_row)
  Stop

End Function

Function checkOutputCellReady(current_row As Integer)
  On Error GoTo eh

  Dim output As Boolean
  output = True

  Dim account_cell As Variant
  account_cell = Worksheets(ADJUSTMENT_WORKSHEET).Range(ACCOUNT_COL & CStr(current_row))

  If (IsEmpty(account_cell) = False) Then
    output = False
  End If

Done:
  checkOutputCellReady = output
  Exit Function

eh:
  MsgBox "checkOutputCellReady:Error: " & Err.Description
  Stop

End Function

Function checkCellFormat(test_cell As Variant)
  On Error GoTo eh

  Dim Msg As String

  Msg = "no match"
  Select Case IsEmpty(test_cell)
      Case True
          Msg = "General."
      Case Else
          Select Case IsNumeric(test_cell)
              Case True
                  Msg = "Number."
              Case Else
                  Select Case IsDate(test_cell)
                      Case True
                          Msg = "Date."
                      Case Else
                          Msg = "Text."
                  End Select
          End Select
  End Select
  ' debug.print "checkCellFormat:" & test_cell.Address & ":" & Msg

Done:
  checkCellFormat = Msg
  Exit Function

eh:
  MsgBox "checkCellFormat:Error: " & Err.Description
  Stop

End Function



Function checkCurrentRowFormatValid(current_row As Integer)
  On Error GoTo eh

  Dim test_cell As Variant
  Dim output As Boolean
  output = True

  test_cell = Worksheets(ADJUSTMENT_WORKSHEET).Range(ISIN_CODE_ESTD_COL & CStr(current_row))
  If "Text." = checkCellFormat(test_cell) Then
    Debug.Print "ISIN_CODE_ESTD_COL:check ok"
  Else
    Debug.Print "ISIN_CODE_ESTD_COL:check failed"
    Debug.Print checkCellFormat(test_cell)
    output = False
  End If

  test_cell = Worksheets(ADJUSTMENT_WORKSHEET).Range(BOOK_COL & CStr(current_row))
  If "Text." = checkCellFormat(test_cell) Then
    Debug.Print "BOOK_COL:check ok"
  Else
    Debug.Print "BOOK_COL:check failed"
    Debug.Print checkCellFormat(test_cell)
    output = False
  End If

  test_cell = Worksheets(ADJUSTMENT_WORKSHEET).Range(BUY_COL & CStr(current_row))

  If "Number." = checkCellFormat(test_cell) Then
    Debug.Print "BUY_COL:check ok"
  Else
    Debug.Print "BUY_COL:check failed"
    Debug.Print checkCellFormat(test_cell)
    output = False
  End If

  test_cell = Worksheets(ADJUSTMENT_WORKSHEET).Range(GRANDTOTAL_COL & CStr(current_row))
  If "Number." = checkCellFormat(test_cell) Then
    Debug.Print "GRANDTOTAL_COL:check ok"
  Else
    Debug.Print "GRANDTOTAL_COL:check failed"
    Debug.Print checkCellFormat(test_cell)
    output = False
  End If

  test_cell = Worksheets(ADJUSTMENT_WORKSHEET).Range(PCCO_COL & CStr(current_row))
  If "Text." = checkCellFormat(test_cell) Then
    Debug.Print "PCCO_COL:check ok"
  Else
    Debug.Print "PCCO_COL:check failed"
    Debug.Print checkCellFormat(test_cell)
    output = False
  End If

  test_cell = Worksheets(ADJUSTMENT_WORKSHEET).Range(AMOUNT_IN_FUNC_COL & CStr(current_row))
  If "Number." = checkCellFormat(test_cell) Then
    Debug.Print "AMOUNT_IN_FUNC_COL:check ok"
  Else
    Debug.Print "AMOUNT_IN_FUNC_COL:check failed"
    Debug.Print checkCellFormat(test_cell)
    output = False
  End If

  test_cell = Worksheets(ADJUSTMENT_WORKSHEET).Range(BMF_ID_COL & CStr(current_row))
  If "Number." = checkCellFormat(test_cell) Then
    Debug.Print "BMF_ID_COL:check ok"
  Else
    Debug.Print "BMF_ID_COL:check failed"
    Debug.Print checkCellFormat(test_cell)
    output = False
  End If


  test_cell = Worksheets(ADJUSTMENT_WORKSHEET).Range(ACCOUNT_COL & CStr(current_row))
  If "General." = checkCellFormat(test_cell) Then
    Debug.Print "ACCOUNT_COL:check ok"
  Else
    Debug.Print "ACCOUNT_COL:check failed"
    Debug.Print checkCellFormat(test_cell)
    output = False
  End If

  test_cell = Worksheets(ADJUSTMENT_WORKSHEET).Range(DEBIT_COL & CStr(current_row))
  If "General." = checkCellFormat(test_cell) Then
    Debug.Print "DEBIT_COL:check ok"
  Else
    Debug.Print "DEBIT_COL:check failed"
    Debug.Print checkCellFormat(test_cell)
    output = False
  End If

  test_cell = Worksheets(ADJUSTMENT_WORKSHEET).Range(CREDIT_COL & CStr(current_row))
  If "General." = checkCellFormat(test_cell) Then
    Debug.Print "CREDIT_COL:check ok"
  Else
    Debug.Print "CREDIT_COL:check failed"
    Debug.Print checkCellFormat(test_cell)
    output = False
  End If

  test_cell = Worksheets(ADJUSTMENT_WORKSHEET).Range(REMARKS_COL & CStr(current_row))
  If "General." = checkCellFormat(test_cell) Then
    Debug.Print "REMARKS_COL:check ok"
  Else
    Debug.Print "REMARKS_COL:check failed"
    Debug.Print checkCellFormat(test_cell)
    output = False
  End If

Done:
  checkCurrentRowFormatValid = output
  Exit Function

eh:
  MsgBox "checkCurrentRowFormatValid:Error: " & Err.Description
  Stop

End Function


Function checkCurrentRowValueValid(current_row As Integer)
  On Error GoTo eh

  Dim output As Boolean
  output = True

  If (checkIfGrandTotalRefNotFound(current_row) = True) Then
    Debug.Print "grand total ref not found"
    output = False
  End If


  If output Then

    Dim grand_total_str As String
    Dim amount_in_func_str As String
    Dim pcco_str As String

    grand_total_str = getGrandTotal(current_row)
    amount_in_func_str = getAmountInFunc(current_row)
    pcco_str = getPCCO(current_row)

    ' Debug.print InStr(pcco_str, "Error") >= 0
    ' Debug.print InStr(amount_in_func_str, "N/A") >= 0
    ' Debug.print InStr(grand_total_str, "N/A") >= 0

    If (Trim(pcco_str) = "" Or InStr(pcco_str, "N/A") > 0 Or InStr(pcco_str, "Error") > 0) Then
      output = False
    End If

    If (Trim(amount_in_func_str) = "" Or InStr(amount_in_func_str, "N/A") > 0 Or InStr(amount_in_func_str, "Error") > 0) Then
      output = False
    End If

    If (Trim(grand_total_str) = "" Or InStr(grand_total_str, "N/A") > 0 Or InStr(grand_total_str, "Error") > 0) Then
      output = False
    End If

  Else
    Debug.Print "skipping as grand total ref not found"
  End If

Done:
  checkCurrentRowValueValid = output
  Exit Function

eh:
  MsgBox "checkCurrentRowValueValid:Error: " & Err.Description
  Stop

End Function

Function checkFullFillCasePCCOCode_P1375000Situation_1(grand_total As Currency, amount_in_func As Currency)
  On Error GoTo eh

  ' ### Special case (if PCCO Code = P1375000):
  ' ### Situation 1:
  ' if Grand total + amount in func > 0 (missing example)
  Dim output As Boolean
  output = (grand_total + amount_in_func > 0)

Done:
  checkFullFillCasePCCOCode_P1375000Situation_1 = output
  Exit Function

eh:
  MsgBox "checkFullFillCasePCCOCode_P1375000Situation_1:Error: " & Err.Description
  Stop

End Function

Function checkFullFillCasePCCOCode_P1375000Situation_2(grand_total As Currency, amount_in_func As Currency)
  On Error GoTo eh

  ' ### Special case (if PCCO Code = P1375000):
  ' ### Situation 2:
  ' if Grand total + amount in func < 0 (row 21)
  Dim output As Boolean
  output = (grand_total + amount_in_func < 0)

Done:
  checkFullFillCasePCCOCode_P1375000Situation_2 = output
  Exit Function

eh:
  MsgBox "checkFullFillCasePCCOCode_P1375000Situation_2:Error: " & Err.Description
  Stop

End Function

Function checkFullFillCasePCCOCode_P1375000Situation_3(grand_total As Currency, amount_in_func As Currency)
  On Error GoTo eh

  ' ### Special case (if PCCO Code = P1375000):
  ' ### Situation 3:
  ' if Grand total + amount in func = 0
  '   - do nothing
  Dim output As Boolean
  output = (grand_total + amount_in_func = 0)

Done:
  checkFullFillCasePCCOCode_P1375000Situation_3 = output
  Exit Function

eh:
  MsgBox "checkFullFillCasePCCOCode_P1375000Situation_3:Error: " & Err.Description
  Stop

End Function

Function checkFullFillCaseCommonSituation_1(grand_total As Currency, amount_in_func As Currency)
  On Error GoTo eh

  ' ### Situation 1:
  ' if Grand total + amount in func > 0 (row 8 ?)
  Dim output As Boolean
  output = (grand_total + amount_in_func > 0)

Done:
  checkFullFillCaseCommonSituation_1 = output
  Exit Function

eh:
  MsgBox "checkFullFillCaseCommonSituation_1:Error: " & Err.Description
  Stop

End Function

Function checkFullFillCaseCommonSituation_2(grand_total As Currency, amount_in_func As Currency)
  On Error GoTo eh

  ' ### Situation 2:
  ' if Grand total + amount in func < 0 (row 8 ?)'
  Dim output As Boolean
  output = (grand_total + amount_in_func < 0)

Done:
  checkFullFillCaseCommonSituation_2 = output
  Exit Function

eh:
  MsgBox "checkFullFillCaseCommonSituation_2:Error: " & Err.Description
  Stop

End Function

Function checkFullFillCaseCommonSituation_3(grand_total As Currency, amount_in_func As Currency)
  On Error GoTo eh

  ' ### Situation 3:
  ' if Grand total + amount in func = 0
  Dim output As Boolean
  output = (grand_total + amount_in_func = 0)

Done:
  checkFullFillCaseCommonSituation_3 = output
  Exit Function

eh:
  MsgBox "checkFullFillCaseCommonSituation_3:Error: " & Err.Description
  Stop
End Function

Function checkIfReferenceNotFound(cell As Variant)
  On Error GoTo eh

  Dim test_text As Variant
  test_text = cell.Text
  Dim output As Boolean

  output = (InStr(test_text, "#REF!") > 0)

Done:
  checkIfReferenceNotFound = output
  Exit Function

eh:
  MsgBox "checkIfReferenceNotFound:Error: " & Err.Description
  Stop

End Function

Function checkIfGrandTotalRefNotFound(current_row As Integer)
  On Error GoTo eh

  Dim test_cell As Variant
  test_cell = Worksheets(ADJUSTMENT_WORKSHEET).Range(GRANDTOTAL_COL & CStr(current_row))

Done:
  checkIfGrandTotalRefNotFound = checkIfReferenceNotFound(Worksheets(ADJUSTMENT_WORKSHEET).Range(GRANDTOTAL_COL & CStr(current_row)))
  Exit Function

eh:
  MsgBox "checkIfGrandTotalRefNotFound:Error: " & Err.Description & ":" & GRANDTOTAL_COL & CStr(current_row)
  Stop

End Function

Function applyCommonSituation_1(current_row As Integer)
  On Error GoTo eh

  Dim net_var As Variant
  Dim grand_total_var As Variant
  Dim amount_in_func_var As Variant

  grand_total_var = CDec(getGrandTotal(current_row))
  amount_in_func_var = CDec(getAmountInFunc(current_row))
  net_var = grand_total_var + amount_in_func_var

' current row
' - Account column: A1311, Debit column: Grand Total+amount in func
  writeAccount current_row, AC_1311
  writeDebit current_row, net_var

' - add row:
'     - account:1020,
'     - debit column: Grand Total+amount in func
  insertNewRowBelow current_row
  current_row = current_row + 1
  writeAccount current_row, AC_1020
  writeDebit current_row, net_var

' - add row:
'     - account:NA8600,
'     - debit: grand total + amount in func
  insertNewRowBelow current_row
  current_row = current_row + 1
  writeAccount current_row, AC_8600
  writeDebit current_row, net_var

' - add row:
'     - account NA8601,
'     - credit: grand total + amount in func
  insertNewRowBelow current_row
  current_row = current_row + 1
  writeAccount current_row, AC_8601
  writeCredit current_row, net_var

Done:
  applyCommonSituation_1 = True
  Exit Function

eh:
  MsgBox "applyCommonSituation_1:Error: " & Err.Description
  Stop

End Function

Function applyCommonSituation_2(current_row As Integer)
  On Error GoTo eh

  Dim net_var As Variant
  Dim grand_total_var As Variant
  Dim amount_in_func_var As Variant

  grand_total_var = CDec(getGrandTotal(current_row))
  amount_in_func_var = CDec(getAmountInFunc(current_row))
  net_var = grand_total_var + amount_in_func_var

' if Grand total + amount in func < 0 (row 8 ?)
'   - Account column: A1311,
'   - Credit column: Grand Total+amount in func
  writeAccount current_row, AC_1311
  writeCredit current_row, net_var

'   - add row:
'     - account:1020,
'     - credit column: Grand Total+amount in func
  insertNewRowBelow current_row
  current_row = current_row + 1
  writeAccount current_row, AC_1020
  writeCredit current_row, net_var

'   - add row:
'     - account:NA8700,
'     - credit: grand total + amount in func
  insertNewRowBelow current_row
  current_row = current_row + 1
  writeAccount current_row, AC_8700
  writeCredit current_row, net_var

'   - add row:
'     - account NA8701,
'     - debit: grand total + amount in func
  insertNewRowBelow current_row
  current_row = current_row + 1
  writeAccount current_row, AC_8701
  writeDebit current_row, net_var

Done:
  applyCommonSituation_2 = True
  Exit Function

eh:
  MsgBox "applyCommonSituation_2:Error: " & Err.Description
  Stop

End Function

Function applyCommonSituation_3(current_row As Integer)
  On Error GoTo eh

' if Grand total + amount in func = 0
' - do nothing
  applyCommonSituation_3 = True

Done:
  applyCommonSituation_3 = True
  Exit Function

eh:
  MsgBox "applyCommonSituation_3:Error: " & Err.Description
  Stop

End Function

Function applyPcco1375Situation_1(current_row As Integer)
  On Error GoTo eh

  Dim net_var As Variant
  Dim grand_total_var As Variant
  Dim amount_in_func_var As Variant

  grand_total_var = CDec(getGrandTotal(current_row))
  amount_in_func_var = CDec(getAmountInFunc(current_row))
  net_var = grand_total_var + amount_in_func_var

  '   - Account
  '     - column: P1375,
  '     - Debit column: Grand Total+amount in func
  writeAccount current_row, AC_1375
  writeDebit current_row, net_var

  '   - add row:
  '     - account:2020,
  '     - debit column: Grand Total+amount in func
  insertNewRowBelow current_row
  current_row = current_row + 1
  writeAccount current_row, AC_2020
  writeDebit current_row, net_var

  '   - add row:
  '     - account:NA8600,
  '     - debit: grand total + amount in func
  insertNewRowBelow current_row
  current_row = current_row + 1
  writeAccount current_row, AC_8600
  writeDebit current_row, net_var

  '   - add row:
  '     - account NA8601,
  '     - credit: grand total + amount in func
  insertNewRowBelow current_row
  current_row = current_row + 1
  writeAccount current_row, AC_8601
  writeCredit current_row, net_var

  applyPcco1375Situation_1 = True

Done:
  applyPcco1375Situation_1 = True
  Exit Function

eh:
  MsgBox "applyPcco1375Situation_1:Error: " & Err.Description
  Stop

End Function

Function applyPcco1375Situation_2(current_row As Integer)
  On Error GoTo eh

  Dim net_var As Variant
  Dim grand_total_var As Variant
  Dim amount_in_func_var As Variant

  grand_total_var = CDec(getGrandTotal(current_row))
  amount_in_func_var = CDec(getAmountInFunc(current_row))
  net_var = grand_total_var + amount_in_func_var

'   - Account
'     - column: P1375,
'     - Credit column: Grand Total+amount in func
  writeAccount current_row, AC_1375
  writeCredit current_row, net_var

'   - add row:
'     - account:2020,
'     - credit column: Grand Total+amount in func
  insertNewRowBelow current_row
  current_row = current_row + 1
  writeAccount current_row, AC_2020
  writeCredit current_row, net_var

'   - add row:
'     - account:8700,
'     - credit: grand total + amount in func
  insertNewRowBelow current_row
  current_row = current_row + 1
  writeAccount current_row, AC_8700
  writeCredit current_row, net_var

'   - add row:
'     - account 8701,
'     - debit: grand total + amount in func
  insertNewRowBelow current_row
  current_row = current_row + 1
  writeAccount current_row, AC_8701
  writeDebit current_row, net_var

  applyPcco1375Situation_2 = True

Done:
  applyPcco1375Situation_2 = True
  Exit Function

eh:
  MsgBox "applyPcco1375Situation_2:Error: " & Err.Description
  Stop

End Function

Function applyPcco1375Situation_3(current_row As Integer)
  On Error GoTo eh

' - do nothing
  applyPcco1375Situation_3 = True

Done:
  applyPcco1375Situation_3 = True
  Exit Function

eh:
  MsgBox "applyPcco1375Situation_3:Error: " & Err.Description
  Stop

End Function


Sub test()
  ' Debug.Assert True = isCurrentRowNeedToProcess("7")
  ' Debug.Assert False = isCurrentRowNeedToProcess("180")
  ' debug.assert False = isAllDone ("7")
  ' debug.assert True = isAllDone ("180")

  ' debug.assert False = checkFullFillCaseCommonSituation_1(-10,1)
  ' debug.assert False = checkFullFillCaseCommonSituation_1(1,-10)
  ' debug.assert False = checkFullFillCaseCommonSituation_1(-10,-10)
  ' debug.assert False = checkFullFillCaseCommonSituation_1(0,0)
  ' debug.assert True = checkFullFillCaseCommonSituation_1(0,1)
  ' debug.assert True = checkFullFillCaseCommonSituation_1(1,0)
  ' debug.assert True = checkFullFillCaseCommonSituation_1(1,1)

  ' debug.assert True = checkFullFillCaseCommonSituation_2(-10,1)
  ' debug.assert True = checkFullFillCaseCommonSituation_2(1,-10)
  ' debug.assert True = checkFullFillCaseCommonSituation_2(-10,-10)
  ' debug.assert False = checkFullFillCaseCommonSituation_2(0,0)
  ' debug.assert False = checkFullFillCaseCommonSituation_2(0,1)
  ' debug.assert False = checkFullFillCaseCommonSituation_2(1,0)
  ' debug.assert False = checkFullFillCaseCommonSituation_2(1,1)

  ' debug.assert True =  checkFullFillCaseCommonSituation_3(0,0)
  ' debug.assert False =  checkFullFillCaseCommonSituation_3(1,1)
  ' debug.assert False =  checkFullFillCaseCommonSituation_3(-10,-10)
  ' debug.assert False =  checkFullFillCaseCommonSituation_3(1,0)
  ' debug.assert False =  checkFullFillCaseCommonSituation_3(0,1)

  ' debug.print Worksheets("Adjustment_TEST").Range("G86")

  init

  current_row = 29
  ' amount_in_func_str = getAmountInFunc(current_row)
  ' pcco_str = getPCCO(current_row)

  ' debug.print current_row
  ' debug.print grand_total_str
  ' debug.print checkIfGrandTotalRefNotFound(current_row)
  Debug.Print checkCurrentRowValueValid(current_row)

  ' current_row_value_valid = False
  ' current_row_value_valid = checkCurrentRowValueValid(pcco_str, amount_in_func_str, grand_total_str)
  ' check_next_row_ready = False
  ' check_next_row_ready = checkOutputCellReady(current_row)

End Sub

Function getLastCheckRow(first_row As Integer)
  getLastCheckRow = first_row + 10
End Function

Function insertNewRowBelow(current_row As Integer)
  Range("A" & current_row + 1).EntireRow.Insert
End Function

Function isAllDone(input_row As Integer)
  On Error GoTo eh

  Dim cell_value As Currency
  Dim output As Boolean
  Dim i As Integer
  output = True

  last_check_row = getLastCheckRow(input_row)
  For i = input_row To last_check_row
    If (isCurrentRowNeedToProcess(i) = True) Then
      output = False
      Exit For
    End If
  Next i

  isAllDone = output

Done:
  isAllDone = output
  Exit Function

eh:
  MsgBox "isAllDone:Error: " & Err.Description
  Stop

End Function

Function isCurrentRowNeedToProcess(input_row As Integer)
  On Error GoTo eh

  Dim output As Boolean
  Dim cell_value As Currency
  Dim input_cell As String
  input_cell = GRANDTOTAL_COL & input_row

Done:
  isCurrentRowNeedToProcess = Not (IsEmpty(Worksheets(ADJUSTMENT_WORKSHEET).Range(input_cell).Value))
  Exit Function

eh:
  MsgBox "isCurrentRowNeedToProcess:Error: " & Err.Description
  Stop

End Function

Sub Main()
  On Error GoTo eh

  Debug.Print "start a new run"

  init

  While isAllDone(current_row) = False
  ' While current_row < 9
    Debug.Print "processing row: " & current_row

    Debug.Print checkCurrentRowFormatValid(current_row)

    current_row_value_valid = False
    current_row_value_valid = checkCurrentRowValueValid(current_row)
    check_next_row_ready = False
    check_next_row_ready = checkOutputCellReady(current_row)

    If (current_row_value_valid And check_next_row_ready) Then
      ' input considered valid after this line

      Dim grand_total_str As String
      Dim amount_in_func_str As String
      Dim pcco_str As String

      grand_total_str = getGrandTotal(current_row)
      amount_in_func_str = getAmountInFunc(current_row)
      pcco_str = getPCCO(current_row)

      Debug.Print "before"
      Debug.Print grand_total_str
      Debug.Print "pass"
      grand_total_curr = CCur(grand_total_str)

      amount_in_func_curr = CCur(amount_in_func_str)

      If (pcco_str = "P1375000") Then
        If (checkFullFillCasePCCOCode_P1375000Situation_1(grand_total_curr, amount_in_func_curr) = True) Then
          writeRemarks current_row, "P1375000 common sit 1"
          applyPcco1375Situation_1 (current_row)
        ElseIf (checkFullFillCasePCCOCode_P1375000Situation_2(grand_total_curr, amount_in_func_curr) = True) Then
          writeRemarks current_row, "P1375000 common sit 2"
          applyPcco1375Situation_2 (current_row)
        ElseIf (checkFullFillCasePCCOCode_P1375000Situation_3(grand_total_curr, amount_in_func_curr) = True) Then
          writeRemarks current_row, "P1375000 common sit 3"
          applyPcco1375Situation_3 (current_row)
        Else
          MsgBox "missing pcco 1375 situation"
        End If

      ElseIf (pcco_str = "N/A") Then
        MsgBox "pcco value is N/A"

      Else
        If (checkFullFillCaseCommonSituation_1(grand_total_curr, amount_in_func_curr) = True) Then
          writeRemarks current_row, "common sit 1"
          applyCommonSituation_1 (current_row)
        ElseIf (checkFullFillCaseCommonSituation_2(grand_total_curr, amount_in_func_curr) = True) Then
          writeRemarks current_row, "common sit 2"
          applyCommonSituation_2 (current_row)
        ElseIf (checkFullFillCaseCommonSituation_3(grand_total_curr, amount_in_func_curr) = True) Then
          writeRemarks current_row, "common sit 3"
          applyCommonSituation_3 (current_row)
        Else
          MsgBox "missing situation"
        End If
      End If

    Else
      Debug.Print "input is not valid, skipping row"

      If (checkOutputCellReady(current_row)) Then
        If (checkCurrentRowValueValid(current_row)) Then
        Else
          writeRemarks current_row, "input is not valid"
        End If
      End If
    End If

    current_row = current_row + 1
  Wend


Done:
  Debug.Print "done"
  Exit Sub

eh:
  MsgBox "Main:Error: " & Err.Description
  Stop

End Sub

Sub helloworld()
  ' debug.print isCurrentRowNeedToProcess ("7")
  ' debug.print isCurrentRowNeedToProcess ("180")

  ' debug.print isAllDone ("7")
  ' debug.print isAllDone ("180")

End Sub
