How to give an automatic discount based on part number

Hi,

When creating a quote, I want to give an automatic discount of 100% per line item based on the part number.

For example, if part_number = x, I want to give a 100% discount for that line item.

How can I achieve this?

I understand that I need to modify line_item.js. Can somebody give me some indication which part of line_item.js I need to modify to achieve the above?

Cheers
GJ

I am not familiar with the js approach, but have you considered doing this from a PHP after_save logic_hook?

You would calculate the discount when the value is saved, and store it in a different field. This would be very simple to achieve but might have some disadvantages (in terms of user interaction) when compared to the Javascript approach.

I will stick with the js approach as I would like to see the discount on the UI before saving the record

Here’s what I have done:

  1. I copied line_items.js from /modules/AOS_Products_Quotes to /custom/modules/AOS_Products_Quotes. All my changes will be done on line_items.js in the /custom/modules/AOS_Products_Quotes directory.

  2. It looks to me that the function calculateLine(ln, key) in line_items.js is good candidate for inserting my code for validation on product_part_number.

  3. But, first of all, I wanted to see whether the code that I’ve writing in line_items.js is affecting the output on the UI.

  4. So I did the following small modification to line_items.js (in red):

if(document.getElementById(key + ‘product_list_price’ + ln) !== null && document.getElementById(key + ‘product_discount’ + ln) !== null && document.getElementById(key + ‘discount’ + ln) !== null){
var listPrice = get_value(key + ‘product_list_price’ + ln);
var discount = get_value(key + ‘product_discount’ + ln);
[color=#ff0000] // get the part_number
var part_number = get_value(key + ‘product_part_number’ + ln);
alert("Part number is: "+part_number);[/color]

  1. All other part of line_items.js is unchanged so far.

  2. I checked developer mode in Admin>System-Settings

  3. I did a Quick Repair & Rebuild and Repair JS files

  4. In Quotes>Edit view, when I select a part number; the line item is filled properly, but I can’t see the alert message: alert("Part number is: "+part_number)

What am I doing wrong?

Cheers
GJ

I managed to see the alert message by working on line_items.js from /modules/AOS_Products_Quotes and clearing browser cache.

Problem is that the part_number on the alert is showing zero whilst non-zero part number is displayed on UI.

Any clue?

Some parts of the SuiteCRM “custom” folder mechanism aren’t complete, so the first thing is to check whether it’s even looking for your custom file.

I found only this

https://github.com/salesagility/SuiteCRM/blob/master/modules/AOS_Products_Quotes/Line_Items.php#L36-L38

which seems to be for Edit view only. Are you on the edit view?

Then about the variable, I don’t know, check if you’re referencing correctly. You might have to check the mechanism for sending the data between the PHP world and JS world, I don’t know how it is moved around.

Using your browser’s JS debugger will help with both issues: see which code you’re getting on the browser, and see which variables you have there, and how to reference them.

1 Like

Success!

Modified line_items.js such that if part_number starts with PKG, then the line item is discounted either by 100% or by the List Price depending on whether the user has chosen discount dropdown as PCT(percentage) or AMT(amount).

Here’s the modification I did to line_items.js (in case that’s useful to somebody else).

function calculateLine(ln, key){

  var required = 'product_list_price';
  if(document.getElementById(key + required + ln) === null){
    required = 'product_unit_price';
  }

  if (document.getElementById(key + 'name' + ln).value === '' || document.getElementById(key + required + ln).value === ''){
    return;
  }

  if(key === "product_" && document.getElementById(key + 'product_qty' + ln) !== null && document.getElementById(key + 'product_qty' + ln).value === ''){
    document.getElementById(key + 'product_qty' + ln).value =1;
  }

  var productUnitPrice = unformat2Number(document.getElementById(key + 'product_unit_price' + ln).value);

/**
 *  Automatic discount based on part_number
 *  1. modified if() else() statement to validate on part_number not being null
 *  2. created part_number variable and use getElementById to assign part_number value
 *  3. modify if () else ()  by adding a condition to identiy part_number starting with PKG that should be discounted either 100% or by listPrice
 *
 *

  // 1
  if(document.getElementById(key + 'product_list_price' + ln) !== null && document.getElementById(key + 'product_discount' + ln) !== null && 
    document.getElementById(key + 'part_number' + ln) !== null &&
    document.getElementById(key + 'discount' + ln) !== null ){
    
    var listPrice = get_value(key + 'product_list_price' + ln);
    var discount = get_value(key + 'product_discount' + ln);

    // 2
   [color=#ff0000] var part_number = document.getElementById(key + 'part_number' + ln).value;[/color]
  
    
    var dis = document.getElementById(key + 'discount' + ln).value;

    if(dis == 'Amount')
    { // 3
      if((discount > listPrice) || (part_number.startsWith("PKG")))
      {
        document.getElementById(key + 'product_discount' + ln).value = listPrice;
        discount = listPrice;
      }
      productUnitPrice = listPrice - discount;
      document.getElementById(key + 'product_unit_price' + ln).value = format2Number(listPrice - discount);
    }
    else if(dis == 'Percentage')
    { // 3
      if((discount > 100) || (part_number.startsWith("PKG")))
      {
        document.getElementById(key + 'product_discount' + ln).value = 100;
        discount = 100;
      } 
.........

It’s not perfect and not a scalable solution as the part_number (PKG) that grants automatic discount is hardcoded. But, it is suffices my requirements for the time being.

Cheers
GJ

1 Like

I’m glad you got it working!

I added “code” tags to your reply. I had to remove the “color” tags though… but without code tags you lose square brackets, so if somebody want to copy-paste your code later,m it wouldn’t work… so it is better this way, I think.

Thanks for sharing your solution.

1 Like

Before the successful outcome, I was making a couple of mistakes:

  1. part_number should be referenced as part_number and not as product_part_number
  2. secondly, the function get_value() is for numeric. Hence, I used: var part_number = document.getElementById(key + ‘part_number’ + ln).value;

Rather than clearing cache everytime, I used Google Chrome’s Developer Tools|Network|Disabled cache (that’s how I found that I was using the wrong part_number reference). Modifying the line_item.js using VIM through SSH rather than cpanel file manager made it easier to test (I was modifying the line_item.js on VIM and accessing the UI through Google Chrome’s Developer Tools - there was no need to do Admin|Repair to see the modifications on line_item.js)

Thank you. :slight_smile: