$product['asset_account_id'] = null;
     }
     $accounts_expense = get_option('ps_account_expense');
     if (isset($accounts_expense) && !empty($accounts_expense)) {
         $product['expense_account_id'] = $accounts_expense;
     } else {
         $product['expense_account_id'] = null;
     }
     $accounts_revenue = get_option('ps_account_revenue');
     if (isset($accounts_revenue) && !empty($accounts_revenue)) {
         $product['income_account_id'] = $accounts_revenue;
     } else {
         $product['income_account_id'] = null;
     }
     $data = json_encode($product);
     $response = $apicall->linksync_postProduct($data);
     linksync_class::add('Product Sync Woo to QBO', 'success', 'Product synced SKU:' . $product['sku'], $LAIDKey);
 } else {
     if ($_POST['original_post_status'] == 'auto-draft' || $_POST['original_post_status'] == 'draft') {
         //if the product is publish new
         // Title, Description, qty, price etc, even if these options are not enabled on the Admin UI.
         $product['name'] = isset($_POST['post_title']) && !empty($_POST['post_title']) ? html_entity_decode($_POST['post_title']) : null;
         if (get_option('ps_price') == 'on') {
             //                                 Tax && Price
             if (isset($_POST['_tax_status']) && $_POST['_tax_status'] == 'taxable') {
                 # Product with TAX
                 $taxname = empty($_POST['_tax_class']) ? 'standard-tax' : $_POST['_tax_class'];
                 $response_taxes = linksyn_tax_QBO($taxname);
                 if ($response_taxes['result'] == 'success') {
                     $product['tax_name'] = html_entity_decode($response_taxes['data']['tax_name']);
                     $product['tax_rate'] = $response_taxes['data']['tax_rate'];
function order_product_post()
{
    global $wpdb;
    $testMode = get_option('linksync_test');
    $LAIDKey = get_option('linksync_laid');
    $apicall = new linksync_class($LAIDKey, $testMode);
    if (isset($_REQUEST['key']) && !empty($_REQUEST['key'])) {
        $orderId = wc_get_orderid_by_order_key($_REQUEST['key']);
    } else {
        $orderId = $_POST['ID'];
    }
    //Checking for already sent Order
    $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))));
            $taxsetup = false;
            $product = array();
            $order = new WC_Order($orderId);
            //Ordered product(s)
            $items = $order->get_items();
            foreach ($items as $item) {
                $product = array();
                $product_id = $item['product_id'];
                $query = mysql_query("SELECT post_status,post_content FROM `" . $wpdb->prefix . "posts` WHERE ID='" . $product_id . "'");
                if (mysql_num_rows($query) != 0) {
                    $result = mysql_fetch_assoc($query);
                    if (isset($result) && !empty($result)) {
                        if ($result['post_status'] != 'trash') {
                            $post_detail = get_post_meta($product_id);
                            if (@empty($post_detail['_sku'][0])) {
                                $post_detail['_sku'][0] = 'sku_' . $product_id;
                            }
                            $post_detail['_sku'][0] = linksync_removespaces_sku_orderProduct($post_detail['_sku'][0]);
                            update_post_meta($product_id, '_sku', $post_detail['_sku'][0]);
                            $product['sku'] = html_entity_decode($post_detail['_sku'][0]);
                            //SKU(unique Key)
                            //product status ->publish
                            $product['active'] = isset($result['post_status']) && $result['post_status'] == 'publish' ? 1 : 0;
                            // Price with Tax
                            if (get_option('woocommerce_calc_taxes') == 'yes') {
                                if (get_option('linksync_woocommerce_tax_option') == 'on') {
                                    if (get_option('woocommerce_prices_include_tax') == 'yes') {
                                        $excluding_tax = 'off';
                                    } else {
                                        $excluding_tax = 'on';
                                    }
                                } else {
                                    $excluding_tax = get_option('excluding_tax');
                                }
                            } else {
                                $excluding_tax = get_option('excluding_tax');
                            }
                            $display_retail_price_tax_inclusive = get_option('linksync_tax_inclusive');
                            if (get_option('ps_price') == 'on') {
                                if (isset($post_detail['_tax_status'][0]) && $post_detail['_tax_status'][0] == 'taxable') {
                                    # Product with TAX
                                    $taxname = empty($post_detail['_tax_class'][0]) ? 'standard-tax' : $post_detail['_tax_class'][0];
                                    $response_taxes = linksyn_get_tax_details($taxname);
                                    // echo"<pre>"; print_r($response_taxes);
                                    if ($response_taxes['result'] == 'success') {
                                        $product['tax_name'] = html_entity_decode($response_taxes['data']['tax_name']);
                                        $product['tax_rate'] = $response_taxes['data']['tax_rate'];
                                        $taxsetup = true;
                                    }
                                }
                                if ($excluding_tax == 'on') {
                                    # https://www.evernote.com/shard/s144/sh/e63f527b-903f-4002-8f00-313ff0652290/d9c1e0ce5a95800a
                                    if ($taxsetup) {
                                        if (isset($post_detail['_regular_price'][0]) && !empty($post_detail['_regular_price'][0])) {
                                            //cost price:_regular_price
                                            $regular_price = (double) $post_detail['_regular_price'][0];
                                            // Get Tax_value
                                            $tax_rate = (double) $product['tax_rate'];
                                            $tax_value = (double) ($regular_price * $tax_rate);
                                            /* For excluding tax (both Woo Tax Excluding and Vend Tax Excluding)
                                             * display_retail_price_tax_inclusive 1, sell_price = Woo Final price + tax
                                             * For display_retail_price_tax_inclusive 0, sell_price = Woo Final price
                                             */
                                            if ($display_retail_price_tax_inclusive == '1') {
                                                $price = $post_detail['_regular_price'][0] + $tax_value;
                                            } elseif ($display_retail_price_tax_inclusive == '0') {
                                                $price = $post_detail['_regular_price'][0];
                                            }
                                            //sell price:_regular_price
                                            $product['sell_price'] = str_replace(',', '', $price);
                                            $product['list_price'] = str_replace(',', '', $price);
                                            $product['tax_value'] = $tax_value;
                                        }
                                    } else {
                                        // excluding tax off and tax not enabled in woocomerce
                                        if (isset($post_detail['_regular_price'][0]) && !empty($post_detail['_regular_price'][0])) {
                                            //cost price:_regular_price
                                            //sell price:_regular_price
                                            $product['sell_price'] = str_replace(',', '', $post_detail['_regular_price'][0]);
                                            $product['list_price'] = str_replace(',', '', $post_detail['_regular_price'][0]);
                                        }
                                    }
                                } else {
                                    // No effect on price
                                    if (isset($post_detail['_regular_price'][0]) && !empty($post_detail['_regular_price'][0])) {
                                        $regular_price = (double) $post_detail['_regular_price'][0];
                                        //                                                 Get Tax_value
                                        $tax_rate = (double) $product['tax_rate'];
                                        $tax_value = $regular_price - $regular_price / (1 + $tax_rate);
                                        if ($display_retail_price_tax_inclusive == '1') {
                                        } elseif ($display_retail_price_tax_inclusive == '0') {
                                            $post_detail['_regular_price'][0] = $post_detail['_regular_price'][0] - $tax_value;
                                        }
                                        //sell price:_regular_price
                                        $product['sell_price'] = str_replace(',', '', $post_detail['_regular_price'][0]);
                                        $product['list_price'] = str_replace(',', '', $post_detail['_regular_price'][0]);
                                    }
                                }
                            }
                            if (isset($post_detail['_stock_status'][0]) && $post_detail['_stock_status'][0] == 'instock') {
                                $product['quantity'] = isset($post_detail['_stock'][0]) ? $post_detail['_stock'][0] : 0;
                            }
                            #Name/Title Check
                            if (get_option('ps_name_title') == 'on') {
                                $product['name'] = html_entity_decode($item['name']);
                            }
                            #Description
                            if (get_option('ps_description') == 'on') {
                                $product['description'] = html_entity_decode($result['post_content']);
                            }
                            $product['includes_tax'] = isset($post_detail['_tax_status'][0]) && $post_detail['_tax_status'][0] == 'taxable' ? true : false;
                            #---Outlet---Product----#
                            if (get_option('ps_quantity') == 'on') {
                                if (get_option('ps_wc_to_vend_outlet') == 'on') {
                                    $getoutlets = get_option('wc_to_vend_outlet_detail');
                                    if (isset($getoutlets) && !empty($getoutlets)) {
                                        $outlet = explode('|', $getoutlets);
                                        if (isset($post_detail['_stock'][0]) && !empty($post_detail['_stock'][0])) {
                                            $product['outlets'] = array(array('name' => html_entity_decode($outlet[0]), 'quantity' => $post_detail['_stock'][0]));
                                        }
                                    }
                                }
                            } else {
                                $product['outlets'] = array(array('quantity' => NULL));
                            }
                            #qunantity
                            //
                            #Tags
                            if (get_option('ps_tags') == 'on') {
                                //To get the Detail of the Tags and Category of the product using product id(Post ID)
                                $tags_query = "SELECT " . $wpdb->prefix . "terms.name FROM `" . $wpdb->prefix . "term_taxonomy` JOIN " . $wpdb->prefix . "terms ON(" . $wpdb->prefix . "terms.term_id=" . $wpdb->prefix . "term_taxonomy.term_id)  JOIN " . $wpdb->prefix . "term_relationships ON(" . $wpdb->prefix . "term_relationships.term_taxonomy_id=" . $wpdb->prefix . "term_taxonomy.term_taxonomy_id) WHERE " . $wpdb->prefix . "term_taxonomy.`taxonomy`='product_tag' AND " . $wpdb->prefix . "term_relationships.object_id='" . $product_id . "'";
                                $result_tags = mysql_query($tags_query) or die(mysql_error());
                                //                    if (!$result_tags)
                                //                        die("Error In  Connection : " . mysql_error() . " Line No. " . __LINE__);
                                if (mysql_num_rows($result_tags) != 0) {
                                    $tags_product_type = array();
                                    while ($row_tags = mysql_fetch_assoc($result_tags)) {
                                        $tags_product_type[] = array('name' => html_entity_decode($row_tags['name']));
                                    }
                                }
                                if (isset($tags_product_type) && !empty($tags_product_type)) {
                                    $product['tags'] = $tags_product_type;
                                }
                                //To free an array to use futher
                                unset($tags_product_type);
                            }
                            #brands
                            if (get_option('ps_brand') == 'on') {
                                //To get the Detail of the Tags and Category of the product using product id(Post ID)
                                $brands_query = "SELECT " . $wpdb->prefix . "terms.name FROM `" . $wpdb->prefix . "term_taxonomy` JOIN " . $wpdb->prefix . "terms ON(" . $wpdb->prefix . "terms.term_id=" . $wpdb->prefix . "term_taxonomy.term_id)  JOIN " . $wpdb->prefix . "term_relationships ON(" . $wpdb->prefix . "term_relationships.term_taxonomy_id=" . $wpdb->prefix . "term_taxonomy.term_taxonomy_id) WHERE " . $wpdb->prefix . "term_taxonomy.`taxonomy`='product_brand' AND " . $wpdb->prefix . "term_relationships.object_id='" . $product_id . "'";
                                $result_brands = mysql_query($brands_query) or die(mysql_error());
                                if (mysql_num_rows($result_brands) != 0) {
                                    while ($row_brands = mysql_fetch_assoc($result_brands)) {
                                        $brands[] = array('name' => html_entity_decode($row_brands['name']));
                                    }
                                }
                                if (!empty($brands)) {
                                    $product['brands'] = $brands;
                                }
                                //To free an array to use futher
                                unset($brands);
                            }
                            #Variants product
                            $variants_data = get_posts(array('post_type' => 'product_variation', 'post_parent' => $product_id));
                            if (isset($variants_data) && !empty($variants_data)) {
                                $total_var_product = 0;
                                foreach ($variants_data as $variant_data) {
                                    $option = array(1 => 'one', 2 => 'two', 3 => 'three', 4 => 'four', 5 => 'five', 6 => 'six', 7 => 'seven', 8 => 'eight', 9 => 'nine', 10 => 'ten');
                                    $variants_detail = get_post_meta($variant_data->ID);
                                    if (@empty($variants_detail['_sku'][0])) {
                                        $variants_detail['_sku'][0] = 'sku_' . $variant_data->ID;
                                    }
                                    $variants_detail['_sku'][0] = linksync_removespaces_sku_orderProduct($variants_detail['_sku'][0]);
                                    update_post_meta($variant_data->ID, '_sku', $variants_detail['_sku'][0]);
                                    $variant['sku'] = html_entity_decode($variants_detail['_sku'][0]);
                                    //SKU(unique Key)
                                    #Name/Title Check
                                    if (get_option('ps_name_title') == 'on') {
                                        $variant['name'] = html_entity_decode($variant_data->post_title);
                                    }
                                    #quantity
                                    if (@$variants_detail['_stock_status'][0] == 'instock') {
                                        $variant['quantity'] = @$variants_detail['_stock'][0];
                                    }
                                    // Price with Tax
                                    if (get_option('ps_price') == 'on') {
                                        if (isset($variants_detail['_tax_status'][0]) && $variants_detail['_tax_status'][0] == 'taxable') {
                                            # Product with TAX
                                            $taxname = empty($variants_detail['_tax_class'][0]) ? 'standard-tax' : $variants_detail['_tax_class'][0];
                                            $response_taxes = linksyn_get_tax_details($taxname);
                                            if ($response_taxes['result'] == 'success') {
                                                $variant['tax_name'] = html_entity_decode($response_taxes['data']['tax_name']);
                                                $variant['tax_rate'] = $response_taxes['data']['tax_rate'];
                                                $taxsetup = true;
                                            }
                                        }
                                        if ($excluding_tax == 'on') {
                                            # https://www.evernote.com/shard/s144/sh/e63f527b-903f-4002-8f00-313ff0652290/d9c1e0ce5a95800a
                                            if ($taxsetup) {
                                                if (isset($variants_detail['_regular_price'][0]) && !empty($variants_detail['_regular_price'][0])) {
                                                    //cost price:_regular_price
                                                    $regular_price = (double) $variants_detail['_regular_price'][0];
                                                    // Get Tax_value
                                                    $tax_rate = (double) $variant['tax_rate'];
                                                    $tax_value = (double) ($regular_price * $tax_rate);
                                                    //sell price:_regular_price
                                                    if ($display_retail_price_tax_inclusive == '1') {
                                                        $price_variant = $variants_detail['_regular_price'][0] + $tax_value;
                                                    } elseif ($display_retail_price_tax_inclusive == '0') {
                                                        $price_variant = $variants_detail['_regular_price'][0];
                                                    }
                                                    $variant['sell_price'] = str_replace(',', '', $price_variant);
                                                    $variant['list_price'] = str_replace(',', '', $price_variant);
                                                    $variant['tax_value'] = $tax_value;
                                                }
                                            } else {
                                                // excluding tax off and tax not enabled in woocomerce
                                                if (isset($variants_detail['_regular_price'][0]) && !empty($variants_detail['_regular_price'][0])) {
                                                    //sell price:_regular_price
                                                    $variant['sell_price'] = str_replace(',', '', $variants_detail['_regular_price'][0]);
                                                    $variant['list_price'] = str_replace(',', '', $variants_detail['_regular_price'][0]);
                                                }
                                            }
                                        } else {
                                            // No effect on price
                                            if (isset($variants_detail['_regular_price'][0]) && !empty($variants_detail['_regular_price'][0])) {
                                                $regular_price = (double) $variants_detail['_regular_price'][0];
                                                //                                                 Get Tax_value
                                                $tax_rate = (double) $variant['tax_rate'];
                                                $tax_value = $regular_price - $regular_price / (1 + $tax_rate);
                                                if ($display_retail_price_tax_inclusive == '1') {
                                                } elseif ($display_retail_price_tax_inclusive == '0') {
                                                    $variants_detail['_regular_price'][0] = $variants_detail['_regular_price'][0] - $tax_value;
                                                }
                                                //sell price:_regular_price
                                                $variant['sell_price'] = str_replace(',', '', $variants_detail['_regular_price'][0]);
                                                $variant['list_price'] = str_replace(',', '', $variants_detail['_regular_price'][0]);
                                                $variant['tax_value'] = $tax_value;
                                            }
                                        }
                                    }
                                    // ATTRIBUTE && VARIANTS
                                    $attributes_select = mysql_query("SELECT * FROM `" . $wpdb->prefix . "woocommerce_attribute_taxonomies`") or die(mysql_error());
                                    $check = 1;
                                    // $variants_detail['attribute_pa_' . strtolower($attributes['attribute_name'])][0]
                                    if (mysql_num_rows($attributes_select) != 0) {
                                        $keys = array_keys($variants_detail);
                                        if (false !== stripos(implode("\n", $keys), "attribute_pa_")) {
                                            while ($attributes = mysql_fetch_assoc($attributes_select)) {
                                                if (isset($variants_detail['attribute_pa_' . strtolower($attributes['attribute_name'])]) && !empty($variants_detail['attribute_pa_' . strtolower($attributes['attribute_name'])])) {
                                                    $attribute_name = str_replace('pa_', '', $attributes['attribute_name']);
                                                    $attribute_query = mysql_query("SELECT attribute_label FROM `" . $wpdb->prefix . "woocommerce_attribute_taxonomies` WHERE `attribute_name` = '" . $attribute_name . "'");
                                                    if (mysql_num_rows($attribute_query) != 0) {
                                                        $attribute_name_result = mysql_fetch_assoc($attribute_query);
                                                        $name = $attribute_name_result['attribute_label'];
                                                    }
                                                    $variant['option_' . $option[$check] . '_name'] = isset($name) ? $name : '';
                                                    $query = mysql_query("SELECT name FROM `" . $wpdb->base_prefix . "terms` WHERE `slug` = '" . $variants_detail['attribute_pa_' . strtolower($attributes['attribute_name'])][0] . "'");
                                                    if (mysql_num_rows($query) != 0) {
                                                        $attribute_value = mysql_fetch_assoc($query);
                                                        $value = $attribute_value['name'];
                                                    }
                                                    $variant['option_' . $option[$check] . '_value'] = isset($value) ? $value : '';
                                                    $check++;
                                                }
                                            }
                                        } else {
                                            if (isset($post_detail['_product_attributes'][0]) && !empty($post_detail['_product_attributes'][0])) {
                                                $_product_attributes = unserialize($post_detail['_product_attributes'][0]);
                                                foreach ($_product_attributes as $attribute_value) {
                                                    $attributeName = $attribute_value['name'];
                                                    $_attribute = explode('|', $attribute_value['value']);
                                                    $value = trim($_attribute[$total_var_product]);
                                                    $variant['option_' . $option[$check] . '_name'] = isset($attributeName) ? $attributeName : '';
                                                    $variant['option_' . $option[$check] . '_value'] = isset($value) ? $value : '';
                                                    $check++;
                                                }
                                            }
                                        }
                                    } else {
                                        if (isset($post_detail['_product_attributes'][0]) && !empty($post_detail['_product_attributes'][0])) {
                                            $_product_attributes = unserialize($post_detail['_product_attributes'][0]);
                                            foreach ($_product_attributes as $attribute_value) {
                                                $attributeName = $attribute_value['name'];
                                                $_attribute = explode('|', $attribute_value['value']);
                                                $value = trim($_attribute[$total_var_product]);
                                                $variant['option_' . $option[$check] . '_name'] = isset($attributeName) ? $attributeName : '';
                                                $variant['option_' . $option[$check] . '_value'] = isset($value) ? $value : '';
                                                $check++;
                                            }
                                        }
                                    }
                                    #qunantity-----UPDATE--variant---
                                    if (get_option('ps_quantity') == 'on') {
                                        if (get_option('ps_wc_to_vend_outlet') == 'on') {
                                            $getoutlets = get_option('wc_to_vend_outlet_detail');
                                            if (isset($getoutlets) && !empty($getoutlets)) {
                                                $outlets = explode('|', $getoutlets);
                                                if (isset($variants_detail['_stock'][0]) && !empty($variants_detail['_stock'][0])) {
                                                    $variant['outlets'] = array(array('name' => $outlets[0], 'quantity' => $variants_detail['_stock'][0]));
                                                } else {
                                                    $variant['outlets'] = array(array('name' => html_entity_decode($outlets[0]), 'quantity' => NULL));
                                                }
                                            } else {
                                                $variant['outlets'] = NULL;
                                            }
                                        }
                                    } else {
                                        $variant['outlets'] = array(array('quantity' => NULL));
                                    }
                                    $product['variants'][] = $variant;
                                    $total_var_product++;
                                }
                            }
                            $data = json_encode($product);
                            $response = $apicall->linksync_postProduct($data);
                            linksync_class::add('Product Sync Woo to Vend', 'success', 'Product synced SKU:' . $product['sku'], $LAIDKey);
                        }
                    }
                }
            }
        }
    }
}