Skip to content

Commit

Permalink
make it optional to only use past bills in balance, in admin settings
Browse files Browse the repository at this point in the history
Signed-off-by: Julien Veyssier <[email protected]>
  • Loading branch information
julien-nc committed Dec 23, 2024
1 parent d3f04b4 commit 74e27c1
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 17 deletions.
5 changes: 4 additions & 1 deletion lib/Service/LocalProjectService.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@

use OCP\Federation\ICloudIdManager;

use OCP\IAppConfig;
use OCP\IConfig;
use OCP\IDateTimeZone;

Expand All @@ -73,6 +74,7 @@ class LocalProjectService implements IProjectService {
public function __construct(
private IL10N $l10n,
private IConfig $config,
private IAppConfig $appConfig,
private ProjectMapper $projectMapper,
private BillMapper $billMapper,
private MemberMapper $memberMapper,
Expand Down Expand Up @@ -341,7 +343,8 @@ public function getProjectInfo(string $projectId): array {
}
}
// compute balances for past bills only
$balance = $this->getBalance($dbProjectId, time());
$balancePastBillsOnly = $this->appConfig->getValueString('cospend', 'balance_past_bills_only', '0') === '1';
$balance = $this->getBalance($dbProjectId, $balancePastBillsOnly ? time() : null);
$currencies = $this->getCurrencies($dbProjectId);
$categories = $this->getCategoriesOrPaymentModes($dbProjectId);
$paymentModes = $this->getCategoriesOrPaymentModes($dbProjectId, false);
Expand Down
14 changes: 10 additions & 4 deletions lib/Settings/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,28 @@
use OCA\Cospend\AppInfo\Application;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Services\IInitialState;
use OCP\IConfig;
use OCP\IAppConfig;
use OCP\Settings\ISettings;

class Admin implements ISettings {
public function __construct(
private IInitialState $initialStateService,
private IConfig $config,
private IAppConfig $appConfig,
) {
}

/**
* @return TemplateResponse
*/
public function getForm(): TemplateResponse {
$federationEnabled = $this->config->getAppValue('cospend', 'federation_enabled', '0') === '1';
$this->initialStateService->provideInitialState('federation_enabled', $federationEnabled);
$federationEnabled = $this->appConfig->getValueString('cospend', 'federation_enabled', '0') === '1';
$balancePastBillsOnly = $this->appConfig->getValueString('cospend', 'balance_past_bills_only', '0') === '1';

$values = [
'federation_enabled' => $federationEnabled,
'balance_past_bills_only' => $balancePastBillsOnly,
];
$this->initialStateService->provideInitialState('admin-settings', $values);

return new TemplateResponse(Application::APP_ID, 'adminSettings');
}
Expand Down
35 changes: 23 additions & 12 deletions src/views/AdminSettings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@
{{ t('cospend', 'Cospend') }}
</h2>
<div id="cospend-content">
<div>
<NcCheckboxRadioSwitch :checked="isFederationEnabled"
:disabled="loading"
type="switch"
@update:checked="saveFederationEnabled">
{{ t('cospend', 'Enable Federation in Cospend') }}
</NcCheckboxRadioSwitch>
</div>
<NcCheckboxRadioSwitch :checked="state.federation_enabled"
:disabled="loading"
type="switch"
@update:checked="saveFederationEnabled">
{{ t('cospend', 'Enable Federation in Cospend') }}
</NcCheckboxRadioSwitch>
<NcCheckboxRadioSwitch :checked="state.balance_past_bills_only"
:disabled="loading"
type="switch"
@update:checked="saveBalancePastBillsOnly">
{{ t('cospend', 'Only consider past bills to compute balances') }}
</NcCheckboxRadioSwitch>
</div>
</div>
</template>
Expand All @@ -24,8 +28,6 @@ import NcCheckboxRadioSwitch from '@nextcloud/vue/dist/Components/NcCheckboxRadi
import { loadState } from '@nextcloud/initial-state'
const FEDERATION_ENABLED = loadState('cospend', 'federation_enabled', false)
export default {
name: 'AdminSettings',
Expand All @@ -36,7 +38,7 @@ export default {
data() {
return {
isFederationEnabled: FEDERATION_ENABLED,
state: loadState('cospend', 'admin-settings', {}),
loading: false,
}
},
Expand All @@ -56,7 +58,16 @@ export default {
OCP.AppConfig.setValue('cospend', 'federation_enabled', value ? '1' : '0', {
success: () => {
this.loading = false
this.isFederationEnabled = value
this.state.isFederationEnabled = value
},
})
},
saveBalancePastBillsOnly(value) {
this.loading = true
OCP.AppConfig.setValue('cospend', 'balance_past_bills_only', value ? '1' : '0', {
success: () => {
this.loading = false
this.state.balance_past_bills_only = value
},
})
},
Expand Down

0 comments on commit 74e27c1

Please sign in to comment.