Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add filter by fiscal year to Expense Claim Summary in PWA #1787

Closed
Closed
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
39 changes: 35 additions & 4 deletions frontend/src/components/ExpenseClaimSummary.vue
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,29 @@
</span>
</div>
</div>
<div class="flex flex-col gap-1.5">
<span class="text-gray-600 text-base font-medium leading-5"> Fiscal Year </span>
<Autocomplete
label="Select Fiscal Year"
class="w-full"
placeholder="Select Fiscal Year"
:options="fiscalYears.data.fiscal_years"
v-model="selectedPeriod"
/>
</div>
</div>
</div>
</template>

<script setup>
import { FeatherIcon } from "frappe-ui"
import { computed } from "vue"
import { FeatherIcon, Autocomplete } from "frappe-ui"
import { computed, ref, watch, inject, onMounted } from "vue"
import { formatCurrency } from "@/utils/formatters"
import { expenseClaimSummary as summary, fiscalYears } from "@/data/claims"

import { expenseClaimSummary as summary } from "@/data/claims"
const dayjs = inject("$dayjs")

import { formatCurrency } from "@/utils/formatters"
let selectedPeriod = ref({})

const total_claimed_amount = computed(() => {
return (
Expand All @@ -84,5 +96,24 @@ const total_claimed_amount = computed(() => {
)
})

const fetchExpenseClaimSummary = (selectedPeriod) => {
const year_start_date = selectedPeriod && selectedPeriod.year_start_date
const year_end_date = selectedPeriod && selectedPeriod.year_end_date
summary.reload({ year_start_date, year_end_date })
}

watch(
() => selectedPeriod.value,
(newValue) => {
return fetchExpenseClaimSummary(newValue)
},
{ deep: true }
)

const company_currency = computed(() => summary.data?.currency)

onMounted(() => {
selectedPeriod.value = fiscalYears.data.current_fiscal_year
fetchExpenseClaimSummary(selectedPeriod.value)
})
</script>
40 changes: 38 additions & 2 deletions frontend/src/data/claims.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,50 @@
import { createResource } from "frappe-ui"
import { employeeResource } from "./employee"
import { reactive } from "vue"
import dayjs from "@/utils/dayjs"

export const expenseClaimSummary = createResource({
url: "hrms.api.get_expense_claim_summary",
makeParams(params) {
return {
employee: employeeResource.data.name,
start_date: params ? params.year_start_date : null,
end_date: params ? params.year_end_date : null,
}
},
auto: true,
})

function getPeriodLabel(period) {
return `${dayjs(period?.year_start_date).format("MMM YYYY")} - ${dayjs(
period?.year_end_date
).format("MMM YYYY")}`
}

const add_options = (period) => {
return {
...period,
value: getPeriodLabel(period),
label: getPeriodLabel(period),
}
}

export const fiscalYears = createResource({
url: "hrms.api.get_fiscal_years_for_company",
params: {
employee: employeeResource.data.name,
company: employeeResource.data?.company,
current_date: dayjs().format("YYYY-MM-DD"),
},
auto: true,
cache: "hrms:expense_claim_summary",
transform: (data) => {
const newdata = {
current_fiscal_year: add_options(data.current_fiscal_year),
fiscal_years: data.fiscal_years.map((period) => {
return add_options(period)
}),
}
return newdata
},
})

const transformClaimData = (data) => {
Expand Down
30 changes: 27 additions & 3 deletions hrms/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from frappe.query_builder import Order
from frappe.utils import getdate, strip_html

from erpnext.accounts.utils import get_fiscal_year, get_fiscal_years

SUPPORTED_FIELD_TYPES = [
"Link",
"Select",
Expand Down Expand Up @@ -369,9 +371,14 @@ def get_expense_claim_filters(


@frappe.whitelist()
def get_expense_claim_summary(employee: str) -> dict:
def get_expense_claim_summary(
employee: str, start_date: str | None = None, end_date: str | None = None
) -> dict:
from frappe.query_builder.functions import Sum

start_date = frappe.utils.data.get_datetime(start_date)
end_date = frappe.utils.data.get_datetime(end_date)

Claim = frappe.qb.DocType("Expense Claim")

pending_claims_case = (
Expand All @@ -393,7 +400,7 @@ def get_expense_claim_summary(employee: str) -> dict:
)
sum_rejected_claims = Sum(rejected_claims_case).as_("total_rejected_amount")

summary = (
query = (
frappe.qb.from_(Claim)
.select(
sum_pending_claims,
Expand All @@ -402,8 +409,12 @@ def get_expense_claim_summary(employee: str) -> dict:
Claim.company,
)
.where((Claim.docstatus != 2) & (Claim.employee == employee))
).run(as_dict=True)[0]
)

if start_date and end_date:
query = query.where((Claim.posting_date >= start_date) & (Claim.posting_date <= end_date))

summary = query.run(as_dict=True)[0]
currency = frappe.db.get_value("Company", summary.company, "default_currency")
summary["currency"] = currency

Expand Down Expand Up @@ -641,3 +652,16 @@ def get_workflow_state_field(doctype: str) -> str | None:
def get_allowed_states_for_workflow(workflow: dict, user_id: str) -> list[str]:
user_roles = frappe.get_roles(user_id)
return [transition.state for transition in workflow.transitions if transition.allowed in user_roles]


@frappe.whitelist()
def is_employee_checkin_allowed():
return cint(frappe.db.get_single_value("HR Settings", "allow_employee_checkin_from_mobile_app"))


@frappe.whitelist()
def get_fiscal_years_for_company(company):
today = getdate()
fiscal_years = get_fiscal_years(company=company, as_dict=True)
current_fiscal_year = get_fiscal_year(company=company, date=today, as_dict=True)
return {"fiscal_years": fiscal_years, "current_fiscal_year": current_fiscal_year}
Loading