function scrap_item($sku)
{
    $sku = trim($sku);
    if (preg_match('%(AMZ|WM|OS|AL|HN|WF|B)(.*)%', $sku, $matches)) {
        switch ($matches[1]) {
            case 'B':
                $result = scrap_amazon('B' . $matches[2]);
                break;
            case 'WM':
                $result = scrap_walmart($matches[2]);
                break;
            case 'OS':
                $result = scrap_overstock($matches[2]);
                break;
            case 'AL':
                $result = scrap_aliexpress($matches[2]);
                break;
            case 'HN':
                $result = scrap_hayneedle($matches[2]);
                break;
            case 'WF':
                $result = scrap_wayfair($matches[2]);
                break;
            default:
                die("Unknown item '{$sku}'");
                break;
        }
    }
    return $result;
}
function ebay_item($item_id, $item_url, $ebay_price, $quantity, $title, $sku, $image_url)
{
    $active_user = 8;
    $sku = str_replace(' ', '', $sku);
    $pattern = '/^AMZ/';
    $posamaz = preg_match($pattern, $sku, $matches, PREG_OFFSET_CAPTURE);
    $pattern = '/^WM/';
    $poswm = preg_match($pattern, $sku, $matches, PREG_OFFSET_CAPTURE);
    $pattern = '/^OS/';
    $posos = preg_match($pattern, $sku, $matches, PREG_OFFSET_CAPTURE);
    $pattern = '/^AL/';
    $posal = preg_match($pattern, $sku, $matches, PREG_OFFSET_CAPTURE);
    $asin = "";
    if ($posamaz > 0) {
        $asin = str_replace('AMZ', '', $sku);
    } elseif ($poswm > 0) {
        $asin = str_replace('WM', '', $sku);
        //$tasin=explode('-',$asin);
        // $asin=$tasin[0];
    } elseif ($posos > 0) {
        $asin = str_replace('OS', '', $sku);
    } elseif ($posal > 0) {
        $asin = str_replace('AL', '', $sku);
    } else {
        $asin = $sku;
    }
    $sql = "SELECT * FROM ebay_asin WHERE item_id = {$item_id} and UserID ={$active_user}";
    $rs = mysql_query($sql);
    if (mysql_num_rows($rs)) {
        $row = mysql_fetch_array($rs);
        $sql = "SELECT * FROM user_products WHERE ItemID = {$item_id}";
        $rs = mysql_query($sql);
        if (mysql_num_rows($rs)) {
            if ($posamaz > 0) {
                $result = scrap_amazon($asin);
                $counter = 1;
                while ($result['scrapok'] == 0 && $counter <= 5) {
                    sleep(2);
                    $result = scrap_amazon($asin);
                    $counter++;
                }
            } elseif ($poswm > 0) {
                $result = scrap_walmart($asin);
            } elseif ($posos > 0) {
                $result = scrap_overstock($asin);
            } elseif ($posal > 0) {
                $result = scrap_aliexpress($asin);
            } else {
                $result = scrap_amazon($asin);
            }
            //$azonprice=scrap_apiprice($asin);
            $amazon_price = $result['offerprice'];
            preg_match_all('/\\d+(\\.\\d+)/', $amazon_price, $matches);
            if (isset($matches[0][0])) {
                $amazon_price = $matches[0][0];
            }
            //$azonprice=substr(trim($azonprice),1,strlen(trim($azonprice)));
            $amazon_price = number_format((double) $amazon_price, 2, '.', '');
            $ebay_price = number_format((double) $ebay_price, 2, '.', '');
            //$azonprice=number_format((float)$azonprice, 2, '.', '');
            $amazon_quantity = $result['quantity'];
            $max_quantity = $quantity;
            $profit_ratio = $ebay_price * 0.87 - $amazon_price;
            //echo $profit_ratio.'='.$ebay_price.'*0.87-'.$amazon_price;die;
            if ($profit_ratio <= 0) {
                $profit_ratio = 5.0;
            }
            $title = str_replace("'", "\\'", $title);
            $insert = "UPDATE  user_products \n                   SET  UserID = {$active_user}, \n                         \n                        Qty = {$quantity}, \n                        Price = {$ebay_price}, \n                        Title = '{$title}', \n                        SKU = '{$sku}', \n                        Image = '{$image_url}', \n                        ItemUrl = '{$item_url}', \n                        AmazonPrice = {$amazon_price},\t\n                        AmazonQty = {$amazon_quantity}, \n                        MaxQty = {$max_quantity},\n                        ProfitRatio = {$profit_ratio}\n\t\t\t\t\t\tWHERE ItemID = {$item_id}";
            $update = " UPDATE ebay_asin \n                    SET quantity = {$quantity}\n                    WHERE asins='{$asin}'\n                    AND UserID={$active_user}\n        ";
            $delete = "DELETE ast,aws FROM \n                    asins_table as ast INNER JOIN \n                    aws_asin as aws\n                    ON ast.asins = aws.asin\n                    AND ast.UserID = aws.UserID \n                    WHERE ast.asins='{$asin}'\n                    AND ast.UserID={$active_user}\n                    ";
            //	$deleteeb="DELETE FROM ebay_asin WHERE asins='$asin' and UserID=$active_user ";
            mysql_query($insert);
            mysql_query($update);
            mysql_query($delete);
            // mysql_query($deleteeb);
        }
    } else {
        if ($asin != "") {
            if ($posamaz > 0) {
                $result = scrap_amazon($asin);
                $counter = 1;
                while ($result['scrapok'] == 0 && $counter <= 5) {
                    sleep(2);
                    $result = scrap_amazon($asin);
                    $counter++;
                }
            } elseif ($poswm > 0) {
                $result = scrap_walmart($asin);
            } elseif ($posos > 0) {
                $result = scrap_overstock($asin);
            } elseif ($posal > 0) {
                $result = scrap_aliexpress($asin);
            } else {
                $result = scrap_amazon($asin);
            }
            $amazon_price = $result['offerprice'];
            preg_match_all('/\\d+(\\.\\d+)/', $amazon_price, $matches);
            if (isset($matches[0][0])) {
                $amazon_price = $matches[0][0];
            }
            //$azonprice=substr(trim($azonprice),1,strlen(trim($azonprice)));
            $amazon_price = number_format((double) $amazon_price, 2, '.', '');
            $ebay_price = number_format((double) $ebay_price, 2, '.', '');
            //$azonprice=number_format((float)$azonprice, 2, '.', '');
            $amazon_quantity = $result['quantity'];
            $profit = $ebay_price * 0.87 - $amazon_price;
            if ($profit <= 0) {
                $profit = 5.0;
            }
            $sql = "SELECT * FROM ebay_asin WHERE asins='{$asin}'";
            $rs = mysql_query($sql);
            echo $asin;
            die;
            if (!mysql_num_rows($rs)) {
                $insert = "INSERT INTO ebay_asin SET UserID={$active_user},asins='{$asin}',item_id={$item_id},ebay_title='{$title}',ebay_price= {$ebay_price},amazon_price={$amazon_price},profit_ratio={$profit},quantity={$quantity},amazon_quantity={$amazon_quantity},max_quantity={$quantity},in_ebay=1,in_amazon=1";
                mysql_query($insert);
            }
            $sql = "SELECT * FROM user_products WHERE SKU='{$sku}'";
            $rs = mysql_query($sql);
            if (!mysql_num_rows($rs)) {
                $insert = "INSERT INTO user_products SET UserID={$active_user},ItemID='{$item_id}',Qty={$quantity},Price={$ebay_price},Title='{$title}',SKU='{$sku}',Image='{$image_url}',ItemUrl='{$item_url}',AmazonPrice={$amazon_price},ProfitRatio={$profit},AmazonQty={$amazon_quantity},MaxQty={$quantity}";
                mysql_query($insert);
            }
        }
    }
    //sleep(1);
    unset($insert);
}