Really, this should be it, for the passing income.
[capital-apms-progress.git] / trigger / wrrchgln.p
blobda1e57c421987cb158023fc0b88f49ccf01b5099
1 &ANALYZE-SUSPEND _VERSION-NUMBER UIB_v8r12
2 &ANALYZE-RESUME
3 &ANALYZE-SUSPEND _UIB-CODE-BLOCK _CUSTOM _DEFINITIONS Procedure
4 /*--------------------------------------------------------------------------
5 File :
6 Purpose : WRITE trigger for RentChargeLine
8 Description :
10 Author(s) : Andrew McMillan
11 Created :
12 Notes :
13 ------------------------------------------------------------------------*/
15 TRIGGER PROCEDURE FOR WRITE OF RentChargeLine OLD OldChargeLine.
17 {inc/ofc-this.i}
18 {inc/ofc-set.i "Rent-Charged-To" "rent-charged-to-text" "WARNING"}
19 IF NOT AVAILABLE(OfficeSetting) THEN rent-charged-to-text = STRING(TODAY + 27).
21 /* _UIB-CODE-BLOCK-END */
22 &ANALYZE-RESUME
25 &ANALYZE-SUSPEND _UIB-PREPROCESSOR-BLOCK
27 /* ******************** Preprocessor Definitions ******************** */
29 &Scoped-define PROCEDURE-TYPE Procedure
33 /* _UIB-PREPROCESSOR-BLOCK-END */
34 &ANALYZE-RESUME
37 /* ************************ Function Prototypes ********************** */
39 &ANALYZE-SUSPEND _UIB-CODE-BLOCK _FUNCTION-FORWARD get-annual-amount Procedure
40 FUNCTION get-annual-amount RETURNS DECIMAL
41 ( INPUT period-amount AS DEC, INPUT freq-type AS CHAR ) FORWARD.
43 /* _UIB-CODE-BLOCK-END */
44 &ANALYZE-RESUME
46 &ANALYZE-SUSPEND _UIB-CODE-BLOCK _FUNCTION-FORWARD get-charge Procedure
47 FUNCTION get-charge RETURNS DECIMAL
48 ( INPUT active-date AS DATE, INPUT end-date AS DATE, INPUT d-from AS DATE, INPUT upto AS DATE, INPUT freq AS CHAR, INPUT amount AS DEC ) FORWARD.
50 /* _UIB-CODE-BLOCK-END */
51 &ANALYZE-RESUME
54 /* *********************** Procedure Settings ************************ */
56 &ANALYZE-SUSPEND _PROCEDURE-SETTINGS
57 /* Settings for THIS-PROCEDURE
58 Type: Procedure
59 Allow:
60 Frames: 0
61 Add Fields to: Neither
62 Other Settings: CODE-ONLY COMPILE
64 &ANALYZE-RESUME _END-PROCEDURE-SETTINGS
66 /* ************************* Create Window ************************** */
68 &ANALYZE-SUSPEND _CREATE-WINDOW
69 /* DESIGN Window definition (used by the UIB)
70 CREATE WINDOW Procedure ASSIGN
71 HEIGHT = .35
72 WIDTH = 39.29.
73 /* END WINDOW DEFINITION */
75 &ANALYZE-RESUME
79 &ANALYZE-SUSPEND _UIB-CODE-BLOCK _CUSTOM _INCLUDED-LIB Procedure
80 /* ************************* Included-Libraries *********************** */
82 {inc/date.i}
84 /* _UIB-CODE-BLOCK-END */
85 &ANALYZE-RESUME
89 &ANALYZE-SUSPEND _UIB-CODE-BLOCK _CUSTOM _MAIN-BLOCK Procedure
92 /* *************************** Main Block *************************** */
94 IF NEW RentChargeLine
95 OR RentChargeLine.Amount <> OldChargeLine.Amount
96 OR RentChargeLine.EndDate <> OldChargeLine.EndDate
97 OR RentChargeLine.FrequencyCode <> OldChargeLine.FrequencyCode
98 OR RentChargeLine.StartDate <> OldChargeLine.StartDate
99 /* OR RentChargeLine.LastChargedDate <> OldChargeLine.LastChargedDate */
100 OR RentChargeLine.RentChargeLineStatus <> OldChargeLine.RentChargeLineStatus
101 THEN
102 RUN recalculate-annual-rental.
104 /* _UIB-CODE-BLOCK-END */
105 &ANALYZE-RESUME
108 /* ********************** Internal Procedures *********************** */
110 &ANALYZE-SUSPEND _UIB-CODE-BLOCK _PROCEDURE recalculate-annual-rental Procedure
111 PROCEDURE recalculate-annual-rental :
112 /*------------------------------------------------------------------------------
113 Purpose: Recalculate the annual rental for this rent charge based on the
114 fact that this RentChargeLine has changed (pass through all of
115 the RentChargeLine records).
116 ------------------------------------------------------------------------------*/
117 DEF BUFFER ChgLine FOR RentChargeLine.
118 DEF VAR annual-charge AS DEC NO-UNDO INITIAL 0.0 .
119 DEF VAR active-date AS DATE NO-UNDO .
120 DEF VAR end-date AS DATE NO-UNDO .
121 DEF VAR this-charge AS DEC NO-UNDO .
122 DEF VAR mo-days AS DEC NO-UNDO .
124 FIND RentCharge OF RentChargeLine NO-LOCK NO-ERROR.
125 IF NOT AVAILABLE(RentCharge) THEN RETURN.
127 active-date = DATE(rent-charged-to-text) + 1.
128 end-date = last-of-month(active-date).
130 FOR EACH ChgLine OF RentCharge WHERE ChgLine.RentChargeLineStatus = "C"
131 AND ROWID(ChgLine) <> ROWID(RentChargeLine) NO-LOCK:
132 IF ChgLine.StartDate <= end-date
133 AND (ChgLine.EndDate >= active-date OR ChgLine.EndDate = ?)
134 THEN DO:
135 annual-charge = annual-charge + get-charge( active-date, end-date,
136 ChgLine.StartDate, ChgLine.EndDate, ChgLine.FrequencyCode, ChgLine.Amount).
137 END.
138 END.
140 IF RentChargeLine.RentChargeLineStatus = "C"
141 AND RentChargeLine.StartDate <= active-date
142 AND (RentChargeLine.EndDate >= active-date OR RentChargeLine.EndDate = ?)
143 THEN DO:
144 annual-charge = annual-charge + get-charge( active-date, end-date,
145 RentChargeLine.StartDate, RentChargeLine.EndDate,
146 RentChargeLine.FrequencyCode, RentChargeLine.Amount).
147 END.
150 IF annual-charge <> RentCharge.CurrentAnnualRental THEN DO:
151 ON WRITE OF RentCharge OVERRIDE DO: END.
152 ON WRITE OF RentChargeLine OVERRIDE DO: END.
153 FIND CURRENT RentCharge EXCLUSIVE-LOCK.
154 RentCharge.CurrentAnnualRental = annual-charge.
155 FIND CURRENT RentCharge NO-LOCK.
156 ON WRITE OF RentChargeLine REVERT.
157 ON WRITE OF RentCharge REVERT.
158 END.
160 END PROCEDURE.
162 /* _UIB-CODE-BLOCK-END */
163 &ANALYZE-RESUME
166 /* ************************ Function Implementations ***************** */
168 &ANALYZE-SUSPEND _UIB-CODE-BLOCK _FUNCTION get-annual-amount Procedure
169 FUNCTION get-annual-amount RETURNS DECIMAL
170 ( INPUT period-amount AS DEC, INPUT freq-type AS CHAR ) :
171 /*------------------------------------------------------------------------------
172 Purpose:
173 Notes:
174 ------------------------------------------------------------------------------*/
175 DEF VAR annual-amount AS DEC NO-UNDO.
177 FIND FrequencyType WHERE FrequencyType.FrequencyCode = freq-type NO-LOCK.
179 annual-amount = ((period-amount / FrequencyType.UnitCount) * (IF FrequencyType.RepeatUnits BEGINS "D" THEN 365 ELSE 12)).
181 RETURN annual-amount.
183 END FUNCTION.
185 /* _UIB-CODE-BLOCK-END */
186 &ANALYZE-RESUME
189 &ANALYZE-SUSPEND _UIB-CODE-BLOCK _FUNCTION get-charge Procedure
190 FUNCTION get-charge RETURNS DECIMAL
191 ( INPUT active-date AS DATE, INPUT end-date AS DATE, INPUT d-from AS DATE, INPUT upto AS DATE, INPUT freq AS CHAR, INPUT amount AS DEC ) :
192 /*------------------------------------------------------------------------------
193 Purpose: Get the charge for the period
194 Notes:
195 ------------------------------------------------------------------------------*/
196 DEF VAR active-days AS DEC NO-UNDO.
197 DEF VAR month-days AS DEC NO-UNDO.
199 month-days = (end-date - active-date) + 1.
200 active-days = month-days.
201 IF d-from > active-date OR (upto < end-date) THEN DO:
202 IF d-from > active-date THEN active-days = active-days - (d-from - active-date).
203 IF upto < end-date THEN active-days = active-days - (end-date - upto).
204 amount = amount * (active-days / month-days).
205 END.
207 RETURN get-annual-amount( amount, freq ).
209 END FUNCTION.
211 /* _UIB-CODE-BLOCK-END */
212 &ANALYZE-RESUME