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

frappe.provide("erpnext.accounts");

erpnext.accounts.JournalVoucher = frappe.ui.form.Controller.extend({
	onload: function() {
		this.load_defaults();
		this.setup_queries();
	},

	onload_post_render: function() {
		cur_frm.get_field("entries").grid.set_multiple_add("account");
	},

	load_defaults: function() {
		if(this.frm.doc.__islocal && this.frm.doc.company) {
			frappe.model.set_default_values(this.frm.doc);
			$.each(this.frm.doc.entries || [], function(i, jvd) {
					frappe.model.set_default_values(jvd);
				}
			);

			if(!this.frm.doc.amended_from) this.frm.doc.posting_date = get_today();
		}
	},

	setup_queries: function() {
		var me = this;

		$.each(["account", "cost_center"], function(i, fieldname) {
			me.frm.set_query(fieldname, "entries", function() {
				frappe.model.validate_missing(me.frm.doc, "company");
				return {
					filters: {
						company: me.frm.doc.company,
						group_or_ledger: "Ledger"
					}
				};
			});
		});

		$.each([["against_voucher", "Purchase Invoice", "credit_to"],
			["against_invoice", "Sales Invoice", "debit_to"]], function(i, opts) {
				me.frm.set_query(opts[0], "entries", function(doc, cdt, cdn) {
					var jvd = frappe.get_doc(cdt, cdn);
					frappe.model.validate_missing(jvd, "account");
					return {
						filters: [
							[opts[1], opts[2], "=", jvd.account],
							[opts[1], "docstatus", "=", 1],
							[opts[1], "outstanding_amount", ">", 0]
						]
					};
				});
		});

		this.frm.set_query("against_jv", "entries", function(doc, cdt, cdn) {
			var jvd = frappe.get_doc(cdt, cdn);
			frappe.model.validate_missing(jvd, "account");

			return {
				query: "erpnext.accounts.doctype.journal_voucher.journal_voucher.get_against_jv",
				filters: { account: jvd.account }
			};
		});
	},

	against_voucher: function(doc, cdt, cdn) {
		var d = frappe.get_doc(cdt, cdn);
		if (d.against_voucher && !flt(d.debit)) {
			this.get_outstanding({
				'doctype': 'Purchase Invoice',
				'docname': d.against_voucher
			}, d)
		}
	},

	against_invoice: function(doc, cdt, cdn) {
		var d = frappe.get_doc(cdt, cdn);
		if (d.against_invoice && !flt(d.credit)) {
			this.get_outstanding({
				'doctype': 'Sales Invoice',
				'docname': d.against_invoice
			}, d)
		}
	},

	against_jv: function(doc, cdt, cdn) {
		var d = frappe.get_doc(cdt, cdn);
		if (d.against_jv && !flt(d.credit) && !flt(d.debit)) {
			this.get_outstanding({
				'doctype': 'Journal Voucher',
				'docname': d.against_jv,
				'account': d.account
			}, d)
		}
	},

	get_outstanding: function(args, child) {
		var me = this;
		return this.frm.call({
			child: child,
			method: "get_outstanding",
			args: { args: args},
			callback: function(r) {
				cur_frm.cscript.update_totals(me.frm.doc);
			}
		});
	}

});

cur_frm.script_manager.make(erpnext.accounts.JournalVoucher);

