/**
  * wcmp_get_vendor_part_from_order function to get vendor due from an order.
  * @access public
  * @param order , vendor term id 
  */
 public function wcmp_get_vendor_part_from_order($order, $vendor_term_id)
 {
     global $WCMp;
     require_once 'class-wcmp-calculate-commission.php';
     $commission_obj = new WCMp_Calculate_Commission();
     $vendor_items = $this->get_vendor_items_from_order($order->id, $vendor_term_id);
     $commission_amt = 0;
     $vendor_due = array();
     $product_value_total = 0;
     foreach ($vendor_items as $item_id => $item) {
         if (isset($item['variation_id']) && !empty($item['variation_id'])) {
             $variation_id = $item['variation_id'];
         } else {
             $variation_id = 0;
         }
         $product_id = $item['product_id'];
         if ($variation_id == 0) {
             $product_id_for_value = $product_id;
         } else {
             $product_id_for_value = $variation_id;
         }
         $product_value = get_post_meta($product_id_for_value, '_price', true);
         if (empty($product_value)) {
             $product_value = 0;
         }
         $product_value_total += $product_value * $item['qty'];
         $commission_amt = (double) $commission_amt + (double) $commission_obj->get_item_commission($product_id, $variation_id, $item, $order->id, $item_id);
         $vendor_due['commission'] = $commission_amt;
         if ($vendor_due['commission'] > $product_value_total) {
             $vendor_due['commission'] = $product_value_total;
         }
         $shipping_tax_total = $this->get_vendor_total_tax_and_shipping($order, $vendor_term_id, $item, $commission_obj);
         if (!isset($vendor_due['tax'])) {
             $vendor_due['tax'] = 0;
         }
         if ($WCMp->vendor_caps->vendor_payment_settings('give_tax')) {
             $vendor_due['tax'] += $shipping_tax_total['tax_subtotal'];
         } else {
             $vendor_due['tax'] += 0;
         }
         if (!isset($vendor_due['shipping'])) {
             $vendor_due['shipping'] = 0;
         }
         if ($WCMp->vendor_caps->vendor_payment_settings('give_shipping')) {
             $vendor_due['shipping'] += $shipping_tax_total['shipping_subtotal'];
         } else {
             $vendor_due['shipping'] += 0;
         }
         $vendor_due['total'] = (double) $commission_amt + (double) $vendor_due['shipping'] + (double) $vendor_due['tax'];
     }
     return apply_filters('vendor_due_per_order', $vendor_due, $order, $vendor_term_id);
 }
