sync = $sync;
}
public function render(): void {
$set = $this->sync->latest();
$mapping = RateMapping::load()->to_array();
$types = $this->types();
foreach ( array_keys( wc_get_product_tax_class_options() ) as $index => $class ) {
$class = (string) $class;
$config = $mapping[ $class ] ?? null;
$type = null === $config ? '' : $config['type'];
$id = self::FIELD . '-' . $index;
?>
|
|
render_countries( $set, $index, $config['per_country'] ?? array(), 'reduced' !== $type ); ?>
|
sync->latest();
$classes = wc_get_product_tax_class_options();
$map = array();
foreach ( $posted as $row ) {
$class = is_array( $row ) && isset( $row['class'] ) ? (string) $row['class'] : null;
$type = is_array( $row ) && isset( $row['type'] ) ? (string) $row['type'] : '';
if ( null === $class || ! array_key_exists( $class, $classes ) || ! array_key_exists( $type, $this->types() ) || '' === $type ) {
continue;
}
$map[ $class ] = array(
'type' => $type,
'per_country' => 'reduced' === $type ? $this->picked_rates( $set, $row['rates'] ?? array() ) : array(),
);
}
update_option( RateMapping::OPTION, $map, false );
}
/**
* @return array
*/
private function types(): array {
return array(
'' => __( 'Not mapped', 'vatright' ),
'standard' => __( 'Standard rate', 'vatright' ),
'reduced' => __( 'Reduced rate', 'vatright' ),
'zero' => __( 'Zero rate', 'vatright' ),
);
}
/**
* @param mixed $rates
* @return array
*/
private function picked_rates( RateSet $set, $rates ): array {
$picked = array();
foreach ( (array) $rates as $country => $value ) {
$country = (string) $country;
if ( ! $set->has( $country ) || ! is_scalar( $value ) ) {
continue;
}
foreach ( $set->reduced( $country ) as $reduced ) {
if ( abs( $reduced['rate'] - (float) $value ) < 0.0001 ) {
$picked[ $country ] = $reduced['rate'];
}
}
}
return $picked;
}
/**
* @param array $current
*/
private function render_countries( RateSet $set, int $index, array $current, bool $hidden ): void {
$countries = WC()->countries->get_countries();
?>
>
country_codes() as $country ) : ?>
rate_options( $set, $country ); ?>
|
|
*/
private function rate_options( RateSet $set, string $country ): array {
$options = array();
foreach ( $set->reduced( $country ) as $reduced ) {
foreach ( $options as $existing ) {
if ( $existing['rate'] === $reduced['rate'] ) {
continue 2;
}
}
$names = array_map(
static function ( $category ) {
return ucfirst( strtolower( str_replace( '_', ' ', $category ) ) );
},
array_slice( $reduced['categories'], 0, 3 )
);
$label = $reduced['rate'] . '%';
$label .= 'super_reduced' === $reduced['type'] ? ' ' . __( '(super reduced)', 'vatright' ) : '';
$options[] = array(
'rate' => $reduced['rate'],
'label' => $names ? $label . ' - ' . implode( ', ', $names ) : $label,
);
}
return $options;
}
}