/**
  * Number of table columns that will be displayed
  * @return int
  */
 public function get_number_of_columns()
 {
     $number_of_columns = 4;
     if ($this->template_options['bewpi_show_sku']) {
         $number_of_columns++;
     }
     $order_taxes = $this->order->get_taxes();
     if ($this->template_options['bewpi_show_tax'] && wc_tax_enabled() && empty($legacy_order) && !empty($order_taxes)) {
         foreach ($order_taxes as $tax_id => $tax_item) {
             $number_of_columns++;
         }
     }
     return $number_of_columns;
 }
示例#2
0
 /**
  * Test: get_taxes
  */
 function test_get_taxes()
 {
     global $wpdb;
     update_option('woocommerce_calc_taxes', 'yes');
     $tax_rate = array('tax_rate_country' => '', 'tax_rate_state' => '', 'tax_rate' => '10.0000', 'tax_rate_name' => 'TAX', 'tax_rate_priority' => '1', 'tax_rate_compound' => '0', 'tax_rate_shipping' => '1', 'tax_rate_order' => '1', 'tax_rate_class' => '');
     WC_Tax::_insert_tax_rate($tax_rate);
     $object = new WC_Order();
     $item_1 = new WC_Order_Item_Product();
     $item_1->set_props(array('product' => WC_Helper_Product::create_simple_product(), 'quantity' => 4));
     $object->add_item($item_1);
     $object->calculate_totals();
     $this->assertCount(1, $object->get_taxes());
     $item = new WC_Order_Item_Tax();
     $item->set_rate(100);
     $item->set_tax_total(100);
     $item->set_shipping_tax_total(100);
     $object->add_item($item);
     $object->save();
     $this->assertCount(2, $object->get_taxes());
     // Cleanup
     $wpdb->query("DELETE FROM {$wpdb->prefix}woocommerce_tax_rates");
     $wpdb->query("DELETE FROM {$wpdb->prefix}woocommerce_tax_rate_locations");
     update_option('woocommerce_calc_taxes', 'no');
 }
