function cw_get_product_variants($product_id, $membership_id = 0, $area = false) { global $tables, $current_area, $current_language, $keys, $cart, $user_account, $addons, $user_account; cw_load('files', 'taxes'); $keys = cw_get_hash_options($product_id); if ($area === false) { $area = $current_area; } if ($area != 'C' || !$addons['wholesale_trading']) { $products_prices_membership = "= 0"; } else { $products_prices_membership = "IN (0, '{$user_account['membership_id']})')"; } $fields[] = "{$tables['products_warehouses_amount']}.avail"; $fields[] = "{$tables['products_warehouses_amount']}.avail_ordered"; $fields[] = "{$tables['products_warehouses_amount']}.avail_sold"; $fields[] = "{$tables['products_warehouses_amount']}.avail_reserved"; if ($current_area == 'C') { $sql = "SELECT {$tables['product_variants']}.*, {$tables['products_prices']}.price, IF({$tables['products_images_var']}.id IS NULL, '', 'Y') as is_image, {$tables['products_images_var']}.image_path as image_path_W, " . implode(", ", $fields) . " FROM {$tables['product_variants']} LEFT JOIN {$tables['products_prices']} ON {$tables['product_variants']}.product_id = {$tables['products_prices']}.product_id AND {$tables['products_prices']}.variant_id = {$tables['product_variants']}.variant_id AND {$tables['products_prices']}.membership_id {$products_prices_membership} AND {$tables['products_prices']}.quantity = 1 LEFT JOIN {$tables['products_warehouses_amount']} on {$tables['products_warehouses_amount']}.product_id={$tables['product_variants']}.product_id and {$tables['products_warehouses_amount']}.variant_id={$tables['product_variants']}.variant_id and {$tables['products_warehouses_amount']}.warehouse_customer_id='" . (AREA_TYPE == 'P' ? $user_account['warehouse_customer_id'] : 0) . "' LEFT JOIN {$tables['products_images_var']} ON {$tables['products_images_var']}.id = {$tables['product_variants']}.variant_id WHERE {$tables['product_variants']}.product_id = '{$product_id}' GROUP BY {$tables['product_variants']}.variant_id"; } else { $sql = "SELECT {$tables['product_variants']}.*, {$tables['products_prices']}.price, IF({$tables['products_images_var']}.id IS NULL, '', 'Y') as is_image, {$tables['products_images_var']}.image_path as image_path_W, " . implode(", ", $fields) . " FROM {$tables['product_variants']} LEFT JOIN {$tables['products_prices']} ON {$tables['product_variants']}.product_id = {$tables['products_prices']}.product_id AND {$tables['products_prices']}.variant_id = {$tables['product_variants']}.variant_id LEFT JOIN {$tables['products_warehouses_amount']} on {$tables['products_warehouses_amount']}.product_id={$tables['product_variants']}.product_id and {$tables['products_warehouses_amount']}.variant_id={$tables['product_variants']}.variant_id and {$tables['products_warehouses_amount']}.warehouse_customer_id='" . (AREA_TYPE == 'P' ? $user_account['warehouse_customer_id'] : 0) . "' LEFT JOIN {$tables['products_images_var']} ON {$tables['products_images_var']}.id = {$tables['product_variants']}.variant_id WHERE {$tables['product_variants']}.product_id = '{$product_id}' GROUP BY {$tables['product_variants']}.variant_id"; } $variants = cw_query_hash($sql, 'variant_id', false); if (!$variants) { return false; } if ($area == 'C') { # Check variants' items $counts = cw_query_column("SELECT COUNT({$tables['product_variant_items']}.option_id) FROM {$tables['product_variant_items']}, {$tables['product_variants']}, {$tables['product_options_values']}, {$tables['product_options']} WHERE {$tables['product_variant_items']}.variant_id = {$tables['product_variants']}.variant_id AND {$tables['product_variants']}.product_id = '{$product_id}' AND {$tables['product_variant_items']}.option_id = {$tables['product_options_values']}.option_id AND {$tables['product_options']}.product_option_id= {$tables['product_options_values']}.product_option_id AND {$tables['product_options_values']}.avail = 1 AND {$tables['product_options']}.avail = 1 GROUP BY {$tables['product_variant_items']}.variant_id"); if (empty($counts) || count($counts) < count($variants)) { return false; } else { $counts = array_unique($counts); if (count($counts) != 1) { return false; } } $chains = cw_query_hash("SELECT {$tables['product_variant_items']}.* FROM {$tables['product_variant_items']}, {$tables['product_variants']}, {$tables['product_options_values']}, {$tables['product_options']} WHERE {$tables['product_variant_items']}.variant_id = {$tables['product_variants']}.variant_id AND {$tables['product_variants']}.product_id = '{$product_id}' AND {$tables['product_variant_items']}.option_id = {$tables['product_options_values']}.option_id AND {$tables['product_options']}.product_option_id = {$tables['product_options_values']}.product_option_id AND {$tables['product_options_values']}.avail = 1 AND {$tables['product_options']}.avail = 1", "variant_id", true, true); } else { $chains = cw_query_hash("SELECT {$tables['product_variant_items']}.* FROM {$tables['product_variant_items']}, {$tables['product_variants']}, {$tables['product_options_values']} WHERE {$tables['product_variant_items']}.variant_id = {$tables['product_variants']}.variant_id AND {$tables['product_variants']}.product_id = '{$product_id}' AND {$tables['product_variant_items']}.option_id = {$tables['product_options_values']}.option_id", "variant_id", true, true); } if (empty($chains)) { return false; } # Get variants' wholesale prices $prices = array(); if ($addons['wholesale_trading']) { $products_prices_membership = ""; $min_amount = 1; if ($area == 'C') { $min_amount = intval(cw_query_first_cell("SELECT min_amount FROM {$tables['products']} WHERE product_id = '{$product_id}'")); $products_prices_membership = "AND membership_id IN (0, '{$user_account['membership_id']}')"; } $prices = cw_query_hash("select *, price from {$tables['products_prices']} as pps where pps.product_id = '{$product_id}' AND pps.variant_id > 0 {$products_prices_membership} GROUP BY variant_id, quantity, membership_id ORDER BY quantity", "variant_id"); if (!empty($prices)) { foreach ($prices as $vid => $ps) { $last_key = false; foreach ($ps as $pid => $p) { cw_unset($ps[$pid], "product_id"); if ($last_key !== false) { $ps[$last_key]['next_quantity'] = $p['quantity']; if ($area == 'C') { if ($min_amount > $ps[$last_key]['next_quantity']) { unset($ps[$last_key]); } elseif ($min_amount > $ps[$last_key]['quantity']) { $ps[$last_key]['quantity'] = $min_amount; } } } $last_key = $pid; } if (empty($ps)) { unset($prices[$vid]); continue; } $ps[$pid]['next_quantity'] = 0; $prices[$vid] = $ps; } } } $product = cw_query_first("SELECT product_id, free_shipping, shipping_freight, distribution, free_tax FROM {$tables['products']} WHERE product_id='{$product_id}'"); $taxes = cw_get_product_tax_rates($product, $user_account); foreach ($variants as $kv => $variant) { # Get references to option array if (empty($chains[$kv])) { if ($area == "C") { unset($variants[$kv]); } continue; } # Get wholesale prices if (isset($prices[$kv])) { $variants[$kv]['wholesale'] = $prices[$kv]; $variants[$kv]['wholesale'][0]['price'] = $variant['price']; unset($prices[$kv]); if ($area == 'C') { $last_price = $variant['price']; foreach ($variants[$kv]['wholesale'] as $wpk => $wpv) { if ($wpv['price'] > $last_price) { unset($variants[$kv]['wholesale'][$wpk]); continue; } $last_price = $wpv['price']; } if (empty($variants[$kv]['wholesale'])) { unset($variants[$kv]['wholesale']); } else { $variants[$kv]['wholesale'] = array_values($variants[$kv]['wholesale']); } } } if ($area == "C") { if ($variant['is_image'] == 'Y') { $variants[$kv]['image'] = cw_image_get('products_images_var', $kv); } # Get variant's tax rates $_taxes = cw_tax_price($variant['price'], 0, true, NULL, "", $taxes); $variants[$kv]['taxed_price'] = $_taxes['taxed_price']; if (!empty($_taxes['taxes'])) { $variants[$kv]['taxes'] = $_taxes['taxes']; } if (!empty($variants[$kv]['wholesale'])) { # Get variant's wholesale prices' tax rates foreach ($variants[$kv]['wholesale'] as $k => $v) { $_taxes = cw_tax_price($v['price'], 0, true, NULL, "", $taxes); $variants[$kv]['wholesale'][$k]['taxed_price'] = $_taxes['taxed_price']; if (!empty($_taxes['taxes'])) { $variants[$kv]['wholesale'][$k]['taxes'] = $_taxes['taxes']; } } } if (!empty($cart['products']) && is_array($cart['products'])) { foreach ($cart['products'] as $v) { if ($v['product_id'] != $product_id) { continue; } if ($kv == cw_get_variant_id($v['options'], $product_id)) { $variants[$kv]['avail'] -= $v['amount']; } } } } elseif ($variant['is_image'] == 'Y') { $variants[$kv]['image'] = cw_image_get('products_images_var', $kv); } $variants[$kv]['options'] = array(); foreach ($chains[$kv] as $oid) { $variants[$kv]['options'][$oid] = $keys[$oid]; } if (empty($variants[$kv]['options']) && $area == "C") { unset($variants[$kv]); continue; } } return $variants; }
function cw_barcode_print_product($product_id, $options) { global $tables, $user_account, $current_area; # kornev, not required to restore... // $current_area = 'C'; $layout = cw_web_get_layout_by_id($options['template_id']); $product = cw_func_call('cw_product_get', array('id' => $product_id, 'user_account' => $user_account, 'info_type' => 17)); if ($layout['data']['use_tax']) { $taxes = cw_get_product_tax_rates($product, $user_info, true, true); cw_get_products_taxes($product, $user_account, false, $taxes, true); $_tmp_price = $product['price']; $product['price'] = $product['list_price']; cw_get_products_taxes($product, $user_account, false, $taxes, true); $product['list_price'] = $product['display_price']; $product['price'] = $_tmp_price; } $amount = $options['amount']; if (!$amount) { $amount = 1; } for ($i = 0; $i < $amount; $i++) { $products[] = $product; } cw_barcode_print($products, $layout, $options); }
function cw_tax_price($price, $user_info = '', $product_id = 0, $disable_abs = false, $discounted_price = NULL, $taxes = "", $price_deducted_tax = false) { global $tables, $config, $addons, $current_language; $return_taxes = array(); $no_discounted_price = false; if (empty($discounted_price)) { $discounted_price = $price; $no_discounted_price = true; } if ($product_id > 0) { $product = cw_query_first("SELECT product_id, free_shipping, shipping_freight, distribution, '{$price}' as price FROM {$tables['products']} WHERE product_id='{$product_id}'"); $taxes = cw_get_product_tax_rates($product, $user_info); } $total_tax_cost = 0; if (is_array($taxes)) { # # Calculate price and tax_value # foreach ($taxes as $k => $tax_rate) { if (!$tax_rate['price_includes_tax'] || $price_deducted_tax) { if (!$price_includes_tax || $price_deducted_tax) { continue; } } if (!preg_match("!\\b(DST|ST)\\b!S", $tax_rate['formula'])) { continue; } if ($tax_rate['rate_type'] == "%") { $_tax_value = $price - $price * 100 / ($tax_rate['rate_value'] + 100); $price -= $_tax_value; if ($discounted_price > 0) { $_tax_value = $discounted_price - $discounted_price * 100 / ($tax_rate['rate_value'] + 100); } $discounted_price -= $_tax_value; } else { $price -= $tax_rate['rate_value']; $discounted_price -= $tax_rate['rate_value']; } } $taxed_price = $discounted_price; $formula_data['ST'] = $price; if (!$no_discounted_price) { $formula_data['DST'] = $discounted_price; } foreach ($taxes as $k => $v) { if (!empty($v['skip'])) { continue; } if (!$v['display_including_tax']) { continue; } if ($v['rate_type'] == "%") { $assessment = cw_cart_calc_assessment($v['formula'], $formula_data); $tax_value = $assessment * $v['rate_value'] / 100; } elseif (!$disable_abs) { $tax_value = $v['rate_value']; } $formula_data[$v['tax_name']] = $tax_value; $total_tax_cost += $tax_value; $taxed_price += $tax_value; $return_taxes['taxes'][$v['tax_id']] = $tax_value; } } $return_taxes['taxed_price'] = $taxed_price; return $return_taxes; }