List vouchers of the loged user.

Any issue about Cyclos 4 scripting and Webservices

Moderators: rmvanarkel, hugo, alexandre

Post Reply
andreu.honzawa
Posts: 30
Joined: Thu Jan 16, 2020 1:41 pm

List vouchers of the loged user.

Post by andreu.honzawa »

Hi, I'm trying to list all vouchers from the logged user in a script, how can I do it?
And then if I export it with exportVoucher() where the pdf of the voucher is?

Thanks
andreu.honzawa
Posts: 30
Joined: Thu Jan 16, 2020 1:41 pm

Re: List vouchers of the loged user.

Post by andreu.honzawa »

In order to filter the logged user voucher's you can use a dynamic field:

Code: Select all

import org.cyclos.impl.access.SessionData
import org.cyclos.impl.banking.VoucherServiceLocal
import org.cyclos.impl.utils.formatting.FormatterImpl
import org.cyclos.model.banking.vouchers.VoucherQuery
import org.cyclos.model.banking.vouchers.VoucherStatus
import org.cyclos.model.system.fields.DynamicFieldValueVO
import org.cyclos.model.users.users.UserVO
import org.cyclos.model.utils.CurrencyAmountDTO

SessionData sessionData = binding.sessionData
VoucherServiceLocal voucherService = binding.voucherService
FormatterImpl formatter = binding.formatter

return voucherService.search(new VoucherQuery(
    statuses: [VoucherStatus.OPEN] as Set,
    buyer: new UserVO(sessionData.loggedUser.id),
    printed: false
    ))
    .collect { v ->
        def amount = new CurrencyAmountDTO(
            amount: v.pack.amount,
            currency: v.pack.type.configuration.currency)
        new DynamicFieldValueVO(
            value: v.id,
            label: "${v.token} of ${formatter.format(amount)}") 
    }
And then for send the email with the voucher:

Code: Select all

import javax.mail.internet.InternetAddress

import org.cyclos.impl.access.SessionData
import org.cyclos.impl.banking.VoucherServiceLocal
import org.cyclos.impl.utils.notifications.MailHandler
import org.cyclos.impl.utils.validation.validations.EmailValidation
import org.cyclos.model.ValidationException
import org.cyclos.model.banking.vouchers.VoucherVO
import org.cyclos.model.system.exportformats.ExportFormatVO
import org.springframework.mail.javamail.MimeMessageHelper

VoucherServiceLocal voucherService = binding.voucherService
MailHandler mailHandler = binding.mailHandler
Map<String, Object> formParameters = binding.formParameters
SessionData sessionData = binding.sessionData

if (!EmailValidation.isValid(formParameters.email)) {
    throw new ValidationException('Email is invalid');
}

def sender = mailHandler.mailSender
def message = sender.createMimeMessage()
def helper = new MimeMessageHelper(message, true)

def file = voucherService.exportVoucher(
    new ExportFormatVO(internalName: 'pdf'),
    new VoucherVO(formParameters.voucher?.value as Long),
    true).content.file

try {
    helper.to = new InternetAddress(formParameters.email)
    helper.from = new InternetAddress(sessionData.configuration.smtpConfiguration.fromAddress)
    helper.subject = "Voucher"
    helper.text = """
Attached you will find a voucher...
"""
    helper.addAttachment("voucher.pdf", { new FileInputStream(file) }, 'application/pdf')
    sender.send message
    
    return "The e-mail was sent"
} finally {
    file.delete()
}
Post Reply