<?php
/**
 * Cost & Reports - reports
 *
 * @package CostReports/includes
 */

defined( 'ABSPATH' ) || exit;

require_once __DIR__ . '/class-cyp-functions.php';

/**
 * Class for reports
 */
class Cyp_Report {
	
	/**
	 * CypFunctions
	 *
	 * @var var $functions.
	 */
	public $functions;

	/**
	 * CypOptions
	 *
	 * @var var $cyp_options.
	 */
	public $cyp_options;

	/**
	 * Constructor
	 */
	public function __construct() {
		$this->functions   = new Cyp_Functions();
		$this->cyp_options = get_option( 'cyp_options' );
	}

	/**
	 * Get main page
	 */
	public function cyp_get_main_page() {

		$tab = ( strlen( filter_input( 1, 'tab', FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ) > 0 ) ? filter_input( 1, 'tab', FILTER_SANITIZE_FULL_SPECIAL_CHARS ) : 'orders';
		
		?>
		<div class="cyp-main">
		<?php
		$this->cyp_get_top_header();

		switch ( $tab ) {
			case 'orders':
				$this->cyp_get_table();
				break;
			case 'products':
				$this->cyp_get_table_products();
				break;
			case 'customers':
				$this->cyp_rep_get_customer_table();
				break;
			default:
				$this->cyp_get_table();
				break;
		}

		?>
		</div>
		<?php
	}

	/**
	 * Get header (tabs), filters and search
	 */
	public function cyp_get_top_header() {

		$range     = ( strlen( filter_input( 1, 'range', FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ) > 0 ) ? filter_input( 1, 'range', FILTER_SANITIZE_FULL_SPECIAL_CHARS ) : 'this_month';
		$tab       = ( strlen( filter_input( 1, 'tab', FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ) > 0 ) ? filter_input( 1, 'tab', FILTER_SANITIZE_FULL_SPECIAL_CHARS ) : 'orders';
		$date_f    = ( strlen( filter_input( 1, 'date_from', FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ) > 0 ) ? filter_input( 1, 'date_from', FILTER_SANITIZE_FULL_SPECIAL_CHARS ) : '';
		$date_t    = ( strlen( filter_input( 1, 'date_to', FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ) > 0 ) ? filter_input( 1, 'date_to', FILTER_SANITIZE_FULL_SPECIAL_CHARS ) : '';
		$customers = ( strlen( filter_input( 1, 'customers', FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ) > 0 ) ? filter_input( 1, 'customers', FILTER_SANITIZE_FULL_SPECIAL_CHARS ) : '';
		$statuses  = ( filter_input( 1, 'statuses', FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ) ? filter_input( 1, 'statuses', FILTER_SANITIZE_FULL_SPECIAL_CHARS ) : '';
		$url       = admin_url( 'admin.php?page=crw&view=rep' );
		$refresh   = $this->cyp_options['cyp_report_order_refresh'];
		$cust_str  = '';
		$stat_str  = '';

		$date_range = '&range=' . $range;

		if ( ! empty( $date_f ) || ! empty( $date_t ) ) {
			$date_range = '&date_from=' . $date_f . '&date_to=' . $date_t;
			$range      = '';
		}
		if ( ! empty( $customers ) ) {
			$cust_str = '&customers=' . $customers;
		}
		if ( ! empty( $statuses ) ) {
			$stat_str = '&statuses=' . $statuses;
		}

		// Toggle stats title
		$stats_txt = ( 'products' === $tab ) ? __( 'Toggle Summary', 'woo-calculate-your-price' ) : __( 'Toggle Order Totals', 'woo-calculate-your-price' );
		
		?>
		<div class="wrap">
			<h1 class="wp-heading-inline"><?php esc_html_e( 'Cost & Reports', 'woo-calculate-your-price' ); ?></h1>
		</div>
		<div>
			<?php $this->functions->cyp_get_page_tab_menu(); ?>
			<div class="cyp-in-flexll" style="margin-top:15px;margin-bottom:15px;padding-left:6px;">
				<span>
					<?php
					$order_count = wc_processing_order_count();
					?>
					<?php wp_nonce_field( 'footer_id' ); ?>
					<input type="hidden" id="cyp-auto-orders-value" value="<?php echo esc_attr( number_format_i18n( $order_count ) ); ?>">
					<?php esc_html_e( 'New orders:', 'woo-calculate-your-price' ); ?> <span class="cyp_circle" id="cyp-auto-orders-value-out"><?php echo esc_attr( number_format_i18n( $order_count ) ); ?></span>
					<span style="margin-left: 10px;color:gray;"><input type="checkbox" id="cyp-auto-orders" <?php echo esc_attr( 'yes' === $refresh ) ? 'checked="checked"' : ''; ?> disabled> <?php esc_html_e( 'Refresh on new orders', 'woo-calculate-your-price' ); ?></span>
				</span>
				<div style="float:right;">
					<input type="search" class="input-txt-search" oninput="reportFilter('<?php echo esc_js( $tab ); ?>');" onmouseover="<?php echo esc_js( $this->functions->cyp_get_autofocus_value( '' ) ); ?>" placeholder="<?php esc_html_e( 'search in rows', 'woo-calculate-your-price' ); ?>" id="report_search" name="report_search">
				</div>
			</div>
			
			<ul class="subsubsub cas-ul-box">
				<li style="width:230px;">
					<?php $this->functions->cyp_get_all_customers_filter(); ?>
				</li>
				<li style="width:200px;">
					<?php self::cyp_get_order_statuses_dropdown(); ?>
				</li>
				<li style="width:260px;">
					<form method="GET" action="admin.php" class="cyp-in-flex" autocomplete="off" id="cyp-preset-dates" style="position:relative;top:-2px;">
						<input type="hidden" name="page" value="crw">
						<input type="hidden" name="view" value="rep">
						<input type="hidden" name="tab" value="<?php esc_attr_e( $tab ); ?>">
						<input type="hidden" name="customers" value="<?php echo esc_attr( $customers ); ?>">
						<input type="hidden" name="statuses" value="<?php echo esc_attr( $statuses ); ?>">
						<select name="range" id="cyp-select-range-filter">
							<option value=""><?php esc_attr_e( 'Select Date Range', 'woo-calculate-your-price' ); ?></option>
							<option value="today" <?php selected( 'today', $range, true ); ?>><?php esc_attr_e( 'Today', 'woo-calculate-your-price' ); ?></option>
							<option value="week" <?php selected( 'week', $range, true ); ?>><?php esc_attr_e( 'Last 7 days', 'woo-calculate-your-price' ); ?></option>
							<option value="this_month" <?php selected( 'this_month', $range, true ); ?>><?php esc_attr_e( 'This month', 'woo-calculate-your-price' ); ?></option>
							<option value="last_month" <?php selected( 'last_month', $range, true ); ?>><?php esc_attr_e( 'Last month', 'woo-calculate-your-price' ); ?></option>
							<option value="last_3_month" <?php selected( 'last_3_month', $range, true ); ?>><?php esc_attr_e( 'Last 3 months', 'woo-calculate-your-price' ); ?></option>
							<option value="last_6_month" <?php selected( 'last_6_month', $range, true ); ?>><?php esc_attr_e( 'Last 6 months', 'woo-calculate-your-price' ); ?></option>
							<option value="last_12_month" <?php selected( 'last_12_month', $range, true ); ?>><?php esc_attr_e( 'Last 12 months', 'woo-calculate-your-price' ); ?></option>
							<option value="prev_year" <?php selected( 'prev_year', $range, true ); ?>><?php esc_attr_e( 'Previous Year', 'woo-calculate-your-price' ); ?></option>
							<option value="year" <?php selected( 'year', $range, true ); ?>><?php esc_attr_e( 'Year ( January - Today )', 'woo-calculate-your-price' ); ?></option>
						</select>
						<button type="submit" class="button" onclick="submit();" style="margin-left:5px;padding-top:3px;padding-bottom:4px;" title="<?php esc_attr_e( 'Get: Customers + Statuses in the Selected Date Range. Default this month, all customers and statuses.', 'woo-calculate-your-price' ); ?>"><?php esc_attr_e( 'Get', 'woo-calculate-your-price' ); ?></button>

					</form>
				</li>
				<li>
					<?php esc_attr_e( 'Custom:', 'woo-calculate-your-price' ); ?>
					<form method="GET" action="admin.php" class="cyp-in-flex" id="cyp-custom-dates" autocomplete="off">
						<input type="hidden" name="page" value="crw">
						<input type="hidden" name="view" value="rep">
						<input type="hidden" name="tab" value="<?php esc_attr_e( $tab ); ?>">
						<input type="hidden" name="customers" value="<?php echo esc_attr( $customers ); ?>">
						<input type="hidden" name="statuses" value="<?php echo esc_attr( $statuses ); ?>">
						<input type="text" class="input-date" name="date_from" id="date_from" placeholder="<?php ( $date_f ) ? esc_attr_e( $date_f ) : esc_attr_e( 'Date From', 'woo-calculate-your-price' ); ?>" value="<?php echo esc_attr( $date_f ); ?>">
						<input type="text" class="input-date" name="date_to" id="date_to" placeholder="<?php ( $date_t ) ? esc_attr_e( $date_t ) : esc_attr_e( 'Date To', 'woo-calculate-your-price' ); ?>"  value="<?php echo esc_attr( $date_t ); ?>">
						<button type="submit" id="btn-go" class="button" style="margin-left: 5px;" title="<?php esc_attr_e( 'Get: Customers + Statuses in Your Custom Date Range', 'woo-calculate-your-price' ); ?>"> <?php esc_attr_e( 'Get Range', 'woo-calculate-your-price' ); ?></button>
						<button type="button" class="button" onclick="cypFilterReset();" style="margin-left: 4px;" title="<?php esc_attr_e( 'Reset all filters', 'woo-calculate-your-price' ); ?>"><?php esc_attr_e( 'Reset', 'woo-calculate-your-price' ); ?></button>
					</form>
				</li>
				<li style="padding-left:10px;">
					<?php if ( 'customers' !== $tab ) : ?>
						<form method="post" id="download_report" action="<?php esc_attr_e( admin_url( 'admin-post.php?csv=true&tab=' . $tab . $date_range . $cust_str . $stat_str ) ); ?>">
							<input type="hidden" name="action" value="return_report_csv_file">
							<button type="submit" name="download_report" class="button" style="margin-top:-3px;margin-left:2px;" title="<?php esc_attr_e( 'Download (.csv)', 'woo-calculate-your-price' ); ?>">
							<i class="fas fa-cloud-download-alt"></i> 
							</button>
						</form>
					<?php endif; ?>
				</li>
				<li>
					<button class="button" onclick="cyp_export_html_table('<?php echo esc_js( $tab ); ?>');" style="margin-top:-3px;margin-left:2px;" title="<?php esc_attr_e( 'Export table', 'woo-calculate-your-price' ); ?>"> <i class="fas fa-file-export"></i> </button>
				</li>
				<?php if ( 'customers' !== $tab ) : ?>
					<li>
						<button class="button" id="cas-toggle-totals-summary" style="margin-top:-3px;margin-left:2px;color:firebrick;" title="<?php echo esc_attr( $stats_txt ); ?>"><i class="fa-solid fa-chart-simple"></i></button>
					</li>
				<?php endif; ?>
			</ul>
			
		</div>
		<?php
	}

	/**
	 * Get Orders Report (table)
	 */
	public function cyp_get_table() {
		
		$results       = $this->functions->cyp_get_orders_report();
		$orders        = $results->orders;
		$cyp_options   = get_option( 'cyp_options' );
		$profit_good   = $cyp_options['cyp_good_profit_percent'];
		$profit_bad    = $cyp_options['cyp_bad_profit_percent'];
		$date_format   = get_option( 'date_format' );
		$time_format   = get_option( 'time_format' );
		$t_sep         = get_option( 'woocommerce_price_thousand_sep' );
		$d_point       = get_option( 'woocommerce_price_decimal_sep' );
		$decimals      = get_option( 'woocommerce_price_num_decimals' );
		$cog_field     = ( $cyp_options['cyp_cost_of_goods_field'] ) ? $cyp_options['cyp_cost_of_goods_field'] : '_cost_of_goods';
		$p_price_field = ( $cyp_options['cyp_purchase_price_field'] ) ? $cyp_options['cyp_purchase_price_field'] : '_purchase_price';
		$page_num      = ( strlen( filter_input( 1, 'paged', FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ) > 0 ) ? filter_input( 1, 'paged', FILTER_SANITIZE_FULL_SPECIAL_CHARS ) : 1;
		$is_mod_date   = ( $cyp_options['cyp_order_date_modified'] && 'yes' === $cyp_options['cyp_order_date_modified'] ) ? true : false;
		
		?>
		<?php $this->cyp_get_paging( $page_num, $results->max_num_pages ); ?>
		
		<div class="cyp-r-row">
		
		<div class="cyp-r-column cyp-r-left" id="div-table-report">
		<table id="table-report" class="wp-list-table widefat fixed striped posts">
			<?php $this->cyp_rep_get_table_header(); ?>
			<tbody>
		<?php

		$total_gross      = 0;
		$total_profit     = 0;
		$total_percent    = 0;
		$total_purchase   = 0;
		$total_items      = 0;
		$total_pp         = 0;
		$total_cog        = 0;
		$total_taxes      = 0;
		$tot_shipping     = 0;
		$orders_gross     = 0;
		$total_refund     = 0;
		$total_refund_qty = 0;
		$refund_total_sum = 0;

		foreach ( $orders as $order ) {

			$profit            = 0;
			$cost_of_goods     = 0;
			$items_total       = 0;
			$order_status      = $order->get_status();
			$items_refund      = 0;
			$qty_refunded      = 0;
			$order_refunded    = 'refunded' === $order_status;
			$refund_total      = $order->get_total_refunded(); // Get total refunded amount for the order - Inc VAT
			$refund_total_sum += $refund_total;

			//Do not include trash orders
			if ( 'trash' === $order_status ) {
				continue;
			}

			foreach ( $order->get_items() as $item ) { 

				$id      = ( $item->get_variation_id() > 0 ) ? $item->get_variation_id() : $item->get_product_id();
				$product = wc_get_product( $id );
				$cost    = '';

				// 1) Prefer stored order item snapshot (historical accuracy)
				$snapshot_cost = $item->get_meta( '_cost_of_goods', true );

				if ( '' !== $snapshot_cost && null !== $snapshot_cost ) {
					$cost = (float) $snapshot_cost;
				} else {

					// 2) Optional: if WooCommerce native COGS is used AND stored on item, prefer that
					// (Only if your site/woo version supports it and you previously set it.)
					if ( is_callable( [ $item, 'get_cogs_value' ] ) ) {
						$item_cogs = $item->get_cogs_value();
						if ( '' !== $item_cogs && null !== $item_cogs ) {
							$cost = (float) $item_cogs;
						}
					}

					// 3) Last resort fallback: product cost (NOT historical)
					if ( '' === $cost || null === $cost ) {
						if ( '_cogs_value' === $cog_field && $product && is_callable( [ $product, 'get_cogs_value' ] ) ) {
							$cost = (float) $product->get_cogs_value();
						} else {
							$cost = (float) get_post_meta( $id, $cog_field, true );
						}
					}
				}

				// Ensure cost is a number and not empty/null to avoid calculation issues
				if ( '' === $cost || null === $cost ) {
					$cost = 0;
				}

				$purchase        = $item->get_meta( $p_price_field ) ? $item->get_meta( $p_price_field ) : get_post_meta( $id, $p_price_field, true );
				$cost_of_goods  += ( $cost && $item->get_quantity() > 0 ) ? (float) $cost * (int) $item->get_quantity() : 0;
				$total_purchase += ( $purchase && $item->get_quantity() > 0 ) ? (float) $purchase * (int) $item->get_quantity() : 0;
				$items_total    += floatval( $item->get_total() ); // ex.VAT
				$items_refund   += ( $order_refunded ) ? $item->get_total() : $order->get_total_refunded_for_item( $item->get_id() );
				$qty_refunded   += ( $order_refunded ) ? -abs( $item->get_quantity() ) : $order->get_qty_refunded_for_item( $item->get_id() );
	
			}
			
			//Calculate
			$total_items      += $order->get_item_count();
			$total_gross      += $items_total - $items_refund; // ex.VAT

			$profit            = ( $cost_of_goods > 0 ) ? floatval( $items_total - $refund_total ) - floatval( $cost_of_goods ) : 0;
			$percent           = ( $profit > 0 ) ? floatval( $profit ) * 100 / floatval( $items_total - $refund_total ) : 0;
			$total_profit     += $profit;
			$total_percent    += $percent;
			$total_pp          = ( $total_profit > 0 ) ? floatval( $total_profit ) * 100 / floatval( $total_gross ) : 0;
			$total_cog        += $cost_of_goods;

			$customer_name     = ( $order->get_billing_first_name() && $order->get_billing_last_name() ) ? $order->get_billing_first_name() . ' ' . $order->get_billing_last_name() : '';
			$total_taxes      += ( $order_refunded ) ? 0 : $order->get_total_tax();
			$tot_shipping     += ( $order_refunded ) ? 0 : max( 0, $order->get_shipping_total() );
			$order_date        = ( $is_mod_date ) ? $order->get_date_modified() : $order->get_date_created();
			$orders_gross     += $order->get_total() - $refund_total; // inc.VAT
			$total_refund     += $items_refund; 
			$total_refund_qty += $qty_refunded;
			
			?>
			<tr class="lozad" data-exclude="">
				<td><a href="post.php?post=<?php echo esc_attr( $order->get_id() ); ?>&action=edit"><?php echo esc_attr( $order->get_id() ); ?></a></td>
				<td><a href="user-edit.php?user_id=<?php echo esc_attr( $order->get_customer_id() ); ?>"><?php echo esc_attr( $customer_name ); ?></a></td>
				<?php if ( 'yes' === $cyp_options['cyp_orders_show_role'] ) : ?>
					<td><?php esc_attr_e( $this->functions->cyp_get_order_role( $order->get_customer_id() ) ); ?></td>
				<?php endif; ?>
				<?php if ( 'yes' === $cyp_options['cyp_orders_show_company'] ) : ?>
					<td><?php echo esc_attr( null !== $order->get_billing_company() ? $order->get_billing_company() : ''  ); ?></td>
				<?php endif; ?>
				<?php if ( 'yes' === $cyp_options['cyp_custom_1_orders'] && strlen( $cyp_options['cyp_custom_field_1'] ) > 0 ) : ?>
					<td><?php echo esc_attr( $order->get_meta( $cyp_options['cyp_custom_field_1'] ) ); ?></td>
				<?php endif; ?>
				<?php if ( 'yes' === $cyp_options['cyp_custom_2_orders'] && strlen( $cyp_options['cyp_custom_field_2'] ) > 0 ) : ?>
					<td><?php echo esc_attr( $order->get_meta( $cyp_options['cyp_custom_field_2'] ) ); ?></td>
				<?php endif; ?>
				<td data-t="d" style="font-size:smaller;"><?php echo esc_attr( date_format( new DateTime( $order_date ), $date_format ) ); ?></td>
				<td>
					<span class="order-status status-<?php echo esc_attr( $order->get_status() ); ?>"><?php echo esc_attr( wc_get_order_status_name( $order->get_status() ) ); ?></span>
				</td>
				<td data-field="qty" class="cyp-center"><?php echo esc_attr( $order->get_item_count() ); ?></td>
				<td class="cyp-center" style="padding-top:6px;">
					<span data-field="refund" class="<?php echo esc_attr( 0 !== intval( $qty_refunded ) ? 'cyp-refund-qty' : '' ); ?>" title="<?php esc_html_e( 'Net Refunded:', 'woo-calculate-your-price' ); ?> <?php echo esc_attr( wc_format_decimal( $items_refund, 2 ) ); ?>">
						<?php echo esc_attr( 0 !== intval( $qty_refunded ) ? $qty_refunded : '' ); ?>
					</span>
				</td>
				<td data-field="profit" class="cyp-right"><?php echo esc_attr( number_format( (float) $profit, $decimals, $d_point, $t_sep ) ); ?></td>
				<td data-field="profit_percent" class="cyp-right <?php echo ( (float) $percent >= $profit_good ) ? 'cyp-good-profit' : ''; ?>
					<?php echo ( (float) $percent <= $profit_bad ) ? 'cyp-bad-profit' : ''; ?>"><?php echo esc_attr( number_format( (float) $percent, 2, $d_point, $t_sep ) ); ?>
				</td>
				<td data-field="cogs" class="cyp-right"><?php echo esc_attr( number_format( (float) $cost_of_goods, $decimals, $d_point, $t_sep ) ); ?></td>
				<td data-field="net_sales" class="cyp-right"><?php echo esc_attr( number_format( (float) $items_total - $items_refund, $decimals, $d_point, $t_sep ) ); ?></td>
				<td data-field="tax" class="cyp-right"><?php echo esc_attr( number_format( ( $order_refunded ) ? 0 : (float) $order->get_total_tax(), $decimals, $d_point, $t_sep ) ); ?></td>
				<td data-field="shipping" class="cyp-right"><?php echo esc_attr( number_format( ( $order_refunded ) ? 0 : (float) $order->get_shipping_total(), $decimals, $d_point, $t_sep ) ); ?></td>
				<td data-field="gross" class="cyp-right"><?php echo esc_attr( number_format( ( $order_refunded ) ? 0 : (float) $order->get_total(), $decimals, $d_point, $t_sep ) ); ?></td>
			</tr>
			<?php
		}

		?>
			<tfoot>
				<tr>
					<td></td>
					<td></td>
					<?php if ( 'yes' === $cyp_options['cyp_orders_show_role'] ) : ?>
						<td></td>
					<?php endif; ?>
					<?php if ( 'yes' === $cyp_options['cyp_orders_show_company'] ) : ?>
						<td></td>
					<?php endif; ?>
					<?php if ( 'yes' === $cyp_options['cyp_custom_1_orders'] && strlen( $cyp_options['cyp_custom_field_1'] ) > 0 ) : ?>
						<td></td>
					<?php endif; ?>
					<?php if ( 'yes' === $cyp_options['cyp_custom_2_orders'] && strlen( $cyp_options['cyp_custom_field_2'] ) > 0 ) : ?>
						<td></td>
					<?php endif; ?>
					<td></td>
					<td></td>
					<td class="cyp-center" id="rep-ord-items"><?php echo esc_attr( number_format( (int) $total_items, 0, $d_point, $t_sep ) ); ?></td>
					<td class="cyp-center" id="rep-refund-items"><?php echo esc_attr( number_format( (int) $total_refund_qty, 0, $d_point, $t_sep ) ); ?></td>
					<td class="cyp-right" id="rep-ord-profit" data-value="<?php echo esc_attr( $total_profit ); ?>"><?php echo esc_attr( wp_strip_all_tags( wc_price( $total_profit ) ) ); ?></td>
					<td></td>
					<td class="cyp-right" id="rep-ord-cogs" data-value="<?php echo esc_attr( $total_cog ); ?>"><?php echo esc_attr( wp_strip_all_tags( wc_price( $total_cog ) ) ); ?></td>
					<td class="cyp-right" id="rep-ord-net" data-value="<?php echo esc_attr( $total_gross ); ?>"><?php echo esc_attr( wp_strip_all_tags( wc_price( $total_gross ) ) ); ?></td>
					<td class="cyp-right" id="rep-ord-tax" data-value="<?php echo esc_attr( $total_taxes ); ?>"><?php echo esc_attr( wp_strip_all_tags( wc_price( $total_taxes ) ) ); ?></td>
					<td class="cyp-right" id="rep-ord-ship" data-value="<?php echo esc_attr( $tot_shipping ); ?>"><?php echo esc_attr( wp_strip_all_tags( wc_price( $tot_shipping ) ) ); ?></td>
					<td class="cyp-right" id="rep-ord-gross" data-value="<?php echo esc_attr( $orders_gross ); ?>"><?php echo esc_attr( wp_strip_all_tags( wc_price( $orders_gross ) ) ); ?></td>
				</tr>
			</tfoot>
			</tbody>
		</table>
		</div>
			<div class="cyp-r-column cyp-r-right" id="cas-div-order-totals">
				<div class="cyp-total-group">
				<div class="cyp-total-item" style="border-right: 5px solid gold;">
					<h2>
						<?php echo wp_kses( wc_price( $orders_gross ), 'post' ); ?>
					</h2>	
					<span><?php esc_html_e( 'gross sales in this period', 'woo-calculate-your-price' ); ?></span>
				</div>
				<div class="cyp-total-item" style="border-right: 5px solid #b1d4ea;">
					<h2>
						<?php echo wp_kses( wc_price( $total_gross ), 'post' ); ?>
					</h2>	
					<span><?php esc_html_e( 'net sales in this period', 'woo-calculate-your-price' ); ?></span>
				</div>
				<div class="cyp-total-item" style="border-right: 5px solid #b1d4ea;">
					<h2>
						<?php echo wp_kses( wc_price( $total_profit ), 'post' ); ?>
					</h2>	
					<span><?php esc_html_e( 'profit in this period', 'woo-calculate-your-price' ); ?></span>
				</div>
				<div class="cyp-total-item" style="border-right: 5px solid firebrick;">
					<h2 class="cas-report-refund-h2">
						<?php echo wp_kses( wc_price( $refund_total_sum ), 'post' ); ?>
					</h2>	
					<span class="cas-report-refund-h2"><?php esc_html_e( 'gross refunded in this period', 'woo-calculate-your-price' ); ?></span>
				</div>
				<div class="cyp-total-item" style="border-right: 5px solid firebrick;">
					<h2 class="cas-report-refund-h2">
						<?php echo wp_kses( wc_price( $total_refund ), 'post' ); ?>
					</h2>	
					<span class="cas-report-refund-h2"><?php esc_html_e( 'net refunded in this period', 'woo-calculate-your-price' ); ?></span>
				</div>
				<div class="cyp-total-item" style="border-right: 5px solid #3498db;">
					<h2>
						<?php echo esc_attr( number_format( (float) $total_pp, $decimals, $d_point, $t_sep ) . '%' ); ?>
					</h2>	
					<span><?php esc_html_e( 'profit this period', 'woo-calculate-your-price' ); ?></span>
				</div>
				<div class="cyp-total-item" style="border-right: 5px solid #3498db;">
					<h2>
						<?php echo wp_kses( wc_price( $total_taxes ), 'post' ); ?>
					</h2>	
					<span><?php esc_html_e( 'taxes this period', 'woo-calculate-your-price' ); ?></span>
				</div>
				<div class="cyp-total-item" style="border-right: 5px solid khaki;">
					<h2>
						<?php echo wp_kses( wc_price( $tot_shipping ), 'post' ); ?>
					</h2>	
					<span><?php esc_html_e( 'shipping this period', 'woo-calculate-your-price' ); ?></span>
				</div>
				<div class="cyp-total-item" style="border-right: 5px solid #3498db;">
					<h2>
						<?php echo ( (float) $total_percent > 0 && $results->total > 0 ) ? esc_attr( number_format( (float) $total_percent / $results->total, $decimals, $d_point, $t_sep ) . '%' ) : '0%'; ?>
					</h2>	
					<span><?php esc_html_e( 'average profit per order', 'woo-calculate-your-price' ); ?></span>
				</div>
				<div class="cyp-total-item" style="border-right: 5px solid lightsalmon;">
					<h2>
						<?php echo ( (float) $total_profit > 0 && $results->total > 0 ) ? esc_attr( wp_strip_all_tags( wc_price( $total_profit / $results->total ) ) ) : '0'; ?>
					</h2>	
					<span><?php esc_html_e( 'average profit per order', 'woo-calculate-your-price' ); ?></span>
				</div>
				<div class="cyp-total-item" style="border-right: 5px solid darkseagreen;">
					<h2>
						<?php echo wp_kses( wc_price( $total_cog ), 'post' ); ?>
					</h2>	
					<span><?php esc_html_e( 'cost of goods for orders', 'woo-calculate-your-price' ); ?></span>
				</div>
				<div class="cyp-total-item" style="border-right: 5px solid orange;">
					<h2>
						<?php echo wp_kses( wc_price( $total_purchase ), 'post' ); ?>
					</h2>	
					<span><?php esc_html_e( 'purchase cost for orders', 'woo-calculate-your-price' ); ?></span>
				</div>
				<div class="cyp-total-item" style="border-right: 5px solid #dbe1e3;">
					<h2>
						<?php echo esc_attr( number_format( (int) $results->total, 0, $d_point, $t_sep ) ); ?>
					</h2>	
					<span><?php esc_html_e( 'orders placed', 'woo-calculate-your-price' ); ?></span>
				</div>
				<div class="cyp-total-item" style="border-right: 5px solid #dbe1e3;">
					<h2>
						<?php echo esc_attr( number_format( (int) $total_items, 0, $d_point, $t_sep ) ); ?>
					</h2>	
					<span><?php esc_html_e( 'items sold', 'woo-calculate-your-price' ); ?></span>
				</div>
				<div class="cyp-total-item" style="border-right: 5px solid firebrick;">
					<h2 class="cas-report-refund-h2">
						<?php echo esc_attr( number_format( (int) $total_refund_qty, 0, $d_point, $t_sep ) ); ?>
					</h2>	
					<span class="cas-report-refund-h2"><?php esc_html_e( 'items refunded', 'woo-calculate-your-price' ); ?></span>
				</div>
				</div>
					
			</div>
		</div>

		<?php $this->cyp_get_paging( $page_num, $results->max_num_pages ); ?>

		<?php

	}

	/**
	 * Get Orders table header
	 */
	public function cyp_rep_get_table_header() {
		$options   = get_option( 'cyp_options' );
		$col_count = 1;
		?>
		<thead>
			<tr class="head-avr-back">
				<th class="head-avr" style="width: 80px;"><?php esc_html_e( 'Average', 'woo-calculate-your-price' ); ?></th>
				<th class="head-avr"></th>
				<?php if ( 'yes' === $options['cyp_orders_show_role'] ) : ?>
					<th class="head-avr"></th>
				<?php endif; ?>
				<?php if ( 'yes' === $options['cyp_orders_show_company'] ) : ?>
					<th class="head-avr"></th>
				<?php endif; ?>
				<?php if ( 'yes' === $options['cyp_custom_1_orders'] && strlen( $options['cyp_custom_field_1'] ) > 0 ) : ?>
					<th class="head-avr"></th>
				<?php endif; ?>
				<?php if ( 'yes' === $options['cyp_custom_2_orders'] && strlen( $options['cyp_custom_field_2'] ) > 0 ) : ?>
					<th class="head-avr"></th>
				<?php endif; ?>
				<th class="head-avr"></th>
				<th class="head-avr"></th>
				<th class="head-avr cyp-center" id="o_avr_qty"></th>
				<th class="head-avr" style="width:70px;"></th>
				<th class="head-avr cyp-right" id="o_avr_profit"></th>
				<th class="head-avr cyp-right" id="o_avr_percent"></th>
				<th class="head-avr cyp-right" id="o_avr_cost"></th>
				<th class="head-avr cyp-right" id="o_avr_sale"></th>
				<th class="head-avr cyp-right" id="o_avr_tax"></th>
				<th class="head-avr cyp-right" id="o_avr_ship"></th>
				<th class="head-avr cyp-right" id="o_avr_gross"></th>
			</tr>
			<tr>
				<th><a href="#" onclick="cypSortColumns(0,'number','r','table-report');"><i id="r0" class="fas fa-sort"></i> <?php esc_html_e( 'Order id', 'woo-calculate-your-price' ); ?></a></th>
				<th><a href="#" onclick="cypSortColumns(1,'text','r','table-report');"><i id="r1" class="fas fa-sort"></i> <?php esc_html_e( 'Customer', 'woo-calculate-your-price' ); ?></a></th>
				<?php if ( 'yes' === $options['cyp_orders_show_role'] ) : ?>
					<?php $col_count++; ?>
					<th>
						<a href="#" onclick="cypSortColumns(<?php echo esc_attr( $col_count ); ?>,'text','r','table-report');"><i id="r<?php echo esc_attr( $col_count ); ?>" class="fas fa-sort"></i> <?php esc_html_e( 'Role', 'woo-calculate-your-price' ); ?></a>
					</th>
				<?php endif; ?>
				<?php if ( 'yes' === $options['cyp_orders_show_company'] ) : ?>
					<?php $col_count++; ?>
					<th>
						<a href="#" onclick="cypSortColumns(<?php echo esc_attr( $col_count ); ?>,'text','r','table-report');"><i id="r<?php echo esc_attr( $col_count ); ?>" class="fas fa-sort"></i> <?php esc_html_e( 'Company', 'woo-calculate-your-price' ); ?></a>
					</th>
				<?php endif; ?>
				<!-- CUSTOM FIELDS -->
				<?php if ( 'yes' === $options['cyp_custom_1_orders'] && strlen( $options['cyp_custom_field_1'] ) > 0 ) : ?>
					<?php $col_count++; ?>
					<th>
						<a href="#" onclick="cypSortColumns(<?php echo esc_attr( $col_count ); ?>,'text','r','table-report');"><i id="r<?php echo esc_attr( $col_count ); ?>" class="fas fa-sort"></i> <?php esc_html_e( $options['cyp_custom_field_1_lbl'] ); ?></a>
					</th>
				<?php endif; ?>
				<?php if ( 'yes' === $options['cyp_custom_2_orders'] && strlen( $options['cyp_custom_field_2'] ) > 0 ) : ?>
					<?php $col_count++; ?>
					<th>
						<a href="#" onclick="cypSortColumns(<?php echo esc_attr( $col_count ); ?>,'text','r','table-report');"><i id="r<?php echo esc_attr( $col_count ); ?>" class="fas fa-sort"></i> <?php esc_html_e( $options['cyp_custom_field_2_lbl'] ); ?></a>
					</th>
				<?php endif; ?>
				<th>
					<?php $col_count++; ?>
					<a href="#" onclick="cypSortColumns(<?php echo esc_attr( $col_count ); ?>,'date','r','table-report');"><i id="r<?php echo esc_attr( $col_count ); ?>" class="fas fa-sort"></i> <?php esc_html_e( 'Date', 'woo-calculate-your-price' ); ?></a>
				</th>
				<th>
					<?php $col_count++; ?>
					<a href="#" onclick="cypSortColumns(<?php echo esc_attr( $col_count ); ?>,'text','r','table-report');"><i id="r<?php echo esc_attr( $col_count ); ?>" class="fas fa-sort"></i> <?php esc_html_e( 'Status', 'woo-calculate-your-price' ); ?></a>
				</th>
				<th class="cyp-center" id="th-items">
					<?php $col_count++; ?>
					<a href="#" onclick="cypSortColumns(<?php echo esc_attr( $col_count ); ?>,'number','r','table-report');"><i id="r<?php echo esc_attr( $col_count ); ?>" class="fas fa-sort"></i> <?php esc_html_e( 'Items', 'woo-calculate-your-price' ); ?></a>
				</th>
				<th class="cyp-center" id="th-items">
					<?php $col_count++; ?>
					<a href="#" onclick="cypSortColumns(<?php echo esc_attr( $col_count ); ?>,'text','r','table-report');"><i id="r<?php echo esc_attr( $col_count ); ?>" class="fas fa-sort"></i> <?php esc_html_e( 'Refunded', 'woo-calculate-your-price' ); ?></a>
				</th>
				<th class="cyp-right" id="th-profit">
					<?php $col_count++; ?>	
					<a href="#" onclick="cypSortColumns(<?php echo esc_attr( $col_count ); ?>,'number','r','table-report');"><i id="r<?php echo esc_attr( $col_count ); ?>" class="fas fa-sort"></i> <?php esc_html_e( 'Profit', 'woo-calculate-your-price' ); ?></a>
				</th>
				<th class="cyp-right" id="th-profit-percent">
					<?php $col_count++; ?>	
					<a href="#" onclick="cypSortColumns(<?php echo esc_attr( $col_count ); ?>,'number','r','table-report');"><i id="r<?php echo esc_attr( $col_count ); ?>" class="fas fa-sort"></i> <?php esc_html_e( 'Profit %', 'woo-calculate-your-price' ); ?></a>
				</th>
				<th class="cyp-right" id="th-cogs">
					<?php $col_count++; ?>	
					<a href="#" onclick="cypSortColumns(<?php echo esc_attr( $col_count ); ?>,'number','r','table-report');"><i id="r<?php echo esc_attr( $col_count ); ?>" class="fas fa-sort"></i> <?php esc_html_e( 'Cost of goods', 'woo-calculate-your-price' ); ?></a></th>
				<th class="cyp-right" id="th-net-sales">
					<?php $col_count++; ?>	
					<a href="#" onclick="cypSortColumns(<?php echo esc_attr( $col_count ); ?>,'number','r','table-report');"><i id="r<?php echo esc_attr( $col_count ); ?>" class="fas fa-sort"></i> <?php esc_html_e( 'Net sales', 'woo-calculate-your-price' ); ?></a></th>
				<th class="cyp-right" id="th-tax">
					<?php $col_count++; ?>	
					<a href="#" onclick="cypSortColumns(<?php echo esc_attr( $col_count ); ?>,'number','r','table-report');"><i id="r<?php echo esc_attr( $col_count ); ?>" class="fas fa-sort"></i> <?php esc_html_e( 'Tax', 'woo-calculate-your-price' ); ?></a></th>
				<th class="cyp-right" id="th-shipping">
					<?php $col_count++; ?>	
					<a href="#" onclick="cypSortColumns(<?php echo esc_attr( $col_count ); ?>,'number','r','table-report');"><i id="r<?php echo esc_attr( $col_count ); ?>" class="fas fa-sort"></i> <?php esc_html_e( 'Shipping', 'woo-calculate-your-price' ); ?></a>
				</th>
				<th class="cyp-right" id="th-gross">
					<?php $col_count++; ?>	
					<a href="#" onclick="cypSortColumns(<?php echo esc_attr( $col_count ); ?>,'number','r','table-report');"><i id="r<?php echo esc_attr( $col_count ); ?>" class="fas fa-sort"></i> <?php esc_html_e( 'Gross', 'woo-calculate-your-price' ); ?></a>
				</th>
			</tr>
		</thead>
		<?php
	}

	/**
	 * Get paging
	 */
	public function cyp_get_paging( $page_num, $max_num_pages ) {
		?>
		<div class="div-paging">
			<span class="cas-pagination">
			<?php
				$args = array( // WPCS: XSS ok.
					'base'      => '%_%',
					'format'    => '?paged=%#%',
					'current'   => $page_num,
					'total'     => $max_num_pages,
					'prev_text' => is_rtl() ? '&rarr;' : '&larr;',
					'next_text' => is_rtl() ? '&larr;' : '&rarr;',
					'type'      => 'plain',
					'end_size'  => 1,
					'mid_size'  => 1,
				);
				echo wp_kses( paginate_links( $args ), 'post' );
				?>
			</span>
		</div>
		<?php
	}

	/**
	 * Get table report - products tab
	 */
	public function cyp_get_table_products() {
		
		$results     = $this->functions->cyp_get_orders_report();
		$orders      = $results->orders;
		$cyp_options = get_option( 'cyp_options' );
		$profit_good = $cyp_options['cyp_good_profit_percent'];
		$profit_bad  = $cyp_options['cyp_bad_profit_percent'];
		$t_sep       = get_option( 'woocommerce_price_thousand_sep' );
		$d_point     = get_option( 'woocommerce_price_decimal_sep' );
		$decimals    = get_option( 'woocommerce_price_num_decimals' );
		$cog_field   = ( $cyp_options['cyp_cost_of_goods_field'] ) ? $cyp_options['cyp_cost_of_goods_field'] : '_cost_of_goods';
		$page_num    = ( ! empty( filter_input( 1, 'paged', FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ) ) ? filter_input( 1, 'paged', FILTER_SANITIZE_FULL_SPECIAL_CHARS ) : 1;
		$options     = get_option( 'cyp_options' );
		$weight_unit = get_option( 'woocommerce_weight_unit' );
		
		?>
		<?php $this->cyp_get_paging( $page_num, $results->max_num_pages ); ?>
		
		<div class="cyp-r-row">
		
		<div class="cyp-r-column cyp-r-left">
		<table id="table-report" class="wp-list-table widefat fixed striped posts">
		<?php $this->cyp_rep_get_table_header_products(); ?>
		<tbody>
		<?php

		foreach ( $orders as $order ) {
			
			$profit        = 0;
			$cost_of_goods = 0;
			$is_refunded   = 'refunded' === $order->get_status();

			//Do not include trash orders
			if ( 'trash' === $order->get_status() ) {
				continue;
			}

			foreach ( $order->get_items() as $item ) {
				
				$id      = ( $item->get_variation_id() > 0 ) ? $item->get_variation_id() : $item->get_product_id();
				$product = wc_get_product( $id );
				$cost    = '';

				// 1) Prefer stored order item snapshot (historical accuracy)
				$snapshot_cost = $item->get_meta( '_cost_of_goods', true );

				if ( '' !== $snapshot_cost && null !== $snapshot_cost ) {
					$cost = (float) $snapshot_cost;
				} else {

					// 2) Optional: if WooCommerce native COGS is used AND stored on item, prefer that
					// (Only if your site/woo version supports it and you previously set it.)
					if ( is_callable( [ $item, 'get_cogs_value' ] ) ) {
						$item_cogs = $item->get_cogs_value();
						if ( '' !== $item_cogs && null !== $item_cogs ) {
							$cost = (float) $item_cogs;
						}
					}

					// 3) Last resort fallback: product cost (NOT historical)
					if ( '' === $cost || null === $cost ) {
						if ( '_cogs_value' === $cog_field && $product && is_callable( [ $product, 'get_cogs_value' ] ) ) {
							$cost = (float) $product->get_cogs_value();
						} else {
							$cost = (float) get_post_meta( $id, $cog_field, true );
						}
					}
				}

				// Ensure cost is a number and not empty/null to avoid calculation issues
				if ( '' === $cost || null === $cost ) {
					$cost = 0;
				}


				$cost_of_goods = ( $cost && (float) $cost > 0 && $item->get_quantity() > 0 ) ? (float) $cost * (int) $item->get_quantity() : 0;
				$profit        = ( $cost_of_goods > 0 ) ? (float) $item->get_total() - (float) $cost_of_goods : 0;
				$percent       = ( $profit > 0 ) ? (float) $profit * 100 / (float) $item->get_total() : 0;
				$price         = (float) $item->get_total() / (int) $item->get_quantity();
				$terms         = ( $item->get_variation_id() > 0 ) ? get_the_terms( $product->get_parent_id(), 'product_cat' ) : get_the_terms( $id, 'product_cat' );
				$categories    = '';
				$url           = admin_url( 'admin.php?page=crw&view=main&product_cat=%1$s' );
			
				if ( $terms ) {
					foreach ( $terms as $category ) {
						$categories .= '<a href="' . sprintf( esc_url( $url ), $category->slug ) . '">' . $category->name . '</a>, ';
					}
					$categories = substr( $categories, 0, strlen( $categories ) -2 );
				}
				
				$value_refund = ( $is_refunded ) ? $item->get_total() : $order->get_total_refunded_for_item( $item->get_id() );
				$qty_refunded = ( $is_refunded ) ? -abs( $item->get_quantity() ) : $order->get_qty_refunded_for_item( $item->get_id() );

				?>
				<tr class="lozad" data-exclude="">
					<td><a href="post.php?post=<?php echo esc_attr( $order->get_id() ); ?>&action=edit" class="<?php echo esc_attr( $is_refunded ? 'cyp-refund-qty' : '' ); ?>"><?php echo esc_attr( $order->get_id() ); ?></a></td>
					<td>
						<div class="cas-td-scroll">
							<?php echo wp_kses( $categories, 'post' ); ?>
						</div>
					</td>
					<td><?php echo esc_attr( $product && strlen( $this->functions->cyp_get_product_name( $product ) ) > 0 ? $this->functions->cyp_get_product_name( $product ) : $item->get_name() ); ?></td>
					<td style="text-transform:uppercase;"><?php echo esc_attr( $product ? $product->get_sku() : '' ); ?></td>
					<!-- CUSTOM FIELDS -->
					<?php if ( 'yes' === $options['cyp_custom_1_products'] && strlen( $options['cyp_custom_field_1'] ) > 0 ) : ?>
						<td><?php echo esc_attr( $order->get_meta( $cyp_options['cyp_custom_field_1'] ) ); ?></td>
					<?php endif; ?>
					<?php if ( 'yes' === $options['cyp_custom_2_products'] && strlen( $options['cyp_custom_field_2'] ) > 0 ) : ?>
						<td><?php echo esc_attr( $order->get_meta( $cyp_options['cyp_custom_field_2'] ) ); ?></td>
					<?php endif; ?>
					<td class="cyp-center" data-field="qty"><?php echo esc_attr( (int) $item->get_quantity() ); ?></td>
					<td class="cyp-center" style="padding-top:6px;">
						<span data-field="refund" class="<?php echo esc_attr( 0 !== intval( $qty_refunded ) ? 'cyp-refund-qty' : '' ); ?>" title="<?php esc_html_e( 'Net Refunded:', 'woo-calculate-your-price' ); ?> <?php echo esc_attr( number_format( (float) $value_refund, 2, $d_point, $t_sep ) ); ?>">
							<?php echo esc_attr( 0 !== intval( $qty_refunded ) ? $qty_refunded : '' ); ?> 
						</span>
					</td>
					<td class="cyp-right">
						<span data-field="refund_value">
							<?php echo esc_attr( $value_refund ? number_format( $value_refund, 2, $d_point, $t_sep ) : '' ); ?>
						</span>
					</td>
					<?php if ( 'yes' === $options['cyp_orders_show_weight'] ) : ?>
						<td class="cyp-center" data-field="weight"><?php echo esc_attr( ( $product && $product->get_weight() ) ? $item->get_quantity() * floatval( $product->get_weight() ) . ' ' . $weight_unit : '' ); ?></td>
					<?php endif; ?>
					<td class="cyp-right" data-field="price"><?php echo esc_attr( number_format( (float) $price, $decimals, $d_point, $t_sep ) ); ?></td>
					<td class="cyp-right" data-field="total"><?php echo esc_attr( number_format( $is_refunded ? 0 : (float) $item->get_total() - $value_refund, $decimals, $d_point, $t_sep ) ); ?></td>
					<td class="cyp-right" data-field="profit"><?php echo esc_attr( number_format( (float) $profit, $decimals, $d_point, $t_sep ) ); ?></td>
					<td data-field="profit_percent" class="cyp-right <?php echo ( (float) $percent >= $profit_good ) ? 'cyp-good-profit' : ''; ?><?php echo ( (float) $percent <= $profit_bad ) ? 'cyp-bad-profit' : ''; ?>"><?php echo esc_attr( number_format( (float) $percent, 2, $d_point, $t_sep ) ); ?></td>	
				</tr>
				<?php
			}
		}
		?>
		</tbody>
			<?php $this->cyp_rep_get_table_footer_products(); ?>
		</table>
		</div>
		<?php $this->cyp_rep_get_product_report_right( $results ); ?>
		<?php $this->cyp_get_paging( $page_num, $results->max_num_pages ); ?>
		
		<?php
	}

	/**
	 * Get table header - products
	 */
	public function cyp_rep_get_table_header_products() {
		$options   = get_option( 'cyp_options' );
		$col_count = 1;
		?>
		<thead>
			<tr class="head-avr-back">
				<th class="head-avr" style="width:70px;"><?php esc_html_e( 'Average', 'woo-calculate-your-price' ); ?></th>
				<th class="head-avr"></th>
				<th class="head-avr"></th>
				<th class="head-avr"></th>
				<!-- CUSTOM FIELDS -->
				<?php if ( 'yes' === $options['cyp_custom_1_products'] && strlen( $options['cyp_custom_field_1'] ) > 0 ) : ?>
					<th class="head-avr"></th>
				<?php endif; ?>
				<?php if ( 'yes' === $options['cyp_custom_2_products'] && strlen( $options['cyp_custom_field_2'] ) > 0 ) : ?>
					<th class="head-avr"></th>
				<?php endif; ?>
				<th class="head-avr cyp-center" id="avr_qty" style="width:55px;"></th>
				<th class="head-avr" style="width:70px;"></th>
				<th class="head-avr" style="width:100px;"></th>
				<?php if ( 'yes' === $options['cyp_orders_show_weight'] ) : ?>
					<th class="head-avr" id="avr_weight"></th>
				<?php endif; ?>
				<th class="head-avr cyp-right" id="avr_price"></th>
				<th class="head-avr cyp-right" id="avr_sale"></th>
				<th class="head-avr cyp-right" id="avr_profit"></th>
				<th class="head-avr cyp-right" id="avr_percent"></th>
			</tr>
			<tr>
				<th>
					<a href="#" onclick="cypSortColumns(0,'number','r','table-report');"><i id="r0" class="fas fa-sort"></i> <?php esc_html_e( 'Order id', 'woo-calculate-your-price' ); ?></a></th>
				<th>
					<a href="#" onclick="cypSortColumns(1,'text','r','table-report');"><i id="r1" class="fas fa-sort"></i> <?php esc_html_e( 'Category', 'woo-calculate-your-price' ); ?></a></th>
				<th>
					<?php $col_count++; ?>
					<a href="#" onclick="cypSortColumns(<?php echo esc_attr( $col_count ); ?>,'text','r','table-report');"><i id="r<?php echo esc_attr( $col_count ); ?>" class="fas fa-sort"></i> <?php esc_html_e( 'Product', 'woo-calculate-your-price' ); ?></a></th>
				<th>
					<?php $col_count++; ?>
					<a href="#" onclick="cypSortColumns(<?php echo esc_attr( $col_count ); ?>,'text','r','table-report');"><i id="r<?php echo esc_attr( $col_count ); ?>" class="fas fa-sort"></i> <?php esc_html_e( 'SKU', 'woo-calculate-your-price' ); ?></a></th>
				<!-- CUSTOM FIELDS -->
				<?php if ( 'yes' === $options['cyp_custom_1_products'] && strlen( $options['cyp_custom_field_1'] ) > 0 ) : ?>
					<?php $col_count++; ?>
					<th>
						<a href="#" onclick="cypSortColumns(<?php echo esc_attr( $col_count ); ?>,'text','r','table-report');"><i id="r<?php echo esc_attr( $col_count ); ?>" class="fas fa-sort"></i> <?php esc_html_e( $options['cyp_custom_field_1_lbl'] ); ?></a>
					</th>
				<?php endif; ?>
				<?php if ( 'yes' === $options['cyp_custom_2_products'] && strlen( $options['cyp_custom_field_2'] ) > 0 ) : ?>
					<?php $col_count++; ?>
					<th>
						<a href="#" onclick="cypSortColumns(<?php echo esc_attr( $col_count ); ?>,'text','r','table-report');"><i id="r<?php echo esc_attr( $col_count ); ?>" class="fas fa-sort"></i> <?php esc_html_e( $options['cyp_custom_field_2_lbl'] ); ?></a>
					</th>
				<?php endif; ?>

				<th class="cyp-center">
					<?php $col_count++; ?>	
					<a href="#" onclick="cypSortColumns(<?php echo esc_attr( $col_count ); ?>,'number','r','table-report');">
						<i id="r<?php echo esc_attr( $col_count ); ?>" class="fas fa-sort"></i> <?php esc_html_e( 'Qty', 'woo-calculate-your-price' ); ?>
					</a> 
				</th>
				<th class="cyp-center">
					<?php $col_count++; ?>	
					<a href="#" onclick="cypSortColumns(<?php echo esc_attr( $col_count ); ?>,'number','r','table-report');">
						<i id="r<?php echo esc_attr( $col_count ); ?>" class="fas fa-sort"></i> <?php esc_html_e( 'Refunded', 'woo-calculate-your-price' ); ?>
					</a> 
				</th>
				<th class="cyp-right">
					<?php $col_count++; ?>	
					<a href="#" onclick="cypSortColumns(<?php echo esc_attr( $col_count ); ?>,'text','r','table-report');">
						<i id="r<?php echo esc_attr( $col_count ); ?>" class="fas fa-sort"></i> <?php esc_html_e( 'Net Refunded', 'woo-calculate-your-price' ); ?>
					</a>
				</th>
				<?php if ( 'yes' === $options['cyp_orders_show_weight'] ) : ?>
					<?php $col_count++; ?>
					<th class="cyp-center">
					<a href="#" onclick="cypSortColumns(<?php echo esc_attr( $col_count ); ?>,'text','r','table-report');"><i id="r<?php echo esc_attr( $col_count ); ?>" class="fas fa-sort"></i> <?php esc_html_e( 'Weight', 'woo-calculate-your-price' ); ?></a></th>
					</th>
				<?php endif; ?>
				<th class="cyp-right">
					<?php $col_count++; ?>	
					<a href="#" onclick="cypSortColumns(<?php echo esc_attr( $col_count ); ?>,'number','r','table-report');"><i id="r<?php echo esc_attr( $col_count ); ?>" class="fas fa-sort"></i> <?php esc_html_e( 'Price', 'woo-calculate-your-price' ); ?></a></th>
				<th class="cyp-right">
					<?php $col_count++; ?>
					<a href="#" onclick="cypSortColumns(<?php echo esc_attr( $col_count ); ?>,'number','r','table-report');"><i id="r<?php echo esc_attr( $col_count ); ?>" class="fas fa-sort"></i> <?php esc_html_e( 'Net sale', 'woo-calculate-your-price' ); ?></a></th>
				<th class="cyp-right">
					<?php $col_count++; ?>
					<a href="#" onclick="cypSortColumns(<?php echo esc_attr( $col_count ); ?>,'number','r','table-report');"><i id="r<?php echo esc_attr( $col_count ); ?>" class="fas fa-sort"></i> <?php esc_html_e( 'Profit', 'woo-calculate-your-price' ); ?></a></th>
				<th class="cyp-right">
					<?php $col_count++; ?>
					<a href="#" onclick="cypSortColumns(<?php echo esc_attr( $col_count ); ?>,'number','r','table-report');"><i id="r<?php echo esc_attr( $col_count ); ?>" class="fas fa-sort"></i> <?php esc_html_e( 'Profit %', 'woo-calculate-your-price' ); ?></a></th>
			</tr>
		</thead>
		<?php
	}

	/**
	 * Get table footer - products
	 */
	public function cyp_rep_get_table_footer_products() {
		$options     = get_option( 'cyp_options' );
		$weight_unit = get_option( 'woocommerce_weight_unit' );
		?>
		<tfoot>
			<tr>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
				<!-- CUSTOM FIELDS -->
				<?php if ( 'yes' === $options['cyp_custom_1_products'] && strlen( $options['cyp_custom_field_1'] ) > 0 ) : ?>
					<td></td>
				<?php endif; ?>
				<?php if ( 'yes' === $options['cyp_custom_2_products'] && strlen( $options['cyp_custom_field_2'] ) > 0 ) : ?>
					<td></td>
				<?php endif; ?>
				<td id="pr5" class="cyp-center"></td>
				<td id="prRefund" class="cyp-center"></td>
				<td id="prRefundValue" class="cyp-right"></td>
				<?php if ( 'yes' === $options['cyp_orders_show_weight'] ) : ?>
					<td class="cyp-center"><span id="prWeight"></span> <?php echo esc_attr( $weight_unit ); ?></td>
				<?php endif; ?>
				<td id="pr6" class="cyp-right"></td>
				<td id="pr7" class="cyp-right"></td>
				<td id="pr8" class="cyp-right"></td>
				<td id="pr9" class="cyp-right"></td>
			</tr>
		</tfoot>
		<?php
	}

	/**
	 * Get product report right
	 */
	public function cyp_rep_get_product_report_right( $orders ) {
		$t_sep    = get_option( 'woocommerce_price_thousand_sep' );
		$d_point  = get_option( 'woocommerce_price_decimal_sep' );
		$decimals = get_option( 'woocommerce_price_num_decimals' );
		?>
		<div class="cyp-r-column cyp-r-right" id="cas-div-order-totals">
			<div class="cyp-total-group">
				<div class="cyp-total-item" style="border-right: 5px solid goldenrod;">
					<h4><?php esc_html_e( '#1 PRODUCT ', 'woo-calculate-your-price' ); ?> 
						 <?php esc_html_e( 'by quantity: ', 'woo-calculate-your-price' ); ?><b id="pr_top1_item_qty"></b></h4>
					<h2 id="pr_top_product"></h2>	
					<span id="pr_top_product_sku"></span><br/>
					<span><?php esc_html_e( 'profit: ', 'woo-calculate-your-price' ); ?><b id="pr_top1_item_profit"></b></span>
					<span><?php esc_html_e( 'sales: ', 'woo-calculate-your-price' ); ?><b id="pr_top1_sales"></b></span><br/>
					<span><?php esc_html_e( 'total turnover (sales): ', 'woo-calculate-your-price' ); ?><b id="pr_top1_turnover"></b></span><br/>
					<span><?php esc_html_e( 'total turnover (qty): ', 'woo-calculate-your-price' ); ?><b id="pr_top1_turnover_qty"></b></span><br/>
					<span><?php esc_html_e( 'category: ', 'woo-calculate-your-price' ); ?><b id="pr_top1_cat"></b></span>
				</div>
				<div class="cyp-total-item" style="border-right: 5px solid #B4B4B4;">
					<h4><?php esc_html_e( '#2 PRODUCT ', 'woo-calculate-your-price' ); ?> 
						  <?php esc_html_e( 'by quantity: ', 'woo-calculate-your-price' ); ?><b id="pr_top2_item_qty"></b></h4>
					<h2 id="pr_top2_product"></h2>	
					<span id="pr_top2_product_sku"></span><br/>
					<span><?php esc_html_e( 'profit: ', 'woo-calculate-your-price' ); ?><b id="pr_top2_item_profit"></b></span>
					<span><?php esc_html_e( 'sales: ', 'woo-calculate-your-price' ); ?><b id="pr_top2_sales"></b></span><br/>
					<span><?php esc_html_e( 'total turnover (sales): ', 'woo-calculate-your-price' ); ?><b id="pr_top2_turnover"></b></span><br/>
					<span><?php esc_html_e( 'total turnover (qty): ', 'woo-calculate-your-price' ); ?><b id="pr_top2_turnover_qty"></b></span><br/>
					<span><?php esc_html_e( 'category: ', 'woo-calculate-your-price' ); ?><b id="pr_top2_cat"></b></span>		
				</div>
				<div class="cyp-total-item" style="border-right: 5px solid goldenrod;">
					<h4><?php esc_html_e( '#1 PRODUCT ', 'woo-calculate-your-price' ); ?> 
						  <?php esc_html_e( 'by profit: ', 'woo-calculate-your-price' ); ?><b id="pr_top1_item_profit_pro"></b></h4>
					<h2 id="pr_top_product_pro"></h2>	
					<span id="pr_top_product_sku_pro"></span><br/>
					<span><?php esc_html_e( 'quantity: ', 'woo-calculate-your-price' ); ?><b id="pr_top1_item_qty_pro"></b></span>
					<span><?php esc_html_e( 'sales: ', 'woo-calculate-your-price' ); ?><b id="pr_top1_sales_pro"></b></span><br/>
					<span><?php esc_html_e( 'total turnover (sales): ', 'woo-calculate-your-price' ); ?><b id="pr_top1_pro_turnover"></b></span><br/>
					<span><?php esc_html_e( 'total turnover (qty): ', 'woo-calculate-your-price' ); ?><b id="pr_top1_pro_turnover_qty"></b></span><br/>
					<span><?php esc_html_e( 'category: ', 'woo-calculate-your-price' ); ?><b id="pr_top1_cat_pro"></b></span>
				</div>
				<div class="cyp-total-item" style="border-right: 5px solid #B4B4B4">
					<h4><?php esc_html_e( '#2 PRODUCT ', 'woo-calculate-your-price' ); ?> 
						  <?php esc_html_e( 'by profit: ', 'woo-calculate-your-price' ); ?><b id="pr_top2_item_profit_pro"></b></h4>
					<h2 id="pr_top2_product_pro"></h2>	
					<span id="pr_top2_product_sku_pro"></span><br/>
					<span><?php esc_html_e( 'quantity: ', 'woo-calculate-your-price' ); ?><b id="pr_top2_item_qty_pro"></b></span>
					<span><?php esc_html_e( 'sales: ', 'woo-calculate-your-price' ); ?><b id="pr_top2_sales_pro"></b></span><br/>
					<span><?php esc_html_e( 'total turnover (sales): ', 'woo-calculate-your-price' ); ?><b id="pr_top2_pro_turnover"></b></span><br/>
					<span><?php esc_html_e( 'total turnover (qty): ', 'woo-calculate-your-price' ); ?><b id="pr_top2_pro_turnover_qty"></b></span><br/>
					<span><?php esc_html_e( 'category: ', 'woo-calculate-your-price' ); ?><b id="pr_top2_cat_pro"></b></span>	
				</div>
				<div class="cyp-total-item" style="border-right: 5px solid #b1d4ea;">
					<h2 id="pr_gross_sales">
					</h2>	
					<span><?php esc_html_e( 'net sales in this period', 'woo-calculate-your-price' ); ?></span>
				</div>
				<div class="cyp-total-item" style="border-right: 5px solid #b1d4ea;">
					<h2 id="pr_total_profit">
					</h2>	
					<span><?php esc_html_e( 'total profit in this period', 'woo-calculate-your-price' ); ?></span>
				</div>
				<div class="cyp-total-item" style="border-right: 5px solid #3498db;">
					<h2 id="pr_profit_percent">
					</h2>	
					<span><?php esc_html_e( 'profit this period', 'woo-calculate-your-price' ); ?></span>
				</div>
				<div class="cyp-total-item" style="border-right: 5px solid #dbe1e3;">
					<h2 id="pr_sum_orders">
					<?php echo esc_attr( number_format( (int) $orders->total, 0, $d_point, $t_sep ) ); ?>
					</h2>	
					<span><?php esc_html_e( 'orders placed', 'woo-calculate-your-price' ); ?></span>
				</div>
				<div class="cyp-total-item" style="border-right: 5px solid #dbe1e3;">
					<h2 id="pr_sum_items">
					</h2>	
					<span><?php esc_html_e( 'items sold', 'woo-calculate-your-price' ); ?></span>
				</div>
				<div class="cyp-total-item" style="border-right: 5px solid firebrick;">
					<h2 id="pr_sum_refunded" class="cas-report-refund-h2">
					</h2>	
					<span class="cas-report-refund-h2"><?php esc_html_e( 'items refunded', 'woo-calculate-your-price' ); ?></span>
				</div>
				<div class="cyp-total-item" style="border-right: 5px solid firebrick;">
					<h2 id="pr_sum_refunded_value" class="cas-report-refund-h2">
					</h2>	
					<span class="cas-report-refund-h2"><?php esc_html_e( 'net refunded', 'woo-calculate-your-price' ); ?></span>
				</div>
			</div>
		</div>
		<?php
	}

	/**
	 * Get customers table
	 */
	public function cyp_rep_get_customer_table() {

		$results     = $this->functions->cyp_get_orders_report();
		$orders      = $results->orders;
		$cyp_options = get_option( 'cyp_options' );
		$date_format = get_option( 'date_format' );
		$time_format = get_option( 'time_format' );
		$page_num    = ( strlen( filter_input( 1, 'paged', FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ) > 0 ) ? filter_input( 1, 'paged', FILTER_SANITIZE_FULL_SPECIAL_CHARS ) : 1;
		$options     = get_option( 'cyp_options' );
		$is_mod_date = ( $options['cyp_order_date_modified'] && 'yes' === $options['cyp_order_date_modified'] ) ? true : false;

		$this->cyp_rep_get_customer_table_head( $page_num, $results->max_num_pages );

		foreach ( $orders as $order ) {

			//Do not include trash orders
			if ( 'trash' === $order->get_status() ) {
				continue;
			}
		
			$is_refunded  = 'refunded' === $order->get_status();
			$refund_total = $order->get_total_refunded(); // Get total refunded amount for the order - Inc VAT
			$refund_qty   = self::cyp_get_total_refunded_quantity( $order );
			$country      = ( strlen( $order->get_billing_country() ) > 0 ) ? WC()->countries->countries[ $order->get_billing_country() ] : '';
			$order_date   = ( $is_mod_date ) ? $order->get_date_modified() : $order->get_date_created();

			$refund_status = $is_refunded ? __( 'Refunded Order', 'woo-calculate-your-price' ) : '';
			?>
			<tr class="lozad" data-exclude="">
				<td><a href="<?php echo esc_attr( admin_url( 'post.php?post=' . $order->get_id() . '&action=edit' ) ); ?>" class="<?php echo esc_attr( $is_refunded ? 'cyp-refund-qty' : '' ); ?>"><?php echo esc_attr( $order->get_id() ); ?></a></td>
				<td><?php echo esc_attr( date_format( new DateTime( $order_date ), $date_format  ) ); ?></td>
				<td>
					<a href="<?php echo esc_attr( admin_url( 'user-edit.php?user_id=' ) . $order->get_customer_id() ); ?>"><?php echo esc_attr( $order->get_billing_first_name() . ' ' . $order->get_billing_last_name() ); ?></a>
				</td>
				<td>
					<i class="fa-solid fa-circle-info cyp-customer-info" data-customer-id="<?php echo esc_attr( $order->get_customer_id() ); ?>" title="<?php esc_attr_e( 'Click for totals', 'woo-calculate-your-price' ); ?>"></i>
				</td>

				<?php if ( 'yes' === $cyp_options['cyp_orders_show_company'] ) : ?>
					<td><?php echo esc_attr( null !== $order->get_billing_company() ? $order->get_billing_company() : ''  ); ?></td>
				<?php endif; ?>
				<td><?php echo esc_attr( $order->get_billing_email() ); ?></td>
				<!-- CUSTOM FIELDS -->
				<?php if ( 'yes' === $options['cyp_custom_1_customers'] && strlen( $options['cyp_custom_field_1'] ) > 0 ) : ?>
					<td><?php echo esc_attr( $order->get_meta( $cyp_options['cyp_custom_field_1'] ) ); ?></td>
				<?php endif; ?>
				<?php if ( 'yes' === $options['cyp_custom_2_customers'] && strlen( $options['cyp_custom_field_2'] ) > 0 ) : ?>
					<td><?php echo esc_attr( $order->get_meta( $cyp_options['cyp_custom_field_2'] ) ); ?></td>
				<?php endif; ?>
				<td><?php echo esc_attr( $order->get_billing_phone() ); ?></td>
				<td data-field="shipping"><?php echo esc_attr( ! $is_refunded ? $order->get_shipping_total() : 0 ); ?></td>
				<td data-field="total" title="<?php echo esc_attr( $refund_status ); ?>" style="<?php echo esc_attr( $is_refunded ? 'color:red;' : '' ); ?>"><?php echo esc_attr( $order->get_total() - $refund_total ); ?></td>
				<td data-field="total_tax"><?php echo esc_attr( ! $is_refunded ? $order->get_total_tax() : 0 ); ?></td>
				<td data-field="qty"><?php echo esc_attr( ! $is_refunded ? $order->get_item_count() - $refund_qty : 0 ); ?></td>
				<td><?php echo esc_attr( $order->get_currency() ); ?></td>
				<td><?php echo esc_attr( $country ); ?></td>
				<td><?php echo esc_attr( $order->get_billing_city() ); ?></td>
				<td style="<?php echo esc_attr( $is_refunded ? 'text-decoration:line-through red;' : '' ); ?>" title="<?php echo esc_attr( $refund_status ); ?>"><?php echo esc_attr( $order->get_payment_method_title() ); ?></td>
			</tr>
			<?php
		}

		?>
		</tbody>
		<?php $this->cyp_rep_get_customer_table_footer(); ?>
		</table>
		<!-- Placeholder for the dialog -->
		<div id="customerDetailsDialog" title="<?php esc_attr_e( 'Customer Details', 'woo-calculate-your-price' ); ?>" style="display:none;">
			<div id="customerDetailsContent">
				
			</div>
		</div>
		<?php

	}

	/**
	 * Customer table head
	 */
	public function cyp_rep_get_customer_table_head( $page_num, $max_num_pages ) {
		
		$options   = get_option( 'cyp_options' );
		$col_count = 3;
		
		?>
		<?php $this->cyp_get_paging( $page_num, $max_num_pages ); ?>
		
		<div style="display:block;height:10px;"></div>
		<table id="table-customers" class="wp-list-table widefat fixed striped posts" style="margin-top: 26px;">
		<thead>
			<tr>
				<th style="width:85px;">
					<a href="#" onclick="cypSortColumns(0,'number','cc','table-customers');"><i id="cc0" class="fas fa-sort"></i> <?php esc_attr_e( 'Order ID', 'woo-calculate-your-price' ); ?></a></th>
				<th style="width:130px;">
					<a href="#" onclick="cypSortColumns(1,'date','cc','table-customers');"><i id="cc1" class="fas fa-sort"></i> <?php esc_attr_e( 'Date', 'woo-calculate-your-price' ); ?></a></th>
				<th>
					<a href="#" onclick="cypSortColumns(2,'text','cc','table-customers');"><i id="cc2" class="fas fa-sort"></i> <?php esc_attr_e( 'Name', 'woo-calculate-your-price' ); ?></a></th>
				<th style="width:25px;"></th>
				<?php if ( 'yes' === $options['cyp_orders_show_company'] ) : ?>
					<th style="width:180px;">
					<?php $col_count++; ?>
					<a href="#" onclick="cypSortColumns(<?php echo esc_attr( $col_count ); ?>,'text','cc','table-customers');"><i id="cc<?php echo esc_attr( $col_count ); ?>" class="fas fa-sort"></i> <?php esc_attr_e( 'Company', 'woo-calculate-your-price' ); ?></a></th>
				<?php endif; ?>
				<th style="width:230px;">
					<?php $col_count++; ?>	
					<a href="#" onclick="cypSortColumns(<?php echo esc_attr( $col_count ); ?>,'text','cc','table-customers');"><i id="cc<?php echo esc_attr( $col_count ); ?>" class="fas fa-sort"></i> <?php esc_attr_e( 'E.mail', 'woo-calculate-your-price' ); ?></a></th>
				<!-- CUSTOM FIELDS -->
				<?php if ( 'yes' === $options['cyp_custom_1_customers'] && strlen( $options['cyp_custom_field_1'] ) > 0 ) : ?>
					<?php $col_count++; ?>
					<th>
					<a href="#" onclick="cypSortColumns(<?php echo esc_attr( $col_count ); ?>,'text','cc','table-customers');"><i id="cc<?php echo esc_attr( $col_count ); ?>" class="fas fa-sort"></i> <?php esc_attr_e( $options['cyp_custom_field_1_lbl'] ); ?></a></th>
					</th>
				<?php endif; ?>
				<?php if ( 'yes' === $options['cyp_custom_2_customers'] && strlen( $options['cyp_custom_field_2'] ) > 0 ) : ?>
					<?php $col_count++; ?>
					<th>
					<a href="#" onclick="cypSortColumns(<?php echo esc_attr( $col_count ); ?>,'text','cc','table-customers');"><i id="cc<?php echo esc_attr( $col_count ); ?>" class="fas fa-sort"></i> <?php esc_attr_e( $options['cyp_custom_field_2_lbl'] ); ?></a></th>
					</th>
				<?php endif; ?>
				<th>
					<?php $col_count++; ?>
					<a href="#" onclick="cypSortColumns(<?php echo esc_attr( $col_count ); ?>,'number','cc','table-customers');"><i id="cc<?php echo esc_attr( $col_count ); ?>" class="fas fa-sort"></i> <?php esc_attr_e( 'Phone', 'woo-calculate-your-price' ); ?></a></th>
				<th>
					<?php $col_count++; ?>
					<a href="#" onclick="cypSortColumns(<?php echo esc_attr( $col_count ); ?>,'number','cc','table-customers');"><i id="cc<?php echo esc_attr( $col_count ); ?>" class="fas fa-sort"></i> <?php esc_attr_e( 'Shipping', 'woo-calculate-your-price' ); ?></a></th>
				<th>
					<?php $col_count++; ?>
					<a href="#" onclick="cypSortColumns(<?php echo esc_attr( $col_count ); ?>,'number','cc','table-customers');"><i id="cc<?php echo esc_attr( $col_count ); ?>" class="fas fa-sort"></i> <?php esc_attr_e( 'Total', 'woo-calculate-your-price' ); ?></a></th>
				<th>
					<?php $col_count++; ?>
					<a href="#" onclick="cypSortColumns(<?php echo esc_attr( $col_count ); ?>,'number','cc','table-customers');"><i id="cc<?php echo esc_attr( $col_count ); ?>" class="fas fa-sort"></i> <?php esc_attr_e( 'Tax', 'woo-calculate-your-price' ); ?></a></th>
				<th style="width:50px;">
					<?php $col_count++; ?>
					<a href="#" onclick="cypSortColumns(<?php echo esc_attr( $col_count ); ?>,'number','cc','table-customers');"><i id="cc<?php echo esc_attr( $col_count ); ?>" class="fas fa-sort"></i> <?php esc_attr_e( 'Items', 'woo-calculate-your-price' ); ?></a></th>
				<th style="width:80px;">
					<?php $col_count++; ?>
					<a href="#" onclick="cypSortColumns(<?php echo esc_attr( $col_count ); ?>,'text','cc','table-customers');"><i id="cc<?php echo esc_attr( $col_count ); ?>" class="fas fa-sort"></i> <?php esc_attr_e( 'Currency', 'woo-calculate-your-price' ); ?></a></th>
				<th>
					<?php $col_count++; ?>
					<a href="#" onclick="cypSortColumns(<?php echo esc_attr( $col_count ); ?>,'text','cc','table-customers');"><i id="cc<?php echo esc_attr( $col_count ); ?>" class="fas fa-sort"></i> <?php esc_attr_e( 'Country', 'woo-calculate-your-price' ); ?></a></th>
				<th>
					<?php $col_count++; ?>
					<a href="#" onclick="cypSortColumns(<?php echo esc_attr( $col_count ); ?>,'text','cc','table-customers');"><i id="cc<?php echo esc_attr( $col_count ); ?>" class="fas fa-sort"></i> <?php esc_attr_e( 'City', 'woo-calculate-your-price' ); ?></a></th>
				<th>
					<?php $col_count++; ?>
					<a href="#" onclick="cypSortColumns(<?php echo esc_attr( $col_count ); ?>,'text','cc','table-customers');"><i id="cc<?php echo esc_attr( $col_count ); ?>" class="fas fa-sort"></i> <?php esc_attr_e( 'Payment', 'woo-calculate-your-price' ); ?></a></th>
			</tr>
		</thead>
		<tbody>
		
		<?php
		
	}

	/**
	 * Customer Get footer
	 */
	public function cyp_rep_get_customer_table_footer() {
		$options = get_option( 'cyp_options' );
		?>
		<tfoot>
			<tr>
				<td colspan="4" class="cyp-left" style="font-size: 11px;"><?php esc_attr_e( 'Totals', 'woo-calculate-your-price' ); ?></td>
				<?php if ( 'yes' === $options['cyp_orders_show_company'] ) : ?>
					<td></td>
				<?php endif; ?>
				<td></td>
				<!-- CUSTOM FIELDS -->
				<?php if ( 'yes' === $options['cyp_custom_1_customers'] && strlen( $options['cyp_custom_field_1'] ) > 0 ) : ?>
					<td></td>
				<?php endif; ?>
				<?php if ( 'yes' === $options['cyp_custom_2_customers'] && strlen( $options['cyp_custom_field_2'] ) > 0 ) : ?>
					<td></td>
				<?php endif; ?>
				<td></td>
				<td id="cf_shipping" class="cyp-left" style="font-size: 11px;"></td>
				<td id="cf_total" class="cyp-left" style="font-size: 11px;"></td>
				<td id="cf_total_tax" class="cyp-left" style="font-size: 11px;"></td>
				<td id="cf_qty" class="cyp-left" style="font-size: 11px;"></td>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
			</tr>
		</tfoot>
		<?php
	}

	/**
	 * Get total refunded quantity on order
	 */
	public static function cyp_get_total_refunded_quantity( $order ) {
		$refunded_qty = 0;

		// Use WooCommerce methods to get refunded quantities
		foreach ( $order->get_items() as $item ) {
			$refunded_qty += $order->get_qty_refunded_for_item( $item->get_id() );
		}

		return $refunded_qty;
	}

	/**
	 * Get order statuses dropdown
	 */
	public static function cyp_get_order_statuses_dropdown() {
		$order_statuses = wc_get_order_statuses();

		//Remove failed and cancelled
		unset( $order_statuses['wc-failed'] );
		unset( $order_statuses['wc-cancelled'] );

		?>
		<select id="cyp-order-statuses-select" class="cyp-select2-statuses">
			<option value=""><?php esc_attr_e( 'Select Statuses', 'woo-calculate-your-price' ); ?></option>
		<?php
		foreach ( $order_statuses as $key => $value ) {
			?>
			<option value="<?php echo esc_attr( $key ); ?>"><?php echo esc_attr( $value ); ?></option>
			<?php
		}
		?>
		</select>
		<?php
	}
	
}


