Add comprehensive recurring payments system with scheduling
- Add RecurringPayment model with flexible scheduling options - Implement node-cron based scheduler for payment processing - Create API endpoints for CRUD operations on recurring payments - Add recurring payments management UI with create/edit forms - Integrate scheduler initialization in hooks.server.ts - Enhance payments/add form with progressive enhancement - Add recurring payments button to main dashboard - Improve server-side rendering for better performance 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
14
src/routes/cospend/recurring/+page.server.ts
Normal file
14
src/routes/cospend/recurring/+page.server.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import type { PageServerLoad } from './$types';
|
||||
import { redirect } from '@sveltejs/kit';
|
||||
|
||||
export const load: PageServerLoad = async ({ locals }) => {
|
||||
const session = await locals.auth();
|
||||
|
||||
if (!session) {
|
||||
throw redirect(302, '/login');
|
||||
}
|
||||
|
||||
return {
|
||||
session
|
||||
};
|
||||
};
|
||||
582
src/routes/cospend/recurring/+page.svelte
Normal file
582
src/routes/cospend/recurring/+page.svelte
Normal file
@@ -0,0 +1,582 @@
|
||||
<script>
|
||||
import { onMount } from 'svelte';
|
||||
import { getCategoryEmoji, getCategoryName } from '$lib/utils/categories';
|
||||
import { getFrequencyDescription, formatNextExecution } from '$lib/utils/recurring';
|
||||
import ProfilePicture from '$lib/components/ProfilePicture.svelte';
|
||||
|
||||
export let data;
|
||||
|
||||
let recurringPayments = [];
|
||||
let loading = true;
|
||||
let error = null;
|
||||
let showActiveOnly = true;
|
||||
|
||||
onMount(async () => {
|
||||
await fetchRecurringPayments();
|
||||
});
|
||||
|
||||
async function fetchRecurringPayments() {
|
||||
try {
|
||||
loading = true;
|
||||
const activeParam = showActiveOnly ? '?active=true' : '';
|
||||
const response = await fetch(`/api/cospend/recurring-payments${activeParam}`);
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to fetch recurring payments');
|
||||
}
|
||||
const result = await response.json();
|
||||
recurringPayments = result.recurringPayments;
|
||||
} catch (err) {
|
||||
error = err.message;
|
||||
} finally {
|
||||
loading = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function toggleActiveStatus(paymentId, currentStatus) {
|
||||
try {
|
||||
const response = await fetch(`/api/cospend/recurring-payments/${paymentId}`, {
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({ isActive: !currentStatus })
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to update payment status');
|
||||
}
|
||||
|
||||
// Refresh the list
|
||||
await fetchRecurringPayments();
|
||||
} catch (err) {
|
||||
alert(`Error: ${err.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
async function deleteRecurringPayment(paymentId, title) {
|
||||
if (!confirm(`Are you sure you want to delete the recurring payment "${title}"?`)) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch(`/api/cospend/recurring-payments/${paymentId}`, {
|
||||
method: 'DELETE'
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to delete payment');
|
||||
}
|
||||
|
||||
// Refresh the list
|
||||
await fetchRecurringPayments();
|
||||
} catch (err) {
|
||||
alert(`Error: ${err.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
function formatCurrency(amount) {
|
||||
return new Intl.NumberFormat('de-CH', {
|
||||
style: 'currency',
|
||||
currency: 'CHF'
|
||||
}).format(Math.abs(amount));
|
||||
}
|
||||
|
||||
function formatDate(dateString) {
|
||||
return new Date(dateString).toLocaleDateString('de-CH');
|
||||
}
|
||||
|
||||
$: if (showActiveOnly !== undefined) {
|
||||
fetchRecurringPayments();
|
||||
}
|
||||
</script>
|
||||
|
||||
<svelte:head>
|
||||
<title>Recurring Payments - Cospend</title>
|
||||
</svelte:head>
|
||||
|
||||
<main class="recurring-payments">
|
||||
<div class="header">
|
||||
<h1>Recurring Payments</h1>
|
||||
<div class="header-actions">
|
||||
<a href="/cospend/recurring/add" class="btn btn-primary">Add Recurring Payment</a>
|
||||
<a href="/cospend" class="back-link">← Back to Cospend</a>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="filters">
|
||||
<label>
|
||||
<input type="checkbox" bind:checked={showActiveOnly} />
|
||||
Show active only
|
||||
</label>
|
||||
</div>
|
||||
|
||||
{#if loading}
|
||||
<div class="loading">Loading recurring payments...</div>
|
||||
{:else if error}
|
||||
<div class="error">Error: {error}</div>
|
||||
{:else if recurringPayments.length === 0}
|
||||
<div class="empty-state">
|
||||
<h2>No recurring payments found</h2>
|
||||
<p>Create your first recurring payment to automate regular expenses like rent, utilities, or subscriptions.</p>
|
||||
<a href="/cospend/recurring/add" class="btn btn-primary">Add Your First Recurring Payment</a>
|
||||
</div>
|
||||
{:else}
|
||||
<div class="payments-grid">
|
||||
{#each recurringPayments as payment}
|
||||
<div class="payment-card" class:inactive={!payment.isActive}>
|
||||
<div class="card-header">
|
||||
<div class="payment-title">
|
||||
<span class="category-emoji">{getCategoryEmoji(payment.category)}</span>
|
||||
<h3>{payment.title}</h3>
|
||||
<span class="status-badge" class:active={payment.isActive} class:inactive={!payment.isActive}>
|
||||
{payment.isActive ? 'Active' : 'Inactive'}
|
||||
</span>
|
||||
</div>
|
||||
<div class="payment-amount">
|
||||
{formatCurrency(payment.amount)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{#if payment.description}
|
||||
<p class="payment-description">{payment.description}</p>
|
||||
{/if}
|
||||
|
||||
<div class="payment-details">
|
||||
<div class="detail-row">
|
||||
<span class="label">Category:</span>
|
||||
<span class="value">{getCategoryName(payment.category)}</span>
|
||||
</div>
|
||||
|
||||
<div class="detail-row">
|
||||
<span class="label">Frequency:</span>
|
||||
<span class="value">{getFrequencyDescription(payment)}</span>
|
||||
</div>
|
||||
|
||||
<div class="detail-row">
|
||||
<span class="label">Paid by:</span>
|
||||
<div class="payer-info">
|
||||
<ProfilePicture username={payment.paidBy} size={20} />
|
||||
<span class="value">{payment.paidBy}</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="detail-row">
|
||||
<span class="label">Next execution:</span>
|
||||
<span class="value next-execution">
|
||||
{formatNextExecution(new Date(payment.nextExecutionDate))}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
{#if payment.lastExecutionDate}
|
||||
<div class="detail-row">
|
||||
<span class="label">Last executed:</span>
|
||||
<span class="value">{formatDate(payment.lastExecutionDate)}</span>
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
{#if payment.endDate}
|
||||
<div class="detail-row">
|
||||
<span class="label">Ends:</span>
|
||||
<span class="value">{formatDate(payment.endDate)}</span>
|
||||
</div>
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
<div class="splits-preview">
|
||||
<h4>Split between:</h4>
|
||||
<div class="splits-list">
|
||||
{#each payment.splits as split}
|
||||
<div class="split-item">
|
||||
<ProfilePicture username={split.username} size={24} />
|
||||
<span class="username">{split.username}</span>
|
||||
<span class="split-amount" class:positive={split.amount < 0} class:negative={split.amount > 0}>
|
||||
{#if split.amount > 0}
|
||||
owes {formatCurrency(split.amount)}
|
||||
{:else if split.amount < 0}
|
||||
gets {formatCurrency(split.amount)}
|
||||
{:else}
|
||||
even
|
||||
{/if}
|
||||
</span>
|
||||
</div>
|
||||
{/each}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="card-actions">
|
||||
<a href="/cospend/recurring/edit/{payment._id}" class="btn btn-secondary btn-small">
|
||||
Edit
|
||||
</a>
|
||||
<button
|
||||
class="btn btn-small"
|
||||
class:btn-warning={payment.isActive}
|
||||
class:btn-success={!payment.isActive}
|
||||
on:click={() => toggleActiveStatus(payment._id, payment.isActive)}
|
||||
>
|
||||
{payment.isActive ? 'Pause' : 'Activate'}
|
||||
</button>
|
||||
<button
|
||||
class="btn btn-danger btn-small"
|
||||
on:click={() => deleteRecurringPayment(payment._id, payment.title)}
|
||||
>
|
||||
Delete
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
{/each}
|
||||
</div>
|
||||
{/if}
|
||||
</main>
|
||||
|
||||
<style>
|
||||
.recurring-payments {
|
||||
max-width: 1200px;
|
||||
margin: 0 auto;
|
||||
padding: 2rem;
|
||||
}
|
||||
|
||||
.header {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
margin-bottom: 2rem;
|
||||
}
|
||||
|
||||
.header h1 {
|
||||
margin: 0;
|
||||
color: #333;
|
||||
}
|
||||
|
||||
.header-actions {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 1rem;
|
||||
}
|
||||
|
||||
.back-link {
|
||||
color: #1976d2;
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
.filters {
|
||||
margin-bottom: 1.5rem;
|
||||
padding: 1rem;
|
||||
background: white;
|
||||
border-radius: 0.5rem;
|
||||
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
|
||||
}
|
||||
|
||||
.filters label {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.5rem;
|
||||
cursor: pointer;
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.loading, .error {
|
||||
text-align: center;
|
||||
padding: 2rem;
|
||||
font-size: 1.1rem;
|
||||
}
|
||||
|
||||
.error {
|
||||
color: #d32f2f;
|
||||
background-color: #ffebee;
|
||||
border-radius: 0.5rem;
|
||||
}
|
||||
|
||||
.empty-state {
|
||||
text-align: center;
|
||||
padding: 4rem 2rem;
|
||||
background: white;
|
||||
border-radius: 0.75rem;
|
||||
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
|
||||
}
|
||||
|
||||
.empty-state h2 {
|
||||
margin-bottom: 1rem;
|
||||
color: #333;
|
||||
}
|
||||
|
||||
.empty-state p {
|
||||
color: #666;
|
||||
margin-bottom: 2rem;
|
||||
max-width: 500px;
|
||||
margin-left: auto;
|
||||
margin-right: auto;
|
||||
}
|
||||
|
||||
.payments-grid {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fill, minmax(400px, 1fr));
|
||||
gap: 1.5rem;
|
||||
}
|
||||
|
||||
.payment-card {
|
||||
background: white;
|
||||
border-radius: 0.75rem;
|
||||
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
|
||||
padding: 1.5rem;
|
||||
transition: transform 0.2s;
|
||||
}
|
||||
|
||||
.payment-card:hover {
|
||||
transform: translateY(-2px);
|
||||
box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
|
||||
}
|
||||
|
||||
.payment-card.inactive {
|
||||
opacity: 0.7;
|
||||
background: #f8f9fa;
|
||||
}
|
||||
|
||||
.card-header {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: flex-start;
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.payment-title {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.75rem;
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
.category-emoji {
|
||||
font-size: 1.5rem;
|
||||
}
|
||||
|
||||
.payment-title h3 {
|
||||
margin: 0;
|
||||
color: #333;
|
||||
font-size: 1.25rem;
|
||||
}
|
||||
|
||||
.status-badge {
|
||||
padding: 0.25rem 0.75rem;
|
||||
border-radius: 1rem;
|
||||
font-size: 0.75rem;
|
||||
font-weight: 600;
|
||||
text-transform: uppercase;
|
||||
letter-spacing: 0.5px;
|
||||
}
|
||||
|
||||
.status-badge.active {
|
||||
background-color: #e8f5e8;
|
||||
color: #2e7d32;
|
||||
}
|
||||
|
||||
.status-badge.inactive {
|
||||
background-color: #fff3e0;
|
||||
color: #f57c00;
|
||||
}
|
||||
|
||||
.payment-amount {
|
||||
font-size: 1.5rem;
|
||||
font-weight: 700;
|
||||
color: #1976d2;
|
||||
}
|
||||
|
||||
.payment-description {
|
||||
color: #666;
|
||||
margin-bottom: 1rem;
|
||||
font-style: italic;
|
||||
}
|
||||
|
||||
.payment-details {
|
||||
margin-bottom: 1.5rem;
|
||||
}
|
||||
|
||||
.detail-row {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
margin-bottom: 0.5rem;
|
||||
padding: 0.25rem 0;
|
||||
}
|
||||
|
||||
.label {
|
||||
font-weight: 500;
|
||||
color: #666;
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
|
||||
.value {
|
||||
color: #333;
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.next-execution {
|
||||
color: #1976d2;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.payer-info {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.5rem;
|
||||
}
|
||||
|
||||
.splits-preview {
|
||||
margin-bottom: 1.5rem;
|
||||
padding: 1rem;
|
||||
background-color: #f8f9fa;
|
||||
border-radius: 0.5rem;
|
||||
}
|
||||
|
||||
.splits-preview h4 {
|
||||
margin: 0 0 0.75rem 0;
|
||||
font-size: 0.9rem;
|
||||
color: #666;
|
||||
text-transform: uppercase;
|
||||
letter-spacing: 0.5px;
|
||||
}
|
||||
|
||||
.splits-list {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 0.5rem;
|
||||
}
|
||||
|
||||
.split-item {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.5rem;
|
||||
}
|
||||
|
||||
.split-item .username {
|
||||
flex: 1;
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.split-amount {
|
||||
font-size: 0.9rem;
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.split-amount.positive {
|
||||
color: #2e7d32;
|
||||
}
|
||||
|
||||
.split-amount.negative {
|
||||
color: #d32f2f;
|
||||
}
|
||||
|
||||
.card-actions {
|
||||
display: flex;
|
||||
gap: 0.5rem;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
|
||||
.btn {
|
||||
padding: 0.75rem 1.5rem;
|
||||
border-radius: 0.5rem;
|
||||
text-decoration: none;
|
||||
font-weight: 500;
|
||||
border: none;
|
||||
cursor: pointer;
|
||||
transition: all 0.2s;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.btn-small {
|
||||
padding: 0.5rem 1rem;
|
||||
font-size: 0.875rem;
|
||||
}
|
||||
|
||||
.btn-primary {
|
||||
background-color: #1976d2;
|
||||
color: white;
|
||||
}
|
||||
|
||||
.btn-primary:hover {
|
||||
background-color: #1565c0;
|
||||
}
|
||||
|
||||
.btn-secondary {
|
||||
background-color: #f5f5f5;
|
||||
color: #333;
|
||||
border: 1px solid #ddd;
|
||||
}
|
||||
|
||||
.btn-secondary:hover {
|
||||
background-color: #e8e8e8;
|
||||
}
|
||||
|
||||
.btn-warning {
|
||||
background-color: #ff9800;
|
||||
color: white;
|
||||
}
|
||||
|
||||
.btn-warning:hover {
|
||||
background-color: #f57c00;
|
||||
}
|
||||
|
||||
.btn-success {
|
||||
background-color: #4caf50;
|
||||
color: white;
|
||||
}
|
||||
|
||||
.btn-success:hover {
|
||||
background-color: #45a049;
|
||||
}
|
||||
|
||||
.btn-danger {
|
||||
background-color: #f44336;
|
||||
color: white;
|
||||
}
|
||||
|
||||
.btn-danger:hover {
|
||||
background-color: #d32f2f;
|
||||
}
|
||||
|
||||
@media (max-width: 768px) {
|
||||
.recurring-payments {
|
||||
padding: 1rem;
|
||||
}
|
||||
|
||||
.header {
|
||||
flex-direction: column;
|
||||
gap: 1rem;
|
||||
align-items: stretch;
|
||||
}
|
||||
|
||||
.header-actions {
|
||||
justify-content: space-between;
|
||||
}
|
||||
|
||||
.payments-grid {
|
||||
grid-template-columns: 1fr;
|
||||
}
|
||||
|
||||
.payment-card {
|
||||
padding: 1rem;
|
||||
}
|
||||
|
||||
.card-header {
|
||||
flex-direction: column;
|
||||
gap: 1rem;
|
||||
}
|
||||
|
||||
.payment-title {
|
||||
justify-content: space-between;
|
||||
}
|
||||
|
||||
.detail-row {
|
||||
flex-direction: column;
|
||||
align-items: flex-start;
|
||||
gap: 0.25rem;
|
||||
}
|
||||
|
||||
.card-actions {
|
||||
justify-content: stretch;
|
||||
}
|
||||
|
||||
.card-actions .btn {
|
||||
flex: 1;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
14
src/routes/cospend/recurring/add/+page.server.ts
Normal file
14
src/routes/cospend/recurring/add/+page.server.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import type { PageServerLoad } from './$types';
|
||||
import { redirect } from '@sveltejs/kit';
|
||||
|
||||
export const load: PageServerLoad = async ({ locals }) => {
|
||||
const session = await locals.auth();
|
||||
|
||||
if (!session) {
|
||||
throw redirect(302, '/login');
|
||||
}
|
||||
|
||||
return {
|
||||
session
|
||||
};
|
||||
};
|
||||
942
src/routes/cospend/recurring/add/+page.svelte
Normal file
942
src/routes/cospend/recurring/add/+page.svelte
Normal file
@@ -0,0 +1,942 @@
|
||||
<script>
|
||||
import { onMount } from 'svelte';
|
||||
import { goto } from '$app/navigation';
|
||||
import { getCategoryOptions } from '$lib/utils/categories';
|
||||
import { PREDEFINED_USERS, isPredefinedUsersMode } from '$lib/config/users';
|
||||
import { validateCronExpression, getFrequencyDescription, calculateNextExecutionDate } from '$lib/utils/recurring';
|
||||
import ProfilePicture from '$lib/components/ProfilePicture.svelte';
|
||||
|
||||
export let data;
|
||||
|
||||
let formData = {
|
||||
title: '',
|
||||
description: '',
|
||||
amount: '',
|
||||
paidBy: data.session?.user?.nickname || '',
|
||||
category: 'groceries',
|
||||
splitMethod: 'equal',
|
||||
splits: [],
|
||||
frequency: 'monthly',
|
||||
cronExpression: '',
|
||||
startDate: new Date().toISOString().split('T')[0],
|
||||
endDate: ''
|
||||
};
|
||||
|
||||
let users = [];
|
||||
let newUser = '';
|
||||
let splitAmounts = {};
|
||||
let personalAmounts = {};
|
||||
let loading = false;
|
||||
let error = null;
|
||||
let personalTotalError = false;
|
||||
let predefinedMode = isPredefinedUsersMode();
|
||||
let cronError = false;
|
||||
let nextExecutionPreview = '';
|
||||
|
||||
$: categoryOptions = getCategoryOptions();
|
||||
|
||||
onMount(() => {
|
||||
if (predefinedMode) {
|
||||
users = [...PREDEFINED_USERS];
|
||||
users.forEach(user => addSplitForUser(user));
|
||||
if (data.session?.user?.nickname && PREDEFINED_USERS.includes(data.session.user.nickname)) {
|
||||
formData.paidBy = data.session.user.nickname;
|
||||
} else {
|
||||
formData.paidBy = PREDEFINED_USERS[0];
|
||||
}
|
||||
} else {
|
||||
if (data.session?.user?.nickname) {
|
||||
users = [data.session.user.nickname];
|
||||
addSplitForUser(data.session.user.nickname);
|
||||
}
|
||||
}
|
||||
updateNextExecutionPreview();
|
||||
});
|
||||
|
||||
function addUser() {
|
||||
if (predefinedMode) return;
|
||||
|
||||
if (newUser.trim() && !users.includes(newUser.trim())) {
|
||||
users = [...users, newUser.trim()];
|
||||
addSplitForUser(newUser.trim());
|
||||
newUser = '';
|
||||
}
|
||||
}
|
||||
|
||||
function removeUser(userToRemove) {
|
||||
if (predefinedMode) return;
|
||||
|
||||
if (users.length > 1 && userToRemove !== data.session.user.nickname) {
|
||||
users = users.filter(u => u !== userToRemove);
|
||||
delete splitAmounts[userToRemove];
|
||||
splitAmounts = { ...splitAmounts };
|
||||
}
|
||||
}
|
||||
|
||||
function addSplitForUser(username) {
|
||||
if (!splitAmounts[username]) {
|
||||
splitAmounts[username] = 0;
|
||||
splitAmounts = { ...splitAmounts };
|
||||
}
|
||||
}
|
||||
|
||||
function calculateEqualSplits() {
|
||||
if (!formData.amount || users.length === 0) return;
|
||||
|
||||
const amountNum = parseFloat(formData.amount);
|
||||
const splitAmount = amountNum / users.length;
|
||||
|
||||
users.forEach(user => {
|
||||
if (user === formData.paidBy) {
|
||||
splitAmounts[user] = splitAmount - amountNum;
|
||||
} else {
|
||||
splitAmounts[user] = splitAmount;
|
||||
}
|
||||
});
|
||||
splitAmounts = { ...splitAmounts };
|
||||
}
|
||||
|
||||
function calculateFullPayment() {
|
||||
if (!formData.amount) return;
|
||||
|
||||
const amountNum = parseFloat(formData.amount);
|
||||
|
||||
users.forEach(user => {
|
||||
if (user === formData.paidBy) {
|
||||
splitAmounts[user] = -amountNum;
|
||||
} else {
|
||||
splitAmounts[user] = 0;
|
||||
}
|
||||
});
|
||||
splitAmounts = { ...splitAmounts };
|
||||
}
|
||||
|
||||
function calculatePersonalEqualSplit() {
|
||||
if (!formData.amount || users.length === 0) return;
|
||||
|
||||
const totalAmount = parseFloat(formData.amount);
|
||||
|
||||
const totalPersonal = users.reduce((sum, user) => {
|
||||
return sum + (parseFloat(personalAmounts[user]) || 0);
|
||||
}, 0);
|
||||
|
||||
const remainder = Math.max(0, totalAmount - totalPersonal);
|
||||
const equalShare = remainder / users.length;
|
||||
|
||||
users.forEach(user => {
|
||||
const personalAmount = parseFloat(personalAmounts[user]) || 0;
|
||||
const totalOwed = personalAmount + equalShare;
|
||||
|
||||
if (user === formData.paidBy) {
|
||||
splitAmounts[user] = totalOwed - totalAmount;
|
||||
} else {
|
||||
splitAmounts[user] = totalOwed;
|
||||
}
|
||||
});
|
||||
splitAmounts = { ...splitAmounts };
|
||||
}
|
||||
|
||||
function handleSplitMethodChange() {
|
||||
if (formData.splitMethod === 'equal') {
|
||||
calculateEqualSplits();
|
||||
} else if (formData.splitMethod === 'full') {
|
||||
calculateFullPayment();
|
||||
} else if (formData.splitMethod === 'personal_equal') {
|
||||
calculatePersonalEqualSplit();
|
||||
}
|
||||
}
|
||||
|
||||
function validateCron() {
|
||||
if (formData.frequency !== 'custom') {
|
||||
cronError = false;
|
||||
return;
|
||||
}
|
||||
cronError = !validateCronExpression(formData.cronExpression);
|
||||
}
|
||||
|
||||
function updateNextExecutionPreview() {
|
||||
try {
|
||||
if (formData.frequency && formData.startDate) {
|
||||
const recurringPayment = {
|
||||
...formData,
|
||||
startDate: new Date(formData.startDate)
|
||||
};
|
||||
const nextDate = calculateNextExecutionDate(recurringPayment, new Date(formData.startDate));
|
||||
nextExecutionPreview = nextDate.toLocaleString('de-CH', {
|
||||
weekday: 'long',
|
||||
year: 'numeric',
|
||||
month: 'long',
|
||||
day: 'numeric',
|
||||
hour: '2-digit',
|
||||
minute: '2-digit'
|
||||
});
|
||||
}
|
||||
} catch (e) {
|
||||
nextExecutionPreview = 'Invalid configuration';
|
||||
}
|
||||
}
|
||||
|
||||
async function handleSubmit() {
|
||||
if (!formData.title.trim() || !formData.amount || parseFloat(formData.amount) <= 0) {
|
||||
error = 'Please fill in all required fields with valid values';
|
||||
return;
|
||||
}
|
||||
|
||||
if (formData.frequency === 'custom' && cronError) {
|
||||
error = 'Please enter a valid cron expression';
|
||||
return;
|
||||
}
|
||||
|
||||
if (formData.splitMethod === 'personal_equal') {
|
||||
const totalPersonal = Object.values(personalAmounts).reduce((sum, val) => sum + (parseFloat(val) || 0), 0);
|
||||
const totalAmount = parseFloat(formData.amount);
|
||||
|
||||
if (totalPersonal > totalAmount) {
|
||||
error = 'Personal amounts cannot exceed the total payment amount';
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (users.length === 0) {
|
||||
error = 'Please add at least one user to split with';
|
||||
return;
|
||||
}
|
||||
|
||||
loading = true;
|
||||
error = null;
|
||||
|
||||
try {
|
||||
const splits = users.map(user => ({
|
||||
username: user,
|
||||
amount: splitAmounts[user] || 0,
|
||||
proportion: formData.splitMethod === 'proportional' ? (splitAmounts[user] || 0) / parseFloat(formData.amount) : undefined,
|
||||
personalAmount: formData.splitMethod === 'personal_equal' ? (parseFloat(personalAmounts[user]) || 0) : undefined
|
||||
}));
|
||||
|
||||
const payload = {
|
||||
...formData,
|
||||
amount: parseFloat(formData.amount),
|
||||
startDate: formData.startDate ? new Date(formData.startDate).toISOString() : new Date().toISOString(),
|
||||
endDate: formData.endDate ? new Date(formData.endDate).toISOString() : null,
|
||||
cronExpression: formData.frequency === 'custom' ? formData.cronExpression : undefined,
|
||||
splits
|
||||
};
|
||||
|
||||
const response = await fetch('/api/cospend/recurring-payments', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify(payload)
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const errorData = await response.json();
|
||||
throw new Error(errorData.message || 'Failed to create recurring payment');
|
||||
}
|
||||
|
||||
const result = await response.json();
|
||||
await goto('/cospend/recurring');
|
||||
|
||||
} catch (err) {
|
||||
error = err.message;
|
||||
} finally {
|
||||
loading = false;
|
||||
}
|
||||
}
|
||||
|
||||
$: if (formData.amount && formData.splitMethod && formData.paidBy) {
|
||||
handleSplitMethodChange();
|
||||
}
|
||||
|
||||
$: if (formData.splitMethod === 'personal_equal' && personalAmounts && formData.amount) {
|
||||
const totalPersonal = Object.values(personalAmounts).reduce((sum, val) => sum + (parseFloat(val) || 0), 0);
|
||||
const totalAmount = parseFloat(formData.amount);
|
||||
personalTotalError = totalPersonal > totalAmount;
|
||||
|
||||
if (!personalTotalError) {
|
||||
calculatePersonalEqualSplit();
|
||||
}
|
||||
}
|
||||
|
||||
$: if (formData.cronExpression) {
|
||||
validateCron();
|
||||
}
|
||||
|
||||
$: if (formData.frequency || formData.cronExpression || formData.startDate) {
|
||||
updateNextExecutionPreview();
|
||||
}
|
||||
</script>
|
||||
|
||||
<svelte:head>
|
||||
<title>Add Recurring Payment - Cospend</title>
|
||||
</svelte:head>
|
||||
|
||||
<main class="add-recurring-payment">
|
||||
<div class="header">
|
||||
<h1>Add Recurring Payment</h1>
|
||||
<a href="/cospend" class="back-link">← Back to Cospend</a>
|
||||
</div>
|
||||
|
||||
<form on:submit|preventDefault={handleSubmit} class="payment-form">
|
||||
<div class="form-section">
|
||||
<h2>Payment Details</h2>
|
||||
|
||||
<div class="form-group">
|
||||
<label for="title">Title *</label>
|
||||
<input
|
||||
type="text"
|
||||
id="title"
|
||||
bind:value={formData.title}
|
||||
required
|
||||
placeholder="e.g., Monthly rent, Weekly groceries"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label for="description">Description</label>
|
||||
<textarea
|
||||
id="description"
|
||||
bind:value={formData.description}
|
||||
placeholder="Additional details about this recurring payment..."
|
||||
rows="3"
|
||||
></textarea>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label for="category">Category *</label>
|
||||
<select id="category" bind:value={formData.category} required>
|
||||
{#each categoryOptions as option}
|
||||
<option value={option.value}>{option.label}</option>
|
||||
{/each}
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<div class="form-row">
|
||||
<div class="form-group">
|
||||
<label for="amount">Amount (CHF) *</label>
|
||||
<input
|
||||
type="number"
|
||||
id="amount"
|
||||
bind:value={formData.amount}
|
||||
required
|
||||
min="0"
|
||||
step="0.01"
|
||||
placeholder="0.00"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label for="paidBy">Paid by</label>
|
||||
<select id="paidBy" bind:value={formData.paidBy} required>
|
||||
{#each users as user}
|
||||
<option value={user}>{user}</option>
|
||||
{/each}
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="form-section">
|
||||
<h2>Recurring Schedule</h2>
|
||||
|
||||
<div class="form-row">
|
||||
<div class="form-group">
|
||||
<label for="frequency">Frequency *</label>
|
||||
<select id="frequency" bind:value={formData.frequency} required>
|
||||
<option value="daily">Daily</option>
|
||||
<option value="weekly">Weekly</option>
|
||||
<option value="monthly">Monthly</option>
|
||||
<option value="custom">Custom (Cron)</option>
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label for="startDate">Start Date *</label>
|
||||
<input
|
||||
type="date"
|
||||
id="startDate"
|
||||
bind:value={formData.startDate}
|
||||
required
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{#if formData.frequency === 'custom'}
|
||||
<div class="form-group">
|
||||
<label for="cronExpression">Cron Expression *</label>
|
||||
<input
|
||||
type="text"
|
||||
id="cronExpression"
|
||||
bind:value={formData.cronExpression}
|
||||
required
|
||||
placeholder="0 9 * * 1 (Every Monday at 9:00 AM)"
|
||||
class:error={cronError}
|
||||
/>
|
||||
<div class="help-text">
|
||||
<p>Cron format: minute hour day-of-month month day-of-week</p>
|
||||
<p>Examples:</p>
|
||||
<ul>
|
||||
<li><code>0 9 * * *</code> - Every day at 9:00 AM</li>
|
||||
<li><code>0 9 1 * *</code> - Every 1st of the month at 9:00 AM</li>
|
||||
<li><code>0 9 * * 1</code> - Every Monday at 9:00 AM</li>
|
||||
<li><code>0 9 1,15 * *</code> - 1st and 15th of every month at 9:00 AM</li>
|
||||
</ul>
|
||||
</div>
|
||||
{#if cronError}
|
||||
<div class="field-error">Invalid cron expression</div>
|
||||
{/if}
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
<div class="form-group">
|
||||
<label for="endDate">End Date (optional)</label>
|
||||
<input
|
||||
type="date"
|
||||
id="endDate"
|
||||
bind:value={formData.endDate}
|
||||
/>
|
||||
<div class="help-text">Leave blank for indefinite recurring payments</div>
|
||||
</div>
|
||||
|
||||
{#if nextExecutionPreview}
|
||||
<div class="execution-preview">
|
||||
<h3>Next Execution</h3>
|
||||
<p class="next-execution">{nextExecutionPreview}</p>
|
||||
<p class="frequency-description">{getFrequencyDescription(formData)}</p>
|
||||
</div>
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
<div class="form-section">
|
||||
<h2>Split Between Users</h2>
|
||||
|
||||
{#if predefinedMode}
|
||||
<div class="predefined-users">
|
||||
<p class="predefined-note">Splitting between predefined users:</p>
|
||||
<div class="users-list">
|
||||
{#each users as user}
|
||||
<div class="user-item with-profile">
|
||||
<ProfilePicture username={user} size={32} />
|
||||
<span class="username">{user}</span>
|
||||
{#if user === data.session?.user?.nickname}
|
||||
<span class="you-badge">You</span>
|
||||
{/if}
|
||||
</div>
|
||||
{/each}
|
||||
</div>
|
||||
</div>
|
||||
{:else}
|
||||
<div class="users-list">
|
||||
{#each users as user}
|
||||
<div class="user-item with-profile">
|
||||
<ProfilePicture username={user} size={32} />
|
||||
<span class="username">{user}</span>
|
||||
{#if user !== data.session.user.nickname}
|
||||
<button type="button" class="remove-user" on:click={() => removeUser(user)}>
|
||||
Remove
|
||||
</button>
|
||||
{/if}
|
||||
</div>
|
||||
{/each}
|
||||
</div>
|
||||
|
||||
<div class="add-user">
|
||||
<input
|
||||
type="text"
|
||||
bind:value={newUser}
|
||||
placeholder="Add user..."
|
||||
on:keydown={(e) => e.key === 'Enter' && (e.preventDefault(), addUser())}
|
||||
/>
|
||||
<button type="button" on:click={addUser}>Add User</button>
|
||||
</div>
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
<div class="form-section">
|
||||
<h2>Split Method</h2>
|
||||
|
||||
<div class="split-method">
|
||||
<label>
|
||||
<input type="radio" bind:group={formData.splitMethod} value="equal" />
|
||||
{predefinedMode && users.length === 2 ? 'Split 50/50' : 'Equal Split'}
|
||||
</label>
|
||||
<label>
|
||||
<input type="radio" bind:group={formData.splitMethod} value="personal_equal" />
|
||||
Personal + Equal Split
|
||||
</label>
|
||||
<label>
|
||||
<input type="radio" bind:group={formData.splitMethod} value="full" />
|
||||
Paid in Full by {formData.paidBy}
|
||||
</label>
|
||||
<label>
|
||||
<input type="radio" bind:group={formData.splitMethod} value="proportional" />
|
||||
Custom Proportions
|
||||
</label>
|
||||
</div>
|
||||
|
||||
{#if formData.splitMethod === 'proportional'}
|
||||
<div class="proportional-splits">
|
||||
<h3>Custom Split Amounts</h3>
|
||||
{#each users as user}
|
||||
<div class="split-input">
|
||||
<label>{user}</label>
|
||||
<input
|
||||
type="number"
|
||||
step="0.01"
|
||||
bind:value={splitAmounts[user]}
|
||||
placeholder="0.00"
|
||||
/>
|
||||
</div>
|
||||
{/each}
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
{#if formData.splitMethod === 'personal_equal'}
|
||||
<div class="personal-splits">
|
||||
<h3>Personal Amounts</h3>
|
||||
<p class="description">Enter personal amounts for each user. The remainder will be split equally.</p>
|
||||
{#each users as user}
|
||||
<div class="split-input">
|
||||
<label>{user}</label>
|
||||
<input
|
||||
type="number"
|
||||
step="0.01"
|
||||
min="0"
|
||||
bind:value={personalAmounts[user]}
|
||||
placeholder="0.00"
|
||||
/>
|
||||
</div>
|
||||
{/each}
|
||||
{#if formData.amount}
|
||||
<div class="remainder-info" class:error={personalTotalError}>
|
||||
<span>Total Personal: CHF {Object.values(personalAmounts).reduce((sum, val) => sum + (parseFloat(val) || 0), 0).toFixed(2)}</span>
|
||||
<span>Remainder to Split: CHF {Math.max(0, parseFloat(formData.amount) - Object.values(personalAmounts).reduce((sum, val) => sum + (parseFloat(val) || 0), 0)).toFixed(2)}</span>
|
||||
{#if personalTotalError}
|
||||
<div class="error-message">⚠️ Personal amounts exceed total payment amount!</div>
|
||||
{/if}
|
||||
</div>
|
||||
{/if}
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
{#if Object.keys(splitAmounts).length > 0}
|
||||
<div class="split-preview">
|
||||
<h3>Split Preview</h3>
|
||||
{#each users as user}
|
||||
<div class="split-item">
|
||||
<div class="split-user">
|
||||
<ProfilePicture username={user} size={24} />
|
||||
<span class="username">{user}</span>
|
||||
</div>
|
||||
<span class="amount" class:positive={splitAmounts[user] < 0} class:negative={splitAmounts[user] > 0}>
|
||||
{#if splitAmounts[user] > 0}
|
||||
owes CHF {splitAmounts[user].toFixed(2)}
|
||||
{:else if splitAmounts[user] < 0}
|
||||
is owed CHF {Math.abs(splitAmounts[user]).toFixed(2)}
|
||||
{:else}
|
||||
even
|
||||
{/if}
|
||||
</span>
|
||||
</div>
|
||||
{/each}
|
||||
</div>
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
{#if error}
|
||||
<div class="error">{error}</div>
|
||||
{/if}
|
||||
|
||||
<div class="form-actions">
|
||||
<button type="button" class="btn-secondary" on:click={() => goto('/cospend')}>
|
||||
Cancel
|
||||
</button>
|
||||
<button type="submit" class="btn-primary" disabled={loading || cronError}>
|
||||
{loading ? 'Creating...' : 'Create Recurring Payment'}
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
</main>
|
||||
|
||||
<style>
|
||||
.add-recurring-payment {
|
||||
max-width: 800px;
|
||||
margin: 0 auto;
|
||||
padding: 2rem;
|
||||
}
|
||||
|
||||
.header {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
margin-bottom: 2rem;
|
||||
}
|
||||
|
||||
.header h1 {
|
||||
margin: 0;
|
||||
color: #333;
|
||||
}
|
||||
|
||||
.back-link {
|
||||
color: #1976d2;
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
.payment-form {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 2rem;
|
||||
}
|
||||
|
||||
.form-section {
|
||||
background: white;
|
||||
padding: 1.5rem;
|
||||
border-radius: 0.75rem;
|
||||
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
|
||||
}
|
||||
|
||||
.form-section h2 {
|
||||
margin-top: 0;
|
||||
margin-bottom: 1rem;
|
||||
color: #333;
|
||||
font-size: 1.25rem;
|
||||
}
|
||||
|
||||
.form-group {
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.form-row {
|
||||
display: grid;
|
||||
grid-template-columns: 1fr 1fr;
|
||||
gap: 1rem;
|
||||
}
|
||||
|
||||
label {
|
||||
display: block;
|
||||
margin-bottom: 0.5rem;
|
||||
font-weight: 500;
|
||||
color: #555;
|
||||
}
|
||||
|
||||
input, textarea, select {
|
||||
width: 100%;
|
||||
padding: 0.75rem;
|
||||
border: 1px solid #ddd;
|
||||
border-radius: 0.5rem;
|
||||
font-size: 1rem;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
input:focus, textarea:focus, select:focus {
|
||||
outline: none;
|
||||
border-color: #1976d2;
|
||||
box-shadow: 0 0 0 2px rgba(25, 118, 210, 0.2);
|
||||
}
|
||||
|
||||
input.error {
|
||||
border-color: #d32f2f;
|
||||
}
|
||||
|
||||
.help-text {
|
||||
margin-top: 0.5rem;
|
||||
color: #666;
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
|
||||
.help-text code {
|
||||
background-color: #f5f5f5;
|
||||
padding: 0.125rem 0.25rem;
|
||||
border-radius: 0.25rem;
|
||||
font-family: monospace;
|
||||
}
|
||||
|
||||
.help-text ul {
|
||||
margin: 0.5rem 0;
|
||||
padding-left: 1rem;
|
||||
}
|
||||
|
||||
.help-text li {
|
||||
margin-bottom: 0.25rem;
|
||||
}
|
||||
|
||||
.field-error {
|
||||
color: #d32f2f;
|
||||
font-size: 0.875rem;
|
||||
margin-top: 0.25rem;
|
||||
}
|
||||
|
||||
.execution-preview {
|
||||
background-color: #e3f2fd;
|
||||
border: 1px solid #2196f3;
|
||||
border-radius: 0.5rem;
|
||||
padding: 1rem;
|
||||
margin-top: 1rem;
|
||||
}
|
||||
|
||||
.execution-preview h3 {
|
||||
margin: 0 0 0.5rem 0;
|
||||
color: #1976d2;
|
||||
font-size: 1rem;
|
||||
}
|
||||
|
||||
.next-execution {
|
||||
font-size: 1.1rem;
|
||||
font-weight: 600;
|
||||
color: #1976d2;
|
||||
margin: 0.5rem 0;
|
||||
}
|
||||
|
||||
.frequency-description {
|
||||
color: #666;
|
||||
font-size: 0.9rem;
|
||||
margin: 0;
|
||||
font-style: italic;
|
||||
}
|
||||
|
||||
.users-list {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: 0.5rem;
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.user-item {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.5rem;
|
||||
background-color: #f5f5f5;
|
||||
padding: 0.5rem 0.75rem;
|
||||
border-radius: 1rem;
|
||||
}
|
||||
|
||||
.user-item.with-profile {
|
||||
gap: 0.75rem;
|
||||
}
|
||||
|
||||
.user-item .username {
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.you-badge {
|
||||
background-color: #1976d2;
|
||||
color: white;
|
||||
padding: 0.125rem 0.5rem;
|
||||
border-radius: 1rem;
|
||||
font-size: 0.75rem;
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.predefined-users {
|
||||
background-color: #f8f9fa;
|
||||
padding: 1rem;
|
||||
border-radius: 0.5rem;
|
||||
border: 1px solid #e9ecef;
|
||||
}
|
||||
|
||||
.predefined-note {
|
||||
margin: 0 0 1rem 0;
|
||||
color: #666;
|
||||
font-size: 0.9rem;
|
||||
font-style: italic;
|
||||
}
|
||||
|
||||
.remove-user {
|
||||
background-color: #d32f2f;
|
||||
color: white;
|
||||
border: none;
|
||||
padding: 0.25rem 0.5rem;
|
||||
border-radius: 0.25rem;
|
||||
font-size: 0.75rem;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.add-user {
|
||||
display: flex;
|
||||
gap: 0.5rem;
|
||||
}
|
||||
|
||||
.add-user input {
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
.add-user button {
|
||||
background-color: #1976d2;
|
||||
color: white;
|
||||
border: none;
|
||||
padding: 0.75rem 1rem;
|
||||
border-radius: 0.5rem;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.split-method {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 0.75rem;
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.split-method label {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.5rem;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.proportional-splits, .personal-splits {
|
||||
border: 1px solid #ddd;
|
||||
border-radius: 0.5rem;
|
||||
padding: 1rem;
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.proportional-splits h3, .personal-splits h3 {
|
||||
margin-top: 0;
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.personal-splits .description {
|
||||
color: #666;
|
||||
font-size: 0.9rem;
|
||||
margin-bottom: 1rem;
|
||||
font-style: italic;
|
||||
}
|
||||
|
||||
.split-input {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 1rem;
|
||||
margin-bottom: 0.5rem;
|
||||
}
|
||||
|
||||
.split-input label {
|
||||
min-width: 100px;
|
||||
margin-bottom: 0;
|
||||
}
|
||||
|
||||
.split-input input {
|
||||
max-width: 120px;
|
||||
}
|
||||
|
||||
.remainder-info {
|
||||
margin-top: 1rem;
|
||||
padding: 1rem;
|
||||
background-color: #f8f9fa;
|
||||
border-radius: 0.5rem;
|
||||
border: 1px solid #e9ecef;
|
||||
}
|
||||
|
||||
.remainder-info.error {
|
||||
background-color: #fff5f5;
|
||||
border-color: #fed7d7;
|
||||
}
|
||||
|
||||
.remainder-info span {
|
||||
display: block;
|
||||
margin-bottom: 0.5rem;
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.error-message {
|
||||
color: #d32f2f;
|
||||
font-weight: 600;
|
||||
margin-top: 0.5rem;
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
|
||||
.split-preview {
|
||||
background-color: #f8f9fa;
|
||||
padding: 1rem;
|
||||
border-radius: 0.5rem;
|
||||
}
|
||||
|
||||
.split-preview h3 {
|
||||
margin-top: 0;
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.split-item {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
margin-bottom: 0.5rem;
|
||||
}
|
||||
|
||||
.split-user {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.5rem;
|
||||
}
|
||||
|
||||
.amount.positive {
|
||||
color: #2e7d32;
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.amount.negative {
|
||||
color: #d32f2f;
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.error {
|
||||
background-color: #ffebee;
|
||||
color: #d32f2f;
|
||||
padding: 1rem;
|
||||
border-radius: 0.5rem;
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.form-actions {
|
||||
display: flex;
|
||||
gap: 1rem;
|
||||
justify-content: flex-end;
|
||||
}
|
||||
|
||||
.btn-primary, .btn-secondary {
|
||||
padding: 0.75rem 1.5rem;
|
||||
border-radius: 0.5rem;
|
||||
font-size: 1rem;
|
||||
cursor: pointer;
|
||||
transition: all 0.2s;
|
||||
}
|
||||
|
||||
.btn-primary {
|
||||
background-color: #1976d2;
|
||||
color: white;
|
||||
border: none;
|
||||
}
|
||||
|
||||
.btn-primary:hover:not(:disabled) {
|
||||
background-color: #1565c0;
|
||||
}
|
||||
|
||||
.btn-primary:disabled {
|
||||
opacity: 0.6;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
.btn-secondary {
|
||||
background-color: #f5f5f5;
|
||||
color: #333;
|
||||
border: 1px solid #ddd;
|
||||
}
|
||||
|
||||
.btn-secondary:hover {
|
||||
background-color: #e8e8e8;
|
||||
}
|
||||
|
||||
@media (max-width: 600px) {
|
||||
.add-recurring-payment {
|
||||
padding: 1rem;
|
||||
}
|
||||
|
||||
.form-row {
|
||||
grid-template-columns: 1fr;
|
||||
}
|
||||
|
||||
.form-actions {
|
||||
flex-direction: column;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
15
src/routes/cospend/recurring/edit/[id]/+page.server.ts
Normal file
15
src/routes/cospend/recurring/edit/[id]/+page.server.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import type { PageServerLoad } from './$types';
|
||||
import { redirect } from '@sveltejs/kit';
|
||||
|
||||
export const load: PageServerLoad = async ({ locals, params }) => {
|
||||
const session = await locals.auth();
|
||||
|
||||
if (!session) {
|
||||
throw redirect(302, '/login');
|
||||
}
|
||||
|
||||
return {
|
||||
session,
|
||||
recurringPaymentId: params.id
|
||||
};
|
||||
};
|
||||
973
src/routes/cospend/recurring/edit/[id]/+page.svelte
Normal file
973
src/routes/cospend/recurring/edit/[id]/+page.svelte
Normal file
@@ -0,0 +1,973 @@
|
||||
<script>
|
||||
import { onMount } from 'svelte';
|
||||
import { goto } from '$app/navigation';
|
||||
import { getCategoryOptions } from '$lib/utils/categories';
|
||||
import { PREDEFINED_USERS, isPredefinedUsersMode } from '$lib/config/users';
|
||||
import { validateCronExpression, getFrequencyDescription, calculateNextExecutionDate } from '$lib/utils/recurring';
|
||||
import ProfilePicture from '$lib/components/ProfilePicture.svelte';
|
||||
|
||||
export let data;
|
||||
|
||||
let formData = {
|
||||
title: '',
|
||||
description: '',
|
||||
amount: '',
|
||||
paidBy: data.session?.user?.nickname || '',
|
||||
category: 'groceries',
|
||||
splitMethod: 'equal',
|
||||
splits: [],
|
||||
frequency: 'monthly',
|
||||
cronExpression: '',
|
||||
startDate: '',
|
||||
endDate: '',
|
||||
isActive: true
|
||||
};
|
||||
|
||||
let users = [];
|
||||
let newUser = '';
|
||||
let splitAmounts = {};
|
||||
let personalAmounts = {};
|
||||
let loading = false;
|
||||
let loadingPayment = true;
|
||||
let error = null;
|
||||
let personalTotalError = false;
|
||||
let predefinedMode = isPredefinedUsersMode();
|
||||
let cronError = false;
|
||||
let nextExecutionPreview = '';
|
||||
|
||||
$: categoryOptions = getCategoryOptions();
|
||||
|
||||
onMount(async () => {
|
||||
await loadRecurringPayment();
|
||||
});
|
||||
|
||||
async function loadRecurringPayment() {
|
||||
try {
|
||||
loadingPayment = true;
|
||||
const response = await fetch(`/api/cospend/recurring-payments/${data.recurringPaymentId}`);
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to load recurring payment');
|
||||
}
|
||||
|
||||
const result = await response.json();
|
||||
const payment = result.recurringPayment;
|
||||
|
||||
// Populate form data
|
||||
formData = {
|
||||
title: payment.title,
|
||||
description: payment.description || '',
|
||||
amount: payment.amount.toString(),
|
||||
paidBy: payment.paidBy,
|
||||
category: payment.category,
|
||||
splitMethod: payment.splitMethod,
|
||||
frequency: payment.frequency,
|
||||
cronExpression: payment.cronExpression || '',
|
||||
startDate: new Date(payment.startDate).toISOString().split('T')[0],
|
||||
endDate: payment.endDate ? new Date(payment.endDate).toISOString().split('T')[0] : '',
|
||||
isActive: payment.isActive
|
||||
};
|
||||
|
||||
// Set up users and splits
|
||||
users = payment.splits.map(split => split.username);
|
||||
users.forEach(user => {
|
||||
const split = payment.splits.find(s => s.username === user);
|
||||
splitAmounts[user] = split.amount;
|
||||
if (split.personalAmount !== undefined) {
|
||||
personalAmounts[user] = split.personalAmount;
|
||||
}
|
||||
});
|
||||
|
||||
updateNextExecutionPreview();
|
||||
|
||||
} catch (err) {
|
||||
error = err.message;
|
||||
} finally {
|
||||
loadingPayment = false;
|
||||
}
|
||||
}
|
||||
|
||||
function addUser() {
|
||||
if (predefinedMode) return;
|
||||
|
||||
if (newUser.trim() && !users.includes(newUser.trim())) {
|
||||
users = [...users, newUser.trim()];
|
||||
addSplitForUser(newUser.trim());
|
||||
newUser = '';
|
||||
}
|
||||
}
|
||||
|
||||
function removeUser(userToRemove) {
|
||||
if (predefinedMode) return;
|
||||
|
||||
if (users.length > 1) {
|
||||
users = users.filter(u => u !== userToRemove);
|
||||
delete splitAmounts[userToRemove];
|
||||
delete personalAmounts[userToRemove];
|
||||
splitAmounts = { ...splitAmounts };
|
||||
personalAmounts = { ...personalAmounts };
|
||||
}
|
||||
}
|
||||
|
||||
function addSplitForUser(username) {
|
||||
if (!splitAmounts[username]) {
|
||||
splitAmounts[username] = 0;
|
||||
splitAmounts = { ...splitAmounts };
|
||||
}
|
||||
}
|
||||
|
||||
function calculateEqualSplits() {
|
||||
if (!formData.amount || users.length === 0) return;
|
||||
|
||||
const amountNum = parseFloat(formData.amount);
|
||||
const splitAmount = amountNum / users.length;
|
||||
|
||||
users.forEach(user => {
|
||||
if (user === formData.paidBy) {
|
||||
splitAmounts[user] = splitAmount - amountNum;
|
||||
} else {
|
||||
splitAmounts[user] = splitAmount;
|
||||
}
|
||||
});
|
||||
splitAmounts = { ...splitAmounts };
|
||||
}
|
||||
|
||||
function calculateFullPayment() {
|
||||
if (!formData.amount) return;
|
||||
|
||||
const amountNum = parseFloat(formData.amount);
|
||||
|
||||
users.forEach(user => {
|
||||
if (user === formData.paidBy) {
|
||||
splitAmounts[user] = -amountNum;
|
||||
} else {
|
||||
splitAmounts[user] = 0;
|
||||
}
|
||||
});
|
||||
splitAmounts = { ...splitAmounts };
|
||||
}
|
||||
|
||||
function calculatePersonalEqualSplit() {
|
||||
if (!formData.amount || users.length === 0) return;
|
||||
|
||||
const totalAmount = parseFloat(formData.amount);
|
||||
|
||||
const totalPersonal = users.reduce((sum, user) => {
|
||||
return sum + (parseFloat(personalAmounts[user]) || 0);
|
||||
}, 0);
|
||||
|
||||
const remainder = Math.max(0, totalAmount - totalPersonal);
|
||||
const equalShare = remainder / users.length;
|
||||
|
||||
users.forEach(user => {
|
||||
const personalAmount = parseFloat(personalAmounts[user]) || 0;
|
||||
const totalOwed = personalAmount + equalShare;
|
||||
|
||||
if (user === formData.paidBy) {
|
||||
splitAmounts[user] = totalOwed - totalAmount;
|
||||
} else {
|
||||
splitAmounts[user] = totalOwed;
|
||||
}
|
||||
});
|
||||
splitAmounts = { ...splitAmounts };
|
||||
}
|
||||
|
||||
function handleSplitMethodChange() {
|
||||
if (formData.splitMethod === 'equal') {
|
||||
calculateEqualSplits();
|
||||
} else if (formData.splitMethod === 'full') {
|
||||
calculateFullPayment();
|
||||
} else if (formData.splitMethod === 'personal_equal') {
|
||||
calculatePersonalEqualSplit();
|
||||
}
|
||||
}
|
||||
|
||||
function validateCron() {
|
||||
if (formData.frequency !== 'custom') {
|
||||
cronError = false;
|
||||
return;
|
||||
}
|
||||
cronError = !validateCronExpression(formData.cronExpression);
|
||||
}
|
||||
|
||||
function updateNextExecutionPreview() {
|
||||
try {
|
||||
if (formData.frequency && formData.startDate) {
|
||||
const recurringPayment = {
|
||||
...formData,
|
||||
startDate: new Date(formData.startDate)
|
||||
};
|
||||
const nextDate = calculateNextExecutionDate(recurringPayment, new Date(formData.startDate));
|
||||
nextExecutionPreview = nextDate.toLocaleString('de-CH', {
|
||||
weekday: 'long',
|
||||
year: 'numeric',
|
||||
month: 'long',
|
||||
day: 'numeric',
|
||||
hour: '2-digit',
|
||||
minute: '2-digit'
|
||||
});
|
||||
}
|
||||
} catch (e) {
|
||||
nextExecutionPreview = 'Invalid configuration';
|
||||
}
|
||||
}
|
||||
|
||||
async function handleSubmit() {
|
||||
if (!formData.title.trim() || !formData.amount || parseFloat(formData.amount) <= 0) {
|
||||
error = 'Please fill in all required fields with valid values';
|
||||
return;
|
||||
}
|
||||
|
||||
if (formData.frequency === 'custom' && cronError) {
|
||||
error = 'Please enter a valid cron expression';
|
||||
return;
|
||||
}
|
||||
|
||||
if (formData.splitMethod === 'personal_equal') {
|
||||
const totalPersonal = Object.values(personalAmounts).reduce((sum, val) => sum + (parseFloat(val) || 0), 0);
|
||||
const totalAmount = parseFloat(formData.amount);
|
||||
|
||||
if (totalPersonal > totalAmount) {
|
||||
error = 'Personal amounts cannot exceed the total payment amount';
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (users.length === 0) {
|
||||
error = 'Please add at least one user to split with';
|
||||
return;
|
||||
}
|
||||
|
||||
loading = true;
|
||||
error = null;
|
||||
|
||||
try {
|
||||
const splits = users.map(user => ({
|
||||
username: user,
|
||||
amount: splitAmounts[user] || 0,
|
||||
proportion: formData.splitMethod === 'proportional' ? (splitAmounts[user] || 0) / parseFloat(formData.amount) : undefined,
|
||||
personalAmount: formData.splitMethod === 'personal_equal' ? (parseFloat(personalAmounts[user]) || 0) : undefined
|
||||
}));
|
||||
|
||||
const payload = {
|
||||
...formData,
|
||||
amount: parseFloat(formData.amount),
|
||||
startDate: formData.startDate ? new Date(formData.startDate).toISOString() : new Date().toISOString(),
|
||||
endDate: formData.endDate ? new Date(formData.endDate).toISOString() : null,
|
||||
cronExpression: formData.frequency === 'custom' ? formData.cronExpression : undefined,
|
||||
splits
|
||||
};
|
||||
|
||||
const response = await fetch(`/api/cospend/recurring-payments/${data.recurringPaymentId}`, {
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify(payload)
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const errorData = await response.json();
|
||||
throw new Error(errorData.message || 'Failed to update recurring payment');
|
||||
}
|
||||
|
||||
const result = await response.json();
|
||||
await goto('/cospend/recurring');
|
||||
|
||||
} catch (err) {
|
||||
error = err.message;
|
||||
} finally {
|
||||
loading = false;
|
||||
}
|
||||
}
|
||||
|
||||
$: if (formData.amount && formData.splitMethod && formData.paidBy && !loadingPayment) {
|
||||
handleSplitMethodChange();
|
||||
}
|
||||
|
||||
$: if (formData.splitMethod === 'personal_equal' && personalAmounts && formData.amount && !loadingPayment) {
|
||||
const totalPersonal = Object.values(personalAmounts).reduce((sum, val) => sum + (parseFloat(val) || 0), 0);
|
||||
const totalAmount = parseFloat(formData.amount);
|
||||
personalTotalError = totalPersonal > totalAmount;
|
||||
|
||||
if (!personalTotalError) {
|
||||
calculatePersonalEqualSplit();
|
||||
}
|
||||
}
|
||||
|
||||
$: if (formData.cronExpression) {
|
||||
validateCron();
|
||||
}
|
||||
|
||||
$: if (formData.frequency || formData.cronExpression || formData.startDate) {
|
||||
updateNextExecutionPreview();
|
||||
}
|
||||
</script>
|
||||
|
||||
<svelte:head>
|
||||
<title>Edit Recurring Payment - Cospend</title>
|
||||
</svelte:head>
|
||||
|
||||
<main class="edit-recurring-payment">
|
||||
<div class="header">
|
||||
<h1>Edit Recurring Payment</h1>
|
||||
<div class="header-actions">
|
||||
<a href="/cospend/recurring" class="back-link">← Back to Recurring Payments</a>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{#if loadingPayment}
|
||||
<div class="loading">Loading recurring payment...</div>
|
||||
{:else if error && !formData.title}
|
||||
<div class="error">Error: {error}</div>
|
||||
{:else}
|
||||
<form on:submit|preventDefault={handleSubmit} class="payment-form">
|
||||
<div class="form-section">
|
||||
<h2>Payment Details</h2>
|
||||
|
||||
<div class="form-group">
|
||||
<label for="title">Title *</label>
|
||||
<input
|
||||
type="text"
|
||||
id="title"
|
||||
bind:value={formData.title}
|
||||
required
|
||||
placeholder="e.g., Monthly rent, Weekly groceries"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label for="description">Description</label>
|
||||
<textarea
|
||||
id="description"
|
||||
bind:value={formData.description}
|
||||
placeholder="Additional details about this recurring payment..."
|
||||
rows="3"
|
||||
></textarea>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label for="category">Category *</label>
|
||||
<select id="category" bind:value={formData.category} required>
|
||||
{#each categoryOptions as option}
|
||||
<option value={option.value}>{option.label}</option>
|
||||
{/each}
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<div class="form-row">
|
||||
<div class="form-group">
|
||||
<label for="amount">Amount (CHF) *</label>
|
||||
<input
|
||||
type="number"
|
||||
id="amount"
|
||||
bind:value={formData.amount}
|
||||
required
|
||||
min="0"
|
||||
step="0.01"
|
||||
placeholder="0.00"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label for="paidBy">Paid by</label>
|
||||
<select id="paidBy" bind:value={formData.paidBy} required>
|
||||
{#each users as user}
|
||||
<option value={user}>{user}</option>
|
||||
{/each}
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label for="isActive">Status</label>
|
||||
<select id="isActive" bind:value={formData.isActive}>
|
||||
<option value={true}>Active</option>
|
||||
<option value={false}>Inactive</option>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="form-section">
|
||||
<h2>Recurring Schedule</h2>
|
||||
|
||||
<div class="form-row">
|
||||
<div class="form-group">
|
||||
<label for="frequency">Frequency *</label>
|
||||
<select id="frequency" bind:value={formData.frequency} required>
|
||||
<option value="daily">Daily</option>
|
||||
<option value="weekly">Weekly</option>
|
||||
<option value="monthly">Monthly</option>
|
||||
<option value="custom">Custom (Cron)</option>
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label for="startDate">Start Date *</label>
|
||||
<input
|
||||
type="date"
|
||||
id="startDate"
|
||||
bind:value={formData.startDate}
|
||||
required
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{#if formData.frequency === 'custom'}
|
||||
<div class="form-group">
|
||||
<label for="cronExpression">Cron Expression *</label>
|
||||
<input
|
||||
type="text"
|
||||
id="cronExpression"
|
||||
bind:value={formData.cronExpression}
|
||||
required
|
||||
placeholder="0 9 * * 1 (Every Monday at 9:00 AM)"
|
||||
class:error={cronError}
|
||||
/>
|
||||
<div class="help-text">
|
||||
<p>Cron format: minute hour day-of-month month day-of-week</p>
|
||||
<p>Examples:</p>
|
||||
<ul>
|
||||
<li><code>0 9 * * *</code> - Every day at 9:00 AM</li>
|
||||
<li><code>0 9 1 * *</code> - Every 1st of the month at 9:00 AM</li>
|
||||
<li><code>0 9 * * 1</code> - Every Monday at 9:00 AM</li>
|
||||
<li><code>0 9 1,15 * *</code> - 1st and 15th of every month at 9:00 AM</li>
|
||||
</ul>
|
||||
</div>
|
||||
{#if cronError}
|
||||
<div class="field-error">Invalid cron expression</div>
|
||||
{/if}
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
<div class="form-group">
|
||||
<label for="endDate">End Date (optional)</label>
|
||||
<input
|
||||
type="date"
|
||||
id="endDate"
|
||||
bind:value={formData.endDate}
|
||||
/>
|
||||
<div class="help-text">Leave blank for indefinite recurring payments</div>
|
||||
</div>
|
||||
|
||||
{#if nextExecutionPreview}
|
||||
<div class="execution-preview">
|
||||
<h3>Next Execution</h3>
|
||||
<p class="next-execution">{nextExecutionPreview}</p>
|
||||
<p class="frequency-description">{getFrequencyDescription(formData)}</p>
|
||||
</div>
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
<div class="form-section">
|
||||
<h2>Split Between Users</h2>
|
||||
|
||||
<div class="users-list">
|
||||
{#each users as user}
|
||||
<div class="user-item with-profile">
|
||||
<ProfilePicture username={user} size={32} />
|
||||
<span class="username">{user}</span>
|
||||
{#if user === data.session?.user?.nickname}
|
||||
<span class="you-badge">You</span>
|
||||
{/if}
|
||||
{#if !predefinedMode && user !== data.session?.user?.nickname}
|
||||
<button type="button" class="remove-user" on:click={() => removeUser(user)}>
|
||||
Remove
|
||||
</button>
|
||||
{/if}
|
||||
</div>
|
||||
{/each}
|
||||
</div>
|
||||
|
||||
{#if !predefinedMode}
|
||||
<div class="add-user">
|
||||
<input
|
||||
type="text"
|
||||
bind:value={newUser}
|
||||
placeholder="Add user..."
|
||||
on:keydown={(e) => e.key === 'Enter' && (e.preventDefault(), addUser())}
|
||||
/>
|
||||
<button type="button" on:click={addUser}>Add User</button>
|
||||
</div>
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
<div class="form-section">
|
||||
<h2>Split Method</h2>
|
||||
|
||||
<div class="split-method">
|
||||
<label>
|
||||
<input type="radio" bind:group={formData.splitMethod} value="equal" />
|
||||
Equal Split
|
||||
</label>
|
||||
<label>
|
||||
<input type="radio" bind:group={formData.splitMethod} value="personal_equal" />
|
||||
Personal + Equal Split
|
||||
</label>
|
||||
<label>
|
||||
<input type="radio" bind:group={formData.splitMethod} value="full" />
|
||||
Paid in Full by {formData.paidBy}
|
||||
</label>
|
||||
<label>
|
||||
<input type="radio" bind:group={formData.splitMethod} value="proportional" />
|
||||
Custom Proportions
|
||||
</label>
|
||||
</div>
|
||||
|
||||
{#if formData.splitMethod === 'proportional'}
|
||||
<div class="proportional-splits">
|
||||
<h3>Custom Split Amounts</h3>
|
||||
{#each users as user}
|
||||
<div class="split-input">
|
||||
<label>{user}</label>
|
||||
<input
|
||||
type="number"
|
||||
step="0.01"
|
||||
bind:value={splitAmounts[user]}
|
||||
placeholder="0.00"
|
||||
/>
|
||||
</div>
|
||||
{/each}
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
{#if formData.splitMethod === 'personal_equal'}
|
||||
<div class="personal-splits">
|
||||
<h3>Personal Amounts</h3>
|
||||
<p class="description">Enter personal amounts for each user. The remainder will be split equally.</p>
|
||||
{#each users as user}
|
||||
<div class="split-input">
|
||||
<label>{user}</label>
|
||||
<input
|
||||
type="number"
|
||||
step="0.01"
|
||||
min="0"
|
||||
bind:value={personalAmounts[user]}
|
||||
placeholder="0.00"
|
||||
/>
|
||||
</div>
|
||||
{/each}
|
||||
{#if formData.amount}
|
||||
<div class="remainder-info" class:error={personalTotalError}>
|
||||
<span>Total Personal: CHF {Object.values(personalAmounts).reduce((sum, val) => sum + (parseFloat(val) || 0), 0).toFixed(2)}</span>
|
||||
<span>Remainder to Split: CHF {Math.max(0, parseFloat(formData.amount) - Object.values(personalAmounts).reduce((sum, val) => sum + (parseFloat(val) || 0), 0)).toFixed(2)}</span>
|
||||
{#if personalTotalError}
|
||||
<div class="error-message">⚠️ Personal amounts exceed total payment amount!</div>
|
||||
{/if}
|
||||
</div>
|
||||
{/if}
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
{#if Object.keys(splitAmounts).length > 0}
|
||||
<div class="split-preview">
|
||||
<h3>Split Preview</h3>
|
||||
{#each users as user}
|
||||
<div class="split-item">
|
||||
<div class="split-user">
|
||||
<ProfilePicture username={user} size={24} />
|
||||
<span class="username">{user}</span>
|
||||
</div>
|
||||
<span class="amount" class:positive={splitAmounts[user] < 0} class:negative={splitAmounts[user] > 0}>
|
||||
{#if splitAmounts[user] > 0}
|
||||
owes CHF {splitAmounts[user].toFixed(2)}
|
||||
{:else if splitAmounts[user] < 0}
|
||||
is owed CHF {Math.abs(splitAmounts[user]).toFixed(2)}
|
||||
{:else}
|
||||
even
|
||||
{/if}
|
||||
</span>
|
||||
</div>
|
||||
{/each}
|
||||
</div>
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
{#if error}
|
||||
<div class="error">{error}</div>
|
||||
{/if}
|
||||
|
||||
<div class="form-actions">
|
||||
<button type="button" class="btn-secondary" on:click={() => goto('/cospend/recurring')}>
|
||||
Cancel
|
||||
</button>
|
||||
<button type="submit" class="btn-primary" disabled={loading || cronError}>
|
||||
{loading ? 'Saving...' : 'Save Changes'}
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
{/if}
|
||||
</main>
|
||||
|
||||
<style>
|
||||
.edit-recurring-payment {
|
||||
max-width: 800px;
|
||||
margin: 0 auto;
|
||||
padding: 2rem;
|
||||
}
|
||||
|
||||
.header {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
margin-bottom: 2rem;
|
||||
}
|
||||
|
||||
.header h1 {
|
||||
margin: 0;
|
||||
color: #333;
|
||||
}
|
||||
|
||||
.back-link {
|
||||
color: #1976d2;
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
.loading {
|
||||
text-align: center;
|
||||
padding: 2rem;
|
||||
font-size: 1.1rem;
|
||||
}
|
||||
|
||||
.payment-form {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 2rem;
|
||||
}
|
||||
|
||||
.form-section {
|
||||
background: white;
|
||||
padding: 1.5rem;
|
||||
border-radius: 0.75rem;
|
||||
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
|
||||
}
|
||||
|
||||
.form-section h2 {
|
||||
margin-top: 0;
|
||||
margin-bottom: 1rem;
|
||||
color: #333;
|
||||
font-size: 1.25rem;
|
||||
}
|
||||
|
||||
.form-group {
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.form-row {
|
||||
display: grid;
|
||||
grid-template-columns: 1fr 1fr;
|
||||
gap: 1rem;
|
||||
}
|
||||
|
||||
label {
|
||||
display: block;
|
||||
margin-bottom: 0.5rem;
|
||||
font-weight: 500;
|
||||
color: #555;
|
||||
}
|
||||
|
||||
input, textarea, select {
|
||||
width: 100%;
|
||||
padding: 0.75rem;
|
||||
border: 1px solid #ddd;
|
||||
border-radius: 0.5rem;
|
||||
font-size: 1rem;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
input:focus, textarea:focus, select:focus {
|
||||
outline: none;
|
||||
border-color: #1976d2;
|
||||
box-shadow: 0 0 0 2px rgba(25, 118, 210, 0.2);
|
||||
}
|
||||
|
||||
input.error {
|
||||
border-color: #d32f2f;
|
||||
}
|
||||
|
||||
.help-text {
|
||||
margin-top: 0.5rem;
|
||||
color: #666;
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
|
||||
.help-text code {
|
||||
background-color: #f5f5f5;
|
||||
padding: 0.125rem 0.25rem;
|
||||
border-radius: 0.25rem;
|
||||
font-family: monospace;
|
||||
}
|
||||
|
||||
.help-text ul {
|
||||
margin: 0.5rem 0;
|
||||
padding-left: 1rem;
|
||||
}
|
||||
|
||||
.help-text li {
|
||||
margin-bottom: 0.25rem;
|
||||
}
|
||||
|
||||
.field-error {
|
||||
color: #d32f2f;
|
||||
font-size: 0.875rem;
|
||||
margin-top: 0.25rem;
|
||||
}
|
||||
|
||||
.execution-preview {
|
||||
background-color: #e3f2fd;
|
||||
border: 1px solid #2196f3;
|
||||
border-radius: 0.5rem;
|
||||
padding: 1rem;
|
||||
margin-top: 1rem;
|
||||
}
|
||||
|
||||
.execution-preview h3 {
|
||||
margin: 0 0 0.5rem 0;
|
||||
color: #1976d2;
|
||||
font-size: 1rem;
|
||||
}
|
||||
|
||||
.next-execution {
|
||||
font-size: 1.1rem;
|
||||
font-weight: 600;
|
||||
color: #1976d2;
|
||||
margin: 0.5rem 0;
|
||||
}
|
||||
|
||||
.frequency-description {
|
||||
color: #666;
|
||||
font-size: 0.9rem;
|
||||
margin: 0;
|
||||
font-style: italic;
|
||||
}
|
||||
|
||||
.users-list {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: 0.5rem;
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.user-item {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.5rem;
|
||||
background-color: #f5f5f5;
|
||||
padding: 0.5rem 0.75rem;
|
||||
border-radius: 1rem;
|
||||
}
|
||||
|
||||
.user-item.with-profile {
|
||||
gap: 0.75rem;
|
||||
}
|
||||
|
||||
.user-item .username {
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.you-badge {
|
||||
background-color: #1976d2;
|
||||
color: white;
|
||||
padding: 0.125rem 0.5rem;
|
||||
border-radius: 1rem;
|
||||
font-size: 0.75rem;
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.remove-user {
|
||||
background-color: #d32f2f;
|
||||
color: white;
|
||||
border: none;
|
||||
padding: 0.25rem 0.5rem;
|
||||
border-radius: 0.25rem;
|
||||
font-size: 0.75rem;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.add-user {
|
||||
display: flex;
|
||||
gap: 0.5rem;
|
||||
}
|
||||
|
||||
.add-user input {
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
.add-user button {
|
||||
background-color: #1976d2;
|
||||
color: white;
|
||||
border: none;
|
||||
padding: 0.75rem 1rem;
|
||||
border-radius: 0.5rem;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.split-method {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 0.75rem;
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.split-method label {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.5rem;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.proportional-splits, .personal-splits {
|
||||
border: 1px solid #ddd;
|
||||
border-radius: 0.5rem;
|
||||
padding: 1rem;
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.proportional-splits h3, .personal-splits h3 {
|
||||
margin-top: 0;
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.personal-splits .description {
|
||||
color: #666;
|
||||
font-size: 0.9rem;
|
||||
margin-bottom: 1rem;
|
||||
font-style: italic;
|
||||
}
|
||||
|
||||
.split-input {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 1rem;
|
||||
margin-bottom: 0.5rem;
|
||||
}
|
||||
|
||||
.split-input label {
|
||||
min-width: 100px;
|
||||
margin-bottom: 0;
|
||||
}
|
||||
|
||||
.split-input input {
|
||||
max-width: 120px;
|
||||
}
|
||||
|
||||
.remainder-info {
|
||||
margin-top: 1rem;
|
||||
padding: 1rem;
|
||||
background-color: #f8f9fa;
|
||||
border-radius: 0.5rem;
|
||||
border: 1px solid #e9ecef;
|
||||
}
|
||||
|
||||
.remainder-info.error {
|
||||
background-color: #fff5f5;
|
||||
border-color: #fed7d7;
|
||||
}
|
||||
|
||||
.remainder-info span {
|
||||
display: block;
|
||||
margin-bottom: 0.5rem;
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.error-message {
|
||||
color: #d32f2f;
|
||||
font-weight: 600;
|
||||
margin-top: 0.5rem;
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
|
||||
.split-preview {
|
||||
background-color: #f8f9fa;
|
||||
padding: 1rem;
|
||||
border-radius: 0.5rem;
|
||||
}
|
||||
|
||||
.split-preview h3 {
|
||||
margin-top: 0;
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.split-item {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
margin-bottom: 0.5rem;
|
||||
}
|
||||
|
||||
.split-user {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.5rem;
|
||||
}
|
||||
|
||||
.amount.positive {
|
||||
color: #2e7d32;
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.amount.negative {
|
||||
color: #d32f2f;
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.error {
|
||||
background-color: #ffebee;
|
||||
color: #d32f2f;
|
||||
padding: 1rem;
|
||||
border-radius: 0.5rem;
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.form-actions {
|
||||
display: flex;
|
||||
gap: 1rem;
|
||||
justify-content: flex-end;
|
||||
}
|
||||
|
||||
.btn-primary, .btn-secondary {
|
||||
padding: 0.75rem 1.5rem;
|
||||
border-radius: 0.5rem;
|
||||
font-size: 1rem;
|
||||
cursor: pointer;
|
||||
transition: all 0.2s;
|
||||
}
|
||||
|
||||
.btn-primary {
|
||||
background-color: #1976d2;
|
||||
color: white;
|
||||
border: none;
|
||||
}
|
||||
|
||||
.btn-primary:hover:not(:disabled) {
|
||||
background-color: #1565c0;
|
||||
}
|
||||
|
||||
.btn-primary:disabled {
|
||||
opacity: 0.6;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
.btn-secondary {
|
||||
background-color: #f5f5f5;
|
||||
color: #333;
|
||||
border: 1px solid #ddd;
|
||||
}
|
||||
|
||||
.btn-secondary:hover {
|
||||
background-color: #e8e8e8;
|
||||
}
|
||||
|
||||
@media (max-width: 600px) {
|
||||
.edit-recurring-payment {
|
||||
padding: 1rem;
|
||||
}
|
||||
|
||||
.form-row {
|
||||
grid-template-columns: 1fr;
|
||||
}
|
||||
|
||||
.form-actions {
|
||||
flex-direction: column;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user