Add way to update Employee ID from profile view

This commit is contained in:
Lance Edgar 2021-09-22 18:29:30 -05:00
parent af8bd246a9
commit 9365dd7b1a
3 changed files with 141 additions and 2 deletions

View file

@ -200,7 +200,51 @@
<div v-if="employee.exists">
<b-field horizontal label="Employee ID">
<div class="level">
<div class="level-left">
<div class="level-item">
<span>{{ employee.id }}</span>
</div>
% if request.has_perm('employees.edit'):
<div class="level-item">
<b-button type="is-primary"
icon-pack="fas"
icon-left="edit"
@click="initEditEmployeeID()">
Edit ID
</b-button>
<b-modal has-modal-card
:active.sync="showEditEmployeeIDDialog">
<div class="modal-card">
<header class="modal-card-head">
<p class="modal-card-title">Employee ID</p>
</header>
<section class="modal-card-body">
<b-field label="Employee ID">
<b-input v-model="newEmployeeID"></b-input>
</b-field>
</section>
<footer class="modal-card-foot">
<b-button @click="showEditEmployeeIDDialog = false">
Cancel
</b-button>
<b-button type="is-primary"
icon-pack="fas"
icon-left="save"
:disabled="updatingEmployeeID"
@click="updateEmployeeID()">
{{ editEmployeeIDSaveButtonText }}
</b-button>
</footer>
</div>
</b-modal>
</div>
% endif
</div>
</div>
</b-field>
<b-field horizontal label="Employee Status">
@ -643,17 +687,79 @@
employeeHistoryEndDate: null,
employeeHistoryEndDateRequired: false,
% if request.has_perm('employees.edit'):
showEditEmployeeIDDialog: false,
newEmployeeID: null,
updatingEmployeeID: false,
% endif
## TODO: should find a better way to handle CSRF token
csrftoken: ${json.dumps(request.session.get_csrf_token() or request.session.new_csrf_token())|n},
}
},
computed: {
% if request.has_perm('employees.edit'):
editEmployeeIDSaveButtonText() {
if (this.updatingEmployeeID) {
return "Working, please wait..."
}
return "Save"
},
% endif
},
methods: {
changeContentTitle(newTitle) {
this.$emit('change-content-title', newTitle)
},
% if request.has_perm('employees.edit'):
initEditEmployeeID() {
this.newEmployeeID = this.employee.id
this.updatingEmployeeID = false
this.showEditEmployeeIDDialog = true
},
updateEmployeeID() {
this.updatingEmployeeID = true
let url = '${url('people.profile_update_employee_id', uuid=instance.uuid)}'
let params = {
'employee_id': this.newEmployeeID,
}
let headers = {
## TODO: should find a better way to handle CSRF token
'X-CSRF-TOKEN': this.csrftoken,
}
## TODO: should find a better way to handle CSRF token
this.$http.post(url, params, {headers: headers}).then(({ data }) => {
if (data.success) {
this.employee.id = data.employee.id
this.showEditEmployeeIDDialog = false
this.updatingEmployeeID = false
} else {
this.$buefy.toast.open({
message: "Save failed: " + data.error,
type: 'is-danger',
duration: 4000, // 4 seconds
})
}
}, response => {
alert("Unexpected error occurred!")
})
},
% endif
% if request.has_perm('people_profile.toggle_employee'):
showStartEmployee() {

View file

@ -186,6 +186,11 @@ class EmployeeView(MasterView):
employee = f.model_instance
f.set_renderer('person', self.render_person)
if self.creating or self.editing:
f.remove('users')
else:
f.set_readonly('users')
f.set_renderer('users', self.render_users)
f.set_renderer('stores', self.render_stores)

View file

@ -634,6 +634,25 @@ class PersonView(MasterView):
'employee_history_data': self.get_context_employee_history(employee),
}
def profile_update_employee_id(self):
"""
View to update an employee's ID value.
"""
app = self.get_rattail_app()
employment = app.get_employment_handler()
person = self.get_instance()
employee = employment.get_employee(person)
data = self.request.json_body
employee.id = data['employee_id']
self.Session.flush()
return {
'success': True,
'employee': self.get_context_employee(employee),
}
def make_note_form(self, mode, person):
schema = NoteSchema().bind(session=self.Session(),
person_uuid=person.uuid)
@ -784,6 +803,15 @@ class PersonView(MasterView):
config.add_view(cls, attr='profile_edit_employee_history', route_name='{}.profile_edit_employee_history'.format(route_prefix),
permission='people_profile.edit_employee_history', renderer='json')
# profile - update employee ID
config.add_route('{}.profile_update_employee_id'.format(route_prefix),
'{}/profile/update-employee-id'.format(instance_url_prefix),
request_method='POST')
config.add_view(cls, attr='profile_update_employee_id',
route_name='{}.profile_update_employee_id'.format(route_prefix),
renderer='json',
permission='employees.edit')
# manage notes from profile view
if cls.manage_notes_from_profile_view: