Skip to content

Commit

Permalink
Add conversion from API, and a conversion notice
Browse files Browse the repository at this point in the history
  • Loading branch information
dingo-d committed Feb 27, 2018
1 parent 06d3f45 commit c002db7
Show file tree
Hide file tree
Showing 8 changed files with 224 additions and 157 deletions.
1 change: 0 additions & 1 deletion admin/class-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,6 @@ public function register_plugin_settings() {
register_setting( 'solo-api-settings-group', 'solo_api_change_mail_from' );
register_setting( 'solo-api-settings-group', 'solo_api_enable_pin' );
register_setting( 'solo-api-settings-group', 'solo_api_enable_iban' );
register_setting( 'solo-api-settings-group', 'solo_api_currency_rate' );
register_setting( 'solo-api-settings-group', 'solo_api_due_date' );
register_setting( 'solo-api-settings-group', 'solo_api_mail_gateway' );
register_setting( 'solo-api-settings-group', 'solo_api_send_pdf' );
Expand Down
16 changes: 8 additions & 8 deletions admin/class-helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,21 @@ class Helpers {
*/
public function get_exchange_rates() {

$currency_transient = get_transient( 'exchange_rate_transient' ); // Get transient.
$currency_rates = get_transient( 'exchange_rate_transient' ); // Get transient.

if ( false === $currency_transient ) { // If no valid transient exists, run this.
$currency_api_url = 'http://hnbex.eu/api/v1/rates/daily/';
$currency_transient = wp_remote_get( $currency_api_url );
if ( false === $currency_rates ) { // If no valid transient exists, run this.
$currency_api_url = 'http://hnbex.eu/api/v1/rates/daily/';
$currency_remote = wp_remote_get( $currency_api_url );

// Is the API up?
if ( ! 200 === wp_remote_retrieve_response_code( $currency_transient ) ) {
if ( ! 200 === wp_remote_retrieve_response_code( $currency_remote ) ) {
return false;
}

set_transient( 'exchange_rate_transient', $currency_transient, 1 * DAY_IN_SECONDS );
}
$currency_rates = json_decode( wp_remote_retrieve_body( $currency_remote ), true );

$currency_rates = json_decode( wp_remote_retrieve_body( $currency_transient ), true );
set_transient( 'exchange_rate_transient', $currency_rates, 1 * DAY_IN_SECONDS );
}

// Are the results in an array?
if ( ! is_array( $currency_rates ) ) {
Expand Down
93 changes: 59 additions & 34 deletions admin/class-request.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,39 +16,28 @@
* @package Woo_Solo_Api\Admin
* @author Denis Žoljom <[email protected]>
*
* @since 1.2 Added bill type check in the request method
* @since 1.4.0 Added additional methods.
* @since 1.2.0 Added bill type check in the request method
* @since 1.0.0
*/
class Request {

/**
* The ID of this plugin.
* Helpers object property.
*
* @since 1.0.0
* @since 1.4.0
* @access private
* @var string $plugin_name The ID of this plugin.
* @var string $helper Helper object instance.
*/
private $plugin_name;

/**
* The version of this plugin.
*
* @since 1.0.0
* @access private
* @var string $version The current version of this plugin.
*/
private $version;
private $helper;

/**
* Initialize the class and set its properties.
*
* @since 1.0.0
* @param string $plugin_name The name of this plugin.
* @param string $version The version of this plugin.
*/
public function __construct( $plugin_name, $version ) {
$this->plugin_name = $plugin_name;
$this->version = $version;
public function __construct() {
$this->helper = new Helpers();
}

/**
Expand Down Expand Up @@ -115,17 +104,16 @@ public function solo_api_send_api_request( $order, $sent_to_admin, $plain_text,
public function execute_solo_api_call( $order ) {

// Options.
$solo_api_token = get_option( 'solo_api_token' );
$solo_api_token = get_option( 'solo_api_token' );
$solo_api_measure = get_option( 'solo_api_measure' );
$solo_api_payment_type = get_option( 'solo_api_payment_type' );
$solo_api_languages = get_option( 'solo_api_languages' );
$solo_api_currency = get_option( 'solo_api_currency' );
$solo_api_service_type = get_option( 'solo_api_service_type' );
$solo_api_show_taxes = get_option( 'solo_api_show_taxes' );
$solo_api_invoice_type = get_option( 'solo_api_invoice_type' );
$solo_api_currency_rate = get_option( 'solo_api_currency_rate' );
$solo_api_due_date = get_option( 'solo_api_due_date' );
$solo_api_token = get_option( 'solo_api_token' );
$solo_api_token = get_option( 'solo_api_token' );
$solo_api_measure = get_option( 'solo_api_measure' );
$solo_api_payment_type = get_option( 'solo_api_payment_type' );
$solo_api_languages = get_option( 'solo_api_languages' );
$solo_api_currency = get_option( 'solo_api_currency' );
$solo_api_service_type = get_option( 'solo_api_service_type' );
$solo_api_show_taxes = get_option( 'solo_api_show_taxes' );
$solo_api_invoice_type = get_option( 'solo_api_invoice_type' );
$solo_api_due_date = get_option( 'solo_api_due_date' );

$order_data = $order->get_data(); // The Order data.

Expand Down Expand Up @@ -266,7 +254,7 @@ public function execute_solo_api_call( $order ) {
$due_date = date( 'Y-m-d', strtotime( '+1 week' ) );
}

$post_url .= '&nacin_placanja=' . $solo_api_payment_type . '&rok_placanja=' . $due_date . '&napomene=' . wp_kses_post( $customer_note );
$post_url .= '&nacin_placanja=' . $solo_api_payment_type . '&rok_placanja=' . $due_date;

if ( ! empty( $iban_number ) ) {
$post_url .= '&iban=' . esc_attr( $iban_number );
Expand All @@ -278,9 +266,44 @@ public function execute_solo_api_call( $order ) {
$post_url .= '&jezik_racuna=' . $solo_api_languages;
}

if ( ! empty( $solo_api_currency_rate ) ) {
$num = (float) str_replace( ',', '.', $solo_api_currency_rate );
$post_url .= '&tecaj=' . str_replace( '.', ',', round( $num, 6 ) );
if ( $solo_api_currency !== '1' ) { // Only for foreign currency.
$currency_helper = array(
'1' => 'HRK',
'2' => 'AUD',
'3' => 'CAD',
'4' => 'CZK',
'5' => 'DKK',
'6' => 'HUF',
'7' => 'JPY',
'8' => 'NOK',
'9' => 'SEK',
'10' => 'CHF',
'11' => 'GBP',
'12' => 'USD',
'13' => 'BAM',
'14' => 'EUR',
'15' => 'PLN',
);

$api_rates = $this->helper->get_exchange_rates();
$currency = $currency_helper[ $solo_api_currency ];

$currency_rate = array_values( array_filter( array_map( function( $el ) use ( $currency ) {
if ( $el['currency_code'] === $currency ) {
return $el['median_rate'];
}
}, $api_rates ) ) );

if ( ! empty( $currency_rate ) ) {
$num = (float) str_replace( ',', '.', $currency_rate[0] );
$post_url .= '&tecaj=' . str_replace( '.', ',', round( $num, 6 ) );

$customer_note .= "\n" . sprintf( '%1$s (1 %2$s = %3$s HRK)',
esc_html__( 'Recalculated at the middle exchange rate of the CNB', 'woo-solo-api' ),
esc_html( $currency ),
esc_html( str_replace( '.', ',', round( $num, 6 ) ) )
);
}
}

if ( $solo_api_bill_type === 'racun' ) {
Expand All @@ -291,6 +314,8 @@ public function execute_solo_api_call( $order ) {
}
}

$post_url .= '&napomene=' . wp_kses_post( $customer_note );

$method_executed = false;

$regular_url = str_replace( ' ', '%20', $post_url );
Expand Down
4 changes: 1 addition & 3 deletions admin/partials/admin-display.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
$solo_api_service_type_option = get_option( 'solo_api_service_type' );
$solo_api_show_taxes_option = get_option( 'solo_api_show_taxes' );
$solo_api_invoice_type_option = get_option( 'solo_api_invoice_type' );
$solo_api_currency_rate_option = get_option( 'solo_api_currency_rate' );
$solo_api_due_date_option = get_option( 'solo_api_due_date' );
$solo_api_send_pdf_option = get_option( 'solo_api_send_pdf' );
$solo_api_send_control_option = get_option( 'solo_api_send_control' );
Expand Down Expand Up @@ -187,8 +186,7 @@
</select>
</div>
<div class="option">
<label for="solo_api_currency_rate" class="subtitle"><?php printf( '%s <a href="https://www.hnb.hr/temeljne-funkcije/monetarna-politika/tecajna-lista/tecajna-lista" target="_blank" rel="noopener noreferrer">%s</a> %s', esc_html__( 'You can check the currency rate at', 'woo-solo-api' ), esc_html__( 'Croatian National Bank', 'woo-solo-api' ), esc_html__( 'to see the current currency rates. If the currency is HRK leave the field blank. Limit the number of decimals to 6 places for other currencies (e.g. 6,123456).', 'woo-solo-api' ) ); ?></label>
<input type="text" id="solo_api_currency_rate" name="solo_api_currency_rate" value="<?php echo esc_attr( $solo_api_currency_rate_option ); ?>">
<div class="subtitle"><?php printf( '%s <a href="https://www.hnb.hr/temeljne-funkcije/monetarna-politika/tecajna-lista/tecajna-lista" target="_blank" rel="noopener noreferrer">%s</a>. %s', esc_html__( 'You can check the currency rate at', 'woo-solo-api' ), esc_html__( 'Croatian National Bank', 'woo-solo-api' ), esc_html__( 'The currency will be automatically added if the selected currency is different from HRK. Also a note about conversion rate will be added to the invoice/offer.', 'woo-solo-api' ) ); ?></div>
</div>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion includes/class-woo-solo-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ private function set_locale() {
*/
private function define_admin_hooks() {
$plugin_admin = new Admin\Admin( $this->get_plugin_name(), $this->get_version() );
$api_request = new Admin\Request( $this->get_plugin_name(), $this->get_version() );
$api_request = new Admin\Request();

$this->loader->add_action( 'woocommerce_email_order_details', $api_request, 'solo_api_send_api_request', 15, 4 );

Expand Down
Binary file modified languages/woo-solo-api-hr.mo
Binary file not shown.
Loading

0 comments on commit c002db7

Please sign in to comment.