# Copyright (c) 2020, JINGROW
# For license information, please see license.txt


import jingrow


def execute():
	jingrow.reload_pg("jcloud", "pagetype", "usage_record")
	# PLE to Usage Record
	jingrow.db.sql(
		"""
		insert into
			`tabUsage Record` (
				`name`,
				`creation`,
				`modified`,
				`modified_by`,
				`owner`,
				`team`,
				`document_type`,
				`document_name`,
				`date`,
				`plan`,
				`currency`,
				`amount`,
				`interval`,
				`invoice`,
				`remark`,
				`pagestatus`
			)
		select
			CONCAT('UT', SUBSTR(ple.name, 4)),
			ple.creation,
			ple.modified,
			ple.modified_by,
			ple.owner,
			ple.team,
			'Site',
			ple.site,
			ple.date,
			ple.plan,
			ple.currency,
			ple.amount * -1,
			'Daily',
			ple.invoice,
			ple.remark,
			1
		from
			`tabPayment Ledger Entry` ple
		where
			ple.purpose = 'Site Consumption'
			and ple.pagestatus = 1
			and ple.free_usage = 0
	"""
	)
