<commit_before>// Copyright (c) 2016, ESS LLP and contributors
// For license information, please see license.txt

frappe.ui.form.on('Patient', {
	refresh: function (frm) {
		frm.set_query("patient", "patient_relation", function () {
			return {
				filters: [
					["Patient", "name", "!=", frm.doc.name]
				]
			};
		});
		if (frappe.defaults.get_default("patient_master_name") != "Naming Series") {
			frm.toggle_display("naming_series", false);
		} else {
			erpnext.toggle_naming_series();
		}
		if (frappe.defaults.get_default("collect_registration_fee") && frm.doc.disabled == 1) {
			frm.add_custom_button(__('Invoice Patient Registration'), function () {
				btn_invoice_registration(frm);
			});
		}
		if (frm.doc.patient_name && frappe.user.has_role("Physician")) {
			frm.add_custom_button(__('Medical Record'), function () {
				frappe.route_options = { "patient": frm.doc.name };
				frappe.set_route("medical_record");
			},"View");
		}
		if (!frm.doc.__islocal && (frappe.user.has_role("Nursing User") || frappe.user.has_role("Physician"))) {
			frm.add_custom_button(__('Vital Signs'), function () {
				btn_create_vital_signs(frm);
			}, "Create");
			frm.add_custom_button(__('Medical Record'), function () {
				create_medical_record(frm);
			}, "Create");
			frm.add_custom_button(__('Patient Encounter'), function () {
				btn_create_encounter(frm);
			}, "Create");
		}
	},
	onload: function (frm) {
		if(!frm.doc.dob){
			$(frm.fields_dict['age_html'].wrapper).html("");
		}
		if(frm.doc.dob){
			$(frm.fields_dict['age_html'].wrapper).html("AGE : " + get_age(frm.doc.dob));
		}
	}
});

frappe.ui.form.on("Patient", "dob", function(frm) {
	if(frm.doc.dob) {
		var today = new Date();
		var birthDate = new Date(frm.doc.dob);
		if(today < birthDate){
			frappe.msgprint(__("Please select a valid Date"));
			frappe.model.set_value(frm.doctype,frm.docname, "dob", "");
		}
		else{
			var age_str = get_age(frm.doc.dob);
			$(frm.fields_dict['age_html'].wrapper).html("AGE : " + age_str);
		}
	}
	else {
		$(frm.fields_dict['age_html'].wrapper).html("");
	}
});

var create_medical_record = function (frm) {
	frappe.route_options = {
		"patient": frm.doc.name,
		"status": "Open",
		"reference_doctype": "Patient Medical Record",
		"reference_owner": frm.doc.owner
	};
	frappe.new_doc("Patient Medical Record");
};

var get_age = function (birth) {
	var ageMS = Date.parse(Date()) - Date.parse(birth);
	var age = new Date();
	age.setTime(ageMS);
	var years = age.getFullYear() - 1970;
	return years + " Year(s) " + age.getMonth() + " Month(s) " + age.getDate() + " Day(s)";
};

var btn_create_vital_signs = function (frm) {
	if (!frm.doc.name) {
		frappe.throw(__("Please save the patient first"));
	}
	frappe.route_options = {
		"patient": frm.doc.name,
	};
	frappe.new_doc("Vital Signs");
};

var btn_create_encounter = function (frm) {
	if (!frm.doc.name) {
		frappe.throw(__("Please save the patient first"));
	}
	frappe.route_options = {
		"patient": frm.doc.name,
	};
	frappe.new_doc("Patient Encounter");
};

