function cw_serials_check_product($product_id, $customer_id = 0)
{
    global $tables, $config;
    $is_required = cw_serials_is_auto_generated($product_id);
    if (!$is_required) {
        return false;
    }
    if ($customer_id) {
        $warehouse_condition = "and warehouse_customer_id='{$customer_id}'";
    }
    $avails = cw_query("select sum(avail) as amount, warehouse_customer_id from {$tables['products_warehouses_amount']} where product_id='{$product_id}' {$warehouse_condition} group by warehouse_customer_id");
    $return = array();
    if ($avails) {
        foreach ($avails as $val) {
            $count_unused = cw_query_first_cell("select count(*) from {$tables['serial_numbers']} where product_id='{$product_id}' and warehouse='{$val['warehouse_customer_id']}' and doc_id=0");
            if ($count_unused < $val['amount']) {
                for ($i = 0; $i < $val['amount'] - $count_unused; $i++) {
                    do {
                        $number = cw_serials_get_number($config['sn']['auto_product_number']);
                        if (cw_query_first_cell("select count(*) from {$tables['serial_numbers']} where sn ='{$number}' and product_id='{$product_id}'")) {
                            $number = false;
                        }
                    } while (!$number);
                    $return[] = $number;
                    cw_serials_add($val['warehouse_customer_id'], $product_id, $number);
                }
            }
        }
    }
    return $return;
}
 $ean_products = array();
 $saved_products_info = array();
 foreach ($eans as $ean) {
     if (empty($ean['ean'])) {
         continue;
     }
     $result = $ean;
     if (!$saved_products_info[$ean['ean']]) {
         $saved_products_info[$ean['ean']] = cw_ean_get_product_info($ean['ean']);
     }
     $product_info =& $saved_products_info[$ean['ean']];
     if ($product_info['product_id']) {
         $result['product_info'] = $product_info;
         $product_id = $product_info['product_id'];
         if ($addons['sn']) {
             $result['auto_generated'] = cw_serials_is_auto_generated($product_id);
             if ($result['auto_generated']) {
                 $result['numbers'] = implode(" ", cw_serials_check_product($product_id));
             } elseif ($ean['serial'] && $ean['serial'] != $default_number) {
                 $res = cw_serials_add($user_account['warehouse_customer_id'], $product_id, $ean['serial']);
                 $result['numbers'] = $ean['serial'];
                 if (!$res) {
                     $result['error'] = 2;
                 }
             }
         }
         if (!$result['error']) {
             $current_avail = cw_warehouse_get_warehouse_avail($user_account['warehouse_customer_id'], $product_info['product_id'], null, $product_info['variant_id']);
             $insert = array();
             $insert['avail'] = intval($current_avail) + 1;
             $insert['warehouse_customer_id'] = $user_account['warehouse_customer_id'];