2 Commits

Author SHA1 Message Date
soraefir 56036d5840 Fix toast & minor category/paymentmode fix 2026-07-02 02:43:33 +02:00
soraefir 4fa29da79d Better category/paymentmode storage and handling 2026-07-02 02:37:40 +02:00
19 changed files with 182 additions and 143 deletions
@@ -57,7 +57,7 @@ class EditBillActivity : AppCompatActivity() {
val hardcoded = if (projectType == ProjectType.LOCAL) { val hardcoded = if (projectType == ProjectType.LOCAL) {
defaultCategories defaultCategories
} else { } else {
listOfNotNull(defaultCategories.find { it.remoteId.toInt() == DBBill.CATEGORY_REIMBURSEMENT }) listOfNotNull(defaultCategories.find { it.remoteId == DBBill.CATEGORY_REIMBURSEMENT.toLong() })
} }
syncedCategories + hardcoded syncedCategories + hardcoded
} }
@@ -144,7 +144,7 @@ class EditBillActivity : AppCompatActivity() {
bill = DBBill( bill = DBBill(
first.id, 0, first.projectId, first.payerId, totalAmount, first.id, 0, first.projectId, first.payerId, totalAmount,
first.timestamp, first.what, first.state, first.repeat, first.timestamp, first.what, first.state, first.repeat,
first.paymentMode, first.categoryRemoteId, first.comment, first.paymentModeRemoteId first.paymentMode, first.categoryId, first.comment, first.paymentModeId
) )
val splits = mutableMapOf<Long, Double>() val splits = mutableMapOf<Long, Double>()
@@ -168,15 +168,15 @@ class EditBillActivity : AppCompatActivity() {
bill = DBBill( bill = DBBill(
0, 0, projectId, 0, 0.0, timeNowSeconds, 0, 0, projectId, 0, 0.0, timeNowSeconds,
"", DBBill.STATE_ADDED, DBBill.NON_REPEATED, "", DBBill.STATE_ADDED, DBBill.NON_REPEATED,
DBBill.PAYMODE_NONE, DBBill.CATEGORY_NONE, "", DBBill.PAYMODE_ID_NONE DBBill.PAYMODE_NONE, DBBill.CATEGORY_NONE.toLong(), "", DBBill.PAYMODE_ID_NONE.toLong()
) )
} else { } else {
val btd = db.getBill(billIdToDuplicate)!! val btd = db.getBill(billIdToDuplicate)!!
bill = DBBill( bill = DBBill(
0, 0, projectId, btd.payerId, btd.amount, 0, 0, projectId, btd.payerId, btd.amount,
timeNowSeconds, btd.what, DBBill.STATE_ADDED, timeNowSeconds, btd.what, DBBill.STATE_ADDED,
btd.repeat, btd.paymentMode, btd.categoryRemoteId, btd.repeat, btd.paymentMode, btd.categoryId,
btd.comment, btd.paymentModeRemoteId btd.comment, btd.paymentModeId
) )
val btdOwers = btd.billOwers val btdOwers = btd.billOwers
val newBillOwers = btdOwers.filter { val newBillOwers = btdOwers.filter {
@@ -239,8 +239,8 @@ class EditBillActivity : AppCompatActivity() {
bill = DBBill( bill = DBBill(
0, 0, bill.projectId, viewModel.payerId, viewModel.amountAsDouble, 0, 0, bill.projectId, viewModel.payerId, viewModel.amountAsDouble,
System.currentTimeMillis() / 1000, viewModel.what, DBBill.STATE_ADDED, System.currentTimeMillis() / 1000, viewModel.what, DBBill.STATE_ADDED,
viewModel.repeat, bill.paymentMode, viewModel.categoryRemoteId, viewModel.repeat, bill.paymentMode, viewModel.categoryId,
viewModel.getFinalComment(), viewModel.paymentModeRemoteId viewModel.getFinalComment(), viewModel.paymentModeId
) )
calendar.timeInMillis = System.currentTimeMillis() calendar.timeInMillis = System.currentTimeMillis()
viewModel.timestamp = calendar.timeInMillis / 1000 viewModel.timestamp = calendar.timeInMillis / 1000
@@ -326,8 +326,8 @@ class EditBillActivity : AppCompatActivity() {
bill.payerId == viewModel.payerId && bill.payerId == viewModel.payerId &&
bill.comment == viewModel.getFinalComment() && bill.comment == viewModel.getFinalComment() &&
bill.repeat == viewModel.repeat && bill.repeat == viewModel.repeat &&
bill.categoryRemoteId == viewModel.categoryRemoteId && bill.categoryId == viewModel.categoryId &&
bill.paymentModeRemoteId == viewModel.paymentModeRemoteId && bill.paymentModeId == viewModel.paymentModeId &&
!owersChanged) !owersChanged)
} }
@@ -386,8 +386,8 @@ class EditBillActivity : AppCompatActivity() {
listOf(memberId), listOf(memberId),
viewModel.repeat, viewModel.repeat,
existingBill.paymentMode, existingBill.paymentMode,
viewModel.paymentModeRemoteId, viewModel.paymentModeId,
viewModel.categoryRemoteId, viewModel.categoryId,
finalComment finalComment
) )
if (firstSavedId == 0L) firstSavedId = billToUseId if (firstSavedId == 0L) firstSavedId = billToUseId
@@ -396,7 +396,7 @@ class EditBillActivity : AppCompatActivity() {
val newBill = DBBill( val newBill = DBBill(
0, 0, bill.projectId, viewModel.payerId, amount, 0, 0, bill.projectId, viewModel.payerId, amount,
viewModel.timestamp, viewModel.what, DBBill.STATE_ADDED, viewModel.repeat, viewModel.timestamp, viewModel.what, DBBill.STATE_ADDED, viewModel.repeat,
bill.paymentMode, viewModel.categoryRemoteId, finalComment, viewModel.paymentModeRemoteId bill.paymentMode, viewModel.categoryId, finalComment, viewModel.paymentModeId
) )
newBill.billOwers = listOf(DBBillOwer(0, 0, memberId)) newBill.billOwers = listOf(DBBillOwer(0, 0, memberId))
val newId = db.addBill(newBill) val newId = db.addBill(newBill)
@@ -441,8 +441,8 @@ class EditBillActivity : AppCompatActivity() {
newOwersIds, newOwersIds,
viewModel.repeat, viewModel.repeat,
bill.paymentMode, bill.paymentMode,
viewModel.paymentModeRemoteId, viewModel.paymentModeId,
viewModel.categoryRemoteId, viewModel.categoryId,
finalComment finalComment
) )
if (groupedBillIds != null) { if (groupedBillIds != null) {
@@ -460,7 +460,7 @@ class EditBillActivity : AppCompatActivity() {
val newBill = DBBill( val newBill = DBBill(
0, 0, bill.projectId, viewModel.payerId, newAmount, 0, 0, bill.projectId, viewModel.payerId, newAmount,
viewModel.timestamp, viewModel.what, DBBill.STATE_ADDED, viewModel.repeat, viewModel.timestamp, viewModel.what, DBBill.STATE_ADDED, viewModel.repeat,
bill.paymentMode, viewModel.categoryRemoteId, finalComment, viewModel.paymentModeRemoteId bill.paymentMode, viewModel.categoryId, finalComment, viewModel.paymentModeId
) )
newOwersIds.forEach { newBill.billOwers += DBBillOwer(0, 0, it) } newOwersIds.forEach { newBill.billOwers += DBBillOwer(0, 0, it) }
val newBillId = db.addBill(newBill) val newBillId = db.addBill(newBill)
@@ -468,7 +468,7 @@ fun BillAdditionalDetailsSection(
var categoryExpanded by remember { mutableStateOf(false) } var categoryExpanded by remember { mutableStateOf(false) }
val selectedCategory = val selectedCategory =
categories.find { it.remoteId.toInt() == viewModel.categoryRemoteId } categories.find { (if (it.id > 0) it.id else it.remoteId) == viewModel.categoryId }
EditableExposedDropdownMenu( EditableExposedDropdownMenu(
value = selectedCategory?.name ?: "", value = selectedCategory?.name ?: "",
@@ -488,7 +488,7 @@ fun BillAdditionalDetailsSection(
}, },
content = { content = {
DropdownMenuItem(onClick = { DropdownMenuItem(onClick = {
viewModel.categoryRemoteId = 0 viewModel.categoryId = 0
categoryExpanded = false categoryExpanded = false
}) { }) {
Icon(Icons.Default.Close, tint = Color.Red, contentDescription = null) Icon(Icons.Default.Close, tint = Color.Red, contentDescription = null)
@@ -497,7 +497,7 @@ fun BillAdditionalDetailsSection(
} }
categories.forEach { category -> categories.forEach { category ->
DropdownMenuItem(onClick = { DropdownMenuItem(onClick = {
viewModel.categoryRemoteId = category.remoteId.toInt() viewModel.categoryId = if (category.id > 0) category.id else category.remoteId
categoryExpanded = false categoryExpanded = false
}) { }) {
Text(text = category.icon, fontSize = 20.sp) Text(text = category.icon, fontSize = 20.sp)
@@ -512,7 +512,7 @@ fun BillAdditionalDetailsSection(
var pmExpanded by remember { mutableStateOf(false) } var pmExpanded by remember { mutableStateOf(false) }
val selectedPm = val selectedPm =
paymentModes.find { it.remoteId.toInt() == viewModel.paymentModeRemoteId } paymentModes.find { (if (it.id > 0) it.id else it.remoteId) == viewModel.paymentModeId }
EditableExposedDropdownMenu( EditableExposedDropdownMenu(
value = selectedPm?.name ?: "", value = selectedPm?.name ?: "",
@@ -532,7 +532,7 @@ fun BillAdditionalDetailsSection(
}, },
content = { content = {
DropdownMenuItem(onClick = { DropdownMenuItem(onClick = {
viewModel.paymentModeRemoteId = 0 viewModel.paymentModeId = 0
pmExpanded = false pmExpanded = false
}) { }) {
Icon(Icons.Default.Close, tint = Color.Red, contentDescription = null) Icon(Icons.Default.Close, tint = Color.Red, contentDescription = null)
@@ -541,7 +541,7 @@ fun BillAdditionalDetailsSection(
} }
paymentModes.forEach { pm -> paymentModes.forEach { pm ->
DropdownMenuItem(onClick = { DropdownMenuItem(onClick = {
viewModel.paymentModeRemoteId = pm.remoteId.toInt() viewModel.paymentModeId = if (pm.id > 0) pm.id else pm.remoteId
pmExpanded = false pmExpanded = false
}) { }) {
Text(text = pm.icon, fontSize = 20.sp) Text(text = pm.icon, fontSize = 20.sp)
@@ -25,8 +25,8 @@ class EditBillViewModel : ViewModel() {
var timestamp by mutableLongStateOf(0L) var timestamp by mutableLongStateOf(0L)
var payerId by mutableLongStateOf(0L) var payerId by mutableLongStateOf(0L)
var repeat by mutableStateOf(DBBill.NON_REPEATED) var repeat by mutableStateOf(DBBill.NON_REPEATED)
var paymentModeRemoteId by mutableIntStateOf(0) var paymentModeId by mutableLongStateOf(0L)
var categoryRemoteId by mutableIntStateOf(0) var categoryId by mutableLongStateOf(0L)
var isNewBill by mutableStateOf(false) var isNewBill by mutableStateOf(false)
var currencies by mutableStateOf<List<DBCurrency>>(emptyList()) var currencies by mutableStateOf<List<DBCurrency>>(emptyList())
@@ -166,8 +166,8 @@ class EditBillViewModel : ViewModel() {
timestamp = bill.timestamp timestamp = bill.timestamp
payerId = bill.payerId payerId = bill.payerId
repeat = bill.repeat ?: DBBill.NON_REPEATED repeat = bill.repeat ?: DBBill.NON_REPEATED
paymentModeRemoteId = bill.paymentModeRemoteId paymentModeId = bill.paymentModeId
categoryRemoteId = bill.categoryRemoteId categoryId = bill.categoryId
val rawComment = bill.comment ?: "" val rawComment = bill.comment ?: ""
@@ -39,15 +39,15 @@ class LabelBillsActivity : AppCompatActivity() {
val members = db.getMembersOfProject(projectId, null) val members = db.getMembersOfProject(projectId, null)
val allBills = db.getBillsOfProject(projectId) val allBills = db.getBillsOfProject(projectId)
val billsToLabel = allBills.filter { it.categoryRemoteId == 0 && it.state != DBBill.STATE_DELETED } val billsToLabel = allBills.filter { it.categoryId == 0L && it.state != DBBill.STATE_DELETED }
val allCategorized = allBills.filter { it.categoryRemoteId != 0 && it.state != DBBill.STATE_DELETED } val allCategorized = allBills.filter { it.categoryId != 0L && it.state != DBBill.STATE_DELETED }
val syncedCategories = db.getCategories(projectId) val syncedCategories = db.getCategories(projectId)
val defaultCategories = CategoryUtils.getDefaultCategories(this@LabelBillsActivity, projectId) val defaultCategories = CategoryUtils.getDefaultCategories(this@LabelBillsActivity, projectId)
val hardcoded = if (projectType == ProjectType.LOCAL) { val hardcoded = if (projectType == ProjectType.LOCAL) {
defaultCategories defaultCategories
} else { } else {
listOfNotNull(defaultCategories.find { it.remoteId.toInt() == DBBill.CATEGORY_REIMBURSEMENT }) listOfNotNull(defaultCategories.find { it.remoteId == DBBill.CATEGORY_REIMBURSEMENT.toLong() })
} }
val categories = syncedCategories + hardcoded val categories = syncedCategories + hardcoded
@@ -56,7 +56,7 @@ class LabelBillsActivity : AppCompatActivity() {
viewModel.billsToLabel = billsToLabel viewModel.billsToLabel = billsToLabel
viewModel.categories = categories viewModel.categories = categories
viewModel.categoriesMap = categories.associateBy { it.remoteId } viewModel.categoriesMap = categories.associateBy { it.id }
viewModel.allCategorizedBills = allCategorized viewModel.allCategorizedBills = allCategorized
viewModel.updateSuggestions() viewModel.updateSuggestions()
@@ -84,7 +84,7 @@ fun LabelBillsScreen(
CategoryButton( CategoryButton(
icon = category.icon, icon = category.icon,
name = category.name ?: "", name = category.name ?: "",
onClick = { viewModel.labelCurrentBill(db, category.remoteId.toInt()) } onClick = { viewModel.labelCurrentBill(db, category.id) }
) )
} }
} }
@@ -126,7 +126,7 @@ fun LabelBillsScreen(
CategoryButton( CategoryButton(
icon = category.icon, icon = category.icon,
name = category.name ?: "", name = category.name ?: "",
onClick = { viewModel.labelCurrentBill(db, category.remoteId.toInt()) } onClick = { viewModel.labelCurrentBill(db, category.id) }
) )
} }
} }
@@ -245,16 +245,16 @@ fun CategoryButton(icon: String, name: String, onClick: () -> Unit) {
fun LabelBillsScreenPreview() { fun LabelBillsScreenPreview() {
val viewModel = LabelBillsViewModel().apply { val viewModel = LabelBillsViewModel().apply {
billsToLabel = listOf( billsToLabel = listOf(
DBBill(1L, 0, 1L, 1L, 120.5, System.currentTimeMillis() / 1000, "Groceries at Aldi", 0, null, null, 0, null, -1) DBBill(1L, 0, 1L, 1L, 120.5, System.currentTimeMillis() / 1000, "Groceries at Aldi", 0, null, null, 0L, null, -1L)
) )
val cats = listOf( val cats = listOf(
DBCategory(1, 1, 1, "Groceries", "🛒", ""), DBCategory(-1, -1, 1, "Groceries", "🛒", ""),
DBCategory(2, 2, 1, "Leisure", "🥳", ""), DBCategory(-2, -2, 1, "Leisure", "🥳", ""),
DBCategory(3, 3, 1, "Rent", "🏠", ""), DBCategory(-3, -3, 1, "Rent", "🏠", ""),
DBCategory(4, 4, 1, "Bills", "💸", "") DBCategory(-4, -4, 1, "Bills", "💸", "")
) )
categories = cats categories = cats
categoriesMap = cats.associateBy { it.remoteId } categoriesMap = cats.associateBy { it.id }
} }
val members = listOf( val members = listOf(
DBMember(1L, 0, 1L, "Alice", true, 1.0, 0, 255, 100, 100, null, null), DBMember(1L, 0, 1L, "Alice", true, 1.0, 0, 255, 100, 100, null, null),
@@ -43,18 +43,18 @@ class LabelBillsViewModel : ViewModel() {
otherName == name || (name.length > 3 && otherName.contains(name)) || (otherName.length > 3 && name.contains(otherName)) otherName == name || (name.length > 3 && otherName.contains(name)) || (otherName.length > 3 && name.contains(otherName))
} }
val counts = matches.groupBy { it.categoryRemoteId } val counts = matches.groupBy { it.categoryId }
.mapValues { it.value.size } .mapValues { it.value.size }
.toList() .toList()
.sortedByDescending { it.second } .sortedByDescending { it.second }
.take(2) .take(2)
suggestedCategories = counts.mapNotNull { (catId, _) -> suggestedCategories = counts.mapNotNull { (catId, _) ->
categoriesMap[catId.toLong()] categoriesMap[catId]
} }
} }
fun labelCurrentBill(db: CowspentSQLiteOpenHelper, categoryId: Int) { fun labelCurrentBill(db: CowspentSQLiteOpenHelper, categoryId: Long) {
currentBill?.let { bill -> currentBill?.let { bill ->
db.updateBillAndSync( db.updateBillAndSync(
bill = bill, bill = bill,
@@ -65,11 +65,11 @@ class LabelBillsViewModel : ViewModel() {
newOwersIds = bill.billOwersIds, newOwersIds = bill.billOwersIds,
newRepeat = bill.repeat, newRepeat = bill.repeat,
newPaymentMode = bill.paymentMode, newPaymentMode = bill.paymentMode,
newPaymentModeRemoteId = bill.paymentModeRemoteId, newPaymentModeId = bill.paymentModeId,
newCategoryId = categoryId, newCategoryId = categoryId,
newComment = bill.comment newComment = bill.comment
) )
bill.categoryRemoteId = categoryId bill.categoryId = categoryId
onBillProcessed?.invoke(bill.id) onBillProcessed?.invoke(bill.id)
moveToNext() moveToNext()
} }
@@ -87,11 +87,11 @@ class LabelBillsViewModel : ViewModel() {
val start = currentBillIndex val start = currentBillIndex
var next = (start + 1) % billsToLabel.size var next = (start + 1) % billsToLabel.size
while (next != start && billsToLabel[next].categoryRemoteId != 0) { while (next != start && billsToLabel[next].categoryId != 0L) {
next = (next + 1) % billsToLabel.size next = (next + 1) % billsToLabel.size
} }
currentBillIndex = if (billsToLabel[next].categoryRemoteId == 0) { currentBillIndex = if (billsToLabel[next].categoryId == 0L) {
next next
} else { } else {
billsToLabel.size billsToLabel.size
@@ -115,18 +115,8 @@ class BillsListViewActivity :
Log.d(TAG, "CREATED project id: $pid") Log.d(TAG, "CREATED project id: $pid")
lifecycleScope.launch { lifecycleScope.launch {
val addedProj = withContext(Dispatchers.IO) { db.getProject(pid) } val addedProj = withContext(Dispatchers.IO) { db.getProject(pid) }
val message: String val message = getString(R.string.msg_project_added, addedProj?.name?.ifEmpty { addedProj.remoteId } ?: pid.toString())
val title: String showToast(this@BillsListViewActivity, message)
if (created) {
Log.e(TAG, "CREATED !!!")
title = getString(R.string.msg_project_added)
message = getString(R.string.msg_project_added, addedProj?.remoteId)
} else {
Log.e(TAG, "ADDED !!!")
title = getString(R.string.msg_project_added)
message = getString(R.string.msg_project_added, addedProj?.remoteId)
}
showDialog(message, title, Icons.Default.AddCircleOutline)
} }
} }
} }
@@ -142,7 +132,7 @@ class BillsListViewActivity :
} }
if (!db.cowspentServerSyncHelper.isSyncPossible) { if (!db.cowspentServerSyncHelper.isSyncPossible) {
if (CowspentServerSyncHelper.isNextcloudAccountConfigured(applicationContext)) { if (CowspentServerSyncHelper.isNextcloudAccountConfigured(applicationContext)) {
Toast.makeText(applicationContext, getString(R.string.error_sync, getString(CospendClientUtil.LoginStatus.NO_NETWORK.str)), Toast.LENGTH_LONG).show() showToast(this@BillsListViewActivity, getString(R.string.error_sync, getString(CospendClientUtil.LoginStatus.NO_NETWORK.str)), Toast.LENGTH_LONG)
} }
} }
} }
@@ -636,13 +626,13 @@ class BillsListViewActivity :
mid == null || mid == it.payerId || it.billOwersIds.contains(mid) mid == null || mid == it.payerId || it.billOwersIds.contains(mid)
} }
viewModel.hasUnlabeledBills = bills.any { it.categoryRemoteId == 0 && it.state != DBBill.STATE_DELETED } viewModel.hasUnlabeledBills = bills.any { it.categoryId == 0L && it.state != DBBill.STATE_DELETED }
val projectMembers = db.getMembersOfProject(projId, null) val projectMembers = db.getMembersOfProject(projId, null)
val memberMap = projectMembers.associateBy { it.id } val memberMap = projectMembers.associateBy { it.id }
val projectPaymentModes = db.getPaymentModes(projId).associateBy { it.remoteId } val projectPaymentModes = db.getPaymentModes(projId).associateBy { it.id }
val projectCategories = db.getCategories(projId).associateBy { it.remoteId } val projectCategories = db.getCategories(projId).associateBy { it.id }
BillFormatter.formatBills( BillFormatter.formatBills(
bills, bills,
@@ -118,8 +118,8 @@ object ProjectImportHelper {
val payerWeight = if (columns.containsKey("payer_weight")) line[columns["payer_weight"]!!].toDouble() else 1.0 val payerWeight = if (columns.containsKey("payer_weight")) line[columns["payer_weight"]!!].toDouble() else 1.0
val owersStr = if (columns.containsKey("owers")) line[columns["owers"]!!] else "" val owersStr = if (columns.containsKey("owers")) line[columns["owers"]!!] else ""
val payerActive = columns.containsKey("payer_active") && line[columns["payer_active"]!!] == "1" val payerActive = columns.containsKey("payer_active") && line[columns["payer_active"]!!] == "1"
val catId = if (columns.containsKey("categoryid") && line[columns["categoryid"]!!].isNotEmpty()) line[columns["categoryid"]!!].toInt() else 0 val catId = if (columns.containsKey("categoryid") && line[columns["categoryid"]!!].isNotEmpty()) line[columns["categoryid"]!!].toLong() else 0L
val pmId = if (columns.containsKey("paymentmodeid") && line[columns["paymentmodeid"]!!].isNotEmpty()) line[columns["paymentmodeid"]!!].toInt() else 0 val pmId = if (columns.containsKey("paymentmodeid") && line[columns["paymentmodeid"]!!].isNotEmpty()) line[columns["paymentmodeid"]!!].toLong() else 0L
val pm = if (columns.containsKey("paymentmode")) line[columns["paymentmode"]!!] else null val pm = if (columns.containsKey("paymentmode")) line[columns["paymentmode"]!!] else null
membersActive[payerName] = payerActive membersActive[payerName] = payerActive
@@ -150,8 +150,16 @@ object ProjectImportHelper {
val memberNameToId = mutableMapOf<String, Long>() val memberNameToId = mutableMapOf<String, Long>()
val pid = db.addProject(DBProject(0, projectRemoteId, "", projectRemoteId, null, null, null, ProjectType.LOCAL, 0L, mainCurrencyName, false, DBProject.ACCESS_LEVEL_UNKNOWN, null)) val pid = db.addProject(DBProject(0, projectRemoteId, "", projectRemoteId, null, null, null, ProjectType.LOCAL, 0L, mainCurrencyName, false, DBProject.ACCESS_LEVEL_UNKNOWN, null))
paymentModes.forEach { db.addPaymentMode(DBPaymentMode(0, it.remoteId, pid, it.name, it.icon, it.color)) } val pmRemoteToLocal = mutableMapOf<Long, Long>()
categories.forEach { db.addCategory(DBCategory(0, it.remoteId, pid, it.name, it.icon, it.color)) } paymentModes.forEach { pm ->
val localId = db.addPaymentMode(DBPaymentMode(0, pm.remoteId, pid, pm.name, pm.icon, pm.color))
pmRemoteToLocal[pm.remoteId] = localId
}
val catRemoteToLocal = mutableMapOf<Long, Long>()
categories.forEach { cat ->
val localId = db.addCategory(DBCategory(0, cat.remoteId, pid, cat.name, cat.icon, cat.color))
catRemoteToLocal[cat.remoteId] = localId
}
currencies.forEach { db.addCurrency(DBCurrency(0, 0, pid, it.name, it.exchangeRate, DBBill.STATE_OK)) } currencies.forEach { db.addCurrency(DBCurrency(0, 0, pid, it.name, it.exchangeRate, DBBill.STATE_OK)) }
membersWeight.keys.forEach { mName -> membersWeight.keys.forEach { mName ->
@@ -160,7 +168,9 @@ object ProjectImportHelper {
bills.forEach { b -> bills.forEach { b ->
val payerId = memberNameToId[billRemoteIdToPayerName[b.remoteId]] ?: 0L val payerId = memberNameToId[billRemoteIdToPayerName[b.remoteId]] ?: 0L
val billId = db.addBill(DBBill(0, 0, pid, payerId, b.amount, b.timestamp, b.what, DBBill.STATE_OK, b.repeat, b.paymentMode, b.categoryRemoteId, b.comment, b.paymentModeRemoteId)) val localCatId = catRemoteToLocal[b.categoryId] ?: b.categoryId
val localPmId = pmRemoteToLocal[b.paymentModeId] ?: b.paymentModeId
val billId = db.addBill(DBBill(0, 0, pid, payerId, b.amount, b.timestamp, b.what, DBBill.STATE_OK, b.repeat, b.paymentMode, localCatId, b.comment, localPmId))
billRemoteIdToOwerStr[b.remoteId]?.split(", ")?.filter { it.isNotEmpty() }?.forEach { ower -> billRemoteIdToOwerStr[b.remoteId]?.split(", ")?.filter { it.isNotEmpty() }?.forEach { ower ->
memberNameToId[ower.trim()]?.let { owerId -> db.addBillower(billId, owerId) } memberNameToId[ower.trim()]?.let { owerId -> db.addBillower(billId, owerId) }
} }
@@ -97,11 +97,11 @@ fun ProjectSankeyDiagram(
var expanded by remember { mutableStateOf(false) } var expanded by remember { mutableStateOf(false) }
val activeBills = remember(allBills) { val activeBills = remember(allBills) {
allBills.filter { it.state != DBBill.STATE_DELETED && it.categoryRemoteId != DBBill.CATEGORY_REIMBURSEMENT } allBills.filter { it.state != DBBill.STATE_DELETED && it.categoryId != DBBill.CATEGORY_REIMBURSEMENT.toLong() }
} }
val membersMap = remember(allMembers) { allMembers.associateBy { it.id } } val membersMap = remember(allMembers) { allMembers.associateBy { it.id } }
val categoriesMap = remember(customCategories) { customCategories.associateBy { it.remoteId.toInt() } } val categoriesMap = remember(customCategories) { customCategories.associateBy { it.id } }
if (activeBills.isEmpty()) { if (activeBills.isEmpty()) {
Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) { Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
@@ -112,13 +112,13 @@ fun ProjectSankeyDiagram(
val spendings = remember(activeBills, selectedMemberId, membersMap) { val spendings = remember(activeBills, selectedMemberId, membersMap) {
val spentMap = mutableMapOf<Long, Double>().apply { membersMap.keys.forEach { put(it, 0.0) } } val spentMap = mutableMapOf<Long, Double>().apply { membersMap.keys.forEach { put(it, 0.0) } }
val catMap = mutableMapOf<Int, Double>() val catMap = mutableMapOf<Long, Double>()
activeBills.forEach { bill -> activeBills.forEach { bill ->
val totalWeight = bill.billOwers.sumOf { membersMap[it.memberId]?.weight ?: 1.0 } val totalWeight = bill.billOwers.sumOf { membersMap[it.memberId]?.weight ?: 1.0 }
if (totalWeight > 0) { if (totalWeight > 0) {
if (selectedMemberId == -1L) { if (selectedMemberId == -1L) {
catMap[bill.categoryRemoteId] = (catMap[bill.categoryRemoteId] ?: 0.0) + bill.amount catMap[bill.categoryId] = (catMap[bill.categoryId] ?: 0.0) + bill.amount
bill.billOwers.forEach { bo -> bill.billOwers.forEach { bo ->
val weight = membersMap[bo.memberId]?.weight ?: 1.0 val weight = membersMap[bo.memberId]?.weight ?: 1.0
spentMap[bo.memberId] = (spentMap[bo.memberId] ?: 0.0) + (bill.amount / totalWeight) * weight spentMap[bo.memberId] = (spentMap[bo.memberId] ?: 0.0) + (bill.amount / totalWeight) * weight
@@ -126,7 +126,7 @@ fun ProjectSankeyDiagram(
} else { } else {
bill.billOwers.find { it.memberId == selectedMemberId }?.let { bo -> bill.billOwers.find { it.memberId == selectedMemberId }?.let { bo ->
val weight = membersMap[bo.memberId]?.weight ?: 1.0 val weight = membersMap[bo.memberId]?.weight ?: 1.0
catMap[bill.categoryRemoteId] = (catMap[bill.categoryRemoteId] ?: 0.0) + (bill.amount / totalWeight) * weight catMap[bill.categoryId] = (catMap[bill.categoryId] ?: 0.0) + (bill.amount / totalWeight) * weight
spentMap[selectedMemberId] = (spentMap[selectedMemberId] ?: 0.0) + (bill.amount / totalWeight) * weight spentMap[selectedMemberId] = (spentMap[selectedMemberId] ?: 0.0) + (bill.amount / totalWeight) * weight
} }
} }
@@ -215,9 +215,9 @@ private fun SankeyContent(
selectedMemberId: Long, selectedMemberId: Long,
totalAmount: Double, totalAmount: Double,
displayMemberSpendings: List<Pair<Long, Double>>, displayMemberSpendings: List<Pair<Long, Double>>,
displayCategorySpendings: List<Pair<Int, Double>>, displayCategorySpendings: List<Pair<Long, Double>>,
membersMap: Map<Long, DBMember>, membersMap: Map<Long, DBMember>,
categoriesMap: Map<Int, DBCategory> categoriesMap: Map<Long, DBCategory>
) { ) {
val density = LocalDensity.current val density = LocalDensity.current
val scope = rememberCoroutineScope() val scope = rememberCoroutineScope()
@@ -99,7 +99,7 @@ fun ProjectStatisticsScreen(
val hardcoded = if (proj.type == ProjectType.LOCAL) { val hardcoded = if (proj.type == ProjectType.LOCAL) {
defaultCategories defaultCategories
} else { } else {
listOfNotNull(defaultCategories.find { it.remoteId.toInt() == DBBill.CATEGORY_REIMBURSEMENT }) listOfNotNull(defaultCategories.find { it.remoteId == DBBill.CATEGORY_REIMBURSEMENT })
} }
(data.categories + hardcoded).distinctBy { it.remoteId } (data.categories + hardcoded).distinctBy { it.remoteId }
} }
@@ -16,9 +16,9 @@ open class DBBill(
var state: Int, var state: Int,
var repeat: String?, var repeat: String?,
var paymentMode: String?, var paymentMode: String?,
var categoryRemoteId: Int, var categoryId: Long,
var comment: String?, var comment: String?,
var paymentModeRemoteId: Int var paymentModeId: Long
) : Item, Serializable { ) : Item, Serializable {
var formattedWhat: String = "" var formattedWhat: String = ""
@@ -39,7 +39,6 @@ open class DBBill(
get() { get() {
val cal = Calendar.getInstance() val cal = Calendar.getInstance()
cal.timeInMillis = timestamp * 1000 cal.timeInMillis = timestamp * 1000
Log.v("ll", "[$what] get date ts $timestamp year ${cal[Calendar.YEAR]}")
val month = cal[Calendar.MONTH] + 1 val month = cal[Calendar.MONTH] + 1
val day = cal[Calendar.DAY_OF_MONTH] val day = cal[Calendar.DAY_OF_MONTH]
return "${cal[Calendar.YEAR]}-${String.format(Locale.ROOT, "%02d", month)}-${ return "${cal[Calendar.YEAR]}-${String.format(Locale.ROOT, "%02d", month)}-${
@@ -58,7 +57,7 @@ open class DBBill(
override fun toString(): String { override fun toString(): String {
return "#DBBill$id/$remoteId,$projectId, $payerId, $amount, $timestamp, $what, $state, $repeat, $paymentMode, $categoryRemoteId" return "#DBBill$id/$remoteId,$projectId, $payerId, $amount, $timestamp, $what, $state, $repeat, $paymentMode, $categoryId"
} }
override fun isSection(): Boolean { override fun isSection(): Boolean {
@@ -73,15 +72,15 @@ open class DBBill(
const val PAYMODE_TRANSFER = "t" const val PAYMODE_TRANSFER = "t"
const val PAYMODE_ONLINE_SERVICE = "o" const val PAYMODE_ONLINE_SERVICE = "o"
const val PAYMODE_ID_NONE = 0 const val PAYMODE_ID_NONE = 0L
const val PAYMODE_ID_CARD = -1 const val PAYMODE_ID_CARD = -1L
const val PAYMODE_ID_CASH = -2 const val PAYMODE_ID_CASH = -2L
const val PAYMODE_ID_CHECK = -3 const val PAYMODE_ID_CHECK = -3L
const val PAYMODE_ID_TRANSFER = -4 const val PAYMODE_ID_TRANSFER = -4L
const val PAYMODE_ID_ONLINE_SERVICE = -5 const val PAYMODE_ID_ONLINE_SERVICE = -5L
@JvmField @JvmField
val oldPmIdToNew: Map<String, Int> = object : HashMap<String, Int>() { val oldPmIdToNew: Map<String, Long> = object : HashMap<String, Long>() {
init { init {
put(PAYMODE_NONE, PAYMODE_ID_NONE) put(PAYMODE_NONE, PAYMODE_ID_NONE)
put(PAYMODE_CARD, PAYMODE_ID_CARD) put(PAYMODE_CARD, PAYMODE_ID_CARD)
@@ -92,19 +91,19 @@ open class DBBill(
} }
} }
const val CATEGORY_NONE = 0 const val CATEGORY_NONE = 0L
const val CATEGORY_GROCERIES = -1 const val CATEGORY_GROCERIES = -1L
const val CATEGORY_LEISURE = -2 const val CATEGORY_LEISURE = -2L
const val CATEGORY_RENT = -3 const val CATEGORY_RENT = -3L
const val CATEGORY_BILLS = -4 const val CATEGORY_BILLS = -4L
const val CATEGORY_CULTURE = -5 const val CATEGORY_CULTURE = -5L
const val CATEGORY_HEALTH = -6 const val CATEGORY_HEALTH = -6L
const val CATEGORY_SHOPPING = -10 const val CATEGORY_SHOPPING = -10L
const val CATEGORY_REIMBURSEMENT = -11 const val CATEGORY_REIMBURSEMENT = -11L
const val CATEGORY_RESTAURANT = -12 const val CATEGORY_RESTAURANT = -12L
const val CATEGORY_ACCOMMODATION = -13 const val CATEGORY_ACCOMMODATION = -13L
const val CATEGORY_TRANSPORT = -14 const val CATEGORY_TRANSPORT = -14L
const val CATEGORY_SPORT = -15 const val CATEGORY_SPORT = -15L
const val STATE_OK = 0 const val STATE_OK = 0
const val STATE_ADDED = 1 const val STATE_ADDED = 1
@@ -15,9 +15,9 @@ class GroupedBill(
sourceBills.first().state, sourceBills.first().state,
sourceBills.first().repeat, sourceBills.first().repeat,
sourceBills.first().paymentMode, sourceBills.first().paymentMode,
sourceBills.first().categoryRemoteId, sourceBills.first().categoryId,
sourceBills.first().comment, sourceBills.first().comment,
sourceBills.first().paymentModeRemoteId sourceBills.first().paymentModeId
), Serializable { ), Serializable {
init { init {
this.formattedWhat = sourceBills.first().formattedWhat this.formattedWhat = sourceBills.first().formattedWhat
@@ -264,6 +264,10 @@ class CowspentServerSyncHelper private constructor(private val dbHelper: Cowspen
val members = dbHelper.getMembersOfProject(project.id, null) val members = dbHelper.getMembersOfProject(project.id, null)
val memberIdToRemoteId = members.associate { it.id to it.remoteId } val memberIdToRemoteId = members.associate { it.id to it.remoteId }
val categories = dbHelper.getCategories(project.id)
val categoryIdToRemoteId = categories.associate { it.id to it.remoteId }
val paymentModes = dbHelper.getPaymentModes(project.id)
val paymentModeIdToRemoteId = paymentModes.associate { it.id to it.remoteId }
val toDelete = dbHelper.getBillsOfProjectWithState(project.id, DBBill.STATE_DELETED) val toDelete = dbHelper.getBillsOfProjectWithState(project.id, DBBill.STATE_DELETED)
for (bToDel in toDelete) { for (bToDel in toDelete) {
@@ -293,7 +297,7 @@ class CowspentServerSyncHelper private constructor(private val dbHelper: Cowspen
val toEdit = dbHelper.getBillsOfProjectWithState(project.id, DBBill.STATE_EDITED) val toEdit = dbHelper.getBillsOfProjectWithState(project.id, DBBill.STATE_EDITED)
for (bToEdit in toEdit) { for (bToEdit in toEdit) {
try { try {
val editRemoteBillResponse = client!!.editRemoteBill(project, bToEdit, memberIdToRemoteId) val editRemoteBillResponse = client!!.editRemoteBill(project, bToEdit, memberIdToRemoteId, categoryIdToRemoteId, paymentModeIdToRemoteId)
if (editRemoteBillResponse.stringContent == bToEdit.remoteId.toString()) { if (editRemoteBillResponse.stringContent == bToEdit.remoteId.toString()) {
dbHelper.setBillState(bToEdit.id, DBBill.STATE_OK) dbHelper.setBillState(bToEdit.id, DBBill.STATE_OK)
Log.d(TAG, "SUCCESSFUL remote bill edition (${editRemoteBillResponse.stringContent})") Log.d(TAG, "SUCCESSFUL remote bill edition (${editRemoteBillResponse.stringContent})")
@@ -307,7 +311,7 @@ class CowspentServerSyncHelper private constructor(private val dbHelper: Cowspen
val toAdd = dbHelper.getBillsOfProjectWithState(project.id, DBBill.STATE_ADDED) val toAdd = dbHelper.getBillsOfProjectWithState(project.id, DBBill.STATE_ADDED)
for (bToAdd in toAdd) { for (bToAdd in toAdd) {
val createRemoteBillResponse = client!!.createRemoteBill(project, bToAdd, memberIdToRemoteId) val createRemoteBillResponse = client!!.createRemoteBill(project, bToAdd, memberIdToRemoteId, categoryIdToRemoteId, paymentModeIdToRemoteId)
val newRemoteId = createRemoteBillResponse.stringContent.toLong() val newRemoteId = createRemoteBillResponse.stringContent.toLong()
if (newRemoteId > 0) { if (newRemoteId > 0) {
dbHelper.updateBill( dbHelper.updateBill(
@@ -613,7 +617,15 @@ class CowspentServerSyncHelper private constructor(private val dbHelper: Cowspen
val localBills = dbHelper.getBillsOfProject(project.id) val localBills = dbHelper.getBillsOfProject(project.id)
val localBillsByRemoteId = localBills.associateBy { it.remoteId } val localBillsByRemoteId = localBills.associateBy { it.remoteId }
val syncedCategories = dbHelper.getCategories(project.id)
val catRemoteToLocal = syncedCategories.associate { it.remoteId to it.id }
val syncedPaymentModes = dbHelper.getPaymentModes(project.id)
val pmRemoteToLocal = syncedPaymentModes.associate { it.remoteId to it.id }
for (remoteBill in remoteBills) { for (remoteBill in remoteBills) {
if (remoteBill.categoryId > 0) remoteBill.categoryId = catRemoteToLocal[remoteBill.categoryId] ?: 0
if (remoteBill.paymentModeId > 0) remoteBill.paymentModeId = pmRemoteToLocal[remoteBill.paymentModeId] ?: 0
if (!localBillsByRemoteId.containsKey(remoteBill.remoteId)) { if (!localBillsByRemoteId.containsKey(remoteBill.remoteId)) {
dbHelper.addBill(remoteBill) dbHelper.addBill(remoteBill)
nbPulledNewBills++ nbPulledNewBills++
@@ -626,8 +638,8 @@ class CowspentServerSyncHelper private constructor(private val dbHelper: Cowspen
localBill.id, null, remoteBill.payerId, localBill.id, null, remoteBill.payerId,
remoteBill.amount, remoteBill.timestamp, remoteBill.amount, remoteBill.timestamp,
remoteBill.what, DBBill.STATE_OK, remoteBill.repeat, remoteBill.what, DBBill.STATE_OK, remoteBill.repeat,
remoteBill.paymentMode, remoteBill.paymentModeRemoteId, remoteBill.paymentMode, remoteBill.paymentModeId,
remoteBill.categoryRemoteId, remoteBill.comment remoteBill.categoryId, remoteBill.comment
) )
nbPulledUpdatedBills++ nbPulledUpdatedBills++
updatedBillsDialogText += "${remoteBill.what}\n" updatedBillsDialogText += "${remoteBill.what}\n"
@@ -1084,8 +1096,8 @@ class CowspentServerSyncHelper private constructor(private val dbHelper: Cowspen
localBill.what == remoteBill.what && localBill.what == remoteBill.what &&
localBill.comment == remoteBill.comment && localBill.comment == remoteBill.comment &&
localBill.paymentMode == remoteBill.paymentMode && localBill.paymentMode == remoteBill.paymentMode &&
localBill.paymentModeRemoteId == remoteBill.paymentModeRemoteId && localBill.paymentModeId == remoteBill.paymentModeId &&
localBill.categoryRemoteId == remoteBill.categoryRemoteId localBill.categoryId == remoteBill.categoryId
) { ) {
val localRepeat = localBill.repeat ?: DBBill.NON_REPEATED val localRepeat = localBill.repeat ?: DBBill.NON_REPEATED
val remoteRepeat = remoteBill.repeat ?: DBBill.NON_REPEATED val remoteRepeat = remoteBill.repeat ?: DBBill.NON_REPEATED
@@ -14,11 +14,11 @@ object BillFormatter {
) { ) {
for (bill in bills) { for (bill in bills) {
var whatPrefix = "" var whatPrefix = ""
val pm = paymentModesMap[bill.paymentModeRemoteId.toLong()] val pm = paymentModesMap[bill.paymentModeId]
if (pm != null) { if (pm != null) {
whatPrefix += pm.icon + " " whatPrefix += pm.icon + " "
} else { } else {
when (bill.paymentModeRemoteId) { when (bill.paymentModeId) {
DBBill.PAYMODE_ID_CARD -> whatPrefix += "\uD83D\uDCB3 " DBBill.PAYMODE_ID_CARD -> whatPrefix += "\uD83D\uDCB3 "
DBBill.PAYMODE_ID_CASH -> whatPrefix += "\uD83D\uDCB5 " DBBill.PAYMODE_ID_CASH -> whatPrefix += "\uD83D\uDCB5 "
DBBill.PAYMODE_ID_CHECK -> whatPrefix += "\uD83C\uDFAB " DBBill.PAYMODE_ID_CHECK -> whatPrefix += "\uD83C\uDFAB "
@@ -27,11 +27,11 @@ object BillFormatter {
} }
} }
val cat = categoriesMap[bill.categoryRemoteId.toLong()] val cat = categoriesMap[bill.categoryId]
if (cat != null) { if (cat != null) {
whatPrefix += cat.icon + " " whatPrefix += cat.icon + " "
} else { } else {
when (bill.categoryRemoteId) { when (bill.categoryId) {
DBBill.CATEGORY_GROCERIES -> whatPrefix += "\uD83D\uDED2 " DBBill.CATEGORY_GROCERIES -> whatPrefix += "\uD83D\uDED2 "
DBBill.CATEGORY_LEISURE -> whatPrefix += "\uD83C\uDF89 " DBBill.CATEGORY_LEISURE -> whatPrefix += "\uD83C\uDF89 "
DBBill.CATEGORY_RENT -> whatPrefix += "\uD83C\uDFE0 " DBBill.CATEGORY_RENT -> whatPrefix += "\uD83C\uDFE0 "
@@ -10,28 +10,40 @@ object CategoryUtils {
fun getDefaultCategories(context: Context, projectId: Long): List<DBCategory> { fun getDefaultCategories(context: Context, projectId: Long): List<DBCategory> {
return listOf( return listOf(
DBCategory(0, DBBill.CATEGORY_GROCERIES.toLong(), projectId, context.getString(R.string.category_groceries), "\uD83D\uDED2", "#ffaa00"), DBCategory(DBBill.CATEGORY_GROCERIES,
DBCategory(0, DBBill.CATEGORY_LEISURE.toLong(), projectId, context.getString(R.string.category_leisure), "\uD83C\uDF89", "#aa55ff"), DBBill.CATEGORY_GROCERIES, projectId, context.getString(R.string.category_groceries), "\uD83D\uDED2", "#ffaa00"),
DBCategory(0, DBBill.CATEGORY_RENT.toLong(), projectId, context.getString(R.string.category_rent), "\uD83C\uDFE0", "#da8733"), DBCategory(DBBill.CATEGORY_LEISURE,
DBCategory(0, DBBill.CATEGORY_BILLS.toLong(), projectId, context.getString(R.string.category_bills), "\uD83C\uDF29", "#4aa6b0"), DBBill.CATEGORY_LEISURE, projectId, context.getString(R.string.category_leisure), "\uD83C\uDF89", "#aa55ff"),
DBCategory(0, DBBill.CATEGORY_CULTURE.toLong(), projectId, context.getString(R.string.category_excursion), "\uD83D\uDEB8", "#0055ff"), DBCategory(DBBill.CATEGORY_RENT,
DBCategory(0, DBBill.CATEGORY_HEALTH.toLong(), projectId, context.getString(R.string.category_health), "\uD83D\uDC9A", "#bf090c"), DBBill.CATEGORY_RENT, projectId, context.getString(R.string.category_rent), "\uD83C\uDFE0", "#da8733"),
DBCategory(0, DBBill.CATEGORY_SHOPPING.toLong(), projectId, context.getString(R.string.category_shopping), "\uD83D\uDECD", "#e167d1"), DBCategory(DBBill.CATEGORY_BILLS,
DBCategory(0, DBBill.CATEGORY_REIMBURSEMENT.toLong(), projectId, context.getString(R.string.category_reimbursement), "\uD83D\uDCB0", "#00ced1"), DBBill.CATEGORY_BILLS, projectId, context.getString(R.string.category_bills), "\uD83C\uDF29", "#4aa6b0"),
DBCategory(0, DBBill.CATEGORY_RESTAURANT.toLong(), projectId, context.getString(R.string.category_restaurant), "\uD83C\uDF74", "#d0d5e1"), DBCategory(DBBill.CATEGORY_CULTURE,
DBCategory(0, DBBill.CATEGORY_ACCOMMODATION.toLong(), projectId, context.getString(R.string.category_accomodation), "\uD83D\uDECC", "#5de1a3"), DBBill.CATEGORY_CULTURE, projectId, context.getString(R.string.category_excursion), "\uD83D\uDEB8", "#0055ff"),
DBCategory(0, DBBill.CATEGORY_TRANSPORT.toLong(), projectId, context.getString(R.string.category_transport), "\uD83D\uDE8C", "#6f2ee1"), DBCategory(DBBill.CATEGORY_HEALTH,
DBCategory(0, DBBill.CATEGORY_SPORT.toLong(), projectId, context.getString(R.string.category_sport), "\uD83C\uDFBE", "#69e177") DBBill.CATEGORY_HEALTH, projectId, context.getString(R.string.category_health), "\uD83D\uDC9A", "#bf090c"),
DBCategory(DBBill.CATEGORY_SHOPPING,
DBBill.CATEGORY_SHOPPING, projectId, context.getString(R.string.category_shopping), "\uD83D\uDECD", "#e167d1"),
DBCategory(DBBill.CATEGORY_REIMBURSEMENT,
DBBill.CATEGORY_REIMBURSEMENT, projectId, context.getString(R.string.category_reimbursement), "\uD83D\uDCB0", "#00ced1"),
DBCategory(DBBill.CATEGORY_RESTAURANT,
DBBill.CATEGORY_RESTAURANT, projectId, context.getString(R.string.category_restaurant), "\uD83C\uDF74", "#d0d5e1"),
DBCategory(DBBill.CATEGORY_ACCOMMODATION,
DBBill.CATEGORY_ACCOMMODATION, projectId, context.getString(R.string.category_accomodation), "\uD83D\uDECC", "#5de1a3"),
DBCategory(DBBill.CATEGORY_TRANSPORT,
DBBill.CATEGORY_TRANSPORT, projectId, context.getString(R.string.category_transport), "\uD83D\uDE8C", "#6f2ee1"),
DBCategory(DBBill.CATEGORY_SPORT,
DBBill.CATEGORY_SPORT, projectId, context.getString(R.string.category_sport), "\uD83C\uDFBE", "#69e177")
) )
} }
fun getDefaultPaymentModes(context: Context, projectId: Long): List<DBPaymentMode> { fun getDefaultPaymentModes(context: Context, projectId: Long): List<DBPaymentMode> {
return listOf( return listOf(
DBPaymentMode(0, DBBill.PAYMODE_ID_CARD.toLong(), projectId, context.getString(R.string.payment_mode_credit_card), "\uD83D\uDCB3", "#ff7f50"), DBPaymentMode(DBBill.PAYMODE_ID_CARD, DBBill.PAYMODE_ID_CARD, projectId, context.getString(R.string.payment_mode_credit_card), "\uD83D\uDCB3", "#ff7f50"),
DBPaymentMode(0, DBBill.PAYMODE_ID_CASH.toLong(), projectId, context.getString(R.string.payment_mode_cash), "\uD83D\uDCB5", "#556b2f"), DBPaymentMode(DBBill.PAYMODE_ID_CASH, DBBill.PAYMODE_ID_CASH, projectId, context.getString(R.string.payment_mode_cash), "\uD83D\uDCB5", "#556b2f"),
DBPaymentMode(0, DBBill.PAYMODE_ID_CHECK.toLong(), projectId, context.getString(R.string.payment_mode_check), "\uD83C\uDFAB", "#a9a9a9"), DBPaymentMode(DBBill.PAYMODE_ID_CHECK, DBBill.PAYMODE_ID_CHECK, projectId, context.getString(R.string.payment_mode_check), "\uD83C\uDFAB", "#a9a9a9"),
DBPaymentMode(0, DBBill.PAYMODE_ID_TRANSFER.toLong(), projectId, context.getString(R.string.payment_mode_transfer), "", "#00ced1"), DBPaymentMode(DBBill.PAYMODE_ID_TRANSFER, DBBill.PAYMODE_ID_TRANSFER, projectId, context.getString(R.string.payment_mode_transfer), "", "#00ced1"),
DBPaymentMode(0, DBBill.PAYMODE_ID_ONLINE_SERVICE.toLong(), projectId, context.getString(R.string.payment_mode_online), "\uD83C\uDF0E", "#9932cc") DBPaymentMode(DBBill.PAYMODE_ID_ONLINE_SERVICE, DBBill.PAYMODE_ID_ONLINE_SERVICE, projectId, context.getString(R.string.payment_mode_online), "\uD83C\uDF0E", "#9932cc")
) )
} }
} }
@@ -48,7 +48,7 @@ object ExportUtil {
} }
owersTxt = owersTxt.replace(",$".toRegex(), "") owersTxt = owersTxt.replace(",$".toRegex(), "")
fileContent += "\"${b.what}\",${b.amount},${b.date},${b.timestamp},\"$payerName\"," + fileContent += "\"${b.what}\",${b.amount},${b.date},${b.timestamp},\"$payerName\"," +
"$payerWeight,$payerActive,\"$owersTxt\",${b.repeat},${b.categoryRemoteId}," + "$payerWeight,$payerActive,\"$owersTxt\",${b.repeat},${b.categoryId}," +
"${b.paymentMode}\n" "${b.paymentMode}\n"
} }
@@ -599,8 +599,8 @@ open class ServerResponse(
var comment = "" var comment = ""
var repeat = DBBill.NON_REPEATED var repeat = DBBill.NON_REPEATED
var paymentMode = DBBill.PAYMODE_NONE var paymentMode = DBBill.PAYMODE_NONE
var paymentModeRemoteId = DBBill.PAYMODE_ID_NONE var paymentModeId = DBBill.PAYMODE_ID_NONE.toLong()
var categoryId = DBBill.CATEGORY_NONE var categoryId = DBBill.CATEGORY_NONE.toLong()
if (!json.isNull("id")) { if (!json.isNull("id")) {
remoteId = json.getLong("id") remoteId = json.getLong("id")
} }
@@ -641,21 +641,21 @@ open class ServerResponse(
paymentMode = json.getString("paymentmode") paymentMode = json.getString("paymentmode")
} }
if (json.has("categoryid") && !json.isNull("categoryid")) { if (json.has("categoryid") && !json.isNull("categoryid")) {
categoryId = json.getInt("categoryid") categoryId = json.getLong("categoryid")
Log.d("PLOP", "LOADED CATTTTTTTTTTTT $categoryId") Log.d("PLOP", "LOADED CATTTTTTTTTTTT $categoryId")
} }
if (json.has("paymentmodeid") && !json.isNull("paymentmodeid")) { if (json.has("paymentmodeid") && !json.isNull("paymentmodeid")) {
paymentModeRemoteId = json.getInt("paymentmodeid") paymentModeId = json.getLong("paymentmodeid")
} }
// old MB, new Cospend is ok as Cospend provides the old pm ID // old MB, new Cospend is ok as Cospend provides the old pm ID
// new MB, old Cospend => set payment mode ID from old one // new MB, old Cospend => set payment mode ID from old one
if (DBBill.PAYMODE_NONE != paymentMode && "" != paymentMode && paymentModeRemoteId == DBBill.PAYMODE_ID_NONE) { if (DBBill.PAYMODE_NONE != paymentMode && "" != paymentMode && paymentModeId == DBBill.PAYMODE_ID_NONE.toLong()) {
Log.d("PaymentMode", "old: $paymentMode and new: 0") Log.d("PaymentMode", "old: $paymentMode and new: 0")
paymentModeRemoteId = DBBill.oldPmIdToNew[paymentMode] ?: DBBill.PAYMODE_ID_NONE paymentModeId = (DBBill.oldPmIdToNew[paymentMode] ?: DBBill.PAYMODE_ID_NONE).toLong()
} }
val bill = DBBill( val bill = DBBill(
0, remoteId, projId, payerId, amount, timestamp, what, 0, remoteId, projId, payerId, amount, timestamp, what,
DBBill.STATE_OK, repeat, paymentMode, categoryId, comment, paymentModeRemoteId DBBill.STATE_OK, repeat, paymentMode, categoryId, comment, paymentModeId
) )
bill.billOwers = getBillOwersFromJson(json, memberRemoteIdToId) bill.billOwers = getBillOwersFromJson(json, memberRemoteIdToId)
return bill return bill
@@ -124,9 +124,9 @@ object SupportUtil {
for (b in dbBills) { for (b in dbBills) {
// don't take deleted bills and respect category filter // don't take deleted bills and respect category filter
if (b.state != DBBill.STATE_DELETED && if (b.state != DBBill.STATE_DELETED &&
((catId == -1000 || catId == -100 || b.categoryRemoteId == catId) && ((catId == -1000 || catId == -100 || b.categoryId == catId.toLong()) &&
(catId != -100 || b.categoryRemoteId != DBBill.CATEGORY_REIMBURSEMENT) && (catId != -100 || b.categoryId != DBBill.CATEGORY_REIMBURSEMENT.toLong()) &&
(paymentModeId == -1000 || b.paymentModeRemoteId == paymentModeId)) && (paymentModeId == -1000 || b.paymentModeId == paymentModeId.toLong())) &&
(dateMin == null || b.date >= dateMin) && (dateMin == null || b.date >= dateMin) &&
(dateMax == null || b.date <= dateMax) (dateMax == null || b.date <= dateMax)
) { ) {
@@ -260,7 +260,13 @@ class VersatileProjectSyncClient(
} }
@Throws(IOException::class, TokenMismatchException::class, NextcloudHttpRequestFailedException::class) @Throws(IOException::class, TokenMismatchException::class, NextcloudHttpRequestFailedException::class)
fun editRemoteBill(project: DBProject, bill: DBBill, memberIdToRemoteId: Map<Long, Long>): ServerResponse.EditRemoteBillResponse { fun editRemoteBill(
project: DBProject,
bill: DBBill,
memberIdToRemoteId: Map<Long, Long>,
categoryIdToRemoteId: Map<Long, Long>,
paymentModeIdToRemoteId: Map<Long, Long>
): ServerResponse.EditRemoteBillResponse {
val paramKeys: MutableList<String> = ArrayList() val paramKeys: MutableList<String> = ArrayList()
val paramValues: MutableList<String> = ArrayList() val paramValues: MutableList<String> = ArrayList()
paramKeys.add("date") paramKeys.add("date")
@@ -307,8 +313,10 @@ class VersatileProjectSyncClient(
payedFor = payedFor.replace(",$".toRegex(), "") payedFor = payedFor.replace(",$".toRegex(), "")
paramValues.add(payedFor) paramValues.add(payedFor)
paramValues.add(bill.paymentMode ?: "") paramValues.add(bill.paymentMode ?: "")
paramValues.add(bill.categoryRemoteId.toString()) val remoteCatId = if (bill.categoryId <= 0) bill.categoryId else categoryIdToRemoteId[bill.categoryId] ?: 0
paramValues.add(bill.paymentModeRemoteId.toString()) val remotePmId = if (bill.paymentModeId <= 0) bill.paymentModeId else paymentModeIdToRemoteId[bill.paymentModeId] ?: 0
paramValues.add(remoteCatId.toString())
paramValues.add(remotePmId.toString())
if (canAccessProjectWithNCLogin(project)) { if (canAccessProjectWithNCLogin(project)) {
username = this.username username = this.username
@@ -512,7 +520,13 @@ class VersatileProjectSyncClient(
} }
@Throws(IOException::class, TokenMismatchException::class, NextcloudHttpRequestFailedException::class) @Throws(IOException::class, TokenMismatchException::class, NextcloudHttpRequestFailedException::class)
fun createRemoteBill(project: DBProject, bill: DBBill, memberIdToRemoteId: Map<Long, Long>): ServerResponse.CreateRemoteBillResponse { fun createRemoteBill(
project: DBProject,
bill: DBBill,
memberIdToRemoteId: Map<Long, Long>,
categoryIdToRemoteId: Map<Long, Long>,
paymentModeIdToRemoteId: Map<Long, Long>
): ServerResponse.CreateRemoteBillResponse {
val paramKeys: MutableList<String> = ArrayList() val paramKeys: MutableList<String> = ArrayList()
val paramValues: MutableList<String> = ArrayList() val paramValues: MutableList<String> = ArrayList()
paramKeys.add("date") paramKeys.add("date")
@@ -554,8 +568,10 @@ class VersatileProjectSyncClient(
payedFor = payedFor.replace(",$".toRegex(), "") payedFor = payedFor.replace(",$".toRegex(), "")
paramValues.add(payedFor) paramValues.add(payedFor)
paramValues.add(bill.paymentMode ?: "") paramValues.add(bill.paymentMode ?: "")
paramValues.add(bill.categoryRemoteId.toString()) val remoteCatId = if (bill.categoryId <= 0) bill.categoryId else categoryIdToRemoteId[bill.categoryId] ?: 0
paramValues.add(bill.paymentModeRemoteId.toString()) val remotePmId = if (bill.paymentModeId <= 0) bill.paymentModeId else paymentModeIdToRemoteId[bill.paymentModeId] ?: 0
paramValues.add(remoteCatId.toString())
paramValues.add(remotePmId.toString())
if (canAccessProjectWithNCLogin(project)) { if (canAccessProjectWithNCLogin(project)) {
username = this.username username = this.username