julien-nc / cospend-nc

💰 💲hared budget manager Nextcloud app

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Splitwise Import wrong total amount

besar22 opened this issue · comments

Hello,

Great project. Amazing work.

I have an issue with a Splitwise project. Lets review an example for the rent split with 3 people. Rent is 1680 and split is even.
Splitwise export:

Date Description Category Cost Currency Person A Person B Person C
2023-09-01 Rent Rent 1680 EUR -560 -560 1120

This line gets imported as a total of 1120 from Person C to Person A & B.
I expect this line to be imported as 1680 paid from Person C to all.

How do you cover this case? Did Splitwise change the export format? What can I do to correct this case?

Looking forward to your thoughts.

This is because the Cospend does not take into account the splits (let's say 1/3), instead it uses the raw values.

So splitting woulnd't be 1680 between 3 people, but rather you paid 1120 and receives back the raw amounts of 560 and 560.

You can see the the to whom always excludes the person that was paying the bill.

Alternatively the import function could check if the splits make sense to be a fraction (1/number of people) and, in those cases, create the right splits.

Here are the relevant lines:

// get those with a negative value, they will be the owers in generated bills
$negativeCols = [];
for ($c = 5; $c < $nbCol; $c++) {
if (!is_numeric($data[$c])) {
fclose($handle);
return ['message' => $this->l10n->t('Malformed CSV, bad amount on line %1$s', [$row])];
}
$amount = (float) $data[$c];
if ($amount < 0) {
$negativeCols[] = $c;
}
}
$owersList = array_map(static function ($c) use ($owersArray) {
return $owersArray[$c - 5];
}, $negativeCols);
// each positive one: bill with member-specific amount (not the full amount), owers are the negative ones
for ($c = 5; $c < $nbCol; $c++) {
$amount = (float) $data[$c];
if ($amount > 0) {
$payer_name = $owersArray[$c - 5];
if (empty($payer_name)) {
fclose($handle);
return ['message' => $this->l10n->t('Malformed CSV, no payer on line %1$s', [$row])];
}
$bill = [
'what' => $what,
'timestamp' => $timestamp,
'amount' => $amount,
'payer_name' => $payer_name,
'owers' => $owersList
];
if ($categoryName !== null) {
$bill['category_name'] = $categoryName;
}
$bills[] = $bill;