/** * * * @param unknown $order * @param unknown $group (optional) * * @return unknown */ public function get_vendor_dues_from_order($order, $group = true) { global $woocommerce; $give_tax = Product_Vendor::$pv_options->get_option('give_tax'); $receiver = array(); $shipping_given = 0; $tax_given = 0; foreach ($order->get_items() as $key => $product) { $product_id = !empty($product['variation_id']) ? $product['variation_id'] : $product['product_id']; $author = PV_Vendors::get_vendor_from_product($product_id); $is_vendor = PV_Vendors::is_vendor($author); $commission = $is_vendor ? PV_Commission::calculate_commission($product['line_subtotal'], $product_id, $order) : 0; $tax = !empty($product['line_tax']) ? (double) $product['line_tax'] : 0; $shipping = PV_Shipping::get_shipping_due($order->id, $product, $author); if ($is_vendor) { $shipping_given += $shipping; $tax_given += $give_tax ? $tax : 0; $give = 0; $give += !empty($receiver[$author]['total']) ? $receiver[$author]['total'] : 0; $give += $shipping; $give += $commission; $give += $give_tax ? $tax : 0; if ($group) { $receiver[$author] = array('vendor_id' => (int) $author, 'commission' => !empty($receiver[$author]['commission']) ? $receiver[$author]['commission'] + $commission : $commission, 'shipping' => !empty($receiver[$author]['shipping']) ? $receiver[$author]['shipping'] + $shipping : $shipping, 'tax' => $give_tax ? !empty($receiver[$author]['tax']) ? $receiver[$author]['tax'] + $tax : $tax : 0, 'qty' => !empty($receiver[$author]['qty']) ? $receiver[$author]['qty'] + $product['qty'] : $product['qty'], 'total' => $give); } else { $receiver[$author][$product_id] = array('vendor_id' => (int) $author, 'product_id' => $product_id, 'commission' => $commission, 'shipping' => $shipping, 'tax' => $give_tax ? $tax : 0, 'qty' => $product['qty'], 'total' => $shipping + $commission + ($give_tax ? $tax : 0)); } } $admin_comm = $product['line_subtotal'] - $commission; if ($group) { $receiver[1] = array('vendor_id' => 1, 'qty' => !empty($receiver[1]['qty']) ? $receiver[1]['qty'] + $product['qty'] : $product['qty'], 'commission' => !empty($receiver[1]['commission']) ? $receiver[1]['commission'] + $admin_comm : $admin_comm, 'total' => !empty($receiver[1]) ? $receiver[1]['total'] + $admin_comm : $admin_comm); } else { $receiver[1][$product_id] = array('vendor_id' => 1, 'product_id' => $product_id, 'commission' => $admin_comm, 'shipping' => 0, 'tax' => 0, 'qty' => $product['qty'], 'total' => $admin_comm); } } // Add remainders on end to admin $discount = $order->get_total_discount(); $shipping = $order->order_shipping - $shipping_given; $tax = $order->order_tax + $order->order_shipping_tax - $tax_given; $total = $tax + $shipping - $discount; if ($group) { $receiver[1]['commission'] = $receiver[1]['commission'] - $discount; $receiver[1]['shipping'] = $shipping; $receiver[1]['tax'] = $tax; $receiver[1]['total'] += $total; } else { $receiver[1][$product_id]['commission'] = $receiver[1][$product_id]['commission'] - $discount; $receiver[1][$product_id]['shipping'] = $order->order_shipping - $shipping_given; $receiver[1][$product_id]['tax'] = $tax; $receiver[1][$product_id]['total'] += $total; } // Reset the array keys // $receivers = array_values( $receiver ); return $receiver; }
/** * Delete due commission for a vendor. * * @param array $users * * @return unknown */ private function purge_user_meta($users) { global $wpdb; $paid = PV_Commission::set_order_commission_paid($this->orders_paid, true); if ($paid) { foreach ($users as $user) { delete_user_meta($user['user_id'], 'pv_total_due'); } return true; } return false; }
/** * Filter products based on current vendor * * @param unknown $orders * * @return unknown */ public function filter_products($orders) { $products = PV_Vendors::get_vendor_products($this->vendor_id); $ids = array(); foreach ($products as $product) { $ids[] = $product->ID; } foreach ($orders as $key => $order) { if (!in_array($order->product_id, $ids)) { unset($orders[$key]); continue; } else { if (!empty($order->line_total)) { $orders[$key]->line_total = PV_Commission::calculate_commission($order->line_total, $order->product_id, $order); } } } return $orders; }
/** * Commission due for a product based on a rate and price * * @param float $product_price * @param unknown $product_id * * @return float */ public function calculate_commission($product_price, $product_id, $order) { $commission_rate = PV_Commission::get_commission_rate($product_id); $commission = $product_price * ($commission_rate / 100); $commission = round($commission, 2); return apply_filters('pv_commission_rate', $commission, $product_id, $product_price, $order); }
/** * * * @param unknown $items * @param unknown $order * * @return unknown */ public function check_items($items, $order) { foreach ($items as $key => $product) { if (empty($product['product_id'])) { unset($items[$key]); continue; } $author = PV_Vendors::get_vendor_from_product($product['product_id']); if ($this->current_vendor != $author) { unset($items[$key]); continue; } else { $commission_due = PV_Commission::calculate_commission($product['line_subtotal'], $product['product_id'], $order); $items[$key]['line_subtotal'] = $commission_due; $items[$key]['line_total'] = $commission_due; unset($items[$key]['line_tax']); } } return $items; }
/** * * * @return unknown */ public function paypal_ap_ipn() { if (empty($_GET['paypal_chain_ipn']) || empty($_GET['order_id'])) { return false; } $order_id = (int) $_GET['order_id']; $order = new WC_Order($order_id); if (!$order) { return false; } if ($_POST['status'] !== 'COMPLETED') { $order->update_status('failed', sprintf(__('Something went wrong. Response from PayPal invalidated this order. Status: %s.', 'wc_product_vendor'), $_POST['status'])); exit; } $order->payment_complete(); $order->add_order_note(__('IPN payment completed', 'wc_product_vendor')); if ($this->instapay) { PV_Commission::set_order_commission_paid($order_id); } exit; }
/** * Format products for easier displaying * * @param object $products * * @return array */ public function format_product_details($products) { if (empty($products)) { return false; } $orders_page = get_permalink(Product_Vendor::$pv_options->get_option('orders_page')); $default_commission = Product_Vendor::$pv_options->get_option('default_commission'); $total_qty = $total_cost = 0; $data = array('products' => array(), 'total_qty' => '', 'total_cost' => ''); foreach ($products as $product) { $ids[] = $product->ID; } $orders = PV_Queries::sum_orders_for_products($ids, array('vendor_id' => get_current_user_id())); if ($orders) { foreach ($orders as $order_item) { if ($order_item->qty < 1) { continue; } $commission_rate = PV_Commission::get_commission_rate($order_item->product_id); $_product = get_product($order_item->product_id); $id = !empty($_product->parent->id) ? $_product->parent->id : $order_item->product_id; $data['products'][$id] = array('id' => $id, 'title' => $_product->get_title(), 'qty' => !empty($data['products'][$id]) ? $data['products'][$id]['qty'] + $order_item->qty : $order_item->qty, 'cost' => !empty($data['products'][$id]) ? $data['products'][$id]['cost'] + $order_item->line_total : $order_item->line_total, 'view_orders_url' => esc_url(add_query_arg('orders_for_product', $id, $orders_page)), 'commission_rate' => $commission_rate); $total_qty += $order_item->qty; $total_cost += $order_item->line_total; } } $data['total_qty'] = $total_qty; $data['total_cost'] = $total_cost; // Sort by product title if (!empty($data['products'])) { usort($data['products'], array($this, 'sort_by_title')); } return $data; }