Skip to content
This repository was archived by the owner on May 18, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 121 additions & 12 deletions client/src/views/reports/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import {
CSpinner,
CRow,
CCol,
CDropdown,
CDropdownToggle,
CDropdownMenu,
CDropdownItem,
} from '@coreui/react'
import { useNavigate } from 'react-router-dom'
import { Helmet } from 'react-helmet'
Expand All @@ -25,6 +29,7 @@ const Reports = () => {
endDate: '',
startDate: '',
status: '',
invoiceStatus: '',
type: '',
query: '',
weight: '',
Expand All @@ -41,19 +46,31 @@ const Reports = () => {
axios
.post(`/freight/deep-search`, { page, ...filters, export_type })
.then((response) => {
if (response.headers['content-type'] === 'text/csv') {
const blob = new Blob([response.data], { type: 'text/csv' })
const contentType = response.headers['content-type']
if (
[
'text/csv',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
].includes(contentType)
) {
const blob = new Blob([response.data], { type: contentType })
const url = window.URL.createObjectURL(blob)
const a = document.createElement('a')
a.href = url
a.download = 'freight_report' + Date.now() + '.csv'
a.download =
new Date().toDateString() +
' Freight Report' +
'.' +
(contentType === 'text/csv' ? 'csv' : 'xlsx')
document.body.appendChild(a)
a.click()
document.body.removeChild(a)
return
}
const filteredData = filters.type
? response.data.data.filter((item) => item.invoice?.status === filters.type)
const filteredData = filters.invoiceStatus
? response.data.data.filter(
(item) => item.invoice?.status === filters.invoiceStatus,
)
: response.data.data
setData(filteredData)
setTotalPages(Math.ceil(filteredData.length / response.data.pageSize))
Expand Down Expand Up @@ -86,6 +103,28 @@ const Reports = () => {
return 'To Pay'
}

const uniqueCustomers = () => {
const ids = new Set()
for (let i = 0; i < data.length; i++) {
const item = data[i]
if (item.user) {
ids.add(item.user.first_name + ' ' + item.user.last_name)
}
}
return ids.size
}

const hasFilters = () => {
return (
(filters.startDate ||
filters.endDate ||
filters.status ||
filters.type ||
filters.query ||
filters.weight) !== ''
)
}

return (
<div>
<Helmet>
Expand Down Expand Up @@ -123,25 +162,37 @@ const Reports = () => {
handleFilterChange('endDate', endDate)
}}
/>
<CFormSelect onChange={(e) => handleFilterChange('status', e.target.value)}>
<CFormSelect
style={{ minWidth: '140px' }}
onChange={(e) => handleFilterChange('status', e.target.value)}
>
<option value="">All</option>
<option value="to_pay">To Pay</option>
<option value="to_ship">To Ship</option>
<option value="to_receive">To Receive</option>
<option value="received">Received</option>
<option value="cancelled">Cancelled</option>
</CFormSelect>
<CFormSelect onChange={(e) => handleFilterChange('type', e.target.value)}>
<CFormSelect
style={{ minWidth: '120px' }}
onChange={(e) => handleFilterChange('type', e.target.value)}
>
<option value="">All</option>
<option value="private">Private</option>
<option value="business">Business</option>
</CFormSelect>
<CFormSelect onChange={(e) => handleFilterChange('type', e.target.value)}>
<CFormSelect
style={{ minWidth: '110px' }}
onChange={(e) => handleFilterChange('invoiceStatus', e.target.value)}
>
<option value="">All</option>
<option value="PAID">Paid</option>
<option value="EXPIRED">Expired</option>
</CFormSelect>
<CFormSelect onChange={(e) => handleFilterChange('weight', e.target.value)}>
<CFormSelect
style={{ minWidth: '100px' }}
onChange={(e) => handleFilterChange('weight', e.target.value)}
>
<option value="">All</option>
<option value="1">&lt; 1 KG</option>
<option value="5">&lt; 5KG </option>
Expand All @@ -153,16 +204,74 @@ const Reports = () => {
<option value="71">&gt; 71KG </option>
</CFormSelect>
<CFormInput
style={{ minWidth: '200px' }}
placeholder="Tracking number"
onChange={(e) => handleFilterChange('query', e.target.value)}
/>
<CButton color="primary" onClick={(e) => fetchData(currentPage)}>
Apply Filters
</CButton>
<CButton color="secondary" onClick={(e) => fetchData(currentPage, 'csv')}>
Export
</CButton>
</div>
<h4 className="text-muted">Summary</h4>
<div className="d-block d-md-flex justify-content-between mb-2">
<div>
<h5>
Shipments: {data.length} &#183;{' '}
{['super_admin', 'admin'].includes(user.role) && (
<>Customers: {uniqueCustomers()}</>
)}{' '}
&#183; Amount:{' '}
{new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'PHP',
}).format(data.reduce((acc, item) => acc + item.amount.value, 0))}{' '}
&#183; Weight: {data.reduce((acc, item) => acc + item.total_weight, 0)} KG
</h5>
</div>
<div>
<CButton
className="me-2"
color="primary"
onClick={() => {
setFilters({
endDate: '',
startDate: '',
status: '',
invoiceStatus: '',
type: '',
query: '',
weight: '',
})
fetchData(currentPage)
}}
disabled={!hasFilters()}
>
Clear Filters
</CButton>
<CDropdown className="bg-primary">
<CDropdownToggle caret={false}>Export</CDropdownToggle>
<CDropdownMenu>
<CDropdownItem
className="d-flex align-items-center"
as="button"
type="button"
onClick={(e) => fetchData(currentPage, 'csv')}
>
CSV
</CDropdownItem>
<CDropdownItem
className="d-flex align-items-center"
as="button"
type="button"
onClick={(e) => fetchData(currentPage, 'excel')}
>
Excel
</CDropdownItem>
</CDropdownMenu>
</CDropdown>
</div>
</div>

<CTable stripedColumns hover responsive className="table-even-width">
<CTableHead>
<CTableRow>
Expand Down
92 changes: 92 additions & 0 deletions server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"ua-parser-js": "^1.0.39",
"useragent": "^2.3.0",
"xendit-node": "^6.0.0",
"xlsx": "^0.18.5",
"zxcvbn": "^4.4.2"
},
"devDependencies": {
Expand Down
Loading