fix(projects): Auto-set employee and company in Timesheet (#16743)
* fix(projects): auto-set employee and company
* only set employee and company if document is unsaved
* use frm.set_value()
diff --git a/erpnext/projects/doctype/timesheet/timesheet.js b/erpnext/projects/doctype/timesheet/timesheet.js
index 8811ab9..8ffc10e 100644
--- a/erpnext/projects/doctype/timesheet/timesheet.js
+++ b/erpnext/projects/doctype/timesheet/timesheet.js
@@ -10,8 +10,8 @@
filters:{
'status': 'Active'
}
- }
- }
+ };
+ };
frm.fields_dict['time_logs'].grid.get_field('task').get_query = function(frm, cdt, cdn) {
var child = locals[cdt][cdn];
@@ -20,33 +20,37 @@
'project': child.project,
'status': ["!=", "Cancelled"]
}
- }
- }
+ };
+ };
frm.fields_dict['time_logs'].grid.get_field('project').get_query = function() {
return{
filters: {
'company': frm.doc.company
}
- }
- }
+ };
+ };
},
onload: function(frm){
if (frm.doc.__islocal && frm.doc.time_logs) {
calculate_time_and_amount(frm);
}
+
+ if (frm.is_new()) {
+ set_employee_and_company(frm);
+ }
},
refresh: function(frm) {
if(frm.doc.docstatus==1) {
if(frm.doc.per_billed < 100 && frm.doc.total_billable_hours && frm.doc.total_billable_hours > frm.doc.total_billed_hours){
- frm.add_custom_button(__("Make Sales Invoice"), function() { frm.trigger("make_invoice") },
+ frm.add_custom_button(__("Make Sales Invoice"), function() { frm.trigger("make_invoice"); },
"fa fa-file-alt");
}
if(!frm.doc.salary_slip && frm.doc.employee){
- frm.add_custom_button(__("Make Salary Slip"), function() { frm.trigger("make_salary_slip") },
+ frm.add_custom_button(__("Make Salary Slip"), function() { frm.trigger("make_salary_slip"); },
"fa fa-file-alt");
}
}
@@ -58,7 +62,7 @@
if ((row.from_time <= frappe.datetime.now_datetime()) && !row.completed) {
button = 'Resume Timer';
}
- })
+ });
frm.add_custom_button(__(button), function() {
var flag = true;
@@ -77,7 +81,7 @@
erpnext.timesheet.timer(frm, row, timestamp);
flag = false;
}
- })
+ });
// If no activities found to start a timer, create new
if (flag) {
erpnext.timesheet.timer(frm);
@@ -94,7 +98,7 @@
frappe.db.get_value('Company', { 'company_name' : frm.doc.company }, 'standard_working_hours')
.then(({ message }) => {
(frappe.working_hours = message.standard_working_hours || 0);
- });
+ });
},
make_invoice: function(frm) {
@@ -125,8 +129,8 @@
frappe.set_route("Form", r.message.doctype, r.message.name);
}
}
- })
- })
+ });
+ });
dialog.show();
},
@@ -136,7 +140,7 @@
frm: frm
});
},
-})
+});
frappe.ui.form.on("Timesheet Detail", {
time_logs_remove: function(frm) {
@@ -171,22 +175,22 @@
.find('[data-fieldname="timer"]')
.append(frappe.render_template("timesheet"));
frm.trigger("control_timer");
- })
+ });
},
hours: function(frm, cdt, cdn) {
- calculate_end_time(frm, cdt, cdn)
+ calculate_end_time(frm, cdt, cdn);
},
billing_hours: function(frm, cdt, cdn) {
- calculate_billing_costing_amount(frm, cdt, cdn)
+ calculate_billing_costing_amount(frm, cdt, cdn);
},
billing_rate: function(frm, cdt, cdn) {
- calculate_billing_costing_amount(frm, cdt, cdn)
+ calculate_billing_costing_amount(frm, cdt, cdn);
},
costing_rate: function(frm, cdt, cdn) {
- calculate_billing_costing_amount(frm, cdt, cdn)
+ calculate_billing_costing_amount(frm, cdt, cdn);
},
billable: function(frm, cdt, cdn) {
@@ -212,7 +216,7 @@
calculate_billing_costing_amount(frm, cdt, cdn);
}
}
- })
+ });
}
});
@@ -240,23 +244,23 @@
frm._setting_hours = true;
frappe.model.set_value(cdt, cdn, "to_time",
d.format(frappe.defaultDatetimeFormat)).then(() => {
- frm._setting_hours = false;
- });
+ frm._setting_hours = false;
+ });
}
}
-}
+};
var update_billing_hours = function(frm, cdt, cdn){
var child = locals[cdt][cdn];
if(!child.billable) frappe.model.set_value(cdt, cdn, 'billing_hours', 0.0);
-}
+};
var update_time_rates = function(frm, cdt, cdn){
var child = locals[cdt][cdn];
if(!child.billable){
frappe.model.set_value(cdt, cdn, 'billing_rate', 0.0);
}
-}
+};
var calculate_billing_costing_amount = function(frm, cdt, cdn){
var child = locals[cdt][cdn];
@@ -270,7 +274,7 @@
frappe.model.set_value(cdt, cdn, 'billing_amount', billing_amount);
frappe.model.set_value(cdt, cdn, 'costing_amount', costing_amount);
calculate_time_and_amount(frm);
-}
+};
var calculate_time_and_amount = function(frm) {
var tl = frm.doc.time_logs || [];
@@ -294,4 +298,17 @@
frm.set_value("total_hours", total_working_hr);
frm.set_value("total_billable_amount", total_billable_amount);
frm.set_value("total_costing_amount", total_costing_amount);
-}
\ No newline at end of file
+};
+
+// set employee (and company) to the one that's currently logged in
+const set_employee_and_company = function(frm) {
+ const options = { user_id: frappe.session.user };
+ const fields = ['name', 'company'];
+ frappe.db.get_value('Employee', options, fields).then(({ message }) => {
+ if (message) {
+ // there is an employee with the currently logged in user_id
+ frm.set_value("employee", message.name);
+ frm.set_value("company", message.company);
+ }
+ });
+};