0

Macro to add a category to provider invoice

Alexandros 1 year ago updated by Angela Madrid 1 year ago 9

Hi,

I have been trying to create a macro to run after a periodic job that creates invoice specifications which will add a specific category to the invoices specifications created. The reason is, we want to be able to distinguish which branch each provider invoice should be made out and confirm the providers invoice the correct branch.

The problem is, I cannot seem to find how to add a category to a provider invoice using a macro. If it were a custom field (which are not available for provider invoices for some reason), it would be straightforward.

Has anyone done anything similar before? 

Would it work to get the relevant category/categories through the CategoryService, then use the addAllCategories() method in the ProviderInvoice class?

Thank you. Any idea where the CategoryService is located? It does not seem to be at CategoryService and I am not sure how to get a list of services. 

I went through something similar with projects/quotes. It seems it's not that easy to add a Category to those objects, I'm not sure if invoices are the same.

Basically, you need to create a CommandGateway service(`org.axonframework.commandhandling.gateway.CommandGateway`) then create a command object to pass to it, in my case either a `AddCategoryToOrderCommand` or `AddCategoryToQuoteCommand` that takes the id of the object you're adding the category to, and the id of the category you want to add. Finally, have the CommandGateway `.send()` that command.

None of this seems to be documented anywhere, luckily for us we had previously had XTRF build us a macro that did a similar thing and I was able to copy and modify what they had to suit our needs. Let me know if you need more information (I'm sure you will)

I would love some more information as this (like most other things) indeed does not seem to be documented anywhere.

Alexandros, I'll attempt to document my code here, but remove some of the non-relevant stuff so hopefully it's a little more useful for you. I actually had queried XTRF about documentation for these functions, to which they replied "Unfortunately, we have no such documentation." which is rather disappointing.

hopefully, this code can be adapted to your needs. Unfortunately it seems the "commands" that you can pass the gateway are not available to us anywhere, so it's a bit of a guessing game as to what you would need for an Invoice. If you have questions about anything please ask and I'll do my best to answer.

import com.radzisz.xtrf.model.AbstractProject
import com.radzisz.xtrf.utils.velocity.VelocityTagUtils
import groovy.json.JsonOutput
import eu.xtrf.pa.project.management.api.dto.ProjectCategoryId
import eu.xtrf.pa.project.management.api.dto.ProjectId
import eu.xtrf.pa.project.management.order.api.commands.AddCategoryToOrderCommand
import eu.xtrf.pa.project.management.quote.api.commands.AddCategoryToQuoteCommand
import org.axonframework.commandhandling.gateway.CommandGateway

class SetCategoryOnQuoteMacro {
private final List<AbstractProject> list
private final Map params
private final CommandGateway commandGateway = new VelocityTagUtils().getService(CommandGateway.getCanonicalName())

SetCategoryOnQuoteMacro(List<AbstractProject> list, Map params) {
this.list = list
this.params = params
}

String runMacro() {
def result = [:]
Long catId = params.category
list.each { AbstractProject abstractProject ->
try {
ProjectId projectId = new ProjectId(abstractProject.getAssistedProjectId().get())
Serializable addCategoryCommand
ProjectCategoryId categoryId = new ProjectCategoryId(catId)
if (abstractProject.isProject()) {
addCategoryCommand = new AddCategoryToOrderCommand(projectId, categoryId)
} else {
addCategoryCommand = new AddCategoryToQuoteCommand(projectId, categoryId)
}
commandGateway.send(addCategoryCommand)

result.put("status", "success")
} catch(Exception e) {
result.put("status", "error")
result.put("detail", "${e}")
}
}
return JsonOutput.toJson(result)
}
}

new SetCategoryOnQuoteMacro(list, params).runMacro()

Thank you for the code. I think without proper documentation, there is no way to make this work. The correct idea would be to get a CategoryService (which does not seem to exist) and get my category through that and add it. If anyone has a list of XTRF services (or knows how to get it) it would be helpful.

Hi Alexandros,

We have several processes that add categories to items at regular intervals and it works well for us. There is definitely a category service you can use to look up your category and depending on the object you're working with (provider invoice, customer invoice, project, etc.) there are several methods you can use to add it. For provider invoices, and as Thijs initially suggested, you will need the addAllCategories() method, which requires a set.

Here's an example of the code that you can try out:

import com.radzisz.xtrf.model.invoice.ProviderInvoice
import com.radzisz.xtrf.model.dictionary.Category
import com.radzisz.xtrf.service.dictionary.CategoryService
import com.radzisz.xtrf.utils.velocity.VelocityTagUtils

class AddCategoryMacro {
    def list
    def params
    VelocityTagUtils utils = new VelocityTagUtils()
    CategoryService categoryService = utils.getService("com.radzisz.xtrf.service.dictionary.CategoryService") as CategoryService
    Category myCategory = categoryService.getByNameCaseInsensitive("YOUR CATEGORY NAME HERE")
  
    AddCategoryMacro(list, params) {
        this.list = list
        this.params = params
    }
  
    def runMacro() {
        list.each { 
        ProviderInvoice providerInvoice ->
            providerInvoice.addAllCategories([myCategory].toSet() as Set<Category>)
        }
        return
    }
}
new AddCategoryMacro(list, params).runMacro()

This is all documented in the latest Javadocs package, which you can obtain from XTRF support. Below is a screenshot of the CategoryService as an example:

Image 2483

Wow, thank you so much! I had been using an older javadoc package which did not include this service which is probably why I could not find it. I will ask for the latest one now! 

Yes, for some reason the services were removed from version 8 of the package, but they are present in version 6 and 9, as far as I know. :)