function cw_accounting_update_stock($doc_data, $product, $way, $field, $destination_warehouse_id = 0, $affect_prices = false, $movements_id = 0, $source_pwa_id = 0)
{
    global $addons, $tables;
    cw_load('product');
    # kornev
    # way = 1, the products has been purchased - we have to create new record with supplier
    #          ps: the record can be already created... if a few movements have to be generated
    # way = 2, the products has been sold - we have to decrease the most old records
    # kornev
    # if we are making the warehouse movements, we have to increase the products in one and decrease in another and visa versa
    $warehouse_customer_id = $doc_data['info']['warehouse_customer_id'];
    if ($destination_warehouse_id) {
        $warehouse_customer_id = $destination_warehouse_id;
        $way = $way == 2 ? 1 : 2;
    }
    $variant_id = 0;
    # kornev, TOFIX
    if ($addons['product_options'] && (!empty($product['extra_data']['product_options']) || !empty($product['options']))) {
        $options = !empty($product['extra_data']['product_options']) ? $product['extra_data']['product_options'] : $product['options'];
        $variant_id = cw_get_variant_id($options);
    }
    $return = array();
    if ($way == 1) {
        $return = $product['amount'];
    } elseif ($way == 2) {
        $return = -$product['amount'];
        if ($field == 'avail' && in_array($doc_data['type'], array('O', 'I', 'G', 'S'))) {
            cw_call('cw_product_run_counter', array('product_id' => $product['product_id'], 'count' => $return, 'type' => 1));
        }
    }
    //    cw_warehouse_check_avail_record($warehouse_customer_id, $product['product_id'], $variant_id);
    if ($return) {
        db_query("update {$tables['products_warehouses_amount']} set {$field} = {$field} + {$return} where product_id='{$product['product_id']}' and warehouse_customer_id=0 and variant_id='{$variant_id}'");
        cw_event('on_accounting_update_stock', array($product, $variant_id, $field, $return));
    }
    cw_warehouse_recalculate($product['product_id'], $variant_id);
    cw_func_call('cw_product_build_flat', array('product_id' => $product['product_id']));
    return $return;
}
             while ($pid = cw_group_edit_each($ge_id, 1, $product_id)) {
                 if ($pid != $product_id) {
                     cw_price_lists_replace_price($pid, $product_data['price'], 0);
                 }
             }
         }
     }
     cw_func_call('cw_items_attribute_classes_save', array('item_id' => $product_id, 'attribute_class_ids' => $product_data['attribute_class_ids'], 'item_type' => 'P'));
     # kornev, it have to be product_data here - because we change the attributes in the error_check function
     cw_call('cw_attributes_save', array('item_id' => $product_id, 'item_type' => 'P', 'attributes' => $product_data['attributes'], 'language' => $edited_language, array('update_posted_only' => true, 'is_default' => false)));
     cw_attributes_group_update($ge_id, $product_id, 'P', $fields);
     cw_func_call('cw_product_build_flat', array('product_id' => $product_id));
     cw_group_edit_end($product_id);
     cw_product_update_system_info($product_id, array('supplier_customer_id' => $product_data['supplier']));
     cw_group_edit_copy_system_info($product_id, array('supplier_customer_id' => $product_data['supplier']));
     cw_warehouse_recalculate($product_id);
     cw_product_filter_recalculate_price_ranges();
     // tags
     if (!empty($product_data['tags'])) {
         $tags = explode(',', $product_data['tags']);
         cw_tags_set_product_tags($tags, $product_id);
     } else {
         cw_tags_clear_product_tags($product_id);
     }
 } else {
     cw_add_top_message($fillerror, 'E');
     $product_modified_data = $product_data;
     $product_modified_data['product_id'] = $product_id;
     cw_core_process_date_fields($product_modified_data, null, array('' => array('membership_ids', 'status')));
     if ($file_upload_data['products_images_thumb']) {
         $file_upload_data['products_images_thumb']['is_redirect'] = false;