Skip to content
Snippets Groups Projects

Dev

Open Кирилл Шустов requested to merge dev into play-market
14 unresolved threads
36 files
+ 895
174
Compare changes
  • Side-by-side
  • Inline
Files
36
@@ -18,9 +18,7 @@ import com.keepsoft_lib.mvp.utils.SmsConstants.SMS_CARD_ACCOUNT_ID
import com.keepsoft_lib.mvp.utils.SmsConstants.SMS_TYPE_VARIANT_EXPENSES
import com.keepsoft_lib.mvp.utils.SmsConstants.SMS_TYPE_VARIANT_INCOMES
import com.keepsoft_lib.mvp.utils.SmsConstants.SMS_TYPE_VARIANT_TRANSFERS
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.*
import java.text.ParseException
import java.util.*
import javax.inject.Inject
@@ -281,9 +279,9 @@ class DatabaseRepository @Inject constructor(private val context: Context) {
return id
}
private fun isExistMatchModel(journalItem: JournalSMS): Int {
private fun isExistMatchModel(typeVariant: Int, description: String): Int {
val cursor = context.contentResolver.query(HomeBuh.SMSMatch.CONTENT_URI, arrayOf(HomeBuh.SMSMatch._ID), HomeBuh.SMSMatch.DESCRIPTION + " = ? AND " +
HomeBuh.SMSMatch.MATCH_TYPE_VARIANT + " = ? ", arrayOf(journalItem.description, journalItem.typeVariant.toString()), null)
HomeBuh.SMSMatch.MATCH_TYPE_VARIANT + " = ? ", arrayOf(description, typeVariant.toString()), null)
if (cursor != null && cursor.count > 0) {
cursor.moveToNext()
val id = cursor.getInt(cursor.getColumnIndex(HomeBuh.SMSMatch._ID))
@@ -293,9 +291,7 @@ class DatabaseRepository @Inject constructor(private val context: Context) {
return -1
}
fun saveMatchModel(category: CategoryModel, journalItem: JournalSMS) {
val matchModel = categoryToMatchModel(category = category, journalSMS = journalItem)
val id = isExistMatchModel(journalItem = journalItem)
private fun partSaveMatchModel(matchModel: MatchModel, id: Int) {
if (id == -1) {
val contentValues = ContentValues()
contentValues.put(HomeBuh.SMSMatch.USER_ACCOUNT_ID, (context as HBApp).currentUser)
@@ -313,6 +309,18 @@ class DatabaseRepository @Inject constructor(private val context: Context) {
contentValues.put(HomeBuh.SMSMatch.SUBCATEGORY, matchModel.subCategoryId)
context.contentResolver.update(HomeBuh.SMSMatch.CONTENT_URI, contentValues, HomeBuh.SMSMatch._ID + " = ? ", arrayOf(id.toString()))
}
}
fun saveMatchModelReceipt(category: CategoryModel, receiptItemUI: ReceiptItemUI) {
val matchModel = categoryToMatchModel(category = category, receipt = receiptItemUI)
val id = isExistMatchModel(0, receiptItemUI.name)
partSaveMatchModel(matchModel, id)
}
fun saveMatchModel(category: CategoryModel, journalItem: JournalSMS) {
val matchModel = categoryToMatchModel(category = category, journalSMS = journalItem)
val id = isExistMatchModel(journalItem.typeVariant, journalItem.description)
partSaveMatchModel(matchModel, id)
updateCategoryInExpensesIncomeTransfer(matchModel, journalItem)
}
@@ -333,7 +341,7 @@ class DatabaseRepository @Inject constructor(private val context: Context) {
fun getAccounts(): ArrayList<Account> {
val accounts = arrayListOf<Account>()
val cursor = context.contentResolver.query(HomeBuh.Accounts_for_journal.CONTENT_URI, HomeBuh.Accounts.DIALOG_REQUEST_COLUMNS_NEW,
HomeBuh.Accounts.NAME+" = ?", arrayOf((context as HBApp).currentUser), null)
HomeBuh.Accounts.NAME + " = ?", arrayOf((context as HBApp).currentUser), null)
if (cursor != null && cursor.count > 0) {
cursor.moveToFirst()
do {
@@ -679,4 +687,73 @@ class DatabaseRepository @Inject constructor(private val context: Context) {
}
suspend fun saveExpenses(items: List<ReceiptItemUI>, account: String, dateTime: Long): Boolean {
return try {
GlobalScope.async {
val accId = getAccountIdByAccountName(account)
for (item in items) {
val contentValues = ContentValues()
contentValues.put(Expenses.ACCOUNT, accId)
if (item.subCategoryId == -1)
contentValues.put(Expenses.CATEGORY, item.categoryId)
else
contentValues.put(Expenses.CATEGORY, item.subCategoryId)
contentValues.put(Expenses.QUANTITY, item.quantity)
try {
contentValues.put(Expenses.MONEY, Constants.parse(item.sum))
contentValues.put(Expenses.MYDATE, Constants.str2unix(context, Utils._convertMillisToStringDate(context, dateTime * 1000L)))
} catch (e: ParseException) {
e.printStackTrace()
}
contentValues.putNull(Expenses.RATE)
contentValues.put(Expenses.LASTCHANGE, System.currentTimeMillis() / 1000)
contentValues.put(Expenses.DELETED, 0)
contentValues.put(Expenses.NUMCURRENCY, getCurrencyByCurrencyList("1"))
context.contentResolver.insert(Expenses.CONTENT_URI, contentValues)
}
true
}
} catch (e: Exception) {
GlobalScope.async { error(e) }
}.await()
}
suspend fun saveExpenses(category: CategoryModel?, subCategory: CategoryModel?, receipt: Receipt?, account: String): Boolean {
return try {
GlobalScope.async {
val contentValues = ContentValues()
val accId = getAccountIdByAccountName(account)
contentValues.put(Expenses.ACCOUNT, accId)
if (subCategory == null)
contentValues.put(Expenses.CATEGORY, category!!.id)
else
contentValues.put(Expenses.CATEGORY, subCategory.id)
contentValues.put(Expenses.QUANTITY, 1.0f)
try {
contentValues.put(Expenses.MONEY, Constants.parse(receipt!!.totalSum))
contentValues.put(Expenses.MYDATE, Constants.str2unix(context, Utils._convertMillisToStringDate(context, receipt.dateTime * 1000L)))
} catch (e: ParseException) {
e.printStackTrace()
}
contentValues.putNull(Expenses.RATE)
contentValues.put(Expenses.LASTCHANGE, System.currentTimeMillis() / 1000)
contentValues.put(Expenses.DELETED, 0)
contentValues.put(Expenses.NUMCURRENCY, getCurrencyByCurrencyList("1"))
context.contentResolver.insert(Expenses.CONTENT_URI, contentValues)
true
}
} catch (e: Exception) {
GlobalScope.async { error(e) }
}.await()
}
private fun getAccountIdByAccountName(account: String): Int {
for (acc in getAccountModels())
if (acc.account == account)
return acc.id
return -1
}
}
\ No newline at end of file
Loading