function linksync_OrderFromBackEnd()
{
    //Ordered product(s)
    global $wpdb;
    $testMode = get_option('linksync_test');
    $LAIDKey = get_option('linksync_laid');
    $apicall = new linksync_class($LAIDKey, $testMode);
    if ($_POST['order_status'] == get_option('order_status_wc_to_vend')) {
        //Checking for already sent Order
        $orderId = $_POST['ID'];
        $sentOrderIds = get_option('linksync_sent_order_id');
        if (isset($sentOrderIds)) {
            if (!empty($sentOrderIds)) {
                $order_id_array = unserialize($sentOrderIds);
            } else {
                $order_id_array = array();
            }
            if (!in_array($orderId, $order_id_array)) {
                update_option('linksync_sent_order_id', serialize(array_merge($order_id_array, array($orderId))));
                $total_quantity = 0;
                $order = new WC_Order($orderId);
                $order_no = $order->get_order_number();
                if (strpos($order_no, '#') !== false) {
                    $order_no = str_replace('#', '', $order_no);
                }
                $get_total = $order->get_total();
                $currency = $order->get_order_currency();
                $order_total = $_POST['_order_total'];
                $taxes_included = false;
                $registerDb = get_option('wc_to_vend_register');
                $vend_user_detail = get_option('wc_to_vend_user');
                if (isset($vend_user_detail) && !empty($vend_user_detail)) {
                    $user = explode('|', $vend_user_detail);
                    $vend_uid = isset($user[0]) ? $user[0] : null;
                    $vend_username = isset($user[1]) ? $user[1] : null;
                }
                //Ordered product(s)
                $items = $order->get_items();
                $taxes = $order->get_taxes();
                foreach ($items as $item) {
                    foreach ($taxes as $tax_label) {
                        $sql = mysql_query("SELECT  tax_rate_id FROM  `" . $wpdb->prefix . "woocommerce_tax_rates` WHERE  tax_rate_name='" . $tax_label['label'] . "' AND tax_rate_class='" . $item['item_meta']['_tax_class'][0] . "'");
                        if (mysql_num_rows($sql) != 0) {
                            $tax_classes = linksync_tax_classes($tax_label['label'], $item['item_meta']['_tax_class'][0]);
                            if ($tax_classes['result'] == 'success') {
                                $vend_taxes = explode('/', $tax_classes['tax_classes']);
                            }
                        }
                    }
                    $taxId = isset($vend_taxes[0]) ? $vend_taxes[0] : null;
                    $taxName = isset($vend_taxes[1]) ? $vend_taxes[1] : null;
                    $taxRate = isset($vend_taxes[2]) ? $vend_taxes[2] : null;
                    if (isset($item['variation_id']) && !empty($item['variation_id'])) {
                        $product_id = $item['variation_id'];
                    } else {
                        $product_id = $item['product_id'];
                    }
                    $pro_object = new WC_Product($product_id);
                    $itemtotal = (double) $item['item_meta']['_line_subtotal'][0];
                    if (isset($item['line_subtotal']) && !empty($item['line_subtotal'])) {
                        $product_amount = (double) ($item['line_subtotal'] / $item['qty']);
                    }
                    $discount = (double) $item['item_meta']['_line_subtotal'][0] - (double) $item['item_meta']['_line_total'][0];
                    if (isset($discount) && !empty($discount)) {
                        $discount = (double) ($discount / $item['qty']);
                    }
                    #---------Changes--------#
                    //Product Amount = product org amount - discount amount
                    $product_total_amount = (double) $product_amount - (double) $discount;
                    $product_sku = $pro_object->get_sku();
                    if (isset($product_total_amount) && isset($taxRate) && !empty($product_total_amount) && !empty($taxRate)) {
                        $taxValue = $product_total_amount * $taxRate;
                    }
                    $products[] = array('sku' => $product_sku, 'title' => $item['name'], 'price' => $product_total_amount, 'quantity' => $item['qty'], 'discountAmount' => isset($discount) ? $discount : 0, 'taxName' => isset($taxName) ? $taxName : null, 'taxId' => isset($taxId) ? $taxId : null, 'taxRate' => isset($taxRate) ? $taxRate : null, 'taxValue' => isset($taxValue) ? $taxValue : null, 'discountTitle' => isset($discountTitle) ? $discountTitle : 'sale');
                    $total_quantity += $item['qty'];
                    unset($taxId);
                    unset($taxName);
                    unset($taxRate);
                    unset($taxValue);
                }
                #---------Discount-----#
                //                $total_discount = $order->get_total_discount();
                //$total_discount = get_post_meta($orderId, '_order_discount', true);
                //                if (isset($total_discount) && !empty($total_discount)) {
                //                    $taxes_Discount = $apicall->linksync_getTaxes();
                //                    if (isset($taxes_Discount) && !empty($taxes_Discount)) {
                //                        if (!isset($taxes_Discount['errorCode'])) {
                //                            if (isset($taxes_Discount['taxes'])) {
                //                                foreach ($taxes_Discount['taxes'] as $select_tax) {
                //                                    if ($select_tax['name'] == 'GST') {
                //                                        $discountTaxName = $select_tax['name'];
                //                                        $discountTaxId = $select_tax['id'];
                //                                        $discountTaxRate = $select_tax['rate'];
                //                                    }
                //                                }
                //                            }
                //                        }
                //                    }
                //                    if (isset($discountTaxRate) && !empty($discountTaxRate)) {
                //                        $taxValue_discount = (float) $discountTaxRate * (float) $total_discount;
                //                    }
                //                    $products[] = array(
                //                        "price" => isset($total_discount) ? $total_discount : null,
                //                        "quantity" => 1,
                //                        "sku" => "vend-discount",
                //                        'taxName' => isset($discountTaxName) ? $discountTaxName : null,
                //                        'taxId' => isset($discountTaxId) ? $discountTaxId : null,
                //                        'taxRate' => isset($discountTaxRate) ? $discountTaxRate : null,
                //                        'taxValue' => isset($taxValue_discount) ? $taxValue_discount : null
                //                    );
                //                    $products[] = array(
                //                        "price" => isset($total_discount) ? $total_discount : null,
                //                        "quantity" => 1,
                //                        "sku" => "vend-discount",
                //                        'taxName' => null,
                //                        'taxId' => null,
                //                        'taxRate' => null,
                //                        'taxValue' => null
                //                    );
                //                }
                #----------Shipping------------#
                foreach ($taxes as $tax_label) {
                    if (isset($tax_label['shipping_tax_amount']) && !empty($tax_label['shipping_tax_amount'])) {
                        $tax_classes = linksync_tax_classes($tax_label['label'], $item['item_meta']['_tax_class'][0]);
                        if ($tax_classes['result'] == 'success') {
                            $vend_taxes = explode('/', $tax_classes['tax_classes']);
                            $taxId_shipping = isset($vend_taxes[0]) ? $vend_taxes[0] : null;
                            $taxName_shipping = isset($vend_taxes[1]) ? $vend_taxes[1] : null;
                            $taxRate_shipping = isset($vend_taxes[2]) ? $vend_taxes[2] : null;
                        }
                    }
                }
                if (isset($_POST['shipping_method_id']) && !empty($_POST['shipping_method_id'][0])) {
                    $shipping_method = $_POST['shipping_method'][$_POST['shipping_method_id'][0]];
                    $shipping_cost = $_POST['shipping_cost'][$_POST['shipping_method_id'][0]];
                    if (isset($shipping_cost) && isset($taxRate_shipping) && !empty($shipping_cost) && !empty($taxRate_shipping)) {
                        $taxValue_shipping = $shipping_cost * $taxRate_shipping;
                    }
                    $products[] = array("price" => isset($shipping_cost) ? $shipping_cost : null, "quantity" => 1, "sku" => "shipping", 'taxName' => isset($taxName_shipping) ? $taxName_shipping : null, 'taxId' => isset($taxId_shipping) ? $taxId_shipping : null, 'taxRate' => isset($taxRate_shipping) ? $taxRate_shipping : null, 'taxValue' => isset($taxValue_shipping) ? $taxValue_shipping : null);
                }
                // Getting Payment id from mapping varabile
                if (isset($_POST['_payment_method']) && !empty($_POST['_payment_method'])) {
                    $wc_payment = get_option('wc_to_vend_payment');
                    $total_payments = explode(",", $wc_payment);
                    foreach ($total_payments as $mapped_payment) {
                        $exploded_mapped_payment = explode("|", $mapped_payment);
                        if (isset($exploded_mapped_payment[2]) && !empty($exploded_mapped_payment[2]) && isset($exploded_mapped_payment[0]) && !empty($exploded_mapped_payment[0])) {
                            if ($exploded_mapped_payment[2] == $_POST['_payment_method']) {
                                $vend_payment_data = explode("%%", $exploded_mapped_payment['0']);
                                if (isset($vend_payment_data[0])) {
                                    $payment_method = $vend_payment_data[0];
                                }
                                if (isset($vend_payment_data[1])) {
                                    $payment_method_id = $vend_payment_data[1];
                                }
                                break;
                            }
                        }
                    }
                    $payment = array("retailer_payment_type_id" => isset($payment_method_id) ? $payment_method_id : null, "amount" => isset($get_total) ? $get_total : 0, "method" => isset($payment_method) ? $payment_method : null, "transactionNumber" => isset($_POST['_transaction_id']) ? $_POST['_transaction_id'] : null);
                }
                //UTC Time
                date_default_timezone_set("UTC");
                $created = date("Y-m-d H:i:s", time());
                $export_user_details = get_option('wc_to_vend_export');
                if (isset($export_user_details) && !empty($export_user_details)) {
                    if ($export_user_details == 'customer') {
                        if (isset($_POST['customer_user']) && !empty($_POST['customer_user'])) {
                            $select_user = mysql_query('SELECT user_email FROM `' . $wpdb->prefix . 'users` WHERE `ID` =' . $_POST['customer_user']);
                            if (mysql_num_rows($select_user) != 0) {
                                $customer_detail = mysql_fetch_assoc($select_user);
                                $primary_email_address = $customer_detail['user_email'];
                            }
                        }
                        $primary_email = isset($primary_email_address) ? $primary_email_address : $_POST['_billing_email'];
                    }
                }
                $OrderArray = array('uid' => isset($vend_uid) ? $vend_uid : null, "orderId" => isset($order_no) ? $order_no : null, "source" => "WooCommerce", 'user_name' => isset($vend_username) ? $vend_username : null, 'created' => isset($created) ? $created : null, 'register_id' => isset($registerDb) ? $registerDb : null, 'primary_email' => isset($primary_email) ? $primary_email : null, 'total' => isset($order_total) ? $order_total : 0, 'total_tax' => isset($total_tax) ? $total_tax : 0, 'taxes_included' => $taxes_included, 'currency' => isset($currency) ? $currency : 'USD', 'shipping_method' => isset($shipping_method) ? $shipping_method : null, 'payment' => isset($payment) && !empty($payment) ? $payment : null, 'products' => isset($products) && !empty($products) ? $products : null, 'payment_type_id' => isset($payment_method_id) ? $payment_method_id : null);
                if (isset($export_user_details) && !empty($export_user_details)) {
                    if ($export_user_details == 'customer') {
                        if (isset($_POST['_billing_first_name'])) {
                            $OrderArray['billingAddress']['firstName'] = $_POST['_billing_first_name'];
                        }
                        if (isset($_POST['_billing_last_name'])) {
                            $OrderArray['billingAddress']['lastName'] = $_POST['_billing_last_name'];
                        }
                        if (isset($_POST['_billing_company'])) {
                            $OrderArray['billingAddress']['company'] = $_POST['_billing_company'];
                        }
                        if (isset($_POST['_billing_address_1'])) {
                            $OrderArray['billingAddress']['street1'] = $_POST['_billing_address_1'];
                        }
                        if (isset($_POST['_billing_address_2'])) {
                            $OrderArray['billingAddress']['street2'] = $_POST['_billing_address_2'];
                        }
                        if (isset($_POST['_billing_city'])) {
                            $OrderArray['billingAddress']['city'] = $_POST['_billing_city'];
                        }
                        if (isset($_POST['_billing_postcode'])) {
                            $OrderArray['billingAddress']['postalCode'] = $_POST['_billing_postcode'];
                        }
                        if (isset($_POST['_billing_country'])) {
                            $OrderArray['billingAddress']['country'] = $_POST['_billing_country'];
                        }
                        if (isset($_POST['_billing_state'])) {
                            $OrderArray['billingAddress']['state'] = $_POST['_billing_state'];
                        }
                        if (isset($_POST['_billing_phone'])) {
                            $OrderArray['billingAddress']['phone'] = $_POST['_billing_phone'];
                        }
                        if (isset($_POST['_shipping_first_name'])) {
                            $OrderArray['deliveryAddress']['firstName'] = $_POST['_shipping_first_name'];
                        }
                        if (isset($_POST['_shipping_last_name'])) {
                            $OrderArray['deliveryAddress']['lastName'] = $_POST['_shipping_last_name'];
                        }
                        if (isset($_POST['_shipping_company'])) {
                            $OrderArray['deliveryAddress']['company'] = $_POST['_shipping_company'];
                        }
                        if (isset($_POST['_shipping_address_1'])) {
                            $OrderArray['deliveryAddress']['street1'] = $_POST['_shipping_address_1'];
                        }
                        if (isset($_POST['_shipping_address_2'])) {
                            $OrderArray['deliveryAddress']['street2'] = $_POST['_shipping_address_2'];
                        }
                        if (isset($_POST['_shipping_city'])) {
                            $OrderArray['deliveryAddress']['city'] = $_POST['_shipping_city'];
                        }
                        if (isset($_POST['_shipping_postcode'])) {
                            $OrderArray['deliveryAddress']['postalCode'] = $_POST['_shipping_postcode'];
                        }
                        if (isset($_POST['_shipping_country'])) {
                            $OrderArray['deliveryAddress']['country'] = $_POST['_shipping_country'];
                        }
                        if (isset($_POST['_shipping_state'])) {
                            $OrderArray['deliveryAddress']['state'] = $_POST['_shipping_state'];
                        }
                        if (isset($_POST['_shipping_phone'])) {
                            $OrderArray['deliveryAddress']['phone'] = $_POST['_shipping_phone'];
                        }
                        if (isset($_POST['excerpt'])) {
                            $OrderArray['comments'] = $_POST['excerpt'];
                        }
                    }
                }
                $json = json_encode($OrderArray);
                $apicall->linksync_postOrder($json);
                linksync_class::add('Order Sync Woo to Vend', 'success', 'Woo Order no:' . $order_no, $LAIDKey);
            }
        }
    }
}
 /**
  * Get order items.
  *
  * @param  WC_Order $order Order data.
  *
  * @return array           Items list, extra amount and shipping cost.
  */
 protected function get_order_items($order)
 {
     $items = array();
     $extra_amount = 0;
     $shipping_cost = 0;
     // Force only one item.
     if ('yes' == $this->gateway->send_only_total) {
         $items[] = array('description' => $this->sanitize_description(sprintf(__('Order %s', 'woocommerce-pagseguro'), $order->get_order_number())), 'amount' => $this->money_format($order->get_total()), 'quantity' => 1);
     } else {
         // Products.
         if (0 < count($order->get_items())) {
             foreach ($order->get_items() as $order_item) {
                 if ($order_item['qty']) {
                     $item_name = $order_item['name'];
                     if (defined('WC_VERSION') && version_compare(WC_VERSION, '2.4.0', '<')) {
                         $item_meta = new WC_Order_Item_Meta($order_item['item_meta']);
                     } else {
                         $item_meta = new WC_Order_Item_Meta($order_item);
                     }
                     if ($meta = $item_meta->display(true, true)) {
                         $item_name .= ' - ' . $meta;
                     }
                     $items[] = array('description' => $this->sanitize_description($item_name), 'amount' => $this->money_format($order->get_item_total($order_item, false)), 'quantity' => $order_item['qty']);
                 }
             }
         }
         // Fees.
         if (0 < count($order->get_fees())) {
             foreach ($order->get_fees() as $fee) {
                 $items[] = array('description' => $this->sanitize_description($fee['name']), 'amount' => $this->money_format($fee['line_total']), 'quantity' => 1);
             }
         }
         // Taxes.
         if (0 < count($order->get_taxes())) {
             foreach ($order->get_taxes() as $tax) {
                 $items[] = array('description' => $this->sanitize_description($tax['label']), 'amount' => $this->money_format($tax['tax_amount'] + $tax['shipping_tax_amount']), 'quantity' => 1);
             }
         }
         // Shipping Cost.
         if (0 < $order->get_total_shipping()) {
             $shipping_cost = $this->money_format($order->get_total_shipping());
         }
         // Discount.
         if (defined('WC_VERSION') && version_compare(WC_VERSION, '2.3', '<')) {
             if (0 < $order->get_order_discount()) {
                 $extra_amount = '-' . $this->money_format($order->get_order_discount());
             }
         }
     }
     return array('items' => $items, 'extra_amount' => $extra_amount, 'shipping_cost' => $shipping_cost);
 }
 /**
  * Beeptify checkout form
  */
 public function generate_beeptify_form($order_id)
 {
     global $woocommerce;
     $order = new WC_Order($order_id);
     /* Setup known values */
     $amount_total = $order->order_total * 100;
     $order_number = str_replace('#', null, $order->get_order_number());
     $order_id = $order_number;
     // Shop URL's
     $decline_url = $woocommerce->cart->get_checkout_url();
     //$woocommerce->api_request_url("WC_payment_gateway_beeptify");
     $callback_url = add_query_arg('wooorderid', $order_id, WC()->api_request_url('WC_payment_gateway_beeptify'));
     /* Sale lines */
     $SaleLines = array();
     /* Make purchase line for display at beeptify */
     $reason = get_bloginfo('name') . " sale";
     $i = 0;
     foreach ($woocommerce->cart->get_cart() as $cart_item_key => $cart_item) {
         $this_product = apply_filters('woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key);
         $this_product_id = apply_filters('woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key);
         $this_product_name = $this_product->get_title();
         $quantities = WC()->cart->get_cart_item_quantities();
         $this_quantity = $quantities[$this_product_id];
         $this_price = $this_product->price;
         //$reason .= "$this_product_name: [br] $this_price". get_woocommerce_currency_symbol() ." x $this_quantity [br] Total: " . $this_total ." ". get_woocommerce_currency_symbol();
         $SaleLines[$i]['ProductId'] = $this_product_id;
         $SaleLines[$i]['ProductName'] = $this_product_name;
         $SaleLines[$i]['ProductDescription'] = "";
         $SaleLines[$i]['Quantity'] = $this_quantity;
         $SaleLines[$i]['PricePerUnit'] = $this_price * 100;
         $SaleLines[$i]['IsLoad'] = 0;
         $SaleLines[$i]['CodeList'] = "";
         /* Check for load on product */
         $bCodes = get_post_meta($this_product_id, '_beeptify_code', false);
         if (!empty($bCodes[0])) {
             $SaleLines[$i]['IsLoad'] = 1;
             $bCodes = explode(',', $bCodes[0]);
             end($bCodes);
             $lastKey = key($bCodes);
             reset($bCodes);
             foreach ($bCodes as $key => $bCode) {
                 $SaleLines[$i]['CodeList'] .= trim($bCode);
                 if ($key != $lastKey) {
                     $SaleLines[$i]['CodeList'] .= ", ";
                 }
             }
         }
         $i++;
     }
     /* Include shipping for sale line */
     if ($order->get_total_shipping() > 0) {
         $SaleLines[$i]['ProductId'] = "x";
         $SaleLines[$i]['ProductName'] = "Shipping";
         $SaleLines[$i]['ProductDescription'] = "";
         $SaleLines[$i]['Quantity'] = 1;
         $SaleLines[$i]['PricePerUnit'] = $order->get_total_shipping() * 100;
         $SaleLines[$i]['IsLoad'] = 0;
         $SaleLines[$i]['CodeList'] = "";
     }
     /* Test for tax, and add as sale line */
     $taxes = $order->get_taxes();
     $total_for_test = $order->get_total_tax();
     if (is_array($taxes) && $total_for_test > 0) {
         // It seems to land in a random row?
         foreach ($taxes as $the_tax) {
             $taxes = $the_tax;
         }
         if (!empty($taxes['tax_amount']) && $taxes['tax_amount'] != 0 || !empty($taxes['shipping_tax_amount']) && $taxes['shipping_tax_amount'] != 0) {
             $i++;
             $SaleLines[$i]['ProductId'] = "t";
             $SaleLines[$i]['ProductName'] = "Tax";
             $SaleLines[$i]['ProductDescription'] = "";
             $SaleLines[$i]['Quantity'] = 1;
             $SaleLines[$i]['PricePerUnit'] = $taxes['tax_amount'] * 100 + $taxes['shipping_tax_amount'] * 100;
             $SaleLines[$i]['IsLoad'] = 0;
             $SaleLines[$i]['CodeList'] = "";
         }
     }
     $css = '';
     /* if frame,send the CSS */
     if ($this->yesnotoint($this->frame)) {
         $css = plugins_url("css/gateway.beeptify.css", __FILE__);
     }
     $beeptify_args = array('_selectHost' => $this->liveurl, 'MsgType' => 'sale', 'MerchantId' => $this->merchant_id, 'Reason' => $reason, 'OrderNumber' => $order_number, 'ProductId' => '21', 'Amount' => $amount_total, 'SuccessUrl' => $callback_url, 'ErrorUrl' => $decline_url, 'CancelUrl' => $decline_url, 'UserType' => "unregistered", 'UserEmail' => $order->billing_email, 'Css' => $css, 'Language' => $this->pay_language, 'Mode' => $this->mode, 'Secret' => $this->secret);
     $html_form = array();
     /* Primary args */
     $to_hash = "";
     foreach ($beeptify_args as $key => $arg) {
         if ($key != '_selectHost' && !empty($arg)) {
             $to_hash .= $arg;
         }
         if ($key != 'Secret') {
             $html_form[] = "<input type='hidden' name='{$key}' value='{$arg}'/>";
         }
     }
     /* Sale lines */
     foreach ($SaleLines as $key => $line) {
         foreach ($line as $line_name => $line_value) {
             $to_hash .= $line_value;
             $html_form[] = "<input type='hidden' name='SaleLines[{$key}].{$line_name}' value='{$line_value}'/>";
         }
     }
     /* Hash */
     $hash = md5($to_hash);
     $html_form[] = "<input type='hidden' name='MD5Check' value='{$hash}'/>";
     // Debugging
     /*
     foreach ($html_form as $entity) {
         echo "<br>" . htmlspecialchars($entity);
     }
     die();
     */
     /* If we are going with an iframe or a redirect */
     if ($this->yesnotoint($this->frame)) {
         return $this->beeptify_iframe($order, $html_form);
     } else {
         return $this->beeptify_redirect($order, $html_form);
     }
 }