java-class for paymenttype

Functionality discussions and feature requests

Moderators: hugo, alexandre, rmvanarkel

Post Reply
norbert.kapp
Posts: 104
Joined: Mon Oct 08, 2007 9:11 am

java-class for paymenttype

Post by norbert.kapp »

Hi,
I wrote a java-class for insert in a paymenttype. It should be generate a new payment.
I 'm not sure, it this generally possible.
However, generally the class work, but not the new generated payment (PermissionDeniedException).
Can anybody help me.
Thanks!
Norbert

Code: Select all

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.List;
 
import nl.strohalm.cyclos.entities.accounts.Account;
import nl.strohalm.cyclos.entities.accounts.AccountStatus;
import nl.strohalm.cyclos.entities.accounts.MemberAccount;
import nl.strohalm.cyclos.entities.accounts.loans.Loan;
import nl.strohalm.cyclos.entities.accounts.loans.Loan.Type;
import nl.strohalm.cyclos.entities.accounts.loans.LoanParameters;
import nl.strohalm.cyclos.entities.accounts.loans.LoanQuery;
import nl.strohalm.cyclos.entities.accounts.loans.LoanQuery.QueryStatus;
import nl.strohalm.cyclos.entities.accounts.transactions.Transfer;
import nl.strohalm.cyclos.entities.accounts.transactions.TransferListenerAdapter;
import nl.strohalm.cyclos.entities.accounts.transactions.TransferType;
import nl.strohalm.cyclos.entities.customization.fields.MemberCustomFieldValue;
import nl.strohalm.cyclos.entities.members.Member;
import nl.strohalm.cyclos.entities.settings.LocalSettings;
import nl.strohalm.cyclos.exceptions.ExternalException;
import nl.strohalm.cyclos.services.accounts.AccountDTO;
import nl.strohalm.cyclos.services.accounts.AccountServiceLocal;
import nl.strohalm.cyclos.services.settings.SettingsServiceLocal;
import nl.strohalm.cyclos.services.transactions.DoPaymentDTO;
import nl.strohalm.cyclos.services.transactions.TransferDTO;
import nl.strohalm.cyclos.services.transactions.PaymentServiceLocal;
import nl.strohalm.cyclos.services.transfertypes.TransactionFeePreviewDTO;
import nl.strohalm.cyclos.services.transfertypes.TransactionFeeServiceLocal;
import nl.strohalm.cyclos.services.transfertypes.TransferTypeServiceLocal;
import nl.strohalm.cyclos.utils.CustomFieldHelper;
import nl.strohalm.cyclos.utils.DataIteratorHelper;
import nl.strohalm.cyclos.utils.TimePeriod;
import nl.strohalm.cyclos.utils.TimePeriod.Field;
 
/**
 * Grants a loan whenever the payer doesn't have enough balance, up to a limit defined by a custom field
 */
public class ZVPaymentListener extends TransferListenerAdapter {
 
    private static final String        CREDIT_LIMIT_FIELD    = "creditLimit";
    private static final Long          ZVO_TRANSFER_TYPE_ID = 33L;
    private AccountServiceLocal        accountService;
    private PaymentServiceLocal        paymentService;
    private SettingsServiceLocal       settingsService;
    private TransferTypeServiceLocal   transferTypeService;
    private TransactionFeeServiceLocal transactionFeeService;
 
    @Override
    public void onTransferProcessed(Transfer transfer) {
        Account fromAccount = transfer.getActualFrom();
        if (!(fromAccount instanceof MemberAccount)) {
            // A payment from a system account. Nothing to do
            return;
        }
 
        // Get the current account status
        AccountStatus status = accountService.getCurrentStatus(new AccountDTO(fromAccount));
        BigDecimal availableBalance = status.getAvailableBalance();
 
        // There could be transaction fees. We need the loan to take that into account
        TransactionFeePreviewDTO preview = transactionFeeService.preview(transfer.getActualFromOwner(), transfer.getActualToOwner(), transfer.getType(), transfer.getAmount());
        BigDecimal totalPaymentAmount = preview.getFinalAmount().add(preview.getTotalFeeAmount());
 
        LocalSettings localSettings = settingsService.getLocalSettings();
 
        // Validate the maximum credit, which is stored on a custom field
        Member member = ((MemberAccount) fromAccount).getMember();
        /*
        MemberCustomFieldValue fieldValue = CustomFieldHelper.getValue(CREDIT_LIMIT_FIELD, member.getCustomValues());
        BigDecimal creditLimit = fieldValue == null ? null : localSettings.getNumberConverter().valueOf(fieldValue.getValue());
        if (creditLimit == null || creditLimit.compareTo(BigDecimal.ZERO) <= 0) {
            throw new ExternalException("No credit limit and not enough balance in account. Cannot perform payment.");
        }
        */
        // Find the transfer type, validating that it is a loan type
        TransferType transferType;
        try {
            transferType = transferTypeService.load(ZVO_TRANSFER_TYPE_ID);
        } catch (Exception e) {
            throw new ExternalException("Loan type couldn't be resolved");
        }
 
        // Calculate the repayment date
        Calendar now = Calendar.getInstance();
 

        // Do the payment
        
        /*
        from: is this source-payment to
        to:   is in a custom-field (type member) in the source-payment
        
        
        ......
        code for payment
        ......
        
        */
        //THIS PAYMENT NOT WORK
        DoPaymentDTO dto = new DoPaymentDTO();
        dto.setDate(now);
        dto.setFrom(transfer.getActualFromOwner());
        dto.setTo(transfer.getActualToOwner());  //later: member from custom-field
        dto.setAmount(new BigDecimal(1));
        dto.setTransferType(transferType);
        dto.setDescription("SV > SVO");
        paymentService.doPayment(dto);
    }
 
    public void setAccountServiceLocal(AccountServiceLocal accountService) {
        this.accountService = accountService;
    }
 
    public final void setPaymentServiceLocal(PaymentServiceLocal paymentService) {
        this.paymentService = paymentService;
    }
 
    public void setSettingsServiceLocal(SettingsServiceLocal settingsService) {
        this.settingsService = settingsService;
    }
 
    public void setTransactionFeeServiceLocal(TransactionFeeServiceLocal transactionFeeService) {
        this.transactionFeeService = transactionFeeService;
    }
 
    public void setTransferTypeServiceLocal(TransferTypeServiceLocal transferTypeService) {
        this.transferTypeService = transferTypeService;
    }
}

Post Reply