Subversion Repositories SmartDukaan

Rev

Rev 7210 | Rev 7224 | 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
 
7199 amar.kumar 34
  <select id="getScansForOrderAndItem" parameterType="map" resultType="scan">
35
    SELECT  s.id as id, s.inventoryItemId as inventoryItemId, s.warehouseId as warehouseId, s.type as type, 
36
    	s.scannedAt as scannedAt, s.quantity as quantity, s.orderId as orderId
37
    FROM scanNew s
38
    JOIN inventoryItem i
39
    	ON (s.inventoryItemId = i.id)
40
    WHERE i.itemId = #{itemId}
41
    AND s.orderId = #{orderId}
42
    ORDER BY scannedAt
43
  </select>  
44
 
45
 
5372 mandeep.dh 46
  <select id="getPurchaseScans" parameterType="map" resultType="detailedPurchaseScan">
5395 mandeep.dh 47
    SELECT p.purchaseOrder_id AS purchaseOrderId, 1000 * UNIX_TIMESTAMP(po.createdAt) AS poCreatedAt, s.name AS supplierName,
6494 amar.kumar 48
        GROUP_CONCAT(DISTINCT(p.invoiceNumber)) AS invoiceNumbers, GROUP_CONCAT(DISTINCT(inv.receivedFrom)) AS receivedBy, i.itemId,
6361 rajveer 49
        brand, modelName, modelNumber, color, unitPrice, SUM(initialQuantity) AS quantity, 1000 * UNIX_TIMESTAMP(receivedOn) as purchasedAt, p.id as purchaseId 
5372 mandeep.dh 50
    FROM purchase p
51
    JOIN purchaseorder po 
52
        ON (po.id = p.purchaseOrder_id)
53
    JOIN inventoryItem i
54
        ON (p.id = i.purchaseId)
55
    JOIN lineitem l
56
        ON (l.itemId = i.itemId AND l.purchaseOrder_id = po.id)
57
    JOIN supplier s
58
        ON (s.id = po.supplierId)
6494 amar.kumar 59
    JOIN invoice inv
60
    	ON (inv.invoiceNumber = p.invoiceNumber)
5372 mandeep.dh 61
    WHERE po.createdAt BETWEEN #{startDate} AND #{endDate} 
6364 rajveer 62
    AND lastScanType != 'DOA_REPLACED' 
6362 rajveer 63
    GROUP BY p.purchaseOrder_id, p.id, i.itemId
64
    ORDER BY p.purchaseOrder_id, p.id, i.itemId
5372 mandeep.dh 65
  </select>
5496 mandeep.dh 66
 
7210 amar.kumar 67
  <select id="getPurchaseScansByGrnDate" parameterType="map" resultType="detailedPurchaseScan">
68
    SELECT p.purchaseOrder_id AS purchaseOrderId, 1000 * UNIX_TIMESTAMP(po.createdAt) AS poCreatedAt, s.name AS supplierName,
69
        p.invoiceNumber AS invoiceNumbers, inv.receivedFrom AS receivedBy, i.itemId,brand, modelName, modelNumber, color, 
70
        unitPrice, SUM(initialQuantity) AS quantity, 1000 * UNIX_TIMESTAMP(receivedOn) as purchasedAt, p.id as purchaseId 
71
    FROM purchase p
72
    JOIN purchaseorder po 
73
        ON (po.id = p.purchaseOrder_id)
74
    JOIN inventoryItem i
75
        ON (p.id = i.purchaseId)
76
    JOIN lineitem l
77
        ON (l.itemId = i.itemId AND l.purchaseOrder_id = po.id)
78
    JOIN supplier s
79
        ON (s.id = po.supplierId)
80
    JOIN invoice inv
81
    	ON (inv.invoiceNumber = p.invoiceNumber)
7216 amar.kumar 82
    WHERE p.receivedOn BETWEEN #{startDate} AND #{endDate}
83
    AND inv.date  &lt; #{endDate}
7210 amar.kumar 84
    AND lastScanType != 'DOA_REPLACED' 
85
    GROUP BY p.id, i.itemId
86
    ORDER BY p.id, i.itemId
87
  </select>
88
 
5496 mandeep.dh 89
  <select id="fetchMismatchScansPerInvoiceNumber" parameterType="java.util.Date" resultType="invoiceScan" >
90
    SELECT i.invoiceNumber, i.numItems, sum(initialQuantity) as scannedQuantity, i.date, s.name as supplierName
91
    FROM invoice i
92
    JOIN supplier s
7090 amar.kumar 93
    	ON (i.supplierId = s.id)
5496 mandeep.dh 94
    LEFT JOIN purchase p
