Customizing Line Item in SugarCRM in upgrade safe manner
Posted By: Doddy.Amijaya on October 25th, 2013 in SugarCRM
No Gravatar

I came across this request not too long a go and it did take a while for me to figure out. Now I’d like to share it with you guys hopefully this will save your hours of researching.
I used Aptana as my text editor because I need to reformat the minified Quotes.js

there are 6 files that you have to modify (7 if you would like to customize print to pdf feature as well):
1. Quotes.js (this is where you’d create the line items form)
2. EditView.js (This is to update the value)
3. editviews.php (this is where you would reference the EditViewFooter.tpl)
4. view.edit.php (this is to set the line items value)
5. EditViewFooter.tpl (this is where you reference the Quotes.js)
6. DetailViewFooter.tpl (this is the detail view of line items form)
7. sugarpdf.standard.php (this is to export to pdf)

Make sure you take a backup of the Quotes module first and then move all these files to the custom folder.

Lets start from the easy one.
open editviews.php and change this line of code

 'form' => array('footerTpl'=>'modules/Quotes/tpls/EditViewFooter.tpl') to 'form' => array('footerTpl'=>'custom/modules/Quotes/tpls/EditViewFooter.tpl') 

Next, open EditViewFooter.tpl and change this line of code

 src="{sugar_getjspath file='modules/Quotes/quotes.js'}" src="{sugar_getjspath file='modules/Quotes/EditView.js'}" to src="{sugar_getjspath file='custom/modules/Quotes/quotes.js'}" src="{sugar_getjspath file='custom/modules/Quotes/EditView.js'}" 

Now all the easy part is out of the way lets edit the Quote.js

This code is locaed on the top of Quotes.js, this is where you would define your new field. you can put your custom fields here or other stock fields that does not already appear in the quote form.
Now lets insert the fields that you just added to the field_list array that would contains all the fields.
this is inside the addRow function definition.

inside that field_list add your custom fields, the position of the parameter doesn’t matter by the way.

Now lets add the header of the custom fields. In this example I added weight field which is a stock field.
Go to the definition of addTableHeader function, and in there add this code :

 var tdEl = this.blankDataLabel.cloneNode(false); tdEl.width = 125; tdEl.appendChild(document.createTextNode("Case Pack")); rowEl.appendChild(tdEl); var tdEl = this.blankDataLabel.cloneNode(false); tdEl.width = 125; tdEl.appendChild(document.createTextNode("Case Weight")); rowEl.appendChild(tdEl); 

in this case where you put the code matter. For this example I want Case Weight header to appear after Case Pack.
Now we’re done with the header, next would be creating the textbox control and again you have to specify where you want the textbox to appear.

In this code snippet I added Weight text box right after Quantity text box.

item_list_MSI[itemName] = textEl;

item_list_MSI[itemName] = contain all the html tags to create the text box for all controls prior the Weight field.
cell3.appenChild(text) = this is how I would append weight text box. without this my weight text box would appear under the quantity text box
and the rest of that would be the definition of the control.

the following code is inside addTableTally, and this is called when you click add row button. essentially this is how you would initialize when you create a new row. the placement of the parameter matter so keep that in mind. please refer to the addRow function definition to see the list of the parameters.

 quotesManager.addRow("", "", "", "",'0', "", "", "", "", "","", "####", this.tableId, '', '', '', '', '', '0', '', '', ''); this.addRow = function(id, quantity, product_template_id, product_name,weight, discount_price, pricing_formula, pricing_formula_name, pricing_factor,tax_class, tax_class_name, mft_part_num, table_id, bundle_stage, bundle_name, bundle_shipping, product_description, type_id, discount_amount, discount_select, deal_calc, product_status) 

that’s the hardest part of all this, not that bad right ? so far, we have added the custom field to the array list and parameters, create the field header and the actual text box.
to assign the value we have to modify the EditView.js and view.edit.php and it’s really straight forward, you just have to be careful with the placement of the parameter for $add_Row array.
in EditView.js take a look how it’s done inside the set_product_return function which is the call back function when you adding a product.

for view.edit.php look for this code and add your custom field. don’t forget to match the placement as shown in the previous snippet code.

$add_row[] = "quotesManager.addRow('','$line_item->quantity','$line_item->product_template_id','$line_item->name', '$line_item->weight'"
								. ", '".format_number($line_item->discount_usdollar, $significantDigits, $significantDigits, array('convert' => true, 'currency_id' => $curid)) . "'"
							    .", '', '', '$line_item->pricing_factor', '$line_item->tax_class', '$tax_class_name',
								'$line_item->mft_part_num', 'group_$product_bundle->id', '$product_bundle->bundle_stage', '$product_bundle->name', '".format_money($product_bundle->shipping,FALSE)
							    ."', '".js_escape(br2nl($line_item->description))."', '"
							    . $line_item->type_id ."','"
							    . $line_item->discount_amount_usdollar."',".($line_item->discount_select?1:0)
							    . ",0, '". $line_item->status."');\n";

And that’s all for the edit screen. if you want your custom fields to appear in the detail view as well then you have to modify the DetailViewFooter.tpl.
it’s the same concept, you have to add the header (a caption for the text box) and the actual field.

 <td scope="row" width="15%" valign="top" style="text-align: center;">Case Weight</td> 

make sure you add the field inside this if statement

{if $line_item->object_name == "Product"}
<td valign="top" style="text-align: center;">{$line_item->weight}</td>

Don’t forget to repair and rebuild and THAT’S IT! you’re done.
For the sugarpdf it’s really straight forward, if you can figure out all this part then the sugarpdf would be really easy.

I hope this post is helpful.

2 Responses to “Customizing Line Item in SugarCRM in upgrade safe manner”

  1. KarlNo Gravatar says:

    Thanks a lot. This saved my time!

  2. RobNo Gravatar says:

    Is it possible to add a line item to quotes that is from a custom module and not from the product catalog in an upgrade safe way? i.e. the select box opens a custom module, not the product catalog.

Leave a Reply