Add option to filter snacks from historical transactions
This commit is contained in:
parent
e1d4de0ea2
commit
30a820f302
|
@ -499,19 +499,27 @@ 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':
|
||||||
try:
|
if month:
|
||||||
dt = datetime.datetime.strptime(month, '%Y-%m')
|
try:
|
||||||
except ValueError:
|
dt = datetime.datetime.strptime(month, '%Y-%m')
|
||||||
raise exceptions.ValidationError(dict(month='Should be YYYY-MM.'))
|
except ValueError:
|
||||||
queryset = queryset.filter(date__year=dt.year)
|
raise exceptions.ValidationError(dict(month='Should be YYYY-MM.'))
|
||||||
queryset = queryset.filter(date__month=dt.month)
|
queryset = queryset.filter(date__year=dt.year)
|
||||||
queryset = queryset.exclude(category='Memberships:Fake Months')
|
queryset = queryset.filter(date__month=dt.month)
|
||||||
return queryset.order_by('-date', '-id')
|
queryset = queryset.exclude(category='Memberships:Fake Months')
|
||||||
elif self.action == 'list':
|
else:
|
||||||
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()
|
||||||
|
|
|
@ -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>
|
||||||
|
|
|
@ -236,6 +236,10 @@ body {
|
||||||
text-overflow: ellipsis;
|
text-overflow: ellipsis;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.filter-option {
|
||||||
|
margin-right: 1rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
.footer {
|
.footer {
|
||||||
margin-top: -20rem;
|
margin-top: -20rem;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user