Skip to content

Commit

Permalink
feat: add shift move
Browse files Browse the repository at this point in the history
  • Loading branch information
krantheman committed Jun 4, 2024
1 parent ce7acf2 commit a5fe047
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
31 changes: 31 additions & 0 deletions hrms/api/roster.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,37 @@ def delete_repeating_shift_assignment(schedule: str) -> None:
frappe.delete_doc("Shift Assignment Schedule", schedule)


@frappe.whitelist()
def move_shift(shift_assignment: str, break_on_date: str, move_to_employee: str, move_to_date: str) -> None:
company = frappe.db.get_value("Employee", move_to_employee, "company")
shift_type, status = frappe.db.get_value("Shift Assignment", shift_assignment, ["shift_type", "status"])
create_shift_assignment(move_to_employee, company, shift_type, move_to_date, move_to_date, status)
break_shift(shift_assignment, break_on_date)


def break_shift(shift_assignment: str, date: str) -> None:
start_date, end_date = frappe.db.get_value(
"Shift Assignment", shift_assignment, ["start_date", "end_date"]
)
if end_date and date_diff(end_date, date) < 0:
frappe.throw("Cannot break shift after end date")
if date_diff(start_date, date) > 0:
frappe.throw("Cannot break shift before start date")

employee, company, shift_type, status = frappe.db.get_value(
"Shift Assignment", shift_assignment, ["employee", "company", "shift_type", "status"]
)

if date_diff(date, start_date) == 0:
frappe.db.set_value("Shift Assignment", shift_assignment, "docstatus", 2)
frappe.delete_doc("Shift Assignment", shift_assignment)
else:
frappe.db.set_value("Shift Assignment", shift_assignment, "end_date", add_days(date, -1))

if not end_date or date_diff(end_date, date) > 0:
create_shift_assignment(employee, company, shift_type, add_days(date, 1), end_date, status)


def _create_repeating_shift_assignment(
employee: str,
company: str,
Expand Down
27 changes: 27 additions & 0 deletions roster/src/components/MonthViewTable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@
hoveredCell.employee = '';
hoveredCell.date = '';
"
@drop="
droppedCell.employee = employee.name;
droppedCell.date = day.date;
moveShift.submit();
"
@dragover.prevent
>
<!-- Holiday -->
<div
Expand Down Expand Up @@ -105,6 +111,7 @@
v-for="shift in events.data?.[employee.name]?.[day.date]"
@mouseover="hoveredCell.shift = shift.name"
@mouseleave="hoveredCell.shift = ''"
:draggable="true"
class="rounded border-2 px-2 py-1 cursor-pointer"
:class="shift.status === 'Inactive' && 'border-dashed'"
:style="{
Expand Down Expand Up @@ -234,6 +241,7 @@ const employeeSearch = ref<{ value: string; label: string }[]>();
const shiftAssignment = ref<string>();
const showShiftAssignmentDialog = ref(false);
const hoveredCell = ref({ employee: "", date: "", shift: "" });
const droppedCell = ref({ employee: "", date: "" });
const daysOfMonth = computed(() => {
const daysOfMonth = [];
Expand Down Expand Up @@ -285,6 +293,25 @@ const events = createResource({
});
defineExpose({ events });
const moveShift = createResource({
url: "hrms.api.roster.move_shift",
makeParams() {
return {
shift_assignment: hoveredCell.value.shift,
break_on_date: hoveredCell.value.date,
move_to_employee: droppedCell.value.employee,
move_to_date: droppedCell.value.date,
};
},
onSuccess: () => {
raiseToast("success", "Shift moved successfully!");
events.fetch();
},
onError(error: { messages: string[] }) {
raiseToast("error", error.messages[0]);
},
});
const mapEventsToDates = (data: Events, mappedEvents: MappedEvents, employee: string) => {
mappedEvents[employee] = {};
for (let d = 1; d <= props.firstOfMonth.daysInMonth(); d++) {
Expand Down
2 changes: 1 addition & 1 deletion roster/src/components/ShiftAssignmentDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ const actions = computed(() => {
label: "Delete Shift Schedule",
icon: "trash",
onClick: async () => {
deleteRepeatingShiftAssignment.submit(form.schedule);
deleteRepeatingShiftAssignment.submit();
},
});
return options;
Expand Down

0 comments on commit a5fe047

Please sign in to comment.