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

refactor(Roster): enforce selection of company #2395

Merged
merged 2 commits into from
Nov 7, 2024
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
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
Loading