Validate Discount amount with Unit price in Quote line items

Hi,

I am using sugar 7.9 version. I am trying to validate the quote line item based on the below criteria.

If Discount amount is greater than Unit price then I need to show a error message like `Total Discount amount should be less than or equal to the Unit price`.

I tried to achieve this using

'validation' => array(
'type' => 'range',
'min' => '0',
'max' => '$discount_price,
),

It's not working as expected. 

But both the fields are in currency datatype. So I am not sure How to achieve this validation.

Thanks in advance

Parents
  • You are missing a quote after $discount_price but I'm not sure that will address your problem.

    I'm not sure you can use variables like we do in sugar logic.

    All the examples I have seen of range have hardcoded numbers.

    The validations that use another field are all dates and have a compareto parameter to get the field value, for example in Meetings the date_start has the following:

          'validation' =>

          array (

            'type' => 'isbefore',

            'compareto' => 'date_end',

            'blank' => false,

          ),

    Perhaps you could put a validation on the total_amount field to check that it's greater than -1, which is greater than or equal to zero (if the discount is higher than the unit price you'll get a negative total_amount).

          'validation' =>

          array (

            'type' => 'range',

            'greaterthan' => -1,

          ),

    HTH

    FrancescaS

  • You will want to call the parent onSaveRowEdit if you want the row to be saved, and trigger the parent cancel if you don't want to save it.

    I've not tried this on quotes, so I don't have the code for you. Look at the original modules/ProductBundles/clients/base/views/quote-data-group-list/quote-data-group-list.js

    to see how they handle things there.

    HTH

    FrancescaS

  • Hi FrancescaS,

    Thanks for your replies.

    I cant found any files like custom/modules/ProductBundles/clients/base/views/quote-data-group-list/quote-data-group-list.js. 

    So I have copied the file from src/modules/ProductBundles/clients/base/views/quote-data-group-list/quote-data-group-list.js  and paste it in the below path

    custom/modules/ProductBundles/clients/base/views/quote-data-group-list/

    And I am trying to validate the unit price and discount price like below

    onSavingRow: function(disableCancelBtn, rowModelCid) {

          _.each(self.layout.collection.models, function(lineItem) {

              if(lineItem.cid === rowModelCid) {

                 if(parseInt(lineItem.attributes.discount_amount) > parseInt(lineItem.attributes.discount_price)) {

                      app.alert.show('closed_confirmation', {
                      level: 'error',
                       messages: app.lang.get('LBL_DISCOUNT_ERROR', 'Quotes'),
                      autoClose: true
                })

                return false;
           }

       }

    }

    I am getting the error message as expected. But after the error message the line item will automatically saved.  I cant control it from save.

    Kindly help me.

    Thanks

  • By copying the whole file you are overriding the entire controller, you should extend it, not override it.

    Your code will look something like this (note that this is NOT tested, I'm just giving you ideas here):

    custom/modules/ProductBundles/clients/base/views/quote-data-group-list/quote-data-group-list.js

    ({
      extendsFrom: 'ProductBundlesQuoteDataGroupListView',
      initialize: function(options){
        this._super('initialize' ,[options]);
      },
      onSaveRowEdit: function(rowModel) {
        this.checkDiscount(rowModel);
      },
      checkDiscount: function(rowModel){
        //note that the rowModel is the model for the row you are on,
        //and for which you just clicked Save
        //you don't need to loop through the whole collection
        if(rowModel.get('discount_amount') > rowModel.get('discount_price')){
          app.alert.show('discount_error', {
            level: 'error',
            messages: app.lang.get('LBL_DISCOUNT_ERROR', 'Quotes'),
            autoClose: true
          });
          //cancel the save
          this.onCancelRowEdit();
        }else{
          //continue to save
          this._super('onSaveRowEdit', [rowModel]);
        }
      },
      _dispose: function() {
        this._super('_dispose');
      },
    })

    HTH

    FrancescaS

  • Thanks for your time,

    I am getting the below error while implementing the above code

    Console error

    Save me.

    Thanks

  • Sorry, my fault, if you look at

    modules/ProductBundles/clients/base/views/quote-data-group-list/quote-data-group-list.js

    you will see that the onCancelRowEdit needs a parameter.

    Just change

    this.onCancelRowEdit();

    to

    this.onCancelRowEdit(rowModel);
  • Thanks FrancescaS,

    I have passed the rowModel to onCancelRowEdit() method

    this.onCancelRowEdit(rowModel);

    Now I didn't get any console error. But the left side edit icon will be disabled and still the record is saved.

    See the attachment

    Left side edit icon disabled

    I try to debug it. But I am unable to. Any thought about this.

    Thanks in advance

  • Hi Vijayakumar S, 

    I'm just wondering if you had any luck resolving this problem. I seem to have a similar issue, I'm trying to apply some custom validation before my Quoted Line Item is saved. However the QLI row seems to be saved no matter what, even if my code does not even run default saving function. i.e. 

    Hi Francesca Shiekh, have you come across this problem since this post? Thanks. 

    My code is :

    ({
      extendsFrom: 'ProductBundlesQuoteDataGroupListView',
      initialize: function(options){
        this._super('initialize' ,[options]);
      },
      onSaveRowEdit: function(rowModel) {
        this.checkQuantity(rowModel);

      },

      checkQuantity: function(rowModel){

        app.alert.show('message-id', {
          level: 'confirmation',
          messages: 'Do you want to save the Row?',
          autoClose: false,
          onConfirm: function(){
            //continue to save
            this._super('onSaveRowEdit', [rowModel]);
          },
          onCancel: function(){
            //cancel the save
            this.onCancelRowEdit(rowModel);
          }
        });
      },

      _dispose: function() {
        this._super('_dispose');
      },
    })
  • I have not but I'm not using it in this way.

    I would recommend going through the original code at modules/ProductBundles/clients/base/views/quote-data-group-list/quote-data-group-list.js and seeing what you might be able to deduce.

    Sorry I'm not more help,

    FrancescaS

Reply Children
No Data