var btn_invoice_registration = function (frm) {
	frappe.call({
		doc: frm.doc,
		method: "invoice_patient_registration",
		callback: function(data){
			if(!data.exc){
				if(data.message.invoice){
					/* frappe.show_alert(__('Sales Invoice {0} created',
					['<a href="#Form/Sales Invoice/'+data.message.invoice+'">' + data.message.invoice+ '</a>'])); */
					frappe.set_route("Form", "Sales Invoice", data.message.invoice);
				}
				cur_frm.reload_doc();
			}
		}
	});
};<commit_msg>[FIX][#15018][PATIENT Relation should be unique]<commit_after>// Copyright (c) 2016, ESS LLP and contributors
// For license information, please see license.txt

frappe.ui.form.on('Patient', {
	refresh: function (frm) {
		frm.set_query("patient", "patient_relation", function () {
			return {
				filters: [
					["Patient", "name", "!=", frm.doc.name]
				]
			};
		});
		if (frappe.defaults.get_default("patient_master_name") != "Naming Series") {
			frm.toggle_display("naming_series", false);
		} else {
			erpnext.toggle_naming_series();
		}
		if (frappe.defaults.get_default("collect_registration_fee") && frm.doc.disabled == 1) {
			frm.add_custom_button(__('Invoice Patient Registration'), function () {
				btn_invoice_registration(frm);
			});
		}
		if (frm.doc.patient_name && frappe.user.has_role("Physician")) {
			frm.add_custom_button(__('Medical Record'), function () {
				frappe.route_options = { "patient": frm.doc.name };
				frappe.set_route("medical_record");
			},"View");
		}
		if (!frm.doc.__islocal && (frappe.user.has_role("Nursing User") || frappe.user.has_role("Physician"))) {
			frm.add_custom_button(__('Vital Signs'), function () {
				btn_create_vital_signs(frm);
			}, "Create");
			frm.add_custom_button(__('Medical Record'), function () {
				create_medical_record(frm);
			}, "Create");
			frm.add_custom_button(__('Patient Encounter'), function () {
				btn_create_encounter(frm);
			}, "Create");
		}
	},
	onload: function (frm) {
		if(!frm.doc.dob){
			$(frm.fields_dict['age_html'].wrapper).html("");
		}
		if(frm.doc.dob){
			$(frm.fields_dict['age_html'].wrapper).html("AGE : " + get_age(frm.doc.dob));
		}
	}
});

frappe.ui.form.on("Patient", "dob", function(frm) {
	if(frm.doc.dob) {
		var today = new Date();
		var birthDate = new Date(frm.doc.dob);
		if(today < birthDate){
			frappe.msgprint(__("Please select a valid Date"));
			frappe.model.set_value(frm.doctype,frm.docname, "dob", "");
		}
		else{
			var age_str = get_age(frm.doc.dob);
			$(frm.fields_dict['age_html'].wrapper).html("AGE : " + age_str);
		}
	}
	else {
		$(frm.fields_dict['age_html'].wrapper).html("");
	}
});

var create_medical_record = function (frm) {
	frappe.route_options = {
		"patient": frm.doc.name,
		"status": "Open",
		"reference_doctype": "Patient Medical Record",
		"reference_owner": frm.doc.owner
	};
	frappe.new_doc("Patient Medical Record");
};

var get_age = function (birth) {
	var ageMS = Date.parse(Date()) - Date.parse(birth);
	var age = new Date();
	age.setTime(ageMS);
	var years = age.getFullYear() - 1970;
	return years + " Year(s) " + age.getMonth() + " Month(s) " + age.getDate() + " Day(s)";
};

var btn_create_vital_signs = function (frm) {
	if (!frm.doc.name) {
		frappe.throw(__("Please save the patient first"));
	}
	frappe.route_options = {
		"patient": frm.doc.name,
	};
	frappe.new_doc("Vital Signs");
};

var btn_create_encounter = function (frm) {
	if (!frm.doc.name) {
		frappe.throw(__("Please save the patient first"));
	}
	frappe.route_options = {
		"patient": frm.doc.name,
	};
	frappe.new_doc("Patient Encounter");
};

var btn_invoice_registration = function (frm) {
	frappe.call({
		doc: frm.doc,
		method: "invoice_patient_registration",
		callback: function(data){
			if(!data.exc){
				if(data.message.invoice){
					/* frappe.show_alert(__('Sales Invoice {0} created',
					['<a href="#Form/Sales Invoice/'+data.message.invoice+'">' + data.message.invoice+ '</a>'])); */
					frappe.set_route("Form", "Sales Invoice", data.message.invoice);
				}
				cur_frm.reload_doc();
			}
		}
	});
};

frappe.ui.form.on('Patient Relation', {
	patient_relation_add: function(frm){
		frm.fields_dict['patient_relation'].grid.get_field('patient').get_query = function(frm){
			var patient_list = [];
			if(!frm.doc.__islocal) patient_list.push(frm.doc.name);
			$.each(frm.doc.patient_relation, function(idx, val){
				if (val.patient) patient_list.push(val.patient);
			});
			return { filters: [['Patient', 'name', 'not in', patient_list]] };
		};
	}
});
{'fraction_non_alphanumeric': 0.1905014592730167, 'fraction_numerical': 0.0033165295834438843, 'mean_word_length': 4.228155339805825, 'pattern_counts': {'":': 14, '<': 9, '<?xml version=': 0, '>': 7, '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': '815822', 'n_tokens_mistral': 3172, 'n_tokens_neox': 2868, 'n_words': 451}