Subversion Repositories SmartDukaan

Rev

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