/src/lib/classes/integration/pdf_invoices_packing_slips/eu_invoice_helper.php

  1. <?php 
  2. namespace Aelia\WC\EU_VAT_Assistant\WCPDF; 
  3. if(!defined('ABSPATH')) exit; // Exit if accessed directly 
  4.  
  5. use Aelia\WC\EU_VAT_Assistant\WC_Aelia_EU_VAT_Assistant; 
  6. use Aelia\WC\EU_VAT_Assistant\Settings; 
  7. use Aelia\WC\EU_VAT_Assistant\Definitions; 
  8.  
  9. /** 
  10. * Alters the rendering of invoices produced by the PDF Invoices and Packing Slips 
  11. * plugin, allowing to render multiple copies of the same invoice in different 
  12. * currencies. 
  13. * 
  14. * @link https://wordpress.org/plugins/woocommerce-pdf-invoices-packing-slips/ 
  15. */ 
  16. class EU_Invoice_Helper { 
  17. // @var int The amount of decimals to use when printing prices 
  18. protected $price_decimals; 
  19.  
  20. // @var string The currency of the order being processed 
  21. public $order_currency; 
  22.  
  23. /** 
  24. * Returns the instance of the EU VAT Assistant plugin. 
  25. * 
  26. * @return \Aelia\WC\EU_VAT_Assistant\WC_Aelia_EU_VAT_Assistant 
  27. */ 
  28. protected function EUVA() { 
  29. return WC_Aelia_EU_VAT_Assistant::instance(); 
  30.  
  31. /** 
  32. * Returns the name of a currency, if present amongst WooCommerce currencies. 
  33. * 
  34. * @param string currency A currency code. 
  35. * @return string|false The currency name, or false if the currency is not 
  36. * among WooCommerce currencies. 
  37. */ 
  38. protected function get_currency_name($currency) { 
  39. if(empty($this->woocommerce_currencies)) { 
  40. $this->woocommerce_currencies = get_woocommerce_currencies(); 
  41. if(!empty($this->woocommerce_currencies[$currency])) { 
  42. return $this->woocommerce_currencies[$currency]; 
  43. return false; 
  44.  
  45. /** 
  46. * Returns a list of target currencies for the invoice. A separate invoice 
  47. * will be generated for each of the target currencies. 
  48. * 
  49. * @return array 
  50. */ 
  51. public function target_currencies() { 
  52. return apply_filters('wc_aelia_euva_invoice_target_currencies', array_unique(array($this->order_currency, $this->vat_currency))); 
  53.  
  54. /** 
  55. * Constructor. 
  56. * 
  57. * @param EU_Invoice_Order order The order for which the invoice will be printed. 
  58. */ 
  59. public function __construct(EU_Invoice_Order $order) { 
  60. $this->text_domain = WC_Aelia_EU_VAT_Assistant::$text_domain; 
  61. $this->price_decimals = absint(get_option('woocommerce_price_num_decimals')); 
  62. $this->order = $order; 
  63. $this->order_currency = $this->order->get_currency(); 
  64.  
  65. $this->vat_currency = WC_Aelia_EU_VAT_Assistant::settings()->get(Settings::FIELD_VAT_CURRENCY); 
  66. $this->vat_currency_symbol = get_woocommerce_currency_symbol($this->vat_currency); 
  67.  
  68. $this->vat_currency_exchange_rate = $this->EUVA()->get_order_vat_exchange_rate($order->get_id()); 
  69. if(empty($this->vat_currency_exchange_rate)) { 
  70. $this->vat_currency_exchange_rate = $this->EUVA()->convert(1, $this->order_currency, $this->vat_currency, 4); 
  71.  
  72. $this->set_hooks(); 
  73.  
  74. /** 
  75. * Factory method. 
  76. * 
  77. * @return Aelia\WC\EU_VAT_Assistant\WCPDF\EU_Invoice_Helper 
  78. */ 
  79. public static function factory(EU_Invoice_Order $order) { 
  80. return new self($order); 
  81.  
  82. /** 
  83. * Sets the hooks required by the class. 
  84. */ 
  85. protected function set_hooks() { 
  86. if($this->target_currencies() >= 2) { 
  87. add_filter('raw_woocommerce_price', array($this, 'raw_woocommerce_price'), 10, 1); 
  88. add_filter('wpo_wcpdf_billing_address', array($this, 'wpo_wcpdf_billing_address'), 10, 1); 
  89.  
  90. /** 
  91. * Removes the hooks created by the class, to prevent clashes. 
  92. */ 
  93. public function clear_hooks() { 
  94. remove_filter('raw_woocommerce_price', array($this, 'raw_woocommerce_price')); 
  95.  
  96. /** 
  97. * Returns a label for the currency used on the invoice. 
  98. */ 
  99. public function invoice_currency() { 
  100. $currency = $this->order->get_currency(); 
  101. $result = $currency; 
  102.  
  103. $currency_name = $this->get_currency_name($currency); 
  104. if(!empty($currency_name)) { 
  105. $result .= " ({$currency_name})"; 
  106. return $result; 
  107.  
  108. /** 
  109. * Indicates if the reverse charge applies to the invoice. A reverse charge 
  110. * applies to all EU sales when customer entered a valid EU VAT number, as long 
  111. * as the customer resides in a country different from shop's base country. 
  112. * 
  113. * @return bool 
  114. */ 
  115. public function reverse_charge() { 
  116. // Check if customer entered a valid VAT number. In such case, display a "Reverse charge" label. 
  117. $eu_vat_evidence = $this->order->get_vat_evidence(); 
  118. if(!empty($eu_vat_evidence['location'])) { 
  119. if(get_value('is_eu_country', $eu_vat_evidence['location']) && 
  120. ($this->order->vat_number_validated == Definitions::VAT_NUMBER_VALIDATION_VALID) && 
  121. ($eu_vat_evidence['location']['billing_country'] != wc()->countries->get_base_country())) { 
  122. return true; 
  123. return false; 
  124.  
  125. /** 
  126. * Recalculates order amounts on the fly, applying the exchange rate used for 
  127. * VAT purposes. This method allows to print multiple copies of the same invoice 
  128. * in different currencies. 
  129. * 
  130. * @param float price The price to convert. 
  131. */ 
  132. public function raw_woocommerce_price($price) { 
  133. $target_currency = $this->order->get_currency(); 
  134.  
  135. switch($target_currency) { 
  136. case $this->order_currency: 
  137. break; 
  138. case $this->vat_currency: 
  139. $price = $price * $this->vat_currency_exchange_rate; 
  140. break; 
  141. default: 
  142. $price = apply_filters('wc_aelia_eu_vat_assistant_convert', 1, $this->order_currency, $this->vat_currency, 4); 
  143. return $price; 
  144.  
  145. /** 
  146. * Alters the billing address on the invoice by adding extra fields. 
  147. * 
  148. * @param string formatted_billing_address The original billing address, already 
  149. * formatted as HTML. 
  150. * @return string The processed billing address, with extra information. 
  151. */ 
  152. public function wpo_wcpdf_billing_address($formatted_billing_address) { 
  153. $vat_number_label = __('VAT #:', $this->text_domain); 
  154. $formatted_billing_address = str_replace($vat_number_label,  
  155. '<span class="vat_number_label">' . $vat_number_label . '</span>',  
  156. $formatted_billing_address); 
  157. return $formatted_billing_address; 
.