Plugin Directory

source: deposits-partial-payments-for-woocommerce/trunk/includes/class-awcdp-backend.php

Last change on this file was 3294582, checked in by acowebscd, 6 months ago

Update to version 1.2.1

File size: 32.6 KB
Line 
1<?php
2if (!defined('ABSPATH'))
3    exit;
4
5use Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController;   
6
7class AWCDP_Backend
8{
9    /**
10     * @var    object
11     * @access  private
12     * @since    1.0.0
13    */
14    private static $_instance = null;
15
16    /**
17     * The version number.
18     * @var     string
19     * @access  public
20     * @since   1.0.0
21    */
22    public $_version;
23
24    /**
25     * The token.
26     * @var     string
27     * @access  public
28     * @since   1.0.0
29    */
30    public $_token;
31
32    /**
33     * The main plugin file.
34     * @var     string
35     * @access  public
36     * @since   1.0.0
37    */
38    public $file;
39
40    /**
41     * The main plugin directory.
42     * @var     string
43     * @access  public
44     * @since   1.0.0
45    */
46    public $dir;
47
48    /**
49     * The plugin assets directory.
50     * @var     string
51     * @access  public
52     * @since   1.0.0
53    */
54    public $assets_dir;
55
56    /**
57     * Suffix for Javascripts.
58     * @var     string
59     * @access  public
60     * @since   1.0.0
61    */
62    public $script_suffix;
63
64    /**
65     * The plugin assets URL.
66     * @var     string
67     * @access  public
68     * @since   1.0.0
69    */
70    public $assets_url;
71    public $hook_suffix = array();
72
73    /**
74     * Constructor function.
75     * @access  public
76     * @return  void
77     * @since   1.0.0
78    */
79    public function __construct( $file = '', $version = '1.0.0' )
80    {
81        $this->_version = $version;
82        $this->_token = AWCDP_TOKEN;
83        $this->file = $file;
84        $this->dir = dirname( $this->file );
85        $this->assets_dir = trailingslashit( $this->dir ) . 'assets';
86        $this->assets_url = esc_url( trailingslashit( plugins_url( '/assets/', $this->file ) ) );
87        $this->script_suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min';
88
89                if ($this->check_woocommerce_active()) {
90
91        //reg activation hook
92        register_activation_hook( $this->file, array( $this, 'install' ) );
93        //reg admin menu
94        add_action( 'admin_menu', array( $this, 'register_root_page' ) );
95        //enqueue scripts & styles
96        add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ), 10, 1 );
97        add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_styles' ), 10, 1 );
98        add_action('admin_head', array( $this, 'awcdp_custom_styles') );
99
100        $plugin = plugin_basename($this->file);
101        //add action links to link to link list display on the plugins page
102        add_filter( "plugin_action_links_$plugin", array( $this, 'add_settings_link' ) );
103
104        //add_action('woocommerce_product_write_panel_tabs', array($this, 'awcdp_tab_panel_tabs'));
105        add_filter( 'woocommerce_product_data_tabs', array($this, 'awcdp_custom_product_tab'), 10, 1 );
106        add_action('woocommerce_product_data_panels', array($this, 'awcdp_tab_data_panels'));
107
108        /*  order */
109        add_filter( 'admin_body_class', array( $this, 'awcdp_admin_body_class') );
110        add_action('admin_footer', array($this, 'awcdp_remove_statuses_deposit'));
111        add_action('woocommerce_admin_order_totals_after_total', array($this, 'awcdp_admin_order_totals_after_total'));
112
113        add_action('add_meta_boxes', array($this, 'awcdp_partial_payments_metabox'), 31, 2);
114        add_action('wp_ajax_awcdp_reload_payments_metabox', array($this, 'ajax_partial_payments_summary'), 10);
115        add_action('woocommerce_ajax_add_order_item_meta', array($this, 'awcdp_add_order_item_meta'), 10, 2);
116        add_action('woocommerce_order_after_calculate_totals', array($this, 'awcdp_recalculate_totals'), 10, 2);
117
118        /// add_action('wp_ajax_wc_deposits_recalculate_deposit', array($this, 'recalculate_deposit_callback'));
119
120        add_action('woocommerce_process_product_meta', array($this, 'awcdp_process_product_meta'));
121
122        // deactivation form
123        add_action('admin_footer', array($this, 'awcdp_deactivation_form'));
124
125                }
126
127    }
128
129    /**
130     *
131     *
132     * Ensures only one instance of AWCDP is loaded or can be loaded.
133     *
134     * @return Main AWCDP instance
135     * @see WordPress_Plugin_Template()
136     * @since 1.0.0
137     * @static
138    */
139    public static function instance($file = '', $version = '1.0.0')
140    {
141        if (is_null(self::$_instance)) {
142            self::$_instance = new self($file, $version);
143        }
144        return self::$_instance;
145    }
146
147        public function check_woocommerce_active() {
148        if (in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins')))) {
149            return true;
150        }
151        if (is_multisite()) {
152            $plugins = get_site_option('active_sitewide_plugins');
153            if (isset($plugins['woocommerce/woocommerce.php']))
154                return true;
155        }
156        return false;
157    }
158
159    /**
160     * Creating admin pages
161     */
162    public function register_root_page()
163    {
164
165        $this->hook_suffix[] = add_menu_page( esc_html__('Deposits For WooCommerce', 'deposits-partial-payments-for-woocommerce'), esc_html__('Deposits', 'deposits-partial-payments-for-woocommerce'), 'manage_woocommerce', AWCDP_TOKEN.'_admin_ui', array($this, 'admin_ui'), esc_url($this->assets_url) . '/images/icon.png', 25);
166        // $this->hook_suffix[] = add_submenu_page( AWCDP_TOKEN.'_admin_ui', __('Settings', 'deposits-partial-payments-for-woocommerce'), __('Settings', 'deposits-partial-payments-for-woocommerce'), 'manage_woocommerce', AWCDP_TOKEN.'_settings_ui', array($this, 'admin_ui_settings'));
167
168
169    }
170
171    /**
172     * Calling view function for admin page components
173    */
174    public function admin_ui()
175    {
176        AWCDP_Backend::view('admin-root', []);
177    }
178
179    /**
180     * Adding new link(Configure) in plugin listing page section
181    */
182    public function add_settings_link($links)
183    {
184        $settings = '<a href="' . admin_url( 'admin.php?page='.AWCDP_TOKEN.'_admin_ui#/' ) . '">' . esc_html__( 'Settings', 'deposits-partial-payments-for-woocommerce' ) . '</a>';
185        array_push( $links, $settings );
186              $upgrade    = '<a href="https://acowebs.com/woocommerce-deposits-partial-payments/" target="_blank" style="font-weight:600;color:#00a32a;">' . __('Upgrade to PRO','deposits-partial-payments-for-woocommerce') . '</a>';
187        array_push( $links, $upgrade );
188        return $links;
189    }
190
191    /**
192     * Including View templates
193    */
194    static function view( $view, $data = array() )
195    {
196        //extract( $data );
197        include( plugin_dir_path(__FILE__) . 'views/' . $view . '.php' );
198    }
199
200
201    function awcdp_admin_body_class( $classes ) {
202      $current_screen = get_current_screen();
203      if( $current_screen->id == 'edit-awcdp_payment' ){
204        return "$classes post-type-shop_order";
205      } else {
206                return $classes;
207          }
208    }
209
210
211    /**
212     * Load admin CSS.
213     * @access  public
214     * @return  void
215     * @since   1.0.0
216     */
217    public function admin_enqueue_styles($hook = '')
218    {
219
220      $currentScreen = get_current_screen();
221      $screenID = $currentScreen->id; //
222      if (strpos($screenID, 'awcdp_') !== false) {
223
224        wp_register_style($this->_token . '-admin', esc_url($this->assets_url) . 'css/backend.css', array(), $this->_version);
225        wp_enqueue_style($this->_token . '-admin');
226
227      }
228
229    }
230
231    /**
232     * Load admin Javascript.
233     * @access  public
234     * @return  void
235     * @since   1.0.0
236    */
237    public function admin_enqueue_scripts($hook = '')
238    {
239        if (!isset($this->hook_suffix) || empty($this->hook_suffix)) {
240            return;
241        }
242
243        $screen = get_current_screen();
244
245        wp_enqueue_script('jquery');
246        // deactivation form js
247        if ( $screen->id == 'plugins' ) {
248            wp_enqueue_script($this->_token . '-deactivation-message', esc_url($this->assets_url).'js/message.js', array());
249        }
250
251        $payment_gateways = WC()->payment_gateways->payment_gateways();
252        $payment_gateway_options = array();
253        foreach ( $payment_gateways as $gateway ) {
254          $payment_gateway_options[ $gateway->id ] = $gateway->get_title();
255        }
256        $payment_gateway_options = array_map(function ($k, $v) {
257            return array( 'id' => $k, 'name'  => $v );
258        }, array_keys($payment_gateway_options), $payment_gateway_options);
259
260        if ( in_array( $screen->id, $this->hook_suffix ) ) {
261            if ( !wp_script_is( 'wp-i18n', 'registered' ) ) {
262                wp_register_script( 'wp-i18n', esc_url( $this->assets_url ) . 'js/i18n.min.js', array('jquery'), $this->_version, true );
263            }
264            wp_enqueue_script( $this->_token . '-backend', esc_url( $this->assets_url ) . 'js/backend.js', array('wp-i18n'), $this->_version, true );
265            wp_localize_script( $this->_token . '-backend', 'awcdp_object', array(
266                    'api_nonce' => wp_create_nonce('wp_rest'),
267                    'root' => rest_url('awcdp/v1/'),
268                    'text_domain' => 'deposits-partial-payments-for-woocommerce',
269                    'assets_url' => $this->assets_url,
270                    'security' => wp_create_nonce('awcdp-deposits-partial-payments-refresh'),
271                    'payment_gateways' => (array)$payment_gateway_options,
272                )
273            );
274
275                 wp_set_script_translations($this->_token . '-backend', 'deposits-partial-payments-for-woocommerce' );
276        }
277    }
278
279    function awcdp_custom_styles(){
280      echo '<style>
281    li.awcdp_deposits_tab a:before {
282      content: "\e01e" !important;
283      font-family: woocommerce !important;
284    }
285   
286    .wc-orders-list-table-awcdp_payment .column-order_number a.order-preview { display: none; }
287  </style>';
288    }
289
290    function awcdp_remove_statuses_deposit(){
291
292      $current_screen = get_current_screen();
293      if( $current_screen->id == 'awcdp_payment' || $current_screen->id == 'woocommerce_page_wc-orders--awcdp_payment' ){
294        ?>
295        <script>
296            jQuery(document).ready(function ($) {
297                jQuery('select#order_status').find('option[value="wc-partially-paid"]').remove();
298                jQuery('select#order_status').find('option[value="wc-processing"]').remove();
299            })
300        </script>
301        <?php
302      }
303
304    }
305
306    function awcdp_custom_product_tab($default_tabs){
307      $default_tabs['awcdp_deposits'] = array(
308          'label'   =>  __( 'Deposits', 'deposits-partial-payments-for-woocommerce' ),
309          'target'  =>  'awcdp_deposits_tab_data',
310          'priority' => 90,
311          'class'   => array('awcdp_deposits_tab')
312      );
313      return $default_tabs;
314    }
315
316    function awcdp_tab_panel_tabs(){
317      ?>
318        <li class="awcdp_deposits_tab"><a href="#awcdp_deposits_tab_data"><span><?php esc_html_e('Deposits', 'deposits-partial-payments-for-woocommerce'); ?></span></a></li>
319      <?php
320    }
321
322    function awcdp_tab_data_panels(){
323               
324               
325                $awcdp_gs = get_option('awcdp_general_settings');
326                if( isset($awcdp_gs['enable_deposits']) && $awcdp_gs['enable_deposits'] == 1){
327                 
328                  global $post;
329        $product = wc_get_product( $post->ID );
330        if($product){
331        ?>
332        <div id="awcdp_deposits_tab_data" class="panel woocommerce_options_panel">
333            <div class="options_group">
334                <p class="form-field">
335                    <?php
336                    woocommerce_wp_select(array(
337                      'id' => '_awcdp_deposit_enabled',
338                      'label' => esc_html__('Enable Deposit ', 'deposits-partial-payments-for-woocommerce'),
339                      'options' => array(
340                        'yes' => esc_html__('Yes', 'deposits-partial-payments-for-woocommerce'),
341                        'no' => esc_html__('No', 'deposits-partial-payments-for-woocommerce'),
342                      ),
343                      'description' => esc_html__('Allow customers to pay a deposit for this product.', 'deposits-partial-payments-for-woocommerce'),
344                      'desc_tip' => true,
345                    ));
346
347                    woocommerce_wp_select(array(
348                      'id' => '_awcdp_deposit_type',
349                      'label' => esc_html__('Deposit type', 'deposits-partial-payments-for-woocommerce'),
350                      'options' => array(
351                        '' => esc_html__('Select', 'deposits-partial-payments-for-woocommerce'),
352                        'fixed' => esc_html__('Fixed', 'deposits-partial-payments-for-woocommerce'),
353                        'percent' => esc_html__('Percentage', 'deposits-partial-payments-for-woocommerce'),
354                      )
355                    ));
356
357                    woocommerce_wp_text_input(array(
358                      'id' => '_awcdp_deposits_deposit_amount',
359                      'label' => esc_html__('Deposit Amount', 'deposits-partial-payments-for-woocommerce'),
360                      'description' => esc_html__('The amount of deposit needed. Do not include currency or percent symbols.', 'deposits-partial-payments-for-woocommerce'),
361                      'desc_tip' => true,
362                      'type' => 'number',
363                      'custom_attributes' => array(
364                          'min' => '0.0',
365                          'step' => '0.01'
366                      )
367                    ));
368                    ?>
369                </p>
370            </div>
371        </div>
372        <?php
373                }
374         
375                } else {
376        ?>
377        <div id="awcdp_deposits_tab_data" class="panel woocommerce_options_panel">
378          <div class="options_group" style="padding: 20px;" >
379            <h3><?php echo esc_html__('Deposit Disabled ', 'deposits-partial-payments-for-woocommerce'); ?></h3>
380            <p style="padding-left: 0px;" >Please enable the deposit option from our <a href="<?php echo admin_url('admin.php?page=awcdp_admin_ui#/'); ?>" target="_blank" >settings</a> page.</p>
381          </div>
382        </div>
383        <?php
384      } 
385         
386    }
387
388
389    function awcdp_process_product_meta($post_id){
390
391        $product = wc_get_product($post_id);
392        $product_type = $product->get_type();
393
394        $enable = isset($_POST['_awcdp_deposit_enabled']) ? sanitize_text_field($_POST['_awcdp_deposit_enabled']) : 'no';
395
396        $type = isset($_POST['_awcdp_deposit_type']) ? sanitize_text_field($_POST['_awcdp_deposit_type']) : '';
397        $amount = isset($_POST['_awcdp_deposits_deposit_amount']) &&
398        is_numeric($_POST['_awcdp_deposits_deposit_amount']) ? floatval(sanitize_text_field($_POST['_awcdp_deposits_deposit_amount'])) : '';
399
400                /*
401        $type = isset($_POST['_awcdp_deposit_type']) ? sanitize_text_field($_POST['_awcdp_deposit_type']) : 'fixed';
402        $amount = isset($_POST['_awcdp_deposits_deposit_amount']) &&
403        is_numeric($_POST['_awcdp_deposits_deposit_amount']) ? floatval(sanitize_text_field($_POST['_awcdp_deposits_deposit_amount'])) : 0.0;
404                */
405
406        $product->update_meta_data(AWCDP_DEPOSITS_META_KEY, $enable);
407        $product->update_meta_data(AWCDP_DEPOSITS_TYPE, $type);
408        $product->update_meta_data(AWCDP_DEPOSITS_AMOUNT, $amount);
409        $product->save();
410    }
411
412
413    function awcdp_admin_order_totals_after_total($order_id){
414        $order = wc_get_order($order_id);
415        if ($order->get_type() == AWCDP_POST_TYPE) {
416          return;
417        }
418        $has_deposit = $order->get_meta('_awcdp_deposits_order_has_deposit', true);
419        if ($has_deposit == 'yes') {
420        $payments = $this->awcdp_get_order_partial_payments($order_id);
421        $deposit = 0; $remaining = 0;
422
423        if ($payments) {
424          foreach ($payments as $payment) {
425            if ($payment->get_meta('_awcdp_deposits_payment_type', true) == 'deposit') {
426              $deposit += $payment->get_total() - $payment->get_total_refunded();
427            } else {
428              $remaining += $payment->get_total() - $payment->get_total_refunded();
429            }
430          }
431        }
432
433        ?>
434        <tr>
435          <td class="label"><?php esc_html_e('Deposit', 'deposits-partial-payments-for-woocommerce'); ?> : </td>
436          <td width="1%"></td>
437          <td class="total paid"><?php echo wp_kses_post( wc_price($deposit, array('currency' => $order->get_currency()))); ?></td>
438        </tr>
439        <tr class="awcdp-remaining">
440          <td class="label"><?php esc_html_e('Future payments', 'deposits-partial-payments-for-woocommerce'); ?>:</td>
441          <td width="1%"></td>
442          <td class="total balance"><?php echo wp_kses_post( wc_price($remaining, array('currency' => $order->get_currency()))); ?></td>
443        </tr>
444
445        <?php
446      }
447    }
448
449    function awcdp_get_order_partial_payments($order_id, $args = array(), $object = true){
450      $orders = array();
451        $default_args = array(
452          'post_type' => AWCDP_POST_TYPE,
453          'post_parent' => $order_id,
454          'post_status' => 'any',
455          'numberposts' => -1,
456        );
457        $args = ($args) ? wp_parse_args($args, $default_args) : $default_args;
458        // $payments = get_posts($args);
459        $payments = wc_get_orders($args);
460        if ( $payments ) {
461          foreach ( $payments as $payment) {
462            // $orders[] = ($object) ? wc_get_order($payment->ID) : $payment->ID;
463            $orders[] = ($object) ? wc_get_order($payment->get_id()) : $payment->ID;
464          }
465        }
466        return $orders;
467    }
468
469    function awcdp_partial_payments_metabox( $post_type, $post_or_order_object ){
470      // global $post;
471      // $order = wc_get_order($post->ID);
472
473      $order = ( $post_or_order_object instanceof \WP_Post ) ? wc_get_order( $post_or_order_object->ID ) : wc_get_order( get_the_id() );
474
475      $screen = wc_get_container()->get( CustomOrdersTableController::class )->custom_orders_table_usage_is_enabled() ? wc_get_page_screen_id( 'shop-order' ) : 'shop_order';
476      $screen1 = wc_get_container()->get( CustomOrdersTableController::class )->custom_orders_table_usage_is_enabled() ? 'woocommerce_page_wc-orders--awcdp_payment' : AWCDP_POST_TYPE ;
477
478      if ($order) {
479        if ($order->get_type() == AWCDP_POST_TYPE) {
480          add_meta_box(
481            'awcdp_deposits_partial_payments',
482            esc_html__('Partial Payments', 'deposits-partial-payments-for-woocommerce'),
483            array($this, 'awcdp_original_order_details'),
484            $screen1,
485            'side',
486            'high'
487          );
488        } else {
489          $has_deposit = $order->get_meta('_awcdp_deposits_order_has_deposit', true) == 'yes';
490          if ($has_deposit || $order->is_editable()) {
491            add_meta_box(
492              'awcdp_deposits_partial_payments',
493              esc_html__('Partial payment details', 'deposits-partial-payments-for-woocommerce'),
494              array($this, 'partial_payments_summary'),
495              $screen,
496              'normal',
497              'high'
498            );
499          }
500        }
501      }
502
503    }
504
505
506    function partial_payments_summary($post_or_order_object){
507
508      // global $post;
509      // $order = wc_get_order($post->ID);
510
511      $order = ( $post_or_order_object instanceof WP_Post ) ? wc_get_order( $post_or_order_object->ID ) : wc_get_order( get_the_id() );
512                  $post_or_order_object;
513
514      $atts = array(
515        'order' => $order,
516      );
517      $wsettings = new AWCDP_Deposits();
518      echo $return_string = $wsettings->awcdp_get_template('admin/order-partial-payments.php', $atts );
519
520    }
521
522    function ajax_partial_payments_summary(){
523
524      check_ajax_referer('awcdp-deposits-partial-payments-refresh', 'security');
525      if (!current_user_can('edit_shop_orders')) {
526        wp_die(-1);
527      }
528      $order_id = absint(sanitize_text_field($_POST['order_id']));
529      $order = wc_get_order($order_id);
530      if($order) {
531        ob_start();
532        $atts = array(
533          'order' => $order,
534        );
535        $wsettings = new AWCDP_Deposits();
536        echo $return_string = $wsettings->awcdp_get_template('admin/order-partial-payments.php', $atts );
537        $html = ob_get_clean();
538        wp_send_json_success(array('html' => $html));
539      }
540      wp_die();
541
542    }
543
544    function awcdp_original_order_details(){
545      // global $post;
546      //$order = wc_get_order($post->ID);
547      $order = wc_get_order( get_the_id() ); 
548      if ($order){
549        $parent = wc_get_order($order->get_parent_id());
550        if ($parent){
551          ?>
552          <p><?php echo wp_kses_post( sprintf(__('This is a partial payment for order %s', 'deposits-partial-payments-for-woocommerce'), $parent->get_order_number()) ); ?></p>
553          <a class="button btn" href="<?php echo esc_url($parent->get_edit_order_url()); ?> "> <?php esc_html_e('View', 'deposits-partial-payments-for-woocommerce'); ?> </a>
554          <?php
555        }
556      }
557    }
558
559    function awcdp_add_order_item_meta($item_id, $item){
560
561      $product = $item->get_product();
562      $awcdp_gs = get_option('awcdp_general_settings');
563      $default_checked = ( isset($awcdp_gs['default_selected']) ) ? $awcdp_gs['default_selected'] : 'deposit';
564
565      if ( $default_checked != 'full'){
566
567        $wfontend = new AWCDP_Front_End();
568        $deposit_enabled = $wfontend->awcdp_deposits_enabled( $product->get_id() );
569
570        if ( $deposit_enabled ) {
571          $deposit = $this->awcdp_calculate_product_deposit($product);
572          $price_include_tax = get_option('woocommerce_prices_include_tax');
573          if ($price_include_tax == 'yes') {
574            $amount = wc_get_price_including_tax($product);
575          } else {
576            $amount = wc_get_price_excluding_tax($product);
577          }
578          $deposit = $deposit * $item->get_quantity();
579          $amount = $amount * $item->get_quantity();
580
581          if ($deposit < $amount && $deposit > 0) {
582            $deposit_meta['enable'] = 'yes';
583            $deposit_meta['deposit'] = $deposit;
584            $deposit_meta['remaining'] = $amount - $deposit;
585            $deposit_meta['total'] = $amount;
586            $item->add_meta_data('awcdp_deposit_meta', $deposit_meta, true);
587            $item->save();
588          }
589        }
590      }
591
592    }
593
594    function awcdp_calculate_product_deposit($product){
595
596      $wfontend = new AWCDP_Front_End();
597      $deposit_enabled = $wfontend->awcdp_deposits_enabled( $product->get_id() );
598      $product_type = $product->get_type();
599      if ($deposit_enabled) {
600
601        $deposit = $wfontend->awcdp_get_deposit_amount($product->get_id());
602        $deposit = floatval($deposit);
603        $type = $wfontend->awcdp_get_deposit_type($product->get_id());
604
605        $price_include_tax = get_option('woocommerce_prices_include_tax');
606        if ($price_include_tax == 'yes') {
607          $amount = wc_get_price_including_tax($product);
608        } else {
609          $amount = wc_get_price_excluding_tax($product);
610        }
611
612        switch ($product_type) {
613            case 'subscription' :
614                if (class_exists('WC_Subscriptions_Product')) {
615                  $amount = \WC_Subscriptions_Product::get_sign_up_fee($product);
616                  if ($type == 'fixed') {
617                  } else {
618                    $deposit = $amount * ($deposit / 100.0);
619                  }
620                }
621                break;
622            case 'yith_bundle' :
623                $amount = $product->price_per_item_tot;
624                if ($type == 'fixed') {
625                } else {
626                    $deposit = $amount * ($deposit / 100.0);
627                }
628                break;
629            case 'variable' :
630                if ($type == 'fixed') {
631                } else {
632                  $deposit = $amount * ($deposit / 100.0);
633                }
634                break;
635            default:
636                if ($type != 'fixed') {
637                  $deposit = $amount * ($deposit / 100.0);
638                }
639                break;
640        }
641        return floatval($deposit);
642      }
643    }
644
645    function awcdp_recalculate_totals($and_taxes, $order){
646
647      $schedule = $order->get_meta('_awcdp_deposits_payment_schedule', true);
648       if (!empty($schedule) && is_array($schedule)) {
649           $payment = null; $second_payment_order = null;
650           $total = 0.0;
651           $due_payments = array();
652           $due_payments_total = 0.0;
653
654           foreach ($schedule as $payment) {
655             $payment_order = wc_get_order($payment['id']);
656             if ($payment_order){
657                if ($payment['type'] !== 'deposit' && $payment_order->get_status() !== 'completed') {
658                  $due_payments[] = $payment_order;
659                  $due_payments_total += floatval($payment_order->get_total());
660                }
661                  $total += floatval($payment_order->get_total());
662             }
663           }
664
665           $difference = floatval($order->get_total()) - $total;
666           if ($difference > 0 || $difference < 0) {
667               $positive = $difference > 0;
668               $difference = abs($difference);
669               $diff_record = $difference;
670               $count = 0;
671               foreach ($due_payments as $key => $due_payment) {
672                   $count++;
673                   $percentage = floatval($due_payment->get_total()) / $due_payments_total * 100;
674                   $amount = $difference / 100 * $percentage;
675                   if (count($due_payments) === $count) {
676                     $amount = $diff_record;
677                   } else {
678                     $diff_record -= $amount;
679                   }
680                   if ($positive) {
681                     foreach ($due_payment->get_fees() as $item) {
682                       $item->set_total(floatval($item->get_total()) + $amount);
683                       $item->save();
684                     }
685                   } else {
686                     foreach ($due_payment->get_fees() as $item) {
687                       $item->set_total(floatval($item->get_total()) - $amount);
688                       $item->save();
689                     }
690                   }
691                   $due_payment->calculate_totals(false);
692                   $due_payment->save();
693               }
694               $second_payment = $order->get_meta('_awcdp_deposits_second_payment', true);
695               if ($positive) {
696                 $second_payment += $difference;
697               } else {
698                 $second_payment -= $difference;
699               }
700               $order->update_meta_data('_awcdp_deposits_second_payment', wc_format_decimal(floatval($second_payment)));
701               $order->save();
702           }
703       }
704
705    }
706
707
708
709              /**
710               * Deactivation form
711              */
712              public function awcdp_deactivation_form() {
713                $currentScreen = get_current_screen();
714                $screenID = $currentScreen->id;
715                if ( $screenID == 'plugins' ) {
716                    $view = '<div id="awcdp-survey-form-wrap"><div id="awcdp-survey-form">
717                    <p>If you have a moment, please let us know why you are deactivating this plugin. All submissions are anonymous and we only use this feedback for improving our plugin.</p>
718                    <form method="POST">
719                        <input name="Plugin" type="hidden" placeholder="Plugin" value="'.AWCDP_TOKEN.'" required>
720                        <input name="Version" type="hidden" placeholder="Version" value="'.AWCDP_VERSION.'" required>
721                        <input name="Date" type="hidden" placeholder="Date" value="'.date("m/d/Y").'" required>
722                        <input name="Website" type="hidden" placeholder="Website" value="'.get_site_url().'" required>
723                        <input name="Title" type="hidden" placeholder="Title" value="'.get_bloginfo( 'name' ).'" required>
724                        <input type="radio" id="'.$this->_token.'-temporarily" name="Reason" value="I\'m only deactivating temporarily">
725                <label for="'.$this->_token.'-temporarily">I\'m only deactivating temporarily</label><br>
726                <input type="radio" id="'.$this->_token.'-notneeded" name="Reason" value="I no longer need the plugin">
727                <label for="'.$this->_token.'-notneeded">I no longer need the plugin</label><br>
728                <input type="radio" id="'.$this->_token.'-short" name="Reason" value="I only needed the plugin for a short period">
729                <label for="'.$this->_token.'-short">I only needed the plugin for a short period</label><br>
730                <input type="radio" id="'.$this->_token.'-better" name="Reason" value="I found a better plugin">
731                <label for="'.$this->_token.'-better">I found a better plugin</label><br>
732                <input type="radio" id="'.$this->_token.'-upgrade" name="Reason" value="Upgrading to PRO version">
733                <label for="'.$this->_token.'-upgrade">Upgrading to PRO version</label><br>
734                <input type="radio" id="'.$this->_token.'-requirement" name="Reason" value="Plugin doesn\'t meets my requirement">
735                <label for="'.$this->_token.'-requirement">Plugin doesn\'t meets my requirement</label><br>
736                <input type="radio" id="'.$this->_token.'-broke" name="Reason" value="Plugin broke my site">
737                <label for="'.$this->_token.'-broke">Plugin broke my site</label><br>
738                <input type="radio" id="'.$this->_token.'-stopped" name="Reason" value="Plugin suddenly stopped working">
739                <label for="'.$this->_token.'-stopped">Plugin suddenly stopped working</label><br>
740                <input type="radio" id="'.$this->_token.'-bug" name="Reason" value="I found a bug">
741                <label for="'.$this->_token.'-bug">I found a bug</label><br>
742                <input type="radio" id="'.$this->_token.'-other" name="Reason" value="Other">
743                <label for="'.$this->_token.'-other">Other</label><br>
744                        <p id="awcdp-error"></p>
745                        <div class="awcdp-comments" style="display:none;">
746                            <textarea type="text" name="Comments" placeholder="Please specify" rows="2"></textarea>
747                            <p>For support queries <a href="https://support.acowebs.com/portal/en/newticket?departmentId=361181000000006907&layoutId=361181000000074011" target="_blank">Submit Ticket</a></p>
748                        </div>
749                        <button type="submit" class="awcdp_button" id="awcdp_deactivate">Submit & Deactivate</button>
750                        <a href="#" class="awcdp_button" id="awcdp_cancel">Cancel</a>
751                        <a href="#" class="awcdp_button" id="awcdp_skip">Skip & Deactivate</a>
752                    </form></div></div>';
753                    echo $view;
754                } ?>
755                <style>
756                    #awcdp-survey-form-wrap{ display: none;position: absolute;top: 0px;bottom: 0px;left: 0px;right: 0px;z-index: 10000;background: rgb(0 0 0 / 63%); } #awcdp-survey-form{ display:none;margin-top: 15px;position: fixed;text-align: left;width: 40%;max-width: 600px;z-index: 100;top: 50%;left: 50%;transform: translate(-50%, -50%);background: rgba(255,255,255,1);padding: 35px;border-radius: 6px;border: 2px solid #fff;font-size: 14px;line-height: 24px;outline: none;}#awcdp-survey-form p{font-size: 14px;line-height: 24px;padding-bottom:20px;margin: 0;} #awcdp-survey-form .awcdp_button { margin: 25px 5px 10px 0px; height: 42px;border-radius: 6px;background-color: #1eb5ff;border: none;padding: 0 36px;color: #fff;outline: none;cursor: pointer;font-size: 15px;font-weight: 600;letter-spacing: 0.1px;color: #ffffff;margin-left: 0 !important;position: relative;display: inline-block;text-decoration: none;line-height: 42px;} #awcdp-survey-form .awcdp_button#awcdp_deactivate{background: #fff;border: solid 1px rgba(88,115,149,0.5);color: #a3b2c5;} #awcdp-survey-form .awcdp_button#awcdp_skip{background: #fff;border: none;color: #a3b2c5;padding: 0px 15px;float:right;} #awcdp-survey-form .awcdp_button[disabled] { cursor: no-drop; }#awcdp-survey-form .awcdp-comments{position: relative;}#awcdp-survey-form .awcdp-comments p{ position: absolute; top: -24px; right: 0px; font-size: 14px; padding: 0px; margin: 0px;} #awcdp-survey-form .awcdp-comments p a{text-decoration:none;}#awcdp-survey-form .awcdp-comments textarea{background: #fff;border: solid 1px rgba(88,115,149,0.5);width: 100%;line-height: 30px;resize:none;margin: 10px 0 0 0;} #awcdp-survey-form p#awcdp-error{margin-top: 10px;padding: 0px;font-size: 13px;color: #ea6464;}
757                </style>
758            <?php }
759
760
761
762
763
764    /**
765     * Cloning is forbidden.
766     *
767     * @since 1.0.0
768     */
769    public function __clone()
770    {
771        _doing_it_wrong(__FUNCTION__, __('Cheatin&#8217; huh?'), $this->_version);
772    }
773
774    /**
775     * Unserializing instances of this class is forbidden.
776     *
777     * @since 1.0.0
778     */
779    public function __wakeup()
780    {
781        _doing_it_wrong(__FUNCTION__, __('Cheatin&#8217; huh?'), $this->_version);
782    }
783
784    /**
785     * Installation. Runs on activation.
786     * @access  public
787     * @return  void
788     * @since   1.0.0
789     */
790    public function install()
791    {
792        $this->_log_version_number();
793
794        flush_rewrite_rules();
795    }
796
797    /**
798     * Log the plugin version number.
799     * @access  public
800     * @return  void
801     * @since   1.0.0
802     */
803    private function _log_version_number()
804    {
805        update_option($this->_token . '_version', $this->_version);
806    }
807
808
809}
Note: See TracBrowser for help on using the repository browser.