[phpBB Debug] PHP Warning: in file [ROOT]/includes/crs/crs_misc_functions.php on line 37: mime_content_type(): Empty filename or path
[phpBB Debug] PHP Warning: in file [ROOT]/includes/crs/crs_misc_functions.php on line 37: mime_content_type(): Empty filename or path
Zen Cart 源代码 functions_prices.php

Zen Cart 源代码 functions_prices.php




下载文件

文件名: functions_prices.php
文件类型: PHP文件
文件大小: 53.53 KiB
MD5: 6e0129c5d69f9aec08c9958d3286ce27

functions_prices.php - 关闭高亮
  1. <?php
  2. /**
  3.  * functions_prices
  4.  *
  5.  * @package functions
  6.  * @copyright Copyright 2003-2011 Zen Cart Development Team
  7.  * @copyright Portions Copyright 2003 osCommerce
  8.  * @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0
  9.  * @version GIT: $Id: Author: ajeh  Wed Jun 26 11:54:36 2013 -0400 Modified in v1.5.2 $
  10.  */
  11.  
  12. ////
  13. //get specials price or sale price
  14.   function zen_get_products_special_price($product_id, $specials_price_only=false) {
  15.     global $db;
  16.     $product = $db->Execute("select products_price, products_model, products_priced_by_attribute from " . TABLE_PRODUCTS . " where products_id = '" . (int)$product_id . "'");
  17.  
  18.     if ($product->RecordCount() > 0) {
  19. //        $product_price = $product->fields['products_price'];
  20.       $product_price = zen_get_products_base_price($product_id);
  21.     } else {
  22.       return false;
  23.     }
  24.  
  25.     $specials = $db->Execute("select specials_new_products_price from " . TABLE_SPECIALS . " where products_id = '" . (int)$product_id . "' and status='1'");
  26.     if ($specials->RecordCount() > 0) {
  27. //      if ($product->fields['products_priced_by_attribute'] == 1) {
  28.         $special_price = $specials->fields['specials_new_products_price'];
  29.     } else {
  30.       $special_price = false;
  31.     }
  32.  
  33.     if(substr($product->fields['products_model'], 0, 4) == 'GIFT') {    //Never apply a salededuction to Ian Wilson's Giftvouchers
  34.       if (zen_not_null($special_price)) {
  35.         return $special_price;
  36.       } else {
  37.         return false;
  38.       }
  39.     }
  40.  
  41. // return special price only
  42.     if ($specials_price_only==true) {
  43.       if (zen_not_null($special_price)) {
  44.         return $special_price;
  45.       } else {
  46.         return false;
  47.       }
  48.     } else {
  49. // get sale price
  50.  
  51. // changed to use master_categories_id
  52. //      $product_to_categories = $db->Execute("select categories_id from " . TABLE_PRODUCTS_TO_CATEGORIES . " where products_id = '" . (int)$product_id . "'");
  53. //      $category = $product_to_categories->fields['categories_id'];
  54.  
  55.       $product_to_categories = $db->Execute("select master_categories_id from " . TABLE_PRODUCTS . " where products_id = '" . $product_id . "'");
  56.       $category = $product_to_categories->fields['master_categories_id'];
  57.  
  58.       $sale = $db->Execute("select sale_specials_condition, sale_deduction_value, sale_deduction_type from " . TABLE_SALEMAKER_SALES . " where sale_categories_all like '%," . $category . ",%' and sale_status = '1' and (sale_date_start <= now() or sale_date_start = '0001-01-01') and (sale_date_end >= now() or sale_date_end = '0001-01-01') and (sale_pricerange_from <= '" . $product_price . "' or sale_pricerange_from = '0') and (sale_pricerange_to >= '" . $product_price . "' or sale_pricerange_to = '0')");
  59.       if ($sale->RecordCount() < 1) {
  60.          return $special_price;
  61.       }
  62.  
  63.       if (!$special_price) {
  64.         $tmp_special_price = $product_price;
  65.       } else {
  66.         $tmp_special_price = $special_price;
  67.       }
  68.       switch ($sale->fields['sale_deduction_type']) {
  69.         case 0:
  70.           $sale_product_price = $product_price - $sale->fields['sale_deduction_value'];
  71.           $sale_special_price = $tmp_special_price - $sale->fields['sale_deduction_value'];
  72.           break;
  73.         case 1:
  74.           $sale_product_price = $product_price - (($product_price * $sale->fields['sale_deduction_value']) / 100);
  75.           $sale_special_price = $tmp_special_price - (($tmp_special_price * $sale->fields['sale_deduction_value']) / 100);
  76.           break;
  77.         case 2:
  78.           $sale_product_price = $sale->fields['sale_deduction_value'];
  79.           $sale_special_price = $sale->fields['sale_deduction_value'];
  80.           break;
  81.         default:
  82.           return $special_price;
  83.       }
  84.  
  85.       if ($sale_product_price < 0) {
  86.         $sale_product_price = 0;
  87.       }
  88.  
  89.       if ($sale_special_price < 0) {
  90.         $sale_special_price = 0;
  91.       }
  92.  
  93.       if (!$special_price) {
  94.         return number_format($sale_product_price, 4, '.', '');
  95.       } else {
  96.         switch($sale->fields['sale_specials_condition']){
  97.           case 0:
  98.             return number_format($sale_product_price, 4, '.', '');
  99.             break;
  100.           case 1:
  101.             return number_format($special_price, 4, '.', '');
  102.             break;
  103.           case 2:
  104.             return number_format($sale_special_price, 4, '.', '');
  105.             break;
  106.           default:
  107.             return number_format($special_price, 4, '.', '');
  108.         }
  109.       }
  110.     }
  111.   }
  112.  
  113.  
  114. ////
  115. // computes products_price + option groups lowest attributes price of each group when on
  116.   function zen_get_products_base_price($products_id) {
  117.     global $db;
  118.       $product_check = $db->Execute("select products_price, products_priced_by_attribute from " . TABLE_PRODUCTS . " where products_id = '" . (int)$products_id . "'");
  119.  
  120. // is there a products_price to add to attributes
  121.       $products_price = $product_check->fields['products_price'];
  122.  
  123.       // do not select display only attributes and attributes_price_base_included is true
  124.       $product_att_query = $db->Execute("select options_id, price_prefix, options_values_price, attributes_display_only, attributes_price_base_included, round(concat(price_prefix, options_values_price), 5) as value from " . TABLE_PRODUCTS_ATTRIBUTES . " where products_id = '" . (int)$products_id . "' and attributes_display_only != '1' and attributes_price_base_included='1'". " order by options_id, value");
  125.  
  126.       $the_options_id= 'x';
  127.       $the_base_price= 0;
  128. // add attributes price to price
  129.       if ($product_check->fields['products_priced_by_attribute'] == '1' and $product_att_query->RecordCount() >= 1) {
  130.         while (!$product_att_query->EOF) {
  131.           if ( $the_options_id != $product_att_query->fields['options_id']) {
  132.             $the_options_id = $product_att_query->fields['options_id'];
  133.             $the_base_price += (($product_att_query->fields['price_prefix'] == '-') ? -1 : 1) * $product_att_query->fields['options_values_price'];
  134.           }
  135.           $product_att_query->MoveNext();
  136.         }
  137.  
  138.         $the_base_price = $products_price + $the_base_price;
  139.       } else {
  140.         $the_base_price = $products_price;
  141.       }
  142.       return $the_base_price;
  143.   }
  144.  
  145.  
  146. ////
  147. // Display Price Retail
  148. // Specials and Tax Included
  149.   function zen_get_products_display_price($products_id) {
  150.     global $db, $currencies;
  151.  
  152.     $free_tag = "";
  153.     $call_tag = "";
  154.  
  155. // 0 = normal shopping
  156. // 1 = Login to shop
  157. // 2 = Can browse but no prices
  158.     // verify display of prices
  159.       switch (true) {
  160.         case (CUSTOMERS_APPROVAL == '1' and $_SESSION['customer_id'] == ''):
  161.         // customer must be logged in to browse
  162.         return '';
  163.         break;
  164.         case (CUSTOMERS_APPROVAL == '2' and $_SESSION['customer_id'] == ''):
  165.         // customer may browse but no prices
  166.         return TEXT_LOGIN_FOR_PRICE_PRICE;
  167.         break;
  168.         case (CUSTOMERS_APPROVAL == '3' and TEXT_LOGIN_FOR_PRICE_PRICE_SHOWROOM != ''):
  169.         // customer may browse but no prices
  170.         return TEXT_LOGIN_FOR_PRICE_PRICE_SHOWROOM;
  171.         break;
  172.         case ((CUSTOMERS_APPROVAL_AUTHORIZATION != '0' and CUSTOMERS_APPROVAL_AUTHORIZATION != '3') and $_SESSION['customer_id'] == ''):
  173.         // customer must be logged in to browse
  174.         return TEXT_AUTHORIZATION_PENDING_PRICE;
  175.         break;
  176.         case ((CUSTOMERS_APPROVAL_AUTHORIZATION != '0' and CUSTOMERS_APPROVAL_AUTHORIZATION != '3') and $_SESSION['customers_authorization'] > '0'):
  177.         // customer must be logged in to browse
  178.         return TEXT_AUTHORIZATION_PENDING_PRICE;
  179.         break;
  180.         default:
  181.         // proceed normally
  182.         break;
  183.       }
  184.  
  185. // show case only
  186.     if (STORE_STATUS != '0') {
  187.       if (STORE_STATUS == '1') {
  188.         return '';
  189.       }
  190.     }
  191.  
  192.     // $new_fields = ', product_is_free, product_is_call, product_is_showroom_only';
  193.     $product_check = $db->Execute("select products_tax_class_id, products_price, products_priced_by_attribute, product_is_free, product_is_call, products_type from " . TABLE_PRODUCTS . " where products_id = '" . (int)$products_id . "'" . " limit 1");
  194.  
  195.     // no prices on Document General
  196.     if ($product_check->fields['products_type'] == 3) {
  197.       return '';
  198.     }
  199.  
  200.     $show_display_price = '';
  201.     $display_normal_price = zen_get_products_base_price($products_id);
  202.     $display_special_price = zen_get_products_special_price($products_id, true);
  203.     $display_sale_price = zen_get_products_special_price($products_id, false);
  204.  
  205.     $show_sale_discount = '';
  206.     if (SHOW_SALE_DISCOUNT_STATUS == '1' and ($display_special_price != 0 or $display_sale_price != 0)) {
  207.       if ($display_sale_price) {
  208.         if (SHOW_SALE_DISCOUNT == 1) {
  209.           if ($display_normal_price != 0) {
  210.             $show_discount_amount = number_format(100 - (($display_sale_price / $display_normal_price) * 100),SHOW_SALE_DISCOUNT_DECIMALS);
  211.           } else {
  212.             $show_discount_amount = '';
  213.           }
  214.           $show_sale_discount = '<span class="productPriceDiscount">' . '<br />' . PRODUCT_PRICE_DISCOUNT_PREFIX . $show_discount_amount . PRODUCT_PRICE_DISCOUNT_PERCENTAGE . '</span>';
  215.  
  216.         } else {
  217.           $show_sale_discount = '<span class="productPriceDiscount">' . '<br />' . PRODUCT_PRICE_DISCOUNT_PREFIX . $currencies->display_price(($display_normal_price - $display_sale_price), zen_get_tax_rate($product_check->fields['products_tax_class_id'])) . PRODUCT_PRICE_DISCOUNT_AMOUNT . '</span>';
  218.         }
  219.       } else {
  220.         if (SHOW_SALE_DISCOUNT == 1) {
  221.           $show_sale_discount = '<span class="productPriceDiscount">' . '<br />' . PRODUCT_PRICE_DISCOUNT_PREFIX . number_format(100 - (($display_special_price / $display_normal_price) * 100),SHOW_SALE_DISCOUNT_DECIMALS) . PRODUCT_PRICE_DISCOUNT_PERCENTAGE . '</span>';
  222.         } else {
  223.           $show_sale_discount = '<span class="productPriceDiscount">' . '<br />' . PRODUCT_PRICE_DISCOUNT_PREFIX . $currencies->display_price(($display_normal_price - $display_special_price), zen_get_tax_rate($product_check->fields['products_tax_class_id'])) . PRODUCT_PRICE_DISCOUNT_AMOUNT . '</span>';
  224.         }
  225.       }
  226.     }
  227.  
  228.     if ($display_special_price) {
  229.       $show_normal_price = '<span class="normalprice">' . $currencies->display_price($display_normal_price, zen_get_tax_rate($product_check->fields['products_tax_class_id'])) . ' </span>';
  230.       if ($display_sale_price && $display_sale_price != $display_special_price) {
  231.         $show_special_price = '&nbsp;' . '<span class="productSpecialPriceSale">' . $currencies->display_price($display_special_price, zen_get_tax_rate($product_check->fields['products_tax_class_id'])) . '</span>';
  232.         if ($product_check->fields['product_is_free'] == '1') {
  233.           $show_sale_price = '<br />' . '<span class="productSalePrice">' . PRODUCT_PRICE_SALE . '<s>' . $currencies->display_price($display_sale_price, zen_get_tax_rate($product_check->fields['products_tax_class_id'])) . '</s>' . '</span>';
  234.         } else {
  235.           $show_sale_price = '<br />' . '<span class="productSalePrice">' . PRODUCT_PRICE_SALE . $currencies->display_price($display_sale_price, zen_get_tax_rate($product_check->fields['products_tax_class_id'])) . '</span>';
  236.         }
  237.       } else {
  238.         if ($product_check->fields['product_is_free'] == '1') {
  239.           $show_special_price = '&nbsp;' . '<span class="productSpecialPrice">' . '<s>' . $currencies->display_price($display_special_price, zen_get_tax_rate($product_check->fields['products_tax_class_id'])) . '</s>' . '</span>';
  240.         } else {
  241.           $show_special_price = '&nbsp;' . '<span class="productSpecialPrice">' . $currencies->display_price($display_special_price, zen_get_tax_rate($product_check->fields['products_tax_class_id'])) . '</span>';
  242.         }
  243.         $show_sale_price = '';
  244.       }
  245.     } else {
  246.       if ($display_sale_price) {
  247.         $show_normal_price = '<span class="normalprice">' . $currencies->display_price($display_normal_price, zen_get_tax_rate($product_check->fields['products_tax_class_id'])) . ' </span>';
  248.         $show_special_price = '';
  249.         $show_sale_price = '<br />' . '<span class="productSalePrice">' . PRODUCT_PRICE_SALE . $currencies->display_price($display_sale_price, zen_get_tax_rate($product_check->fields['products_tax_class_id'])) . '</span>';
  250.       } else {
  251.         if ($product_check->fields['product_is_free'] == '1') {
  252.           $show_normal_price = '<s>' . $currencies->display_price($display_normal_price, zen_get_tax_rate($product_check->fields['products_tax_class_id'])) . '</s>';
  253.         } else {
  254.           $show_normal_price = $currencies->display_price($display_normal_price, zen_get_tax_rate($product_check->fields['products_tax_class_id']));
  255.         }
  256.         $show_special_price = '';
  257.         $show_sale_price = '';
  258.       }
  259.     }
  260.  
  261.     if ($display_normal_price == 0) {
  262.       // don't show the $0.00
  263.       $final_display_price = $show_special_price . $show_sale_price . $show_sale_discount;
  264.     } else {
  265.       $final_display_price = $show_normal_price . $show_special_price . $show_sale_price . $show_sale_discount;
  266.     }
  267.  
  268.     // If Free, Show it
  269.     if ($product_check->fields['product_is_free'] == '1') {
  270.       if (OTHER_IMAGE_PRICE_IS_FREE_ON=='0') {
  271.         $free_tag = '<br />' . PRODUCTS_PRICE_IS_FREE_TEXT;
  272.       } else {
  273.         $free_tag = '<br />' . zen_image(DIR_WS_TEMPLATE_IMAGES . OTHER_IMAGE_PRICE_IS_FREE, PRODUCTS_PRICE_IS_FREE_TEXT);
  274.       }
  275.     }
  276.  
  277.     // If Call for Price, Show it
  278.     if ($product_check->fields['product_is_call']) {
  279.       if (PRODUCTS_PRICE_IS_CALL_IMAGE_ON=='0') {
  280.         $call_tag = '<br />' . PRODUCTS_PRICE_IS_CALL_FOR_PRICE_TEXT;
  281.       } else {
  282.         $call_tag = '<br />' . zen_image(DIR_WS_TEMPLATE_IMAGES . OTHER_IMAGE_CALL_FOR_PRICE, PRODUCTS_PRICE_IS_CALL_FOR_PRICE_TEXT);
  283.       }
  284.     }
  285.  
  286.     return $final_display_price . $free_tag . $call_tag;
  287.   }
  288.  
  289. ////
  290. // Is the product free?
  291.   function zen_get_products_price_is_free($products_id) {
  292.     global $db;
  293.     $product_check = $db->Execute("select product_is_free from " . TABLE_PRODUCTS . " where products_id = '" . (int)$products_id . "'" . " limit 1");
  294.     if ($product_check->fields['product_is_free'] == '1') {
  295.       $the_free_price = true;
  296.     } else {
  297.       $the_free_price = false;
  298.     }
  299.     return $the_free_price;
  300.   }
  301.  
  302. ////
  303. // Is the product call for price?
  304.   function zen_get_products_price_is_call($products_id) {
  305.     global $db;
  306.     $product_check = $db->Execute("select product_is_call from " . TABLE_PRODUCTS . " where products_id = '" . (int)$products_id . "'" . " limit 1");
  307.     if ($product_check->fields['product_is_call'] == '1') {
  308.       $the_call_price = true;
  309.     } else {
  310.       $the_call_price = false;
  311.     }
  312.     return $the_call_price;
  313.   }
  314.  
  315. ////
  316. // Is the product priced by attributes?
  317.   function zen_get_products_price_is_priced_by_attributes($products_id) {
  318.     global $db;
  319.     $product_check = $db->Execute("select products_priced_by_attribute from " . TABLE_PRODUCTS . " where products_id = '" . (int)$products_id . "'" . " limit 1");
  320.     if ($product_check->fields['products_priced_by_attribute'] == '1') {
  321.       $the_products_priced_by_attribute = true;
  322.     } else {
  323.       $the_products_priced_by_attribute = false;
  324.     }
  325.     return $the_products_priced_by_attribute;
  326.   }
  327.  
  328. ////
  329. // Return a product's minimum quantity
  330. // TABLES: products
  331.   function zen_get_products_quantity_order_min($product_id) {
  332.     global $db;
  333.  
  334.     $the_products_quantity_order_min = $db->Execute("select products_id, products_quantity_order_min from " . TABLE_PRODUCTS . " where products_id = '" . (int)$product_id . "'");
  335.     return $the_products_quantity_order_min->fields['products_quantity_order_min'];
  336.   }
  337.  
  338.  
  339. ////
  340. // Return a product's minimum unit order
  341. // TABLES: products
  342.   function zen_get_products_quantity_order_units($product_id) {
  343.     global $db;
  344.  
  345.     $the_products_quantity_order_units = $db->Execute("select products_id, products_quantity_order_units from " . TABLE_PRODUCTS . " where products_id = '" . (int)$product_id . "'");
  346.     return $the_products_quantity_order_units->fields['products_quantity_order_units'];
  347.   }
  348.  
  349. ////
  350. // Return a product's maximum quantity
  351. // TABLES: products
  352.   function zen_get_products_quantity_order_max($product_id) {
  353.     global $db;
  354.  
  355.     $the_products_quantity_order_max = $db->Execute("select products_id, products_quantity_order_max from " . TABLE_PRODUCTS . " where products_id = '" . (int)$product_id . "'");
  356.     return $the_products_quantity_order_max->fields['products_quantity_order_max'];
  357.   }
  358.  
  359. ////
  360. // Return a product's quantity box status
  361. // TABLES: products
  362.   function zen_get_products_qty_box_status($product_id) {
  363.     global $db;
  364.  
  365.     $the_products_qty_box_status = $db->Execute("select products_id, products_qty_box_status  from " . TABLE_PRODUCTS . " where products_id = '" . (int)$product_id . "'");
  366.     return $the_products_qty_box_status->fields['products_qty_box_status'];
  367.   }
  368.  
  369. ////
  370. // Return a product mixed setting
  371. // TABLES: products
  372.   function zen_get_products_quantity_mixed($product_id) {
  373.     global $db;
  374.  
  375. // don't check for mixed if not attributes
  376.     $chk_attrib = zen_has_product_attributes((int)$product_id);
  377.     if ($chk_attrib == true) {
  378.       $the_products_quantity_mixed = $db->Execute("select products_id, products_quantity_mixed from " . TABLE_PRODUCTS . " where products_id = '" . (int)$product_id . "'");
  379.       if ($the_products_quantity_mixed->fields['products_quantity_mixed'] == '1') {
  380.         $look_up = true;
  381.       } else {
  382.         $look_up = false;
  383.       }
  384.     } else {
  385.       $look_up = 'none';
  386.     }
  387.  
  388.     return $look_up;
  389.   }
  390.  
  391.  
  392. ////
  393. // Return a products quantity minimum and units display
  394.   function zen_get_products_quantity_min_units_display($product_id, $include_break = true, $shopping_cart_msg = false) {
  395.     $check_min = zen_get_products_quantity_order_min($product_id);
  396.     $check_units = zen_get_products_quantity_order_units($product_id);
  397.  
  398.     $the_min_units='';
  399.  
  400.     if ($check_min != 1 or $check_units != 1) {
  401.       if ($check_min != 1) {
  402.         $the_min_units .= PRODUCTS_QUANTITY_MIN_TEXT_LISTING . '&nbsp;' . $check_min;
  403.       }
  404.       if ($check_units != 1) {
  405.         $the_min_units .= ($the_min_units ? ' ' : '' ) . PRODUCTS_QUANTITY_UNIT_TEXT_LISTING . '&nbsp;' . $check_units;
  406.       }
  407.  
  408. // don't check for mixed if not attributes
  409.       $chk_mix = zen_get_products_quantity_mixed((int)$product_id);
  410.       if ($chk_mix != 'none') {
  411.         if (($check_min > 0 or $check_units > 0)) {
  412.           if ($include_break == true) {
  413.             $the_min_units .= '<br />' . ($shopping_cart_msg == false ? TEXT_PRODUCTS_MIX_OFF : TEXT_PRODUCTS_MIX_OFF_SHOPPING_CART);
  414.           } else {
  415.             $the_min_units .= '&nbsp;&nbsp;' . ($shopping_cart_msg == false ? TEXT_PRODUCTS_MIX_OFF : TEXT_PRODUCTS_MIX_OFF_SHOPPING_CART);
  416.           }
  417.         } else {
  418.           if ($include_break == true) {
  419.             $the_min_units .= '<br />' . ($shopping_cart_msg == false ? TEXT_PRODUCTS_MIX_ON : TEXT_PRODUCTS_MIX_ON_SHOPPING_CART);
  420.           } else {
  421.             $the_min_units .= '&nbsp;&nbsp;' . ($shopping_cart_msg == false ? TEXT_PRODUCTS_MIX_ON : TEXT_PRODUCTS_MIX_ON_SHOPPING_CART);
  422.           }
  423.         }
  424.       }
  425.     }
  426.  
  427.     // quantity max
  428.     $check_max = zen_get_products_quantity_order_max($product_id);
  429.  
  430.     if ($check_max != 0) {
  431.       if ($include_break == true) {
  432.         $the_min_units .= ($the_min_units != '' ? '<br />' : '') . PRODUCTS_QUANTITY_MAX_TEXT_LISTING . '&nbsp;' . $check_max;
  433.       } else {
  434.         $the_min_units .= ($the_min_units != '' ? '&nbsp;&nbsp;' : '') . PRODUCTS_QUANTITY_MAX_TEXT_LISTING . '&nbsp;' . $check_max;
  435.       }
  436.     }
  437.  
  438.     return $the_min_units;
  439.   }
  440.  
  441.  
  442. ////
  443. // Return quantity buy now
  444.   function zen_get_buy_now_qty($product_id) {
  445.     global $cart;
  446.     $check_min = zen_get_products_quantity_order_min($product_id);
  447.     $check_units = zen_get_products_quantity_order_units($product_id);
  448.     $buy_now_qty=1;
  449. // works on Mixed ON
  450.     switch (true) {
  451.       case ($_SESSION['cart']->in_cart_mixed($product_id) == 0 ):
  452.         if ($check_min >= $check_units) {
  453.           $buy_now_qty = $check_min;
  454.         } else {
  455.           $buy_now_qty = $check_units;
  456.         }
  457.         break;
  458.       case ($_SESSION['cart']->in_cart_mixed($product_id) < $check_min):
  459.         $buy_now_qty = $check_min - $_SESSION['cart']->in_cart_mixed($product_id);
  460.         break;
  461.       case ($_SESSION['cart']->in_cart_mixed($product_id) > $check_min):
  462.       // set to units or difference in units to balance cart
  463.         $new_units = $check_units - fmod_round($_SESSION['cart']->in_cart_mixed($product_id), $check_units);
  464. //echo 'Cart: ' . $_SESSION['cart']->in_cart_mixed($product_id) . ' Min: ' . $check_min . ' Units: ' . $check_units . ' fmod: ' . fmod($_SESSION['cart']->in_cart_mixed($product_id), $check_units) . '<br />';
  465.         $buy_now_qty = ($new_units > 0 ? $new_units : $check_units);
  466.         break;
  467.       default:
  468.         $buy_now_qty = $check_units;
  469.         break;
  470.     }
  471.     if ($buy_now_qty <= 0) {
  472.       $buy_now_qty = 1;
  473.     }
  474.     return $buy_now_qty;
  475.   }
  476.  
  477.  
  478. ////
  479. // compute product discount to be applied to attributes or other values
  480.   function zen_get_discount_calc($product_id, $attributes_id = false, $attributes_amount = false, $check_qty= false) {
  481.     global $discount_type_id, $sale_maker_discount;
  482.     global $cart;
  483.  
  484.     // no charge
  485.     if ($attributes_id > 0 and $attributes_amount == 0) {
  486.       return 0;
  487.     }
  488.  
  489.     $new_products_price = zen_get_products_base_price($product_id);
  490.     $new_special_price = zen_get_products_special_price($product_id, true);
  491.     $new_sale_price = zen_get_products_special_price($product_id, false);
  492.  
  493.     $discount_type_id = zen_get_products_sale_discount_type($product_id);
  494.  
  495.     if ($new_products_price != 0) {
  496.       $special_price_discount = ($new_special_price != 0 ? ($new_special_price/$new_products_price) : 1);
  497.     } else {
  498.       $special_price_discount = '';
  499.     }
  500.     $sale_maker_discount = zen_get_products_sale_discount_type($product_id, '', 'amount');
  501.  
  502.     // percentage adjustment of discount
  503.     if (($discount_type_id == 120 or $discount_type_id == 1209) or ($discount_type_id == 110 or $discount_type_id == 1109)) {
  504.       $sale_maker_discount = ($sale_maker_discount != 0 ? (100 - $sale_maker_discount)/100 : 1);
  505.     }
  506.  
  507.    $qty = $check_qty;
  508.  
  509. // fix here
  510. // BOF: percentage discounts apply to price
  511.     switch (true) {
  512.       case (zen_get_discount_qty($product_id, $qty) and !$attributes_id):
  513.         // discount quanties exist and this is not an attribute
  514.         // $this->contents[$products_id]['qty']
  515.         $check_discount_qty_price = zen_get_products_discount_price_qty($product_id, $qty, $attributes_amount);
  516. //echo 'How much 1 ' . $qty . ' : ' . $attributes_amount . ' vs ' . $check_discount_qty_price . '<br />';
  517.         return $check_discount_qty_price;
  518.         break;
  519.  
  520.       case (zen_get_discount_qty($product_id, $qty) and zen_get_products_price_is_priced_by_attributes($product_id)):
  521.         // discount quanties exist and this is not an attribute
  522.         // $this->contents[$products_id]['qty']
  523.         $check_discount_qty_price = zen_get_products_discount_price_qty($product_id, $qty, $attributes_amount);
  524. //echo 'How much 2 ' . $qty . ' : ' . $attributes_amount . ' vs ' . $check_discount_qty_price . '<br />';
  525.  
  526.         return $check_discount_qty_price;
  527.         break;
  528.  
  529.       case ($discount_type_id == 5):
  530.         // No Sale and No Special
  531. //        $sale_maker_discount = 1;
  532.         if (!$attributes_id) {
  533.           $sale_maker_discount = $sale_maker_discount;
  534.         } else {
  535.           // compute attribute amount
  536.           if ($attributes_amount != 0) {
  537.             if ($special_price_discount != 0) {
  538.               $calc = ($attributes_amount * $special_price_discount);
  539.             } else {
  540.               $calc = $attributes_amount;
  541.             }
  542.  
  543.             $sale_maker_discount = $calc;
  544.           } else {
  545.             $sale_maker_discount = $sale_maker_discount;
  546.           }
  547.         }
  548. //echo 'How much 3 - ' . $qty . ' : ' . $product_id . ' : ' . $qty . ' x ' .  $attributes_amount . ' vs ' . $check_discount_qty_price . ' - ' . $sale_maker_discount . '<br />';
  549.         break;
  550.       case ($discount_type_id == 59):
  551.         // No Sale and Special
  552. //        $sale_maker_discount = $special_price_discount;
  553.         if (!$attributes_id) {
  554.           $sale_maker_discount = $sale_maker_discount;
  555.         } else {
  556.           // compute attribute amount
  557.           if ($attributes_amount != 0) {
  558.             $calc = ($attributes_amount * $special_price_discount);
  559.             $sale_maker_discount = $calc;
  560.           } else {
  561.             $sale_maker_discount = $sale_maker_discount;
  562.           }
  563.         }
  564.         break;
  565. // EOF: percentage discount apply to price
  566.  
  567. // BOF: percentage discounts apply to Sale
  568.       case ($discount_type_id == 120):
  569.         // percentage discount Sale and Special without a special
  570.         if (!$attributes_id) {
  571.           $sale_maker_discount = $sale_maker_discount;
  572.         } else {
  573.           // compute attribute amount
  574.           if ($attributes_amount != 0) {
  575.             $calc = ($attributes_amount * $sale_maker_discount);
  576.             $sale_maker_discount = $calc;
  577.           } else {
  578.             $sale_maker_discount = $sale_maker_discount;
  579.           }
  580.         }
  581.         break;
  582.       case ($discount_type_id == 1209):
  583.         // percentage discount on Sale and Special with a special
  584.         if (!$attributes_id) {
  585.           $sale_maker_discount = $sale_maker_discount;
  586.         } else {
  587.           // compute attribute amount
  588.           if ($attributes_amount != 0) {
  589.             $calc = ($attributes_amount * $special_price_discount);
  590.             $calc2 = $calc - ($calc * $sale_maker_discount);
  591.             $sale_maker_discount = $calc - $calc2;
  592.           } else {
  593.             $sale_maker_discount = $sale_maker_discount;
  594.           }
  595.         }
  596.         break;
  597. // EOF: percentage discounts apply to Sale
  598.  
  599. // BOF: percentage discounts skip specials
  600.       case ($discount_type_id == 110):
  601.         // percentage discount Sale and Special without a special
  602.         if (!$attributes_id) {
  603.           $sale_maker_discount = $sale_maker_discount;
  604.         } else {
  605.           // compute attribute amount
  606.           if ($attributes_amount != 0) {
  607.             $calc = ($attributes_amount * $sale_maker_discount);
  608.             $sale_maker_discount = $calc;
  609.           } else {
  610. //            $sale_maker_discount = $sale_maker_discount;
  611.             if ($attributes_amount != 0) {
  612. //            $calc = ($attributes_amount * $special_price_discount);
  613. //            $calc2 = $calc - ($calc * $sale_maker_discount);
  614. //            $sale_maker_discount = $calc - $calc2;
  615.               $calc = $attributes_amount - ($attributes_amount * $sale_maker_discount);
  616.               $sale_maker_discount = $calc;
  617.             } else {
  618.               $sale_maker_discount = $sale_maker_discount;
  619.             }
  620.           }
  621.         }
  622.         break;
  623.       case ($discount_type_id == 1109):
  624.         // percentage discount on Sale and Special with a special
  625.         if (!$attributes_id) {
  626.           $sale_maker_discount = $sale_maker_discount;
  627.         } else {
  628.           // compute attribute amount
  629.           if ($attributes_amount != 0) {
  630.             $calc = ($attributes_amount * $special_price_discount);
  631. //            $calc2 = $calc - ($calc * $sale_maker_discount);
  632. //            $sale_maker_discount = $calc - $calc2;
  633.             $sale_maker_discount = $calc;
  634.           } else {
  635.             $sale_maker_discount = $sale_maker_discount;
  636.           }
  637.         }
  638.         break;
  639. // EOF: percentage discounts skip specials
  640.  
  641. // BOF: flat amount discounts
  642.       case ($discount_type_id == 20):
  643.         // flat amount discount Sale and Special without a special
  644.         if (!$attributes_id) {
  645.           $sale_maker_discount = $sale_maker_discount;
  646.         } else {
  647.           // compute attribute amount
  648.           if ($attributes_amount != 0) {
  649.             $calc = ($attributes_amount - $sale_maker_discount);
  650.             $sale_maker_discount = $calc;
  651.           } else {
  652.             $sale_maker_discount = $sale_maker_discount;
  653.           }
  654.         }
  655.         break;
  656.       case ($discount_type_id == 209):
  657.         // flat amount discount on Sale and Special with a special
  658.         if (!$attributes_id) {
  659.           $sale_maker_discount = $sale_maker_discount;
  660.         } else {
  661.           // compute attribute amount
  662.           if ($attributes_amount != 0) {
  663.             $calc = ($attributes_amount * $special_price_discount);
  664.             $calc2 = ($calc - $sale_maker_discount);
  665.             $sale_maker_discount = $calc2;
  666.           } else {
  667.             $sale_maker_discount = $sale_maker_discount;
  668.           }
  669.         }
  670.         break;
  671. // EOF: flat amount discounts
  672.  
  673. // BOF: flat amount discounts Skip Special
  674.       case ($discount_type_id == 10):
  675.         // flat amount discount Sale and Special without a special
  676.         if (!$attributes_id) {
  677.           $sale_maker_discount = $sale_maker_discount;
  678.         } else {
  679.           // compute attribute amount
  680.           if ($attributes_amount != 0) {
  681.             $calc = ($attributes_amount - $sale_maker_discount);
  682.             $sale_maker_discount = $calc;
  683.           } else {
  684.             $sale_maker_discount = $sale_maker_discount;
  685.           }
  686.         }
  687.         break;
  688.       case ($discount_type_id == 109):
  689.         // flat amount discount on Sale and Special with a special
  690.         if (!$attributes_id) {
  691.           $sale_maker_discount = 1;
  692.         } else {
  693.           // compute attribute amount based on Special
  694.           if ($attributes_amount != 0) {
  695.             $calc = ($attributes_amount * $special_price_discount);
  696.             $sale_maker_discount = $calc;
  697.           } else {
  698.             $sale_maker_discount = $sale_maker_discount;
  699.           }
  700.         }
  701.         break;
  702. // EOF: flat amount discounts Skip Special
  703.  
  704. // BOF: New Price amount discounts
  705.       case ($discount_type_id == 220):
  706.         // New Price amount discount Sale and Special without a special
  707.         if (!$attributes_id) {
  708.           $sale_maker_discount = $sale_maker_discount;
  709.         } else {
  710.           // compute attribute amount
  711.           if ($attributes_amount != 0) {
  712.             $calc = ($attributes_amount * $special_price_discount);
  713.             $sale_maker_discount = $calc;
  714. //echo '<br />attr ' . $attributes_amount . ' spec ' . $special_price_discount . ' Calc ' . $calc . 'Calc2 ' . $calc2 . '<br />';
  715.           } else {
  716.             $sale_maker_discount = $sale_maker_discount;
  717.           }
  718.         }
  719.         break;
  720.       case ($discount_type_id == 2209):
  721.         // New Price amount discount on Sale and Special with a special
  722.         if (!$attributes_id) {
  723. //          $sale_maker_discount = $sale_maker_discount;
  724.           $sale_maker_discount = $sale_maker_discount;
  725.         } else {
  726.           // compute attribute amount
  727.           if ($attributes_amount != 0) {
  728.             $calc = ($attributes_amount * $special_price_discount);
  729. //echo '<br />attr ' . $attributes_amount . ' spec ' . $special_price_discount . ' Calc ' . $calc . 'Calc2 ' . $calc2 . '<br />';
  730.             $sale_maker_discount = $calc;
  731.           } else {
  732.             $sale_maker_discount = $sale_maker_discount;
  733.           }
  734.         }
  735.         break;
  736. // EOF: New Price amount discounts
  737.  
  738. // BOF: New Price amount discounts - Skip Special
  739.       case ($discount_type_id == 210):
  740.         // New Price amount discount Sale and Special without a special
  741.         if (!$attributes_id) {
  742.           $sale_maker_discount = $sale_maker_discount;
  743.         } else {
  744.           // compute attribute amount
  745.           if ($attributes_amount != 0) {
  746.             $calc = ($attributes_amount * $special_price_discount);
  747.             $sale_maker_discount = $calc;
  748. //echo '<br />attr ' . $attributes_amount . ' spec ' . $special_price_discount . ' Calc ' . $calc . 'Calc2 ' . $calc2 . '<br />';
  749.           } else {
  750.             $sale_maker_discount = $sale_maker_discount;
  751.           }
  752.         }
  753.         break;
  754.       case ($discount_type_id == 2109):
  755.         // New Price amount discount on Sale and Special with a special
  756.         if (!$attributes_id) {
  757. //          $sale_maker_discount = $sale_maker_discount;
  758.           $sale_maker_discount = $sale_maker_discount;
  759.         } else {
  760.           // compute attribute amount
  761.           if ($attributes_amount != 0) {
  762.             $calc = ($attributes_amount * $special_price_discount);
  763. //echo '<br />attr ' . $attributes_amount . ' spec ' . $special_price_discount . ' Calc ' . $calc . 'Calc2 ' . $calc2 . '<br />';
  764.             $sale_maker_discount = $calc;
  765.           } else {
  766.             $sale_maker_discount = $sale_maker_discount;
  767.           }
  768.         }
  769.         break;
  770. // EOF: New Price amount discounts - Skip Special
  771.  
  772.       case ($discount_type_id == 0 or $discount_type_id == 9):
  773.       // flat discount
  774.         return $sale_maker_discount;
  775.         break;
  776.       default:
  777.         $sale_maker_discount = 7000;
  778.         break;
  779.     }
  780.  
  781.     return $sale_maker_discount;
  782.   }
  783.  
  784. ////
  785. // look up discount in sale makers - attributes only can have discounts if set as percentages
  786. // this gets the discount amount this does not determin when to apply the discount
  787.   function zen_get_products_sale_discount_type($product_id = false, $categories_id = false, $return_value = false) {
  788.     global $currencies;
  789.     global $db;
  790.  
  791. /*
  792.  
  793. 0 = flat amount off base price with a special
  794. 1 = Percentage off base price with a special
  795. 2 = New Price with a special
  796.  
  797. 5 = No Sale or Skip Products with Special
  798.  
  799. special options + option * 10
  800. 0 = Ignore special and apply to Price
  801. 1 = Skip Products with Specials switch to 5
  802. 2 = Apply to Special Price
  803.  
  804. If a special exist * 10+9
  805.  
  806. 0*100 + 0*10 = flat apply to price = 0 or 9
  807. 0*100 + 1*10 = flat skip Specials = 5 or 59
  808. 0*100 + 2*10 = flat apply to special = 20 or 209
  809.  
  810. 1*100 + 0*10 = Percentage apply to price = 100 or 1009
  811. 1*100 + 1*10 = Percentage skip Specials = 110 or 1109 / 5 or 59
  812. 1*100 + 2*10 = Percentage apply to special = 120 or 1209
  813.  
  814. 2*100 + 0*10 = New Price apply to price = 200 or 2009
  815. 2*100 + 1*10 = New Price skip Specials = 210 or 2109 / 5 or 59
  816. 2*100 + 2*10 = New Price apply to Special = 220 or 2209
  817.  
  818. */
  819.  
  820. // get products category
  821.     if ($categories_id == true) {
  822.       $check_category = $categories_id;
  823.     } else {
  824.       $check_category = zen_get_products_category_id($product_id);
  825.     }
  826. /*
  827.     $deduction_type_array = array(array('id' => '0', 'text' => DEDUCTION_TYPE_DROPDOWN_0),
  828.                                   array('id' => '1', 'text' => DEDUCTION_TYPE_DROPDOWN_1),
  829.                                   array('id' => '2', 'text' => DEDUCTION_TYPE_DROPDOWN_2));
  830. */
  831.     $sale_exists = 'false';
  832.     $sale_maker_discount = '';
  833.     $sale_maker_special_condition = '';
  834.     $salemaker_sales = $db->Execute("select sale_id, sale_status, sale_name, sale_categories_all, sale_deduction_value, sale_deduction_type, sale_pricerange_from, sale_pricerange_to, sale_specials_condition, sale_categories_selected, sale_date_start, sale_date_end, sale_date_added, sale_date_last_modified, sale_date_status_change from " . TABLE_SALEMAKER_SALES . " where sale_status='1'");
  835.     while (!$salemaker_sales->EOF) {
  836.       $categories = explode(',', $salemaker_sales->fields['sale_categories_all']);
  837.       while (list($key,$value) = each($categories)) {
  838.         if ($value == $check_category) {
  839.           $sale_exists = 'true';
  840.           $sale_maker_discount = $salemaker_sales->fields['sale_deduction_value'];
  841.           $sale_maker_special_condition = $salemaker_sales->fields['sale_specials_condition'];
  842.           $sale_maker_discount_type = $salemaker_sales->fields['sale_deduction_type'];
  843.           break;
  844.         }
  845.       }
  846.       $salemaker_sales->MoveNext();
  847.     }
  848.  
  849.     $check_special = zen_get_products_special_price($product_id, true);
  850.  
  851.     if ($sale_exists == 'true' and $sale_maker_special_condition != 0) {
  852.       $sale_maker_discount_type = (($sale_maker_discount_type * 100) + ($sale_maker_special_condition * 10));
  853.     } else {
  854.       $sale_maker_discount_type = 5;
  855.     }
  856.  
  857.     if (!$check_special) {
  858.       // do nothing
  859.     } else {
  860.       $sale_maker_discount_type = ($sale_maker_discount_type * 10) + 9;
  861.     }
  862.  
  863.     switch (true) {
  864.       case (!$return_value):
  865.         return $sale_maker_discount_type;
  866.         break;
  867.       case ($return_value == 'amount'):
  868.         return $sale_maker_discount;
  869.         break;
  870.       default:
  871.         return 'Unknown Request';
  872.         break;
  873.     }
  874.   }
  875.  
  876. ////
  877. // look up discount in sale makers - attributes only can have discounts if set as percentages
  878. // this gets the discount amount this does not determin when to apply the discount
  879.   function zen_get_products_sale_discount($product_id = false, $categories_id = false, $display_type = false) {
  880.     global $currencies;
  881.     global $db;
  882.  
  883. // NOT USED
  884. echo '<br />' . 'I SHOULD use zen_get_discount_calc' . '<br />';
  885.  
  886. /*
  887.  
  888. 0 = flat amount off base price with a special
  889. 1 = Percentage off base price with a special
  890. 2 = New Price with a special
  891.  
  892. 5 = No Sale or Skip Products with Special
  893.  
  894. special options + option * 10
  895. 0 = Ignore special and apply to Price
  896. 1 = Skip Products with Specials switch to 5
  897. 2 = Apply to Special Price
  898.  
  899. If a special exist * 10
  900.  
  901. 0+7 + 0+10 = flat apply to price = 17 or 170
  902. 0+7 + 1+10 = flat skip Specials = 5 or 50
  903. 0+7 + 2+10 = flat apply to special = 27 or 270
  904.  
  905. 1+7 + 0+10 = Percentage apply to price = 18 or 180
  906. 1+7 + 1+10 = Percentage skip Specials = 5 or 50
  907. 1+7 + 2+10 = Percentage apply to special = 20 or 200
  908.  
  909. 2+7 + 0+10 = New Price apply to price = 19 or 190
  910. 2+7 + 1+10 = New Price skip Specials = 5 or 50
  911. 2+7 + 2+10 = New Price apply to Special = 21 or 210
  912.  
  913. */
  914.  
  915. /*
  916. // get products category
  917.     if ($categories_id == true) {
  918.       $check_category = $categories_id;
  919.     } else {
  920.       $check_category = zen_get_products_category_id($product_id);
  921.     }
  922.  
  923.     $deduction_type_array = array(array('id' => '0', 'text' => DEDUCTION_TYPE_DROPDOWN_0),
  924.                                   array('id' => '1', 'text' => DEDUCTION_TYPE_DROPDOWN_1),
  925.                                   array('id' => '2', 'text' => DEDUCTION_TYPE_DROPDOWN_2));
  926.  
  927.     $sale_maker_discount = 0;
  928.     $salemaker_sales = $db->Execute("select sale_id, sale_status, sale_name, sale_categories_all, sale_deduction_value, sale_deduction_type, sale_pricerange_from, sale_pricerange_to, sale_specials_condition, sale_categories_selected, sale_date_start, sale_date_end, sale_date_added, sale_date_last_modified, sale_date_status_change from " . TABLE_SALEMAKER_SALES . " where sale_status='1'");
  929.     while (!$salemaker_sales->EOF) {
  930.       $categories = explode(',', $salemaker_sales->fields['sale_categories_all']);
  931.       while (list($key,$value) = each($categories)) {
  932.         if ($value == $check_category) {
  933.           $sale_maker_discount = $salemaker_sales->fields['sale_deduction_value'];
  934.           $sale_maker_discount_type = $salemaker_sales->fields['sale_deduction_type'];
  935.           break;
  936.         }
  937.       }
  938.       $salemaker_sales->MoveNext();
  939.     }
  940.  
  941.     switch(true) {
  942.       // percentage discount only
  943.       case ($sale_maker_discount_type == 1):
  944.         $sale_maker_discount = (1 - ($sale_maker_discount / 100));
  945.         break;
  946.       case ($sale_maker_discount_type == 0 and $display_type == true):
  947.         $sale_maker_discount = $sale_maker_discount;
  948.         break;
  949.       case ($sale_maker_discount_type == 0 and $display_type == false):
  950.         $sale_maker_discount = $sale_maker_discount;
  951.         break;
  952.       case ($sale_maker_discount_type == 2 and $display_type == true):
  953.         $sale_maker_discount = $sale_maker_discount;
  954.         break;
  955.       default:
  956.         $sale_maker_discount = 1;
  957.         break;
  958.     }
  959.  
  960.     if ($display_type == true) {
  961.       if ($sale_maker_discount != 1 and $sale_maker_discount !=0) {
  962.         switch(true) {
  963.           case ($sale_maker_discount_type == 0):
  964.             $sale_maker_discount = $currencies->format($sale_maker_discount) . ' ' . $deduction_type_array[$sale_maker_discount_type]['text'];
  965.             break;
  966.           case ($sale_maker_discount_type == 2):
  967.             $sale_maker_discount = $currencies->format($sale_maker_discount) . ' ' . $deduction_type_array[$sale_maker_discount_type]['text'];
  968.             break;
  969.           case ($sale_maker_discount_type == 1):
  970.             $sale_maker_discount = number_format( (1.00 - $sale_maker_discount),2,".","") . ' ' . $deduction_type_array[$sale_maker_discount_type]['text'];
  971.             break;
  972.         }
  973.       } else {
  974.         $sale_maker_discount = '';
  975.       }
  976.     }
  977.     return $sale_maker_discount;
  978. */
  979.  
  980.   }
  981.  
  982. ////
  983. // Actual Price Retail
  984. // Specials and Tax Included
  985.   function zen_get_products_actual_price($products_id) {
  986.     global $db, $currencies;
  987.     $product_check = $db->Execute("select products_tax_class_id, products_price, products_priced_by_attribute, product_is_free, product_is_call from " . TABLE_PRODUCTS . " where products_id = '" . (int)$products_id . "'" . " limit 1");
  988.  
  989.     $show_display_price = '';
  990.     $display_normal_price = zen_get_products_base_price($products_id);
  991.     $display_special_price = zen_get_products_special_price($products_id, true);
  992.     $display_sale_price = zen_get_products_special_price($products_id, false);
  993.  
  994.     $products_actual_price = $display_normal_price;
  995.  
  996.     if ($display_special_price) {
  997.       $products_actual_price = $display_special_price;
  998.     }
  999.     if ($display_sale_price) {
  1000.       $products_actual_price = $display_sale_price;
  1001.     }
  1002.  
  1003.     // If Free, Show it
  1004.     if ($product_check->fields['product_is_free'] == '1') {
  1005.       $products_actual_price = 0;
  1006.     }
  1007.  
  1008.     return $products_actual_price;
  1009.   }
  1010.  
  1011. ////
  1012. // return attributes_price_factor
  1013.   function zen_get_attributes_price_factor($price, $special, $factor, $offset) {
  1014.     if (ATTRIBUTES_PRICE_FACTOR_FROM_SPECIAL =='1' and $special) {
  1015.       // calculate from specials_new_products_price
  1016.       $calculated_price = $special * ($factor - $offset);
  1017.     } else {
  1018.       // calculate from products_price
  1019.       $calculated_price = $price * ($factor - $offset);
  1020.     }
  1021. //    return '$price ' . $price . ' $special ' . $special . ' $factor ' . $factor . ' $offset ' . $offset;
  1022.     return $calculated_price;
  1023.   }
  1024.  
  1025.  
  1026. ////
  1027. // return attributes_qty_prices or attributes_qty_prices_onetime based on qty
  1028.   function zen_get_attributes_qty_prices_onetime($string, $qty) {
  1029.     $attribute_qty = preg_split("/[:,]/" , $string);
  1030.     $new_price = 0;
  1031.     $size = sizeof($attribute_qty);
  1032. // if an empty string is passed then $attributes_qty will consist of a 1 element array
  1033.     if ($size > 1) {
  1034.       for ($i=0, $n=$size; $i<$n; $i+=2) {
  1035.         $new_price = $attribute_qty[$i+1];
  1036.         if ($qty <= $attribute_qty[$i]) {
  1037.           $new_price = $attribute_qty[$i+1];
  1038.           break;
  1039.         }
  1040.       }
  1041.     }
  1042.     return $new_price;
  1043.   }
  1044.  
  1045.  
  1046. ////
  1047. // Check specific attributes_qty_prices or attributes_qty_prices_onetime for a given quantity price
  1048.   function zen_get_attributes_quantity_price($check_what, $check_for) {
  1049. // $check_what='1:3.00,5:2.50,10:2.25,20:2.00';
  1050. // $check_for=50;
  1051.       $attribute_table_cost = preg_split("/[:,]/" , $check_what);
  1052.       $size = sizeof($attribute_table_cost);
  1053.       for ($i=0, $n=$size; $i<$n; $i+=2) {
  1054.         if ($check_for >= $attribute_table_cost[$i]) {
  1055.           $attribute_quantity_check = $attribute_table_cost[$i];
  1056.           $attribute_quantity_price = $attribute_table_cost[$i+1];
  1057.         }
  1058.       }
  1059. //          echo '<br>Cost ' . $check_for . ' - '  .  $attribute_quantity_check . ' x ' . $attribute_quantity_price;
  1060.      return $attribute_quantity_price;
  1061.   }
  1062.  
  1063.  
  1064. ////
  1065. // attributes final price
  1066.   function zen_get_attributes_price_final($attribute, $qty = 1, $pre_selected, $include_onetime = 'false') {
  1067.     global $db;
  1068.     global $cart;
  1069.  
  1070.     $attributes_price_final = 0;
  1071.  
  1072.     if ($pre_selected == '' or $attribute != $pre_selected->fields["products_attributes_id"]) {
  1073.       $pre_selected = $db->Execute("select pa.* from " . TABLE_PRODUCTS_ATTRIBUTES . " pa where pa.products_attributes_id= '" . (int)$attribute . "'");
  1074.     } else {
  1075.       // use existing select
  1076.     }
  1077.  
  1078.     // normal attributes price
  1079.     if ($pre_selected->fields["price_prefix"] == '-') {
  1080.       $attributes_price_final -= $pre_selected->fields["options_values_price"];
  1081.     } else {
  1082.       $attributes_price_final += $pre_selected->fields["options_values_price"];
  1083.     }
  1084.     // qty discounts
  1085.     $attributes_price_final += zen_get_attributes_qty_prices_onetime($pre_selected->fields["attributes_qty_prices"], $qty);
  1086.  
  1087.     // price factor
  1088.     $display_normal_price = zen_get_products_actual_price($pre_selected->fields["products_id"]);
  1089.     $display_special_price = zen_get_products_special_price($pre_selected->fields["products_id"]);
  1090.  
  1091.     $attributes_price_final += zen_get_attributes_price_factor($display_normal_price, $display_special_price, $pre_selected->fields["attributes_price_factor"], $pre_selected->fields["attributes_price_factor_offset"]);
  1092.  
  1093.     // per word and letter charges
  1094.     if (zen_get_attributes_type($attribute) == PRODUCTS_OPTIONS_TYPE_TEXT) {
  1095.       // calc per word or per letter
  1096.     }
  1097.  
  1098. // onetime charges
  1099.     if ($include_onetime == 'true') {
  1100.       $pre_selected_onetime = $pre_selected;
  1101.       $attributes_price_final += zen_get_attributes_price_final_onetime($pre_selected->fields["products_attributes_id"], 1, $pre_selected_onetime);
  1102.     }
  1103.  
  1104.     return $attributes_price_final;
  1105.   }
  1106.  
  1107.  
  1108. ////
  1109. // attributes final price onetime
  1110.   function zen_get_attributes_price_final_onetime($attribute, $qty= 1, $pre_selected_onetime) {
  1111.     global $db;
  1112.     global $cart;
  1113.  
  1114.     if ($pre_selected_onetime == '' or $attribute != $pre_selected_onetime->fields["products_attributes_id"]) {
  1115.       $pre_selected_onetime = $db->Execute("select pa.* from " . TABLE_PRODUCTS_ATTRIBUTES . " pa where pa.products_attributes_id= '" . (int)$attribute . "'");
  1116.     } else {
  1117.       // use existing select
  1118.     }
  1119.  
  1120. // one time charges
  1121.     // onetime charge
  1122.       $attributes_price_final_onetime += $pre_selected_onetime->fields["attributes_price_onetime"];
  1123.  
  1124.     // price factor
  1125.     $display_normal_price = zen_get_products_actual_price($pre_selected_onetime->fields["products_id"]);
  1126.     $display_special_price = zen_get_products_special_price($pre_selected_onetime->fields["products_id"]);
  1127.  
  1128.     // price factor one time
  1129.       $attributes_price_final_onetime += zen_get_attributes_price_factor($display_normal_price, $display_special_price, $pre_selected_onetime->fields["attributes_price_factor_onetime"], $pre_selected_onetime->fields["attributes_price_factor_onetime_offset"]);
  1130.  
  1131.     // onetime charge qty price
  1132.       $attributes_price_final_onetime += zen_get_attributes_qty_prices_onetime($pre_selected_onetime->fields["attributes_qty_prices_onetime"], 1);
  1133.  
  1134.       return $attributes_price_final_onetime;
  1135.     }
  1136.  
  1137.  
  1138. ////
  1139. // get attributes type
  1140.   function zen_get_attributes_type($check_attribute) {
  1141.     global $db;
  1142.     $check_options_id_query = $db->Execute("select options_id from " . TABLE_PRODUCTS_ATTRIBUTES . " where products_attributes_id='" . (int)$check_attribute . "'");
  1143.     $check_type_query = $db->Execute("select products_options_type from " . TABLE_PRODUCTS_OPTIONS . " where products_options_id='" . (int)$check_options_id_query->fields['options_id'] . "'");
  1144.     return $check_type_query->fields['products_options_type'];
  1145.   }
  1146.  
  1147.  
  1148. ////
  1149. // calculate words
  1150.   function zen_get_word_count($string, $free=0) {
  1151.     $string = str_replace(array("\r\n", "\n", "\r", "\t"), ' ', $string);
  1152.  
  1153.     if ($string != '') {
  1154.       while (strstr($string, '  ')) $string = str_replace('  ', ' ', $string);
  1155.       $string = trim($string);
  1156.       $word_count = substr_count($string, ' ');
  1157.       return (($word_count+1) - $free);
  1158.     } else {
  1159.       // nothing to count
  1160.       return 0;
  1161.     }
  1162.   }
  1163.  
  1164.  
  1165. ////
  1166. // calculate words price
  1167.   function zen_get_word_count_price($string, $free=0, $price) {
  1168.  
  1169.     $word_count = zen_get_word_count($string, $free);
  1170.     if ($word_count >= 1) {
  1171.       return ($word_count * $price);
  1172.     } else {
  1173.       return 0;
  1174.     }
  1175.   }
  1176.  
  1177.  
  1178. ////
  1179. // calculate letters
  1180.   function zen_get_letters_count($string, $free=0) {
  1181.     $string = str_replace(array("\r\n", "\n", "\r", "\t"), ' ', $string);
  1182.  
  1183.     while (strstr($string, '  ')) $string = str_replace('  ', ' ', $string);
  1184.     $string = trim($string);
  1185.     if (TEXT_SPACES_FREE == '1') {
  1186.       $letters_count = strlen(str_replace(' ', '', $string));
  1187.     } else {
  1188.       $letters_count = strlen($string);
  1189.     }
  1190.     if ($letters_count - $free >= 1) {
  1191.       return ($letters_count - $free);
  1192.     } else {
  1193.       return 0;
  1194.     }
  1195.   }
  1196.  
  1197.  
  1198. ////
  1199. // calculate letters price
  1200.   function zen_get_letters_count_price($string, $free=0, $price) {
  1201.  
  1202.     $letters_price = zen_get_letters_count($string, $free) * $price;
  1203.     if ($letters_price <= 0) {
  1204.       return 0;
  1205.     } else {
  1206.       return $letters_price;
  1207.     }
  1208.   }
  1209.  
  1210.  
  1211. ////
  1212. // compute discount based on qty
  1213.   function zen_get_products_discount_price_qty($product_id, $check_qty, $check_amount=0) {
  1214.     global $db, $cart;
  1215.       $new_qty = $_SESSION['cart']->in_cart_mixed_discount_quantity($product_id);
  1216.       // check for discount qty mix
  1217.       if ($new_qty > $check_qty) {
  1218.         $check_qty = $new_qty;
  1219.       }
  1220.       $product_id = (int)$product_id;
  1221.       $products_query = $db->Execute("select products_discount_type, products_discount_type_from, products_priced_by_attribute from " . TABLE_PRODUCTS . " where products_id='" . (int)$product_id . "'");
  1222.       $products_discounts_query = $db->Execute("select * from " . TABLE_PRODUCTS_DISCOUNT_QUANTITY . " where products_id='" . (int)$product_id . "' and discount_qty <='" . (float)$check_qty . "' order by discount_qty desc");
  1223.  
  1224.       $display_price = zen_get_products_base_price($product_id);
  1225.       $display_specials_price = zen_get_products_special_price($product_id, false);
  1226.  
  1227.       switch ($products_query->fields['products_discount_type']) {
  1228.         // none
  1229.         case ($products_discounts_query->EOF):
  1230.           //no discount applies
  1231.           $discounted_price = zen_get_products_actual_price($product_id);
  1232.           break;
  1233.         case '0':
  1234.           $discounted_price = zen_get_products_actual_price($product_id);
  1235.           break;
  1236.         // percentage discount
  1237.         case '1':
  1238.           if ($products_query->fields['products_discount_type_from'] == '0') {
  1239.             // priced by attributes
  1240.             if ($check_amount != 0) {
  1241.               $discounted_price = $check_amount - ($check_amount * ($products_discounts_query->fields['discount_price']/100));
  1242. //echo 'ID#' . $product_id . ' Amount is: ' . $check_amount . ' discount: ' . $discounted_price . '<br />';
  1243. //echo 'I SEE 2 for ' . $products_query->fields['products_discount_type'] . ' - ' . $products_query->fields['products_discount_type_from'] . ' - '. $check_amount . ' new: ' . $discounted_price . ' qty: ' . $check_qty;
  1244.             } else {
  1245.               $discounted_price = $display_price - ($display_price * ($products_discounts_query->fields['discount_price']/100));
  1246.             }
  1247.           } else {
  1248.             if (!$display_specials_price) {
  1249.               // priced by attributes
  1250.               if ($check_amount != 0) {
  1251.                 $discounted_price = $check_amount - ($check_amount * ($products_discounts_query->fields['discount_price']/100));
  1252.               } else {
  1253.                 $discounted_price = $display_price - ($display_price * ($products_discounts_query->fields['discount_price']/100));
  1254.               }
  1255.             } else {
  1256.               $discounted_price = $display_specials_price - ($display_specials_price * ($products_discounts_query->fields['discount_price']/100));
  1257.             }
  1258.           }
  1259.  
  1260.           break;
  1261.         // actual price
  1262.         case '2':
  1263.           if ($products_query->fields['products_discount_type_from'] == '0') {
  1264.             $discounted_price = $products_discounts_query->fields['discount_price'];
  1265.           } else {
  1266.             $discounted_price = $products_discounts_query->fields['discount_price'];
  1267.           }
  1268.           break;
  1269.         // amount offprice
  1270.         case '3':
  1271.           if ($products_query->fields['products_discount_type_from'] == '0') {
  1272.             $discounted_price = $display_price - $products_discounts_query->fields['discount_price'];
  1273.           } else {
  1274.             if (!$display_specials_price) {
  1275.               $discounted_price = $display_price - $products_discounts_query->fields['discount_price'];
  1276.             } else {
  1277.               $discounted_price = $display_specials_price - $products_discounts_query->fields['discount_price'];
  1278.             }
  1279.           }
  1280.           break;
  1281.       }
  1282.  
  1283.       return $discounted_price;
  1284.   }
  1285.  
  1286.  
  1287. ////
  1288. // are there discount quantities
  1289.   function zen_get_discount_qty($product_id, $check_qty) {
  1290.     global $db;
  1291.  
  1292.     $product_id = (int)$product_id;
  1293.  
  1294.     $discounts_qty_query = $db->Execute("select pqd.*, p.products_discount_type
  1295.              from " . TABLE_PRODUCTS_DISCOUNT_QUANTITY . " pqd, " .
  1296.               TABLE_PRODUCTS . " p
  1297.             where pqd.products_id='" . (int)$product_id . "' and pqd.discount_qty != 0
  1298.             and p.products_id = pqd.products_id");
  1299. //echo 'zen_get_discount_qty: ' . $product_id . ' - ' . $check_qty . '<br />';
  1300.     if ($discounts_qty_query->RecordCount() > 0 and $check_qty > 0 && $discounts_qty_query->fields['products_discount_type'] !=0) {
  1301.       return true;
  1302.     } else {
  1303.       return false;
  1304.     }
  1305.   }
  1306.  
  1307. ////
  1308. // set the products_price_sorter
  1309.   function zen_update_products_price_sorter($product_id) {
  1310.     global $db;
  1311.  
  1312.     $products_price_sorter = zen_get_products_actual_price($product_id);
  1313.  
  1314.     $db->Execute("update " . TABLE_PRODUCTS . " set
  1315.                  products_price_sorter='" . zen_db_prepare_input($products_price_sorter) . "'
  1316.                  where products_id='" . (int)$product_id . "'");
  1317.   }
  1318.  
  1319. ////
  1320. // salemaker categories array
  1321.   function zen_parse_salemaker_categories($clist) {
  1322.     $clist_array = explode(',', $clist);
  1323.  
  1324. // make sure no duplicate category IDs exist which could lock the server in a loop
  1325.     $tmp_array = array();
  1326.     $n = sizeof($clist_array);
  1327.     for ($i=0; $i<$n; $i++) {
  1328.       if (!in_array($clist_array[$i], $tmp_array)) {
  1329.         $tmp_array[] = $clist_array[$i];
  1330.       }
  1331.     }
  1332.     return $tmp_array;
  1333.   }
  1334.  
  1335. ////
  1336. // update salemaker product prices per category per product
  1337.   function zen_update_salemaker_product_prices($salemaker_id) {
  1338.     global $db;
  1339.     $zv_categories = $db->Execute("select sale_categories_selected from " . TABLE_SALEMAKER_SALES . " where sale_id = '" . (int)$salemaker_id . "'");
  1340.  
  1341.     $za_salemaker_categories = zen_parse_salemaker_categories($zv_categories->fields['sale_categories_selected']);
  1342.     $n = sizeof($za_salemaker_categories);
  1343.     for ($i=0; $i<$n; $i++) {
  1344.       $update_products_price = $db->Execute("select products_id from " . TABLE_PRODUCTS_TO_CATEGORIES . " where categories_id='" . (int)$za_salemaker_categories[$i] . "'");
  1345.       while (!$update_products_price->EOF) {
  1346.         zen_update_products_price_sorter($update_products_price->fields['products_id']);
  1347.         $update_products_price->MoveNext();
  1348.       }
  1349.     }
  1350.   }
  1351.  
  1352.