AeliaWCEU_VAT_AssistantWCPDFEU_Invoice_Helper

Alters the rendering of invoices produced by the PDF Invoices and Packing Slips plugin, allowing to render multiple copies of the same invoice in different currencies.

Defined (1)

The class is defined in the following location(s).

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