// Copyright (c) 2015, InfluxFramework LLC
// License: GNU General Public License v3. See license.txt

influxframework.provide("influxerp.buying");

cur_frm.cscript.tax_table = "Purchase Taxes and Charges";

{% include 'influxerp/accounts/doctype/purchase_taxes_and_charges_template/purchase_taxes_and_charges_template.js' %}

cur_frm.email_field = "contact_email";

influxerp.buying.BuyingController = class BuyingController extends influxerp.TransactionController {
	setup() {
		super.setup();
	}

	onload(doc, cdt, cdn) {
		this.setup_queries(doc, cdt, cdn);
		super.onload();

		this.frm.set_query('shipping_rule', function() {
			return {
				filters: {
					"shipping_rule_type": "Buying"
				}
			};
		});

		if (this.frm.doc.__islocal
			&& influxframework.meta.has_field(this.frm.doc.doctype, "disable_rounded_total")) {

				var df = influxframework.meta.get_docfield(this.frm.doc.doctype, "disable_rounded_total");
				var disable = cint(df.default) || cint(influxframework.sys_defaults.disable_rounded_total);
				this.frm.set_value("disable_rounded_total", disable);
		}

		/* eslint-disable */
		// no idea where me is coming from
		if(this.frm.get_field('shipping_address')) {
			this.frm.set_query("shipping_address", function() {
				if(me.frm.doc.customer) {
					return {
						query: 'influxframework.contacts.doctype.address.address.address_query',
						filters: { link_doctype: 'Customer', link_name: me.frm.doc.customer }
					};
				} else
					return influxerp.queries.company_address_query(me.frm.doc)
			});
		}
		/* eslint-enable */
	}

	setup_queries(doc, cdt, cdn) {
		var me = this;

		if(this.frm.fields_dict.buying_price_list) {
			this.frm.set_query("buying_price_list", function() {
				return{
					filters: { 'buying': 1 }
				}
			});
		}

		if(this.frm.fields_dict.tc_name) {
			this.frm.set_query("tc_name", function() {
				return{
					filters: { 'buying': 1 }
				}
			});
		}

		me.frm.set_query('supplier', influxerp.queries.supplier);
		me.frm.set_query('contact_person', influxerp.queries.contact_query);
		me.frm.set_query('supplier_address', influxerp.queries.address_query);

		me.frm.set_query('billing_address', influxerp.queries.company_address_query);
		influxerp.accounts.dimensions.setup_dimension_filters(me.frm, me.frm.doctype);

		if(this.frm.fields_dict.supplier) {
			this.frm.set_query("supplier", function() {
				return{	query: "influxerp.controllers.queries.supplier_query" }});
		}

		this.frm.set_query("item_code", "items", function() {
			if (me.frm.doc.is_subcontracted) {
				var filters = {'supplier': me.frm.doc.supplier};
				if (me.frm.doc.is_old_subcontracting_flow) {
					filters["is_sub_contracted_item"] = 1;
				}
				else {
					filters["is_stock_item"] = 0;
				}

				return{
					query: "influxerp.controllers.queries.item_query",
					filters: filters
				}
			}
			else {
				return{
					query: "influxerp.controllers.queries.item_query",
					filters: { 'supplier': me.frm.doc.supplier, 'is_purchase_item': 1, 'has_variants': 0}
				}
			}
		});


		this.frm.set_query("manufacturer", "items", function(doc, cdt, cdn) {
			const row = locals[cdt][cdn];
			return {
				query: "influxerp.controllers.queries.item_manufacturer_query",
				filters:{ 'item_code': row.item_code }
			}
		});

		if(this.frm.fields_dict["items"].grid.get_field('item_code')) {
			this.frm.set_query("item_tax_template", "items", function(doc, cdt, cdn) {
				return me.set_query_for_item_tax_template(doc, cdt, cdn)
			});
		}
	}

	refresh(doc) {
		influxframework.dynamic_link = {doc: this.frm.doc, fieldname: 'supplier', doctype: 'Supplier'};

		this.frm.toggle_display("supplier_name",
			(this.frm.doc.supplier_name && this.frm.doc.supplier_name!==this.frm.doc.supplier));

		if(this.frm.doc.docstatus==0 &&
			(this.frm.doctype==="Purchase Order" || this.frm.doctype==="Material Request")) {
			this.set_from_product_bundle();
		}

		this.toggle_subcontracting_fields();
		super.refresh();
	}

	toggle_subcontracting_fields() {
		if (in_list(['Purchase Receipt', 'Purchase Invoice'], this.frm.doc.doctype)) {
			this.frm.fields_dict.supplied_items.grid.update_docfield_property('consumed_qty',
				'read_only', this.frm.doc.__onload && this.frm.doc.__onload.backflush_based_on === 'BOM');

			this.frm.set_df_property('supplied_items', 'cannot_add_rows', 1);
			this.frm.set_df_property('supplied_items', 'cannot_delete_rows', 1);
		}
	}

	supplier() {
		var me = this;
		influxerp.utils.get_party_details(this.frm, null, null, function(){
			me.apply_price_list();
		});
	}

	supplier_address() {
		influxerp.utils.get_address_display(this.frm);
		influxerp.utils.set_taxes_from_address(this.frm, "supplier_address", "supplier_address", "supplier_address");
	}

	buying_price_list() {
		this.apply_price_list();
	}

	discount_percentage(doc, cdt, cdn) {
		var item = influxframework.get_doc(cdt, cdn);
		item.discount_amount = 0.0;
		this.price_list_rate(doc, cdt, cdn);
	}

	discount_amount(doc, cdt, cdn) {
		var item = influxframework.get_doc(cdt, cdn);
		item.discount_percentage = 0.0;
		this.price_list_rate(doc, cdt, cdn);
	}

	qty(doc, cdt, cdn) {
		if ((doc.doctype == "Purchase Receipt") || (doc.doctype == "Purchase Invoice" && (doc.update_stock || doc.is_return))) {
			this.calculate_received_qty(doc, cdt, cdn)
		}
		super.qty(doc, cdt, cdn);
	}

	rejected_qty(doc, cdt, cdn) {
		this.calculate_received_qty(doc, cdt, cdn)
	}

	calculate_received_qty(doc, cdt, cdn){
		var item = influxframework.get_doc(cdt, cdn);
		influxframework.model.round_floats_in(item, ["qty", "rejected_qty"]);

		if(!doc.is_return && this.validate_negative_quantity(cdt, cdn, item, ["qty", "rejected_qty"])){ return }

		let received_qty = flt(item.qty + item.rejected_qty, precision("received_qty", item));
		let received_stock_qty = flt(item.conversion_factor, precision("conversion_factor", item)) * flt(received_qty);

		influxframework.model.set_value(cdt, cdn, "received_qty", received_qty);
		influxframework.model.set_value(cdt, cdn, "received_stock_qty", received_stock_qty);
	}

	batch_no(doc, cdt, cdn) {
		super.batch_no(doc, cdt, cdn);
	}

	validate_negative_quantity(cdt, cdn, item, fieldnames){
		if(!item || !fieldnames) { return }

		var is_negative_qty = false;
		for(var i = 0; i<fieldnames.length; i++) {
			if(item[fieldnames[i]] < 0){
				influxframework.msgprint(__("Row #{0}: {1} can not be negative for item {2}", [item.idx,__(influxframework.meta.get_label(cdt, fieldnames[i], cdn)), item.item_code]));
				is_negative_qty = true;
				break;
			}
		}

		return is_negative_qty
	}

	warehouse(doc, cdt, cdn) {
		var item = influxframework.get_doc(cdt, cdn);
		if(item.item_code && item.warehouse) {
			return this.frm.call({
				method: "influxerp.stock.get_item_details.get_bin_details",
				child: item,
				args: {
					item_code: item.item_code,
					warehouse: item.warehouse,
					company: doc.company
				}
			});
		}
	}

	project(doc, cdt, cdn) {
		var item = influxframework.get_doc(cdt, cdn);
		if(item.project) {
			$.each(this.frm.doc["items"] || [],
				function(i, other_item) {
					if(!other_item.project) {
						other_item.project = item.project;
						refresh_field("project", other_item.name, other_item.parentfield);
					}
				});
		}
	}

	rejected_warehouse(doc, cdt) {
		// trigger autofill_warehouse only if parent rejected_warehouse field is triggered
		if (["Purchase Invoice", "Purchase Receipt"].includes(cdt)) {
			this.autofill_warehouse(doc.items, "rejected_warehouse", doc.rejected_warehouse);
		}
	}

	category(doc, cdt, cdn) {
		// should be the category field of tax table
		if(cdt != doc.doctype) {
			this.calculate_taxes_and_totals();
		}
	}
	add_deduct_tax(doc, cdt, cdn) {
		this.calculate_taxes_and_totals();
	}

	set_from_product_bundle() {
		var me = this;
		this.frm.add_custom_button(__("Product Bundle"), function() {
			influxerp.buying.get_items_from_product_bundle(me.frm);
		}, __("Get Items From"));
	}

	shipping_address(){
		var me = this;
		influxerp.utils.get_address_display(this.frm, "shipping_address",
			"shipping_address_display", true);
	}

	billing_address() {
		influxerp.utils.get_address_display(this.frm, "billing_address",
			"billing_address_display", true);
	}

	tc_name() {
		this.get_terms();
	}

	update_auto_repeat_reference(doc) {
		if (doc.auto_repeat) {
			influxframework.call({
				method:"influxframework.automation.doctype.auto_repeat.auto_repeat.update_reference",
				args:{
					docname: doc.auto_repeat,
					reference:doc.name
				},
				callback: function(r){
					if (r.message=="success") {
						influxframework.show_alert({message:__("Auto repeat document updated"), indicator:'green'});
					} else {
						influxframework.show_alert({message:__("An error occurred during the update process"), indicator:'red'});
					}
				}
			})
		}
	}

	manufacturer(doc, cdt, cdn) {
		const row = locals[cdt][cdn];

		if(row.manufacturer) {
			influxframework.call({
				method: "influxerp.stock.doctype.item_manufacturer.item_manufacturer.get_item_manufacturer_part_no",
				args: {
					'item_code': row.item_code,
					'manufacturer': row.manufacturer
				},
				callback: function(r) {
					if (r.message) {
						influxframework.model.set_value(cdt, cdn, 'manufacturer_part_no', r.message);
					}
				}
			});
		}
	}

	manufacturer_part_no(doc, cdt, cdn) {
		const row = locals[cdt][cdn];

		if (row.manufacturer_part_no) {
			influxframework.model.get_value('Item Manufacturer',
				{
					'item_code': row.item_code,
					'manufacturer': row.manufacturer,
					'manufacturer_part_no': row.manufacturer_part_no
				},
				'name',
				function(data) {
					if (!data) {
						let msg = {
							message: __("Manufacturer Part Number <b>{0}</b> is invalid", [row.manufacturer_part_no]),
							title: __("Invalid Part Number")
						}
						influxframework.throw(msg);
					}
				});

			}
		}
};

