How to make Quoted Line Items Negative based on a dropdown value in the Quote header?

I have an integration with my ERP where Returns/Credits are posted as negative dollar values.  Prior to Sugar 7.8 in the old BWC quotes module I was able to alter cost_price, discount_price and list_price of newly added Product catalog entries in an after save hook.  

Basically it was along the lines of:

If($bean->order_or_return_c == 'return')

{
    //Loop through my products
        // If list price > 0, multiply it by -1
        // If cost_price > 0, multiply it by -1
        // If discount_price > 0, multiply it by -1
        


}

How would I accomplish this in the new sidecar Quotes module?  When an item is added to the quote, if the dropdown = 'return', multiply all its currency fields by -1?

Now that QLI's are a seperate part of the view, I dont know how to cross the threshold with JS to affect one based on the other.  

  • Hmmm, I guess in theory this would work for Cost and List price as those are not editable on quote, but I am using disount_price as "Sale Price" which is editable on a quote and needs to be negative as well.  If I were to use a calculated field on that, it would render the field un-editable.  I cant trust the User to put a negative sign in front.  

    There's also the side effect of cascading saves which triggers calculations and slows performance which I'm trying to avoid.  In the past I've had calculated fields which worked fine until the database grew and then the amount of time it was taking to save an Account exceeded the PHP timeout and I had to jack it up super high to avoid server errors as it recalculated every single record related to the parent Account.  Like 45+ seconds to save certain Account records due to calculated fields.  I try to avoid them whenever possible.  

    I was thinking more along the lines of extending a JS controller ton handle this per record client side but there seem to be so many of these for line items on a quote.  I'm not sure which one to play with. 

    custom/modules/Products/clients/base/fields/quote-data-*   (There are several of these)

    custom/modules/ProductTemplates/clients/base/fields/quote-data-* (There are several of these as well)

  • I swear I've been working on this on and off for a month.  Kept coming back to it and finally figured it out.  It's actually VERY simple to do in sidecar.  Finding the correct controller (there's so many for QLI's..... WHY Sugar WHY?!?!) and a trigger field was the trick.   

    I placed my code in custom/modules/Products/clients/base/fields/quote-data-relate/quote-data-relate.js

    I then decided to trigger off a change in cost_price.  Every time a new item is added, the cost_price changes no matter what

    Here's the code to do it

    ({
    	extendsFrom: 'ProductsQuoteDataRelateField',
    
    	initialize: function(options){
    		this._super('initialize', [options]);
    		this.model.on('change:cost_price', this.checkRA,this); // detects if a new line is added
    	  },
    	  checkRA: function()
    	  {
    		  
    		quoteObject = this.context.parent.get('model');  // Get a reference to the parent Quote
    		quote_order_c = quoteObject.get('quote_order_c');  // Pull in the Quote header field which has a value of 'quote' 'order' 'credit'
    		
    		console.log('Quote Type = ' + quote_order_c);
    		
    		if(quote_order_c == 'credit')  // This is a Return/Credit
    		{
    			var cost_price = this.model.get('cost_price');
    			var discount_price = this.model.get('discount_price');
    			var list_price = this.model.get('list_price');
    			
    			//Check Cost, List and Sale price.  If any are positive, multiply by -1
    			if(cost_price > 0)
    			{
    				this.model.set('cost_price', cost_price * -1);
    			}
    			if(discount_price > 0)
    			{
    				this.model.set('discount_price', discount_price * -1);
    			}
    			if(list_price > 0)
    			{
    				this.model.set('list_price', list_price * -1);
    			}
    		}
    	  },
        
    
    });