示例#2
0
    /**
     * WCMp Product Data Searching
     */
    function search_product_data()
    {
        global $WCMp;
        $product_id = $_POST['product_id'];
        $start_date = $_POST['start_date'];
        $end_date = $_POST['end_date'];
        $report_chart = $report_html = '';
        if ($product_id) {
            $is_variation = false;
            $_product = array();
            $vendor = false;
            $_product = get_product($product_id);
            if ($_product->is_type('variation')) {
                $title = $_product->get_formatted_name();
                $is_variation = true;
            } else {
                $title = $_product->get_title();
            }
            if (isset($product_id) && !$is_variation) {
                $vendor = get_wcmp_product_vendors($product_id);
            } else {
                if (isset($product_id) && $is_variation) {
                    $variatin_parent = wp_get_post_parent_id($product_id);
                    $vendor = get_wcmp_product_vendors($variatin_parent);
                }
            }
            if ($vendor) {
                $orders = array();
                if ($_product->is_type('variable')) {
                    $get_children = $_product->get_children();
                    if (!empty($get_children)) {
                        foreach ($get_children as $child) {
                            $orders = array_merge($orders, $vendor->get_vendor_orders_by_product($vendor->term_id, $child));
                        }
                        $orders = array_unique($orders);
                    }
                } else {
                    $orders = array_unique($vendor->get_vendor_orders_by_product($vendor->term_id, $product_id));
                }
            }
            $order_items = array();
            $i = 0;
            if (!empty($orders)) {
                foreach ($orders as $order_id) {
                    $order = new WC_Order($order_id);
                    $order_line_items = $order->get_items('line_item');
                    if (!empty($order_line_items)) {
                        foreach ($order_line_items as $line_item) {
                            if ($line_item['product_id'] == $product_id || $line_item['variation_id'] == $product_id) {
                                if ($_product->is_type('variation')) {
                                    $order_items_product_id = $line_item['product_id'];
                                    $order_items_variation_id = $line_item['variation_id'];
                                } else {
                                    $order_items_product_id = $line_item['product_id'];
                                    $order_items_variation_id = $line_item['variation_id'];
                                }
                                $order_date_str = strtotime($order->order_date);
                                if ($order_date_str > $start_date && $order_date_str < $end_date) {
                                    $order_items[$i] = array('order_id' => $order_id, 'product_id' => $order_items_product_id, 'variation_id' => $order_items_variation_id, 'line_total' => $line_item['line_total'], 'item_quantity' => $line_item['qty'], 'post_date' => $order->order_date, 'multiple_product' => 0);
                                    if (count($order_line_items) > 1) {
                                        $order_items[$i]['multiple_product'] = 1;
                                    }
                                    $i++;
                                }
                            }
                        }
                    }
                }
            }
            $total_sales = $admin_earnings = array();
            $max_total_sales = 0;
            if (isset($order_items) && !empty($order_items)) {
                foreach ($order_items as $order_item) {
                    if ($order_item['line_total'] == 0 && $order_item['item_quantity'] == 0) {
                        continue;
                    }
                    // Get date
                    $date = date('Ym', strtotime($order_item['post_date']));
                    if ($order_item['variation_id'] != 0) {
                        $variation_id = $order_item['variation_id'];
                        $product_id_1 = $order_item['variation_id'];
                    } else {
                        $variation_id = 0;
                        $product_id_1 = $order_item['product_id'];
                    }
                    if (!$vendor) {
                        break;
                    }
                    $vendor_earnings = 0;
                    if ($order_item['multiple_product'] == 0) {
                        $commissions = false;
                        $args = array('post_type' => 'dc_commission', 'post_status' => array('publish', 'private'), 'posts_per_page' => -1, 'meta_query' => array(array('key' => '_commission_vendor', 'value' => absint($vendor->term_id), 'compare' => '='), array('key' => '_commission_order_id', 'value' => absint($order_item['order_id']), 'compare' => '='), array('key' => '_commission_product', 'value' => absint($product_id_1), 'compare' => 'LIKE')));
                        $commissions = get_posts($args);
                        if (!empty($commissions)) {
                            foreach ($commissions as $commission) {
                                $vendor_earnings = $vendor_earnings + get_post_meta($commission->ID, '_commission_amount', true);
                            }
                        }
                    } else {
                        if ($order_item['multiple_product'] == 1) {
                            $vendor_obj = new WCMp_Vendor();
                            $vendor_items = $vendor_obj->get_vendor_items_from_order($order_item['order_id'], $vendor->term_id);
                            foreach ($vendor_items as $vendor_item) {
                                if ($variation_id == 0) {
                                    if ($vendor_item['product_id'] == $product_id) {
                                        $item = $vendor_item;
                                        break;
                                    }
                                } else {
                                    if ($vendor_item['variation_id'] == $variation_id) {
                                        $item = $vendor_item;
                                        break;
                                    }
                                }
                            }
                            if (!$is_variation) {
                                $commission_obj = new WCMp_Calculate_Commission();
                                $vendor_earnings = $commission_obj->get_item_commission($product_id, $variation_id, $item, $order_item['order_id']);
                            } else {
                                $commission_obj = new WCMp_Calculate_Commission();
                                $vendor_earnings = $commission_obj->get_item_commission($variatin_parent, $variation_id, $item, $order_item['order_id']);
                            }
                        }
                    }
                    $total_sales[$date] = isset($total_sales[$date]) ? $total_sales[$date] + $order_item['line_total'] : $order_item['line_total'];
                    $admin_earnings[$date] = isset($admin_earnings[$date]) ? $admin_earnings[$date] + $order_item['line_total'] - $vendor_earnings : $order_item['line_total'] - $vendor_earnings;
                    if ($total_sales[$date] > $max_total_sales) {
                        $max_total_sales = $total_sales[$date];
                    }
                }
            }
            if (sizeof($total_sales) > 0) {
                foreach ($total_sales as $date => $sales) {
                    $width = $sales > 0 ? round($sales) / round($max_total_sales) * 100 : 0;
                    $width2 = $admin_earnings[$date] > 0 ? round($admin_earnings[$date]) / round($max_total_sales) * 100 : 0;
                    $report_chart .= '<tr><th>' . date_i18n('F', strtotime($date . '01')) . '</th>
						<td width="1%"><span>' . woocommerce_price($sales) . '</span><span class="alt">' . woocommerce_price($admin_earnings[$date]) . '</span></td>
						<td class="bars">
							<span style="width:' . esc_attr($width) . '%">&nbsp;</span>
							<span class="alt" style="width:' . esc_attr($width2) . '%">&nbsp;</span>
						</td></tr>';
                }
                $report_html = '
					<h4>' . __("Sales and Earnings", $WCMp->text_domain) . '</h4>
					<div class="bar_indecator">
						<div class="bar1">&nbsp;</div>
						<span class="">' . __("Gross Sales", $WCMp->text_domain) . '</span>
						<div class="bar2">&nbsp;</div>
						<span class="">' . __("My Earnings", $WCMp->text_domain) . '</span>
					</div>
					<table class="bar_chart">
						<thead>
							<tr>
								<th>' . __("Month", $WCMp->text_domain) . '</th>
								<th colspan="2">' . __("Sales Report", $WCMp->text_domain) . '</th>
							</tr>
						</thead>
						<tbody>
							' . $report_chart . '
						</tbody>
					</table>
				';
            } else {
                $report_html = '<tr><td colspan="3">' . __('This product was not sold in the given period.', $WCMp->text_domain) . '</td></tr>';
            }
            echo $report_html;
        } else {
            echo '<tr><td colspan="3">' . __('Please select a product.', $WCMp->text_domain) . '</td></tr>';
        }
        die;
    }
