Skip to content

Commit 07cb687

Browse files
committed
feat: add filter by fiscal year to Expense Claim Summary in PWA
1 parent 1a2ece2 commit 07cb687

File tree

3 files changed

+110
-24
lines changed

3 files changed

+110
-24
lines changed

frontend/src/components/ExpenseClaimSummary.vue

+50-19
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@
2323
</div>
2424
<span class="text-gray-800 text-base font-semibold leading-6">
2525
{{
26-
formatCurrency(
27-
summary.data?.total_pending_amount,
28-
company_currency
29-
)
30-
}}
26+
formatCurrency(
27+
summary.data?.total_pending_amount,
28+
company_currency
29+
)
30+
}}
3131
</span>
3232
</div>
3333
<div class="flex flex-col gap-1">
@@ -39,11 +39,11 @@
3939
</div>
4040
<span class="text-gray-800 text-base font-semibold leading-6">
4141
{{
42-
formatCurrency(
43-
summary.data?.total_approved_amount,
44-
company_currency
45-
)
46-
}}
42+
formatCurrency(
43+
summary.data?.total_approved_amount,
44+
company_currency
45+
)
46+
}}
4747
</span>
4848
</div>
4949

@@ -56,25 +56,37 @@
5656
</div>
5757
<span class="text-gray-800 text-base font-semibold leading-6">
5858
{{
59-
formatCurrency(
60-
summary.data?.total_rejected_amount,
61-
company_currency
62-
)
63-
}}
59+
formatCurrency(
60+
summary.data?.total_rejected_amount,
61+
company_currency
62+
)
63+
}}
6464
</span>
6565
</div>
6666
</div>
67+
<div class="flex flex-col gap-1.5">
68+
<span class="text-gray-600 text-base font-medium leading-5"> Fiscal Year </span>
69+
<Autocomplete
70+
label="Select Fiscal Year"
71+
class="w-full"
72+
placeholder="Select Fiscal Year"
73+
:options="fiscalYears.data.fiscal_years"
74+
v-model="selectedPeriod"
75+
/>
76+
</div>
6777
</div>
6878
</div>
6979
</template>
7080

