@@ -6,11 +6,12 @@ import android.content.Context
66import android.database.Cursor
77import android.database.sqlite.SQLiteDatabase
88import android.database.sqlite.SQLiteOpenHelper
9+ import android.util.Log
910import androidx.core.content.contentValuesOf
1011import androidx.lifecycle.ViewModelProvider
1112
1213class DBFarm (val context : Context , val factory : SQLiteDatabase .CursorFactory ? ) :
13- SQLiteOpenHelper (context, " user" , factory, 5 ){
14+ SQLiteOpenHelper (context, " user" , factory, 8 ){
1415 override fun onCreate (db : SQLiteDatabase ? ) {
1516 val query = " CREATE TABLE users (idUsers INTEGER PRIMARY KEY AUTOINCREMENT, login TEXT, pass TEXT)"
1617 db!! .execSQL(query)
@@ -96,6 +97,7 @@ class DBFarm(val context: Context, val factory: SQLiteDatabase.CursorFactory?) :
9697 valuesSalary.put(" hourly_rate" , salary.hoursRate)
9798 valuesSalary.put(" hectares" , salary.hectares)
9899 valuesSalary.put(" hectare_rate" , salary.hectaresRate)
100+
99101 val db = this .writableDatabase
100102 db.insert(" salary" , null , valuesSalary)
101103 db.close()
@@ -119,4 +121,32 @@ class DBFarm(val context: Context, val factory: SQLiteDatabase.CursorFactory?) :
119121
120122 }
121123
124+ fun calculateSalary (employeeId : Int , startDate : String , endDate : String ): Double {
125+ val db = this .readableDatabase
126+ Log .d(" DBFarm" , " Calculating salary for employeeId: $employeeId , from: $startDate to: $endDate " )
127+ val query = """
128+ SELECT SUM(hours * hourly_rate + hectares * hectare_rate)
129+ FROM salary
130+ WHERE employee_id = ?
131+ AND work_date BETWEEN ? AND ?
132+ """
133+ // Логирование запроса и параметров
134+ Log .d(" DBFarm" , " SQL Query: $query " )
135+ Log .d(" DBFarm" , " Parameters: ${arrayOf(employeeId.toString(), startDate, endDate).joinToString(" , " )} " )
136+ val cursor: Cursor = db.rawQuery(query, arrayOf(employeeId.toString(), startDate, endDate))
137+ val totalSalary = if (cursor.moveToFirst()) {
138+ val result = cursor.getDouble(0 )
139+ Log .d(" DBFarm" , " Total Salary: $result " )
140+ result
141+ } else {
142+ Log .d(" DBFarm" , " No data found." )
143+ 0.0
144+ }
145+ cursor.close()
146+ db.close()
147+ return totalSalary
148+ }
149+
150+
151+
122152}
0 commit comments