Automate Balance Updates in PaperCut
A few days ago one of our resellers came to us with an interesting problem. They have a customer who is using the multiple personal accounts feature in PaperCut and they wanted to automate balance updates if the balance went under a certain amount. Updating the balance after so many days have passed works for lots of users, however, this wasn’t the case for our customer.
PaperCut has a very handy quota scheduling feature but it works on days rather than the amount. This, in most cases, would be fine. It can also be done manually by using the bulk user actions but you can’t automate it.
PaperCut has a handy quota scheduling feature for automating balances. It works by updating the balance on a set date, rather than updating the balance after it has reached a certain balance – updating the balance after so many days has passed works for lots of users, however, this wasn’t the case for our customer. This can also be done manually by using the bulk user actions, but… you can’t automate it.
As per usual, we took the request and had a think about it, then broke it down into smaller parts that we could work with:
- Get the user’s current balance for an account
- Compare it to a threshold
- Top the account up to a certain amount
The server command application has a useful option to get a user’s account balance and also has an argument for an account name if you are using multiple personal accounts.
server-command get-user-account-balance username accountname
We can also top up an account by using the server command and leave a comment so that the admin can see where the balance increase came from… which looks something like:
server-command adjust-user-account-balance username amount comment accountname
So, we have the balance for the account and we know how to adjust an account balance… Now, all that is left is to compare the account balance with a threshold and put everything together. This gives us the scripts below which can be scheduled using whatever options your OS of choice has.
Powershell
CLS # Set the path to the server command binary $serverCommand = 'C:\Program Files\PaperCut MF\server\bin\win\server-command.exe' # The Maximum amount you want the purse to contain $maxTopupAmount = 5 # Anything under this will trigger the topup $lowBalanceThreshold = 0.75 # This is the purse to update $purseToUpdate = 'default' # A comment can be added to the transaction so you know where it came from $comment = 'Updated by script' # Get a list of all the users $users = &$serverCommand list-user-accounts # Loop over every user foreach ($user in $users) { # Get the balance of the purse we want to update $balance = &$serverCommand get-user-account-balance $user $purseToUpdate # If the current balance is less than our low balance threshold if ($balance -lt $lowBalanceThreshold) { # Work out how much to add so we don't exceed the max topup amount $amountToTopup = $maxTopupAmount - $balance # Bit of console output Write-Host "Adding '£$amountToTopup' to '$user'" # Add the credit to the required purse &$serverCommand adjust-user-account-balance $user $amountToTopup $comment $purseToUpdate } }
Bash
#!/bin/bash # Set the path to the server command binary serverCommand=/home/papercut/server/bin/linux-x64/server-command # The Maximum amount you want the purse to contain maxTopupAmount=5 # Anything under this will trigger the topup lowBalanceThreshold=0.75 # This is the purse to update purseToUpdate='default' # A comment can be added to the transaction so you know where it came from comment='Updated by script' "${serverCommand}" list-user-accounts | while read user ; do # Get the balance of the purse we want to update balance=$("${serverCommand}" get-user-account-balance $user $purseToUpdate) # If the current balance is less than our low balance threshold if [[ $balance < $lowBalanceThreshold ]] ; then # Work out how much to add so we don't exceed the max topup amount amountToTopup=$(echo "$maxTopupAmount - $balance" | bc) # Bit of console output echo "Adding '£$amountToTopup' to '$user'" # Add the credit to the required purse "${serverCommand}" adjust-user-account-balance $user $amountToTopup "$comment" "$purseToUpdate" fi done