patrickjust3k

Calculating commissions

Apr 19th, 2023
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.97 KB | None | 0 0
  1. dealDetails = zoho.crm.getRecordById("Deals",dealId);
  2. type = dealDetails.get("Type");
  3. if(type == "Seller")
  4. {
  5. //gather all commission details
  6. deal_details = zoho.crm.getRecordById("Deals",dealId);
  7. contract_price = ifNull(deal_details.get("Contract_Price"),deal_details.get("Amount"));
  8. number_of_listing_agents = deal_details.get("Number_of_Listing_Agents");
  9. total_listing_commission = ifNull(deal_details.get("Total_Listing_Commissions"),0);
  10. listing_broker_split = ifNull(deal_details.get("Listing_Broker_Split"),0);
  11. primary_listing_agent_split = ifNull(deal_details.get("Primary_Listing_Agent_Split"),100);
  12. co_listing_agent_split = ifNull(deal_details.get("Co_Listing_Agent_Split"),0);
  13. //-------------------------------------------initial Calculations for reusable fees---------------------------
  14. //calculate listing agent concessions
  15. listing_agent_concessions = ifNull(deal_details.get("Listing_Agent_Concessions"),0);
  16. if(listing_agent_concessions.contains("$"))
  17. {
  18. //if it contains a $ just convert to number
  19. listing_agent_concessions_amount = listing_agent_concessions.remove("$").toDecimal();
  20. }
  21. if(listing_agent_concessions.contains("%"))
  22. {
  23. //if it contains a % then convert to number and calculate as a portion of the total contract price
  24. listing_agent_concessions_amount = listing_agent_concessions.remove("%").toDecimal() * contract_price / 100;
  25. }
  26. info "concessions: " + listing_agent_concessions_amount;
  27. if(listing_agent_concessions = 0)
  28. {
  29. //if concessions is returned null, just set amount to 0
  30. listing_agent_concessions_amount = 0;
  31. half_listing_agent_concession = 0;
  32. }
  33. else
  34. {
  35. //if not null then divide amount by 2, otherwise results in an error
  36. half_concession = listing_agent_concessions_amount / 2;
  37. }
  38. //calculate listing concessions
  39. if(total_listing_commission.contains("$"))
  40. {
  41. //if listing commission contains "$"" then remove "$" and convert to #
  42. total_listing_commission_amount = total_listing_commission.remove("$").toDecimal();
  43. }
  44. else
  45. {
  46. //if listing commission does not contains "$" then assume it is %
  47. total_listing_commission_amount = total_listing_commission.remove("%").toDecimal() * contract_price / 100;
  48. }
  49. info "listing commission total:" + total_listing_commission_amount;
  50. //calculate referral fees
  51. referral_fee = ifNull(deal_details.get("Referral_Fee_Terms"),"$0");
  52. referral_fee = referral_fee.removeAllAlpha().trim();
  53. if(referral_fee.contains("$"))
  54. {
  55. referral_fee_amount = referral_fee.remove("$").toDecimal();
  56. }
  57. if(referral_fee.contains("%"))
  58. {
  59. referral_fee_amount = total_listing_commission_amount * referral_fee.remove("%").toDecimal() / 100;
  60. }
  61. info "referral fee: " + referral_fee;
  62. //------------------------commission calculations-----------------------------------------------
  63. if(listing_broker_split.contains("$"))
  64. {
  65. //if listing broker is being paid flat $ amount
  66. listing_broker_amount = listing_broker_split.remove("$").toDecimal();
  67. info "listing broker amount: " + listing_broker_amount;
  68. agentPool = total_listing_commission_amount - listing_broker_amount - referral_fee_amount;
  69. if(primary_listing_agent_split.contains("$"))
  70. {
  71. //if broker is on flat $ primary agent is being paid flat $ amount
  72. primary_listing_agent_amount = primary_listing_agent_split.remove("$").toDecimal();
  73. co_listing_agent_amount = co_listing_agent_split.remove("$").toDecimal();
  74. }
  75. else
  76. {
  77. // broker is flat $ and agents are on %
  78. if(number_of_listing_agents == "1")
  79. {
  80. //if one agent, that agent just collects from pool - concessions
  81. primary_listing_agent_amount = agentPool - listing_agent_concessions_amount;
  82. co_listing_agent_amount = 0;
  83. }
  84. else
  85. {
  86. //if 2 agents on %, they collect their % from agent pool -1/2 concessions
  87. primary_listing_agent_amount = agentPool * primary_listing_agent_split.remove("%").toDecimal() / 100 - half_concession;
  88. co_listing_agent_amount = agentPool * co_listing_agent_split.remove("%").toDecimal() / 100 - half_concession;
  89. }
  90. }
  91. }
  92. else
  93. {
  94. //if broker is on % split
  95. listing_broker_amount = total_listing_commission_amount * listing_broker_split.remove("%").toDecimal() / 100;
  96. agentPool = total_listing_commission_amount - listing_broker_amount - referral_fee_amount;
  97. if(primary_listing_agent_split.contains("$"))
  98. {
  99. //if broker is on % but agents are on flat $ (for manual corrections)
  100. primary_listing_agent_amount = primary_listing_agent_split.remove("$").toDecimal();
  101. co_listing_agent_amount = co_listing_agent_split.remove("$").toDecimal();
  102. }
  103. else
  104. {
  105. //if broker on % split and agents on % split
  106. if(number_of_listing_agents == "1")
  107. {
  108. //if only 1 agent on % split with broker, agent just collects difference - concessions
  109. primary_listing_agent_amount = agentPool - listing_agent_concessions_amount;
  110. co_listing_agent_amount = 0;
  111. }
  112. else
  113. {
  114. //if 2 agents are on % split with broker
  115. primary_listing_agent_amount = agentPool * primary_listing_agent_split.remove("%").toDecimal() / 100 - half_concession;
  116. co_listing_agent_amount = agentPool * co_listing_agent_split.remove("%").toDecimal() / 100 - half_concession;
  117. }
  118. }
  119. }
  120. info "-----------------Listing Commissions---------------";
  121. total_listing_commission_amount = total_listing_commission_amount.round(2);
  122. info "total listing commission: " + total_listing_commission_amount;
  123. listing_broker_amount = listing_broker_amount.round(2);
  124. info "listing broker amount:" + listing_broker_amount;
  125. info "referral fee: " + referral_fee_amount;
  126. info "pool left for agents: " + agentPool;
  127. listing_agent_concessions_amount = listing_agent_concessions_amount.round(2);
  128. info "listing agent concession amount: " + listing_agent_concessions_amount;
  129. primary_listing_agent_amount = primary_listing_agent_amount.round(2);
  130. info "primary listing agent amount: " + primary_listing_agent_amount;
  131. co_listing_agent_amount = co_listing_agent_amount.round(2);
  132. info "co-listing agent amount: " + co_listing_agent_amount;
  133. //run check on commissions
  134. total = primary_listing_agent_amount + co_listing_agent_amount + listing_broker_amount + referral_fee_amount + listing_agent_concessions_amount;
  135. difference = total_listing_commission_amount - total;
  136. info "difference:" + difference;
  137. if(!total = total_listing_commission_amount || difference.abs() <= 0.01)
  138. {
  139. if(total != total_listing_commission_amount)
  140. {
  141. primary_listing_agent_amount = primary_listing_agent_amount + difference;
  142. }
  143. info "new primary amount: " + primary_listing_agent_amount;
  144. update_map = Map();
  145. update_map.put("Primary_Listing_Agent_Commission_fx",primary_listing_agent_amount);
  146. update_map.put("Co_Listing_Agent_Commission_fx",co_listing_agent_amount);
  147. update_map.put("Listing_Broker_Commission_fx",listing_broker_amount);
  148. update_map.put("Referring_Agent_Amount_fx",referral_fee_amount);
  149. update_map.put("Total_Commissions_new_fx",total_listing_commission_amount);
  150. updateRecord = zoho.crm.updateRecord("Deals",dealId,update_map);
  151. info updateRecord;
  152. }
  153. }
  154. if(type == "Buyer")
  155. {
  156. //gather all necessary fields
  157. dealDetails = zoho.crm.getRecordById("Deals",dealId);
  158. contractPrice = ifNull(dealDetails.get("Contract_Price"),dealDetails.get("Amount"));
  159. info "contract price: " + contractPrice;
  160. numberOfAgents = dealDetails.get("Number_of_Buyer_Agents");
  161. totalCommission = ifNull(dealDetails.get("Buyer_Agent_Commission_Offered"),0);
  162. info "total commission: " + totalCommission;
  163. brokerSplit = ifNull(dealDetails.get("Buyer_Broker_Split"),0);
  164. primaryAgentSplit = ifNull(dealDetails.get("Primary_Buyer_s_Agent_Split"),100);
  165. coAgentSplit = ifNull(dealDetails.get("Co_Buyer_s_Agent_Split"),0);
  166. //-----------------------calculate some initital values----------------
  167. //calculate total commission amount
  168. if(totalCommission.contains("$"))
  169. {
  170. //if BAC is flat $ amount, just convert to a number
  171. totalCommissionAmount = totalCommission.remove("$").toDecimal();
  172. }
  173. else
  174. {
  175. //if it is a %, calc as total of contract price
  176. totalCommissionAmount = totalCommission.remove("%").toDecimal() * contractPrice / 100;
  177. }
  178. info "total BAC: " + totalCommissionAmount;
  179. //buyer agent concessions
  180. agentConcessions = ifNull(dealDetails.get("Buyer_Agent_Concessions"),0);
  181. if(agentConcessions.contains("$"))
  182. {
  183. //if buyer agent concessions is $ convert to #
  184. concessionAmount = agentConcessions.remove("$").toDecimal();
  185. }
  186. else
  187. {
  188. //if contains % calculate as a % of contract price
  189. concessionAmount = agentConcessions.remove("%").toDecimal() * contractPrice / 100;
  190. }
  191. if(agentConcessions = 0)
  192. {
  193. //if concessions is 0 set amounts and half amount to 0
  194. concessionAmount = 0;
  195. halfConcessionAmount = 0;
  196. }
  197. else
  198. {
  199. //if agent concession is not 0 set half amount to concession/2
  200. halfConcessionAmount = concessionAmount / 2;
  201. }
  202. info "buyer concessions: " + concessionAmount;
  203. info "half concession: " + halfConcessionAmount;
  204. //calculate referral fees
  205. referralFee = ifNull(dealDetails.get("Referral_Fee_Terms"),"$0");
  206. referralFee = referralFee.removeAllAlpha().trim();
  207. if(referralFee.contains("$"))
  208. {
  209. //if referral fee is $ convert to number
  210. referralFeeAmount = referralFee.remove("$").toNumber();
  211. }
  212. if(referralFee.contains("%"))
  213. {
  214. //if referral fee is a % cal as % of total commissions
  215. referralFeeAmount = referralFee.remove("%").toNumber() * totalCommissionAmount / 100;
  216. }
  217. info "referral fee amount:" + referralFeeAmount;
  218. //------------------------------------Commission Calculations-----------------------
  219. if(brokerSplit.contains("$"))
  220. {
  221. //if broker is paid flat $ amount
  222. brokerAmount = brokerSplit.remove("$").toNumber();
  223. agentPool = totalCommissionAmount - brokerAmount - referralFeeAmount;
  224. agentPool = agentPool.round(2);
  225. if(primaryAgentSplit.contains("$"))
  226. {
  227. //broker is flat $ and agents are paid flat $ amounts
  228. primaryAgentAmount = primaryAgentSplit.remove("$").toDecimal();
  229. coAgentAmount = coAgentSplit.remove("$").toDecimal();
  230. }
  231. else
  232. {
  233. //if broker is flat $ and agents are on %
  234. if(numberOfAgents == "1")
  235. {
  236. //broker is falt $ and 1 agent collects the agent pool - concessions
  237. primaryAgentAmount = agentPool - concessionAmount;
  238. coAgentAmount = 0;
  239. }
  240. else
  241. {
  242. //broker is flat $ and agents collect their % portion and split concessions
  243. primaryAgentAmount = primaryAgentSplit.remove("%").toDecimal() / 100 * agentPool - halfConcessionAmount;
  244. coAgentAmount = coAgentSplit.remove("%").toDecimal() / 100 * agentPool - halfConcessionAmount;
  245. }
  246. }
  247. }
  248. else
  249. {
  250. //broker is on a % split
  251. brokerAmount = brokerSplit.remove("%").toNumber() / 100 * totalCommissionAmount;
  252. agentPool = totalCommissionAmount - brokerAmount - referralFeeAmount;
  253. info "agent pool: " + agentPool;
  254. if(primaryAgentSplit.contains("$"))
  255. {
  256. //broker on % split and agents on $ amount (for manual overrides)
  257. primaryAgentAmount = primaryAgentSplit.remove("$").toDecimal();
  258. coAgentAmount = coAgentSplit.remove("$").toDecimal();
  259. }
  260. else
  261. {
  262. //broker on % split and agents on % split
  263. if(numberOfAgents == "1")
  264. {
  265. //if one agent, he collects agent pool - concessions
  266. primaryAgentAmount = agentPool - concessionAmount;
  267. coAgentAmount = 0;
  268. }
  269. else
  270. {
  271. //if broker is on % and there are 2 agents on %, they collect their % from pool - 1/2 concessions
  272. primaryAgentAmount = primaryAgentSplit.remove("%").toDecimal() / 100 * agentPool - halfConcessionAmount;
  273. coAgentAmount = coAgentSplit.remove("%").toDecimal() / 100 * agentPool - halfConcessionAmount;
  274. }
  275. }
  276. }
  277. info "----------- CalculatedCommissions-------------";
  278. totalCommissionAmount = totalCommissionAmount.round(2);
  279. info "total commissions: " + totalCommissionAmount;
  280. primaryAgentAmount = primaryAgentAmount.round(2);
  281. info "primary Agent Commission: " + primaryAgentAmount;
  282. coAgentAmount = coAgentAmount.round(2);
  283. info "co agent amount: " + coAgentAmount;
  284. brokerAmount = brokerAmount.round(2);
  285. info "broker cut: " + brokerAmount;
  286. concessionAmount = concessionAmount.round(2);
  287. info "concessions: " + concessionAmount;
  288. referralFeeAmount = referralFeeAmount.round(2);
  289. info "referral fee: " + referralFeeAmount;
  290. //check total = correct
  291. difference = 0;
  292. addTotal = primaryAgentAmount + coAgentAmount + brokerAmount + concessionAmount + referralFeeAmount;
  293. if(!addTotal = totalCommissionAmount)
  294. {
  295. difference = totalCommissionAmount - addTotal;
  296. info "difference: " + difference;
  297. }
  298. if(difference.abs() <= 0.01)
  299. {
  300. primaryAgentAmount = primaryAgentAmount + difference;
  301. updateMap = Map();
  302. updateMap.put("Primary_Buyer_Agent_Commission_fx",primaryAgentAmount);
  303. updateMap.put("Co_Buyer_Agent_Commission_fx",coAgentAmount);
  304. updateMap.put("Buyer_Broker_Commission_fx",brokerAmount);
  305. updateMap.put("Total_Commissions_new_fx",totalCommissionAmount);
  306. updateMap.put("Referring_Agent_Amount_fx",referralFeeAmount);
  307. updateRecord = zoho.crm.updateRecord("Deals",dealId,updateMap);
  308. info updateRecord;
  309. }
  310. }
Add Comment
Please, Sign In to add comment