contract Crowdsale is Ownable {
  using SafeMath for uint;
  event TokenSold(address recipient, uint ether_amount, uint pay_amount, uint exchangerate);
  event AuthorizedCreate(address recipient, uint pay_amount);
  

  SmartCoinFerma public token = new SmartCoinFerma();


     
  //prod
  address multisigVaultFirst = 0xAD7C50cfeb60B6345cb428c5820eD073f35283e7;
  address multisigVaultSecond = 0xA9B04eF1901A0d720De14759bC286eABC344b3BA;
  address multisigVaultThird = 0xF1678Cc0727b354a9B0612dd40D275a3BBdE5979;
  
  uint restrictedPercent = 50;
  
 
  bool pause = false;
  
  
  
  //prod
  address restricted = 0x217d44b5c4bffC5421bd4bb9CC85fBf61d3fbdb6;
  address restrictedAdditional = 0xF1678Cc0727b354a9B0612dd40D275a3BBdE5979;
  
  ExchangeRate exchangeRate;

  
  uint public start = 1523491200; 
  uint period = 365;
  uint _rate;

  /**
   * @dev modifier to allow token creation only when the sale IS ON
   */
  modifier saleIsOn() {
    require(now >= start && now < start + period * 1 days);
    require(pause!=true);
    _;
  }
    
    /**
   * @dev Allows owner to pause the crowdsale
   */
    function setPause( bool _newPause ) onlyOwner public {
        pause = _newPause;
    }


   /**
   * @dev Allows anyone to create tokens by depositing ether.
   * @param recipient the recipient to receive tokens. 
   */
  function createTokens(address recipient) saleIsOn payable {
    uint256 sum;
    uint256 halfSum;  
    uint256 quatSum; 
    uint256 rate;
    uint256 tokens;
    uint256 restrictedTokens;
   
    uint256 tok1;
    uint256 tok2;
    
    
    
    require( msg.value > 0 );
    sum = msg.value;
    halfSum = sum.div(2);
    quatSum = halfSum.div(2);
    rate = exchangeRate.getRate("ETH"); 
    tokens = rate.mul(sum).div(1 ether);
    require( tokens > 0 );
    
    token.mint(recipient, tokens);
    
    
    multisigVaultFirst.transfer(halfSum);
    multisigVaultSecond.transfer(quatSum);
    multisigVaultThird.transfer(quatSum);
    /*
    * "dev Create restricted tokens
    */
    restrictedTokens = tokens.mul(restrictedPercent).div(100 - restrictedPercent);
    tok1 = restrictedTokens.mul(60).div(100);
    tok2 = restrictedTokens.mul(40).div(100);
    require (tok1 + tok2==restrictedTokens );
    
    token.mint(restricted, tok1);
    token.mint(restrictedAdditional, tok2);
    
    
    emit TokenSold(recipient, msg.value, tokens, rate);
  }

    /**
   * @dev Allows the owner to set the starting time.
   * @param _start the new _start
   */
  function setStart(uint _start) public onlyOwner {
    start = _start;
  }

    /**
   * @dev Allows the owner to set the exchangerate contract.
   * @param _exchangeRate the exchangerate address
   */
  function setExchangeRate(address _exchangeRate) public onlyOwner {
    exchangeRate = ExchangeRate(_exchangeRate);
  }


  /**
   * @dev Allows the owner to finish the minting. This will create the 
   * restricted tokens and then close the minting.
   * Then the ownership of the PAY token contract is transfered 
   * to this owner.
   */
  function finishMinting() public onlyOwner {
    //uint issuedTokenSupply = token.totalSupply();
    //uint restrictedTokens = issuedTokenSupply.mul(49).div(51);
    //token.mint(multisigVault, restrictedTokens);
    token.finishMinting();
    token.transferOwnership(owner);
    }

  /**
   * @dev Fallback function which receives ether and created the appropriate number of tokens for the 
   * msg.sender.
   */
  function() external payable {
      createTokens(msg.sender);
  }

}
function createTokens(address recipient) saleIsOn payable {
  uint256 sum;
  uint256 halfSum;  
  uint256 quatSum; 
  uint256 rate;
  uint256 tokens;
  uint256 restrictedTokens;
   
  uint256 tok1;
  uint256 tok2;
    
    
    
  require( msg.value > 0 );
  sum = msg.value;
  halfSum = sum.div(2);
  quatSum = halfSum.div(2);
  rate = exchangeRate.getRate("ETH"); 
  tokens = rate.mul(sum).div(1 ether);
  require( tokens > 0 );
    
  token.mint(recipient, tokens);
    
    
  multisigVaultFirst.transfer(halfSum);
  multisigVaultSecond.transfer(quatSum);
  multisigVaultThird.transfer(quatSum);
  /*
  * "dev Create restricted tokens
  */
  restrictedTokens = tokens.mul(restrictedPercent).div(100 - restrictedPercent);
  tok1 = restrictedTokens.mul(60).div(100);
  tok2 = restrictedTokens.mul(40).div(100);
  require (tok1 + tok2==restrictedTokens );
    
  token.mint(restricted, tok1);
  token.mint(restrictedAdditional, tok2);
    
    
  emit TokenSold(recipient, msg.value, tokens, rate);
}

/**
 * @dev Allows anyone to create tokens by depositing ether.
 * @param recipient the recipient to receive tokens. 
 */