public void makeValidations() {
    if (entity.getItems().size() == 0) {
      facesMessages.add("En az bir detay girmelisiniz!");
      throw new RuntimeException("Hatalar Var! Kayıt gerçekleşmedi!");
    }

    for (PaymentItem item : entity.getItems()) {
      if (item.getAmount().getValue().compareTo(BigDecimal.ZERO) < 0) {
        facesMessages.add("Sıfırdan büyük bir değer girmelisiniz");
        throw new RuntimeException("Hatalar Var! Kayıt gerçekleşmedi!");
      }
    }
  }
  public void createNewLine() {
    manualFlush();
    if (entity == null) {
      return;
    }
    PaymentItem it = new PaymentItem();
    it.setOwner(entity);
    it.setLineType(PaymentType.Cash);
    it.getAmount().setCurrency(BaseConsts.SYSTEM_CURRENCY_CODE);

    entity.getItems().add(it);

    log.debug("yeni item eklendi");
  }
  public void calculateTotals(PaymentItem item) {
    BigDecimal totalAmount = BigDecimal.ZERO;

    if (item.getAmount().getValue().compareTo(BigDecimal.ZERO) < 0) {
      facesMessages.add("Sıfırdan büyük bir değer girmelisiniz");
      throw new RuntimeException("Hatalar Var! Kayıt gerçekleşmedi!");
    }

    dailyRatesDefinedValidation(item.getAmount());

    getCurrencyHelper().setLocalAmountOf(item.getAmount());

    totalAmount = entity.getTotalAmount().getValue().add(item.getAmount().getLocalAmount());
    entity.getTotalAmount().setValue(totalAmount);
  }
  public void deleteItem(int i) {

    PaymentItem item = entity.getItems().get(i);

    try {
      // if (item instanceof PaymentItemCheque) {
      if (item.getLineType().equals(PaymentType.Cheque)) {
        Cheque cheque = ((PaymentItemCheque) item).getCheque();
        entityManager.remove(entityManager.getReference(PaymentItem.class, item.getId()));
        entityManager.remove(entityManager.getReference(Cheque.class, cheque.getId()));

      }
      // else if(item instanceof PaymentItemPromissoryNote) {
      else if (item.getLineType().equals(PaymentType.PromissoryNote)) {
        PromissoryNote promissory = ((PaymentItemPromissoryNote) item).getPromissoryNote();

        entityManager.remove(entityManager.getReference(PaymentItem.class, item.getId()));
        entityManager.remove(entityManager.getReference(PromissoryNote.class, promissory.getId()));
      } else {
        entityManager.remove(entityManager.getReference(PaymentItem.class, item.getId()));
      }

    } catch (Exception e) {
      facesMessages.add("Silmede hata!");
      // log.debug("silme hatası #0", e.getMessage());
      log.info("silme hatası #0 ", e);
      log.debug("hata sebeo #0 :", e);
    }
  }
  public void controllItem() {
    if (getEntity() == null) return;

    try {
      if (getEntity().getId() != null) {
        if (entity.getItems() != null && entity.getItems().size() != 0) {
          for (PaymentItem item : entity.getItems()) {

            if (item.getId() == null) {
              return;
            }
            if (item instanceof PaymentItemCheque) {

              Cheque cheque = ((PaymentItemCheque) item).getCheque();

              if (cheque.getLastStatus() != cheque.getPreviousStatus()) {

                isEditable = false;
                break;
              }

            } else if (item instanceof PaymentItemPromissoryNote) {

              PromissoryNote promissory = ((PaymentItemPromissoryNote) item).getPromissoryNote();

              if (promissory.getLastStatus() != promissory.getPreviousStatus()) {

                isEditable = false;
                break;
              }
            }
          }
        } else {
          isEditable = true;
        }
      }
    } catch (Exception e) {
      log.error("Hata :", e);
    }
  }
  @Override
  @TransactionAttribute(TransactionAttributeType.REQUIRED)
  public String save() {
    String res = BaseConsts.FAIL;
    manualFlush();
    recalculate();
    // TODO: Çoklu dil desteği.

    try {
      makeValidations();

      res = super.save();

      if (res.equals(BaseConsts.SUCCESS)) {

        saveMatches();

        // FIXME: bu kodlar burada ne arıyor?
        for (PaymentItem pItem : entity.getItems()) {

          if (pItem instanceof PaymentItemCheque) {

            Cheque pic = ((PaymentItemCheque) pItem).getCheque();
            ChequeParamModel cpm;

            if (pic != null) {

              pic.setContact(entity.getContact());

              if (!chequeAction.alreadySaved(pic, DocumentType.Collection, entity.getId())) {

                cpm = new ChequeParamModel();
                cpm.setCheque(pic);
                cpm.setDocumentId(entity.getId());
                cpm.setDocumentType(DocumentType.Collection);
                cpm.setNewStatus(ChequeStatus.Portfoy);
                cpm.setWorkBunch(pItem.getWorkBunch());
                cpm.setDocumentSerial(entity.getSerial());
                cpm.setToAccount(entity.getAccount());
                cpm.setToContact(entity.getContact());
                chequeAction.changePosition(cpm);

                entityManager.merge(pic);
              }
            }

          } else if (pItem instanceof PaymentItemPromissoryNote) {

            PromissoryNote pipn = ((PaymentItemPromissoryNote) pItem).getPromissoryNote();
            PromissoryParamModel cpm;

            if (pipn != null) {

              pipn.setContact(entity.getContact());

              if (!promissoryAction.alreadySaved(pipn, DocumentType.Collection, entity.getId())) {

                cpm = new PromissoryParamModel();
                cpm.setPromissory(pipn);
                cpm.setDocumentId(entity.getId());
                cpm.setDocumentType(DocumentType.Collection);
                cpm.setNewStatus(ChequeStatus.Portfoy);
                cpm.setWorkBunch(pItem.getWorkBunch());
                cpm.setDocumentSerial(entity.getSerial());
                cpm.setToContact(entity.getContact());
                cpm.setToAccount(entity.getAccount());
                promissoryAction.changePosition(cpm);

                entityManager.merge(pipn);
              }
            }
          }
        }

        financeTxnAction.savePayment(entity);
        accountTxnAction.savePayment(entity);

        if (entity.getItems() != null && entity.getItems().size() != 0) {

          for (PaymentItem pItem : entity.getItems()) {

            if (pItem.getLineType() != PaymentType.Cash) {

              financeTxnAction.savePaymentItem(pItem);
            }
          }
        }
        entityManager.flush();
      }

    } catch (Exception e) {
      FacesMessages.afterPhase();
      facesMessages.clear();
      facesMessages.add(e.getMessage());
      log.error("Hata :", e);
      res = BaseConsts.FAIL;
    }
    return res;
  }