contract PostboyToken is ERC20 {
    using SafeMath for uint256;

    struct Account {
        uint256 balance;
        uint256 lastDividends;
    }

    string public constant name = "PostboyToken"; // solium-disable-line uppercase
    string public constant symbol = "PBY"; // solium-disable-line uppercase
    uint8 public constant decimals = 0; // solium-disable-line uppercase

    uint256 public constant INITIAL_SUPPLY = 100000;

    uint256 public totalDividends;
    uint256 totalSupply_;
    
    mapping (address => Account) accounts;
    mapping (address => mapping (address => uint256)) internal allowed;

    address public admin;
    address public payer;

  /**
   * @dev Constructor that gives msg.sender all of existing tokens.
   */
    constructor() public {
        totalSupply_ = INITIAL_SUPPLY;
        totalDividends = 0;
        accounts[msg.sender].balance = INITIAL_SUPPLY;
        admin = msg.sender;
        payer = address(0);
        emit Transfer(address(0), msg.sender, INITIAL_SUPPLY);
    }

    /**
    * @dev Total number of tokens in existence
    */
    function totalSupply() public view returns (uint256) {
        return totalSupply_;
    }

    /**
    * @dev Transfer token for a specified address
    * @param _to The address to transfer to.
    * @param _value The amount to be transferred.
    */
    function transfer(address _to, uint256 _value) public returns (bool) {
        _transfer(msg.sender, _to, _value);
        return true;
    }

    /**
    * @dev Transfer tokens from one address to another
    * @param _from address The address which you want to send tokens from
    * @param _to address The address which you want to transfer to
    * @param _value uint256 the amount of tokens to be transferred
    */
    function transferFrom(
        address _from,
        address _to,
        uint256 _value
    )
      public
      returns (bool)
    {
        require(_value <= allowed[_from][msg.sender]);

        allowed[_from][msg.sender] = allowed[_from][msg.sender].sub(_value);
        _transfer(_from, _to, _value);

        return true;
    }

    /**
    * @dev Gets the balance of the specified address.
    * @param _owner The address to query the the balance of.
    * @return An uint256 representing the amount owned by the passed address.
    */
    function balanceOf(address _owner) public view returns (uint256) {
        return accounts[_owner].balance;
    }

    /**
    * @dev Approve the passed address to spend the specified amount of tokens on behalf of msg.sender.
    * Beware that changing an allowance with this method brings the risk that someone may use both the old
    * and the new allowance by unfortunate transaction ordering. One possible solution to mitigate this
    * race condition is to first reduce the spender's allowance to 0 and set the desired value afterwards:
    * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
    * @param _spender The address which will spend the funds.
    * @param _value The amount of tokens to be spent.
    */
    function approve(address _spender, uint256 _value) public returns (bool) {
        allowed[msg.sender][_spender] = _value;
        emit Approval(msg.sender, _spender, _value);
        return true;
    }

    /**
    * @dev Function to check the amount of tokens that an owner allowed to a spender.
    * @param _owner address The address which owns the funds.
    * @param _spender address The address which will spend the funds.
    * @return A uint256 specifying the amount of tokens still available for the spender.
    */
    function allowance(
        address _owner,
        address _spender
    )
      public
      view
      returns (uint256)
    {
        return allowed[_owner][_spender];
    }

    /**
    * @dev Increase the amount of tokens that an owner allowed to a spender.
    * approve should be called when allowed[_spender] == 0. To increment
    * allowed value is better to use this function to avoid 2 calls (and wait until
    * the first transaction is mined)
    * From MonolithDAO Token.sol
    * @param _spender The address which will spend the funds.
    * @param _addedValue The amount of tokens to increase the allowance by.
    */
    function increaseApproval(
        address _spender,
        uint256 _addedValue
    )
      public
      returns (bool)
    {
        allowed[msg.sender][_spender] = (
            allowed[msg.sender][_spender].add(_addedValue));
        emit Approval(msg.sender, _spender, allowed[msg.sender][_spender]);
        return true;
    }

    /**
    * @dev Decrease the amount of tokens that an owner allowed to a spender.
    * approve should be called when allowed[_spender] == 0. To decrement
    * allowed value is better to use this function to avoid 2 calls (and wait until
    * the first transaction is mined)
    * From MonolithDAO Token.sol
    * @param _spender The address which will spend the funds.
    * @param _subtractedValue The amount of tokens to decrease the allowance by.
    */
    function decreaseApproval(
        address _spender,
        uint256 _subtractedValue
    )
      public
      returns (bool)
    {
        uint256 oldValue = allowed[msg.sender][_spender];
        if (_subtractedValue > oldValue) {
            allowed[msg.sender][_spender] = 0;
        } else {
            allowed[msg.sender][_spender] = oldValue.sub(_subtractedValue);
        }
        emit Approval(msg.sender, _spender, allowed[msg.sender][_spender]);
        return true;
    }

    /**
    * @dev Get dividents sum by address
    */
    function dividendBalanceOf(address account) public view returns (uint256) {
        uint256 newDividends = totalDividends.sub(accounts[account].lastDividends);
        uint256 product = accounts[account].balance.mul(newDividends);
        return product.div(totalSupply_);
    }

    /**
    * @dev Withdraw dividends
    */
    function claimDividend() public {
        uint256 owing = dividendBalanceOf(msg.sender);
        if (owing > 0) {
            accounts[msg.sender].lastDividends = totalDividends;
            msg.sender.transfer(owing);
        }
    }


    /**
    * @dev Tokens transfer will not work if sender or recipient has dividends
    */
    function _transfer(address _from, address _to, uint256 _value) internal {
        require(_to != address(0));
        require(_value <= accounts[_from].balance);
        require(accounts[_to].balance + _value >= accounts[_to].balance);
    
        uint256 fromOwing = dividendBalanceOf(_from);
        uint256 toOwing = dividendBalanceOf(_to);
        require(fromOwing <= 0 && toOwing <= 0);
    
        accounts[_from].balance = accounts[_from].balance.sub(_value);
        accounts[_to].balance = accounts[_to].balance.add(_value);
    
        accounts[_to].lastDividends = accounts[_from].lastDividends;
    
        emit Transfer(_from, _to, _value);
    }

    function changePayer(address _payer) public returns (bool) {
        require(msg.sender == admin);
        payer = _payer;
    }

    function sendDividends() public payable {
        require(msg.sender == payer);
        
        totalDividends = totalDividends.add(msg.value);
    }

    function () external payable {
        require(false);
    }
}
/**
 * @title SimpleToken
 * @dev Very simple ERC20 Token example, where all tokens are pre-assigned to the creator.
 * Note they can later distribute these tokens as they wish using `transfer` and other
 * `StandardToken` functions.
 */
function _transfer(address _from, address _to, uint256 _value) internal {
    require(_to != address(0));
    require(_value <= accounts[_from].balance);
    require(accounts[_to].balance + _value >= accounts[_to].balance);
    
    uint256 fromOwing = dividendBalanceOf(_from);
    uint256 toOwing = dividendBalanceOf(_to);
    require(fromOwing <= 0 && toOwing <= 0);
    
    accounts[_from].balance = accounts[_from].balance.sub(_value);
    accounts[_to].balance = accounts[_to].balance.add(_value);
    
    accounts[_to].lastDividends = accounts[_from].lastDividends;
    
    emit Transfer(_from, _to, _value);
}

/**
 * @dev Tokens transfer will not work if sender or recipient has dividends
 */