function cw_aom_add_new_products(&$doc, $products, $variants = array(), $amounts = array(), $discounts = array(), $prices = array())
{
    global $tables, $config, $customer_id, $current_area, $addons;
    $saved_data = compact('customer_id', 'current_area');
    $customer_id = $doc['userinfo']['customer_id'];
    # kornev
    # pos, warehouse movement, supplier order or usual sale
    if (in_array($doc['type'], array('G', 'D'))) {
        $current_area = 'G';
    } elseif (in_array($doc['type'], array('P', 'Q', 'R'))) {
        $current_area = 'S';
    } else {
        $current_area = 'C';
    }
    $customer_membership_id = $doc['userinfo']['membership_id'];
    $out_of_stock_products = array();
    if (is_array($products)) {
        foreach ($products as $index => $newproduct_id) {
            if (empty($newproduct_id)) {
                continue;
            }
            if ($prd = cw_func_call('cw_product_get', array('id' => $newproduct_id, 'user_account' => $doc['userinfo'], 'info_type' => 9))) {
                if ($prices[$index]) {
                    $prd['price'] = $prices[$index];
                }
                #kornev, salesman can create the doc with any warehouses, because it's only cart (customer will place the real doc later)
                if (AREA_TYPE != 'B') {
                    $_cart_warehouse = $doc['info']['warehouse_customer_id'];
                    if (!$_cart_warehouse) {
                        $_cart_warehouse = cw_warehouse_get_max_amount_warehouse($newproduct_id, $variants[$index]);
                        $doc['info']['warehouse_customer_id'] = $_cart_warehouse;
                    }
                    $_avail = cw_warehouse_get_warehouse_avail($_cart_warehouse, $newproduct_id, null, $variants[$index]);
                    if (!$_avail && !$config['unlimited_products']) {
                        $out_of_stock_products[] = array(1, $prd['product']);
                        continue;
                    }
                    $prd['avail'] = $_avail;
                    $prd['warehouse_customer_id'] = $_cart_warehouse;
                }
                # kornev, TOFIX
                if ($addons['product_options']) {
                    $prd['extra_data']['product_options'] = cw_get_default_options($newproduct_id, 1, $customer_membership_id, $variants[$index]);
                    list($variant, $product_options_result) = cw_get_product_options_data($newproduct_id, $prd['extra_data']['product_options'], $customer_membership_id);
                    $surcharge = 0;
                    $prd['product_options'] = $product_options_result;
                    if ($product_options_result) {
                        foreach ($product_options_result as $key => $o) {
                            $surcharge += $o['modifier_type'] == '%' ? $prd['price'] * $o['price_modifier'] / 100 : $o['price_modifier'];
                        }
                    }
                    $prd['price'] = price_format($prd['price'] + $surcharge);
                }
                if ($discounts[$index]) {
                    list($discount, $is_persent) = cw_core_parse_discount($discounts[$index]);
                    if ($discount) {
                        if ($is_persent == '%') {
                            $prd['price'] = $prd['price'] * (100 - $discount) / 100;
                        } else {
                            $prd['price'] = $prd['price'] - $discount;
                        }
                        if ($prd['price'] < 0) {
                            $prd['price'] = 0;
                        }
                    }
                }
                $prd['amount'] = intval($amounts[$index]) ? intval($amounts[$index]) : 1;
                $prd['new'] = true;
                if (empty($doc['max_cartid'])) {
                    $doc['max_cartid'] = 0;
                }
                $doc['max_cartid']++;
                $prd['cartid'] = $doc['max_cartid'];
                if (in_array($doc['type'], array('P', 'Q', 'R'))) {
                    $prd['is_auto_calc'] = $config['docs']['is_auto_calc'] == 'Y';
                    # kornev, if we are modify the supplier order, we should get the supplier price instead of the usual one (get latest)
                    /*
                                    cw_load('supplier');
                                    $supplier_net_price = cw_supplier_get_price($doc['userinfo']['customer_id'], $prd['product_id']);
                    /*
                                    if (!$supplier_net_price)
                                        $out_of_stock_products[] = array(3, $newproduct_id);
                                    else {
                    */
                    $prd['net_price'] = $supplier_net_price;
                    //                    $prd['is_net_price'] = $supplier_net_price;
                    $prd['discount_formula'] = cw_query_first_cell("select discount from {$tables['products_supplied_amount']} where product_id='{$prd['product_id']}' and supplier_customer_id='" . $doc['userinfo']['customer_id'] . "' order by date asc");
                    if (!$prd['discount_formula']) {
                        $prd['discount_formula'] = cw_user_get_discount_formula($doc['userinfo']['customer_id']);
                    }
                    $prd['price'] = cw_user_apply_discount_by_formula($prd['discount_formula'], $supplier_net_price);
                    $spl = cw_supplier_get_product_info($doc['userinfo']['customer_id'], $prd['product_id']);
                    $prd['productcode'] = $spl['productcode'];
                    $doc['products'][] = $prd;
                    //                }
                } else {
                    $doc['products'][] = $prd;
                }
                unset($prd);
            } else {
                $out_of_stock_products[] = array(2, $newproduct_id);
            }
        }
    }
    cw_load('cart_process');
    $doc['products'] = cw_call('cw_products_in_cart', array($doc, $doc['userinfo'], true));
    cw_cart_normalize($doc);
    extract($saved_data);
    return $out_of_stock_products;
}
         }
     }
 }
 $is_copy_price = false;
 if (in_array($aom_orders[$doc_id]['type'], array('P', 'Q', 'R')) && !$aom_orders[$doc_id]['products'][$index]['is_net_price']) {
     $is_copy_price = true;
 }
 if ($current_area == 'G' && !$accl['100001']) {
     $v['price'] = $aom_orders[$doc_id]['products'][$index]['net_price'];
 }
 // net_price is a price before update, I hope
 $aom_orders[$doc_id]['products'][$index]['net_price'] = $aom_orders[$doc_id]['products'][$index]['price'];
 if ($current_area == 'G' && $accl['100000'] || $current_area != 'G') {
     if ($v['use_discount']) {
         $aom_orders[$doc_id]['products'][$index]['discount_formula'] = $v['discount'];
         $v['price'] = cw_user_apply_discount_by_formula($v['discount'], $v['price']);
         $is_copy_price = false;
     } elseif ($aom_orders[$doc_id]['products'][$index]['price'] != $v['price']) {
         $aom_orders[$doc_id]['products'][$index]['discount_formula'] = '';
     }
 } else {
     $aom_orders[$doc_id]['products'][$index]['discount_formula'] = '';
 }
 $aom_orders[$doc_id]['products'][$index]['price'] = $v['price'];
 $aom_orders[$doc_id]['products'][$index]['product_options'] = $product_options_result;
 $aom_orders[$doc_id]['products'][$index]['extra_data']['product_options'] = $v['product_options'];
 if ($is_copy_price) {
     $aom_orders[$doc_id]['products'][$index]['net_price'] = $aom_orders[$doc_id]['products'][$index]['price'];
 }
 if ($v['productcode']) {
     if ($aom_orders[$doc_id]['type'] == 'D' && $aom_orders[$doc_id]['products'][$index]['productcode'] != $v['productcode']) {