<!-- wp:html -->
<p style="font-size: 20px;">
A few days ago one of our resellers came to us with an interesting problem. They have a customer who is using the <a href="https://www.papercut.com/support/resources/manuals/ng-mf/applicationserver/topics/account-multi-personal.html">multiple personal accounts</a> 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. <br><br></p>
<!-- /wp:html -->

<!-- wp:html /-->

<!-- wp:paragraph -->
<p>PaperCut has a very handy <a href="https://www.papercut.com/support/resources/manuals/ng-mf/applicationserver/topics/user-quota-allocation.html">quota scheduling feature</a> 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.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>PaperCut has a handy <a href="https://www.papercut.com/support/resources/manuals/ng-mf/applicationserver/topics/user-quota-allocation.html">quota scheduling</a> 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.</p>
<!-- /wp:paragraph -->

<!-- wp:html -->
<br>
<!-- /wp:html -->

<!-- wp:paragraph -->
<p>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:<br></p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>Get the user's current balance for an account</li><li>Compare it to a threshold</li><li>Top the account up to a certain amount</li></ol>
<!-- /wp:list -->

<!-- wp:html -->
<br>
<!-- /wp:html -->

<!-- wp:paragraph -->
<p>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.<br></p>
<!-- /wp:paragraph -->

<!-- wp:preformatted -->
<pre class="wp-block-preformatted">server-command get-user-account-balance username accountname<br></pre>
<!-- /wp:preformatted -->

<!-- wp:html -->
<br>
<!-- /wp:html -->

<!-- wp:paragraph -->
<p>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:<br></p>
<!-- /wp:paragraph -->

<!-- wp:preformatted -->
<pre class="wp-block-preformatted">server-command adjust-user-account-balance username amount comment accountname<br></pre>
<!-- /wp:preformatted -->

<!-- wp:html -->
<br>
<!-- /wp:html -->

<!-- wp:paragraph -->
<p>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.<br></p>
<!-- /wp:paragraph -->

<!-- wp:html -->
<br>
<!-- /wp:html -->

<!-- wp:heading {"level":3,"align":"left"} -->
<h3 style="text-align:left">Powershell</h3>
<!-- /wp:heading -->

<!-- wp:html -->
<pre class="">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 = &amp;$serverCommand list-user-accounts
 
# Loop over every user
foreach ($user in $users) {
	# Get the balance of the purse we want to update
	$balance = &amp;$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
		&amp;$serverCommand adjust-user-account-balance $user $amountToTopup $comment $purseToUpdate
	}
}


</pre>
<!-- /wp:html -->

<!-- wp:heading {"level":3,"align":"left"} -->
<h3 style="text-align:left">Bash</h3>
<!-- /wp:heading -->

<!-- wp:html -->
<pre class="">#!/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

</pre>
<!-- /wp:html -->

<!-- wp:html -->
<br>
<!-- /wp:html -->

<!-- wp:heading {"level":3,"align":"left"} -->
<h3 style="text-align:left">If you need a hand implementing this command, <a href="https://staging.selectec.com/contact/">get in touch</a> with the team! </h3>
<!-- /wp:heading -->

<!-- wp:html -->
<br>
<!-- /wp:html -->

<!-- wp:html -->
<h4 style="text-align:left; line-height: 1.5em;">You can check out some of our other tips on PaperCut scripts below - if you would like to discuss a custom script that you'd like to implement, we'd love to hear from you.</h4>
<!-- /wp:html -->

<!-- wp:paragraph -->
<p><a href="/peppermint-technology-papercut-integration/">Pepermint integrations</a></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><a href="https://staging.selectec.com/grant-temporary-access-to-papercut/">Granting temporary access to printers</a></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><a href="https://staging.selectec.com/mobility-print-from-ubuntu-client/">Mobility print from Ubuntu client</a></p>
<!-- /wp:paragraph -->