95
        ON (p.invoiceNumber = i.invoiceNumber)
7089 amar.kumar 96
    LEFT JOIN inventoryItem ii 
97
    	ON (ii.purchaseId = p.id)
98
    JOIN scanNew sn
99
    	ON (sn.inventoryItemId = ii.id)
5496 mandeep.dh 100
    WHERE i.date = #{date}
7089 amar.kumar 101
    AND	sn.scannedAt between #{date} and ADDDATE(#{date},1)
102
    AND sn.type = 'PURCHASE'
103
    AND s.id !=1
5496 mandeep.dh 104
    GROUP BY i.invoiceNumber, i.numItems
105
    HAVING SUM(initialQuantity) != i.numItems
106
  </select>
5372 mandeep.dh 107
 
5711 mandeep.dh 108
  <select id="getInventoryAge" resultType="inventoryAge">
109
    SELECT t.itemId, brand, modelName, modelNumber, color,
110
        SUM((age &lt; 1) * currentQuantity) AS freshCount, 
5768 mandeep.dh 111
        SUM((age &gt;= 1 and age &lt; 2) * currentQuantity) AS oneToTwoCount, 
112
        SUM((age &gt;= 2 and age &lt; 3) * currentQuantity) AS twoToThreeCount, 
113
        SUM((age &gt;= 3 and age &lt; 4) * currentQuantity) AS threeToFourCount, 
114
        SUM((age &gt;= 4) * currentQuantity) as fourPlusCount,
115
        SUM((age &gt;= 1) * currentQuantity) as onePlusCount,
116
        SUM((age &gt;= 1) * unitPrice) as onePlusCost,
117
        SUM(currentQuantity) as zeroPlusCount,
118
        SUM(l.unitPrice) as zeroPlusCost,
119
        l.productGroup as category
5711 mandeep.dh 120
    FROM  (SELECT ii.id, ii.itemId, ii.currentQuantity, ii.purchaseId,
121
                DATEDIFF(now(), MAX(s.scannedAt)) / 7 as age
122
           FROM inventoryItem ii 
123
           JOIN scanNew s 
124
                ON (s.inventoryItemId = ii.id) 
125
           WHERE ii.lastScanType IN ('PURCHASE', 'SALE_RET') 
126
           GROUP BY ii.id, ii.itemId, ii.currentQuantity) t 
127
    JOIN purchase p 
128
        ON (p.id = t.purchaseId) 
129
    JOIN lineitem l 
130
        ON (l.purchaseOrder_id = p.purchaseOrder_id AND l.itemId = t.itemId)
131
    GROUP BY t.itemId 
132
    HAVING SUM(currentQuantity) != 0
133
  </select>
6322 amar.kumar 134
 
7089 amar.kumar 135
  <!--select id="inventoryAgeOnePlus" resultType="detailedInventoryAge">
136
    SELECT t.itemId, brand, modelName, modelNumber, color, scannedAt, i.warehouseId
137
        SUM((age &gt;= 1) * currentQuantity) AS onePlusCount, 
138
        SUM((age &gt;= 1) * unitPrice) as onePlusCost,
139
        l.productGroup as category
140
    FROM  (SELECT ii.id, ii.itemId, ii.currentQuantity, ii.purchaseId, MIN(s.scannedAt)
141
                DATEDIFF(now(), MAX(s.scannedAt)) / 7 as age
142
           FROM inventoryItem ii 
143
           JOIN scanNew s 
144
                ON (s.inventoryItemId = ii.id) 
145
           WHERE ii.lastScanType IN ('PURCHASE', 'SALE_RET') 
146
           GROUP BY ii.id, ii.itemId, ii.currentQuantity) t 
147
    JOIN purchase p 
148
        ON (p.id = t.purchaseId) 
149
    JOIN lineitem l 
150
        ON (l.purchaseOrder_id = p.purchaseOrder_id AND l.itemId = t.itemId)
151
    GROUP BY t.itemId, i.warehouseId 
152
    HAVING SUM(currentQuantity) != 0
153
  </select-->
154
 
155
 
156
 
6322 amar.kumar 157
  <select id = "getScansForItem" parameterType = "map" resultType = "scan">
158
	  SELECT s.id, s.inventoryItemId, s.warehouseId, s.type, s.scannedAt, 
159
	  s.quantity, s.orderId from scanNew s JOIN inventoryItem i 
160
	  ON 
161
	  s.inventoryItemId = i.id 
162
	  AND
6414 amar.kumar 163
	  i.itemId = #{itemId} 
6322 amar.kumar 164
	  AND
