Subversion Repositories SmartDukaan

Rev

Rev 7089 | Rev 7199 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
4500 mandeep.dh 1
<?xml version="1.0" encoding="UTF-8"?>
2
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 
3
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
4
 
5
<mapper namespace="in.shop2020.warehouse.persistence.ScanMapper">
6
  <resultMap type="scan" id="scanResult">
7
    <id property="id" column="id"/>
8
  </resultMap>
9
 
10
  <insert id="insert" parameterType="scan" useGeneratedKeys="true" keyProperty="id">
11
    INSERT INTO scanNew
12
      (inventoryItemId, warehouseId, type, scannedAt, quantity, orderId)
13
    VALUES
14
      (#{inventoryItemId}, #{warehouseId}, #{type}, NOW(), #{quantity}, #{orderId})
15
  </insert>
16
 
17
  <select id="get" parameterType="map" resultType="scan">
18
    SELECT * 
19
    FROM scanNew
20
    WHERE inventoryItemId = #{inventoryItemId}
21
    <if test="scanType != null">
22
        AND type = #{scanType}
23
    </if>
24
    ORDER BY scannedAt
25
  </select>
5361 mandeep.dh 26
 
27
  <select id="getScansFromOrderId" parameterType="java.lang.Long" resultType="scan">
28
    SELECT * 
29
    FROM scanNew
30
    WHERE orderId = #{orderId}
31
    ORDER BY scannedAt
32
  </select>
5372 mandeep.dh 33
 
34
  <select id="getPurchaseScans" parameterType="map" resultType="detailedPurchaseScan">
5395 mandeep.dh 35
    SELECT p.purchaseOrder_id AS purchaseOrderId, 1000 * UNIX_TIMESTAMP(po.createdAt) AS poCreatedAt, s.name AS supplierName,
6494 amar.kumar 36
        GROUP_CONCAT(DISTINCT(p.invoiceNumber)) AS invoiceNumbers, GROUP_CONCAT(DISTINCT(inv.receivedFrom)) AS receivedBy, i.itemId,
6361 rajveer 37
        brand, modelName, modelNumber, color, unitPrice, SUM(initialQuantity) AS quantity, 1000 * UNIX_TIMESTAMP(receivedOn) as purchasedAt, p.id as purchaseId 
5372 mandeep.dh 38
    FROM purchase p
39
    JOIN purchaseorder po 
40
        ON (po.id = p.purchaseOrder_id)
41
    JOIN inventoryItem i
42
        ON (p.id = i.purchaseId)
43
    JOIN lineitem l
44
        ON (l.itemId = i.itemId AND l.purchaseOrder_id = po.id)
45
    JOIN supplier s
46
        ON (s.id = po.supplierId)
6494 amar.kumar 47
    JOIN invoice inv
48
    	ON (inv.invoiceNumber = p.invoiceNumber)
5372 mandeep.dh 49
    WHERE po.createdAt BETWEEN #{startDate} AND #{endDate} 
6364 rajveer 50
    AND lastScanType != 'DOA_REPLACED' 
6362 rajveer 51
    GROUP BY p.purchaseOrder_id, p.id, i.itemId
52
    ORDER BY p.purchaseOrder_id, p.id, i.itemId
5372 mandeep.dh 53
  </select>
5496 mandeep.dh 54
 
55
  <select id="fetchMismatchScansPerInvoiceNumber" parameterType="java.util.Date" resultType="invoiceScan" >
56
    SELECT i.invoiceNumber, i.numItems, sum(initialQuantity) as scannedQuantity, i.date, s.name as supplierName
57
    FROM invoice i
58
    JOIN supplier s
7090 amar.kumar 59
    	ON (i.supplierId = s.id)
5496 mandeep.dh 60
    LEFT JOIN purchase p
61
        ON (p.invoiceNumber = i.invoiceNumber)
7089 amar.kumar 62
    LEFT JOIN inventoryItem ii 
63
    	ON (ii.purchaseId = p.id)
64
    JOIN scanNew sn
65
    	ON (sn.inventoryItemId = ii.id)
5496 mandeep.dh 66
    WHERE i.date = #{date}
7089 amar.kumar 67
    AND	sn.scannedAt between #{date} and ADDDATE(#{date},1)
68
    AND sn.type = 'PURCHASE'
69
    AND s.id !=1
5496 mandeep.dh 70
    GROUP BY i.invoiceNumber, i.numItems
71
    HAVING SUM(initialQuantity) != i.numItems
72
  </select>
5372 mandeep.dh 73
 
5711 mandeep.dh 74
  <select id="getInventoryAge" resultType="inventoryAge">
75
    SELECT t.itemId, brand, modelName, modelNumber, color,
76
        SUM((age &lt; 1) * currentQuantity) AS freshCount, 
5768 mandeep.dh 77
        SUM((age &gt;= 1 and age &lt; 2) * currentQuantity) AS oneToTwoCount, 
78
        SUM((age &gt;= 2 and age &lt; 3) * currentQuantity) AS twoToThreeCount, 
79
        SUM((age &gt;= 3 and age &lt; 4) * currentQuantity) AS threeToFourCount, 
80
        SUM((age &gt;= 4) * currentQuantity) as fourPlusCount,
81
        SUM((age &gt;= 1) * currentQuantity) as onePlusCount,
82
        SUM((age &gt;= 1) * unitPrice) as onePlusCost,
83
        SUM(currentQuantity) as zeroPlusCount,
84
        SUM(l.unitPrice) as zeroPlusCost,
85
        l.productGroup as category
5711 mandeep.dh 86
    FROM  (SELECT ii.id, ii.itemId, ii.currentQuantity, ii.purchaseId,
87
                DATEDIFF(now(), MAX(s.scannedAt)) / 7 as age
88
           FROM inventoryItem ii 
89
           JOIN scanNew s 
90
                ON (s.inventoryItemId = ii.id) 
91
           WHERE ii.lastScanType IN ('PURCHASE', 'SALE_RET') 
92
           GROUP BY ii.id, ii.itemId, ii.currentQuantity) t 
93
    JOIN purchase p 
94
        ON (p.id = t.purchaseId) 
95
    JOIN lineitem l 
96
        ON (l.purchaseOrder_id = p.purchaseOrder_id AND l.itemId = t.itemId)
97
    GROUP BY t.itemId 
98
    HAVING SUM(currentQuantity) != 0
99
  </select>
6322 amar.kumar 100
 
7089 amar.kumar 101
  <!--select id="inventoryAgeOnePlus" resultType="detailedInventoryAge">
102
    SELECT t.itemId, brand, modelName, modelNumber, color, scannedAt, i.warehouseId
103
        SUM((age &gt;= 1) * currentQuantity) AS onePlusCount, 
104
        SUM((age &gt;= 1) * unitPrice) as onePlusCost,
105
        l.productGroup as category
106
    FROM  (SELECT ii.id, ii.itemId, ii.currentQuantity, ii.purchaseId, MIN(s.scannedAt)
107
                DATEDIFF(now(), MAX(s.scannedAt)) / 7 as age
108
           FROM inventoryItem ii 
109
           JOIN scanNew s 
110
                ON (s.inventoryItemId = ii.id) 
111
           WHERE ii.lastScanType IN ('PURCHASE', 'SALE_RET') 
112
           GROUP BY ii.id, ii.itemId, ii.currentQuantity) t 
113
    JOIN purchase p 
114
        ON (p.id = t.purchaseId) 
115
    JOIN lineitem l 
116
        ON (l.purchaseOrder_id = p.purchaseOrder_id AND l.itemId = t.itemId)
117
    GROUP BY t.itemId, i.warehouseId 
118
    HAVING SUM(currentQuantity) != 0
119
  </select-->
120
 
121
 
122
 
6322 amar.kumar 123
  <select id = "getScansForItem" parameterType = "map" resultType = "scan">
124
	  SELECT s.id, s.inventoryItemId, s.warehouseId, s.type, s.scannedAt, 
125
	  s.quantity, s.orderId from scanNew s JOIN inventoryItem i 
126
	  ON 
127
	  s.inventoryItemId = i.id 
128
	  AND
6414 amar.kumar 129
	  i.itemId = #{itemId} 
6322 amar.kumar 130
	  AND
131
	  s.scannedAt BETWEEN #{fromDate} AND #{toDate};
132
  </select>
133
 
6558 amar.kumar 134
  <insert id="genericScan" parameterType="scan" useGeneratedKeys="true" keyProperty="id">
6467 amar.kumar 135
    INSERT INTO scanNew
136
      (inventoryItemId, warehouseId, type, scannedAt, quantity)
137
    VALUES
138
      (#{inventoryItemId}, #{warehouseId}, #{type}, NOW(), #{quantity})
139
  </insert>
140
 
6558 amar.kumar 141
  <select id = "getCurrentSerializedInventoryByScans" resultType = "inventoryAvailability">
142
	SELECT x.itemId, ci.brand, ci.model_name as modelName, 
6630 amar.kumar 143
	ci.model_number as modelNumber, ci.color as color, (x.inx-if(y.outx is null,0,y.outx)) as quantity
6558 amar.kumar 144
	from 
145
	(select i.itemId, count(*) as inx from scanNew s 
146
		JOIN inventoryItem i ON (i.id = s.inventoryItemId) 
147
		JOIN catalog.item c ON i.itemId = c.id 
148
		where s.type in ('PURCHASE','SALE_RET') and c.type = 'SERIALIZED' 
149
		group by i.itemId ) as x 
6630 amar.kumar 150
	LEFT JOIN 
6558 amar.kumar 151
	(select i.itemId, count(*) as outx from scanNew s 
152
		JOIN inventoryItem i on i.id = s.inventoryItemId 
153
		JOIN catalog.item c ON i.itemId = c.id 
154
		where s.type in ('SALE','LOST_IN_WAREHOUSE','PURCHASE_RETURN') and c.type = 'SERIALIZED' 
155
		group by i.itemId) as y 
156
	ON x.itemId = y.itemId 
157
	JOIN catalog.item ci ON ci.id = x.itemId 
6630 amar.kumar 158
	where ci.type = 'SERIALIZED' having quantity>0  
6558 amar.kumar 159
  </select>
6762 amar.kumar 160
 
161
<select id = "getHistoricSerializedInventoryByScans" resultType = "inventoryAvailability" parameterType = "date">
162
	SELECT x.itemId, ci.brand, ci.model_name as modelName, 
163
	ci.model_number as modelNumber, ci.color as color, (x.inx-if(y.outx is null,0,y.outx)) as quantity
164
	from 
165
	(select i.itemId, count(*) as inx from scanNew s 
166
		JOIN inventoryItem i ON (i.id = s.inventoryItemId) 
167
		JOIN catalog.item c ON i.itemId = c.id 
168
		where s.type in ('PURCHASE','SALE_RET') and c.type = 'SERIALIZED'
169
		and s.scannedAt &lt; #{date} 
170
		group by i.itemId ) as x 
171
	LEFT JOIN 
172
	(select i.itemId, count(*) as outx from scanNew s 
173
		JOIN inventoryItem i on i.id = s.inventoryItemId 
174
		JOIN catalog.item c ON i.itemId = c.id 
175
		where s.type in ('SALE','LOST_IN_WAREHOUSE','PURCHASE_RETURN') and c.type = 'SERIALIZED'
176
		and s.scannedAt &lt; #{date} 
177
		group by i.itemId) as y 
178
	ON x.itemId = y.itemId 
179
	JOIN catalog.item ci ON ci.id = x.itemId 
180
	where ci.type = 'SERIALIZED' having quantity>0  
181
  </select>
182
 
6558 amar.kumar 183
 
6630 amar.kumar 184
  <select id = "getCurrentNonSerializedInventoryByScans" resultType = "inventoryAvailability">
185
	SELECT x.itemId, ci.brand, ci.model_name as modelName, 
186
	ci.model_number as modelNumber, ci.color as color, (x.inx-if(y.outx is null,0,y.outx)) as quantity
187
	from 
188
	(select i.itemId, sum(quantity) as inx from scanNew s 
189
		JOIN inventoryItem i ON (i.id = s.inventoryItemId) 
190
		JOIN catalog.item c ON i.itemId = c.id 
191
		where s.type in ('PURCHASE','SALE_RET') and c.type = 'NON_SERIALIZED' 
192
		group by i.itemId ) as x 
193
	LEFT JOIN 
194
	(select i.itemId, sum(quantity) as outx from scanNew s 
195
		JOIN inventoryItem i on i.id = s.inventoryItemId 
196
		JOIN catalog.item c ON i.itemId = c.id 
197
		where s.type in ('SALE','LOST_IN_WAREHOUSE','PURCHASE_RETURN') and c.type = 'NON_SERIALIZED' 
198
		group by i.itemId) as y 
199
	ON x.itemId = y.itemId 
200
	JOIN catalog.item ci ON ci.id = x.itemId 
201
	where ci.type = 'NON_SERIALIZED' having quantity>0  
202
  </select>
6762 amar.kumar 203
 
204
<select id = "getHistoricNonSerializedInventoryByScans" resultType = "inventoryAvailability" parameterType = "date">
205
	SELECT x.itemId, ci.brand, ci.model_name as modelName, 
206
	ci.model_number as modelNumber, ci.color as color, (x.inx-if(y.outx is null,0,y.outx)) as quantity
207
	from 
208
	(select i.itemId, sum(quantity) as inx from scanNew s 
209
		JOIN inventoryItem i ON (i.id = s.inventoryItemId) 
210
		JOIN catalog.item c ON i.itemId = c.id 
211
		where s.type in ('PURCHASE','SALE_RET') and c.type = 'NON_SERIALIZED' 
212
		and s.scannedAt &lt; #{date}
213
		group by i.itemId ) as x 
214
	LEFT JOIN 
215
	(select i.itemId, sum(quantity) as outx from scanNew s 
216
		JOIN inventoryItem i on i.id = s.inventoryItemId 
217
		JOIN catalog.item c ON i.itemId = c.id 
218
		where s.type in ('SALE','LOST_IN_WAREHOUSE','PURCHASE_RETURN') and c.type = 'NON_SERIALIZED'
219
		and s.scannedAt &lt; #{date} 
220
		group by i.itemId) as y 
221
	ON x.itemId = y.itemId 
222
	JOIN catalog.item ci ON ci.id = x.itemId 
223
	where ci.type = 'NON_SERIALIZED' having quantity>0  
224
  </select>
225
 
6880 amar.kumar 226
 <select id = "getMovementSerializedInventoryByScans" resultType = "inventoryMovement" parameterType = "map">
6762 amar.kumar 227
 	select itemId, brand, model_name as modelName, model_number as modelNumber, color, s.type, count(*) as quantity 
228
 	FROM inventoryItem i JOIN scanNew s ON i.id = s.inventoryItemId 
229
 	LEFT JOIN catalog.item c ON i.itemId = c.id 
6963 amar.kumar 230
 	where s.scannedAt between #{startDate} and #{endDate} and c.type = 'SERIALIZED'
231
 	and i.currentWarehouseId not in (1,2,6,9) 
6762 amar.kumar 232
 	group by itemId, s.type
233
 </select>
234
 
235
 <select id = "getMovementNonSerializedInventoryByScans" resultType = "inventoryMovement" parameterType = "map">
6880 amar.kumar 236
 	select itemId, brand, model_name as modelName, model_number as modelNumber, color, s.type, sum(quantity) as quantity 
6762 amar.kumar 237
 	FROM inventoryItem i JOIN scanNew s ON i.id = s.inventoryItemId 
238
 	LEFT JOIN catalog.item c ON i.itemId = c.id 
6963 amar.kumar 239
 	where s.scannedAt between #{startDate} and #{endDate} and c.type = 'NON_SERIALIZED'
240
 	and i.currentWarehouseId not in (1,2,6,9)  
6762 amar.kumar 241
 	group by itemId, s.type
6880 amar.kumar 242
 </select>
6630 amar.kumar 243
 
4500 mandeep.dh 244
</mapper>