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?
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 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.
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.
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.
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]
All other part of line_items.js is unchanged so far.
I checked developer mode in Admin>System-Settings
I did a Quick Repair & Rebuild and Repair JS files
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)
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.
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.
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.
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.
Before the successful outcome, I was making a couple of mistakes:
part_number should be referenced as part_number and not as product_part_number
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)