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

import jingrow
from jingrow.model.page import Page


class MarketplaceAppPayment(Page):
	def has_threshold_passed(self):
		exchange_rate = jingrow.db.get_single_value("Jcloud Settings", "usd_rate")
		total = self.total_usd + (self.total_cny / exchange_rate) if exchange_rate > 0 else 80
		return total >= jingrow.db.get_single_value("Jcloud Settings", "threshold")

	def get_commission(self, total):
		# TODO: Handle partial commission
		# if first month collection: $20, second month: $1000 and $500 - cap/threshold
		# then commission should be calculated for $520 from second month collection onwards
		return (
			total * jingrow.db.get_single_value("Jcloud Settings", "commission")
			if self.has_threshold_passed()
			else total
		)
