/woocommerce-invoicexpress.php

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