Ejemplo n.º 1
0
 /**
  * Filter products based on current vendor
  *
  * @param unknown $orders
  *
  * @return unknown
  */
 public function filter_products($orders)
 {
     $products = WCV_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 = WCV_Commission::calculate_commission($order->line_total, $order->product_id, $order);
             }
         }
     }
     return $orders;
 }
Ejemplo n.º 2
0
 /**
  *
  *
  * @param unknown $order
  * @param unknown $group (optional)
  *
  * @return unknown
  */
 public static function get_vendor_dues_from_order($order, $group = true)
 {
     global $woocommerce;
     $give_tax = WC_Vendors::$pv_options->get_option('give_tax');
     $give_shipping = WC_Vendors::$pv_options->get_option('give_shipping');
     $receiver = array();
     $shipping_given = 0;
     $tax_given = 0;
     WCV_Shipping::$pps_shipping_costs = array();
     foreach ($order->get_items() as $key => $product) {
         $product_id = !empty($product['variation_id']) ? $product['variation_id'] : $product['product_id'];
         $author = WCV_Vendors::get_vendor_from_product($product_id);
         $give_tax_override = get_user_meta($author, 'wcv_give_vendor_tax', true);
         $give_shipping_override = get_user_meta($author, 'wcv_give_vendor_shipping', true);
         $is_vendor = WCV_Vendors::is_vendor($author);
         $commission = $is_vendor ? WCV_Commission::calculate_commission($product['line_subtotal'], $product_id, $order) : 0;
         $tax = !empty($product['line_tax']) ? (double) $product['line_tax'] : 0;
         // Check if shipping is enabled
         if (get_option('woocommerce_calc_shipping') === 'no') {
             $shipping = 0;
             $shipping_tax = 0;
         } else {
             $shipping_costs = WCV_Shipping::get_shipping_due($order->id, $product, $author);
             $shipping = $shipping_costs['amount'];
             $shipping_tax = $shipping_costs['tax'];
         }
         // Add line item tax and shipping taxes together
         $total_tax = (double) $tax + (double) $shipping_tax;
         // Tax override on a per vendor basis
         if ($give_tax_override) {
             $give_tax = true;
         }
         // Shipping override
         if ($give_shipping_override) {
             $give_shipping = true;
         }
         if ($is_vendor) {
             $shipping_given += $give_shipping ? $shipping : 0;
             $tax_given += $give_tax ? $total_tax : 0;
             $give = 0;
             $give += !empty($receiver[$author]['total']) ? $receiver[$author]['total'] : 0;
             $give += $give_shipping ? $shipping : 0;
             $give += $commission;
             $give += $give_tax ? $total_tax : 0;
             if ($group) {
                 $receiver[$author] = array('vendor_id' => (int) $author, 'commission' => !empty($receiver[$author]['commission']) ? $receiver[$author]['commission'] + $commission : $commission, 'shipping' => $give_shipping ? !empty($receiver[$author]['shipping']) ? $receiver[$author]['shipping'] + $shipping : $shipping : 0, 'tax' => $give_tax ? !empty($receiver[$author]['tax']) ? $receiver[$author]['tax'] + $total_tax : $total_tax : 0, 'qty' => !empty($receiver[$author]['qty']) ? $receiver[$author]['qty'] + $product['qty'] : $product['qty'], 'total' => $give);
             } else {
                 $receiver[$author][$key] = array('vendor_id' => (int) $author, 'product_id' => $product_id, 'commission' => $commission, 'shipping' => $give_shipping ? $shipping : 0, 'tax' => $give_tax ? $total_tax : 0, 'qty' => $product['qty'], 'total' => ($give_shipping ? $shipping : 0) + $commission + ($give_tax ? $total_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][$key] = 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 = round($order->order_tax + $order->order_shipping_tax - $tax_given, 2);
     $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][$key]['commission'] = $receiver[1][$key]['commission'] - $discount;
         $receiver[1][$key]['shipping'] = $order->order_shipping - $shipping_given;
         $receiver[1][$key]['tax'] = $tax;
         $receiver[1][$key]['total'] += $total;
     }
     // Reset the array keys
     // $receivers = array_values( $receiver );
     return $receiver;
 }
Ejemplo n.º 3
0
 /**
  * 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(WC_Vendors::$pv_options->get_option('orders_page'));
     $default_commission = WC_Vendors::$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 = WCV_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 = WCV_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;
 }
Ejemplo n.º 4
0
 /**
  * Split order into vendor orders (when applicable) after checkout
  *
  * @since 
  * @param int $order_id
  * @return void
  */
 public static function create_child_orders($order_id)
 {
     $order = wc_get_order($order_id);
     $items = $order->get_items();
     $vendor_items = array();
     foreach ($items as $item_id => $item) {
         if (isset($item['product_id']) && $item['product_id'] !== 0) {
             // check if product is from vendor
             $product_author = get_post_field('post_author', $item['product_id']);
             if (WCV_Vendors::is_vendor($product_author)) {
                 $vendor_items[$product_author][$item_id] = array('item_id' => $item_id, 'qty' => $item['qty'], 'total' => $item['line_total'], 'subtotal' => $item['line_subtotal'], 'tax' => $item['line_tax'], 'subtotal_tax' => $item['line_subtotal_tax'], 'tax_data' => maybe_unserialize($item['line_tax_data']), 'commission' => WCV_Commission::calculate_commission($item['line_subtotal'], $item['product_id'], $order, $item['qty']));
             }
         }
     }
     foreach ($vendor_items as $vendor_id => $items) {
         if (!empty($items)) {
             $vendor_order = WCV_Vendors::create_vendor_order(array('order_id' => $order_id, 'vendor_id' => $vendor_id, 'line_items' => $items));
         }
     }
 }
Ejemplo n.º 5
0
 /**
  * Log commission to the pv_commission table
  *
  * Will either update or insert to the database
  *
  * @param array $orders
  *
  * @return unknown
  */
 public static function insert_new_commission($orders = array())
 {
     global $wpdb;
     if (empty($orders)) {
         return false;
     }
     $table = $wpdb->prefix . "pv_commission";
     // Insert the time and default status 'due'
     foreach ($orders as $key => $order) {
         $orders[$key]['time'] = $order['time'];
         $orders[$key]['status'] = $order['total_due'] == 0 ? 'paid' : 'due';
     }
     foreach ($orders as $key => $order) {
         $where = array('order_id' => $order['order_id'], 'product_id' => $order['product_id'], 'vendor_id' => $order['vendor_id'], 'qty' => $order['qty']);
         // Is the commission already paid?
         $count = WCV_Commission::check_commission_status($order, 'paid');
         if ($count == 0) {
             $update = $wpdb->update($table, $order, $where);
             if (!$update) {
                 $insert = $wpdb->insert($table, $order);
             }
         }
     }
     do_action('wcv_commissions_inserted', $orders);
 }
Ejemplo n.º 6
0
 /**
  *
  *
  * @param unknown $items
  * @param unknown $order
  *
  * @return unknown
  */
 function check_items($items, $order)
 {
     foreach ($items as $key => $product) {
         //  If this is a line item
         if ($product['type'] == 'line_item') {
             $author = WCV_Vendors::get_vendor_from_product($product['product_id']);
             if ($this->current_vendor != $author) {
                 unset($items[$key]);
                 continue;
             } else {
                 $commission_due = WCV_Commission::calculate_commission($product['line_subtotal'], $product['product_id'], $order);
                 $items[$key]['line_subtotal'] = $commission_due;
                 $items[$key]['line_total'] = $commission_due;
                 // Don't display tax if give tax is not enabled.
                 // if ( !WC_Vendors::$pv_options->get_option( 'give_tax' ) ) {
                 // 	unset($items[ $key ][ 'line_tax' ]) ;
                 // }
             }
         }
     }
     return $items;
 }
Ejemplo n.º 7
0
 /**
  * Delete due commission for a vendor.
  *
  * @param array $users
  *
  * @return unknown
  */
 private function purge_user_meta($users)
 {
     global $wpdb;
     return WCV_Commission::set_order_commission_paid($this->orders_paid, true);
 }
Ejemplo n.º 8
0
 /**
  *
  *
  * @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.', 'wcvendors'), $_POST['status']));
         exit;
     }
     $order->payment_complete();
     $order->add_order_note(__('IPN payment completed', 'wcvendors'));
     if ($this->instapay) {
         WCV_Commission::set_order_commission_paid($order_id);
     }
     exit;
 }
Ejemplo n.º 9
0
 /**
  * Commission due for a product based on a rate and price
  *
  * @param float   $product_price
  * @param unknown $product_id
  *
  * @return float
  */
 public static function calculate_commission($product_price, $product_id, $order)
 {
     $commission_rate = WCV_Commission::get_commission_rate($product_id);
     $commission = $product_price * ($commission_rate / 100);
     $commission = round($commission, 2);
     return apply_filters('wcv_commission_rate', $commission, $product_id, $product_price, $order);
 }
Ejemplo n.º 10
0
 /**
  *
  *
  * @param unknown $items
  * @param unknown $order
  *
  * @return unknown
  */
 function check_items($items, $order)
 {
     $settings = get_option('woocommerce_vendor_new_order_settings');
     foreach ($items as $key => $product) {
         //  If this is a line item
         if ($product['type'] == 'line_item') {
             $author = WCV_Vendors::get_vendor_from_product($product['product_id']);
             if ($this->current_vendor != $author) {
                 unset($items[$key]);
                 continue;
             } else {
                 // If display commission is ticked show this otherwise show the full price.
                 if ('yes' == $settings['commission_display']) {
                     $commission_due = WCV_Commission::calculate_commission($product['line_subtotal'], $product['product_id'], $order, $product['qty']);
                     $items[$key]['line_subtotal'] = $commission_due;
                     $items[$key]['line_total'] = $commission_due;
                     // Don't display tax if give tax is not enabled.
                     if (!WC_Vendors::$pv_options->get_option('give_tax')) {
                         unset($items[$key]['line_tax']);
                     }
                 }
             }
         }
     }
     return $items;
 }
Ejemplo n.º 11
0
 /**
  *
  *
  * @param unknown $items
  * @param unknown $order
  *
  * @return unknown
  */
 function check_items($items, $order)
 {
     foreach ($items as $key => $product) {
         //  If this is a line item
         if ($product['type'] == 'line_item') {
             $author = WCV_Vendors::get_vendor_from_product($product['product_id']);
             if ($this->current_vendor != $author) {
                 unset($items[$key]);
                 continue;
             } else {
                 $commission_due = WCV_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;
 }
Ejemplo n.º 12
0
 /**
  * Delete due commission for a vendor.
  *
  * @param array $users
  *
  * @return unknown
  */
 private function purge_user_meta($vendor_ids)
 {
     global $wpdb;
     return WCV_Commission::set_vendor_commission_paid($vendor_ids);
 }