Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- dealDetails = zoho.crm.getRecordById("Deals",dealId);
- type = dealDetails.get("Type");
- if(type == "Seller")
- {
- //gather all commission details
- deal_details = zoho.crm.getRecordById("Deals",dealId);
- contract_price = ifNull(deal_details.get("Contract_Price"),deal_details.get("Amount"));
- number_of_listing_agents = deal_details.get("Number_of_Listing_Agents");
- total_listing_commission = ifNull(deal_details.get("Total_Listing_Commissions"),0);
- listing_broker_split = ifNull(deal_details.get("Listing_Broker_Split"),0);
- primary_listing_agent_split = ifNull(deal_details.get("Primary_Listing_Agent_Split"),100);
- co_listing_agent_split = ifNull(deal_details.get("Co_Listing_Agent_Split"),0);
- //-------------------------------------------initial Calculations for reusable fees---------------------------
- //calculate listing agent concessions
- listing_agent_concessions = ifNull(deal_details.get("Listing_Agent_Concessions"),0);
- if(listing_agent_concessions.contains("$"))
- {
- //if it contains a $ just convert to number
- listing_agent_concessions_amount = listing_agent_concessions.remove("$").toDecimal();
- }
- if(listing_agent_concessions.contains("%"))
- {
- //if it contains a % then convert to number and calculate as a portion of the total contract price
- listing_agent_concessions_amount = listing_agent_concessions.remove("%").toDecimal() * contract_price / 100;
- }
- info "concessions: " + listing_agent_concessions_amount;
- if(listing_agent_concessions = 0)
- {
- //if concessions is returned null, just set amount to 0
- listing_agent_concessions_amount = 0;
- half_listing_agent_concession = 0;
- }
- else
- {
- //if not null then divide amount by 2, otherwise results in an error
- half_concession = listing_agent_concessions_amount / 2;
- }
- //calculate listing concessions
- if(total_listing_commission.contains("$"))
- {
- //if listing commission contains "$"" then remove "$" and convert to #
- total_listing_commission_amount = total_listing_commission.remove("$").toDecimal();
- }
- else
- {
- //if listing commission does not contains "$" then assume it is %
- total_listing_commission_amount = total_listing_commission.remove("%").toDecimal() * contract_price / 100;
- }
- info "listing commission total:" + total_listing_commission_amount;
- //calculate referral fees
- referral_fee = ifNull(deal_details.get("Referral_Fee_Terms"),"$0");
- referral_fee = referral_fee.removeAllAlpha().trim();
- if(referral_fee.contains("$"))
- {
- referral_fee_amount = referral_fee.remove("$").toDecimal();
- }
- if(referral_fee.contains("%"))
- {
- referral_fee_amount = total_listing_commission_amount * referral_fee.remove("%").toDecimal() / 100;
- }
- info "referral fee: " + referral_fee;
- //------------------------commission calculations-----------------------------------------------
- if(listing_broker_split.contains("$"))
- {
- //if listing broker is being paid flat $ amount
- listing_broker_amount = listing_broker_split.remove("$").toDecimal();
- info "listing broker amount: " + listing_broker_amount;
- agentPool = total_listing_commission_amount - listing_broker_amount - referral_fee_amount;
- if(primary_listing_agent_split.contains("$"))
- {
- //if broker is on flat $ primary agent is being paid flat $ amount
- primary_listing_agent_amount = primary_listing_agent_split.remove("$").toDecimal();
- co_listing_agent_amount = co_listing_agent_split.remove("$").toDecimal();
- }
- else
- {
- // broker is flat $ and agents are on %
- if(number_of_listing_agents == "1")
- {
- //if one agent, that agent just collects from pool - concessions
- primary_listing_agent_amount = agentPool - listing_agent_concessions_amount;
- co_listing_agent_amount = 0;
- }
- else
- {
- //if 2 agents on %, they collect their % from agent pool -1/2 concessions
- primary_listing_agent_amount = agentPool * primary_listing_agent_split.remove("%").toDecimal() / 100 - half_concession;
- co_listing_agent_amount = agentPool * co_listing_agent_split.remove("%").toDecimal() / 100 - half_concession;
- }
- }
- }
- else
- {
- //if broker is on % split
- listing_broker_amount = total_listing_commission_amount * listing_broker_split.remove("%").toDecimal() / 100;
- agentPool = total_listing_commission_amount - listing_broker_amount - referral_fee_amount;
- if(primary_listing_agent_split.contains("$"))
- {
- //if broker is on % but agents are on flat $ (for manual corrections)
- primary_listing_agent_amount = primary_listing_agent_split.remove("$").toDecimal();
- co_listing_agent_amount = co_listing_agent_split.remove("$").toDecimal();
- }
- else
- {
- //if broker on % split and agents on % split
- if(number_of_listing_agents == "1")
- {
- //if only 1 agent on % split with broker, agent just collects difference - concessions
- primary_listing_agent_amount = agentPool - listing_agent_concessions_amount;
- co_listing_agent_amount = 0;
- }
- else
- {
- //if 2 agents are on % split with broker
- primary_listing_agent_amount = agentPool * primary_listing_agent_split.remove("%").toDecimal() / 100 - half_concession;
- co_listing_agent_amount = agentPool * co_listing_agent_split.remove("%").toDecimal() / 100 - half_concession;
- }
- }
- }
- info "-----------------Listing Commissions---------------";
- total_listing_commission_amount = total_listing_commission_amount.round(2);
- info "total listing commission: " + total_listing_commission_amount;
- listing_broker_amount = listing_broker_amount.round(2);
- info "listing broker amount:" + listing_broker_amount;
- info "referral fee: " + referral_fee_amount;
- info "pool left for agents: " + agentPool;
- listing_agent_concessions_amount = listing_agent_concessions_amount.round(2);
- info "listing agent concession amount: " + listing_agent_concessions_amount;
- primary_listing_agent_amount = primary_listing_agent_amount.round(2);
- info "primary listing agent amount: " + primary_listing_agent_amount;
- co_listing_agent_amount = co_listing_agent_amount.round(2);
- info "co-listing agent amount: " + co_listing_agent_amount;
- //run check on commissions
- total = primary_listing_agent_amount + co_listing_agent_amount + listing_broker_amount + referral_fee_amount + listing_agent_concessions_amount;
- difference = total_listing_commission_amount - total;
- info "difference:" + difference;
- if(!total = total_listing_commission_amount || difference.abs() <= 0.01)
- {
- if(total != total_listing_commission_amount)
- {
- primary_listing_agent_amount = primary_listing_agent_amount + difference;
- }
- info "new primary amount: " + primary_listing_agent_amount;
- update_map = Map();
- update_map.put("Primary_Listing_Agent_Commission_fx",primary_listing_agent_amount);
- update_map.put("Co_Listing_Agent_Commission_fx",co_listing_agent_amount);
- update_map.put("Listing_Broker_Commission_fx",listing_broker_amount);
- update_map.put("Referring_Agent_Amount_fx",referral_fee_amount);
- update_map.put("Total_Commissions_new_fx",total_listing_commission_amount);
- updateRecord = zoho.crm.updateRecord("Deals",dealId,update_map);
- info updateRecord;
- }
- }
- if(type == "Buyer")
- {
- //gather all necessary fields
- dealDetails = zoho.crm.getRecordById("Deals",dealId);
- contractPrice = ifNull(dealDetails.get("Contract_Price"),dealDetails.get("Amount"));
- info "contract price: " + contractPrice;
- numberOfAgents = dealDetails.get("Number_of_Buyer_Agents");
- totalCommission = ifNull(dealDetails.get("Buyer_Agent_Commission_Offered"),0);
- info "total commission: " + totalCommission;
- brokerSplit = ifNull(dealDetails.get("Buyer_Broker_Split"),0);
- primaryAgentSplit = ifNull(dealDetails.get("Primary_Buyer_s_Agent_Split"),100);
- coAgentSplit = ifNull(dealDetails.get("Co_Buyer_s_Agent_Split"),0);
- //-----------------------calculate some initital values----------------
- //calculate total commission amount
- if(totalCommission.contains("$"))
- {
- //if BAC is flat $ amount, just convert to a number
- totalCommissionAmount = totalCommission.remove("$").toDecimal();
- }
- else
- {
- //if it is a %, calc as total of contract price
- totalCommissionAmount = totalCommission.remove("%").toDecimal() * contractPrice / 100;
- }
- info "total BAC: " + totalCommissionAmount;
- //buyer agent concessions
- agentConcessions = ifNull(dealDetails.get("Buyer_Agent_Concessions"),0);
- if(agentConcessions.contains("$"))
- {
- //if buyer agent concessions is $ convert to #
- concessionAmount = agentConcessions.remove("$").toDecimal();
- }
- else
- {
- //if contains % calculate as a % of contract price
- concessionAmount = agentConcessions.remove("%").toDecimal() * contractPrice / 100;
- }
- if(agentConcessions = 0)
- {
- //if concessions is 0 set amounts and half amount to 0
- concessionAmount = 0;
- halfConcessionAmount = 0;
- }
- else
- {
- //if agent concession is not 0 set half amount to concession/2
- halfConcessionAmount = concessionAmount / 2;
- }
- info "buyer concessions: " + concessionAmount;
- info "half concession: " + halfConcessionAmount;
- //calculate referral fees
- referralFee = ifNull(dealDetails.get("Referral_Fee_Terms"),"$0");
- referralFee = referralFee.removeAllAlpha().trim();
- if(referralFee.contains("$"))
- {
- //if referral fee is $ convert to number
- referralFeeAmount = referralFee.remove("$").toNumber();
- }
- if(referralFee.contains("%"))
- {
- //if referral fee is a % cal as % of total commissions
- referralFeeAmount = referralFee.remove("%").toNumber() * totalCommissionAmount / 100;
- }
- info "referral fee amount:" + referralFeeAmount;
- //------------------------------------Commission Calculations-----------------------
- if(brokerSplit.contains("$"))
- {
- //if broker is paid flat $ amount
- brokerAmount = brokerSplit.remove("$").toNumber();
- agentPool = totalCommissionAmount - brokerAmount - referralFeeAmount;
- agentPool = agentPool.round(2);
- if(primaryAgentSplit.contains("$"))
- {
- //broker is flat $ and agents are paid flat $ amounts
- primaryAgentAmount = primaryAgentSplit.remove("$").toDecimal();
- coAgentAmount = coAgentSplit.remove("$").toDecimal();
- }
- else
- {
- //if broker is flat $ and agents are on %
- if(numberOfAgents == "1")
- {
- //broker is falt $ and 1 agent collects the agent pool - concessions
- primaryAgentAmount = agentPool - concessionAmount;
- coAgentAmount = 0;
- }
- else
- {
- //broker is flat $ and agents collect their % portion and split concessions
- primaryAgentAmount = primaryAgentSplit.remove("%").toDecimal() / 100 * agentPool - halfConcessionAmount;
- coAgentAmount = coAgentSplit.remove("%").toDecimal() / 100 * agentPool - halfConcessionAmount;
- }
- }
- }
- else
- {
- //broker is on a % split
- brokerAmount = brokerSplit.remove("%").toNumber() / 100 * totalCommissionAmount;
- agentPool = totalCommissionAmount - brokerAmount - referralFeeAmount;
- info "agent pool: " + agentPool;
- if(primaryAgentSplit.contains("$"))
- {
- //broker on % split and agents on $ amount (for manual overrides)
- primaryAgentAmount = primaryAgentSplit.remove("$").toDecimal();
- coAgentAmount = coAgentSplit.remove("$").toDecimal();
- }
- else
- {
- //broker on % split and agents on % split
- if(numberOfAgents == "1")
- {
- //if one agent, he collects agent pool - concessions
- primaryAgentAmount = agentPool - concessionAmount;
- coAgentAmount = 0;
- }
- else
- {
- //if broker is on % and there are 2 agents on %, they collect their % from pool - 1/2 concessions
- primaryAgentAmount = primaryAgentSplit.remove("%").toDecimal() / 100 * agentPool - halfConcessionAmount;
- coAgentAmount = coAgentSplit.remove("%").toDecimal() / 100 * agentPool - halfConcessionAmount;
- }
- }
- }
- info "----------- CalculatedCommissions-------------";
- totalCommissionAmount = totalCommissionAmount.round(2);
- info "total commissions: " + totalCommissionAmount;
- primaryAgentAmount = primaryAgentAmount.round(2);
- info "primary Agent Commission: " + primaryAgentAmount;
- coAgentAmount = coAgentAmount.round(2);
- info "co agent amount: " + coAgentAmount;
- brokerAmount = brokerAmount.round(2);
- info "broker cut: " + brokerAmount;
- concessionAmount = concessionAmount.round(2);
- info "concessions: " + concessionAmount;
- referralFeeAmount = referralFeeAmount.round(2);
- info "referral fee: " + referralFeeAmount;
- //check total = correct
- difference = 0;
- addTotal = primaryAgentAmount + coAgentAmount + brokerAmount + concessionAmount + referralFeeAmount;
- if(!addTotal = totalCommissionAmount)
- {
- difference = totalCommissionAmount - addTotal;
- info "difference: " + difference;
- }
- if(difference.abs() <= 0.01)
- {
- primaryAgentAmount = primaryAgentAmount + difference;
- updateMap = Map();
- updateMap.put("Primary_Buyer_Agent_Commission_fx",primaryAgentAmount);
- updateMap.put("Co_Buyer_Agent_Commission_fx",coAgentAmount);
- updateMap.put("Buyer_Broker_Commission_fx",brokerAmount);
- updateMap.put("Total_Commissions_new_fx",totalCommissionAmount);
- updateMap.put("Referring_Agent_Amount_fx",referralFeeAmount);
- updateRecord = zoho.crm.updateRecord("Deals",dealId,updateMap);
- info updateRecord;
- }
- }
Add Comment
Please, Sign In to add comment