1import { Controller } from "@hotwired/stimulus" 2 3export default class extends Controller { 4 static targets = ["paymentMethod", "amount", "grossAmount", "feeAmount", "contactSelect", "emailOptions"] 5 6 connect() { 7 // Only sync if we have values already (for edit forms) 8 if (this.hasGrossAmountTarget && this.grossAmountTarget.value) { 9 this.syncFromGross() 10 } 11 this.toggleEmailOptions() 12 } 13 14 contactChanged() { 15 this.toggleEmailOptions() 16 } 17 18 toggleEmailOptions() { 19 if (!this.hasEmailOptionsTarget || !this.hasContactSelectTarget) return 20 21 if (this.contactSelectTarget.value) { 22 this.emailOptionsTarget.classList.remove("hidden") 23 } else { 24 this.emailOptionsTarget.classList.add("hidden") 25 } 26 } 27 28 paymentMethodChanged() { 29 // When payment method changes, sync gross to net for cash/check 30 this.syncFromGross() 31 } 32 33 grossAmountChanged() { 34 // When gross amount changes, update net amount (gross - fee) 35 this.syncFromGross() 36 } 37 38 amountChanged() { 39 // When net amount changes, update gross amount (net + fee) 40 this.syncFromNet() 41 } 42 43 feeAmountChanged() { 44 // When fee changes, recalculate net from gross 45 this.syncFromGross() 46 } 47 48 syncFromGross() { 49 if (!this.hasGrossAmountTarget || !this.hasAmountTarget || !this.hasFeeAmountTarget) return 50 51 const paymentMethod = this.hasPaymentMethodTarget ? this.paymentMethodTarget.value : "" 52 const grossAmount = parseFloat(this.grossAmountTarget.value) || 0 53 54 if (paymentMethod === "cash" || paymentMethod === "check") { 55 // For cash/check, no fees - net equals gross 56 this.feeAmountTarget.value = "0.00" 57 this.amountTarget.value = grossAmount.toFixed(2) 58 } else { 59 // For other methods, net = gross - fee 60 const feeAmount = parseFloat(this.feeAmountTarget.value) || 0 61 const netAmount = grossAmount - feeAmount 62 this.amountTarget.value = netAmount.toFixed(2) 63 } 64 } 65 66 syncFromNet() { 67 if (!this.hasGrossAmountTarget || !this.hasAmountTarget || !this.hasFeeAmountTarget) return 68 69 const paymentMethod = this.hasPaymentMethodTarget ? this.paymentMethodTarget.value : "" 70 const netAmount = parseFloat(this.amountTarget.value) || 0 71 72 if (paymentMethod === "cash" || paymentMethod === "check") { 73 // For cash/check, no fees - gross equals net 74 this.feeAmountTarget.value = "0.00" 75 this.grossAmountTarget.value = netAmount.toFixed(2) 76 } else { 77 // For other methods, gross = net + fee 78 const feeAmount = parseFloat(this.feeAmountTarget.value) || 0 79 const grossAmount = netAmount + feeAmount 80 this.grossAmountTarget.value = grossAmount.toFixed(2) 81 } 82 } 83};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.