# Copyright (c) 2020, InfluxFramework LLC
# MIT License. See license.txt


import influxframework


def execute():
	influxframework.reload_doc("accounts", "doctype", "POS Invoice Merge Log")
	influxframework.reload_doc("accounts", "doctype", "POS Closing Entry")
	if influxframework.db.count("POS Invoice Merge Log"):
		influxframework.db.sql(
			"""
			UPDATE
				`tabPOS Invoice Merge Log` log, `tabPOS Invoice Reference` log_ref
			SET
				log.pos_closing_entry = (
					SELECT clo_ref.parent FROM `tabPOS Invoice Reference` clo_ref
					WHERE clo_ref.pos_invoice = log_ref.pos_invoice
					AND clo_ref.parenttype = 'POS Closing Entry' LIMIT 1
				)
			WHERE
				log_ref.parent = log.name
		"""
		)

		influxframework.db.sql("""UPDATE `tabPOS Closing Entry` SET status = 'Submitted' where docstatus = 1""")
		influxframework.db.sql("""UPDATE `tabPOS Closing Entry` SET status = 'Cancelled' where docstatus = 2""")
