I have been tasked with combining two if statements in Js
for a papercut script. It is a print management software. I have everything I need I believe in the script below. The problem is combining these two if's into one statement I believe. I am not familiar with Javascript as well as I am with python. I am hoping for some help in rearranging this script to do as stated below.
PaperCut print script API reference
Goal:
Only do the cost center popup if they print jobs 10+ pages, otherwise just automatically charge the job to the firm non-billable (ADM-3900) account. If the job is 50+ pages, redirect it from the HP to the larger copier. In this case, from test_printer3 to Copier – Color.
/* * Redirect large jobs without confirmation * * Users printing jobs larger than the defined number of pages have their jobs * automatically redirected to another printer or virtual queue. * This can be used to redirect large jobs from slower or high cost printers * to more efficient or faster high volume printers. */ function printJobHook(inputs, actions) { /* * This print hook will need access to all job details * so return if full job analysis is not yet complete. * The only job details that are available before analysis * are metadata such as username, printer name, and date. * * See reference documentation for full explanation. */ /* * NOTE: The high-volume printer must be compatible with the source printer. * i.e. use the same printer language like PCL or Postscript. * If this is a virtual queue, all printers in the queue must use * the same printer language. */ if (!inputs.job.isAnalysisComplete) { // No job details yet so return. return; actions.job.chargeToPersonalAccount(); return; if (inputs.job.totalPages < 10) { // Charge to the firm non-bill account actions.job.chargeToSharedAccount(ADM-3900); } // Account Selection will still show } var LIMIT = 5; // Redirect jobs over 5 pages. var HIGH_VOL_PRINTER = "Copier - Color"; if (inputs.job.totalPages > LIMIT) { /* * Specify actions.job.bypassReleaseQueue() if you wish to bypass the release queue * on the original printer the job was sent to. (Otherwise if held at the target, * the job will need to be released from two different queues before it will print.) */ actions.job.bypassReleaseQueue(); /* * Job is larger than our page limit, so redirect to high-volume printer, * and send a message to the user. * Specify "allowHoldAtTarget":true to allow the job to be held at the hold/release * queue for the high-volume printer, if one is defined. */ actions.job.redirect(HIGH_VOL_PRINTER, {allowHoldAtTarget: true}); // Notify the user that the job was automatically redirected. actions.client.sendMessage( "The print job was over " + LIMIT + " pages and was sent to " + " printer: " + HIGH_VOL_PRINTER + "."); // Record that the job was redirected in the application log. actions.log.info("Large job redirected from printer '" + inputs.job.printerName + "' to printer '" + HIGH_VOL_PRINTER + "'."); } }
4 Answers
Answers 1
I believe this is what you're looking for, but it's not entirely clear. It is difficult to merge conditions without knowing all the logic branches.
/* * Redirect large jobs without confirmation * * Users printing jobs larger than the defined number of pages have their jobs * automatically redirected to another printer or virtual queue. * This can be used to redirect large jobs from slower or high cost printers * to more efficient or faster high volume printers. */ function printJobHook(inputs, actions) { /* * This print hook will need access to all job details * so return if full job analysis is not yet complete. * The only job details that are available before analysis * are metadata such as username, printer name, and date. * * See reference documentation for full explanation. */ /* * NOTE: The high-volume printer must be compatible with the source printer. * i.e. use the same printer language like PCL or Postscript. * If this is a virtual queue, all printers in the queue must use * the same printer language. */ var LIMIT = 5; // Redirect jobs over 5 pages. var HIGH_VOL_PRINTER = "Copier - Color"; if (!inputs.job.isAnalysisComplete) { return;// No job details yet so return. } //Charge jobs with less than 10 pages to non-bill account if (inputs.job.totalPages < 10) { // Charge to the firm non-bill account actions.job.chargeToSharedAccount(ADM-3900); } else //Charge jobs with more than 10 pages to the personal account { actions.job.chargeToPersonalAccount(); if (inputs.job.totalPages > LIMIT) { /* * Specify actions.job.bypassReleaseQueue() if you wish to bypass the release queue * on the original printer the job was sent to. (Otherwise if held at the target, * the job will need to be released from two different queues before it will print.) */ actions.job.bypassReleaseQueue(); /* * Job is larger than our page limit, so redirect to high-volume printer, * and send a message to the user. * Specify "allowHoldAtTarget":true to allow the job to be held at the hold/release * queue for the high-volume printer, if one is defined. */ actions.job.redirect(HIGH_VOL_PRINTER, {allowHoldAtTarget: true}); // Notify the user that the job was automatically redirected. actions.client.sendMessage( "The print job was over " + LIMIT + " pages and was sent to " + " printer: " + HIGH_VOL_PRINTER + "."); // Record that the job was redirected in the application log. actions.log.info("Large job redirected from printer '" + inputs.job.printerName + "' to printer '" + HIGH_VOL_PRINTER + "'."); } } return }
Answers 2
I think that the problem you have here has to deal with the multiple return statements you have in that if statement block you mentioned.
This block is what you have...
if (!inputs.job.isAnalysisComplete) { return; actions.job.chargeToPersonalAccount(); return; if (inputs.job.totalPages < 10) { actions.job.chargeToSharedAccount(ADM-3900); } }
I think this block would be more accurate if it was something like this...
/*No details of print analysis? Return the the function immediately!*/ if (!inputs.job.isAnalysisComplete) { return; } /*Job less than ten pages? Charge shared account. Otherwise charge personal account.*/ if (inputs.job.totalPages < 10) { /*Also, my bet is that the ADM-3900 needs to be in quotes for a string unless other wise stated in the manual.*/ actions.job.chargeToSharedAccount("ADM-3900"); } else { actions.job.chargeToPersonalAccount(); }
Note, this is my best guess seeing as I am not familiar with Papercut software.
If that didn't help there is always technical support
Answers 3
Based on what i have understood form your GOAL i will do next changes to your code:
/* * Redirect large jobs without confirmation * * Users printing jobs larger than the defined number of pages have their jobs * automatically redirected to another printer or virtual queue. * This can be used to redirect large jobs from slower or high cost printers * to more efficient or faster high volume printers. */ // Setup limit for charge the job to ADM-3900. var ADM_3900_CHARGE_LIMIT = 10; // Setup of redirection for larger jobs. var REDIRECT_PAGE_LIMIT = 50; var REDIRECT_PRINTER = "Copier - Color"; function printJobHook(inputs, actions) { /* * This print hook will need access to all job details * so return if full job analysis is not yet complete. * The only job details that are available before analysis * are metadata such as username, printer name, and date. * * See reference documentation for full explanation. */ /* * NOTE: The high-volume printer must be compatible with the source printer. * i.e. use the same printer language like PCL or Postscript. * If this is a virtual queue, all printers in the queue must use * the same printer language. */ // Check if job analysis is completed (return if not) if (!inputs.job.isAnalysisComplete) { // No job details yet so return. // XXX: We should return some value that the client can // identify and know he have to call the method again on a // few seconds (when job analysis is complete). the client could // also check this condition before calling us. return false; } // If pages to print is less than ADM_3900_CHARGE_LIMIT, // just charge the job to the firm non-billable (ADM-3900) account. if (inputs.job.totalPages < ADM_3900_CHARGE_LIMIT) { // Charge to the firm non-bill account. actions.job.chargeToSharedAccount(ADM-3900); // Return with success. return true; } // At this point, we have to charge to personal account. actions.job.chargeToPersonalAccount(); // Finally, check if we have to redirect to a more efficient or // faster high volume printer. if (inputs.job.totalPages > REDIRECT_PAGE_LIMIT) { /* * Specify actions.job.bypassReleaseQueue() if you wish to bypass * the release queue on the original printer the job was sent to. * (Otherwise if held at the target, the job will need to be released * from two different queues before it will print.) */ actions.job.bypassReleaseQueue(); /* * Job is larger than our page limit, so redirect to high-volume printer, * and send a message to the user. * Specify "allowHoldAtTarget":true to allow the job to be held at the * hold/release queue for the high-volume printer, if one is defined. */ actions.job.redirect(REDIRECT_PRINTER, {allowHoldAtTarget: true}); // Notify the user that the job was automatically redirected. actions.client.sendMessage( "The print job was over " + REDIRECT_PAGE_LIMIT + " pages and" + " was sent to printer: " + REDIRECT_PRINTER + "." ); // Record that the job was redirected in the application log. actions.log.info( "Large job redirected from printer '" + inputs.job.printerName + "' to printer '" + REDIRECT_PRINTER + "'." ); } // Return with success. return true; }
Answers 4
Code from me, to reach specified Goal:
/* * Redirect large jobs without confirmation * * Users printing jobs larger than the defined number of pages have their jobs * automatically redirected to another printer or virtual queue. * This can be used to redirect large jobs from slower or high cost printers * to more efficient or faster high volume printers. */ function printJobHook(inputs, actions) { /* * This print hook will need access to all job details * so return if full job analysis is not yet complete. * The only job details that are available before analysis * are metadata such as username, printer name, and date. * * See reference documentation for full explanation. */ if (!inputs.job.isAnalysisComplete) { // No job details yet so return. return; } /* * NOTE: The high-volume printer must be compatible with the source printer. * i.e. use the same printer language like PCL or Postscript. * If this is a virtual queue, all printers in the queue must use * the same printer language. */ if (inputs.job.totalPages < 10) { // Below 10 - charge to the firm non-bill account actions.job.chargeToSharedAccount("ADM-3900"); } else{ //job is 10+ pages - do the cost center popup actions.job.chargeToPersonalAccount(); // Account Selection will still show var LIMIT = 50; // Redirect jobs over 50+ pages. var HIGH_VOL_PRINTER = "Copier - Color"; if (inputs.job.totalPages > LIMIT) { //The job is 50+ pages /* * Specify actions.job.bypassReleaseQueue() if you wish to bypass the release queue * on the original printer the job was sent to. (Otherwise if held at the target, * the job will need to be released from two different queues before it will print.) */ actions.job.bypassReleaseQueue(); /* * Job is larger than our page limit, so redirect to high-volume printer, * and send a message to the user. * Specify "allowHoldAtTarget":true to allow the job to be held at the hold/release * queue for the high-volume printer, if one is defined. */ actions.job.redirect(HIGH_VOL_PRINTER, {allowHoldAtTarget: true}); // Notify the user that the job was automatically redirected. actions.client.sendMessage( "The print job was over " + LIMIT + " pages and was sent to " + " printer: " + HIGH_VOL_PRINTER + "."); // Record that the job was redirected in the application log. actions.log.info("Large job redirected from printer '" + inputs.job.printerName + "' to printer '" + HIGH_VOL_PRINTER + "'."); } } }
0 comments:
Post a Comment