Exchange rate calculation for Singapore subsidiary in transaction PDF.

Jira Code: PROF-59,PROF-60,PROF-

For the transaction, PDF print needs to convert the Quantity, Rate and Amount to Singapore exchange rates only for the Singapore subsidiary.

User Event

/**
 * @NApiVersion 2.x
 * @NScriptType UserEventScript
 * @NModuleScope SameAccount
 */
/*******************************************************************************
 * CLIENTNAME:PROFICIUM
 * PROF-60
 * Convert to SGD
 *************************************************************************
 * Date :19/03/2019
 *
 * Author:Jobin and Jismi IT services LLP
 * Script Description :Convert to SGD
 * Date created : 19/03/2019
 *
 * REVISION HISTORY
 *
 * Revision 1.0 ${19/03/2019} nd :marg created
 * 
 * 
 *
 ******************************************************************************/
define(['N/record', 'N/search', 'N/error', 'N/currency'],

    function(record, search, error, currency) {

        function beforeSubmit(scriptContext) {
            try {

               

                var record = scriptContext.newRecord;
                

                var customform = record.getValue({
                    fieldId: 'customform'
                });

                log.debug('customform', customform)

                if (customform == 177 || customform == 176 || customform == 175) {

                    var sub = record.getValue({
                        fieldId: 'subsidiary'
                    });

                    var trandate = record.getValue({
                        fieldId: 'trandate'
                    });
    log.debug('trandate ', trandate )
              
                    var exchangerate = currency.exchangeRate({
                        source: 'USD',
                        target: 'SGD',
                        date: new Date(trandate)
                    });
                    var tofixexchangerate = parseFloat(exchangerate).toFixed(3);
                 log.debug('exchangerate ', exchangerate )
  log.debug('tofixexchangerate ', tofixexchangerate )
                    //to get the value of sgd to usd exchange rate according to the created date

                    var exchangerate1 = currency.exchangeRate({
                        source: 'SGD',
                        target: 'USD',
                        date: new Date(trandate)
                    });
  log.debug('exchangerate1 ', exchangerate1 )
                    var tofixexchangerate1 = parseFloat(exchangerate1).toFixed(3);
                  
 log.debug('tofixexchangerate1 ', tofixexchangerate1 )


                    if (sub == 5) {


                        var rate_sgd;
                        var amount_sgd;



                        // set exchange rates to record
                     record.setValue({                       
                            fieldId: 'custbody_jj_usd_to_sgd',
                            value: tofixexchangerate
                           
                        });

                    

               
                   
           var sgdtousd =        record.setValue({
                            fieldId: 'custbody_jj_sgd_to_usd',
                            value: tofixexchangerate1
                           
                        });
log.debug('sgdtousd ', sgdtousd )



                        var numLines = record.getLineCount({
                            sublistId: 'item'
                        });

                  

                        for (var i = 0; i < numLines; i++) {
                            
                          

                            var quantity = record.getSublistValue({
                                sublistId: 'item',
                                fieldId: 'quantity',
                                  line: i

                            });
                            log.debug('quantity', quantity);
                            var rate = record.getSublistValue({
                                sublistId: 'item',
                                fieldId: 'rate',
                                  line: i

                            });
                           

                            rate_sgd = rate * exchangerate;

                            rate_sgd = parseFloat(rate_sgd).toFixed(2);

                            record.setSublistValue({
                                sublistId: 'item',
                                fieldId: 'custcol_jj_rate_sgd_new',
                                  line: i,
                                value: rate_sgd,
                                ignoreFieldChange: true
                            });



                            var amount = record.getSublistValue({
                                sublistId: 'item',
                                fieldId: 'amount',
                                  line: i
                            });
                           
                            amount_sgd = rate_sgd * quantity;

                          

                           var subtotal1 = record.setSublistValue({
                                sublistId: 'item',
                                fieldId: 'custcol_jj_prof_59_subtotal',
                                  line: i,
                                value: amount_sgd,
                                ignoreFieldChange: true
                            });

                        

                            record.setSublistValue({
                                sublistId: 'item',
                                fieldId: 'custcol_jj_amount_sgd_new',                          
                                  line: i,
                                value: amount_sgd,
                                ignoreFieldChange: true
                            });
                         
                          
                        }

                        //sum up the subtotal field 

                        var sumsubtotal = 0;
                        for (var i = 0; i < numLines; i++) {

                           


                            var subtamt = record.getSublistValue({
                                sublistId: 'item',
                                fieldId: 'custcol_jj_prof_59_subtotal',
                                  line: i
                        
                            });

                         


                            sumsubtotal = sumsubtotal + subtamt;


                        }




                        // convert total, subtotal, frieght, tax to sgd

                        var total = record.getValue({
                            fieldId: 'total'
                        });
                      
                        var total_sgd = total * exchangerate;

                        var subtotal = record.getValue({
                            fieldId: 'subtotal'
                        });

                        var subtotal_sgd = subtotal * exchangerate;


                        var tax = record.getValue({
                            fieldId: 'taxtotal'
                        });
                        var tax_sgd = tax * exchangerate;

                        var freight = record.getValue({
                            fieldId: 'shippingcost'
                        });




                        record.setValue({
                            fieldId: 'custbody_jj_subtotal_sgd',
                            value: sumsubtotal,
                            ignoreFieldChange: true
                        });



                        record.setValue({
                            fieldId: 'custbody_jj_taxtotal_sgd',
                            value: tax_sgd,
                            ignoreFieldChange: true
                        });

                        var totalamnt = sumsubtotal + tax_sgd;

                        record.setValue({
                            fieldId: 'custbody_jj_total_sgd',
                            value: totalamnt,
                            ignoreFieldChange: true
                        });


                        if ((freight != null) && (freight != undefined) && (freight != "")) {

                            var freight_sgd = freight * exchangerate;

                         

                        } else {
                            var freight_sgd = 0.00;
                        }
                        record.setValue({
                            fieldId: 'custbody_jj_freight_sgd',
                            value: freight_sgd,
                            ignoreFieldChange: true
                        });

                       
                    }
                }


} catch (e) {

                log.debug('error', e.message)


            }
        }
         


        return {

            beforeSubmit: beforeSubmit

        };

    });

Leave a comment

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