contract RxEALTestSaleContract {
  // Extend uint256 to use SafeMath functions
  using SafeMath for uint256;

  /* ********** Defined Variables ********** */

  // The token being sold
  RxEALTokenContract public token;

  // Start and end timestamps where sales are allowed (both inclusive)
  uint256 public startTime = 1514808000;
  uint256 public endTime = 1523448000;

  // Address where funds are collected
  address public wallet1 = 0x56E4e5d451dF045827e214FE10bBF99D730d9683;
  address public wallet2 = 0x8C0988711E60CfF153359Ab6CFC8d45565C6ce79;
  address public wallet3 = 0x0EdF5c34ddE2573f162CcfEede99EeC6aCF1c2CB;
  address public wallet4 = 0xcBdC5eE000f77f3bCc0eFeF0dc47d38911CBD45B;

  // How many token units a buyer gets per wei. Rate per ether equals rate * (10 ** token.decimals())
  // Cap in ethers

  // Rate and cap for tier 1
  uint256 public tier_rate_1 = 1800;
  uint256 public tier_cap_1 = 48;
  // Rate and cap for tier 2
  uint256 public tier_rate_2 = 1440;
  uint256 public tier_cap_2 = 144;
  // Rate and cap for tier 3
  uint256 public tier_rate_3 = 1320;
  uint256 public tier_cap_3 = 144;
  // Rate and cap for tier 4
  uint256 public tier_rate_4 = 1200;
  uint256 public tier_cap_4 = 144;

  uint256 public hard_cap;

  // Current tier
  uint8 public current_tier = 1;

  // Amount of raised money in wei
  uint256 public weiRaised;

  // Amount of sold tokens
  uint256 public soldTokens;
  uint256 public current_tier_sold_tokens;

  /* ********** Events ********** */

  // Event for token purchase logging
  event TokenPurchase(address indexed purchaser, address indexed beneficiary, uint256 value, uint256 tokens);

  /* ********** Functions ********** */

  // Constructor
  function RxEALTestSaleContract() {
    token = RxEALTokenContract(0xD6682Db9106e0cfB530B697cA0EcDC8F5597CD15);

    tier_cap_1 = tier_cap_1 * (10 ** token.decimals());
    tier_cap_2 = tier_cap_2 * (10 ** token.decimals());
    tier_cap_3 = tier_cap_3 * (10 ** token.decimals());
    tier_cap_4 = tier_cap_4 * (10 ** token.decimals());

    hard_cap = tier_cap_1 + tier_cap_2 + tier_cap_3 + tier_cap_4;
  }

  // Fallback function can be used to buy tokens
  function () payable {
    buyTokens(msg.sender);
  }

  // Tier calculation function
  function tier_action(
    uint8 tier,
    uint256 left_wei,
    uint256 tokens_amount,
    uint8 next_tier,
    uint256 tier_rate,
    uint256 tier_cap
  ) internal returns (uint256, uint256) {
    if (current_tier == tier) {
      // Tokens to be sold
      uint256 tokens_can_be_sold;
      // Temp tokens to be sold
      uint256 tokens_to_be_sold = left_wei.mul(tier_rate);
      // New temporary sold tier tokens
      uint256 new_tier_sold_tokens = current_tier_sold_tokens.add(tokens_to_be_sold);

      if (new_tier_sold_tokens >= tier_cap) {
        // If purchase reached tier cap

        // Calculate spare tokens
        uint256 spare_tokens = new_tier_sold_tokens.sub(tier_cap);
        // Tokens to be sold
        tokens_can_be_sold = tokens_to_be_sold.sub(spare_tokens);

        // Reset current tier sold tokens
        current_tier_sold_tokens = 0;
        // Switch to next tier
        current_tier = next_tier;
      } else {
        // If purchase not reached tier cap

        // Tokens to be sold
        tokens_can_be_sold = tokens_to_be_sold;
        // Update current tier sold tokens
        current_tier_sold_tokens = new_tier_sold_tokens;
      }

      // Wei to buy amount of tokens
      uint256 wei_amount = tokens_can_be_sold.div(tier_rate);
      // Spare wei amount
      left_wei = left_wei.sub(wei_amount);
      // Tokens to be sold in purchase
      tokens_amount = tokens_amount.add(tokens_can_be_sold);
    }

    return (left_wei, tokens_amount);
  }

  // Low level token purchase function
  function buyTokens(address beneficiary) public payable {
    require(validPurchase());

    uint256 left_wei = msg.value;
    uint256 tokens_amount;

    (left_wei, tokens_amount) = tier_action(1, left_wei, tokens_amount, 2, tier_rate_1, tier_cap_1);
    (left_wei, tokens_amount) = tier_action(2, left_wei, tokens_amount, 3, tier_rate_2, tier_cap_2);
    (left_wei, tokens_amount) = tier_action(3, left_wei, tokens_amount, 4, tier_rate_3, tier_cap_3);
    (left_wei, tokens_amount) = tier_action(4, left_wei, tokens_amount, 4, tier_rate_4, tier_cap_4);

    // Update state of raised wei amount and sold tokens ammount
    uint256 purchase_wei_amount = msg.value.sub(left_wei);
    weiRaised = weiRaised.add(purchase_wei_amount);
    soldTokens = soldTokens.add(tokens_amount);

    // If have spare wei, send it back to beneficiary
    if (left_wei > 0) {
      beneficiary.transfer(left_wei);
    }

    // Tranfer tokens from vault
    token.transferTokensFromVault(msg.sender, beneficiary, tokens_amount);
    TokenPurchase(msg.sender, beneficiary, purchase_wei_amount, tokens_amount);

    forwardFunds(purchase_wei_amount);
  }

  // Send wei to the fund collection wallets
  function forwardFunds(uint256 weiAmount) internal {
    uint256 value = weiAmount.div(4);

    // If buyer sends amount of wei that can not be divided to 4 without float point, send all wei to first wallet
    if (value.mul(4) != weiAmount) {
      wallet1.transfer(weiAmount);
    } else {
      wallet1.transfer(value);
      wallet2.transfer(value);
      wallet3.transfer(value);
      wallet4.transfer(value);
    }
  }

  // Validate if the transaction can buy tokens
  function validPurchase() internal constant returns (bool) {
    bool withinCap = soldTokens < hard_cap;
    bool withinPeriod = now >= startTime && now <= endTime;
    bool nonZeroPurchase = msg.value != 0;
    return withinPeriod && nonZeroPurchase && withinCap;
  }

  // Validate if crowdsale event has ended
  function hasEnded() public constant returns (bool) {
    return now > endTime || soldTokens >= hard_cap;
  }
}
/* ********** RxEAL Presale Contract ********** */
function forwardFunds(uint256 weiAmount) internal {
    uint256 value = weiAmount.div(4);

    // If buyer sends amount of wei that can not be divided to 4 without float point, send all wei to first wallet
    if (value.mul(4) != weiAmount) {
      wallet1.transfer(weiAmount);
    } else {
      wallet1.transfer(value);
      wallet2.transfer(value);
      wallet3.transfer(value);
      wallet4.transfer(value);
    }
  }
// Send wei to the fund collection wallets