Subversion Repositories SmartDukaan

Rev

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