Last week as I was working on customizing Quotes for a client, and I wanted to display the subtotal for each individual line item on the quote taking into account the quantity and discount. SugarLogic and Calculated Fields are the perfect tools for this use case. If you haven't looked into adding SugarLogic to your custom SugarCRM development tool belt, you should definitely take some time to learn more about it.
SugarLogic a great way to add simple logic and calculations to module fields without dealing with messy logic hook hacks. Best of all, SugarLogic is a key feature of the much anticipated SugarCRM 7 so you can rest assured your customizations will last.
Without further ado, on to the code... To help better communicate each line item on the Quote, I created a new field on the Products module called 'subtotal_c' of type 'Currency'. To enable SugarLogic and Calculated Fields, all you need to do is check the 'Calculated Value' checkbox and click the 'Edit Formula' button to start writing your logic. Here's the SugarLogic I came up with:
To break this down a bit, there are two primary things that need to be calculated: discount and quantity.
Products on Quotes can be discounted two ways, by a specific amount or by a percentage. To handle these two cases, I started things off with the ifElse function. $discount_select is a checkbox field which evaluates to true (checked) or false (not checked) in SugarLogic, so I used it as the boolean to check against. It basically says "If $discount_select is true, then the Product is using a percentage-based discount. If $discount_select is false, then the Product is using a flat-rate discount".
The middle part (lines 3-15) handles the calculation for the percentage based discount. It calculates the percentage amount from the $discount_amount, then subtracts that amount from the $discount_price. The last part (lines 16-22) handles the calculation for the flat-rate discount. It simply subtracts the $discount_amount from the $discount_price. Both cases then handle the quantity by multiplying the price less discount by the given quantity.
To use this field, all you need to do is add it to your views or Quote templates and it'll show the calculated value. And that's it!
In the above image, you can see how the right column displays the subtotal of the line item with respect to the discount and quantity.
There is one caveat. Due to how SugarLogic works, after adding the subtotal_c field, only newly create Quotes and Product line items will be given a calculated value. To get subtotal_c to show up for previously created items, you'll need to edit and save each product record so that the calculated field can be generated. You can do that with a simple script like this:
How have you used SugarLogic in the past? Have you ever made customizations like this in the past?
OneClick Sudo Login for SugarCRM 8 / 9
Sudo Login (or Masquerade) allows Administrators to log in as any regular user without asking for their password and without disconnecting them. Specially useful for configuring their workspace (filters, reports, dashboard) or to support users (reproduce issues, help them step by step, etc).
All In One CTI
All In One CTI is a computer telephony integration between SugarCRM and most popular PBXs.
Easy install. Stable work. 100% support
Scoring / Rating field module
It has never been easier to Score Your Data Add rating fields to any SugarCRM module right from within Studio. No coding required ! Choose the maximum number of stars to display, allow (or not) half stars and you're ready to go ! Works on on all major views including Detail, Edit, List, and Sear...
- Show more addons