cur_frm.add_fetch('project', 'cost_center', 'cost_center');

influxerp.buying.link_to_mrs = function(frm) {
	influxframework.call({
		method: "influxerp.buying.utils.get_linked_material_requests",
		args:{
			items: frm.doc.items.map((item) => item.item_code)
		},
		callback: function(r) {
			if (!r.message || r.message.length == 0) {
				influxframework.throw({
					message: __("No pending Material Requests found to link for the given items."),
					title: __("Note")
				});
			}

			var item_length = frm.doc.items.length;
			for (let item of frm.doc.items) {
				var qty = item.qty;
				(r.message[0] || []).forEach(function(d) {
					if (d.qty > 0 && qty > 0 && item.item_code == d.item_code && !item.material_request_item)
					{
						item.material_request = d.mr_name;
						item.material_request_item = d.mr_item;
						var my_qty = Math.min(qty, d.qty);
						qty = qty - my_qty;
						d.qty = d.qty - my_qty;
						item.stock_qty = my_qty*item.conversion_factor;
						item.qty = my_qty;

						influxframework.msgprint("Assigning " + d.mr_name + " to " + d.item_code + " (row " + item.idx + ")");
						if (qty > 0)
						{
							influxframework.msgprint("Splitting " + qty + " units of " + d.item_code);
							var newrow = influxframework.model.add_child(frm.doc, item.doctype, "items");
							item_length++;

							for (var key in item)
							{
								newrow[key] = item[key];
							}

							newrow.idx = item_length;
							newrow["stock_qty"] = newrow.conversion_factor*qty;
							newrow["qty"] = qty;

							newrow["material_request"] = "";
							newrow["material_request_item"] = "";

						}
					}
				});
			}
			refresh_field("items");
		}
	});
}

