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 createNew() {
   cheque = new Cheque();
   cheque.setIssueDate(calendarManager.getCurrentDate());
   cheque.setSerialNo(sequenceManager.getNewSerialNumber(SequenceType.SERIAL_CHEQUE));
   cheque.setEntryDate(calendarManager.getCurrentDate());
   cheque.setClientCheque(isClient);
   cheque.getMoney().setCurrency(BaseConsts.SYSTEM_CURRENCY_CODE);
 }
 public void setChequeFromChequeStub() {
   if (chequeStub != null) {
     cheque.setAccountNo(chequeStub.getAccountNo());
     cheque.setBankBranch(chequeStub.getBankBranch());
     cheque.setBankName(chequeStub.getBankName());
     cheque.setBankAccountId(chequeStub.getBankAccount().getId());
     cheque.setBankBranchId(chequeStub.getBankAccount().getBankBranch().getId());
     cheque.setPaymentPlace(chequeStub.getPaymentPlace());
     cheque.setIban(chequeStub.getIban());
     cheque.setChequeOwner(chequeStub.getChequeOwner());
     cheque.getMoney().setCurrency(chequeStub.getBankAccount().getCurrency());
   }
 }
  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);
    }
  }
  public void cekSatirininDetaylariniAta(PaymentItemCheque itemCheque, Cheque cheque) {

    itemCheque.setOwner(entity);
    itemCheque.getAmount().setCurrency(cheque.getMoney().getCurrency());
    itemCheque.setCheque(cheque);
    itemCheque.setAmount(new MoneySet(cheque.getMoney()));
    itemCheque.getAmount().setValue(cheque.getMoney().getValue());
    itemCheque.getAmount().setCurrency(cheque.getMoney().getCurrency());

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

    String info =
        "Çek No: "
            + cheque.getReferenceNo()
            + " ,Banka: "
            + cheque.getBankName()
            + "/"
            + cheque.getBankBranch()
            + " ,Hesap No: "
            + cheque.getAccountNo()
            + " ,Sahibi: "
            + cheque.getChequeOwner()
            + " ,Vade: "
            + date;

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

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

    itemCheque.setInfo(info);
  }
  @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;
  }