Exemplo n.º 1
0
  /**
   * Generates a payment for given pay selection line. The payment will be also processed.
   *
   * <p>NOTE: this method is NOT checking if the payment was already created or it's not needed.
   *
   * @param line
   * @return generated payment.
   */
  private I_C_Payment createPayment(final I_C_PaySelectionLine line) {
    final I_C_PaySelection paySelection = line.getC_PaySelection();
    final int ownBankAccountId = paySelection.getC_BP_BankAccount_ID();
    final Timestamp payDate = paySelection.getPayDate();

    final org.compiere.model.I_C_Payment payment =
        Services.get(IPaymentBL.class)
            .newBuilder(line)
            .setC_Invoice(line.getC_Invoice())
            .setAD_Org_ID(line.getAD_Org_ID())
            .setC_BP_BankAccount_ID(ownBankAccountId)
            .setDateAcct(payDate)
            .setDateTrx(payDate)
            .setC_BPartner_ID(line.getC_BPartner_ID())
            .setTenderType(TenderType.ACH)
            .setPayAmt(line.getPayAmt())
            .setDiscountAmt(line.getDiscountAmt())
            //
            .createAndProcess();

    return InterfaceWrapperHelper.create(payment, I_C_Payment.class);
  }
Exemplo n.º 2
0
  @Override
  public void createBankStatementLines(
      final I_C_BankStatement bankStatement, final I_C_PaySelection paySelection) {
    Check.errorIf(
        bankStatement.getC_BP_BankAccount_ID() != paySelection.getC_BP_BankAccount_ID(),
        "C_BankStatement {} with C_BP_BankAccount_ID={} and C_PaySelection {} with C_BP_BankAccount_ID={} need to have the same C_BP_BankAccount_ID",
        bankStatement,
        bankStatement.getC_BP_BankAccount_ID(),
        paySelection,
        paySelection.getC_BP_BankAccount_ID());

    // services
    final IPaySelectionDAO paySelectionDAO = Services.get(IPaySelectionDAO.class);
    final IInvoiceBL invoiceBL = Services.get(IInvoiceBL.class);
    final IBankStatementBL bankStatementBL = Services.get(IBankStatementBL.class);

    I_C_BankStatementLine bankStatementLine = null;
    int nextReferenceLineNo = 10;

    final List<I_C_PaySelectionLine> paySelectionLines =
        paySelectionDAO.retrievePaySelectionLines(paySelection, I_C_PaySelectionLine.class);
    for (final I_C_PaySelectionLine psl : paySelectionLines) {
      // Skip if already in a bank statement
      if (isInBankStatement(psl)) {
        continue;
      }

      // Skip if no invoice
      if (psl.getC_Invoice_ID() <= 0) {
        continue;
      }

      //
      // Create the bank statement line (if not already created)
      if (bankStatementLine == null) {
        bankStatementLine =
            InterfaceWrapperHelper.newInstance(I_C_BankStatementLine.class, paySelection);
        bankStatementLine.setAD_Org_ID(paySelection.getAD_Org_ID());
        bankStatementLine.setC_BankStatement(bankStatement);
        bankStatementLine.setIsMultiplePaymentOrInvoice(
            true); // we have a reference line for each invoice
        bankStatementLine.setIsMultiplePayment(true); // each invoice shall have it's own payment
        bankStatementLine.setC_Currency_ID(bankStatement.getC_BP_BankAccount().getC_Currency_ID());
        bankStatementLine.setValutaDate(paySelection.getPayDate());
        bankStatementLine.setDateAcct(paySelection.getPayDate());
        bankStatementLine.setStatementLineDate(paySelection.getPayDate());
        bankStatementLine.setReferenceNo(null); // no ReferenceNo at this level
        bankStatementLine.setC_BPartner(
            null); // no partner because we will have it on "line reference" level
        bankStatementLine.setStmtAmt(BigDecimal.ZERO); // will be updated at the end
        bankStatementLine.setTrxAmt(BigDecimal.ZERO); // will be updated at the end
        bankStatementLine.setChargeAmt(BigDecimal.ZERO);
        bankStatementLine.setInterestAmt(BigDecimal.ZERO);
        InterfaceWrapperHelper.save(bankStatementLine);
      }

      //
      // Create new bank statement line reference for our current pay selection line.
      final I_C_BankStatementLine_Ref bankStatementLineRef =
          InterfaceWrapperHelper.newInstance(I_C_BankStatementLine_Ref.class, bankStatementLine);
      bankStatementLineRef.setAD_Org_ID(bankStatementLine.getAD_Org_ID());
      bankStatementLineRef.setC_BankStatementLine(bankStatementLine);
      IBankStatementBL.DYNATTR_DisableBankStatementLineRecalculateFromReferences.setValue(
          bankStatementLineRef, true); // disable recalculation. we will do it at the end

      //
      // Set Invoice from pay selection line
      bankStatementLineRef.setC_BPartner_ID(psl.getC_BPartner_ID());
      final I_C_Invoice invoice = psl.getC_Invoice();
      bankStatementLineRef.setC_Invoice(invoice);
      bankStatementLineRef.setC_Currency_ID(invoice.getC_Currency_ID());

      //
      // Get pay schedule line amounts:
      final boolean isReceipt;
      if (invoiceBL.isCreditMemo(invoice)) {
        // SOTrx=Y, but credit memo => receipt=N
        isReceipt = !invoice.isSOTrx();
      } else {
        // SOTrx=Y => receipt=Y
        isReceipt = invoice.isSOTrx();
      }
      final BigDecimal factor = isReceipt ? BigDecimal.ONE : BigDecimal.ONE.negate();
      final BigDecimal linePayAmt = psl.getPayAmt().multiply(factor);
      final BigDecimal lineDiscountAmt = psl.getDiscountAmt().multiply(factor);

      // we store the psl's discount amount, because if we create a payment from this line, then we
      // don't want the psl's Discount to end up as a mere underpayment.
      bankStatementLineRef.setDiscountAmt(lineDiscountAmt);
      bankStatementLineRef.setTrxAmt(linePayAmt);
      bankStatementLineRef.setReferenceNo(psl.getReference());
      bankStatementLineRef.setLine(nextReferenceLineNo);

      //
      // Set Payment from pay selection line.
      // NOTE: In case the pay selection line does not already have a payment generated,
      // we are generating it now because it's the most convenient for the user.
      createPaymentIfNeeded(psl);
      bankStatementLineRef.setC_Payment_ID(psl.getC_Payment_ID());

      //
      // Save the bank statement line reference
      InterfaceWrapperHelper.save(bankStatementLineRef);
      nextReferenceLineNo += 10;

      //
      // Update pay selection line => mark it as reconciled
      linkBankStatementLine(psl, bankStatementLine, bankStatementLineRef);
    }

    //
    // Update Bank Statement Line's totals:
    if (bankStatementLine != null) {
      bankStatementBL.recalculateStatementLineAmounts(bankStatementLine);
    }
  }