Add option to filter snacks from historical transactions

This commit is contained in:
Tanner Collin 2023-05-03 18:57:48 +00:00
parent e1d4de0ea2
commit 30a820f302
3 changed files with 53 additions and 22 deletions

View File

@ -499,8 +499,11 @@ class TransactionViewSet(Base, List, Create, Retrieve, Update):
def get_queryset(self): def get_queryset(self):
queryset = models.Transaction.objects queryset = models.Transaction.objects
month = self.request.query_params.get('month', '') month = self.request.query_params.get('month', '')
exclude_paypal = self.request.query_params.get('exclude_paypal', '') == 'true'
exclude_snacks = self.request.query_params.get('exclude_snacks', '') == 'true'
if self.action == 'list' and month: if self.action == 'list':
if month:
try: try:
dt = datetime.datetime.strptime(month, '%Y-%m') dt = datetime.datetime.strptime(month, '%Y-%m')
except ValueError: except ValueError:
@ -508,10 +511,15 @@ class TransactionViewSet(Base, List, Create, Retrieve, Update):
queryset = queryset.filter(date__year=dt.year) queryset = queryset.filter(date__year=dt.year)
queryset = queryset.filter(date__month=dt.month) queryset = queryset.filter(date__month=dt.month)
queryset = queryset.exclude(category='Memberships:Fake Months') queryset = queryset.exclude(category='Memberships:Fake Months')
return queryset.order_by('-date', '-id') else:
elif self.action == 'list':
queryset = queryset.exclude(report_type__isnull=True) queryset = queryset.exclude(report_type__isnull=True)
queryset = queryset.exclude(report_type='') queryset = queryset.exclude(report_type='')
if exclude_paypal:
queryset = queryset.exclude(account_type='PayPal')
if exclude_snacks:
queryset = queryset.exclude(category='Snacks')
return queryset.order_by('-date', '-id') return queryset.order_by('-date', '-id')
else: else:
return queryset.all() return queryset.all()

View File

@ -43,29 +43,24 @@ export function AdminReportedTransactions(props) {
let transactionsCache = false; let transactionsCache = false;
let summaryCache = false; let summaryCache = false;
let excludePayPalCache = false;
export function AdminHistoricalTransactions(props) { export function AdminHistoricalTransactions(props) {
const { token } = props; const { token } = props;
const [input, setInput] = useState({ month: moment() }); const [input, setInput] = useState({ month: moment() });
const [transactions, setTransactions] = useState(transactionsCache); const [transactions, setTransactions] = useState(transactionsCache);
const [summary, setSummary] = useState(summaryCache); const [summary, setSummary] = useState(summaryCache);
const [excludePayPal, setExcludePayPal] = useState(excludePayPalCache); const [excludePayPal, setExcludePayPal] = useState(false);
const [excludeSnacks, setExcludeSnacks] = useState(true);
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
const [error, setError] = useState(false); const [error, setError] = useState(false);
const handleDatetime = (v) => setInput({ ...input, month: v }); const handleDatetime = (v) => setInput({ ...input, month: v });
const handleExcludePayPal = (e, v) => { const makeRequest = () => {
setExcludePayPal(v.checked);
excludePayPalCache = v.checked;
};
const handleSubmit = (e) => {
if (loading) return; if (loading) return;
setLoading(true); setLoading(true);
const month = input.month.format('YYYY-MM'); const month = input.month.format('YYYY-MM');
requester('/transactions/?month=' + month, 'GET', token) requester('/transactions/?month=' + month + '&exclude_paypal=' + excludePayPal + '&exclude_snacks=' + excludeSnacks, 'GET', token)
.then(res => { .then(res => {
setLoading(false); setLoading(false);
setError(false); setError(false);
@ -92,6 +87,22 @@ export function AdminHistoricalTransactions(props) {
}); });
}; };
const handleSubmit = (e) => {
makeRequest();
};
const handleExcludePayPal = (e, v) => {
setExcludePayPal(v.checked);
};
const handleExcludeSnacks = (e, v) => {
setExcludeSnacks(v.checked);
};
useEffect(() => {
makeRequest();
}, [excludePayPal, excludeSnacks]);
return ( return (
<div> <div>
<Form onSubmit={handleSubmit}> <Form onSubmit={handleSubmit}>
@ -150,12 +161,20 @@ export function AdminHistoricalTransactions(props) {
} }
<Checkbox <Checkbox
className='filter-option'
label='Exclude PayPal' label='Exclude PayPal'
onChange={handleExcludePayPal} onChange={handleExcludePayPal}
checked={excludePayPal} checked={excludePayPal}
/> />
<TransactionList transactions={transactions.filter(x => !excludePayPal || x.account_type !== 'PayPal')} /> <Checkbox
className='filter-option'
label='Exclude Snacks'
onChange={handleExcludeSnacks}
checked={excludeSnacks}
/>
<TransactionList transactions={transactions} />
</div> </div>
: :
<p>Error loading transactions.</p> <p>Error loading transactions.</p>

View File

@ -236,6 +236,10 @@ body {
text-overflow: ellipsis; text-overflow: ellipsis;
} }
.filter-option {
margin-right: 1rem;
}
.footer { .footer {
margin-top: -20rem; margin-top: -20rem;