WC_Billogram_Extended

The WooCommerce Billogram Integration WC Billogram Extended class.

Defined (1)

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

/woocommerce-billogram2-extended.php  
  1. class WC_Billogram_Extended { 
  2.  
  3. private $general_settings_key = 'woocommerce_billogram_general_settings'; 
  4. private $accounting_settings_key = 'woocommerce_billogram_accounting_settings'; 
  5. private $order_settings_key = 'woocommerce_billogram_order_settings'; 
  6. private $support_key = 'woocommerce_billogram_support'; 
  7. private $manual_action_key = 'woocommerce_billogram_manual_action'; 
  8. private $start_action_key = 'woocommerce_billogram_start_action'; 
  9. private $general_settings; 
  10. private $accounting_settings; 
  11. private $plugin_options_key = 'woocommerce_billogram_options'; 
  12. private $plugin_settings_tabs = array(); 
  13.  
  14. public $FORTNOX_ERROR_CODE_PRODUCT_NOT_EXIST = 2001302; 
  15. public $FORTNOX_ERROR_CODE_ORDER_EXISTS = 2000861; 
  16.  
  17. public function __construct() { 
  18.  
  19. //call register settings function 
  20. add_action( 'init', array( &$this, 'load_settings' ) ); 
  21. add_action( 'admin_init', array( &$this, 'register_woocommerce_billogram_start_action' )); 
  22. add_action( 'admin_init', array( &$this, 'register_woocommerce_billogram_general_settings' )); 
  23. add_action( 'admin_init', array( &$this, 'register_woocommerce_billogram_order_settings' )); 
  24. add_action( 'admin_init', array( &$this, 'register_woocommerce_billogram_manual_action' )); 
  25. add_action( 'admin_init', array( &$this, 'register_woocommerce_billogram_support' )); 
  26. add_action( 'admin_menu', array( &$this, 'add_admin_menus' ) ); 
  27. //add_action( 'admin_head', array( &$this, 'woobill_admin_message' ) ); 
  28.  
  29. // Register WooCommerce Hooks 
  30. if(!AUTOMATED_TESTING) 
  31. add_action( 'woocommerce_order_status_completed', array(&$this, 'send_contact_to_billogram'), 10, 1 ); 
  32.  
  33. if(!AUTOMATED_TESTING) 
  34. add_action( 'save_post', array(&$this, 'send_product_to_billogram'), 10, 1 ); 
  35.  
  36. /*********************************************************************************************************** 
  37. * ADMIN SETUP 
  38. ***********************************************************************************************************/ 
  39.  
  40. /** 
  41. * Adds admin menu 
  42. * @access public 
  43. * @param void 
  44. * @return void 
  45. */ 
  46. function add_admin_menus() { 
  47. add_menu_page( 'WooCommerce Billogram Integration', 'Billogram', 'manage_options', $this->plugin_options_key, array( &$this, 'woocommerce_billogram_options_page' ) ); 
  48. /**$current_tab = isset( $_GET['tab'] ) ? $_GET['tab'] : $this->start_action_key; 
  49. $this->plugin_settings_tabs[$this->start_action_key] = 'Vlkommen!'; 
  50. $this->plugin_settings_tabs[$this->general_settings_key] = 'Allmnna instllningar'; 
  51. $this->plugin_settings_tabs[$this->order_settings_key] = 'Orderinstllningar'; 
  52. $this->plugin_settings_tabs[$this->manual_action_key] = 'Manuella funktioner'; 
  53. $this->plugin_settings_tabs[$this->support_key] = 'Support'; 
  54. foreach ( $this->plugin_settings_tabs as $tab_key => $tab_caption ) { 
  55. $active = $current_tab == $tab_key ? 'nav-tab-active' : ''; 
  56. add_submenu_page( $this->plugin_options_key, $tab_caption, $tab_caption, 'manage_options', $this->plugin_options_key.'&tab=' . $tab_key, array( &$this, 'woocommerce_billogram_options_page' ) ); 
  57. }*/ 
  58.  
  59. /** 
  60. * Generates html for textfield for given settings params 
  61. * @access public 
  62. * @param void 
  63. * @return void 
  64. */ 
  65. function field_gateway($args) { 
  66. $options = get_option($args['tab_key']);?> 
  67.  
  68. <input type="hidden" name="<?php echo $args['tab_key']; ?>[<?php echo $args['key']; ?>]" value="<?php echo $args['key']; ?>" /> 
  69.  
  70. <select name="<?php echo $args['tab_key']; ?>[<?php echo $args['key'] . "_payment_method"; ?>]" >'; 
  71. <option value=""<?php if(isset($options[$args['key'] . "_payment_method"]) && $options[$args['key'] . "_payment_method"] == '') {echo 'selected="selected"';}?>>Vlj nedan</option> 
  72. <option value="CARD"<?php if(isset($options[$args['key'] . "_payment_method"]) && $options[$args['key'] . "_payment_method"] == 'CARD') {echo 'selected="selected"';}?>>Kortbetalning</option> 
  73. <option value="BANK"<?php if(isset($options[$args['key'] . "_payment_method"]) && $options[$args['key'] . "_payment_method"] == 'BANK') {echo 'selected="selected"';}?>>Bankgiro/Postgiro</option> 
  74. </select> 
  75. <?php 
  76. $str = ''; 
  77. if(isset($options[$args['key'] . "_book_keep"])) { 
  78. if($options[$args['key'] . "_book_keep"] == 'on') { 
  79. $str = 'checked = checked'; 
  80. ?> 
  81. <span>Bokfr automatiskt: </span> 
  82. <input type="checkbox" name="<?php echo $args['tab_key']; ?>[<?php echo $args['key'] . "_book_keep"; ?>]" <?php echo $str; ?> /> 
  83.  
  84. <?php 
  85.  
  86. /** 
  87. * Generates html for textfield for given settings params 
  88. * @access public 
  89. * @param void 
  90. * @return void 
  91. */ 
  92. function field_option_text($args) { 
  93. $options = get_option($args['tab_key']); 
  94. $val = ''; 
  95. if(isset($options[$args['key']] )) { 
  96. $val = esc_attr( $options[$args['key']] ); 
  97. ?> 
  98. <input <?php echo isset($args['id'])? 'id="'.$args['id'].'"': ''; ?> type="text" name="<?php echo $args['tab_key']; ?>[<?php echo $args['key']; ?>]" value="<?php echo $val; ?>" /> 
  99. <span><i><?php echo $args['desc']; ?></i></span> 
  100. <?php 
  101.  
  102.  
  103. /** 
  104. * Generates html for date field for given settings params 
  105. * @access public 
  106. * @param void 
  107. * @return void 
  108. */ 
  109. function field_option_date($args) { 
  110. $options = get_option($args['tab_key']); 
  111. $val = ''; 
  112. if(isset($options[$args['key']] )) { 
  113. $val = esc_attr( $options[$args['key']] ); 
  114. ?> 
  115. <input <?php echo isset($args['id'])? 'id="'.$args['id'].'"': ''; ?> type="date" name="<?php echo $args['tab_key']; ?>[<?php echo $args['key']; ?>]" value="<?php echo $val; ?>" /> 
  116. <span><i><?php echo $args['desc']; ?></i></span> 
  117. <?php 
  118.  
  119. /** 
  120. * Generates html for dropdown for given settings of sandbox params 
  121. * @access public 
  122. * @param void 
  123. * @return void 
  124. */ 
  125. function field_mode_dropdown($args) { 
  126. $options = get_option($args['tab_key']); 
  127. $str = ''; 
  128. $str2 = ''; 
  129. if(isset($options[$args['key']])) { 
  130. if($options[$args['key']] == 'Live') { 
  131. $str = 'selected'; 
  132. else 
  133. $str2 = 'selected'; 
  134.  
  135. ?> 
  136. <select <?php echo isset($args['id'])? 'id="'.$args['id'].'"': ''; ?> name="<?php echo $args['tab_key']; ?>[<?php echo $args['key']; ?>]"> 
  137. <option <?php echo $str; ?>>Live</option> 
  138. <option <?php echo $str2; ?>>Sandbox</option> 
  139. </select> 
  140. <span id="sandbox-mode"><i><?php echo $args['desc']; ?></i></span> 
  141. <?php 
  142.  
  143.  
  144.  
  145. /** 
  146. * Generates html for dropdown for given settings of Stock reduction handle 
  147. * @access public 
  148. * @param void 
  149. * @return void 
  150. */ 
  151. function field_stock_dropdown($args) { 
  152. $options = get_option($args['tab_key']); 
  153. $str1 = ''; 
  154. $str2 = ''; 
  155. if(isset($options[$args['key']])) { 
  156. if($options[$args['key']] == 'invoice') { 
  157. $str1 = 'selected'; 
  158. elseif($options[$args['key']] == 'checkout') { 
  159. $str2 = 'selected'; 
  160.  
  161. ?> 
  162. <select <?php echo isset($args['id'])? 'id="'.$args['id'].'"': ''; ?> name="<?php echo $args['tab_key']; ?>[<?php echo $args['key']; ?>]"> 
  163. <option value='invoice' <?php echo $str1; ?>>Nr faktura r betald</option> 
  164. <option value='checkout' <?php echo $str2; ?>>Vid inkommen bestllning</option> 
  165. </select> 
  166. <span><i><?php echo $args['desc']; ?></i></span> 
  167. <?php 
  168.  
  169. /** 
  170. * Generates html for dropdown for given settings params 
  171. * @access public 
  172. * @param void 
  173. * @return void 
  174. */ 
  175. function field_option_dropdown($args) { 
  176. $options = get_option($args['tab_key']); 
  177. $str = ''; 
  178. $str2 = ''; 
  179. $str3 = ''; 
  180. $str4 = ''; 
  181. if(isset($options[$args['key']])) { 
  182. if($options[$args['key']] == 'Skapa faktura och skicka som epost') { 
  183. $str = 'selected'; 
  184. elseif($options[$args['key']] == 'Skapa faktura och skicka som brev') { 
  185. $str2 = 'selected'; 
  186. elseif($options[$args['key']] == 'Spara utkast') { 
  187. $str3 = 'selected'; 
  188. elseif($options[$args['key']] == 'Skapa fakturor och skicka som e-post och brev') { 
  189. $str5 = 'selected'; 
  190. else{ 
  191. $str4 = 'selected'; 
  192. ?> 
  193.  
  194. <select <?php echo isset($args['id'])? 'id="'.$args['id'].'"':''; ?> name="<?php echo $args['tab_key']; ?>[<?php echo $args['key']; ?>]"> 
  195. <option <?php echo $str; ?>>Skapa faktura och skicka som epost</option> 
  196. <option <?php echo $str2; ?>>Skapa faktura och skicka som brev</option> 
  197. <option <?php echo $str5; ?>>Skapa fakturor och skicka som e-post och brev</option> 
  198. <option <?php echo $str3; ?>>Spara utkast</option> 
  199. <?php 
  200. if($args['id'] == 'order-sync-other') { 
  201. ?> 
  202. <option <?php echo $str4; ?>>Gra ingenting</option> 
  203. <?php 
  204. ?> 
  205. </select> 
  206. <span><i><?php echo $args['desc']; ?></i></span> 
  207. <?php 
  208.  
  209.  
  210. /** 
  211. * Generates html for checkbox for given settings params 
  212. * @access public 
  213. * @param void 
  214. * @return void 
  215. */ 
  216. function field_option_checkbox($args) { 
  217. $options = get_option($args['tab_key']); 
  218. $str = ''; 
  219. if(isset($options[$args['key']])) { 
  220. if($options[$args['key']] == 'on') { 
  221. $str = 'checked = checked'; 
  222.  
  223. ?> 
  224. <input <?php echo isset($args['id'])? 'id="'.$args['id'].'"': ''; ?> type="checkbox" name="<?php echo $args['tab_key']; ?>[<?php echo $args['key']; ?>]" <?php echo $str; ?> /> 
  225. <span><i><?php echo $args['desc']; ?></i></span> 
  226. <?php 
  227.  
  228. /** 
  229. * WooCommerce Loads settigns 
  230. * @access public 
  231. * @param void 
  232. * @return void 
  233. */ 
  234. function load_settings() { 
  235. $this->general_settings = (array) get_option( $this->general_settings_key ); 
  236. $this->accounting_settings = (array) get_option( $this->accounting_settings_key ); 
  237. $this->order_settings = (array) get_option( $this->order_settings_key ); 
  238.  
  239. /** 
  240. * Tabs and plugin page setup 
  241. * @access public 
  242. * @param void 
  243. * @return void 
  244. */ 
  245. function plugin_options_tabs() { 
  246. $current_tab = isset( $_GET['tab'] ) ? $_GET['tab'] : $this->start_action_key; 
  247. $options = get_option('woocommerce_billogram_general_settings'); 
  248. echo '<div class="wrap"><h2>WooCommerce Billogram Integration</h2><div id="icon-edit" class="icon32"></div></div>'; 
  249. $key_status = $this->is_license_key_valid(); 
  250. if(!isset($options['license-key']) || $options['license-key'] == '' || $key_status!='Active') { 
  251. echo "<button type=\"button button-primary\" class=\"button button-primary\" title=\"\" style=\"margin:5px\" onclick=\"window.open('http://whmcs.onlineforce.net/cart.php?a=add&pid=54&billingcycle=annually', '_blank');\">Hmta licens-nyckel</button> <div class='key_error'>License Key ".$key_status."</div>"; 
  252.  
  253.  
  254. echo '<h2 class="nav-tab-wrapper">'; 
  255.  
  256. foreach ( $this->plugin_settings_tabs as $tab_key => $tab_caption ) { 
  257. $active = $current_tab == $tab_key ? 'nav-tab-active' : ''; 
  258. echo '<a class="nav-tab ' . $active . '" href="?page=' . $this->plugin_options_key . '&tab=' . $tab_key . '">' . $tab_caption . '</a>'; 
  259. echo '</h2>'; 
  260.  
  261.  
  262. /** 
  263. * WooCommerce Billogram General Settings 
  264. * @access public 
  265. * @param void 
  266. * @return void 
  267. */ 
  268. function register_woocommerce_billogram_general_settings() { 
  269.  
  270. $this->plugin_settings_tabs[$this->general_settings_key] = 'Allmnna instllningar'; 
  271.  
  272. register_setting( $this->general_settings_key, $this->general_settings_key ); 
  273. add_settings_section( 'section_general', 'Allmnna instllningar', array( &$this, 'section_general_desc' ), $this->general_settings_key ); 
  274. add_settings_field( 'woocommerce-billogram-license-key', 'Licens-nyckel', array( &$this, 'field_option_text' ), $this->general_settings_key, 'section_general', array ( 'id' => 'license-key', 'tab_key' => $this->general_settings_key, 'key' => 'license-key', 'desc' => 'Hr anges License-nyckeln du har erhllit frn oss via mail.') ); 
  275. add_settings_field( 'woocommerce-billogram-api-key', 'Billogram API-anvndar ID', array( &$this, 'field_option_text' ), $this->general_settings_key, 'section_general', array ( 'tab_key' => $this->general_settings_key, 'key' => 'api-key', 'desc' => 'Hr anges din API-anvndar ID frn Billogram. <a target="_blank" href="http://vimeo.com/62060237#t=0m50s">Videoinstruktion</a>') ); 
  276. add_settings_field( 'woocommerce-billogram-authorization-code', 'Billogram Lsenord', array( &$this, 'field_option_text' ), $this->general_settings_key, 'section_general', array ( 'tab_key' => $this->general_settings_key, 'key' => 'authorization_code', 'desc' => 'Hr anges din API kod frn Billogram. <a target="_blank" href="http://vimeo.com/62060237#t=0m50s">Videoinstruktion</a>') ); 
  277. add_settings_field( 'woocommerce-billogram-billogram-mode', 'Billogram lge', array( &$this, 'field_mode_dropdown'), $this->general_settings_key, 'section_general', array ( 'id' => 'billogram-mode', 'tab_key' => $this->general_settings_key, 'key' => 'billogram-mode', 'desc' => 'Vlj LIVE. SANDBOX lge anvnds endast av utvecklare')); 
  278. add_settings_field( 'woocommerce-billogram-activate-orders', 'Aktivera ORDER synkning', array( &$this, 'field_option_checkbox' ), $this->general_settings_key, 'section_general', array ( 'id' => 'activate-order-sync', 'tab_key' => $this->general_settings_key, 'key' => 'activate-orders', 'desc' => 'Skal vara vald fr att ordrar skal synkas till Billogram') ); 
  279. add_settings_field( 'woocommerce-billogram-activate-invoices', 'ORDER synkning method for Billogram checkout', array( &$this, 'field_option_dropdown'), $this->general_settings_key, 'section_general', array ( 'id' => 'order-sync', 'tab_key' => $this->general_settings_key, 'key' => 'activate-invoices', 'desc' => 'Vlj hr vad som skal hnda i Billogram nr en order i woocommerce synkas ditt')); 
  280. add_settings_field( 'woocommerce-billogram-activate-invoices-other', 'ORDER synkning method for other checkout', array( &$this, 'field_option_dropdown'), $this->general_settings_key, 'section_general', array ( 'id' => 'order-sync-other', 'tab_key' => $this->general_settings_key, 'key' => 'activate-invoices-other', 'desc' => 'Vlj hr vad som ska skapas p Billogram fr woocommerce order checkout via andra betalningsmetoder.')); 
  281. //add_settings_field( 'woocommerce-billogram-activate-allsync', 'Aktivera alla bestllningar synkning', array( &$this, 'field_option_checkbox' ), $this->general_settings_key, 'section_general', array ( 'tab_key' => $this->general_settings_key, 'key' => 'activate-allsync', 'desc' => 'Synka alla ordrar frn WooCommerce till Billogram oavsett om kund vljer annat betalningsalternativ (t.ex; Paypa, Dibs, Stripe, Payson etc.) <br><i style="margin-left:25px; color: #F00;">Om du r osker vad du ska vlja hr rekommenderar vi att du inte markerar detta alternativ.</i>') ); 
  282. add_settings_field( 'woocommerce-billogram-activate-prices', 'Aktivera PRODUKT synkning', array( &$this, 'field_option_checkbox' ), $this->general_settings_key, 'section_general', array ( 'tab_key' => $this->general_settings_key, 'key' => 'activate-prices', 'desc' => '') ); 
  283. add_settings_field( 'woocommerce-billogram-stock-reduction', 'Minska lagersaldo', array( &$this, 'field_stock_dropdown' ), $this->general_settings_key, 'section_general', array ( 'tab_key' => $this->general_settings_key, 'key' => 'stock-reduction', 'desc' => 'Vlj hr om lagersaldo i WooCommerce skal minskas nr Billogram registrerar fakturan som betald eller redan vid bestllningen.') );  
  284.  
  285. /** 
  286. * WooCommerce Billogram Accounting Settings 
  287. * @access public 
  288. * @param void 
  289. * @return void 
  290. */ 
  291. function register_woocommerce_billogram_accounting_settings() { 
  292.  
  293. $this->plugin_settings_tabs[$this->accounting_settings_key] = 'Bokfringsinstllningar'; 
  294.  
  295. register_setting( $this->accounting_settings_key, $this->accounting_settings_key ); 
  296. add_settings_section( 'section_accounting', 'Bokfringsinstllningar', array( &$this, 'section_accounting_desc' ), $this->accounting_settings_key ); 
  297. add_settings_field( 'woocommerce-billogram-account-25-vat', 'Konto frsljning 25% Moms', array( &$this, 'field_option_text'), $this->accounting_settings_key, 'section_accounting', array ( 'tab_key' => $this->accounting_settings_key, 'key' => 'account-25-vat', 'desc' => '') ); 
  298. add_settings_field( 'woocommerce-billogram-account-12-vat', 'Konto frsljning 12% Moms', array( &$this, 'field_option_text' ), $this->accounting_settings_key, 'section_accounting', array ( 'tab_key' => $this->accounting_settings_key, 'key' => 'account-12-vat', 'desc' => '') ); 
  299. add_settings_field( 'woocommerce-billogram-account-6-vat', 'Konto frsljning 6% Moms', array( &$this, 'field_option_text' ), $this->accounting_settings_key, 'section_accounting', array ( 'tab_key' => $this->accounting_settings_key, 'key' => 'account-6-vat', 'desc' => '') ); 
  300. add_settings_field( 'woocommerce-billogram-taxclass-account-25-vat', 'Skatteklass fr 25% Moms', array( &$this, 'field_option_text' ), $this->accounting_settings_key, 'section_accounting', array ( 'tab_key' => $this->accounting_settings_key, 'key' => 'taxclass-account-25-vat', 'desc' => '') ); 
  301. add_settings_field( 'woocommerce-billogram-taxclass-account-12-vat', 'Skatteklass fr 12% Moms', array( &$this, 'field_option_text' ), $this->accounting_settings_key, 'section_accounting', array ( 'tab_key' => $this->accounting_settings_key, 'key' => 'taxclass-account-12-vat', 'desc' => '') ); 
  302. add_settings_field( 'woocommerce-billogram-taxclass-account-6-vat', 'Skatteklass fr 6% Moms', array( &$this, 'field_option_text' ), $this->accounting_settings_key, 'section_accounting', array ( 'tab_key' => $this->accounting_settings_key, 'key' => 'taxclass-account-6-vat', 'desc' => '') ); 
  303.  
  304.  
  305.  
  306. /** 
  307. * WooCommerce Manual Actions Settings 
  308. * @access public 
  309. * @param void 
  310. * @return void 
  311. */ 
  312. function register_woocommerce_billogram_manual_action() { 
  313.  
  314. $this->plugin_settings_tabs[$this->manual_action_key] = 'Manuella funktioner'; 
  315. register_setting( $this->manual_action_key, $this->manual_action_key ); 
  316.  
  317.  
  318. /** 
  319. * WooCommerce Start Actions 
  320. * @access public 
  321. * @param void 
  322. * @return void 
  323. */ 
  324. function register_woocommerce_billogram_start_action() { 
  325. $this->plugin_settings_tabs[$this->start_action_key] = 'Vlkommen!'; 
  326. register_setting( $this->start_action_key, $this->start_action_key ); 
  327.  
  328.  
  329. /** 
  330. * WooCommerce Billogram Order Settings 
  331. * @access public 
  332. * @param void 
  333. * @return void 
  334. */ 
  335. function register_woocommerce_billogram_order_settings() { 
  336.  
  337. $this->plugin_settings_tabs[$this->order_settings_key] = 'Orderinstllningar'; 
  338.  
  339. register_setting( $this->order_settings_key, $this->order_settings_key ); 
  340. add_settings_section( 'section_order', 'Orderinstllningar', array( &$this, 'section_order_desc' ), $this->order_settings_key ); 
  341. add_settings_field( 'woocommerce-billogram-admin-fee', 'Administrationsavgift', array( &$this, 'field_option_text'), $this->order_settings_key, 'section_order', array ( 'id' => 'admin-fee', 'tab_key' => $this->order_settings_key, 'key' => 'admin-fee', 'desc' => '<br>Hr anges fakturaavgiften/administrationsavgiften fr Billogram <br>Lmna fltet tomt om avgift redan r konfigurerat i Billogram kontot under: Mitt konto --> Instllningar --> Fakturainstllningar --> Faktura avgift') ); 
  342.  
  343. //add_settings_field( 'woocommerce-billogram-due-date', 'Invoice Due date', array( &$this, 'field_option_date'), $this->order_settings_key, 'section_order', array ( 'id' => 'due-date', 'tab_key' => $this->order_settings_key, 'key' => 'due-date', 'desc' => '<br>Om inte instlld, d standard kommer att vara 30 dagar efter fakturadatum (eller beroende p grund dagar)') ); 
  344.  
  345. add_settings_field( 'woocommerce-billogram-due-days', 'Antal dagar till fakturans frfallodatum', array( &$this, 'field_option_text'), $this->order_settings_key, 'section_order', array ( 'id' => 'due-days', 'tab_key' => $this->order_settings_key, 'key' => 'due-days', 'desc' => '<br>Om detta ej stlls in s r standard 30 dagar') ); 
  346. /**add_settings_field( 'woocommerce-billogram-payment-options', 'Betalningsvillkor fr order', array( &$this, 'field_option_text'), $this->order_settings_key, 'section_order', array ( 'tab_key' => $this->order_settings_key, 'key' => 'payment-options', 'desc' => 'Hr anges Billogram-koden fr betalningsalternativ fr ordern. Koder finns under INSTLLNINGAR->BOKFRING->BETALNINGSALTERNATIV i Billogram.') );*/ 
  347.  
  348. /** 
  349. * WooCommerce Billogram Accounting Settings 
  350. * @access public 
  351. * @param void 
  352. * @return void 
  353. */ 
  354. function register_woocommerce_billogram_support() { 
  355.  
  356. $this->plugin_settings_tabs[$this->support_key] = 'Support'; 
  357. register_setting( $this->support_key, $this->support_key ); 
  358.  
  359. /** 
  360. * The description for the general section 
  361. * @access public 
  362. * @param void 
  363. * @return void 
  364. */ 
  365. function section_general_desc() { echo 'Hr anges grundinstllningar fr Billogramkopplingen och hr kan man styra vilka delar som ska synkas till Billogram'; } 
  366.  
  367. /** 
  368. * The description for the accounting section 
  369. * @access public 
  370. * @param void 
  371. * @return void 
  372. */ 
  373. function section_accounting_desc() { echo 'Beskrivning bokfringsinstllningar.'; } 
  374.  
  375. /** 
  376. * The description for the shipping section 
  377. * @access public 
  378. * @param void 
  379. * @return void 
  380. */ 
  381. function section_order_desc() { echo ''; } 
  382.  
  383. /** 
  384. * Options page 
  385. * @access public 
  386. * @param void 
  387. * @return void 
  388. */ 
  389. function woocommerce_billogram_options_page() { 
  390. $tab = isset( $_GET['tab'] ) ? $_GET['tab'] : $this->start_action_key;?> 
  391.  
  392. <!-- CSS --> 
  393. <style> 
  394. li.logo, { 
  395. float: left; 
  396. width: 100%; 
  397. padding: 20px; 
  398. li.full { 
  399. padding: 10px 0; 
  400. height: 50px; 
  401. li.full img, img.test_load{ 
  402. float: left; 
  403. margin: -5px 0 0 5px; 
  404. display: none; 
  405. span.test_warning{ 
  406. float: left; 
  407. margin:25px 0px 0px 10px; 
  408. li.col-two { 
  409. float: left; 
  410. width: 380px; 
  411. margin-left: 1%; 
  412. li.col-onethird, li.col-twothird { 
  413. float: left; 
  414. li.col-twothird { 
  415. max-width: 772px; 
  416. margin-right: 20px; 
  417. li.col-onethird { 
  418. width: 300px; 
  419. .mailsupport { 
  420. background: #dadada; 
  421. border-radius: 4px; 
  422. -moz-border-radius: 4px; 
  423. -webkit-border-radius: 4px; 
  424. max-width: 230px; 
  425. padding: 0 0 20px 20px; 
  426. .mailsupport > h2 { 
  427. font-size: 20px; 
  428. form#support table.form-table tbody tr td, form#installationSupport table.form-table tbody tr td { 
  429. padding: 4px 0 !important; 
  430. form#support input, form#support textarea, form#installationSupport input, form#support textarea { 
  431. border: 1px solid #b7b7b7; 
  432. border-radius: 3px; 
  433. -moz-border-radius: 3px; 
  434. -webkit-border-radius: 3px; 
  435. box-shadow: none; 
  436. width: 210px; 
  437. form#support textarea, form#installationSupport textarea { 
  438. height: 60px; 
  439. form#support button, form#installationSupport button { 
  440. float: left; 
  441. margin: 0 !important; 
  442. min-width: 100px; 
  443. ul.manuella li.full button.button { 
  444. clear: left; 
  445. float: left; 
  446. min-width: 250px; 
  447. ul.manuella li.full > p { 
  448. clear: right; 
  449. float: left; 
  450. margin: 2px 0 20px 11px; 
  451. max-width: 440px; 
  452. padding: 5px 10px; 
  453. .key_error 
  454. background-color: white; 
  455. color: red; 
  456. display: inline; 
  457. font-weight: bold; 
  458. margin-top: 5px; 
  459. padding: 5px; 
  460. position: absolute; 
  461. text-align: center; 
  462. width: 200px; 
  463. .testConnection{ 
  464. float:left; 
  465.  
  466. p.submit{ 
  467. float: left; 
  468. width: auto; 
  469. padding: 0px; 
  470. /*li.wp-first-item{ 
  471. display:none; 
  472. }*/ 
  473. span#sandbox-mode{ 
  474. color:#F00 
  475. span.error{ 
  476. color:#F00 
  477. </style> 
  478. <script type="text/javascript"> 
  479. jQuery(document).ready(function() { 
  480. var element = jQuery('#order-sync').parent().parent(); 
  481. if(jQuery('#activate-order-sync').is(':checked')) { 
  482. element.show(); 
  483. }else{ 
  484. element.hide(); 
  485. jQuery('#activate-order-sync').change(function() { 
  486. if(this.checked) { 
  487. element.show(300);  
  488. }else{ 
  489. element.hide(300); 
  490. }); 
  491.  
  492. //script for sandbox text 
  493. if(jQuery('#billogram-mode').val() == "Live") { 
  494. jQuery('#sandbox-mode').hide(); 
  495. jQuery('#billogram-mode').change(function() { 
  496. if(jQuery('#billogram-mode').val() == "Live") { 
  497. jQuery('#sandbox-mode').hide(100); 
  498. }else{ 
  499. jQuery('#sandbox-mode').show(100); 
  500. }); 
  501.  
  502. jQuery("#license-key").keyup(function() { 
  503. var str = jQuery("#license-key").val(); 
  504. var patt = /wbm-[a-zA-Z0-9][^\W]+/gi; 
  505. var licenseMatch = patt.exec(str); 
  506. if(licenseMatch) { 
  507. licenseMatch = licenseMatch.toString(); 
  508. if(licenseMatch.length == 24) { 
  509. jQuery("#license-key").next().removeClass("error"); 
  510. jQuery("#license-key").next().children("i").html("Hr anges License-nyckeln du har erhllit frn oss via mail."); 
  511. }else{ 
  512. jQuery("#license-key").next().children("i").html("Ogiltigt format"); 
  513. jQuery("#license-key").next().addClass("error"); 
  514. }else{ 
  515. jQuery("#license-key").next().children("i").html("Ogiltigt format"); 
  516. jQuery("#license-key").next().addClass("error"); 
  517. }); 
  518.  
  519. jQuery("#admin-fee").keyup(function() { 
  520. if(!jQuery.isNumeric(jQuery(this).val()) && jQuery(this).val() != '') { 
  521. jQuery(this).next().children("i").html("Ogiltigt format"); 
  522. jQuery(this).next().addClass("error"); 
  523. }else{ 
  524. jQuery(this).next().removeClass("error"); 
  525. jQuery(this).next().children("i").html("<br>Hr anges fakturaavgiften/administrationsavgiften fr Billogram <br>Lmna fltet tomt om avgift redan r konfigurerat i Billogram kontot under: Mitt konto --> Instllningar --> Fakturainstllningar --> Faktura avgift"); 
  526. }); 
  527.  
  528. jQuery("#due-date").keyup(function() { 
  529. if(!jQuery.isNumeric(jQuery(this).val()) && jQuery(this).val() != '') { 
  530. jQuery(this).next().children("i").html("Ogiltigt format"); 
  531. jQuery(this).next().addClass("error"); 
  532. }else{ 
  533. jQuery(this).next().removeClass("error"); 
  534. jQuery(this).next().children("i").html("<br>Om inte instlld, d standard kommer att vara 30 dagar efter fakturadatum (eller beroende p grund dagar)"); 
  535. }); 
  536.  
  537. jQuery("#due-days").keyup(function() { 
  538. if(!jQuery.isNumeric(jQuery(this).val()) && jQuery(this).val() != '') { 
  539. jQuery(this).next().children("i").html("Ogiltigt format"); 
  540. jQuery(this).next().addClass("error"); 
  541. }else{ 
  542. jQuery(this).next().removeClass("error"); 
  543. jQuery(this).next().children("i").html("<br>Om inte instlld, kommer standard vara 30 dagar (eller beroende p frfallodagen)"); 
  544. }); 
  545.  
  546. jQuery("#billogramOrderinstallningar").submit(function(e) { 
  547. if(!jQuery.isNumeric(jQuery("#admin-fee").val()) && jQuery("#admin-fee").val() != '') { 
  548. e.preventDefault(); 
  549. jQuery("#admin-fee").next().children("i").html("Ogiltigt format"); 
  550. jQuery("#admin-fee").next().addClass("error"); 
  551. }else{ 
  552. jQuery("#admin-fee").next().removeClass("error"); 
  553. jQuery("#admin-fee").next().children("i").html("<br>Hr anges fakturaavgiften/administrationsavgiften fr Billogram <br>Lmna fltet tomt om avgift redan r konfigurerat i Billogram kontot under: Mitt konto --> Instllningar --> Fakturainstllningar --> Faktura avgift"); 
  554. }); 
  555. }); 
  556. </script> 
  557. <?php 
  558. if($tab == $this->support_key) { ?> 
  559. <div class="wrap"> 
  560. <?php $this->plugin_options_tabs(); ?> 
  561. <ul> 
  562. <li class="logo"><?php echo '<img src="' . plugins_url( 'img/logo_landscape.png', __FILE__ ) . '" > '; ?></li> 
  563. <li class="col-two"><a href="http://woobill.com/category/faq/"><?php echo '<img src="' . plugins_url( 'img/awp_faq.png', __FILE__ ) . '" > '; ?></a></li> 
  564. <li class="col-two"><a href="http://woobill.com/"><?php echo '<img src="' . plugins_url( 'img/awp_support.png', __FILE__ ) . '" > '; ?></a></li> 
  565. </div> 
  566. <?php 
  567. else if($tab == $this->general_settings_key) { ?> 
  568. <div class="wrap"> 
  569. <?php $this->plugin_options_tabs(); ?> 
  570. <form method="post" id="billogramGeneralSettings" action="options.php"> 
  571. <?php wp_nonce_field( 'update-options' ); ?> 
  572. <?php settings_fields( $tab ); ?> 
  573. <?php do_settings_sections( $tab ); ?> 
  574. <?php submit_button('Spara ndringar'); ?> 
  575. <button style="margin: 20px 0px 0px 10px;" type="button" name="testConnection" class="button button-primary testConnection" onclick="test_connection()" />Testa anslutning</button> 
  576. <span class="test_warning">OBS! Spara ndringar innan du testar anslutning</span> 
  577. <img style="margin: 10px 0px 0px 10px;" src="<?php echo plugins_url( 'img/ajax-loader.gif', __FILE__ );?>" class="test_load" > 
  578. </form> 
  579. </div> 
  580. <?php } 
  581. else if($tab == $this->manual_action_key) { ?> 
  582. <div class="wrap"> 
  583. <?php $this->plugin_options_tabs(); ?> 
  584. <ul class="manuella"> 
  585. <li class="full"> 
  586. <button type="button" class="button" title="Manuell Synkning" style="margin:5px" onclick="fetch_contacts()">Manuell synkning kontakter</button> 
  587. <img src="<?php echo plugins_url( 'img/ajax-loader.gif', __FILE__ );?>" class="customer_load" > 
  588. <p>Hmtar alla kunder frn er Billogram. Detta grs fr att undvika dubbletter.</p> 
  589. </li> 
  590. <li class="full"> 
  591. <button type="button" class="button" title="Manuell Synkning Orders" style="margin:5px" onclick="sync_orders()">Manuell synkning ordrar</button> 
  592. <img src="<?php echo plugins_url( 'img/ajax-loader.gif', __FILE__ );?>" class="order_load" > 
  593. <p>Synkroniserar alla ordrar som misslyckats att synkronisera.     </p> 
  594. </li> 
  595. <li class="full"> 
  596. <button type="button" class="button" title="Manuell Synkning Products" style="margin:5px" onclick="initial_sync_products()">Manuell synkning produkter</button> 
  597. <img src="<?php echo plugins_url( 'img/ajax-loader.gif', __FILE__ );?>" class="product_load" > 
  598. <p>Skicka alla produkter till er Billogram. Om ni har mnga produkter kan det ta ett tag.</p> 
  599. </li> 
  600. </ul> 
  601. </div> 
  602. <?php } 
  603. else if($tab == $this->start_action_key) { 
  604. $options = get_option('woocommerce_billogram_general_settings'); 
  605. ?> 
  606. <div class="wrap"> 
  607. <?php $this->plugin_options_tabs(); ?> 
  608. <ul> 
  609. <li> 
  610. <?php echo '<img src="' . plugins_url( 'img/banner-772x250.png', __FILE__ ) . '" > '; ?> 
  611. </li> 
  612. <li class="col-twothird"> 
  613. <iframe src="https://player.vimeo.com/video/125486646" width="500" height="281" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe> 
  614. </li> 
  615. <?php if(!isset($options['license-key']) || $options['license-key'] == '') { ?> 
  616. <li class="col-onethird"> 
  617. <div class="mailsupport"> 
  618. <h2>Installationssupport</h2> 
  619. <form method="post" id="installationSupport"> 
  620. <input type="hidden" value="send_support_mail" name="action"> 
  621. <table class="form-table"> 
  622.  
  623. <tbody> 
  624. <tr valign="top"> 
  625. <td> 
  626. <input type="text" value="" placeholder="Fretag" name="company"> 
  627. </td> 
  628. </tr> 
  629. <tr valign="top"> 
  630. <td> 
  631. <input type="text" value="" placeholder="Namn" name="name"> 
  632. </td> 
  633. </tr> 
  634. <tr valign="top"> 
  635. <td> 
  636. <input type="text" value="" placeholder="Telefon" name="telephone"> 
  637. </td> 
  638. </tr> 
  639. <tr valign="top"> 
  640. <td> 
  641. <input type="text" value="" placeholder="Email" name="email"> 
  642. </td> 
  643. </tr> 
  644. <tr valign="top"> 
  645. <td> 
  646. <textarea placeholder="rende" name="subject"></textarea> 
  647. </td> 
  648. </tr> 
  649. <tr valign="top"> 
  650. <td> 
  651. <button type="button" class="button button-primary" title="send_support_mail" style="margin:5px" onclick="send_support_mail('installationSupport')">Skicka</button> 
  652. </td> 
  653. </tr> 
  654. </tbody> 
  655. </table> 
  656. <!-- p class="submit"> 
  657. <button type="button" class="button button-primary" title="send_support_mail" style="margin:5px" onclick="send_support_mail()">Skicka</button>  
  658. </p --> 
  659. </form> 
  660. </div> 
  661. </li> 
  662. <?php } else{ ?> 
  663. <li class="col-onethird"> 
  664. <div class="mailsupport"> 
  665. <h2>Support</h2> 
  666. <form method="post" id="support"> 
  667. <input type="hidden" value="send_support_mail" name="action"> 
  668. <table class="form-table"> 
  669.  
  670. <tbody> 
  671. <tr valign="top"> 
  672. <td> 
  673. <input type="text" value="" placeholder="Fretag" name="company"> 
  674. </td> 
  675. </tr> 
  676. <tr valign="top"> 
  677. <td> 
  678. <input type="text" value="" placeholder="Namn" name="name"> 
  679. </td> 
  680. </tr> 
  681. <tr valign="top"> 
  682. <td> 
  683. <input type="text" value="" placeholder="Telefon" name="telephone"> 
  684. </td> 
  685. </tr> 
  686. <tr valign="top"> 
  687. <td> 
  688. <input type="text" value="" placeholder="Email" name="email"> 
  689. </td> 
  690. </tr> 
  691. <tr valign="top"> 
  692. <td> 
  693. <textarea placeholder="rende" name="subject"></textarea> 
  694. </td> 
  695. </tr> 
  696. <tr valign="top"> 
  697. <td> 
  698. <input type="hidden" name="supportForm" value="support" /> 
  699. <button type="button" class="button button-primary" title="send_support_mail" style="margin:5px" onclick="send_support_mail('support')">Skicka</button> 
  700. </td> 
  701. </tr> 
  702. </tbody> 
  703. </table> 
  704. <!-- p class="submit"> 
  705. <button type="button" class="button button-primary" title="send_support_mail" style="margin:5px" onclick="send_support_mail()">Skicka</button>  
  706. </p --> 
  707. </form> 
  708. </div> 
  709. </li> 
  710. <?php } ?> 
  711. </ul> 
  712. </div> 
  713. <?php } 
  714. else{ ?> 
  715. <div class="wrap"> 
  716. <?php $this->plugin_options_tabs(); ?> 
  717. <form method="post" id="billogramOrderinstallningar" action="options.php"> 
  718. <?php wp_nonce_field( 'update-options' ); ?> 
  719. <?php settings_fields( $tab ); ?> 
  720. <?php do_settings_sections( $tab ); ?> 
  721. <?php submit_button(); ?> 
  722. </form> 
  723. </div> 
  724. <?php } 
  725.  
  726. /*********************************************************************************************************** 
  727. * BILLOGRAM FUNCTIONS 
  728. ***********************************************************************************************************/ 
  729.  
  730.  
  731. /** 
  732. * Fetches contacts from Billogram and writes them local db 
  733. * @access public 
  734. * @return void 
  735. */ 
  736. public function fetch_billogram_contacts() { 
  737. include_once("class-billogram2-order-xml.php"); 
  738. include_once("class-billogram2-database-interface.php"); 
  739. include_once("class-billogram2-api.php"); 
  740.  
  741. $apiInterface = new WCB_API(); 
  742. $customers = $apiInterface->get_customers(); 
  743. $databaseInterface = new WCB_Database_Interface(); 
  744.  
  745. foreach($customers as $customer) { 
  746. $exist = $databaseInterface->get_customer_by_email($customer->contact->email); 
  747. if(empty($exist)) { 
  748. $databaseInterface->create_existing_customer($customer); 
  749. return true; 
  750.  
  751. /** 
  752. * Sends contact to Billogram API 
  753. * @access public 
  754. * @param int $orderId 
  755. * @return void 
  756. */ 
  757. public function send_contact_to_billogram($orderId) { 
  758. global $wcdn, $woocommerce; 
  759. $options = get_option('woocommerce_billogram_general_settings'); 
  760. if($this->is_license_key_valid()=='Active') { 
  761. include_once("class-billogram2-contact-xml.php"); 
  762. include_once("class-billogram2-database-interface.php"); 
  763. include_once("class-billogram2-api.php"); 
  764. //fetch Order 
  765. $database = new WCB_Database_Interface(); 
  766. $order = new WC_Order($orderId); 
  767. if(class_exists('WC_Subscriptions_Order')) { 
  768. logthis('Total_initial_payment: '.WC_Subscriptions_Order::get_total_initial_payment( $order )); 
  769. if(WC_Subscriptions_Order::get_total_initial_payment( $order ) == 0) { 
  770. logthis('Order is not synced, because the subscription has a free trial period and no signup fee, invoice for 0 amount is not created'); 
  771. return true; 
  772. //logthis("Payment method: ".$order->payment_method); 
  773. if($order->payment_method != 'billogram-invoice') { 
  774. if($options['activate-invoices-other'] == "Gra ingenting") { 
  775. $unsyncedOrders = $database->read_unsynced_orders(); 
  776. foreach($unsyncedOrders as $unsyncedOrder) { 
  777. if($unsyncedOrder->order_id == $orderId) { 
  778. logthis('Payment method is not Billogram faktura and "Do nothing" is selected for "ORDER synkning method for other checkout". Order: '.$orderId.' is registerd as unsynced order already.'); 
  779. return false; 
  780. else{ 
  781. $database->create_unsynced_order($orderId); 
  782. logthis('Payment method is not Billogram faktura and "Do nothing" is selected for "ORDER synkning method for other checkout". Order: '.$orderId.' is registerd as unsynced order.'); 
  783. return false; 
  784. $database->create_unsynced_order($orderId); 
  785. return false; 
  786.  
  787. logthis('send_contact_to_billogram'); 
  788. $customerNumber = $this->get_or_create_customer($order); 
  789. logthis("CREATE UNSYNCED ORDER"); 
  790. if(!$database->is_synced_order($orderId)) { 
  791. //Save 
  792. $database->create_unsynced_order($orderId); 
  793. if(!isset($options['activate-orders'])) { 
  794. return true; 
  795.  
  796. if($options['activate-orders'] == 'on') { 
  797. $orderNumber = $this->send_order_to_billogram($orderId, $customerNumber); 
  798. if($orderNumber == 0) { 
  799. $database->set_as_synced($orderId); 
  800. return true; 
  801.  
  802. /** 
  803. * Sends order to Billogram API 
  804. * @access public 
  805. * @param int $orderId 
  806. * @param $customerNumber 
  807. * @return void 
  808. */ 
  809. public function send_order_to_billogram($orderId, $customerNumber) { 
  810. global $wcdn; 
  811. $options = get_option('woocommerce_billogram_general_settings'); 
  812. if(!isset($options['activate-orders'])) { 
  813. return; 
  814. if($options['activate-orders'] == 'on') { 
  815. include_once("class-billogram2-order-xml.php"); 
  816. include_once("class-billogram2-database-interface.php"); 
  817. include_once("class-billogram2-api.php"); 
  818.  
  819. //fetch Order 
  820. $order = new WC_Order($orderId); 
  821. logthis("ORDER"); 
  822. logthis($order); 
  823. //Init API 
  824. $apiInterface = new WCB_API(); 
  825.  
  826. //create Order XML 
  827. $orderDoc = new WCB_Order_XML_Document(); 
  828. $orderXml = $orderDoc->create($order, $customerNumber); 
  829.  
  830. logthis("orderxml:"); 
  831. logthis($orderXml); 
  832.  
  833. //send Order XML 
  834. $orderResponse = $apiInterface->create_order_request($orderXml); 
  835.  
  836. logthis("OrderResponse: ".$orderResponse); 
  837. //Error handling 
  838. if(array_key_exists('Error', $orderResponse)) { 
  839. logthis(print_r($orderResponse, true)); 
  840. // if order exists 
  841. if($orderResponse['Code'] == $this->BILLOGRAM_ERROR_CODE_ORDER_EXISTS) { 
  842. logthis("ORDER EXISTS"); 
  843. $apiInterface->update_order_request($orderXml, $orderId); 
  844. // if products dont exist 
  845. elseif($orderResponse['Code'] == $this->BILLOGRAM_ERROR_CODE_PRODUCT_NOT_EXIST) { 
  846. logthis("PRODUCT DOES NOT EXIST"); 
  847.  
  848. foreach($order->get_items() as $item) { 
  849. //if variable product there might be a different SKU 
  850. if(empty($item['variation_id'])) { 
  851. $productId = $item['product_id']; 
  852. else{ 
  853. $productId = $item['variation_id']; 
  854. $this->send_product_to_billogram($productId); 
  855. $orderResponse = $apiInterface->create_order_request($orderXml); 
  856. else{ 
  857. logthis("CREATE UNSYNCED ORDER"); 
  858. //Init DB 2000861 
  859. $database = new WCB_Database_Interface(); 
  860. //Save 
  861. $database->create_unsynced_order($orderId); 
  862. return 1; 
  863. /**if(!isset($options['activate-invoices'])) { 
  864. return; 
  865. }*/ 
  866. $this->create_billogram_invoice($order, $apiInterface, $orderResponse, $options); 
  867. return 0; 
  868.  
  869. /** 
  870. * Creates invoice at Billogram. 
  871. * @access public 
  872. * @param obj $order 
  873. * @param obj apiInterface API object 
  874. * @param order post data XML 
  875. * @param plugin objects array options 
  876. * @return void 
  877. */ 
  878. public function create_billogram_invoice($order, $apiInterface, $orderResponse, $options) { 
  879. $InvoiceSendMethod = ''; 
  880. if(($order->payment_method == 'billogram-invoice' && $options['activate-invoices'] == 'Skapa faktura och skicka som epost') || ($order->payment_method != 'billogram-invoice' && $options['activate-invoices-other'] == 'Skapa faktura och skicka som epost')) { 
  881. $InvoiceSendMethod = 'email'; 
  882. if(($order->payment_method == 'billogram-invoice' && $options['activate-invoices'] == 'Skapa faktura och skicka som brev') || ($order->payment_method != 'billogram-invoice' && $options['activate-invoices-other'] == 'Skapa faktura och skicka som brev')) { 
  883. $InvoiceSendMethod = 'letter'; 
  884. if(($order->payment_method == 'billogram-invoice' && $options['activate-invoices'] == 'Skapa fakturor och skicka som e-post och brev') || ($order->payment_method != 'billogram-invoice' && $options['activate-invoices-other'] == 'Skapa fakturor och skicka som e-post och brev')) { 
  885. $InvoiceSendMethod = 'email+letter'; 
  886.  
  887. if($order->payment_method == 'billogram-invoice') { 
  888. if(($options['activate-invoices'] == 'Skapa faktura och skicka som epost' || $options['activate-invoices'] == 'Skapa faktura och skicka som brev' || $options['activate-invoices'] == 'Skapa fakturor och skicka som e-post och brev')) { 
  889. //Create invoice 
  890. $invoiceResponse = $apiInterface->create_order_invoice_request($orderResponse, $InvoiceSendMethod); 
  891. }else{ 
  892. if(($options['activate-invoices-other'] == 'Skapa faktura och skicka som epost' || $options['activate-invoices-other'] == 'Skapa faktura och skicka som brev' || $options['activate-invoices-other'] == 'Skapa fakturor och skicka som e-post och brev')) { 
  893. //Create invoice 
  894. $invoiceResponse = $apiInterface->create_order_invoice_request($orderResponse, $InvoiceSendMethod); 
  895.  
  896.  
  897.  
  898. /** 
  899. * Sends order to Billogram API 
  900. * @access public 
  901. * @param obj $order 
  902. * @param $customerNumber 
  903. * @return void 
  904. */ 
  905. public function send_scheduled_subscription_order_to_billogram($amount_to_charge, $order, $customerNumber) { 
  906. global $wcdn; 
  907. $options = get_option('woocommerce_billogram_general_settings'); 
  908. include_once("class-billogram2-order-xml.php"); 
  909. include_once("class-billogram2-database-interface.php"); 
  910. include_once("class-billogram2-api.php"); 
  911. //Init API 
  912. $apiInterface = new WCB_API(); 
  913.  
  914. //create Order XML 
  915. $orderDoc = new WCB_Order_XML_Document(); 
  916.  
  917. $orderXml = $orderDoc->create_scheduled_subscription($amount_to_charge, $order, $customerNumber); 
  918.  
  919. logthis("subscription orderxml:"); 
  920. logthis($orderXml); 
  921.  
  922. //send Order XML 
  923. $orderResponse = $apiInterface->create_order_request($orderXml); 
  924.  
  925. logthis("OrderResponse: ".$orderResponse); 
  926. //Error handling 
  927. if(array_key_exists('Error', $orderResponse)) { 
  928. logthis(print_r($orderResponse, true)); 
  929. // if order exists 
  930. if($orderResponse['Code'] == $this->BILLOGRAM_ERROR_CODE_ORDER_EXISTS) { 
  931. logthis("ORDER EXISTS"); 
  932. $apiInterface->update_order_request($orderXml, $order->id); 
  933. // if products dont exist 
  934. elseif($orderResponse['Code'] == $this->BILLOGRAM_ERROR_CODE_PRODUCT_NOT_EXIST) { 
  935. logthis("PRODUCT DOES NOT EXIST"); 
  936.  
  937. foreach($order->get_items() as $item) { 
  938. //if variable product there might be a different SKU 
  939. if(empty($item['variation_id'])) { 
  940. $productId = $item['product_id']; 
  941. else{ 
  942. $productId = $item['variation_id']; 
  943. $this->send_product_to_billogram($productId); 
  944. $orderResponse = $apiInterface->create_order_request($orderXml); 
  945. else{ 
  946. logthis("CREATE UNSYNCED ORDER"); 
  947. //Init DB 2000861 
  948. $database = new WCB_Database_Interface(); 
  949. //Save 
  950. $database->create_unsynced_order($order->id); 
  951. return 1; 
  952. $this->create_billogram_invoice($order, $apiInterface, $orderResponse, $options); 
  953. return 0; 
  954.  
  955. /** 
  956. * Sends ALL unsynced orders to Billogram API 
  957. * @access public 
  958. * @return void 
  959. */ 
  960. public function sync_orders_to_billogram() { 
  961. include_once("class-billogram2-order-xml.php"); 
  962. include_once("class-billogram2-contact-xml.php"); 
  963. include_once("class-billogram2-database-interface.php"); 
  964. include_once("class-billogram2-api.php"); 
  965.  
  966. $options = get_option('woocommerce_billogram_general_settings'); 
  967.  
  968. $apiInterface = new WCB_API(); 
  969. $databaseInterface = new WCB_Database_Interface(); 
  970. $unsyncedOrders = $databaseInterface->read_unsynced_orders(); 
  971.  
  972. foreach($unsyncedOrders as $order) { 
  973.  
  974. $orderId = $order->order_id; 
  975.  
  976. $order = new WC_Order($orderId); 
  977. if($order->payment_method != 'billogram-invoice') { 
  978. if($options['activate-allsync'] != "on") { 
  979. return true; 
  980.  
  981. $customerNumber = $this->get_or_create_customer($order); 
  982.  
  983. //create Order XML 
  984. $orderDoc = new WCB_Order_XML_Document(); 
  985. $orderXml = $orderDoc->create($order, $customerNumber); 
  986.  
  987. //send Order XML 
  988. $orderResponse = $apiInterface->create_order_request($orderXml); 
  989.  
  990. if($orderResponse->id) { 
  991. $databaseInterface->set_as_synced($orderId);  
  992. else{ 
  993. return false; 
  994. if(!isset($options['activate-invoices'])) { 
  995. continue; 
  996. $this->create_billogram_invoice($order, $apiInterface, $orderResponse, $options); 
  997. return true; 
  998.  
  999.  
  1000. /** 
  1001. * Syncs ALL products to Billogram API 
  1002. * @access public 
  1003. * @return bool 
  1004. */ 
  1005. public function initial_products_sync() { 
  1006. $args = array( 
  1007. 'post_type' => array('product', 'product_variation'),  
  1008. 'orderby' => 'id',  
  1009. 'posts_per_page' => -1,  
  1010. ); 
  1011. $the_query = new WP_Query( $args ); 
  1012. //logthis($the_query->get_posts()); 
  1013. foreach($the_query->get_posts() as $product) { 
  1014. $this->send_product_to_billogram($product->ID); 
  1015. wp_reset_postdata(); 
  1016. return true; 
  1017.  
  1018. /** 
  1019. * Sends product to Billogram API 
  1020. * @access public 
  1021. * @param $productId 
  1022. * @internal param int $orderId 
  1023. * @return void 
  1024. */ 
  1025. public function send_product_to_billogram($productId) { 
  1026. $post = get_post($productId); 
  1027. if(($post->post_type == 'product' || $post->post_type == 'product_variation') && $post->post_status == 'publish') { 
  1028. logthis('send_product_to_billogram called'); 
  1029. $args = array( 
  1030. 'post_parent' => $productId,  
  1031. 'post_type' => 'product_variation',  
  1032. 'numberposts' => -1,  
  1033. 'post_status' => 'publish'  
  1034. );  
  1035. $children_array = get_children( $args ); 
  1036. //logthis('Product ID is being synced :'.$productId.' does it have child products (variations)?:'); 
  1037. //logthis($children_array); 
  1038. if(is_array($children_array) && !empty($children_array)) { 
  1039. //logthis('Product has variation and syncing one by one!'); 
  1040. //logthis($children_array); 
  1041. $this->call_send_product_to_billogram($productId); 
  1042. foreach ($children_array as $variation_product) { 
  1043. //logthis('syncing variatoin product ID: '.$variation_product->ID); 
  1044. $this->call_send_product_to_billogram($variation_product->ID); 
  1045. }else{ 
  1046. //logthis('syncing product ID: '.$productId); 
  1047. return $this->call_send_product_to_billogram($productId); 
  1048. }else{ 
  1049. logthis('send_product_to_billogram exit, because saved object is not a product: object ID: '.$productId.' object type: '.$post->post_type); 
  1050. return true; 
  1051.  
  1052. public function call_send_product_to_billogram($productId) { 
  1053. global $wcdn; 
  1054. $options = get_option('woocommerce_billogram_general_settings'); 
  1055. if(!isset($options['activate-prices'])) { 
  1056. return; 
  1057.  
  1058. if($options['activate-prices'] == 'on' && $this->is_license_key_valid()=='Active') { 
  1059.  
  1060. include_once("class-billogram2-product-xml.php"); 
  1061. include_once("class-billogram2-database-interface.php"); 
  1062. include_once("class-billogram2-api.php"); 
  1063. //fetch Product 
  1064.  
  1065. $product = new WC_Product($productId); 
  1066. //logthis($product); 
  1067.  
  1068. //fetch meta 
  1069. //Init API 
  1070. $apiInterface = new WCB_API(); 
  1071. //create Product XML 
  1072. $productDoc = new WCB_Product_XML_Document(); 
  1073. $databaseInterface = new WCB_Database_Interface(); 
  1074. $cur_sku = $product->get_sku(); 
  1075. //$cur_sku_ajax = get_post_meta( $productId, '_sku', true ); 
  1076. $sku_array = $databaseInterface->get_product_sku($productId); 
  1077. $sku = $sku_array[0]->product_sku; 
  1078.  
  1079. $isSynced = get_post_meta( $productId, '_is_synced_to_billogram' ); 
  1080. logthis($productId); 
  1081. if (!empty($isSynced) && !empty($sku_array)) { 
  1082. logthis("UPDATE PRODUCT"); 
  1083. //echo $cur_sku."|".$sku."|".$cur_sku_ajax; 
  1084. //exit; 
  1085. if($cur_sku!=$sku) 
  1086. $productXml = $productDoc->create($product); 
  1087. else 
  1088. $productXml = $productDoc->update($product); 
  1089.  
  1090. logthis($productXml); 
  1091.  
  1092. $updateResponse = $apiInterface->update_product_request($productXml, $sku); 
  1093. /** $productPriceXml = $productDoc->update_price($product); 
  1094. $apiInterface->update_product_price_request($productPriceXml, $sku);*/ 
  1095.  
  1096. if($updateResponse->item_no) { 
  1097. $billogramId = $updateResponse->item_no; 
  1098.  
  1099. //set sku; 
  1100. $databaseInterface->update_product_sku($productId, $billogramId); 
  1101. update_post_meta($productId, '_is_synced_to_billogram', 1); 
  1102.  
  1103. return $updateResponse; 
  1104. else{ 
  1105. logthis("CREATE PRODUCT"); 
  1106. $productXml = $productDoc->create($product); 
  1107.  
  1108. $productResponseCode = $apiInterface->create_product_request($productXml); 
  1109. //print_r($productResponseCode); 
  1110. $billogramId = $productResponseCode->item_no; 
  1111. //set sku; 
  1112. if($productResponseCode->item_no) { 
  1113. //echo $billogramId = $productResponseCode->item_no; 
  1114. $databaseInterface->set_product_sku($productId, $billogramId); 
  1115. update_post_meta($productId, '_sku', $billogramId); 
  1116. update_post_meta($productId, '_is_synced_to_billogram', 1); 
  1117.  
  1118. return $productResponseCode; 
  1119.  
  1120. /*********************************************************************************************************** 
  1121. * WP-PLUGS API FUNCTIONS 
  1122. ***********************************************************************************************************/ 
  1123.  
  1124. /** 
  1125. * Checks if API-key is valid 
  1126. * @access public 
  1127. * @return void 
  1128. */ 
  1129. public function is_api_key_valid() { 
  1130. include_once("class-billogram2-api.php"); 
  1131. $apiInterface = new WCB_API(); 
  1132. return $apiInterface->create_api_validation_request(); 
  1133.  
  1134. /** 
  1135. * Checks if license-key is valid 
  1136. * @access public 
  1137. * @return void 
  1138. */ 
  1139. public function is_license_key_valid() { 
  1140. include_once("class-billogram2-api.php"); 
  1141. $apiInterface = new WCB_API(); 
  1142. $result = $apiInterface->create_license_validation_request(); 
  1143. switch ($result['status']) { 
  1144. case "Active": 
  1145. // get new local key and save it somewhere 
  1146. $localkeydata = $result['localkey']; 
  1147. update_option( 'local_key_billogram_plugin', $localkeydata ); 
  1148. return $result['status']; 
  1149. break; 
  1150. case "Invalid": 
  1151. logthis("License key is Invalid"); 
  1152. return $result['status']; 
  1153. break; 
  1154. case "Expired": 
  1155. logthis("License key is Expired"); 
  1156. return $result['status']; 
  1157. break; 
  1158. case "Suspended": 
  1159. logthis("License key is Suspended"); 
  1160. return $result['status']; 
  1161. break; 
  1162. default: 
  1163. logthis("Invalid Response"); 
  1164. break; 
  1165.  
  1166. /** 
  1167. * Get user data of current order completed 
  1168. * @access public 
  1169. * @return void 
  1170. */ 
  1171. public function get_orderd_user_data($orderId , $key) { 
  1172.  
  1173. return $value = get_post_meta( $orderId , '_'.$key, true ); 
  1174. /** 
  1175. * Fetches customer from DB or creates it at Billogram 
  1176. * @access public 
  1177. * @param $order 
  1178. * @return void 
  1179. */ 
  1180. public function get_or_create_customer($order) { 
  1181. include_once("class-billogram2-database-interface.php"); 
  1182. include_once("class-billogram2-api.php"); 
  1183. include_once("class-billogram2-contact-xml.php"); 
  1184. $databaseInterface = new WCB_Database_Interface(); 
  1185. //$customeremail = $this->get_orderd_user_data($order->id, 'billing_email'); 
  1186. $customer = $databaseInterface->get_customer_by_email($order->billing_email); 
  1187.  
  1188. //Init API 
  1189. $apiInterface = new WCB_API(); 
  1190. //create Contact XML 
  1191. $contactDoc = new WCB_Contact_XML_Document(); 
  1192. $contactXml = $contactDoc->create($order); 
  1193. //logthis("billing emal:".$order->billing_email); 
  1194. //logthis("customer:".print_r($customer, true)); 
  1195. if(empty($customer)) { 
  1196.  
  1197. $customerId = $databaseInterface->create_customer($order->billing_email); 
  1198. //send Contact XML 
  1199. $contactResponseCode = $apiInterface->create_customer_request($contactXml); 
  1200. logthis("contactResponseCode:".$contactResponseCode); 
  1201. $customerNumber = $contactResponseCode->customer_no; 
  1202. $databaseInterface->update_customer($customerId, $customerNumber); 
  1203.  
  1204. else{ 
  1205. $customerNumber = $customer[0]->customer_number; 
  1206. $apiInterface->update_customer_request($contactXml, $customerNumber); 
  1207.  
  1208. return $customerNumber;