/**
  * Get billing history
  *
  * @param card
  * @param limit
  * @param start
  * @return
  */
 public String getHistory(Card card, Integer limit, Integer start) {
   this.currentCard = card;
   JSONObject json = new JSONObject();
   List<Invoice> history = invoiceService.getIncome(currentCard);
   history.addAll(invoiceService.getOutcome(currentCard));
   try {
     json.put("total", history.size());
     for (int i = start; i <= start + limit && i < history.size(); i++) {
       json.accumulate("history", history.get(i).toJson());
     }
   } catch (JSONException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
   }
   return json.toString();
 }
 /**
  * Pay
  *
  * @param amount
  * @param to
  * @param r
  * @return
  */
 public String pay(Double amount, Integer to, HttpServletRequest r) {
   JSONObject json = new JSONObject();
   double currentAmount = currentCard.getAmount();
   if (currentAmount < amount) {
     try {
       json.put("sucess", false);
       json.put("result", "No enough money.Choose anothe opperation.");
     } catch (JSONException e) {
       e
           .printStackTrace(); // To change body of catch statement use File | Settings | File
                               // Templates.
     }
   } else {
     currentCard.setAmount(currentAmount - amount);
     Invoice invoice = currentCard.createInvoice();
     invoice.setToCard(to);
     invoice.setAmount(amount);
     String date =
         new SimpleDateFormat("dd.MM.yyyy-hh.mm-a")
             .format(GregorianCalendar.getInstance().getTime());
     invoice.setDate(date);
     invoice.setCurrency(currentCard.getCurrency());
     invoiceService.addInvoice(invoice);
     try {
       json.put("success", true);
     } catch (JSONException e) {
       e
           .printStackTrace(); // To change body of catch statement use File | Settings | File
                               // Templates.
     }
   }
   cardService.update(currentCard);
   r.getSession().setAttribute("Card", currentCard);
   return json.toString();
 }
  /**
   * Transfer money from current to another card
   *
   * @param destCardId
   * @param amount
   * @param r
   * @return result in JSON format
   */
  @Transactional
  public String transferMoney(Integer destCardId, double amount, HttpServletRequest r) {
    Card toCard = cardService.getCard(destCardId);
    double currentAmount = currentCard.getAmount();
    JSONObject json = new JSONObject();
    if (currentAmount < amount) {
      try {
        json.put("sucess", false);
        json.put("result", "Недостаточно средств.");
      } catch (JSONException e) {
        e
            .printStackTrace(); // To change body of catch statement use File | Settings | File
                                // Templates.
      }

    } else {
      currentCard.setAmount(currentAmount - amount);
      double newAmount = converter.convert(currentCard.getCurrency(), toCard.getCurrency(), amount);
      toCard.setAmount(toCard.getAmount() + newAmount);
      Invoice invoice = currentCard.createInvoice();
      invoice.setToCard(destCardId);
      invoice.setAmount(newAmount);
      String date =
          new SimpleDateFormat("dd.MM.yyyy-hh.mm-a")
              .format(GregorianCalendar.getInstance().getTime());
      invoice.setDate(date);
      invoice.setCurrency(toCard.getCurrency());
      invoiceService.addInvoice(invoice);
      try {
        json.put("sucess", true);
        json.put("result", "Успешно.");
      } catch (JSONException e) {
        e
            .printStackTrace(); // To change body of catch statement use File | Settings | File
                                // Templates.
      }
    }
    cardService.update(currentCard);
    r.getSession().setAttribute("Card", currentCard);
    cardService.update(toCard);
    return json.toString();
  }