<?php
/**
 * FeeCharge class for document-level charges (fees).
 *
 * @package     EDD\Pro\Invoices\XML\Elements
 * @copyright   Copyright (c) 2026, Sandhills Development, LLC
 * @license     https://opensource.org/licenses/gpl-2.0.php GNU Public License
 * @since       3.6.7
 */

namespace EDD\Pro\Invoices\XML\Elements;

// Exit if accessed directly.
defined( 'ABSPATH' ) || exit; // @codeCoverageIgnore

/**
 * FeeCharge class.
 *
 * Outputs a document-level charge element for an order fee,
 * required by Peppol BIS 3.0 to satisfy BR-CO-15 arithmetic.
 *
 * @since 3.6.7
 */
class FeeCharge extends Element {

	/**
	 * Get the data.
	 *
	 * @since 3.6.7
	 * @return array
	 */
	protected function get_data(): array {
		if ( empty( $this->item ) ) {
			return array();
		}

		$amount = (float) $this->item->subtotal;

		return array(
			'name'     => 'cac:AllowanceCharge',
			'children' => array(
				array(
					'name'  => 'cbc:ChargeIndicator',
					'value' => 'true',
				),
				array(
					'name'  => 'cbc:AllowanceChargeReason',
					'value' => ! empty( $this->item->description ) ? $this->item->description : 'Fee',
				),
				array(
					'name'       => 'cbc:Amount',
					'value'      => $this->format_number( $amount ),
					'attributes' => array(
						'currencyID' => $this->order->currency,
					),
				),
				array(
					'name'     => 'cac:TaxCategory',
					'children' => $this->get_tax_category_children(),
				),
			),
		);
	}
}
