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

frappe.provide("erpnext.stock");

erpnext.stock.StockController = class StockController extends frappe.ui.form.Controller {
	onload() {
		// warehouse query if company
		if (this.frm.fields_dict.company) {
			this.setup_warehouse_query();
		}
	}

	onload_post_render() {
		this.set_route_options_for_new_doc();
	}

	set_route_options_for_new_doc() {
		// While creating a Batch or Serial and Batch Bundle from the link
		// field, copy details from the line item to the new form
		if (!this.frm.fields_dict.items) return;

		let batch_no_field = this.frm.get_docfield("items", "batch_no");
		if (batch_no_field) {
			batch_no_field.get_route_options_for_new_doc = (row) => {
				return {
					item: row.doc.item_code,
				};
			};
		}

		let sbb_field = this.frm.get_docfield("items", "serial_and_batch_bundle");
		if (sbb_field) {
			sbb_field.get_route_options_for_new_doc = (row) => {
				return {
					item_code: row.doc.item_code,
					warehouse: row.doc.warehouse || row.doc.s_warehouse || row.doc.t_warehouse,
					voucher_type: this.frm.doc.doctype,
				};
			};
		}
	}

	barcode(doc, cdt, cdn) {
		let row = locals[cdt][cdn];
		if (row.barcode) {
			erpnext.stock.utils.set_item_details_using_barcode(this.frm, row, (r) => {
				frappe.model.set_value(cdt, cdn, {
					item_code: r.message.item_code,
					qty: 1,
				});
			});
		}
	}

	setup_warehouse_query() {
		var me = this;
		erpnext.queries.setup_queries(this.frm, "Warehouse", function () {
			return erpnext.queries.warehouse(me.frm.doc);
		});
	}

	setup_posting_date_time_check() {
		// make posting date default and read only unless explictly checked
		frappe.ui.form.on(this.frm.doctype, "set_posting_date_and_time_read_only", function (frm) {
			if (frm.doc.docstatus == 0 && frm.doc.set_posting_time) {
				frm.set_df_property("posting_date", "read_only", 0);
				frm.set_df_property("posting_time", "read_only", 0);
			} else {
				frm.set_df_property("posting_date", "read_only", 1);
				frm.set_df_property("posting_time", "read_only", 1);
			}
		});

		frappe.ui.form.on(this.frm.doctype, "set_posting_time", function (frm) {
			frm.trigger("set_posting_date_and_time_read_only");
		});

		frappe.ui.form.on(this.frm.doctype, "refresh", function (frm) {
			// set default posting date / time
			if (frm.doc.docstatus == 0) {
				if (!frm.doc.posting_date) {
					frm.set_value("posting_date", frappe.datetime.nowdate());
				}
				if (!frm.doc.posting_time) {
					frm.set_value("posting_time", frappe.datetime.now_time());
				}
				frm.trigger("set_posting_date_and_time_read_only");
			}
		});
	}

	show_stock_ledger() {
		var me = this;
		if (this.frm.doc.docstatus > 0) {
			cur_frm.add_custom_button(
				__("Stock Ledger"),
				function () {
					frappe.route_options = {
						voucher_no: me.frm.doc.name,
						from_date: me.frm.doc.posting_date,
						to_date: moment(me.frm.doc.modified).format("YYYY-MM-DD"),
						company: me.frm.doc.company,
						show_cancelled_entries: me.frm.doc.docstatus === 2,
						ignore_prepared_report: true,
					};
					frappe.set_route("query-report", "Stock Ledger");
				},
				__("View")
			);
		}
	}

	show_general_ledger() {
		let me = this;
		if (this.frm.doc.docstatus > 0) {
			cur_frm.add_custom_button(
				__("Accounting Ledger"),
				function () {
					frappe.route_options = {
						voucher_no: me.frm.doc.name,
						from_date: me.frm.doc.posting_date,
						to_date: moment(me.frm.doc.modified).format("YYYY-MM-DD"),
						company: me.frm.doc.company,
						categorize_by: "Categorize by Voucher (Consolidated)",
						show_cancelled_entries: me.frm.doc.docstatus === 2,
						ignore_prepared_report: true,
					};
					frappe.set_route("query-report", "General Ledger");
				},
				__("View")
			);
		}
	}
};
