<commit_before>// ERPNext - web based ERP (http://erpnext.com)
// Copyright (C) 2012 Web Notes Technologies Pvt Ltd
// 
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

// Preset
// ------
// cur_frm.cscript.tname - Details table name
// cur_frm.cscript.fname - Details fieldname

wn.provide("erpnext.buying");
wn.require("app/js/transaction.js");

erpnext.buying.BuyingController = erpnext.TransactionController.extend({
	setup: function() {
		var me = this;
		
		if(this.frm.fields_dict.price_list_name) {
			this.frm.set_query("price_list_name", function() {
				return repl("select name, currency from `tabPrice List` \
					where buying_or_selling = 'Buying' and name like \"%s%%\"");
			});
			
			this.frm.set_query("price_list_currency", function() {
				return repl("select distinct ref_currency from `tabItem Price` \
					where price_list_name=\"%(price_list_name)s\" and buying_or_selling = 'Buying' \
					and ref_currency like \"%s%%\"", 
					{price_list_name: me.frm.doc.price_list_name});
			});
		}
		
		if(this.frm.fields_dict.supplier) {
			this.frm.set_query("supplier", erpnext.utils.supplier_query);
		}
		
		this.frm.set_query("item_code", this.frm.cscript.fname, function() {
			if(me.frm.doc.is_subcontracted == "Yes") {
				return erpnext.queries.item({'ifnull(tabItem.is_sub_contracted_item, "No")': "Yes"});
			} else {
				return erpnext.queries.item({'ifnull(tabItem.is_purchase_item, "No")': "Yes"});
			}
		});
	},
	
	refresh: function(doc) {
		this.frm.toggle_display("supplier_name", 
			(this.supplier_name && this.frm.doc.supplier_name!==this.frm.doc.supplier));
		this._super();
	},
	
	supplier: function() {
		if(this.frm.doc.supplier || this.frm.doc.credit_to) {
			if(!this.frm.doc.company) {
				this.frm.set_value("supplier", null);
				msgprint(wn._("Please specify Company"));
			} else {
				var me = this;
				var price_list_name = this.frm.doc.price_list_name;

				this.frm.call({
					doc: this.frm.doc,
					method: "set_supplier_defaults",
					freeze: true,
					callback: function(r) {
						if(!r.exc) {
							me.frm.refresh_fields();
							if(me.frm.doc.price_list_name !== price_list_name) me.price_list_name();
						}
					}
				});
			}
		}
	},
	
	item_code: function(doc, cdt, cdn) {
		var me = this;
		var item = wn.model.get_doc(cdt, cdn);
		if(item.item_code) {
			if(!this.validate_company_and_party("supplier")) {
				item.item_code = null;
				refresh_field("item_code", item.name, item.parentfield);
			} else {
				this.frm.call({
					method: "buying.utils.get_item_details",
					child: item,
					args: {
						args: {
							item_code: item.item_code,
							warehouse: item.warehouse,
							doctype: me.frm.doc.doctype,
							docname: me.frm.doc.name,
							supplier: me.frm.doc.supplier,
							conversion_rate: me.frm.doc.conversion_rate,
							price_list_name: me.frm.doc.price_list_name,
							price_list_currency: me.frm.doc.price_list_currency,
							plc_conversion_rate: me.frm.doc.plc_conversion_rate,
							is_subcontracted: me.frm.doc.is_subcontracted,
							company: me.frm.doc.company,
							currency: me.frm.doc.currency,
							transaction_date: me.frm.doc.transaction_date
						}
					},
					callback: function(r) {
						if(!r.exc) {
							me.import_ref_rate(me.frm.doc, cdt, cdn);
						}
					}
				});
			}
		}
	},
	
	price_list_name: function() {
		this._super("buying");
	},
	
	import_ref_rate: function(doc, cdt, cdn) {
		var item = wn.model.get_doc(cdt, cdn);
		wn.model.round_floats_in(item, ["import_ref_rate", "discount_rate"]);
		
		item.import_rate = flt(item.import_ref_rate * (1 - item.discount_rate / 100.0),
			precision("import_rate", item));
		
		this.calculate_taxes_and_totals();
	},
	
	discount_rate: function(doc, cdt, cdn) {
		this.import_ref_rate(doc, cdt, cdn);
	},
	
	import_rate: function(doc, cdt, cdn) {
		var item = wn.model.get_doc(cdt, cdn);
		wn.model.round_floats_in(item, ["import_rate", "discount_rate"]);
		
		if(item.import_ref_rate) {
			item.discount_rate = flt((1 - item.import_rate / item.import_ref_rate) * 100.0,
				precision("discount_rate", item));
		} else {
			item.discount_rate = 0.0;
		}
		
		this.calculate_taxes_and_totals();
	},
	
	uom: function(doc, cdt, cdn) {
		var me = this;
		var item = wn.model.get_doc(cdt, cdn);
		if(item.item_code && item.uom) {
			this.frm.call({
				method: "buying.utils.get_conversion_factor",
				child: item,
				args: {
					item_code: item.item_code,
					uom: item.uom
				},
				callback: function(r) {
					if(!r.exc) {
						me.conversion_factor(me.frm.doc, cdt, cdn);
					}
				}
			});
		}
	},
	
	qty: function(doc, cdt, cdn) {
		this._super(doc, cdt, cdn);
		this.conversion_factor(doc, cdt, cdn);
	},
	
	conversion_factor: function(doc, cdt, cdn) {
		if(wn.meta.get_docfield(cdt, "stock_qty", cdn)) {
			var item = wn.model.get_doc(cdt, cdn);
			wn.model.round_floats_in(item, ["qty", "conversion_factor"]);
			item.stock_qty = flt(item.qty * item.conversion_factor, precision("stock_qty", item));
			refresh_field("stock_qty", item.name, item.parentfield);
		}
	},
	
	warehouse: function(doc, cdt, cdn) {
		var item = wn.model.get_doc(cdt, cdn);
		if(item.item_code && item.warehouse) {
			this.frm.call({
				method: "buying.utils.get_conversion_factor",
				child: item,
				args: {
					item_code: item.item_code,
					warehouse: item.warehouse
				}
			});
		}
	},
	
	project_name: function(doc, cdt, cdn) {
		var item = wn.model.get_doc(cdt, cdn);
		if(item.project_name) {
			$.each(wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name, {parentfield: this.fname}),
				function(i, other_item) { 
					if(!other_item.project_name) {
						other_item.project_name = item.project_name;
						refresh_field("project_name", other_item.name, other_item.parentfield);
					}
				});
		}<commit_msg>[Fix]Warehouse in Item grid of Purchase Order to call get_projected_qty<commit_after>// ERPNext - web based ERP (http://erpnext.com)
// Copyright (C) 2012 Web Notes Technologies Pvt Ltd
// 
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

// Preset
// ------
// cur_frm.cscript.tname - Details table name
// cur_frm.cscript.fname - Details fieldname

wn.provide("erpnext.buying");
wn.require("app/js/transaction.js");

erpnext.buying.BuyingController = erpnext.TransactionController.extend({
	setup: function() {
		var me = this;
		
		if(this.frm.fields_dict.price_list_name) {
			this.frm.set_query("price_list_name", function() {
				return repl("select name, currency from `tabPrice List` \
					where buying_or_selling = 'Buying' and name like \"%s%%\"");
			});
			
			this.frm.set_query("price_list_currency", function() {
				return repl("select distinct ref_currency from `tabItem Price` \
					where price_list_name=\"%(price_list_name)s\" and buying_or_selling = 'Buying' \
					and ref_currency like \"%s%%\"", 
					{price_list_name: me.frm.doc.price_list_name});
			});
		}
		
		if(this.frm.fields_dict.supplier) {
			this.frm.set_query("supplier", erpnext.utils.supplier_query);
		}
		
		this.frm.set_query("item_code", this.frm.cscript.fname, function() {
			if(me.frm.doc.is_subcontracted == "Yes") {
				return erpnext.queries.item({'ifnull(tabItem.is_sub_contracted_item, "No")': "Yes"});
			} else {
				return erpnext.queries.item({'ifnull(tabItem.is_purchase_item, "No")': "Yes"});
			}
		});
	},
	
	refresh: function(doc) {
		this.frm.toggle_display("supplier_name", 
			(this.supplier_name && this.frm.doc.supplier_name!==this.frm.doc.supplier));
		this._super();
	},
	
	supplier: function() {
		if(this.frm.doc.supplier || this.frm.doc.credit_to) {
			if(!this.frm.doc.company) {
				this.frm.set_value("supplier", null);
				msgprint(wn._("Please specify Company"));
			} else {
				var me = this;
				var price_list_name = this.frm.doc.price_list_name;

				this.frm.call({
					doc: this.frm.doc,
					method: "set_supplier_defaults",
					freeze: true,
					callback: function(r) {
						if(!r.exc) {
							me.frm.refresh_fields();
							if(me.frm.doc.price_list_name !== price_list_name) me.price_list_name();
						}
					}
				});
			}
		}
	},
	
	item_code: function(doc, cdt, cdn) {
		var me = this;
		var item = wn.model.get_doc(cdt, cdn);
		if(item.item_code) {
			if(!this.validate_company_and_party("supplier")) {
				item.item_code = null;
				refresh_field("item_code", item.name, item.parentfield);
			} else {
				this.frm.call({
					method: "buying.utils.get_item_details",
					child: item,
					args: {
						args: {
							item_code: item.item_code,
							warehouse: item.warehouse,
							doctype: me.frm.doc.doctype,
							docname: me.frm.doc.name,
							supplier: me.frm.doc.supplier,
							conversion_rate: me.frm.doc.conversion_rate,
							price_list_name: me.frm.doc.price_list_name,
							price_list_currency: me.frm.doc.price_list_currency,
							plc_conversion_rate: me.frm.doc.plc_conversion_rate,
							is_subcontracted: me.frm.doc.is_subcontracted,
							company: me.frm.doc.company,
							currency: me.frm.doc.currency,
							transaction_date: me.frm.doc.transaction_date
						}
					},
					callback: function(r) {
						if(!r.exc) {
							me.import_ref_rate(me.frm.doc, cdt, cdn);
						}
					}
				});
			}
		}
	},
	
	price_list_name: function() {
		this._super("buying");
	},
	
	import_ref_rate: function(doc, cdt, cdn) {
		var item = wn.model.get_doc(cdt, cdn);
		wn.model.round_floats_in(item, ["import_ref_rate", "discount_rate"]);
		
		item.import_rate = flt(item.import_ref_rate * (1 - item.discount_rate / 100.0),
			precision("import_rate", item));
		
		this.calculate_taxes_and_totals();
	},
	
	discount_rate: function(doc, cdt, cdn) {
		this.import_ref_rate(doc, cdt, cdn);
	},
	
	import_rate: function(doc, cdt, cdn) {
		var item = wn.model.get_doc(cdt, cdn);
		wn.model.round_floats_in(item, ["import_rate", "discount_rate"]);
		
		if(item.import_ref_rate) {
			item.discount_rate = flt((1 - item.import_rate / item.import_ref_rate) * 100.0,
				precision("discount_rate", item));
		} else {
			item.discount_rate = 0.0;
		}
		
		this.calculate_taxes_and_totals();
	},
	
	uom: function(doc, cdt, cdn) {
		var me = this;
		var item = wn.model.get_doc(cdt, cdn);
		if(item.item_code && item.uom) {
			this.frm.call({
				method: "buying.utils.get_conversion_factor",
				child: item,
				args: {
					item_code: item.item_code,
					uom: item.uom
				},
				callback: function(r) {
					if(!r.exc) {
						me.conversion_factor(me.frm.doc, cdt, cdn);
					}
				}
			});
		}
	},
	
	qty: function(doc, cdt, cdn) {
		this._super(doc, cdt, cdn);
		this.conversion_factor(doc, cdt, cdn);
	},
	
	conversion_factor: function(doc, cdt, cdn) {
		if(wn.meta.get_docfield(cdt, "stock_qty", cdn)) {
			var item = wn.model.get_doc(cdt, cdn);
			wn.model.round_floats_in(item, ["qty", "conversion_factor"]);
			item.stock_qty = flt(item.qty * item.conversion_factor, precision("stock_qty", item));
			refresh_field("stock_qty", item.name, item.parentfield);
		}
	},
	
	warehouse: function(doc, cdt, cdn) {
		var item = wn.model.get_doc(cdt, cdn);
		if(item.item_code && item.warehouse) {
			this.frm.call({
				method: "buying.utils.get_projected_qty",
				child: item,
				args: {
					item_code: item.item_code,
					warehouse: item.warehouse
				}
			});
		}
	},
	
	project_name: function(doc, cdt, cdn) {
		var item = wn.model.get_doc(cdt, cdn);
		if(item.project_name) {
			$.each(wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name, {parentfield: this.fname}),
				function(i, other_item) { 
					if(!other_item.project_name) {
						other_item.project_name = item.project_name;
						refresh_field("project_name", other_item.name, other_item.parentfield);
					}
				});
		}
{'fraction_non_alphanumeric': 0.14885021752641392, 'fraction_numerical': 0.0026413921690490987, 'mean_word_length': 3.265407554671968, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5388933', 'n_tokens_mistral': 5787, 'n_tokens_neox': 4784, 'n_words': 875}