cur_frm.cscript.refresh = function(doc) {
	cur_frm.cscript.is_opening(doc)
	erpnext.toggle_naming_series();
	cur_frm.cscript.voucher_type(doc);
	if(doc.docstatus==1) {
		cur_frm.appframe.add_button(__('View Ledger'), function() {
			frappe.route_options = {
				"voucher_no": doc.name,
				"from_date": doc.posting_date,<commit_msg>Journal Voucher Detail: show Account Balance as Dr or Cr. Fixes #675<commit_after>// Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
// License: GNU General Public License v3. See license.txt

frappe.provide("erpnext.accounts");

erpnext.accounts.JournalVoucher = frappe.ui.form.Controller.extend({
	onload: function() {
		this.load_defaults();
		this.setup_queries();
		this.setup_balance_formatter();
	},

	onload_post_render: function() {
		cur_frm.get_field("entries").grid.set_multiple_add("account");
	},

	load_defaults: function() {
		if(this.frm.doc.__islocal && this.frm.doc.company) {
			frappe.model.set_default_values(this.frm.doc);
			$.each(this.frm.doc.entries || [], function(i, jvd) {
					frappe.model.set_default_values(jvd);
				}
			);

			if(!this.frm.doc.amended_from) this.frm.doc.posting_date = get_today();
		}
	},

	setup_queries: function() {
		var me = this;

		$.each(["account", "cost_center"], function(i, fieldname) {
			me.frm.set_query(fieldname, "entries", function() {
				frappe.model.validate_missing(me.frm.doc, "company");
				return {
					filters: {
						company: me.frm.doc.company,
						group_or_ledger: "Ledger"
					}
				};
			});
		});

		$.each([["against_voucher", "Purchase Invoice", "credit_to"],
			["against_invoice", "Sales Invoice", "debit_to"]], function(i, opts) {
				me.frm.set_query(opts[0], "entries", function(doc, cdt, cdn) {
					var jvd = frappe.get_doc(cdt, cdn);
					frappe.model.validate_missing(jvd, "account");
					return {
						filters: [
							[opts[1], opts[2], "=", jvd.account],
							[opts[1], "docstatus", "=", 1],
							[opts[1], "outstanding_amount", ">", 0]
						]
					};
				});
		});

		this.frm.set_query("against_jv", "entries", function(doc, cdt, cdn) {
			var jvd = frappe.get_doc(cdt, cdn);
			frappe.model.validate_missing(jvd, "account");

			return {
				query: "erpnext.accounts.doctype.journal_voucher.journal_voucher.get_against_jv",
				filters: { account: jvd.account }
			};
		});
	},

	setup_balance_formatter: function() {
		var df = frappe.meta.get_docfield("Journal Voucher Detail", "balance", this.frm.doc.name);
		df.formatter = function(value, df, options, doc) {
			var currency = frappe.meta.get_field_currency(df, doc);
			var dr_or_cr = value ? ('<label>' + (value > 0.0 ? __("Dr") : __("Cr")) + '</label>') : "";
			return "<div style='text-align: right'>"
				+ ((value==null || value==="") ? "" : format_currency(Math.abs(value), currency))
				+ " " + dr_or_cr
				+ "</div>";
		}
	},

	against_voucher: function(doc, cdt, cdn) {
		var d = frappe.get_doc(cdt, cdn);
		if (d.against_voucher && !flt(d.debit)) {
			this.get_outstanding({
				'doctype': 'Purchase Invoice',
				'docname': d.against_voucher
			}, d)
		}
	},

	against_invoice: function(doc, cdt, cdn) {
		var d = frappe.get_doc(cdt, cdn);
		if (d.against_invoice && !flt(d.credit)) {
			this.get_outstanding({
				'doctype': 'Sales Invoice',
				'docname': d.against_invoice
			}, d)
		}
	},

	against_jv: function(doc, cdt, cdn) {
		var d = frappe.get_doc(cdt, cdn);
		if (d.against_jv && !flt(d.credit) && !flt(d.debit)) {
			this.get_outstanding({
				'doctype': 'Journal Voucher',
				'docname': d.against_jv,
				'account': d.account
			}, d)
		}
	},

	get_outstanding: function(args, child) {
		var me = this;
		return this.frm.call({
			child: child,
			method: "get_outstanding",
			args: { args: args},
			callback: function(r) {
				cur_frm.cscript.update_totals(me.frm.doc);
			}
		});
	}
});

cur_frm.script_manager.make(erpnext.accounts.JournalVoucher);

cur_frm.cscript.refresh = function(doc) {
	cur_frm.cscript.is_opening(doc)
	erpnext.toggle_naming_series();
	cur_frm.cscript.voucher_type(doc);
	if(doc.docstatus==1) {
		cur_frm.appframe.add_button(__('View Ledger'), function() {
			frappe.route_options = {
				"voucher_no": doc.name,
				"from_date": doc.posting_date,
{'fraction_non_alphanumeric': 0.1770268391044361, 'fraction_numerical': 0.0043109442358503685, 'mean_word_length': 3.7191601049868765, 'pattern_counts': {'":': 4, '<': 7, '<?xml version=': 0, '>': 10, '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': '5829402', 'n_tokens_mistral': 3316, 'n_tokens_neox': 2828, 'n_words': 448}