function cw_products_from_scratch($scratch_products, $user_info, $persistent_products, $leave_info = false) { global $addons, $tables, $config, $app_main_dir; global $current_area, $current_language, $customer_id; cw_load('image'); $products = array(); if (empty($scratch_products)) { return $products; } $pids = array(); foreach ($scratch_products as $product_data) { $pids[] = $product_data['product_id']; } $int_res = cw_query_hash("SELECT * FROM {$tables['products_lng']} WHERE code = '{$current_language}' AND product_id IN ('" . implode("','", $pids) . "')", "product_id", false); unset($pids); cw_event('on_before_products_from_scratch', array(&$scratch_products)); $hash = array(); cw_load('warehouse'); foreach ($scratch_products as $product_data) { $product_id = $product_data['product_id']; $cartid = $product_data['cartid']; $amount = $product_data['amount']; $variant_id = $product_data['variant_id']; $warehouse = $product_data['warehouse_customer_id']; if (!cw_warehouse_is_customer($customer_id, $warehouse)) { continue; } if (!is_numeric($amount)) { $amount = 0; } $options = $product_data['options']; $product_options = false; $variant = array(); # kornev, TOFIX if ($addons['product_options'] && !empty($options) && is_array($options)) { if (!cw_check_product_options($product_id, $options)) { continue; } list($variant, $product_options) = cw_get_product_options_data($product_id, $options, $membership_id); if (empty($variant_id) && isset($variant['variant_id'])) { $variant_id = $variant['variant_id']; } } $fields[] = "p.*"; # kornev, supplier has got it's own prices if ($current_area != 'S') { $fields[] = "min(pq.price) as price"; } $fields[] = 'avail'; $status = cw_core_get_required_status($current_area); $products_array = cw_func_call('cw_product_get', array('id' => $product_id, 'variant_id' => $variant_id, 'amount' => $amount, 'user_account' => $user_info, 'info_type' => 8192)); //cw_query_first($sql="select ".implode(', ', $fields)." from $tables[products] as p, $tables[products_prices] as pq, $tables[products_enabled] as pe left join $tables[products_warehouses_amount] as pwa on pwa.product_id=pe.product_id and pwa.variant_id='$variant_id' and pwa.warehouse_customer_id='$warehouse' WHERE p.product_id= pe.product_id and pe.product_id=pq.product_id AND pe.status in (".implode(", ", $status).") AND pe.product_id='$product_id' AND pq.quantity<='$amount' AND pq.membership_id IN(0, '$user_info[membership_id]') AND pq.variant_id = '$variant_id' ORDER BY pq.quantity DESC"); $unlimited_products = true; if ($products_array['avail'] < $amount && in_array($current_area, array('G', 'C'))) { $unlimited_products = cw_query_first_cell("select backorder & " . ($current_area == 'G' ? 2 : 1) . " from {$tables['warehouse_divisions']} where division_id = '{$warehouse}'"); if (!$unlimited_products) { $amount = $products_array['avail']; } } if ($products_array) { $products_array = cw_array_merge($product_data, $products_array); if ($leave_info) { $products_array['price'] = abs($product_data['price']); } $products_array['warehouse_customer_id'] = $warehouse; $hash_key = $product_id . "|" . $warehouse; cw_event('on_product_from_scratch', array(&$products_array)); # # If priduct's price is 0 then use customer-defined price # $free_price = false; if ($products_array['price'] == 0) { $free_price = true; $products_array['taxed_price'] = $products_array['price'] = price_format($product_data['free_price'] ? $product_data['free_price'] : 0); } # kornev, TOFIX if ($addons['product_options'] && $options) { if (!empty($variant)) { # kornev, it's not allow to set the variant price. // unset($variant['price']); if (is_null($variant['pimage_path'])) { cw_unset($variant, "pimage_path", "pimage_x", "pimage_y"); } else { $variant['is_pimage'] = 'W'; } $products_array = cw_array_merge($products_array, $variant); } $hash_key .= "|" . $products_array['variant_id']; if ($product_options === false) { unset($product_options); } else { $variant['price'] = $products_array['price']; $variant['cost'] = $products_array['cost']; $products_array['options_surcharge'] = 0; $products_array['cost_surcharge'] = 0; if ($product_options) { foreach ($product_options as $o) { $products_array['options_surcharge'] += $o['modifier_type'] ? $products_array['price'] * $o['price_modifier'] / 100 : $o['price_modifier']; $products_array['cost_surcharge'] += $o['cost_modifier_type'] ? $products_array['cost'] * $o['cost_modifier'] / 100 : $o['cost_modifier']; } } } } if (!$unlimited_products && !$persistent_products && $products_array['avail'] - $hash[$hash_key] < $amount) { continue; } # Get thumbnail's URL (uses only if images stored in FS) $products_array['image_thumb'] = cw_image_get('products_images_thumb', $product_id); $products_array['price'] += $products_array['options_surcharge']; $products_array['cost'] += $products_array['cost_surcharge']; if ($products_array['price'] < 0) { $products_array['price'] = 0; } if ($products_array['cost'] < 0) { $products_array['cost'] = 0; } if (in_array($current_area, array('C', 'G'))) { $products_array['taxes'] = cw_get_products_taxes($products_array, $user_info, false, '', $current_area == 'G' && $customer_info['usertype'] != 'R'); if ($config['Taxes']['display_taxed_order_totals'] == 'Y') { $products_array['display_price'] = $products_array['taxed_price']; $products_array['display_net_price'] = $products_array['taxed_net_price']; } else { $products_array['display_price'] = $products_array['price']; $products_array['display_net_price'] = $products_array['net_price']; } } $products_array['total'] = $amount * $products_array['price']; $products_array['product_options'] = $product_options; $products_array['options'] = $options; $products_array['amount'] = $amount; $products_array['cartid'] = $cartid; $products_array['product_orig'] = $products_array['product']; if (isset($int_res[$product_id])) { $products_array['product'] = stripslashes($int_res[$product_id]['product']); $products_array['descr'] = stripslashes($int_res[$product_id]['descr']); $products_array['fulldescr'] = stripslashes($int_res[$product_id]['fulldescr']); cw_unset($int_res, $product_id); } if ($products_array['descr'] == strip_tags($products_array['descr'])) { $products_array['descr'] = str_replace("\n", "<br />", $products_array['descr']); } if ($products_array['fulldescr'] == strip_tags($products_array['fulldescr'])) { $products_array['fulldescr'] = str_replace("\n", "<br />", $products_array['fulldescr']); } // Order hash defines how all products in cart will be split by orders // Listen for the event and return own part of hash $order_hash = cw_event('on_build_order_hash', array($products_array), array()); $order_hash[] = 'W' . $products_array['warehouse_customer_id']; $products_array['order_hash'] = join('-', $order_hash); $products[] = $products_array; $hash[$hash_key] += $amount; } } //cw_var_dump($products); return $products; }
$ids[] = addslashes($v['customer_id']); } $counts = cw_query_hash("SELECT {$tables['wishlist']}.customer_id, COUNT({$tables['products']}.product_id) as products_count FROM {$tables['wishlist']}, {$tables['products']} WHERE {$tables['wishlist']}.product_id = {$tables['products']}.product_id AND {$tables['wishlist']}.customer_id IN ('" . implode("','", $ids) . "') GROUP BY {$tables['wishlist']}.customer_id", "customer_id", false, true); foreach ($wishlists as $k => $v) { $wishlists[$k]['products_count'] = intval($counts[$v['customer_id']]); } $smarty->assign('wishlists', $wishlists); } # Display wishlist } elseif ($mode == "wishlist" && $customer) { $wishlist = cw_query("SELECT * FROM {$tables['wishlist']}, {$tables['products']}, {$tables['customers']} WHERE {$tables['wishlist']}.product_id = {$tables['products']}.product_id AND {$tables['wishlist']}.customer_id={$tables['customers']}.customer_id AND {$tables['wishlist']}.customer_id='{$customer}' "); if (empty($wishlist)) { cw_header_location("index.php?target=wishlists"); } foreach ($wishlist as $k => $v) { if (!empty($v['options'])) { $v['options'] = unserialize($v['options']); list($variant, $v['product_options']) = cw_get_product_options_data($v['product_id'], $v['options'], $v['membership_id']); if (!empty($variant)) { $v = cw_array_merge($v, $variant); } $wishlist[$k] = $v; } } $location[count($location) - 1][1] = "wishlists.php"; $location[] = array(cw_get_langvar_by_name("lbl_wish_list"), ""); $smarty->assign('wishlist', $wishlist); $smarty->assign('main', "wishlist"); } $smarty->assign('mode', $mode); $smarty->assign('search_data', $search_data);
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; }
function cw_doc_get($doc_id, $info_type = 0) { global $tables; global $config, $addons; global $app_main_dir; global $smarty; cw_load('warehouse', 'profile_fields'); $doc = cw_query_first("select * from {$tables['docs']} where doc_id='{$doc_id}'"); $doc['info'] = cw_query_first("select * from {$tables['docs_info']} where doc_info_id='{$doc['doc_info_id']}'"); $doc['info']['carrier'] = cw_call('cw_shipping_get_carrier', array($doc['info']['shipping_id'])); $doc['info']['applied_taxes'] = unserialize($doc['info']['applied_taxes']); $doc['info']['extra'] = unserialize($doc['info']['extra']); $doc['info']['extras'] = cw_call('cw_doc_get_extras_data', array($doc_id)); $doc['userinfo'] = cw_query_first("select * from {$tables['docs_user_info']} where doc_info_id='{$doc['doc_info_id']}'"); $doc['userinfo']['main_address'] = cw_user_get_address(null, $doc['userinfo']['main_address_id']); $doc['userinfo']['current_address'] = cw_user_get_address(null, $doc['userinfo']['current_address_id']); $fields_area = cw_profile_fields_get_area($doc['userinfo']['customer_id'], $doc['userinfo']['membership_id']); list($profile_sections, $profile_fields, $additional_fields) = cw_profile_fields_get_sections('U', true, $fields_area); $doc['userinfo']['profile_sections'] = $profile_sections; $doc['userinfo']['profile_fields'] = $profile_fields; $doc['related_docs'] = cw_doc_get_related($doc_id); $doc['settings'] = cw_doc_get_settings($doc['doc_info_id']); if ($addons['egoods']) { $join .= " left join {$tables['download_keys']} ON {$tables['docs_items']}.item_id={$tables['download_keys']}.item_id AND {$tables['download_keys']}.product_id={$tables['docs_items']}.product_id"; $fields .= ", {$tables['download_keys']}.download_key, {$tables['download_keys']}.expires"; } $join .= " left join {$tables['products_system_info']} on {$tables['products_system_info']}.product_id={$tables['docs_items']}.product_id"; $fields .= ", {$tables['products_system_info']}.supplier_customer_id"; $products = cw_query("select {$tables['products']}.*, {$tables['products']}.productcode as sku, {$tables['docs_items']}.*, IF({$tables['products']}.product_id IS NULL, 'Y', '') as is_deleted, IF({$tables['docs_items']}.product = '', {$tables['products']}.product, {$tables['docs_items']}.product) as product {$fields} FROM {$tables['docs_items']} LEFT JOIN {$tables['products']} ON {$tables['docs_items']}.product_id = {$tables['products']}.product_id {$join} WHERE {$tables['docs_items']}.doc_id='{$doc_id}'"); $products = cw_doc_translate_products($products, $doc['info']['language']); $is_returns = false; cw_load('warehouse'); $gift_doc_ids = cw_doc_get_related_docs($doc_id); $gift_doc_ids = array_unique(array_merge($gift_doc_ids, array($doc_id))); $giftcerts = cw_query("SELECT * {$gc_add_date} FROM {$tables['giftcerts']} WHERE doc_id in ('" . implode(',', $gift_doc_ids) . "')"); if (!empty($giftcerts) && $config['General']['use_counties'] == "Y") { foreach ($giftcerts as $k => $v) { if (!empty($v['recipient_county'])) { $giftcerts[$k]['recipient_countyname'] = cw_get_county($v['recipient_county']); } } } $doc['giftcerts'] = $giftcerts; if ($doc['info']['giftcert_ids']) { $doc['info']['applied_giftcerts'] = explode('*', $doc['info']['giftcert_ids']); if ($doc['info']['applied_giftcerts']) { $tmp = array(); foreach ($doc['info']['applied_giftcerts'] as $k => $v) { if (empty($v)) { continue; } list($arr['giftcert_id'], $arr['giftcert_cost']) = explode(':', $v); $tmp[] = $arr; } $doc['info']['applied_giftcerts'] = $tmp; } } $doc['is_returns'] = $is_returns; if (cw_query_first_cell("select count(*) from {$tables['docs_items']}, {$tables['download_keys']} WHERE {$tables['docs_items']}.doc_id = '{$doc_id}' and {$tables['download_keys']}.item_id = {$tables['docs_items']}.item_id ")) { $doc['is_egood'] = 'Y'; } elseif (cw_query_first_cell("select count(*) from {$tables['docs_items']}, {$tables['products']} WHERE {$tables['docs_items']}.doc_id = '{$doc_id}' and {$tables['docs_items']}.product_id={$tables['products']}.product_id AND {$tables['products']}.distribution != ''")) { $doc['is_egood'] = 'E'; } if (preg_match("/(free_ship|percent|absolute)(?:``)(.+)/S", $doc['coupon'], $found)) { $doc['coupon'] = $found[2]; $doc['coupon_type'] = $found[1]; } $order['info']['extra']['tax_info']['product_tax_name'] = ''; $_products_taxes = array(); if ($products) { foreach ($products as $k => $v) { if ($addons['sn']) { $v['serial_numbers'] = cw_query("select * from {$tables['docs_items_serials']} where item_id='{$v['item_id']}'"); } $v['product_options_txt'] = $v['product_options']; if ($v['extra_data']) { $v['extra_data'] = unserialize($v['extra_data']); if (is_array(@$v['extra_data']['display'])) { foreach ($v['extra_data']['display'] as $i => $j) { $v["display_" . $i] = $j; } } if (is_array($v['extra_data']['taxes'])) { foreach ($v['extra_data']['taxes'] as $i => $j) { if ($j['tax_value'] > 0) { $_products_taxes[$i] = $j['tax_display_name']; } } } } $v['original_price'] = $v['ordered_price'] = $v['price']; $v['price_deducted_tax'] = "Y"; if ($v['is_deleted'] != 'Y') { $v['original_price'] = cw_query_first_cell("SELECT {$tables['products_prices']}.price FROM {$tables['products_prices']} WHERE {$tables['products_prices']}.product_id = '{$v['product_id']}' AND {$tables['products_prices']}.membership_id IN (0, '{$userinfo['membership_id']}') AND {$tables['products_prices']}.quantity <= '{$v['amount']}' AND {$tables['products_prices']}.variant_id = 0"); # kornev, TOFIX if ($addons['product_options'] && $v['extra_data']['product_options']) { list($variant, $product_options) = cw_get_product_options_data($v['product_id'], $v['extra_data']['product_options'], $userinfo['membership_id']); if ($product_options === false) { unset($product_options); } else { if (empty($variant['price'])) { $variant['price'] = $v['original_price']; } $v['original_price'] = $variant['price']; unset($variant['price']); if ($product_options) { foreach ($product_options as $o) { if ($o['modifier_type'] == '%') { $v['original_price'] += $v['original_price'] * $o['price_modifier'] / 100; } else { $v['original_price'] += $o['price_modifier']; } } } $v['product_options'] = $product_options; # Check current and saved product options set if (!empty($v['product_options_txt'])) { $flag_txt = true; # Check saved product options $count = 0; foreach ($v['product_options'] as $opt) { if (preg_match("/" . preg_quote($opt['class'], "/") . ": " . preg_quote($opt['option_name'], "/") . "/Sm", $v['product_options_txt'])) { $count++; } } if ($count != count($v['product_options'])) { $flag_txt = false; } # Check current product options set if ($flag_txt) { $count = 0; $tmp = explode("\n", $v['product_options_txt']); foreach ($tmp as $txt_row) { if (!preg_match("/^([^:]+): (.*)\$/S", trim($txt_row), $match)) { continue; } foreach ($v['product_options'] as $opt) { if ($match[1] == $opt['option_name'] && $match[2] == trim($opt['name'])) { $count++; break; } } } if ($count != count($tmp)) { $flag_txt = false; } } # Force display saved product options set # if saved and current product options sets wasn't equal if (!$flag_txt) { $v['force_product_options_txt'] = true; } } if (!empty($variant)) { $v = cw_array_merge($v, $variant); } } } } $products[$k] = $v; } } $doc['products'] = $products; if (count($_products_taxes) == 1) { $order['info']['extra']['tax_info']['product_tax_name'] = array_pop($_products_taxes); } if ($order['coupon_type'] == "free_ship") { $order['shipping_cost'] = $order['coupon_discount']; $order['discounted_subtotal'] += $order['coupon_discount']; } $order['discounted_subtotal'] = price_format($order['discounted_subtotal']); # kornev, 512 is free for now (something was removed) if ($info_type & 1024 && $addons['pos']) { cw_load('pos'); $doc['pos'] = cw_pos_get_doc_info($doc['doc_info_id']); } if ($info_type & 2048) { $doc['warehouse'] = cw_warehouse_get_like_user($doc['info']['warehouse_customer_id'], $doc['info']['warehouse_customer_id']); } if ($info_type & 4096) { $doc['quotes'] = cw_doc_get_quotes($doc_id); } if ($info_type & 8192) { $doc['info']['details'] = text_decrypt($doc['info']['details']); } if ($info_type & 1) { $doc['attributes'] = cw_func_call('cw_attributes_get', array('item_id' => $doc_id, 'item_type' => 'O')); } return $doc; }
# kornev # pos orders && supplier orders are unlimited if ($v['amount'] > 0) { $aom_orders[$doc_id]['products'][$index]['amount'] = $config['unlimited_products'] ? $v['amount'] : min($v['amount'], $count_product_in_stock); } $v['price'] = cw_aom_validate_price($v['price']); if ($config['Taxes']['display_taxed_order_totals'] == 'Y') { $v['price'] = cw_taxes_price_without_tax($v['price'], $aom_orders[$doc_id]['products'][$index]['taxes']); } $product_options_result = array(); # kornev, TOFIX if ($v['product_options'] && $addons['product_options']) { if (!cw_check_product_options($product_id, $v['product_options'])) { $v['product_options'] = cw_get_default_options($product_id, $v['amount'], $aom_orders[$doc_id]['userinfo']['membership_id']); } list($variant, $product_options_result) = cw_get_product_options_data($product_id, $v['product_options'], $aom_orders[$doc_id]['userinfo']['membership_id']); $aom_orders[$doc_id]['products'][$index]['options_surcharge'] = 0; if ($product_options_result) { foreach ($product_options_result as $key => $o) { $aom_orders[$doc_id]['products'][$index]['options_surcharge'] += $o['modifier_type'] == '%' ? $v['price'] * $o['price_modifier'] / 100 : $o['price_modifier']; } } } $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