woocommerce_invoicexpress

The WooCommerce InvoiceXpress Extension woocommerce invoicexpress class.

Defined (1)

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

/woocommerce-invoicexpress.php  
  1. class woocommerce_invoicexpress { 
  2. function __construct() { 
  3. require_once('InvoiceXpressRequest-PHP-API/lib/InvoiceXpressRequest.php'); 
  4.  
  5. $this->subdomain = get_option('wc_ie_subdomain'); 
  6. $this->token = get_option('wc_ie_api_token'); 
  7.  
  8. add_action('admin_init', array(&$this, 'settings_init')); 
  9. add_action('admin_menu', array(&$this, 'menu')); 
  10. //add_action('woocommerce_checkout_order_processed', array(&$this, 'process')); // Check if user is InvoiceXpress client (create if not) and create invoice. 
  11.  
  12. add_action('woocommerce_order_status_processing', array(&$this, 'process')); 
  13. //add_action('woocommerce_order_status_completed', array(&$this, 'process')); 
  14.  
  15. add_action('woocommerce_order_actions', array(&$this, 'my_woocommerce_order_actions'), 10, 1); 
  16. add_action('woocommerce_order_action_my_action', array(&$this, 'do_my_action'), 10, 1);  
  17.  
  18. function my_woocommerce_order_actions($actions) { 
  19. $actions['my_action'] = "Create Invoice (InvoiceXpress)"; 
  20. return $actions; 
  21.  
  22.  
  23. function do_my_action($order) { 
  24. // Do something here with the WooCommerce $order object 
  25. $this->process($order->id); 
  26.  
  27.  
  28. function menu() { 
  29. add_submenu_page('woocommerce', __('InvoiceXpress', 'wc_invoicexpress'), __('InvoiceXpress', 'wc_invoicexpress') , 'manage_woocommerce', 'woocommerce_invoicexpress', array(&$this, 'options_page')); 
  30.  
  31. function settings_init() { 
  32. global $woocommerce; 
  33. wp_enqueue_style('woocommerce_admin_styles', $woocommerce->plugin_url().'/assets/css/admin.css'); 
  34.  
  35. $settings = array( 
  36. array( 
  37. 'name' => 'wc_ie_settings',  
  38. 'title' => __('InvoiceXpress for WooCommerce Settings', 'wc_invoicexpress'),  
  39. 'page' => 'woocommerce_invoicexpress',  
  40. 'settings' => array( 
  41. array( 
  42. 'name' => 'wc_ie_subdomain',  
  43. 'title' => __('Subdomain', 'wc_invoicexpress'),  
  44. ),  
  45. array( 
  46. 'name' => 'wc_ie_api_token',  
  47. 'title' => __('API Token', 'wc_invoicexpress'),  
  48. ),  
  49. array( 
  50. 'name' => 'wc_ie_create_invoice',  
  51. 'title' => __('Create Invoice', 'wc_invoicexpress'),  
  52. ),  
  53. array( 
  54. 'name' => 'wc_ie_send_invoice',  
  55. 'title' => __('Send Invoice', 'wc_invoicexpress'),  
  56. ),  
  57. array( 
  58. 'name' => 'wc_ie_create_simplified_invoice',  
  59. 'title' => __('Create Simplified Invoice', 'wc_invoicexpress'),  
  60. ),  
  61. ),  
  62. ); 
  63.  
  64. foreach($settings as $sections=>$section) { 
  65. add_settings_section($section['name'], $section['title'], array(&$this, $section['name']), $section['page']); 
  66. foreach($section['settings'] as $setting=>$option) { 
  67. add_settings_field($option['name'], $option['title'], array(&$this, $option['name']), $section['page'], $section['name']); 
  68. register_setting($section['page'], $option['name']); 
  69. $this->$option['name'] = get_option($option['name']); 
  70.  
  71.  
  72.  
  73. function wc_ie_settings() { 
  74. echo '<p>'.__('Please fill in the necessary settings below. InvoiceXpress for WooCommerce works by creating an invoice when order status is updated to processing.', 'wc_invoicexpress').'</p>'; 
  75. function wc_ie_subdomain() { 
  76. echo '<input type="text" name="wc_ie_subdomain" id="wc_ie_subdomain" value="'.get_option('wc_ie_subdomain').'" />'; 
  77. echo ' <label for="wc_ie_subdomain">When you access InvoiceXpress you use https://<b>subdomain</b>.invoicexpress.net</label>'; 
  78. function wc_ie_api_token() { 
  79. echo '<input type="password" name="wc_ie_api_token" id="wc_ie_api_token" value="'.get_option('wc_ie_api_token').'" />'; 
  80. echo ' <label for="wc_ie_api_token">Go to Settings >> API in InvoiceXpress to get one.</label>'; 
  81. function wc_ie_create_invoice() { 
  82. $checked = (get_option('wc_ie_create_invoice')==1) ? 'checked="checked"' : ''; 
  83. echo '<input type="hidden" name="wc_ie_create_invoice" value="0" />'; 
  84. echo '<input type="checkbox" name="wc_ie_create_invoice" id="wc_ie_create_invoice" value="1" '.$checked.' />'; 
  85. echo ' <label for="wc_ie_create_invoice">Create invoices for orders that come in, otherwise only the client is created (<i>recommended</i>).</label>'; 
  86. function wc_ie_send_invoice() { 
  87. $checked = (get_option('wc_ie_send_invoice')==1) ? 'checked="checked"' : ''; 
  88. echo '<input type="hidden" name="wc_ie_send_invoice" value="0" />'; 
  89. echo '<input type="checkbox" name="wc_ie_send_invoice" id="wc_ie_send_invoice" value="1" '.$checked.' />'; 
  90. echo ' <label for="wc_ie_send_invoice">Send the client an e-mail with the order invoice attached (<i>recommended</i>).</label>'; 
  91.  
  92. function wc_ie_create_simplified_invoice() { 
  93. $checked = (get_option('wc_ie_create_simplified_invoice')==1) ? 'checked="checked"' : ''; 
  94. echo '<input type="hidden" name="wc_ie_create_simplified_invoice" value="0" />'; 
  95. echo '<input type="checkbox" name="wc_ie_create_simplified_invoice" id="wc_ie_create_simplified_invoice" value="1" '.$checked.' />'; 
  96. echo ' <label for="wc_ie_create_simplified_invoice">Create simplified invoices. Only available for Portuguese accounts.</label>'; 
  97.  
  98.  
  99. function options_page() { ?> 
  100. <div class="wrap woocommerce"> 
  101. <form method="post" id="mainform" action="options.php"> 
  102. <div class="icon32 icon32-woocommerce-settings" id="icon-woocommerce"><br /></div> 
  103. <h2><?php _e('InvoiceXpress for WooCommerce', 'wc_invoicexpress'); ?></h2> 
  104. <?php settings_fields('woocommerce_invoicexpress'); ?> 
  105. <?php do_settings_sections('woocommerce_invoicexpress'); ?> 
  106. <p class="submit"><input type="submit" class="button-primary" value="Save Changes" /></p> 
  107. </form> 
  108. </div> 
  109. <?php } 
  110.  
  111. function process($order_id) { 
  112.  
  113. InvoiceXpressRequest::init($this->subdomain, $this->token); 
  114.  
  115. $order = new WC_Order($order_id); 
  116.  
  117. $client_name = $order->billing_first_name." ".$order->billing_last_name; 
  118.  
  119. $countries = new WC_Countries(); 
  120. $countries_list = $countries->countries; 
  121. $country = $countries_list[$order->billing_country]; 
  122.  
  123. $vat=''; 
  124. if(isset($order->billing_company)) 
  125. $vat = $order->billing_company; 
  126.  
  127.  
  128. $client_email = $order->billing_email; 
  129.  
  130. // Lets get the user's InvoiceXpress data 
  131.  
  132. $client_data = array( 
  133. 'name' => $client_name. " (".$client_email.")",  
  134. 'email' => $client_email,  
  135. 'phone' => $order->billing_phone,  
  136. 'address' => $order->billing_address_1."\n".$order->billing_address_2."\n",  
  137. 'postal_code' => $order->billing_postcode . " - " . $order->billing_city,  
  138. 'country' => $country,  
  139. 'fiscal_id' => $vat,  
  140. 'send_options' => 3 
  141.  
  142. ); 
  143.  
  144. if(get_option('wc_ie_create_invoice')==1) { 
  145. foreach($order->get_items() as $item) {  
  146. $pid = $item['item_meta']['_product_id'][0]; 
  147.  
  148. $prod = get_product($pid); 
  149.  
  150. $items[] = array( 
  151. 'name' => "#".$pid, //$item['name'],  
  152. 'description' => get_the_title($pid), //'('.$item['qty'].') '.$item['name'],  
  153. 'unit_price' => $prod->price / 1.23, // subtract tax 23% 
  154. 'quantity' => $item['qty'],  
  155. 'unit' => 'unit',  
  156. 'tax' => array( 
  157. 'name' => 'IVA23' 
  158. ); 
  159. // divide the value of shipping per 1.23 so that when 
  160. // invoicexpress calculates the tax is correct 
  161. $shipping_unit_price = $order->get_shipping() / 1.23; 
  162. $items[] = array( 
  163. 'name' => 'Envio',  
  164. 'description' => 'Custos de Envio',  
  165. 'unit_price' => $shipping_unit_price,  
  166. 'quantity' => 1,  
  167. 'tax' => array( 
  168. 'name' => 'IVA23' 
  169. ); 
  170.  
  171. if(get_option('wc_ie_create_simplified_invoice')==1) { 
  172. $data = array( 
  173. 'simplified_invoice' => array( 
  174. 'date' => $order->completed_date,  
  175. 'due_date' => $order->completed_date,  
  176. 'client' => $client_data,  
  177. 'reference' => $order_id,  
  178. 'items' => array( 
  179. 'item' => $items 
  180. ); 
  181. } else { 
  182. $data = array( 
  183. 'invoice' => array( 
  184. 'date' => $order->completed_date,  
  185. 'due_date' => $order->completed_date,  
  186. 'client' => $client_data,  
  187. 'reference' => $order_id,  
  188. 'items' => array( 
  189. 'item' => $items 
  190. ); 
  191.  
  192. if(get_option('wc_ie_create_simplified_invoice')==1) { 
  193. $invoice = new InvoiceXpressRequest('simplified_invoices.create');  
  194. } else { 
  195. $invoice = new InvoiceXpressRequest('invoices.create'); 
  196.  
  197. $invoice->post($data); 
  198. $invoice->request(); 
  199. if($invoice->success()) { 
  200. $response = $invoice->getResponse(); 
  201. $invoice_id = $response['id']; 
  202. $order->add_order_note(__('Client invoice in InvoiceXpress', 'wc_invoicexpress').' #'.$invoice_id); 
  203. add_post_meta($order_id, 'wc_ie_inv_num', $invoice_id, true); 
  204.  
  205. // extra request to change status to final 
  206. if(get_option('wc_ie_create_simplified_invoice')==1) { 
  207. $invoice = new InvoiceXpressRequest('simplified_invoices.change-state'); 
  208. } else { 
  209. $invoice = new InvoiceXpressRequest('invoices.change-state'); 
  210. $data = array('invoice' => array('state' => 'finalized')); 
  211. $invoice->post($data); 
  212. $invoice->request($invoice_id); 
  213.  
  214. if($invoice->success()) { // keep the invoice sequence number in a meta 
  215. $response = $invoice->getResponse(); 
  216. $inv_seq_number = $response['sequence_number']; 
  217. add_post_meta($order_id, 'wc_ie_inv_seq_num', $inv_seq_number, true); 
  218.  
  219. $data = array('invoice' => array('state' => 'settled')); 
  220. $invoice->post($data); 
  221. $invoice->request($invoice_id); 
  222. } else { 
  223. $order->add_order_note(__('InvoiceXpress Invoice API Error:', 'wc_invoicexpress').': '.$invoice->getError()); 
  224.  
  225. if(get_option('wc_ie_send_invoice')==1 && isset($invoice_id)) { 
  226. $data = array( 
  227. 'message' => array( 
  228. 'client' => array( 
  229. 'email' => $order->billing_email,  
  230. 'save' => 1 
  231. ),  
  232. 'subject' => __('Order Invoice', 'wc_invoicexpress'),  
  233. 'body' => __('Please find your invoice in attach. Archive this e-mail as proof of payment.', 'wc_invoicexpress') 
  234. ); 
  235.  
  236. if(get_option('wc_ie_create_simplified_invoice')==1) { 
  237. $send_invoice = new InvoiceXpressRequest('simplified_invoices.email-invoice'); 
  238. } else { 
  239. $send_invoice = new InvoiceXpressRequest('invoices.email-invoice'); 
  240. $send_invoice->post($data); 
  241. $send_invoice->request($invoice_id); 
  242.  
  243. if($send_invoice->success()) { 
  244. $response = $send_invoice->getResponse(); 
  245. $order->add_order_note(__('Client invoice sent from InvoiceXpress', 'wc_invoicexpress')); 
  246. } else { 
  247. $order->add_order_note(__('InvoiceXpress Send Invoice API Error', 'wc_invoicexpress').': '.$send_invoice->getError()); 
  248.  
  249.  
  250.