contract Presale is CommonTokensale {
    
    // In case min (soft) cap is not reached, token buyers will be able to 
    // refund their contributions during 3 months after presale is finished.
    uint public refundDeadlineTime;

    // Total amount of wei refunded if min (soft) cap is not reached.
    uint public totalWeiRefunded;
    
    event RefundEthEvent(address indexed _buyer, uint256 _amountWei);
    
    function Presale(
        address _token,
        address _beneficiary1,
        address _beneficiary2,
        address _beneficiary3,
        uint _startTime,
        uint _endTime
    ) CommonTokensale(
        _token,
        _beneficiary1,
        _beneficiary2,
        _beneficiary3,
        _startTime,
        _endTime
    ) public {
        minCapWei = 2000 ether;
        maxCapWei = 4000 ether;
        refundDeadlineTime = _endTime + 3 * 30 days;
    }

    /** 
     * During presale it will be possible to withdraw only in two cases:
     * min cap reached OR refund period expired.
     */
    function canWithdraw() public view returns (bool) {
        return totalWeiReceived >= minCapWei || now > refundDeadlineTime;
    }
    
    /** 
     * It will be possible to refund only if min (soft) cap is not reached and 
     * refund requested during 3 months after presale finished.
     */
    function canRefund() public view returns (bool) {
        return totalWeiReceived < minCapWei && endTime < now && now <= refundDeadlineTime;
    }

    function refund() public {
        require(canRefund());
        
        address buyer = msg.sender;
        uint amount = buyerToSentWei[buyer];
        require(amount > 0);
        
        // Redistribute left balance between three beneficiaries.
        uint newBal = this.balance.sub(amount);
        uint part = newBal / 3;
        balance1 = newBal - part * 2;
        balance2 = part;
        balance3 = part;
        
        RefundEthEvent(buyer, amount);
        buyerToSentWei[buyer] = 0;
        totalWeiRefunded = totalWeiRefunded.add(amount);
        buyer.transfer(amount);
    }
}
function canRefund() public view returns (bool) {
    return totalWeiReceived < minCapWei && endTime < now && now <= refundDeadlineTime;
}

/** 
 * It will be possible to refund only if min (soft) cap is not reached and 
 * refund requested during 3 months after presale finished.
 */