contract DebitableToken is StandardToken, Ownable {
  event Debit(address collateral_contract, uint256 amount);
  event Deposit(address indexed _to_debitor, uint256 _value);  
  event DebitFinished();

  using SafeMath for uint256;
  bool public debitingFinished = false;

  modifier canDebit() {
    require(!debitingFinished);
    _;
  }

  modifier hasDebitPermission() {
    require(msg.sender == owner);
    _;
  }
  
  /**
   * @dev Function to debit tokens
   * @param _to The address that will receive the drawdown tokens.
   * @param _amount The amount of tokens to debit.
   * @return A boolean that indicates if the operation was successful.
   */
  function debit(
    address _to,
    uint256 _amount
  )
    public
    hasDebitPermission
    canDebit
    returns (bool)
  {
    dc = COLLATERAL(collateral_contract);
    uint256 rate = dc.CreditRate();
    uint256 deci = 10 ** decimals; 
    uint256 _amount_1 =  _amount / deci / rate;
    uint256 _amount_2 =  _amount_1 * deci * rate;
    
    require( _amount_1 > 0);
    dc.credit( _amount_1 );  
    
    uint256 _amountx = _amount_2;
    totalSupply = totalSupply.add(_amountx);
    balances[_to] = balances[_to].add(_amountx);
    emit Debit(collateral_contract, _amountx);
    emit Deposit( _to, _amountx);
    return true;
  }

  /**
   * @dev To stop debiting tokens.
   * @return True if the operation was successful.
   */
  function finishDebit() public onlyOwner canDebit returns (bool) {
    debitingFinished = true;
    emit DebitFinished();
    return true;
  }

  
}
/**
 * @title Debitable token
 * @dev ERC20 Token, with debitable token creation
 */
function finishDebit() public onlyOwner canDebit returns (bool) {
  debitingFinished = true;
  emit DebitFinished();
  return true;
}

/**
 * @dev To stop debiting tokens.
 * @return True if the operation was successful.
 */