7181
<script setup>
72-
import { FeatherIcon } from "frappe-ui"
73-
import { computed } from "vue"
82+
import { FeatherIcon, Autocomplete } from "frappe-ui"
83+
import { computed, ref, watch, inject, onMounted } from "vue"
84+
import { formatCurrency } from "@/utils/formatters"
85+
import { expenseClaimSummary as summary, fiscalYears } from "@/data/claims"
7486
75-
import { expenseClaimSummary as summary } from "@/data/claims"
87+
const dayjs = inject("$dayjs")
7688
77-
import { formatCurrency } from "@/utils/formatters"
89+
let selectedPeriod = ref({})
7890
7991
const total_claimed_amount = computed(() => {
8092
return (
@@ -84,5 +96,24 @@ const total_claimed_amount = computed(() => {
8496
)
8597
})
8698
99+
const fetchExpenseClaimSummary = (selectedPeriod) => {
100+
const year_start_date = selectedPeriod && selectedPeriod.year_start_date
101+
const year_end_date = selectedPeriod && selectedPeriod.year_end_date
102+
summary.reload({ year_start_date, year_end_date })
103+
}
104+
105+
watch(
106+
() => selectedPeriod.value,
107+
(newValue) => {
108+
return fetchExpenseClaimSummary(newValue)
109+
},
110+
{ deep: true }
111+
)
112+
87113
const company_currency = computed(() => summary.data?.currency)
114+
115+
onMounted(() => {
116+
selectedPeriod.value = fiscalYears.data.current_fiscal_year
117+
fetchExpenseClaimSummary(selectedPeriod.value)
118+
})
88119
</script>

frontend/src/data/claims.js

+38-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,50 @@
11
import { createResource } from "frappe-ui"
22
import { employeeResource } from "./employee"
33
import { reactive } from "vue"
4+
import dayjs from "@/utils/dayjs"
45

56
export const expenseClaimSummary = createResource({
67
url: "hrms.api.get_expense_claim_summary",
8+
makeParams(params) {
9+
return {
10+
employee: employeeResource.data.name,
11+
start_date: params ? params.year_start_date : null,
12+
end_date: params ? params.year_end_date : null,
13+
}
14+
},
15+
auto: true,
16+
})
17+
18+
function getPeriodLabel(period) {
19+
return `${dayjs(period?.year_start_date).format("MMM YYYY")} - ${dayjs(
20+
period?.year_end_date
21+
).format("MMM YYYY")}`
22+
}
23+
24+
const add_options = (period) => {
25+
return {
26+
...period,
27+
value: getPeriodLabel(period),
28+
label: getPeriodLabel(period),
29+
}
30+
}
31+
32+
export const fiscalYears = createResource({
33+
url: "hrms.api.get_fiscal_years_for_company",
734
params: {
8-
employee: employeeResource.data.name,
35+
company: employeeResource.data?.company,
36+
current_date: dayjs().format("YYYY-MM-DD"),
937
},
1038
auto: true,
11-
cache: "hrms:expense_claim_summary",
39+
transform: (data) => {
40+
const newdata = {
41+
current_fiscal_year: add_options(data.current_fiscal_year),
42+
fiscal_years: data.fiscal_years.map((period) => {
43+
return add_options(period)
44+
}),
45+
}
46+
return newdata
47+
},
1248
})
1349

1450
const transformClaimData = (data) => {

hrms/api/__init__.py

+22-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
from frappe.utils import getdate
66
from frappe.utils.data import cint
77

8+
from erpnext.accounts.utils import get_fiscal_year, get_fiscal_years
9+
810
SUPPORTED_FIELD_TYPES = [
911
"Link",
1012
"Select",
@@ -355,9 +357,14 @@ def get_expense_claim_filters(
355357

356358

357359
@frappe.whitelist()
358-
def get_expense_claim_summary(employee: str) -> dict:
360+
def get_expense_claim_summary(
361+
employee: str, start_date: str | None = None, end_date: str | None = None
362+
) -> dict:
359363
from frappe.query_builder.functions import Sum
360364

365+
start_date = frappe.utils.data.get_datetime(start_date)
366+
end_date = frappe.utils.data.get_datetime(end_date)
367+
361368
Claim = frappe.qb.DocType("Expense Claim")
362369

363370
pending_claims_case = (
@@ -379,7 +386,7 @@ def get_expense_claim_summary(employee: str) -> dict:
379386
)
380387
sum_rejected_claims = Sum(rejected_claims_case).as_("total_rejected_amount")
381388

382-
summary = (
389+
query = (
383390
frappe.qb.from_(Claim)
384391
.select(
385392
sum_pending_claims,
@@ -388,8 +395,12 @@ def get_expense_claim_summary(employee: str) -> dict:
388395
Claim.company,
389396
)
390397
.where((Claim.docstatus != 2) & (Claim.employee == employee))
391-
).run(as_dict=True)[0]
398+
)
392399

400+
if start_date and end_date:
401+
query = query.where((Claim.posting_date >= start_date) & (Claim.posting_date <= end_date))
402+
403+
summary = query.run(as_dict=True)[0]
393404
currency = frappe.db.get_value("Company", summary.company, "default_currency")
394405
summary["currency"] = currency
395406

@@ -632,3 +643,11 @@ def get_allowed_states_for_workflow(workflow: dict, user_id: str) -> list[str]:
632643
@frappe.whitelist()
633644
def is_employee_checkin_allowed():
634645
return cint(frappe.db.get_single_value("HR Settings", "allow_employee_checkin_from_mobile_app"))
646+
647+
648+
@frappe.whitelist()
649+
def get_fiscal_years_for_company(company):
650+
today = getdate()
651+
fiscal_years = get_fiscal_years(company=company, as_dict=True)
652+
current_fiscal_year = get_fiscal_year(company=company, date=today, as_dict=True)
653+
return {"fiscal_years": fiscal_years, "current_fiscal_year": current_fiscal_year}

0 commit comments

Comments
 (0)