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

fix(projects): Auto-set employee and company in Timesheet #16743

Merged
merged 5 commits into from
Mar 26, 2019
Merged
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
71 changes: 44 additions & 27 deletions erpnext/projects/doctype/timesheet/timesheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ frappe.ui.form.on("Timesheet", {
filters:{
'status': 'Active'
}
}
}
};
};

frm.fields_dict['time_logs'].grid.get_field('task').get_query = function(frm, cdt, cdn) {
var child = locals[cdt][cdn];
Expand All @@ -20,33 +20,37 @@ frappe.ui.form.on("Timesheet", {
'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");
}
}
Expand All @@ -58,7 +62,7 @@ frappe.ui.form.on("Timesheet", {
if ((row.from_time <= frappe.datetime.now_datetime()) && !row.completed) {
button = 'Resume Timer';
}
})
});

frm.add_custom_button(__(button), function() {
var flag = true;
Expand All @@ -77,7 +81,7 @@ frappe.ui.form.on("Timesheet", {
erpnext.timesheet.timer(frm, row, timestamp);
flag = false;
}
})
});
// If no activities found to start a timer, create new
if (flag) {
erpnext.timesheet.timer(frm);
Expand All @@ -94,7 +98,7 @@ frappe.ui.form.on("Timesheet", {
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) {
Expand Down Expand Up @@ -125,8 +129,8 @@ frappe.ui.form.on("Timesheet", {
frappe.set_route("Form", r.message.doctype, r.message.name);
}
}
})
})
});
});
dialog.show();
},

Expand All @@ -136,7 +140,7 @@ frappe.ui.form.on("Timesheet", {
frm: frm
});
},
})
});

frappe.ui.form.on("Timesheet Detail", {
time_logs_remove: function(frm) {
Expand Down Expand Up @@ -171,22 +175,22 @@ frappe.ui.form.on("Timesheet Detail", {
.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) {
Expand All @@ -212,7 +216,7 @@ frappe.ui.form.on("Timesheet Detail", {
calculate_billing_costing_amount(frm, cdt, cdn);
}
}
})
});
}
});

Expand Down Expand Up @@ -240,23 +244,23 @@ var calculate_end_time = function(frm, cdt, cdn) {
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];
Expand All @@ -270,7 +274,7 @@ var calculate_billing_costing_amount = function(frm, cdt, cdn){
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 || [];
Expand All @@ -294,4 +298,17 @@ var calculate_time_and_amount = function(frm) {
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);
}
};

// 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);
}
});
};