Available in PaperCut MF only.

Example: Discount the copying price for students during off-peak hours and for staff at all times

NOTE
  • Device scripting is currently in Percolator, so bear on mind that it will be refined before launch. If you have any feedback or recipe requests, check out the Project Myrtle Percolator page.

  • You can also use Print scripting to define and fine tune your printing policies.

West Face University has a student population of 10,000 full-time students and over 1,000 staff. Currently their costing model has a charge of $0.50 for A3 and $0.40 for A4 pages. They would, however, like more control over the cost structure by setting a different price for staff and students. They would like to charge staff $0.30 for A3 and $0.15 for A4 pages.

They would also like to encourage students to do their copying during off-peak periods to decrease the queues at the MFDs. West Face University are willing to offer a 10% discount for copy jobs performed before 11am and after 3pm.

Requirements

Using device scripting, the West Face University will:

  • apply a different cost model for staff and students

  • offer a discount for any job performed between 3pm and 11 am.

Implementation

This example will demonstrate how to create a device script from scratch, without the use of a recipe or snippetCode snippets are small code fragments that demonstrate how to use the scripting API (inputs, functions, and methods)..

To create this script, you will:

  1. Add a deviceJobLogHook

  2. Create a function to adjust the cost for staff

  3. Create a function to adjust the copy pricing in off-peak periods

  4. Test your script and go live

You can also download the script here and then copy it into the Script Editor.

TIP

For more information about creating device scripts, see:

Add a deviceJobLogHook

Every script needs to start with a "hook". This hook defines the event that triggers the script to run. In this example, the script needs to be triggered when a user performs a copy job, so deviceJobLogHook is the right hook.

  1. Click the Devices tab.

    The External Device List page is displayed.

  2. Select a test device.

    The Device Details page is displayed.

    IMPORTANT

    Device scripts are applied as soon as you click Apply, so make sure you create your script on a test device

  3. Click the Scripting tab.

  4. Select the Enable device script checkbox.

    By default, the deviceLoginHook is included in the script editor.

    In this example, you want to use deviceJobLogHook. This function needs to:

    • check if it is NOT a copy job, and if it is not, don't run the script (see 1 in the image below).

    • check if the user is a staff member and if so, call the adjustCopyCostForStaff function, which calculates the copy job cost (see 2 in the image below). This function is defined later in the script.

    • check if the user is a student and has performed the copy job during the off-peak period and if so, call the adjustCopyCostForOffPeak function, which calculates the copy job cost (see 3 in the image below). This function is defined later in the script.

  5. In the function name, change deviceLoginHook to deviceJobLogHook.

  6. Below the function name, add an if statement to check if the job is a copy job.

    // If it's not a copy job we won't run the rest of this script. if (!inputs.job.isCopy) { return; }
  7. Add an if statement to check if the user is a staff member and if so, calculate the copy job cost. This statement calls the function (adjustCopyCostForStaff) that you will define in Create a function to adjust the cost for staff.

    // staff get charged under a different cost model if (inputs.user.isInGroup("Staff")) { adjustCopyCostForStaff(inputs,actions); }
  8. Add an if statement to check if the user is a student and has performed the copy job during the off-peak period and if so, calculate the copy job cost. This statement calls the function (adjustCopyCostForOffPeak) that you will define in Create a function to adjust the copy pricing in off-peak periods.

    // students can get charged extra for using the copier // during peak hour. if(inputs.user.isInGroup("Students")) { adjustCopyCostForOffPeak(inputs,actions); }
    TIP

    Make sure you have not accidentally deleted the closing parentheses...it's very easy to do.

Create a function to adjust the cost for staff

Next you need to create a function that calculates the copy cost for staff. This function is called from the deviceJobLogHook and includes the following:

  • a variable that defines the staff cost model (see 1 in the image below).

  • variables used to determine the page size and number of pages in the copy job (see 2 in the image below).

  • variables used to calculate the adjusted job cost (see 3 in the image below).

  • applies the adjusted job cost (see 4 in the image below).

  1. Add the function:

    /** * This function adjusts the job cost if it's a staff member. */ function adjustCopyCostForStaff(inputs, actions) { }
  2. Before the closing parenthesis, add a variable that defines the staff cost model. These variables will be referenced when the cost is calculated.

    // Using a customised pricing model here // We'll keep it simple and differentiate only by paper size - // we won't worry about duplex or color mode for this example. var staffSimpleModel = { A3: 0.3, A4: 0.15, Default: 0.15 // fall back to default if we can't find the right size };
  3. Add the variables used to determine the page size and number of pages in the copy job:

    // analyze the job var totalPages = inputs.job.totalPages; var paperSizeName = inputs.job.paperSizeName;
  4. Add the variables used to calculate the adjusted job cost based:

    // do a simple calculation var costPerPage = staffSimpleModel[paperSizeName] ? staffSimpleModel[paperSizeName] : staffSimpleModel["Default"]; var adjustedCost = costPerPage * totalPages;
  5. Add the actions to apply the adjusted job cost and add a comment to the Job Log.

    // change the job cost actions.job.setCost(adjustedCost); actions.job.addComment("Costs adjusted for staff member");

Create a function to adjust the copy pricing in off-peak periods

Next you need to create a function that calculates the copy cost for staff. This function is called from the deviceJobLogHook and includes the following:

  • variables that define the peak period and discounted rate for off-peak (see 1 in the image below).

  • variables used to determine if the coy job has been performed in the peak period (see 2 in the image below).

  • determines if the job has been performed in the off-peak period and if it is, applies the adjusted job cost (see 3 in the image below).

  1. Add the function:

    /** * For this function we'll adjust the copy job pricing * if the job was done during peak hours. */ function adjustCopyCostForOffPeak(inputs, actions) { }
  2. Before the closing parenthesis, variables that define the peak period and discounted rate for off-peak.

    var PEAK_PERIOD_START = 11; // 11am var PEAK_PERIOD_END = 15; // 3pm var DISCOUNTED_RATE = 10; // 10% discount for off peak };
  3. Add the variables used to determine if the copy job has been performed in the peak period. These variables will be referenced when the cost is calculated.

    var cost = inputs.job.cost; var date = new Date(); var hour = date.getHours(); };
  4. Add the actions to determine if the job has been performed in the off-peak period and if it is, applies the adjusted job cost:

    if (hour < PEAK_PERIOD_START || hour >= PEAK_PERIOD_END){ var discountedCost = cost - (cost * (DISCOUNTED_RATE / 100)); actions.job.setCost(discountedCost); actions.job.addComment("Discount applied for copying during off peak"); }
  5. Click Apply. You have now created the script.

Test your script and go live

Before you copy your script to production devices, make sure you check that it works in on a test device.

  1. Check the following on a test device:

    • as a student, perform a copy job after 3pm and check if the user account was charged the discounted cost.

    • as a student, perform a copy job between 11am and 3pm and check if the user account was charged the standard cost.

    • as a staff member, perform a copy job and check if the user account was charged the cheaper cost.

  2. Copy your device script to the required devices.