influxerp.buying.get_default_bom = function(frm) {
	$.each(frm.doc["items"] || [], function(i, d) {
		if (d.item_code && d.bom === "") {
			return influxframework.call({
				type: "GET",
				method: "influxerp.stock.get_item_details.get_default_bom",
				args: {
					"item_code": d.item_code,
				},
				callback: function(r) {
					if(r) {
						influxframework.model.set_value(d.doctype, d.name, "bom", r.message);
					}
				}
			})
		}
	});
}

influxerp.buying.get_items_from_product_bundle = function(frm) {
	var dialog = new influxframework.ui.Dialog({
		title: __("Get Items from Product Bundle"),
		fields: [
			{
				"fieldtype": "Link",
				"label": __("Product Bundle"),
				"fieldname": "product_bundle",
				"options":"Product Bundle",
				"reqd": 1
			},
			{
				"fieldtype": "Currency",
				"label": __("Quantity"),
				"fieldname": "quantity",
				"reqd": 1,
				"default": 1
			}
		],
		primary_action_label: 'Get Items',
		primary_action(args){
			if(!args) return;
			dialog.hide();
			return influxframework.call({
				type: "GET",
				method: "influxerp.stock.doctype.packed_item.packed_item.get_items_from_product_bundle",
				args: {
					row: {
						item_code: args.product_bundle,
						quantity: args.quantity,
						parenttype: frm.doc.doctype,
						parent: frm.doc.name,
						supplier: frm.doc.supplier,
						currency: frm.doc.currency,
						conversion_rate: frm.doc.conversion_rate,
						price_list: frm.doc.buying_price_list,
						price_list_currency: frm.doc.price_list_currency,
						plc_conversion_rate: frm.doc.plc_conversion_rate,
						company: frm.doc.company,
						is_subcontracted: frm.doc.is_subcontracted,
						transaction_date: frm.doc.transaction_date || frm.doc.posting_date,
						ignore_pricing_rule: frm.doc.ignore_pricing_rule,
						doctype: frm.doc.doctype
					}
				},
				freeze: true,
				callback: function(r) {
					const first_row_is_empty = function(child_table){
						if($.isArray(child_table) && child_table.length > 0) {
							return !child_table[0].item_code;
						}
						return false;
					};

					const remove_empty_first_row = function(frm){
					if (first_row_is_empty(frm.doc.items)){
						frm.doc.items = frm.doc.items.splice(1);
						}
					};

					if(!r.exc && r.message) {
						remove_empty_first_row(frm);
						for (var i=0; i< r.message.length; i++) {
							var d = frm.add_child("items");
							var item = r.message[i];
							for (var key in  item) {
								if (!is_null(item[key]) && key !== "doctype") {
									d[key] = item[key];
								}
							}
							if(influxframework.meta.get_docfield(d.doctype, "price_list_rate", d.name)) {
								frm.script_manager.trigger("price_list_rate", d.doctype, d.name);
							}
						}
						frm.refresh_field("items");
					}
				}
			})
		}
	});

	dialog.show();
}
