public void controllItem() {
    if (getEntity() == null) return;

    try {
      if (getEntity().getId() != null) {
        if (getEntity().getDetails() != null && getEntity().getDetails().size() != 0) {
          for (PromissoryFromContactPayrollDetail items : getEntity().getDetails()) {

            if (items.getId() == null) {
              return;
            }

            PromissoryNote promissoryNote = items.getPromissory();

            if (promissoryNote.getLastStatus() != ChequeStatus.Portfoy) {

              isEditable = false;
              break;
            }
          }
        }
      } else {
        isEditable = true;
      }

    } catch (Exception e) {
      log.error("Hata :", e);
    }
  }
  @Observer("promissoryFromContactPayrollHome:popupNotice:client.promissory")
  public void selectClientPromissory(PromissoryNote promissory) {
    manualFlush();

    if (entity == null) {
      return;
    }

    PromissoryFromContactPayrollDetail cpd = new PromissoryFromContactPayrollDetail();
    cpd.setOwner(entity);
    cpd.setPromissory(promissory);
    cpd.getPromissory().setMoney(promissory.getMoney());
    cpd.getPromissory().getMoney().setCurrency(promissory.getMoney().getCurrency());
    cpd.getPromissory().getMoney().setValue(promissory.getMoney().getValue());
    cpd.getPromissory().setInfo(promissory.getInfo());
    cpd.getPromissory().setReferenceNo(promissory.getReferenceNo());

    if (cpd.getPromissory().getId() != null && entity.getDetails().contains(cpd)) {
      facesMessages.add("Senet zaten mevcut");
      return;
    }

    entity.getDetails().add(cpd);

    log.debug("yeni item eklendi");
  }
  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 senetSatirininDetaylariniAta(
      PaymentItemPromissoryNote promissoryNote, PromissoryNote promissory) {

    promissoryNote.setOwner(entity);
    promissoryNote.getAmount().setCurrency(promissory.getMoney().getCurrency());
    promissoryNote.setPromissoryNote(promissory);
    promissoryNote.setAmount(new MoneySet(promissory.getMoney()));
    promissoryNote.getAmount().setValue(promissory.getMoney().getValue());

    String date = DateFormat.getDateInstance().format(promissory.getMaturityDate());

    String info =
        "Senet No: "
            + promissory.getReferenceNo()
            + " ,Keşide Yeri: "
            + promissory.getPaymentPlace()
            + " ,Sahibi: "
            + promissory.getPromissorynoteOwner()
            + " ,Vade: "
            + date;

    if (promissory.getInfo().length() > 0) {
      info = info + ", " + promissory.getInfo();
    }

    if (info.length() > 255) {
      info.substring(0, 255);
    }

    promissoryNote.setInfo(info);
  }
  @Transactional
  public String save() {
    String res = null;

    try {

      if (entity.getDetails().size() <= 0) {
        facesMessages.add("Senet Bilgisi Girilmedi!");
        return BaseConsts.FAIL;
      }

      res = super.save();

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

        for (PromissoryFromContactPayrollDetail cpd : entity.getDetails()) {
          PromissoryNote promissory = cpd.getPromissory();
          // senedin carisini kaydet metodunda da kaydedelim. popupPromissoryCreate metodunda master
          // da cari seçmeden senet eklenirse senedin carisi null oluyordu.
          promissory.setContact(entity.getContact());
          if (!promissoryAction.alreadySaved(
              promissory, DocumentType.PromissoryFromContactPayroll, entity.getId())) {

            PromissoryParamModel cpm = new PromissoryParamModel();
            cpm.setPromissory(promissory);
            cpm.setNewStatus(ChequeStatus.Portfoy);
            cpm.setDocumentId(entity.getId());
            cpm.setDocumentType(DocumentType.PromissoryFromContactPayroll);
            promissoryAction.changePosition(cpm);

            entityManager.merge(promissory);
          }
        }
        financeTxnAction.savePromissoryFromContactPayroll(entity);

        entityManager.flush();
      }

    } catch (Exception e) {
      log.error("Hata :", e);
      e.printStackTrace();
      res = BaseConsts.FAIL;
    }
    return res;
  }
  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;
  }