示例#3
0
 /**
  * On activation, include the installer and run it.
  *
  * @access public
  * @return void
  */
 function wcmp_plugins_loaded()
 {
     global $WCMp, $wpdb;
     //delete_option('dc_product_vendor_plugin_db_version');
     $previous_plugin_version = get_option('dc_product_vendor_plugin_db_version');
     if (!$previous_plugin_version || $previous_plugin_version < $WCMp->version) {
         $prev_general = get_option('dc_general_settings_name');
         $prev_product = get_option('dc_product_settings_name');
         $prev_capability = get_option('dc_capabilities_settings_name');
         $prev_pages = get_option('dc_pages_settings_name');
         $prev_payment = get_option('dc_payment_settings_name');
         $new_general = $new_product = $new_capability = $new_pages = $new_payment = $new_frontend = array();
         $new_payment = $prev_payment;
         if (!empty($prev_general)) {
             if (isset($prev_general['enable_registration'])) {
                 $new_general['enable_registration'] = 'Enable';
             }
             if (isset($prev_general['approve_vendor_manually'])) {
                 $new_general['approve_vendor_manually'] = 'Enable';
             }
             if (isset($prev_general['notify_configure_vendor_store'])) {
                 $new_general['notify_configure_vendor_store'] = $prev_general['notify_configure_vendor_store'];
             }
             if (isset($prev_general['default_commission'])) {
                 $new_payment['default_commission'] = $prev_general['default_commission'];
             }
             if (isset($prev_general['commission_type'])) {
                 $new_payment['commission_type'] = $prev_general['commission_type'];
             }
             if (isset($prev_general['commission_include_coupon'])) {
                 $new_payment['commission_include_coupon'] = $prev_general['commission_include_coupon'];
             }
             if (isset($prev_general['sold_by_catalog'])) {
                 $new_frontend['sold_by_catalog'] = $prev_general['sold_by_catalog'];
             }
             if (isset($prev_general['catalog_colorpicker'])) {
                 $new_frontend['catalog_colorpicker'] = $prev_general['catalog_colorpicker'];
             }
             if (isset($prev_general['catalog_hover_colorpicker'])) {
                 $new_frontend['catalog_hover_colorpicker'] = $prev_general['catalog_hover_colorpicker'];
             }
             if (isset($prev_general['sold_by_cart_and_checkout'])) {
                 $new_frontend['sold_by_cart_and_checkout'] = $prev_general['sold_by_cart_and_checkout'];
             }
             if (isset($prev_general['sold_by_text'])) {
                 $new_frontend['sold_by_text'] = $prev_general['sold_by_text'];
             }
             if (isset($prev_general['block_vendor_desc'])) {
                 $new_frontend['block_vendor_desc'] = $prev_general['block_vendor_desc'];
             }
         }
         if (!empty($prev_capability)) {
             $new_capability = $prev_capability;
             if (isset($new_capability['give_tax'])) {
                 $new_payment['give_tax'] = $new_capability['give_tax'];
                 unset($new_capability['give_tax']);
             }
             if (isset($new_capability['give_shipping'])) {
                 $new_payment['give_shipping'] = $new_capability['give_shipping'];
                 unset($new_capability['give_shipping']);
             }
         }
         if (!empty($prev_product)) {
             update_option('wcmp_product_settings_name', $prev_product);
         }
         if (!empty($prev_pages)) {
             update_option('wcmp_pages_settings_name', $prev_pages);
         }
         if (!empty($new_general)) {
             update_option('wcmp_general_settings_name', $new_general);
         }
         if (!empty($new_capability)) {
             update_option('wcmp_capabilities_settings_name', $new_capability);
         }
         if (!empty($new_payment)) {
             update_option('wcmp_payment_settings_name', $new_payment);
         }
         if (!empty($new_frontend)) {
             update_option('wcmp_frontend_settings_name', $new_frontend);
         }
         delete_option('dc_general_settings_name');
         delete_option('dc_product_settings_name');
         delete_option('dc_capabilities_settings_name');
         delete_option('dc_payment_settings_name');
         delete_option('dc_pages_settings_name');
         $vendors = get_wcmp_vendors();
         if (!empty($vendors)) {
             foreach ($vendors as $vendor) {
                 $vendorusers = new WP_User($vendor->id);
                 $vendorusers->remove_cap('manage_woocommerce');
                 $vendor_products = $vendor->get_products();
                 if (!empty($vendor_products)) {
                     foreach ($vendor_products as $vendor_product) {
                         wp_update_post(array('ID' => $vendor_product->ID, 'post_author' => $vendor->id));
                         $product_obj = wc_get_product($vendor_product->ID);
                         if ($product_obj->is_type('variable')) {
                             $childrens = $product_obj->get_children();
                             foreach ($childrens as $child_id) {
                                 wp_update_post(array('ID' => $child_id, 'post_author' => $vendor->id));
                             }
                         }
                     }
                 }
             }
         }
         $vendor_role = get_role('dc_vendor');
         $vendor_role->remove_cap('manage_woocommerce');
         $wcmp_pages = get_option('wcmp_pages_settings_name');
         $page_slug = 'wcmp_withdrawal_request';
         $page_found = $wpdb->get_var("SELECT ID FROM " . $wpdb->posts . " WHERE post_name = '{$page_slug}' LIMIT 1;");
         if (!$page_found) {
             $page_data = array('post_status' => 'publish', 'post_type' => 'page', 'post_author' => 1, 'post_name' => $page_slug, 'post_title' => __('Withdrawal Request Status', $WCMp->text_domain), 'post_content' => '[transaction_thankyou]', 'comment_status' => 'closed');
             $transaction_withdrawal_page_id = wp_insert_post($page_data);
             update_option('wcmp_product_vendor_transaction_widthdrawal_page_id', $transaction_withdrawal_page_id);
             $wcmp_pages['vendor_transaction_thankyou'] = $transaction_withdrawal_page_id;
         }
         $page_slug = 'wcmp_transaction_details';
         $page_foundd = $wpdb->get_var("SELECT ID FROM " . $wpdb->posts . " WHERE post_name = '{$page_slug}' LIMIT 1;");
         if (!$page_foundd) {
             $page_data = array('post_status' => 'publish', 'post_type' => 'page', 'post_author' => 1, 'post_name' => $page_slug, 'post_title' => __('Transaction Details', $WCMp->text_domain), 'post_content' => '[transaction_details]', 'comment_status' => 'closed');
             $transaction_details_page_id = wp_insert_post($page_data);
             update_option('wcmp_product_vendor_transaction_details_page_id', $transaction_details_page_id);
             $wcmp_pages['vendor_transaction_detail'] = $transaction_details_page_id;
         }
         $page_slug = 'wcmp_vendor_policies';
         $page_foundd = $wpdb->get_var("SELECT ID FROM " . $wpdb->posts . " WHERE post_name = '{$page_slug}' LIMIT 1;");
         if (!$page_foundd) {
             $page_data = array('post_status' => 'publish', 'post_type' => 'page', 'post_author' => 1, 'post_name' => $page_slug, 'post_title' => __('Vendor Policies', $WCMp->text_domain), 'post_content' => '[vendor_policies]', 'comment_status' => 'closed');
             $policy_page_id = wp_insert_post($page_data);
             update_option('wcmp_product_vendor_policies_page_id', $policy_page_id);
             $wcmp_pages['vendor_policies'] = $policy_page_id;
         }
         $page_slug = 'wcmp_vendor_billing';
         $page_foundd = $wpdb->get_var("SELECT ID FROM " . $wpdb->posts . " WHERE post_name = '{$page_slug}' LIMIT 1;");
         if (!$page_foundd) {
             $page_data = array('post_status' => 'publish', 'post_type' => 'page', 'post_author' => 1, 'post_name' => $page_slug, 'post_title' => __('Vendor Billing', $WCMp->text_domain), 'post_content' => '[vendor_billing]', 'comment_status' => 'closed');
             $vendor_billing_page_id = wp_insert_post($page_data);
             update_option('wcmp_product_vendor_billing_page_id', $vendor_billing_page_id);
             $wcmp_pages['vendor_billing'] = $vendor_billing_page_id;
         }
         $page_slug = 'wcmp_vendor_shipping';
         $page_foundd = $wpdb->get_var("SELECT ID FROM " . $wpdb->posts . " WHERE post_name = '{$page_slug}' LIMIT 1;");
         if (!$page_foundd) {
             $page_data = array('post_status' => 'publish', 'post_type' => 'page', 'post_author' => 1, 'post_name' => $page_slug, 'post_title' => __('Vendor Shipping', $WCMp->text_domain), 'post_content' => '[vendor_shipping_settings]', 'comment_status' => 'closed');
             $vendor_shipping_page_id = wp_insert_post($page_data);
             update_option('wcmp_product_vendor_shipping_page_id', $vendor_shipping_page_id);
             $wcmp_pages['vendor_shipping'] = $vendor_shipping_page_id;
         }
         $page_slug = 'wcmp_vendor_report';
         $page_foundd = $wpdb->get_var("SELECT ID FROM " . $wpdb->posts . " WHERE post_name = '{$page_slug}' LIMIT 1;");
         if (!$page_foundd) {
             $page_data = array('post_status' => 'publish', 'post_type' => 'page', 'post_author' => 1, 'post_name' => $page_slug, 'post_title' => __('Vendor Report', $WCMp->text_domain), 'post_content' => '[vendor_report]', 'comment_status' => 'closed');
             $vendor_report_page_id = wp_insert_post($page_data);
             update_option('wcmp_product_vendor_report_page_id', $vendor_report_page_id);
             $wcmp_pages['vendor_report'] = $vendor_report_page_id;
         }
         $page_slug = 'wcmp_vendor_widthdrawals';
         $page_foundd = $wpdb->get_var("SELECT ID FROM " . $wpdb->posts . " WHERE post_name = '{$page_slug}' LIMIT 1;");
         if (!$page_foundd) {
             $page_data = array('post_status' => 'publish', 'post_type' => 'page', 'post_author' => 1, 'post_name' => $page_slug, 'post_title' => __('Vendor Widthdrawals', $WCMp->text_domain), 'post_content' => '[vendor_widthdrawals]', 'comment_status' => 'closed');
             $vendor_widthdrawals_page_id = wp_insert_post($page_data);
             update_option('wcmp_product_vendor_widthdrawals_page_id', $vendor_widthdrawals_page_id);
             $wcmp_pages['vendor_widthdrawals'] = $vendor_widthdrawals_page_id;
         }
         $page_slug = 'wcmp_vendor_university';
         $page_foundd = $wpdb->get_var("SELECT ID FROM " . $wpdb->posts . " WHERE post_name = '{$page_slug}' LIMIT 1;");
         if (!$page_foundd) {
             $page_data = array('post_status' => 'publish', 'post_type' => 'page', 'post_author' => 1, 'post_name' => $page_slug, 'post_title' => __('Vendor University', $WCMp->text_domain), 'post_content' => '[vendor_university]', 'comment_status' => 'closed');
             $vendor_university_page_id = wp_insert_post($page_data);
             update_option('wcmp_product_vendor_university_page_id', $vendor_university_page_id);
             $wcmp_pages['vendor_university'] = $vendor_university_page_id;
         }
         $page_slug = 'wcmp_vendor_announcements';
         $page_foundd = $wpdb->get_var("SELECT ID FROM " . $wpdb->posts . " WHERE post_name = 'wcmp_vendor_messages' LIMIT 1;");
         $page_foundd2 = $wpdb->get_var("SELECT ID FROM " . $wpdb->posts . " WHERE post_name = 'wcmp_vendor_announcements' LIMIT 1;");
         if (!$page_foundd && !$page_foundd2) {
             $page_data = array('post_status' => 'publish', 'post_type' => 'page', 'post_author' => 1, 'post_name' => $page_slug, 'post_title' => __('Vendor Announcements', $WCMp->text_domain), 'post_content' => '[vendor_announcements]', 'comment_status' => 'closed');
             $vendor_announcements_page_id = wp_insert_post($page_data);
             update_option('wcmp_product_vendor_announcements_page_id', $vendor_announcements_page_id);
             $wcmp_pages['vendor_announcements'] = $vendor_announcements_page_id;
         }
         if ($page_foundd && !$page_foundd2) {
             wp_update_post(array('ID' => $wcmp_pages['vendor_messages'], 'post_content' => '[vendor_announcements]', 'post_name' => 'vendor_announcements', 'post_title' => 'Vendor Announcements'));
             $wcmp_pages['vendor_announcements'] = $wcmp_pages['vendor_messages'];
             unset($wcmp_pages['vendor_messages']);
         }
         wp_update_post(array('ID' => $wcmp_pages['vendor_dashboard'], 'post_content' => '[vendor_dashboard]'));
         wp_update_post(array('ID' => $wcmp_pages['view_order'], 'post_content' => '[vendor_orders]'));
         update_option('wcmp_pages_settings_name', $wcmp_pages);
         if (!empty($wpdb->charset)) {
             $charset_collate = "DEFAULT CHARACTER SET {$wpdb->charset}";
         }
         if (!empty($wpdb->collate)) {
             $charset_collate .= " COLLATE {$wpdb->collate}";
         }
         $migs = array();
         // Create course_purchase table
         $migs[] = "\n\t\t\t\tCREATE TABLE IF NOT EXISTS `" . $wpdb->prefix . "wcmp_vendor_orders` (\n\t\t\t\t`ID` bigint(20) NOT NULL AUTO_INCREMENT,\n\t\t\t\t`order_id` bigint(20) NOT NULL,\n\t\t\t\t`commission_id` bigint(20) NOT NULL,\n\t\t\t\t`vendor_id` bigint(20) NOT NULL,\n\t\t\t\t`shipping_status` varchar(255) NOT NULL,\n\t\t\t\t`order_item_id` bigint(20) NOT NULL,\n\t\t\t\t`product_id` bigint(20) NOT NULL,\n\t\t\t\t`commission_amount` varchar(255) NOT NULL,\n\t\t\t\t`shipping` varchar(255) NOT NULL,\n\t\t\t\t`tax` varchar(255) NOT NULL,\n\t\t\t\t`is_trashed` varchar(10) NOT NULL,\t\t\t\t\n\t\t\t\t`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,\t\t\t\t\n\t\t\t\tPRIMARY KEY (`ID`),\n\t\t\t\tCONSTRAINT vendor_orders UNIQUE (order_id, vendor_id, commission_id, product_id)\n\t\t\t){$charset_collate};";
         $needed_migration = count($migs);
         for ($i = 0; $i < $needed_migration; $i++) {
             $mig = $migs[$i];
             $wpdb->query($mig);
         }
         $WCMp_Calculate_Commission_obj = new WCMp_Calculate_Commission();
         $vendors = get_wcmp_vendors();
         if (!empty($vendors)) {
             $vendor_orders_array = array();
             foreach ($vendors as $vendor) {
                 $vendor_orders = $vendor->get_orders();
                 if (!empty($vendor_orders)) {
                     foreach ($vendor_orders as $commission_id => $order_id) {
                         $vendor_shipping_array = get_post_meta($order_id, 'dc_pv_shipped', true);
                         $order = new WC_Order($order_id);
                         $commission_array = array();
                         $mark_ship = false;
                         $items = $order->get_items('line_item');
                         foreach ($items as $order_item_id => $item) {
                             $comm_pro_id = $product_id = $order->get_item_meta($order_item_id, '_product_id', true);
                             $variation_id = $order->get_item_meta($order_item_id, '_variation_id', true);
                             if ($variation_id) {
                                 $comm_pro_id = $variation_id;
                             }
                             if ($product_id) {
                                 $product_vendors = get_wcmp_product_vendors($product_id);
                                 if ($product_vendors) {
                                     if (isset($product_vendors->id)) {
                                         if (isset($vendor_shipping_array) && !empty($vendor_shipping_array)) {
                                             if (in_array($product_vendors->id, $vendor_shipping_array)) {
                                                 $mark_ship = true;
                                             } else {
                                                 $mark_ship = 0;
                                             }
                                         } else {
                                             $mark_ship = 0;
                                         }
                                         $item_commission = $WCMp_Calculate_Commission_obj->get_item_commission($comm_pro_id, $comm_pro_id, $item, $order_id, $order_item_id);
                                         $vendor_shipping_costs = $vendor->get_wcmp_vendor_shipping_total($order_id, $item);
                                         $item_shipping = $vendor_shipping_costs['shipping_amount'] + $vendor_shipping_costs['shipping_tax'];
                                         $item_tax = get_metadata('order_item', $order_item_id, '_line_tax', true);
                                         $commission_vendor_term_id = get_post_meta($commission_id, '_commission_vendor', true);
                                         $vendor_term_id = get_user_meta($product_vendors->id, '_vendor_term_id', true);
                                         if ($commission_vendor_term_id == $vendor_term_id) {
                                             $vendor_orders_array[] = array($order_id, $commission_id, $product_vendors->id, $mark_ship, $order_item_id, $comm_pro_id, $order->order_date, $item_commission, $item_shipping, $item_tax);
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
             if (!empty($vendor_orders_array)) {
                 usort($vendor_orders_array, function ($a, $b) {
                     return $a[0] - $b[0];
                 });
                 if (!get_option('wcmp_vendor_orders_update')) {
                     foreach ($vendor_orders_array as $vendor_orders) {
                         $insert_query = $wpdb->query($wpdb->prepare("INSERT INTO `{$wpdb->prefix}wcmp_vendor_orders` ( order_id, commission_id, vendor_id, shipping_status, order_item_id, product_id, created, commission_amount, shipping, tax )\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t VALUES\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t ( %d, %d, %d, %s, %d, %d, %s, %s, %s, %s )", $vendor_orders[0], $vendor_orders[1], $vendor_orders[2], $vendor_orders[3], $vendor_orders[4], $vendor_orders[5], $vendor_orders[6], $vendor_orders[7], $vendor_orders[8], $vendor_orders[9]));
                     }
                 }
                 update_option('wcmp_vendor_orders_update', 1);
             }
         }
         update_option('dc_product_vendor_plugin_db_version', $WCMp->version);
     }
 }
    /**
     * Output the report
     */
    public function output_report()
    {
        global $wpdb, $woocommerce, $WCMp;
        $ranges = array('year' => __('Year', $WCMp->text_domain), 'last_month' => __('Last Month', $WCMp->text_domain), 'month' => __('This Month', $WCMp->text_domain), '7day' => __('Last 7 Days', $WCMp->text_domain));
        $current_range = !empty($_GET['range']) ? sanitize_text_field($_GET['range']) : '7day';
        if (!in_array($current_range, array('custom', 'year', 'last_month', 'month', '7day'))) {
            $current_range = '7day';
        }
        $this->calculate_current_range($current_range);
        if (isset($_POST['search_product'])) {
            $is_variation = false;
            $product_id = $_POST['search_product'];
            $_product = get_product($product_id);
            if ($_product->is_type('variation')) {
                $title = $_product->get_formatted_name();
                $is_variation = true;
            } else {
                $title = $_product->get_title();
            }
        }
        if (isset($product_id)) {
            $option = '<option value="' . $product_id . '" selected="selected">' . $title . '</option>';
        } else {
            $option = '<option></option>';
        }
        $start_date = $this->start_date;
        $end_date = $this->end_date;
        $end_date = strtotime('+1 day', $end_date);
        $products = $product_ids = array();
        $vendor = false;
        $current_user_id = '';
        $current_user_id = get_current_user_id();
        $vendor = get_wcmp_vendor($current_user_id);
        if ($vendor) {
            $products = $vendor->get_products();
            foreach ($products as $product) {
                $product_ids[] = $product->ID;
            }
        } else {
            $args = array('posts_per_page' => -1, 'offset' => 0, 'orderby' => 'date', 'order' => 'DESC', 'post_type' => 'product', 'post_status' => 'publish');
            $products = get_posts($args);
            foreach ($products as $product) {
                $product_ids[] = $product->ID;
            }
        }
        $total_sales = $admin_earnings = array();
        $max_total_sales = $index = 0;
        $product_report = $report_bk = array();
        if (isset($product_ids) && !empty($product_ids)) {
            foreach ($product_ids as $product_id) {
                $is_variation = false;
                $_product = array();
                $vendor = false;
                $_product = get_product($product_id);
                if ($_product->is_type('variation')) {
                    $title = $_product->get_formatted_name();
                    $is_variation = true;
                } else {
                    $title = $_product->get_title();
                }
                if (isset($product_id) && !$is_variation) {
                    $vendor = get_wcmp_product_vendors($product_id);
                } else {
                    if (isset($product_id) && $is_variation) {
                        $variation_parent = wp_get_post_parent_id($product_id);
                        $vendor = get_wcmp_product_vendors($variation_parent);
                    }
                }
                if ($vendor) {
                    $orders = array();
                    if ($_product->is_type('variable')) {
                        $get_children = $_product->get_children();
                        if (!empty($get_children)) {
                            foreach ($get_children as $child) {
                                $orders = array_merge($orders, $vendor->get_vendor_orders_by_product($vendor->term_id, $child));
                            }
                            $orders = array_unique($orders);
                        }
                    } else {
                        $orders = array_unique($vendor->get_vendor_orders_by_product($vendor->term_id, $product_id));
                    }
                }
                $order_items = array();
                $i = 0;
                if (!empty($orders)) {
                    foreach ($orders as $order_id) {
                        $order = new WC_Order($order_id);
                        $order_line_items = $order->get_items('line_item');
                        if (!empty($order_line_items)) {
                            foreach ($order_line_items as $line_item) {
                                if ($line_item['product_id'] == $product_id) {
                                    if ($_product->is_type('variation')) {
                                        $order_items_product_id = $line_item['product_id'];
                                        $order_items_variation_id = $line_item['variation_id'];
                                    } else {
                                        $order_items_product_id = $line_item['product_id'];
                                        $order_items_variation_id = $line_item['variation_id'];
                                    }
                                    $order_date_str = strtotime($order->order_date);
                                    if ($order_date_str > $start_date && $order_date_str < $end_date) {
                                        $order_items[$i] = array('order_id' => $order_id, 'product_id' => $order_items_product_id, 'variation_id' => $order_items_variation_id, 'line_total' => $line_item['line_total'], 'item_quantity' => $line_item['qty'], 'post_date' => $order->order_date, 'multiple_product' => 0);
                                        if (count($order_line_items) > 1) {
                                            $order_items[$i]['multiple_product'] = 1;
                                        }
                                        $i++;
                                    }
                                }
                            }
                        }
                    }
                }
                if (isset($order_items) && !empty($order_items)) {
                    foreach ($order_items as $item_id => $order_item) {
                        if ($order_item['line_total'] == 0 && $order_item['item_quantity'] == 0) {
                            continue;
                        }
                        if ($order_item['variation_id'] != 0) {
                            $variation_id = $order_item['variation_id'];
                            $product_id_1 = $order_item['variation_id'];
                        } else {
                            $variation_id = 0;
                            $product_id_1 = $order_item['product_id'];
                        }
                        $vendor = get_wcmp_product_vendors($product_id);
                        if (!$vendor) {
                            break;
                        }
                        $commissions = false;
                        $vendor_earnings = 0;
                        if ($order_item['multiple_product'] == 0) {
                            $args = array('post_type' => 'dc_commission', 'post_status' => array('publish', 'private'), 'posts_per_page' => -1, 'meta_query' => array(array('key' => '_commission_vendor', 'value' => absint($vendor->term_id), 'compare' => '='), array('key' => '_commission_order_id', 'value' => absint($order_item['order_id']), 'compare' => '='), array('key' => '_commission_product', 'value' => absint($product_id_1), 'compare' => 'LIKE')));
                            $commissions = get_posts($args);
                            if (!empty($commissions)) {
                                foreach ($commissions as $item_id => $commission) {
                                    $vendor_earnings = $vendor_earnings + get_post_meta($commission->ID, '_commission_amount', true);
                                }
                            }
                        } else {
                            if ($order_item['multiple_product'] == 1) {
                                $vendor_obj = new WCMp_Vendor();
                                $vendor_items = $vendor_obj->get_vendor_items_from_order($order_item['order_id'], $vendor->term_id);
                                foreach ($vendor_items as $vendor_item) {
                                    if ($variation_id == 0) {
                                        if ($vendor_item['product_id'] == $product_id) {
                                            $item = $vendor_item;
                                            break;
                                        }
                                    } else {
                                        if ($vendor_item['product_id'] == $product_id && $vendor_item['variation_id'] == $variation_id) {
                                            $item = $vendor_item;
                                            break;
                                        }
                                    }
                                }
                                $commission_obj = new WCMp_Calculate_Commission();
                                $vendor_earnings = $commission_obj->get_item_commission($product_id, $variation_id, $item, $order_item['order_id'], $item_id);
                            }
                        }
                        if ($vendor_earnings <= 0) {
                            continue;
                        }
                        $total_sales[$product_id] = isset($total_sales[$product_id]) ? $total_sales[$product_id] + $order_item['line_total'] : $order_item['line_total'];
                        $vendor = get_wcmp_vendor($current_user_id);
                        if (!$vendor) {
                            $admin_earnings[$product_id] = isset($admin_earnings[$product_id]) ? $admin_earnings[$product_id] + $order_item['line_total'] - $vendor_earnings : $order_item['line_total'] - $vendor_earnings;
                        } else {
                            $admin_earnings[$product_id] = isset($admin_earnings[$product_id]) ? $admin_earnings[$product_id] + $vendor_earnings : $vendor_earnings;
                        }
                        if ($total_sales[$product_id] > $max_total_sales) {
                            $max_total_sales = $total_sales[$product_id];
                        }
                    }
                }
                if (!empty($total_sales[$product_id]) && !empty($admin_earnings[$product_id])) {
                    $product_report[$index]['product_id'] = $product_id;
                    $product_report[$index]['total_sales'] = $total_sales[$product_id];
                    $product_report[$index++]['admin_earning'] = $admin_earnings[$product_id];
                    $report_bk[$product_id]['total_sales'] = $total_sales[$product_id];
                    $report_bk[$product_id]['admin_earning'] = $admin_earnings[$product_id];
                }
            }
            $i = 0;
            $max_value = 10;
            $report_sort_arr = array();
            $total_sales_sort = $admin_earning_sort = array();
            if (!empty($product_report) && !empty($report_bk)) {
                $total_sales_sort = wp_list_pluck($product_report, 'total_sales', 'product_id');
                $admin_earning_sort = wp_list_pluck($product_report, 'admin_earning', 'product_id');
                foreach ($total_sales_sort as $key => $value) {
                    $total_sales_sort_arr[$key]['total_sales'] = $report_bk[$key]['total_sales'];
                    $total_sales_sort_arr[$key]['admin_earning'] = $report_bk[$key]['admin_earning'];
                }
                arsort($total_sales_sort);
                foreach ($total_sales_sort as $product_id => $value) {
                    if ($i++ < $max_value) {
                        $report_sort_arr[$product_id]['total_sales'] = $report_bk[$product_id]['total_sales'];
                        $report_sort_arr[$product_id]['admin_earning'] = $report_bk[$product_id]['admin_earning'];
                    }
                }
            }
            wp_localize_script('wcmp_report_js', 'wcmp_report_product', array('product_report' => $product_report, 'report_bk' => $report_bk, 'total_sales_sort' => $total_sales_sort, 'admin_earning_sort' => $admin_earning_sort, 'max_total_sales' => $max_total_sales, 'start_date' => $start_date, 'end_date' => $end_date));
            $report_chart = $report_html = '';
            if (sizeof($report_sort_arr) > 0) {
                foreach ($report_sort_arr as $product_id => $sales_report) {
                    $width = $sales_report['total_sales'] > 0 ? round($sales_report['total_sales']) / round($max_total_sales) * 100 : 0;
                    $width2 = $sales_report['admin_earning'] > 0 ? round($sales_report['admin_earning']) / round($max_total_sales) * 100 : 0;
                    $product = new WC_Product($product_id);
                    $product_url = admin_url('post.php?post=' . $product_id . '&action=edit');
                    $report_chart .= '<tr><th><a href="' . $product_url . '">' . $product->get_title() . '</a></th>
						<td width="1%"><span>' . woocommerce_price($sales_report['total_sales']) . '</span><span class="alt">' . woocommerce_price($sales_report['admin_earning']) . '</span></td>
						<td class="bars">
							<span style="width:' . esc_attr($width) . '%">&nbsp;</span>
							<span class="alt" style="width:' . esc_attr($width2) . '%">&nbsp;</span>
						</td></tr>';
                }
                $report_html = '
					<h4>' . __("Sales and Earnings", $WCMp->text_domain) . '</h4>
					<div class="bar_indecator">
						<div class="bar1">&nbsp;</div>
						<span class="">' . __("Gross Sales", $WCMp->text_domain) . '</span>
						<div class="bar2">&nbsp;</div>
						<span class="">' . __("My Earnings", $WCMp->text_domain) . '</span>
					</div>
					<table class="bar_chart">
						<thead>
							<tr>
								<th>' . __("Month", $WCMp->text_domain) . '</th>
								<th colspan="2">' . __("Sales Report", $WCMp->text_domain) . '</th>
							</tr>
						</thead>
						<tbody>
							' . $report_chart . '
						</tbody>
					</table>
				';
            } else {
                $report_html = '<tr><td colspan="3">' . __('No product was sold in the given period.', $WCMp->text_domain) . '</td></tr>';
            }
        } else {
            $report_html = '<tr><td colspan="3">' . __('Your store has no products.', $WCMp->text_domain) . '</td></tr>';
        }
        include $WCMp->plugin_path . '/classes/reports/views/html-wcmp-report-by-product.php';
    }