| 1 |
import { Component, onWillStart, useState } from "@odoo/owl"; |
| 2 |
|
| 3 |
import { ConfirmationDialog } from "@web/core/confirmation_dialog/confirmation_dialog"; |
| 4 |
import { _t } from "@web/core/l10n/translation"; |
| 5 |
import { registry } from "@web/core/registry"; |
| 6 |
import { useService } from "@web/core/utils/hooks"; |
| 7 |
|
| 8 |
import { useRecordObserver } from "@web/model/relational_model/utils"; |
| 9 |
|
| 10 |
import { standardFieldProps } from "@web/views/fields/standard_field_props"; |
| 11 |
|
| 12 |
export class AccrualLevels extends Component { |
| 13 |
static template = "hr_holidays.AccrualLevels"; |
| 14 |
static props = { |
| 15 |
...standardFieldProps |
| 16 |
}; |
| 17 |
|
| 18 |
setup() { |
| 19 |
this.orm = useService("orm"); |
| 20 |
this.action = useService("action"); |
| 21 |
this.dialog = useService("dialog"); |
| 22 |
this.state = useState({}); |
| 23 |
|
| 24 |
useRecordObserver(async (record) => { |
| 25 |
this.state.carryOverDate = this.getCarryOverDate(record); |
| 26 |
this.state.data = await this.orm.read("hr.leave.accrual.level", record.data[this.props.name]._currentIds); |
| 27 |
if (!this.state.newMilestoneIds.some(id => record.data[this.props.name]._currentIds.includes(id))) { |
| 28 |
this.state.newMilestoneIds = record.data[this.props.name]._currentIds; |
| 29 |
} |
| 30 |
}); |
| 31 |
|
| 32 |
onWillStart(async () => { |
| 33 |
this.state.newMilestoneIds = this.props.record.data[this.props.name]._currentIds; |
| 34 |
}); |
| 35 |
} |
| 36 |
|
| 37 |
get milestones() { |
| 38 |
return this.props.record.data[this.props.name].records.map((record) => ({ |
| 39 |
id: record.id, |
| 40 |
resId: record.evalContext.id, |
| 41 |
data: this.state.data.filter((r) => r.id === record.evalContext.id)[0], |
| 42 |
onDelete: () => this.deleteMilestone(record.id), |
| 43 |
})); |
| 44 |
} |
| 45 |
|
| 46 |
getFullDay(day) { |
| 47 |
return luxon.DateTime.fromFormat(day, "c", { |
| 48 |
locale: this.env.model.config.context.lang.replace("_","-")}).toLocaleString({ weekday: "long" }); |
| 49 |
} |
| 50 |
|
| 51 |
getFullMonth(month) { |
| 52 |
return luxon.DateTime.fromFormat(month, "M", { |
| 53 |
locale: this.env.model.config.context.lang.replace("_","-")}).toLocaleString({ month: "long" }); |
| 54 |
} |
| 55 |
|
| 56 |
getNewMilestoneClass(id){ |
| 57 |
return this.state.newMilestoneIds.includes(id) ? "" : "new"; |
| 58 |
} |
| 59 |
|
| 60 |
getCarryOverDate(planRecord) { |
| 61 |
switch (planRecord._values.carryover_date) { |
| 62 |
case "year_start": |
| 63 |
return _t("start of the year"); |
| 64 |
case "allocation": |
| 65 |
return _t("allocation date"); |
| 66 |
default: |
| 67 |
return luxon.DateTime.fromFormat( |
| 68 |
`${planRecord._values.carryover_day} ${planRecord._values.carryover_month} 2020`, |
| 69 |
"d M y", |
| 70 |
{locale: this.env.model.config.context.lang.replace("_","-")}) |
| 71 |
.toLocaleString({ day:"numeric", month: "long" }); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
async openMilestone(id) { |
| 76 |
let action; |
| 77 |
if (id) { |
| 78 |
action = await this.orm.call("hr.leave.accrual.plan", "action_open_accrual_plan_level", |
| 79 |
[this.props.record.evalContext.id], { level_id: id }); |
| 80 |
} else { |
| 81 |
action = await this.orm.call("hr.leave.accrual.plan", "action_create_accrual_plan_level", |
| 82 |
[this.props.record.evalContext.id]); |
| 83 |
} |
| 84 |
this.action.doAction(action, { |
| 85 |
additionalContext: { |
| 86 |
active_id: this.props.record.evalContext.id, |
| 87 |
}, |
| 88 |
onClose: () => this.env.model.root.load(), |
| 89 |
}); |
| 90 |
} |
| 91 |
|
| 92 |
deleteMilestone(id) { |
| 93 |
const milestoneRecord = this.props.record.data[this.props.name].records.find( |
| 94 |
(record) => record.id === id |
| 95 |
); |
| 96 |
this.props.record.data[this.props.name].delete(milestoneRecord); |
| 97 |
} |
| 98 |
|
| 99 |
async editMilestone(id){ |
| 100 |
if (this.props.record.dirty) { |
| 101 |
this.dialog.add(ConfirmationDialog, { |
| 102 |
body: id ? |
| 103 |
_t("Do you want to save the changes made to the accrual plan before editing this milestone?") : |
| 104 |
_t("Do you want to save the changes made to the accrual plan before creating a new milestone?"), |
| 105 |
confirmLabel: _t("Yes, save changes"), |
| 106 |
cancelLabel: _t("No, keep the old version"), |
| 107 |
cancel: async () => await this.openMilestone(id), |
| 108 |
confirm: async () => { |
| 109 |
await this.props.record.save({ reload: false }); |
| 110 |
await this.openMilestone(id); |
| 111 |
}, |
| 112 |
}); |
| 113 |
} else { |
| 114 |
await this.openMilestone(id); |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
export const accrualLevels = { |
| 120 |
component: AccrualLevels, |
| 121 |
fieldDependencies: [ |
| 122 |
{ name: "carryover_day", type: "integer" } |
| 123 |
], |
| 124 |
relatedFields: () => [{ name: "id", type: "integer" }], |
| 125 |
}; |
| 126 |
|
| 127 |
registry.category("fields").add("accrual_levels", accrualLevels); |