Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
37443 ranu 1
package com.spice.profitmandi.dao.model;
2
 
3
import javax.persistence.*;
4
import java.util.Objects;
5
 
6
/**
7
 * Brand × movement-status roll-up of what a single RBM sold, for the RBM DRR
8
 * dashboard drill-down. Same auth_user → fofo_store derivation as
9
 * {@link TodayAchievedMovementModel}. Value = line-item revenue (quantity
10
 * × selling_price) so multi-line orders aren't multi-counted per bucket.
11
 * Rows are grouped by (brand, movement_status); the controller aggregates
12
 * to brand totals and per-brand bucket splits for the UI.
13
 */
14
@Entity
15
@NamedNativeQueries({
16
        @NamedNativeQuery(
17
                name = "RbmDrr.BrandSoldByRbm",
18
                query = "SELECT li.brand                                       AS brand," +
19
                        "       COALESCE(cc.status, 'OTHER')                    AS movement_status," +
20
                        "       COALESCE(SUM(li.quantity), 0)                  AS quantity," +
21
                        "       COALESCE(SUM(li.quantity * t.selling_price), 0) AS value_sold" +
22
                        " FROM (SELECT au.id AS auth_id, fs.id AS fofo_id, fs.warehouse_id" +
23
                        "       FROM auth.auth_user au" +
24
                        "                JOIN cs.position p ON p.auth_user_id = au.id" +
25
                        "                JOIN cs.partner_position pp ON pp.position_id = p.id" +
26
                        "                JOIN fofo.fofo_store fs ON fs.id = pp.partner_id" +
27
                        "       WHERE pp.partner_id != 0" +
28
                        "         AND p.category_id = 18" +
29
                        "         AND p.escalation_type = 'L1'" +
30
                        "         AND fs.active = 1" +
31
                        "         AND fs.internal = false" +
32
                        "       UNION ALL" +
33
                        "       SELECT au.id AS auth_id, fs.id AS fofo_id, fs.warehouse_id" +
34
                        "       FROM auth.auth_user au" +
35
                        "                JOIN cs.position p ON p.auth_user_id = au.id" +
36
                        "                JOIN cs.partner_position pp ON pp.position_id = p.id" +
37
                        "                JOIN cs.region r ON pp.region_id = r.id" +
38
                        "                JOIN cs.partner_region pr ON pr.region_id = pp.region_id" +
39
                        "                JOIN fofo.fofo_store fs ON fs.id = pr.fofo_id" +
40
                        "       WHERE pp.partner_id = 0" +
41
                        "         AND p.category_id = 18" +
42
                        "         AND fs.active = 1" +
43
                        "         AND fs.internal = false" +
44
                        "         AND p.escalation_type = 'L1') a" +
45
                        " JOIN transaction.order odr ON odr.customer_id = a.fofo_id" +
46
                        " JOIN transaction.lineitem li ON li.order_id = odr.id" +
47
                        " JOIN catalog.item i ON i.id = li.item_id" +
48
                        " JOIN catalog.tag_listing t ON t.item_id = i.id" +
49
                        " LEFT JOIN catalog.catagoriesd_catalog cc" +
50
                        "        ON cc.catalog_id = i.catalog_item_id" +
51
                        "       AND cc.start_date <= odr.created_timestamp" +
52
                        "       AND (cc.end_date IS NULL OR cc.end_date > odr.created_timestamp)" +
53
                        " WHERE a.auth_id = :authId" +
54
                        "   AND a.warehouse_id = :warehouseId" +
55
                        "   AND odr.created_timestamp >= :startDate" +
56
                        "   AND odr.created_timestamp <  :endDate" +
57
                        "   AND odr.refund_timestamp IS NULL" +
58
                        "   AND li.brand NOT IN ('Smartdukaan', 'Dummy', 'FOC', 'Live Demo')" +
59
                        " GROUP BY li.brand, movement_status" +
60
                        " ORDER BY value_sold DESC",
61
                resultSetMapping = "RbmDrrBrandSoldByRbm")
62
})
63
@SqlResultSetMappings({
64
        @SqlResultSetMapping(
65
                name = "RbmDrrBrandSoldByRbm",
66
                classes = {
67
                        @ConstructorResult(
68
                                targetClass = RbmBrandSoldModel.class,
69
                                columns = {
70
                                        @ColumnResult(name = "brand", type = String.class),
71
                                        @ColumnResult(name = "movement_status", type = String.class),
72
                                        @ColumnResult(name = "quantity", type = Double.class),
73
                                        @ColumnResult(name = "value_sold", type = Double.class)
74
                                }
75
                        )
76
                }
77
        )
78
})
79
public class RbmBrandSoldModel {
80
 
81
    private String brand;
82
    private String movementStatus;
83
    private Double quantity;
84
    private Double valueSold;
85
 
86
    @Id
87
    @GeneratedValue(strategy = GenerationType.IDENTITY)
88
    private Long id;
89
 
90
    public RbmBrandSoldModel() {
91
    }
92
 
93
    public RbmBrandSoldModel(String brand, String movementStatus, Double quantity, Double valueSold) {
94
        this.brand = brand;
95
        this.movementStatus = movementStatus;
96
        this.quantity = quantity;
97
        this.valueSold = valueSold;
98
    }
99
 
100
    public String getBrand() {
101
        return brand;
102
    }
103
 
104
    public void setBrand(String brand) {
105
        this.brand = brand;
106
    }
107
 
108
    public String getMovementStatus() {
109
        return movementStatus;
110
    }
111
 
112
    public void setMovementStatus(String movementStatus) {
113
        this.movementStatus = movementStatus;
114
    }
115
 
116
    public Double getQuantity() {
117
        return quantity;
118
    }
119
 
120
    public void setQuantity(Double quantity) {
121
        this.quantity = quantity;
122
    }
123
 
124
    public Double getValueSold() {
125
        return valueSold;
126
    }
127
 
128
    public void setValueSold(Double valueSold) {
129
        this.valueSold = valueSold;
130
    }
131
 
132
    @Override
133
    public boolean equals(Object o) {
134
        if (this == o) return true;
135
        if (o == null || getClass() != o.getClass()) return false;
136
        RbmBrandSoldModel that = (RbmBrandSoldModel) o;
137
        return Objects.equals(brand, that.brand)
138
                && Objects.equals(movementStatus, that.movementStatus)
139
                && Objects.equals(quantity, that.quantity)
140
                && Objects.equals(valueSold, that.valueSold);
141
    }
142
 
143
    @Override
144
    public int hashCode() {
145
        return Objects.hash(brand, movementStatus, quantity, valueSold);
146
    }
147
 
148
    @Override
149
    public String toString() {
150
        return "RbmBrandSoldModel{brand='" + brand + "', movementStatus='" + movementStatus
151
                + "', quantity=" + quantity + ", valueSold=" + valueSold + '}';
152
    }
153
}