'Activity module
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim SQL1 As SQL
Dim SQL2 As SQL
Dim dbCursor As Cursor
Dim sets As Map
sets.Initialize
Dim hhid As String
Dim invnum As String
Dim Taxrate1 As Double
Dim dayofmonth As Long
Dim Firstdayofmonth As Long
Dim lastdayofmonth As Long
Dim create As String
Dim installdate As String
Dim installtime As String
Dim PersonalEmail As String
Dim Profemail As String
Dim Gmailusername As String
Dim Password As String
Dim ftpdate As String
Dim ftpinvnum As Int
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Dim btnHelp As Button
Dim txtNewInstall As EditText
Dim txtInstallDate As EditText
Dim txtHandheldID As EditText
Dim txtInstallTime As EditText
Dim Button1 As Button
Dim txtInvNum As EditText
Dim cbDefault As CheckBox
Dim btnRI As Button
Dim wvRI As WebView
Dim lblTaxRate As Label
Dim lblInvNum As Label
Dim lblVehID As Label
Dim txtStockNum As EditText
Dim txtDate As EditText
Dim txtYear As EditText
Dim btnVehUpdate As Button
Dim EditText1 As EditText
Dim Label1 As Label
Dim btnPayments As Button
Dim Label7 As Label
Dim Label8 As Label
Dim btnBackUp As Button
Dim emailaddy As String
Dim sentto As String
Dim lvBackupcasetext As String
Dim lblDateUnix As Label
Dim lblCurrencysign As Label
Dim lvBackup As ListView
'Dim ftpdate As String
Dim FTPHost As String
Dim FTPPort As Int
Dim FTPUser As String
Dim FTPPassword As String
Dim FTPServerfilepath As String
Dim FTPDevicefolder As String
Dim FTPDeviceFile As String
Dim lastupload As Long
Dim spnFTP As Spinner
Dim ListView1 As ListView
End Sub
Sub Activity_Create(FirstTime As Boolean)
'#### Load the layout ####
Activity.LoadLayout("Menu")
'#### Add menu items ####
Activity.AddMenuItem("Help","help")
Activity.AddMenuItem("About","help")
'Activity.AddMenuItem("Delete all invoices","help")
'#### Check and load the database ####
If File.Exists(File.DirInternal, "arinvoice.db") = False Then
File.Copy(File.DirAssets, "arinvoice.db", File.DirInternal, "arinvoice.db")
End If
'****Initialize the database****
SQL1.Initialize(File.DirInternal, "arinvoice.db", False)
'#### Load the setup info and see if it is a new install ####
txtNewInstall.Text = SQL1.ExecQuerySingleResult("SELECT newinstall FROM settings")
txtHandheldID.Text = SQL1.ExecQuerySingleResult("SELECT handheldid FROM settings")
txtInvNum.Text = SQL1.ExecQuerySingleResult("SELECT invnum FROM settings")
txtInstallDate.Text = SQL1.ExecQuerySingleResult("SELECT installdate FROM settings")
txtInstallTime.Text = SQL1.ExecQuerySingleResult("SELECT installtime FROM settings")
lblDateUnix.Text = SQL1.ExecQuerySingleResult("SELECT installdateunix FROM settings")
lblCurrencysign.Text = SQL1.ExecQuerySingleResult("SELECT currencysign FROM settings")
lblTaxRate.Text = SQL1.ExecQuerySingleResult("SELECT taxrate FROM settings")
'****Load the email setup info****
Password = SQL1.ExecQuerySingleResult("SELECT gmailpassword FROM services")
Gmailusername = SQL1.ExecQuerySingleResult("SELECT gmailusername FROM services")
PersonalEmail = SQL1.ExecQuerySingleResult("SELECT peremail FROM services")
Profemail = SQL1.ExecQuerySingleResult("SELECT busemail FROM services")
'#### If it is a new install, set the installation info for and write it to the .set file ####
If txtNewInstall.Text = "new" Then
Msgbox("This is a new install. You must set up now.","")
txtNewInstall.Text = "ok"
Dim now As Long
Dim jdate As Long
now = DateTime.now
txtInstallDate.Text = DateTime.Date(now) 'sets the install date
txtInstallTime.Text = DateTime.Time(now) 'sets the install time
jdate = DateTime.DateParse(txtInstallDate.Text) 'sets the install date to unix
'****Add all of the above info to the database****
SQL1.ExecNonQuery2("UPDATE settings SET newinstall = ?, installdate = ?, installdateunix = ?, installtime = ?, taxrate = ? WHERE rowid = 1", Array As String(txtNewInstall.Text, txtInstallDate.Text, jdate, txtInstallTime.Text, TaxRate))
'****On a new install we go to the setup activity, else we load the menu****
Activity.finish
StartActivity("HandheldID")
End If
'#### Assign the various variables ####
hhid = txtHandheldID.Text
invnum = txtInvNum.Text
create = ""
installdate = txtInstallDate.Text
installtime = txtInstallTime.Text
Taxrate1 = lblTaxRate.text
'#### Get month to date sales ####
Try
Dim mtdsales As Long
Dim unixdate As Long
Dim today As String
Dim thismonth As Long
Dim thisyear As Long
today = DateTime.Date(DateTime.now)
unixdate = DateTime.DateParse(today)
thismonth = DateTime.GetMonth(unixdate)
thisyear = DateTime.GetYear(unixdate)
dayofmonth = DateTime.GetDayOfMonth(DateTime.now)
Firstdayofmonth = DateTime.Add(unixdate, 0, 0, (dayofmonth * -1) + 1)
lastdayofmonth = DateTime.Add(Firstdayofmonth, 0, 1, -1)
mtdsales = SQL1.ExecQuerySingleResult("SELECT sum(total) FROM invoice WHERE invest = 'Inv#' AND extra2 BETWEEN '" & Firstdayofmonth & "' AND '"& lastdayofmonth & "'")
mtdsales = NumberFormat2(mtdsales,0,2,2,False)
Label8.Text = mtdsales
Label8.Text = NumberFormat2(Label8.Text,0,2,2,False)
Label7.Text = "Month to Date Sales - " & Label8.Text
Catch
End Try
'#### FTP Stuff ####
dbCursor = SQL1.ExecQuery("SELECT * FROM ftpinfo") 'Grab the data from the ftp file
For i = 0 To dbCursor.RowCount -1
dbCursor.Position = i
FTPHost = dbCursor.GetString("host")
FTPPort = dbCursor.GetString("port")
FTPUser = dbCursor.GetString("user")
FTPPassword = dbCursor.GetString("password")
FTPServerfilepath = dbCursor.GetString("serverfilepath")
FTPDevicefolder = dbCursor.GetString("devicefolder")
FTPDeviceFile = dbCursor.GetString("devicefile")
Next
If FirstTime Then
Services.FTP.Initialize("FTP", FTPHost, FTPPort, FTPUser, FTPPassword)
End If
Services.FTP.PassiveMode = True
'Grab todays date to upload to ftp
datetoday
'Upload the date to ftp
SQL1.ExecNonQuery2("UPDATE ftpinfo SET lastupdated = ?", Array As String(lastupload))
'#################
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
'###############################
'#### Create activity stuff ####
'###############################
Sub help_click
'#### Menu items code ####
Select Sender
Case "Help"
Msgbox("Beta version. Please let me know of any problems. Thanks, Rick [email protected]","")
Case "About"
Msgbox("Version 1.0b","")
'Case "Delete all invoices"
'Dim result As Int
' result = Msgbox2("Do you want to delete all invoices ?", _
'"A T T E N T I O N", "Yes", "", "No", Null)
'If result = DialogResponse.POSITIVE Then
'SQL1.ExecNonQuery("DELETE FROM panels")
'SQL1.ExecNonQuery("DELETE FROM vehicle")
'SQL1.ExecNonQuery("DELETE FROM invoice")
'End If
End Select
End Sub
Sub timestamp
'#### Set the timestamp ####
Dim now As Long
Dim dt As String
DateTime.DateFormat = "yyMMddHHmmss"
dt = DateTime.Date(now)
txtInstallDate.Text = dt
End Sub
Sub datetoday
'#### Grab todays date ####
Dim today As String
Dim now As Long
DateTime.DateFormat = "yyyyMMdd"
today = DateTime.Date(DateTime.now)
ftpdate = today
'Set the variable for the ftp last upload time
lastupload = DateTime.now
End Sub
Sub Activity_KeyPress (KeyCode As Int) As Boolean 'Return True to consume the event
'****Testing the back arrow code-not used now****
'Dim Answ As Int
'If KeyCode = KeyCodes.KEYCODE_BACK Then
'Answ = Msgbox2("Do you want to quit the program ?", _
'"A T T E N T I O N", "Yes", "", "No", Null)
'If Answ = DialogResponse.NEGATIVE Then
'Return True
'End If
'End If
'Return False
'***********************************************
End Sub
'##########################
'#### Menu buttons Code####
'##########################
Sub btnCreate_Click
'****Set the variable to create mode"
create = "create"
Invoice.timerflag = 1
'****Start the Create Invoice activity****
StartActivity("Invoice")
'*****************************************
End Sub
Sub btnEdit_Click
'****Start the Edit Invoice activity****
Invoice.timerflag = 0
create = "edit"
Activity.finish
StartActivity("Invoice")
'***************************************
End Sub
Sub btnActions_Click
'****Start the maintenance activity****
StartActivity("Maintenance")
'**************************************
End Sub
Sub btnReports_Click
'****Start the reports activity****
StartActivity("Reports")
'**********************************
End Sub
Sub btnExit_Click
'****Close the app****
Activity.finish
'*********************
End Sub
Sub Label1_Click
'****Start Payments screen****
StartActivity("Payments")
'*****************************
End Sub
Sub btnPayments_Click
'****Start Payments screen****
StartActivity("Payments")
'*****************************
End Sub
Sub btnBackUp_Click
'****Show the backup listview****
lvBackup.Visible = True
lvbackupshow
'********************************
End Sub
Sub lblCurrencysign_Click
'****Show the report editor****
Activity.Finish
StartActivity("ReportEditor")
'******************************
End Sub
Sub OpenSpinner(s As Spinner)
'****Code for opening the spinner with a button****
Dim r As Reflector
r.Target = s
r.RunMethod("performClick")
'**************************************************
End Sub
Sub lvbackupshow
'****Clear the backup listview****
lvBackup.Clear
'****Set the height and width****
lvBackup.Height = 100%y
lvBackup.Width = 100%x
'****Add the lines****
lvBackup.AddSingleLine("Personal Email")
lvBackup.AddSingleLine("Business Email")
lvBackup.AddSingleLine("FTP")
lvBackup.AddSingleLine("Exit")
'****Set the settings****
lvBackup.SingleLineLayout.ItemHeight = 50dip
lvBackup.SingleLineLayout.Label.TextSize = 18
lvBackup.SingleLineLayout.Label.TextColor = Colors.Black
lvBackup.SingleLineLayout.Label.Gravity = Gravity.CENTER_VERTICAL
lvBackup.ScrollingBackgroundColor = Colors.Transparent
'************************
End Sub
Sub lvBackup_ItemClick (Position As Int, Value As Object)
'****Set the variable of the selected item****
lvBackupcasetext = lvBackup.GetItem(Position)
'****Perform the actions****
Select lvBackupcasetext
Case "Personal Email"
peremail
lvBackup.Visible = False
Case "Business Email"
proemail
lvBackup.Visible = False
Case "FTP"
OpenSpinner(spnFTP)
spnFTP.Clear
spnFTP.Add("All")
spnFTP.Add("Single Invoice")
lvBackup.Visible = False
Case "Exit"
lvBackup.Visible = False
End Select
'**************************
End Sub
Sub peremail
'#### Setup emailing to personal address ####
'****Set the filename variable****
Dim filename As String
filename = ftpdate & "_" & hhid & "_" & "arinvoice.zip"
'****Zip the file****
Services.myZip.ABZipfile(File.DirInternal & "/", "arinvoice.db", File.DirRootExternal & "/ARIBackup/" & filename)
'**** Grab the info****
Dim M As Map
M = DBUtils.ExecuteMap(SQL1, "SELECT peremail, busemail, gmailusername, gmailpassword FROM services",Null)
If M = Null Then 'Null will return if there is no match
Msgbox("Services not yet set up.","")
Else
PersonalEmail = M.Get("peremail")
Profemail = M.Get("busemail")
Gmailusername = M.Get("gmailusername")
Password = M.Get("gmailpassword")
emailaddy = PersonalEmail
End If
'****Set the email address variable****
sentto = emailaddy
'****Send the email****
'If FirstTime Then password-gogogogo1
Invoice.SMTP.Initialize("smtp.gmail.com", 465, Gmailusername, Password, "SMTP")
Invoice.SMTP.UseSSL = True 'Gmail requires SSL.
Invoice.SMTP.To.Add(emailaddy)
Invoice.SMTP.Subject = "Backup files from ARIDroid"
Invoice.SMTP.Body = "Here is your backup files."
Invoice.SMTP.AddAttachment(File.DirRootExternal & "/ARIBackup/", filename)
Invoice.SMTP.Send
Msgbox("Backup sent to " & sentto,"")
'***********************
End Sub
Sub proemail
'#### Setup emailing to business address ####
'****Set the filename variable****
Dim filename As String
filename = ftpdate & "_" & hhid & "_" & "arinvoice.zip"
'****Zip the file****
Services.myZip.ABZipfile(File.DirInternal & "/", "arinvoice.db", File.DirRootExternal & "/" & filename)
'**** Grab the info****
Dim M As Map
M = DBUtils.ExecuteMap(SQL1, "SELECT peremail, busemail, gmailusername, gmailpassword FROM services",Null)
If M = Null Then 'Null will return if there is no match
Msgbox("Services not yet set up.","")
Else
PersonalEmail = M.Get("peremail")
Profemail = M.Get("busemail")
Gmailusername = M.Get("gmailusername")
Password = M.Get("gmailpassword")
emailaddy = Profemail
End If
'****Set the email address variable****
sentto = emailaddy
'****Send the email****
Invoice.SMTP.Initialize("smtp.gmail.com", 465, Gmailusername, Password, "SMTP")
Invoice.SMTP.UseSSL = True 'Gmail requires SSL.
Invoice.SMTP.To.Add(emailaddy)
Invoice.SMTP.Subject = "Backup files from ARIDroid"
Invoice.SMTP.Body = "Here is your backup files."
Invoice.SMTP.AddAttachment(File.DirRootExternal & "/ARIBackup/", filename)
Invoice.SMTP.Send
Msgbox("Backup sent to " & sentto,"")
'***********************
End Sub
Sub ftpsend
'#### Setup the FTP Send Stuff ####
'****Zip the file****
Dim serverpath As String
Services.myZip.ABZipfile(File.DirInternal & "/", "arinvoice.db", File.DirRootExternal & "/ARIBackup/" & "arinvoice.zip")
'**** Upload the file****
Services.FTP.UploadFile(File.DirRootExternal & "/ARIBackup/", "arinvoice.zip", False, ftpdate & "_" & hhid & "_" & FTPServerfilepath)
'****Set the lastupload field in the database****
lastupload = DateTime.now
SQL1.ExecNonQuery2("UPDATE ftpinfo SET lastupdated = ?", Array As String(lastupload))
Msgbox("File uploaded","")
'************************************************
End Sub
Sub FTP_UploadProgress (ServerPath As String, TotalUploaded As Long, Total As Long)
'****Upload progress****
Dim s As String
s = "Uploaded " & Round(TotalUploaded / 1000) & "KB"
If Total > 0 Then s = s & " out of " & Round(Total / 1000) & "KB"
Log(s)
'***********************
End Sub
Sub FTP_UploadCompleted (ServerPath As String, Success As Boolean)
'****Upload completed****
Log(ServerPath & ", Success=" & Success)
If Success = False Then Log(LastException.Message)
'************************
End Sub
Sub spnFTP_ItemClick (Position As Int, Value As Object)
'****Commands based on selections****
If spnFTP.SelectedItem = "All" Then
Try
Dim table1 As List
table1 = DBUtils.ExecuteMemoryTable(SQL1,"SELECT * FROM invoice WHERE extra1 > '" & lastupload & "'",Null,0)
Dim si As StringUtils
si.SaveCSV(File.DirRootExternal & "/ARITempdb/", "invoice.csv", ";", table1)
Catch
End Try
Try
Dim table1 As List
table1 = DBUtils.ExecuteMemoryTable(SQL1,"SELECT * FROM vehicle WHERE extra1 > '" & lastupload & "'",Null,0)
Dim si As StringUtils
si.SaveCSV(File.DirRootExternal & "/ARITempdb/", "vehicle.csv", ";", table1)
Catch
End Try
Try
Dim table1 As List
table1 = DBUtils.ExecuteMemoryTable(SQL1,"SELECT * FROM panels WHERE extra1 > '" & lastupload & "'",Null,0)
Dim si As StringUtils
si.SaveCSV(File.DirRootExternal & "/ARITempdb/", "panels.csv", ";", table1)
Catch
End Try
Services.myZip.ABZipDirectory( File.DirRootExternal & "/ARITempdb", File.DirRootExternal & "/ARIBackup/" & "arinvoice.zip")
'**** Upload the file****
Services.FTP.UploadFile(File.DirRootExternal & "/ARIBackup/", "arinvoice.zip", False, ftpdate & "_" & hhid & "_" & "arinvoice.zip") 'FTPServerfilepath)
' '****Set the lastupload field in the database****
'
Msgbox("File uploaded","")
File.Delete(File.DirRootExternal & "/ARITempdb/","*.*")
End If
If spnFTP.SelectedItem = "Single Invoice" Then
ListView1.Visible = True
invoicelistview
End If
'************************************
End Sub
Sub invoicelistview
' This part populates the ListView
ListView1.clear
'DBUtils.ExecuteListView(SQL1, "Select [invoicenumber] || ' ' || [date] || ' ' || [name] from invoice ORDER by invoicenumber DESC", Null, 0, ListView1, False)
dbCursor = SQL1.ExecQuery("SELECT invoicenumber, date, name FROM invoice ORDER by invoicenumber DESC")
For i = 0 To dbCursor.RowCount - 1
dbCursor.Position = i
' Set up the ListView
ListView1.AddSingleLine(dbCursor.GetString("invoicenumber") & " : " _
& dbCursor.GetString("date") & " " & dbCursor.GetString("name"))
Next
ListView1.SingleLineLayout.ItemHeight = 75dip
ListView1.SingleLineLayout.Label.TextSize = 18
ListView1.SingleLineLayout.Label.TextColor = Colors.White
ListView1.SingleLineLayout.Label.Gravity = Gravity.LEFT
ListView1.ScrollingBackgroundColor = Colors.Transparent
End Sub
Sub ListView1_ItemLongClick (Position As Int, Value As Object)
ListView1.Visible = False
'What to do when we make a selection
Dim selectedrecord As String
selectedrecord = Value
countit = selectedrecord.IndexOf(":")
selectedrecord = selectedrecord.SubString2(0,countit)
ftpinvnum = selectedrecord
Try
Dim table1 As List
table1 = DBUtils.ExecuteMemoryTable(SQL1,"SELECT * FROM invoice WHERE invoicenumber = '" & ftpinvnum & "'",Null,0)
Dim si As StringUtils
si.SaveCSV(File.DirRootExternal & "/ARITempdb/", "invoice.csv", ";", table1)
Catch
End Try
Try
Dim table1 As List
table1 = DBUtils.ExecuteMemoryTable(SQL1,"SELECT * FROM vehicle WHERE invoicenumber = '" & ftpinvnum & "'",Null,0)
Dim si As StringUtils
si.SaveCSV(File.DirRootExternal & "/ARITempdb/", "vehicle.csv", ";", table1)
Catch
End Try
Try
Dim table1 As List
table1 = DBUtils.ExecuteMemoryTable(SQL1,"SELECT * FROM panels WHERE invoicenumber = '" & ftpinvnum & "'",Null,0)
Dim si As StringUtils
si.SaveCSV(File.DirRootExternal & "/ARITempdb/", "panels.csv", ";", table1)
Catch
End Try
Services.myZip.ABZipDirectory( File.DirRootExternal & "/ARITempdb", File.DirRootExternal & "/ARIBackup/" & "arinvoice.zip")
'**** Upload the file****
Services.FTP.UploadFile(File.DirRootExternal & "/ARIBackup/", "arinvoice.zip", False, ftpdate & "_" & hhid & "_inv#_" & ftpinvnum & "arinvoice.zip") 'FTPServerfilepath)
'****Set the lastupload field in the database****
Msgbox("File uploaded","")
File.Delete(File.DirRootExternal & "/ARITempdb/","*.*")
End Sub