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