Skip to content

Commit

Permalink
Merge pull request #2395 from frappe/roster-default-company
Browse files Browse the repository at this point in the history
  • Loading branch information
krantheman authored Nov 7, 2024
2 parents d043e05 + 56973da commit 11f9482
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
5 changes: 5 additions & 0 deletions hrms/api/roster.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
from hrms.hr.doctype.shift_assignment_tool.shift_assignment_tool import create_shift_assignment


@frappe.whitelist()
def get_default_company() -> str:
return frappe.defaults.get_user_default("Company")


@frappe.whitelist()
def get_values(doctype: str, name: str, fields: list) -> dict[str, str]:
return frappe.db.get_value(doctype, name, fields, as_dict=True)
Expand Down
22 changes: 15 additions & 7 deletions roster/src/components/MonthViewHeader.vue
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

<script setup lang="ts">
import { ref, reactive, watch } from "vue";
import { FormControl, createListResource } from "frappe-ui";
import { FormControl, createResource, createListResource } from "frappe-ui";
import { Dayjs } from "dayjs";
import { raiseToast } from "../utils";
Expand Down Expand Up @@ -72,7 +72,7 @@ const filters: {
watch(
() => filters.company.model,
(val) => {
if (val?.value) return getFilterOptions("department", { company: val.value });
if (val?.value) getFilterOptions("department", { company: val.value });
else {
filters.department.model = null;
filters.department.options = [];
Expand All @@ -99,23 +99,31 @@ const toTitleCase = (str: string) =>
// RESOURCES
const defaultCompany = createResource({
url: "hrms.api.roster.get_default_company",
auto: true,
onSuccess: () => {
["company", "branch", "designation", "shift_type"].forEach((field) =>
getFilterOptions(field as FilterField),
);
},
});
const getFilterOptions = (field: FilterField, listFilters: { company?: string } = {}) => {
createListResource({
doctype: toTitleCase(field),
fields: ["name"],
filters: listFilters,
pageLength: 100,
auto: true,
onSuccess: (data: { name: string }[]) => {
filters[field].model = { value: "" };
const value = field === "company" ? defaultCompany.data : "";
filters[field].model = { value };
filters[field].options = data.map((item) => item.name);
},
onError(error: { messages: string[] }) {
raiseToast("error", error.messages[0]);
},
});
};
["company", "branch", "designation", "shift_type"].forEach((field) =>
getFilterOptions(field as FilterField),
);
</script>
7 changes: 6 additions & 1 deletion roster/src/views/MonthView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@
@addToMonth="addToMonth"
/>
<MonthViewTable
v-if="isCompanySelected"
ref="monthViewTable"
:firstOfMonth="firstOfMonth"
:employees="employees.data || []"
:employeeFilters="employeeFilters"
:shiftTypeFilter="shiftTypeFilter"
/>
<div v-else class="my-40 text-center">Please select a company.</div>
</div>
</div>
<ShiftAssignmentDialog
Expand Down Expand Up @@ -61,6 +63,7 @@ export type EmployeeFilters = {
};
const monthViewTable = ref<InstanceType<typeof MonthViewTable>>();
const isCompanySelected = ref(false);
const showShiftAssignmentDialog = ref(false);
const firstOfMonth = ref(dayjs().date(1).startOf("D"));
const shiftTypeFilter = ref("");
Expand All @@ -73,6 +76,8 @@ const addToMonth = (change: number) => {
};
const updateFilters = (newFilters: EmployeeFilters & { shift_type: string }) => {
isCompanySelected.value = !!newFilters.company;
if (!isCompanySelected.value) return;
let employeeUpdated = false;
(Object.entries(newFilters) as [keyof EmployeeFilters | "shift_type", string][]).forEach(
([key, value]) => {
Expand All @@ -95,7 +100,7 @@ const employees = createListResource({
doctype: "Employee",
fields: ["name", "employee_name", "designation", "image"],
filters: employeeFilters,
auto: true,
pageLength: 99999,
onError(error: { messages: string[] }) {
raiseToast("error", error.messages[0]);
},
Expand Down

0 comments on commit 11f9482

Please sign in to comment.