PluginProbe
Billingo Official for WooCommerce / 2.0
Billingo Official for WooCommerce v2.0
4.3.3 4.3.2 trunk 0.0.2 1.0.0 2.0 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 All 82 releases
billingo / index.php

index.php in Billingo Official for WooCommerce 2.0, at index.php

955 lines 39.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: Integration for Billingo & WooCommerce - Extended
4 Plugin URI:
5 Description: Billingo összeköttetés WooCommercehez - kiegészített verzió
6 Author: Vizmeg Viktor & Bokor Bence & Viszt Péter
7 Version: 1.2.4
8 */
9
10 if (!defined('ABSPATH')) exit; // Exit if accessed directly
11
12 //Load billingo API
13 require_once plugin_dir_path(__FILE__) . 'lib/autoload.php';
14
15 //Setup Billingo API
16 use Billingo\API\Connector\HTTP\Request;
17
18 class WC_Billingo
19 {
20
21 public static $plugin_prefix;
22 public static $plugin_url;
23 public static $plugin_path;
24 public static $plugin_basename;
25 public static $version;
26 private $billingo;
27
28 //Construct
29 public function __construct()
30 {
31
32 //Default variables
33 self::$plugin_prefix = 'wc_billingo_';
34 self::$plugin_basename = plugin_basename(__FILE__);
35 self::$plugin_url = plugin_dir_url(self::$plugin_basename);
36 self::$plugin_path = trailingslashit(dirname(__FILE__));
37 self::$version = '1.2.3';
38
39 //Wordpress and Woocommerce hooks&filter
40 add_action('admin_init', array($this, 'wc_billingo_admin_init'));
41
42 add_filter('woocommerce_general_settings', array($this, 'billingo_settings'), 20, 1);
43 add_action('add_meta_boxes', array($this, 'wc_billingo_add_metabox'));
44
45 add_action('wp_ajax_wc_billingo_generate_invoice', array($this, 'generate_invoice_with_ajax'));
46 add_action('wp_ajax_nopriv_wc_billingo_generate_invoice', array($this, 'generate_invoice_with_ajax'));
47
48 add_action('wp_ajax_wc_billingo_already', array($this, 'wc_billingo_already'));
49 add_action('wp_ajax_nopriv_wc_billingo_already', array($this, 'wc_billingo_already'));
50
51 add_action('wp_ajax_wc_billingo_already_back', array($this, 'wc_billingo_already_back'));
52 add_action('wp_ajax_nopriv_wc_billingo_already_back', array($this, 'wc_billingo_already_back'));
53
54 add_action('woocommerce_order_status_completed', array($this, 'on_order_complete'));
55 add_action('woocommerce_order_status_changed', array($this, 'on_order_processing'));
56
57 add_action('woocommerce_admin_order_actions_end', array($this, 'add_listing_actions'));
58
59 if (get_option('wc_billingo_vat_number_form')) {
60 add_filter('woocommerce_checkout_fields', array($this, 'add_vat_number_checkout_field'));
61 add_filter('woocommerce_before_checkout_form', array($this, 'add_vat_number_info_notice'));
62 add_action('woocommerce_checkout_update_order_meta', array($this, 'save_vat_number'));
63 add_action('woocommerce_admin_order_data_after_billing_address', array($this, 'display_vat_number'));
64 }
65 }
66
67
68 //Add CSS & JS
69 public function wc_billingo_admin_init()
70 {
71 wp_enqueue_script('billingo_js', plugins_url('/global.js', __FILE__), array('jquery'), TRUE);
72 wp_enqueue_style('billingo_css', plugins_url('/global.css', __FILE__));
73
74 $wc_billingo_local = array('loading' => plugins_url('/images/ajax-loader.gif', __FILE__));
75 wp_localize_script('billingo_js', 'wc_billingo_params', $wc_billingo_local);
76 }
77
78
79 //Settings
80 public function billingo_settings($settings)
81 {
82 $settings[] = array(
83 'type' => 'title',
84 'title' => __('Billingo Beállítások', 'wc-billingo'),
85 'id' => 'woocommerce_billingo_options',
86 'desc' => __('A nyilvános és privát kulcs megadása és elmentése után megjelenik a fizetési módok szekció. Ha nem látszik, rossz valamelyik kulcs.', 'wc-billingo'),
87 );
88
89 $settings[] = array(
90 'title' => __('Nyilvános kulcs', 'wc-billingo'),
91 'id' => 'wc_billingo_public_key',
92 'type' => 'text',
93 'desc' => __('Vezérlőpulton az API menüben találod meg', 'wc-billingo'),
94 );
95
96 $settings[] = array(
97 'title' => __('Privát kulcs', 'wc-billingo'),
98 'id' => 'wc_billingo_secret_key',
99 'type' => 'password',
100 'desc' => __('Vezérlőpulton az API menüben találod meg', 'wc-billingo'),
101 );
102
103 $settings[] = array(
104 'title' => __('Fizetési határidő(nap)', 'wc-billingo'),
105 'id' => 'wc_billingo_payment_deadline',
106 'type' => 'number',
107 'desc' => __('Globális érték, lejjebb görgetve részletezheted fizetési módonként', 'wc-billingo'),
108 );
109
110 $settings[] = array(
111 'title' => __('Megjegyzés', 'wc-billingo'),
112 'id' => 'wc_billingo_note',
113 'type' => 'text'
114 );
115
116 $settings[] = array(
117 'title' => __('Elektronikus számla', 'wc-billingo'),
118 'id' => 'wc_billingo_electronic',
119 'type' => 'checkbox'
120 );
121
122 $settings[] = array(
123 'title' => __('Számla küldése emailben', 'wc-billingo'),
124 'id' => 'wc_billingo_email',
125 'type' => 'checkbox',
126 'desc' => __('Ha be van kapcsolva, akkor a billingo emailben elküldi a vásárlónak a generált számlát.', 'wc-billingo'),
127 );
128
129 $settings[] = array(
130 'title' => __('Automata számlakészítés', 'wc-billingo'),
131 'id' => 'wc_billingo_auto',
132 'type' => 'checkbox',
133 'desc' => __('Ha be van kapcsolva, akkor a rendelés lezárásakor automatán kiállításra kerül a számla.', 'wc-billingo'),
134 );
135
136 $settings[] = array(
137 'title' => __('Díjbekérő létrehozása', 'wc-billingo'),
138 'id' => 'wc_billingo_payment_request_auto',
139 'type' => 'checkbox',
140 'desc' => __('Ha be van kapcsolva, akkor ha a rendelés függőben lévő státuszra vált, automatán kiállításra kerül egy díjbekérő.', 'wc-billingo'),
141 );
142
143 $settings[] = array(
144 'title' => __('Fejlesztői mód', 'wc-billingo'),
145 'id' => 'wc_billingo_debug',
146 'type' => 'checkbox'
147 );
148
149 $settings[] = array(
150 'title' => __('Számlatömb ID', 'wc-billingo'),
151 'id' => 'wc_billingo_invoice_block',
152 'type' => 'text',
153 'desc' => __('Vezérlőpulton a számlatömböknél találod meg', 'wc-billingo')
154 );
155
156
157 $settings[] = array(
158 'title' => __('Számla nyelve', 'wc-billingo'),
159 'id' => 'wc_billingo_invoice_lang',
160 'type' => 'select',
161 'options' => array(
162 'hu' => __('Magyar', 'wc-billingo'),
163 'en' => __('Angol', 'wc-billingo'),
164 'de' => __('Német', 'wc-billingo'),
165 ),
166 );
167
168 $settings[] = array(
169 'title' => __('Kerekítés', 'wc-billingo'),
170 'id' => 'wc_billingo_invoice_round',
171 'type' => 'select',
172 'options' => array(
173 0 => __('Nincs', 'wc-billingo'),
174 1 => __('1-re', 'wc-billingo'),
175 5 => __('5-re', 'wc-billingo'),
176 10 => __('10-re', 'wc-billingo'),
177 ),
178 );
179
180 $payment_methods = $this->get_available_payment_methods();
181 $billingo_payment_methods = $this->get_billingo_payment_methods();
182
183 if ($billingo_payment_methods) {
184 $settings[] = array('type' => 'sectionend', 'id' => 'woocommerce_billingo_options');
185
186 $settings[] = array(
187 'type' => 'title',
188 'title' => __('Fizetési módok összehangolása', 'wc-billingo'),
189 'id' => 'woocommerce_billingo_options',
190 'desc' => __('A Billingo-n előre meghatározott fizetési módok közül lehet választani. Párosítsd össze a webshopodban lévő fizetési módokat a billingo-val megegyezőre', 'wc-billingo'),
191 );
192
193 foreach ($payment_methods as $payment_method_id => $payment_method) {
194 $settings[] = array(
195 'title' => $payment_method,
196 'id' => 'wc_billingo_payment_method_' . $payment_method_id,
197 'type' => 'select',
198 'options' => $billingo_payment_methods
199 );
200 }
201 }
202
203
204 if ($billingo_payment_methods) {
205 $settings[] = array('type' => 'sectionend', 'id' => 'woocommerce_billingo_options');
206
207 $settings[] = array(
208 'type' => 'title',
209 'title' => __('Fizetési módokhoz tartozó fizetési határidő', 'wc-billingo'),
210 'id' => 'woocommerce_billingo_options',
211 'desc' => __('Állítsd be, melyik fizetési módhoz hány nap fizetési határidő tartozik', 'wc-billingo'),
212 );
213
214 foreach ($payment_methods as $payment_method_id => $payment_method) {
215 $settings[] = array(
216 'title' => $payment_method,
217 'id' => 'wc_billingo_paymentdue_' . $payment_method_id,
218 'type' => 'number',
219 );
220 }
221 }
222
223 $settings[] = array(
224 'title' => __('Adószám mező vásárláskor', 'wc-billingo'),
225 'id' => 'wc_billingo_vat_number_form',
226 'type' => 'checkbox',
227 'desc' => __('Vásárláskor 100e ft áfa feletti rendeléskor a számlázási adatok megadásakor, megjelenik egy üzenet a fizetés oldalon, hogy adószámot meg kell adni, ha van, ami a számlázási adatok alján egy új mezőben lesz bekérve. Eltárolja a rendelés adataiban, illetve számlára is ráírja. Ha kézzel kell megadni utólag a rendeléskezelőben, akkor az egyedi mezőknél az "adoszam" mezőt kell kitölteni.', 'wc-szamlazz'),
228 );
229
230 $settings[] = array(
231 'title' => __('Adószám figyelmeztetés', 'wc-billingo'),
232 'id' => 'wc_billingo_vat_number_notice',
233 'type' => 'text',
234 'default' => __('A vásárlás áfatartalma több, mint 100.000 Ft, ezért amennyiben rendelkezik adószámmal, azt kötelező megadni a számlázási adatoknál.', 'wc-szamlazz'),
235 'desc' => __('Ez az üzenet jelenik meg, ha az adószám mező be van pipálva felül a fizetés oldalon.', 'wc-szamlazz'),
236 );
237
238
239 $settings[] = array('type' => 'sectionend', 'id' => 'woocommerce_billingo_options');
240
241 return $settings;
242 }
243
244
245 //Meta box on order page
246 public function wc_billingo_add_metabox($post_type)
247 {
248 add_meta_box('custom_order_option', 'Billingo számla', array($this, 'render_meta_box_content'), 'shop_order', 'side');
249 }
250
251
252 //Render metabox content
253 public function render_meta_box_content($post)
254 {
255 ?>
256 <?php if (!get_option('wc_billingo_public_key') || !get_option('wc_billingo_secret_key')): ?>
257 <p style="text-align: center;"><?php _e('A számlakészítéshez meg kell adnod a Billingo API kulcsokat a Woocommerce beállításokban!', 'wc-billingo'); ?></p>
258 <?php else: ?>
259 <div id="wc-billingo-messages"></div>
260 <?php if (get_post_meta($post->ID, '_wc_billingo_own', true)): ?>
261 <div style="text-align:center;" id="billingo_already_div">
262 <?php $note = get_post_meta($post->ID, '_wc_billingo_own', true); ?>
263 <p><?php _e('A számlakészítés ki lett kapcsolva, mert: ', 'wc-billingo'); ?>
264 <strong><?php echo $note; ?></strong><br>
265 <a id="wc_billingo_already_back" href="#"
266 data-nonce="<?php echo wp_create_nonce('wc_already_invoice'); ?>"
267 data-order="<?php echo $post->ID; ?>"><?php _e('Visszakapcsolás', 'wc-billingo'); ?></a>
268 </p>
269 </div>
270 <?php endif; ?>
271 <?php if (get_post_meta($post->ID, '_wc_billingo_dijbekero_pdf', true)): ?>
272 <p>Díjbekérő <span
273 class="alignright"><?php echo get_post_meta($post->ID, '_wc_billingo_dijbekero', true); ?> - <a
274 href="<?php echo $this->generate_download_link($post->ID, true); ?>">Letöltés</a></span></p>
275 <hr/>
276 <?php endif; ?>
277
278 <?php if ($this->is_invoice_generated($post->ID) && !get_post_meta($post->ID, '_wc_billingo_own', true)): ?>
279 <div style="text-align:center;">
280 <p><?php echo __('Számla sikeresen létrehozva.', 'wc-billingo'); ?></p>
281 <p><?php _e('A számla sorszáma:', 'wc-billingo'); ?>
282 <strong><?php echo get_post_meta($post->ID, '_wc_billingo', true); ?></strong></p>
283 <p><a href="<?php echo $this->generate_download_link($post->ID); ?>" id="wc_billingo_download"
284 data-nonce="<?php echo wp_create_nonce('wc_generate_invoice'); ?>"
285 class="button button-primary"><?php _e('Számla megtekintése', 'wc-billingo'); ?></a></p>
286 </div>
287 <?php else: ?>
288 <div style="text-align:center;<?php if (get_post_meta($post->ID, '_wc_billingo_own', true)): ?>display:none;<?php endif; ?>" id="wc-billingo-generate-button">
289 <p><a href="#" id="wc_billingo_generate" data-order="<?php echo $post->ID; ?>"
290 data-nonce="<?php echo wp_create_nonce('wc_generate_invoice'); ?>"
291 class="button button-primary"><?php _e('Számlakészítés', 'wc-billingo'); ?></a><br><a href="#"
292 id="wc_billingo_options"><?php _e('Opciók', 'wc-billingo'); ?></a>
293 </p>
294 <div id="wc_billingo_options_form" style="display:none;">
295 <div class="fields">
296 <h4><?php _e('Megjegyzés', 'wc-billingo'); ?></h4>
297 <input type="text" id="wc_billingo_invoice_note"
298 value="<?php echo get_option('wc_billingo_note'); ?>"/>
299 <h5><?php _e('Csak akkor kerül elküldésre, ha a Fizetési határidő mezőt is megadja.', 'wc-billingo'); ?></h5>
300 <h4><?php _e('Fizetési határidő(nap)', 'wc-billingo'); ?></h4>
301 <input type="number" step="1" id="wc_billingo_invoice_deadline" />
302 <h4><?php _e('Teljesítés dátum', 'wc-billingo'); ?></h4>
303 <input type="text" class="date-picker" id="wc_billingo_invoice_completed" maxlength="10"
304 value="<?php echo date('Y-m-d'); ?>"
305 pattern="[0-9]{4}-(0[1-9]|1[012])-(0[1-9]|1[0-9]|2[0-9]|3[01])">
306 <h4><?php _e('Díjbekérő számla', 'wc-billingo'); ?></h4>
307 <input type="checkbox" id="wc_billingo_invoice_request" value="1"/>
308 </div>
309 <a id="wc_billingo_already" href="#"
310 data-nonce="<?php echo wp_create_nonce('wc_already_invoice'); ?>"
311 data-order="<?php echo $post->ID; ?>"><?php _e('Számlakészítés kikapcsolása', 'wc-billingo'); ?></a>
312 </div>
313 <?php if (get_option('wc_billingo_auto') == 'yes'): ?>
314 <p>
315 <small><?php _e('A számla automatikusan elkészül és el lesz küldve a vásárlónak, ha a rendelés állapota befejezettre lesz átállítva.', 'wc-billingo'); ?></small>
316 </p>
317 <?php endif; ?>
318 </div>
319 <?php endif; ?>
320 <?php endif; ?>
321 <?php
322 }
323
324
325 //Generate Invoice with Ajax
326 public function generate_invoice_with_ajax()
327 {
328 check_ajax_referer('wc_generate_invoice', 'nonce');
329
330 $orderid = $_POST['order'];
331 $return_info = $this->generate_invoice($orderid);
332 wp_send_json_success($return_info);
333 }
334
335
336 public function generate_invoice($orderId, $payment_request = false)
337 {
338 global $wpdb, $woocommerce;
339 $order = new WC_Order($orderId);
340 $order_items = $order->get_items();
341
342 //Load billingo API
343 $this->billingo = new Request(array(
344 'public_key' => get_option('wc_billingo_public_key'),
345 'private_key' => get_option('wc_billingo_secret_key')
346 ));
347
348 //Create response
349 $response = array();
350 $response['error'] = false;
351
352 //Setup client first
353 $clientData = [
354 'name' => ($this->get_order_property('billing_company', $order) ? $this->get_order_property('billing_company', $order) . ' - ' : '') . $this->get_order_property('billing_first_name', $order) . ' ' . $this->get_order_property('billing_last_name', $order),
355 'email' => $this->get_order_property('billing_email', $order),
356 'billing_address' => [
357 'street_name' => $this->get_order_property('billing_address_1', $order),
358 'street_type' => '',
359 'house_nr' => '',
360 'city' => $this->get_order_property('billing_city', $order),
361 'postcode' => $this->get_order_property('billing_postcode', $order),
362 'country' => WC()->countries->countries[$this->get_order_property('billing_country', $order)] ? : '',
363 ]
364 ];
365
366 //If VAT number
367 if ($adoszam = get_post_meta($orderId, 'adoszam', true)) {
368 $clientData['taxcode'] = $adoszam;
369 }
370
371 //Create client
372 try {
373 $client = $this->billingo->post('clients', apply_filters('wc_billingo_clientdata', $clientData, $order));
374 } catch (Exception $e) {
375 $response['error'] = true;
376 $response['messages'][] = $e->getMessage();
377 $order->add_order_note(__('Billingo számlakészítés (ügyfél létrehozása) sikertelen! Hibakód: ', 'wc-billingo') . $e->getMessage());
378 return $response;
379 }
380
381 //Get client ID
382 if ($client['id']) {
383 $clientID = $client['id'];
384 } else {
385 $response['error'] = true;
386 $response['messages'][] = __('Nem sikerült létrehozni az ügyfelet.', 'billingo');
387 return $response;
388 }
389
390 //Get billingo payment method id & block id
391 $paymentMethod = get_option('wc_billingo_payment_method_' . $this->get_order_property('payment_method', $order));
392
393
394 //If custom details
395 if (isset($_POST['note']) && isset($_POST['deadline']) && $_POST['deadline'] != '' && isset($_POST['completed'])) {
396 $note = $_POST['note'];
397 $deadline = $_POST['deadline'];
398 $complated_date = $_POST['completed'];
399 } elseif (get_option('wc_billingo_paymentdue_' . $this->get_order_property('payment_method', $order)) != '') {
400 $note = get_option('wc_billingo_note');
401 $deadline = (int)get_option('wc_billingo_paymentdue_' . $this->get_order_property('payment_method', $order));
402 $complated_date = date('Y-m-d');
403 } else {
404 $note = get_option('wc_billingo_note');
405 $deadline = (int)get_option('wc_billingo_payment_deadline');
406 $complated_date = date('Y-m-d');
407 }
408
409 /*
410 $fl = fopen(get_home_path() . '_billingo-payment.txt', 'a');
411 fwrite($fl, PHP_EOL . date('Y-m-d H:i:s') . PHP_EOL);
412 fwrite($fl, 'orderprop: ' . print_r($this->get_order_property('payment_method', $order), true) . PHP_EOL);
413 fwrite($fl, 'option: ' . print_r(get_option('wc_billingo_paymentdue_' . $this->get_order_property('payment_method', $order)), true) . PHP_EOL);
414 fclose($fl);
415 */
416
417 if (!$note) {
418 $note = '';
419 }
420
421
422 $langs = array(
423 'hu' => __('Magyar', 'wc-billingo'),
424 'en' => __('Angol', 'wc-billingo'),
425 'de' => __('Német', 'wc-billingo'),
426 );
427 $lang = get_option('wc_billingo_invoice_lang');
428 if (isset($langs[$lang])) {
429 $lang = $lang;
430 } else {
431 $lang = 'hu';
432 }
433
434 //Create invoce data array
435 $invoiceData = [
436 'fulfillment_date' => $complated_date,
437 'due_date' => date('Y-m-d'),
438 'payment_method' => (int)$paymentMethod,
439 'comment' => $note,
440 'template_lang_code' => $lang, //VV
441 'electronic_invoice' => 0,
442 'currency' => $order->get_order_currency() ?: 'HUF', //VV
443 'client_uid' => $clientID,
444 'block_uid' => (int)get_option('wc_billingo_invoice_block'),
445 'round_to' => (int)get_option('wc_billingo_invoice_round'),
446 'type' => 3
447 ];
448
449 //If deadline is set
450 if ($deadline) {
451 $invoiceData['due_date'] = date('Y-m-d', strtotime('+' . $deadline . ' days'));
452 }
453
454 //If electronic_invoice
455 if (get_option('wc_billingo_electronic') == 'yes') {
456 $invoiceData['electronic_invoice'] = 1;
457 }
458
459 //If its a proforma invoice
460 if ($payment_request) {
461 $invoiceData['type'] = 1;
462 } else {
463 if (isset($_POST['request']) && $_POST['request'] == 'on') {
464 $invoiceData['type'] = 1;
465 $payment_request = true;
466 } else {
467 $invoiceData['type'] = 3;
468 }
469 }
470
471
472 //Add products
473 foreach ($order_items as $termek) {
474 $product_item = array(
475 'unit' => 'db',
476 );
477
478
479 if (method_exists($order, 'get_id')) {
480
481 //WooCommerce >= 3.0
482 $product_item['description'] = $termek->get_name();
483 $product_item['qty'] = (int)$termek->get_quantity();
484 if (round($termek->get_subtotal(), wc_get_price_decimals()) == 0) {
485 $product_item['net_unit_price'] = 0;
486 $product_item['vat_id'] = 1;
487 } else {
488 $product_net_price = round($order->get_item_total($termek, true, true), 2);
489 $vat_percentage = $termek->get_subtotal_tax() / $termek->get_subtotal();
490 //$single_product_total_price = round($product_net_price * (1 + $vat_percentage), 2);
491 //$single_product_net_total = round($single_product_total_price / (1 + $vat_percentage), 2);
492 //$product_item['net_unit_price'] = $product_net_price; //$single_product_net_total;
493 $product_item['gross_unit_price'] = $product_net_price; //$single_product_total_price;
494 $product_item['vat_id'] = $this->get_billingo_vat_id(round(($termek->get_subtotal_tax() / $termek->get_subtotal()) * 100) + '%');
495 }
496
497 } else {
498
499 //Woocmmerce < 3.0
500 $product_item['description'] = $termek['name'];
501 $product_item['qty'] = (int)$termek['qty'];
502 if (round($termek['line_total'], wc_get_price_decimals()) == 0) {
503 $product_item['net_unit_price'] = 0;
504 $product_item['vat_id'] = 1;
505 } else {
506 $product_net_price = round($order->get_item_total($termek, true, true), 2);
507 $vat_percentage = $termek['line_tax'] / $termek['line_total'];
508 //$single_product_total_price = round($product_net_price * (1 + $vat_percentage), 2);
509 //$single_product_net_total = round($single_product_total_price / (1 + $vat_percentage), 2);
510 //$product_item['net_unit_price'] = $product_net_price; //$single_product_net_total;
511 $product_item['gross_unit_price'] = $product_net_price; //$single_product_total_price;
512 $product_item['vat_id'] = $this->get_billingo_vat_id(round(($termek['line_tax'] / $termek['line_total']) * 100) + '%');
513 }
514
515 }
516
517 $invoiceData['items'][] = $product_item;
518
519 }
520
521 //Shipping
522 if ($order->get_shipping_methods()) {
523 $product_item = array(
524 'description' => $order->get_shipping_method(),
525 'qty' => 1,
526 'unit' => 'db',
527 );
528 $order_shipping = method_exists($order, 'get_shipping_total') ? $order->get_shipping_total() : $order->order_shipping;
529 $order_shipping_tax = method_exists($order, 'get_shipping_tax') ? $order->get_shipping_tax() : $order->order_shipping_tax;
530
531 if ($order_shipping > 0) {
532 $shipping_total = round($order_shipping_tax + $order_shipping, 2);
533 $vat_percentage = round($order_shipping_tax / $order_shipping, 2);
534 $shipping_net_total = round($shipping_total / (1 + $vat_percentage), 2);
535 } else {
536 $shipping_net_total = 0;
537 }
538
539 $product_item['net_unit_price'] = $shipping_net_total;
540 //$product_item['gross_unit_price'] = $shipping_total;
541 if ($order_shipping == 0) {
542 $product_item['vat_id'] = 1;
543 } else {
544 $product_item['vat_id'] = $this->get_billingo_vat_id(round(($order_shipping_tax / $order_shipping) * 100) + '%');
545 }
546
547 $invoiceData['items'][] = $product_item;
548 }
549
550 // Extra Fees
551 $fees = $order->get_fees();
552 if (!empty($fees)) {
553 foreach ($fees as $fee) {
554 $product_item = array(
555 'description' => $fee['name'],
556 'qty' => 1,
557 'unit' => '',
558 'net_unit_price' => round($fee['line_total'], wc_get_price_decimals()),
559 'vat_id' => $this->get_billingo_vat_id(round(($fee['line_tax'] / $fee['line_total']) * 100) + '%'),
560 );
561
562 $invoiceData['items'][] = $product_item;
563 }
564 }
565
566
567 // Coupons
568 // TODO: find another way.
569 /*$coupons = $order->get_used_coupons();
570 if (!empty($coupons)) {
571 foreach ($coupons as $coupon_name) {
572 $coupons_obj = new WC_Coupon($coupon_name);
573
574 if ($coupons_obj->get_type == 'fixed_cart') {
575 $val = $coupons_obj->get_amount();
576
577 $product_item = array(
578 'description' => 'Kupon/Coupon: ' . $coupon_name,
579 'qty' => 1,
580 'unit' => '',
581 'net_unit_price' => round($val, wc_get_price_decimals()),
582 'vat_id' => 1, //$this->get_billingo_vat_id(round(($fee['line_tax'] / $fee['line_total']) * 100) + '%'),
583 );
584
585 $invoiceData['items'][] = $product_item;
586 }
587 }
588 }*/
589
590
591 //Create invoice
592 try {
593 $invoice = $this->billingo->post('invoices', apply_filters('wc_billingo_invoicedata', $invoiceData, $order));
594 } catch (Exception $e) {
595 $response['error'] = true;
596 $response['messages'][] = $e->getMessage();
597 $order->add_order_note(__('Billingo számlakészítés sikertelen! Hibakód: ', 'wc-billingo') . $e->getMessage());
598 return $response;
599 }
600
601 //Get client ID
602 if (!$invoice['id']) {
603 $response['error'] = true;
604 $response['messages'][] = __('Nem sikerült létrehozni a számlát.', 'billingo');
605 return $response;
606 }
607
608 //Create download link
609 try {
610 $file_name = $this->billingo->get("invoices/{$invoice['id']}/code");
611 $file_name = $file_name['code'];
612 } catch (Exception $e) {
613 $response['messages'][] = __('Nem sikerült létrehozni a letöltési linket a számlához.', 'billingo');
614 }
615
616 //Send via email if needed
617 try {
618 if (get_option('wc_billingo_email') == 'yes') {
619 $this->billingo->get("invoices/{$invoice['id']}/send");
620 }
621 } catch (Exception $e) {
622 $response['messages'][] = __('Nem sikerült elküldeni emailben a számlát', 'billingo');
623 return false;
624 }
625
626 //Create response
627 $szamlaszam = $invoice['attributes']['invoice_no'];
628 if (!$szamlaszam) {
629 $szamlaszam = $invoice['id'];
630 }
631 $response['invoice_name'] = $szamlaszam;
632
633 //Save data
634 if ($payment_request) {
635 $response['messages'][] = __('Díjbekérő sikeresen létrehozva.', 'wc-billingo');
636
637 //Store as a custom field
638 update_post_meta($orderId, '_wc_billingo_dijbekero', $szamlaszam);
639
640 //Update order notes
641 $order->add_order_note(__('Billingo díjbekérő sikeresen létrehozva. A számla sorszáma: ', 'wc-billingo') . $szamlaszam);
642
643 //Store the filename
644 update_post_meta($orderId, '_wc_billingo_dijbekero_pdf', $file_name);
645 update_post_meta($orderId, '_wc_billingo_dijbekero_id', $invoice['id']);
646 } else {
647 $response['messages'][] = __('Számla sikeresen létrehozva.', 'wc-billingo');
648
649 //Store as a custom field
650 update_post_meta($orderId, '_wc_billingo', $szamlaszam);
651
652 //Update order notes
653 $order->add_order_note(__('Billingo számla sikeresen létrehozva. A számla sorszáma: ', 'wc-billingo') . $szamlaszam);
654
655 //Store the filename
656 update_post_meta($orderId, '_wc_billingo_pdf', $file_name);
657 update_post_meta($orderId, '_wc_billingo_id', $invoice['id']);
658 }
659
660 //Return the download url
661 $response['link'] = '<p><a href="' . $this->generate_download_link($orderId) . '" id="wc_billingo_download" class="button button-primary">' . __('Számla megtekintése', 'wc-billingo') . '</a></p>';
662 return $response;
663 }
664
665
666 //Autogenerate invoice
667 public function on_order_complete($order_id)
668 {
669 //Only generate invoice, if it wasn't already generated & only if automatic invoice is enabled
670 if (get_option('wc_billingo_auto') == 'yes') {
671 if (!$this->is_invoice_generated($order_id)) {
672 $return_info = $this->generate_invoice($order_id);
673 }
674 }
675
676 }
677
678
679 //Autogenerate invoice
680 public function on_order_processing($order_id)
681 {
682 //Only generate invoice, if it wasn't already generated & only if automatic invoice is enabled
683 if (get_option('wc_billingo_payment_request_auto') == 'yes') {
684 if (!$this->is_invoice_generated($order_id)) {
685
686 $order = new WC_Order($order_id);
687
688 if ($this->get_order_property('payment_method', $order) != 'cod') {
689 $return_info = $this->generate_invoice($order_id, true);
690 }
691 }
692 }
693 }
694
695
696 //Check if it was already generated or not
697 public function is_invoice_generated($order_id)
698 {
699 $invoice_name = get_post_meta($order_id, '_wc_billingo', true);
700 $invoice_own = get_post_meta($order_id, '_wc_billingo_own', true);
701 if ($invoice_name || $invoice_own) {
702 return true;
703 }
704
705 return false;
706 }
707
708
709 //Add icon to order list to show invoice
710 public function add_listing_actions($order)
711 {
712 $order_id = $this->get_order_id($order);
713
714 if ($this->is_invoice_generated($order_id)):
715 ?>
716 <a href="<?php echo $this->generate_download_link($order_id); ?>" class="button tips wc_szamlazz"
717 target="_blank" alt="" data-tip="<?php _e('Billingo számla', 'wc-billingo'); ?>">
718 <img src="<?php echo WC_Billingo::$plugin_url . 'images/invoice.png'; ?>" alt="" width="16" height="16">
719 </a>
720 <?php
721 endif;
722
723 if (get_post_meta($order_id, '_wc_billingo_dijbekero_pdf', true)):
724 ?>
725 <a href="<?php echo $this->generate_download_link($order_id, true); ?>" class="button tips wc_szamlazz"
726 target="_blank" alt="" data-tip="<?php _e('Billingo díjbekérő', 'wc-billingo'); ?>">
727 <img src="<?php echo WC_Billingo::$plugin_url . 'images/payment_request.png'; ?>" alt="" width="16"
728 height="16">
729 </a>
730 <?php
731 endif;
732 }
733
734
735 //Generate download url
736 public function generate_download_link($order_id, $payment_request = false)
737 {
738 if ($order_id) {
739 if ($payment_request) {
740 $pdf_name = get_post_meta($order_id, '_wc_billingo_dijbekero_pdf', true);
741 } else {
742 $pdf_name = get_post_meta($order_id, '_wc_billingo_pdf', true);
743 }
744 return 'https://www.billingo.hu/access/c:' . $pdf_name;
745 } else {
746 return false;
747 }
748 }
749
750
751 //Get available checkout methods and payment gateways
752 public function get_available_payment_methods()
753 {
754 $available_gateways = WC()->payment_gateways->payment_gateways();
755 $available = array();
756 foreach ($available_gateways as $available_gateway) {
757 if ($available_gateway->enabled == 'yes') {
758 $available[$available_gateway->id] = $available_gateway->title;
759 }
760 }
761 return $available;
762 }
763
764
765 //Get billingo payment methods and cache it
766 public function get_billingo_payment_methods()
767 {
768 $payment_methods = get_transient('wc_billingo_payment_methods');
769 if (!$payment_methods) {
770
771 //Load billingo API
772 $this->billingo = new Request(array(
773 'public_key' => get_option('wc_billingo_public_key'),
774 'private_key' => get_option('wc_billingo_secret_key')
775 ));
776
777 try {
778 $billingo_payment_methods = $this->billingo->get('payment_methods/hu');
779 } catch (Exception $e) {
780 error_log($e->getMessage(), true);
781 }
782
783 if (empty($billingo_payment_methods)) {
784 return false;
785 }
786
787 //Create a simple array
788 $payment_methods = array();
789 foreach ($billingo_payment_methods as $payment_method) {
790 $payment_methods[$payment_method['id']] = $payment_method['attributes']['name'];
791 }
792
793 //Save payment methods for a day
794 set_transient('wc_billingo_payment_methods', $payment_methods, 60 * 60 * 24);
795 }
796
797 return $payment_methods;
798 }
799
800
801 //Get billingo VAT id's
802 public function get_billingo_vat_id($percentage)
803 {
804 $vat_ids = get_transient('wc_billingo_vat_ids');
805 if (!$vat_ids) {
806
807 //Load billingo API
808 $this->billingo = new Request(array(
809 'public_key' => get_option('wc_billingo_public_key'),
810 'private_key' => get_option('wc_billingo_secret_key')
811 ));
812
813 try {
814 $billingo_vat_ids = $this->billingo->get('vat');
815 } catch (Exception $e) {
816 error_log($e->getMessage(), true);
817 }
818
819 //Create a simple array
820 $vat_ids = array();
821 foreach ($billingo_vat_ids as $billingo_vat_id) {
822 $vat_ids[$billingo_vat_id['id']] = $billingo_vat_id['attributes']['description'];
823 }
824
825 //Save vat ids for a day
826 set_transient('wc_billingo_vat_ids', $vat_ids, 60 * 60 * 24);
827 }
828
829 //Find the closest one
830 $vat_id = 1;
831 foreach ($vat_ids as $billingo_vat_id => $billingo_vat_id_name) {
832 if ($billingo_vat_id_name == $percentage) {
833 $vat_id = $billingo_vat_id;
834 }
835 }
836
837 return (int)$vat_id;
838 }
839
840
841 //If the invoice is already generated without the plugin
842 public function wc_billingo_already()
843 {
844 check_ajax_referer('wc_already_invoice', 'nonce');
845 if (true) {
846 if (!current_user_can('edit_shop_orders')) {
847 wp_die(__('You do not have sufficient permissions to access this page.'));
848 }
849
850 $orderid = $_POST['order'];
851 $note = $_POST['note'];
852 update_post_meta($orderid, '_wc_billingo_own', $note);
853
854 $response = array();
855 $response['error'] = false;
856 $response['messages'][] = __('Saját számla sikeresen hozzáadva.', 'wc-billingo');
857 $response['invoice_name'] = $note;
858
859 wp_send_json_success($response);
860 }
861 }
862
863
864 //If the invoice is already generated without the plugin, turn it off
865 public function wc_billingo_already_back()
866 {
867 check_ajax_referer('wc_already_invoice', 'nonce');
868 if (true) {
869 if (!current_user_can('edit_shop_orders')) {
870 wp_die(__('You do not have sufficient permissions to access this page.'));
871 }
872
873 $orderid = $_POST['order'];
874 $note = $_POST['note'];
875 update_post_meta($orderid, '_wc_billingo_own', '');
876
877 $response = array();
878 $response['error'] = false;
879 $response['messages'][] = __('Visszakapcsolás sikeres.', 'wc-billingo');
880
881 wp_send_json_success($response);
882 }
883 }
884
885
886 //Add vat number field to checkout page
887 public function add_vat_number_checkout_field($fields)
888 {
889
890 if (WC()->cart->get_taxes_total() > 100000) {
891 $fields['billing']['adoszam'] = array(
892 'label' => __('Adószám', 'wc-billingo'),
893 'placeholder' => _x('12345678-1-23', 'placeholder', 'wc-billingo'),
894 'required' => false,
895 'class' => array('form-row-wide'),
896 'clear' => true
897 );
898 }
899
900 return $fields;
901 }
902
903
904 public function add_vat_number_info_notice($checkout)
905 {
906 if (WC()->cart->get_taxes_total() > 100000) {
907 wc_print_notice(get_option('wc_billingo_vat_number_notice'), 'notice');
908 }
909 }
910
911
912 public function save_vat_number($order_id)
913 {
914 if (!empty($_POST['adoszam'])) {
915 update_post_meta($order_id, 'adoszam', sanitize_text_field($_POST['adoszam']));
916 }
917 }
918
919
920 public function display_vat_number($order)
921 {
922 $order_id = $this->get_order_id($order);
923 if ($adoszam = get_post_meta($order_id, 'adoszam', true)) {
924 echo '<p><strong>' . __('Adószám') . ':</strong> ' . $adoszam . '</p>';
925 }
926 }
927
928
929 //Get order ID(backward compatibility), for WC3.0+
930 public function get_order_id($order)
931 {
932 $id = method_exists($order, 'get_id') ? $order->get_id() : $order->id;
933 return $id;
934 }
935
936
937 //Get order details(backward compatiblity), for WC3.0+
938 public function get_order_property($property, $order)
939 {
940 //3.0+
941 $value = '';
942 if (method_exists($order, 'get_id')) {
943 $property = 'get_' . $property;
944 $value = $order->$property();
945 } else {
946 $value = $order->$property;
947 }
948
949 return $value;
950
951 }
952 }
953
954 $GLOBALS['wc_billingo'] = new WC_Billingo();
955