function cw_on_warehouse_recalculate($product_id)
{
    global $tables;
    if (cw_product_has_variants($product_id)) {
        $to_update = cw_query_first("select sum(avail) as avail, sum(avail_ordered) as avail_ordered, sum(avail_sold) as avail_sold, sum(avail_reserved) as avail_reserved from {$tables['products_warehouses_amount']} where product_id='{$product_id}' and warehouse_customer_id = 0 and variant_id != 0");
        $to_update['warehouse_customer_id'] = 0;
        $to_update['product_id'] = $product_id;
        $to_update['variant_id'] = 0;
        cw_call('cw_warehouse_insert_avail', array('insert' => $to_update));
    }
}
        while ($pid = cw_group_edit_each($ge_id, 100, $product_id)) {
            cw_image_delete($pid, 'products_images_det');
        }
    }
    cw_refresh($product_id);
}
if ($product_id) {
    $smarty->assign('main', 'product_modify');
} else {
    $smarty->assign('main', 'product_add');
}
if ($REQUEST_METHOD == "POST" && ($action == "product_modify" || $action == 'add') && (AREA_TYPE == 'A' || AREA_TYPE == 'V')) {
    $is_variant = false;
    # kornev, TOFIX
    if ($product_id && $addons['product_options']) {
        $is_variant = cw_product_has_variants($product_id);
    }
    $last_added_product_type = $product_data['product_type'];
    if (!$product_data['eancode'] && $addons['barcode'] && $config['barcode']['gen_product_code']) {
        $product_data['eancode'] = cw_product_generate_sku($config['barcode']['gen_product_code'], 'eancode');
    }
    cw_ean_clear($product_data['eancode']);
    if ($mode == 'add') {
        if (!$product_data['productcode']) {
            $product_data['productcode'] = cw_product_generate_sku();
        }
        if (!$product_data['membership_ids']) {
            $product_data['membership_ids'] = array_keys(unserialize($config['product_settings']['default_product_memberships']));
        }
    }
    $rules = array('category_id' => '', 'product' => '', 'productcode' => array('func' => 'cw_error_sku_exists'), 'manufacturer_code' => array('func' => 'cw_error_manufacturer_code_exists'));