Advertisement
Guest User

Untitled

a guest
Mar 7th, 2025
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. function onEdit(e) {
  2. if (!e || !e.range) return;
  3.  
  4. const sheet = e.source.getActiveSheet();
  5. const sheetName = 'Daily Transactions';
  6. const servicesSheetName = 'Services & Products';
  7.  
  8. if (sheet.getName() === sheetName) {
  9. const serviceCol = 2; // Column B (Service/Product)
  10. const priceCol = 3; // Column C (Price)
  11. const installmentCol = 8; // Column H (Installment?)
  12. const paymentCol = 9; // Column I (Payment Made)
  13. const discountCol = 10; // Column J (Discount)
  14. const commissionCol = 7; // Column G (Commission)
  15.  
  16. let paymentMade = sheet.getRange(editedRow, paymentCol).getValue();
  17.  
  18. if (editedColumn === paymentCol || editedColumn === serviceCol || editedColumn === discountCol) {
  19. updateFinalPrice(sheet, editedRow, serviceCol, priceCol, installmentCol, paymentCol, discountCol, commissionCol, servicesSheetName);
  20. }
  21. }
  22. }
  23.  
  24. function updateFinalPrice(sheet, row, serviceCol, priceCol, installmentCol, paymentCol, discountCol, commissionCol, servicesSheetName) {
  25. const servicesSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(servicesSheetName);
  26. if (!servicesSheet) return;
  27.  
  28. const servicesData = servicesSheet.getDataRange().getValues();
  29. let serviceName = sheet.getRange(row, serviceCol).getValue();
  30. let installment = sheet.getRange(row, installmentCol).getValue();
  31. let paymentMade = sheet.getRange(row, paymentCol).getValue();
  32. let discount = sheet.getRange(row, discountCol).getValue();
  33.  
  34. let servicePrice = null;
  35. let sessionCount = null;
  36.  
  37. for (let i = 1; i < servicesData.length; i++) {
  38. if (servicesData[i][0] === serviceName) {
  39. servicePrice = servicesData[i][3];
  40. sessionCount = servicesData[i][1];
  41. break;
  42. }
  43. }
  44.  
  45. if (servicePrice === null) return;
  46.  
  47. servicePrice = parseFloat(servicePrice);
  48. if (isNaN(servicePrice)) return;
  49.  
  50. discount = discount ? parseFloat(discount) : 0;
  51. if (discount > 1) discount /= 100;
  52.  
  53. let discountedPrice = servicePrice * (1 - discount);
  54.  
  55. if (paymentMade === "Custom") {
  56. sheet.getRange(row, priceCol).setValue("");
  57. return;
  58. }
  59.  
  60. if (installment === "Yes") {
  61. let installmentAmount = 0;
  62. if (paymentMade === '1st Payment (60%)') {
  63. installmentAmount = discountedPrice * 0.6;
  64. } else if (paymentMade === '2nd Payment (30%)') {
  65. installmentAmount = discountedPrice * 0.3;
  66. } else if (paymentMade === '3rd Payment (10%)') {
  67. installmentAmount = discountedPrice * 0.1;
  68. }
  69.  
  70. if (installmentAmount > 0) {
  71. sheet.getRange(row, priceCol).setValue(installmentAmount).setNumberFormat("₱#,##0.00");
  72. updateCommission(sheet, row, discountedPrice, sessionCount, commissionCol);
  73. return;
  74. }
  75. }
  76.  
  77. sheet.getRange(row, priceCol).setValue(discountedPrice).setNumberFormat("₱#,##0.00");
  78. updateCommission(sheet, row, discountedPrice, sessionCount, commissionCol);
  79. }
  80.  
  81. function updateCommission(sheet, row, discountedPrice, sessionCount, commissionCol) {
  82. let perSessionPrice = (sessionCount && [2, 3, 4, 6, 10, 13, 15].includes(sessionCount)) ? discountedPrice / sessionCount : discountedPrice;
  83. let commission = perSessionPrice * 0.1;
  84. sheet.getRange(row, commissionCol).setValue(commission).setNumberFormat("₱#,##0.00");
  85. }
  86.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement