fix: remittance — correct unremitted endpoint, send paymentIds in submit, fix channel field
This commit is contained in:
@@ -14,7 +14,7 @@ export default function RemittancesScreen() {
|
||||
const [remittancesQ, unremittedQ] = useQueries({
|
||||
queries: [
|
||||
{ queryKey: ['remittances'], queryFn: () => api.get('/api/v1/remittances?limit=50').then(r => r.data?.data ?? r.data) },
|
||||
{ queryKey: ['unremitted'], queryFn: () => api.get('/api/v1/payments?unremitted=true').then(r => r.data).catch(() => null) },
|
||||
{ queryKey: ['unremitted'], queryFn: () => api.get('/api/v1/payments/unremitted').then(r => r.data).catch(() => []) },
|
||||
],
|
||||
});
|
||||
|
||||
@@ -24,14 +24,9 @@ export default function RemittancesScreen() {
|
||||
const refetchAll = () => { remittancesQ.refetch(); unremittedQ.refetch(); };
|
||||
|
||||
// Compute unremitted total from raw payments or summary
|
||||
const unremittedPayments: any[] = Array.isArray(unremittedQ.data?.data)
|
||||
? unremittedQ.data.data
|
||||
: Array.isArray(unremittedQ.data)
|
||||
? unremittedQ.data
|
||||
: [];
|
||||
const unremittedTotal = unremittedQ.data?.totalUnremitted
|
||||
?? unremittedPayments.reduce((s: number, p: any) => s + Number(p.amount ?? 0), 0);
|
||||
const unremittedCount = unremittedQ.data?.count ?? unremittedPayments.length;
|
||||
const unremittedPayments: any[] = Array.isArray(unremittedQ.data) ? unremittedQ.data : [];
|
||||
const unremittedTotal = unremittedPayments.reduce((s: number, p: any) => s + Number(p.amount ?? 0), 0);
|
||||
const unremittedCount = unremittedPayments.length;
|
||||
|
||||
return (
|
||||
<SafeAreaView style={{ flex: 1, backgroundColor: '#0891B2' }} edges={['top']}>
|
||||
|
||||
@@ -9,25 +9,21 @@ export default function SubmitRemittanceScreen() {
|
||||
const [notes, setNotes] = useState('');
|
||||
const [loading, setLoading] = useState(false);
|
||||
|
||||
// Fetch unremitted payments to auto-fill amount
|
||||
// Fetch unremitted payments — correct endpoint is GET /api/v1/payments/unremitted
|
||||
const { data: unremittedData, isLoading: loadingUnremitted } = useQuery({
|
||||
queryKey: ['unremitted'],
|
||||
queryFn: () => api.get('/api/v1/payments?unremitted=true').then(r => r.data).catch(() => null),
|
||||
queryFn: () => api.get('/api/v1/payments/unremitted').then(r => r.data).catch(() => []),
|
||||
});
|
||||
|
||||
const unremittedPayments: any[] = Array.isArray(unremittedData?.data)
|
||||
? unremittedData.data
|
||||
: Array.isArray(unremittedData)
|
||||
? unremittedData
|
||||
: [];
|
||||
const totalAmount = unremittedData?.totalUnremitted
|
||||
?? unremittedPayments.reduce((s: number, p: any) => s + Number(p.amount ?? 0), 0);
|
||||
const unremittedPayments: any[] = Array.isArray(unremittedData) ? unremittedData : [];
|
||||
const totalAmount = unremittedPayments.reduce((s: number, p: any) => s + Number(p.amount ?? 0), 0);
|
||||
|
||||
const submit = async () => {
|
||||
if (totalAmount <= 0) return Alert.alert('Nothing to Submit', 'You have no unremitted payments to submit.');
|
||||
setLoading(true);
|
||||
try {
|
||||
await api.post('/api/v1/remittances', { totalAmount: Number(totalAmount), notes: notes.trim() || undefined });
|
||||
const paymentIds = unremittedPayments.map((p: any) => p.id);
|
||||
await api.post('/api/v1/remittances', { paymentIds, notes: notes.trim() || undefined });
|
||||
Alert.alert('Submitted!', `₱${Number(totalAmount).toLocaleString()} remittance submitted.`, [
|
||||
{ text: 'OK', onPress: () => router.back() },
|
||||
]);
|
||||
@@ -77,7 +73,7 @@ export default function SubmitRemittanceScreen() {
|
||||
<View key={p.id} style={{ backgroundColor: '#FFF', borderRadius: 14, padding: 16, marginBottom: 8, flexDirection: 'row', justifyContent: 'space-between', borderWidth: 1, borderColor: '#F1F5F9' }}>
|
||||
<View>
|
||||
<Text style={{ fontSize: 16, fontWeight: '600', color: '#0F172A' }}>{p.client?.firstName} {p.client?.lastName}</Text>
|
||||
<Text style={{ fontSize: 14, color: '#64748B', marginTop: 2 }}>{p.paymentMethod} · {p.client?.accountNumber}</Text>
|
||||
<Text style={{ fontSize: 14, color: '#64748B', marginTop: 2 }}>{p.channel} · {p.client?.accountNumber}</Text>
|
||||
</View>
|
||||
<Text style={{ fontSize: 17, fontWeight: '800', color: '#166534' }}>₱{Number(p.amount).toLocaleString()}</Text>
|
||||
</View>
|
||||
|
||||
Reference in New Issue
Block a user