Merge branch 'develop' into refactor/buying/reports
diff --git a/erpnext/accounts/utils.py b/erpnext/accounts/utils.py
index 95ba3d8..02dcd68 100644
--- a/erpnext/accounts/utils.py
+++ b/erpnext/accounts/utils.py
@@ -1171,6 +1171,10 @@
 		where voucher_type=%s and voucher_no=%s""",
 		(voucher_type, voucher_no),
 	)
+	ple = qb.DocType("Payment Ledger Entry")
+	qb.from_(ple).delete().where(
+		(ple.voucher_type == voucher_type) & (ple.voucher_no == voucher_no)
+	).run()
 
 
 def sort_stock_vouchers_by_posting_date(
diff --git a/erpnext/hooks.py b/erpnext/hooks.py
index b8f51f8..6bc17a3 100644
--- a/erpnext/hooks.py
+++ b/erpnext/hooks.py
@@ -274,8 +274,6 @@
 	"Timesheet": "erpnext.controllers.website_list_for_contact.has_website_permission",
 }
 
-dump_report_map = "erpnext.startup.report_data_map.data_map"
-
 before_tests = "erpnext.setup.utils.before_tests"
 
 standard_queries = {
diff --git a/erpnext/projects/workspace/projects/projects.json b/erpnext/projects/workspace/projects/projects.json
index 1253649..4bdb1db 100644
--- a/erpnext/projects/workspace/projects/projects.json
+++ b/erpnext/projects/workspace/projects/projects.json
@@ -5,7 +5,7 @@
    "label": "Open Projects"
   }
  ],
- "content": "[{\"type\":\"chart\",\"data\":{\"chart_name\":\"Open Projects\",\"col\":12}},{\"type\":\"spacer\",\"data\":{\"col\":12}},{\"type\":\"header\",\"data\":{\"text\":\"<span class=\\\"h4\\\"><b>Your Shortcuts</b></span>\",\"col\":12}},{\"type\":\"shortcut\",\"data\":{\"shortcut_name\":\"Task\",\"col\":3}},{\"type\":\"shortcut\",\"data\":{\"shortcut_name\":\"Project\",\"col\":3}},{\"type\":\"shortcut\",\"data\":{\"shortcut_name\":\"Timesheet\",\"col\":3}},{\"type\":\"shortcut\",\"data\":{\"shortcut_name\":\"Project Billing Summary\",\"col\":3}},{\"type\":\"shortcut\",\"data\":{\"shortcut_name\":\"Dashboard\",\"col\":3}},{\"type\":\"spacer\",\"data\":{\"col\":12}},{\"type\":\"header\",\"data\":{\"text\":\"<span class=\\\"h4\\\"><b>Reports & Masters</b></span>\",\"col\":12}},{\"type\":\"card\",\"data\":{\"card_name\":\"Projects\",\"col\":4}},{\"type\":\"card\",\"data\":{\"card_name\":\"Time Tracking\",\"col\":4}},{\"type\":\"card\",\"data\":{\"card_name\":\"Reports\",\"col\":4}}]",
+ "content": "[{\"type\":\"chart\",\"data\":{\"chart_name\":\"Open Projects\",\"col\":12}},{\"type\":\"spacer\",\"data\":{\"col\":12}},{\"type\":\"header\",\"data\":{\"text\":\"<span class=\\\"h4\\\"><b>Your Shortcuts</b></span>\",\"col\":12}},{\"type\":\"shortcut\",\"data\":{\"shortcut_name\":\"Task\",\"col\":3}},{\"type\":\"shortcut\",\"data\":{\"shortcut_name\":\"Project\",\"col\":3}},{\"type\":\"shortcut\",\"data\":{\"shortcut_name\":\"Timesheet\",\"col\":3}},{\"type\":\"shortcut\",\"data\":{\"shortcut_name\":\"Project Billing Summary\",\"col\":3}},{\"type\":\"shortcut\",\"data\":{\"shortcut_name\":\"Dashboard\",\"col\":3}},{\"type\":\"spacer\",\"data\":{\"col\":12}},{\"type\":\"header\",\"data\":{\"text\":\"<span class=\\\"h4\\\"><b>Reports &amp; Masters</b></span>\",\"col\":12}},{\"type\":\"card\",\"data\":{\"card_name\":\"Projects\",\"col\":4}},{\"type\":\"card\",\"data\":{\"card_name\":\"Time Tracking\",\"col\":4}},{\"type\":\"card\",\"data\":{\"card_name\":\"Reports\",\"col\":4}},{\"type\":\"card\",\"data\":{\"card_name\":\"Settings\",\"col\":4}}]",
  "creation": "2020-03-02 15:46:04.874669",
  "docstatus": 0,
  "doctype": "Workspace",
@@ -170,9 +170,27 @@
    "link_type": "Report",
    "onboard": 0,
    "type": "Link"
+  },
+  {
+   "hidden": 0,
+   "is_query_report": 0,
+   "label": "Settings",
+   "link_count": 1,
+   "onboard": 0,
+   "type": "Card Break"
+  },
+  {
+   "hidden": 0,
+   "is_query_report": 0,
+   "label": "Projects Settings",
+   "link_count": 0,
+   "link_to": "Projects Settings",
+   "link_type": "DocType",
+   "onboard": 0,
+   "type": "Link"
   }
  ],
- "modified": "2022-06-28 12:31:30.167740",
+ "modified": "2022-10-11 22:39:10.436311",
  "modified_by": "Administrator",
  "module": "Projects",
  "name": "Projects",
diff --git a/erpnext/setup/doctype/brand/brand.js b/erpnext/setup/doctype/brand/brand.js
index 3680906..0abb71a 100644
--- a/erpnext/setup/doctype/brand/brand.js
+++ b/erpnext/setup/doctype/brand/brand.js
@@ -1,13 +1,71 @@
 // Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
 // License: GNU General Public License v3. See license.txt
 
+frappe.ui.form.on('Brand', {
+	setup: (frm) => {
+        frm.fields_dict["brand_defaults"].grid.get_field("default_warehouse").get_query = function(doc, cdt, cdn) {
+			const row = locals[cdt][cdn];
+			return {
+				filters: { company: row.company }
+			}
+		}
 
+        frm.fields_dict["brand_defaults"].grid.get_field("default_discount_account").get_query = function(doc, cdt, cdn) {
+			const row = locals[cdt][cdn];
+			return {
+				filters: {
+					'report_type': 'Profit and Loss',
+					'company': row.company,
+					"is_group": 0
+				}
+			};
+		}
 
-//--------- ONLOAD -------------
-cur_frm.cscript.onload = function(doc, cdt, cdn) {
+        frm.fields_dict["brand_defaults"].grid.get_field("buying_cost_center").get_query = function(doc, cdt, cdn) {
+			const row = locals[cdt][cdn];
+			return {
+				filters: {
+					"is_group": 0,
+					"company": row.company
+				}
+			}
+		}
 
-}
+        frm.fields_dict["brand_defaults"].grid.get_field("expense_account").get_query = function(doc, cdt, cdn) {
+			const row = locals[cdt][cdn];
+			return {
+				query: "erpnext.controllers.queries.get_expense_account",
+				filters: { company: row.company }
+			}
+		}
 
-cur_frm.cscript.refresh = function(doc, cdt, cdn) {
+        frm.fields_dict["brand_defaults"].grid.get_field("default_provisional_account").get_query = function(doc, cdt, cdn) {
+			const row = locals[cdt][cdn];
+			return {
+				filters: {
+					"company": row.company,
+					"root_type": ["in", ["Liability", "Asset"]],
+					"is_group": 0
+				}
+			};
+		}
 
-}
+        frm.fields_dict["brand_defaults"].grid.get_field("selling_cost_center").get_query = function(doc, cdt, cdn) {
+			const row = locals[cdt][cdn];
+			return {
+				filters: {
+					"is_group": 0,
+					"company": row.company
+				}
+			}
+		}
+
+        frm.fields_dict["brand_defaults"].grid.get_field("income_account").get_query = function(doc, cdt, cdn) {
+			const row = locals[cdt][cdn];
+			return {
+				query: "erpnext.controllers.queries.get_income_account",
+				filters: { company: row.company }
+			}
+		}
+	}
+});
\ No newline at end of file
diff --git a/erpnext/startup/report_data_map.py b/erpnext/startup/report_data_map.py
deleted file mode 100644
index f8c1b6c..0000000
--- a/erpnext/startup/report_data_map.py
+++ /dev/null
@@ -1,327 +0,0 @@
-# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
-# License: GNU General Public License v3. See license.txt
-
-
-# mappings for table dumps
-# "remember to add indexes!"
-
-data_map = {
-	"Company": {"columns": ["name"], "conditions": ["docstatus < 2"]},
-	"Fiscal Year": {
-		"columns": ["name", "year_start_date", "year_end_date"],
-		"conditions": ["docstatus < 2"],
-	},
-	# Accounts
-	"Account": {
-		"columns": ["name", "parent_account", "lft", "rgt", "report_type", "company", "is_group"],
-		"conditions": ["docstatus < 2"],
-		"order_by": "lft",
-		"links": {
-			"company": ["Company", "name"],
-		},
-	},
-	"Cost Center": {
-		"columns": ["name", "lft", "rgt"],
-		"conditions": ["docstatus < 2"],
-		"order_by": "lft",
-	},
-	"GL Entry": {
-		"columns": [
-			"name",
-			"account",
-			"posting_date",
-			"cost_center",
-			"debit",
-			"credit",
-			"is_opening",
-			"company",
-			"voucher_type",
-			"voucher_no",
-			"remarks",
-		],
-		"order_by": "posting_date, account",
-		"links": {
-			"account": ["Account", "name"],
-			"company": ["Company", "name"],
-			"cost_center": ["Cost Center", "name"],
-		},
-	},
-	# Stock
-	"Item": {
-		"columns": [
-			"name",
-			"if(item_name=name, '', item_name) as item_name",
-			"description",
-			"item_group as parent_item_group",
-			"stock_uom",
-			"brand",
-			"valuation_method",
-		],
-		# "conditions": ["docstatus < 2"],
-		"order_by": "name",
-		"links": {"parent_item_group": ["Item Group", "name"], "brand": ["Brand", "name"]},
-	},
-	"Item Group": {
-		"columns": ["name", "parent_item_group"],
-		# "conditions": ["docstatus < 2"],
-		"order_by": "lft",
-	},
-	"Brand": {"columns": ["name"], "conditions": ["docstatus < 2"], "order_by": "name"},
-	"Project": {"columns": ["name"], "conditions": ["docstatus < 2"], "order_by": "name"},
-	"Warehouse": {"columns": ["name"], "conditions": ["docstatus < 2"], "order_by": "name"},
-	"Stock Ledger Entry": {
-		"columns": [
-			"name",
-			"posting_date",
-			"posting_time",
-			"item_code",
-			"warehouse",
-			"actual_qty as qty",
-			"voucher_type",
-			"voucher_no",
-			"project",
-			"incoming_rate as incoming_rate",
-			"stock_uom",
-			"serial_no",
-			"qty_after_transaction",
-			"valuation_rate",
-		],
-		"order_by": "posting_date, posting_time, creation",
-		"links": {
-			"item_code": ["Item", "name"],
-			"warehouse": ["Warehouse", "name"],
-			"project": ["Project", "name"],
-		},
-		"force_index": "posting_sort_index",
-	},
-	"Serial No": {
-		"columns": ["name", "purchase_rate as incoming_rate"],
-		"conditions": ["docstatus < 2"],
-		"order_by": "name",
-	},
-	"Stock Entry": {
-		"columns": ["name", "purpose"],
-		"conditions": ["docstatus=1"],
-		"order_by": "posting_date, posting_time, name",
-	},
-	"Material Request Item": {
-		"columns": ["item.name as name", "item_code", "warehouse", "(qty - ordered_qty) as qty"],
-		"from": "`tabMaterial Request Item` item, `tabMaterial Request` main",
-		"conditions": [
-			"item.parent = main.name",
-			"main.docstatus=1",
-			"main.status != 'Stopped'",
-			"ifnull(warehouse, '')!=''",
-			"qty > ordered_qty",
-		],
-		"links": {"item_code": ["Item", "name"], "warehouse": ["Warehouse", "name"]},
-	},
-	"Purchase Order Item": {
-		"columns": [
-			"item.name as name",
-			"item_code",
-			"warehouse",
-			"(qty - received_qty)*conversion_factor as qty",
-		],
-		"from": "`tabPurchase Order Item` item, `tabPurchase Order` main",
-		"conditions": [
-			"item.parent = main.name",
-			"main.docstatus=1",
-			"main.status != 'Stopped'",
-			"ifnull(warehouse, '')!=''",
-			"qty > received_qty",
-		],
-		"links": {"item_code": ["Item", "name"], "warehouse": ["Warehouse", "name"]},
-	},
-	"Sales Order Item": {
-		"columns": [
-			"item.name as name",
-			"item_code",
-			"(qty - delivered_qty)*conversion_factor as qty",
-			"warehouse",
-		],
-		"from": "`tabSales Order Item` item, `tabSales Order` main",
-		"conditions": [
-			"item.parent = main.name",
-			"main.docstatus=1",
-			"main.status != 'Stopped'",
-			"ifnull(warehouse, '')!=''",
-			"qty > delivered_qty",
-		],
-		"links": {"item_code": ["Item", "name"], "warehouse": ["Warehouse", "name"]},
-	},
-	# Sales
-	"Customer": {
-		"columns": [
-			"name",
-			"if(customer_name=name, '', customer_name) as customer_name",
-			"customer_group as parent_customer_group",
-			"territory as parent_territory",
-		],
-		"conditions": ["docstatus < 2"],
-		"order_by": "name",
-		"links": {
-			"parent_customer_group": ["Customer Group", "name"],
-			"parent_territory": ["Territory", "name"],
-		},
-	},
-	"Customer Group": {
-		"columns": ["name", "parent_customer_group"],
-		"conditions": ["docstatus < 2"],
-		"order_by": "lft",
-	},
-	"Territory": {
-		"columns": ["name", "parent_territory"],
-		"conditions": ["docstatus < 2"],
-		"order_by": "lft",
-	},
-	"Sales Invoice": {
-		"columns": ["name", "customer", "posting_date", "company"],
-		"conditions": ["docstatus=1"],
-		"order_by": "posting_date",
-		"links": {"customer": ["Customer", "name"], "company": ["Company", "name"]},
-	},
-	"Sales Invoice Item": {
-		"columns": ["name", "parent", "item_code", "stock_qty as qty", "base_net_amount"],
-		"conditions": ["docstatus=1", "ifnull(parent, '')!=''"],
-		"order_by": "parent",
-		"links": {"parent": ["Sales Invoice", "name"], "item_code": ["Item", "name"]},
-	},
-	"Sales Order": {
-		"columns": ["name", "customer", "transaction_date as posting_date", "company"],
-		"conditions": ["docstatus=1"],
-		"order_by": "transaction_date",
-		"links": {"customer": ["Customer", "name"], "company": ["Company", "name"]},
-	},
-	"Sales Order Item[Sales Analytics]": {
-		"columns": ["name", "parent", "item_code", "stock_qty as qty", "base_net_amount"],
-		"conditions": ["docstatus=1", "ifnull(parent, '')!=''"],
-		"order_by": "parent",
-		"links": {"parent": ["Sales Order", "name"], "item_code": ["Item", "name"]},
-	},
-	"Delivery Note": {
-		"columns": ["name", "customer", "posting_date", "company"],
-		"conditions": ["docstatus=1"],
-		"order_by": "posting_date",
-		"links": {"customer": ["Customer", "name"], "company": ["Company", "name"]},
-	},
-	"Delivery Note Item[Sales Analytics]": {
-		"columns": ["name", "parent", "item_code", "stock_qty as qty", "base_net_amount"],
-		"conditions": ["docstatus=1", "ifnull(parent, '')!=''"],
-		"order_by": "parent",
-		"links": {"parent": ["Delivery Note", "name"], "item_code": ["Item", "name"]},
-	},
-	"Supplier": {
-		"columns": [
-			"name",
-			"if(supplier_name=name, '', supplier_name) as supplier_name",
-			"supplier_group as parent_supplier_group",
-		],
-		"conditions": ["docstatus < 2"],
-		"order_by": "name",
-		"links": {
-			"parent_supplier_group": ["Supplier Group", "name"],
-		},
-	},
-	"Supplier Group": {
-		"columns": ["name", "parent_supplier_group"],
-		"conditions": ["docstatus < 2"],
-		"order_by": "name",
-	},
-	"Purchase Invoice": {
-		"columns": ["name", "supplier", "posting_date", "company"],
-		"conditions": ["docstatus=1"],
-		"order_by": "posting_date",
-		"links": {"supplier": ["Supplier", "name"], "company": ["Company", "name"]},
-	},
-	"Purchase Invoice Item": {
-		"columns": ["name", "parent", "item_code", "stock_qty as qty", "base_net_amount"],
-		"conditions": ["docstatus=1", "ifnull(parent, '')!=''"],
-		"order_by": "parent",
-		"links": {"parent": ["Purchase Invoice", "name"], "item_code": ["Item", "name"]},
-	},
-	"Purchase Order": {
-		"columns": ["name", "supplier", "transaction_date as posting_date", "company"],
-		"conditions": ["docstatus=1"],
-		"order_by": "posting_date",
-		"links": {"supplier": ["Supplier", "name"], "company": ["Company", "name"]},
-	},
-	"Purchase Order Item[Purchase Analytics]": {
-		"columns": ["name", "parent", "item_code", "stock_qty as qty", "base_net_amount"],
-		"conditions": ["docstatus=1", "ifnull(parent, '')!=''"],
-		"order_by": "parent",
-		"links": {"parent": ["Purchase Order", "name"], "item_code": ["Item", "name"]},
-	},
-	"Purchase Receipt": {
-		"columns": ["name", "supplier", "posting_date", "company"],
-		"conditions": ["docstatus=1"],
-		"order_by": "posting_date",
-		"links": {"supplier": ["Supplier", "name"], "company": ["Company", "name"]},
-	},
-	"Purchase Receipt Item[Purchase Analytics]": {
-		"columns": ["name", "parent", "item_code", "stock_qty as qty", "base_net_amount"],
-		"conditions": ["docstatus=1", "ifnull(parent, '')!=''"],
-		"order_by": "parent",
-		"links": {"parent": ["Purchase Receipt", "name"], "item_code": ["Item", "name"]},
-	},
-	# Support
-	"Issue": {
-		"columns": ["name", "status", "creation", "resolution_date", "first_responded_on"],
-		"conditions": ["docstatus < 2"],
-		"order_by": "creation",
-	},
-	# Manufacturing
-	"Work Order": {
-		"columns": [
-			"name",
-			"status",
-			"creation",
-			"planned_start_date",
-			"planned_end_date",
-			"status",
-			"actual_start_date",
-			"actual_end_date",
-			"modified",
-		],
-		"conditions": ["docstatus = 1"],
-		"order_by": "creation",
-	},
-	# Medical
-	"Patient": {
-		"columns": [
-			"name",
-			"creation",
-			"owner",
-			"if(patient_name=name, '', patient_name) as patient_name",
-		],
-		"conditions": ["docstatus < 2"],
-		"order_by": "name",
-		"links": {"owner": ["User", "name"]},
-	},
-	"Patient Appointment": {
-		"columns": [
-			"name",
-			"appointment_type",
-			"patient",
-			"practitioner",
-			"appointment_date",
-			"department",
-			"status",
-			"company",
-		],
-		"order_by": "name",
-		"links": {
-			"practitioner": ["Healthcare Practitioner", "name"],
-			"appointment_type": ["Appointment Type", "name"],
-		},
-	},
-	"Healthcare Practitioner": {
-		"columns": ["name", "department"],
-		"order_by": "name",
-		"links": {
-			"department": ["Department", "name"],
-		},
-	},
-	"Appointment Type": {"columns": ["name"], "order_by": "name"},
-	"Medical Department": {"columns": ["name"], "order_by": "name"},
-}
diff --git a/erpnext/stock/doctype/repost_item_valuation/test_repost_item_valuation.py b/erpnext/stock/doctype/repost_item_valuation/test_repost_item_valuation.py
index edd2553..e0f2479 100644
--- a/erpnext/stock/doctype/repost_item_valuation/test_repost_item_valuation.py
+++ b/erpnext/stock/doctype/repost_item_valuation/test_repost_item_valuation.py
@@ -9,6 +9,7 @@
 from frappe.utils import nowdate
 from frappe.utils.data import add_to_date, today
 
+from erpnext.accounts.doctype.sales_invoice.test_sales_invoice import create_sales_invoice
 from erpnext.accounts.utils import repost_gle_for_stock_vouchers
 from erpnext.controllers.stock_controller import create_item_wise_repost_entries
 from erpnext.stock.doctype.item.test_item import make_item
@@ -272,3 +273,57 @@
 			[{"credit": 50, "debit": 0}],
 			gle_filters={"account": "Stock In Hand - TCP1"},
 		)
+
+	def test_duplicate_ple_on_repost(self):
+		from erpnext.accounts import utils
+
+		# lower numbers to simplify test
+		orig_chunk_size = utils.GL_REPOSTING_CHUNK
+		utils.GL_REPOSTING_CHUNK = 2
+		self.addCleanup(setattr, utils, "GL_REPOSTING_CHUNK", orig_chunk_size)
+
+		rate = 100
+		item = self.make_item()
+		item.valuation_rate = 90
+		item.allow_negative_stock = 1
+		item.save()
+
+		company = "_Test Company with perpetual inventory"
+
+		# consume non-existing stock
+		sinv = create_sales_invoice(
+			company=company,
+			posting_date=today(),
+			debit_to="Debtors - TCP1",
+			income_account="Sales - TCP1",
+			expense_account="Cost of Goods Sold - TCP1",
+			warehouse="Stores - TCP1",
+			update_stock=1,
+			currency="INR",
+			item_code=item.name,
+			cost_center="Main - TCP1",
+			qty=1,
+			rate=rate,
+		)
+
+		# backdated receipt triggers repost
+		make_stock_entry(
+			item=item.name,
+			company=company,
+			qty=5,
+			rate=rate,
+			target="Stores - TCP1",
+			posting_date=add_to_date(today(), days=-1),
+		)
+
+		ple_entries = frappe.db.get_list(
+			"Payment Ledger Entry",
+			filters={"voucher_type": sinv.doctype, "voucher_no": sinv.name, "delinked": 0},
+		)
+
+		# assert successful deduplication on PLE
+		self.assertEqual(len(ple_entries), 1)
+
+		# outstanding should not be affected
+		sinv.reload()
+		self.assertEqual(sinv.outstanding_amount, 100)
diff --git a/erpnext/utilities/doctype/video/video.py b/erpnext/utilities/doctype/video/video.py
index 15dbccd..13b7877 100644
--- a/erpnext/utilities/doctype/video/video.py
+++ b/erpnext/utilities/doctype/video/video.py
@@ -59,7 +59,7 @@
 		"Video Settings", "Video Settings", ["enable_youtube_tracking", "frequency"]
 	)
 
-	if not enable_youtube_tracking:
+	if not cint(enable_youtube_tracking):
 		return
 
 	frequency = get_frequency(frequency)