<?php
//
// +----------------------------------------------------------------------+
// |zen-cart Open Source E-commerce                                       |
// +----------------------------------------------------------------------+
// | Copyright (c) 2004 The zen-cart developers                           |
// |                                                                      |
// | http://www.zen-cart.com/index.php                                    |
// |                                                                      |
// | Portions Copyright (c) 2003 osCommerce                               |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the GPL license,       |
// | that is bundled with this package in the file LICENSE, and is        |
// | available through the world-wide-web at the following url:           |
// | http://www.zen-cart.com/license/2_0.txt.                             |
// | If you did not receive a copy of the zen-cart license and are unable |
// | to obtain it through the world-wide-web, please send a note to       |
// | license@zen-cart.com so we can mail you a copy immediately.          |
// +----------------------------------------------------------------------+
// $Id: products_discount_prices.php,v 1.1.1.1 2004/09/01 04:47:37 shidairiki Exp $
//

  require(DIR_WS_MODULES . 'require_languages.php');

// create products discount table

  $products_discounts_query = $db->Execute("select * from " . TABLE_PRODUCTS_DISCOUNT_QUANTITY . " where products_id='" . $products_id_current . "' and discount_qty !=0 " . " order by discount_qty");

  $discount_col_cnt = DISCOUNT_QUANTITY_PRICES_COLUMN;
?>
  <table border="0" cellspacing="2" cellpadding="2" class="plainBox" align="center">
    <tr>
      <td colspan="<?php echo $discount_col_cnt; ?>" align="center">
<?php
  switch ($products_discount_type) {
    case '1':
      echo TEXT_HEADER_DISCOUNT_PRICES_PERCENTAGE;
      break;
    case '2':
      echo TEXT_HEADER_DISCOUNT_PRICES_ACTUAL_PRICE;
      break;
    case '3':
      echo TEXT_HEADER_DISCOUNT_PRICES_AMOUNT_OFF;
      break;
  }
?>
      </td>
    </tr>

    <tr>
<?php
  $display_price = zen_get_products_base_price($products_id_current);
  $display_specials_price = zen_get_products_special_price($products_id_current, true);

// set first price value
  if ($display_specials_price == false) {
    $show_price = $display_price;
  } else {
    $show_price = $display_specials_price;
  }

  switch (true) {
    case ($products_discounts_query->fields['discount_qty'] <= 2):
      $show_qty = '1';
      break;
    default:
      $show_qty = '1 - ' . number_format($products_discounts_query->fields['discount_qty']-1);
      break;
  }
?>
  <td class="main" align="center"><?php echo $show_qty . '<br />' . $currencies->display_price($show_price, zen_get_tax_rate($products_tax_class_id)); ?></td>

<?php
//$discounted_price = $products_discounts_query->fields['discount_price'];
// $currencies->display_price($discounted_price, zen_get_tax_rate(1), 1)
  $display_price = zen_get_products_base_price($products_id_current);
  $display_specials_price = zen_get_products_special_price($products_id_current, true);
  $disc_cnt = 1;
  while (!$products_discounts_query->EOF) {
      $disc_cnt++;
      switch ($products_discount_type) {
        // none
        case '0':
          $discounted_price = 0;
          break;
        // percentage discount
        case '1':
          if ($products_discount_type_from == '0') {
            $discounted_price = $display_price - ($display_price * ($products_discounts_query->fields['discount_price']/100));
          } else {
            if (!$display_specials_price) {
              $discounted_price = $display_price - ($display_price * ($products_discounts_query->fields['discount_price']/100));
            } else {
              $discounted_price = $display_specials_price - ($display_specials_price * ($products_discounts_query->fields['discount_price']/100));
            }
          }

          break;
        // actual price
        case '2':
          if ($products_discount_type_from == '0') {
            $discounted_price = $products_discounts_query->fields['discount_price'];
          } else {
            $discounted_price = $products_discounts_query->fields['discount_price'];
          }
          break;
        // amount offprice
        case '3':
          if ($products_discount_type_from == '0') {
            $discounted_price = $display_price - $products_discounts_query->fields['discount_price'];
          } else {
            if (!$display_specials_price) {
              $discounted_price = $display_price - $products_discounts_query->fields['discount_price'];
            } else {
              $discounted_price = $display_specials_price - $products_discounts_query->fields['discount_price'];
            }
          }
          break;
      }

      $show_qty = number_format($products_discounts_query->fields['discount_qty']);
      $products_discounts_query->MoveNext();
      if ($products_discounts_query->EOF) {
        $show_qty .= '+';
      } else {
        $show_qty .= '-' . number_format($products_discounts_query->fields['discount_qty']-1);
      }
      echo '<td class="main" align="center">' . $show_qty . '<br />' . $currencies->display_price($discounted_price, zen_get_tax_rate($products_tax_class_id)) . '</td>';
      if ($discount_col_cnt == $disc_cnt and !$products_discounts_query->EOF) {
        echo '</tr><tr>';
        $disc_cnt=0;
      }
    }
?>
    </tr>
<?php
  if (zen_has_product_attributes($products_id_current)) {
?>
    <tr>
      <td colspan="<?php echo $discount_col_cnt; ?>" align="center">
        <?php echo TEXT_FOOTER_DISCOUNT_QUANTITIES; ?>
      </td>
    </tr>
<?php } ?>
  </table>
