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

import jingrow
from tqdm import tqdm


def execute():
	exchange_rate = jingrow.db.get_single_value("Jcloud Settings", "usd_rate")
	payout_orders = jingrow.get_all(
		"Payout Order",
		{"pagestatus": 0},
		["name", "net_total_cny", "net_total_usd", "recipient_currency"],
	)

	for payout_order in tqdm(payout_orders):
		total_amount = 0
		if payout_order.recipient_currency == "USD":
			cny_in_usd = 0
			if payout_order.net_total_cny > 0:
				cny_in_usd = payout_order.net_total_cny / exchange_rate
			total_amount = payout_order.net_total_usd + cny_in_usd
		elif payout_order.recipient_currency == "CNY":
			total_amount = (
				payout_order.net_total_cny + payout_order.net_total_usd * exchange_rate
			)

		jingrow.db.set_value("Payout Order", payout_order.name, "total_amount", total_amount)
