' **************************************
 '       Program Name: Amort.bas
 '      By Kevin Diggins(MrBCX)
 ' **************************************
 '  AMORT is a small loan amortization
 '  program coded entirely in BCX 2.82
 ' **************************************
 
 GUI "Amort"
 
 SUB FORMLOAD()
 
   GLOBAL Form1   AS CONTROL
   GLOBAL Input1  AS CONTROL
   GLOBAL Input2  AS CONTROL
   GLOBAL Input3  AS CONTROL
   GLOBAL Label1  AS CONTROL
   GLOBAL Label2  AS CONTROL
   GLOBAL Label3  AS CONTROL
   GLOBAL Label4  AS CONTROL
   GLOBAL Button1 AS CONTROL
   GLOBAL LView1  AS CONTROL
 
   ' **************************************************************************
   Form1 = BCX_FORM("Amortization Schedule by Kevin Diggins", 0, 0, 200, 220)
   Input1 = BCX_INPUT("8000", Form1, 0, 137, 3, 46, 14)
   Input2 = BCX_INPUT("9.25", Form1, 0, 137, 18, 46, 14)
   Input3 = BCX_INPUT("3", Form1, 0, 137, 33, 46, 14)
   Label1 = BCX_LABEL("Principal", Form1, 0, 5, 52, 40, 14)
   Label2 = BCX_LABEL("Interest", Form1, 0, 5, 65, 40, 14)
   Label3 = BCX_LABEL("", Form1, 0, 50, 52, 40, 14)
   Label4 = BCX_LABEL("", Form1, 0, 50, 65, 40, 14)
   Button1 = BCX_BUTTON("Calc", Form1, 101, 140, 52, 40, 14)
   LView1 = BCX_LISTVIEW("", Form1, 0, 5, 80, 150, 123)
   BCX_LABEL("Loan Amount", Form1, 0, 4, 7, 63, 11)
   BCX_LABEL("Interest Rate", Form1, 0, 5, 22, 63, 11)
   BCX_LABEL("No. of Years", Form1, 0, 5, 37, 96, 11)
   ' **************************************************************************
 
   Set_ColumnText(LView1, 0, "Payment")
   Set_ColumnText(LView1, 1, "Interest")
   Set_ColumnText(LView1, 2, "Principal")
   Set_ColumnText(LView1, 3, "Balance")
 
   SetFocus(Input1)
   CENTER(Form1)
   SHOW(Form1)
 END SUB
 
 BEGIN EVENTS
   IF CBCTL = 101 THEN    ' CALC Button Clicked ?
     FillListView()
   END IF
 END EVENTS
 
 SUB FillListView()
 
   '==========================
   DIM Amount AS DOUBLE
   DIM Interest AS DOUBLE
   DIM Years AS DOUBLE
   DIM Pmt AS DOUBLE
   DIM Int_Payment AS DOUBLE
   DIM Prin_Payment AS DOUBLE
   DIM lvItem AS LV_ITEM
   DIM SumPrinc AS DOUBLE
   DIM SumInt AS DOUBLE
   DIM z AS Long
   '==========================
 
   Amount = VAL(BCX_GET_TEXT$(Input1))
   Interest = VAL(BCX_GET_TEXT$(Input2))
   Years = VAL(BCX_GET_TEXT$(Input3))
   Pmt = PMT(Interest, Years * 12, Amount, 0)
 
   BCX_LV_Reset(LView1, 4, Years * 12)
 
   Set_ColumnText(LView1, 0, "Payment")
   Set_ColumnText(LView1, 1, "Interest")
   Set_ColumnText(LView1, 2, "Principal")
   Set_ColumnText(LView1, 3, "Balance")
 
   FOR z = 1 TO Years * 12
     Int_Payment = INTEREST_PAYMENT(Interest, Amount)
     Prin_Payment = Pmt - INTEREST_PAYMENT(Interest, Amount)
     SumPrinc = SumPrinc + Prin_Payment
     SumInt = SumInt + Int_Payment
     ListView_SetItemText(LView1, z - 1, 0, STR$(z))
     ListView_SetItemText(LView1, z - 1, 1, USING$("###.##", Int_Payment))
     ListView_SetItemText(LView1, z - 1, 2, USING$("###.##", Prin_Payment))
     Amount = Amount - Prin_Payment
     ListView_SetItemText(LView1, z - 1, 3, USING$("###.##", Amount))
   NEXT
 
   ListView_SetItemText(LView1, z, 3, " ")  ' add a line at the bottom
 
   FOR z = 0 TO 3
     SendMessage(LView1, LVM_SETCOLUMNWIDTH, z, LVSCW_AUTOSIZE_USEHEADER)
   NEXT
 
   BCX_LV_Justify(LView1, 0, HDF_CENTER)
   BCX_LV_Justify(LView1, 1, HDF_RIGHT)
   BCX_LV_Justify(LView1, 2, HDF_RIGHT)
   BCX_LV_Justify(LView1, 3, HDF_RIGHT)
 
   BCX_SET_TEXT(Label3, USING$("###.##", SumPrinc))
   BCX_SET_TEXT(Label4, USING$("###.##", SumInt))
 END SUB
  
 FUNCTION INTEREST_PAYMENT(ByVal i AS DOUBLE, ByVal Balance AS DOUBLE) AS DOUBLE
   FUNCTION =(i/12/100) * Balance
 END FUNCTION
  
 FUNCTION PMT(ByVal i AS DOUBLE, ByVal np AS DOUBLE, ByVal pv AS DOUBLE, ByVal fv AS DOUBLE) AS DOUBLE
   DIM RAW q1 AS DOUBLE
   DIM RAW ir AS DOUBLE
   ir = i / 12 / 100
   q1 = POW(1 + ir, np)
   FUNCTION =((ir *(fv + q1 * pv))/(-1 + q1))
 END FUNCTION
  
 SUB Set_ColumnText(ByVal hWnd AS HWND, ByVal Column, ByVal Text$)
   LOCAL lvc AS LV_COLUMN
   lvc.mask = LVCF_TEXT
   lvc.pszText = Text$
   SendMessage(hWnd, LVM_SETCOLUMN, Column, &lvc)
   SendMessage(LView1, LVM_SETCOLUMNWIDTH, Column, LVSCW_AUTOSIZE_USEHEADER)
 END SUB
  
 SUB BCX_LV_Reset(ByVal LView AS CONTROL, ByVal Columns, ByVal Rows)
   LOCAL lvItem AS LV_ITEM
   LOCAL(i)
   ListView_DeleteAllItems(LView)
   REPEAT(Rows)
     lvItem.mask = LVIF_TEXT
     lvItem.pszText = " "
     lvItem.iSubItem = 0
     ListView_InsertItem(LView1, &lvItem)
   END REPEAT
 END SUB
  
 SUB BCX_LV_Justify(ByVal LV AS HANDLE, ByVal Column, ByVal JustifyType)
   LOCAL hHeader AS LONG
   LOCAL hdi AS HD_ITEM
   '*******************************************
   CONST HDF_LEFT = 0    'JustifyType
   CONST HDF_RIGHT = 1    'JustifyType
   CONST HDF_CENTER = 2    'JustifyType
   '*******************************************
   CONST LVM_FIRST = 4096
   CONST LVM_GETHEADER = LVM_FIRST + 31
   '*******************************************
   hHeader = SendMessage(LV, LVM_GETHEADER, 0, 0)
   hdi.mask = HDI_FORMAT
   hdi.pszText = " "
   hdi.fmt = HDF_STRING OR JustifyType
   SendMessage((HWND)hHeader, HDM_SETITEM, Column, &hdi)
 END SUB