<commit_before>// Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
// License: GNU General Public License v3. See license.txt

cur_frm.add_fetch('employee','employee_name','employee_name');

frappe.ui.form.on("Leave Allocation", {
	onload: function(frm) {
		if(!frm.doc.from_date) frm.set_value("from_date", frappe.datetime.get_today());

		frm.set_query("employee", function() {
			return {
				query: "erpnext.controllers.queries.employee_query"
			}
		});
		frm.set_query("leave_type", function() {
			return {
				filters: {
					is_lwp: 0
				}
			}
		})
	},

	refresh: function(frm) {
		if(frm.doc.docstatus === 1 && frm.doc.status === "Active") {
			// expire current allocation
			frm.add_custom_button(__('Expire Allocation'), function() {
				frm.trigger("expire_allocation");
			});

			// opens leave balance report for employee
			frm.add_custom_button(__('Leave Balance'), function() {
				frappe.route_options = {
					employee: frm.doc.employee,
				};
				frappe.set_route("query-report", "Employee Leave Balance");
			});
		}
	},

	expire_allocation: function(frm) {
		frappe.call({
			method: 'erpnext.hr.doctype.leave_application.leave_application.expire_previous_allocation',
			args: {
				ref_doc: frm.doc
			},
			freeze: true,
			callback: function(r){
				if(!r.exc){
					frappe.msgprint(__("Allocation Expired!"));
				}
			}
		});
	},

	employee: function(frm) {
		frm.trigger("calculate_total_leaves_allocated");
	},

	leave_type: function(frm) {
		frm.trigger("calculate_total_leaves_allocated");
	},

	carry_forward: function(frm) {
		frm.trigger("calculate_total_leaves_allocated");
	},

	carry_forwarded_leaves: function(frm) {
		frm.set_value("total_leaves_allocated",
			flt(frm.doc.carry_forwarded_leaves) + flt(frm.doc.new_leaves_allocated));
	},

	new_leaves_allocated: function(frm) {
		frm.set_value("total_leaves_allocated",
			flt(frm.doc.carry_forwarded_leaves) + flt(frm.doc.new_leaves_allocated));
	},

	calculate_total_leaves_allocated: function(frm) {
		if (cint(frm.doc.carry_forward) == 1 && frm.doc.leave_type && frm.doc.employee) {
			return frappe.call({
				method: "erpnext.hr.doctype.leave_allocation.leave_allocation.get_carry_forwarded_leaves",
				args: {
					"employee": frm.doc.employee,
					"date": frm.doc.from_date,
					"leave_type": frm.doc.leave_type,
					"carry_forward": frm.doc.carry_forward<commit_msg>feat: fetch annual allocation based on leave policy<commit_after>// Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
// License: GNU General Public License v3. See license.txt

cur_frm.add_fetch('employee','employee_name','employee_name');

frappe.ui.form.on("Leave Allocation", {
	onload: function(frm) {
		if(!frm.doc.from_date) frm.set_value("from_date", frappe.datetime.get_today());

		frm.set_query("employee", function() {
			return {
				query: "erpnext.controllers.queries.employee_query"
			}
		});
		frm.set_query("leave_type", function() {
			return {
				filters: {
					is_lwp: 0
				}
			}
		})
	},

	refresh: function(frm) {
		if(frm.doc.docstatus === 1 && frm.doc.status === "Active") {
			// expire current allocation
			frm.add_custom_button(__('Expire Allocation'), function() {
				frm.trigger("expire_allocation");
			});

			// opens leave balance report for employee
			frm.add_custom_button(__('Leave Balance'), function() {
				frappe.route_options = {
					employee: frm.doc.employee,
				};
				frappe.set_route("query-report", "Employee Leave Balance");
			});
		}
	},

	expire_allocation: function(frm) {
		frappe.call({
			method: 'erpnext.hr.doctype.leave_application.leave_application.expire_previous_allocation',
			args: {
				ref_doc: frm.doc
			},
			freeze: true,
			callback: function(r){
				if(!r.exc){
					frappe.msgprint(__("Allocation Expired!"));
				}
			}
		});
	},

	employee: function(frm) {
		frm.trigger("calculate_total_leaves_allocated");
	},

	leave_type: function(frm) {
		frm.trigger("leave_policy")
		frm.trigger("calculate_total_leaves_allocated");
	},

	carry_forward: function(frm) {
		frm.trigger("calculate_total_leaves_allocated");
	},

	carry_forwarded_leaves: function(frm) {
		frm.set_value("total_leaves_allocated",
			flt(frm.doc.carry_forwarded_leaves) + flt(frm.doc.new_leaves_allocated));
	},

	new_leaves_allocated: function(frm) {
		frm.set_value("total_leaves_allocated",
			flt(frm.doc.carry_forwarded_leaves) + flt(frm.doc.new_leaves_allocated));
	},

	leave_policy: function(frm) {
		if(frm.doc.leave_policy && frm.doc.leave_type) {
			frappe.db.get_value("Leave Policy Detail",
				{'parent': frm.doc.leave_policy, 'leave_type': frm.doc.leave_type},
				'annual_allocation', (r) => {
				if (!r.exc) {
					frm.set_value("new_leaves_allocated", flt(r.annual_allocation));
				}
			}, "Leave Policy")
		}
	},
	calculate_total_leaves_allocated: function(frm) {
		if (cint(frm.doc.carry_forward) == 1 && frm.doc.leave_type && frm.doc.employee) {
			return frappe.call({
				method: "erpnext.hr.doctype.leave_allocation.leave_allocation.get_carry_forwarded_leaves",
				args: {
					"employee": frm.doc.employee,
					"date": frm.doc.from_date,
					"leave_type": frm.doc.leave_type,
					"carry_forward": frm.doc.carry_forward
{'fraction_non_alphanumeric': 0.150939777522056, 'fraction_numerical': 0.0030686612965093976, 'mean_word_length': 4.251762336354481, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3951748', 'n_tokens_mistral': 2277, 'n_tokens_neox': 1956, 'n_words': 266}