SUITE LET PAGE TO REPLICATE STANDARD PRINT CHECK

This is the script to replicate the standard print check page design this suite let page take the batch number from the bill payments as filter

/**
 * @NApiVersion 2.1
 * @NScriptType Suitelet
 */
define(['N/ui/serverWidget', 'N/search', 'N/record', 'N/render', 'N/file'], function(serverWidget, search, record, render, file) {

    function onRequest(context) {
        if (context.request.method === 'GET') {
            // Create the form
            let form = serverWidget.createForm({
                title: 'Print Checks'
            });

            // Add Batch No field
            let batchNoField = form.addField({
                id: 'custpage_batch_no',
                type: serverWidget.FieldType.TEXT,
                label: 'Batch No'
            });

            // Add Date field
            form.addField({
                id: 'custpage_date',
                type: serverWidget.FieldType.DATE,
                label: 'Date'
            });

            // Add Check Type field
            form.addField({
                id: 'custpage_check_type',
                type: serverWidget.FieldType.TEXT,
                label: 'Check Type'
            }).defaultValue = 'Standard';

            // Add First Check Number field
            let firstCheckNumberField = form.addField({
                id: 'custpage_first_check_number',
                type: serverWidget.FieldType.TEXT,
                label: 'First Check Number'
            }).isMandatory = true;

            // Add Last Check Number field
            let lastCheckField = form.addField({
                id: 'custpage_last_check',
                type: serverWidget.FieldType.TEXT,
                label: 'Last Check Number'
            });
            lastCheckField.updateDisplayType({
                displayType: serverWidget.FieldDisplayType.DISABLED
            });

            // Add Back to Front checkbox field
            form.addField({
                id: 'custpage_backtofront',
                type: serverWidget.FieldType.CHECKBOX,
                label: 'Back to Front'
            });

            // Add standard buttons
            form.addSubmitButton({
                label: 'Print'
            });
            form.addButton({
                id: 'markall',
                label: 'Mark All',
                functionName: 'markAll'
            });
            form.addButton({
                id: 'unmarkall',
                label: 'Unmark All',
                functionName: 'unmarkAll'
            });
            form.addResetButton({
                label: 'Cancel'
            });

            // Add client script for mark/unmark all functionality
            form.clientScriptModulePath = '/SuiteScripts/jj_cs_printsuitelet_advaun_345.js'; // Use the correct path to the client script

            // Add sublist for bill payments
            let sublist = form.addSublist({
                id: 'custpage_sublist',
                type: serverWidget.SublistType.LIST,
                label: 'Bills'
            });

            sublist.addField({
                id: 'custpage_print',
                type: serverWidget.FieldType.CHECKBOX,
                label: 'Print'
            });
            sublist.addField({
                id: 'custpage_date',
                type: serverWidget.FieldType.DATE,
                label: 'Date'
            });
            sublist.addField({
                id: 'custpage_transaction_number',
                type: serverWidget.FieldType.TEXT,
                label: 'Transaction Number'
            });
            sublist.addField({
                id: 'custpage_payee',
                type: serverWidget.FieldType.TEXT,
                label: 'Payee'
            });
            sublist.addField({
                id: 'custpage_type',
                type: serverWidget.FieldType.TEXT,
                label: 'Type'
            });
            sublist.addField({
                id: 'custpage_currency',
                type: serverWidget.FieldType.TEXT,
                label: 'Currency'
            });
            sublist.addField({
                id: 'custpage_amount',
                type: serverWidget.FieldType.CURRENCY,
                label: 'Amount'
            });

            // Populate sublist if Batch No is provided
            if (context.request.parameters.custpage_batch_no) {
                let batchNo = context.request.parameters.custpage_batch_no;
                batchNoField.defaultValue = batchNo;

                // Search for bill payments with the given batch number
                let billPaymentSearch = search.create({
                    type: search.Type.VENDOR_PAYMENT,
                    filters: [
                        ["type","anyof","VendPymt"], 
                        "AND", 
                        ["tobeprinted","is","T"],
                        "AND", 
                        ['custbody_jj_batchnumber', 'is', batchNo]
                    ],
                    columns: [
                        'trandate',
                        'tranid',
                        'entity',
                        'type',
                        'currency',
                        'amount',
                        'account'
                    ]
                });

                let results = billPaymentSearch.run().getRange(0, 1000);
                let firstCheckNumber;

                // Populate sublist and get the first check number
                for (let i = 0; i < results.length; i++) {
                    let result = results[i];
                    sublist.setSublistValue({
                        id: 'custpage_date',
                        line: i,
                        value: result.getValue('trandate')
                    });
                    sublist.setSublistValue({
                        id: 'custpage_transaction_number',
                        line: i,
                        value: result.getValue('tranid')
                    });
                    sublist.setSublistValue({
                        id: 'custpage_payee',
                        line: i,
                        value: result.getText('entity')
                    });
                    sublist.setSublistValue({
                        id: 'custpage_type',
                        line: i,
                        value: result.getText('type')
                    });
                    sublist.setSublistValue({
                        id: 'custpage_currency',
                        line: i,
                        value: result.getText('currency')
                    });
                    sublist.setSublistValue({
                        id: 'custpage_amount',
                        line: i,
                        value: result.getValue('amount')
                    });

                    if (!firstCheckNumber) {
                        let accountId = result.getValue('account');
                        let accountRecord = record.load({
                            type: record.Type.ACCOUNT,
                            id: accountId
                        });
                        firstCheckNumber = accountRecord.getValue('curdocnum');
                    }
                }

                firstCheckNumberField.defaultValue = firstCheckNumber;
            }

            context.response.writePage(form);
        } else {
            // Handle POST request logic (printing and saving the data)
            let request = context.request;
            let batchNo = request.parameters.custpage_batch_no;
            let newDate = request.parameters.custpage_date;
            let checkNumber = parseInt(request.parameters.custpage_first_check_number);
            let isBackToFront = request.parameters.custpage_backtofront === 'T';

            // Fetch data using saved search
            let searchResult = search.create({
                type: search.Type.VENDOR_PAYMENT,
                filters: [
                    ['custbody_jj_batchnumber', 'is', batchNo]
                ],
                columns: [
                    'internalid', // Use internal ID for record updates
                    'trandate',
                    'tranid',
                    'entity',
                    'type',
                    'currency',
                    'amount'
                ]
            }).run();

            let results = searchResult.getRange(0, 1000);
            let checks = [];
            let totalChecks = results.length;

            if (isBackToFront) {
                checkNumber += totalChecks - 1; // Set check number to the highest number for back to front
            }

            for (let i = 0; i < totalChecks; i++) {
                let result = results[i];
                let paymentId = result.getValue('internalid'); // Get internal ID for record update
                
                // Update transaction date
                let billRecord = record.load({
                    type: record.Type.VENDOR_PAYMENT,
                    id: paymentId
                });
                billRecord.setValue({
                    fieldId: 'trandate',
                    value: new Date(newDate)
                });
                billRecord.save();

                // Store check details
                checks.push({
                    batchNumber: batchNo,
                    checkNumber: checkNumber,
                    billTransaction: paymentId
                });

                if (isBackToFront) {
                    checkNumber--; // Decrement check number for back to front
                } else {
                    checkNumber++; // Increment check number for front to back
                }
            }

            // Save the check details in a file
            let fileObj = file.create({
                name: 'check_details.json',
                fileType: file.Type.JSON,
                contents: JSON.stringify(checks),
                folder: -15 // SuiteScripts folder (use the internal ID for SuiteScripts folder)
            });
            fileObj.save();

            // Generate and render the print template
            let renderer = render.create();
            renderer.setTemplateByScriptId('CUSTTMPL_PRINT_CHECKS'); // Replace with your template script ID
            renderer.addSearchResults({
                templateName: 'bills',
                searchResult: results
            });

            let pdfFile = renderer.renderAsPdf();
            context.response.writeFile(pdfFile);
        }
    }

    return {
        onRequest: onRequest
    };

});

Leave a comment

Your email address will not be published. Required fields are marked *