function cw_distribute_discount($field_name, $products, $discount, $discount_type, $avail_discount_total = 0, $taxes = array()) { global $config; $sum_discount = 0; $return = array(); $_orig_discount = $taxed_discount = $discount; if (!empty($taxes) && $config['Taxes']['display_taxed_order_totals'] == "Y" && $config['Taxes']['apply_discount_on_taxed_amount'] == "Y") { if ($discount_type == "absolute") { $_taxes = cw_tax_price($discount, 0, false, NULL, "", $taxes, false); $taxed_discount = $_taxes['net_price']; } else { $_taxes = cw_tax_price($discount, 0, false, NULL, "", $taxes, true); $taxed_discount = $_taxes['taxed_price']; } } if ($discount_type == "absolute" && $avail_discount_total > 0) { # Distribute absolute discount among the products $index = 0; $_considered_sum_discount = 0; $_total_discounted_products = 0; foreach ($products as $k => $product) { if (@$product['deleted']) { continue; } if ($product['hidden']) { continue; } $_total_discounted_products++; } foreach ($products as $k => $product) { if (@$product['deleted']) { continue; } if ($product['hidden']) { continue; } $index++; if ($field_name == "coupon_discount" || $product['discount_avail']) { $koefficient = $product['price'] / $avail_discount_total; if ($index < $_total_discounted_products) { $products[$k][$field_name] = $taxed_discount * $koefficient * $product['amount']; $products[$k]["taxed_" . $field_name] = $taxed_discount * $koefficient * $product['amount']; $_considered_sum_discount += $products[$k][$field_name]; $_considered_sum_taxed_discount += $products[$k]["taxed_" . $field_name]; } else { $products[$k][$field_name] = $taxed_discount - $_considered_sum_discount; $products[$k]["taxed_" . $field_name] = $taxed_discount - $_considered_sum_taxed_discount; } $products[$k]['discounted_price'] = max($products[$k]['discounted_price'] - $products[$k][$field_name], 0.0); } } } elseif ($discount_type == "percent") { # Distribute percent discount among the products foreach ($products as $k => $product) { if (@$product['deleted']) { continue; } if ($product['hidden']) { continue; } if ($field_name == "coupon_discount" || $product['discount_avail']) { $products[$k][$field_name] = $product['price'] * $discount / 100 * $product['amount']; if ($taxed_discount != $discount) { if ($product['display_price'] > 0) { $_price = $product['display_price']; } else { $_price = $product['taxed_price']; } $products[$k]["taxed_" . $field_name] = $_price * $_orig_discount / 100 * $product['amount']; } else { $products[$k]["taxed_" . $field_name] = $products[$k][$field_name]; } $products[$k]['discounted_price'] = max($product['discounted_price'] - $products[$k][$field_name], 0.0); } } } foreach ($products as $product) { if ($product['hidden']) { continue; } $sum_discount += $product["taxed_" . $field_name]; } if ($discount_type == "absolute" && $sum_discount > $discount) { $sum_discount = $discount; } if ($discount_type == "percent") { $return[$field_name . "_orig"] = $sum_discount; } else { $return[$field_name . "_orig"] = $_orig_discount; } $return['products'] = $products; $return[$field_name] = $sum_discount; return $return; }
function cw_discount_coupons_cart_calc_discounts($params, $return) { global $tables, $config, $addons, $global_store; extract($params); $coupon = $cart['info']['coupon']; foreach ($products as $k => $product) { if ($product['hidden']) { continue; } $products[$k]['coupon_discount'] = 0; } $return['coupon_discount'] = 0; $return['coupon'] = 0; if (!empty($coupon)) { $coupon_total = 0; $coupon_amount = 0; cw_load('salesman'); $is_salesman_coupon = cw_is_salesman_coupon($discount_coupon); if ($is_salesman_coupon) { $discount_coupon_data = cw_query_first("select * from {$tables['discount_coupons']} where coupon='{$discount_coupon}'"); if ($discount_coupon_data['from_account']) { $discount_coupon_data['discount'] = cw_get_salesman_discount($products, $discount_coupon, $membership_id, $warehouse); $discount_coupon_data['coupon_type'] = 'absolute'; } } else { if (!empty($global_store['discount_coupons'])) { $discount_coupon_data = array(); foreach ($global_store['discount_coupons'] as $v) { if ($v['__override'] || $v['coupon'] == $coupon && $v['warehouse_customer_id'] == $warehouse) { $discount_coupon_data = $v; break; } } } else { $discount_coupon_data = cw_query_first("select * from {$tables['discount_coupons']} where coupon='{$coupon}'"); } } $return['discount_coupon_data'] = $discount_coupon_data; $return['coupon_type'] = $discount_coupon_data['coupon_type']; if (!empty($discount_coupon_data) && ($discount_coupon_data['coupon_type'] == 'absolute' || $discount_coupon_data['coupon_type'] == 'percent')) { $coupon_discount = 0; if ($discount_coupon_data['product_id'] > 0) { foreach ($products as $k => $product) { if ($product['product_id'] != $discount_coupon_data['product_id']) { continue; } $price = $product['discounted_price']; if ($discount_coupon_data['coupon_type'] == 'absolute' && $discount_coupon_data['discount'] > $price) { $discount_coupon_data['discount'] = 100; $discount_coupon_data['coupon_type'] = 'percent'; } if ($discount_coupon_data['coupon_type'] == 'absolute' && $discount_coupon_data['apply_product_once'] == 0) { $multiplier = $product['amount']; } else { $multiplier = 1; } $_coupon_discount = $_taxed_coupon_discount = $discount_coupon_data['discount'] * $multiplier; if ($config['Taxes']['apply_discount_on_taxed_amount'] == "Y" && !empty($product['taxes']) && is_array($product['taxes'])) { $_taxes = cw_tax_price($_coupon_discount, 0, false, NULL, '', $product['taxes'], $discount_coupon_data['coupon_type'] == 'percent'); $_taxed_coupon_discount = $_taxes['taxed_price']; $_coupon_discount = $_taxes['net_price']; } if ($discount_coupon_data['coupon_type'] == 'absolute') { $taxed_coupon_discount = $_taxed_coupon_discount; $taxed_coupon_discount = $coupon_discount = $_coupon_discount; } else { $taxed_coupon_discount = $price * $_taxed_coupon_discount / 100; $coupon_discount = $price * $_coupon_discount / 100; } $products[$k]['coupon_discount'] = $taxed_coupon_discount; $products[$k]['discounted_price'] = max($price - $coupon_discount, 0.0); $return['coupon_discount'] += $taxed_coupon_discount; } } elseif ($discount_coupon_data['category_id'] > 0) { $category_ids[] = $discount_coupon_data['category_id']; if ($discount_coupon_data['recursive']) { $category_ids = cw_category_get_subcategory_ids($discount_coupon_data['category_id']); } if ($discount_coupon_data['coupon_type'] == 'absolute') { foreach ($products as $k => $product) { if ($config['Appearance']['categories_in_products'] == '1') { $product_categories = cw_query("SELECT category_id FROM {$tables['products_categories']} WHERE product_id='{$product['product_id']}'"); $is_valid_product = false; foreach ($product_categories as $pc) { if (in_array($pc['category_id'], $category_ids)) { $is_valid_product = true; break; } } } if ($is_valid_product) { if ($discount_coupon_data['coupon_type'] == "absolute" && ~$discount_coupon_data['apply_product_once']) { $multiplier = $product['amount']; } else { $multiplier = 1; } $sum_discount += $discount_coupon_data['discount'] * $multiplier; } } if ($sum_discount > $return['total']) { $discount_coupon_data['discount'] = 100; $discount_coupon_data['coupon_type'] = 'percent'; } } foreach ($products as $k => $product) { if ($config['Appearance']['categories_in_products'] == '1') { $product_categories = cw_query("SELECT category_id FROM {$tables['products_categories']} WHERE product_id='{$product['product_id']}'"); $is_valid_product = false; foreach ($product_categories as $pc) { if (in_array($pc['category_id'], $category_ids)) { $is_valid_product = true; break; } } } if ($is_valid_product) { if ($discount_coupon_data['coupon_type'] == "absolute" && $discount_coupon_data['apply_product_once'] == "N") { $multiplier = $product['amount']; } else { $multiplier = 1; } $_coupon_discount = $_taxed_coupon_discount = $discount_coupon_data['discount'] * $multiplier; if ($config['Taxes']['apply_discount_on_taxed_amount'] == "Y" && !empty($product['taxes']) && is_array($product['taxes'])) { $_taxes = cw_tax_price($_coupon_discount, 0, false, NULL, "", $product['taxes'], $discount_coupon_data['coupon_type'] == "percent"); $_taxed_coupon_discount = $_taxes['taxed_price']; $_coupon_discount = $_taxes['net_price']; } $price = $product['discounted_price']; if ($discount_coupon_data['coupon_type'] == "absolute") { $taxed_coupon_discount = $_taxed_coupon_discount; $coupon_discount = $_coupon_discount; } else { $taxed_coupon_discount = $price * $_taxed_coupon_discount / 100; $coupon_discount = $price * $_coupon_discount / 100; } $taxed_coupon_discount = $taxed_coupon_discount; $products[$k]['coupon_discount'] = $taxed_coupon_discount; $products[$k]['discounted_price'] = max($price - $coupon_discount, 0.0); $return['coupon_discount'] += $taxed_coupon_discount; if ($discount_coupon_data['coupon_type'] == "absolute" && $discount_coupon_data['apply_category_once'] == "Y") { break; } } } } else { if ($discount_coupon_data['coupon_type'] == 'absolute' && $discount_coupon_data['discount'] > $return['total']) { $discount_coupon_data['discount'] = 100; $discount_coupon_data['coupon_type'] = 'percent'; } if ($discount_coupon_data['coupon_type'] == 'absolute') { $return['coupon_discount'] = $discount_coupon_data['discount']; } elseif ($discount_coupon_data['coupon_type'] == 'percent') { $return['coupon_discount'] = $return['total'] * $discount_coupon_data['discount'] / 100; } $updated = cw_distribute_discount("coupon_discount", $products, $discount_coupon_data['discount'], $discount_coupon_data['coupon_type'], $return['total'], $_taxes); extract($updated); unset($updated); $return['coupon_discount'] = $coupon_discount; } } if (isset($coupon_discount_orig)) { $return['coupon_discount_orig'] = $coupon_discount_orig; } else { $return['coupon_discount_orig'] = $return['coupon_discount']; } $return['products'] = $products; } return $return; }
function cw_ac_get_product_info($product_id) { global $user_account, $addons, $tables, $config; $product_id = intval($product_id); if (empty($product_id)) { return false; } $product = cw_func_call('cw_product_get', array('id' => $product_id, 'info_type' => 8 | 32 | 64 | 128 | 2048, 'user_account' => $user_account)); if (!(!empty($product) && is_array($product))) { return false; } if (!empty($addons['wholesale_trading'])) { $allowed_membership_ids = array(0); $membership_id = intval($user_account['membership_id']); if (!empty($membership_id)) { $allowed_membership_ids[] = $membership_id; } $query = "SELECT pp.quantity, pp.price " . "FROM {$tables['products_prices']} AS pp " . "WHERE pp.product_id = '" . $product_id . "' AND pp.membership_id IN ('" . implode("','", $allowed_membership_ids) . "') " . "AND pp.quantity > 1 AND pp.variant_id = 0 " . "GROUP BY pp.quantity " . "ORDER BY pp.quantity"; $wholesale_prices = cw_query($query); if (!empty($wholesale_prices) && is_array($wholesale_prices)) { $query = "SELECT MIN(pp.price) " . "FROM {$tables['products_prices']} AS pp " . "WHERE pp.quantity = 1 AND pp.membership_id IN ('" . implode("','", $allowed_membership_ids) . "') " . "AND pp.variant_id = 0 AND pp.product_id = '" . $product_id . "'"; $min_price = doubleval(cw_query_first_cell($query)); $prev_key = false; foreach ($wholesale_prices as $k => $wholesale_price) { if (doubleval($wholesale_price['price']) > $min_price) { unset($wholesale_prices[$k]); continue; } $min_price = doubleval($wholesale_price['price']); $wholesale_taxes = cw_tax_price(intval($wholesale_price['price']), $user_account, $product_id); $wholesale_prices[$k]['taxed_price'] = $wholesale_taxes['taxed_price']; $wholesale_prices[$k]['taxes'] = $wholesale_taxes['taxes']; if ($prev_key !== false && isset($wholesale_prices[$prev_key])) { $wholesale_prices[$prev_key]['next_quantity'] = intval($wholesale_price['quantity']) - 1; if (intval($product_accessory['min_amount']) > intval($wholesale_prices[$prev_key]['next_quantity'])) { unset($wholesale_prices[$prev_key]); } elseif (intval($product_accessory['min_amount']) > intval($wholesale_price['quantity'])) { $wholesale_prices[$prev_key]['quantity'] = intval($product_accessory['min_amount']); } } $prev_key = $k; } $wholesale_prices = array_values($wholesale_prices); $product['product_wholesale'] = $wholesale_prices; } } return $product; }
<?php cw_load('taxes'); $price_lists_where = "0, '{$user_account['memebrship_id']}'"; $wresult = cw_query("SELECT {$tables['products_prices']}.quantity, {$tables['products_prices']}.price FROM {$tables['products_prices']} where {$tables['products_prices']}.product_id='{$product_id}' AND {$tables['products_prices']}.membership_id IN ({$price_lists_where}) AND {$tables['products_prices']}.quantity > 1 AND {$tables['products_prices']}.variant_id = 0 group by {$tables['products_prices']}.quantity order by {$tables['products_prices']}.quantity"); if ($wresult) { $last_price = doubleval(cw_query_first_cell("SELECT MIN(price) FROM {$tables['products_prices']} WHERE quantity = 1 AND membership_id IN ({$price_lists_where}) AND variant_id = 0 AND product_id = '{$product_id}'")); $last_k = false; foreach ($wresult as $wk => $wv) { if ($wv['price'] > $last_price) { unset($wresult[$wk]); continue; } $last_price = $wv['price']; $_taxes = cw_tax_price($wv['price'], $user_info, $product_id); $wresult[$wk]['taxed_price'] = $_taxes['taxed_price']; $wresult[$wk]['taxes'] = $_taxes['taxes']; if ($last_k !== false && isset($wresult[$last_k])) { $wresult[$last_k]['next_quantity'] = $wv['quantity'] - 1; if ($product_info['min_amount'] > $wresult[$last_k]['next_quantity']) { unset($wresult[$last_k]); } elseif ($product_info['min_amount'] > $wresult[$last_k]['quantity']) { $wresult[$last_k]['quantity'] = $product_info['min_amount']; } } $last_k = $wk; } $wresult = array_values($wresult); if (count($wresult) > 0) { $wresult[count($wresult) - 1]['next_quantity'] = 0; $smarty->assign("product_wholesale", $wresult);
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; }