Guest User

Untitled

a guest
Jun 16th, 2020
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 38.54 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  
  5. * WCFMmp plugin core
  6.  
  7. *
  8.  
  9. * WCfMmp Refund
  10.  
  11. *
  12.  
  13. * @author WC Lovers
  14.  
  15. * @package wcfmmp/core
  16.  
  17. * @version 1.0.0
  18.  
  19. */
  20.  
  21.  
  22.  
  23. class WCFMmp_Refund {
  24.  
  25.  
  26.  
  27. public function __construct() {
  28.  
  29. global $WCFM, $WCFMmp;
  30.  
  31.  
  32.  
  33. add_filter( 'wcfm_query_vars', array( &$this, 'wcfm_refund_query_vars' ), 20 );
  34.  
  35. add_filter( 'wcfm_endpoint_title', array( &$this, 'wcfm_refund_endpoint_title' ), 20, 2 );
  36.  
  37. add_action( 'init', array( &$this, 'wcfm_refund_init' ), 20 );
  38.  
  39.  
  40.  
  41. // Refund Endpoint Edit
  42.  
  43. add_filter( 'wcfm_endpoints_slug', array( $this, 'refund_wcfm_endpoints_slug' ) );
  44.  
  45.  
  46.  
  47. // Refund menu on WCfM dashboard
  48.  
  49. if( apply_filters( 'wcfm_is_allow_refund', true ) && apply_filters( 'wcfm_is_allow_refund_requests', true ) ) {
  50.  
  51. add_filter( 'wcfm_menus', array( &$this, 'wcfm_refund_menus' ), 30 );
  52.  
  53. }
  54.  
  55.  
  56.  
  57. // Refund Load Scripts
  58.  
  59. add_action( 'wcfm_load_scripts', array( &$this, 'load_scripts' ), 30 );
  60.  
  61. add_action( 'after_wcfm_load_scripts', array( &$this, 'load_scripts' ), 30 );
  62.  
  63.  
  64.  
  65. // Refund Load Styles
  66.  
  67. add_action( 'wcfm_load_styles', array( &$this, 'load_styles' ), 30 );
  68.  
  69. add_action( 'after_wcfm_load_styles', array( &$this, 'load_styles' ), 30 );
  70.  
  71.  
  72.  
  73. // Refund Load views
  74.  
  75. add_action( 'wcfm_load_views', array( &$this, 'load_views' ), 30 );
  76.  
  77.  
  78.  
  79. // Refund Ajax Controllers
  80.  
  81. add_action( 'after_wcfm_ajax_controller', array( &$this, 'ajax_controller' ) );
  82.  
  83. add_action( 'wp_ajax_nopriv_wcfm_ajax_controller', array( &$this, 'ajax_controller' ) );
  84.  
  85.  
  86.  
  87. // Generate Refund Form Html
  88.  
  89. add_action('wp_ajax_wcfmmp_refund_requests_form_html', array( &$this, 'wcfmmp_refund_requests_form_html' ) );
  90.  
  91.  
  92.  
  93. if( wcfm_is_vendor() ) {
  94.  
  95. add_filter( 'wcfmmarketplace_orders_actions', array( &$this, 'wcfmmp_refund_orders_actions' ), 100, 4 );
  96.  
  97. }
  98.  
  99.  
  100.  
  101. // WC My Account Order action - Refund
  102.  
  103. add_filter( 'woocommerce_my_account_my_orders_actions', array( &$this, 'wcfmmp_myaccount_refund_order_action' ), 110, 2 );
  104.  
  105.  
  106.  
  107. //enqueue scripts
  108.  
  109. add_action('wp_enqueue_scripts', array(&$this, 'wcfm_refund_scripts'));
  110.  
  111. //enqueue styles
  112.  
  113. add_action('wp_enqueue_scripts', array(&$this, 'wcfm_refund_styles'));
  114.  
  115. }
  116.  
  117.  
  118.  
  119. /**
  120.  
  121. * Refund Query Var
  122.  
  123. */
  124.  
  125. function wcfm_refund_query_vars( $query_vars ) {
  126.  
  127. $wcfm_modified_endpoints = wcfm_get_option( 'wcfm_endpoints', array() );
  128.  
  129.  
  130.  
  131. $query_refund_vars = array(
  132.  
  133. 'wcfm-refund-requests' => ! empty( $wcfm_modified_endpoints['wcfm-refund-requests'] ) ? $wcfm_modified_endpoints['wcfm-refund-requests'] : 'refund-requests',
  134.  
  135. );
  136.  
  137.  
  138.  
  139. $query_vars = array_merge( $query_vars, $query_refund_vars );
  140.  
  141.  
  142.  
  143. return $query_vars;
  144.  
  145. }
  146.  
  147.  
  148.  
  149. /**
  150.  
  151. * Refund End Point Title
  152.  
  153. */
  154.  
  155. function wcfm_refund_endpoint_title( $title, $endpoint ) {
  156.  
  157. global $wp;
  158.  
  159. switch ( $endpoint ) {
  160.  
  161. case 'wcfm-refund-requests' :
  162.  
  163. $title = __( 'Refund Requests', 'wc-multivendor-marketplace' );
  164.  
  165. break;
  166.  
  167. }
  168.  
  169.  
  170.  
  171. return $title;
  172.  
  173. }
  174.  
  175.  
  176.  
  177. /**
  178.  
  179. * Refund Endpoint Intialize
  180.  
  181. */
  182.  
  183. function wcfm_refund_init() {
  184.  
  185. global $WCFM_Query;
  186.  
  187.  
  188.  
  189. // Intialize WCFM End points
  190.  
  191. $WCFM_Query->init_query_vars();
  192.  
  193. $WCFM_Query->add_endpoints();
  194.  
  195.  
  196.  
  197. if( !get_option( 'wcfm_updated_end_point_refund' ) ) {
  198.  
  199. // Flush rules after endpoint update
  200.  
  201. flush_rewrite_rules();
  202.  
  203. update_option( 'wcfm_updated_end_point_refund', 1 );
  204.  
  205. }
  206.  
  207. }
  208.  
  209.  
  210.  
  211. /**
  212.  
  213. * Refund Endpoiint Edit
  214.  
  215. */
  216.  
  217. function refund_wcfm_endpoints_slug( $endpoints ) {
  218.  
  219.  
  220.  
  221. $refund_endpoints = array(
  222.  
  223. 'wcfm-refund-requests' => 'refund-requests',
  224.  
  225. );
  226.  
  227.  
  228.  
  229. $endpoints = array_merge( $endpoints, $refund_endpoints );
  230.  
  231.  
  232.  
  233. return $endpoints;
  234.  
  235. }
  236.  
  237.  
  238.  
  239. /**
  240.  
  241. * WCFM Refund Menu
  242.  
  243. */
  244.  
  245. function wcfm_refund_menus( $menus ) {
  246.  
  247. global $WCFM;
  248.  
  249.  
  250.  
  251. //if( !wcfm_is_vendor() ) {
  252.  
  253. $menus = array_slice($menus, 0, 3, true) +
  254.  
  255. array( 'wcfm-refund-requests' => array( 'label' => __( 'Refund', 'wc-multivendor-marketplace' ),
  256.  
  257. 'url' => wcfm_refund_requests_url(),
  258.  
  259. 'icon' => 'retweet',
  260.  
  261. 'menu_for' => 'admin',
  262.  
  263. 'priority' => 69.5
  264.  
  265. ) ) +
  266.  
  267. array_slice($menus, 3, count($menus) - 3, true) ;
  268.  
  269. //}
  270.  
  271. return $menus;
  272.  
  273. }
  274.  
  275.  
  276.  
  277. /**
  278.  
  279. * Refund Scripts
  280.  
  281. */
  282.  
  283. public function load_scripts( $end_point ) {
  284.  
  285. global $WCFM, $WCFMmp;
  286.  
  287.  
  288.  
  289. switch( $end_point ) {
  290.  
  291. case 'wcfm-refund-requests':
  292.  
  293. $WCFM->library->load_select2_lib();
  294.  
  295. $WCFM->library->load_datatable_lib();
  296.  
  297. $WCFM->library->load_datatable_download_lib();
  298.  
  299. wp_enqueue_script( 'wcfmmp_refund_requests_js', $WCFMmp->library->js_lib_url . 'refund/wcfmmp-script-refund-requests.js', array('jquery'), $WCFMmp->version, true );
  300.  
  301.  
  302.  
  303. $wcfm_screen_manager_data = array();
  304.  
  305. if( wcfm_is_vendor() ) {
  306.  
  307. $wcfm_screen_manager_data[3] = 'yes';
  308.  
  309. }
  310.  
  311. $wcfm_screen_manager_data = apply_filters( 'wcfm_refund_screen_manage', $wcfm_screen_manager_data );
  312.  
  313. $wcfm_screen_manager_data = apply_filters( 'wcfm_screen_manager_data_columns', $wcfm_screen_manager_data, 'refund' );
  314.  
  315. wp_localize_script( 'wcfmmp_refund_requests_js', 'wcfm_refund_screen_manage', $wcfm_screen_manager_data );
  316.  
  317. break;
  318.  
  319.  
  320.  
  321. case 'wcfm-orders':
  322.  
  323. wp_enqueue_script( 'wcfmmp_refund_requests_form_js', $WCFMmp->library->js_lib_url . 'refund/wcfmmp-script-refund-requests-popup.js', array('jquery'), $WCFMmp->version, true );
  324.  
  325. // Localized Script
  326.  
  327. $wcfm_messages = get_wcfm_refund_requests_messages();
  328.  
  329. wp_localize_script( 'wcfmmp_refund_requests_form_js', 'wcfm_refund_requests_messages', $wcfm_messages );
  330.  
  331. break;
  332.  
  333. }
  334.  
  335. }
  336.  
  337.  
  338.  
  339. /**
  340.  
  341. * Refund Styles
  342.  
  343. */
  344.  
  345. public function load_styles( $end_point ) {
  346.  
  347. global $WCFM, $WCFMmp;
  348.  
  349.  
  350.  
  351. switch( $end_point ) {
  352.  
  353. case 'wcfm-refund-requests':
  354.  
  355. wp_enqueue_style( 'wcfmu_refund_requests_css', $WCFMmp->library->css_lib_url . 'refund/wcfmmp-style-refund-requests.css', array(), $WCFMmp->version );
  356.  
  357. break;
  358.  
  359.  
  360.  
  361. case 'wcfm-orders':
  362.  
  363. wp_enqueue_style( 'wcfmmp_refund_requests_form_css', $WCFMmp->library->css_lib_url . 'refund/wcfmmp-style-refund-requests-popup.css', array(), $WCFMmp->version );
  364.  
  365. break;
  366.  
  367. }
  368.  
  369. }
  370.  
  371.  
  372.  
  373. /**
  374.  
  375. * Refund Views
  376.  
  377. */
  378.  
  379. public function load_views( $end_point ) {
  380.  
  381. global $WCFM, $WCFMmp;
  382.  
  383.  
  384.  
  385. switch( $end_point ) {
  386.  
  387. case 'wcfm-refund-requests':
  388.  
  389. $WCFMmp->template->get_template( 'refund/wcfmmp-view-refund-requests.php' );
  390.  
  391. break;
  392.  
  393. }
  394.  
  395. }
  396.  
  397.  
  398.  
  399. /**
  400.  
  401. * Refund Ajax Controllers
  402.  
  403. */
  404.  
  405. public function ajax_controller() {
  406.  
  407. global $WCFM, $WCFMmp;
  408.  
  409.  
  410.  
  411. $controllers_path = $WCFMmp->plugin_path . 'controllers/refund/';
  412.  
  413.  
  414.  
  415. $controller = '';
  416.  
  417. if( isset( $_POST['controller'] ) ) {
  418.  
  419. $controller = sanitize_text_field( $_POST['controller'] );
  420.  
  421.  
  422.  
  423. switch( $controller ) {
  424.  
  425. case 'wcfm-refund-requests':
  426.  
  427. include_once( $controllers_path . 'wcfmmp-controller-refund-requests.php' );
  428.  
  429. new WCFMmp_Refund_Requests_Controller();
  430.  
  431. break;
  432.  
  433.  
  434.  
  435. case 'wcfm-refund-requests-form':
  436.  
  437. include_once( $controllers_path . 'wcfmmp-controller-refund-requests-form.php' );
  438.  
  439. new WCFMmp_Refund_Requests_Form_Controller();
  440.  
  441. break;
  442.  
  443.  
  444.  
  445. case 'wcfm-refund-requests-approve':
  446.  
  447. include_once( $controllers_path . 'wcfmmp-controller-refund-requests-actions.php' );
  448.  
  449. new WCFMmp_Refund_Requests_Approve_Controller();
  450.  
  451. break;
  452.  
  453.  
  454.  
  455. case 'wcfm-refund-requests-cancel':
  456.  
  457. include_once( $controllers_path . 'wcfmmp-controller-refund-requests-actions.php' );
  458.  
  459. new WCFMmp_Refund_Requests_Cancel_Controller();
  460.  
  461. break;
  462.  
  463. }
  464.  
  465. }
  466.  
  467. }
  468.  
  469.  
  470.  
  471. /**
  472.  
  473. * Refund Requests Form HTML
  474.  
  475. */
  476.  
  477. function wcfmmp_refund_requests_form_html() {
  478.  
  479. global $WCFM, $WCFMmp, $_POST;
  480.  
  481. if( isset( $_POST['order_id'] ) && !empty( $_POST['order_id'] ) ) {
  482.  
  483. $WCFMmp->template->get_template( 'refund/wcfmmp-view-refund-requests-popup.php' );
  484.  
  485. }
  486.  
  487. die;
  488.  
  489. }
  490.  
  491.  
  492.  
  493. public function wcfmmp_refund_orders_actions( $actions, $vendor_id, $order, $the_order ) {
  494.  
  495. global $WCFM, $WCFMmp;
  496.  
  497.  
  498.  
  499. $order_status = sanitize_title( $the_order->get_status() );
  500.  
  501. if( in_array( $order_status, apply_filters( 'wcfm_refund_disable_order_status', array( 'failed', 'cancelled', 'refunded', 'pending', 'on-hold', 'request', 'proposal', 'proposal-sent', 'proposal-expired', 'proposal-rejected', 'proposal-canceled', 'proposal-accepted' ) ) ) ) return $actions;
  502.  
  503.  
  504.  
  505. if( !apply_filters( 'wcfm_is_allow_refund_requests', true ) ) return $actions;
  506.  
  507. if( !apply_filters( 'wcfm_is_allow_paid_order_refund', false ) && ($order->withdraw_status != 'pending') && !in_array( $the_order->get_payment_method(), array( 'wirecard', 'stripe_split' ) ) ) return $actions;
  508.  
  509.  
  510.  
  511. $refund_statuses = explode( ",", $order->refund_statuses );
  512.  
  513. //if( in_array( 'requested', $refund_statuses ) ) return $actions;
  514.  
  515.  
  516.  
  517. $is_refundeds = explode( ",", $order->is_refundeds );
  518.  
  519. if( !in_array( 0, $is_refundeds ) ) return $actions;
  520.  
  521.  
  522.  
  523. // Refund Threshold check
  524.  
  525. $refund_threshold = isset( $WCFMmp->wcfmmp_refund_options['refund_threshold'] ) ? $WCFMmp->wcfmmp_refund_options['refund_threshold'] : '';
  526.  
  527. if( $refund_threshold ) {
  528.  
  529. $current_time = strtotime( 'midnight', current_time( 'timestamp' ) );
  530.  
  531. $date = date( 'Y-m-d', $current_time );
  532.  
  533. $created_date = date( 'Y-m-d', strtotime($order->created) );
  534.  
  535. $datetime1 = new DateTime( $date );
  536.  
  537. $datetime2 = new DateTime( $created_date );
  538.  
  539. $interval = $datetime2->diff( $datetime1 );
  540.  
  541. $interval = $interval->format( '%r%a' );
  542.  
  543. if( ( (int) $interval >= 0 ) && ( (int) $interval > (int) $refund_threshold ) ) return $actions;
  544.  
  545. }
  546.  
  547.  
  548.  
  549. $actions .= '<a class="wcfmmp_order_refund_request wcfm-action-icon" href="#" data-item="' . $order->item_id . '" data-commission="' . $order->ID . '" data-order="' . $order->order_id . '"><span class="wcfmfa fa-retweet text_tip" data-tip="' . esc_attr__( 'Refund Request', 'wc-multivendor-marketplace' ) . '"></span></a>';
  550.  
  551.  
  552.  
  553. return $actions;
  554.  
  555. }
  556.  
  557.  
  558.  
  559. /**
  560.  
  561. * WCFM Refund action at My Account Order actions
  562.  
  563. */
  564.  
  565. function wcfmmp_myaccount_refund_order_action( $actions, $order ) {
  566.  
  567. global $WCFM, $WCFMmp, $wpdb;
  568.  
  569.  
  570.  
  571. if( !apply_filters( 'wcfm_is_allow_customer_refund', true ) ) return $actions;
  572.  
  573.  
  574.  
  575. $refund_by_customer = isset( $WCFMmp->wcfmmp_refund_options['refund_by_customer'] ) ? $WCFMmp->wcfmmp_refund_options['refund_by_customer'] : 'no';
  576.  
  577. if( $refund_by_customer == 'no' ) return $actions;
  578.  
  579.  
  580.  
  581. $order_id = $order->get_id();
  582.  
  583.  
  584.  
  585. $wcfm_refund_request = get_post_meta( $order_id, '_wcfm_refund_request', true );
  586.  
  587. if( $wcfm_refund_request ) return $actions;
  588.  
  589.  
  590.  
  591. $order_status = sanitize_title( $order->get_status() );
  592.  
  593. if( in_array( $order_status, apply_filters( 'wcfm_refund_disable_order_status', array( 'failed', 'cancelled', 'refunded', 'pending', 'on-hold', 'request', 'proposal', 'proposal-sent', 'proposal-expired', 'proposal-rejected', 'proposal-canceled', 'proposal-accepted' ) ) ) ) return $actions;
  594.  
  595.  
  596.  
  597. $withdraw_status = 'pending';
  598.  
  599. $refund_status = '';
  600.  
  601. $vendor_id = 0;
  602.  
  603. $is_refunded = 0;
  604.  
  605.  
  606.  
  607. $sql = 'SELECT ID, withdraw_status, vendor_id, refund_status, is_refunded FROM ' . $wpdb->prefix . 'wcfm_marketplace_orders AS commission';
  608.  
  609. $sql .= ' WHERE 1=1';
  610.  
  611. $sql .= " AND `order_id` = " . $order_id;
  612.  
  613. $commissions = $wpdb->get_results( $sql );
  614.  
  615. if( !empty( $commissions ) ) {
  616.  
  617. foreach( $commissions as $commission ) {
  618.  
  619. $withdraw_status = $commission->withdraw_status;
  620.  
  621. $vendor_id = $commission->vendor_id;
  622.  
  623. $refund_status = $commission->refund_status;
  624.  
  625. $is_refunded = $commission->is_refunded;
  626.  
  627.  
  628.  
  629. if( !$is_refunded && ( $refund_status != 'requested' ) ) break;
  630.  
  631. }
  632.  
  633. }
  634.  
  635.  
  636.  
  637. //if( $withdraw_status != 'pending' ) return $actions;
  638.  
  639. if( $refund_status == 'requested' ) return $actions;
  640.  
  641. if( $is_refunded ) return $actions;
  642.  
  643.  
  644.  
  645. // Refund Threshold check
  646.  
  647. $refund_threshold = isset( $WCFMmp->wcfmmp_refund_options['refund_threshold'] ) ? $WCFMmp->wcfmmp_refund_options['refund_threshold'] : '';
  648.  
  649. if( $refund_threshold ) {
  650.  
  651. $current_time = strtotime( 'midnight', current_time( 'timestamp' ) );
  652.  
  653. $date = date( 'Y-m-d', $current_time );
  654.  
  655. $order_date = ( version_compare( WC_VERSION, '2.7', '<' ) ) ? $order->order_date : $order->get_date_created();
  656.  
  657. $created_date = date( 'Y-m-d', strtotime($order_date) );
  658.  
  659. $datetime1 = new DateTime( $date );
  660.  
  661. $datetime2 = new DateTime( $created_date );
  662.  
  663. $interval = $datetime2->diff( $datetime1 );
  664.  
  665. $interval = $interval->format( '%r%a' );
  666.  
  667. if( ( (int) $interval >= 0 ) && ( (int) $interval > (int) $refund_threshold ) ) return $actions;
  668.  
  669. }
  670.  
  671.  
  672.  
  673. $actions['wcfm-refund-action'] = array( 'name' => __( 'Refund', 'wc-multivendor-marketplace' ), 'url' => '#' . $order_id );
  674.  
  675. return $actions;
  676.  
  677. }
  678.  
  679.  
  680.  
  681. public function wcfmmp_refund_processed( $vendor_id, $order_id, $commission_id, $item_id, $refund_reason, $refunded_amount = 1, $refunded_qty = '', $refunded_tax = array(), $refund_request = 'full', $refund_status = 'requested' ) {
  682.  
  683. global $WCFM, $WCFMmp, $wpdb;
  684.  
  685.  
  686.  
  687. $requested_by = get_current_user_id();
  688.  
  689.  
  690.  
  691. $is_partially_refunded = 0;
  692.  
  693. $refund_type = 'refund';
  694.  
  695. if( $refund_request == 'partial' ) {
  696.  
  697. $is_partially_refunded = 1;
  698.  
  699. $refund_type = 'partial-refund';
  700.  
  701. }
  702.  
  703.  
  704.  
  705. $wpdb->query(
  706.  
  707. $wpdb->prepare(
  708.  
  709. "INSERT INTO `{$wpdb->prefix}wcfm_marketplace_refund_request`
  710.  
  711. ( vendor_id
  712.  
  713. , order_id
  714.  
  715. , commission_id
  716.  
  717. , item_id
  718.  
  719. , requested_by
  720.  
  721. , refunded_amount
  722.  
  723. , refund_reason
  724.  
  725. , is_partially_refunded
  726.  
  727. , refund_status
  728.  
  729. , created
  730.  
  731. ) VALUES ( %d
  732.  
  733. , %d
  734.  
  735. , %d
  736.  
  737. , %d
  738.  
  739. , %d
  740.  
  741. , %s
  742.  
  743. , %s
  744.  
  745. , %d
  746.  
  747. , %s
  748.  
  749. , %s
  750.  
  751. ) ON DUPLICATE KEY UPDATE `created` = %s"
  752.  
  753. , $vendor_id
  754.  
  755. , $order_id
  756.  
  757. , $commission_id
  758.  
  759. , $item_id
  760.  
  761. , $requested_by
  762.  
  763. , $refunded_amount
  764.  
  765. , $refund_reason
  766.  
  767. , $is_partially_refunded
  768.  
  769. , $refund_status
  770.  
  771. , date( 'Y-m-d H:i:s', current_time( 'timestamp', 0 ) )
  772.  
  773. , date( 'Y-m-d H:i:s', current_time( 'timestamp', 0 ) )
  774.  
  775. )
  776.  
  777. );
  778.  
  779. $refund_request_id = $wpdb->insert_id;
  780.  
  781.  
  782.  
  783. $this->wcfmmp_update_refund_meta( $refund_request_id, 'refunded_qty', $refunded_qty );
  784.  
  785. $this->wcfmmp_update_refund_meta( $refund_request_id, 'refunded_tax', serialize( $refunded_tax ) );
  786.  
  787.  
  788.  
  789. // Set Order Meta
  790.  
  791. update_post_meta( $order_id, '_wcfm_refund_request', 'yes' );
  792.  
  793.  
  794.  
  795. do_action( 'wcfmmp_refund_request_processed', $refund_request_id, $vendor_id, $order_id, $commission_id, $refunded_amount, $refund_type );
  796.  
  797. return $refund_request_id;
  798.  
  799. }
  800.  
  801.  
  802.  
  803. /**
  804.  
  805. * Update Refund metas
  806.  
  807. */
  808.  
  809. public function wcfmmp_update_refund_meta( $refund_id, $key, $value ) {
  810.  
  811. global $WCFM, $WCFMmp, $wpdb;
  812.  
  813.  
  814.  
  815. $wpdb->query(
  816.  
  817. $wpdb->prepare(
  818.  
  819. "INSERT INTO `{$wpdb->prefix}wcfm_marketplace_refund_request_meta`
  820.  
  821. ( refund_id
  822.  
  823. , `key`
  824.  
  825. , `value`
  826.  
  827. ) VALUES ( %d
  828.  
  829. , %s
  830.  
  831. , %s
  832.  
  833. )"
  834.  
  835. , $refund_id
  836.  
  837. , $key
  838.  
  839. , $value
  840.  
  841. )
  842.  
  843. );
  844.  
  845. $refund_meta_id = $wpdb->insert_id;
  846.  
  847. return $refund_meta_id;
  848.  
  849. }
  850.  
  851.  
  852.  
  853. /**
  854.  
  855. * Get Refund metas
  856.  
  857. */
  858.  
  859. public function wcfmmp_get_refund_meta( $refund_id, $key ) {
  860.  
  861. global $WCFM, $WCFMmp, $wpdb;
  862.  
  863.  
  864.  
  865. $commission_meta = $wpdb->get_var(
  866.  
  867. $wpdb->prepare(
  868.  
  869. "SELECT `value` FROM `{$wpdb->prefix}wcfm_marketplace_refund_request_meta`
  870.  
  871. WHERE
  872.  
  873. `refund_id` = %d
  874.  
  875. AND `key` = %s
  876.  
  877. "
  878.  
  879. , $refund_id
  880.  
  881. , $key
  882.  
  883. )
  884.  
  885. );
  886.  
  887. return $commission_meta;
  888.  
  889. }
  890.  
  891.  
  892.  
  893. /**
  894.  
  895. * Refund status update by Refund ID
  896.  
  897. */
  898.  
  899. public function wcfmmp_refund_status_update_by_refund( $refund_id, $status = 'completed', $refund_note = '' ) {
  900.  
  901. global $WCFM, $WCFMmp, $wpdb;
  902.  
  903.  
  904.  
  905. if( !$refund_id ) return;
  906.  
  907.  
  908.  
  909. if( $status == 'completed' ) {
  910.  
  911. $vendor_id = 0;
  912.  
  913. $order_id = 0;
  914.  
  915.  
  916.  
  917. // On complete Commission table update
  918.  
  919. $sql = 'SELECT item_id, commission_id, vendor_id, order_id, is_partially_refunded, refunded_amount, refund_reason FROM ' . $wpdb->prefix . 'wcfm_marketplace_refund_request';
  920.  
  921. $sql .= ' WHERE 1=1';
  922.  
  923. $sql .= " AND ID = " . $refund_id;
  924.  
  925. $refund_infos = $wpdb->get_results( $sql );
  926.  
  927. if( !empty( $refund_infos ) ) {
  928.  
  929. foreach( $refund_infos as $refund_info ) {
  930.  
  931. $item_id = absint( $refund_info->item_id );
  932.  
  933. $vendor_id = absint( $refund_info->vendor_id );
  934.  
  935. $order_id = absint( $refund_info->order_id );
  936.  
  937. $commission_id = absint( $refund_info->commission_id );
  938.  
  939. $refunded_amount = (float) $refund_info->refunded_amount;
  940.  
  941. $c_refunded_amount = $refunded_amount;
  942.  
  943. $c_refunded_qty = $this->wcfmmp_get_refund_meta( $refund_id, 'refunded_qty' );
  944.  
  945. $refund_reason = $refund_info->refund_reason;
  946.  
  947. $is_partially_refunded = $refund_info->is_partially_refunded;
  948.  
  949. $is_refunded = 0;
  950.  
  951. if( !$is_partially_refunded ) $is_refunded = 1;
  952.  
  953.  
  954.  
  955. $wc_refund_processed = true;
  956.  
  957.  
  958.  
  959. // Create WC Refund Item
  960.  
  961. if( $order_id ) {
  962.  
  963. $order = wc_get_order( $order_id );
  964.  
  965.  
  966.  
  967. // API Refund Check
  968.  
  969. $api_refund = false;
  970.  
  971. if ( WC()->payment_gateways() ) {
  972.  
  973. $payment_gateways = WC()->payment_gateways->payment_gateways();
  974.  
  975. if ( isset( $payment_gateways[ $order->get_payment_method() ] ) && $payment_gateways[ $order->get_payment_method() ]->supports( 'refunds' ) ) {
  976.  
  977. $api_refund = true;
  978.  
  979. }
  980.  
  981. }
  982.  
  983. $api_refund = apply_filters( 'wcfm_is_allow_api_refund', $api_refund, $order->get_payment_method() );
  984.  
  985.  
  986.  
  987. $restock_refunded_items = 'true';
  988.  
  989. $refund_tax = $this->wcfmmp_get_refund_meta( $refund_id, 'refunded_tax' );
  990.  
  991. if( !$refund_tax ) $refund_tax = array();
  992.  
  993. else $refund_tax = unserialize( $refund_tax );
  994.  
  995.  
  996.  
  997. if( $is_refunded ) {
  998.  
  999. $line_item = new WC_Order_Item_Product( $item_id );
  1000.  
  1001. $product = $line_item->get_product();
  1002.  
  1003.  
  1004.  
  1005. /*$refund_tax = $line_item->get_taxes();
  1006.  
  1007. $c_refunded_qty = $line_item->get_quantity();
  1008.  
  1009. if( !empty( $refund_tax ) && is_array( $refund_tax ) ) {
  1010.  
  1011. if( isset( $refund_tax['total'] ) ) {
  1012.  
  1013. $refund_tax = $refund_tax['total'];
  1014.  
  1015. }
  1016.  
  1017. if( !empty( $refund_tax ) && is_array( $refund_tax ) ) {
  1018.  
  1019. foreach( $refund_tax as $refund_tax_id => $refund_tax_price ) {
  1020.  
  1021. $refunded_amount += (float) $refund_tax_price;
  1022.  
  1023. }
  1024.  
  1025. }
  1026.  
  1027. }*/
  1028.  
  1029.  
  1030.  
  1031. // Item Shipping Refund
  1032.  
  1033. $vendor_shipping = $WCFMmp->wcfmmp_shipping->get_order_vendor_shipping( $order_id );
  1034.  
  1035.  
  1036.  
  1037. $shipping_cost = $shipping_tax = 0;
  1038.  
  1039. if ( !empty($vendor_shipping) && isset($vendor_shipping[$vendor_id]) && $vendor_shipping[$vendor_id]['shipping_item_id'] && ( $product && $product->needs_shipping() ) ) {
  1040.  
  1041. $shipping_item_id = $vendor_shipping[$vendor_id]['shipping_item_id'];
  1042.  
  1043. $package_qty = absint( $vendor_shipping[$vendor_id]['package_qty'] );
  1044.  
  1045. if( !$package_qty ) $package_qty = $line_item->get_quantity();
  1046.  
  1047. $shipping_item = new WC_Order_Item_Shipping( $shipping_item_id );
  1048.  
  1049. $refund_shipping_tax = $shipping_item->get_taxes();
  1050.  
  1051. $shipping_tax_refund = array();
  1052.  
  1053. if( !empty( $refund_shipping_tax ) && is_array( $refund_shipping_tax ) ) {
  1054.  
  1055. if( isset( $refund_shipping_tax['total'] ) ) {
  1056.  
  1057. $refund_shipping_tax = $refund_shipping_tax['total'];
  1058.  
  1059. }
  1060.  
  1061. if( !empty( $refund_shipping_tax ) && is_array( $refund_shipping_tax ) ) {
  1062.  
  1063. foreach( $refund_shipping_tax as $refund_shipping_tax_id => $refund_shipping_tax_price ) {
  1064.  
  1065. $refunded_amount += round( ((float) $refund_shipping_tax_price/$package_qty) * $line_item->get_quantity(), 2);
  1066.  
  1067. $shipping_tax_refund[$refund_shipping_tax_id] = round( ((float) $refund_shipping_tax_price/$package_qty) * $line_item->get_quantity(), 2);
  1068.  
  1069. }
  1070.  
  1071. }
  1072.  
  1073. }
  1074.  
  1075.  
  1076.  
  1077. $shipping_cost = (float) round(($vendor_shipping[$vendor_id]['shipping'] / $package_qty) * $line_item->get_quantity(), 2);
  1078.  
  1079. $refunded_amount += $shipping_cost;
  1080.  
  1081. $line_items[ $shipping_item_id ] = array(
  1082.  
  1083. 'refund_total' => $shipping_cost,
  1084.  
  1085. 'refund_tax' => $shipping_tax_refund,
  1086.  
  1087. );
  1088.  
  1089. }
  1090.  
  1091. }
  1092.  
  1093.  
  1094.  
  1095. if( !empty( $refund_tax ) && is_array( $refund_tax ) ) {
  1096.  
  1097. foreach( $refund_tax as $refund_tax_id => $refund_tax_price ) {
  1098.  
  1099. $refunded_amount += (float) $refund_tax_price;
  1100.  
  1101. }
  1102.  
  1103. }
  1104.  
  1105.  
  1106.  
  1107. try {
  1108.  
  1109. $line_items[ $item_id ] = array(
  1110.  
  1111. 'refund_total' => $c_refunded_amount,
  1112.  
  1113. 'refund_tax' => $refund_tax,
  1114.  
  1115. );
  1116.  
  1117.  
  1118.  
  1119. //if( $is_refunded ) {
  1120.  
  1121. $line_items[ $item_id ]['qty'] = $c_refunded_qty;
  1122.  
  1123. //}
  1124.  
  1125.  
  1126.  
  1127. $wcfm_create_refund_args = apply_filters( 'wcfm_create_refund_args', array(
  1128.  
  1129. 'amount' => round( $refunded_amount, 2 ),
  1130.  
  1131. 'reason' => $refund_reason,
  1132.  
  1133. 'order_id' => $order_id,
  1134.  
  1135. 'line_items' => $line_items,
  1136.  
  1137. 'refund_payment' => $api_refund,
  1138.  
  1139. 'restock_items' => $restock_refunded_items,
  1140.  
  1141. ), $refund_id, $order_id );
  1142.  
  1143.  
  1144.  
  1145. //print_r($wcfm_create_refund_args);
  1146.  
  1147.  
  1148.  
  1149. // Create the refund object.
  1150.  
  1151. $refund = wc_create_refund( $wcfm_create_refund_args );
  1152.  
  1153.  
  1154.  
  1155. if ( is_wp_error( $refund ) ) {
  1156.  
  1157. $wc_refund_processed = false;
  1158.  
  1159. wcfm_log( $refund->get_error_message() . " args => " . json_encode( $wcfm_create_refund_args ) );
  1160.  
  1161. }
  1162.  
  1163. } catch ( Exception $e ) {
  1164.  
  1165. $wc_refund_processed = false;
  1166.  
  1167. wcfm_log($e->getMessage());
  1168.  
  1169. }
  1170.  
  1171. }
  1172.  
  1173.  
  1174.  
  1175. // Processing Vendor Commission Refund
  1176.  
  1177. if( $wc_refund_processed ) {
  1178.  
  1179. $commission_amount = 0;
  1180.  
  1181. $total_commission = 0;
  1182.  
  1183. $refunded_tax_amount = 0;
  1184.  
  1185.  
  1186.  
  1187. if( !empty( $refund_tax ) && is_array( $refund_tax ) ) {
  1188.  
  1189. foreach( $refund_tax as $refund_tax_id => $refund_tax_price ) {
  1190.  
  1191. $refunded_tax_amount += (float) $refund_tax_price;
  1192.  
  1193. }
  1194.  
  1195. }
  1196.  
  1197.  
  1198.  
  1199. if( $commission_id ) {
  1200.  
  1201.  
  1202.  
  1203. $commission_tax = (float) $WCFMmp->wcfmmp_commission->wcfmmp_get_commission_meta( $commission_id, 'commission_tax' );
  1204.  
  1205. $transaction_charge = (float) $WCFMmp->wcfmmp_commission->wcfmmp_get_commission_meta( $commission_id, 'transaction_charge' );
  1206.  
  1207. $aff_commission = (float) $WCFMmp->wcfmmp_commission->wcfmmp_get_commission_meta( $commission_id, '_wcfm_affiliate_commission' );
  1208.  
  1209.  
  1210.  
  1211. // Fetch Commission details & recalculate commission
  1212.  
  1213. $sql = 'SELECT order_id, product_id, variation_id, item_sub_total, item_total, quantity, commission_amount, total_commission, refunded_amount, tax, shipping, shipping_tax_amount, commission_status FROM ' . $wpdb->prefix . 'wcfm_marketplace_orders';
  1214.  
  1215. $sql .= ' WHERE 1=1';
  1216.  
  1217. $sql .= " AND ID = " . $commission_id;
  1218.  
  1219. $sql .= " AND vendor_id = " . $vendor_id;
  1220.  
  1221. $commission_infos = $wpdb->get_results( $sql );
  1222.  
  1223. if( !empty( $commission_infos ) ) {
  1224.  
  1225. foreach( $commission_infos as $commission_info ) {
  1226.  
  1227. $commission_amount = (float) $commission_info->commission_amount;
  1228.  
  1229. $total_commission = (float) $commission_info->total_commission;
  1230.  
  1231. $total_commission = ( $total_commission + $commission_tax + $aff_commission + $transaction_charge ) - $commission_amount;
  1232.  
  1233.  
  1234.  
  1235. $tax = (float) $commission_info->tax;
  1236.  
  1237. $gross_tax = (float) $WCFMmp->wcfmmp_commission->wcfmmp_get_commission_meta( $commission_id, 'gross_tax_cost' );
  1238.  
  1239. $refunded_tax = (float) ( $gross_tax - $tax );
  1240.  
  1241.  
  1242.  
  1243. $shipping = (float) $commission_info->shipping;
  1244.  
  1245. $gross_shipping = (float) $WCFMmp->wcfmmp_commission->wcfmmp_get_commission_meta( $commission_id, 'gross_shipping_cost' );
  1246.  
  1247. $refunded_shipping = (float) ( $gross_shipping - $shipping );
  1248.  
  1249.  
  1250.  
  1251. $shipping_tax = (float) $commission_info->shipping_tax_amount;
  1252.  
  1253. $gross_shipping_tax = (float) $WCFMmp->wcfmmp_commission->wcfmmp_get_commission_meta( $commission_id, 'gross_shipping_tax' );
  1254.  
  1255. $refunded_shipping_tax = (float) ( $gross_shipping_tax - $shipping_tax );
  1256.  
  1257.  
  1258.  
  1259. $commission_status = $commission_info->commission_status;
  1260.  
  1261. if( $is_partially_refunded ) {
  1262.  
  1263. $commission_rule = unserialize( $WCFMmp->wcfmmp_commission->wcfmmp_get_commission_meta( $commission_id, 'commission_rule' ) );
  1264.  
  1265. if( isset( $commission_rule['coupon_deduct'] ) && ( $commission_rule['coupon_deduct'] == 'yes' ) ) {
  1266.  
  1267. $item_total = (float) ( $commission_info->item_total + $refunded_tax + $refunded_shipping + $refunded_shipping_tax ) - ( $c_refunded_amount + (float) $commission_info->refunded_amount );
  1268.  
  1269. } else {
  1270.  
  1271. $item_total = (float) ( $commission_info->item_sub_total + $refunded_tax + $refunded_shipping + $refunded_shipping_tax ) - ( $c_refunded_amount + (float) $commission_info->refunded_amount );
  1272.  
  1273. }
  1274.  
  1275. $commission_amount = $WCFMmp->wcfmmp_commission->wcfmmp_get_order_item_commission( $commission_info->order_id, $vendor_id, $commission_info->product_id, $commission_info->variation_id, $item_total, $commission_info->quantity, $commission_rule );
  1276.  
  1277. $total_commission += (float) $commission_amount;
  1278.  
  1279. $total_commission -= (float) $refunded_tax_amount;
  1280.  
  1281. $total_commission -= (float) $aff_commission;
  1282.  
  1283. $total_commission -= (float) $transaction_charge; // Not right, have to recalculate
  1284.  
  1285.  
  1286.  
  1287. // Commission Tax Calculation
  1288.  
  1289. if( isset( $commission_rule['tax_enable'] ) && ( $commission_rule['tax_enable'] == 'yes' ) ) {
  1290.  
  1291. $commission_tax = $total_commission * ( (float)$commission_rule['tax_percent'] / 100 );
  1292.  
  1293. $commission_tax = apply_filters( 'wcfmmp_commission_deducted_tax', $commission_tax, $vendor_id, $commission_info->product_id, $commission_info->order_id, $total_commission, $commission_rule );
  1294.  
  1295. $total_commission -= (float) $commission_tax;
  1296.  
  1297.  
  1298.  
  1299. $WCFMmp->wcfmmp_commission->wcfmmp_delete_commission_meta( $commission_id, 'commission_tax' );
  1300.  
  1301. $WCFMmp->wcfmmp_commission->wcfmmp_update_commission_meta( $commission_id, 'commission_tax', round($commission_tax, 2) );
  1302.  
  1303. }
  1304.  
  1305.  
  1306.  
  1307. $c_refunded_amount = $refunded_amount;
  1308.  
  1309. $tax = (float)$tax - (float)$refunded_tax_amount;
  1310.  
  1311. $WCFMmp->wcfmmp_commission->wcfmmp_update_commission_meta( $commission_id, 'gross_tax_cost', round($tax, 2) );
  1312.  
  1313. } else {
  1314.  
  1315. $c_refunded_amount = $refunded_amount; //(float) $commission_info->total_commission;
  1316.  
  1317. $commission_amount = 0;
  1318.  
  1319. $total_commission = 0;
  1320.  
  1321. $remaining_tax_amount =
  1322.  
  1323. $commission_status = 'refunded';
  1324.  
  1325. }
  1326.  
  1327.  
  1328.  
  1329. $refunded_amount = $refunded_amount + (float) $commission_info->refunded_amount;
  1330.  
  1331.  
  1332.  
  1333. $wpdb->update("{$wpdb->prefix}wcfm_marketplace_orders", array('refunded_id' => $refund_id, 'refund_status' => $status, 'refunded_amount' => $refunded_amount, 'is_refunded' => $is_refunded, 'is_partially_refunded' => $is_partially_refunded, 'commission_amount' => $commission_amount, 'tax' => $tax, 'total_commission' => $total_commission, 'commission_status' => $commission_status ), array('ID' => $commission_id), array('%d', '%s', '%s', '%d', '%d', '%s', '%s', '%s', '%s'), array('%d'));
  1334.  
  1335. }
  1336.  
  1337. }
  1338.  
  1339.  
  1340.  
  1341.  
  1342.  
  1343. // Update commission ledger status - not sure
  1344.  
  1345. if( $is_partially_refunded ) {
  1346.  
  1347. $WCFMmp->wcfmmp_ledger->wcfmmp_ledger_status_update( $commission_id, 'partial-refunded' );
  1348.  
  1349. } else {
  1350.  
  1351. $WCFMmp->wcfmmp_ledger->wcfmmp_ledger_status_update( $commission_id, 'refunded' );
  1352.  
  1353. }
  1354.  
  1355.  
  1356.  
  1357. // Vendor Notification
  1358.  
  1359. if( $vendor_id ) {
  1360.  
  1361. $wcfm_messages = sprintf( __( 'Your Refund Request approved for Order <b>%s</b>.', 'wc-multivendor-marketplace' ), '<a target="_blank" class="wcfm_dashboard_item_title" href="' . get_wcfm_view_order_url( $order_id ) . '">#' . $order->get_order_number() . '</a>' );
  1362.  
  1363. if( $refund_note ) {
  1364.  
  1365. $wcfm_messages .= "<br /><b>" . __( 'Note', 'wc-multivendor-marketplace' ) . "</b>: " . $refund_note;
  1366.  
  1367. }
  1368.  
  1369. $WCFM->wcfm_notification->wcfm_send_direct_message( -1, $vendor_id, 1, 0, $wcfm_messages, 'refund-request' );
  1370.  
  1371. }
  1372.  
  1373.  
  1374.  
  1375. // Refund Item Meta
  1376.  
  1377. if ( !is_wp_error( $refund ) && $vendor_id ) {
  1378.  
  1379. $refund->set_refunded_by( $vendor_id );
  1380.  
  1381. $refund->save();
  1382.  
  1383. }
  1384.  
  1385.  
  1386.  
  1387. do_action( 'wcfmmp_commission_refund_status_completed', $refund_id, $commission_id, $order_id, $vendor_id, $refund );
  1388.  
  1389. }
  1390.  
  1391.  
  1392.  
  1393. // Refund Status Updated
  1394.  
  1395. $wpdb->update("{$wpdb->prefix}wcfm_marketplace_refund_request", array('refund_status' => $status, 'refunded_amount' => $c_refunded_amount, 'refund_paid_date' => date('Y-m-d H:i:s', current_time( 'timestamp', 0 ))), array('ID' => $refund_id), array('%s', '%s', '%s'), array('%d'));
  1396.  
  1397.  
  1398.  
  1399. // On refund complete ledge entry status update
  1400.  
  1401. if( $is_partially_refunded ) {
  1402.  
  1403. $WCFMmp->wcfmmp_ledger->wcfmmp_ledger_status_update( $refund_id, 'completed', 'partial-refund' );
  1404.  
  1405. } else {
  1406.  
  1407. $wpdb->update("{$wpdb->prefix}wcfm_marketplace_vendor_ledger", array('debit' => $c_refunded_amount), array('reference_id' => $refund_id, 'reference' => 'refund'), array('%s'), array('%d', '%s'));
  1408.  
  1409. $WCFMmp->wcfmmp_ledger->wcfmmp_ledger_status_update( $refund_id, 'completed', 'refund' );
  1410.  
  1411. }
  1412.  
  1413.  
  1414.  
  1415. // Order Not Added
  1416.  
  1417. $wcfm_messages = sprintf( __( 'Refund Request approved for Order <b>%s</b>.', 'wc-multivendor-marketplace' ), '#' . $order->get_order_number() );
  1418.  
  1419. if( $refund_note ) {
  1420.  
  1421. $wcfm_messages .= "<br /><b>" . __( 'Note', 'wc-multivendor-marketplace' ) . "</b>: " . $refund_note;
  1422.  
  1423. }
  1424.  
  1425.  
  1426.  
  1427. $comment_id = $order->add_order_note( $wcfm_messages, apply_filters( 'wcfm_is_allow_refund_update_note_for_customer', '1' ) );
  1428.  
  1429. if( $vendor_id ) {
  1430.  
  1431. add_comment_meta( $comment_id, '_vendor_id', $vendor_id );
  1432.  
  1433. }
  1434.  
  1435.  
  1436.  
  1437. // Update Order Meta
  1438.  
  1439. delete_post_meta( $order_id, '_wcfm_refund_request' );
  1440.  
  1441.  
  1442.  
  1443. do_action( 'wcfmmp_refund_status_completed', $refund_id, $order_id, $vendor_id, $refund );
  1444.  
  1445.  
  1446.  
  1447. return true;
  1448.  
  1449. }
  1450.  
  1451. }
  1452.  
  1453. }
  1454.  
  1455. } else {
  1456.  
  1457. // Order Status Updated
  1458.  
  1459. $sql = 'SELECT commission_id, vendor_id, order_id FROM ' . $wpdb->prefix . 'wcfm_marketplace_refund_request';
  1460.  
  1461. $sql .= ' WHERE 1=1';
  1462.  
  1463. $sql .= " AND ID = " . $refund_id;
  1464.  
  1465. $refund_infos = $wpdb->get_results( $sql );
  1466.  
  1467. if( !empty( $refund_infos ) ) {
  1468.  
  1469. foreach( $refund_infos as $refund_info ) {
  1470.  
  1471. $vendor_id = absint( $refund_info->vendor_id );
  1472.  
  1473. $order_id = absint( $refund_info->order_id );
  1474.  
  1475. $commission_id = absint( $refund_info->commission_id );
  1476.  
  1477.  
  1478.  
  1479. $order = wc_get_order( $order_id );
  1480.  
  1481.  
  1482.  
  1483. $wpdb->update("{$wpdb->prefix}wcfm_marketplace_orders", array( 'refund_status' => $status ), array('ID' => $commission_id), array('%s'), array('%d'));
  1484.  
  1485.  
  1486.  
  1487. // Vendor Notification
  1488.  
  1489. if( $vendor_id ) {
  1490.  
  1491. $wcfm_messages = sprintf( __( 'Your Refund Request cancelled for Order <b>%s</b>.', 'wc-multivendor-marketplace' ), '<a target="_blank" class="wcfm_dashboard_item_title" href="' . get_wcfm_view_order_url( $order_id ) . '">#' . $order->get_order_number() . '</a>' );
  1492.  
  1493. if( $refund_note ) {
  1494.  
  1495. $wcfm_messages .= "<br /><b>" . __( 'Note', 'wc-multivendor-marketplace' ) . "</b>: " . $refund_note;
  1496.  
  1497. }
  1498.  
  1499. $WCFM->wcfm_notification->wcfm_send_direct_message( -1, $vendor_id, 1, 0, $wcfm_messages, 'refund-request' );
  1500.  
  1501. }
  1502.  
  1503.  
  1504.  
  1505. do_action( 'wcfmmp_commission_refund_status_'.$status, $refund_id, $commission_id, $order_id );
  1506.  
  1507.  
  1508.  
  1509. // Refund Status Updated
  1510.  
  1511. $wpdb->update("{$wpdb->prefix}wcfm_marketplace_refund_request", array('refund_status' => $status, 'refund_paid_date' => date('Y-m-d H:i:s', current_time( 'timestamp', 0 ))), array('ID' => $refund_id), array('%s', '%s'), array('%d'));
  1512.  
  1513.  
  1514.  
  1515. // Ledger Status Update
  1516.  
  1517. $WCFMmp->wcfmmp_ledger->wcfmmp_ledger_status_update( $refund_id, $status, 'refund' );
  1518.  
  1519. $WCFMmp->wcfmmp_ledger->wcfmmp_ledger_status_update( $refund_id, $status, 'partial-refund' );
  1520.  
  1521.  
  1522.  
  1523. // Order Not Added
  1524.  
  1525. $wcfm_messages = sprintf( __( 'Refund Request cancelled for Order <b>%s</b>.', 'wc-multivendor-marketplace' ), '#' . $order->get_order_number() );
  1526.  
  1527. if( $refund_note ) {
  1528.  
  1529. $wcfm_messages .= "<br /><b>" . __( 'Note', 'wc-multivendor-marketplace' ) . "</b>: " . $refund_note;
  1530.  
  1531. }
  1532.  
  1533. $comment_id = $order->add_order_note( $wcfm_messages, apply_filters( 'wcfm_is_allow_refund_update_note_for_customer', '1' ) );
  1534.  
  1535. if( $vendor_id ) {
  1536.  
  1537. add_comment_meta( $comment_id, '_vendor_id', $vendor_id );
  1538.  
  1539. }
  1540.  
  1541.  
  1542.  
  1543. // Update Order Meta
  1544.  
  1545. delete_post_meta( $order_id, '_wcfm_refund_request' );
  1546.  
  1547.  
  1548.  
  1549. do_action( 'wcfmmp_refund_status_'.$status, $refund_id, $order_id );
  1550.  
  1551.  
  1552.  
  1553. return true;
  1554.  
  1555. }
  1556.  
  1557. }
  1558.  
  1559. }
  1560.  
  1561. return false;
  1562.  
  1563. }
  1564.  
  1565.  
  1566.  
  1567. /**
  1568.  
  1569. * Withdraw status update by commission status change
  1570.  
  1571. */
  1572.  
  1573. public function wcfmmp_refund_status_update_by_commission( $commission_id, $status = 'cancelled' ) {
  1574.  
  1575. global $WCFM, $WCFMmp, $wpdb;
  1576.  
  1577.  
  1578.  
  1579. if( !$commission_id ) return;
  1580.  
  1581.  
  1582.  
  1583. // Order Status Updated
  1584.  
  1585. $sql = 'SELECT ID, vendor_id, order_id FROM ' . $wpdb->prefix . 'wcfm_marketplace_refund_request';
  1586.  
  1587. $sql .= ' WHERE 1=1';
  1588.  
  1589. $sql .= " AND commission_id = " . $commission_id;
  1590.  
  1591. $refund_infos = $wpdb->get_results( $sql );
  1592.  
  1593. if( !empty( $refund_infos ) ) {
  1594.  
  1595. foreach( $refund_infos as $refund_info ) {
  1596.  
  1597. $vendor_id = absint( $refund_info->vendor_id );
  1598.  
  1599. $order_id = absint( $refund_info->order_id );
  1600.  
  1601. $refund_id = absint( $refund_info->ID );
  1602.  
  1603.  
  1604.  
  1605. $wpdb->update("{$wpdb->prefix}wcfm_marketplace_refund_request", array('refund_status' => $status, 'refund_paid_date' => date('Y-m-d H:i:s', current_time( 'timestamp', 0 ))), array('ID' => $refund_id), array('%s', '%s'), array('%d'));
  1606.  
  1607.  
  1608.  
  1609. // Ledger Status Update
  1610.  
  1611. $WCFMmp->wcfmmp_ledger->wcfmmp_ledger_status_update( $refund_id, $status, 'refund' );
  1612.  
  1613. $WCFMmp->wcfmmp_ledger->wcfmmp_ledger_status_update( $refund_id, $status, 'partial-refund' );
  1614.  
  1615.  
  1616.  
  1617. // Vendor Notification
  1618.  
  1619. $wcfm_messages = sprintf( __( 'Your Refund Request cancelled for Order <b>%s</b>.', 'wc-multivendor-marketplace' ), '<a target="_blank" class="wcfm_dashboard_item_title" href="' . get_wcfm_view_order_url( $order_id ) . '">#' . $order_id . '</a>' );
  1620.  
  1621. $WCFM->wcfm_notification->wcfm_send_direct_message( -1, $vendor_id, 1, 0, $wcfm_messages, 'refund-request' );
  1622.  
  1623.  
  1624.  
  1625. // Update Order Meta
  1626.  
  1627. delete_post_meta( $order_id, '_wcfm_refund_request' );
  1628.  
  1629.  
  1630.  
  1631. do_action( 'wcfmmp_refund_status_'.$status, $refund_id, $order_id );
  1632.  
  1633. }
  1634.  
  1635. }
  1636.  
  1637. }
  1638.  
  1639.  
  1640.  
  1641. /**
  1642.  
  1643. * Get Refund amount by Vendor
  1644.  
  1645. */
  1646.  
  1647. public function wcfm_get_refund_by_vendor( $vendor_id, $interval = '7day' ) {
  1648.  
  1649. global $wpdb, $WCFM, $WCFMmp;
  1650.  
  1651.  
  1652.  
  1653. $sql = "SELECT SUM(refunded_amount) AS total_refunded FROM {$wpdb->prefix}wcfm_marketplace_orders AS commission";
  1654.  
  1655. $sql .= " WHERE 1=1";
  1656.  
  1657. $sql .= " AND commission.vendor_id = %d";
  1658.  
  1659. $sql .= " AND commission.is_trashed != -1";
  1660.  
  1661. $sql = wcfm_query_time_range_filter( $sql, 'created', $interval, '', '', 'commission' );
  1662.  
  1663.  
  1664.  
  1665. $results = $wpdb->get_results( $wpdb->prepare( $sql, $vendor_id ) );
  1666.  
  1667. $refunded_amount = 0;
  1668.  
  1669. foreach( $results as $data ) {
  1670.  
  1671. $refunded_amount = $data->total_refunded;
  1672.  
  1673. }
  1674.  
  1675.  
  1676.  
  1677. return $refunded_amount;
  1678.  
  1679. }
  1680.  
  1681.  
  1682.  
  1683. /**
  1684.  
  1685. * WCFM My Account Refund JS
  1686.  
  1687. */
  1688.  
  1689. function wcfm_refund_scripts() {
  1690.  
  1691. global $WCFM, $WCFMmp, $wp, $WCFM_Query;
  1692.  
  1693.  
  1694.  
  1695. if( is_account_page() ) {
  1696.  
  1697. if( is_user_logged_in() ) {
  1698.  
  1699. $WCFM->library->load_blockui_lib();
  1700.  
  1701. wp_enqueue_script( 'wcfmmp_refund_requests_form_js', $WCFMmp->library->js_lib_url . 'refund/wcfmmp-script-refund-requests-popup.js', array('jquery'), $WCFMmp->version, true );
  1702.  
  1703. // Localized Script
  1704.  
  1705. $wcfm_messages = get_wcfm_refund_requests_messages();
  1706.  
  1707. wp_localize_script( 'wcfmmp_refund_requests_form_js', 'wcfm_refund_requests_messages', $wcfm_messages );
  1708.  
  1709. }
  1710.  
  1711. }
  1712.  
  1713. }
  1714.  
  1715.  
  1716.  
  1717. /**
  1718.  
  1719. * WCFM My Account Refund CSS
  1720.  
  1721. */
  1722.  
  1723. function wcfm_refund_styles() {
  1724.  
  1725. global $WCFM, $WCFMmp, $wp, $WCFM_Query;
  1726.  
  1727.  
  1728.  
  1729. if( is_account_page() ) {
  1730.  
  1731. if( is_user_logged_in() ) {
  1732.  
  1733. wp_enqueue_style( 'wcfmmp_refund_requests_form_css', $WCFMmp->library->css_lib_url . 'refund/wcfmmp-style-refund-requests-popup.css', array(), $WCFMmp->version );
  1734.  
  1735. }
  1736.  
  1737. }
  1738.  
  1739. }
  1740.  
  1741. }
Add Comment
Please, Sign In to add comment