165
	  s.scannedAt BETWEEN #{fromDate} AND #{toDate};
166
  </select>
167
 
6558 amar.kumar 168
  <insert id="genericScan" parameterType="scan" useGeneratedKeys="true" keyProperty="id">
6467 amar.kumar 169
    INSERT INTO scanNew
170
      (inventoryItemId, warehouseId, type, scannedAt, quantity)
171
    VALUES
172
      (#{inventoryItemId}, #{warehouseId}, #{type}, NOW(), #{quantity})
173
  </insert>
174
 
6558 amar.kumar 175
  <select id = "getCurrentSerializedInventoryByScans" resultType = "inventoryAvailability">
176
	SELECT x.itemId, ci.brand, ci.model_name as modelName, 
6630 amar.kumar 177
	ci.model_number as modelNumber, ci.color as color, (x.inx-if(y.outx is null,0,y.outx)) as quantity
6558 amar.kumar 178
	from 
179
	(select i.itemId, count(*) as inx from scanNew s 
180
		JOIN inventoryItem i ON (i.id = s.inventoryItemId) 
181
		JOIN catalog.item c ON i.itemId = c.id 
182
		where s.type in ('PURCHASE','SALE_RET') and c.type = 'SERIALIZED' 
183
		group by i.itemId ) as x 
6630 amar.kumar 184
	LEFT JOIN 
6558 amar.kumar 185
	(select i.itemId, count(*) as outx from scanNew s 
186
		JOIN inventoryItem i on i.id = s.inventoryItemId 
187
		JOIN catalog.item c ON i.itemId = c.id 
188
		where s.type in ('SALE','LOST_IN_WAREHOUSE','PURCHASE_RETURN') and c.type = 'SERIALIZED' 
189
		group by i.itemId) as y 
190
	ON x.itemId = y.itemId 
191
	JOIN catalog.item ci ON ci.id = x.itemId 
6630 amar.kumar 192
	where ci.type = 'SERIALIZED' having quantity>0  
6558 amar.kumar 193
  </select>
6762 amar.kumar 194
 
195
<select id = "getHistoricSerializedInventoryByScans" resultType = "inventoryAvailability" parameterType = "date">
196
	SELECT x.itemId, ci.brand, ci.model_name as modelName, 
197
	ci.model_number as modelNumber, ci.color as color, (x.inx-if(y.outx is null,0,y.outx)) as quantity
198
	from 
199
	(select i.itemId, count(*) as inx from scanNew s 
200
		JOIN inventoryItem i ON (i.id = s.inventoryItemId) 
201
		JOIN catalog.item c ON i.itemId = c.id 
202
		where s.type in ('PURCHASE','SALE_RET') and c.type = 'SERIALIZED'
203
		and s.scannedAt &lt; #{date} 
204
		group by i.itemId ) as x 
205
	LEFT JOIN 
206
	(select i.itemId, count(*) as outx from scanNew s 
207
		JOIN inventoryItem i on i.id = s.inventoryItemId 
208
		JOIN catalog.item c ON i.itemId = c.id 
209
		where s.type in ('SALE','LOST_IN_WAREHOUSE','PURCHASE_RETURN') and c.type = 'SERIALIZED'
210
		and s.scannedAt &lt; #{date} 
211
		group by i.itemId) as y 
212
	ON x.itemId = y.itemId 
213
	JOIN catalog.item ci ON ci.id = x.itemId 
214
	where ci.type = 'SERIALIZED' having quantity>0  
215
  </select>
216
 
6558 amar.kumar 217
 
6630 amar.kumar 218
  <select id = "getCurrentNonSerializedInventoryByScans" resultType = "inventoryAvailability">
219
	SELECT x.itemId, ci.brand, ci.model_name as modelName, 
220
	ci.model_number as modelNumber, ci.color as color, (x.inx-if(y.outx is null,0,y.outx)) as quantity
221
	from 
222
	(select i.itemId, sum(quantity) as inx from scanNew s 
223
		JOIN inventoryItem i ON (i.id = s.inventoryItemId) 
224
		JOIN catalog.item c ON i.itemId = c.id 
225
		where s.type in ('PURCHASE','SALE_RET') and c.type = 'NON_SERIALIZED' 
226
		group by i.itemId ) as x 
227
	LEFT JOIN 
228
	(select i.itemId, sum(quantity) as outx from scanNew s 
229
		JOIN inventoryItem i on i.id = s.inventoryItemId 
230
		JOIN catalog.item c ON i.itemId = c.id 
231
		where s.type in ('SALE','LOST_IN_WAREHOUSE','PURCHASE_RETURN') and c.type = 'NON_SERIALIZED' 
232
		group by i.itemId) as y 
233
	ON x.itemId = y.itemId 
234
	JOIN catalog.item ci ON ci.id = x.itemId 
235
	where ci.type = 'NON_SERIALIZED' having quantity>0  
236
  </select>
6762 amar.kumar 237
 
238
<select id = "getHistoricNonSerializedInventoryByScans" resultType = "inventoryAvailability" parameterType = "date">
239
	SELECT x.itemId, ci.brand, ci.model_name as modelName, 
240
	ci.model_number as modelNumber, ci.color as color, (x.inx-if(y.outx is null,0,y.outx)) as quantity
241
	from 
242
	(select i.itemId, sum(quantity) as inx from scanNew s 
243
		JOIN inventoryItem i ON (i.id = s.inventoryItemId) 
244
		JOIN catalog.item c ON i.itemId = c.id 
245
		where s.type in ('PURCHASE','SALE_RET') and c.type = 'NON_SERIALIZED' 
246
		and s.scannedAt &lt; #{date}
247
		group by i.itemId ) as x 
248
	LEFT JOIN 
249
	(select i.itemId, sum(quantity) as outx from scanNew s 
250
		JOIN inventoryItem i on i.id = s.inventoryItemId 
251
		JOIN catalog.item c ON i.itemId = c.id 
252
		where s.type in ('SALE','LOST_IN_WAREHOUSE','PURCHASE_RETURN') and c.type = 'NON_SERIALIZED'
253
		and s.scannedAt &lt; #{date} 
254
		group by i.itemId) as y 
255
	ON x.itemId = y.itemId 
256
	JOIN catalog.item ci ON ci.id = x.itemId 
257
	where ci.type = 'NON_SERIALIZED' having quantity>0  
258
  </select>
259
 
6880 amar.kumar 260
 <select id = "getMovementSerializedInventoryByScans" resultType = "inventoryMovement" parameterType = "map">
6762 amar.kumar 261
 	select itemId, brand, model_name as modelName, model_number as modelNumber, color, s.type, count(*) as quantity 
262
 	FROM inventoryItem i JOIN scanNew s ON i.id = s.inventoryItemId 
263
 	LEFT JOIN catalog.item c ON i.itemId = c.id 
6963 amar.kumar 264
 	where s.scannedAt between #{startDate} and #{endDate} and c.type = 'SERIALIZED'
265
 	and i.currentWarehouseId not in (1,2,6,9) 
6762 amar.kumar 266
 	group by itemId, s.type
267
 </select>
268
 
269
 <select id = "getMovementNonSerializedInventoryByScans" resultType = "inventoryMovement" parameterType = "map">
6880 amar.kumar 270
 	select itemId, brand, model_name as modelName, model_number as modelNumber, color, s.type, sum(quantity) as quantity 
6762 amar.kumar 271
 	FROM inventoryItem i JOIN scanNew s ON i.id = s.inventoryItemId 
272
 	LEFT JOIN catalog.item c ON i.itemId = c.id 
6963 amar.kumar 273
 	where s.scannedAt between #{startDate} and #{endDate} and c.type = 'NON_SERIALIZED'
274
 	and i.currentWarehouseId not in (1,2,6,9)  
6762 amar.kumar 275
 	group by itemId, s.type
6880 amar.kumar 276
 </select>
7216 amar.kumar 277
 
278
 <select id = "getCompleteMovementSerializedInventoryByScans" resultType = "inventoryMovement" parameterType = "map">
279
 	select itemId, brand, model_name as modelName, model_number as modelNumber, color, s.type, count(*) as quantity 
280
 	FROM inventoryItem i JOIN scanNew s ON i.id = s.inventoryItemId 
281
 	LEFT JOIN catalog.item c ON i.itemId = c.id 
282
 	where s.scannedAt between #{startDate} and #{endDate} and c.type = 'SERIALIZED'
283
 	group by itemId, s.type
284
 </select>
285
 
286
 <select id = "getCompleteMovementNonSerializedInventoryByScans" resultType = "inventoryMovement" parameterType = "map">
287
 	select itemId, brand, model_name as modelName, model_number as modelNumber, color, s.type, sum(quantity) as quantity 
288
 	FROM inventoryItem i JOIN scanNew s ON i.id = s.inventoryItemId 
289
 	LEFT JOIN catalog.item c ON i.itemId = c.id 
290
 	where s.scannedAt between #{startDate} and #{endDate} and c.type = 'NON_SERIALIZED'
291
 	group by itemId, s.type
292
 </select>
6630 amar.kumar 293
 
4500 mandeep.dh 294
</mapper>