Subversion Repositories SmartDukaan

Rev

Rev 37592 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
37588 amit 1
package com.spice.profitmandi.dao.entity.fofo;
2
 
3
import javax.persistence.Column;
4
import javax.persistence.Entity;
5
import javax.persistence.Id;
6
import javax.persistence.Table;
7
import java.io.Serializable;
8
import java.math.BigDecimal;
9
import java.math.RoundingMode;
10
import java.time.LocalDate;
11
import java.time.LocalDateTime;
12
 
13
/**
14
 * Current investment snapshot for one partner, maintained by the 2-minute sweep.
15
 *
16
 * <p>All the fast-moving columns are written in a single pass so that coupled terms can never be
17
 * read at different vintages. Wallet and utilised are coupled (an advance payment swept to a loan
18
 * moves both); in-stock and aged-Apple are coupled (the aged units are a subset of stock). Mixing
19
 * vintages between any such pair double-counts the money that moved between them.
20
 *
21
 * <p>{@link #getBaseValue()} is the single number the credit-limit tier consumes. The sweep
22
 * compares it at 2dp against the stored value to decide whether a partner's limit needs
23
 * recomputing, so it must never carry raw double noise.
24
 */
25
@Entity
26
@Table(name = "fofo.partner_investment")
27
public class PartnerInvestment implements Serializable {
28
 
29
    private static final long serialVersionUID = 1L;
30
 
31
    /** Money is rounded to paise everywhere; base_value is a comparison key, not an accumulator. */
32
    public static final int MONEY_SCALE = 2;
33
 
34
    @Id
35
    @Column(name = "fofo_id")
36
    private int fofoId;
37
 
38
    @Column(name = "wallet_amount")
39
    private BigDecimal walletAmount = BigDecimal.ZERO;
40
 
41
    @Column(name = "in_stock_amount")
42
    private BigDecimal inStockAmount = BigDecimal.ZERO;
43
 
44
    @Column(name = "unbilled_amount")
45
    private BigDecimal unbilledAmount = BigDecimal.ZERO;
46
 
47
    @Column(name = "grn_pending_amount")
48
    private BigDecimal grnPendingAmount = BigDecimal.ZERO;
49
 
50
    @Column(name = "activated_stock_amount")
51
    private BigDecimal activatedStockAmount = BigDecimal.ZERO;
52
 
53
    @Column(name = "activated_grn_pending_amount")
54
    private BigDecimal activatedGrnPendingAmount = BigDecimal.ZERO;
55
 
56
    @Column(name = "return_in_transit_amount")
57
    private BigDecimal returnInTransitAmount = BigDecimal.ZERO;
58
 
59
    @Column(name = "utilized_amount")
60
    private BigDecimal utilizedAmount = BigDecimal.ZERO;
61
 
62
    @Column(name = "aged_apple_stock_amount")
63
    private BigDecimal agedAppleStockAmount = BigDecimal.ZERO;
64
 
65
    @Column(name = "demo_excluded_amount")
66
    private BigDecimal demoExcludedAmount = BigDecimal.ZERO;
67
 
68
    @Column(name = "aged_refreshed_on")
69
    private LocalDate agedRefreshedOn;
70
 
71
    @Column(name = "total_investment")
72
    private BigDecimal totalInvestment = BigDecimal.ZERO;
73
 
74
    @Column(name = "base_value")
75
    private BigDecimal baseValue = BigDecimal.ZERO;
76
 
77
    @Column(name = "snapshot_timestamp")
78
    private LocalDateTime snapshotTimestamp;
79
 
80
    public PartnerInvestment() {
81
    }
82
 
83
    public PartnerInvestment(int fofoId) {
84
        this.fofoId = fofoId;
85
    }
86
 
87
    private static BigDecimal scaled(BigDecimal value) {
88
        return (value == null ? BigDecimal.ZERO : value).setScale(MONEY_SCALE, RoundingMode.HALF_UP);
89
    }
90
 
91
    /**
92
     * Recomputes {@code totalInvestment} and {@code baseValue} from the component columns.
93
     *
94
     * <p>Mirrors PartnerDailyInvestment.getTotalInvestment() less the aged-Apple haircut, then less
95
     * utilisation — the same chain PartnerLimitHelper.getSuggestedAmount() applies before the tier
96
     * multiply. Keeping it here means the limit never has to re-derive it from separate reads.
97
     */
98
    public void recalculate() {
99
        BigDecimal total = scaled(walletAmount)
100
                .add(scaled(inStockAmount))
101
                .add(scaled(unbilledAmount))
102
                .add(scaled(grnPendingAmount))
103
                .add(scaled(returnInTransitAmount))
104
                .subtract(scaled(activatedStockAmount))
105
                .subtract(scaled(activatedGrnPendingAmount));
106
        this.totalInvestment = scaled(total);
107
        this.baseValue = scaled(this.totalInvestment
108
                .subtract(scaled(agedAppleStockAmount))
109
                .subtract(scaled(utilizedAmount)));
110
    }
111
 
112
    /** True when the base has moved enough to matter, i.e. at all once rounded to paise. */
113
    public boolean baseDiffersFrom(PartnerInvestment other) {
114
        if (other == null) {
115
            return true;
116
        }
117
        return scaled(this.baseValue).compareTo(scaled(other.baseValue)) != 0;
118
    }
119
 
120
    public int getFofoId() {
121
        return fofoId;
122
    }
123
 
124
    public void setFofoId(int fofoId) {
125
        this.fofoId = fofoId;
126
    }
127
 
128
    public BigDecimal getWalletAmount() {
129
        return walletAmount;
130
    }
131
 
132
    public void setWalletAmount(BigDecimal walletAmount) {
133
        this.walletAmount = scaled(walletAmount);
134
    }
135
 
136
    public BigDecimal getInStockAmount() {
137
        return inStockAmount;
138
    }
139
 
140
    public void setInStockAmount(BigDecimal inStockAmount) {
141
        this.inStockAmount = scaled(inStockAmount);
142
    }
143
 
144
    public BigDecimal getUnbilledAmount() {
145
        return unbilledAmount;
146
    }
147
 
148
    public void setUnbilledAmount(BigDecimal unbilledAmount) {
149
        this.unbilledAmount = scaled(unbilledAmount);
150
    }
151
 
152
    public BigDecimal getGrnPendingAmount() {
153
        return grnPendingAmount;
154
    }
155
 
156
    public void setGrnPendingAmount(BigDecimal grnPendingAmount) {
157
        this.grnPendingAmount = scaled(grnPendingAmount);
158
    }
159
 
160
    public BigDecimal getActivatedStockAmount() {
161
        return activatedStockAmount;
162
    }
163
 
164
    public void setActivatedStockAmount(BigDecimal activatedStockAmount) {
165
        this.activatedStockAmount = scaled(activatedStockAmount);
166
    }
167
 
168
    public BigDecimal getActivatedGrnPendingAmount() {
169
        return activatedGrnPendingAmount;
170
    }
171
 
172
    public void setActivatedGrnPendingAmount(BigDecimal activatedGrnPendingAmount) {
173
        this.activatedGrnPendingAmount = scaled(activatedGrnPendingAmount);
174
    }
175
 
176
    public BigDecimal getReturnInTransitAmount() {
177
        return returnInTransitAmount;
178
    }
179
 
180
    public void setReturnInTransitAmount(BigDecimal returnInTransitAmount) {
181
        this.returnInTransitAmount = scaled(returnInTransitAmount);
182
    }
183
 
184
    public BigDecimal getUtilizedAmount() {
185
        return utilizedAmount;
186
    }
187
 
188
    public void setUtilizedAmount(BigDecimal utilizedAmount) {
189
        this.utilizedAmount = scaled(utilizedAmount);
190
    }
191
 
192
    public BigDecimal getAgedAppleStockAmount() {
193
        return agedAppleStockAmount;
194
    }
195
 
196
    public void setAgedAppleStockAmount(BigDecimal agedAppleStockAmount) {
197
        this.agedAppleStockAmount = scaled(agedAppleStockAmount);
198
    }
199
 
200
    public BigDecimal getDemoExcludedAmount() {
201
        return demoExcludedAmount;
202
    }
203
 
204
    public void setDemoExcludedAmount(BigDecimal demoExcludedAmount) {
205
        this.demoExcludedAmount = scaled(demoExcludedAmount);
206
    }
207
 
208
    public LocalDate getAgedRefreshedOn() {
209
        return agedRefreshedOn;
210
    }
211
 
212
    public void setAgedRefreshedOn(LocalDate agedRefreshedOn) {
213
        this.agedRefreshedOn = agedRefreshedOn;
214
    }
215
 
216
    public BigDecimal getTotalInvestment() {
217
        return totalInvestment;
218
    }
219
 
220
    public void setTotalInvestment(BigDecimal totalInvestment) {
221
        this.totalInvestment = scaled(totalInvestment);
222
    }
223
 
224
    public BigDecimal getBaseValue() {
225
        return baseValue;
226
    }
227
 
228
    public void setBaseValue(BigDecimal baseValue) {
229
        this.baseValue = scaled(baseValue);
230
    }
231
 
232
    public LocalDateTime getSnapshotTimestamp() {
233
        return snapshotTimestamp;
234
    }
235
 
236
    public void setSnapshotTimestamp(LocalDateTime snapshotTimestamp) {
237
        this.snapshotTimestamp = snapshotTimestamp;
238
    }
239
 
240
    @Override
241
    public String toString() {
242
        return "PartnerInvestment{fofoId=" + fofoId
243
                + ", wallet=" + walletAmount
244
                + ", inStock=" + inStockAmount
245
                + ", unbilled=" + unbilledAmount
246
                + ", grnPending=" + grnPendingAmount
247
                + ", activatedStock=" + activatedStockAmount
248
                + ", activatedGrnPending=" + activatedGrnPendingAmount
249
                + ", agedApple=" + agedAppleStockAmount
250
                + ", demoExcluded=" + demoExcludedAmount
251
                + ", utilised=" + utilizedAmount
252
                + ", total=" + totalInvestment
253
                + ", base=" + baseValue
254
                + ", at=" + snapshotTimestamp + '}';
255
    }
256
}