I have a visualforce page that we use for product selection on our opportunities. I've updated it recently with a couple of Jquery functions that allow users to edit the sales price and auto-calculate the discount and vice versa. It works great if there is only one product added, but once I add more than a single product the calculations goes wonky -- I assume because the code does not knwo which row to update. Can anyone help me sort it out?
Screen Shots:
<script type='text/javascript'>
function calculateDiscount(){
var discount= 100*($('[id$=dp]').text() - $('[id$=sp]').val())/$('[id$=dp]').text();
var discountRounded = discount.toFixed(1);
$('[id$=disc]').val(discountRounded);
}
function calculateSalesPrice(){
var salesPrice= $('[id$=dp]').text() - ($('[id$=dp]').text() * $('[id$=disc]').val())/100;
var salesPriceRounded = salesPrice.toFixed(2);
$('[id$=sp]').val(salesPriceRounded);
}
</script>
<apex:form >
<apex:pageBlock title="Selected {!$ObjectType.Product2.LabelPlural}" id="selected">
<apex:pageblockTable value="{!shoppingCart}" var="s">
<apex:column >
<apex:commandLink value="Remove" action="{!removeFromShoppingCart}" reRender="selected,searchResults" immediate="true">
<!-- this param is how we send an argument to the controller, so it knows which row we clicked 'remove' on -->
<apex:param value="{!s.PriceBookEntryId}" assignTo="{!toUnselect}" name="toUnselect"/>
</apex:commandLink>
</apex:column>
<!--********** Product Name **********-->
<apex:column headerValue="{!$ObjectType.Product2.LabelPlural}" value="{!s.PriceBookEntry.Product2.Name}"/>
<!--********** Product SKU **********-->
<apex:column headerValue="{!$ObjectType.OpportunityLineItem.Fields.SKU__c.Label}" value="{!s.PriceBookEntry.Product2.SKU__c}"/>
<!--********** Annual Product Term **********-->
<apex:column headerValue="Annual Term" id="pt" rendered="false" value="{!s.PriceBookEntry.Product2.Product_Term_Years__c}"/>
<!--********** Quantity **********-->
<apex:column headerValue="{!$ObjectType.OpportunityLineItem.Fields.Quantity.Label}">
<apex:inputField value="{!s.Quantity}" id="quant" style="width:70px" required="true">
<apex:actionSupport event="onchange" reRender="tot"/>
</apex:inputField>
</apex:column>
<!--********** Service Term in Months **********-->
<apex:column headerValue="{!$ObjectType.OpportunityLineItem.Fields.Service_Term_in_Months__c.Label}">
<apex:inputField value="{!s.Service_Term_in_Months__c}" id="svc" style="width:70px" required="true">
<apex:actionSupport event="onchange" reRender="tot"/>
</apex:inputField>
</apex:column>
<!--********** Discount (Manual) **********-->
<apex:column headerValue="{!$ObjectType.OpportunityLineItem.Fields.Discount_off_list_manual__c.Label}">
<apex:inputField value="{!s.Discount_off_list_manual__c}" id="disc" style="width:70px" required="True" onkeyup="calculateSalesPrice();">
<apex:actionSupport event="onkeyup" reRender="spproxy"/>
<!--<apex:actionSupport event="onkeyup" action="{!calculateSalesPrice}"/>-->
</apex:inputField>
</apex:column>
<!--********** Sales Price **********-->
<apex:column headerValue="{!$ObjectType.OpportunityLineItem.Fields.UnitPrice.Label}">
<apex:inputField value="{!s.UnitPrice}" id="sp" style="width:70px" required="true" onkeyup="calculateDiscount();">
<apex:actionSupport event="onchange" reRender="tot"/>
</apex:inputField>
</apex:column>
<!--********** Disti Transfer Price (Standard) Hidden **********-->
<apex:column headerValue="{!$ObjectType.OpportunityLineItem.Fields.Disti_Transfer_Price__c.Label}" rendered="True">
<apex:outputText id="dp" value="{!s.PriceBookEntry.Disti_Transfer_Price__c}"/>
</apex:column>
<!--********** List Price (Standard) Hidden **********-->
<apex:column headerValue="{!$ObjectType.OpportunityLineItem.Fields.ListPrice.Label}" id="lp" value="{!s.PriceBookEntry.UnitPrice}" rendered="True"/>
<!--********** Total Price (display only field) **********-->
<apex:column headerValue="Total Price">
<apex:outputText id="tot" value="${0, number, ###,###,###,##0.00}">
<apex:param value="{!IF(s.PriceBookEntry.Product2.Product_Term_Years__c != null,(s.Quantity * s.Service_Term_in_Months__c * s.UnitPrice)/(s.PriceBookEntry.Product2.Product_Term_Years__c*12),(s.Quantity * s.UnitPrice)) }"/>
</apex:outputText>
</apex:column>
</apex:pageblockTable>
<apex:pageBlockButtons >
<apex:commandButton action="{!onSave}" value="Save"/>
<apex:commandButton action="{!onCancel}" value="Cancel" immediate="true"/>
</apex:pageBlockButtons>
</apex:pageBlock>